1 //===- Writer.cpp ---------------------------------------------------------===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "Writer.h" 11 12 #include "llvm/ADT/DenseSet.h" 13 #include "Config.h" 14 #include "InputChunks.h" 15 #include "OutputSections.h" 16 #include "OutputSegment.h" 17 #include "SymbolTable.h" 18 #include "WriterUtils.h" 19 #include "lld/Common/ErrorHandler.h" 20 #include "lld/Common/Memory.h" 21 #include "lld/Common/Threads.h" 22 #include "llvm/Support/FileOutputBuffer.h" 23 #include "llvm/Support/Format.h" 24 #include "llvm/Support/FormatVariadic.h" 25 #include "llvm/Support/LEB128.h" 26 27 #include <cstdarg> 28 #include <map> 29 30 #define DEBUG_TYPE "lld" 31 32 using namespace llvm; 33 using namespace llvm::wasm; 34 using namespace lld; 35 using namespace lld::wasm; 36 37 static constexpr int kStackAlignment = 16; 38 static constexpr int kInitialTableOffset = 1; 39 40 namespace { 41 42 // Traits for using WasmSignature in a DenseMap. 43 struct WasmSignatureDenseMapInfo { 44 static WasmSignature getEmptyKey() { 45 WasmSignature Sig; 46 Sig.ReturnType = 1; 47 return Sig; 48 } 49 static WasmSignature getTombstoneKey() { 50 WasmSignature Sig; 51 Sig.ReturnType = 2; 52 return Sig; 53 } 54 static unsigned getHashValue(const WasmSignature &Sig) { 55 uintptr_t Value = 0; 56 Value += DenseMapInfo<int32_t>::getHashValue(Sig.ReturnType); 57 for (int32_t Param : Sig.ParamTypes) 58 Value += DenseMapInfo<int32_t>::getHashValue(Param); 59 return Value; 60 } 61 static bool isEqual(const WasmSignature &LHS, const WasmSignature &RHS) { 62 return LHS == RHS; 63 } 64 }; 65 66 // A Wasm export to be written into the export section. 67 struct WasmExportEntry { 68 const Symbol *Sym; 69 StringRef FieldName; // may not match the Symbol name 70 }; 71 72 // The writer writes a SymbolTable result to a file. 73 class Writer { 74 public: 75 void run(); 76 77 private: 78 void openFile(); 79 80 uint32_t lookupType(const WasmSignature &Sig); 81 uint32_t registerType(const WasmSignature &Sig); 82 void createCtorFunction(); 83 void calculateInitFunctions(); 84 void assignIndexes(); 85 void calculateImports(); 86 void calculateExports(); 87 void calculateTypes(); 88 void createOutputSegments(); 89 void layoutMemory(); 90 void createHeader(); 91 void createSections(); 92 SyntheticSection *createSyntheticSection(uint32_t Type, 93 StringRef Name = ""); 94 95 // Builtin sections 96 void createTypeSection(); 97 void createFunctionSection(); 98 void createTableSection(); 99 void createGlobalSection(); 100 void createExportSection(); 101 void createImportSection(); 102 void createMemorySection(); 103 void createElemSection(); 104 void createStartSection(); 105 void createCodeSection(); 106 void createDataSection(); 107 108 // Custom sections 109 void createRelocSections(); 110 void createLinkingSection(); 111 void createNameSection(); 112 113 void writeHeader(); 114 void writeSections(); 115 116 uint64_t FileSize = 0; 117 uint32_t DataSize = 0; 118 uint32_t NumMemoryPages = 0; 119 120 std::vector<const WasmSignature *> Types; 121 DenseMap<WasmSignature, int32_t, WasmSignatureDenseMapInfo> TypeIndices; 122 std::vector<const Symbol *> ImportedFunctions; 123 std::vector<const Symbol *> ImportedGlobals; 124 std::vector<WasmExportEntry> ExportedSymbols; 125 std::vector<const Symbol *> DefinedGlobals; 126 std::vector<InputFunction *> DefinedFunctions; 127 std::vector<const Symbol *> IndirectFunctions; 128 std::vector<WasmInitFunc> InitFunctions; 129 130 // Elements that are used to construct the final output 131 std::string Header; 132 std::vector<OutputSection *> OutputSections; 133 134 std::unique_ptr<FileOutputBuffer> Buffer; 135 std::unique_ptr<SyntheticFunction> CtorFunction; 136 std::string CtorFunctionBody; 137 138 std::vector<OutputSegment *> Segments; 139 llvm::SmallDenseMap<StringRef, OutputSegment *> SegmentMap; 140 }; 141 142 } // anonymous namespace 143 144 static void debugPrint(const char *fmt, ...) { 145 if (!errorHandler().Verbose) 146 return; 147 fprintf(stderr, "lld: "); 148 va_list ap; 149 va_start(ap, fmt); 150 vfprintf(stderr, fmt, ap); 151 va_end(ap); 152 } 153 154 void Writer::createImportSection() { 155 uint32_t NumImports = ImportedFunctions.size() + ImportedGlobals.size(); 156 if (Config->ImportMemory) 157 ++NumImports; 158 159 if (NumImports == 0) 160 return; 161 162 SyntheticSection *Section = createSyntheticSection(WASM_SEC_IMPORT); 163 raw_ostream &OS = Section->getStream(); 164 165 writeUleb128(OS, NumImports, "import count"); 166 167 for (const Symbol *Sym : ImportedFunctions) { 168 WasmImport Import; 169 Import.Module = "env"; 170 Import.Field = Sym->getName(); 171 Import.Kind = WASM_EXTERNAL_FUNCTION; 172 Import.SigIndex = lookupType(Sym->getFunctionType()); 173 writeImport(OS, Import); 174 } 175 176 if (Config->ImportMemory) { 177 WasmImport Import; 178 Import.Module = "env"; 179 Import.Field = "memory"; 180 Import.Kind = WASM_EXTERNAL_MEMORY; 181 Import.Memory.Flags = 0; 182 Import.Memory.Initial = NumMemoryPages; 183 writeImport(OS, Import); 184 } 185 186 for (const Symbol *Sym : ImportedGlobals) { 187 WasmImport Import; 188 Import.Module = "env"; 189 Import.Field = Sym->getName(); 190 Import.Kind = WASM_EXTERNAL_GLOBAL; 191 Import.Global.Mutable = false; 192 Import.Global.Type = WASM_TYPE_I32; 193 writeImport(OS, Import); 194 } 195 } 196 197 void Writer::createTypeSection() { 198 SyntheticSection *Section = createSyntheticSection(WASM_SEC_TYPE); 199 raw_ostream &OS = Section->getStream(); 200 writeUleb128(OS, Types.size(), "type count"); 201 for (const WasmSignature *Sig : Types) 202 writeSig(OS, *Sig); 203 } 204 205 void Writer::createFunctionSection() { 206 if (DefinedFunctions.empty()) 207 return; 208 209 SyntheticSection *Section = createSyntheticSection(WASM_SEC_FUNCTION); 210 raw_ostream &OS = Section->getStream(); 211 212 writeUleb128(OS, DefinedFunctions.size(), "function count"); 213 for (const InputFunction *Func : DefinedFunctions) 214 writeUleb128(OS, lookupType(Func->Signature), "sig index"); 215 } 216 217 void Writer::createMemorySection() { 218 if (Config->ImportMemory) 219 return; 220 221 SyntheticSection *Section = createSyntheticSection(WASM_SEC_MEMORY); 222 raw_ostream &OS = Section->getStream(); 223 224 writeUleb128(OS, 1, "memory count"); 225 writeUleb128(OS, 0, "memory limits flags"); 226 writeUleb128(OS, NumMemoryPages, "initial pages"); 227 } 228 229 void Writer::createGlobalSection() { 230 if (DefinedGlobals.empty()) 231 return; 232 233 SyntheticSection *Section = createSyntheticSection(WASM_SEC_GLOBAL); 234 raw_ostream &OS = Section->getStream(); 235 236 writeUleb128(OS, DefinedGlobals.size(), "global count"); 237 for (const Symbol *Sym : DefinedGlobals) { 238 WasmGlobal Global; 239 Global.Type = WASM_TYPE_I32; 240 Global.Mutable = Sym == Config->StackPointerSymbol; 241 Global.InitExpr.Opcode = WASM_OPCODE_I32_CONST; 242 Global.InitExpr.Value.Int32 = Sym->getVirtualAddress(); 243 writeGlobal(OS, Global); 244 } 245 } 246 247 void Writer::createTableSection() { 248 // Always output a table section, even if there are no indirect calls. 249 // There are two reasons for this: 250 // 1. For executables it is useful to have an empty table slot at 0 251 // which can be filled with a null function call handler. 252 // 2. If we don't do this, any program that contains a call_indirect but 253 // no address-taken function will fail at validation time since it is 254 // a validation error to include a call_indirect instruction if there 255 // is not table. 256 uint32_t TableSize = kInitialTableOffset + IndirectFunctions.size(); 257 258 SyntheticSection *Section = createSyntheticSection(WASM_SEC_TABLE); 259 raw_ostream &OS = Section->getStream(); 260 261 writeUleb128(OS, 1, "table count"); 262 writeSleb128(OS, WASM_TYPE_ANYFUNC, "table type"); 263 writeUleb128(OS, WASM_LIMITS_FLAG_HAS_MAX, "table flags"); 264 writeUleb128(OS, TableSize, "table initial size"); 265 writeUleb128(OS, TableSize, "table max size"); 266 } 267 268 void Writer::createExportSection() { 269 bool ExportMemory = !Config->Relocatable && !Config->ImportMemory; 270 271 uint32_t NumExports = (ExportMemory ? 1 : 0) + ExportedSymbols.size(); 272 if (!NumExports) 273 return; 274 275 SyntheticSection *Section = createSyntheticSection(WASM_SEC_EXPORT); 276 raw_ostream &OS = Section->getStream(); 277 278 writeUleb128(OS, NumExports, "export count"); 279 280 if (ExportMemory) { 281 WasmExport MemoryExport; 282 MemoryExport.Name = "memory"; 283 MemoryExport.Kind = WASM_EXTERNAL_MEMORY; 284 MemoryExport.Index = 0; 285 writeExport(OS, MemoryExport); 286 } 287 288 for (const WasmExportEntry &E : ExportedSymbols) { 289 DEBUG(dbgs() << "Export: " << E.Sym->getName() << "\n"); 290 WasmExport Export; 291 Export.Name = E.FieldName; 292 Export.Index = E.Sym->getOutputIndex(); 293 if (E.Sym->isFunction()) 294 Export.Kind = WASM_EXTERNAL_FUNCTION; 295 else 296 Export.Kind = WASM_EXTERNAL_GLOBAL; 297 writeExport(OS, Export); 298 } 299 } 300 301 void Writer::createStartSection() {} 302 303 void Writer::createElemSection() { 304 if (IndirectFunctions.empty()) 305 return; 306 307 SyntheticSection *Section = createSyntheticSection(WASM_SEC_ELEM); 308 raw_ostream &OS = Section->getStream(); 309 310 writeUleb128(OS, 1, "segment count"); 311 writeUleb128(OS, 0, "table index"); 312 WasmInitExpr InitExpr; 313 InitExpr.Opcode = WASM_OPCODE_I32_CONST; 314 InitExpr.Value.Int32 = kInitialTableOffset; 315 writeInitExpr(OS, InitExpr); 316 writeUleb128(OS, IndirectFunctions.size(), "elem count"); 317 318 uint32_t TableIndex = kInitialTableOffset; 319 for (const Symbol *Sym : IndirectFunctions) { 320 assert(Sym->getTableIndex() == TableIndex); 321 writeUleb128(OS, Sym->getOutputIndex(), "function index"); 322 ++TableIndex; 323 } 324 } 325 326 void Writer::createCodeSection() { 327 if (DefinedFunctions.empty()) 328 return; 329 330 log("createCodeSection"); 331 332 auto Section = make<CodeSection>(DefinedFunctions); 333 OutputSections.push_back(Section); 334 } 335 336 void Writer::createDataSection() { 337 if (!Segments.size()) 338 return; 339 340 log("createDataSection"); 341 auto Section = make<DataSection>(Segments); 342 OutputSections.push_back(Section); 343 } 344 345 // Create relocations sections in the final output. 346 // These are only created when relocatable output is requested. 347 void Writer::createRelocSections() { 348 log("createRelocSections"); 349 // Don't use iterator here since we are adding to OutputSection 350 size_t OrigSize = OutputSections.size(); 351 for (size_t i = 0; i < OrigSize; i++) { 352 OutputSection *S = OutputSections[i]; 353 const char *name; 354 uint32_t Count = S->numRelocations(); 355 if (!Count) 356 continue; 357 358 if (S->Type == WASM_SEC_DATA) 359 name = "reloc.DATA"; 360 else if (S->Type == WASM_SEC_CODE) 361 name = "reloc.CODE"; 362 else 363 llvm_unreachable("relocations only supported for code and data"); 364 365 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, name); 366 raw_ostream &OS = Section->getStream(); 367 writeUleb128(OS, S->Type, "reloc section"); 368 writeUleb128(OS, Count, "reloc count"); 369 S->writeRelocations(OS); 370 } 371 } 372 373 // Create the custom "linking" section containing linker metadata. 374 // This is only created when relocatable output is requested. 375 void Writer::createLinkingSection() { 376 SyntheticSection *Section = 377 createSyntheticSection(WASM_SEC_CUSTOM, "linking"); 378 raw_ostream &OS = Section->getStream(); 379 380 SubSection DataSizeSubSection(WASM_DATA_SIZE); 381 writeUleb128(DataSizeSubSection.getStream(), DataSize, "data size"); 382 DataSizeSubSection.finalizeContents(); 383 DataSizeSubSection.writeToStream(OS); 384 385 if (!Config->Relocatable) 386 return; 387 388 std::vector<std::pair<StringRef, uint32_t>> SymbolInfo; 389 auto addSymInfo = [&](const Symbol *Sym, StringRef ExternalName) { 390 uint32_t Flags = 391 (Sym->isLocal() ? WASM_SYMBOL_BINDING_LOCAL : 392 Sym->isWeak() ? WASM_SYMBOL_BINDING_WEAK : 0) | 393 (Sym->isHidden() ? WASM_SYMBOL_VISIBILITY_HIDDEN : 0); 394 if (Flags) 395 SymbolInfo.emplace_back(ExternalName, Flags); 396 }; 397 // (Imports can't have internal linkage, their names don't need to be budged.) 398 for (const Symbol *Sym : ImportedFunctions) 399 addSymInfo(Sym, Sym->getName()); 400 for (const Symbol *Sym : ImportedGlobals) 401 addSymInfo(Sym, Sym->getName()); 402 for (const WasmExportEntry &E : ExportedSymbols) 403 addSymInfo(E.Sym, E.FieldName); 404 if (!SymbolInfo.empty()) { 405 SubSection SubSection(WASM_SYMBOL_INFO); 406 writeUleb128(SubSection.getStream(), SymbolInfo.size(), "num sym info"); 407 for (auto Pair: SymbolInfo) { 408 writeStr(SubSection.getStream(), Pair.first, "sym name"); 409 writeUleb128(SubSection.getStream(), Pair.second, "sym flags"); 410 } 411 SubSection.finalizeContents(); 412 SubSection.writeToStream(OS); 413 } 414 415 if (Segments.size()) { 416 SubSection SubSection(WASM_SEGMENT_INFO); 417 writeUleb128(SubSection.getStream(), Segments.size(), "num data segments"); 418 for (const OutputSegment *S : Segments) { 419 writeStr(SubSection.getStream(), S->Name, "segment name"); 420 writeUleb128(SubSection.getStream(), S->Alignment, "alignment"); 421 writeUleb128(SubSection.getStream(), 0, "flags"); 422 } 423 SubSection.finalizeContents(); 424 SubSection.writeToStream(OS); 425 } 426 427 if (!InitFunctions.empty()) { 428 SubSection SubSection(WASM_INIT_FUNCS); 429 writeUleb128(SubSection.getStream(), InitFunctions.size(), 430 "num init functions"); 431 for (const WasmInitFunc &F : InitFunctions) { 432 writeUleb128(SubSection.getStream(), F.Priority, "priority"); 433 writeUleb128(SubSection.getStream(), F.FunctionIndex, "function index"); 434 } 435 SubSection.finalizeContents(); 436 SubSection.writeToStream(OS); 437 } 438 439 struct ComdatEntry { unsigned Kind; uint32_t Index; }; 440 std::map<StringRef,std::vector<ComdatEntry>> Comdats; 441 442 for (const InputFunction *F : DefinedFunctions) { 443 StringRef Comdat = F->getComdat(); 444 if (!Comdat.empty()) 445 Comdats[Comdat].emplace_back( 446 ComdatEntry{WASM_COMDAT_FUNCTION, F->getOutputIndex()}); 447 } 448 for (uint32_t I = 0; I < Segments.size(); ++I) { 449 const auto &InputSegments = Segments[I]->InputSegments; 450 if (InputSegments.empty()) 451 continue; 452 StringRef Comdat = InputSegments[0]->getComdat(); 453 #ifndef NDEBUG 454 for (const InputSegment *IS : InputSegments) 455 assert(IS->getComdat() == Comdat); 456 #endif 457 if (!Comdat.empty()) 458 Comdats[Comdat].emplace_back(ComdatEntry{WASM_COMDAT_DATA, I}); 459 } 460 461 if (!Comdats.empty()) { 462 SubSection SubSection(WASM_COMDAT_INFO); 463 writeUleb128(SubSection.getStream(), Comdats.size(), "num comdats"); 464 for (const auto &C : Comdats) { 465 writeStr(SubSection.getStream(), C.first, "comdat name"); 466 writeUleb128(SubSection.getStream(), 0, "comdat flags"); // flags for future use 467 writeUleb128(SubSection.getStream(), C.second.size(), "num entries"); 468 for (const ComdatEntry &Entry : C.second) { 469 writeUleb128(SubSection.getStream(), Entry.Kind, "entry kind"); 470 writeUleb128(SubSection.getStream(), Entry.Index, "entry index"); 471 } 472 } 473 SubSection.finalizeContents(); 474 SubSection.writeToStream(OS); 475 } 476 } 477 478 // Create the custom "name" section containing debug symbol names. 479 void Writer::createNameSection() { 480 unsigned NumNames = ImportedFunctions.size(); 481 for (const InputFunction *F : DefinedFunctions) 482 if (!F->getName().empty()) 483 ++NumNames; 484 485 if (NumNames == 0) 486 return; 487 488 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, "name"); 489 490 SubSection FunctionSubsection(WASM_NAMES_FUNCTION); 491 raw_ostream &OS = FunctionSubsection.getStream(); 492 writeUleb128(OS, NumNames, "name count"); 493 494 // Names must appear in function index order. As it happens ImportedFunctions 495 // and DefinedFunctions are numbers in order with imported functions coming 496 // first. 497 for (const Symbol *S : ImportedFunctions) { 498 writeUleb128(OS, S->getOutputIndex(), "import index"); 499 writeStr(OS, S->getName(), "symbol name"); 500 } 501 for (const InputFunction *F : DefinedFunctions) { 502 if (!F->getName().empty()) { 503 writeUleb128(OS, F->getOutputIndex(), "func index"); 504 writeStr(OS, F->getName(), "symbol name"); 505 } 506 } 507 508 FunctionSubsection.finalizeContents(); 509 FunctionSubsection.writeToStream(Section->getStream()); 510 } 511 512 void Writer::writeHeader() { 513 memcpy(Buffer->getBufferStart(), Header.data(), Header.size()); 514 } 515 516 void Writer::writeSections() { 517 uint8_t *Buf = Buffer->getBufferStart(); 518 parallelForEach(OutputSections, [Buf](OutputSection *S) { S->writeTo(Buf); }); 519 } 520 521 // Fix the memory layout of the output binary. This assigns memory offsets 522 // to each of the input data sections as well as the explicit stack region. 523 void Writer::layoutMemory() { 524 uint32_t MemoryPtr = 0; 525 if (!Config->Relocatable) { 526 MemoryPtr = Config->GlobalBase; 527 debugPrint("mem: global base = %d\n", Config->GlobalBase); 528 } 529 530 createOutputSegments(); 531 532 // Static data comes first 533 for (OutputSegment *Seg : Segments) { 534 MemoryPtr = alignTo(MemoryPtr, Seg->Alignment); 535 Seg->StartVA = MemoryPtr; 536 debugPrint("mem: %-15s offset=%-8d size=%-8d align=%d\n", 537 Seg->Name.str().c_str(), MemoryPtr, Seg->Size, Seg->Alignment); 538 MemoryPtr += Seg->Size; 539 } 540 541 DataSize = MemoryPtr; 542 if (!Config->Relocatable) 543 DataSize -= Config->GlobalBase; 544 debugPrint("mem: static data = %d\n", DataSize); 545 546 // Stack comes after static data 547 if (!Config->Relocatable) { 548 MemoryPtr = alignTo(MemoryPtr, kStackAlignment); 549 if (Config->ZStackSize != alignTo(Config->ZStackSize, kStackAlignment)) 550 error("stack size must be " + Twine(kStackAlignment) + "-byte aligned"); 551 debugPrint("mem: stack size = %d\n", Config->ZStackSize); 552 debugPrint("mem: stack base = %d\n", MemoryPtr); 553 MemoryPtr += Config->ZStackSize; 554 Config->StackPointerSymbol->setVirtualAddress(MemoryPtr); 555 debugPrint("mem: stack top = %d\n", MemoryPtr); 556 // Set `__heap_base` to directly follow the end of the stack. We don't 557 // allocate any heap memory up front, but instead really on the malloc/brk 558 // implementation growing the memory at runtime. 559 Config->HeapBaseSymbol->setVirtualAddress(MemoryPtr); 560 debugPrint("mem: heap base = %d\n", MemoryPtr); 561 } 562 563 uint32_t MemSize = alignTo(MemoryPtr, WasmPageSize); 564 NumMemoryPages = MemSize / WasmPageSize; 565 debugPrint("mem: total pages = %d\n", NumMemoryPages); 566 } 567 568 SyntheticSection *Writer::createSyntheticSection(uint32_t Type, 569 StringRef Name) { 570 auto Sec = make<SyntheticSection>(Type, Name); 571 log("createSection: " + toString(*Sec)); 572 OutputSections.push_back(Sec); 573 return Sec; 574 } 575 576 void Writer::createSections() { 577 // Known sections 578 createTypeSection(); 579 createImportSection(); 580 createFunctionSection(); 581 createTableSection(); 582 createMemorySection(); 583 createGlobalSection(); 584 createExportSection(); 585 createStartSection(); 586 createElemSection(); 587 createCodeSection(); 588 createDataSection(); 589 590 // Custom sections 591 if (Config->Relocatable) 592 createRelocSections(); 593 createLinkingSection(); 594 if (!Config->StripDebug && !Config->StripAll) 595 createNameSection(); 596 597 for (OutputSection *S : OutputSections) { 598 S->setOffset(FileSize); 599 S->finalizeContents(); 600 FileSize += S->getSize(); 601 } 602 } 603 604 void Writer::calculateImports() { 605 for (Symbol *Sym : Symtab->getSymbols()) { 606 if (!Sym->isUndefined() || (Sym->isWeak() && !Config->Relocatable)) 607 continue; 608 609 if (Sym->isFunction()) { 610 Sym->setOutputIndex(ImportedFunctions.size()); 611 ImportedFunctions.push_back(Sym); 612 } else { 613 Sym->setOutputIndex(ImportedGlobals.size()); 614 ImportedGlobals.push_back(Sym); 615 } 616 } 617 } 618 619 void Writer::calculateExports() { 620 bool ExportHidden = Config->Relocatable; 621 StringSet<> UsedNames; 622 auto BudgeLocalName = [&](const Symbol *Sym) { 623 StringRef SymName = Sym->getName(); 624 // We can't budge non-local names. 625 if (!Sym->isLocal()) 626 return SymName; 627 // We must budge local names that have a collision with a symbol that we 628 // haven't yet processed. 629 if (!Symtab->find(SymName) && UsedNames.insert(SymName).second) 630 return SymName; 631 for (unsigned I = 1; ; ++I) { 632 std::string NameBuf = (SymName + "." + Twine(I)).str(); 633 if (!UsedNames.count(NameBuf)) { 634 StringRef Name = Saver.save(NameBuf); 635 UsedNames.insert(Name); // Insert must use safe StringRef from save() 636 return Name; 637 } 638 } 639 }; 640 641 if (Config->CtorSymbol && (!Config->CtorSymbol->isHidden() || ExportHidden)) 642 ExportedSymbols.emplace_back( 643 WasmExportEntry{Config->CtorSymbol, Config->CtorSymbol->getName()}); 644 645 for (ObjFile *File : Symtab->ObjectFiles) { 646 for (Symbol *Sym : File->getSymbols()) { 647 if (!Sym->isDefined() || File != Sym->getFile()) 648 continue; 649 if (Sym->isGlobal()) 650 continue; 651 if (Sym->getFunction()->Discarded) 652 continue; 653 654 if ((Sym->isHidden() || Sym->isLocal()) && !ExportHidden) 655 continue; 656 ExportedSymbols.emplace_back(WasmExportEntry{Sym, BudgeLocalName(Sym)}); 657 } 658 } 659 660 for (const Symbol *Sym : DefinedGlobals) { 661 // Can't export the SP right now because its mutable, and mutuable globals 662 // are yet supported in the official binary format. 663 // TODO(sbc): Remove this if/when the "mutable global" proposal is accepted. 664 if (Sym == Config->StackPointerSymbol) 665 continue; 666 ExportedSymbols.emplace_back(WasmExportEntry{Sym, BudgeLocalName(Sym)}); 667 } 668 } 669 670 uint32_t Writer::lookupType(const WasmSignature &Sig) { 671 auto It = TypeIndices.find(Sig); 672 if (It == TypeIndices.end()) { 673 error("type not found: " + toString(Sig)); 674 return 0; 675 } 676 return It->second; 677 } 678 679 uint32_t Writer::registerType(const WasmSignature &Sig) { 680 auto Pair = TypeIndices.insert(std::make_pair(Sig, Types.size())); 681 if (Pair.second) { 682 DEBUG(dbgs() << "type " << toString(Sig) << "\n"); 683 Types.push_back(&Sig); 684 } 685 return Pair.first->second; 686 } 687 688 void Writer::calculateTypes() { 689 for (ObjFile *File : Symtab->ObjectFiles) { 690 File->TypeMap.reserve(File->getWasmObj()->types().size()); 691 for (const WasmSignature &Sig : File->getWasmObj()->types()) 692 File->TypeMap.push_back(registerType(Sig)); 693 } 694 695 for (Symbol *Sym : Symtab->getSymbols()) 696 if (Sym->isFunction()) 697 registerType(Sym->getFunctionType()); 698 } 699 700 void Writer::assignIndexes() { 701 uint32_t GlobalIndex = ImportedGlobals.size() + DefinedGlobals.size(); 702 uint32_t FunctionIndex = ImportedFunctions.size() + DefinedFunctions.size(); 703 704 if (Config->StackPointerSymbol) { 705 DefinedGlobals.emplace_back(Config->StackPointerSymbol); 706 Config->StackPointerSymbol->setOutputIndex(GlobalIndex++); 707 } 708 709 if (Config->HeapBaseSymbol) { 710 DefinedGlobals.emplace_back(Config->HeapBaseSymbol); 711 Config->HeapBaseSymbol->setOutputIndex(GlobalIndex++); 712 } 713 714 if (Config->Relocatable) 715 DefinedGlobals.reserve(Symtab->getSymbols().size()); 716 717 uint32_t TableIndex = kInitialTableOffset; 718 719 for (ObjFile *File : Symtab->ObjectFiles) { 720 if (Config->Relocatable) { 721 DEBUG(dbgs() << "Globals: " << File->getName() << "\n"); 722 for (Symbol *Sym : File->getSymbols()) { 723 // Create wasm globals for data symbols defined in this file 724 if (!Sym->isDefined() || File != Sym->getFile()) 725 continue; 726 if (Sym->isFunction()) 727 continue; 728 729 DefinedGlobals.emplace_back(Sym); 730 Sym->setOutputIndex(GlobalIndex++); 731 } 732 } 733 } 734 735 for (ObjFile *File : Symtab->ObjectFiles) { 736 DEBUG(dbgs() << "Functions: " << File->getName() << "\n"); 737 for (InputFunction *Func : File->Functions) { 738 if (Func->Discarded) 739 continue; 740 DefinedFunctions.emplace_back(Func); 741 Func->setOutputIndex(FunctionIndex++); 742 } 743 } 744 745 for (ObjFile *File : Symtab->ObjectFiles) { 746 DEBUG(dbgs() << "Table Indexes: " << File->getName() << "\n"); 747 auto HandleTableRelocs = [&](InputChunk *Chunk) { 748 if (Chunk->Discarded) 749 return; 750 for (const WasmRelocation& Reloc : Chunk->getRelocations()) { 751 if (Reloc.Type != R_WEBASSEMBLY_TABLE_INDEX_I32 && 752 Reloc.Type != R_WEBASSEMBLY_TABLE_INDEX_SLEB) 753 continue; 754 Symbol *Sym = File->getFunctionSymbol(Reloc.Index); 755 if (Sym->hasTableIndex() || !Sym->hasOutputIndex()) 756 continue; 757 Sym->setTableIndex(TableIndex++); 758 IndirectFunctions.emplace_back(Sym); 759 } 760 }; 761 for (InputFunction* Function : File->Functions) 762 HandleTableRelocs(Function); 763 for (InputSegment* Segment : File->Segments) 764 HandleTableRelocs(Segment); 765 } 766 } 767 768 static StringRef getOutputDataSegmentName(StringRef Name) { 769 if (Config->Relocatable) 770 return Name; 771 772 for (StringRef V : 773 {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.", 774 ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.", 775 ".gcc_except_table.", ".tdata.", ".ARM.exidx.", ".ARM.extab."}) { 776 StringRef Prefix = V.drop_back(); 777 if (Name.startswith(V) || Name == Prefix) 778 return Prefix; 779 } 780 781 return Name; 782 } 783 784 void Writer::createOutputSegments() { 785 for (ObjFile *File : Symtab->ObjectFiles) { 786 for (InputSegment *Segment : File->Segments) { 787 if (Segment->Discarded) 788 continue; 789 StringRef Name = getOutputDataSegmentName(Segment->getName()); 790 OutputSegment *&S = SegmentMap[Name]; 791 if (S == nullptr) { 792 DEBUG(dbgs() << "new segment: " << Name << "\n"); 793 S = make<OutputSegment>(Name); 794 Segments.push_back(S); 795 } 796 S->addInputSegment(Segment); 797 DEBUG(dbgs() << "added data: " << Name << ": " << S->Size << "\n"); 798 } 799 } 800 } 801 802 static const int OPCODE_CALL = 0x10; 803 static const int OPCODE_END = 0xb; 804 805 // Create synthetic "__wasm_call_ctors" function based on ctor functions 806 // in input object. 807 void Writer::createCtorFunction() { 808 uint32_t FunctionIndex = ImportedFunctions.size() + DefinedFunctions.size(); 809 Config->CtorSymbol->setOutputIndex(FunctionIndex); 810 811 // First write the body bytes to a string. 812 std::string FunctionBody; 813 static WasmSignature Signature = {{}, WASM_TYPE_NORESULT}; 814 { 815 raw_string_ostream OS(FunctionBody); 816 writeUleb128(OS, 0, "num locals"); 817 for (const WasmInitFunc &F : InitFunctions) { 818 writeU8(OS, OPCODE_CALL, "CALL"); 819 writeUleb128(OS, F.FunctionIndex, "function index"); 820 } 821 writeU8(OS, OPCODE_END, "END"); 822 } 823 824 // Once we know the size of the body we can create the final function body 825 raw_string_ostream OS(CtorFunctionBody); 826 writeUleb128(OS, FunctionBody.size(), "function size"); 827 OS.flush(); 828 CtorFunctionBody += FunctionBody; 829 ArrayRef<uint8_t> BodyArray( 830 reinterpret_cast<const uint8_t *>(CtorFunctionBody.data()), 831 CtorFunctionBody.size()); 832 CtorFunction = llvm::make_unique<SyntheticFunction>( 833 Signature, BodyArray, Config->CtorSymbol->getName()); 834 CtorFunction->setOutputIndex(FunctionIndex); 835 DefinedFunctions.emplace_back(CtorFunction.get()); 836 } 837 838 // Populate InitFunctions vector with init functions from all input objects. 839 // This is then used either when creating the output linking section or to 840 // synthesize the "__wasm_call_ctors" function. 841 void Writer::calculateInitFunctions() { 842 for (ObjFile *File : Symtab->ObjectFiles) { 843 const WasmLinkingData &L = File->getWasmObj()->linkingData(); 844 InitFunctions.reserve(InitFunctions.size() + L.InitFunctions.size()); 845 for (const WasmInitFunc &F : L.InitFunctions) 846 InitFunctions.emplace_back(WasmInitFunc{ 847 F.Priority, File->relocateFunctionIndex(F.FunctionIndex)}); 848 } 849 // Sort in order of priority (lowest first) so that they are called 850 // in the correct order. 851 std::sort(InitFunctions.begin(), InitFunctions.end(), 852 [](const WasmInitFunc &L, const WasmInitFunc &R) { 853 return L.Priority < R.Priority; 854 }); 855 } 856 857 void Writer::run() { 858 log("-- calculateTypes"); 859 calculateTypes(); 860 log("-- calculateImports"); 861 calculateImports(); 862 log("-- assignIndexes"); 863 assignIndexes(); 864 log("-- calculateExports"); 865 calculateExports(); 866 log("-- calculateInitFunctions"); 867 calculateInitFunctions(); 868 if (!Config->Relocatable) 869 createCtorFunction(); 870 871 if (errorHandler().Verbose) { 872 log("Defined Functions: " + Twine(DefinedFunctions.size())); 873 log("Defined Globals : " + Twine(DefinedGlobals.size())); 874 log("Function Imports : " + Twine(ImportedFunctions.size())); 875 log("Global Imports : " + Twine(ImportedGlobals.size())); 876 log("Total Imports : " + 877 Twine(ImportedFunctions.size() + ImportedGlobals.size())); 878 for (ObjFile *File : Symtab->ObjectFiles) 879 File->dumpInfo(); 880 } 881 882 log("-- layoutMemory"); 883 layoutMemory(); 884 885 createHeader(); 886 log("-- createSections"); 887 createSections(); 888 889 log("-- openFile"); 890 openFile(); 891 if (errorCount()) 892 return; 893 894 writeHeader(); 895 896 log("-- writeSections"); 897 writeSections(); 898 if (errorCount()) 899 return; 900 901 if (Error E = Buffer->commit()) 902 fatal("failed to write the output file: " + toString(std::move(E))); 903 } 904 905 // Open a result file. 906 void Writer::openFile() { 907 log("writing: " + Config->OutputFile); 908 ::remove(Config->OutputFile.str().c_str()); 909 910 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = 911 FileOutputBuffer::create(Config->OutputFile, FileSize, 912 FileOutputBuffer::F_executable); 913 914 if (!BufferOrErr) 915 error("failed to open " + Config->OutputFile + ": " + 916 toString(BufferOrErr.takeError())); 917 else 918 Buffer = std::move(*BufferOrErr); 919 } 920 921 void Writer::createHeader() { 922 raw_string_ostream OS(Header); 923 writeBytes(OS, WasmMagic, sizeof(WasmMagic), "wasm magic"); 924 writeU32(OS, WasmVersion, "wasm version"); 925 OS.flush(); 926 FileSize += Header.size(); 927 } 928 929 void lld::wasm::writeResult() { Writer().run(); } 930