1 //===- lib/MC/WasmObjectWriter.cpp - Wasm File Writer ---------------------===// 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 // This file implements Wasm object file writer information. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/ADT/STLExtras.h" 14 #include "llvm/ADT/SmallPtrSet.h" 15 #include "llvm/BinaryFormat/Wasm.h" 16 #include "llvm/Config/llvm-config.h" 17 #include "llvm/MC/MCAsmBackend.h" 18 #include "llvm/MC/MCAsmLayout.h" 19 #include "llvm/MC/MCAssembler.h" 20 #include "llvm/MC/MCContext.h" 21 #include "llvm/MC/MCExpr.h" 22 #include "llvm/MC/MCFixupKindInfo.h" 23 #include "llvm/MC/MCObjectWriter.h" 24 #include "llvm/MC/MCSectionWasm.h" 25 #include "llvm/MC/MCSymbolWasm.h" 26 #include "llvm/MC/MCValue.h" 27 #include "llvm/MC/MCWasmObjectWriter.h" 28 #include "llvm/Support/Casting.h" 29 #include "llvm/Support/Debug.h" 30 #include "llvm/Support/EndianStream.h" 31 #include "llvm/Support/ErrorHandling.h" 32 #include "llvm/Support/LEB128.h" 33 #include "llvm/Support/StringSaver.h" 34 #include <vector> 35 36 using namespace llvm; 37 38 #define DEBUG_TYPE "mc" 39 40 namespace { 41 42 // Went we ceate the indirect function table we start at 1, so that there is 43 // and emtpy slot at 0 and therefore calling a null function pointer will trap. 44 static const uint32_t InitialTableOffset = 1; 45 46 // For patching purposes, we need to remember where each section starts, both 47 // for patching up the section size field, and for patching up references to 48 // locations within the section. 49 struct SectionBookkeeping { 50 // Where the size of the section is written. 51 uint64_t SizeOffset; 52 // Where the section header ends (without custom section name). 53 uint64_t PayloadOffset; 54 // Where the contents of the section starts. 55 uint64_t ContentsOffset; 56 uint32_t Index; 57 }; 58 59 // The signature of a wasm function or event, in a struct capable of being used 60 // as a DenseMap key. 61 // TODO: Consider using wasm::WasmSignature directly instead. 62 struct WasmSignature { 63 // Support empty and tombstone instances, needed by DenseMap. 64 enum { Plain, Empty, Tombstone } State = Plain; 65 66 // The return types of the function. 67 SmallVector<wasm::ValType, 1> Returns; 68 69 // The parameter types of the function. 70 SmallVector<wasm::ValType, 4> Params; 71 72 bool operator==(const WasmSignature &Other) const { 73 return State == Other.State && Returns == Other.Returns && 74 Params == Other.Params; 75 } 76 }; 77 78 // Traits for using WasmSignature in a DenseMap. 79 struct WasmSignatureDenseMapInfo { 80 static WasmSignature getEmptyKey() { 81 WasmSignature Sig; 82 Sig.State = WasmSignature::Empty; 83 return Sig; 84 } 85 static WasmSignature getTombstoneKey() { 86 WasmSignature Sig; 87 Sig.State = WasmSignature::Tombstone; 88 return Sig; 89 } 90 static unsigned getHashValue(const WasmSignature &Sig) { 91 uintptr_t Value = Sig.State; 92 for (wasm::ValType Ret : Sig.Returns) 93 Value += DenseMapInfo<uint32_t>::getHashValue(uint32_t(Ret)); 94 for (wasm::ValType Param : Sig.Params) 95 Value += DenseMapInfo<uint32_t>::getHashValue(uint32_t(Param)); 96 return Value; 97 } 98 static bool isEqual(const WasmSignature &LHS, const WasmSignature &RHS) { 99 return LHS == RHS; 100 } 101 }; 102 103 // A wasm data segment. A wasm binary contains only a single data section 104 // but that can contain many segments, each with their own virtual location 105 // in memory. Each MCSection data created by llvm is modeled as its own 106 // wasm data segment. 107 struct WasmDataSegment { 108 MCSectionWasm *Section; 109 StringRef Name; 110 uint32_t InitFlags; 111 uint32_t Offset; 112 uint32_t Alignment; 113 uint32_t LinkerFlags; 114 SmallVector<char, 4> Data; 115 }; 116 117 // A wasm function to be written into the function section. 118 struct WasmFunction { 119 uint32_t SigIndex; 120 const MCSymbolWasm *Sym; 121 }; 122 123 // A wasm global to be written into the global section. 124 struct WasmGlobal { 125 wasm::WasmGlobalType Type; 126 uint64_t InitialValue; 127 }; 128 129 // Information about a single item which is part of a COMDAT. For each data 130 // segment or function which is in the COMDAT, there is a corresponding 131 // WasmComdatEntry. 132 struct WasmComdatEntry { 133 unsigned Kind; 134 uint32_t Index; 135 }; 136 137 // Information about a single relocation. 138 struct WasmRelocationEntry { 139 uint64_t Offset; // Where is the relocation. 140 const MCSymbolWasm *Symbol; // The symbol to relocate with. 141 int64_t Addend; // A value to add to the symbol. 142 unsigned Type; // The type of the relocation. 143 const MCSectionWasm *FixupSection; // The section the relocation is targeting. 144 145 WasmRelocationEntry(uint64_t Offset, const MCSymbolWasm *Symbol, 146 int64_t Addend, unsigned Type, 147 const MCSectionWasm *FixupSection) 148 : Offset(Offset), Symbol(Symbol), Addend(Addend), Type(Type), 149 FixupSection(FixupSection) {} 150 151 bool hasAddend() const { return wasm::relocTypeHasAddend(Type); } 152 153 void print(raw_ostream &Out) const { 154 Out << wasm::relocTypetoString(Type) << " Off=" << Offset 155 << ", Sym=" << *Symbol << ", Addend=" << Addend 156 << ", FixupSection=" << FixupSection->getName(); 157 } 158 159 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 160 LLVM_DUMP_METHOD void dump() const { print(dbgs()); } 161 #endif 162 }; 163 164 static const uint32_t InvalidIndex = -1; 165 166 struct WasmCustomSection { 167 168 StringRef Name; 169 MCSectionWasm *Section; 170 171 uint32_t OutputContentsOffset; 172 uint32_t OutputIndex; 173 174 WasmCustomSection(StringRef Name, MCSectionWasm *Section) 175 : Name(Name), Section(Section), OutputContentsOffset(0), 176 OutputIndex(InvalidIndex) {} 177 }; 178 179 #if !defined(NDEBUG) 180 raw_ostream &operator<<(raw_ostream &OS, const WasmRelocationEntry &Rel) { 181 Rel.print(OS); 182 return OS; 183 } 184 #endif 185 186 // Write X as an (unsigned) LEB value at offset Offset in Stream, padded 187 // to allow patching. 188 static void writePatchableLEB(raw_pwrite_stream &Stream, uint32_t X, 189 uint64_t Offset) { 190 uint8_t Buffer[5]; 191 unsigned SizeLen = encodeULEB128(X, Buffer, 5); 192 assert(SizeLen == 5); 193 Stream.pwrite((char *)Buffer, SizeLen, Offset); 194 } 195 196 // Write X as an signed LEB value at offset Offset in Stream, padded 197 // to allow patching. 198 static void writePatchableSLEB(raw_pwrite_stream &Stream, int32_t X, 199 uint64_t Offset) { 200 uint8_t Buffer[5]; 201 unsigned SizeLen = encodeSLEB128(X, Buffer, 5); 202 assert(SizeLen == 5); 203 Stream.pwrite((char *)Buffer, SizeLen, Offset); 204 } 205 206 // Write X as a plain integer value at offset Offset in Stream. 207 static void patchI32(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) { 208 uint8_t Buffer[4]; 209 support::endian::write32le(Buffer, X); 210 Stream.pwrite((char *)Buffer, sizeof(Buffer), Offset); 211 } 212 213 class WasmObjectWriter : public MCObjectWriter { 214 support::endian::Writer W; 215 216 /// The target specific Wasm writer instance. 217 std::unique_ptr<MCWasmObjectTargetWriter> TargetObjectWriter; 218 219 // Relocations for fixing up references in the code section. 220 std::vector<WasmRelocationEntry> CodeRelocations; 221 uint32_t CodeSectionIndex; 222 223 // Relocations for fixing up references in the data section. 224 std::vector<WasmRelocationEntry> DataRelocations; 225 uint32_t DataSectionIndex; 226 227 // Index values to use for fixing up call_indirect type indices. 228 // Maps function symbols to the index of the type of the function 229 DenseMap<const MCSymbolWasm *, uint32_t> TypeIndices; 230 // Maps function symbols to the table element index space. Used 231 // for TABLE_INDEX relocation types (i.e. address taken functions). 232 DenseMap<const MCSymbolWasm *, uint32_t> TableIndices; 233 // Maps function/global symbols to the function/global/event/section index 234 // space. 235 DenseMap<const MCSymbolWasm *, uint32_t> WasmIndices; 236 DenseMap<const MCSymbolWasm *, uint32_t> GOTIndices; 237 // Maps data symbols to the Wasm segment and offset/size with the segment. 238 DenseMap<const MCSymbolWasm *, wasm::WasmDataReference> DataLocations; 239 240 // Stores output data (index, relocations, content offset) for custom 241 // section. 242 std::vector<WasmCustomSection> CustomSections; 243 std::unique_ptr<WasmCustomSection> ProducersSection; 244 std::unique_ptr<WasmCustomSection> TargetFeaturesSection; 245 // Relocations for fixing up references in the custom sections. 246 DenseMap<const MCSectionWasm *, std::vector<WasmRelocationEntry>> 247 CustomSectionsRelocations; 248 249 // Map from section to defining function symbol. 250 DenseMap<const MCSection *, const MCSymbol *> SectionFunctions; 251 252 DenseMap<WasmSignature, uint32_t, WasmSignatureDenseMapInfo> SignatureIndices; 253 SmallVector<WasmSignature, 4> Signatures; 254 SmallVector<WasmDataSegment, 4> DataSegments; 255 unsigned NumFunctionImports = 0; 256 unsigned NumGlobalImports = 0; 257 unsigned NumEventImports = 0; 258 uint32_t SectionCount = 0; 259 260 // TargetObjectWriter wrappers. 261 bool is64Bit() const { return TargetObjectWriter->is64Bit(); } 262 bool isEmscripten() const { return TargetObjectWriter->isEmscripten(); } 263 264 void startSection(SectionBookkeeping &Section, unsigned SectionId); 265 void startCustomSection(SectionBookkeeping &Section, StringRef Name); 266 void endSection(SectionBookkeeping &Section); 267 268 public: 269 WasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW, 270 raw_pwrite_stream &OS) 271 : W(OS, support::little), TargetObjectWriter(std::move(MOTW)) {} 272 273 private: 274 void reset() override { 275 CodeRelocations.clear(); 276 DataRelocations.clear(); 277 TypeIndices.clear(); 278 WasmIndices.clear(); 279 GOTIndices.clear(); 280 TableIndices.clear(); 281 DataLocations.clear(); 282 CustomSections.clear(); 283 ProducersSection.reset(); 284 TargetFeaturesSection.reset(); 285 CustomSectionsRelocations.clear(); 286 SignatureIndices.clear(); 287 Signatures.clear(); 288 DataSegments.clear(); 289 SectionFunctions.clear(); 290 NumFunctionImports = 0; 291 NumGlobalImports = 0; 292 MCObjectWriter::reset(); 293 } 294 295 void writeHeader(const MCAssembler &Asm); 296 297 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, 298 const MCFragment *Fragment, const MCFixup &Fixup, 299 MCValue Target, uint64_t &FixedValue) override; 300 301 void executePostLayoutBinding(MCAssembler &Asm, 302 const MCAsmLayout &Layout) override; 303 304 uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override; 305 306 void writeString(const StringRef Str) { 307 encodeULEB128(Str.size(), W.OS); 308 W.OS << Str; 309 } 310 311 void writeI32(int32_t val) { 312 char Buffer[4]; 313 support::endian::write32le(Buffer, val); 314 W.OS.write(Buffer, sizeof(Buffer)); 315 } 316 317 void writeI64(int64_t val) { 318 char Buffer[8]; 319 support::endian::write64le(Buffer, val); 320 W.OS.write(Buffer, sizeof(Buffer)); 321 } 322 323 void writeValueType(wasm::ValType Ty) { W.OS << static_cast<char>(Ty); } 324 325 void writeTypeSection(ArrayRef<WasmSignature> Signatures); 326 void writeImportSection(ArrayRef<wasm::WasmImport> Imports, uint32_t DataSize, 327 uint32_t NumElements); 328 void writeFunctionSection(ArrayRef<WasmFunction> Functions); 329 void writeExportSection(ArrayRef<wasm::WasmExport> Exports); 330 void writeElemSection(ArrayRef<uint32_t> TableElems); 331 void writeDataCountSection(); 332 void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout, 333 ArrayRef<WasmFunction> Functions); 334 void writeDataSection(); 335 void writeEventSection(ArrayRef<wasm::WasmEventType> Events); 336 void writeGlobalSection(ArrayRef<wasm::WasmGlobal> Globals); 337 void writeRelocSection(uint32_t SectionIndex, StringRef Name, 338 std::vector<WasmRelocationEntry> &Relocations); 339 void writeLinkingMetaDataSection( 340 ArrayRef<wasm::WasmSymbolInfo> SymbolInfos, 341 ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs, 342 const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats); 343 void writeCustomSection(WasmCustomSection &CustomSection, 344 const MCAssembler &Asm, const MCAsmLayout &Layout); 345 void writeCustomRelocSections(); 346 void 347 updateCustomSectionRelocations(const SmallVector<WasmFunction, 4> &Functions, 348 const MCAsmLayout &Layout); 349 350 uint32_t getProvisionalValue(const WasmRelocationEntry &RelEntry); 351 void applyRelocations(ArrayRef<WasmRelocationEntry> Relocations, 352 uint64_t ContentsOffset); 353 354 uint32_t getRelocationIndexValue(const WasmRelocationEntry &RelEntry); 355 uint32_t getFunctionType(const MCSymbolWasm &Symbol); 356 uint32_t getEventType(const MCSymbolWasm &Symbol); 357 void registerFunctionType(const MCSymbolWasm &Symbol); 358 void registerEventType(const MCSymbolWasm &Symbol); 359 }; 360 361 } // end anonymous namespace 362 363 // Write out a section header and a patchable section size field. 364 void WasmObjectWriter::startSection(SectionBookkeeping &Section, 365 unsigned SectionId) { 366 LLVM_DEBUG(dbgs() << "startSection " << SectionId << "\n"); 367 W.OS << char(SectionId); 368 369 Section.SizeOffset = W.OS.tell(); 370 371 // The section size. We don't know the size yet, so reserve enough space 372 // for any 32-bit value; we'll patch it later. 373 encodeULEB128(0, W.OS, 5); 374 375 // The position where the section starts, for measuring its size. 376 Section.ContentsOffset = W.OS.tell(); 377 Section.PayloadOffset = W.OS.tell(); 378 Section.Index = SectionCount++; 379 } 380 381 void WasmObjectWriter::startCustomSection(SectionBookkeeping &Section, 382 StringRef Name) { 383 LLVM_DEBUG(dbgs() << "startCustomSection " << Name << "\n"); 384 startSection(Section, wasm::WASM_SEC_CUSTOM); 385 386 // The position where the section header ends, for measuring its size. 387 Section.PayloadOffset = W.OS.tell(); 388 389 // Custom sections in wasm also have a string identifier. 390 writeString(Name); 391 392 // The position where the custom section starts. 393 Section.ContentsOffset = W.OS.tell(); 394 } 395 396 // Now that the section is complete and we know how big it is, patch up the 397 // section size field at the start of the section. 398 void WasmObjectWriter::endSection(SectionBookkeeping &Section) { 399 uint64_t Size = W.OS.tell(); 400 // /dev/null doesn't support seek/tell and can report offset of 0. 401 // Simply skip this patching in that case. 402 if (!Size) 403 return; 404 405 Size -= Section.PayloadOffset; 406 if (uint32_t(Size) != Size) 407 report_fatal_error("section size does not fit in a uint32_t"); 408 409 LLVM_DEBUG(dbgs() << "endSection size=" << Size << "\n"); 410 411 // Write the final section size to the payload_len field, which follows 412 // the section id byte. 413 writePatchableLEB(static_cast<raw_pwrite_stream &>(W.OS), Size, 414 Section.SizeOffset); 415 } 416 417 // Emit the Wasm header. 418 void WasmObjectWriter::writeHeader(const MCAssembler &Asm) { 419 W.OS.write(wasm::WasmMagic, sizeof(wasm::WasmMagic)); 420 W.write<uint32_t>(wasm::WasmVersion); 421 } 422 423 void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm, 424 const MCAsmLayout &Layout) { 425 // Build a map of sections to the function that defines them, for use 426 // in recordRelocation. 427 for (const MCSymbol &S : Asm.symbols()) { 428 const auto &WS = static_cast<const MCSymbolWasm &>(S); 429 if (WS.isDefined() && WS.isFunction() && !WS.isVariable()) { 430 const auto &Sec = static_cast<const MCSectionWasm &>(S.getSection()); 431 auto Pair = SectionFunctions.insert(std::make_pair(&Sec, &S)); 432 if (!Pair.second) 433 report_fatal_error("section already has a defining function: " + 434 Sec.getName()); 435 } 436 } 437 } 438 439 void WasmObjectWriter::recordRelocation(MCAssembler &Asm, 440 const MCAsmLayout &Layout, 441 const MCFragment *Fragment, 442 const MCFixup &Fixup, MCValue Target, 443 uint64_t &FixedValue) { 444 // The WebAssembly backend should never generate FKF_IsPCRel fixups 445 assert(!(Asm.getBackend().getFixupKindInfo(Fixup.getKind()).Flags & 446 MCFixupKindInfo::FKF_IsPCRel)); 447 448 const auto &FixupSection = cast<MCSectionWasm>(*Fragment->getParent()); 449 uint64_t C = Target.getConstant(); 450 uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); 451 MCContext &Ctx = Asm.getContext(); 452 453 if (const MCSymbolRefExpr *RefB = Target.getSymB()) { 454 // To get here the A - B expression must have failed evaluateAsRelocatable. 455 // This means either A or B must be undefined and in WebAssembly we can't 456 // support either of those cases. 457 const auto &SymB = cast<MCSymbolWasm>(RefB->getSymbol()); 458 Ctx.reportError( 459 Fixup.getLoc(), 460 Twine("symbol '") + SymB.getName() + 461 "': unsupported subtraction expression used in relocation."); 462 return; 463 } 464 465 // We either rejected the fixup or folded B into C at this point. 466 const MCSymbolRefExpr *RefA = Target.getSymA(); 467 const auto *SymA = cast<MCSymbolWasm>(&RefA->getSymbol()); 468 469 // The .init_array isn't translated as data, so don't do relocations in it. 470 if (FixupSection.getName().startswith(".init_array")) { 471 SymA->setUsedInInitArray(); 472 return; 473 } 474 475 if (SymA->isVariable()) { 476 const MCExpr *Expr = SymA->getVariableValue(); 477 const auto *Inner = cast<MCSymbolRefExpr>(Expr); 478 if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) 479 llvm_unreachable("weakref used in reloc not yet implemented"); 480 } 481 482 // Put any constant offset in an addend. Offsets can be negative, and 483 // LLVM expects wrapping, in contrast to wasm's immediates which can't 484 // be negative and don't wrap. 485 FixedValue = 0; 486 487 unsigned Type = TargetObjectWriter->getRelocType(Target, Fixup); 488 489 // Absolute offset within a section or a function. 490 // Currently only supported for for metadata sections. 491 // See: test/MC/WebAssembly/blockaddress.ll 492 if (Type == wasm::R_WASM_FUNCTION_OFFSET_I32 || 493 Type == wasm::R_WASM_SECTION_OFFSET_I32) { 494 if (!FixupSection.getKind().isMetadata()) 495 report_fatal_error("relocations for function or section offsets are " 496 "only supported in metadata sections"); 497 498 const MCSymbol *SectionSymbol = nullptr; 499 const MCSection &SecA = SymA->getSection(); 500 if (SecA.getKind().isText()) 501 SectionSymbol = SectionFunctions.find(&SecA)->second; 502 else 503 SectionSymbol = SecA.getBeginSymbol(); 504 if (!SectionSymbol) 505 report_fatal_error("section symbol is required for relocation"); 506 507 C += Layout.getSymbolOffset(*SymA); 508 SymA = cast<MCSymbolWasm>(SectionSymbol); 509 } 510 511 // Relocation other than R_WASM_TYPE_INDEX_LEB are required to be 512 // against a named symbol. 513 if (Type != wasm::R_WASM_TYPE_INDEX_LEB) { 514 if (SymA->getName().empty()) 515 report_fatal_error("relocations against un-named temporaries are not yet " 516 "supported by wasm"); 517 518 SymA->setUsedInReloc(); 519 } 520 521 if (RefA->getKind() == MCSymbolRefExpr::VK_GOT) 522 SymA->setUsedInGOT(); 523 524 WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection); 525 LLVM_DEBUG(dbgs() << "WasmReloc: " << Rec << "\n"); 526 527 if (FixupSection.isWasmData()) { 528 DataRelocations.push_back(Rec); 529 } else if (FixupSection.getKind().isText()) { 530 CodeRelocations.push_back(Rec); 531 } else if (FixupSection.getKind().isMetadata()) { 532 CustomSectionsRelocations[&FixupSection].push_back(Rec); 533 } else { 534 llvm_unreachable("unexpected section type"); 535 } 536 } 537 538 static const MCSymbolWasm *resolveSymbol(const MCSymbolWasm &Symbol) { 539 const MCSymbolWasm* Ret = &Symbol; 540 while (Ret->isVariable()) { 541 const MCExpr *Expr = Ret->getVariableValue(); 542 auto *Inner = cast<MCSymbolRefExpr>(Expr); 543 Ret = cast<MCSymbolWasm>(&Inner->getSymbol()); 544 } 545 return Ret; 546 } 547 548 // Compute a value to write into the code at the location covered 549 // by RelEntry. This value isn't used by the static linker; it just serves 550 // to make the object format more readable and more likely to be directly 551 // useable. 552 uint32_t 553 WasmObjectWriter::getProvisionalValue(const WasmRelocationEntry &RelEntry) { 554 if ((RelEntry.Type == wasm::R_WASM_GLOBAL_INDEX_LEB || 555 RelEntry.Type == wasm::R_WASM_GLOBAL_INDEX_I32) && 556 !RelEntry.Symbol->isGlobal()) { 557 assert(GOTIndices.count(RelEntry.Symbol) > 0 && "symbol not found in GOT index space"); 558 return GOTIndices[RelEntry.Symbol]; 559 } 560 561 switch (RelEntry.Type) { 562 case wasm::R_WASM_TABLE_INDEX_REL_SLEB: 563 case wasm::R_WASM_TABLE_INDEX_SLEB: 564 case wasm::R_WASM_TABLE_INDEX_I32: { 565 // Provisional value is table address of the resolved symbol itself 566 const MCSymbolWasm *Sym = resolveSymbol(*RelEntry.Symbol); 567 assert(Sym->isFunction()); 568 return TableIndices[Sym]; 569 } 570 case wasm::R_WASM_TYPE_INDEX_LEB: 571 // Provisional value is same as the index 572 return getRelocationIndexValue(RelEntry); 573 case wasm::R_WASM_FUNCTION_INDEX_LEB: 574 case wasm::R_WASM_GLOBAL_INDEX_LEB: 575 case wasm::R_WASM_GLOBAL_INDEX_I32: 576 case wasm::R_WASM_EVENT_INDEX_LEB: 577 // Provisional value is function/global/event Wasm index 578 assert(WasmIndices.count(RelEntry.Symbol) > 0 && "symbol not found in wasm index space"); 579 return WasmIndices[RelEntry.Symbol]; 580 case wasm::R_WASM_FUNCTION_OFFSET_I32: 581 case wasm::R_WASM_SECTION_OFFSET_I32: { 582 const auto &Section = 583 static_cast<const MCSectionWasm &>(RelEntry.Symbol->getSection()); 584 return Section.getSectionOffset() + RelEntry.Addend; 585 } 586 case wasm::R_WASM_MEMORY_ADDR_LEB: 587 case wasm::R_WASM_MEMORY_ADDR_I32: 588 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB: 589 case wasm::R_WASM_MEMORY_ADDR_SLEB: { 590 // Provisional value is address of the global 591 const MCSymbolWasm *Sym = resolveSymbol(*RelEntry.Symbol); 592 // For undefined symbols, use zero 593 if (!Sym->isDefined()) 594 return 0; 595 const wasm::WasmDataReference &Ref = DataLocations[Sym]; 596 const WasmDataSegment &Segment = DataSegments[Ref.Segment]; 597 // Ignore overflow. LLVM allows address arithmetic to silently wrap. 598 return Segment.Offset + Ref.Offset + RelEntry.Addend; 599 } 600 default: 601 llvm_unreachable("invalid relocation type"); 602 } 603 } 604 605 static void addData(SmallVectorImpl<char> &DataBytes, 606 MCSectionWasm &DataSection) { 607 LLVM_DEBUG(errs() << "addData: " << DataSection.getName() << "\n"); 608 609 DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment())); 610 611 for (const MCFragment &Frag : DataSection) { 612 if (Frag.hasInstructions()) 613 report_fatal_error("only data supported in data sections"); 614 615 if (auto *Align = dyn_cast<MCAlignFragment>(&Frag)) { 616 if (Align->getValueSize() != 1) 617 report_fatal_error("only byte values supported for alignment"); 618 // If nops are requested, use zeros, as this is the data section. 619 uint8_t Value = Align->hasEmitNops() ? 0 : Align->getValue(); 620 uint64_t Size = 621 std::min<uint64_t>(alignTo(DataBytes.size(), Align->getAlignment()), 622 DataBytes.size() + Align->getMaxBytesToEmit()); 623 DataBytes.resize(Size, Value); 624 } else if (auto *Fill = dyn_cast<MCFillFragment>(&Frag)) { 625 int64_t NumValues; 626 if (!Fill->getNumValues().evaluateAsAbsolute(NumValues)) 627 llvm_unreachable("The fill should be an assembler constant"); 628 DataBytes.insert(DataBytes.end(), Fill->getValueSize() * NumValues, 629 Fill->getValue()); 630 } else if (auto *LEB = dyn_cast<MCLEBFragment>(&Frag)) { 631 const SmallVectorImpl<char> &Contents = LEB->getContents(); 632 DataBytes.insert(DataBytes.end(), Contents.begin(), Contents.end()); 633 } else { 634 const auto &DataFrag = cast<MCDataFragment>(Frag); 635 const SmallVectorImpl<char> &Contents = DataFrag.getContents(); 636 DataBytes.insert(DataBytes.end(), Contents.begin(), Contents.end()); 637 } 638 } 639 640 LLVM_DEBUG(dbgs() << "addData -> " << DataBytes.size() << "\n"); 641 } 642 643 uint32_t 644 WasmObjectWriter::getRelocationIndexValue(const WasmRelocationEntry &RelEntry) { 645 if (RelEntry.Type == wasm::R_WASM_TYPE_INDEX_LEB) { 646 if (!TypeIndices.count(RelEntry.Symbol)) 647 report_fatal_error("symbol not found in type index space: " + 648 RelEntry.Symbol->getName()); 649 return TypeIndices[RelEntry.Symbol]; 650 } 651 652 return RelEntry.Symbol->getIndex(); 653 } 654 655 // Apply the portions of the relocation records that we can handle ourselves 656 // directly. 657 void WasmObjectWriter::applyRelocations( 658 ArrayRef<WasmRelocationEntry> Relocations, uint64_t ContentsOffset) { 659 auto &Stream = static_cast<raw_pwrite_stream &>(W.OS); 660 for (const WasmRelocationEntry &RelEntry : Relocations) { 661 uint64_t Offset = ContentsOffset + 662 RelEntry.FixupSection->getSectionOffset() + 663 RelEntry.Offset; 664 665 LLVM_DEBUG(dbgs() << "applyRelocation: " << RelEntry << "\n"); 666 uint32_t Value = getProvisionalValue(RelEntry); 667 668 switch (RelEntry.Type) { 669 case wasm::R_WASM_FUNCTION_INDEX_LEB: 670 case wasm::R_WASM_TYPE_INDEX_LEB: 671 case wasm::R_WASM_GLOBAL_INDEX_LEB: 672 case wasm::R_WASM_MEMORY_ADDR_LEB: 673 case wasm::R_WASM_EVENT_INDEX_LEB: 674 writePatchableLEB(Stream, Value, Offset); 675 break; 676 case wasm::R_WASM_TABLE_INDEX_I32: 677 case wasm::R_WASM_MEMORY_ADDR_I32: 678 case wasm::R_WASM_FUNCTION_OFFSET_I32: 679 case wasm::R_WASM_SECTION_OFFSET_I32: 680 case wasm::R_WASM_GLOBAL_INDEX_I32: 681 patchI32(Stream, Value, Offset); 682 break; 683 case wasm::R_WASM_TABLE_INDEX_SLEB: 684 case wasm::R_WASM_TABLE_INDEX_REL_SLEB: 685 case wasm::R_WASM_MEMORY_ADDR_SLEB: 686 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB: 687 writePatchableSLEB(Stream, Value, Offset); 688 break; 689 default: 690 llvm_unreachable("invalid relocation type"); 691 } 692 } 693 } 694 695 void WasmObjectWriter::writeTypeSection(ArrayRef<WasmSignature> Signatures) { 696 if (Signatures.empty()) 697 return; 698 699 SectionBookkeeping Section; 700 startSection(Section, wasm::WASM_SEC_TYPE); 701 702 encodeULEB128(Signatures.size(), W.OS); 703 704 for (const WasmSignature &Sig : Signatures) { 705 W.OS << char(wasm::WASM_TYPE_FUNC); 706 encodeULEB128(Sig.Params.size(), W.OS); 707 for (wasm::ValType Ty : Sig.Params) 708 writeValueType(Ty); 709 encodeULEB128(Sig.Returns.size(), W.OS); 710 for (wasm::ValType Ty : Sig.Returns) 711 writeValueType(Ty); 712 } 713 714 endSection(Section); 715 } 716 717 void WasmObjectWriter::writeImportSection(ArrayRef<wasm::WasmImport> Imports, 718 uint32_t DataSize, 719 uint32_t NumElements) { 720 if (Imports.empty()) 721 return; 722 723 uint32_t NumPages = (DataSize + wasm::WasmPageSize - 1) / wasm::WasmPageSize; 724 725 SectionBookkeeping Section; 726 startSection(Section, wasm::WASM_SEC_IMPORT); 727 728 encodeULEB128(Imports.size(), W.OS); 729 for (const wasm::WasmImport &Import : Imports) { 730 writeString(Import.Module); 731 writeString(Import.Field); 732 W.OS << char(Import.Kind); 733 734 switch (Import.Kind) { 735 case wasm::WASM_EXTERNAL_FUNCTION: 736 encodeULEB128(Import.SigIndex, W.OS); 737 break; 738 case wasm::WASM_EXTERNAL_GLOBAL: 739 W.OS << char(Import.Global.Type); 740 W.OS << char(Import.Global.Mutable ? 1 : 0); 741 break; 742 case wasm::WASM_EXTERNAL_MEMORY: 743 encodeULEB128(0, W.OS); // flags 744 encodeULEB128(NumPages, W.OS); // initial 745 break; 746 case wasm::WASM_EXTERNAL_TABLE: 747 W.OS << char(Import.Table.ElemType); 748 encodeULEB128(0, W.OS); // flags 749 encodeULEB128(NumElements, W.OS); // initial 750 break; 751 case wasm::WASM_EXTERNAL_EVENT: 752 encodeULEB128(Import.Event.Attribute, W.OS); 753 encodeULEB128(Import.Event.SigIndex, W.OS); 754 break; 755 default: 756 llvm_unreachable("unsupported import kind"); 757 } 758 } 759 760 endSection(Section); 761 } 762 763 void WasmObjectWriter::writeFunctionSection(ArrayRef<WasmFunction> Functions) { 764 if (Functions.empty()) 765 return; 766 767 SectionBookkeeping Section; 768 startSection(Section, wasm::WASM_SEC_FUNCTION); 769 770 encodeULEB128(Functions.size(), W.OS); 771 for (const WasmFunction &Func : Functions) 772 encodeULEB128(Func.SigIndex, W.OS); 773 774 endSection(Section); 775 } 776 777 void WasmObjectWriter::writeEventSection(ArrayRef<wasm::WasmEventType> Events) { 778 if (Events.empty()) 779 return; 780 781 SectionBookkeeping Section; 782 startSection(Section, wasm::WASM_SEC_EVENT); 783 784 encodeULEB128(Events.size(), W.OS); 785 for (const wasm::WasmEventType &Event : Events) { 786 encodeULEB128(Event.Attribute, W.OS); 787 encodeULEB128(Event.SigIndex, W.OS); 788 } 789 790 endSection(Section); 791 } 792 793 void WasmObjectWriter::writeGlobalSection(ArrayRef<wasm::WasmGlobal> Globals) { 794 if (Globals.empty()) 795 return; 796 797 SectionBookkeeping Section; 798 startSection(Section, wasm::WASM_SEC_GLOBAL); 799 800 encodeULEB128(Globals.size(), W.OS); 801 for (const wasm::WasmGlobal &Global : Globals) { 802 encodeULEB128(Global.Type.Type, W.OS); 803 W.OS << char(Global.Type.Mutable); 804 W.OS << char(Global.InitExpr.Opcode); 805 switch (Global.Type.Type) { 806 case wasm::WASM_TYPE_I32: 807 encodeSLEB128(0, W.OS); 808 break; 809 case wasm::WASM_TYPE_I64: 810 encodeSLEB128(0, W.OS); 811 break; 812 case wasm::WASM_TYPE_F32: 813 writeI32(0); 814 break; 815 case wasm::WASM_TYPE_F64: 816 writeI64(0); 817 break; 818 default: 819 llvm_unreachable("unexpected type"); 820 } 821 W.OS << char(wasm::WASM_OPCODE_END); 822 } 823 824 endSection(Section); 825 } 826 827 void WasmObjectWriter::writeExportSection(ArrayRef<wasm::WasmExport> Exports) { 828 if (Exports.empty()) 829 return; 830 831 SectionBookkeeping Section; 832 startSection(Section, wasm::WASM_SEC_EXPORT); 833 834 encodeULEB128(Exports.size(), W.OS); 835 for (const wasm::WasmExport &Export : Exports) { 836 writeString(Export.Name); 837 W.OS << char(Export.Kind); 838 encodeULEB128(Export.Index, W.OS); 839 } 840 841 endSection(Section); 842 } 843 844 void WasmObjectWriter::writeElemSection(ArrayRef<uint32_t> TableElems) { 845 if (TableElems.empty()) 846 return; 847 848 SectionBookkeeping Section; 849 startSection(Section, wasm::WASM_SEC_ELEM); 850 851 encodeULEB128(1, W.OS); // number of "segments" 852 encodeULEB128(0, W.OS); // the table index 853 854 // init expr for starting offset 855 W.OS << char(wasm::WASM_OPCODE_I32_CONST); 856 encodeSLEB128(InitialTableOffset, W.OS); 857 W.OS << char(wasm::WASM_OPCODE_END); 858 859 encodeULEB128(TableElems.size(), W.OS); 860 for (uint32_t Elem : TableElems) 861 encodeULEB128(Elem, W.OS); 862 863 endSection(Section); 864 } 865 866 void WasmObjectWriter::writeDataCountSection() { 867 if (DataSegments.empty()) 868 return; 869 870 SectionBookkeeping Section; 871 startSection(Section, wasm::WASM_SEC_DATACOUNT); 872 encodeULEB128(DataSegments.size(), W.OS); 873 endSection(Section); 874 } 875 876 void WasmObjectWriter::writeCodeSection(const MCAssembler &Asm, 877 const MCAsmLayout &Layout, 878 ArrayRef<WasmFunction> Functions) { 879 if (Functions.empty()) 880 return; 881 882 SectionBookkeeping Section; 883 startSection(Section, wasm::WASM_SEC_CODE); 884 CodeSectionIndex = Section.Index; 885 886 encodeULEB128(Functions.size(), W.OS); 887 888 for (const WasmFunction &Func : Functions) { 889 auto &FuncSection = static_cast<MCSectionWasm &>(Func.Sym->getSection()); 890 891 int64_t Size = 0; 892 if (!Func.Sym->getSize()->evaluateAsAbsolute(Size, Layout)) 893 report_fatal_error(".size expression must be evaluatable"); 894 895 encodeULEB128(Size, W.OS); 896 FuncSection.setSectionOffset(W.OS.tell() - Section.ContentsOffset); 897 Asm.writeSectionData(W.OS, &FuncSection, Layout); 898 } 899 900 // Apply fixups. 901 applyRelocations(CodeRelocations, Section.ContentsOffset); 902 903 endSection(Section); 904 } 905 906 void WasmObjectWriter::writeDataSection() { 907 if (DataSegments.empty()) 908 return; 909 910 SectionBookkeeping Section; 911 startSection(Section, wasm::WASM_SEC_DATA); 912 DataSectionIndex = Section.Index; 913 914 encodeULEB128(DataSegments.size(), W.OS); // count 915 916 for (const WasmDataSegment &Segment : DataSegments) { 917 encodeULEB128(Segment.InitFlags, W.OS); // flags 918 if (Segment.InitFlags & wasm::WASM_SEGMENT_HAS_MEMINDEX) 919 encodeULEB128(0, W.OS); // memory index 920 if ((Segment.InitFlags & wasm::WASM_SEGMENT_IS_PASSIVE) == 0) { 921 W.OS << char(wasm::WASM_OPCODE_I32_CONST); 922 encodeSLEB128(Segment.Offset, W.OS); // offset 923 W.OS << char(wasm::WASM_OPCODE_END); 924 } 925 encodeULEB128(Segment.Data.size(), W.OS); // size 926 Segment.Section->setSectionOffset(W.OS.tell() - Section.ContentsOffset); 927 W.OS << Segment.Data; // data 928 } 929 930 // Apply fixups. 931 applyRelocations(DataRelocations, Section.ContentsOffset); 932 933 endSection(Section); 934 } 935 936 void WasmObjectWriter::writeRelocSection( 937 uint32_t SectionIndex, StringRef Name, 938 std::vector<WasmRelocationEntry> &Relocs) { 939 // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md 940 // for descriptions of the reloc sections. 941 942 if (Relocs.empty()) 943 return; 944 945 // First, ensure the relocations are sorted in offset order. In general they 946 // should already be sorted since `recordRelocation` is called in offset 947 // order, but for the code section we combine many MC sections into single 948 // wasm section, and this order is determined by the order of Asm.Symbols() 949 // not the sections order. 950 llvm::stable_sort( 951 Relocs, [](const WasmRelocationEntry &A, const WasmRelocationEntry &B) { 952 return (A.Offset + A.FixupSection->getSectionOffset()) < 953 (B.Offset + B.FixupSection->getSectionOffset()); 954 }); 955 956 SectionBookkeeping Section; 957 startCustomSection(Section, std::string("reloc.") + Name.str()); 958 959 encodeULEB128(SectionIndex, W.OS); 960 encodeULEB128(Relocs.size(), W.OS); 961 for (const WasmRelocationEntry &RelEntry : Relocs) { 962 uint64_t Offset = 963 RelEntry.Offset + RelEntry.FixupSection->getSectionOffset(); 964 uint32_t Index = getRelocationIndexValue(RelEntry); 965 966 W.OS << char(RelEntry.Type); 967 encodeULEB128(Offset, W.OS); 968 encodeULEB128(Index, W.OS); 969 if (RelEntry.hasAddend()) 970 encodeSLEB128(RelEntry.Addend, W.OS); 971 } 972 973 endSection(Section); 974 } 975 976 void WasmObjectWriter::writeCustomRelocSections() { 977 for (const auto &Sec : CustomSections) { 978 auto &Relocations = CustomSectionsRelocations[Sec.Section]; 979 writeRelocSection(Sec.OutputIndex, Sec.Name, Relocations); 980 } 981 } 982 983 void WasmObjectWriter::writeLinkingMetaDataSection( 984 ArrayRef<wasm::WasmSymbolInfo> SymbolInfos, 985 ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs, 986 const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats) { 987 SectionBookkeeping Section; 988 startCustomSection(Section, "linking"); 989 encodeULEB128(wasm::WasmMetadataVersion, W.OS); 990 991 SectionBookkeeping SubSection; 992 if (SymbolInfos.size() != 0) { 993 startSection(SubSection, wasm::WASM_SYMBOL_TABLE); 994 encodeULEB128(SymbolInfos.size(), W.OS); 995 for (const wasm::WasmSymbolInfo &Sym : SymbolInfos) { 996 encodeULEB128(Sym.Kind, W.OS); 997 encodeULEB128(Sym.Flags, W.OS); 998 switch (Sym.Kind) { 999 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 1000 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 1001 case wasm::WASM_SYMBOL_TYPE_EVENT: 1002 encodeULEB128(Sym.ElementIndex, W.OS); 1003 if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0 || 1004 (Sym.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) 1005 writeString(Sym.Name); 1006 break; 1007 case wasm::WASM_SYMBOL_TYPE_DATA: 1008 writeString(Sym.Name); 1009 if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) { 1010 encodeULEB128(Sym.DataRef.Segment, W.OS); 1011 encodeULEB128(Sym.DataRef.Offset, W.OS); 1012 encodeULEB128(Sym.DataRef.Size, W.OS); 1013 } 1014 break; 1015 case wasm::WASM_SYMBOL_TYPE_SECTION: { 1016 const uint32_t SectionIndex = 1017 CustomSections[Sym.ElementIndex].OutputIndex; 1018 encodeULEB128(SectionIndex, W.OS); 1019 break; 1020 } 1021 default: 1022 llvm_unreachable("unexpected kind"); 1023 } 1024 } 1025 endSection(SubSection); 1026 } 1027 1028 if (DataSegments.size()) { 1029 startSection(SubSection, wasm::WASM_SEGMENT_INFO); 1030 encodeULEB128(DataSegments.size(), W.OS); 1031 for (const WasmDataSegment &Segment : DataSegments) { 1032 writeString(Segment.Name); 1033 encodeULEB128(Segment.Alignment, W.OS); 1034 encodeULEB128(Segment.LinkerFlags, W.OS); 1035 } 1036 endSection(SubSection); 1037 } 1038 1039 if (!InitFuncs.empty()) { 1040 startSection(SubSection, wasm::WASM_INIT_FUNCS); 1041 encodeULEB128(InitFuncs.size(), W.OS); 1042 for (auto &StartFunc : InitFuncs) { 1043 encodeULEB128(StartFunc.first, W.OS); // priority 1044 encodeULEB128(StartFunc.second, W.OS); // function index 1045 } 1046 endSection(SubSection); 1047 } 1048 1049 if (Comdats.size()) { 1050 startSection(SubSection, wasm::WASM_COMDAT_INFO); 1051 encodeULEB128(Comdats.size(), W.OS); 1052 for (const auto &C : Comdats) { 1053 writeString(C.first); 1054 encodeULEB128(0, W.OS); // flags for future use 1055 encodeULEB128(C.second.size(), W.OS); 1056 for (const WasmComdatEntry &Entry : C.second) { 1057 encodeULEB128(Entry.Kind, W.OS); 1058 encodeULEB128(Entry.Index, W.OS); 1059 } 1060 } 1061 endSection(SubSection); 1062 } 1063 1064 endSection(Section); 1065 } 1066 1067 void WasmObjectWriter::writeCustomSection(WasmCustomSection &CustomSection, 1068 const MCAssembler &Asm, 1069 const MCAsmLayout &Layout) { 1070 SectionBookkeeping Section; 1071 auto *Sec = CustomSection.Section; 1072 startCustomSection(Section, CustomSection.Name); 1073 1074 Sec->setSectionOffset(W.OS.tell() - Section.ContentsOffset); 1075 Asm.writeSectionData(W.OS, Sec, Layout); 1076 1077 CustomSection.OutputContentsOffset = Section.ContentsOffset; 1078 CustomSection.OutputIndex = Section.Index; 1079 1080 endSection(Section); 1081 1082 // Apply fixups. 1083 auto &Relocations = CustomSectionsRelocations[CustomSection.Section]; 1084 applyRelocations(Relocations, CustomSection.OutputContentsOffset); 1085 } 1086 1087 uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm &Symbol) { 1088 assert(Symbol.isFunction()); 1089 assert(TypeIndices.count(&Symbol)); 1090 return TypeIndices[&Symbol]; 1091 } 1092 1093 uint32_t WasmObjectWriter::getEventType(const MCSymbolWasm &Symbol) { 1094 assert(Symbol.isEvent()); 1095 assert(TypeIndices.count(&Symbol)); 1096 return TypeIndices[&Symbol]; 1097 } 1098 1099 void WasmObjectWriter::registerFunctionType(const MCSymbolWasm &Symbol) { 1100 assert(Symbol.isFunction()); 1101 1102 WasmSignature S; 1103 const MCSymbolWasm *ResolvedSym = resolveSymbol(Symbol); 1104 if (auto *Sig = ResolvedSym->getSignature()) { 1105 S.Returns = Sig->Returns; 1106 S.Params = Sig->Params; 1107 } 1108 1109 auto Pair = SignatureIndices.insert(std::make_pair(S, Signatures.size())); 1110 if (Pair.second) 1111 Signatures.push_back(S); 1112 TypeIndices[&Symbol] = Pair.first->second; 1113 1114 LLVM_DEBUG(dbgs() << "registerFunctionType: " << Symbol 1115 << " new:" << Pair.second << "\n"); 1116 LLVM_DEBUG(dbgs() << " -> type index: " << Pair.first->second << "\n"); 1117 } 1118 1119 void WasmObjectWriter::registerEventType(const MCSymbolWasm &Symbol) { 1120 assert(Symbol.isEvent()); 1121 1122 // TODO Currently we don't generate imported exceptions, but if we do, we 1123 // should have a way of infering types of imported exceptions. 1124 WasmSignature S; 1125 if (auto *Sig = Symbol.getSignature()) { 1126 S.Returns = Sig->Returns; 1127 S.Params = Sig->Params; 1128 } 1129 1130 auto Pair = SignatureIndices.insert(std::make_pair(S, Signatures.size())); 1131 if (Pair.second) 1132 Signatures.push_back(S); 1133 TypeIndices[&Symbol] = Pair.first->second; 1134 1135 LLVM_DEBUG(dbgs() << "registerEventType: " << Symbol << " new:" << Pair.second 1136 << "\n"); 1137 LLVM_DEBUG(dbgs() << " -> type index: " << Pair.first->second << "\n"); 1138 } 1139 1140 static bool isInSymtab(const MCSymbolWasm &Sym) { 1141 if (Sym.isUsedInReloc() || Sym.isUsedInInitArray()) 1142 return true; 1143 1144 if (Sym.isComdat() && !Sym.isDefined()) 1145 return false; 1146 1147 if (Sym.isTemporary()) 1148 return false; 1149 1150 if (Sym.isSection()) 1151 return false; 1152 1153 return true; 1154 } 1155 1156 uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm, 1157 const MCAsmLayout &Layout) { 1158 uint64_t StartOffset = W.OS.tell(); 1159 1160 LLVM_DEBUG(dbgs() << "WasmObjectWriter::writeObject\n"); 1161 1162 // Collect information from the available symbols. 1163 SmallVector<WasmFunction, 4> Functions; 1164 SmallVector<uint32_t, 4> TableElems; 1165 SmallVector<wasm::WasmImport, 4> Imports; 1166 SmallVector<wasm::WasmExport, 4> Exports; 1167 SmallVector<wasm::WasmEventType, 1> Events; 1168 SmallVector<wasm::WasmGlobal, 1> Globals; 1169 SmallVector<wasm::WasmSymbolInfo, 4> SymbolInfos; 1170 SmallVector<std::pair<uint16_t, uint32_t>, 2> InitFuncs; 1171 std::map<StringRef, std::vector<WasmComdatEntry>> Comdats; 1172 uint32_t DataSize = 0; 1173 1174 // For now, always emit the memory import, since loads and stores are not 1175 // valid without it. In the future, we could perhaps be more clever and omit 1176 // it if there are no loads or stores. 1177 wasm::WasmImport MemImport; 1178 MemImport.Module = "env"; 1179 MemImport.Field = "__linear_memory"; 1180 MemImport.Kind = wasm::WASM_EXTERNAL_MEMORY; 1181 Imports.push_back(MemImport); 1182 1183 // For now, always emit the table section, since indirect calls are not 1184 // valid without it. In the future, we could perhaps be more clever and omit 1185 // it if there are no indirect calls. 1186 wasm::WasmImport TableImport; 1187 TableImport.Module = "env"; 1188 TableImport.Field = "__indirect_function_table"; 1189 TableImport.Kind = wasm::WASM_EXTERNAL_TABLE; 1190 TableImport.Table.ElemType = wasm::WASM_TYPE_FUNCREF; 1191 Imports.push_back(TableImport); 1192 1193 // Populate SignatureIndices, and Imports and WasmIndices for undefined 1194 // symbols. This must be done before populating WasmIndices for defined 1195 // symbols. 1196 for (const MCSymbol &S : Asm.symbols()) { 1197 const auto &WS = static_cast<const MCSymbolWasm &>(S); 1198 1199 // Register types for all functions, including those with private linkage 1200 // (because wasm always needs a type signature). 1201 if (WS.isFunction()) 1202 registerFunctionType(WS); 1203 1204 if (WS.isEvent()) 1205 registerEventType(WS); 1206 1207 if (WS.isTemporary()) 1208 continue; 1209 1210 // If the symbol is not defined in this translation unit, import it. 1211 if (!WS.isDefined() && !WS.isComdat()) { 1212 if (WS.isFunction()) { 1213 wasm::WasmImport Import; 1214 Import.Module = WS.getImportModule(); 1215 Import.Field = WS.getImportName(); 1216 Import.Kind = wasm::WASM_EXTERNAL_FUNCTION; 1217 Import.SigIndex = getFunctionType(WS); 1218 Imports.push_back(Import); 1219 assert(WasmIndices.count(&WS) == 0); 1220 WasmIndices[&WS] = NumFunctionImports++; 1221 } else if (WS.isGlobal()) { 1222 if (WS.isWeak()) 1223 report_fatal_error("undefined global symbol cannot be weak"); 1224 1225 wasm::WasmImport Import; 1226 Import.Field = WS.getImportName(); 1227 Import.Kind = wasm::WASM_EXTERNAL_GLOBAL; 1228 Import.Module = WS.getImportModule(); 1229 Import.Global = WS.getGlobalType(); 1230 Imports.push_back(Import); 1231 assert(WasmIndices.count(&WS) == 0); 1232 WasmIndices[&WS] = NumGlobalImports++; 1233 } else if (WS.isEvent()) { 1234 if (WS.isWeak()) 1235 report_fatal_error("undefined event symbol cannot be weak"); 1236 1237 wasm::WasmImport Import; 1238 Import.Module = WS.getImportModule(); 1239 Import.Field = WS.getImportName(); 1240 Import.Kind = wasm::WASM_EXTERNAL_EVENT; 1241 Import.Event.Attribute = wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION; 1242 Import.Event.SigIndex = getEventType(WS); 1243 Imports.push_back(Import); 1244 assert(WasmIndices.count(&WS) == 0); 1245 WasmIndices[&WS] = NumEventImports++; 1246 } 1247 } 1248 } 1249 1250 // Add imports for GOT globals 1251 for (const MCSymbol &S : Asm.symbols()) { 1252 const auto &WS = static_cast<const MCSymbolWasm &>(S); 1253 if (WS.isUsedInGOT()) { 1254 wasm::WasmImport Import; 1255 if (WS.isFunction()) 1256 Import.Module = "GOT.func"; 1257 else 1258 Import.Module = "GOT.mem"; 1259 Import.Field = WS.getName(); 1260 Import.Kind = wasm::WASM_EXTERNAL_GLOBAL; 1261 Import.Global = {wasm::WASM_TYPE_I32, true}; 1262 Imports.push_back(Import); 1263 assert(GOTIndices.count(&WS) == 0); 1264 GOTIndices[&WS] = NumGlobalImports++; 1265 } 1266 } 1267 1268 // Populate DataSegments and CustomSections, which must be done before 1269 // populating DataLocations. 1270 for (MCSection &Sec : Asm) { 1271 auto &Section = static_cast<MCSectionWasm &>(Sec); 1272 StringRef SectionName = Section.getName(); 1273 1274 // .init_array sections are handled specially elsewhere. 1275 if (SectionName.startswith(".init_array")) 1276 continue; 1277 1278 // Code is handled separately 1279 if (Section.getKind().isText()) 1280 continue; 1281 1282 if (Section.isWasmData()) { 1283 uint32_t SegmentIndex = DataSegments.size(); 1284 DataSize = alignTo(DataSize, Section.getAlignment()); 1285 DataSegments.emplace_back(); 1286 WasmDataSegment &Segment = DataSegments.back(); 1287 Segment.Name = SectionName; 1288 Segment.InitFlags = 1289 Section.getPassive() ? (uint32_t)wasm::WASM_SEGMENT_IS_PASSIVE : 0; 1290 Segment.Offset = DataSize; 1291 Segment.Section = &Section; 1292 addData(Segment.Data, Section); 1293 Segment.Alignment = Log2_32(Section.getAlignment()); 1294 Segment.LinkerFlags = 0; 1295 DataSize += Segment.Data.size(); 1296 Section.setSegmentIndex(SegmentIndex); 1297 1298 if (const MCSymbolWasm *C = Section.getGroup()) { 1299 Comdats[C->getName()].emplace_back( 1300 WasmComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex}); 1301 } 1302 } else { 1303 // Create custom sections 1304 assert(Sec.getKind().isMetadata()); 1305 1306 StringRef Name = SectionName; 1307 1308 // For user-defined custom sections, strip the prefix 1309 if (Name.startswith(".custom_section.")) 1310 Name = Name.substr(strlen(".custom_section.")); 1311 1312 MCSymbol *Begin = Sec.getBeginSymbol(); 1313 if (Begin) { 1314 WasmIndices[cast<MCSymbolWasm>(Begin)] = CustomSections.size(); 1315 if (SectionName != Begin->getName()) 1316 report_fatal_error("section name and begin symbol should match: " + 1317 Twine(SectionName)); 1318 } 1319 1320 // Separate out the producers and target features sections 1321 if (Name == "producers") { 1322 ProducersSection = std::make_unique<WasmCustomSection>(Name, &Section); 1323 continue; 1324 } 1325 if (Name == "target_features") { 1326 TargetFeaturesSection = 1327 std::make_unique<WasmCustomSection>(Name, &Section); 1328 continue; 1329 } 1330 1331 CustomSections.emplace_back(Name, &Section); 1332 } 1333 } 1334 1335 // Populate WasmIndices and DataLocations for defined symbols. 1336 for (const MCSymbol &S : Asm.symbols()) { 1337 // Ignore unnamed temporary symbols, which aren't ever exported, imported, 1338 // or used in relocations. 1339 if (S.isTemporary() && S.getName().empty()) 1340 continue; 1341 1342 const auto &WS = static_cast<const MCSymbolWasm &>(S); 1343 LLVM_DEBUG( 1344 dbgs() << "MCSymbol: " << toString(WS.getType()) << " '" << S << "'" 1345 << " isDefined=" << S.isDefined() << " isExternal=" 1346 << S.isExternal() << " isTemporary=" << S.isTemporary() 1347 << " isWeak=" << WS.isWeak() << " isHidden=" << WS.isHidden() 1348 << " isVariable=" << WS.isVariable() << "\n"); 1349 1350 if (WS.isVariable()) 1351 continue; 1352 if (WS.isComdat() && !WS.isDefined()) 1353 continue; 1354 1355 if (WS.isFunction()) { 1356 unsigned Index; 1357 if (WS.isDefined()) { 1358 if (WS.getOffset() != 0) 1359 report_fatal_error( 1360 "function sections must contain one function each"); 1361 1362 if (WS.getSize() == nullptr) 1363 report_fatal_error( 1364 "function symbols must have a size set with .size"); 1365 1366 // A definition. Write out the function body. 1367 Index = NumFunctionImports + Functions.size(); 1368 WasmFunction Func; 1369 Func.SigIndex = getFunctionType(WS); 1370 Func.Sym = &WS; 1371 WasmIndices[&WS] = Index; 1372 Functions.push_back(Func); 1373 1374 auto &Section = static_cast<MCSectionWasm &>(WS.getSection()); 1375 if (const MCSymbolWasm *C = Section.getGroup()) { 1376 Comdats[C->getName()].emplace_back( 1377 WasmComdatEntry{wasm::WASM_COMDAT_FUNCTION, Index}); 1378 } 1379 1380 if (WS.hasExportName()) { 1381 wasm::WasmExport Export; 1382 Export.Name = WS.getExportName(); 1383 Export.Kind = wasm::WASM_EXTERNAL_FUNCTION; 1384 Export.Index = Index; 1385 Exports.push_back(Export); 1386 } 1387 } else { 1388 // An import; the index was assigned above. 1389 Index = WasmIndices.find(&WS)->second; 1390 } 1391 1392 LLVM_DEBUG(dbgs() << " -> function index: " << Index << "\n"); 1393 1394 } else if (WS.isData()) { 1395 if (!isInSymtab(WS)) 1396 continue; 1397 1398 if (!WS.isDefined()) { 1399 LLVM_DEBUG(dbgs() << " -> segment index: -1" 1400 << "\n"); 1401 continue; 1402 } 1403 1404 if (!WS.getSize()) 1405 report_fatal_error("data symbols must have a size set with .size: " + 1406 WS.getName()); 1407 1408 int64_t Size = 0; 1409 if (!WS.getSize()->evaluateAsAbsolute(Size, Layout)) 1410 report_fatal_error(".size expression must be evaluatable"); 1411 1412 auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection()); 1413 if (!DataSection.isWasmData()) 1414 report_fatal_error("data symbols must live in a data section: " + 1415 WS.getName()); 1416 1417 // For each data symbol, export it in the symtab as a reference to the 1418 // corresponding Wasm data segment. 1419 wasm::WasmDataReference Ref = wasm::WasmDataReference{ 1420 DataSection.getSegmentIndex(), 1421 static_cast<uint32_t>(Layout.getSymbolOffset(WS)), 1422 static_cast<uint32_t>(Size)}; 1423 DataLocations[&WS] = Ref; 1424 LLVM_DEBUG(dbgs() << " -> segment index: " << Ref.Segment << "\n"); 1425 1426 } else if (WS.isGlobal()) { 1427 // A "true" Wasm global (currently just __stack_pointer) 1428 if (WS.isDefined()) { 1429 assert(WasmIndices.count(&WS) == 0); 1430 wasm::WasmGlobal Global; 1431 Global.Type = WS.getGlobalType(); 1432 Global.Index = NumGlobalImports + Globals.size(); 1433 switch (Global.Type.Type) { 1434 case wasm::WASM_TYPE_I32: 1435 Global.InitExpr.Opcode = wasm::WASM_OPCODE_I32_CONST; 1436 break; 1437 case wasm::WASM_TYPE_I64: 1438 Global.InitExpr.Opcode = wasm::WASM_OPCODE_I64_CONST; 1439 break; 1440 case wasm::WASM_TYPE_F32: 1441 Global.InitExpr.Opcode = wasm::WASM_OPCODE_F32_CONST; 1442 break; 1443 case wasm::WASM_TYPE_F64: 1444 Global.InitExpr.Opcode = wasm::WASM_OPCODE_F64_CONST; 1445 break; 1446 default: 1447 llvm_unreachable("unexpected type"); 1448 } 1449 WasmIndices[&WS] = Global.Index; 1450 Globals.push_back(Global); 1451 } else { 1452 // An import; the index was assigned above 1453 LLVM_DEBUG(dbgs() << " -> global index: " 1454 << WasmIndices.find(&WS)->second << "\n"); 1455 } 1456 } else if (WS.isEvent()) { 1457 // C++ exception symbol (__cpp_exception) 1458 unsigned Index; 1459 if (WS.isDefined()) { 1460 assert(WasmIndices.count(&WS) == 0); 1461 Index = NumEventImports + Events.size(); 1462 wasm::WasmEventType Event; 1463 Event.SigIndex = getEventType(WS); 1464 Event.Attribute = wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION; 1465 WasmIndices[&WS] = Index; 1466 Events.push_back(Event); 1467 } else { 1468 // An import; the index was assigned above. 1469 assert(WasmIndices.count(&WS) > 0); 1470 } 1471 LLVM_DEBUG(dbgs() << " -> event index: " << WasmIndices.find(&WS)->second 1472 << "\n"); 1473 1474 } else { 1475 assert(WS.isSection()); 1476 } 1477 } 1478 1479 // Populate WasmIndices and DataLocations for aliased symbols. We need to 1480 // process these in a separate pass because we need to have processed the 1481 // target of the alias before the alias itself and the symbols are not 1482 // necessarily ordered in this way. 1483 for (const MCSymbol &S : Asm.symbols()) { 1484 if (!S.isVariable()) 1485 continue; 1486 1487 assert(S.isDefined()); 1488 1489 // Find the target symbol of this weak alias and export that index 1490 const auto &WS = static_cast<const MCSymbolWasm &>(S); 1491 const MCSymbolWasm *ResolvedSym = resolveSymbol(WS); 1492 LLVM_DEBUG(dbgs() << WS.getName() << ": weak alias of '" << *ResolvedSym 1493 << "'\n"); 1494 1495 if (ResolvedSym->isFunction()) { 1496 assert(WasmIndices.count(ResolvedSym) > 0); 1497 uint32_t WasmIndex = WasmIndices.find(ResolvedSym)->second; 1498 assert(WasmIndices.count(&WS) == 0); 1499 WasmIndices[&WS] = WasmIndex; 1500 LLVM_DEBUG(dbgs() << " -> index:" << WasmIndex << "\n"); 1501 } else if (ResolvedSym->isData()) { 1502 assert(DataLocations.count(ResolvedSym) > 0); 1503 const wasm::WasmDataReference &Ref = 1504 DataLocations.find(ResolvedSym)->second; 1505 DataLocations[&WS] = Ref; 1506 LLVM_DEBUG(dbgs() << " -> index:" << Ref.Segment << "\n"); 1507 } else { 1508 report_fatal_error("don't yet support global/event aliases"); 1509 } 1510 } 1511 1512 // Finally, populate the symbol table itself, in its "natural" order. 1513 for (const MCSymbol &S : Asm.symbols()) { 1514 const auto &WS = static_cast<const MCSymbolWasm &>(S); 1515 if (!isInSymtab(WS)) { 1516 WS.setIndex(InvalidIndex); 1517 continue; 1518 } 1519 LLVM_DEBUG(dbgs() << "adding to symtab: " << WS << "\n"); 1520 1521 uint32_t Flags = 0; 1522 if (WS.isWeak()) 1523 Flags |= wasm::WASM_SYMBOL_BINDING_WEAK; 1524 if (WS.isHidden()) 1525 Flags |= wasm::WASM_SYMBOL_VISIBILITY_HIDDEN; 1526 if (!WS.isExternal() && WS.isDefined()) 1527 Flags |= wasm::WASM_SYMBOL_BINDING_LOCAL; 1528 if (WS.isUndefined()) 1529 Flags |= wasm::WASM_SYMBOL_UNDEFINED; 1530 if (WS.isNoStrip()) { 1531 Flags |= wasm::WASM_SYMBOL_NO_STRIP; 1532 if (isEmscripten()) { 1533 Flags |= wasm::WASM_SYMBOL_EXPORTED; 1534 } 1535 } 1536 if (WS.hasImportName()) 1537 Flags |= wasm::WASM_SYMBOL_EXPLICIT_NAME; 1538 if (WS.hasExportName()) 1539 Flags |= wasm::WASM_SYMBOL_EXPORTED; 1540 1541 wasm::WasmSymbolInfo Info; 1542 Info.Name = WS.getName(); 1543 Info.Kind = WS.getType(); 1544 Info.Flags = Flags; 1545 if (!WS.isData()) { 1546 assert(WasmIndices.count(&WS) > 0); 1547 Info.ElementIndex = WasmIndices.find(&WS)->second; 1548 } else if (WS.isDefined()) { 1549 assert(DataLocations.count(&WS) > 0); 1550 Info.DataRef = DataLocations.find(&WS)->second; 1551 } 1552 WS.setIndex(SymbolInfos.size()); 1553 SymbolInfos.emplace_back(Info); 1554 } 1555 1556 { 1557 auto HandleReloc = [&](const WasmRelocationEntry &Rel) { 1558 // Functions referenced by a relocation need to put in the table. This is 1559 // purely to make the object file's provisional values readable, and is 1560 // ignored by the linker, which re-calculates the relocations itself. 1561 if (Rel.Type != wasm::R_WASM_TABLE_INDEX_I32 && 1562 Rel.Type != wasm::R_WASM_TABLE_INDEX_SLEB) 1563 return; 1564 assert(Rel.Symbol->isFunction()); 1565 const MCSymbolWasm &WS = *resolveSymbol(*Rel.Symbol); 1566 uint32_t FunctionIndex = WasmIndices.find(&WS)->second; 1567 uint32_t TableIndex = TableElems.size() + InitialTableOffset; 1568 if (TableIndices.try_emplace(&WS, TableIndex).second) { 1569 LLVM_DEBUG(dbgs() << " -> adding " << WS.getName() 1570 << " to table: " << TableIndex << "\n"); 1571 TableElems.push_back(FunctionIndex); 1572 registerFunctionType(WS); 1573 } 1574 }; 1575 1576 for (const WasmRelocationEntry &RelEntry : CodeRelocations) 1577 HandleReloc(RelEntry); 1578 for (const WasmRelocationEntry &RelEntry : DataRelocations) 1579 HandleReloc(RelEntry); 1580 } 1581 1582 // Translate .init_array section contents into start functions. 1583 for (const MCSection &S : Asm) { 1584 const auto &WS = static_cast<const MCSectionWasm &>(S); 1585 if (WS.getName().startswith(".fini_array")) 1586 report_fatal_error(".fini_array sections are unsupported"); 1587 if (!WS.getName().startswith(".init_array")) 1588 continue; 1589 if (WS.getFragmentList().empty()) 1590 continue; 1591 1592 // init_array is expected to contain a single non-empty data fragment 1593 if (WS.getFragmentList().size() != 3) 1594 report_fatal_error("only one .init_array section fragment supported"); 1595 1596 auto IT = WS.begin(); 1597 const MCFragment &EmptyFrag = *IT; 1598 if (EmptyFrag.getKind() != MCFragment::FT_Data) 1599 report_fatal_error(".init_array section should be aligned"); 1600 1601 IT = std::next(IT); 1602 const MCFragment &AlignFrag = *IT; 1603 if (AlignFrag.getKind() != MCFragment::FT_Align) 1604 report_fatal_error(".init_array section should be aligned"); 1605 if (cast<MCAlignFragment>(AlignFrag).getAlignment() != (is64Bit() ? 8 : 4)) 1606 report_fatal_error(".init_array section should be aligned for pointers"); 1607 1608 const MCFragment &Frag = *std::next(IT); 1609 if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data) 1610 report_fatal_error("only data supported in .init_array section"); 1611 1612 uint16_t Priority = UINT16_MAX; 1613 unsigned PrefixLength = strlen(".init_array"); 1614 if (WS.getName().size() > PrefixLength) { 1615 if (WS.getName()[PrefixLength] != '.') 1616 report_fatal_error( 1617 ".init_array section priority should start with '.'"); 1618 if (WS.getName().substr(PrefixLength + 1).getAsInteger(10, Priority)) 1619 report_fatal_error("invalid .init_array section priority"); 1620 } 1621 const auto &DataFrag = cast<MCDataFragment>(Frag); 1622 const SmallVectorImpl<char> &Contents = DataFrag.getContents(); 1623 for (const uint8_t * 1624 P = (const uint8_t *)Contents.data(), 1625 *End = (const uint8_t *)Contents.data() + Contents.size(); 1626 P != End; ++P) { 1627 if (*P != 0) 1628 report_fatal_error("non-symbolic data in .init_array section"); 1629 } 1630 for (const MCFixup &Fixup : DataFrag.getFixups()) { 1631 assert(Fixup.getKind() == 1632 MCFixup::getKindForSize(is64Bit() ? 8 : 4, false)); 1633 const MCExpr *Expr = Fixup.getValue(); 1634 auto *SymRef = dyn_cast<MCSymbolRefExpr>(Expr); 1635 if (!SymRef) 1636 report_fatal_error("fixups in .init_array should be symbol references"); 1637 const auto &TargetSym = cast<const MCSymbolWasm>(SymRef->getSymbol()); 1638 if (TargetSym.getIndex() == InvalidIndex) 1639 report_fatal_error("symbols in .init_array should exist in symtab"); 1640 if (!TargetSym.isFunction()) 1641 report_fatal_error("symbols in .init_array should be for functions"); 1642 InitFuncs.push_back( 1643 std::make_pair(Priority, TargetSym.getIndex())); 1644 } 1645 } 1646 1647 // Write out the Wasm header. 1648 writeHeader(Asm); 1649 1650 writeTypeSection(Signatures); 1651 writeImportSection(Imports, DataSize, TableElems.size()); 1652 writeFunctionSection(Functions); 1653 // Skip the "table" section; we import the table instead. 1654 // Skip the "memory" section; we import the memory instead. 1655 writeEventSection(Events); 1656 writeGlobalSection(Globals); 1657 writeExportSection(Exports); 1658 writeElemSection(TableElems); 1659 writeDataCountSection(); 1660 writeCodeSection(Asm, Layout, Functions); 1661 writeDataSection(); 1662 for (auto &CustomSection : CustomSections) 1663 writeCustomSection(CustomSection, Asm, Layout); 1664 writeLinkingMetaDataSection(SymbolInfos, InitFuncs, Comdats); 1665 writeRelocSection(CodeSectionIndex, "CODE", CodeRelocations); 1666 writeRelocSection(DataSectionIndex, "DATA", DataRelocations); 1667 writeCustomRelocSections(); 1668 if (ProducersSection) 1669 writeCustomSection(*ProducersSection, Asm, Layout); 1670 if (TargetFeaturesSection) 1671 writeCustomSection(*TargetFeaturesSection, Asm, Layout); 1672 1673 // TODO: Translate the .comment section to the output. 1674 return W.OS.tell() - StartOffset; 1675 } 1676 1677 std::unique_ptr<MCObjectWriter> 1678 llvm::createWasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW, 1679 raw_pwrite_stream &OS) { 1680 return std::make_unique<WasmObjectWriter>(std::move(MOTW), OS); 1681 } 1682