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