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 "InputEvent.h" 13 #include "InputGlobal.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/Object/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 47 namespace { 48 49 // The writer writes a SymbolTable result to a file. 50 class Writer { 51 public: 52 void run(); 53 54 private: 55 void openFile(); 56 57 void createInitMemoryFunction(); 58 void createApplyRelocationsFunction(); 59 void createCallCtorsFunction(); 60 void createInitTLSFunction(); 61 62 void assignIndexes(); 63 void populateSymtab(); 64 void populateProducers(); 65 void populateTargetFeatures(); 66 void calculateInitFunctions(); 67 void calculateImports(); 68 void calculateExports(); 69 void calculateCustomSections(); 70 void calculateTypes(); 71 void createOutputSegments(); 72 void layoutMemory(); 73 void createHeader(); 74 75 void addSection(OutputSection *sec); 76 77 void addSections(); 78 79 void createCustomSections(); 80 void createSyntheticSections(); 81 void finalizeSections(); 82 83 // Custom sections 84 void createRelocSections(); 85 86 void writeHeader(); 87 void writeSections(); 88 89 uint64_t fileSize = 0; 90 91 std::vector<WasmInitEntry> initFunctions; 92 llvm::StringMap<std::vector<InputSection *>> customSectionMapping; 93 94 // Elements that are used to construct the final output 95 std::string header; 96 std::vector<OutputSection *> outputSections; 97 98 std::unique_ptr<FileOutputBuffer> buffer; 99 100 std::vector<OutputSegment *> segments; 101 llvm::SmallDenseMap<StringRef, OutputSegment *> segmentMap; 102 }; 103 104 } // anonymous namespace 105 106 void Writer::calculateCustomSections() { 107 log("calculateCustomSections"); 108 bool stripDebug = config->stripDebug || config->stripAll; 109 for (ObjFile *file : symtab->objectFiles) { 110 for (InputSection *section : file->customSections) { 111 StringRef name = section->getName(); 112 // These custom sections are known the linker and synthesized rather than 113 // blindly copied. 114 if (name == "linking" || name == "name" || name == "producers" || 115 name == "target_features" || name.startswith("reloc.")) 116 continue; 117 // These custom sections are generated by `clang -fembed-bitcode`. 118 // These are used by the rust toolchain to ship LTO data along with 119 // compiled object code, but they don't want this included in the linker 120 // output. 121 if (name == ".llvmbc" || name == ".llvmcmd") 122 continue; 123 // Strip debug section in that option was specified. 124 if (stripDebug && name.startswith(".debug_")) 125 continue; 126 // Otherwise include custom sections by default and concatenate their 127 // contents. 128 customSectionMapping[name].push_back(section); 129 } 130 } 131 } 132 133 void Writer::createCustomSections() { 134 log("createCustomSections"); 135 for (auto &pair : customSectionMapping) { 136 StringRef name = pair.first(); 137 LLVM_DEBUG(dbgs() << "createCustomSection: " << name << "\n"); 138 139 OutputSection *sec = make<CustomSection>(std::string(name), pair.second); 140 if (config->relocatable || config->emitRelocs) { 141 auto *sym = make<OutputSectionSymbol>(sec); 142 out.linkingSec->addToSymtab(sym); 143 sec->sectionSym = sym; 144 } 145 addSection(sec); 146 } 147 } 148 149 // Create relocations sections in the final output. 150 // These are only created when relocatable output is requested. 151 void Writer::createRelocSections() { 152 log("createRelocSections"); 153 // Don't use iterator here since we are adding to OutputSection 154 size_t origSize = outputSections.size(); 155 for (size_t i = 0; i < origSize; i++) { 156 LLVM_DEBUG(dbgs() << "check section " << i << "\n"); 157 OutputSection *sec = outputSections[i]; 158 159 // Count the number of needed sections. 160 uint32_t count = sec->getNumRelocations(); 161 if (!count) 162 continue; 163 164 StringRef name; 165 if (sec->type == WASM_SEC_DATA) 166 name = "reloc.DATA"; 167 else if (sec->type == WASM_SEC_CODE) 168 name = "reloc.CODE"; 169 else if (sec->type == WASM_SEC_CUSTOM) 170 name = saver.save("reloc." + sec->name); 171 else 172 llvm_unreachable( 173 "relocations only supported for code, data, or custom sections"); 174 175 addSection(make<RelocSection>(name, sec)); 176 } 177 } 178 179 void Writer::populateProducers() { 180 for (ObjFile *file : symtab->objectFiles) { 181 const WasmProducerInfo &info = file->getWasmObj()->getProducerInfo(); 182 out.producersSec->addInfo(info); 183 } 184 } 185 186 void Writer::writeHeader() { 187 memcpy(buffer->getBufferStart(), header.data(), header.size()); 188 } 189 190 void Writer::writeSections() { 191 uint8_t *buf = buffer->getBufferStart(); 192 parallelForEach(outputSections, [buf](OutputSection *s) { 193 assert(s->isNeeded()); 194 s->writeTo(buf); 195 }); 196 } 197 198 // Fix the memory layout of the output binary. This assigns memory offsets 199 // to each of the input data sections as well as the explicit stack region. 200 // The default memory layout is as follows, from low to high. 201 // 202 // - initialized data (starting at Config->globalBase) 203 // - BSS data (not currently implemented in llvm) 204 // - explicit stack (Config->ZStackSize) 205 // - heap start / unallocated 206 // 207 // The --stack-first option means that stack is placed before any static data. 208 // This can be useful since it means that stack overflow traps immediately 209 // rather than overwriting global data, but also increases code size since all 210 // static data loads and stores requires larger offsets. 211 void Writer::layoutMemory() { 212 uint64_t memoryPtr = 0; 213 214 auto placeStack = [&]() { 215 if (config->relocatable || config->isPic) 216 return; 217 memoryPtr = alignTo(memoryPtr, stackAlignment); 218 if (config->zStackSize != alignTo(config->zStackSize, stackAlignment)) 219 error("stack size must be " + Twine(stackAlignment) + "-byte aligned"); 220 log("mem: stack size = " + Twine(config->zStackSize)); 221 log("mem: stack base = " + Twine(memoryPtr)); 222 memoryPtr += config->zStackSize; 223 auto *sp = cast<DefinedGlobal>(WasmSym::stackPointer); 224 sp->global->global.InitExpr.Value.Int32 = memoryPtr; 225 log("mem: stack top = " + Twine(memoryPtr)); 226 }; 227 228 if (config->stackFirst) { 229 placeStack(); 230 } else { 231 memoryPtr = config->globalBase; 232 log("mem: global base = " + Twine(config->globalBase)); 233 } 234 235 if (WasmSym::globalBase) 236 WasmSym::globalBase->setVirtualAddress(memoryPtr); 237 238 uint64_t dataStart = memoryPtr; 239 240 // Arbitrarily set __dso_handle handle to point to the start of the data 241 // segments. 242 if (WasmSym::dsoHandle) 243 WasmSym::dsoHandle->setVirtualAddress(dataStart); 244 245 out.dylinkSec->memAlign = 0; 246 for (OutputSegment *seg : segments) { 247 out.dylinkSec->memAlign = std::max(out.dylinkSec->memAlign, seg->alignment); 248 memoryPtr = alignTo(memoryPtr, 1ULL << seg->alignment); 249 seg->startVA = memoryPtr; 250 log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}", seg->name, 251 memoryPtr, seg->size, seg->alignment)); 252 memoryPtr += seg->size; 253 254 if (WasmSym::tlsSize && seg->name == ".tdata") { 255 auto *tlsSize = cast<DefinedGlobal>(WasmSym::tlsSize); 256 tlsSize->global->global.InitExpr.Value.Int32 = seg->size; 257 258 auto *tlsAlign = cast<DefinedGlobal>(WasmSym::tlsAlign); 259 tlsAlign->global->global.InitExpr.Value.Int32 = 1U << seg->alignment; 260 } 261 } 262 263 // Make space for the memory initialization flag 264 if (WasmSym::initMemoryFlag) { 265 memoryPtr = alignTo(memoryPtr, 4); 266 WasmSym::initMemoryFlag->setVirtualAddress(memoryPtr); 267 log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}", 268 "__wasm_init_memory_flag", memoryPtr, 4, 4)); 269 memoryPtr += 4; 270 } 271 272 if (WasmSym::dataEnd) 273 WasmSym::dataEnd->setVirtualAddress(memoryPtr); 274 275 log("mem: static data = " + Twine(memoryPtr - dataStart)); 276 277 if (config->shared) { 278 out.dylinkSec->memSize = memoryPtr; 279 return; 280 } 281 282 if (!config->stackFirst) 283 placeStack(); 284 285 // Set `__heap_base` to directly follow the end of the stack or global data. 286 // The fact that this comes last means that a malloc/brk implementation 287 // can grow the heap at runtime. 288 log("mem: heap base = " + Twine(memoryPtr)); 289 if (WasmSym::heapBase) 290 WasmSym::heapBase->setVirtualAddress(memoryPtr); 291 292 if (config->initialMemory != 0) { 293 if (config->initialMemory != alignTo(config->initialMemory, WasmPageSize)) 294 error("initial memory must be " + Twine(WasmPageSize) + "-byte aligned"); 295 if (memoryPtr > config->initialMemory) 296 error("initial memory too small, " + Twine(memoryPtr) + " bytes needed"); 297 if (config->initialMemory > (1ULL << 32)) 298 error("initial memory too large, cannot be greater than 4294967296"); 299 memoryPtr = config->initialMemory; 300 } 301 out.dylinkSec->memSize = memoryPtr; 302 out.memorySec->numMemoryPages = 303 alignTo(memoryPtr, WasmPageSize) / WasmPageSize; 304 log("mem: total pages = " + Twine(out.memorySec->numMemoryPages)); 305 306 // Check max if explicitly supplied or required by shared memory 307 if (config->maxMemory != 0 || config->sharedMemory) { 308 if (config->maxMemory != alignTo(config->maxMemory, WasmPageSize)) 309 error("maximum memory must be " + Twine(WasmPageSize) + "-byte aligned"); 310 if (memoryPtr > config->maxMemory) 311 error("maximum memory too small, " + Twine(memoryPtr) + " bytes needed"); 312 if (config->maxMemory > (1ULL << 32)) 313 error("maximum memory too large, cannot be greater than 4294967296"); 314 out.memorySec->maxMemoryPages = config->maxMemory / WasmPageSize; 315 log("mem: max pages = " + Twine(out.memorySec->maxMemoryPages)); 316 } 317 } 318 319 void Writer::addSection(OutputSection *sec) { 320 if (!sec->isNeeded()) 321 return; 322 log("addSection: " + toString(*sec)); 323 sec->sectionIndex = outputSections.size(); 324 outputSections.push_back(sec); 325 } 326 327 // If a section name is valid as a C identifier (which is rare because of 328 // the leading '.'), linkers are expected to define __start_<secname> and 329 // __stop_<secname> symbols. They are at beginning and end of the section, 330 // respectively. This is not requested by the ELF standard, but GNU ld and 331 // gold provide the feature, and used by many programs. 332 static void addStartStopSymbols(const OutputSegment *seg) { 333 StringRef name = seg->name; 334 if (!isValidCIdentifier(name)) 335 return; 336 LLVM_DEBUG(dbgs() << "addStartStopSymbols: " << name << "\n"); 337 uint32_t start = seg->startVA; 338 uint32_t stop = start + seg->size; 339 symtab->addOptionalDataSymbol(saver.save("__start_" + name), start); 340 symtab->addOptionalDataSymbol(saver.save("__stop_" + name), stop); 341 } 342 343 void Writer::addSections() { 344 addSection(out.dylinkSec); 345 addSection(out.typeSec); 346 addSection(out.importSec); 347 addSection(out.functionSec); 348 addSection(out.tableSec); 349 addSection(out.memorySec); 350 addSection(out.eventSec); 351 addSection(out.globalSec); 352 addSection(out.exportSec); 353 addSection(out.startSec); 354 addSection(out.elemSec); 355 addSection(out.dataCountSec); 356 357 addSection(make<CodeSection>(out.functionSec->inputFunctions)); 358 addSection(make<DataSection>(segments)); 359 360 createCustomSections(); 361 362 addSection(out.linkingSec); 363 if (config->emitRelocs || config->relocatable) { 364 createRelocSections(); 365 } 366 367 addSection(out.nameSec); 368 addSection(out.producersSec); 369 addSection(out.targetFeaturesSec); 370 } 371 372 void Writer::finalizeSections() { 373 for (OutputSection *s : outputSections) { 374 s->setOffset(fileSize); 375 s->finalizeContents(); 376 fileSize += s->getSize(); 377 } 378 } 379 380 void Writer::populateTargetFeatures() { 381 StringMap<std::string> used; 382 StringMap<std::string> required; 383 StringMap<std::string> disallowed; 384 SmallSet<std::string, 8> &allowed = out.targetFeaturesSec->features; 385 bool tlsUsed = false; 386 387 // Only infer used features if user did not specify features 388 bool inferFeatures = !config->features.hasValue(); 389 390 if (!inferFeatures) { 391 auto &explicitFeatures = config->features.getValue(); 392 allowed.insert(explicitFeatures.begin(), explicitFeatures.end()); 393 if (!config->checkFeatures) 394 return; 395 } 396 397 // Find the sets of used, required, and disallowed features 398 for (ObjFile *file : symtab->objectFiles) { 399 StringRef fileName(file->getName()); 400 for (auto &feature : file->getWasmObj()->getTargetFeatures()) { 401 switch (feature.Prefix) { 402 case WASM_FEATURE_PREFIX_USED: 403 used.insert({feature.Name, std::string(fileName)}); 404 break; 405 case WASM_FEATURE_PREFIX_REQUIRED: 406 used.insert({feature.Name, std::string(fileName)}); 407 required.insert({feature.Name, std::string(fileName)}); 408 break; 409 case WASM_FEATURE_PREFIX_DISALLOWED: 410 disallowed.insert({feature.Name, std::string(fileName)}); 411 break; 412 default: 413 error("Unrecognized feature policy prefix " + 414 std::to_string(feature.Prefix)); 415 } 416 } 417 418 // Find TLS data segments 419 auto isTLS = [](InputSegment *segment) { 420 StringRef name = segment->getName(); 421 return segment->live && 422 (name.startswith(".tdata") || name.startswith(".tbss")); 423 }; 424 tlsUsed = tlsUsed || 425 std::any_of(file->segments.begin(), file->segments.end(), isTLS); 426 } 427 428 if (inferFeatures) 429 for (const auto &key : used.keys()) 430 allowed.insert(std::string(key)); 431 432 if (!config->relocatable && allowed.count("atomics") && 433 !config->sharedMemory) { 434 if (inferFeatures) 435 error(Twine("'atomics' feature is used by ") + used["atomics"] + 436 ", so --shared-memory must be used"); 437 else 438 error("'atomics' feature is used, so --shared-memory must be used"); 439 } 440 441 if (!config->checkFeatures) 442 return; 443 444 if (config->sharedMemory) { 445 if (disallowed.count("shared-mem")) 446 error("--shared-memory is disallowed by " + disallowed["shared-mem"] + 447 " because it was not compiled with 'atomics' or 'bulk-memory' " 448 "features."); 449 450 for (auto feature : {"atomics", "bulk-memory"}) 451 if (!allowed.count(feature)) 452 error(StringRef("'") + feature + 453 "' feature must be used in order to use shared memory"); 454 } 455 456 if (tlsUsed) { 457 for (auto feature : {"atomics", "bulk-memory"}) 458 if (!allowed.count(feature)) 459 error(StringRef("'") + feature + 460 "' feature must be used in order to use thread-local storage"); 461 } 462 463 // Validate that used features are allowed in output 464 if (!inferFeatures) { 465 for (auto &feature : used.keys()) { 466 if (!allowed.count(std::string(feature))) 467 error(Twine("Target feature '") + feature + "' used by " + 468 used[feature] + " is not allowed."); 469 } 470 } 471 472 // Validate the required and disallowed constraints for each file 473 for (ObjFile *file : symtab->objectFiles) { 474 StringRef fileName(file->getName()); 475 SmallSet<std::string, 8> objectFeatures; 476 for (auto &feature : file->getWasmObj()->getTargetFeatures()) { 477 if (feature.Prefix == WASM_FEATURE_PREFIX_DISALLOWED) 478 continue; 479 objectFeatures.insert(feature.Name); 480 if (disallowed.count(feature.Name)) 481 error(Twine("Target feature '") + feature.Name + "' used in " + 482 fileName + " is disallowed by " + disallowed[feature.Name] + 483 ". Use --no-check-features to suppress."); 484 } 485 for (auto &feature : required.keys()) { 486 if (!objectFeatures.count(std::string(feature))) 487 error(Twine("Missing target feature '") + feature + "' in " + fileName + 488 ", required by " + required[feature] + 489 ". Use --no-check-features to suppress."); 490 } 491 } 492 } 493 494 void Writer::calculateImports() { 495 for (Symbol *sym : symtab->getSymbols()) { 496 if (!sym->isUndefined()) 497 continue; 498 if (sym->isWeak() && !config->relocatable) 499 continue; 500 if (!sym->isLive()) 501 continue; 502 if (!sym->isUsedInRegularObj) 503 continue; 504 // We don't generate imports for data symbols. They however can be imported 505 // as GOT entries. 506 if (isa<DataSymbol>(sym)) 507 continue; 508 509 LLVM_DEBUG(dbgs() << "import: " << sym->getName() << "\n"); 510 out.importSec->addImport(sym); 511 } 512 } 513 514 void Writer::calculateExports() { 515 if (config->relocatable) 516 return; 517 518 if (!config->relocatable && !config->importMemory) 519 out.exportSec->exports.push_back( 520 WasmExport{"memory", WASM_EXTERNAL_MEMORY, 0}); 521 522 if (!config->relocatable && config->exportTable) 523 out.exportSec->exports.push_back( 524 WasmExport{functionTableName, WASM_EXTERNAL_TABLE, 0}); 525 526 unsigned globalIndex = 527 out.importSec->getNumImportedGlobals() + out.globalSec->numGlobals(); 528 529 for (Symbol *sym : symtab->getSymbols()) { 530 if (!sym->isExported()) 531 continue; 532 if (!sym->isLive()) 533 continue; 534 535 StringRef name = sym->getName(); 536 WasmExport export_; 537 if (auto *f = dyn_cast<DefinedFunction>(sym)) { 538 if (Optional<StringRef> exportName = f->function->getExportName()) { 539 name = *exportName; 540 } 541 export_ = {name, WASM_EXTERNAL_FUNCTION, f->getFunctionIndex()}; 542 } else if (auto *g = dyn_cast<DefinedGlobal>(sym)) { 543 // TODO(sbc): Remove this check once to mutable global proposal is 544 // implement in all major browsers. 545 // See: https://github.com/WebAssembly/mutable-global 546 if (g->getGlobalType()->Mutable) { 547 // Only __stack_pointer and __tls_base should ever be create as mutable. 548 assert(g == WasmSym::stackPointer || g == WasmSym::tlsBase); 549 continue; 550 } 551 export_ = {name, WASM_EXTERNAL_GLOBAL, g->getGlobalIndex()}; 552 } else if (auto *e = dyn_cast<DefinedEvent>(sym)) { 553 export_ = {name, WASM_EXTERNAL_EVENT, e->getEventIndex()}; 554 } else { 555 auto *d = cast<DefinedData>(sym); 556 out.globalSec->dataAddressGlobals.push_back(d); 557 export_ = {name, WASM_EXTERNAL_GLOBAL, globalIndex++}; 558 } 559 560 LLVM_DEBUG(dbgs() << "Export: " << name << "\n"); 561 out.exportSec->exports.push_back(export_); 562 } 563 } 564 565 void Writer::populateSymtab() { 566 if (!config->relocatable && !config->emitRelocs) 567 return; 568 569 for (Symbol *sym : symtab->getSymbols()) 570 if (sym->isUsedInRegularObj && sym->isLive()) 571 out.linkingSec->addToSymtab(sym); 572 573 for (ObjFile *file : symtab->objectFiles) { 574 LLVM_DEBUG(dbgs() << "Local symtab entries: " << file->getName() << "\n"); 575 for (Symbol *sym : file->getSymbols()) 576 if (sym->isLocal() && !isa<SectionSymbol>(sym) && sym->isLive()) 577 out.linkingSec->addToSymtab(sym); 578 } 579 } 580 581 void Writer::calculateTypes() { 582 // The output type section is the union of the following sets: 583 // 1. Any signature used in the TYPE relocation 584 // 2. The signatures of all imported functions 585 // 3. The signatures of all defined functions 586 // 4. The signatures of all imported events 587 // 5. The signatures of all defined events 588 589 for (ObjFile *file : symtab->objectFiles) { 590 ArrayRef<WasmSignature> types = file->getWasmObj()->types(); 591 for (uint32_t i = 0; i < types.size(); i++) 592 if (file->typeIsUsed[i]) 593 file->typeMap[i] = out.typeSec->registerType(types[i]); 594 } 595 596 for (const Symbol *sym : out.importSec->importedSymbols) { 597 if (auto *f = dyn_cast<FunctionSymbol>(sym)) 598 out.typeSec->registerType(*f->signature); 599 else if (auto *e = dyn_cast<EventSymbol>(sym)) 600 out.typeSec->registerType(*e->signature); 601 } 602 603 for (const InputFunction *f : out.functionSec->inputFunctions) 604 out.typeSec->registerType(f->signature); 605 606 for (const InputEvent *e : out.eventSec->inputEvents) 607 out.typeSec->registerType(e->signature); 608 } 609 610 static void scanRelocations() { 611 for (ObjFile *file : symtab->objectFiles) { 612 LLVM_DEBUG(dbgs() << "scanRelocations: " << file->getName() << "\n"); 613 for (InputChunk *chunk : file->functions) 614 scanRelocations(chunk); 615 for (InputChunk *chunk : file->segments) 616 scanRelocations(chunk); 617 for (auto &p : file->customSections) 618 scanRelocations(p); 619 } 620 } 621 622 void Writer::assignIndexes() { 623 // Seal the import section, since other index spaces such as function and 624 // global are effected by the number of imports. 625 out.importSec->seal(); 626 627 for (InputFunction *func : symtab->syntheticFunctions) 628 out.functionSec->addFunction(func); 629 630 for (ObjFile *file : symtab->objectFiles) { 631 LLVM_DEBUG(dbgs() << "Functions: " << file->getName() << "\n"); 632 for (InputFunction *func : file->functions) 633 out.functionSec->addFunction(func); 634 } 635 636 for (InputGlobal *global : symtab->syntheticGlobals) 637 out.globalSec->addGlobal(global); 638 639 for (ObjFile *file : symtab->objectFiles) { 640 LLVM_DEBUG(dbgs() << "Globals: " << file->getName() << "\n"); 641 for (InputGlobal *global : file->globals) 642 out.globalSec->addGlobal(global); 643 } 644 645 for (ObjFile *file : symtab->objectFiles) { 646 LLVM_DEBUG(dbgs() << "Events: " << file->getName() << "\n"); 647 for (InputEvent *event : file->events) 648 out.eventSec->addEvent(event); 649 } 650 651 out.globalSec->assignIndexes(); 652 } 653 654 static StringRef getOutputDataSegmentName(StringRef name) { 655 // With PIC code we currently only support a single data segment since 656 // we only have a single __memory_base to use as our base address. 657 if (config->isPic) 658 return ".data"; 659 // We only support one thread-local segment, so we must merge the segments 660 // despite --no-merge-data-segments. 661 // We also need to merge .tbss into .tdata so they share the same offsets. 662 if (name.startswith(".tdata") || name.startswith(".tbss")) 663 return ".tdata"; 664 if (!config->mergeDataSegments) 665 return name; 666 if (name.startswith(".text.")) 667 return ".text"; 668 if (name.startswith(".data.")) 669 return ".data"; 670 if (name.startswith(".bss.")) 671 return ".bss"; 672 if (name.startswith(".rodata.")) 673 return ".rodata"; 674 return name; 675 } 676 677 void Writer::createOutputSegments() { 678 for (ObjFile *file : symtab->objectFiles) { 679 for (InputSegment *segment : file->segments) { 680 if (!segment->live) 681 continue; 682 StringRef name = getOutputDataSegmentName(segment->getName()); 683 OutputSegment *&s = segmentMap[name]; 684 if (s == nullptr) { 685 LLVM_DEBUG(dbgs() << "new segment: " << name << "\n"); 686 s = make<OutputSegment>(name); 687 if (config->sharedMemory || name == ".tdata") 688 s->initFlags = WASM_SEGMENT_IS_PASSIVE; 689 // Exported memories are guaranteed to be zero-initialized, so no need 690 // to emit data segments for bss sections. 691 // TODO: consider initializing bss sections with memory.fill 692 // instructions when memory is imported and bulk-memory is available. 693 if (!config->importMemory && !config->relocatable && 694 name.startswith(".bss")) 695 s->isBss = true; 696 segments.push_back(s); 697 } 698 s->addInputSegment(segment); 699 LLVM_DEBUG(dbgs() << "added data: " << name << ": " << s->size << "\n"); 700 } 701 } 702 703 // Sort segments by type, placing .bss last 704 std::stable_sort(segments.begin(), segments.end(), 705 [](const OutputSegment *a, const OutputSegment *b) { 706 auto order = [](StringRef name) { 707 return StringSwitch<int>(name) 708 .StartsWith(".rodata", 0) 709 .StartsWith(".data", 1) 710 .StartsWith(".tdata", 2) 711 .StartsWith(".bss", 4) 712 .Default(3); 713 }; 714 return order(a->name) < order(b->name); 715 }); 716 717 for (size_t i = 0; i < segments.size(); ++i) 718 segments[i]->index = i; 719 } 720 721 static void createFunction(DefinedFunction *func, StringRef bodyContent) { 722 std::string functionBody; 723 { 724 raw_string_ostream os(functionBody); 725 writeUleb128(os, bodyContent.size(), "function size"); 726 os << bodyContent; 727 } 728 ArrayRef<uint8_t> body = arrayRefFromStringRef(saver.save(functionBody)); 729 cast<SyntheticFunction>(func->function)->setBody(body); 730 } 731 732 void Writer::createInitMemoryFunction() { 733 LLVM_DEBUG(dbgs() << "createInitMemoryFunction\n"); 734 assert(WasmSym::initMemoryFlag); 735 uint32_t flagAddress = WasmSym::initMemoryFlag->getVirtualAddress(); 736 std::string bodyContent; 737 { 738 raw_string_ostream os(bodyContent); 739 writeUleb128(os, 0, "num locals"); 740 741 if (segments.size()) { 742 // Initialize memory in a thread-safe manner. The thread that successfully 743 // increments the flag from 0 to 1 is is responsible for performing the 744 // memory initialization. Other threads go sleep on the flag until the 745 // first thread finishing initializing memory, increments the flag to 2, 746 // and wakes all the other threads. Once the flag has been set to 2, 747 // subsequently started threads will skip the sleep. All threads 748 // unconditionally drop their passive data segments once memory has been 749 // initialized. The generated code is as follows: 750 // 751 // (func $__wasm_init_memory 752 // (if 753 // (i32.atomic.rmw.cmpxchg align=2 offset=0 754 // (i32.const $__init_memory_flag) 755 // (i32.const 0) 756 // (i32.const 1) 757 // ) 758 // (then 759 // (drop 760 // (i32.atomic.wait align=2 offset=0 761 // (i32.const $__init_memory_flag) 762 // (i32.const 1) 763 // (i32.const -1) 764 // ) 765 // ) 766 // ) 767 // (else 768 // ( ... initialize data segments ... ) 769 // (i32.atomic.store align=2 offset=0 770 // (i32.const $__init_memory_flag) 771 // (i32.const 2) 772 // ) 773 // (drop 774 // (i32.atomic.notify align=2 offset=0 775 // (i32.const $__init_memory_flag) 776 // (i32.const -1u) 777 // ) 778 // ) 779 // ) 780 // ) 781 // ( ... drop data segments ... ) 782 // ) 783 784 // Atomically check whether this is the main thread. 785 writeI32Const(os, flagAddress, "flag address"); 786 writeI32Const(os, 0, "expected flag value"); 787 writeI32Const(os, 1, "flag value"); 788 writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix"); 789 writeUleb128(os, WASM_OPCODE_I32_RMW_CMPXCHG, "i32.atomic.rmw.cmpxchg"); 790 writeMemArg(os, 2, 0); 791 writeU8(os, WASM_OPCODE_IF, "IF"); 792 writeU8(os, WASM_TYPE_NORESULT, "blocktype"); 793 794 // Did not increment 0, so wait for main thread to initialize memory 795 writeI32Const(os, flagAddress, "flag address"); 796 writeI32Const(os, 1, "expected flag value"); 797 writeI64Const(os, -1, "timeout"); 798 writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix"); 799 writeUleb128(os, WASM_OPCODE_I32_ATOMIC_WAIT, "i32.atomic.wait"); 800 writeMemArg(os, 2, 0); 801 writeU8(os, WASM_OPCODE_DROP, "drop"); 802 803 writeU8(os, WASM_OPCODE_ELSE, "ELSE"); 804 805 // Did increment 0, so conditionally initialize passive data segments 806 for (const OutputSegment *s : segments) { 807 if (s->initFlags & WASM_SEGMENT_IS_PASSIVE && s->name != ".tdata") { 808 // destination address 809 writeI32Const(os, s->startVA, "destination address"); 810 // source segment offset 811 writeI32Const(os, 0, "segment offset"); 812 // memory region size 813 writeI32Const(os, s->size, "memory region size"); 814 // memory.init instruction 815 writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix"); 816 writeUleb128(os, WASM_OPCODE_MEMORY_INIT, "memory.init"); 817 writeUleb128(os, s->index, "segment index immediate"); 818 writeU8(os, 0, "memory index immediate"); 819 } 820 } 821 822 // Set flag to 2 to mark end of initialization 823 writeI32Const(os, flagAddress, "flag address"); 824 writeI32Const(os, 2, "flag value"); 825 writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix"); 826 writeUleb128(os, WASM_OPCODE_I32_ATOMIC_STORE, "i32.atomic.store"); 827 writeMemArg(os, 2, 0); 828 829 // Notify any waiters that memory initialization is complete 830 writeI32Const(os, flagAddress, "flag address"); 831 writeI32Const(os, -1, "number of waiters"); 832 writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix"); 833 writeUleb128(os, WASM_OPCODE_ATOMIC_NOTIFY, "atomic.notify"); 834 writeMemArg(os, 2, 0); 835 writeU8(os, WASM_OPCODE_DROP, "drop"); 836 837 writeU8(os, WASM_OPCODE_END, "END"); 838 839 // Unconditionally drop passive data segments 840 for (const OutputSegment *s : segments) { 841 if (s->initFlags & WASM_SEGMENT_IS_PASSIVE && s->name != ".tdata") { 842 // data.drop instruction 843 writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix"); 844 writeUleb128(os, WASM_OPCODE_DATA_DROP, "data.drop"); 845 writeUleb128(os, s->index, "segment index immediate"); 846 } 847 } 848 } 849 writeU8(os, WASM_OPCODE_END, "END"); 850 } 851 852 createFunction(WasmSym::initMemory, bodyContent); 853 } 854 855 // For -shared (PIC) output, we create create a synthetic function which will 856 // apply any relocations to the data segments on startup. This function is 857 // called __wasm_apply_relocs and is added at the beginning of __wasm_call_ctors 858 // before any of the constructors run. 859 void Writer::createApplyRelocationsFunction() { 860 LLVM_DEBUG(dbgs() << "createApplyRelocationsFunction\n"); 861 // First write the body's contents to a string. 862 std::string bodyContent; 863 { 864 raw_string_ostream os(bodyContent); 865 writeUleb128(os, 0, "num locals"); 866 for (const OutputSegment *seg : segments) 867 for (const InputSegment *inSeg : seg->inputSegments) 868 inSeg->generateRelocationCode(os); 869 writeU8(os, WASM_OPCODE_END, "END"); 870 } 871 872 createFunction(WasmSym::applyRelocs, bodyContent); 873 } 874 875 // Create synthetic "__wasm_call_ctors" function based on ctor functions 876 // in input object. 877 void Writer::createCallCtorsFunction() { 878 if (!WasmSym::callCtors->isLive()) 879 return; 880 881 // First write the body's contents to a string. 882 std::string bodyContent; 883 { 884 raw_string_ostream os(bodyContent); 885 writeUleb128(os, 0, "num locals"); 886 887 if (config->isPic) { 888 writeU8(os, WASM_OPCODE_CALL, "CALL"); 889 writeUleb128(os, WasmSym::applyRelocs->getFunctionIndex(), 890 "function index"); 891 } 892 893 // Call constructors 894 for (const WasmInitEntry &f : initFunctions) { 895 writeU8(os, WASM_OPCODE_CALL, "CALL"); 896 writeUleb128(os, f.sym->getFunctionIndex(), "function index"); 897 } 898 writeU8(os, WASM_OPCODE_END, "END"); 899 } 900 901 createFunction(WasmSym::callCtors, bodyContent); 902 } 903 904 void Writer::createInitTLSFunction() { 905 if (!WasmSym::initTLS->isLive()) 906 return; 907 908 std::string bodyContent; 909 { 910 raw_string_ostream os(bodyContent); 911 912 OutputSegment *tlsSeg = nullptr; 913 for (auto *seg : segments) { 914 if (seg->name == ".tdata") { 915 tlsSeg = seg; 916 break; 917 } 918 } 919 920 writeUleb128(os, 0, "num locals"); 921 if (tlsSeg) { 922 writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get"); 923 writeUleb128(os, 0, "local index"); 924 925 writeU8(os, WASM_OPCODE_GLOBAL_SET, "global.set"); 926 writeUleb128(os, WasmSym::tlsBase->getGlobalIndex(), "global index"); 927 928 writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get"); 929 writeUleb128(os, 0, "local index"); 930 931 writeI32Const(os, 0, "segment offset"); 932 933 writeI32Const(os, tlsSeg->size, "memory region size"); 934 935 writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix"); 936 writeUleb128(os, WASM_OPCODE_MEMORY_INIT, "MEMORY.INIT"); 937 writeUleb128(os, tlsSeg->index, "segment index immediate"); 938 writeU8(os, 0, "memory index immediate"); 939 } 940 writeU8(os, WASM_OPCODE_END, "end function"); 941 } 942 943 createFunction(WasmSym::initTLS, bodyContent); 944 } 945 946 // Populate InitFunctions vector with init functions from all input objects. 947 // This is then used either when creating the output linking section or to 948 // synthesize the "__wasm_call_ctors" function. 949 void Writer::calculateInitFunctions() { 950 if (!config->relocatable && !WasmSym::callCtors->isLive()) 951 return; 952 953 for (ObjFile *file : symtab->objectFiles) { 954 const WasmLinkingData &l = file->getWasmObj()->linkingData(); 955 for (const WasmInitFunc &f : l.InitFunctions) { 956 FunctionSymbol *sym = file->getFunctionSymbol(f.Symbol); 957 // comdat exclusions can cause init functions be discarded. 958 if (sym->isDiscarded()) 959 continue; 960 assert(sym->isLive()); 961 if (*sym->signature != WasmSignature{{}, {}}) 962 error("invalid signature for init func: " + toString(*sym)); 963 LLVM_DEBUG(dbgs() << "initFunctions: " << toString(*sym) << "\n"); 964 initFunctions.emplace_back(WasmInitEntry{sym, f.Priority}); 965 } 966 } 967 968 // Sort in order of priority (lowest first) so that they are called 969 // in the correct order. 970 llvm::stable_sort(initFunctions, 971 [](const WasmInitEntry &l, const WasmInitEntry &r) { 972 return l.priority < r.priority; 973 }); 974 } 975 976 void Writer::createSyntheticSections() { 977 out.dylinkSec = make<DylinkSection>(); 978 out.typeSec = make<TypeSection>(); 979 out.importSec = make<ImportSection>(); 980 out.functionSec = make<FunctionSection>(); 981 out.tableSec = make<TableSection>(); 982 out.memorySec = make<MemorySection>(); 983 out.eventSec = make<EventSection>(); 984 out.globalSec = make<GlobalSection>(); 985 out.exportSec = make<ExportSection>(); 986 out.startSec = make<StartSection>(segments.size()); 987 out.elemSec = make<ElemSection>(); 988 out.dataCountSec = make<DataCountSection>(segments); 989 out.linkingSec = make<LinkingSection>(initFunctions, segments); 990 out.nameSec = make<NameSection>(); 991 out.producersSec = make<ProducersSection>(); 992 out.targetFeaturesSec = make<TargetFeaturesSection>(); 993 } 994 995 void Writer::run() { 996 if (config->relocatable || config->isPic) 997 config->globalBase = 0; 998 999 // For PIC code the table base is assigned dynamically by the loader. 1000 // For non-PIC, we start at 1 so that accessing table index 0 always traps. 1001 if (!config->isPic) { 1002 config->tableBase = 1; 1003 if (WasmSym::definedTableBase) 1004 WasmSym::definedTableBase->setVirtualAddress(config->tableBase); 1005 } 1006 1007 log("-- createOutputSegments"); 1008 createOutputSegments(); 1009 log("-- createSyntheticSections"); 1010 createSyntheticSections(); 1011 log("-- populateProducers"); 1012 populateProducers(); 1013 log("-- populateTargetFeatures"); 1014 populateTargetFeatures(); 1015 log("-- calculateImports"); 1016 calculateImports(); 1017 log("-- layoutMemory"); 1018 layoutMemory(); 1019 1020 if (!config->relocatable) { 1021 // Create linker synthesized __start_SECNAME/__stop_SECNAME symbols 1022 // This has to be done after memory layout is performed. 1023 for (const OutputSegment *seg : segments) 1024 addStartStopSymbols(seg); 1025 } 1026 1027 log("-- scanRelocations"); 1028 scanRelocations(); 1029 log("-- assignIndexes"); 1030 assignIndexes(); 1031 log("-- calculateInitFunctions"); 1032 calculateInitFunctions(); 1033 1034 if (!config->relocatable) { 1035 // Create linker synthesized functions 1036 if (config->sharedMemory) 1037 createInitMemoryFunction(); 1038 if (config->isPic) 1039 createApplyRelocationsFunction(); 1040 createCallCtorsFunction(); 1041 } 1042 1043 if (!config->relocatable && config->sharedMemory && !config->shared) 1044 createInitTLSFunction(); 1045 1046 if (errorCount()) 1047 return; 1048 1049 log("-- calculateTypes"); 1050 calculateTypes(); 1051 log("-- calculateExports"); 1052 calculateExports(); 1053 log("-- calculateCustomSections"); 1054 calculateCustomSections(); 1055 log("-- populateSymtab"); 1056 populateSymtab(); 1057 log("-- addSections"); 1058 addSections(); 1059 1060 if (errorHandler().verbose) { 1061 log("Defined Functions: " + Twine(out.functionSec->inputFunctions.size())); 1062 log("Defined Globals : " + Twine(out.globalSec->numGlobals())); 1063 log("Defined Events : " + Twine(out.eventSec->inputEvents.size())); 1064 log("Function Imports : " + 1065 Twine(out.importSec->getNumImportedFunctions())); 1066 log("Global Imports : " + Twine(out.importSec->getNumImportedGlobals())); 1067 log("Event Imports : " + Twine(out.importSec->getNumImportedEvents())); 1068 for (ObjFile *file : symtab->objectFiles) 1069 file->dumpInfo(); 1070 } 1071 1072 createHeader(); 1073 log("-- finalizeSections"); 1074 finalizeSections(); 1075 1076 log("-- openFile"); 1077 openFile(); 1078 if (errorCount()) 1079 return; 1080 1081 writeHeader(); 1082 1083 log("-- writeSections"); 1084 writeSections(); 1085 if (errorCount()) 1086 return; 1087 1088 if (Error e = buffer->commit()) 1089 fatal("failed to write the output file: " + toString(std::move(e))); 1090 } 1091 1092 // Open a result file. 1093 void Writer::openFile() { 1094 log("writing: " + config->outputFile); 1095 1096 Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr = 1097 FileOutputBuffer::create(config->outputFile, fileSize, 1098 FileOutputBuffer::F_executable); 1099 1100 if (!bufferOrErr) 1101 error("failed to open " + config->outputFile + ": " + 1102 toString(bufferOrErr.takeError())); 1103 else 1104 buffer = std::move(*bufferOrErr); 1105 } 1106 1107 void Writer::createHeader() { 1108 raw_string_ostream os(header); 1109 writeBytes(os, WasmMagic, sizeof(WasmMagic), "wasm magic"); 1110 writeU32(os, WasmVersion, "wasm version"); 1111 os.flush(); 1112 fileSize += header.size(); 1113 } 1114 1115 void writeResult() { Writer().run(); } 1116 1117 } // namespace wasm 1118 } // namespace lld 1119