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 #include "Config.h" 12 #include "InputChunks.h" 13 #include "InputEvent.h" 14 #include "InputGlobal.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/Strings.h" 22 #include "lld/Common/Threads.h" 23 #include "llvm/ADT/DenseSet.h" 24 #include "llvm/ADT/StringMap.h" 25 #include "llvm/BinaryFormat/Wasm.h" 26 #include "llvm/Object/WasmTraits.h" 27 #include "llvm/Support/FileOutputBuffer.h" 28 #include "llvm/Support/Format.h" 29 #include "llvm/Support/FormatVariadic.h" 30 #include "llvm/Support/LEB128.h" 31 32 #include <cstdarg> 33 #include <map> 34 35 #define DEBUG_TYPE "lld" 36 37 using namespace llvm; 38 using namespace llvm::wasm; 39 using namespace lld; 40 using namespace lld::wasm; 41 42 static constexpr int kStackAlignment = 16; 43 static constexpr const char *kFunctionTableName = "__indirect_function_table"; 44 45 namespace { 46 47 // An init entry to be written to either the synthetic init func or the 48 // linking metadata. 49 struct WasmInitEntry { 50 const FunctionSymbol *Sym; 51 uint32_t Priority; 52 }; 53 54 // The writer writes a SymbolTable result to a file. 55 class Writer { 56 public: 57 void run(); 58 59 private: 60 void openFile(); 61 62 uint32_t lookupType(const WasmSignature &Sig); 63 uint32_t registerType(const WasmSignature &Sig); 64 65 void createCtorFunction(); 66 void calculateInitFunctions(); 67 void assignIndexes(); 68 void calculateImports(); 69 void calculateExports(); 70 void calculateCustomSections(); 71 void assignSymtab(); 72 void calculateTypes(); 73 void createOutputSegments(); 74 void layoutMemory(); 75 void createHeader(); 76 void createSections(); 77 SyntheticSection *createSyntheticSection(uint32_t Type, StringRef Name = ""); 78 79 // Builtin sections 80 void createTypeSection(); 81 void createFunctionSection(); 82 void createTableSection(); 83 void createGlobalSection(); 84 void createEventSection(); 85 void createExportSection(); 86 void createImportSection(); 87 void createMemorySection(); 88 void createElemSection(); 89 void createCodeSection(); 90 void createDataSection(); 91 void createCustomSections(); 92 93 // Custom sections 94 void createDylinkSection(); 95 void createRelocSections(); 96 void createLinkingSection(); 97 void createNameSection(); 98 99 void writeHeader(); 100 void writeSections(); 101 102 uint64_t FileSize = 0; 103 uint32_t TableBase = 0; 104 uint32_t NumMemoryPages = 0; 105 uint32_t MaxMemoryPages = 0; 106 // Memory size and aligment. Written to the "dylink" section 107 // when build with -shared or -pie. 108 uint32_t MemAlign = 0; 109 uint32_t MemSize = 0; 110 111 std::vector<const WasmSignature *> Types; 112 DenseMap<WasmSignature, int32_t> TypeIndices; 113 std::vector<const Symbol *> ImportedSymbols; 114 unsigned NumImportedFunctions = 0; 115 unsigned NumImportedGlobals = 0; 116 unsigned NumImportedEvents = 0; 117 std::vector<WasmExport> Exports; 118 std::vector<const DefinedData *> DefinedFakeGlobals; 119 std::vector<InputGlobal *> InputGlobals; 120 std::vector<InputFunction *> InputFunctions; 121 std::vector<InputEvent *> InputEvents; 122 std::vector<const FunctionSymbol *> IndirectFunctions; 123 std::vector<const Symbol *> SymtabEntries; 124 std::vector<WasmInitEntry> InitFunctions; 125 126 llvm::StringMap<std::vector<InputSection *>> CustomSectionMapping; 127 llvm::StringMap<SectionSymbol *> CustomSectionSymbols; 128 129 // Elements that are used to construct the final output 130 std::string Header; 131 std::vector<OutputSection *> OutputSections; 132 133 std::unique_ptr<FileOutputBuffer> Buffer; 134 135 std::vector<OutputSegment *> Segments; 136 llvm::SmallDenseMap<StringRef, OutputSegment *> SegmentMap; 137 }; 138 139 } // anonymous namespace 140 141 void Writer::createImportSection() { 142 uint32_t NumImports = ImportedSymbols.size(); 143 if (Config->ImportMemory) 144 ++NumImports; 145 if (Config->ImportTable) 146 ++NumImports; 147 148 if (NumImports == 0) 149 return; 150 151 SyntheticSection *Section = createSyntheticSection(WASM_SEC_IMPORT); 152 raw_ostream &OS = Section->getStream(); 153 154 writeUleb128(OS, NumImports, "import count"); 155 156 if (Config->ImportMemory) { 157 WasmImport Import; 158 Import.Module = "env"; 159 Import.Field = "memory"; 160 Import.Kind = WASM_EXTERNAL_MEMORY; 161 Import.Memory.Flags = 0; 162 Import.Memory.Initial = NumMemoryPages; 163 if (MaxMemoryPages != 0) { 164 Import.Memory.Flags |= WASM_LIMITS_FLAG_HAS_MAX; 165 Import.Memory.Maximum = MaxMemoryPages; 166 } 167 if (Config->SharedMemory) 168 Import.Memory.Flags |= WASM_LIMITS_FLAG_IS_SHARED; 169 writeImport(OS, Import); 170 } 171 172 if (Config->ImportTable) { 173 uint32_t TableSize = TableBase + IndirectFunctions.size(); 174 WasmImport Import; 175 Import.Module = "env"; 176 Import.Field = kFunctionTableName; 177 Import.Kind = WASM_EXTERNAL_TABLE; 178 Import.Table.ElemType = WASM_TYPE_ANYFUNC; 179 Import.Table.Limits = {0, TableSize, 0}; 180 writeImport(OS, Import); 181 } 182 183 for (const Symbol *Sym : ImportedSymbols) { 184 WasmImport Import; 185 Import.Module = "env"; 186 Import.Field = Sym->getName(); 187 if (auto *FunctionSym = dyn_cast<FunctionSymbol>(Sym)) { 188 Import.Kind = WASM_EXTERNAL_FUNCTION; 189 Import.SigIndex = lookupType(*FunctionSym->Signature); 190 } else if (auto *GlobalSym = dyn_cast<GlobalSymbol>(Sym)) { 191 Import.Kind = WASM_EXTERNAL_GLOBAL; 192 Import.Global = *GlobalSym->getGlobalType(); 193 } else { 194 auto *EventSym = cast<EventSymbol>(Sym); 195 Import.Kind = WASM_EXTERNAL_EVENT; 196 Import.Event.Attribute = EventSym->getEventType()->Attribute; 197 Import.Event.SigIndex = lookupType(*EventSym->Signature); 198 } 199 writeImport(OS, Import); 200 } 201 } 202 203 void Writer::createTypeSection() { 204 SyntheticSection *Section = createSyntheticSection(WASM_SEC_TYPE); 205 raw_ostream &OS = Section->getStream(); 206 writeUleb128(OS, Types.size(), "type count"); 207 for (const WasmSignature *Sig : Types) 208 writeSig(OS, *Sig); 209 } 210 211 void Writer::createFunctionSection() { 212 if (InputFunctions.empty()) 213 return; 214 215 SyntheticSection *Section = createSyntheticSection(WASM_SEC_FUNCTION); 216 raw_ostream &OS = Section->getStream(); 217 218 writeUleb128(OS, InputFunctions.size(), "function count"); 219 for (const InputFunction *Func : InputFunctions) 220 writeUleb128(OS, lookupType(Func->Signature), "sig index"); 221 } 222 223 void Writer::createMemorySection() { 224 if (Config->ImportMemory) 225 return; 226 227 SyntheticSection *Section = createSyntheticSection(WASM_SEC_MEMORY); 228 raw_ostream &OS = Section->getStream(); 229 230 bool HasMax = MaxMemoryPages != 0; 231 writeUleb128(OS, 1, "memory count"); 232 unsigned Flags = 0; 233 if (HasMax) 234 Flags |= WASM_LIMITS_FLAG_HAS_MAX; 235 if (Config->SharedMemory) 236 Flags |= WASM_LIMITS_FLAG_IS_SHARED; 237 writeUleb128(OS, Flags, "memory limits flags"); 238 writeUleb128(OS, NumMemoryPages, "initial pages"); 239 if (HasMax) 240 writeUleb128(OS, MaxMemoryPages, "max pages"); 241 } 242 243 void Writer::createGlobalSection() { 244 unsigned NumGlobals = InputGlobals.size() + DefinedFakeGlobals.size(); 245 if (NumGlobals == 0) 246 return; 247 248 SyntheticSection *Section = createSyntheticSection(WASM_SEC_GLOBAL); 249 raw_ostream &OS = Section->getStream(); 250 251 writeUleb128(OS, NumGlobals, "global count"); 252 for (const InputGlobal *G : InputGlobals) 253 writeGlobal(OS, G->Global); 254 for (const DefinedData *Sym : DefinedFakeGlobals) { 255 WasmGlobal Global; 256 Global.Type = {WASM_TYPE_I32, false}; 257 Global.InitExpr.Opcode = WASM_OPCODE_I32_CONST; 258 Global.InitExpr.Value.Int32 = Sym->getVirtualAddress(); 259 writeGlobal(OS, Global); 260 } 261 } 262 263 // The event section contains a list of declared wasm events associated with the 264 // module. Currently the only supported event kind is exceptions. A single event 265 // entry represents a single event with an event tag. All C++ exceptions are 266 // represented by a single event. An event entry in this section contains 267 // information on what kind of event it is (e.g. exception) and the type of 268 // values contained in a single event object. (In wasm, an event can contain 269 // multiple values of primitive types. But for C++ exceptions, we just throw a 270 // pointer which is an i32 value (for wasm32 architecture), so the signature of 271 // C++ exception is (i32)->(void), because all event types are assumed to have 272 // void return type to share WasmSignature with functions.) 273 void Writer::createEventSection() { 274 unsigned NumEvents = InputEvents.size(); 275 if (NumEvents == 0) 276 return; 277 278 SyntheticSection *Section = createSyntheticSection(WASM_SEC_EVENT); 279 raw_ostream &OS = Section->getStream(); 280 281 writeUleb128(OS, NumEvents, "event count"); 282 for (InputEvent *E : InputEvents) { 283 E->Event.Type.SigIndex = lookupType(E->Signature); 284 writeEvent(OS, E->Event); 285 } 286 } 287 288 void Writer::createTableSection() { 289 if (Config->ImportTable) 290 return; 291 292 // Always output a table section (or table import), even if there are no 293 // indirect calls. There are two reasons for this: 294 // 1. For executables it is useful to have an empty table slot at 0 295 // which can be filled with a null function call handler. 296 // 2. If we don't do this, any program that contains a call_indirect but 297 // no address-taken function will fail at validation time since it is 298 // a validation error to include a call_indirect instruction if there 299 // is not table. 300 uint32_t TableSize = TableBase + IndirectFunctions.size(); 301 302 SyntheticSection *Section = createSyntheticSection(WASM_SEC_TABLE); 303 raw_ostream &OS = Section->getStream(); 304 305 writeUleb128(OS, 1, "table count"); 306 WasmLimits Limits = {WASM_LIMITS_FLAG_HAS_MAX, TableSize, TableSize}; 307 writeTableType(OS, WasmTable{WASM_TYPE_ANYFUNC, Limits}); 308 } 309 310 void Writer::createExportSection() { 311 if (!Exports.size()) 312 return; 313 314 SyntheticSection *Section = createSyntheticSection(WASM_SEC_EXPORT); 315 raw_ostream &OS = Section->getStream(); 316 317 writeUleb128(OS, Exports.size(), "export count"); 318 for (const WasmExport &Export : Exports) 319 writeExport(OS, Export); 320 } 321 322 void Writer::calculateCustomSections() { 323 log("calculateCustomSections"); 324 bool StripDebug = Config->StripDebug || Config->StripAll; 325 for (ObjFile *File : Symtab->ObjectFiles) { 326 for (InputSection *Section : File->CustomSections) { 327 StringRef Name = Section->getName(); 328 // These custom sections are known the linker and synthesized rather than 329 // blindly copied 330 if (Name == "linking" || Name == "name" || Name.startswith("reloc.")) 331 continue; 332 // .. or it is a debug section 333 if (StripDebug && Name.startswith(".debug_")) 334 continue; 335 CustomSectionMapping[Name].push_back(Section); 336 } 337 } 338 } 339 340 void Writer::createCustomSections() { 341 log("createCustomSections"); 342 for (auto &Pair : CustomSectionMapping) { 343 StringRef Name = Pair.first(); 344 345 auto P = CustomSectionSymbols.find(Name); 346 if (P != CustomSectionSymbols.end()) { 347 uint32_t SectionIndex = OutputSections.size(); 348 P->second->setOutputSectionIndex(SectionIndex); 349 } 350 351 LLVM_DEBUG(dbgs() << "createCustomSection: " << Name << "\n"); 352 OutputSections.push_back(make<CustomSection>(Name, Pair.second)); 353 } 354 } 355 356 void Writer::createElemSection() { 357 if (IndirectFunctions.empty()) 358 return; 359 360 SyntheticSection *Section = createSyntheticSection(WASM_SEC_ELEM); 361 raw_ostream &OS = Section->getStream(); 362 363 writeUleb128(OS, 1, "segment count"); 364 writeUleb128(OS, 0, "table index"); 365 WasmInitExpr InitExpr; 366 if (Config->Pic) { 367 InitExpr.Opcode = WASM_OPCODE_GET_GLOBAL; 368 InitExpr.Value.Global = WasmSym::TableBase->getGlobalIndex(); 369 } else { 370 InitExpr.Opcode = WASM_OPCODE_I32_CONST; 371 InitExpr.Value.Int32 = TableBase; 372 } 373 writeInitExpr(OS, InitExpr); 374 writeUleb128(OS, IndirectFunctions.size(), "elem count"); 375 376 uint32_t TableIndex = TableBase; 377 for (const FunctionSymbol *Sym : IndirectFunctions) { 378 assert(Sym->getTableIndex() == TableIndex); 379 writeUleb128(OS, Sym->getFunctionIndex(), "function index"); 380 ++TableIndex; 381 } 382 } 383 384 void Writer::createCodeSection() { 385 if (InputFunctions.empty()) 386 return; 387 388 log("createCodeSection"); 389 390 auto Section = make<CodeSection>(InputFunctions); 391 OutputSections.push_back(Section); 392 } 393 394 void Writer::createDataSection() { 395 if (!Segments.size()) 396 return; 397 398 log("createDataSection"); 399 auto Section = make<DataSection>(Segments); 400 OutputSections.push_back(Section); 401 } 402 403 // Create relocations sections in the final output. 404 // These are only created when relocatable output is requested. 405 void Writer::createRelocSections() { 406 log("createRelocSections"); 407 // Don't use iterator here since we are adding to OutputSection 408 size_t OrigSize = OutputSections.size(); 409 for (size_t I = 0; I < OrigSize; I++) { 410 OutputSection *OSec = OutputSections[I]; 411 uint32_t Count = OSec->numRelocations(); 412 if (!Count) 413 continue; 414 415 StringRef Name; 416 if (OSec->Type == WASM_SEC_DATA) 417 Name = "reloc.DATA"; 418 else if (OSec->Type == WASM_SEC_CODE) 419 Name = "reloc.CODE"; 420 else if (OSec->Type == WASM_SEC_CUSTOM) 421 Name = Saver.save("reloc." + OSec->Name); 422 else 423 llvm_unreachable( 424 "relocations only supported for code, data, or custom sections"); 425 426 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, Name); 427 raw_ostream &OS = Section->getStream(); 428 writeUleb128(OS, I, "reloc section"); 429 writeUleb128(OS, Count, "reloc count"); 430 OSec->writeRelocations(OS); 431 } 432 } 433 434 static uint32_t getWasmFlags(const Symbol *Sym) { 435 uint32_t Flags = 0; 436 if (Sym->isLocal()) 437 Flags |= WASM_SYMBOL_BINDING_LOCAL; 438 if (Sym->isWeak()) 439 Flags |= WASM_SYMBOL_BINDING_WEAK; 440 if (Sym->isHidden()) 441 Flags |= WASM_SYMBOL_VISIBILITY_HIDDEN; 442 if (Sym->isUndefined()) 443 Flags |= WASM_SYMBOL_UNDEFINED; 444 return Flags; 445 } 446 447 // Some synthetic sections (e.g. "name" and "linking") have subsections. 448 // Just like the synthetic sections themselves these need to be created before 449 // they can be written out (since they are preceded by their length). This 450 // class is used to create subsections and then write them into the stream 451 // of the parent section. 452 class SubSection { 453 public: 454 explicit SubSection(uint32_t Type) : Type(Type) {} 455 456 void writeTo(raw_ostream &To) { 457 OS.flush(); 458 writeUleb128(To, Type, "subsection type"); 459 writeUleb128(To, Body.size(), "subsection size"); 460 To.write(Body.data(), Body.size()); 461 } 462 463 private: 464 uint32_t Type; 465 std::string Body; 466 467 public: 468 raw_string_ostream OS{Body}; 469 }; 470 471 // Create the custom "dylink" section containing information for the dynamic 472 // linker. 473 // See 474 // https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md 475 void Writer::createDylinkSection() { 476 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, "dylink"); 477 raw_ostream &OS = Section->getStream(); 478 479 writeUleb128(OS, MemSize, "MemSize"); 480 writeUleb128(OS, int(log2(MemAlign)), "MemAlign"); 481 writeUleb128(OS, IndirectFunctions.size(), "TableSize"); 482 writeUleb128(OS, 0, "TableAlign"); 483 writeUleb128(OS, 0, "Needed"); // TODO: Support "needed" shared libraries 484 } 485 486 // Create the custom "linking" section containing linker metadata. 487 // This is only created when relocatable output is requested. 488 void Writer::createLinkingSection() { 489 SyntheticSection *Section = 490 createSyntheticSection(WASM_SEC_CUSTOM, "linking"); 491 raw_ostream &OS = Section->getStream(); 492 493 writeUleb128(OS, WasmMetadataVersion, "Version"); 494 495 if (!SymtabEntries.empty()) { 496 SubSection Sub(WASM_SYMBOL_TABLE); 497 writeUleb128(Sub.OS, SymtabEntries.size(), "num symbols"); 498 499 for (const Symbol *Sym : SymtabEntries) { 500 assert(Sym->isDefined() || Sym->isUndefined()); 501 WasmSymbolType Kind = Sym->getWasmType(); 502 uint32_t Flags = getWasmFlags(Sym); 503 504 writeU8(Sub.OS, Kind, "sym kind"); 505 writeUleb128(Sub.OS, Flags, "sym flags"); 506 507 if (auto *F = dyn_cast<FunctionSymbol>(Sym)) { 508 writeUleb128(Sub.OS, F->getFunctionIndex(), "index"); 509 if (Sym->isDefined()) 510 writeStr(Sub.OS, Sym->getName(), "sym name"); 511 } else if (auto *G = dyn_cast<GlobalSymbol>(Sym)) { 512 writeUleb128(Sub.OS, G->getGlobalIndex(), "index"); 513 if (Sym->isDefined()) 514 writeStr(Sub.OS, Sym->getName(), "sym name"); 515 } else if (auto *E = dyn_cast<EventSymbol>(Sym)) { 516 writeUleb128(Sub.OS, E->getEventIndex(), "index"); 517 if (Sym->isDefined()) 518 writeStr(Sub.OS, Sym->getName(), "sym name"); 519 } else if (isa<DataSymbol>(Sym)) { 520 writeStr(Sub.OS, Sym->getName(), "sym name"); 521 if (auto *DataSym = dyn_cast<DefinedData>(Sym)) { 522 writeUleb128(Sub.OS, DataSym->getOutputSegmentIndex(), "index"); 523 writeUleb128(Sub.OS, DataSym->getOutputSegmentOffset(), 524 "data offset"); 525 writeUleb128(Sub.OS, DataSym->getSize(), "data size"); 526 } 527 } else { 528 auto *S = cast<SectionSymbol>(Sym); 529 writeUleb128(Sub.OS, S->getOutputSectionIndex(), "sym section index"); 530 } 531 } 532 533 Sub.writeTo(OS); 534 } 535 536 if (Segments.size()) { 537 SubSection Sub(WASM_SEGMENT_INFO); 538 writeUleb128(Sub.OS, Segments.size(), "num data segments"); 539 for (const OutputSegment *S : Segments) { 540 writeStr(Sub.OS, S->Name, "segment name"); 541 writeUleb128(Sub.OS, S->Alignment, "alignment"); 542 writeUleb128(Sub.OS, 0, "flags"); 543 } 544 Sub.writeTo(OS); 545 } 546 547 if (!InitFunctions.empty()) { 548 SubSection Sub(WASM_INIT_FUNCS); 549 writeUleb128(Sub.OS, InitFunctions.size(), "num init functions"); 550 for (const WasmInitEntry &F : InitFunctions) { 551 writeUleb128(Sub.OS, F.Priority, "priority"); 552 writeUleb128(Sub.OS, F.Sym->getOutputSymbolIndex(), "function index"); 553 } 554 Sub.writeTo(OS); 555 } 556 557 struct ComdatEntry { 558 unsigned Kind; 559 uint32_t Index; 560 }; 561 std::map<StringRef, std::vector<ComdatEntry>> Comdats; 562 563 for (const InputFunction *F : InputFunctions) { 564 StringRef Comdat = F->getComdatName(); 565 if (!Comdat.empty()) 566 Comdats[Comdat].emplace_back( 567 ComdatEntry{WASM_COMDAT_FUNCTION, F->getFunctionIndex()}); 568 } 569 for (uint32_t I = 0; I < Segments.size(); ++I) { 570 const auto &InputSegments = Segments[I]->InputSegments; 571 if (InputSegments.empty()) 572 continue; 573 StringRef Comdat = InputSegments[0]->getComdatName(); 574 #ifndef NDEBUG 575 for (const InputSegment *IS : InputSegments) 576 assert(IS->getComdatName() == Comdat); 577 #endif 578 if (!Comdat.empty()) 579 Comdats[Comdat].emplace_back(ComdatEntry{WASM_COMDAT_DATA, I}); 580 } 581 582 if (!Comdats.empty()) { 583 SubSection Sub(WASM_COMDAT_INFO); 584 writeUleb128(Sub.OS, Comdats.size(), "num comdats"); 585 for (const auto &C : Comdats) { 586 writeStr(Sub.OS, C.first, "comdat name"); 587 writeUleb128(Sub.OS, 0, "comdat flags"); // flags for future use 588 writeUleb128(Sub.OS, C.second.size(), "num entries"); 589 for (const ComdatEntry &Entry : C.second) { 590 writeU8(Sub.OS, Entry.Kind, "entry kind"); 591 writeUleb128(Sub.OS, Entry.Index, "entry index"); 592 } 593 } 594 Sub.writeTo(OS); 595 } 596 } 597 598 // Create the custom "name" section containing debug symbol names. 599 void Writer::createNameSection() { 600 unsigned NumNames = NumImportedFunctions; 601 for (const InputFunction *F : InputFunctions) 602 if (!F->getName().empty() || !F->getDebugName().empty()) 603 ++NumNames; 604 605 if (NumNames == 0) 606 return; 607 608 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, "name"); 609 610 SubSection Sub(WASM_NAMES_FUNCTION); 611 writeUleb128(Sub.OS, NumNames, "name count"); 612 613 // Names must appear in function index order. As it happens ImportedSymbols 614 // and InputFunctions are numbered in order with imported functions coming 615 // first. 616 for (const Symbol *S : ImportedSymbols) { 617 if (auto *F = dyn_cast<FunctionSymbol>(S)) { 618 writeUleb128(Sub.OS, F->getFunctionIndex(), "func index"); 619 writeStr(Sub.OS, toString(*S), "symbol name"); 620 } 621 } 622 for (const InputFunction *F : InputFunctions) { 623 if (!F->getName().empty()) { 624 writeUleb128(Sub.OS, F->getFunctionIndex(), "func index"); 625 if (!F->getDebugName().empty()) { 626 writeStr(Sub.OS, F->getDebugName(), "symbol name"); 627 } else { 628 writeStr(Sub.OS, maybeDemangleSymbol(F->getName()), "symbol name"); 629 } 630 } 631 } 632 633 Sub.writeTo(Section->getStream()); 634 } 635 636 void Writer::writeHeader() { 637 memcpy(Buffer->getBufferStart(), Header.data(), Header.size()); 638 } 639 640 void Writer::writeSections() { 641 uint8_t *Buf = Buffer->getBufferStart(); 642 parallelForEach(OutputSections, [Buf](OutputSection *S) { S->writeTo(Buf); }); 643 } 644 645 // Fix the memory layout of the output binary. This assigns memory offsets 646 // to each of the input data sections as well as the explicit stack region. 647 // The default memory layout is as follows, from low to high. 648 // 649 // - initialized data (starting at Config->GlobalBase) 650 // - BSS data (not currently implemented in llvm) 651 // - explicit stack (Config->ZStackSize) 652 // - heap start / unallocated 653 // 654 // The --stack-first option means that stack is placed before any static data. 655 // This can be useful since it means that stack overflow traps immediately 656 // rather than overwriting global data, but also increases code size since all 657 // static data loads and stores requires larger offsets. 658 void Writer::layoutMemory() { 659 createOutputSegments(); 660 661 uint32_t MemoryPtr = 0; 662 663 auto PlaceStack = [&]() { 664 if (Config->Relocatable || Config->Shared) 665 return; 666 MemoryPtr = alignTo(MemoryPtr, kStackAlignment); 667 if (Config->ZStackSize != alignTo(Config->ZStackSize, kStackAlignment)) 668 error("stack size must be " + Twine(kStackAlignment) + "-byte aligned"); 669 log("mem: stack size = " + Twine(Config->ZStackSize)); 670 log("mem: stack base = " + Twine(MemoryPtr)); 671 MemoryPtr += Config->ZStackSize; 672 auto *SP = cast<DefinedGlobal>(WasmSym::StackPointer); 673 SP->Global->Global.InitExpr.Value.Int32 = MemoryPtr; 674 log("mem: stack top = " + Twine(MemoryPtr)); 675 }; 676 677 if (Config->StackFirst) { 678 PlaceStack(); 679 } else { 680 MemoryPtr = Config->GlobalBase; 681 log("mem: global base = " + Twine(Config->GlobalBase)); 682 } 683 684 uint32_t DataStart = MemoryPtr; 685 686 // Arbitrarily set __dso_handle handle to point to the start of the data 687 // segments. 688 if (WasmSym::DsoHandle) 689 WasmSym::DsoHandle->setVirtualAddress(DataStart); 690 691 MemAlign = 0; 692 for (OutputSegment *Seg : Segments) { 693 MemAlign = std::max(MemAlign, Seg->Alignment); 694 MemoryPtr = alignTo(MemoryPtr, Seg->Alignment); 695 Seg->StartVA = MemoryPtr; 696 log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}", Seg->Name, 697 MemoryPtr, Seg->Size, Seg->Alignment)); 698 MemoryPtr += Seg->Size; 699 } 700 701 // TODO: Add .bss space here. 702 if (WasmSym::DataEnd) 703 WasmSym::DataEnd->setVirtualAddress(MemoryPtr); 704 705 log("mem: static data = " + Twine(MemoryPtr - DataStart)); 706 707 if (Config->Shared) { 708 MemSize = MemoryPtr; 709 return; 710 } 711 712 if (!Config->StackFirst) 713 PlaceStack(); 714 715 // Set `__heap_base` to directly follow the end of the stack or global data. 716 // The fact that this comes last means that a malloc/brk implementation 717 // can grow the heap at runtime. 718 if (!Config->Relocatable) { 719 WasmSym::HeapBase->setVirtualAddress(MemoryPtr); 720 log("mem: heap base = " + Twine(MemoryPtr)); 721 } 722 723 if (Config->InitialMemory != 0) { 724 if (Config->InitialMemory != alignTo(Config->InitialMemory, WasmPageSize)) 725 error("initial memory must be " + Twine(WasmPageSize) + "-byte aligned"); 726 if (MemoryPtr > Config->InitialMemory) 727 error("initial memory too small, " + Twine(MemoryPtr) + " bytes needed"); 728 else 729 MemoryPtr = Config->InitialMemory; 730 } 731 MemSize = MemoryPtr; 732 NumMemoryPages = alignTo(MemoryPtr, WasmPageSize) / WasmPageSize; 733 log("mem: total pages = " + Twine(NumMemoryPages)); 734 735 if (Config->MaxMemory != 0) { 736 if (Config->MaxMemory != alignTo(Config->MaxMemory, WasmPageSize)) 737 error("maximum memory must be " + Twine(WasmPageSize) + "-byte aligned"); 738 if (MemoryPtr > Config->MaxMemory) 739 error("maximum memory too small, " + Twine(MemoryPtr) + " bytes needed"); 740 MaxMemoryPages = Config->MaxMemory / WasmPageSize; 741 log("mem: max pages = " + Twine(MaxMemoryPages)); 742 } 743 } 744 745 SyntheticSection *Writer::createSyntheticSection(uint32_t Type, 746 StringRef Name) { 747 auto Sec = make<SyntheticSection>(Type, Name); 748 log("createSection: " + toString(*Sec)); 749 OutputSections.push_back(Sec); 750 return Sec; 751 } 752 753 void Writer::createSections() { 754 // Known sections 755 if (Config->Pic) 756 createDylinkSection(); 757 createTypeSection(); 758 createImportSection(); 759 createFunctionSection(); 760 createTableSection(); 761 createMemorySection(); 762 createGlobalSection(); 763 createEventSection(); 764 createExportSection(); 765 createElemSection(); 766 createCodeSection(); 767 createDataSection(); 768 createCustomSections(); 769 770 // Custom sections 771 if (Config->Relocatable) { 772 createLinkingSection(); 773 createRelocSections(); 774 } 775 if (!Config->StripDebug && !Config->StripAll) 776 createNameSection(); 777 778 for (OutputSection *S : OutputSections) { 779 S->setOffset(FileSize); 780 S->finalizeContents(); 781 FileSize += S->getSize(); 782 } 783 } 784 785 void Writer::calculateImports() { 786 for (Symbol *Sym : Symtab->getSymbols()) { 787 if (!Sym->isUndefined()) 788 continue; 789 if (isa<DataSymbol>(Sym)) 790 continue; 791 if (Sym->isWeak() && !Config->Relocatable) 792 continue; 793 if (!Sym->isLive()) 794 continue; 795 if (!Sym->IsUsedInRegularObj) 796 continue; 797 798 LLVM_DEBUG(dbgs() << "import: " << Sym->getName() << "\n"); 799 ImportedSymbols.emplace_back(Sym); 800 if (auto *F = dyn_cast<FunctionSymbol>(Sym)) 801 F->setFunctionIndex(NumImportedFunctions++); 802 else if (auto *G = dyn_cast<GlobalSymbol>(Sym)) 803 G->setGlobalIndex(NumImportedGlobals++); 804 else 805 cast<EventSymbol>(Sym)->setEventIndex(NumImportedEvents++); 806 } 807 } 808 809 void Writer::calculateExports() { 810 if (Config->Relocatable) 811 return; 812 813 if (!Config->Relocatable && !Config->ImportMemory) 814 Exports.push_back(WasmExport{"memory", WASM_EXTERNAL_MEMORY, 0}); 815 816 if (!Config->Relocatable && Config->ExportTable) 817 Exports.push_back(WasmExport{kFunctionTableName, WASM_EXTERNAL_TABLE, 0}); 818 819 unsigned FakeGlobalIndex = NumImportedGlobals + InputGlobals.size(); 820 821 for (Symbol *Sym : Symtab->getSymbols()) { 822 if (!Sym->isExported()) 823 continue; 824 if (!Sym->isLive()) 825 continue; 826 827 StringRef Name = Sym->getName(); 828 WasmExport Export; 829 if (auto *F = dyn_cast<DefinedFunction>(Sym)) { 830 Export = {Name, WASM_EXTERNAL_FUNCTION, F->getFunctionIndex()}; 831 } else if (auto *G = dyn_cast<DefinedGlobal>(Sym)) { 832 // TODO(sbc): Remove this check once to mutable global proposal is 833 // implement in all major browsers. 834 // See: https://github.com/WebAssembly/mutable-global 835 if (G->getGlobalType()->Mutable) { 836 // Only the __stack_pointer should ever be create as mutable. 837 assert(G == WasmSym::StackPointer); 838 continue; 839 } 840 Export = {Name, WASM_EXTERNAL_GLOBAL, G->getGlobalIndex()}; 841 } else if (auto *E = dyn_cast<DefinedEvent>(Sym)) { 842 Export = {Name, WASM_EXTERNAL_EVENT, E->getEventIndex()}; 843 } else { 844 auto *D = cast<DefinedData>(Sym); 845 DefinedFakeGlobals.emplace_back(D); 846 Export = {Name, WASM_EXTERNAL_GLOBAL, FakeGlobalIndex++}; 847 } 848 849 LLVM_DEBUG(dbgs() << "Export: " << Name << "\n"); 850 Exports.push_back(Export); 851 } 852 } 853 854 void Writer::assignSymtab() { 855 if (!Config->Relocatable) 856 return; 857 858 StringMap<uint32_t> SectionSymbolIndices; 859 860 unsigned SymbolIndex = SymtabEntries.size(); 861 for (ObjFile *File : Symtab->ObjectFiles) { 862 LLVM_DEBUG(dbgs() << "Symtab entries: " << File->getName() << "\n"); 863 for (Symbol *Sym : File->getSymbols()) { 864 if (Sym->getFile() != File) 865 continue; 866 867 if (auto *S = dyn_cast<SectionSymbol>(Sym)) { 868 StringRef Name = S->getName(); 869 if (CustomSectionMapping.count(Name) == 0) 870 continue; 871 872 auto SSI = SectionSymbolIndices.find(Name); 873 if (SSI != SectionSymbolIndices.end()) { 874 Sym->setOutputSymbolIndex(SSI->second); 875 continue; 876 } 877 878 SectionSymbolIndices[Name] = SymbolIndex; 879 CustomSectionSymbols[Name] = cast<SectionSymbol>(Sym); 880 881 Sym->markLive(); 882 } 883 884 // (Since this is relocatable output, GC is not performed so symbols must 885 // be live.) 886 assert(Sym->isLive()); 887 Sym->setOutputSymbolIndex(SymbolIndex++); 888 SymtabEntries.emplace_back(Sym); 889 } 890 } 891 892 // For the moment, relocatable output doesn't contain any synthetic functions, 893 // so no need to look through the Symtab for symbols not referenced by 894 // Symtab->ObjectFiles. 895 } 896 897 uint32_t Writer::lookupType(const WasmSignature &Sig) { 898 auto It = TypeIndices.find(Sig); 899 if (It == TypeIndices.end()) { 900 error("type not found: " + toString(Sig)); 901 return 0; 902 } 903 return It->second; 904 } 905 906 uint32_t Writer::registerType(const WasmSignature &Sig) { 907 auto Pair = TypeIndices.insert(std::make_pair(Sig, Types.size())); 908 if (Pair.second) { 909 LLVM_DEBUG(dbgs() << "type " << toString(Sig) << "\n"); 910 Types.push_back(&Sig); 911 } 912 return Pair.first->second; 913 } 914 915 void Writer::calculateTypes() { 916 // The output type section is the union of the following sets: 917 // 1. Any signature used in the TYPE relocation 918 // 2. The signatures of all imported functions 919 // 3. The signatures of all defined functions 920 // 4. The signatures of all imported events 921 // 5. The signatures of all defined events 922 923 for (ObjFile *File : Symtab->ObjectFiles) { 924 ArrayRef<WasmSignature> Types = File->getWasmObj()->types(); 925 for (uint32_t I = 0; I < Types.size(); I++) 926 if (File->TypeIsUsed[I]) 927 File->TypeMap[I] = registerType(Types[I]); 928 } 929 930 for (const Symbol *Sym : ImportedSymbols) { 931 if (auto *F = dyn_cast<FunctionSymbol>(Sym)) 932 registerType(*F->Signature); 933 else if (auto *E = dyn_cast<EventSymbol>(Sym)) 934 registerType(*E->Signature); 935 } 936 937 for (const InputFunction *F : InputFunctions) 938 registerType(F->Signature); 939 940 for (const InputEvent *E : InputEvents) 941 registerType(E->Signature); 942 } 943 944 void Writer::assignIndexes() { 945 assert(InputFunctions.empty()); 946 uint32_t FunctionIndex = NumImportedFunctions; 947 auto AddDefinedFunction = [&](InputFunction *Func) { 948 if (!Func->Live) 949 return; 950 InputFunctions.emplace_back(Func); 951 Func->setFunctionIndex(FunctionIndex++); 952 }; 953 954 for (InputFunction *Func : Symtab->SyntheticFunctions) 955 AddDefinedFunction(Func); 956 957 for (ObjFile *File : Symtab->ObjectFiles) { 958 LLVM_DEBUG(dbgs() << "Functions: " << File->getName() << "\n"); 959 for (InputFunction *Func : File->Functions) 960 AddDefinedFunction(Func); 961 } 962 963 uint32_t TableIndex = TableBase; 964 auto HandleRelocs = [&](InputChunk *Chunk) { 965 if (!Chunk->Live) 966 return; 967 ObjFile *File = Chunk->File; 968 ArrayRef<WasmSignature> Types = File->getWasmObj()->types(); 969 for (const WasmRelocation &Reloc : Chunk->getRelocations()) { 970 if (Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_I32 || 971 Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_SLEB) { 972 FunctionSymbol *Sym = File->getFunctionSymbol(Reloc.Index); 973 if (Sym->hasTableIndex() || !Sym->hasFunctionIndex()) 974 continue; 975 Sym->setTableIndex(TableIndex++); 976 IndirectFunctions.emplace_back(Sym); 977 } else if (Reloc.Type == R_WEBASSEMBLY_TYPE_INDEX_LEB) { 978 // Mark target type as live 979 File->TypeMap[Reloc.Index] = registerType(Types[Reloc.Index]); 980 File->TypeIsUsed[Reloc.Index] = true; 981 } 982 } 983 }; 984 985 for (ObjFile *File : Symtab->ObjectFiles) { 986 LLVM_DEBUG(dbgs() << "Handle relocs: " << File->getName() << "\n"); 987 for (InputChunk *Chunk : File->Functions) 988 HandleRelocs(Chunk); 989 for (InputChunk *Chunk : File->Segments) 990 HandleRelocs(Chunk); 991 for (auto &P : File->CustomSections) 992 HandleRelocs(P); 993 } 994 995 assert(InputGlobals.empty()); 996 uint32_t GlobalIndex = NumImportedGlobals; 997 auto AddDefinedGlobal = [&](InputGlobal *Global) { 998 if (Global->Live) { 999 LLVM_DEBUG(dbgs() << "AddDefinedGlobal: " << GlobalIndex << "\n"); 1000 Global->setGlobalIndex(GlobalIndex++); 1001 InputGlobals.push_back(Global); 1002 } 1003 }; 1004 1005 for (InputGlobal *Global : Symtab->SyntheticGlobals) 1006 AddDefinedGlobal(Global); 1007 1008 for (ObjFile *File : Symtab->ObjectFiles) { 1009 LLVM_DEBUG(dbgs() << "Globals: " << File->getName() << "\n"); 1010 for (InputGlobal *Global : File->Globals) 1011 AddDefinedGlobal(Global); 1012 } 1013 1014 assert(InputEvents.empty()); 1015 uint32_t EventIndex = NumImportedEvents; 1016 auto AddDefinedEvent = [&](InputEvent *Event) { 1017 if (Event->Live) { 1018 LLVM_DEBUG(dbgs() << "AddDefinedEvent: " << EventIndex << "\n"); 1019 Event->setEventIndex(EventIndex++); 1020 InputEvents.push_back(Event); 1021 } 1022 }; 1023 1024 for (ObjFile *File : Symtab->ObjectFiles) { 1025 LLVM_DEBUG(dbgs() << "Events: " << File->getName() << "\n"); 1026 for (InputEvent *Event : File->Events) 1027 AddDefinedEvent(Event); 1028 } 1029 } 1030 1031 static StringRef getOutputDataSegmentName(StringRef Name) { 1032 // With PIC code we currently only support a single data segment since 1033 // we only have a single __memory_base to use as our base address. 1034 if (Config->Pic) 1035 return "data"; 1036 if (!Config->MergeDataSegments) 1037 return Name; 1038 if (Name.startswith(".text.")) 1039 return ".text"; 1040 if (Name.startswith(".data.")) 1041 return ".data"; 1042 if (Name.startswith(".bss.")) 1043 return ".bss"; 1044 if (Name.startswith(".rodata.")) 1045 return ".rodata"; 1046 return Name; 1047 } 1048 1049 void Writer::createOutputSegments() { 1050 for (ObjFile *File : Symtab->ObjectFiles) { 1051 for (InputSegment *Segment : File->Segments) { 1052 if (!Segment->Live) 1053 continue; 1054 StringRef Name = getOutputDataSegmentName(Segment->getName()); 1055 OutputSegment *&S = SegmentMap[Name]; 1056 if (S == nullptr) { 1057 LLVM_DEBUG(dbgs() << "new segment: " << Name << "\n"); 1058 S = make<OutputSegment>(Name, Segments.size()); 1059 Segments.push_back(S); 1060 } 1061 S->addInputSegment(Segment); 1062 LLVM_DEBUG(dbgs() << "added data: " << Name << ": " << S->Size << "\n"); 1063 } 1064 } 1065 } 1066 1067 static const int OPCODE_CALL = 0x10; 1068 static const int OPCODE_END = 0xb; 1069 1070 // Create synthetic "__wasm_call_ctors" function based on ctor functions 1071 // in input object. 1072 void Writer::createCtorFunction() { 1073 // First write the body's contents to a string. 1074 std::string BodyContent; 1075 { 1076 raw_string_ostream OS(BodyContent); 1077 writeUleb128(OS, 0, "num locals"); 1078 for (const WasmInitEntry &F : InitFunctions) { 1079 writeU8(OS, OPCODE_CALL, "CALL"); 1080 writeUleb128(OS, F.Sym->getFunctionIndex(), "function index"); 1081 } 1082 writeU8(OS, OPCODE_END, "END"); 1083 } 1084 1085 // Once we know the size of the body we can create the final function body 1086 std::string FunctionBody; 1087 { 1088 raw_string_ostream OS(FunctionBody); 1089 writeUleb128(OS, BodyContent.size(), "function size"); 1090 OS << BodyContent; 1091 } 1092 1093 ArrayRef<uint8_t> Body = arrayRefFromStringRef(Saver.save(FunctionBody)); 1094 cast<SyntheticFunction>(WasmSym::CallCtors->Function)->setBody(Body); 1095 } 1096 1097 // Populate InitFunctions vector with init functions from all input objects. 1098 // This is then used either when creating the output linking section or to 1099 // synthesize the "__wasm_call_ctors" function. 1100 void Writer::calculateInitFunctions() { 1101 for (ObjFile *File : Symtab->ObjectFiles) { 1102 const WasmLinkingData &L = File->getWasmObj()->linkingData(); 1103 for (const WasmInitFunc &F : L.InitFunctions) { 1104 FunctionSymbol *Sym = File->getFunctionSymbol(F.Symbol); 1105 if (*Sym->Signature != WasmSignature{{}, {}}) 1106 error("invalid signature for init func: " + toString(*Sym)); 1107 InitFunctions.emplace_back(WasmInitEntry{Sym, F.Priority}); 1108 } 1109 } 1110 1111 // Sort in order of priority (lowest first) so that they are called 1112 // in the correct order. 1113 std::stable_sort(InitFunctions.begin(), InitFunctions.end(), 1114 [](const WasmInitEntry &L, const WasmInitEntry &R) { 1115 return L.Priority < R.Priority; 1116 }); 1117 } 1118 1119 void Writer::run() { 1120 if (Config->Relocatable || Config->Pic) 1121 Config->GlobalBase = 0; 1122 1123 // For PIC code the table base is assigned dynamically by the loader. 1124 // For non-PIC, we start at 1 so that accessing table index 0 always traps. 1125 if (!Config->Pic) 1126 TableBase = 1; 1127 1128 log("-- calculateImports"); 1129 calculateImports(); 1130 log("-- assignIndexes"); 1131 assignIndexes(); 1132 log("-- calculateInitFunctions"); 1133 calculateInitFunctions(); 1134 if (!Config->Relocatable) 1135 createCtorFunction(); 1136 log("-- calculateTypes"); 1137 calculateTypes(); 1138 log("-- layoutMemory"); 1139 layoutMemory(); 1140 log("-- calculateExports"); 1141 calculateExports(); 1142 log("-- calculateCustomSections"); 1143 calculateCustomSections(); 1144 log("-- assignSymtab"); 1145 assignSymtab(); 1146 1147 if (errorHandler().Verbose) { 1148 log("Defined Functions: " + Twine(InputFunctions.size())); 1149 log("Defined Globals : " + Twine(InputGlobals.size())); 1150 log("Defined Events : " + Twine(InputEvents.size())); 1151 log("Function Imports : " + Twine(NumImportedFunctions)); 1152 log("Global Imports : " + Twine(NumImportedGlobals)); 1153 log("Event Imports : " + Twine(NumImportedEvents)); 1154 for (ObjFile *File : Symtab->ObjectFiles) 1155 File->dumpInfo(); 1156 } 1157 1158 createHeader(); 1159 log("-- createSections"); 1160 createSections(); 1161 1162 log("-- openFile"); 1163 openFile(); 1164 if (errorCount()) 1165 return; 1166 1167 writeHeader(); 1168 1169 log("-- writeSections"); 1170 writeSections(); 1171 if (errorCount()) 1172 return; 1173 1174 if (Error E = Buffer->commit()) 1175 fatal("failed to write the output file: " + toString(std::move(E))); 1176 } 1177 1178 // Open a result file. 1179 void Writer::openFile() { 1180 log("writing: " + Config->OutputFile); 1181 1182 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = 1183 FileOutputBuffer::create(Config->OutputFile, FileSize, 1184 FileOutputBuffer::F_executable); 1185 1186 if (!BufferOrErr) 1187 error("failed to open " + Config->OutputFile + ": " + 1188 toString(BufferOrErr.takeError())); 1189 else 1190 Buffer = std::move(*BufferOrErr); 1191 } 1192 1193 void Writer::createHeader() { 1194 raw_string_ostream OS(Header); 1195 writeBytes(OS, WasmMagic, sizeof(WasmMagic), "wasm magic"); 1196 writeU32(OS, WasmVersion, "wasm version"); 1197 OS.flush(); 1198 FileSize += Header.size(); 1199 } 1200 1201 void lld::wasm::writeResult() { Writer().run(); } 1202