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