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