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