1 //===- Writer.cpp ---------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "Writer.h" 10 #include "Config.h" 11 #include "InputChunks.h" 12 #include "InputElement.h" 13 #include "MapFile.h" 14 #include "OutputSections.h" 15 #include "OutputSegment.h" 16 #include "Relocations.h" 17 #include "SymbolTable.h" 18 #include "SyntheticSections.h" 19 #include "WriterUtils.h" 20 #include "lld/Common/ErrorHandler.h" 21 #include "lld/Common/Memory.h" 22 #include "lld/Common/Strings.h" 23 #include "llvm/ADT/DenseSet.h" 24 #include "llvm/ADT/SmallSet.h" 25 #include "llvm/ADT/SmallVector.h" 26 #include "llvm/ADT/StringMap.h" 27 #include "llvm/BinaryFormat/Wasm.h" 28 #include "llvm/BinaryFormat/WasmTraits.h" 29 #include "llvm/Support/FileOutputBuffer.h" 30 #include "llvm/Support/Format.h" 31 #include "llvm/Support/FormatVariadic.h" 32 #include "llvm/Support/LEB128.h" 33 #include "llvm/Support/Parallel.h" 34 35 #include <cstdarg> 36 #include <map> 37 38 #define DEBUG_TYPE "lld" 39 40 using namespace llvm; 41 using namespace llvm::wasm; 42 43 namespace lld { 44 namespace wasm { 45 static constexpr int stackAlignment = 16; 46 static constexpr int heapAlignment = 16; 47 48 namespace { 49 50 // The writer writes a SymbolTable result to a file. 51 class Writer { 52 public: 53 void run(); 54 55 private: 56 void openFile(); 57 58 bool needsPassiveInitialization(const OutputSegment *segment); 59 bool hasPassiveInitializedSegments(); 60 61 void createSyntheticInitFunctions(); 62 void createInitMemoryFunction(); 63 void createStartFunction(); 64 void createApplyDataRelocationsFunction(); 65 void createApplyGlobalRelocationsFunction(); 66 void createApplyGlobalTLSRelocationsFunction(); 67 void createCallCtorsFunction(); 68 void createInitTLSFunction(); 69 void createCommandExportWrappers(); 70 void createCommandExportWrapper(uint32_t functionIndex, DefinedFunction *f); 71 72 void assignIndexes(); 73 void populateSymtab(); 74 void populateProducers(); 75 void populateTargetFeatures(); 76 void calculateInitFunctions(); 77 void calculateImports(); 78 void calculateExports(); 79 void calculateCustomSections(); 80 void calculateTypes(); 81 void createOutputSegments(); 82 OutputSegment *createOutputSegment(StringRef name); 83 void combineOutputSegments(); 84 void layoutMemory(); 85 void createHeader(); 86 87 void addSection(OutputSection *sec); 88 89 void addSections(); 90 91 void createCustomSections(); 92 void createSyntheticSections(); 93 void createSyntheticSectionsPostLayout(); 94 void finalizeSections(); 95 96 // Custom sections 97 void createRelocSections(); 98 99 void writeHeader(); 100 void writeSections(); 101 102 uint64_t fileSize = 0; 103 104 std::vector<WasmInitEntry> initFunctions; 105 llvm::StringMap<std::vector<InputChunk *>> customSectionMapping; 106 107 // Stable storage for command export wrapper function name strings. 108 std::list<std::string> commandExportWrapperNames; 109 110 // Elements that are used to construct the final output 111 std::string header; 112 std::vector<OutputSection *> outputSections; 113 114 std::unique_ptr<FileOutputBuffer> buffer; 115 116 std::vector<OutputSegment *> segments; 117 llvm::SmallDenseMap<StringRef, OutputSegment *> segmentMap; 118 }; 119 120 } // anonymous namespace 121 122 void Writer::calculateCustomSections() { 123 log("calculateCustomSections"); 124 bool stripDebug = config->stripDebug || config->stripAll; 125 for (ObjFile *file : symtab->objectFiles) { 126 for (InputChunk *section : file->customSections) { 127 // Exclude COMDAT sections that are not selected for inclusion 128 if (section->discarded) 129 continue; 130 StringRef name = section->getName(); 131 // These custom sections are known the linker and synthesized rather than 132 // blindly copied. 133 if (name == "linking" || name == "name" || name == "producers" || 134 name == "target_features" || name.startswith("reloc.")) 135 continue; 136 // These custom sections are generated by `clang -fembed-bitcode`. 137 // These are used by the rust toolchain to ship LTO data along with 138 // compiled object code, but they don't want this included in the linker 139 // output. 140 if (name == ".llvmbc" || name == ".llvmcmd") 141 continue; 142 // Strip debug section in that option was specified. 143 if (stripDebug && name.startswith(".debug_")) 144 continue; 145 // Otherwise include custom sections by default and concatenate their 146 // contents. 147 customSectionMapping[name].push_back(section); 148 } 149 } 150 } 151 152 void Writer::createCustomSections() { 153 log("createCustomSections"); 154 for (auto &pair : customSectionMapping) { 155 StringRef name = pair.first(); 156 LLVM_DEBUG(dbgs() << "createCustomSection: " << name << "\n"); 157 158 OutputSection *sec = make<CustomSection>(std::string(name), pair.second); 159 if (config->relocatable || config->emitRelocs) { 160 auto *sym = make<OutputSectionSymbol>(sec); 161 out.linkingSec->addToSymtab(sym); 162 sec->sectionSym = sym; 163 } 164 addSection(sec); 165 } 166 } 167 168 // Create relocations sections in the final output. 169 // These are only created when relocatable output is requested. 170 void Writer::createRelocSections() { 171 log("createRelocSections"); 172 // Don't use iterator here since we are adding to OutputSection 173 size_t origSize = outputSections.size(); 174 for (size_t i = 0; i < origSize; i++) { 175 LLVM_DEBUG(dbgs() << "check section " << i << "\n"); 176 OutputSection *sec = outputSections[i]; 177 178 // Count the number of needed sections. 179 uint32_t count = sec->getNumRelocations(); 180 if (!count) 181 continue; 182 183 StringRef name; 184 if (sec->type == WASM_SEC_DATA) 185 name = "reloc.DATA"; 186 else if (sec->type == WASM_SEC_CODE) 187 name = "reloc.CODE"; 188 else if (sec->type == WASM_SEC_CUSTOM) 189 name = saver.save("reloc." + sec->name); 190 else 191 llvm_unreachable( 192 "relocations only supported for code, data, or custom sections"); 193 194 addSection(make<RelocSection>(name, sec)); 195 } 196 } 197 198 void Writer::populateProducers() { 199 for (ObjFile *file : symtab->objectFiles) { 200 const WasmProducerInfo &info = file->getWasmObj()->getProducerInfo(); 201 out.producersSec->addInfo(info); 202 } 203 } 204 205 void Writer::writeHeader() { 206 memcpy(buffer->getBufferStart(), header.data(), header.size()); 207 } 208 209 void Writer::writeSections() { 210 uint8_t *buf = buffer->getBufferStart(); 211 parallelForEach(outputSections, [buf](OutputSection *s) { 212 assert(s->isNeeded()); 213 s->writeTo(buf); 214 }); 215 } 216 217 static void setGlobalPtr(DefinedGlobal *g, uint64_t memoryPtr) { 218 g->global->setPointerValue(memoryPtr); 219 } 220 221 // Fix the memory layout of the output binary. This assigns memory offsets 222 // to each of the input data sections as well as the explicit stack region. 223 // The default memory layout is as follows, from low to high. 224 // 225 // - initialized data (starting at Config->globalBase) 226 // - BSS data (not currently implemented in llvm) 227 // - explicit stack (Config->ZStackSize) 228 // - heap start / unallocated 229 // 230 // The --stack-first option means that stack is placed before any static data. 231 // This can be useful since it means that stack overflow traps immediately 232 // rather than overwriting global data, but also increases code size since all 233 // static data loads and stores requires larger offsets. 234 void Writer::layoutMemory() { 235 uint64_t memoryPtr = 0; 236 237 auto placeStack = [&]() { 238 if (config->relocatable || config->isPic) 239 return; 240 memoryPtr = alignTo(memoryPtr, stackAlignment); 241 if (config->zStackSize != alignTo(config->zStackSize, stackAlignment)) 242 error("stack size must be " + Twine(stackAlignment) + "-byte aligned"); 243 log("mem: stack size = " + Twine(config->zStackSize)); 244 log("mem: stack base = " + Twine(memoryPtr)); 245 memoryPtr += config->zStackSize; 246 setGlobalPtr(cast<DefinedGlobal>(WasmSym::stackPointer), memoryPtr); 247 log("mem: stack top = " + Twine(memoryPtr)); 248 }; 249 250 if (config->stackFirst) { 251 placeStack(); 252 } else { 253 memoryPtr = config->globalBase; 254 log("mem: global base = " + Twine(config->globalBase)); 255 } 256 257 if (WasmSym::globalBase) 258 WasmSym::globalBase->setVA(memoryPtr); 259 260 uint64_t dataStart = memoryPtr; 261 262 // Arbitrarily set __dso_handle handle to point to the start of the data 263 // segments. 264 if (WasmSym::dsoHandle) 265 WasmSym::dsoHandle->setVA(dataStart); 266 267 out.dylinkSec->memAlign = 0; 268 for (OutputSegment *seg : segments) { 269 out.dylinkSec->memAlign = std::max(out.dylinkSec->memAlign, seg->alignment); 270 memoryPtr = alignTo(memoryPtr, 1ULL << seg->alignment); 271 seg->startVA = memoryPtr; 272 log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}", seg->name, 273 memoryPtr, seg->size, seg->alignment)); 274 275 if (!config->relocatable && seg->isTLS()) { 276 if (config->sharedMemory) { 277 auto *tlsSize = cast<DefinedGlobal>(WasmSym::tlsSize); 278 setGlobalPtr(tlsSize, seg->size); 279 280 auto *tlsAlign = cast<DefinedGlobal>(WasmSym::tlsAlign); 281 setGlobalPtr(tlsAlign, int64_t{1} << seg->alignment); 282 } else if (WasmSym::tlsBase) { 283 auto *tlsBase = cast<DefinedGlobal>(WasmSym::tlsBase); 284 setGlobalPtr(tlsBase, memoryPtr); 285 } 286 } 287 288 memoryPtr += seg->size; 289 } 290 291 // Make space for the memory initialization flag 292 if (config->sharedMemory && hasPassiveInitializedSegments()) { 293 memoryPtr = alignTo(memoryPtr, 4); 294 WasmSym::initMemoryFlag = symtab->addSyntheticDataSymbol( 295 "__wasm_init_memory_flag", WASM_SYMBOL_VISIBILITY_HIDDEN); 296 WasmSym::initMemoryFlag->markLive(); 297 WasmSym::initMemoryFlag->setVA(memoryPtr); 298 log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}", 299 "__wasm_init_memory_flag", memoryPtr, 4, 4)); 300 memoryPtr += 4; 301 } 302 303 if (WasmSym::dataEnd) 304 WasmSym::dataEnd->setVA(memoryPtr); 305 306 uint64_t staticDataSize = memoryPtr - dataStart; 307 log("mem: static data = " + Twine(staticDataSize)); 308 if (config->isPic) 309 out.dylinkSec->memSize = staticDataSize; 310 311 if (!config->stackFirst) 312 placeStack(); 313 314 if (WasmSym::heapBase) { 315 // Set `__heap_base` to follow the end of the stack or global data. The 316 // fact that this comes last means that a malloc/brk implementation can 317 // grow the heap at runtime. 318 // We'll align the heap base here because memory allocators might expect 319 // __heap_base to be aligned already. 320 memoryPtr = alignTo(memoryPtr, heapAlignment); 321 log("mem: heap base = " + Twine(memoryPtr)); 322 WasmSym::heapBase->setVA(memoryPtr); 323 } 324 325 uint64_t maxMemorySetting = 1ULL 326 << (config->is64.getValueOr(false) ? 48 : 32); 327 328 if (config->initialMemory != 0) { 329 if (config->initialMemory != alignTo(config->initialMemory, WasmPageSize)) 330 error("initial memory must be " + Twine(WasmPageSize) + "-byte aligned"); 331 if (memoryPtr > config->initialMemory) 332 error("initial memory too small, " + Twine(memoryPtr) + " bytes needed"); 333 if (config->initialMemory > maxMemorySetting) 334 error("initial memory too large, cannot be greater than " + 335 Twine(maxMemorySetting)); 336 memoryPtr = config->initialMemory; 337 } 338 out.memorySec->numMemoryPages = 339 alignTo(memoryPtr, WasmPageSize) / WasmPageSize; 340 log("mem: total pages = " + Twine(out.memorySec->numMemoryPages)); 341 342 if (config->maxMemory != 0) { 343 if (config->maxMemory != alignTo(config->maxMemory, WasmPageSize)) 344 error("maximum memory must be " + Twine(WasmPageSize) + "-byte aligned"); 345 if (memoryPtr > config->maxMemory) 346 error("maximum memory too small, " + Twine(memoryPtr) + " bytes needed"); 347 if (config->maxMemory > maxMemorySetting) 348 error("maximum memory too large, cannot be greater than " + 349 Twine(maxMemorySetting)); 350 } 351 352 // Check max if explicitly supplied or required by shared memory 353 if (config->maxMemory != 0 || config->sharedMemory) { 354 uint64_t max = config->maxMemory; 355 if (max == 0) { 356 // If no maxMemory config was supplied but we are building with 357 // shared memory, we need to pick a sensible upper limit. 358 if (config->isPic) 359 max = maxMemorySetting; 360 else 361 max = alignTo(memoryPtr, WasmPageSize); 362 } 363 out.memorySec->maxMemoryPages = max / WasmPageSize; 364 log("mem: max pages = " + Twine(out.memorySec->maxMemoryPages)); 365 } 366 } 367 368 void Writer::addSection(OutputSection *sec) { 369 if (!sec->isNeeded()) 370 return; 371 log("addSection: " + toString(*sec)); 372 sec->sectionIndex = outputSections.size(); 373 outputSections.push_back(sec); 374 } 375 376 // If a section name is valid as a C identifier (which is rare because of 377 // the leading '.'), linkers are expected to define __start_<secname> and 378 // __stop_<secname> symbols. They are at beginning and end of the section, 379 // respectively. This is not requested by the ELF standard, but GNU ld and 380 // gold provide the feature, and used by many programs. 381 static void addStartStopSymbols(const OutputSegment *seg) { 382 StringRef name = seg->name; 383 if (!isValidCIdentifier(name)) 384 return; 385 LLVM_DEBUG(dbgs() << "addStartStopSymbols: " << name << "\n"); 386 uint64_t start = seg->startVA; 387 uint64_t stop = start + seg->size; 388 symtab->addOptionalDataSymbol(saver.save("__start_" + name), start); 389 symtab->addOptionalDataSymbol(saver.save("__stop_" + name), stop); 390 } 391 392 void Writer::addSections() { 393 addSection(out.dylinkSec); 394 addSection(out.typeSec); 395 addSection(out.importSec); 396 addSection(out.functionSec); 397 addSection(out.tableSec); 398 addSection(out.memorySec); 399 addSection(out.tagSec); 400 addSection(out.globalSec); 401 addSection(out.exportSec); 402 addSection(out.startSec); 403 addSection(out.elemSec); 404 addSection(out.dataCountSec); 405 406 addSection(make<CodeSection>(out.functionSec->inputFunctions)); 407 addSection(make<DataSection>(segments)); 408 409 createCustomSections(); 410 411 addSection(out.linkingSec); 412 if (config->emitRelocs || config->relocatable) { 413 createRelocSections(); 414 } 415 416 addSection(out.nameSec); 417 addSection(out.producersSec); 418 addSection(out.targetFeaturesSec); 419 } 420 421 void Writer::finalizeSections() { 422 for (OutputSection *s : outputSections) { 423 s->setOffset(fileSize); 424 s->finalizeContents(); 425 fileSize += s->getSize(); 426 } 427 } 428 429 void Writer::populateTargetFeatures() { 430 StringMap<std::string> used; 431 StringMap<std::string> required; 432 StringMap<std::string> disallowed; 433 SmallSet<std::string, 8> &allowed = out.targetFeaturesSec->features; 434 bool tlsUsed = false; 435 436 // Only infer used features if user did not specify features 437 bool inferFeatures = !config->features.hasValue(); 438 439 if (!inferFeatures) { 440 auto &explicitFeatures = config->features.getValue(); 441 allowed.insert(explicitFeatures.begin(), explicitFeatures.end()); 442 if (!config->checkFeatures) 443 return; 444 } 445 446 // Find the sets of used, required, and disallowed features 447 for (ObjFile *file : symtab->objectFiles) { 448 StringRef fileName(file->getName()); 449 for (auto &feature : file->getWasmObj()->getTargetFeatures()) { 450 switch (feature.Prefix) { 451 case WASM_FEATURE_PREFIX_USED: 452 used.insert({feature.Name, std::string(fileName)}); 453 break; 454 case WASM_FEATURE_PREFIX_REQUIRED: 455 used.insert({feature.Name, std::string(fileName)}); 456 required.insert({feature.Name, std::string(fileName)}); 457 break; 458 case WASM_FEATURE_PREFIX_DISALLOWED: 459 disallowed.insert({feature.Name, std::string(fileName)}); 460 break; 461 default: 462 error("Unrecognized feature policy prefix " + 463 std::to_string(feature.Prefix)); 464 } 465 } 466 467 // Find TLS data segments 468 auto isTLS = [](InputChunk *segment) { 469 return segment->live && segment->isTLS(); 470 }; 471 tlsUsed = tlsUsed || llvm::any_of(file->segments, isTLS); 472 } 473 474 if (inferFeatures) 475 for (const auto &key : used.keys()) 476 allowed.insert(std::string(key)); 477 478 if (!config->checkFeatures) 479 return; 480 481 if (!config->relocatable && allowed.count("mutable-globals") == 0) { 482 for (const Symbol *sym : out.importSec->importedSymbols) { 483 if (auto *global = dyn_cast<GlobalSymbol>(sym)) { 484 if (global->getGlobalType()->Mutable) { 485 error(Twine("mutable global imported but 'mutable-globals' feature " 486 "not present in inputs: `") + 487 toString(*sym) + "`. Use --no-check-features to suppress."); 488 } 489 } 490 } 491 for (const Symbol *sym : out.exportSec->exportedSymbols) { 492 if (isa<GlobalSymbol>(sym)) { 493 error(Twine("mutable global exported but 'mutable-globals' feature " 494 "not present in inputs: `") + 495 toString(*sym) + "`. Use --no-check-features to suppress."); 496 } 497 } 498 } 499 500 if (config->sharedMemory) { 501 if (disallowed.count("shared-mem")) 502 error("--shared-memory is disallowed by " + disallowed["shared-mem"] + 503 " because it was not compiled with 'atomics' or 'bulk-memory' " 504 "features."); 505 506 for (auto feature : {"atomics", "bulk-memory"}) 507 if (!allowed.count(feature)) 508 error(StringRef("'") + feature + 509 "' feature must be used in order to use shared memory"); 510 } 511 512 if (tlsUsed) { 513 for (auto feature : {"atomics", "bulk-memory"}) 514 if (!allowed.count(feature)) 515 error(StringRef("'") + feature + 516 "' feature must be used in order to use thread-local storage"); 517 } 518 519 // Validate that used features are allowed in output 520 if (!inferFeatures) { 521 for (const auto &feature : used.keys()) { 522 if (!allowed.count(std::string(feature))) 523 error(Twine("Target feature '") + feature + "' used by " + 524 used[feature] + " is not allowed."); 525 } 526 } 527 528 // Validate the required and disallowed constraints for each file 529 for (ObjFile *file : symtab->objectFiles) { 530 StringRef fileName(file->getName()); 531 SmallSet<std::string, 8> objectFeatures; 532 for (const auto &feature : file->getWasmObj()->getTargetFeatures()) { 533 if (feature.Prefix == WASM_FEATURE_PREFIX_DISALLOWED) 534 continue; 535 objectFeatures.insert(feature.Name); 536 if (disallowed.count(feature.Name)) 537 error(Twine("Target feature '") + feature.Name + "' used in " + 538 fileName + " is disallowed by " + disallowed[feature.Name] + 539 ". Use --no-check-features to suppress."); 540 } 541 for (const auto &feature : required.keys()) { 542 if (!objectFeatures.count(std::string(feature))) 543 error(Twine("Missing target feature '") + feature + "' in " + fileName + 544 ", required by " + required[feature] + 545 ". Use --no-check-features to suppress."); 546 } 547 } 548 } 549 550 static bool shouldImport(Symbol *sym) { 551 // We don't generate imports for data symbols. They however can be imported 552 // as GOT entries. 553 if (isa<DataSymbol>(sym)) 554 return false; 555 if (!sym->isLive()) 556 return false; 557 if (!sym->isUsedInRegularObj) 558 return false; 559 560 // When a symbol is weakly defined in a shared library we need to allow 561 // it to be overridden by another module so need to both import 562 // and export the symbol. 563 if (config->shared && sym->isDefined() && sym->isWeak()) 564 return true; 565 if (!sym->isUndefined()) 566 return false; 567 if (sym->isWeak() && !config->relocatable && !config->isPic) 568 return false; 569 570 // In PIC mode we only need to import functions when they are called directly. 571 // Indirect usage all goes via GOT imports. 572 if (config->isPic) { 573 if (auto *f = dyn_cast<UndefinedFunction>(sym)) 574 if (!f->isCalledDirectly) 575 return false; 576 } 577 578 if (config->isPic || config->relocatable || config->importUndefined) 579 return true; 580 if (config->allowUndefinedSymbols.count(sym->getName()) != 0) 581 return true; 582 583 return sym->importName.hasValue(); 584 } 585 586 void Writer::calculateImports() { 587 // Some inputs require that the indirect function table be assigned to table 588 // number 0, so if it is present and is an import, allocate it before any 589 // other tables. 590 if (WasmSym::indirectFunctionTable && 591 shouldImport(WasmSym::indirectFunctionTable)) 592 out.importSec->addImport(WasmSym::indirectFunctionTable); 593 594 for (Symbol *sym : symtab->getSymbols()) { 595 if (!shouldImport(sym)) 596 continue; 597 if (sym == WasmSym::indirectFunctionTable) 598 continue; 599 LLVM_DEBUG(dbgs() << "import: " << sym->getName() << "\n"); 600 out.importSec->addImport(sym); 601 } 602 } 603 604 void Writer::calculateExports() { 605 if (config->relocatable) 606 return; 607 608 if (!config->relocatable && !config->importMemory) 609 out.exportSec->exports.push_back( 610 WasmExport{"memory", WASM_EXTERNAL_MEMORY, 0}); 611 612 unsigned globalIndex = 613 out.importSec->getNumImportedGlobals() + out.globalSec->numGlobals(); 614 615 for (Symbol *sym : symtab->getSymbols()) { 616 if (!sym->isExported()) 617 continue; 618 if (!sym->isLive()) 619 continue; 620 621 StringRef name = sym->getName(); 622 WasmExport export_; 623 if (auto *f = dyn_cast<DefinedFunction>(sym)) { 624 if (Optional<StringRef> exportName = f->function->getExportName()) { 625 name = *exportName; 626 } 627 export_ = {name, WASM_EXTERNAL_FUNCTION, f->getExportedFunctionIndex()}; 628 } else if (auto *g = dyn_cast<DefinedGlobal>(sym)) { 629 if (g->getGlobalType()->Mutable && !g->getFile() && !g->forceExport) { 630 // Avoid exporting mutable globals are linker synthesized (e.g. 631 // __stack_pointer or __tls_base) unless they are explicitly exported 632 // from the command line. 633 // Without this check `--export-all` would cause any program using the 634 // stack pointer to export a mutable global even if none of the input 635 // files were built with the `mutable-globals` feature. 636 continue; 637 } 638 export_ = {name, WASM_EXTERNAL_GLOBAL, g->getGlobalIndex()}; 639 } else if (auto *t = dyn_cast<DefinedTag>(sym)) { 640 export_ = {name, WASM_EXTERNAL_TAG, t->getTagIndex()}; 641 } else if (auto *d = dyn_cast<DefinedData>(sym)) { 642 out.globalSec->dataAddressGlobals.push_back(d); 643 export_ = {name, WASM_EXTERNAL_GLOBAL, globalIndex++}; 644 } else { 645 auto *t = cast<DefinedTable>(sym); 646 export_ = {name, WASM_EXTERNAL_TABLE, t->getTableNumber()}; 647 } 648 649 LLVM_DEBUG(dbgs() << "Export: " << name << "\n"); 650 out.exportSec->exports.push_back(export_); 651 out.exportSec->exportedSymbols.push_back(sym); 652 } 653 } 654 655 void Writer::populateSymtab() { 656 if (!config->relocatable && !config->emitRelocs) 657 return; 658 659 for (Symbol *sym : symtab->getSymbols()) 660 if (sym->isUsedInRegularObj && sym->isLive()) 661 out.linkingSec->addToSymtab(sym); 662 663 for (ObjFile *file : symtab->objectFiles) { 664 LLVM_DEBUG(dbgs() << "Local symtab entries: " << file->getName() << "\n"); 665 for (Symbol *sym : file->getSymbols()) 666 if (sym->isLocal() && !isa<SectionSymbol>(sym) && sym->isLive()) 667 out.linkingSec->addToSymtab(sym); 668 } 669 } 670 671 void Writer::calculateTypes() { 672 // The output type section is the union of the following sets: 673 // 1. Any signature used in the TYPE relocation 674 // 2. The signatures of all imported functions 675 // 3. The signatures of all defined functions 676 // 4. The signatures of all imported tags 677 // 5. The signatures of all defined tags 678 679 for (ObjFile *file : symtab->objectFiles) { 680 ArrayRef<WasmSignature> types = file->getWasmObj()->types(); 681 for (uint32_t i = 0; i < types.size(); i++) 682 if (file->typeIsUsed[i]) 683 file->typeMap[i] = out.typeSec->registerType(types[i]); 684 } 685 686 for (const Symbol *sym : out.importSec->importedSymbols) { 687 if (auto *f = dyn_cast<FunctionSymbol>(sym)) 688 out.typeSec->registerType(*f->signature); 689 else if (auto *t = dyn_cast<TagSymbol>(sym)) 690 out.typeSec->registerType(*t->signature); 691 } 692 693 for (const InputFunction *f : out.functionSec->inputFunctions) 694 out.typeSec->registerType(f->signature); 695 696 for (const InputTag *t : out.tagSec->inputTags) 697 out.typeSec->registerType(t->signature); 698 } 699 700 // In a command-style link, create a wrapper for each exported symbol 701 // which calls the constructors and destructors. 702 void Writer::createCommandExportWrappers() { 703 // This logic doesn't currently support Emscripten-style PIC mode. 704 assert(!config->isPic); 705 706 // If there are no ctors and there's no libc `__wasm_call_dtors` to 707 // call, don't wrap the exports. 708 if (initFunctions.empty() && WasmSym::callDtors == NULL) 709 return; 710 711 std::vector<DefinedFunction *> toWrap; 712 713 for (Symbol *sym : symtab->getSymbols()) 714 if (sym->isExported()) 715 if (auto *f = dyn_cast<DefinedFunction>(sym)) 716 toWrap.push_back(f); 717 718 for (auto *f : toWrap) { 719 auto funcNameStr = (f->getName() + ".command_export").str(); 720 commandExportWrapperNames.push_back(funcNameStr); 721 const std::string &funcName = commandExportWrapperNames.back(); 722 723 auto func = make<SyntheticFunction>(*f->getSignature(), funcName); 724 if (f->function->getExportName().hasValue()) 725 func->setExportName(f->function->getExportName()->str()); 726 else 727 func->setExportName(f->getName().str()); 728 729 DefinedFunction *def = 730 symtab->addSyntheticFunction(funcName, f->flags, func); 731 def->markLive(); 732 733 def->flags |= WASM_SYMBOL_EXPORTED; 734 def->flags &= ~WASM_SYMBOL_VISIBILITY_HIDDEN; 735 def->forceExport = f->forceExport; 736 737 f->flags |= WASM_SYMBOL_VISIBILITY_HIDDEN; 738 f->flags &= ~WASM_SYMBOL_EXPORTED; 739 f->forceExport = false; 740 741 out.functionSec->addFunction(func); 742 743 createCommandExportWrapper(f->getFunctionIndex(), def); 744 } 745 } 746 747 static void finalizeIndirectFunctionTable() { 748 if (!WasmSym::indirectFunctionTable) 749 return; 750 751 if (shouldImport(WasmSym::indirectFunctionTable) && 752 !WasmSym::indirectFunctionTable->hasTableNumber()) { 753 // Processing -Bsymbolic relocations resulted in a late requirement that the 754 // indirect function table be present, and we are running in --import-table 755 // mode. Add the table now to the imports section. Otherwise it will be 756 // added to the tables section later in assignIndexes. 757 out.importSec->addImport(WasmSym::indirectFunctionTable); 758 } 759 760 uint32_t tableSize = config->tableBase + out.elemSec->numEntries(); 761 WasmLimits limits = {0, tableSize, 0}; 762 if (WasmSym::indirectFunctionTable->isDefined() && !config->growableTable) { 763 limits.Flags |= WASM_LIMITS_FLAG_HAS_MAX; 764 limits.Maximum = limits.Minimum; 765 } 766 WasmSym::indirectFunctionTable->setLimits(limits); 767 } 768 769 static void scanRelocations() { 770 for (ObjFile *file : symtab->objectFiles) { 771 LLVM_DEBUG(dbgs() << "scanRelocations: " << file->getName() << "\n"); 772 for (InputChunk *chunk : file->functions) 773 scanRelocations(chunk); 774 for (InputChunk *chunk : file->segments) 775 scanRelocations(chunk); 776 for (auto &p : file->customSections) 777 scanRelocations(p); 778 } 779 } 780 781 void Writer::assignIndexes() { 782 // Seal the import section, since other index spaces such as function and 783 // global are effected by the number of imports. 784 out.importSec->seal(); 785 786 for (InputFunction *func : symtab->syntheticFunctions) 787 out.functionSec->addFunction(func); 788 789 for (ObjFile *file : symtab->objectFiles) { 790 LLVM_DEBUG(dbgs() << "Functions: " << file->getName() << "\n"); 791 for (InputFunction *func : file->functions) 792 out.functionSec->addFunction(func); 793 } 794 795 for (InputGlobal *global : symtab->syntheticGlobals) 796 out.globalSec->addGlobal(global); 797 798 for (ObjFile *file : symtab->objectFiles) { 799 LLVM_DEBUG(dbgs() << "Globals: " << file->getName() << "\n"); 800 for (InputGlobal *global : file->globals) 801 out.globalSec->addGlobal(global); 802 } 803 804 for (ObjFile *file : symtab->objectFiles) { 805 LLVM_DEBUG(dbgs() << "Tags: " << file->getName() << "\n"); 806 for (InputTag *tag : file->tags) 807 out.tagSec->addTag(tag); 808 } 809 810 for (ObjFile *file : symtab->objectFiles) { 811 LLVM_DEBUG(dbgs() << "Tables: " << file->getName() << "\n"); 812 for (InputTable *table : file->tables) 813 out.tableSec->addTable(table); 814 } 815 816 for (InputTable *table : symtab->syntheticTables) 817 out.tableSec->addTable(table); 818 819 out.globalSec->assignIndexes(); 820 out.tableSec->assignIndexes(); 821 } 822 823 static StringRef getOutputDataSegmentName(const InputChunk &seg) { 824 // We always merge .tbss and .tdata into a single TLS segment so all TLS 825 // symbols are be relative to single __tls_base. 826 if (seg.isTLS()) 827 return ".tdata"; 828 StringRef name = seg.getName(); 829 if (!config->mergeDataSegments) 830 return name; 831 if (name.startswith(".text.")) 832 return ".text"; 833 if (name.startswith(".data.")) 834 return ".data"; 835 if (name.startswith(".bss.")) 836 return ".bss"; 837 if (name.startswith(".rodata.")) 838 return ".rodata"; 839 return name; 840 } 841 842 OutputSegment *Writer::createOutputSegment(StringRef name) { 843 LLVM_DEBUG(dbgs() << "new segment: " << name << "\n"); 844 OutputSegment *s = make<OutputSegment>(name); 845 if (config->sharedMemory) 846 s->initFlags = WASM_DATA_SEGMENT_IS_PASSIVE; 847 // Exported memories are guaranteed to be zero-initialized, so no need 848 // to emit data segments for bss sections. 849 // TODO: consider initializing bss sections with memory.fill 850 // instructions when memory is imported and bulk-memory is available. 851 if (!config->importMemory && !config->relocatable && name.startswith(".bss")) 852 s->isBss = true; 853 segments.push_back(s); 854 return s; 855 } 856 857 void Writer::createOutputSegments() { 858 for (ObjFile *file : symtab->objectFiles) { 859 for (InputChunk *segment : file->segments) { 860 if (!segment->live) 861 continue; 862 StringRef name = getOutputDataSegmentName(*segment); 863 OutputSegment *s = nullptr; 864 // When running in relocatable mode we can't merge segments that are part 865 // of comdat groups since the ultimate linker needs to be able exclude or 866 // include them individually. 867 if (config->relocatable && !segment->getComdatName().empty()) { 868 s = createOutputSegment(name); 869 } else { 870 if (segmentMap.count(name) == 0) 871 segmentMap[name] = createOutputSegment(name); 872 s = segmentMap[name]; 873 } 874 s->addInputSegment(segment); 875 } 876 } 877 878 // Sort segments by type, placing .bss last 879 std::stable_sort(segments.begin(), segments.end(), 880 [](const OutputSegment *a, const OutputSegment *b) { 881 auto order = [](StringRef name) { 882 return StringSwitch<int>(name) 883 .StartsWith(".tdata", 0) 884 .StartsWith(".rodata", 1) 885 .StartsWith(".data", 2) 886 .StartsWith(".bss", 4) 887 .Default(3); 888 }; 889 return order(a->name) < order(b->name); 890 }); 891 892 for (size_t i = 0; i < segments.size(); ++i) 893 segments[i]->index = i; 894 895 // Merge MergeInputSections into a single MergeSyntheticSection. 896 LLVM_DEBUG(dbgs() << "-- finalize input semgments\n"); 897 for (OutputSegment *seg : segments) 898 seg->finalizeInputSegments(); 899 } 900 901 void Writer::combineOutputSegments() { 902 // With PIC code we currently only support a single active data segment since 903 // we only have a single __memory_base to use as our base address. This pass 904 // combines all data segments into a single .data segment. 905 // This restructions can be relaxed once we have extended constant 906 // expressions available: 907 // https://github.com/WebAssembly/extended-const 908 assert(config->isPic && !config->sharedMemory); 909 if (segments.size() <= 1) 910 return; 911 OutputSegment *combined = make<OutputSegment>(".data"); 912 combined->startVA = segments[0]->startVA; 913 for (OutputSegment *s : segments) { 914 bool first = true; 915 for (InputChunk *inSeg : s->inputSegments) { 916 if (first) 917 inSeg->alignment = std::max(inSeg->alignment, s->alignment); 918 first = false; 919 #ifndef NDEBUG 920 uint64_t oldVA = inSeg->getVA(); 921 #endif 922 combined->addInputSegment(inSeg); 923 #ifndef NDEBUG 924 uint64_t newVA = inSeg->getVA(); 925 LLVM_DEBUG(dbgs() << "added input segment. name=" << inSeg->getName() 926 << " oldVA=" << oldVA << " newVA=" << newVA << "\n"); 927 assert(oldVA == newVA); 928 #endif 929 } 930 } 931 932 segments = {combined}; 933 } 934 935 static void createFunction(DefinedFunction *func, StringRef bodyContent) { 936 std::string functionBody; 937 { 938 raw_string_ostream os(functionBody); 939 writeUleb128(os, bodyContent.size(), "function size"); 940 os << bodyContent; 941 } 942 ArrayRef<uint8_t> body = arrayRefFromStringRef(saver.save(functionBody)); 943 cast<SyntheticFunction>(func->function)->setBody(body); 944 } 945 946 bool Writer::needsPassiveInitialization(const OutputSegment *segment) { 947 return segment->initFlags & WASM_DATA_SEGMENT_IS_PASSIVE && 948 !segment->isTLS() && !segment->isBss; 949 } 950 951 bool Writer::hasPassiveInitializedSegments() { 952 return llvm::any_of(segments, [this](const OutputSegment *s) { 953 return this->needsPassiveInitialization(s); 954 }); 955 } 956 957 void Writer::createSyntheticInitFunctions() { 958 if (config->relocatable) 959 return; 960 961 static WasmSignature nullSignature = {{}, {}}; 962 963 // Passive segments are used to avoid memory being reinitialized on each 964 // thread's instantiation. These passive segments are initialized and 965 // dropped in __wasm_init_memory, which is registered as the start function 966 if (config->sharedMemory && hasPassiveInitializedSegments()) { 967 WasmSym::initMemory = symtab->addSyntheticFunction( 968 "__wasm_init_memory", WASM_SYMBOL_VISIBILITY_HIDDEN, 969 make<SyntheticFunction>(nullSignature, "__wasm_init_memory")); 970 WasmSym::initMemory->markLive(); 971 } 972 973 if (config->isPic) { 974 // For PIC code we create synthetic functions that apply relocations. 975 // These get called from __wasm_call_ctors before the user-level 976 // constructors. 977 WasmSym::applyDataRelocs = symtab->addSyntheticFunction( 978 "__wasm_apply_data_relocs", WASM_SYMBOL_VISIBILITY_HIDDEN, 979 make<SyntheticFunction>(nullSignature, "__wasm_apply_data_relocs")); 980 WasmSym::applyDataRelocs->markLive(); 981 982 if (out.globalSec->needsRelocations()) { 983 WasmSym::applyGlobalRelocs = symtab->addSyntheticFunction( 984 "__wasm_apply_global_relocs", WASM_SYMBOL_VISIBILITY_HIDDEN, 985 make<SyntheticFunction>(nullSignature, "__wasm_apply_global_relocs")); 986 WasmSym::applyGlobalRelocs->markLive(); 987 if (config->sharedMemory) { 988 WasmSym::applyGlobalTLSRelocs = symtab->addSyntheticFunction( 989 "__wasm_apply_global_tls_relocs", WASM_SYMBOL_VISIBILITY_HIDDEN, 990 make<SyntheticFunction>(nullSignature, 991 "__wasm_apply_global_tls_relocs")); 992 WasmSym::applyGlobalTLSRelocs->markLive(); 993 } 994 } 995 } 996 997 if (WasmSym::applyGlobalRelocs && WasmSym::initMemory) { 998 WasmSym::startFunction = symtab->addSyntheticFunction( 999 "__wasm_start", WASM_SYMBOL_VISIBILITY_HIDDEN, 1000 make<SyntheticFunction>(nullSignature, "__wasm_start")); 1001 WasmSym::startFunction->markLive(); 1002 } 1003 } 1004 1005 void Writer::createInitMemoryFunction() { 1006 LLVM_DEBUG(dbgs() << "createInitMemoryFunction\n"); 1007 assert(WasmSym::initMemory); 1008 assert(WasmSym::initMemoryFlag); 1009 assert(hasPassiveInitializedSegments()); 1010 uint64_t flagAddress = WasmSym::initMemoryFlag->getVA(); 1011 bool is64 = config->is64.getValueOr(false); 1012 std::string bodyContent; 1013 { 1014 raw_string_ostream os(bodyContent); 1015 // Initialize memory in a thread-safe manner. The thread that successfully 1016 // increments the flag from 0 to 1 is is responsible for performing the 1017 // memory initialization. Other threads go sleep on the flag until the 1018 // first thread finishing initializing memory, increments the flag to 2, 1019 // and wakes all the other threads. Once the flag has been set to 2, 1020 // subsequently started threads will skip the sleep. All threads 1021 // unconditionally drop their passive data segments once memory has been 1022 // initialized. The generated code is as follows: 1023 // 1024 // (func $__wasm_init_memory 1025 // (block $drop 1026 // (block $wait 1027 // (block $init 1028 // (br_table $init $wait $drop 1029 // (i32.atomic.rmw.cmpxchg align=2 offset=0 1030 // (i32.const $__init_memory_flag) 1031 // (i32.const 0) 1032 // (i32.const 1) 1033 // ) 1034 // ) 1035 // ) ;; $init 1036 // ( ... initialize data segments ... ) 1037 // (i32.atomic.store align=2 offset=0 1038 // (i32.const $__init_memory_flag) 1039 // (i32.const 2) 1040 // ) 1041 // (drop 1042 // (i32.atomic.notify align=2 offset=0 1043 // (i32.const $__init_memory_flag) 1044 // (i32.const -1u) 1045 // ) 1046 // ) 1047 // (br $drop) 1048 // ) ;; $wait 1049 // (drop 1050 // (i32.atomic.wait align=2 offset=0 1051 // (i32.const $__init_memory_flag) 1052 // (i32.const 1) 1053 // (i32.const -1) 1054 // ) 1055 // ) 1056 // ) ;; $drop 1057 // ( ... drop data segments ... ) 1058 // ) 1059 // 1060 // When we are building with PIC, calculate the flag location using: 1061 // 1062 // (global.get $__memory_base) 1063 // (i32.const $__init_memory_flag) 1064 // (i32.const 1) 1065 1066 // With PIC code we cache the flag address in local 0 1067 if (config->isPic) { 1068 writeUleb128(os, 1, "num local decls"); 1069 writeUleb128(os, 1, "local count"); 1070 writeU8(os, is64 ? WASM_TYPE_I64 : WASM_TYPE_I32, "address type"); 1071 writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET"); 1072 writeUleb128(os, WasmSym::memoryBase->getGlobalIndex(), "memory_base"); 1073 writePtrConst(os, flagAddress, is64, "flag address"); 1074 writeU8(os, is64 ? WASM_OPCODE_I64_ADD : WASM_OPCODE_I32_ADD, "add"); 1075 writeU8(os, WASM_OPCODE_LOCAL_SET, "local.set"); 1076 writeUleb128(os, 0, "local 0"); 1077 } else { 1078 writeUleb128(os, 0, "num locals"); 1079 } 1080 1081 auto writeGetFlagAddress = [&]() { 1082 if (config->isPic) { 1083 writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get"); 1084 writeUleb128(os, 0, "local 0"); 1085 } else { 1086 writePtrConst(os, flagAddress, is64, "flag address"); 1087 } 1088 }; 1089 1090 // Set up destination blocks 1091 writeU8(os, WASM_OPCODE_BLOCK, "block $drop"); 1092 writeU8(os, WASM_TYPE_NORESULT, "block type"); 1093 writeU8(os, WASM_OPCODE_BLOCK, "block $wait"); 1094 writeU8(os, WASM_TYPE_NORESULT, "block type"); 1095 writeU8(os, WASM_OPCODE_BLOCK, "block $init"); 1096 writeU8(os, WASM_TYPE_NORESULT, "block type"); 1097 1098 // Atomically check whether we win the race. 1099 writeGetFlagAddress(); 1100 writeI32Const(os, 0, "expected flag value"); 1101 writeI32Const(os, 1, "new flag value"); 1102 writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix"); 1103 writeUleb128(os, WASM_OPCODE_I32_RMW_CMPXCHG, "i32.atomic.rmw.cmpxchg"); 1104 writeMemArg(os, 2, 0); 1105 1106 // Based on the value, decide what to do next. 1107 writeU8(os, WASM_OPCODE_BR_TABLE, "br_table"); 1108 writeUleb128(os, 2, "label vector length"); 1109 writeUleb128(os, 0, "label $init"); 1110 writeUleb128(os, 1, "label $wait"); 1111 writeUleb128(os, 2, "default label $drop"); 1112 1113 // Initialize passive data segments 1114 writeU8(os, WASM_OPCODE_END, "end $init"); 1115 for (const OutputSegment *s : segments) { 1116 if (needsPassiveInitialization(s)) { 1117 // destination address 1118 writePtrConst(os, s->startVA, is64, "destination address"); 1119 if (config->isPic) { 1120 writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET"); 1121 writeUleb128(os, WasmSym::memoryBase->getGlobalIndex(), 1122 "memory_base"); 1123 writeU8(os, is64 ? WASM_OPCODE_I64_ADD : WASM_OPCODE_I32_ADD, 1124 "i32.add"); 1125 } 1126 // source segment offset 1127 writeI32Const(os, 0, "segment offset"); 1128 // memory region size 1129 writeI32Const(os, s->size, "memory region size"); 1130 // memory.init instruction 1131 writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix"); 1132 writeUleb128(os, WASM_OPCODE_MEMORY_INIT, "memory.init"); 1133 writeUleb128(os, s->index, "segment index immediate"); 1134 writeU8(os, 0, "memory index immediate"); 1135 } 1136 } 1137 1138 // Set flag to 2 to mark end of initialization 1139 writeGetFlagAddress(); 1140 writeI32Const(os, 2, "flag value"); 1141 writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix"); 1142 writeUleb128(os, WASM_OPCODE_I32_ATOMIC_STORE, "i32.atomic.store"); 1143 writeMemArg(os, 2, 0); 1144 1145 // Notify any waiters that memory initialization is complete 1146 writeGetFlagAddress(); 1147 writeI32Const(os, -1, "number of waiters"); 1148 writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix"); 1149 writeUleb128(os, WASM_OPCODE_ATOMIC_NOTIFY, "atomic.notify"); 1150 writeMemArg(os, 2, 0); 1151 writeU8(os, WASM_OPCODE_DROP, "drop"); 1152 1153 // Branch to drop the segments 1154 writeU8(os, WASM_OPCODE_BR, "br"); 1155 writeUleb128(os, 1, "label $drop"); 1156 1157 // Wait for the winning thread to initialize memory 1158 writeU8(os, WASM_OPCODE_END, "end $wait"); 1159 writeGetFlagAddress(); 1160 writeI32Const(os, 1, "expected flag value"); 1161 writeI64Const(os, -1, "timeout"); 1162 1163 writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix"); 1164 writeUleb128(os, WASM_OPCODE_I32_ATOMIC_WAIT, "i32.atomic.wait"); 1165 writeMemArg(os, 2, 0); 1166 writeU8(os, WASM_OPCODE_DROP, "drop"); 1167 1168 // Unconditionally drop passive data segments 1169 writeU8(os, WASM_OPCODE_END, "end $drop"); 1170 for (const OutputSegment *s : segments) { 1171 if (needsPassiveInitialization(s)) { 1172 // data.drop instruction 1173 writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix"); 1174 writeUleb128(os, WASM_OPCODE_DATA_DROP, "data.drop"); 1175 writeUleb128(os, s->index, "segment index immediate"); 1176 } 1177 } 1178 1179 // End the function 1180 writeU8(os, WASM_OPCODE_END, "END"); 1181 } 1182 1183 createFunction(WasmSym::initMemory, bodyContent); 1184 } 1185 1186 void Writer::createStartFunction() { 1187 if (WasmSym::startFunction) { 1188 std::string bodyContent; 1189 { 1190 raw_string_ostream os(bodyContent); 1191 writeUleb128(os, 0, "num locals"); 1192 writeU8(os, WASM_OPCODE_CALL, "CALL"); 1193 writeUleb128(os, WasmSym::initMemory->getFunctionIndex(), 1194 "function index"); 1195 writeU8(os, WASM_OPCODE_CALL, "CALL"); 1196 writeUleb128(os, WasmSym::applyGlobalRelocs->getFunctionIndex(), 1197 "function index"); 1198 writeU8(os, WASM_OPCODE_END, "END"); 1199 } 1200 createFunction(WasmSym::startFunction, bodyContent); 1201 } else if (WasmSym::initMemory) { 1202 WasmSym::startFunction = WasmSym::initMemory; 1203 } else if (WasmSym::applyGlobalRelocs) { 1204 WasmSym::startFunction = WasmSym::applyGlobalRelocs; 1205 } 1206 } 1207 1208 // For -shared (PIC) output, we create create a synthetic function which will 1209 // apply any relocations to the data segments on startup. This function is 1210 // called `__wasm_apply_data_relocs` and is added at the beginning of 1211 // `__wasm_call_ctors` before any of the constructors run. 1212 void Writer::createApplyDataRelocationsFunction() { 1213 LLVM_DEBUG(dbgs() << "createApplyDataRelocationsFunction\n"); 1214 // First write the body's contents to a string. 1215 std::string bodyContent; 1216 { 1217 raw_string_ostream os(bodyContent); 1218 writeUleb128(os, 0, "num locals"); 1219 for (const OutputSegment *seg : segments) 1220 for (const InputChunk *inSeg : seg->inputSegments) 1221 inSeg->generateRelocationCode(os); 1222 1223 writeU8(os, WASM_OPCODE_END, "END"); 1224 } 1225 1226 createFunction(WasmSym::applyDataRelocs, bodyContent); 1227 } 1228 1229 // Similar to createApplyDataRelocationsFunction but generates relocation code 1230 // fro WebAssembly globals. Because these globals are not shared between threads 1231 // these relocation need to run on every thread. 1232 void Writer::createApplyGlobalRelocationsFunction() { 1233 // First write the body's contents to a string. 1234 std::string bodyContent; 1235 { 1236 raw_string_ostream os(bodyContent); 1237 writeUleb128(os, 0, "num locals"); 1238 out.globalSec->generateRelocationCode(os, false); 1239 writeU8(os, WASM_OPCODE_END, "END"); 1240 } 1241 1242 createFunction(WasmSym::applyGlobalRelocs, bodyContent); 1243 } 1244 1245 // Similar to createApplyGlobalRelocationsFunction but for 1246 // TLS symbols. This cannot be run during the start function 1247 // but must be delayed until __wasm_init_tls is called. 1248 void Writer::createApplyGlobalTLSRelocationsFunction() { 1249 // First write the body's contents to a string. 1250 std::string bodyContent; 1251 { 1252 raw_string_ostream os(bodyContent); 1253 writeUleb128(os, 0, "num locals"); 1254 out.globalSec->generateRelocationCode(os, true); 1255 writeU8(os, WASM_OPCODE_END, "END"); 1256 } 1257 1258 createFunction(WasmSym::applyGlobalTLSRelocs, bodyContent); 1259 } 1260 1261 // Create synthetic "__wasm_call_ctors" function based on ctor functions 1262 // in input object. 1263 void Writer::createCallCtorsFunction() { 1264 // If __wasm_call_ctors isn't referenced, there aren't any ctors, and we 1265 // aren't calling `__wasm_apply_data_relocs` for Emscripten-style PIC, don't 1266 // define the `__wasm_call_ctors` function. 1267 if (!WasmSym::callCtors->isLive() && !WasmSym::applyDataRelocs && 1268 initFunctions.empty()) 1269 return; 1270 1271 // First write the body's contents to a string. 1272 std::string bodyContent; 1273 { 1274 raw_string_ostream os(bodyContent); 1275 writeUleb128(os, 0, "num locals"); 1276 1277 if (WasmSym::applyDataRelocs) { 1278 writeU8(os, WASM_OPCODE_CALL, "CALL"); 1279 writeUleb128(os, WasmSym::applyDataRelocs->getFunctionIndex(), 1280 "function index"); 1281 } 1282 1283 // Call constructors 1284 for (const WasmInitEntry &f : initFunctions) { 1285 writeU8(os, WASM_OPCODE_CALL, "CALL"); 1286 writeUleb128(os, f.sym->getFunctionIndex(), "function index"); 1287 for (size_t i = 0; i < f.sym->signature->Returns.size(); i++) { 1288 writeU8(os, WASM_OPCODE_DROP, "DROP"); 1289 } 1290 } 1291 1292 writeU8(os, WASM_OPCODE_END, "END"); 1293 } 1294 1295 createFunction(WasmSym::callCtors, bodyContent); 1296 } 1297 1298 // Create a wrapper around a function export which calls the 1299 // static constructors and destructors. 1300 void Writer::createCommandExportWrapper(uint32_t functionIndex, 1301 DefinedFunction *f) { 1302 // First write the body's contents to a string. 1303 std::string bodyContent; 1304 { 1305 raw_string_ostream os(bodyContent); 1306 writeUleb128(os, 0, "num locals"); 1307 1308 // Call `__wasm_call_ctors` which call static constructors (and 1309 // applies any runtime relocations in Emscripten-style PIC mode) 1310 if (WasmSym::callCtors->isLive()) { 1311 writeU8(os, WASM_OPCODE_CALL, "CALL"); 1312 writeUleb128(os, WasmSym::callCtors->getFunctionIndex(), 1313 "function index"); 1314 } 1315 1316 // Call the user's code, leaving any return values on the operand stack. 1317 for (size_t i = 0; i < f->signature->Params.size(); ++i) { 1318 writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get"); 1319 writeUleb128(os, i, "local index"); 1320 } 1321 writeU8(os, WASM_OPCODE_CALL, "CALL"); 1322 writeUleb128(os, functionIndex, "function index"); 1323 1324 // Call the function that calls the destructors. 1325 if (DefinedFunction *callDtors = WasmSym::callDtors) { 1326 writeU8(os, WASM_OPCODE_CALL, "CALL"); 1327 writeUleb128(os, callDtors->getFunctionIndex(), "function index"); 1328 } 1329 1330 // End the function, returning the return values from the user's code. 1331 writeU8(os, WASM_OPCODE_END, "END"); 1332 } 1333 1334 createFunction(f, bodyContent); 1335 } 1336 1337 void Writer::createInitTLSFunction() { 1338 std::string bodyContent; 1339 { 1340 raw_string_ostream os(bodyContent); 1341 1342 OutputSegment *tlsSeg = nullptr; 1343 for (auto *seg : segments) { 1344 if (seg->name == ".tdata") { 1345 tlsSeg = seg; 1346 break; 1347 } 1348 } 1349 1350 writeUleb128(os, 0, "num locals"); 1351 if (tlsSeg) { 1352 writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get"); 1353 writeUleb128(os, 0, "local index"); 1354 1355 writeU8(os, WASM_OPCODE_GLOBAL_SET, "global.set"); 1356 writeUleb128(os, WasmSym::tlsBase->getGlobalIndex(), "global index"); 1357 1358 // FIXME(wvo): this local needs to be I64 in wasm64, or we need an extend op. 1359 writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get"); 1360 writeUleb128(os, 0, "local index"); 1361 1362 writeI32Const(os, 0, "segment offset"); 1363 1364 writeI32Const(os, tlsSeg->size, "memory region size"); 1365 1366 writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix"); 1367 writeUleb128(os, WASM_OPCODE_MEMORY_INIT, "MEMORY.INIT"); 1368 writeUleb128(os, tlsSeg->index, "segment index immediate"); 1369 writeU8(os, 0, "memory index immediate"); 1370 } 1371 1372 if (WasmSym::applyGlobalTLSRelocs) { 1373 writeU8(os, WASM_OPCODE_CALL, "CALL"); 1374 writeUleb128(os, WasmSym::applyGlobalTLSRelocs->getFunctionIndex(), 1375 "function index"); 1376 } 1377 writeU8(os, WASM_OPCODE_END, "end function"); 1378 } 1379 1380 createFunction(WasmSym::initTLS, bodyContent); 1381 } 1382 1383 // Populate InitFunctions vector with init functions from all input objects. 1384 // This is then used either when creating the output linking section or to 1385 // synthesize the "__wasm_call_ctors" function. 1386 void Writer::calculateInitFunctions() { 1387 if (!config->relocatable && !WasmSym::callCtors->isLive()) 1388 return; 1389 1390 for (ObjFile *file : symtab->objectFiles) { 1391 const WasmLinkingData &l = file->getWasmObj()->linkingData(); 1392 for (const WasmInitFunc &f : l.InitFunctions) { 1393 FunctionSymbol *sym = file->getFunctionSymbol(f.Symbol); 1394 // comdat exclusions can cause init functions be discarded. 1395 if (sym->isDiscarded() || !sym->isLive()) 1396 continue; 1397 if (sym->signature->Params.size() != 0) 1398 error("constructor functions cannot take arguments: " + toString(*sym)); 1399 LLVM_DEBUG(dbgs() << "initFunctions: " << toString(*sym) << "\n"); 1400 initFunctions.emplace_back(WasmInitEntry{sym, f.Priority}); 1401 } 1402 } 1403 1404 // Sort in order of priority (lowest first) so that they are called 1405 // in the correct order. 1406 llvm::stable_sort(initFunctions, 1407 [](const WasmInitEntry &l, const WasmInitEntry &r) { 1408 return l.priority < r.priority; 1409 }); 1410 } 1411 1412 void Writer::createSyntheticSections() { 1413 out.dylinkSec = make<DylinkSection>(); 1414 out.typeSec = make<TypeSection>(); 1415 out.importSec = make<ImportSection>(); 1416 out.functionSec = make<FunctionSection>(); 1417 out.tableSec = make<TableSection>(); 1418 out.memorySec = make<MemorySection>(); 1419 out.tagSec = make<TagSection>(); 1420 out.globalSec = make<GlobalSection>(); 1421 out.exportSec = make<ExportSection>(); 1422 out.startSec = make<StartSection>(); 1423 out.elemSec = make<ElemSection>(); 1424 out.producersSec = make<ProducersSection>(); 1425 out.targetFeaturesSec = make<TargetFeaturesSection>(); 1426 } 1427 1428 void Writer::createSyntheticSectionsPostLayout() { 1429 out.dataCountSec = make<DataCountSection>(segments); 1430 out.linkingSec = make<LinkingSection>(initFunctions, segments); 1431 out.nameSec = make<NameSection>(segments); 1432 } 1433 1434 void Writer::run() { 1435 if (config->relocatable || config->isPic) 1436 config->globalBase = 0; 1437 1438 // For PIC code the table base is assigned dynamically by the loader. 1439 // For non-PIC, we start at 1 so that accessing table index 0 always traps. 1440 if (!config->isPic) { 1441 config->tableBase = 1; 1442 if (WasmSym::definedTableBase) 1443 WasmSym::definedTableBase->setVA(config->tableBase); 1444 if (WasmSym::definedTableBase32) 1445 WasmSym::definedTableBase32->setVA(config->tableBase); 1446 } 1447 1448 log("-- createOutputSegments"); 1449 createOutputSegments(); 1450 log("-- createSyntheticSections"); 1451 createSyntheticSections(); 1452 log("-- layoutMemory"); 1453 layoutMemory(); 1454 1455 if (!config->relocatable) { 1456 // Create linker synthesized __start_SECNAME/__stop_SECNAME symbols 1457 // This has to be done after memory layout is performed. 1458 for (const OutputSegment *seg : segments) { 1459 addStartStopSymbols(seg); 1460 } 1461 } 1462 1463 for (auto &pair : config->exportedSymbols) { 1464 Symbol *sym = symtab->find(pair.first()); 1465 if (sym && sym->isDefined()) 1466 sym->forceExport = true; 1467 } 1468 1469 // Delay reporting error about explict exports until after addStartStopSymbols 1470 // which can create optional symbols. 1471 for (auto &name : config->requiredExports) { 1472 Symbol *sym = symtab->find(name); 1473 if (!sym || !sym->isDefined()) { 1474 if (config->unresolvedSymbols == UnresolvedPolicy::ReportError) 1475 error(Twine("symbol exported via --export not found: ") + name); 1476 if (config->unresolvedSymbols == UnresolvedPolicy::Warn) 1477 warn(Twine("symbol exported via --export not found: ") + name); 1478 } 1479 } 1480 1481 if (config->isPic && !config->sharedMemory) { 1482 // In shared memory mode all data segments are passive and initilized 1483 // via __wasm_init_memory. 1484 log("-- combineOutputSegments"); 1485 combineOutputSegments(); 1486 } 1487 1488 log("-- createSyntheticSectionsPostLayout"); 1489 createSyntheticSectionsPostLayout(); 1490 log("-- populateProducers"); 1491 populateProducers(); 1492 log("-- calculateImports"); 1493 calculateImports(); 1494 log("-- scanRelocations"); 1495 scanRelocations(); 1496 log("-- finalizeIndirectFunctionTable"); 1497 finalizeIndirectFunctionTable(); 1498 log("-- createSyntheticInitFunctions"); 1499 createSyntheticInitFunctions(); 1500 log("-- assignIndexes"); 1501 assignIndexes(); 1502 log("-- calculateInitFunctions"); 1503 calculateInitFunctions(); 1504 1505 if (!config->relocatable) { 1506 // Create linker synthesized functions 1507 if (WasmSym::applyDataRelocs) 1508 createApplyDataRelocationsFunction(); 1509 if (WasmSym::applyGlobalRelocs) 1510 createApplyGlobalRelocationsFunction(); 1511 if (WasmSym::applyGlobalTLSRelocs) 1512 createApplyGlobalTLSRelocationsFunction(); 1513 if (WasmSym::initMemory) 1514 createInitMemoryFunction(); 1515 createStartFunction(); 1516 1517 createCallCtorsFunction(); 1518 1519 // Create export wrappers for commands if needed. 1520 // 1521 // If the input contains a call to `__wasm_call_ctors`, either in one of 1522 // the input objects or an explicit export from the command-line, we 1523 // assume ctors and dtors are taken care of already. 1524 if (!config->relocatable && !config->isPic && 1525 !WasmSym::callCtors->isUsedInRegularObj && 1526 !WasmSym::callCtors->isExported()) { 1527 log("-- createCommandExportWrappers"); 1528 createCommandExportWrappers(); 1529 } 1530 } 1531 1532 if (WasmSym::initTLS && WasmSym::initTLS->isLive()) 1533 createInitTLSFunction(); 1534 1535 if (errorCount()) 1536 return; 1537 1538 log("-- calculateTypes"); 1539 calculateTypes(); 1540 log("-- calculateExports"); 1541 calculateExports(); 1542 log("-- calculateCustomSections"); 1543 calculateCustomSections(); 1544 log("-- populateSymtab"); 1545 populateSymtab(); 1546 log("-- populateTargetFeatures"); 1547 populateTargetFeatures(); 1548 log("-- addSections"); 1549 addSections(); 1550 1551 if (errorHandler().verbose) { 1552 log("Defined Functions: " + Twine(out.functionSec->inputFunctions.size())); 1553 log("Defined Globals : " + Twine(out.globalSec->numGlobals())); 1554 log("Defined Tags : " + Twine(out.tagSec->inputTags.size())); 1555 log("Defined Tables : " + Twine(out.tableSec->inputTables.size())); 1556 log("Function Imports : " + 1557 Twine(out.importSec->getNumImportedFunctions())); 1558 log("Global Imports : " + Twine(out.importSec->getNumImportedGlobals())); 1559 log("Tag Imports : " + Twine(out.importSec->getNumImportedTags())); 1560 log("Table Imports : " + Twine(out.importSec->getNumImportedTables())); 1561 } 1562 1563 createHeader(); 1564 log("-- finalizeSections"); 1565 finalizeSections(); 1566 1567 log("-- writeMapFile"); 1568 writeMapFile(outputSections); 1569 1570 log("-- openFile"); 1571 openFile(); 1572 if (errorCount()) 1573 return; 1574 1575 writeHeader(); 1576 1577 log("-- writeSections"); 1578 writeSections(); 1579 if (errorCount()) 1580 return; 1581 1582 if (Error e = buffer->commit()) 1583 fatal("failed to write the output file: " + toString(std::move(e))); 1584 } 1585 1586 // Open a result file. 1587 void Writer::openFile() { 1588 log("writing: " + config->outputFile); 1589 1590 Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr = 1591 FileOutputBuffer::create(config->outputFile, fileSize, 1592 FileOutputBuffer::F_executable); 1593 1594 if (!bufferOrErr) 1595 error("failed to open " + config->outputFile + ": " + 1596 toString(bufferOrErr.takeError())); 1597 else 1598 buffer = std::move(*bufferOrErr); 1599 } 1600 1601 void Writer::createHeader() { 1602 raw_string_ostream os(header); 1603 writeBytes(os, WasmMagic, sizeof(WasmMagic), "wasm magic"); 1604 writeU32(os, WasmVersion, "wasm version"); 1605 os.flush(); 1606 fileSize += header.size(); 1607 } 1608 1609 void writeResult() { Writer().run(); } 1610 1611 } // namespace wasm 1612 } // namespace lld 1613