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