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