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