[WebAssembly] Section symbols must have local binding
[llvm-core.git] / lib / MC / WasmObjectWriter.cpp
blob05fc3362745a2e631823bcde9e5fc95155d9db64
1 //===- lib/MC/WasmObjectWriter.cpp - Wasm File Writer ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements Wasm object file writer information.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/BinaryFormat/Wasm.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/ErrorHandling.h"
31 #include "llvm/Support/LEB128.h"
32 #include "llvm/Support/StringSaver.h"
33 #include <vector>
35 using namespace llvm;
37 #define DEBUG_TYPE "mc"
39 namespace {
41 // Went we ceate the indirect function table we start at 1, so that there is
42 // and emtpy slot at 0 and therefore calling a null function pointer will trap.
43 static const uint32_t kInitialTableOffset = 1;
45 // For patching purposes, we need to remember where each section starts, both
46 // for patching up the section size field, and for patching up references to
47 // locations within the section.
48 struct SectionBookkeeping {
49 // Where the size of the section is written.
50 uint64_t SizeOffset;
51 // Where the section header ends (without custom section name).
52 uint64_t PayloadOffset;
53 // Where the contents of the section starts.
54 uint64_t ContentsOffset;
55 uint32_t Index;
58 // The signature of a wasm function, in a struct capable of being used as a
59 // DenseMap key.
60 struct WasmFunctionType {
61 // Support empty and tombstone instances, needed by DenseMap.
62 enum { Plain, Empty, Tombstone } State;
64 // The return types of the function.
65 SmallVector<wasm::ValType, 1> Returns;
67 // The parameter types of the function.
68 SmallVector<wasm::ValType, 4> Params;
70 WasmFunctionType() : State(Plain) {}
72 bool operator==(const WasmFunctionType &Other) const {
73 return State == Other.State && Returns == Other.Returns &&
74 Params == Other.Params;
78 // Traits for using WasmFunctionType in a DenseMap.
79 struct WasmFunctionTypeDenseMapInfo {
80 static WasmFunctionType getEmptyKey() {
81 WasmFunctionType FuncTy;
82 FuncTy.State = WasmFunctionType::Empty;
83 return FuncTy;
85 static WasmFunctionType getTombstoneKey() {
86 WasmFunctionType FuncTy;
87 FuncTy.State = WasmFunctionType::Tombstone;
88 return FuncTy;
90 static unsigned getHashValue(const WasmFunctionType &FuncTy) {
91 uintptr_t Value = FuncTy.State;
92 for (wasm::ValType Ret : FuncTy.Returns)
93 Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Ret));
94 for (wasm::ValType Param : FuncTy.Params)
95 Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Param));
96 return Value;
98 static bool isEqual(const WasmFunctionType &LHS,
99 const WasmFunctionType &RHS) {
100 return LHS == RHS;
104 // A wasm data segment. A wasm binary contains only a single data section
105 // but that can contain many segments, each with their own virtual location
106 // in memory. Each MCSection data created by llvm is modeled as its own
107 // wasm data segment.
108 struct WasmDataSegment {
109 MCSectionWasm *Section;
110 StringRef Name;
111 uint32_t Offset;
112 uint32_t Alignment;
113 uint32_t Flags;
114 SmallVector<char, 4> Data;
117 // A wasm function to be written into the function section.
118 struct WasmFunction {
119 int32_t Type;
120 const MCSymbolWasm *Sym;
123 // A wasm global to be written into the global section.
124 struct WasmGlobal {
125 wasm::WasmGlobalType Type;
126 uint64_t InitialValue;
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;
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.
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) {}
151 bool hasAddend() const {
152 switch (Type) {
153 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
154 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
155 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
156 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
157 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
158 return true;
159 default:
160 return false;
164 void print(raw_ostream &Out) const {
165 Out << "Off=" << Offset << ", Sym=" << *Symbol << ", Addend=" << Addend
166 << ", Type=" << Type
167 << ", FixupSection=" << FixupSection->getSectionName();
170 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
171 LLVM_DUMP_METHOD void dump() const { print(dbgs()); }
172 #endif
175 struct WasmCustomSection {
176 const uint32_t INVALID_INDEX = -1;
178 StringRef Name;
179 MCSectionWasm *Section;
181 uint32_t OutputContentsOffset;
182 uint32_t OutputIndex;
184 WasmCustomSection(StringRef Name, MCSectionWasm *Section)
185 : Name(Name), Section(Section), OutputContentsOffset(0),
186 OutputIndex(INVALID_INDEX) {}
189 #if !defined(NDEBUG)
190 raw_ostream &operator<<(raw_ostream &OS, const WasmRelocationEntry &Rel) {
191 Rel.print(OS);
192 return OS;
194 #endif
196 class WasmObjectWriter : public MCObjectWriter {
197 /// The target specific Wasm writer instance.
198 std::unique_ptr<MCWasmObjectTargetWriter> TargetObjectWriter;
200 // Relocations for fixing up references in the code section.
201 std::vector<WasmRelocationEntry> CodeRelocations;
202 uint32_t CodeSectionIndex;
204 // Relocations for fixing up references in the data section.
205 std::vector<WasmRelocationEntry> DataRelocations;
206 uint32_t DataSectionIndex;
208 // Index values to use for fixing up call_indirect type indices.
209 // Maps function symbols to the index of the type of the function
210 DenseMap<const MCSymbolWasm *, uint32_t> TypeIndices;
211 // Maps function symbols to the table element index space. Used
212 // for TABLE_INDEX relocation types (i.e. address taken functions).
213 DenseMap<const MCSymbolWasm *, uint32_t> TableIndices;
214 // Maps function/global symbols to the (shared) Symbol index space.
215 DenseMap<const MCSymbolWasm *, uint32_t> SymbolIndices;
216 // Maps function/global symbols to the function/global Wasm index space.
217 DenseMap<const MCSymbolWasm *, uint32_t> WasmIndices;
218 // Maps data symbols to the Wasm segment and offset/size with the segment.
219 DenseMap<const MCSymbolWasm *, wasm::WasmDataReference> DataLocations;
220 // Maps section symbols to the section.
221 DenseMap<const MCSymbolWasm *, const MCSectionWasm *> CustomSectionSymbols;
223 // Stores output data (index, relocations, content offset) for custom
224 // section.
225 std::vector<WasmCustomSection> CustomSections;
226 // Relocations for fixing up references in the custom sections.
227 DenseMap<const MCSectionWasm *, std::vector<WasmRelocationEntry>>
228 CustomSectionsRelocations;
230 DenseMap<WasmFunctionType, int32_t, WasmFunctionTypeDenseMapInfo>
231 FunctionTypeIndices;
232 SmallVector<WasmFunctionType, 4> FunctionTypes;
233 SmallVector<WasmGlobal, 4> Globals;
234 SmallVector<WasmDataSegment, 4> DataSegments;
235 unsigned NumFunctionImports = 0;
236 unsigned NumGlobalImports = 0;
237 uint32_t SectionCount = 0;
239 // TargetObjectWriter wrappers.
240 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
241 unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup) const {
242 return TargetObjectWriter->getRelocType(Target, Fixup);
245 void startSection(SectionBookkeeping &Section, unsigned SectionId);
246 void startCustomSection(SectionBookkeeping &Section, StringRef Name);
247 void endSection(SectionBookkeeping &Section);
249 public:
250 WasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
251 raw_pwrite_stream &OS)
252 : MCObjectWriter(OS, /*IsLittleEndian=*/true),
253 TargetObjectWriter(std::move(MOTW)) {}
255 ~WasmObjectWriter() override;
257 private:
258 void reset() override {
259 CodeRelocations.clear();
260 DataRelocations.clear();
261 TypeIndices.clear();
262 SymbolIndices.clear();
263 WasmIndices.clear();
264 TableIndices.clear();
265 DataLocations.clear();
266 CustomSectionsRelocations.clear();
267 FunctionTypeIndices.clear();
268 FunctionTypes.clear();
269 Globals.clear();
270 DataSegments.clear();
271 CustomSectionSymbols.clear();
272 MCObjectWriter::reset();
273 NumFunctionImports = 0;
274 NumGlobalImports = 0;
277 void writeHeader(const MCAssembler &Asm);
279 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
280 const MCFragment *Fragment, const MCFixup &Fixup,
281 MCValue Target, uint64_t &FixedValue) override;
283 void executePostLayoutBinding(MCAssembler &Asm,
284 const MCAsmLayout &Layout) override;
286 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
288 void writeString(const StringRef Str) {
289 encodeULEB128(Str.size(), getStream());
290 writeBytes(Str);
293 void writeValueType(wasm::ValType Ty) {
294 write8(static_cast<uint8_t>(Ty));
297 void writeTypeSection(ArrayRef<WasmFunctionType> FunctionTypes);
298 void writeImportSection(ArrayRef<wasm::WasmImport> Imports, uint32_t DataSize,
299 uint32_t NumElements);
300 void writeFunctionSection(ArrayRef<WasmFunction> Functions);
301 void writeGlobalSection();
302 void writeExportSection(ArrayRef<wasm::WasmExport> Exports);
303 void writeElemSection(ArrayRef<uint32_t> TableElems);
304 void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
305 ArrayRef<WasmFunction> Functions);
306 void writeDataSection();
307 void writeRelocSection(uint32_t SectionIndex, StringRef Name,
308 ArrayRef<WasmRelocationEntry> Relocations);
309 void writeLinkingMetaDataSection(
310 ArrayRef<wasm::WasmSymbolInfo> SymbolInfos,
311 ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
312 const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats);
313 void writeCustomSections(const MCAssembler &Asm, const MCAsmLayout &Layout);
314 void writeCustomRelocSections();
315 void
316 updateCustomSectionRelocations(const SmallVector<WasmFunction, 4> &Functions,
317 const MCAsmLayout &Layout);
319 uint32_t getProvisionalValue(const WasmRelocationEntry &RelEntry);
320 void applyRelocations(ArrayRef<WasmRelocationEntry> Relocations,
321 uint64_t ContentsOffset);
323 uint32_t getRelocationIndexValue(const WasmRelocationEntry &RelEntry);
324 uint32_t getFunctionType(const MCSymbolWasm &Symbol);
325 uint32_t registerFunctionType(const MCSymbolWasm &Symbol);
328 } // end anonymous namespace
330 WasmObjectWriter::~WasmObjectWriter() {}
332 // Write out a section header and a patchable section size field.
333 void WasmObjectWriter::startSection(SectionBookkeeping &Section,
334 unsigned SectionId) {
335 DEBUG(dbgs() << "startSection " << SectionId << "\n");
336 write8(SectionId);
338 Section.SizeOffset = getStream().tell();
340 // The section size. We don't know the size yet, so reserve enough space
341 // for any 32-bit value; we'll patch it later.
342 encodeULEB128(UINT32_MAX, getStream());
344 // The position where the section starts, for measuring its size.
345 Section.ContentsOffset = getStream().tell();
346 Section.PayloadOffset = getStream().tell();
347 Section.Index = SectionCount++;
350 void WasmObjectWriter::startCustomSection(SectionBookkeeping &Section,
351 StringRef Name) {
352 DEBUG(dbgs() << "startCustomSection " << Name << "\n");
353 startSection(Section, wasm::WASM_SEC_CUSTOM);
355 // The position where the section header ends, for measuring its size.
356 Section.PayloadOffset = getStream().tell();
358 // Custom sections in wasm also have a string identifier.
359 writeString(Name);
361 // The position where the custom section starts.
362 Section.ContentsOffset = getStream().tell();
365 // Now that the section is complete and we know how big it is, patch up the
366 // section size field at the start of the section.
367 void WasmObjectWriter::endSection(SectionBookkeeping &Section) {
368 uint64_t Size = getStream().tell() - Section.PayloadOffset;
369 if (uint32_t(Size) != Size)
370 report_fatal_error("section size does not fit in a uint32_t");
372 DEBUG(dbgs() << "endSection size=" << Size << "\n");
374 // Write the final section size to the payload_len field, which follows
375 // the section id byte.
376 uint8_t Buffer[16];
377 unsigned SizeLen = encodeULEB128(Size, Buffer, 5);
378 assert(SizeLen == 5);
379 getStream().pwrite((char *)Buffer, SizeLen, Section.SizeOffset);
382 // Emit the Wasm header.
383 void WasmObjectWriter::writeHeader(const MCAssembler &Asm) {
384 writeBytes(StringRef(wasm::WasmMagic, sizeof(wasm::WasmMagic)));
385 writeLE32(wasm::WasmVersion);
388 void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
389 const MCAsmLayout &Layout) {
392 void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
393 const MCAsmLayout &Layout,
394 const MCFragment *Fragment,
395 const MCFixup &Fixup, MCValue Target,
396 uint64_t &FixedValue) {
397 MCAsmBackend &Backend = Asm.getBackend();
398 bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
399 MCFixupKindInfo::FKF_IsPCRel;
400 const auto &FixupSection = cast<MCSectionWasm>(*Fragment->getParent());
401 uint64_t C = Target.getConstant();
402 uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
403 MCContext &Ctx = Asm.getContext();
405 // The .init_array isn't translated as data, so don't do relocations in it.
406 if (FixupSection.getSectionName().startswith(".init_array"))
407 return;
409 // TODO: Add support for non-debug metadata sections?
410 if (FixupSection.getKind().isMetadata() &&
411 !FixupSection.getSectionName().startswith(".debug_"))
412 return;
414 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
415 assert(RefB->getKind() == MCSymbolRefExpr::VK_None &&
416 "Should not have constructed this");
418 // Let A, B and C being the components of Target and R be the location of
419 // the fixup. If the fixup is not pcrel, we want to compute (A - B + C).
420 // If it is pcrel, we want to compute (A - B + C - R).
422 // In general, Wasm has no relocations for -B. It can only represent (A + C)
423 // or (A + C - R). If B = R + K and the relocation is not pcrel, we can
424 // replace B to implement it: (A - R - K + C)
425 if (IsPCRel) {
426 Ctx.reportError(
427 Fixup.getLoc(),
428 "No relocation available to represent this relative expression");
429 return;
432 const auto &SymB = cast<MCSymbolWasm>(RefB->getSymbol());
434 if (SymB.isUndefined()) {
435 Ctx.reportError(Fixup.getLoc(),
436 Twine("symbol '") + SymB.getName() +
437 "' can not be undefined in a subtraction expression");
438 return;
441 assert(!SymB.isAbsolute() && "Should have been folded");
442 const MCSection &SecB = SymB.getSection();
443 if (&SecB != &FixupSection) {
444 Ctx.reportError(Fixup.getLoc(),
445 "Cannot represent a difference across sections");
446 return;
449 uint64_t SymBOffset = Layout.getSymbolOffset(SymB);
450 uint64_t K = SymBOffset - FixupOffset;
451 IsPCRel = true;
452 C -= K;
455 // We either rejected the fixup or folded B into C at this point.
456 const MCSymbolRefExpr *RefA = Target.getSymA();
457 const auto *SymA = RefA ? cast<MCSymbolWasm>(&RefA->getSymbol()) : nullptr;
459 if (SymA && SymA->isVariable()) {
460 const MCExpr *Expr = SymA->getVariableValue();
461 const auto *Inner = cast<MCSymbolRefExpr>(Expr);
462 if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
463 llvm_unreachable("weakref used in reloc not yet implemented");
466 // Put any constant offset in an addend. Offsets can be negative, and
467 // LLVM expects wrapping, in contrast to wasm's immediates which can't
468 // be negative and don't wrap.
469 FixedValue = 0;
471 if (SymA)
472 SymA->setUsedInReloc();
474 assert(!IsPCRel);
475 assert(SymA);
477 unsigned Type = getRelocType(Target, Fixup);
479 WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection);
480 DEBUG(dbgs() << "WasmReloc: " << Rec << "\n");
482 // Relocation other than R_WEBASSEMBLY_TYPE_INDEX_LEB,
483 // R_WEBASSEMBLY_SECTION_OFFSET_I32 or R_WEBASSEMBLY_FUNCTION_OFFSET_I32
484 // are currently required to be against a named symbol.
485 // TODO(sbc): Add support for relocations against unnamed temporaries such
486 // as those generated by llvm's `blockaddress`.
487 // See: test/MC/WebAssembly/blockaddress.ll
488 if (SymA->getName().empty() &&
489 !(Type == wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB ||
490 Type == wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32 ||
491 Type == wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32))
492 report_fatal_error("relocations against un-named temporaries are not yet "
493 "supported by wasm");
495 if (FixupSection.isWasmData()) {
496 DataRelocations.push_back(Rec);
497 } else if (FixupSection.getKind().isText()) {
498 CodeRelocations.push_back(Rec);
499 } else if (FixupSection.getKind().isMetadata()) {
500 assert(FixupSection.getSectionName().startswith(".debug_"));
501 CustomSectionsRelocations[&FixupSection].push_back(Rec);
502 } else {
503 llvm_unreachable("unexpected section type");
507 // Write X as an (unsigned) LEB value at offset Offset in Stream, padded
508 // to allow patching.
509 static void
510 WritePatchableLEB(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
511 uint8_t Buffer[5];
512 unsigned SizeLen = encodeULEB128(X, Buffer, 5);
513 assert(SizeLen == 5);
514 Stream.pwrite((char *)Buffer, SizeLen, Offset);
517 // Write X as an signed LEB value at offset Offset in Stream, padded
518 // to allow patching.
519 static void
520 WritePatchableSLEB(raw_pwrite_stream &Stream, int32_t X, uint64_t Offset) {
521 uint8_t Buffer[5];
522 unsigned SizeLen = encodeSLEB128(X, Buffer, 5);
523 assert(SizeLen == 5);
524 Stream.pwrite((char *)Buffer, SizeLen, Offset);
527 // Write X as a plain integer value at offset Offset in Stream.
528 static void WriteI32(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
529 uint8_t Buffer[4];
530 support::endian::write32le(Buffer, X);
531 Stream.pwrite((char *)Buffer, sizeof(Buffer), Offset);
534 static const MCSymbolWasm* ResolveSymbol(const MCSymbolWasm& Symbol) {
535 if (Symbol.isVariable()) {
536 const MCExpr *Expr = Symbol.getVariableValue();
537 auto *Inner = cast<MCSymbolRefExpr>(Expr);
538 return cast<MCSymbolWasm>(&Inner->getSymbol());
540 return &Symbol;
543 // Compute a value to write into the code at the location covered
544 // by RelEntry. This value isn't used by the static linker; it just serves
545 // to make the object format more readable and more likely to be directly
546 // useable.
547 uint32_t
548 WasmObjectWriter::getProvisionalValue(const WasmRelocationEntry &RelEntry) {
549 switch (RelEntry.Type) {
550 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
551 case wasm::R_WEBASSEMBLY_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];
557 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
558 // Provisional value is same as the index
559 return getRelocationIndexValue(RelEntry);
560 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
561 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
562 // Provisional value is function/global Wasm index
563 if (!WasmIndices.count(RelEntry.Symbol))
564 report_fatal_error("symbol not found in wasm index space: " +
565 RelEntry.Symbol->getName());
566 return WasmIndices[RelEntry.Symbol];
567 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32: {
568 const auto &Section =
569 static_cast<const MCSectionWasm &>(RelEntry.Symbol->getSection());
570 return Section.getSectionOffset() + RelEntry.Addend;
572 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32: {
573 const auto &Section = *CustomSectionSymbols.find(RelEntry.Symbol)->second;
574 return Section.getSectionOffset() + RelEntry.Addend;
576 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
577 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
578 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: {
579 // Provisional value is address of the global
580 const MCSymbolWasm *Sym = ResolveSymbol(*RelEntry.Symbol);
581 // For undefined symbols, use zero
582 if (!Sym->isDefined())
583 return 0;
584 const wasm::WasmDataReference &Ref = DataLocations[Sym];
585 const WasmDataSegment &Segment = DataSegments[Ref.Segment];
586 // Ignore overflow. LLVM allows address arithmetic to silently wrap.
587 return Segment.Offset + Ref.Offset + RelEntry.Addend;
589 default:
590 llvm_unreachable("invalid relocation type");
594 static void addData(SmallVectorImpl<char> &DataBytes,
595 MCSectionWasm &DataSection) {
596 DEBUG(errs() << "addData: " << DataSection.getSectionName() << "\n");
598 DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment()));
600 for (const MCFragment &Frag : DataSection) {
601 if (Frag.hasInstructions())
602 report_fatal_error("only data supported in data sections");
604 if (auto *Align = dyn_cast<MCAlignFragment>(&Frag)) {
605 if (Align->getValueSize() != 1)
606 report_fatal_error("only byte values supported for alignment");
607 // If nops are requested, use zeros, as this is the data section.
608 uint8_t Value = Align->hasEmitNops() ? 0 : Align->getValue();
609 uint64_t Size = std::min<uint64_t>(alignTo(DataBytes.size(),
610 Align->getAlignment()),
611 DataBytes.size() +
612 Align->getMaxBytesToEmit());
613 DataBytes.resize(Size, Value);
614 } else if (auto *Fill = dyn_cast<MCFillFragment>(&Frag)) {
615 int64_t Size;
616 if (!Fill->getSize().evaluateAsAbsolute(Size))
617 llvm_unreachable("The fill should be an assembler constant");
618 DataBytes.insert(DataBytes.end(), Size, Fill->getValue());
619 } else {
620 const auto &DataFrag = cast<MCDataFragment>(Frag);
621 const SmallVectorImpl<char> &Contents = DataFrag.getContents();
623 DataBytes.insert(DataBytes.end(), Contents.begin(), Contents.end());
627 DEBUG(dbgs() << "addData -> " << DataBytes.size() << "\n");
630 uint32_t
631 WasmObjectWriter::getRelocationIndexValue(const WasmRelocationEntry &RelEntry) {
632 if (RelEntry.Type == wasm::R_WEBASSEMBLY_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];
639 if (!SymbolIndices.count(RelEntry.Symbol))
640 report_fatal_error("symbol not found in symbol index space: " +
641 RelEntry.Symbol->getName());
642 return SymbolIndices[RelEntry.Symbol];
645 // Apply the portions of the relocation records that we can handle ourselves
646 // directly.
647 void WasmObjectWriter::applyRelocations(
648 ArrayRef<WasmRelocationEntry> Relocations, uint64_t ContentsOffset) {
649 raw_pwrite_stream &Stream = getStream();
650 for (const WasmRelocationEntry &RelEntry : Relocations) {
651 uint64_t Offset = ContentsOffset +
652 RelEntry.FixupSection->getSectionOffset() +
653 RelEntry.Offset;
655 DEBUG(dbgs() << "applyRelocation: " << RelEntry << "\n");
656 uint32_t Value = getProvisionalValue(RelEntry);
658 switch (RelEntry.Type) {
659 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
660 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
661 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
662 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
663 WritePatchableLEB(Stream, Value, Offset);
664 break;
665 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
666 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
667 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
668 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
669 WriteI32(Stream, Value, Offset);
670 break;
671 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
672 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
673 WritePatchableSLEB(Stream, Value, Offset);
674 break;
675 default:
676 llvm_unreachable("invalid relocation type");
681 void WasmObjectWriter::writeTypeSection(
682 ArrayRef<WasmFunctionType> FunctionTypes) {
683 if (FunctionTypes.empty())
684 return;
686 SectionBookkeeping Section;
687 startSection(Section, wasm::WASM_SEC_TYPE);
689 encodeULEB128(FunctionTypes.size(), getStream());
691 for (const WasmFunctionType &FuncTy : FunctionTypes) {
692 write8(wasm::WASM_TYPE_FUNC);
693 encodeULEB128(FuncTy.Params.size(), getStream());
694 for (wasm::ValType Ty : FuncTy.Params)
695 writeValueType(Ty);
696 encodeULEB128(FuncTy.Returns.size(), getStream());
697 for (wasm::ValType Ty : FuncTy.Returns)
698 writeValueType(Ty);
701 endSection(Section);
704 void WasmObjectWriter::writeImportSection(ArrayRef<wasm::WasmImport> Imports,
705 uint32_t DataSize,
706 uint32_t NumElements) {
707 if (Imports.empty())
708 return;
710 uint32_t NumPages = (DataSize + wasm::WasmPageSize - 1) / wasm::WasmPageSize;
712 SectionBookkeeping Section;
713 startSection(Section, wasm::WASM_SEC_IMPORT);
715 encodeULEB128(Imports.size(), getStream());
716 for (const wasm::WasmImport &Import : Imports) {
717 writeString(Import.Module);
718 writeString(Import.Field);
719 write8(Import.Kind);
721 switch (Import.Kind) {
722 case wasm::WASM_EXTERNAL_FUNCTION:
723 encodeULEB128(Import.SigIndex, getStream());
724 break;
725 case wasm::WASM_EXTERNAL_GLOBAL:
726 write8(Import.Global.Type);
727 write8(Import.Global.Mutable ? 1 : 0);
728 break;
729 case wasm::WASM_EXTERNAL_MEMORY:
730 encodeULEB128(0, getStream()); // flags
731 encodeULEB128(NumPages, getStream()); // initial
732 break;
733 case wasm::WASM_EXTERNAL_TABLE:
734 write8(Import.Table.ElemType);
735 encodeULEB128(0, getStream()); // flags
736 encodeULEB128(NumElements, getStream()); // initial
737 break;
738 default:
739 llvm_unreachable("unsupported import kind");
743 endSection(Section);
746 void WasmObjectWriter::writeFunctionSection(ArrayRef<WasmFunction> Functions) {
747 if (Functions.empty())
748 return;
750 SectionBookkeeping Section;
751 startSection(Section, wasm::WASM_SEC_FUNCTION);
753 encodeULEB128(Functions.size(), getStream());
754 for (const WasmFunction &Func : Functions)
755 encodeULEB128(Func.Type, getStream());
757 endSection(Section);
760 void WasmObjectWriter::writeGlobalSection() {
761 if (Globals.empty())
762 return;
764 SectionBookkeeping Section;
765 startSection(Section, wasm::WASM_SEC_GLOBAL);
767 encodeULEB128(Globals.size(), getStream());
768 for (const WasmGlobal &Global : Globals) {
769 writeValueType(static_cast<wasm::ValType>(Global.Type.Type));
770 write8(Global.Type.Mutable);
772 write8(wasm::WASM_OPCODE_I32_CONST);
773 encodeSLEB128(Global.InitialValue, getStream());
774 write8(wasm::WASM_OPCODE_END);
777 endSection(Section);
780 void WasmObjectWriter::writeExportSection(ArrayRef<wasm::WasmExport> Exports) {
781 if (Exports.empty())
782 return;
784 SectionBookkeeping Section;
785 startSection(Section, wasm::WASM_SEC_EXPORT);
787 encodeULEB128(Exports.size(), getStream());
788 for (const wasm::WasmExport &Export : Exports) {
789 writeString(Export.Name);
790 write8(Export.Kind);
791 encodeULEB128(Export.Index, getStream());
794 endSection(Section);
797 void WasmObjectWriter::writeElemSection(ArrayRef<uint32_t> TableElems) {
798 if (TableElems.empty())
799 return;
801 SectionBookkeeping Section;
802 startSection(Section, wasm::WASM_SEC_ELEM);
804 encodeULEB128(1, getStream()); // number of "segments"
805 encodeULEB128(0, getStream()); // the table index
807 // init expr for starting offset
808 write8(wasm::WASM_OPCODE_I32_CONST);
809 encodeSLEB128(kInitialTableOffset, getStream());
810 write8(wasm::WASM_OPCODE_END);
812 encodeULEB128(TableElems.size(), getStream());
813 for (uint32_t Elem : TableElems)
814 encodeULEB128(Elem, getStream());
816 endSection(Section);
819 void WasmObjectWriter::writeCodeSection(const MCAssembler &Asm,
820 const MCAsmLayout &Layout,
821 ArrayRef<WasmFunction> Functions) {
822 if (Functions.empty())
823 return;
825 SectionBookkeeping Section;
826 startSection(Section, wasm::WASM_SEC_CODE);
827 CodeSectionIndex = Section.Index;
829 encodeULEB128(Functions.size(), getStream());
831 for (const WasmFunction &Func : Functions) {
832 auto &FuncSection = static_cast<MCSectionWasm &>(Func.Sym->getSection());
834 int64_t Size = 0;
835 if (!Func.Sym->getSize()->evaluateAsAbsolute(Size, Layout))
836 report_fatal_error(".size expression must be evaluatable");
838 encodeULEB128(Size, getStream());
839 FuncSection.setSectionOffset(getStream().tell() - Section.ContentsOffset);
840 Asm.writeSectionData(&FuncSection, Layout);
843 // Apply fixups.
844 applyRelocations(CodeRelocations, Section.ContentsOffset);
846 endSection(Section);
849 void WasmObjectWriter::writeDataSection() {
850 if (DataSegments.empty())
851 return;
853 SectionBookkeeping Section;
854 startSection(Section, wasm::WASM_SEC_DATA);
855 DataSectionIndex = Section.Index;
857 encodeULEB128(DataSegments.size(), getStream()); // count
859 for (const WasmDataSegment &Segment : DataSegments) {
860 encodeULEB128(0, getStream()); // memory index
861 write8(wasm::WASM_OPCODE_I32_CONST);
862 encodeSLEB128(Segment.Offset, getStream()); // offset
863 write8(wasm::WASM_OPCODE_END);
864 encodeULEB128(Segment.Data.size(), getStream()); // size
865 Segment.Section->setSectionOffset(getStream().tell() - Section.ContentsOffset);
866 writeBytes(Segment.Data); // data
869 // Apply fixups.
870 applyRelocations(DataRelocations, Section.ContentsOffset);
872 endSection(Section);
875 void WasmObjectWriter::writeRelocSection(
876 uint32_t SectionIndex, StringRef Name,
877 ArrayRef<WasmRelocationEntry> Relocations) {
878 // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
879 // for descriptions of the reloc sections.
881 if (Relocations.empty())
882 return;
884 SectionBookkeeping Section;
885 startCustomSection(Section, std::string("reloc.") + Name.str());
887 raw_pwrite_stream &Stream = getStream();
889 encodeULEB128(SectionIndex, Stream);
890 encodeULEB128(Relocations.size(), Stream);
891 for (const WasmRelocationEntry& RelEntry : Relocations) {
892 uint64_t Offset = RelEntry.Offset +
893 RelEntry.FixupSection->getSectionOffset();
894 uint32_t Index = getRelocationIndexValue(RelEntry);
896 write8(RelEntry.Type);
897 encodeULEB128(Offset, Stream);
898 encodeULEB128(Index, Stream);
899 if (RelEntry.hasAddend())
900 encodeSLEB128(RelEntry.Addend, Stream);
903 endSection(Section);
906 void WasmObjectWriter::writeCustomRelocSections() {
907 for (const auto &Sec : CustomSections) {
908 auto &Relocations = CustomSectionsRelocations[Sec.Section];
909 writeRelocSection(Sec.OutputIndex, Sec.Name, Relocations);
913 void WasmObjectWriter::writeLinkingMetaDataSection(
914 ArrayRef<wasm::WasmSymbolInfo> SymbolInfos,
915 ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
916 const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats) {
917 SectionBookkeeping Section;
918 startCustomSection(Section, "linking");
919 encodeULEB128(wasm::WasmMetadataVersion, getStream());
921 SectionBookkeeping SubSection;
922 if (SymbolInfos.size() != 0) {
923 startSection(SubSection, wasm::WASM_SYMBOL_TABLE);
924 encodeULEB128(SymbolInfos.size(), getStream());
925 for (const wasm::WasmSymbolInfo &Sym : SymbolInfos) {
926 encodeULEB128(Sym.Kind, getStream());
927 encodeULEB128(Sym.Flags, getStream());
928 switch (Sym.Kind) {
929 case wasm::WASM_SYMBOL_TYPE_FUNCTION:
930 case wasm::WASM_SYMBOL_TYPE_GLOBAL:
931 encodeULEB128(Sym.ElementIndex, getStream());
932 if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0)
933 writeString(Sym.Name);
934 break;
935 case wasm::WASM_SYMBOL_TYPE_DATA:
936 writeString(Sym.Name);
937 if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) {
938 encodeULEB128(Sym.DataRef.Segment, getStream());
939 encodeULEB128(Sym.DataRef.Offset, getStream());
940 encodeULEB128(Sym.DataRef.Size, getStream());
942 break;
943 case wasm::WASM_SYMBOL_TYPE_SECTION: {
944 const uint32_t SectionIndex =
945 CustomSections[Sym.ElementIndex].OutputIndex;
946 encodeULEB128(SectionIndex, getStream());
947 break;
949 default:
950 llvm_unreachable("unexpected kind");
953 endSection(SubSection);
956 if (DataSegments.size()) {
957 startSection(SubSection, wasm::WASM_SEGMENT_INFO);
958 encodeULEB128(DataSegments.size(), getStream());
959 for (const WasmDataSegment &Segment : DataSegments) {
960 writeString(Segment.Name);
961 encodeULEB128(Segment.Alignment, getStream());
962 encodeULEB128(Segment.Flags, getStream());
964 endSection(SubSection);
967 if (!InitFuncs.empty()) {
968 startSection(SubSection, wasm::WASM_INIT_FUNCS);
969 encodeULEB128(InitFuncs.size(), getStream());
970 for (auto &StartFunc : InitFuncs) {
971 encodeULEB128(StartFunc.first, getStream()); // priority
972 encodeULEB128(StartFunc.second, getStream()); // function index
974 endSection(SubSection);
977 if (Comdats.size()) {
978 startSection(SubSection, wasm::WASM_COMDAT_INFO);
979 encodeULEB128(Comdats.size(), getStream());
980 for (const auto &C : Comdats) {
981 writeString(C.first);
982 encodeULEB128(0, getStream()); // flags for future use
983 encodeULEB128(C.second.size(), getStream());
984 for (const WasmComdatEntry &Entry : C.second) {
985 encodeULEB128(Entry.Kind, getStream());
986 encodeULEB128(Entry.Index, getStream());
989 endSection(SubSection);
992 endSection(Section);
995 void WasmObjectWriter::writeCustomSections(const MCAssembler &Asm,
996 const MCAsmLayout &Layout) {
997 for (auto &CustomSection : CustomSections) {
998 SectionBookkeeping Section;
999 auto *Sec = CustomSection.Section;
1000 startCustomSection(Section, CustomSection.Name);
1002 Sec->setSectionOffset(getStream().tell() - Section.ContentsOffset);
1003 Asm.writeSectionData(Sec, Layout);
1005 CustomSection.OutputContentsOffset = Section.ContentsOffset;
1006 CustomSection.OutputIndex = Section.Index;
1008 endSection(Section);
1012 void WasmObjectWriter::updateCustomSectionRelocations(
1013 const SmallVector<WasmFunction, 4> &Functions, const MCAsmLayout &Layout) {
1014 std::map<const MCSection *, const MCSymbolWasm *> SectionSymbols;
1015 for (const auto &P : CustomSectionSymbols)
1016 SectionSymbols[P.second] = P.first;
1017 std::map<const MCSection *, const MCSymbolWasm *> FuncSymbols;
1018 for (const auto &FuncInfo : Functions)
1019 FuncSymbols[&FuncInfo.Sym->getSection()] = FuncInfo.Sym;
1021 // Patch relocation records for R_WEBASSEMBLY_FUNCTION_OFFSET_I32 and
1022 // R_WEBASSEMBLY_SECTION_OFFSET_I32. The Addend is stuffed the offset from
1023 // the beginning of the function or custom section -- all such relocations
1024 // target the function or custom section starts.
1025 for (auto &Section : CustomSections) {
1026 auto &Relocations = CustomSectionsRelocations[Section.Section];
1027 for (WasmRelocationEntry &RelEntry : Relocations) {
1028 switch (RelEntry.Type) {
1029 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32: {
1030 assert(RelEntry.hasAddend());
1031 auto &Section =
1032 static_cast<MCSectionWasm &>(RelEntry.Symbol->getSection());
1033 RelEntry.Addend += Layout.getSymbolOffset(*RelEntry.Symbol);
1034 RelEntry.Symbol = FuncSymbols[&Section];
1035 break;
1037 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32: {
1038 assert(RelEntry.hasAddend());
1039 auto &Section =
1040 static_cast<MCSectionWasm &>(RelEntry.Symbol->getSection());
1041 RelEntry.Addend += Layout.getSymbolOffset(*RelEntry.Symbol);
1042 RelEntry.Symbol = SectionSymbols[&Section];
1043 break;
1045 default:
1046 break;
1050 // Apply fixups.
1051 applyRelocations(Relocations, Section.OutputContentsOffset);
1055 uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm& Symbol) {
1056 assert(Symbol.isFunction());
1057 assert(TypeIndices.count(&Symbol));
1058 return TypeIndices[&Symbol];
1061 uint32_t WasmObjectWriter::registerFunctionType(const MCSymbolWasm& Symbol) {
1062 assert(Symbol.isFunction());
1064 WasmFunctionType F;
1065 const MCSymbolWasm* ResolvedSym = ResolveSymbol(Symbol);
1066 F.Returns = ResolvedSym->getReturns();
1067 F.Params = ResolvedSym->getParams();
1069 auto Pair =
1070 FunctionTypeIndices.insert(std::make_pair(F, FunctionTypes.size()));
1071 if (Pair.second)
1072 FunctionTypes.push_back(F);
1073 TypeIndices[&Symbol] = Pair.first->second;
1075 DEBUG(dbgs() << "registerFunctionType: " << Symbol << " new:" << Pair.second << "\n");
1076 DEBUG(dbgs() << " -> type index: " << Pair.first->second << "\n");
1077 return Pair.first->second;
1080 void WasmObjectWriter::writeObject(MCAssembler &Asm,
1081 const MCAsmLayout &Layout) {
1082 DEBUG(dbgs() << "WasmObjectWriter::writeObject\n");
1083 MCContext &Ctx = Asm.getContext();
1085 // Collect information from the available symbols.
1086 SmallVector<WasmFunction, 4> Functions;
1087 SmallVector<uint32_t, 4> TableElems;
1088 SmallVector<wasm::WasmImport, 4> Imports;
1089 SmallVector<wasm::WasmExport, 4> Exports;
1090 SmallVector<wasm::WasmSymbolInfo, 4> SymbolInfos;
1091 SmallVector<std::pair<uint16_t, uint32_t>, 2> InitFuncs;
1092 std::map<StringRef, std::vector<WasmComdatEntry>> Comdats;
1093 uint32_t DataSize = 0;
1095 // For now, always emit the memory import, since loads and stores are not
1096 // valid without it. In the future, we could perhaps be more clever and omit
1097 // it if there are no loads or stores.
1098 MCSymbolWasm *MemorySym =
1099 cast<MCSymbolWasm>(Ctx.getOrCreateSymbol("__linear_memory"));
1100 wasm::WasmImport MemImport;
1101 MemImport.Module = MemorySym->getModuleName();
1102 MemImport.Field = MemorySym->getName();
1103 MemImport.Kind = wasm::WASM_EXTERNAL_MEMORY;
1104 Imports.push_back(MemImport);
1106 // For now, always emit the table section, since indirect calls are not
1107 // valid without it. In the future, we could perhaps be more clever and omit
1108 // it if there are no indirect calls.
1109 MCSymbolWasm *TableSym =
1110 cast<MCSymbolWasm>(Ctx.getOrCreateSymbol("__indirect_function_table"));
1111 wasm::WasmImport TableImport;
1112 TableImport.Module = TableSym->getModuleName();
1113 TableImport.Field = TableSym->getName();
1114 TableImport.Kind = wasm::WASM_EXTERNAL_TABLE;
1115 TableImport.Table.ElemType = wasm::WASM_TYPE_ANYFUNC;
1116 Imports.push_back(TableImport);
1118 // Populate FunctionTypeIndices, and Imports and WasmIndices for undefined
1119 // symbols. This must be done before populating WasmIndices for defined
1120 // symbols.
1121 for (const MCSymbol &S : Asm.symbols()) {
1122 const auto &WS = static_cast<const MCSymbolWasm &>(S);
1124 // Register types for all functions, including those with private linkage
1125 // (because wasm always needs a type signature).
1126 if (WS.isFunction())
1127 registerFunctionType(WS);
1129 if (WS.isTemporary())
1130 continue;
1132 // If the symbol is not defined in this translation unit, import it.
1133 if (!WS.isDefined() && !WS.isComdat()) {
1134 if (WS.isFunction()) {
1135 wasm::WasmImport Import;
1136 Import.Module = WS.getModuleName();
1137 Import.Field = WS.getName();
1138 Import.Kind = wasm::WASM_EXTERNAL_FUNCTION;
1139 Import.SigIndex = getFunctionType(WS);
1140 Imports.push_back(Import);
1141 WasmIndices[&WS] = NumFunctionImports++;
1142 } else if (WS.isGlobal()) {
1143 if (WS.isWeak())
1144 report_fatal_error("undefined global symbol cannot be weak");
1146 wasm::WasmImport Import;
1147 Import.Module = WS.getModuleName();
1148 Import.Field = WS.getName();
1149 Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
1150 Import.Global = WS.getGlobalType();
1151 Imports.push_back(Import);
1152 WasmIndices[&WS] = NumGlobalImports++;
1157 // Populate DataSegments, which must be done before populating DataLocations.
1158 for (MCSection &Sec : Asm) {
1159 auto &Section = static_cast<MCSectionWasm &>(Sec);
1161 if (Section.getSectionName().startswith(".custom_section.")) {
1162 if (Section.getFragmentList().empty())
1163 continue;
1164 if (Section.getFragmentList().size() != 1)
1165 report_fatal_error(
1166 "only one .custom_section section fragment supported");
1167 const MCFragment &Frag = *Section.begin();
1168 if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
1169 report_fatal_error("only data supported in .custom_section section");
1170 const auto &DataFrag = cast<MCDataFragment>(Frag);
1171 if (!DataFrag.getFixups().empty())
1172 report_fatal_error("fixups not supported in .custom_section section");
1173 StringRef UserName = Section.getSectionName().substr(16);
1174 CustomSections.emplace_back(UserName, &Section);
1175 continue;
1178 if (!Section.isWasmData())
1179 continue;
1181 // .init_array sections are handled specially elsewhere.
1182 if (cast<MCSectionWasm>(Sec).getSectionName().startswith(".init_array"))
1183 continue;
1185 uint32_t SegmentIndex = DataSegments.size();
1186 DataSize = alignTo(DataSize, Section.getAlignment());
1187 DataSegments.emplace_back();
1188 WasmDataSegment &Segment = DataSegments.back();
1189 Segment.Name = Section.getSectionName();
1190 Segment.Offset = DataSize;
1191 Segment.Section = &Section;
1192 addData(Segment.Data, Section);
1193 Segment.Alignment = Section.getAlignment();
1194 Segment.Flags = 0;
1195 DataSize += Segment.Data.size();
1196 Section.setSegmentIndex(SegmentIndex);
1198 if (const MCSymbolWasm *C = Section.getGroup()) {
1199 Comdats[C->getName()].emplace_back(
1200 WasmComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex});
1204 // Create symbols for debug/custom sections.
1205 for (MCSection &Sec : Asm) {
1206 auto &DebugSection = static_cast<MCSectionWasm &>(Sec);
1207 StringRef SectionName = DebugSection.getSectionName();
1209 // TODO: Add support for non-debug metadata sections?
1210 if (!Sec.getKind().isMetadata() || !SectionName.startswith(".debug_"))
1211 continue;
1213 uint32_t ElementIndex = CustomSections.size();
1214 CustomSections.emplace_back(SectionName, &DebugSection);
1216 MCSymbolWasm *SectionSym =
1217 cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SectionName));
1218 CustomSectionSymbols[SectionSym] = &DebugSection;
1220 wasm::WasmSymbolInfo Info;
1221 Info.Name = SectionSym->getName();
1222 Info.Kind = wasm::WASM_SYMBOL_TYPE_SECTION;
1223 Info.Flags = wasm::WASM_SYMBOL_BINDING_LOCAL;
1224 Info.ElementIndex = ElementIndex;
1225 SymbolIndices[SectionSym] = SymbolInfos.size();
1226 SymbolInfos.emplace_back(Info);
1229 // Populate WasmIndices and DataLocations for defined symbols.
1230 for (const MCSymbol &S : Asm.symbols()) {
1231 // Ignore unnamed temporary symbols, which aren't ever exported, imported,
1232 // or used in relocations.
1233 if (S.isTemporary() && S.getName().empty())
1234 continue;
1236 const auto &WS = static_cast<const MCSymbolWasm &>(S);
1237 DEBUG(dbgs() << "MCSymbol: '" << S << "'"
1238 << " isDefined=" << S.isDefined()
1239 << " isExternal=" << S.isExternal()
1240 << " isTemporary=" << S.isTemporary()
1241 << " isFunction=" << WS.isFunction()
1242 << " isWeak=" << WS.isWeak()
1243 << " isHidden=" << WS.isHidden()
1244 << " isVariable=" << WS.isVariable() << "\n");
1246 if (WS.isVariable())
1247 continue;
1248 if (WS.isComdat() && !WS.isDefined())
1249 continue;
1251 if (WS.isFunction()) {
1252 unsigned Index;
1253 if (WS.isDefined()) {
1254 if (WS.getOffset() != 0)
1255 report_fatal_error(
1256 "function sections must contain one function each");
1258 if (WS.getSize() == 0)
1259 report_fatal_error(
1260 "function symbols must have a size set with .size");
1262 // A definition. Write out the function body.
1263 Index = NumFunctionImports + Functions.size();
1264 WasmFunction Func;
1265 Func.Type = getFunctionType(WS);
1266 Func.Sym = &WS;
1267 WasmIndices[&WS] = Index;
1268 Functions.push_back(Func);
1270 auto &Section = static_cast<MCSectionWasm &>(WS.getSection());
1271 if (const MCSymbolWasm *C = Section.getGroup()) {
1272 Comdats[C->getName()].emplace_back(
1273 WasmComdatEntry{wasm::WASM_COMDAT_FUNCTION, Index});
1275 } else {
1276 // An import; the index was assigned above.
1277 Index = WasmIndices.find(&WS)->second;
1280 DEBUG(dbgs() << " -> function index: " << Index << "\n");
1281 } else if (WS.isData()) {
1282 if (WS.isTemporary() && !WS.getSize())
1283 continue;
1285 if (!WS.isDefined()) {
1286 DEBUG(dbgs() << " -> segment index: -1");
1287 continue;
1290 if (!WS.getSize())
1291 report_fatal_error("data symbols must have a size set with .size: " +
1292 WS.getName());
1294 int64_t Size = 0;
1295 if (!WS.getSize()->evaluateAsAbsolute(Size, Layout))
1296 report_fatal_error(".size expression must be evaluatable");
1298 auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection());
1299 assert(DataSection.isWasmData());
1301 // For each data symbol, export it in the symtab as a reference to the
1302 // corresponding Wasm data segment.
1303 wasm::WasmDataReference Ref = wasm::WasmDataReference{
1304 DataSection.getSegmentIndex(),
1305 static_cast<uint32_t>(Layout.getSymbolOffset(WS)),
1306 static_cast<uint32_t>(Size)};
1307 DataLocations[&WS] = Ref;
1308 DEBUG(dbgs() << " -> segment index: " << Ref.Segment);
1309 } else {
1310 // A "true" Wasm global (currently just __stack_pointer)
1311 if (WS.isDefined())
1312 report_fatal_error("don't yet support defined globals");
1314 // An import; the index was assigned above
1315 DEBUG(dbgs() << " -> global index: " << WasmIndices.find(&WS)->second
1316 << "\n");
1320 // Populate WasmIndices and DataLocations for aliased symbols. We need to
1321 // process these in a separate pass because we need to have processed the
1322 // target of the alias before the alias itself and the symbols are not
1323 // necessarily ordered in this way.
1324 for (const MCSymbol &S : Asm.symbols()) {
1325 if (!S.isVariable())
1326 continue;
1328 assert(S.isDefined());
1330 // Find the target symbol of this weak alias and export that index
1331 const auto &WS = static_cast<const MCSymbolWasm &>(S);
1332 const MCSymbolWasm *ResolvedSym = ResolveSymbol(WS);
1333 DEBUG(dbgs() << WS.getName() << ": weak alias of '" << *ResolvedSym << "'\n");
1335 if (WS.isFunction()) {
1336 assert(WasmIndices.count(ResolvedSym) > 0);
1337 uint32_t WasmIndex = WasmIndices.find(ResolvedSym)->second;
1338 WasmIndices[&WS] = WasmIndex;
1339 DEBUG(dbgs() << " -> index:" << WasmIndex << "\n");
1340 } else if (WS.isData()) {
1341 assert(DataLocations.count(ResolvedSym) > 0);
1342 const wasm::WasmDataReference &Ref =
1343 DataLocations.find(ResolvedSym)->second;
1344 DataLocations[&WS] = Ref;
1345 DEBUG(dbgs() << " -> index:" << Ref.Segment << "\n");
1346 } else {
1347 report_fatal_error("don't yet support global aliases");
1351 // Finally, populate the symbol table itself, in its "natural" order.
1352 for (const MCSymbol &S : Asm.symbols()) {
1353 const auto &WS = static_cast<const MCSymbolWasm &>(S);
1354 if (WS.isTemporary() && WS.getName().empty())
1355 continue;
1356 if (WS.isComdat() && !WS.isDefined())
1357 continue;
1358 if (WS.isTemporary() && WS.isData() && !WS.getSize())
1359 continue;
1361 uint32_t Flags = 0;
1362 if (WS.isWeak())
1363 Flags |= wasm::WASM_SYMBOL_BINDING_WEAK;
1364 if (WS.isHidden())
1365 Flags |= wasm::WASM_SYMBOL_VISIBILITY_HIDDEN;
1366 if (!WS.isExternal() && WS.isDefined())
1367 Flags |= wasm::WASM_SYMBOL_BINDING_LOCAL;
1368 if (WS.isUndefined())
1369 Flags |= wasm::WASM_SYMBOL_UNDEFINED;
1371 wasm::WasmSymbolInfo Info;
1372 Info.Name = WS.getName();
1373 Info.Kind = WS.getType();
1374 Info.Flags = Flags;
1375 if (!WS.isData())
1376 Info.ElementIndex = WasmIndices.find(&WS)->second;
1377 else if (WS.isDefined())
1378 Info.DataRef = DataLocations.find(&WS)->second;
1379 SymbolIndices[&WS] = SymbolInfos.size();
1380 SymbolInfos.emplace_back(Info);
1384 auto HandleReloc = [&](const WasmRelocationEntry &Rel) {
1385 // Functions referenced by a relocation need to put in the table. This is
1386 // purely to make the object file's provisional values readable, and is
1387 // ignored by the linker, which re-calculates the relocations itself.
1388 if (Rel.Type != wasm::R_WEBASSEMBLY_TABLE_INDEX_I32 &&
1389 Rel.Type != wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB)
1390 return;
1391 assert(Rel.Symbol->isFunction());
1392 const MCSymbolWasm &WS = *ResolveSymbol(*Rel.Symbol);
1393 uint32_t FunctionIndex = WasmIndices.find(&WS)->second;
1394 uint32_t TableIndex = TableElems.size() + kInitialTableOffset;
1395 if (TableIndices.try_emplace(&WS, TableIndex).second) {
1396 DEBUG(dbgs() << " -> adding " << WS.getName()
1397 << " to table: " << TableIndex << "\n");
1398 TableElems.push_back(FunctionIndex);
1399 registerFunctionType(WS);
1403 for (const WasmRelocationEntry &RelEntry : CodeRelocations)
1404 HandleReloc(RelEntry);
1405 for (const WasmRelocationEntry &RelEntry : DataRelocations)
1406 HandleReloc(RelEntry);
1409 // Translate .init_array section contents into start functions.
1410 for (const MCSection &S : Asm) {
1411 const auto &WS = static_cast<const MCSectionWasm &>(S);
1412 if (WS.getSectionName().startswith(".fini_array"))
1413 report_fatal_error(".fini_array sections are unsupported");
1414 if (!WS.getSectionName().startswith(".init_array"))
1415 continue;
1416 if (WS.getFragmentList().empty())
1417 continue;
1418 if (WS.getFragmentList().size() != 2)
1419 report_fatal_error("only one .init_array section fragment supported");
1420 const MCFragment &AlignFrag = *WS.begin();
1421 if (AlignFrag.getKind() != MCFragment::FT_Align)
1422 report_fatal_error(".init_array section should be aligned");
1423 if (cast<MCAlignFragment>(AlignFrag).getAlignment() != (is64Bit() ? 8 : 4))
1424 report_fatal_error(".init_array section should be aligned for pointers");
1425 const MCFragment &Frag = *std::next(WS.begin());
1426 if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
1427 report_fatal_error("only data supported in .init_array section");
1428 uint16_t Priority = UINT16_MAX;
1429 if (WS.getSectionName().size() != 11) {
1430 if (WS.getSectionName()[11] != '.')
1431 report_fatal_error(".init_array section priority should start with '.'");
1432 if (WS.getSectionName().substr(12).getAsInteger(10, Priority))
1433 report_fatal_error("invalid .init_array section priority");
1435 const auto &DataFrag = cast<MCDataFragment>(Frag);
1436 const SmallVectorImpl<char> &Contents = DataFrag.getContents();
1437 for (const uint8_t *p = (const uint8_t *)Contents.data(),
1438 *end = (const uint8_t *)Contents.data() + Contents.size();
1439 p != end; ++p) {
1440 if (*p != 0)
1441 report_fatal_error("non-symbolic data in .init_array section");
1443 for (const MCFixup &Fixup : DataFrag.getFixups()) {
1444 assert(Fixup.getKind() == MCFixup::getKindForSize(is64Bit() ? 8 : 4, false));
1445 const MCExpr *Expr = Fixup.getValue();
1446 auto *Sym = dyn_cast<MCSymbolRefExpr>(Expr);
1447 if (!Sym)
1448 report_fatal_error("fixups in .init_array should be symbol references");
1449 if (Sym->getKind() != MCSymbolRefExpr::VK_WebAssembly_FUNCTION)
1450 report_fatal_error("symbols in .init_array should be for functions");
1451 auto I = SymbolIndices.find(cast<MCSymbolWasm>(&Sym->getSymbol()));
1452 if (I == SymbolIndices.end())
1453 report_fatal_error("symbols in .init_array should be defined");
1454 uint32_t Index = I->second;
1455 InitFuncs.push_back(std::make_pair(Priority, Index));
1459 // Write out the Wasm header.
1460 writeHeader(Asm);
1462 writeTypeSection(FunctionTypes);
1463 writeImportSection(Imports, DataSize, TableElems.size());
1464 writeFunctionSection(Functions);
1465 // Skip the "table" section; we import the table instead.
1466 // Skip the "memory" section; we import the memory instead.
1467 writeGlobalSection();
1468 writeExportSection(Exports);
1469 writeElemSection(TableElems);
1470 writeCodeSection(Asm, Layout, Functions);
1471 writeDataSection();
1472 writeCustomSections(Asm, Layout);
1473 updateCustomSectionRelocations(Functions, Layout);
1474 writeLinkingMetaDataSection(SymbolInfos, InitFuncs, Comdats);
1475 writeRelocSection(CodeSectionIndex, "CODE", CodeRelocations);
1476 writeRelocSection(DataSectionIndex, "DATA", DataRelocations);
1477 writeCustomRelocSections();
1479 // TODO: Translate the .comment section to the output.
1482 std::unique_ptr<MCObjectWriter>
1483 llvm::createWasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
1484 raw_pwrite_stream &OS) {
1485 return llvm::make_unique<WasmObjectWriter>(std::move(MOTW), OS);