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