blob: 26dea0a97462d5763963f0fed0dbe66ad872fa54 [file] [log] [blame]
Dan Gohman18eafb62017-02-22 01:23:18 +00001//===- lib/MC/WasmObjectWriter.cpp - Wasm File Writer ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements Wasm object file writer information.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ADT/STLExtras.h"
15#include "llvm/ADT/SmallPtrSet.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000016#include "llvm/BinaryFormat/Wasm.h"
Nico Weber432a3882018-04-30 14:59:11 +000017#include "llvm/Config/llvm-config.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000018#include "llvm/MC/MCAsmBackend.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000019#include "llvm/MC/MCAsmLayout.h"
20#include "llvm/MC/MCAssembler.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCFixupKindInfo.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000024#include "llvm/MC/MCObjectWriter.h"
25#include "llvm/MC/MCSectionWasm.h"
26#include "llvm/MC/MCSymbolWasm.h"
27#include "llvm/MC/MCValue.h"
28#include "llvm/MC/MCWasmObjectWriter.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000029#include "llvm/Support/Casting.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000030#include "llvm/Support/Debug.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000031#include "llvm/Support/ErrorHandling.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000032#include "llvm/Support/LEB128.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000033#include "llvm/Support/StringSaver.h"
34#include <vector>
35
36using namespace llvm;
37
Sam Clegg5e3d33a2017-07-07 02:01:29 +000038#define DEBUG_TYPE "mc"
Dan Gohman18eafb62017-02-22 01:23:18 +000039
Sam Clegga165f2d2018-04-30 19:40:57 +000040static std::string toString(wasm::WasmSymbolType type) {
41 switch (type) {
42 case wasm::WASM_SYMBOL_TYPE_FUNCTION:
43 return "WASM_SYMBOL_TYPE_FUNCTION";
44 case wasm::WASM_SYMBOL_TYPE_GLOBAL:
45 return "WASM_SYMBOL_TYPE_GLOBAL";
46 case wasm::WASM_SYMBOL_TYPE_DATA:
47 return "WASM_SYMBOL_TYPE_DATA";
48 case wasm::WASM_SYMBOL_TYPE_SECTION:
49 return "WASM_SYMBOL_TYPE_SECTION";
50 }
51}
52
53static std::string relocTypetoString(uint32_t type) {
54 switch (type) {
55#define WASM_RELOC(NAME, VALUE) case VALUE: return #NAME;
56#include "llvm/BinaryFormat/WasmRelocs.def"
57#undef WASM_RELOC
58 default:
59 llvm_unreachable("uknown reloc type");
60 }
61}
62
Dan Gohman18eafb62017-02-22 01:23:18 +000063namespace {
Sam Clegg9e15f352017-06-03 02:01:24 +000064
Sam Clegg30e1bbc2018-01-19 18:57:01 +000065// Went we ceate the indirect function table we start at 1, so that there is
66// and emtpy slot at 0 and therefore calling a null function pointer will trap.
67static const uint32_t kInitialTableOffset = 1;
68
Dan Gohmand934cb82017-02-24 23:18:00 +000069// For patching purposes, we need to remember where each section starts, both
70// for patching up the section size field, and for patching up references to
71// locations within the section.
72struct SectionBookkeeping {
73 // Where the size of the section is written.
74 uint64_t SizeOffset;
Sam Clegg6a31a0d2018-04-26 19:27:28 +000075 // Where the section header ends (without custom section name).
76 uint64_t PayloadOffset;
77 // Where the contents of the section starts.
Dan Gohmand934cb82017-02-24 23:18:00 +000078 uint64_t ContentsOffset;
Sam Clegg6f08c842018-04-24 18:11:36 +000079 uint32_t Index;
Dan Gohmand934cb82017-02-24 23:18:00 +000080};
81
Sam Clegg9e15f352017-06-03 02:01:24 +000082// The signature of a wasm function, in a struct capable of being used as a
83// DenseMap key.
84struct WasmFunctionType {
85 // Support empty and tombstone instances, needed by DenseMap.
86 enum { Plain, Empty, Tombstone } State;
87
88 // The return types of the function.
89 SmallVector<wasm::ValType, 1> Returns;
90
91 // The parameter types of the function.
92 SmallVector<wasm::ValType, 4> Params;
93
94 WasmFunctionType() : State(Plain) {}
95
96 bool operator==(const WasmFunctionType &Other) const {
97 return State == Other.State && Returns == Other.Returns &&
98 Params == Other.Params;
99 }
100};
101
102// Traits for using WasmFunctionType in a DenseMap.
103struct WasmFunctionTypeDenseMapInfo {
104 static WasmFunctionType getEmptyKey() {
105 WasmFunctionType FuncTy;
106 FuncTy.State = WasmFunctionType::Empty;
107 return FuncTy;
108 }
109 static WasmFunctionType getTombstoneKey() {
110 WasmFunctionType FuncTy;
111 FuncTy.State = WasmFunctionType::Tombstone;
112 return FuncTy;
113 }
114 static unsigned getHashValue(const WasmFunctionType &FuncTy) {
115 uintptr_t Value = FuncTy.State;
116 for (wasm::ValType Ret : FuncTy.Returns)
117 Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Ret));
118 for (wasm::ValType Param : FuncTy.Params)
119 Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Param));
120 return Value;
121 }
122 static bool isEqual(const WasmFunctionType &LHS,
123 const WasmFunctionType &RHS) {
124 return LHS == RHS;
125 }
126};
127
Sam Clegg7c395942017-09-14 23:07:53 +0000128// A wasm data segment. A wasm binary contains only a single data section
129// but that can contain many segments, each with their own virtual location
130// in memory. Each MCSection data created by llvm is modeled as its own
131// wasm data segment.
132struct WasmDataSegment {
133 MCSectionWasm *Section;
Sam Cleggd95ed952017-09-20 19:03:35 +0000134 StringRef Name;
Sam Clegg7c395942017-09-14 23:07:53 +0000135 uint32_t Offset;
Sam Clegg63ebb812017-09-29 16:50:08 +0000136 uint32_t Alignment;
137 uint32_t Flags;
Sam Clegg7c395942017-09-14 23:07:53 +0000138 SmallVector<char, 4> Data;
139};
140
Sam Clegg9e15f352017-06-03 02:01:24 +0000141// A wasm function to be written into the function section.
142struct WasmFunction {
143 int32_t Type;
144 const MCSymbolWasm *Sym;
145};
146
Sam Clegg9e15f352017-06-03 02:01:24 +0000147// A wasm global to be written into the global section.
148struct WasmGlobal {
Sam Clegg6e7f1822018-01-31 19:50:14 +0000149 wasm::WasmGlobalType Type;
Sam Clegg9e15f352017-06-03 02:01:24 +0000150 uint64_t InitialValue;
Sam Clegg9e15f352017-06-03 02:01:24 +0000151};
152
Sam Cleggea7cace2018-01-09 23:43:14 +0000153// Information about a single item which is part of a COMDAT. For each data
154// segment or function which is in the COMDAT, there is a corresponding
155// WasmComdatEntry.
156struct WasmComdatEntry {
157 unsigned Kind;
158 uint32_t Index;
159};
160
Sam Clegg6dc65e92017-06-06 16:38:59 +0000161// Information about a single relocation.
162struct WasmRelocationEntry {
Sam Cleggfe6414b2017-06-21 23:46:41 +0000163 uint64_t Offset; // Where is the relocation.
164 const MCSymbolWasm *Symbol; // The symbol to relocate with.
165 int64_t Addend; // A value to add to the symbol.
166 unsigned Type; // The type of the relocation.
167 const MCSectionWasm *FixupSection;// The section the relocation is targeting.
Sam Clegg6dc65e92017-06-06 16:38:59 +0000168
169 WasmRelocationEntry(uint64_t Offset, const MCSymbolWasm *Symbol,
170 int64_t Addend, unsigned Type,
Sam Cleggfe6414b2017-06-21 23:46:41 +0000171 const MCSectionWasm *FixupSection)
Sam Clegg6dc65e92017-06-06 16:38:59 +0000172 : Offset(Offset), Symbol(Symbol), Addend(Addend), Type(Type),
173 FixupSection(FixupSection) {}
174
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000175 bool hasAddend() const {
176 switch (Type) {
Sam Clegg13a2e892017-09-01 17:32:01 +0000177 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
178 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
179 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000180 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
181 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000182 return true;
183 default:
184 return false;
185 }
186 }
187
Sam Clegg6dc65e92017-06-06 16:38:59 +0000188 void print(raw_ostream &Out) const {
Sam Clegga165f2d2018-04-30 19:40:57 +0000189 Out << relocTypetoString(Type)
190 << " Off=" << Offset << ", Sym=" << *Symbol << ", Addend=" << Addend
Sam Clegg759631c2017-09-15 20:54:59 +0000191 << ", FixupSection=" << FixupSection->getSectionName();
Sam Clegg6dc65e92017-06-06 16:38:59 +0000192 }
Sam Cleggb7787fd2017-06-20 04:04:59 +0000193
Aaron Ballman615eb472017-10-15 14:32:27 +0000194#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sam Cleggb7787fd2017-06-20 04:04:59 +0000195 LLVM_DUMP_METHOD void dump() const { print(dbgs()); }
196#endif
Sam Clegg6dc65e92017-06-06 16:38:59 +0000197};
198
Sam Cleggcfd44a22018-04-05 17:01:39 +0000199struct WasmCustomSection {
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000200 const uint32_t INVALID_INDEX = -1;
Sam Cleggcfd44a22018-04-05 17:01:39 +0000201
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000202 StringRef Name;
203 MCSectionWasm *Section;
204
205 uint32_t OutputContentsOffset;
206 uint32_t OutputIndex;
207
208 WasmCustomSection(StringRef Name, MCSectionWasm *Section)
209 : Name(Name), Section(Section), OutputContentsOffset(0),
210 OutputIndex(INVALID_INDEX) {}
Sam Cleggcfd44a22018-04-05 17:01:39 +0000211};
212
Sam Clegg1fb8daa2017-06-20 05:05:10 +0000213#if !defined(NDEBUG)
Sam Clegg7f055de2017-06-20 04:47:58 +0000214raw_ostream &operator<<(raw_ostream &OS, const WasmRelocationEntry &Rel) {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000215 Rel.print(OS);
216 return OS;
217}
Sam Clegg1fb8daa2017-06-20 05:05:10 +0000218#endif
Sam Cleggb7787fd2017-06-20 04:04:59 +0000219
Dan Gohman18eafb62017-02-22 01:23:18 +0000220class WasmObjectWriter : public MCObjectWriter {
Dan Gohman18eafb62017-02-22 01:23:18 +0000221 /// The target specific Wasm writer instance.
222 std::unique_ptr<MCWasmObjectTargetWriter> TargetObjectWriter;
223
Dan Gohmand934cb82017-02-24 23:18:00 +0000224 // Relocations for fixing up references in the code section.
225 std::vector<WasmRelocationEntry> CodeRelocations;
Sam Clegg6f08c842018-04-24 18:11:36 +0000226 uint32_t CodeSectionIndex;
Dan Gohmand934cb82017-02-24 23:18:00 +0000227
228 // Relocations for fixing up references in the data section.
229 std::vector<WasmRelocationEntry> DataRelocations;
Sam Clegg6f08c842018-04-24 18:11:36 +0000230 uint32_t DataSectionIndex;
Dan Gohmand934cb82017-02-24 23:18:00 +0000231
Dan Gohmand934cb82017-02-24 23:18:00 +0000232 // Index values to use for fixing up call_indirect type indices.
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000233 // Maps function symbols to the index of the type of the function
234 DenseMap<const MCSymbolWasm *, uint32_t> TypeIndices;
Sam Cleggd99f6072017-06-12 23:52:44 +0000235 // Maps function symbols to the table element index space. Used
236 // for TABLE_INDEX relocation types (i.e. address taken functions).
Sam Cleggf9edbe92018-01-31 19:28:47 +0000237 DenseMap<const MCSymbolWasm *, uint32_t> TableIndices;
Sam Clegg6c899ba2018-02-23 05:08:34 +0000238 // Maps function/global symbols to the (shared) Symbol index space.
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000239 DenseMap<const MCSymbolWasm *, uint32_t> SymbolIndices;
Sam Clegga165f2d2018-04-30 19:40:57 +0000240 // Maps function/global symbols to the function/global/section index space.
Sam Clegg6c899ba2018-02-23 05:08:34 +0000241 DenseMap<const MCSymbolWasm *, uint32_t> WasmIndices;
242 // Maps data symbols to the Wasm segment and offset/size with the segment.
243 DenseMap<const MCSymbolWasm *, wasm::WasmDataReference> DataLocations;
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000244 // Maps section symbols to the section.
245 DenseMap<const MCSymbolWasm *, const MCSectionWasm *> CustomSectionSymbols;
246
247 // Stores output data (index, relocations, content offset) for custom
248 // section.
249 std::vector<WasmCustomSection> CustomSections;
250 // Relocations for fixing up references in the custom sections.
251 DenseMap<const MCSectionWasm *, std::vector<WasmRelocationEntry>>
252 CustomSectionsRelocations;
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000253
254 DenseMap<WasmFunctionType, int32_t, WasmFunctionTypeDenseMapInfo>
255 FunctionTypeIndices;
Sam Clegg5e3d33a2017-07-07 02:01:29 +0000256 SmallVector<WasmFunctionType, 4> FunctionTypes;
Sam Clegg7c395942017-09-14 23:07:53 +0000257 SmallVector<WasmGlobal, 4> Globals;
Sam Clegg6c899ba2018-02-23 05:08:34 +0000258 SmallVector<WasmDataSegment, 4> DataSegments;
Sam Clegg9f3fe422018-01-17 19:28:43 +0000259 unsigned NumFunctionImports = 0;
Sam Clegg7c395942017-09-14 23:07:53 +0000260 unsigned NumGlobalImports = 0;
Chandler Carruth7e1c3342018-04-24 20:30:56 +0000261 uint32_t SectionCount = 0;
Dan Gohmand934cb82017-02-24 23:18:00 +0000262
Dan Gohman18eafb62017-02-22 01:23:18 +0000263 // TargetObjectWriter wrappers.
264 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
Sam Cleggae03c1e72017-06-13 18:51:50 +0000265 unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup) const {
266 return TargetObjectWriter->getRelocType(Target, Fixup);
Dan Gohman18eafb62017-02-22 01:23:18 +0000267 }
268
Sam Clegg2322a932018-04-23 19:16:19 +0000269 void startSection(SectionBookkeeping &Section, unsigned SectionId);
270 void startCustomSection(SectionBookkeeping &Section, StringRef Name);
Dan Gohmand934cb82017-02-24 23:18:00 +0000271 void endSection(SectionBookkeeping &Section);
272
Dan Gohman18eafb62017-02-22 01:23:18 +0000273public:
Lang Hames1301a872017-10-10 01:15:10 +0000274 WasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
275 raw_pwrite_stream &OS)
276 : MCObjectWriter(OS, /*IsLittleEndian=*/true),
277 TargetObjectWriter(std::move(MOTW)) {}
Dan Gohman18eafb62017-02-22 01:23:18 +0000278
Dan Gohman18eafb62017-02-22 01:23:18 +0000279 ~WasmObjectWriter() override;
280
Dan Gohman0917c9e2018-01-15 17:06:23 +0000281private:
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000282 void reset() override {
283 CodeRelocations.clear();
284 DataRelocations.clear();
285 TypeIndices.clear();
286 SymbolIndices.clear();
Sam Clegg6c899ba2018-02-23 05:08:34 +0000287 WasmIndices.clear();
Sam Cleggf9edbe92018-01-31 19:28:47 +0000288 TableIndices.clear();
Sam Clegg6c899ba2018-02-23 05:08:34 +0000289 DataLocations.clear();
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000290 CustomSectionsRelocations.clear();
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000291 FunctionTypeIndices.clear();
Sam Clegg5e3d33a2017-07-07 02:01:29 +0000292 FunctionTypes.clear();
Sam Clegg7c395942017-09-14 23:07:53 +0000293 Globals.clear();
Sam Clegg6c899ba2018-02-23 05:08:34 +0000294 DataSegments.clear();
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000295 CustomSectionSymbols.clear();
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000296 MCObjectWriter::reset();
Sam Clegg9f3fe422018-01-17 19:28:43 +0000297 NumFunctionImports = 0;
Sam Clegg7c395942017-09-14 23:07:53 +0000298 NumGlobalImports = 0;
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000299 }
300
Dan Gohman18eafb62017-02-22 01:23:18 +0000301 void writeHeader(const MCAssembler &Asm);
302
303 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
304 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindolaceecfe5b2017-07-11 23:56:10 +0000305 MCValue Target, uint64_t &FixedValue) override;
Dan Gohman18eafb62017-02-22 01:23:18 +0000306
307 void executePostLayoutBinding(MCAssembler &Asm,
308 const MCAsmLayout &Layout) override;
309
310 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Sam Clegg9e15f352017-06-03 02:01:24 +0000311
Sam Cleggb7787fd2017-06-20 04:04:59 +0000312 void writeString(const StringRef Str) {
313 encodeULEB128(Str.size(), getStream());
314 writeBytes(Str);
315 }
316
Sam Clegg9e15f352017-06-03 02:01:24 +0000317 void writeValueType(wasm::ValType Ty) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000318 write8(static_cast<uint8_t>(Ty));
Sam Clegg9e15f352017-06-03 02:01:24 +0000319 }
320
Sam Clegg457fb0b2017-09-15 19:50:44 +0000321 void writeTypeSection(ArrayRef<WasmFunctionType> FunctionTypes);
Sam Clegg8defa952018-02-12 22:41:29 +0000322 void writeImportSection(ArrayRef<wasm::WasmImport> Imports, uint32_t DataSize,
Sam Cleggf950b242017-12-11 23:03:38 +0000323 uint32_t NumElements);
Sam Clegg457fb0b2017-09-15 19:50:44 +0000324 void writeFunctionSection(ArrayRef<WasmFunction> Functions);
Sam Clegg7c395942017-09-14 23:07:53 +0000325 void writeGlobalSection();
Sam Clegg8defa952018-02-12 22:41:29 +0000326 void writeExportSection(ArrayRef<wasm::WasmExport> Exports);
Sam Clegg457fb0b2017-09-15 19:50:44 +0000327 void writeElemSection(ArrayRef<uint32_t> TableElems);
Sam Clegg9e15f352017-06-03 02:01:24 +0000328 void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
Sam Clegg457fb0b2017-09-15 19:50:44 +0000329 ArrayRef<WasmFunction> Functions);
Sam Clegg6c899ba2018-02-23 05:08:34 +0000330 void writeDataSection();
Sam Clegg6f08c842018-04-24 18:11:36 +0000331 void writeRelocSection(uint32_t SectionIndex, StringRef Name,
332 ArrayRef<WasmRelocationEntry> Relocations);
Sam Clegg31a2c802017-09-20 21:17:04 +0000333 void writeLinkingMetaDataSection(
Sam Clegg86b4a092018-02-27 23:57:37 +0000334 ArrayRef<wasm::WasmSymbolInfo> SymbolInfos,
Sam Cleggea7cace2018-01-09 23:43:14 +0000335 ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
Sam Clegg6c899ba2018-02-23 05:08:34 +0000336 const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats);
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000337 void writeCustomSections(const MCAssembler &Asm, const MCAsmLayout &Layout);
338 void writeCustomRelocSections();
339 void
340 updateCustomSectionRelocations(const SmallVector<WasmFunction, 4> &Functions,
341 const MCAsmLayout &Layout);
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000342
Sam Clegg7c395942017-09-14 23:07:53 +0000343 uint32_t getProvisionalValue(const WasmRelocationEntry &RelEntry);
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000344 void applyRelocations(ArrayRef<WasmRelocationEntry> Relocations,
345 uint64_t ContentsOffset);
346
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000347 uint32_t getRelocationIndexValue(const WasmRelocationEntry &RelEntry);
Sam Clegg6f08c842018-04-24 18:11:36 +0000348 uint32_t getFunctionType(const MCSymbolWasm &Symbol);
349 uint32_t registerFunctionType(const MCSymbolWasm &Symbol);
Dan Gohman18eafb62017-02-22 01:23:18 +0000350};
Sam Clegg9e15f352017-06-03 02:01:24 +0000351
Dan Gohman18eafb62017-02-22 01:23:18 +0000352} // end anonymous namespace
353
354WasmObjectWriter::~WasmObjectWriter() {}
355
Dan Gohmand934cb82017-02-24 23:18:00 +0000356// Write out a section header and a patchable section size field.
357void WasmObjectWriter::startSection(SectionBookkeeping &Section,
Sam Clegg2322a932018-04-23 19:16:19 +0000358 unsigned SectionId) {
359 DEBUG(dbgs() << "startSection " << SectionId << "\n");
Sam Clegg03e101f2018-03-01 18:06:21 +0000360 write8(SectionId);
Dan Gohmand934cb82017-02-24 23:18:00 +0000361
362 Section.SizeOffset = getStream().tell();
363
364 // The section size. We don't know the size yet, so reserve enough space
365 // for any 32-bit value; we'll patch it later.
366 encodeULEB128(UINT32_MAX, getStream());
367
368 // The position where the section starts, for measuring its size.
369 Section.ContentsOffset = getStream().tell();
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000370 Section.PayloadOffset = getStream().tell();
Sam Clegg6f08c842018-04-24 18:11:36 +0000371 Section.Index = SectionCount++;
Sam Clegg2322a932018-04-23 19:16:19 +0000372}
Dan Gohmand934cb82017-02-24 23:18:00 +0000373
Sam Clegg2322a932018-04-23 19:16:19 +0000374void WasmObjectWriter::startCustomSection(SectionBookkeeping &Section,
375 StringRef Name) {
376 DEBUG(dbgs() << "startCustomSection " << Name << "\n");
377 startSection(Section, wasm::WASM_SEC_CUSTOM);
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000378
379 // The position where the section header ends, for measuring its size.
380 Section.PayloadOffset = getStream().tell();
381
Dan Gohmand934cb82017-02-24 23:18:00 +0000382 // Custom sections in wasm also have a string identifier.
Sam Clegg2322a932018-04-23 19:16:19 +0000383 writeString(Name);
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000384
385 // The position where the custom section starts.
386 Section.ContentsOffset = getStream().tell();
Dan Gohmand934cb82017-02-24 23:18:00 +0000387}
388
389// Now that the section is complete and we know how big it is, patch up the
390// section size field at the start of the section.
391void WasmObjectWriter::endSection(SectionBookkeeping &Section) {
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000392 uint64_t Size = getStream().tell() - Section.PayloadOffset;
Dan Gohmand934cb82017-02-24 23:18:00 +0000393 if (uint32_t(Size) != Size)
394 report_fatal_error("section size does not fit in a uint32_t");
395
Sam Cleggb7787fd2017-06-20 04:04:59 +0000396 DEBUG(dbgs() << "endSection size=" << Size << "\n");
Dan Gohmand934cb82017-02-24 23:18:00 +0000397
398 // Write the final section size to the payload_len field, which follows
399 // the section id byte.
400 uint8_t Buffer[16];
Sam Clegg66a99e42017-09-15 20:34:47 +0000401 unsigned SizeLen = encodeULEB128(Size, Buffer, 5);
Dan Gohmand934cb82017-02-24 23:18:00 +0000402 assert(SizeLen == 5);
403 getStream().pwrite((char *)Buffer, SizeLen, Section.SizeOffset);
404}
405
Dan Gohman18eafb62017-02-22 01:23:18 +0000406// Emit the Wasm header.
407void WasmObjectWriter::writeHeader(const MCAssembler &Asm) {
Dan Gohman7ea5adf2017-02-22 18:50:20 +0000408 writeBytes(StringRef(wasm::WasmMagic, sizeof(wasm::WasmMagic)));
409 writeLE32(wasm::WasmVersion);
Dan Gohman18eafb62017-02-22 01:23:18 +0000410}
411
412void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
413 const MCAsmLayout &Layout) {
414}
415
416void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
417 const MCAsmLayout &Layout,
418 const MCFragment *Fragment,
419 const MCFixup &Fixup, MCValue Target,
Rafael Espindolaceecfe5b2017-07-11 23:56:10 +0000420 uint64_t &FixedValue) {
421 MCAsmBackend &Backend = Asm.getBackend();
422 bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
423 MCFixupKindInfo::FKF_IsPCRel;
Sam Cleggfe6414b2017-06-21 23:46:41 +0000424 const auto &FixupSection = cast<MCSectionWasm>(*Fragment->getParent());
Dan Gohmand934cb82017-02-24 23:18:00 +0000425 uint64_t C = Target.getConstant();
426 uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
427 MCContext &Ctx = Asm.getContext();
428
Sam Cleggbafe6902017-12-15 00:17:10 +0000429 // The .init_array isn't translated as data, so don't do relocations in it.
430 if (FixupSection.getSectionName().startswith(".init_array"))
431 return;
432
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000433 // TODO: Add support for non-debug metadata sections?
434 if (FixupSection.getKind().isMetadata() &&
435 !FixupSection.getSectionName().startswith(".debug_"))
Sam Cleggb7a54692018-02-16 18:06:05 +0000436 return;
437
Dan Gohmand934cb82017-02-24 23:18:00 +0000438 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
439 assert(RefB->getKind() == MCSymbolRefExpr::VK_None &&
440 "Should not have constructed this");
441
442 // Let A, B and C being the components of Target and R be the location of
443 // the fixup. If the fixup is not pcrel, we want to compute (A - B + C).
444 // If it is pcrel, we want to compute (A - B + C - R).
445
446 // In general, Wasm has no relocations for -B. It can only represent (A + C)
447 // or (A + C - R). If B = R + K and the relocation is not pcrel, we can
448 // replace B to implement it: (A - R - K + C)
449 if (IsPCRel) {
450 Ctx.reportError(
451 Fixup.getLoc(),
452 "No relocation available to represent this relative expression");
453 return;
454 }
455
456 const auto &SymB = cast<MCSymbolWasm>(RefB->getSymbol());
457
458 if (SymB.isUndefined()) {
459 Ctx.reportError(Fixup.getLoc(),
460 Twine("symbol '") + SymB.getName() +
461 "' can not be undefined in a subtraction expression");
462 return;
463 }
464
465 assert(!SymB.isAbsolute() && "Should have been folded");
466 const MCSection &SecB = SymB.getSection();
467 if (&SecB != &FixupSection) {
468 Ctx.reportError(Fixup.getLoc(),
469 "Cannot represent a difference across sections");
470 return;
471 }
472
473 uint64_t SymBOffset = Layout.getSymbolOffset(SymB);
474 uint64_t K = SymBOffset - FixupOffset;
475 IsPCRel = true;
476 C -= K;
477 }
478
479 // We either rejected the fixup or folded B into C at this point.
480 const MCSymbolRefExpr *RefA = Target.getSymA();
481 const auto *SymA = RefA ? cast<MCSymbolWasm>(&RefA->getSymbol()) : nullptr;
482
Dan Gohmand934cb82017-02-24 23:18:00 +0000483 if (SymA && SymA->isVariable()) {
484 const MCExpr *Expr = SymA->getVariableValue();
Sam Clegg6ad8f192017-07-11 02:21:57 +0000485 const auto *Inner = cast<MCSymbolRefExpr>(Expr);
486 if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
487 llvm_unreachable("weakref used in reloc not yet implemented");
Dan Gohmand934cb82017-02-24 23:18:00 +0000488 }
489
490 // Put any constant offset in an addend. Offsets can be negative, and
491 // LLVM expects wrapping, in contrast to wasm's immediates which can't
492 // be negative and don't wrap.
493 FixedValue = 0;
494
Sam Clegg6ad8f192017-07-11 02:21:57 +0000495 if (SymA)
496 SymA->setUsedInReloc();
Dan Gohmand934cb82017-02-24 23:18:00 +0000497
Sam Cleggae03c1e72017-06-13 18:51:50 +0000498 assert(!IsPCRel);
Sam Clegg9d24fb72017-06-16 23:59:10 +0000499 assert(SymA);
500
Sam Cleggae03c1e72017-06-13 18:51:50 +0000501 unsigned Type = getRelocType(Target, Fixup);
502
Dan Gohmand934cb82017-02-24 23:18:00 +0000503 WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection);
Sam Cleggb7787fd2017-06-20 04:04:59 +0000504 DEBUG(dbgs() << "WasmReloc: " << Rec << "\n");
Dan Gohmand934cb82017-02-24 23:18:00 +0000505
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000506 // Relocation other than R_WEBASSEMBLY_TYPE_INDEX_LEB,
507 // R_WEBASSEMBLY_SECTION_OFFSET_I32 or R_WEBASSEMBLY_FUNCTION_OFFSET_I32
508 // are currently required to be against a named symbol.
Sam Cleggb7a54692018-02-16 18:06:05 +0000509 // TODO(sbc): Add support for relocations against unnamed temporaries such
510 // as those generated by llvm's `blockaddress`.
511 // See: test/MC/WebAssembly/blockaddress.ll
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000512 if (SymA->getName().empty() &&
513 !(Type == wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB ||
514 Type == wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32 ||
515 Type == wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32))
Sam Cleggb7a54692018-02-16 18:06:05 +0000516 report_fatal_error("relocations against un-named temporaries are not yet "
517 "supported by wasm");
518
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000519 if (FixupSection.isWasmData()) {
Dan Gohmand934cb82017-02-24 23:18:00 +0000520 DataRelocations.push_back(Rec);
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000521 } else if (FixupSection.getKind().isText()) {
Sam Clegg12fd3da2017-10-20 21:28:38 +0000522 CodeRelocations.push_back(Rec);
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000523 } else if (FixupSection.getKind().isMetadata()) {
524 assert(FixupSection.getSectionName().startswith(".debug_"));
525 CustomSectionsRelocations[&FixupSection].push_back(Rec);
526 } else {
Sam Clegg12fd3da2017-10-20 21:28:38 +0000527 llvm_unreachable("unexpected section type");
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000528 }
Dan Gohmand934cb82017-02-24 23:18:00 +0000529}
530
Dan Gohmand934cb82017-02-24 23:18:00 +0000531// Write X as an (unsigned) LEB value at offset Offset in Stream, padded
532// to allow patching.
533static void
534WritePatchableLEB(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
535 uint8_t Buffer[5];
Sam Clegg66a99e42017-09-15 20:34:47 +0000536 unsigned SizeLen = encodeULEB128(X, Buffer, 5);
Dan Gohmand934cb82017-02-24 23:18:00 +0000537 assert(SizeLen == 5);
538 Stream.pwrite((char *)Buffer, SizeLen, Offset);
539}
540
541// Write X as an signed LEB value at offset Offset in Stream, padded
542// to allow patching.
543static void
544WritePatchableSLEB(raw_pwrite_stream &Stream, int32_t X, uint64_t Offset) {
545 uint8_t Buffer[5];
Sam Clegg66a99e42017-09-15 20:34:47 +0000546 unsigned SizeLen = encodeSLEB128(X, Buffer, 5);
Dan Gohmand934cb82017-02-24 23:18:00 +0000547 assert(SizeLen == 5);
548 Stream.pwrite((char *)Buffer, SizeLen, Offset);
549}
550
551// Write X as a plain integer value at offset Offset in Stream.
552static void WriteI32(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
553 uint8_t Buffer[4];
554 support::endian::write32le(Buffer, X);
555 Stream.pwrite((char *)Buffer, sizeof(Buffer), Offset);
556}
557
Sam Cleggaff1c4d2017-09-15 19:22:01 +0000558static const MCSymbolWasm* ResolveSymbol(const MCSymbolWasm& Symbol) {
559 if (Symbol.isVariable()) {
560 const MCExpr *Expr = Symbol.getVariableValue();
561 auto *Inner = cast<MCSymbolRefExpr>(Expr);
562 return cast<MCSymbolWasm>(&Inner->getSymbol());
563 }
564 return &Symbol;
565}
566
Dan Gohmand934cb82017-02-24 23:18:00 +0000567// Compute a value to write into the code at the location covered
Sam Clegg60ec3032018-01-23 01:23:17 +0000568// by RelEntry. This value isn't used by the static linker; it just serves
569// to make the object format more readable and more likely to be directly
570// useable.
Sam Clegg7c395942017-09-14 23:07:53 +0000571uint32_t
572WasmObjectWriter::getProvisionalValue(const WasmRelocationEntry &RelEntry) {
Sam Clegg60ec3032018-01-23 01:23:17 +0000573 switch (RelEntry.Type) {
574 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
Sam Cleggf9edbe92018-01-31 19:28:47 +0000575 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32: {
576 // Provisional value is table address of the resolved symbol itself
577 const MCSymbolWasm *Sym = ResolveSymbol(*RelEntry.Symbol);
578 assert(Sym->isFunction());
579 return TableIndices[Sym];
580 }
Sam Clegg60ec3032018-01-23 01:23:17 +0000581 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
Sam Clegg6c899ba2018-02-23 05:08:34 +0000582 // Provisional value is same as the index
Sam Clegg60ec3032018-01-23 01:23:17 +0000583 return getRelocationIndexValue(RelEntry);
Sam Clegg6c899ba2018-02-23 05:08:34 +0000584 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
585 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
586 // Provisional value is function/global Wasm index
587 if (!WasmIndices.count(RelEntry.Symbol))
588 report_fatal_error("symbol not found in wasm index space: " +
589 RelEntry.Symbol->getName());
590 return WasmIndices[RelEntry.Symbol];
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000591 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32: {
592 const auto &Section =
593 static_cast<const MCSectionWasm &>(RelEntry.Symbol->getSection());
594 return Section.getSectionOffset() + RelEntry.Addend;
595 }
596 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32: {
597 const auto &Section = *CustomSectionSymbols.find(RelEntry.Symbol)->second;
598 return Section.getSectionOffset() + RelEntry.Addend;
599 }
Sam Clegg60ec3032018-01-23 01:23:17 +0000600 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
601 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
602 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: {
Sam Cleggf9edbe92018-01-31 19:28:47 +0000603 // Provisional value is address of the global
Sam Clegg60ec3032018-01-23 01:23:17 +0000604 const MCSymbolWasm *Sym = ResolveSymbol(*RelEntry.Symbol);
605 // For undefined symbols, use zero
606 if (!Sym->isDefined())
607 return 0;
Sam Clegg6c899ba2018-02-23 05:08:34 +0000608 const wasm::WasmDataReference &Ref = DataLocations[Sym];
609 const WasmDataSegment &Segment = DataSegments[Ref.Segment];
Sam Clegg60ec3032018-01-23 01:23:17 +0000610 // Ignore overflow. LLVM allows address arithmetic to silently wrap.
Sam Clegg6c899ba2018-02-23 05:08:34 +0000611 return Segment.Offset + Ref.Offset + RelEntry.Addend;
Sam Clegg60ec3032018-01-23 01:23:17 +0000612 }
613 default:
614 llvm_unreachable("invalid relocation type");
615 }
Dan Gohmand934cb82017-02-24 23:18:00 +0000616}
617
Sam Clegg759631c2017-09-15 20:54:59 +0000618static void addData(SmallVectorImpl<char> &DataBytes,
Sam Clegg63ebb812017-09-29 16:50:08 +0000619 MCSectionWasm &DataSection) {
Sam Clegg759631c2017-09-15 20:54:59 +0000620 DEBUG(errs() << "addData: " << DataSection.getSectionName() << "\n");
621
Sam Clegg63ebb812017-09-29 16:50:08 +0000622 DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment()));
623
Sam Clegg759631c2017-09-15 20:54:59 +0000624 for (const MCFragment &Frag : DataSection) {
625 if (Frag.hasInstructions())
626 report_fatal_error("only data supported in data sections");
627
628 if (auto *Align = dyn_cast<MCAlignFragment>(&Frag)) {
629 if (Align->getValueSize() != 1)
630 report_fatal_error("only byte values supported for alignment");
631 // If nops are requested, use zeros, as this is the data section.
632 uint8_t Value = Align->hasEmitNops() ? 0 : Align->getValue();
633 uint64_t Size = std::min<uint64_t>(alignTo(DataBytes.size(),
634 Align->getAlignment()),
635 DataBytes.size() +
636 Align->getMaxBytesToEmit());
637 DataBytes.resize(Size, Value);
638 } else if (auto *Fill = dyn_cast<MCFillFragment>(&Frag)) {
Rafael Espindolad707c372018-01-09 22:48:37 +0000639 int64_t Size;
640 if (!Fill->getSize().evaluateAsAbsolute(Size))
641 llvm_unreachable("The fill should be an assembler constant");
642 DataBytes.insert(DataBytes.end(), Size, Fill->getValue());
Sam Clegg759631c2017-09-15 20:54:59 +0000643 } else {
644 const auto &DataFrag = cast<MCDataFragment>(Frag);
645 const SmallVectorImpl<char> &Contents = DataFrag.getContents();
646
647 DataBytes.insert(DataBytes.end(), Contents.begin(), Contents.end());
648 }
649 }
650
651 DEBUG(dbgs() << "addData -> " << DataBytes.size() << "\n");
652}
653
Sam Clegg60ec3032018-01-23 01:23:17 +0000654uint32_t
655WasmObjectWriter::getRelocationIndexValue(const WasmRelocationEntry &RelEntry) {
656 if (RelEntry.Type == wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB) {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000657 if (!TypeIndices.count(RelEntry.Symbol))
Sam Clegg5e3d33a2017-07-07 02:01:29 +0000658 report_fatal_error("symbol not found in type index space: " +
Sam Cleggb7787fd2017-06-20 04:04:59 +0000659 RelEntry.Symbol->getName());
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000660 return TypeIndices[RelEntry.Symbol];
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000661 }
Sam Clegg60ec3032018-01-23 01:23:17 +0000662
663 if (!SymbolIndices.count(RelEntry.Symbol))
Sam Clegg6c899ba2018-02-23 05:08:34 +0000664 report_fatal_error("symbol not found in symbol index space: " +
Sam Clegg60ec3032018-01-23 01:23:17 +0000665 RelEntry.Symbol->getName());
666 return SymbolIndices[RelEntry.Symbol];
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000667}
668
Dan Gohmand934cb82017-02-24 23:18:00 +0000669// Apply the portions of the relocation records that we can handle ourselves
670// directly.
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000671void WasmObjectWriter::applyRelocations(
672 ArrayRef<WasmRelocationEntry> Relocations, uint64_t ContentsOffset) {
673 raw_pwrite_stream &Stream = getStream();
Dan Gohmand934cb82017-02-24 23:18:00 +0000674 for (const WasmRelocationEntry &RelEntry : Relocations) {
675 uint64_t Offset = ContentsOffset +
676 RelEntry.FixupSection->getSectionOffset() +
677 RelEntry.Offset;
Dan Gohmand934cb82017-02-24 23:18:00 +0000678
Sam Cleggb7787fd2017-06-20 04:04:59 +0000679 DEBUG(dbgs() << "applyRelocation: " << RelEntry << "\n");
Sam Clegg60ec3032018-01-23 01:23:17 +0000680 uint32_t Value = getProvisionalValue(RelEntry);
681
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000682 switch (RelEntry.Type) {
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000683 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
Sam Clegg9d24fb72017-06-16 23:59:10 +0000684 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
Sam Clegg60ec3032018-01-23 01:23:17 +0000685 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
686 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
Dan Gohmand934cb82017-02-24 23:18:00 +0000687 WritePatchableLEB(Stream, Value, Offset);
688 break;
Sam Clegg60ec3032018-01-23 01:23:17 +0000689 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
690 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000691 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
692 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
Dan Gohmand934cb82017-02-24 23:18:00 +0000693 WriteI32(Stream, Value, Offset);
694 break;
Sam Clegg60ec3032018-01-23 01:23:17 +0000695 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
696 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
697 WritePatchableSLEB(Stream, Value, Offset);
698 break;
Dan Gohmand934cb82017-02-24 23:18:00 +0000699 default:
Sam Clegg9d24fb72017-06-16 23:59:10 +0000700 llvm_unreachable("invalid relocation type");
Dan Gohmand934cb82017-02-24 23:18:00 +0000701 }
702 }
Dan Gohman18eafb62017-02-22 01:23:18 +0000703}
704
Sam Clegg9e15f352017-06-03 02:01:24 +0000705void WasmObjectWriter::writeTypeSection(
Sam Clegg457fb0b2017-09-15 19:50:44 +0000706 ArrayRef<WasmFunctionType> FunctionTypes) {
Sam Clegg9e15f352017-06-03 02:01:24 +0000707 if (FunctionTypes.empty())
708 return;
709
710 SectionBookkeeping Section;
711 startSection(Section, wasm::WASM_SEC_TYPE);
712
713 encodeULEB128(FunctionTypes.size(), getStream());
714
715 for (const WasmFunctionType &FuncTy : FunctionTypes) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000716 write8(wasm::WASM_TYPE_FUNC);
Sam Clegg9e15f352017-06-03 02:01:24 +0000717 encodeULEB128(FuncTy.Params.size(), getStream());
718 for (wasm::ValType Ty : FuncTy.Params)
719 writeValueType(Ty);
720 encodeULEB128(FuncTy.Returns.size(), getStream());
721 for (wasm::ValType Ty : FuncTy.Returns)
722 writeValueType(Ty);
723 }
724
725 endSection(Section);
726}
727
Sam Clegg8defa952018-02-12 22:41:29 +0000728void WasmObjectWriter::writeImportSection(ArrayRef<wasm::WasmImport> Imports,
Sam Cleggf950b242017-12-11 23:03:38 +0000729 uint32_t DataSize,
730 uint32_t NumElements) {
Sam Clegg9e15f352017-06-03 02:01:24 +0000731 if (Imports.empty())
732 return;
733
Sam Cleggf950b242017-12-11 23:03:38 +0000734 uint32_t NumPages = (DataSize + wasm::WasmPageSize - 1) / wasm::WasmPageSize;
735
Sam Clegg9e15f352017-06-03 02:01:24 +0000736 SectionBookkeeping Section;
737 startSection(Section, wasm::WASM_SEC_IMPORT);
738
739 encodeULEB128(Imports.size(), getStream());
Sam Clegg8defa952018-02-12 22:41:29 +0000740 for (const wasm::WasmImport &Import : Imports) {
741 writeString(Import.Module);
742 writeString(Import.Field);
Sam Clegg03e101f2018-03-01 18:06:21 +0000743 write8(Import.Kind);
Sam Clegg9e15f352017-06-03 02:01:24 +0000744
745 switch (Import.Kind) {
746 case wasm::WASM_EXTERNAL_FUNCTION:
Sam Clegg8defa952018-02-12 22:41:29 +0000747 encodeULEB128(Import.SigIndex, getStream());
Sam Clegg9e15f352017-06-03 02:01:24 +0000748 break;
749 case wasm::WASM_EXTERNAL_GLOBAL:
Sam Clegg03e101f2018-03-01 18:06:21 +0000750 write8(Import.Global.Type);
751 write8(Import.Global.Mutable ? 1 : 0);
Sam Clegg9e15f352017-06-03 02:01:24 +0000752 break;
Sam Cleggf950b242017-12-11 23:03:38 +0000753 case wasm::WASM_EXTERNAL_MEMORY:
754 encodeULEB128(0, getStream()); // flags
755 encodeULEB128(NumPages, getStream()); // initial
756 break;
757 case wasm::WASM_EXTERNAL_TABLE:
Sam Clegg03e101f2018-03-01 18:06:21 +0000758 write8(Import.Table.ElemType);
Sam Cleggf950b242017-12-11 23:03:38 +0000759 encodeULEB128(0, getStream()); // flags
760 encodeULEB128(NumElements, getStream()); // initial
761 break;
Sam Clegg9e15f352017-06-03 02:01:24 +0000762 default:
763 llvm_unreachable("unsupported import kind");
764 }
765 }
766
767 endSection(Section);
768}
769
Sam Clegg457fb0b2017-09-15 19:50:44 +0000770void WasmObjectWriter::writeFunctionSection(ArrayRef<WasmFunction> Functions) {
Sam Clegg9e15f352017-06-03 02:01:24 +0000771 if (Functions.empty())
772 return;
773
774 SectionBookkeeping Section;
775 startSection(Section, wasm::WASM_SEC_FUNCTION);
776
777 encodeULEB128(Functions.size(), getStream());
778 for (const WasmFunction &Func : Functions)
779 encodeULEB128(Func.Type, getStream());
780
781 endSection(Section);
782}
783
Sam Clegg7c395942017-09-14 23:07:53 +0000784void WasmObjectWriter::writeGlobalSection() {
Sam Clegg9e15f352017-06-03 02:01:24 +0000785 if (Globals.empty())
786 return;
787
788 SectionBookkeeping Section;
789 startSection(Section, wasm::WASM_SEC_GLOBAL);
790
791 encodeULEB128(Globals.size(), getStream());
792 for (const WasmGlobal &Global : Globals) {
Sam Clegg6e7f1822018-01-31 19:50:14 +0000793 writeValueType(static_cast<wasm::ValType>(Global.Type.Type));
794 write8(Global.Type.Mutable);
Sam Clegg9e15f352017-06-03 02:01:24 +0000795
Sam Clegg6e7f1822018-01-31 19:50:14 +0000796 write8(wasm::WASM_OPCODE_I32_CONST);
797 encodeSLEB128(Global.InitialValue, getStream());
Sam Clegg9e15f352017-06-03 02:01:24 +0000798 write8(wasm::WASM_OPCODE_END);
799 }
800
801 endSection(Section);
802}
803
Sam Clegg8defa952018-02-12 22:41:29 +0000804void WasmObjectWriter::writeExportSection(ArrayRef<wasm::WasmExport> Exports) {
Sam Clegg9e15f352017-06-03 02:01:24 +0000805 if (Exports.empty())
806 return;
807
808 SectionBookkeeping Section;
809 startSection(Section, wasm::WASM_SEC_EXPORT);
810
811 encodeULEB128(Exports.size(), getStream());
Sam Clegg8defa952018-02-12 22:41:29 +0000812 for (const wasm::WasmExport &Export : Exports) {
813 writeString(Export.Name);
Sam Clegg03e101f2018-03-01 18:06:21 +0000814 write8(Export.Kind);
Sam Clegg9e15f352017-06-03 02:01:24 +0000815 encodeULEB128(Export.Index, getStream());
816 }
817
818 endSection(Section);
819}
820
Sam Clegg457fb0b2017-09-15 19:50:44 +0000821void WasmObjectWriter::writeElemSection(ArrayRef<uint32_t> TableElems) {
Sam Clegg9e15f352017-06-03 02:01:24 +0000822 if (TableElems.empty())
823 return;
824
825 SectionBookkeeping Section;
826 startSection(Section, wasm::WASM_SEC_ELEM);
827
828 encodeULEB128(1, getStream()); // number of "segments"
829 encodeULEB128(0, getStream()); // the table index
830
831 // init expr for starting offset
832 write8(wasm::WASM_OPCODE_I32_CONST);
Sam Clegg30e1bbc2018-01-19 18:57:01 +0000833 encodeSLEB128(kInitialTableOffset, getStream());
Sam Clegg9e15f352017-06-03 02:01:24 +0000834 write8(wasm::WASM_OPCODE_END);
835
836 encodeULEB128(TableElems.size(), getStream());
837 for (uint32_t Elem : TableElems)
838 encodeULEB128(Elem, getStream());
839
840 endSection(Section);
841}
842
Sam Clegg457fb0b2017-09-15 19:50:44 +0000843void WasmObjectWriter::writeCodeSection(const MCAssembler &Asm,
844 const MCAsmLayout &Layout,
845 ArrayRef<WasmFunction> Functions) {
Sam Clegg9e15f352017-06-03 02:01:24 +0000846 if (Functions.empty())
847 return;
848
849 SectionBookkeeping Section;
850 startSection(Section, wasm::WASM_SEC_CODE);
Sam Clegg6f08c842018-04-24 18:11:36 +0000851 CodeSectionIndex = Section.Index;
Sam Clegg9e15f352017-06-03 02:01:24 +0000852
853 encodeULEB128(Functions.size(), getStream());
854
855 for (const WasmFunction &Func : Functions) {
Sam Cleggfe6414b2017-06-21 23:46:41 +0000856 auto &FuncSection = static_cast<MCSectionWasm &>(Func.Sym->getSection());
Sam Clegg9e15f352017-06-03 02:01:24 +0000857
Sam Clegg9e15f352017-06-03 02:01:24 +0000858 int64_t Size = 0;
859 if (!Func.Sym->getSize()->evaluateAsAbsolute(Size, Layout))
860 report_fatal_error(".size expression must be evaluatable");
861
862 encodeULEB128(Size, getStream());
Sam Cleggfe6414b2017-06-21 23:46:41 +0000863 FuncSection.setSectionOffset(getStream().tell() - Section.ContentsOffset);
Sam Clegg9e15f352017-06-03 02:01:24 +0000864 Asm.writeSectionData(&FuncSection, Layout);
865 }
866
Sam Clegg9e15f352017-06-03 02:01:24 +0000867 // Apply fixups.
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000868 applyRelocations(CodeRelocations, Section.ContentsOffset);
Sam Clegg9e15f352017-06-03 02:01:24 +0000869
870 endSection(Section);
871}
872
Sam Clegg6c899ba2018-02-23 05:08:34 +0000873void WasmObjectWriter::writeDataSection() {
874 if (DataSegments.empty())
Sam Clegg7c395942017-09-14 23:07:53 +0000875 return;
Sam Clegg9e15f352017-06-03 02:01:24 +0000876
877 SectionBookkeeping Section;
878 startSection(Section, wasm::WASM_SEC_DATA);
Sam Clegg6f08c842018-04-24 18:11:36 +0000879 DataSectionIndex = Section.Index;
Sam Clegg9e15f352017-06-03 02:01:24 +0000880
Sam Clegg6c899ba2018-02-23 05:08:34 +0000881 encodeULEB128(DataSegments.size(), getStream()); // count
Sam Clegg7c395942017-09-14 23:07:53 +0000882
Sam Clegg6c899ba2018-02-23 05:08:34 +0000883 for (const WasmDataSegment &Segment : DataSegments) {
Sam Clegg7c395942017-09-14 23:07:53 +0000884 encodeULEB128(0, getStream()); // memory index
885 write8(wasm::WASM_OPCODE_I32_CONST);
886 encodeSLEB128(Segment.Offset, getStream()); // offset
887 write8(wasm::WASM_OPCODE_END);
888 encodeULEB128(Segment.Data.size(), getStream()); // size
889 Segment.Section->setSectionOffset(getStream().tell() - Section.ContentsOffset);
890 writeBytes(Segment.Data); // data
891 }
Sam Clegg9e15f352017-06-03 02:01:24 +0000892
893 // Apply fixups.
Sam Clegg7c395942017-09-14 23:07:53 +0000894 applyRelocations(DataRelocations, Section.ContentsOffset);
Sam Clegg9e15f352017-06-03 02:01:24 +0000895
896 endSection(Section);
Sam Clegg9e15f352017-06-03 02:01:24 +0000897}
898
Sam Clegg6f08c842018-04-24 18:11:36 +0000899void WasmObjectWriter::writeRelocSection(
900 uint32_t SectionIndex, StringRef Name,
901 ArrayRef<WasmRelocationEntry> Relocations) {
Sam Clegg9e15f352017-06-03 02:01:24 +0000902 // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
903 // for descriptions of the reloc sections.
904
Sam Clegg6f08c842018-04-24 18:11:36 +0000905 if (Relocations.empty())
Sam Clegg9e15f352017-06-03 02:01:24 +0000906 return;
907
908 SectionBookkeeping Section;
Sam Clegg6f08c842018-04-24 18:11:36 +0000909 startCustomSection(Section, std::string("reloc.") + Name.str());
Sam Clegg9e15f352017-06-03 02:01:24 +0000910
Sam Clegg6f08c842018-04-24 18:11:36 +0000911 raw_pwrite_stream &Stream = getStream();
Sam Clegg9e15f352017-06-03 02:01:24 +0000912
Sam Clegg6f08c842018-04-24 18:11:36 +0000913 encodeULEB128(SectionIndex, Stream);
914 encodeULEB128(Relocations.size(), Stream);
915 for (const WasmRelocationEntry& RelEntry : Relocations) {
916 uint64_t Offset = RelEntry.Offset +
917 RelEntry.FixupSection->getSectionOffset();
918 uint32_t Index = getRelocationIndexValue(RelEntry);
Sam Clegg9e15f352017-06-03 02:01:24 +0000919
Sam Clegg6f08c842018-04-24 18:11:36 +0000920 write8(RelEntry.Type);
921 encodeULEB128(Offset, Stream);
922 encodeULEB128(Index, Stream);
923 if (RelEntry.hasAddend())
924 encodeSLEB128(RelEntry.Addend, Stream);
925 }
Sam Clegg9e15f352017-06-03 02:01:24 +0000926
927 endSection(Section);
928}
929
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000930void WasmObjectWriter::writeCustomRelocSections() {
931 for (const auto &Sec : CustomSections) {
932 auto &Relocations = CustomSectionsRelocations[Sec.Section];
933 writeRelocSection(Sec.OutputIndex, Sec.Name, Relocations);
934 }
935}
936
Sam Clegg9e15f352017-06-03 02:01:24 +0000937void WasmObjectWriter::writeLinkingMetaDataSection(
Sam Clegg86b4a092018-02-27 23:57:37 +0000938 ArrayRef<wasm::WasmSymbolInfo> SymbolInfos,
Sam Cleggea7cace2018-01-09 23:43:14 +0000939 ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
Sam Clegg6c899ba2018-02-23 05:08:34 +0000940 const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats) {
Sam Clegg9e15f352017-06-03 02:01:24 +0000941 SectionBookkeeping Section;
Sam Clegg2322a932018-04-23 19:16:19 +0000942 startCustomSection(Section, "linking");
Sam Clegg6bb5a412018-04-26 18:15:32 +0000943 encodeULEB128(wasm::WasmMetadataVersion, getStream());
Sam Clegg9e15f352017-06-03 02:01:24 +0000944
Sam Clegg6bb5a412018-04-26 18:15:32 +0000945 SectionBookkeeping SubSection;
Sam Clegg6c899ba2018-02-23 05:08:34 +0000946 if (SymbolInfos.size() != 0) {
947 startSection(SubSection, wasm::WASM_SYMBOL_TABLE);
948 encodeULEB128(SymbolInfos.size(), getStream());
949 for (const wasm::WasmSymbolInfo &Sym : SymbolInfos) {
950 encodeULEB128(Sym.Kind, getStream());
951 encodeULEB128(Sym.Flags, getStream());
952 switch (Sym.Kind) {
953 case wasm::WASM_SYMBOL_TYPE_FUNCTION:
954 case wasm::WASM_SYMBOL_TYPE_GLOBAL:
955 encodeULEB128(Sym.ElementIndex, getStream());
956 if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0)
957 writeString(Sym.Name);
958 break;
959 case wasm::WASM_SYMBOL_TYPE_DATA:
960 writeString(Sym.Name);
961 if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) {
962 encodeULEB128(Sym.DataRef.Segment, getStream());
963 encodeULEB128(Sym.DataRef.Offset, getStream());
964 encodeULEB128(Sym.DataRef.Size, getStream());
965 }
966 break;
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000967 case wasm::WASM_SYMBOL_TYPE_SECTION: {
968 const uint32_t SectionIndex =
969 CustomSections[Sym.ElementIndex].OutputIndex;
970 encodeULEB128(SectionIndex, getStream());
971 break;
972 }
Sam Clegg6c899ba2018-02-23 05:08:34 +0000973 default:
974 llvm_unreachable("unexpected kind");
975 }
Sam Cleggb7787fd2017-06-20 04:04:59 +0000976 }
977 endSection(SubSection);
978 }
Sam Clegg9e15f352017-06-03 02:01:24 +0000979
Sam Clegg6c899ba2018-02-23 05:08:34 +0000980 if (DataSegments.size()) {
Sam Clegg63ebb812017-09-29 16:50:08 +0000981 startSection(SubSection, wasm::WASM_SEGMENT_INFO);
Sam Clegg6c899ba2018-02-23 05:08:34 +0000982 encodeULEB128(DataSegments.size(), getStream());
983 for (const WasmDataSegment &Segment : DataSegments) {
Sam Cleggd95ed952017-09-20 19:03:35 +0000984 writeString(Segment.Name);
Sam Clegg63ebb812017-09-29 16:50:08 +0000985 encodeULEB128(Segment.Alignment, getStream());
986 encodeULEB128(Segment.Flags, getStream());
987 }
Sam Cleggd95ed952017-09-20 19:03:35 +0000988 endSection(SubSection);
989 }
990
Sam Cleggbafe6902017-12-15 00:17:10 +0000991 if (!InitFuncs.empty()) {
992 startSection(SubSection, wasm::WASM_INIT_FUNCS);
993 encodeULEB128(InitFuncs.size(), getStream());
994 for (auto &StartFunc : InitFuncs) {
995 encodeULEB128(StartFunc.first, getStream()); // priority
996 encodeULEB128(StartFunc.second, getStream()); // function index
997 }
998 endSection(SubSection);
999 }
1000
Sam Cleggea7cace2018-01-09 23:43:14 +00001001 if (Comdats.size()) {
1002 startSection(SubSection, wasm::WASM_COMDAT_INFO);
1003 encodeULEB128(Comdats.size(), getStream());
1004 for (const auto &C : Comdats) {
1005 writeString(C.first);
1006 encodeULEB128(0, getStream()); // flags for future use
1007 encodeULEB128(C.second.size(), getStream());
1008 for (const WasmComdatEntry &Entry : C.second) {
1009 encodeULEB128(Entry.Kind, getStream());
1010 encodeULEB128(Entry.Index, getStream());
1011 }
1012 }
1013 endSection(SubSection);
1014 }
1015
Sam Clegg9e15f352017-06-03 02:01:24 +00001016 endSection(Section);
1017}
1018
Sam Clegg6a31a0d2018-04-26 19:27:28 +00001019void WasmObjectWriter::writeCustomSections(const MCAssembler &Asm,
1020 const MCAsmLayout &Layout) {
1021 for (auto &CustomSection : CustomSections) {
Sam Cleggcfd44a22018-04-05 17:01:39 +00001022 SectionBookkeeping Section;
Sam Clegg6a31a0d2018-04-26 19:27:28 +00001023 auto *Sec = CustomSection.Section;
Sam Clegg2322a932018-04-23 19:16:19 +00001024 startCustomSection(Section, CustomSection.Name);
Sam Clegg6a31a0d2018-04-26 19:27:28 +00001025
1026 Sec->setSectionOffset(getStream().tell() - Section.ContentsOffset);
1027 Asm.writeSectionData(Sec, Layout);
1028
1029 CustomSection.OutputContentsOffset = Section.ContentsOffset;
1030 CustomSection.OutputIndex = Section.Index;
1031
Sam Cleggcfd44a22018-04-05 17:01:39 +00001032 endSection(Section);
1033 }
1034}
1035
Sam Clegg6a31a0d2018-04-26 19:27:28 +00001036void WasmObjectWriter::updateCustomSectionRelocations(
1037 const SmallVector<WasmFunction, 4> &Functions, const MCAsmLayout &Layout) {
1038 std::map<const MCSection *, const MCSymbolWasm *> SectionSymbols;
1039 for (const auto &P : CustomSectionSymbols)
1040 SectionSymbols[P.second] = P.first;
1041 std::map<const MCSection *, const MCSymbolWasm *> FuncSymbols;
1042 for (const auto &FuncInfo : Functions)
1043 FuncSymbols[&FuncInfo.Sym->getSection()] = FuncInfo.Sym;
1044
1045 // Patch relocation records for R_WEBASSEMBLY_FUNCTION_OFFSET_I32 and
1046 // R_WEBASSEMBLY_SECTION_OFFSET_I32. The Addend is stuffed the offset from
1047 // the beginning of the function or custom section -- all such relocations
1048 // target the function or custom section starts.
1049 for (auto &Section : CustomSections) {
1050 auto &Relocations = CustomSectionsRelocations[Section.Section];
1051 for (WasmRelocationEntry &RelEntry : Relocations) {
1052 switch (RelEntry.Type) {
1053 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32: {
1054 assert(RelEntry.hasAddend());
1055 auto &Section =
1056 static_cast<MCSectionWasm &>(RelEntry.Symbol->getSection());
1057 RelEntry.Addend += Layout.getSymbolOffset(*RelEntry.Symbol);
1058 RelEntry.Symbol = FuncSymbols[&Section];
1059 break;
1060 }
1061 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32: {
1062 assert(RelEntry.hasAddend());
1063 auto &Section =
1064 static_cast<MCSectionWasm &>(RelEntry.Symbol->getSection());
1065 RelEntry.Addend += Layout.getSymbolOffset(*RelEntry.Symbol);
1066 RelEntry.Symbol = SectionSymbols[&Section];
1067 break;
1068 }
1069 default:
1070 break;
1071 }
1072 }
1073
1074 // Apply fixups.
1075 applyRelocations(Relocations, Section.OutputContentsOffset);
1076 }
1077}
1078
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001079uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm& Symbol) {
1080 assert(Symbol.isFunction());
1081 assert(TypeIndices.count(&Symbol));
1082 return TypeIndices[&Symbol];
1083}
1084
1085uint32_t WasmObjectWriter::registerFunctionType(const MCSymbolWasm& Symbol) {
1086 assert(Symbol.isFunction());
1087
1088 WasmFunctionType F;
Sam Cleggaff1c4d2017-09-15 19:22:01 +00001089 const MCSymbolWasm* ResolvedSym = ResolveSymbol(Symbol);
1090 F.Returns = ResolvedSym->getReturns();
1091 F.Params = ResolvedSym->getParams();
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001092
1093 auto Pair =
1094 FunctionTypeIndices.insert(std::make_pair(F, FunctionTypes.size()));
1095 if (Pair.second)
1096 FunctionTypes.push_back(F);
1097 TypeIndices[&Symbol] = Pair.first->second;
1098
1099 DEBUG(dbgs() << "registerFunctionType: " << Symbol << " new:" << Pair.second << "\n");
1100 DEBUG(dbgs() << " -> type index: " << Pair.first->second << "\n");
1101 return Pair.first->second;
1102}
1103
Dan Gohman18eafb62017-02-22 01:23:18 +00001104void WasmObjectWriter::writeObject(MCAssembler &Asm,
1105 const MCAsmLayout &Layout) {
Sam Cleggb7787fd2017-06-20 04:04:59 +00001106 DEBUG(dbgs() << "WasmObjectWriter::writeObject\n");
Dan Gohman82607f52017-02-24 23:46:05 +00001107 MCContext &Ctx = Asm.getContext();
Dan Gohmand934cb82017-02-24 23:18:00 +00001108
1109 // Collect information from the available symbols.
Dan Gohmand934cb82017-02-24 23:18:00 +00001110 SmallVector<WasmFunction, 4> Functions;
1111 SmallVector<uint32_t, 4> TableElems;
Sam Clegg8defa952018-02-12 22:41:29 +00001112 SmallVector<wasm::WasmImport, 4> Imports;
1113 SmallVector<wasm::WasmExport, 4> Exports;
Sam Clegg6c899ba2018-02-23 05:08:34 +00001114 SmallVector<wasm::WasmSymbolInfo, 4> SymbolInfos;
Sam Cleggbafe6902017-12-15 00:17:10 +00001115 SmallVector<std::pair<uint16_t, uint32_t>, 2> InitFuncs;
Sam Cleggea7cace2018-01-09 23:43:14 +00001116 std::map<StringRef, std::vector<WasmComdatEntry>> Comdats;
Sam Clegg7c395942017-09-14 23:07:53 +00001117 uint32_t DataSize = 0;
Dan Gohmand934cb82017-02-24 23:18:00 +00001118
Sam Cleggf950b242017-12-11 23:03:38 +00001119 // For now, always emit the memory import, since loads and stores are not
1120 // valid without it. In the future, we could perhaps be more clever and omit
1121 // it if there are no loads or stores.
1122 MCSymbolWasm *MemorySym =
1123 cast<MCSymbolWasm>(Ctx.getOrCreateSymbol("__linear_memory"));
Sam Clegg8defa952018-02-12 22:41:29 +00001124 wasm::WasmImport MemImport;
1125 MemImport.Module = MemorySym->getModuleName();
1126 MemImport.Field = MemorySym->getName();
Sam Cleggf950b242017-12-11 23:03:38 +00001127 MemImport.Kind = wasm::WASM_EXTERNAL_MEMORY;
1128 Imports.push_back(MemImport);
1129
1130 // For now, always emit the table section, since indirect calls are not
1131 // valid without it. In the future, we could perhaps be more clever and omit
1132 // it if there are no indirect calls.
1133 MCSymbolWasm *TableSym =
1134 cast<MCSymbolWasm>(Ctx.getOrCreateSymbol("__indirect_function_table"));
Sam Clegg8defa952018-02-12 22:41:29 +00001135 wasm::WasmImport TableImport;
1136 TableImport.Module = TableSym->getModuleName();
1137 TableImport.Field = TableSym->getName();
Sam Cleggf950b242017-12-11 23:03:38 +00001138 TableImport.Kind = wasm::WASM_EXTERNAL_TABLE;
Sam Clegg8defa952018-02-12 22:41:29 +00001139 TableImport.Table.ElemType = wasm::WASM_TYPE_ANYFUNC;
Sam Cleggf950b242017-12-11 23:03:38 +00001140 Imports.push_back(TableImport);
1141
Nicholas Wilson586320c2018-02-28 17:19:48 +00001142 // Populate FunctionTypeIndices, and Imports and WasmIndices for undefined
1143 // symbols. This must be done before populating WasmIndices for defined
1144 // symbols.
Dan Gohman32ce5ca2017-12-05 18:29:48 +00001145 for (const MCSymbol &S : Asm.symbols()) {
1146 const auto &WS = static_cast<const MCSymbolWasm &>(S);
1147
1148 // Register types for all functions, including those with private linkage
Sam Clegg9f3fe422018-01-17 19:28:43 +00001149 // (because wasm always needs a type signature).
Dan Gohman32ce5ca2017-12-05 18:29:48 +00001150 if (WS.isFunction())
1151 registerFunctionType(WS);
1152
1153 if (WS.isTemporary())
1154 continue;
1155
1156 // If the symbol is not defined in this translation unit, import it.
Sam Clegg6c899ba2018-02-23 05:08:34 +00001157 if (!WS.isDefined() && !WS.isComdat()) {
Dan Gohman32ce5ca2017-12-05 18:29:48 +00001158 if (WS.isFunction()) {
Sam Clegg6c899ba2018-02-23 05:08:34 +00001159 wasm::WasmImport Import;
1160 Import.Module = WS.getModuleName();
1161 Import.Field = WS.getName();
Dan Gohman32ce5ca2017-12-05 18:29:48 +00001162 Import.Kind = wasm::WASM_EXTERNAL_FUNCTION;
Sam Clegg8defa952018-02-12 22:41:29 +00001163 Import.SigIndex = getFunctionType(WS);
Sam Clegg6c899ba2018-02-23 05:08:34 +00001164 Imports.push_back(Import);
1165 WasmIndices[&WS] = NumFunctionImports++;
1166 } else if (WS.isGlobal()) {
Nicholas Wilson15f349f2018-03-09 16:30:44 +00001167 if (WS.isWeak())
1168 report_fatal_error("undefined global symbol cannot be weak");
1169
Sam Clegg6c899ba2018-02-23 05:08:34 +00001170 wasm::WasmImport Import;
1171 Import.Module = WS.getModuleName();
1172 Import.Field = WS.getName();
Dan Gohman32ce5ca2017-12-05 18:29:48 +00001173 Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
Sam Clegg6c899ba2018-02-23 05:08:34 +00001174 Import.Global = WS.getGlobalType();
1175 Imports.push_back(Import);
1176 WasmIndices[&WS] = NumGlobalImports++;
Dan Gohman32ce5ca2017-12-05 18:29:48 +00001177 }
Dan Gohman32ce5ca2017-12-05 18:29:48 +00001178 }
1179 }
1180
Nicholas Wilson586320c2018-02-28 17:19:48 +00001181 // Populate DataSegments, which must be done before populating DataLocations.
Sam Clegg759631c2017-09-15 20:54:59 +00001182 for (MCSection &Sec : Asm) {
1183 auto &Section = static_cast<MCSectionWasm &>(Sec);
Sam Cleggcfd44a22018-04-05 17:01:39 +00001184
Sam Clegg6a31a0d2018-04-26 19:27:28 +00001185 if (Section.getSectionName().startswith(".custom_section.")) {
Sam Cleggcfd44a22018-04-05 17:01:39 +00001186 if (Section.getFragmentList().empty())
1187 continue;
1188 if (Section.getFragmentList().size() != 1)
1189 report_fatal_error(
1190 "only one .custom_section section fragment supported");
1191 const MCFragment &Frag = *Section.begin();
1192 if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
1193 report_fatal_error("only data supported in .custom_section section");
1194 const auto &DataFrag = cast<MCDataFragment>(Frag);
1195 if (!DataFrag.getFixups().empty())
1196 report_fatal_error("fixups not supported in .custom_section section");
1197 StringRef UserName = Section.getSectionName().substr(16);
Sam Clegg6a31a0d2018-04-26 19:27:28 +00001198 CustomSections.emplace_back(UserName, &Section);
Sam Cleggcfd44a22018-04-05 17:01:39 +00001199 continue;
1200 }
1201
Sam Clegg12fd3da2017-10-20 21:28:38 +00001202 if (!Section.isWasmData())
Sam Clegg759631c2017-09-15 20:54:59 +00001203 continue;
1204
Sam Cleggbafe6902017-12-15 00:17:10 +00001205 // .init_array sections are handled specially elsewhere.
1206 if (cast<MCSectionWasm>(Sec).getSectionName().startswith(".init_array"))
1207 continue;
1208
Sam Clegg329e76d2018-01-31 04:21:44 +00001209 uint32_t SegmentIndex = DataSegments.size();
Sam Clegg759631c2017-09-15 20:54:59 +00001210 DataSize = alignTo(DataSize, Section.getAlignment());
1211 DataSegments.emplace_back();
1212 WasmDataSegment &Segment = DataSegments.back();
Sam Cleggd95ed952017-09-20 19:03:35 +00001213 Segment.Name = Section.getSectionName();
Sam Clegg759631c2017-09-15 20:54:59 +00001214 Segment.Offset = DataSize;
1215 Segment.Section = &Section;
Sam Clegg63ebb812017-09-29 16:50:08 +00001216 addData(Segment.Data, Section);
1217 Segment.Alignment = Section.getAlignment();
1218 Segment.Flags = 0;
Sam Clegg759631c2017-09-15 20:54:59 +00001219 DataSize += Segment.Data.size();
Sam Clegg6c899ba2018-02-23 05:08:34 +00001220 Section.setSegmentIndex(SegmentIndex);
Sam Cleggea7cace2018-01-09 23:43:14 +00001221
1222 if (const MCSymbolWasm *C = Section.getGroup()) {
1223 Comdats[C->getName()].emplace_back(
Sam Clegg329e76d2018-01-31 04:21:44 +00001224 WasmComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex});
Sam Cleggea7cace2018-01-09 23:43:14 +00001225 }
Sam Clegg759631c2017-09-15 20:54:59 +00001226 }
1227
Sam Clegg6a31a0d2018-04-26 19:27:28 +00001228 // Create symbols for debug/custom sections.
1229 for (MCSection &Sec : Asm) {
1230 auto &DebugSection = static_cast<MCSectionWasm &>(Sec);
1231 StringRef SectionName = DebugSection.getSectionName();
1232
1233 // TODO: Add support for non-debug metadata sections?
1234 if (!Sec.getKind().isMetadata() || !SectionName.startswith(".debug_"))
1235 continue;
1236
1237 uint32_t ElementIndex = CustomSections.size();
1238 CustomSections.emplace_back(SectionName, &DebugSection);
1239
1240 MCSymbolWasm *SectionSym =
1241 cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SectionName));
1242 CustomSectionSymbols[SectionSym] = &DebugSection;
1243
1244 wasm::WasmSymbolInfo Info;
1245 Info.Name = SectionSym->getName();
1246 Info.Kind = wasm::WASM_SYMBOL_TYPE_SECTION;
Sam Cleggd5504a02018-04-27 00:17:21 +00001247 Info.Flags = wasm::WASM_SYMBOL_BINDING_LOCAL;
Sam Clegg6a31a0d2018-04-26 19:27:28 +00001248 Info.ElementIndex = ElementIndex;
1249 SymbolIndices[SectionSym] = SymbolInfos.size();
1250 SymbolInfos.emplace_back(Info);
1251 }
1252
Nicholas Wilson586320c2018-02-28 17:19:48 +00001253 // Populate WasmIndices and DataLocations for defined symbols.
Dan Gohmand934cb82017-02-24 23:18:00 +00001254 for (const MCSymbol &S : Asm.symbols()) {
1255 // Ignore unnamed temporary symbols, which aren't ever exported, imported,
1256 // or used in relocations.
1257 if (S.isTemporary() && S.getName().empty())
1258 continue;
Sam Cleggb7787fd2017-06-20 04:04:59 +00001259
Dan Gohmand934cb82017-02-24 23:18:00 +00001260 const auto &WS = static_cast<const MCSymbolWasm &>(S);
Sam Clegga165f2d2018-04-30 19:40:57 +00001261 DEBUG(dbgs() << "MCSymbol: " << toString(WS.getType())
1262 << " '" << S << "'"
Sam Clegg329e76d2018-01-31 04:21:44 +00001263 << " isDefined=" << S.isDefined()
1264 << " isExternal=" << S.isExternal()
1265 << " isTemporary=" << S.isTemporary()
Sam Cleggb7787fd2017-06-20 04:04:59 +00001266 << " isWeak=" << WS.isWeak()
Sam Clegga2b35da2017-12-03 01:19:23 +00001267 << " isHidden=" << WS.isHidden()
Sam Cleggb7787fd2017-06-20 04:04:59 +00001268 << " isVariable=" << WS.isVariable() << "\n");
1269
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001270 if (WS.isVariable())
1271 continue;
Sam Clegg6c899ba2018-02-23 05:08:34 +00001272 if (WS.isComdat() && !WS.isDefined())
1273 continue;
Sam Cleggb7787fd2017-06-20 04:04:59 +00001274
Dan Gohmand934cb82017-02-24 23:18:00 +00001275 if (WS.isFunction()) {
Sam Clegg6c899ba2018-02-23 05:08:34 +00001276 unsigned Index;
Sam Cleggcd65f692018-01-11 23:59:16 +00001277 if (WS.isDefined()) {
Sam Cleggb7787fd2017-06-20 04:04:59 +00001278 if (WS.getOffset() != 0)
1279 report_fatal_error(
1280 "function sections must contain one function each");
1281
1282 if (WS.getSize() == 0)
1283 report_fatal_error(
1284 "function symbols must have a size set with .size");
1285
Sam Clegg6c899ba2018-02-23 05:08:34 +00001286 // A definition. Write out the function body.
Sam Clegg9f3fe422018-01-17 19:28:43 +00001287 Index = NumFunctionImports + Functions.size();
Dan Gohmand934cb82017-02-24 23:18:00 +00001288 WasmFunction Func;
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001289 Func.Type = getFunctionType(WS);
Dan Gohmand934cb82017-02-24 23:18:00 +00001290 Func.Sym = &WS;
Sam Clegg6c899ba2018-02-23 05:08:34 +00001291 WasmIndices[&WS] = Index;
Dan Gohmand934cb82017-02-24 23:18:00 +00001292 Functions.push_back(Func);
Sam Clegg6c899ba2018-02-23 05:08:34 +00001293
1294 auto &Section = static_cast<MCSectionWasm &>(WS.getSection());
1295 if (const MCSymbolWasm *C = Section.getGroup()) {
1296 Comdats[C->getName()].emplace_back(
1297 WasmComdatEntry{wasm::WASM_COMDAT_FUNCTION, Index});
1298 }
Dan Gohmand934cb82017-02-24 23:18:00 +00001299 } else {
1300 // An import; the index was assigned above.
Sam Clegg6c899ba2018-02-23 05:08:34 +00001301 Index = WasmIndices.find(&WS)->second;
Dan Gohmand934cb82017-02-24 23:18:00 +00001302 }
1303
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001304 DEBUG(dbgs() << " -> function index: " << Index << "\n");
Sam Clegg6c899ba2018-02-23 05:08:34 +00001305 } else if (WS.isData()) {
Sam Cleggc38e9472017-06-02 01:05:24 +00001306 if (WS.isTemporary() && !WS.getSize())
1307 continue;
Dan Gohmand934cb82017-02-24 23:18:00 +00001308
Sam Clegg6c899ba2018-02-23 05:08:34 +00001309 if (!WS.isDefined()) {
Sam Clegga165f2d2018-04-30 19:40:57 +00001310 DEBUG(dbgs() << " -> segment index: -1" << "\n");
Sam Cleggfe6414b2017-06-21 23:46:41 +00001311 continue;
Sam Clegg6c899ba2018-02-23 05:08:34 +00001312 }
Sam Cleggc38e9472017-06-02 01:05:24 +00001313
Sam Cleggfe6414b2017-06-21 23:46:41 +00001314 if (!WS.getSize())
1315 report_fatal_error("data symbols must have a size set with .size: " +
1316 WS.getName());
Sam Cleggc38e9472017-06-02 01:05:24 +00001317
Sam Cleggfe6414b2017-06-21 23:46:41 +00001318 int64_t Size = 0;
1319 if (!WS.getSize()->evaluateAsAbsolute(Size, Layout))
1320 report_fatal_error(".size expression must be evaluatable");
Dan Gohmand934cb82017-02-24 23:18:00 +00001321
Sam Clegg759631c2017-09-15 20:54:59 +00001322 auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection());
Sam Cleggea7cace2018-01-09 23:43:14 +00001323 assert(DataSection.isWasmData());
Sam Clegg7c395942017-09-14 23:07:53 +00001324
Sam Clegg6c899ba2018-02-23 05:08:34 +00001325 // For each data symbol, export it in the symtab as a reference to the
1326 // corresponding Wasm data segment.
1327 wasm::WasmDataReference Ref = wasm::WasmDataReference{
1328 DataSection.getSegmentIndex(),
1329 static_cast<uint32_t>(Layout.getSymbolOffset(WS)),
1330 static_cast<uint32_t>(Size)};
1331 DataLocations[&WS] = Ref;
Sam Clegga165f2d2018-04-30 19:40:57 +00001332 DEBUG(dbgs() << " -> segment index: " << Ref.Segment << "\n");
1333 } else if (WS.isGlobal()) {
Sam Clegg6c899ba2018-02-23 05:08:34 +00001334 // A "true" Wasm global (currently just __stack_pointer)
Eric Christopher545932b2018-02-23 21:14:47 +00001335 if (WS.isDefined())
Sam Clegg6c899ba2018-02-23 05:08:34 +00001336 report_fatal_error("don't yet support defined globals");
Sam Clegg6c899ba2018-02-23 05:08:34 +00001337
Eric Christopher545932b2018-02-23 21:14:47 +00001338 // An import; the index was assigned above
1339 DEBUG(dbgs() << " -> global index: " << WasmIndices.find(&WS)->second
1340 << "\n");
Sam Clegga165f2d2018-04-30 19:40:57 +00001341 } else {
1342 assert(WS.isSection());
Dan Gohmand934cb82017-02-24 23:18:00 +00001343 }
1344 }
1345
Nicholas Wilson586320c2018-02-28 17:19:48 +00001346 // Populate WasmIndices and DataLocations for aliased symbols. We need to
1347 // process these in a separate pass because we need to have processed the
1348 // target of the alias before the alias itself and the symbols are not
1349 // necessarily ordered in this way.
Sam Cleggb7787fd2017-06-20 04:04:59 +00001350 for (const MCSymbol &S : Asm.symbols()) {
1351 if (!S.isVariable())
1352 continue;
Sam Clegg31a2c802017-09-20 21:17:04 +00001353
Sam Cleggcd65f692018-01-11 23:59:16 +00001354 assert(S.isDefined());
Sam Cleggb7787fd2017-06-20 04:04:59 +00001355
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001356 // Find the target symbol of this weak alias and export that index
Sam Cleggaff1c4d2017-09-15 19:22:01 +00001357 const auto &WS = static_cast<const MCSymbolWasm &>(S);
1358 const MCSymbolWasm *ResolvedSym = ResolveSymbol(WS);
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001359 DEBUG(dbgs() << WS.getName() << ": weak alias of '" << *ResolvedSym << "'\n");
Sam Cleggb7787fd2017-06-20 04:04:59 +00001360
Sam Clegg6c899ba2018-02-23 05:08:34 +00001361 if (WS.isFunction()) {
1362 assert(WasmIndices.count(ResolvedSym) > 0);
1363 uint32_t WasmIndex = WasmIndices.find(ResolvedSym)->second;
1364 WasmIndices[&WS] = WasmIndex;
1365 DEBUG(dbgs() << " -> index:" << WasmIndex << "\n");
1366 } else if (WS.isData()) {
1367 assert(DataLocations.count(ResolvedSym) > 0);
1368 const wasm::WasmDataReference &Ref =
1369 DataLocations.find(ResolvedSym)->second;
1370 DataLocations[&WS] = Ref;
1371 DEBUG(dbgs() << " -> index:" << Ref.Segment << "\n");
1372 } else {
1373 report_fatal_error("don't yet support global aliases");
1374 }
Nicholas Wilson586320c2018-02-28 17:19:48 +00001375 }
Sam Clegg31a2c802017-09-20 21:17:04 +00001376
Nicholas Wilson586320c2018-02-28 17:19:48 +00001377 // Finally, populate the symbol table itself, in its "natural" order.
1378 for (const MCSymbol &S : Asm.symbols()) {
1379 const auto &WS = static_cast<const MCSymbolWasm &>(S);
1380 if (WS.isTemporary() && WS.getName().empty())
1381 continue;
1382 if (WS.isComdat() && !WS.isDefined())
1383 continue;
1384 if (WS.isTemporary() && WS.isData() && !WS.getSize())
1385 continue;
1386
1387 uint32_t Flags = 0;
1388 if (WS.isWeak())
1389 Flags |= wasm::WASM_SYMBOL_BINDING_WEAK;
1390 if (WS.isHidden())
1391 Flags |= wasm::WASM_SYMBOL_VISIBILITY_HIDDEN;
1392 if (!WS.isExternal() && WS.isDefined())
1393 Flags |= wasm::WASM_SYMBOL_BINDING_LOCAL;
1394 if (WS.isUndefined())
1395 Flags |= wasm::WASM_SYMBOL_UNDEFINED;
1396
1397 wasm::WasmSymbolInfo Info;
1398 Info.Name = WS.getName();
1399 Info.Kind = WS.getType();
1400 Info.Flags = Flags;
1401 if (!WS.isData())
1402 Info.ElementIndex = WasmIndices.find(&WS)->second;
1403 else if (WS.isDefined())
1404 Info.DataRef = DataLocations.find(&WS)->second;
1405 SymbolIndices[&WS] = SymbolInfos.size();
1406 SymbolInfos.emplace_back(Info);
Sam Cleggb7787fd2017-06-20 04:04:59 +00001407 }
1408
Sam Clegg6006e092017-12-22 20:31:39 +00001409 {
1410 auto HandleReloc = [&](const WasmRelocationEntry &Rel) {
Sam Cleggf9edbe92018-01-31 19:28:47 +00001411 // Functions referenced by a relocation need to put in the table. This is
1412 // purely to make the object file's provisional values readable, and is
1413 // ignored by the linker, which re-calculates the relocations itself.
1414 if (Rel.Type != wasm::R_WEBASSEMBLY_TABLE_INDEX_I32 &&
1415 Rel.Type != wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB)
1416 return;
1417 assert(Rel.Symbol->isFunction());
1418 const MCSymbolWasm &WS = *ResolveSymbol(*Rel.Symbol);
Sam Clegg6c899ba2018-02-23 05:08:34 +00001419 uint32_t FunctionIndex = WasmIndices.find(&WS)->second;
Sam Cleggf9edbe92018-01-31 19:28:47 +00001420 uint32_t TableIndex = TableElems.size() + kInitialTableOffset;
1421 if (TableIndices.try_emplace(&WS, TableIndex).second) {
1422 DEBUG(dbgs() << " -> adding " << WS.getName()
1423 << " to table: " << TableIndex << "\n");
Sam Clegg6c899ba2018-02-23 05:08:34 +00001424 TableElems.push_back(FunctionIndex);
Sam Cleggf9edbe92018-01-31 19:28:47 +00001425 registerFunctionType(WS);
Sam Clegg6006e092017-12-22 20:31:39 +00001426 }
1427 };
Dan Gohman970d02c2017-03-30 23:58:19 +00001428
Sam Clegg6006e092017-12-22 20:31:39 +00001429 for (const WasmRelocationEntry &RelEntry : CodeRelocations)
1430 HandleReloc(RelEntry);
1431 for (const WasmRelocationEntry &RelEntry : DataRelocations)
1432 HandleReloc(RelEntry);
Dan Gohmand934cb82017-02-24 23:18:00 +00001433 }
1434
Sam Cleggbafe6902017-12-15 00:17:10 +00001435 // Translate .init_array section contents into start functions.
1436 for (const MCSection &S : Asm) {
1437 const auto &WS = static_cast<const MCSectionWasm &>(S);
1438 if (WS.getSectionName().startswith(".fini_array"))
1439 report_fatal_error(".fini_array sections are unsupported");
1440 if (!WS.getSectionName().startswith(".init_array"))
1441 continue;
1442 if (WS.getFragmentList().empty())
1443 continue;
1444 if (WS.getFragmentList().size() != 2)
1445 report_fatal_error("only one .init_array section fragment supported");
1446 const MCFragment &AlignFrag = *WS.begin();
1447 if (AlignFrag.getKind() != MCFragment::FT_Align)
1448 report_fatal_error(".init_array section should be aligned");
1449 if (cast<MCAlignFragment>(AlignFrag).getAlignment() != (is64Bit() ? 8 : 4))
1450 report_fatal_error(".init_array section should be aligned for pointers");
1451 const MCFragment &Frag = *std::next(WS.begin());
1452 if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
1453 report_fatal_error("only data supported in .init_array section");
1454 uint16_t Priority = UINT16_MAX;
1455 if (WS.getSectionName().size() != 11) {
1456 if (WS.getSectionName()[11] != '.')
1457 report_fatal_error(".init_array section priority should start with '.'");
1458 if (WS.getSectionName().substr(12).getAsInteger(10, Priority))
1459 report_fatal_error("invalid .init_array section priority");
1460 }
1461 const auto &DataFrag = cast<MCDataFragment>(Frag);
1462 const SmallVectorImpl<char> &Contents = DataFrag.getContents();
1463 for (const uint8_t *p = (const uint8_t *)Contents.data(),
1464 *end = (const uint8_t *)Contents.data() + Contents.size();
1465 p != end; ++p) {
1466 if (*p != 0)
1467 report_fatal_error("non-symbolic data in .init_array section");
1468 }
1469 for (const MCFixup &Fixup : DataFrag.getFixups()) {
1470 assert(Fixup.getKind() == MCFixup::getKindForSize(is64Bit() ? 8 : 4, false));
1471 const MCExpr *Expr = Fixup.getValue();
1472 auto *Sym = dyn_cast<MCSymbolRefExpr>(Expr);
1473 if (!Sym)
1474 report_fatal_error("fixups in .init_array should be symbol references");
1475 if (Sym->getKind() != MCSymbolRefExpr::VK_WebAssembly_FUNCTION)
1476 report_fatal_error("symbols in .init_array should be for functions");
1477 auto I = SymbolIndices.find(cast<MCSymbolWasm>(&Sym->getSymbol()));
1478 if (I == SymbolIndices.end())
1479 report_fatal_error("symbols in .init_array should be defined");
1480 uint32_t Index = I->second;
1481 InitFuncs.push_back(std::make_pair(Priority, Index));
1482 }
1483 }
1484
Dan Gohman18eafb62017-02-22 01:23:18 +00001485 // Write out the Wasm header.
1486 writeHeader(Asm);
1487
Sam Clegg9e15f352017-06-03 02:01:24 +00001488 writeTypeSection(FunctionTypes);
Sam Cleggf950b242017-12-11 23:03:38 +00001489 writeImportSection(Imports, DataSize, TableElems.size());
Sam Clegg9e15f352017-06-03 02:01:24 +00001490 writeFunctionSection(Functions);
Sam Cleggf950b242017-12-11 23:03:38 +00001491 // Skip the "table" section; we import the table instead.
1492 // Skip the "memory" section; we import the memory instead.
Sam Clegg7c395942017-09-14 23:07:53 +00001493 writeGlobalSection();
Sam Clegg9e15f352017-06-03 02:01:24 +00001494 writeExportSection(Exports);
Sam Clegg9e15f352017-06-03 02:01:24 +00001495 writeElemSection(TableElems);
Sam Cleggacd7d2b2017-06-06 19:15:05 +00001496 writeCodeSection(Asm, Layout, Functions);
Sam Clegg6c899ba2018-02-23 05:08:34 +00001497 writeDataSection();
Sam Clegg6a31a0d2018-04-26 19:27:28 +00001498 writeCustomSections(Asm, Layout);
1499 updateCustomSectionRelocations(Functions, Layout);
Nicholas Wilsonc22bfb62018-03-05 12:59:03 +00001500 writeLinkingMetaDataSection(SymbolInfos, InitFuncs, Comdats);
Sam Clegg6f08c842018-04-24 18:11:36 +00001501 writeRelocSection(CodeSectionIndex, "CODE", CodeRelocations);
1502 writeRelocSection(DataSectionIndex, "DATA", DataRelocations);
Sam Clegg6a31a0d2018-04-26 19:27:28 +00001503 writeCustomRelocSections();
Dan Gohman970d02c2017-03-30 23:58:19 +00001504
Dan Gohmand934cb82017-02-24 23:18:00 +00001505 // TODO: Translate the .comment section to the output.
Dan Gohman18eafb62017-02-22 01:23:18 +00001506}
1507
Lang Hames60fbc7c2017-10-10 16:28:07 +00001508std::unique_ptr<MCObjectWriter>
Lang Hames1301a872017-10-10 01:15:10 +00001509llvm::createWasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
1510 raw_pwrite_stream &OS) {
Dan Gohman0917c9e2018-01-15 17:06:23 +00001511 return llvm::make_unique<WasmObjectWriter>(std::move(MOTW), OS);
Dan Gohman18eafb62017-02-22 01:23:18 +00001512}