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