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