blob: 0696d6f495237875ab0543cbc272dd1152617085 [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
40namespace {
Sam Clegg9e15f352017-06-03 02:01:24 +000041
Sam Clegg30e1bbc2018-01-19 18:57:01 +000042// Went we ceate the indirect function table we start at 1, so that there is
43// and emtpy slot at 0 and therefore calling a null function pointer will trap.
44static const uint32_t kInitialTableOffset = 1;
45
Dan Gohmand934cb82017-02-24 23:18:00 +000046// For patching purposes, we need to remember where each section starts, both
47// for patching up the section size field, and for patching up references to
48// locations within the section.
49struct SectionBookkeeping {
50 // Where the size of the section is written.
51 uint64_t SizeOffset;
Sam Clegg6a31a0d2018-04-26 19:27:28 +000052 // Where the section header ends (without custom section name).
53 uint64_t PayloadOffset;
54 // Where the contents of the section starts.
Dan Gohmand934cb82017-02-24 23:18:00 +000055 uint64_t ContentsOffset;
Sam Clegg6f08c842018-04-24 18:11:36 +000056 uint32_t Index;
Dan Gohmand934cb82017-02-24 23:18:00 +000057};
58
Sam Clegg9e15f352017-06-03 02:01:24 +000059// The signature of a wasm function, in a struct capable of being used as a
60// DenseMap key.
61struct WasmFunctionType {
62 // Support empty and tombstone instances, needed by DenseMap.
63 enum { Plain, Empty, Tombstone } State;
64
65 // The return types of the function.
66 SmallVector<wasm::ValType, 1> Returns;
67
68 // The parameter types of the function.
69 SmallVector<wasm::ValType, 4> Params;
70
71 WasmFunctionType() : State(Plain) {}
72
73 bool operator==(const WasmFunctionType &Other) const {
74 return State == Other.State && Returns == Other.Returns &&
75 Params == Other.Params;
76 }
77};
78
79// Traits for using WasmFunctionType in a DenseMap.
80struct WasmFunctionTypeDenseMapInfo {
81 static WasmFunctionType getEmptyKey() {
82 WasmFunctionType FuncTy;
83 FuncTy.State = WasmFunctionType::Empty;
84 return FuncTy;
85 }
86 static WasmFunctionType getTombstoneKey() {
87 WasmFunctionType FuncTy;
88 FuncTy.State = WasmFunctionType::Tombstone;
89 return FuncTy;
90 }
91 static unsigned getHashValue(const WasmFunctionType &FuncTy) {
92 uintptr_t Value = FuncTy.State;
93 for (wasm::ValType Ret : FuncTy.Returns)
94 Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Ret));
95 for (wasm::ValType Param : FuncTy.Params)
96 Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Param));
97 return Value;
98 }
99 static bool isEqual(const WasmFunctionType &LHS,
100 const WasmFunctionType &RHS) {
101 return LHS == RHS;
102 }
103};
104
Sam Clegg7c395942017-09-14 23:07:53 +0000105// A wasm data segment. A wasm binary contains only a single data section
106// but that can contain many segments, each with their own virtual location
107// in memory. Each MCSection data created by llvm is modeled as its own
108// wasm data segment.
109struct WasmDataSegment {
110 MCSectionWasm *Section;
Sam Cleggd95ed952017-09-20 19:03:35 +0000111 StringRef Name;
Sam Clegg7c395942017-09-14 23:07:53 +0000112 uint32_t Offset;
Sam Clegg63ebb812017-09-29 16:50:08 +0000113 uint32_t Alignment;
114 uint32_t Flags;
Sam Clegg7c395942017-09-14 23:07:53 +0000115 SmallVector<char, 4> Data;
116};
117
Sam Clegg9e15f352017-06-03 02:01:24 +0000118// A wasm function to be written into the function section.
119struct WasmFunction {
120 int32_t Type;
121 const MCSymbolWasm *Sym;
122};
123
Sam Clegg9e15f352017-06-03 02:01:24 +0000124// A wasm global to be written into the global section.
125struct WasmGlobal {
Sam Clegg6e7f1822018-01-31 19:50:14 +0000126 wasm::WasmGlobalType Type;
Sam Clegg9e15f352017-06-03 02:01:24 +0000127 uint64_t InitialValue;
Sam Clegg9e15f352017-06-03 02:01:24 +0000128};
129
Sam Cleggea7cace2018-01-09 23:43:14 +0000130// Information about a single item which is part of a COMDAT. For each data
131// segment or function which is in the COMDAT, there is a corresponding
132// WasmComdatEntry.
133struct WasmComdatEntry {
134 unsigned Kind;
135 uint32_t Index;
136};
137
Sam Clegg6dc65e92017-06-06 16:38:59 +0000138// Information about a single relocation.
139struct WasmRelocationEntry {
Sam Cleggfe6414b2017-06-21 23:46:41 +0000140 uint64_t Offset; // Where is the relocation.
141 const MCSymbolWasm *Symbol; // The symbol to relocate with.
142 int64_t Addend; // A value to add to the symbol.
143 unsigned Type; // The type of the relocation.
144 const MCSectionWasm *FixupSection;// The section the relocation is targeting.
Sam Clegg6dc65e92017-06-06 16:38:59 +0000145
146 WasmRelocationEntry(uint64_t Offset, const MCSymbolWasm *Symbol,
147 int64_t Addend, unsigned Type,
Sam Cleggfe6414b2017-06-21 23:46:41 +0000148 const MCSectionWasm *FixupSection)
Sam Clegg6dc65e92017-06-06 16:38:59 +0000149 : Offset(Offset), Symbol(Symbol), Addend(Addend), Type(Type),
150 FixupSection(FixupSection) {}
151
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000152 bool hasAddend() const {
153 switch (Type) {
Sam Clegg13a2e892017-09-01 17:32:01 +0000154 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
155 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
156 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000157 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
158 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000159 return true;
160 default:
161 return false;
162 }
163 }
164
Sam Clegg6dc65e92017-06-06 16:38:59 +0000165 void print(raw_ostream &Out) const {
Sam Clegg5f87ab32018-05-14 22:42:07 +0000166 Out << wasm::relocTypetoString(Type)
Sam Clegga165f2d2018-04-30 19:40:57 +0000167 << " Off=" << Offset << ", Sym=" << *Symbol << ", Addend=" << Addend
Sam Clegg759631c2017-09-15 20:54:59 +0000168 << ", FixupSection=" << FixupSection->getSectionName();
Sam Clegg6dc65e92017-06-06 16:38:59 +0000169 }
Sam Cleggb7787fd2017-06-20 04:04:59 +0000170
Aaron Ballman615eb472017-10-15 14:32:27 +0000171#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sam Cleggb7787fd2017-06-20 04:04:59 +0000172 LLVM_DUMP_METHOD void dump() const { print(dbgs()); }
173#endif
Sam Clegg6dc65e92017-06-06 16:38:59 +0000174};
175
Sam Clegg25d8e682018-05-08 00:08:21 +0000176static const uint32_t INVALID_INDEX = -1;
177
Sam Cleggcfd44a22018-04-05 17:01:39 +0000178struct WasmCustomSection {
Sam Cleggcfd44a22018-04-05 17:01:39 +0000179
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000180 StringRef Name;
181 MCSectionWasm *Section;
182
183 uint32_t OutputContentsOffset;
184 uint32_t OutputIndex;
185
186 WasmCustomSection(StringRef Name, MCSectionWasm *Section)
187 : Name(Name), Section(Section), OutputContentsOffset(0),
188 OutputIndex(INVALID_INDEX) {}
Sam Cleggcfd44a22018-04-05 17:01:39 +0000189};
190
Sam Clegg1fb8daa2017-06-20 05:05:10 +0000191#if !defined(NDEBUG)
Sam Clegg7f055de2017-06-20 04:47:58 +0000192raw_ostream &operator<<(raw_ostream &OS, const WasmRelocationEntry &Rel) {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000193 Rel.print(OS);
194 return OS;
195}
Sam Clegg1fb8daa2017-06-20 05:05:10 +0000196#endif
Sam Cleggb7787fd2017-06-20 04:04:59 +0000197
Dan Gohman18eafb62017-02-22 01:23:18 +0000198class WasmObjectWriter : public MCObjectWriter {
Dan Gohman18eafb62017-02-22 01:23:18 +0000199 /// The target specific Wasm writer instance.
200 std::unique_ptr<MCWasmObjectTargetWriter> TargetObjectWriter;
201
Dan Gohmand934cb82017-02-24 23:18:00 +0000202 // Relocations for fixing up references in the code section.
203 std::vector<WasmRelocationEntry> CodeRelocations;
Sam Clegg6f08c842018-04-24 18:11:36 +0000204 uint32_t CodeSectionIndex;
Dan Gohmand934cb82017-02-24 23:18:00 +0000205
206 // Relocations for fixing up references in the data section.
207 std::vector<WasmRelocationEntry> DataRelocations;
Sam Clegg6f08c842018-04-24 18:11:36 +0000208 uint32_t DataSectionIndex;
Dan Gohmand934cb82017-02-24 23:18:00 +0000209
Dan Gohmand934cb82017-02-24 23:18:00 +0000210 // Index values to use for fixing up call_indirect type indices.
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000211 // Maps function symbols to the index of the type of the function
212 DenseMap<const MCSymbolWasm *, uint32_t> TypeIndices;
Sam Cleggd99f6072017-06-12 23:52:44 +0000213 // Maps function symbols to the table element index space. Used
214 // for TABLE_INDEX relocation types (i.e. address taken functions).
Sam Cleggf9edbe92018-01-31 19:28:47 +0000215 DenseMap<const MCSymbolWasm *, uint32_t> TableIndices;
Sam Clegga165f2d2018-04-30 19:40:57 +0000216 // Maps function/global symbols to the function/global/section index space.
Sam Clegg6c899ba2018-02-23 05:08:34 +0000217 DenseMap<const MCSymbolWasm *, uint32_t> WasmIndices;
218 // Maps data symbols to the Wasm segment and offset/size with the segment.
219 DenseMap<const MCSymbolWasm *, wasm::WasmDataReference> DataLocations;
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000220
221 // Stores output data (index, relocations, content offset) for custom
222 // section.
223 std::vector<WasmCustomSection> CustomSections;
224 // Relocations for fixing up references in the custom sections.
225 DenseMap<const MCSectionWasm *, std::vector<WasmRelocationEntry>>
226 CustomSectionsRelocations;
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000227
Sam Cleggc0d41192018-05-17 17:15:15 +0000228 // Map from section to defining function symbol.
Sam Clegg6ccb59b2018-05-16 20:09:05 +0000229 DenseMap<const MCSection *, const MCSymbol *> SectionFunctions;
230
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000231 DenseMap<WasmFunctionType, int32_t, WasmFunctionTypeDenseMapInfo>
232 FunctionTypeIndices;
Sam Clegg5e3d33a2017-07-07 02:01:29 +0000233 SmallVector<WasmFunctionType, 4> FunctionTypes;
Sam Clegg7c395942017-09-14 23:07:53 +0000234 SmallVector<WasmGlobal, 4> Globals;
Sam Clegg6c899ba2018-02-23 05:08:34 +0000235 SmallVector<WasmDataSegment, 4> DataSegments;
Sam Clegg9f3fe422018-01-17 19:28:43 +0000236 unsigned NumFunctionImports = 0;
Sam Clegg7c395942017-09-14 23:07:53 +0000237 unsigned NumGlobalImports = 0;
Chandler Carruth7e1c3342018-04-24 20:30:56 +0000238 uint32_t SectionCount = 0;
Dan Gohmand934cb82017-02-24 23:18:00 +0000239
Dan Gohman18eafb62017-02-22 01:23:18 +0000240 // TargetObjectWriter wrappers.
241 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
Sam Cleggae03c1e72017-06-13 18:51:50 +0000242 unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup) const {
243 return TargetObjectWriter->getRelocType(Target, Fixup);
Dan Gohman18eafb62017-02-22 01:23:18 +0000244 }
245
Sam Clegg2322a932018-04-23 19:16:19 +0000246 void startSection(SectionBookkeeping &Section, unsigned SectionId);
247 void startCustomSection(SectionBookkeeping &Section, StringRef Name);
Dan Gohmand934cb82017-02-24 23:18:00 +0000248 void endSection(SectionBookkeeping &Section);
249
Dan Gohman18eafb62017-02-22 01:23:18 +0000250public:
Lang Hames1301a872017-10-10 01:15:10 +0000251 WasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
252 raw_pwrite_stream &OS)
253 : MCObjectWriter(OS, /*IsLittleEndian=*/true),
254 TargetObjectWriter(std::move(MOTW)) {}
Dan Gohman18eafb62017-02-22 01:23:18 +0000255
Dan Gohman18eafb62017-02-22 01:23:18 +0000256 ~WasmObjectWriter() override;
257
Dan Gohman0917c9e2018-01-15 17:06:23 +0000258private:
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000259 void reset() override {
260 CodeRelocations.clear();
261 DataRelocations.clear();
262 TypeIndices.clear();
Sam Clegg6c899ba2018-02-23 05:08:34 +0000263 WasmIndices.clear();
Sam Cleggf9edbe92018-01-31 19:28:47 +0000264 TableIndices.clear();
Sam Clegg6c899ba2018-02-23 05:08:34 +0000265 DataLocations.clear();
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000266 CustomSectionsRelocations.clear();
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000267 FunctionTypeIndices.clear();
Sam Clegg5e3d33a2017-07-07 02:01:29 +0000268 FunctionTypes.clear();
Sam Clegg7c395942017-09-14 23:07:53 +0000269 Globals.clear();
Sam Clegg6c899ba2018-02-23 05:08:34 +0000270 DataSegments.clear();
Sam Clegg6ccb59b2018-05-16 20:09:05 +0000271 SectionFunctions.clear();
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000272 MCObjectWriter::reset();
Sam Clegg9f3fe422018-01-17 19:28:43 +0000273 NumFunctionImports = 0;
Sam Clegg7c395942017-09-14 23:07:53 +0000274 NumGlobalImports = 0;
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000275 }
276
Dan Gohman18eafb62017-02-22 01:23:18 +0000277 void writeHeader(const MCAssembler &Asm);
278
279 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
280 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindolaceecfe5b2017-07-11 23:56:10 +0000281 MCValue Target, uint64_t &FixedValue) override;
Dan Gohman18eafb62017-02-22 01:23:18 +0000282
283 void executePostLayoutBinding(MCAssembler &Asm,
284 const MCAsmLayout &Layout) override;
285
286 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Sam Clegg9e15f352017-06-03 02:01:24 +0000287
Sam Cleggb7787fd2017-06-20 04:04:59 +0000288 void writeString(const StringRef Str) {
289 encodeULEB128(Str.size(), getStream());
290 writeBytes(Str);
291 }
292
Sam Clegg9e15f352017-06-03 02:01:24 +0000293 void writeValueType(wasm::ValType Ty) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000294 write8(static_cast<uint8_t>(Ty));
Sam Clegg9e15f352017-06-03 02:01:24 +0000295 }
296
Sam Clegg457fb0b2017-09-15 19:50:44 +0000297 void writeTypeSection(ArrayRef<WasmFunctionType> FunctionTypes);
Sam Clegg8defa952018-02-12 22:41:29 +0000298 void writeImportSection(ArrayRef<wasm::WasmImport> Imports, uint32_t DataSize,
Sam Cleggf950b242017-12-11 23:03:38 +0000299 uint32_t NumElements);
Sam Clegg457fb0b2017-09-15 19:50:44 +0000300 void writeFunctionSection(ArrayRef<WasmFunction> Functions);
Sam Clegg7c395942017-09-14 23:07:53 +0000301 void writeGlobalSection();
Sam Clegg8defa952018-02-12 22:41:29 +0000302 void writeExportSection(ArrayRef<wasm::WasmExport> Exports);
Sam Clegg457fb0b2017-09-15 19:50:44 +0000303 void writeElemSection(ArrayRef<uint32_t> TableElems);
Sam Clegg9e15f352017-06-03 02:01:24 +0000304 void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
Sam Clegg457fb0b2017-09-15 19:50:44 +0000305 ArrayRef<WasmFunction> Functions);
Sam Clegg6c899ba2018-02-23 05:08:34 +0000306 void writeDataSection();
Sam Clegg6f08c842018-04-24 18:11:36 +0000307 void writeRelocSection(uint32_t SectionIndex, StringRef Name,
308 ArrayRef<WasmRelocationEntry> Relocations);
Sam Clegg31a2c802017-09-20 21:17:04 +0000309 void writeLinkingMetaDataSection(
Sam Clegg86b4a092018-02-27 23:57:37 +0000310 ArrayRef<wasm::WasmSymbolInfo> SymbolInfos,
Sam Cleggea7cace2018-01-09 23:43:14 +0000311 ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
Sam Clegg6c899ba2018-02-23 05:08:34 +0000312 const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats);
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000313 void writeCustomSections(const MCAssembler &Asm, const MCAsmLayout &Layout);
314 void writeCustomRelocSections();
315 void
316 updateCustomSectionRelocations(const SmallVector<WasmFunction, 4> &Functions,
317 const MCAsmLayout &Layout);
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000318
Sam Clegg7c395942017-09-14 23:07:53 +0000319 uint32_t getProvisionalValue(const WasmRelocationEntry &RelEntry);
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000320 void applyRelocations(ArrayRef<WasmRelocationEntry> Relocations,
321 uint64_t ContentsOffset);
322
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000323 uint32_t getRelocationIndexValue(const WasmRelocationEntry &RelEntry);
Sam Clegg6f08c842018-04-24 18:11:36 +0000324 uint32_t getFunctionType(const MCSymbolWasm &Symbol);
325 uint32_t registerFunctionType(const MCSymbolWasm &Symbol);
Dan Gohman18eafb62017-02-22 01:23:18 +0000326};
Sam Clegg9e15f352017-06-03 02:01:24 +0000327
Dan Gohman18eafb62017-02-22 01:23:18 +0000328} // end anonymous namespace
329
330WasmObjectWriter::~WasmObjectWriter() {}
331
Dan Gohmand934cb82017-02-24 23:18:00 +0000332// Write out a section header and a patchable section size field.
333void WasmObjectWriter::startSection(SectionBookkeeping &Section,
Sam Clegg2322a932018-04-23 19:16:19 +0000334 unsigned SectionId) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000335 LLVM_DEBUG(dbgs() << "startSection " << SectionId << "\n");
Sam Clegg03e101f2018-03-01 18:06:21 +0000336 write8(SectionId);
Dan Gohmand934cb82017-02-24 23:18:00 +0000337
338 Section.SizeOffset = getStream().tell();
339
340 // The section size. We don't know the size yet, so reserve enough space
341 // for any 32-bit value; we'll patch it later.
342 encodeULEB128(UINT32_MAX, getStream());
343
344 // The position where the section starts, for measuring its size.
345 Section.ContentsOffset = getStream().tell();
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000346 Section.PayloadOffset = getStream().tell();
Sam Clegg6f08c842018-04-24 18:11:36 +0000347 Section.Index = SectionCount++;
Sam Clegg2322a932018-04-23 19:16:19 +0000348}
Dan Gohmand934cb82017-02-24 23:18:00 +0000349
Sam Clegg2322a932018-04-23 19:16:19 +0000350void WasmObjectWriter::startCustomSection(SectionBookkeeping &Section,
351 StringRef Name) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000352 LLVM_DEBUG(dbgs() << "startCustomSection " << Name << "\n");
Sam Clegg2322a932018-04-23 19:16:19 +0000353 startSection(Section, wasm::WASM_SEC_CUSTOM);
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000354
355 // The position where the section header ends, for measuring its size.
356 Section.PayloadOffset = getStream().tell();
357
Dan Gohmand934cb82017-02-24 23:18:00 +0000358 // Custom sections in wasm also have a string identifier.
Sam Clegg2322a932018-04-23 19:16:19 +0000359 writeString(Name);
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000360
361 // The position where the custom section starts.
362 Section.ContentsOffset = getStream().tell();
Dan Gohmand934cb82017-02-24 23:18:00 +0000363}
364
365// Now that the section is complete and we know how big it is, patch up the
366// section size field at the start of the section.
367void WasmObjectWriter::endSection(SectionBookkeeping &Section) {
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000368 uint64_t Size = getStream().tell() - Section.PayloadOffset;
Dan Gohmand934cb82017-02-24 23:18:00 +0000369 if (uint32_t(Size) != Size)
370 report_fatal_error("section size does not fit in a uint32_t");
371
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000372 LLVM_DEBUG(dbgs() << "endSection size=" << Size << "\n");
Dan Gohmand934cb82017-02-24 23:18:00 +0000373
374 // Write the final section size to the payload_len field, which follows
375 // the section id byte.
376 uint8_t Buffer[16];
Sam Clegg66a99e42017-09-15 20:34:47 +0000377 unsigned SizeLen = encodeULEB128(Size, Buffer, 5);
Dan Gohmand934cb82017-02-24 23:18:00 +0000378 assert(SizeLen == 5);
379 getStream().pwrite((char *)Buffer, SizeLen, Section.SizeOffset);
380}
381
Dan Gohman18eafb62017-02-22 01:23:18 +0000382// Emit the Wasm header.
383void WasmObjectWriter::writeHeader(const MCAssembler &Asm) {
Dan Gohman7ea5adf2017-02-22 18:50:20 +0000384 writeBytes(StringRef(wasm::WasmMagic, sizeof(wasm::WasmMagic)));
385 writeLE32(wasm::WasmVersion);
Dan Gohman18eafb62017-02-22 01:23:18 +0000386}
387
388void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
389 const MCAsmLayout &Layout) {
Sam Clegg6ccb59b2018-05-16 20:09:05 +0000390 // Build a map of sections to the function that defines them, for use
391 // in recordRelocation.
392 for (const MCSymbol &S : Asm.symbols()) {
393 const auto &WS = static_cast<const MCSymbolWasm &>(S);
394 if (WS.isDefined() && WS.isFunction() && !WS.isVariable()) {
395 const auto &Sec = static_cast<const MCSectionWasm &>(S.getSection());
396 auto Pair = SectionFunctions.insert(std::make_pair(&Sec, &S));
397 if (!Pair.second)
398 report_fatal_error("section already has a defining function: " +
399 Sec.getSectionName());
400 }
401 }
Dan Gohman18eafb62017-02-22 01:23:18 +0000402}
403
404void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
405 const MCAsmLayout &Layout,
406 const MCFragment *Fragment,
407 const MCFixup &Fixup, MCValue Target,
Rafael Espindolaceecfe5b2017-07-11 23:56:10 +0000408 uint64_t &FixedValue) {
409 MCAsmBackend &Backend = Asm.getBackend();
410 bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
411 MCFixupKindInfo::FKF_IsPCRel;
Sam Cleggfe6414b2017-06-21 23:46:41 +0000412 const auto &FixupSection = cast<MCSectionWasm>(*Fragment->getParent());
Dan Gohmand934cb82017-02-24 23:18:00 +0000413 uint64_t C = Target.getConstant();
414 uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
415 MCContext &Ctx = Asm.getContext();
416
Sam Cleggbafe6902017-12-15 00:17:10 +0000417 // The .init_array isn't translated as data, so don't do relocations in it.
418 if (FixupSection.getSectionName().startswith(".init_array"))
419 return;
420
Dan Gohmand934cb82017-02-24 23:18:00 +0000421 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
422 assert(RefB->getKind() == MCSymbolRefExpr::VK_None &&
423 "Should not have constructed this");
424
425 // Let A, B and C being the components of Target and R be the location of
426 // the fixup. If the fixup is not pcrel, we want to compute (A - B + C).
427 // If it is pcrel, we want to compute (A - B + C - R).
428
429 // In general, Wasm has no relocations for -B. It can only represent (A + C)
430 // or (A + C - R). If B = R + K and the relocation is not pcrel, we can
431 // replace B to implement it: (A - R - K + C)
432 if (IsPCRel) {
433 Ctx.reportError(
434 Fixup.getLoc(),
435 "No relocation available to represent this relative expression");
436 return;
437 }
438
439 const auto &SymB = cast<MCSymbolWasm>(RefB->getSymbol());
440
441 if (SymB.isUndefined()) {
442 Ctx.reportError(Fixup.getLoc(),
443 Twine("symbol '") + SymB.getName() +
444 "' can not be undefined in a subtraction expression");
445 return;
446 }
447
448 assert(!SymB.isAbsolute() && "Should have been folded");
449 const MCSection &SecB = SymB.getSection();
450 if (&SecB != &FixupSection) {
451 Ctx.reportError(Fixup.getLoc(),
452 "Cannot represent a difference across sections");
453 return;
454 }
455
456 uint64_t SymBOffset = Layout.getSymbolOffset(SymB);
457 uint64_t K = SymBOffset - FixupOffset;
458 IsPCRel = true;
459 C -= K;
460 }
461
462 // We either rejected the fixup or folded B into C at this point.
463 const MCSymbolRefExpr *RefA = Target.getSymA();
464 const auto *SymA = RefA ? cast<MCSymbolWasm>(&RefA->getSymbol()) : nullptr;
465
Dan Gohmand934cb82017-02-24 23:18:00 +0000466 if (SymA && SymA->isVariable()) {
467 const MCExpr *Expr = SymA->getVariableValue();
Sam Clegg6ad8f192017-07-11 02:21:57 +0000468 const auto *Inner = cast<MCSymbolRefExpr>(Expr);
469 if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
470 llvm_unreachable("weakref used in reloc not yet implemented");
Dan Gohmand934cb82017-02-24 23:18:00 +0000471 }
472
473 // Put any constant offset in an addend. Offsets can be negative, and
474 // LLVM expects wrapping, in contrast to wasm's immediates which can't
475 // be negative and don't wrap.
476 FixedValue = 0;
477
Sam Clegg4d57fbd2018-05-02 23:11:38 +0000478 unsigned Type = getRelocType(Target, Fixup);
Sam Cleggae03c1e72017-06-13 18:51:50 +0000479 assert(!IsPCRel);
Sam Clegg9d24fb72017-06-16 23:59:10 +0000480 assert(SymA);
481
Sam Clegg4d57fbd2018-05-02 23:11:38 +0000482 // Absolute offset within a section or a function.
483 // Currently only supported for for metadata sections.
484 // See: test/MC/WebAssembly/blockaddress.ll
485 if (Type == wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32 ||
486 Type == wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32) {
487 if (!FixupSection.getKind().isMetadata())
488 report_fatal_error("relocations for function or section offsets are "
489 "only supported in metadata sections");
490
491 const MCSymbol *SectionSymbol = nullptr;
492 const MCSection &SecA = SymA->getSection();
493 if (SecA.getKind().isText())
Sam Clegg6ccb59b2018-05-16 20:09:05 +0000494 SectionSymbol = SectionFunctions.find(&SecA)->second;
Sam Clegg4d57fbd2018-05-02 23:11:38 +0000495 else
496 SectionSymbol = SecA.getBeginSymbol();
497 if (!SectionSymbol)
498 report_fatal_error("section symbol is required for relocation");
499
500 C += Layout.getSymbolOffset(*SymA);
501 SymA = cast<MCSymbolWasm>(SectionSymbol);
502 }
503
504 // Relocation other than R_WEBASSEMBLY_TYPE_INDEX_LEB are required to be
505 // against a named symbol.
506 if (Type != wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB) {
507 if (SymA->getName().empty())
508 report_fatal_error("relocations against un-named temporaries are not yet "
509 "supported by wasm");
510
511 SymA->setUsedInReloc();
512 }
Sam Cleggae03c1e72017-06-13 18:51:50 +0000513
Dan Gohmand934cb82017-02-24 23:18:00 +0000514 WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000515 LLVM_DEBUG(dbgs() << "WasmReloc: " << Rec << "\n");
Dan Gohmand934cb82017-02-24 23:18:00 +0000516
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000517 if (FixupSection.isWasmData()) {
Dan Gohmand934cb82017-02-24 23:18:00 +0000518 DataRelocations.push_back(Rec);
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000519 } else if (FixupSection.getKind().isText()) {
Sam Clegg12fd3da2017-10-20 21:28:38 +0000520 CodeRelocations.push_back(Rec);
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000521 } else if (FixupSection.getKind().isMetadata()) {
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000522 CustomSectionsRelocations[&FixupSection].push_back(Rec);
523 } else {
Sam Clegg12fd3da2017-10-20 21:28:38 +0000524 llvm_unreachable("unexpected section type");
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000525 }
Dan Gohmand934cb82017-02-24 23:18:00 +0000526}
527
Dan Gohmand934cb82017-02-24 23:18:00 +0000528// Write X as an (unsigned) LEB value at offset Offset in Stream, padded
529// to allow patching.
530static void
531WritePatchableLEB(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
532 uint8_t Buffer[5];
Sam Clegg66a99e42017-09-15 20:34:47 +0000533 unsigned SizeLen = encodeULEB128(X, Buffer, 5);
Dan Gohmand934cb82017-02-24 23:18:00 +0000534 assert(SizeLen == 5);
535 Stream.pwrite((char *)Buffer, SizeLen, Offset);
536}
537
538// Write X as an signed LEB value at offset Offset in Stream, padded
539// to allow patching.
540static void
541WritePatchableSLEB(raw_pwrite_stream &Stream, int32_t X, uint64_t Offset) {
542 uint8_t Buffer[5];
Sam Clegg66a99e42017-09-15 20:34:47 +0000543 unsigned SizeLen = encodeSLEB128(X, Buffer, 5);
Dan Gohmand934cb82017-02-24 23:18:00 +0000544 assert(SizeLen == 5);
545 Stream.pwrite((char *)Buffer, SizeLen, Offset);
546}
547
548// Write X as a plain integer value at offset Offset in Stream.
549static void WriteI32(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
550 uint8_t Buffer[4];
551 support::endian::write32le(Buffer, X);
552 Stream.pwrite((char *)Buffer, sizeof(Buffer), Offset);
553}
554
Sam Cleggaff1c4d2017-09-15 19:22:01 +0000555static const MCSymbolWasm* ResolveSymbol(const MCSymbolWasm& Symbol) {
556 if (Symbol.isVariable()) {
557 const MCExpr *Expr = Symbol.getVariableValue();
558 auto *Inner = cast<MCSymbolRefExpr>(Expr);
559 return cast<MCSymbolWasm>(&Inner->getSymbol());
560 }
561 return &Symbol;
562}
563
Dan Gohmand934cb82017-02-24 23:18:00 +0000564// Compute a value to write into the code at the location covered
Sam Clegg60ec3032018-01-23 01:23:17 +0000565// by RelEntry. This value isn't used by the static linker; it just serves
566// to make the object format more readable and more likely to be directly
567// useable.
Sam Clegg7c395942017-09-14 23:07:53 +0000568uint32_t
569WasmObjectWriter::getProvisionalValue(const WasmRelocationEntry &RelEntry) {
Sam Clegg60ec3032018-01-23 01:23:17 +0000570 switch (RelEntry.Type) {
571 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
Sam Cleggf9edbe92018-01-31 19:28:47 +0000572 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32: {
573 // Provisional value is table address of the resolved symbol itself
574 const MCSymbolWasm *Sym = ResolveSymbol(*RelEntry.Symbol);
575 assert(Sym->isFunction());
576 return TableIndices[Sym];
577 }
Sam Clegg60ec3032018-01-23 01:23:17 +0000578 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
Sam Clegg6c899ba2018-02-23 05:08:34 +0000579 // Provisional value is same as the index
Sam Clegg60ec3032018-01-23 01:23:17 +0000580 return getRelocationIndexValue(RelEntry);
Sam Clegg6c899ba2018-02-23 05:08:34 +0000581 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
582 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
583 // Provisional value is function/global Wasm index
584 if (!WasmIndices.count(RelEntry.Symbol))
585 report_fatal_error("symbol not found in wasm index space: " +
586 RelEntry.Symbol->getName());
587 return WasmIndices[RelEntry.Symbol];
Sam Clegg4d57fbd2018-05-02 23:11:38 +0000588 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
589 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32: {
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000590 const auto &Section =
591 static_cast<const MCSectionWasm &>(RelEntry.Symbol->getSection());
592 return Section.getSectionOffset() + RelEntry.Addend;
593 }
Sam Clegg60ec3032018-01-23 01:23:17 +0000594 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
595 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
596 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: {
Sam Cleggf9edbe92018-01-31 19:28:47 +0000597 // Provisional value is address of the global
Sam Clegg60ec3032018-01-23 01:23:17 +0000598 const MCSymbolWasm *Sym = ResolveSymbol(*RelEntry.Symbol);
599 // For undefined symbols, use zero
600 if (!Sym->isDefined())
601 return 0;
Sam Clegg6c899ba2018-02-23 05:08:34 +0000602 const wasm::WasmDataReference &Ref = DataLocations[Sym];
603 const WasmDataSegment &Segment = DataSegments[Ref.Segment];
Sam Clegg60ec3032018-01-23 01:23:17 +0000604 // Ignore overflow. LLVM allows address arithmetic to silently wrap.
Sam Clegg6c899ba2018-02-23 05:08:34 +0000605 return Segment.Offset + Ref.Offset + RelEntry.Addend;
Sam Clegg60ec3032018-01-23 01:23:17 +0000606 }
607 default:
608 llvm_unreachable("invalid relocation type");
609 }
Dan Gohmand934cb82017-02-24 23:18:00 +0000610}
611
Sam Clegg759631c2017-09-15 20:54:59 +0000612static void addData(SmallVectorImpl<char> &DataBytes,
Sam Clegg63ebb812017-09-29 16:50:08 +0000613 MCSectionWasm &DataSection) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000614 LLVM_DEBUG(errs() << "addData: " << DataSection.getSectionName() << "\n");
Sam Clegg759631c2017-09-15 20:54:59 +0000615
Sam Clegg63ebb812017-09-29 16:50:08 +0000616 DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment()));
617
Sam Clegg759631c2017-09-15 20:54:59 +0000618 for (const MCFragment &Frag : DataSection) {
619 if (Frag.hasInstructions())
620 report_fatal_error("only data supported in data sections");
621
622 if (auto *Align = dyn_cast<MCAlignFragment>(&Frag)) {
623 if (Align->getValueSize() != 1)
624 report_fatal_error("only byte values supported for alignment");
625 // If nops are requested, use zeros, as this is the data section.
626 uint8_t Value = Align->hasEmitNops() ? 0 : Align->getValue();
627 uint64_t Size = std::min<uint64_t>(alignTo(DataBytes.size(),
628 Align->getAlignment()),
629 DataBytes.size() +
630 Align->getMaxBytesToEmit());
631 DataBytes.resize(Size, Value);
632 } else if (auto *Fill = dyn_cast<MCFillFragment>(&Frag)) {
Rafael Espindolad707c372018-01-09 22:48:37 +0000633 int64_t Size;
634 if (!Fill->getSize().evaluateAsAbsolute(Size))
635 llvm_unreachable("The fill should be an assembler constant");
636 DataBytes.insert(DataBytes.end(), Size, Fill->getValue());
Sam Clegg759631c2017-09-15 20:54:59 +0000637 } else {
638 const auto &DataFrag = cast<MCDataFragment>(Frag);
639 const SmallVectorImpl<char> &Contents = DataFrag.getContents();
640
641 DataBytes.insert(DataBytes.end(), Contents.begin(), Contents.end());
642 }
643 }
644
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000645 LLVM_DEBUG(dbgs() << "addData -> " << DataBytes.size() << "\n");
Sam Clegg759631c2017-09-15 20:54:59 +0000646}
647
Sam Clegg60ec3032018-01-23 01:23:17 +0000648uint32_t
649WasmObjectWriter::getRelocationIndexValue(const WasmRelocationEntry &RelEntry) {
650 if (RelEntry.Type == wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB) {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000651 if (!TypeIndices.count(RelEntry.Symbol))
Sam Clegg5e3d33a2017-07-07 02:01:29 +0000652 report_fatal_error("symbol not found in type index space: " +
Sam Cleggb7787fd2017-06-20 04:04:59 +0000653 RelEntry.Symbol->getName());
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000654 return TypeIndices[RelEntry.Symbol];
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000655 }
Sam Clegg60ec3032018-01-23 01:23:17 +0000656
Sam Clegg25d8e682018-05-08 00:08:21 +0000657 return RelEntry.Symbol->getIndex();
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000658}
659
Dan Gohmand934cb82017-02-24 23:18:00 +0000660// Apply the portions of the relocation records that we can handle ourselves
661// directly.
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000662void WasmObjectWriter::applyRelocations(
663 ArrayRef<WasmRelocationEntry> Relocations, uint64_t ContentsOffset) {
664 raw_pwrite_stream &Stream = getStream();
Dan Gohmand934cb82017-02-24 23:18:00 +0000665 for (const WasmRelocationEntry &RelEntry : Relocations) {
666 uint64_t Offset = ContentsOffset +
667 RelEntry.FixupSection->getSectionOffset() +
668 RelEntry.Offset;
Dan Gohmand934cb82017-02-24 23:18:00 +0000669
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000670 LLVM_DEBUG(dbgs() << "applyRelocation: " << RelEntry << "\n");
Sam Clegg60ec3032018-01-23 01:23:17 +0000671 uint32_t Value = getProvisionalValue(RelEntry);
672
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000673 switch (RelEntry.Type) {
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000674 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
Sam Clegg9d24fb72017-06-16 23:59:10 +0000675 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
Sam Clegg60ec3032018-01-23 01:23:17 +0000676 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
677 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
Dan Gohmand934cb82017-02-24 23:18:00 +0000678 WritePatchableLEB(Stream, Value, Offset);
679 break;
Sam Clegg60ec3032018-01-23 01:23:17 +0000680 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
681 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000682 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
683 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
Dan Gohmand934cb82017-02-24 23:18:00 +0000684 WriteI32(Stream, Value, Offset);
685 break;
Sam Clegg60ec3032018-01-23 01:23:17 +0000686 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
687 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
688 WritePatchableSLEB(Stream, Value, Offset);
689 break;
Dan Gohmand934cb82017-02-24 23:18:00 +0000690 default:
Sam Clegg9d24fb72017-06-16 23:59:10 +0000691 llvm_unreachable("invalid relocation type");
Dan Gohmand934cb82017-02-24 23:18:00 +0000692 }
693 }
Dan Gohman18eafb62017-02-22 01:23:18 +0000694}
695
Sam Clegg9e15f352017-06-03 02:01:24 +0000696void WasmObjectWriter::writeTypeSection(
Sam Clegg457fb0b2017-09-15 19:50:44 +0000697 ArrayRef<WasmFunctionType> FunctionTypes) {
Sam Clegg9e15f352017-06-03 02:01:24 +0000698 if (FunctionTypes.empty())
699 return;
700
701 SectionBookkeeping Section;
702 startSection(Section, wasm::WASM_SEC_TYPE);
703
704 encodeULEB128(FunctionTypes.size(), getStream());
705
706 for (const WasmFunctionType &FuncTy : FunctionTypes) {
Sam Clegg03e101f2018-03-01 18:06:21 +0000707 write8(wasm::WASM_TYPE_FUNC);
Sam Clegg9e15f352017-06-03 02:01:24 +0000708 encodeULEB128(FuncTy.Params.size(), getStream());
709 for (wasm::ValType Ty : FuncTy.Params)
710 writeValueType(Ty);
711 encodeULEB128(FuncTy.Returns.size(), getStream());
712 for (wasm::ValType Ty : FuncTy.Returns)
713 writeValueType(Ty);
714 }
715
716 endSection(Section);
717}
718
Sam Clegg8defa952018-02-12 22:41:29 +0000719void WasmObjectWriter::writeImportSection(ArrayRef<wasm::WasmImport> Imports,
Sam Cleggf950b242017-12-11 23:03:38 +0000720 uint32_t DataSize,
721 uint32_t NumElements) {
Sam Clegg9e15f352017-06-03 02:01:24 +0000722 if (Imports.empty())
723 return;
724
Sam Cleggf950b242017-12-11 23:03:38 +0000725 uint32_t NumPages = (DataSize + wasm::WasmPageSize - 1) / wasm::WasmPageSize;
726
Sam Clegg9e15f352017-06-03 02:01:24 +0000727 SectionBookkeeping Section;
728 startSection(Section, wasm::WASM_SEC_IMPORT);
729
730 encodeULEB128(Imports.size(), getStream());
Sam Clegg8defa952018-02-12 22:41:29 +0000731 for (const wasm::WasmImport &Import : Imports) {
732 writeString(Import.Module);
733 writeString(Import.Field);
Sam Clegg03e101f2018-03-01 18:06:21 +0000734 write8(Import.Kind);
Sam Clegg9e15f352017-06-03 02:01:24 +0000735
736 switch (Import.Kind) {
737 case wasm::WASM_EXTERNAL_FUNCTION:
Sam Clegg8defa952018-02-12 22:41:29 +0000738 encodeULEB128(Import.SigIndex, getStream());
Sam Clegg9e15f352017-06-03 02:01:24 +0000739 break;
740 case wasm::WASM_EXTERNAL_GLOBAL:
Sam Clegg03e101f2018-03-01 18:06:21 +0000741 write8(Import.Global.Type);
742 write8(Import.Global.Mutable ? 1 : 0);
Sam Clegg9e15f352017-06-03 02:01:24 +0000743 break;
Sam Cleggf950b242017-12-11 23:03:38 +0000744 case wasm::WASM_EXTERNAL_MEMORY:
745 encodeULEB128(0, getStream()); // flags
746 encodeULEB128(NumPages, getStream()); // initial
747 break;
748 case wasm::WASM_EXTERNAL_TABLE:
Sam Clegg03e101f2018-03-01 18:06:21 +0000749 write8(Import.Table.ElemType);
Sam Cleggf950b242017-12-11 23:03:38 +0000750 encodeULEB128(0, getStream()); // flags
751 encodeULEB128(NumElements, getStream()); // initial
752 break;
Sam Clegg9e15f352017-06-03 02:01:24 +0000753 default:
754 llvm_unreachable("unsupported import kind");
755 }
756 }
757
758 endSection(Section);
759}
760
Sam Clegg457fb0b2017-09-15 19:50:44 +0000761void WasmObjectWriter::writeFunctionSection(ArrayRef<WasmFunction> Functions) {
Sam Clegg9e15f352017-06-03 02:01:24 +0000762 if (Functions.empty())
763 return;
764
765 SectionBookkeeping Section;
766 startSection(Section, wasm::WASM_SEC_FUNCTION);
767
768 encodeULEB128(Functions.size(), getStream());
769 for (const WasmFunction &Func : Functions)
770 encodeULEB128(Func.Type, getStream());
771
772 endSection(Section);
773}
774
Sam Clegg7c395942017-09-14 23:07:53 +0000775void WasmObjectWriter::writeGlobalSection() {
Sam Clegg9e15f352017-06-03 02:01:24 +0000776 if (Globals.empty())
777 return;
778
779 SectionBookkeeping Section;
780 startSection(Section, wasm::WASM_SEC_GLOBAL);
781
782 encodeULEB128(Globals.size(), getStream());
783 for (const WasmGlobal &Global : Globals) {
Sam Clegg6e7f1822018-01-31 19:50:14 +0000784 writeValueType(static_cast<wasm::ValType>(Global.Type.Type));
785 write8(Global.Type.Mutable);
Sam Clegg9e15f352017-06-03 02:01:24 +0000786
Sam Clegg6e7f1822018-01-31 19:50:14 +0000787 write8(wasm::WASM_OPCODE_I32_CONST);
788 encodeSLEB128(Global.InitialValue, getStream());
Sam Clegg9e15f352017-06-03 02:01:24 +0000789 write8(wasm::WASM_OPCODE_END);
790 }
791
792 endSection(Section);
793}
794
Sam Clegg8defa952018-02-12 22:41:29 +0000795void WasmObjectWriter::writeExportSection(ArrayRef<wasm::WasmExport> Exports) {
Sam Clegg9e15f352017-06-03 02:01:24 +0000796 if (Exports.empty())
797 return;
798
799 SectionBookkeeping Section;
800 startSection(Section, wasm::WASM_SEC_EXPORT);
801
802 encodeULEB128(Exports.size(), getStream());
Sam Clegg8defa952018-02-12 22:41:29 +0000803 for (const wasm::WasmExport &Export : Exports) {
804 writeString(Export.Name);
Sam Clegg03e101f2018-03-01 18:06:21 +0000805 write8(Export.Kind);
Sam Clegg9e15f352017-06-03 02:01:24 +0000806 encodeULEB128(Export.Index, getStream());
807 }
808
809 endSection(Section);
810}
811
Sam Clegg457fb0b2017-09-15 19:50:44 +0000812void WasmObjectWriter::writeElemSection(ArrayRef<uint32_t> TableElems) {
Sam Clegg9e15f352017-06-03 02:01:24 +0000813 if (TableElems.empty())
814 return;
815
816 SectionBookkeeping Section;
817 startSection(Section, wasm::WASM_SEC_ELEM);
818
819 encodeULEB128(1, getStream()); // number of "segments"
820 encodeULEB128(0, getStream()); // the table index
821
822 // init expr for starting offset
823 write8(wasm::WASM_OPCODE_I32_CONST);
Sam Clegg30e1bbc2018-01-19 18:57:01 +0000824 encodeSLEB128(kInitialTableOffset, getStream());
Sam Clegg9e15f352017-06-03 02:01:24 +0000825 write8(wasm::WASM_OPCODE_END);
826
827 encodeULEB128(TableElems.size(), getStream());
828 for (uint32_t Elem : TableElems)
829 encodeULEB128(Elem, getStream());
830
831 endSection(Section);
832}
833
Sam Clegg457fb0b2017-09-15 19:50:44 +0000834void WasmObjectWriter::writeCodeSection(const MCAssembler &Asm,
835 const MCAsmLayout &Layout,
836 ArrayRef<WasmFunction> Functions) {
Sam Clegg9e15f352017-06-03 02:01:24 +0000837 if (Functions.empty())
838 return;
839
840 SectionBookkeeping Section;
841 startSection(Section, wasm::WASM_SEC_CODE);
Sam Clegg6f08c842018-04-24 18:11:36 +0000842 CodeSectionIndex = Section.Index;
Sam Clegg9e15f352017-06-03 02:01:24 +0000843
844 encodeULEB128(Functions.size(), getStream());
845
846 for (const WasmFunction &Func : Functions) {
Sam Cleggfe6414b2017-06-21 23:46:41 +0000847 auto &FuncSection = static_cast<MCSectionWasm &>(Func.Sym->getSection());
Sam Clegg9e15f352017-06-03 02:01:24 +0000848
Sam Clegg9e15f352017-06-03 02:01:24 +0000849 int64_t Size = 0;
850 if (!Func.Sym->getSize()->evaluateAsAbsolute(Size, Layout))
851 report_fatal_error(".size expression must be evaluatable");
852
853 encodeULEB128(Size, getStream());
Sam Cleggfe6414b2017-06-21 23:46:41 +0000854 FuncSection.setSectionOffset(getStream().tell() - Section.ContentsOffset);
Sam Clegg9e15f352017-06-03 02:01:24 +0000855 Asm.writeSectionData(&FuncSection, Layout);
856 }
857
Sam Clegg9e15f352017-06-03 02:01:24 +0000858 // Apply fixups.
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000859 applyRelocations(CodeRelocations, Section.ContentsOffset);
Sam Clegg9e15f352017-06-03 02:01:24 +0000860
861 endSection(Section);
862}
863
Sam Clegg6c899ba2018-02-23 05:08:34 +0000864void WasmObjectWriter::writeDataSection() {
865 if (DataSegments.empty())
Sam Clegg7c395942017-09-14 23:07:53 +0000866 return;
Sam Clegg9e15f352017-06-03 02:01:24 +0000867
868 SectionBookkeeping Section;
869 startSection(Section, wasm::WASM_SEC_DATA);
Sam Clegg6f08c842018-04-24 18:11:36 +0000870 DataSectionIndex = Section.Index;
Sam Clegg9e15f352017-06-03 02:01:24 +0000871
Sam Clegg6c899ba2018-02-23 05:08:34 +0000872 encodeULEB128(DataSegments.size(), getStream()); // count
Sam Clegg7c395942017-09-14 23:07:53 +0000873
Sam Clegg6c899ba2018-02-23 05:08:34 +0000874 for (const WasmDataSegment &Segment : DataSegments) {
Sam Clegg7c395942017-09-14 23:07:53 +0000875 encodeULEB128(0, getStream()); // memory index
876 write8(wasm::WASM_OPCODE_I32_CONST);
877 encodeSLEB128(Segment.Offset, getStream()); // offset
878 write8(wasm::WASM_OPCODE_END);
879 encodeULEB128(Segment.Data.size(), getStream()); // size
880 Segment.Section->setSectionOffset(getStream().tell() - Section.ContentsOffset);
881 writeBytes(Segment.Data); // data
882 }
Sam Clegg9e15f352017-06-03 02:01:24 +0000883
884 // Apply fixups.
Sam Clegg7c395942017-09-14 23:07:53 +0000885 applyRelocations(DataRelocations, Section.ContentsOffset);
Sam Clegg9e15f352017-06-03 02:01:24 +0000886
887 endSection(Section);
Sam Clegg9e15f352017-06-03 02:01:24 +0000888}
889
Sam Clegg6f08c842018-04-24 18:11:36 +0000890void WasmObjectWriter::writeRelocSection(
891 uint32_t SectionIndex, StringRef Name,
892 ArrayRef<WasmRelocationEntry> Relocations) {
Sam Clegg9e15f352017-06-03 02:01:24 +0000893 // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
894 // for descriptions of the reloc sections.
895
Sam Clegg6f08c842018-04-24 18:11:36 +0000896 if (Relocations.empty())
Sam Clegg9e15f352017-06-03 02:01:24 +0000897 return;
898
899 SectionBookkeeping Section;
Sam Clegg6f08c842018-04-24 18:11:36 +0000900 startCustomSection(Section, std::string("reloc.") + Name.str());
Sam Clegg9e15f352017-06-03 02:01:24 +0000901
Sam Clegg6f08c842018-04-24 18:11:36 +0000902 raw_pwrite_stream &Stream = getStream();
Sam Clegg9e15f352017-06-03 02:01:24 +0000903
Sam Clegg6f08c842018-04-24 18:11:36 +0000904 encodeULEB128(SectionIndex, Stream);
905 encodeULEB128(Relocations.size(), Stream);
906 for (const WasmRelocationEntry& RelEntry : Relocations) {
907 uint64_t Offset = RelEntry.Offset +
908 RelEntry.FixupSection->getSectionOffset();
909 uint32_t Index = getRelocationIndexValue(RelEntry);
Sam Clegg9e15f352017-06-03 02:01:24 +0000910
Sam Clegg6f08c842018-04-24 18:11:36 +0000911 write8(RelEntry.Type);
912 encodeULEB128(Offset, Stream);
913 encodeULEB128(Index, Stream);
914 if (RelEntry.hasAddend())
915 encodeSLEB128(RelEntry.Addend, Stream);
916 }
Sam Clegg9e15f352017-06-03 02:01:24 +0000917
918 endSection(Section);
919}
920
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000921void WasmObjectWriter::writeCustomRelocSections() {
922 for (const auto &Sec : CustomSections) {
923 auto &Relocations = CustomSectionsRelocations[Sec.Section];
924 writeRelocSection(Sec.OutputIndex, Sec.Name, Relocations);
925 }
926}
927
Sam Clegg9e15f352017-06-03 02:01:24 +0000928void WasmObjectWriter::writeLinkingMetaDataSection(
Sam Clegg86b4a092018-02-27 23:57:37 +0000929 ArrayRef<wasm::WasmSymbolInfo> SymbolInfos,
Sam Cleggea7cace2018-01-09 23:43:14 +0000930 ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
Sam Clegg6c899ba2018-02-23 05:08:34 +0000931 const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats) {
Sam Clegg9e15f352017-06-03 02:01:24 +0000932 SectionBookkeeping Section;
Sam Clegg2322a932018-04-23 19:16:19 +0000933 startCustomSection(Section, "linking");
Sam Clegg6bb5a412018-04-26 18:15:32 +0000934 encodeULEB128(wasm::WasmMetadataVersion, getStream());
Sam Clegg9e15f352017-06-03 02:01:24 +0000935
Sam Clegg6bb5a412018-04-26 18:15:32 +0000936 SectionBookkeeping SubSection;
Sam Clegg6c899ba2018-02-23 05:08:34 +0000937 if (SymbolInfos.size() != 0) {
938 startSection(SubSection, wasm::WASM_SYMBOL_TABLE);
939 encodeULEB128(SymbolInfos.size(), getStream());
940 for (const wasm::WasmSymbolInfo &Sym : SymbolInfos) {
941 encodeULEB128(Sym.Kind, getStream());
942 encodeULEB128(Sym.Flags, getStream());
943 switch (Sym.Kind) {
944 case wasm::WASM_SYMBOL_TYPE_FUNCTION:
945 case wasm::WASM_SYMBOL_TYPE_GLOBAL:
946 encodeULEB128(Sym.ElementIndex, getStream());
947 if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0)
948 writeString(Sym.Name);
949 break;
950 case wasm::WASM_SYMBOL_TYPE_DATA:
951 writeString(Sym.Name);
952 if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) {
953 encodeULEB128(Sym.DataRef.Segment, getStream());
954 encodeULEB128(Sym.DataRef.Offset, getStream());
955 encodeULEB128(Sym.DataRef.Size, getStream());
956 }
957 break;
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000958 case wasm::WASM_SYMBOL_TYPE_SECTION: {
959 const uint32_t SectionIndex =
960 CustomSections[Sym.ElementIndex].OutputIndex;
961 encodeULEB128(SectionIndex, getStream());
962 break;
963 }
Sam Clegg6c899ba2018-02-23 05:08:34 +0000964 default:
965 llvm_unreachable("unexpected kind");
966 }
Sam Cleggb7787fd2017-06-20 04:04:59 +0000967 }
968 endSection(SubSection);
969 }
Sam Clegg9e15f352017-06-03 02:01:24 +0000970
Sam Clegg6c899ba2018-02-23 05:08:34 +0000971 if (DataSegments.size()) {
Sam Clegg63ebb812017-09-29 16:50:08 +0000972 startSection(SubSection, wasm::WASM_SEGMENT_INFO);
Sam Clegg6c899ba2018-02-23 05:08:34 +0000973 encodeULEB128(DataSegments.size(), getStream());
974 for (const WasmDataSegment &Segment : DataSegments) {
Sam Cleggd95ed952017-09-20 19:03:35 +0000975 writeString(Segment.Name);
Sam Clegg63ebb812017-09-29 16:50:08 +0000976 encodeULEB128(Segment.Alignment, getStream());
977 encodeULEB128(Segment.Flags, getStream());
978 }
Sam Cleggd95ed952017-09-20 19:03:35 +0000979 endSection(SubSection);
980 }
981
Sam Cleggbafe6902017-12-15 00:17:10 +0000982 if (!InitFuncs.empty()) {
983 startSection(SubSection, wasm::WASM_INIT_FUNCS);
984 encodeULEB128(InitFuncs.size(), getStream());
985 for (auto &StartFunc : InitFuncs) {
986 encodeULEB128(StartFunc.first, getStream()); // priority
987 encodeULEB128(StartFunc.second, getStream()); // function index
988 }
989 endSection(SubSection);
990 }
991
Sam Cleggea7cace2018-01-09 23:43:14 +0000992 if (Comdats.size()) {
993 startSection(SubSection, wasm::WASM_COMDAT_INFO);
994 encodeULEB128(Comdats.size(), getStream());
995 for (const auto &C : Comdats) {
996 writeString(C.first);
997 encodeULEB128(0, getStream()); // flags for future use
998 encodeULEB128(C.second.size(), getStream());
999 for (const WasmComdatEntry &Entry : C.second) {
1000 encodeULEB128(Entry.Kind, getStream());
1001 encodeULEB128(Entry.Index, getStream());
1002 }
1003 }
1004 endSection(SubSection);
1005 }
1006
Sam Clegg9e15f352017-06-03 02:01:24 +00001007 endSection(Section);
1008}
1009
Sam Clegg6a31a0d2018-04-26 19:27:28 +00001010void WasmObjectWriter::writeCustomSections(const MCAssembler &Asm,
1011 const MCAsmLayout &Layout) {
1012 for (auto &CustomSection : CustomSections) {
Sam Cleggcfd44a22018-04-05 17:01:39 +00001013 SectionBookkeeping Section;
Sam Clegg6a31a0d2018-04-26 19:27:28 +00001014 auto *Sec = CustomSection.Section;
Sam Clegg2322a932018-04-23 19:16:19 +00001015 startCustomSection(Section, CustomSection.Name);
Sam Clegg6a31a0d2018-04-26 19:27:28 +00001016
1017 Sec->setSectionOffset(getStream().tell() - Section.ContentsOffset);
1018 Asm.writeSectionData(Sec, Layout);
1019
1020 CustomSection.OutputContentsOffset = Section.ContentsOffset;
1021 CustomSection.OutputIndex = Section.Index;
1022
Sam Cleggcfd44a22018-04-05 17:01:39 +00001023 endSection(Section);
Sam Clegg6a31a0d2018-04-26 19:27:28 +00001024
1025 // Apply fixups.
Sam Clegg4d57fbd2018-05-02 23:11:38 +00001026 auto &Relocations = CustomSectionsRelocations[CustomSection.Section];
1027 applyRelocations(Relocations, CustomSection.OutputContentsOffset);
Sam Clegg6a31a0d2018-04-26 19:27:28 +00001028 }
1029}
1030
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001031uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm& Symbol) {
1032 assert(Symbol.isFunction());
1033 assert(TypeIndices.count(&Symbol));
1034 return TypeIndices[&Symbol];
1035}
1036
1037uint32_t WasmObjectWriter::registerFunctionType(const MCSymbolWasm& Symbol) {
1038 assert(Symbol.isFunction());
1039
1040 WasmFunctionType F;
Sam Cleggaff1c4d2017-09-15 19:22:01 +00001041 const MCSymbolWasm* ResolvedSym = ResolveSymbol(Symbol);
1042 F.Returns = ResolvedSym->getReturns();
1043 F.Params = ResolvedSym->getParams();
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001044
1045 auto Pair =
1046 FunctionTypeIndices.insert(std::make_pair(F, FunctionTypes.size()));
1047 if (Pair.second)
1048 FunctionTypes.push_back(F);
1049 TypeIndices[&Symbol] = Pair.first->second;
1050
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001051 LLVM_DEBUG(dbgs() << "registerFunctionType: " << Symbol
1052 << " new:" << Pair.second << "\n");
1053 LLVM_DEBUG(dbgs() << " -> type index: " << Pair.first->second << "\n");
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001054 return Pair.first->second;
1055}
1056
Sam Clegg4d57fbd2018-05-02 23:11:38 +00001057static bool isInSymtab(const MCSymbolWasm &Sym) {
1058 if (Sym.isUsedInReloc())
1059 return true;
1060
1061 if (Sym.isComdat() && !Sym.isDefined())
1062 return false;
1063
1064 if (Sym.isTemporary() && Sym.getName().empty())
1065 return false;
1066
1067 if (Sym.isTemporary() && Sym.isData() && !Sym.getSize())
1068 return false;
1069
1070 if (Sym.isSection())
1071 return false;
1072
1073 return true;
1074}
1075
Dan Gohman18eafb62017-02-22 01:23:18 +00001076void WasmObjectWriter::writeObject(MCAssembler &Asm,
1077 const MCAsmLayout &Layout) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001078 LLVM_DEBUG(dbgs() << "WasmObjectWriter::writeObject\n");
Dan Gohman82607f52017-02-24 23:46:05 +00001079 MCContext &Ctx = Asm.getContext();
Dan Gohmand934cb82017-02-24 23:18:00 +00001080
1081 // Collect information from the available symbols.
Dan Gohmand934cb82017-02-24 23:18:00 +00001082 SmallVector<WasmFunction, 4> Functions;
1083 SmallVector<uint32_t, 4> TableElems;
Sam Clegg8defa952018-02-12 22:41:29 +00001084 SmallVector<wasm::WasmImport, 4> Imports;
1085 SmallVector<wasm::WasmExport, 4> Exports;
Sam Clegg6c899ba2018-02-23 05:08:34 +00001086 SmallVector<wasm::WasmSymbolInfo, 4> SymbolInfos;
Sam Cleggbafe6902017-12-15 00:17:10 +00001087 SmallVector<std::pair<uint16_t, uint32_t>, 2> InitFuncs;
Sam Cleggea7cace2018-01-09 23:43:14 +00001088 std::map<StringRef, std::vector<WasmComdatEntry>> Comdats;
Sam Clegg7c395942017-09-14 23:07:53 +00001089 uint32_t DataSize = 0;
Dan Gohmand934cb82017-02-24 23:18:00 +00001090
Sam Cleggf950b242017-12-11 23:03:38 +00001091 // For now, always emit the memory import, since loads and stores are not
1092 // valid without it. In the future, we could perhaps be more clever and omit
1093 // it if there are no loads or stores.
1094 MCSymbolWasm *MemorySym =
1095 cast<MCSymbolWasm>(Ctx.getOrCreateSymbol("__linear_memory"));
Sam Clegg8defa952018-02-12 22:41:29 +00001096 wasm::WasmImport MemImport;
1097 MemImport.Module = MemorySym->getModuleName();
1098 MemImport.Field = MemorySym->getName();
Sam Cleggf950b242017-12-11 23:03:38 +00001099 MemImport.Kind = wasm::WASM_EXTERNAL_MEMORY;
1100 Imports.push_back(MemImport);
1101
1102 // For now, always emit the table section, since indirect calls are not
1103 // valid without it. In the future, we could perhaps be more clever and omit
1104 // it if there are no indirect calls.
1105 MCSymbolWasm *TableSym =
1106 cast<MCSymbolWasm>(Ctx.getOrCreateSymbol("__indirect_function_table"));
Sam Clegg8defa952018-02-12 22:41:29 +00001107 wasm::WasmImport TableImport;
1108 TableImport.Module = TableSym->getModuleName();
1109 TableImport.Field = TableSym->getName();
Sam Cleggf950b242017-12-11 23:03:38 +00001110 TableImport.Kind = wasm::WASM_EXTERNAL_TABLE;
Sam Clegg8defa952018-02-12 22:41:29 +00001111 TableImport.Table.ElemType = wasm::WASM_TYPE_ANYFUNC;
Sam Cleggf950b242017-12-11 23:03:38 +00001112 Imports.push_back(TableImport);
1113
Nicholas Wilson586320c2018-02-28 17:19:48 +00001114 // Populate FunctionTypeIndices, and Imports and WasmIndices for undefined
1115 // symbols. This must be done before populating WasmIndices for defined
1116 // symbols.
Dan Gohman32ce5ca2017-12-05 18:29:48 +00001117 for (const MCSymbol &S : Asm.symbols()) {
1118 const auto &WS = static_cast<const MCSymbolWasm &>(S);
1119
1120 // Register types for all functions, including those with private linkage
Sam Clegg9f3fe422018-01-17 19:28:43 +00001121 // (because wasm always needs a type signature).
Dan Gohman32ce5ca2017-12-05 18:29:48 +00001122 if (WS.isFunction())
1123 registerFunctionType(WS);
1124
1125 if (WS.isTemporary())
1126 continue;
1127
1128 // If the symbol is not defined in this translation unit, import it.
Sam Clegg6c899ba2018-02-23 05:08:34 +00001129 if (!WS.isDefined() && !WS.isComdat()) {
Dan Gohman32ce5ca2017-12-05 18:29:48 +00001130 if (WS.isFunction()) {
Sam Clegg6c899ba2018-02-23 05:08:34 +00001131 wasm::WasmImport Import;
1132 Import.Module = WS.getModuleName();
1133 Import.Field = WS.getName();
Dan Gohman32ce5ca2017-12-05 18:29:48 +00001134 Import.Kind = wasm::WASM_EXTERNAL_FUNCTION;
Sam Clegg8defa952018-02-12 22:41:29 +00001135 Import.SigIndex = getFunctionType(WS);
Sam Clegg6c899ba2018-02-23 05:08:34 +00001136 Imports.push_back(Import);
1137 WasmIndices[&WS] = NumFunctionImports++;
1138 } else if (WS.isGlobal()) {
Nicholas Wilson15f349f2018-03-09 16:30:44 +00001139 if (WS.isWeak())
1140 report_fatal_error("undefined global symbol cannot be weak");
1141
Sam Clegg6c899ba2018-02-23 05:08:34 +00001142 wasm::WasmImport Import;
1143 Import.Module = WS.getModuleName();
1144 Import.Field = WS.getName();
Dan Gohman32ce5ca2017-12-05 18:29:48 +00001145 Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
Sam Clegg6c899ba2018-02-23 05:08:34 +00001146 Import.Global = WS.getGlobalType();
1147 Imports.push_back(Import);
1148 WasmIndices[&WS] = NumGlobalImports++;
Dan Gohman32ce5ca2017-12-05 18:29:48 +00001149 }
Dan Gohman32ce5ca2017-12-05 18:29:48 +00001150 }
1151 }
1152
Sam Clegg4d57fbd2018-05-02 23:11:38 +00001153 // Populate DataSegments and CustomSections, which must be done before
1154 // populating DataLocations.
Sam Clegg759631c2017-09-15 20:54:59 +00001155 for (MCSection &Sec : Asm) {
1156 auto &Section = static_cast<MCSectionWasm &>(Sec);
Sam Clegg4d57fbd2018-05-02 23:11:38 +00001157 StringRef SectionName = Section.getSectionName();
Sam Clegg759631c2017-09-15 20:54:59 +00001158
Sam Cleggbafe6902017-12-15 00:17:10 +00001159 // .init_array sections are handled specially elsewhere.
Sam Clegg4d57fbd2018-05-02 23:11:38 +00001160 if (SectionName.startswith(".init_array"))
Sam Cleggbafe6902017-12-15 00:17:10 +00001161 continue;
1162
Sam Clegg4d57fbd2018-05-02 23:11:38 +00001163 // Code is handled separately
1164 if (Section.getKind().isText())
1165 continue;
Sam Cleggea7cace2018-01-09 23:43:14 +00001166
Sam Clegg4d57fbd2018-05-02 23:11:38 +00001167 if (Section.isWasmData()) {
1168 uint32_t SegmentIndex = DataSegments.size();
1169 DataSize = alignTo(DataSize, Section.getAlignment());
1170 DataSegments.emplace_back();
1171 WasmDataSegment &Segment = DataSegments.back();
1172 Segment.Name = SectionName;
1173 Segment.Offset = DataSize;
1174 Segment.Section = &Section;
1175 addData(Segment.Data, Section);
1176 Segment.Alignment = Section.getAlignment();
1177 Segment.Flags = 0;
1178 DataSize += Segment.Data.size();
1179 Section.setSegmentIndex(SegmentIndex);
1180
1181 if (const MCSymbolWasm *C = Section.getGroup()) {
1182 Comdats[C->getName()].emplace_back(
1183 WasmComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex});
1184 }
1185 } else {
1186 // Create custom sections
1187 assert(Sec.getKind().isMetadata());
1188
1189 StringRef Name = SectionName;
1190
1191 // For user-defined custom sections, strip the prefix
1192 if (Name.startswith(".custom_section."))
1193 Name = Name.substr(strlen(".custom_section."));
1194
1195 MCSymbol* Begin = Sec.getBeginSymbol();
Sam Cleggfb807d42018-05-07 19:40:50 +00001196 if (Begin) {
Sam Clegg4d57fbd2018-05-02 23:11:38 +00001197 WasmIndices[cast<MCSymbolWasm>(Begin)] = CustomSections.size();
Sam Cleggb210c642018-05-10 17:38:35 +00001198 if (SectionName != Begin->getName())
Sam Cleggfb807d42018-05-07 19:40:50 +00001199 report_fatal_error("section name and begin symbol should match: " +
Sam Cleggb210c642018-05-10 17:38:35 +00001200 Twine(SectionName));
Sam Cleggfb807d42018-05-07 19:40:50 +00001201 }
Sam Clegg4d57fbd2018-05-02 23:11:38 +00001202 CustomSections.emplace_back(Name, &Section);
Sam Cleggea7cace2018-01-09 23:43:14 +00001203 }
Sam Clegg759631c2017-09-15 20:54:59 +00001204 }
1205
Nicholas Wilson586320c2018-02-28 17:19:48 +00001206 // Populate WasmIndices and DataLocations for defined symbols.
Dan Gohmand934cb82017-02-24 23:18:00 +00001207 for (const MCSymbol &S : Asm.symbols()) {
1208 // Ignore unnamed temporary symbols, which aren't ever exported, imported,
1209 // or used in relocations.
1210 if (S.isTemporary() && S.getName().empty())
1211 continue;
Sam Cleggb7787fd2017-06-20 04:04:59 +00001212
Dan Gohmand934cb82017-02-24 23:18:00 +00001213 const auto &WS = static_cast<const MCSymbolWasm &>(S);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001214 LLVM_DEBUG(
1215 dbgs() << "MCSymbol: " << toString(WS.getType()) << " '" << S << "'"
1216 << " isDefined=" << S.isDefined() << " isExternal="
1217 << S.isExternal() << " isTemporary=" << S.isTemporary()
1218 << " isWeak=" << WS.isWeak() << " isHidden=" << WS.isHidden()
1219 << " isVariable=" << WS.isVariable() << "\n");
Sam Cleggb7787fd2017-06-20 04:04:59 +00001220
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001221 if (WS.isVariable())
1222 continue;
Sam Clegg6c899ba2018-02-23 05:08:34 +00001223 if (WS.isComdat() && !WS.isDefined())
1224 continue;
Sam Cleggb7787fd2017-06-20 04:04:59 +00001225
Dan Gohmand934cb82017-02-24 23:18:00 +00001226 if (WS.isFunction()) {
Sam Clegg6c899ba2018-02-23 05:08:34 +00001227 unsigned Index;
Sam Cleggcd65f692018-01-11 23:59:16 +00001228 if (WS.isDefined()) {
Sam Cleggb7787fd2017-06-20 04:04:59 +00001229 if (WS.getOffset() != 0)
1230 report_fatal_error(
1231 "function sections must contain one function each");
1232
1233 if (WS.getSize() == 0)
1234 report_fatal_error(
1235 "function symbols must have a size set with .size");
1236
Sam Clegg6c899ba2018-02-23 05:08:34 +00001237 // A definition. Write out the function body.
Sam Clegg9f3fe422018-01-17 19:28:43 +00001238 Index = NumFunctionImports + Functions.size();
Dan Gohmand934cb82017-02-24 23:18:00 +00001239 WasmFunction Func;
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001240 Func.Type = getFunctionType(WS);
Dan Gohmand934cb82017-02-24 23:18:00 +00001241 Func.Sym = &WS;
Sam Clegg6c899ba2018-02-23 05:08:34 +00001242 WasmIndices[&WS] = Index;
Dan Gohmand934cb82017-02-24 23:18:00 +00001243 Functions.push_back(Func);
Sam Clegg6c899ba2018-02-23 05:08:34 +00001244
1245 auto &Section = static_cast<MCSectionWasm &>(WS.getSection());
1246 if (const MCSymbolWasm *C = Section.getGroup()) {
1247 Comdats[C->getName()].emplace_back(
1248 WasmComdatEntry{wasm::WASM_COMDAT_FUNCTION, Index});
1249 }
Dan Gohmand934cb82017-02-24 23:18:00 +00001250 } else {
1251 // An import; the index was assigned above.
Sam Clegg6c899ba2018-02-23 05:08:34 +00001252 Index = WasmIndices.find(&WS)->second;
Dan Gohmand934cb82017-02-24 23:18:00 +00001253 }
1254
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001255 LLVM_DEBUG(dbgs() << " -> function index: " << Index << "\n");
Sam Clegg6c899ba2018-02-23 05:08:34 +00001256 } else if (WS.isData()) {
Sam Cleggc38e9472017-06-02 01:05:24 +00001257 if (WS.isTemporary() && !WS.getSize())
1258 continue;
Dan Gohmand934cb82017-02-24 23:18:00 +00001259
Sam Clegg6c899ba2018-02-23 05:08:34 +00001260 if (!WS.isDefined()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001261 LLVM_DEBUG(dbgs() << " -> segment index: -1"
1262 << "\n");
Sam Cleggfe6414b2017-06-21 23:46:41 +00001263 continue;
Sam Clegg6c899ba2018-02-23 05:08:34 +00001264 }
Sam Cleggc38e9472017-06-02 01:05:24 +00001265
Sam Cleggfe6414b2017-06-21 23:46:41 +00001266 if (!WS.getSize())
1267 report_fatal_error("data symbols must have a size set with .size: " +
1268 WS.getName());
Sam Cleggc38e9472017-06-02 01:05:24 +00001269
Sam Cleggfe6414b2017-06-21 23:46:41 +00001270 int64_t Size = 0;
1271 if (!WS.getSize()->evaluateAsAbsolute(Size, Layout))
1272 report_fatal_error(".size expression must be evaluatable");
Dan Gohmand934cb82017-02-24 23:18:00 +00001273
Sam Clegg759631c2017-09-15 20:54:59 +00001274 auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection());
Sam Cleggea7cace2018-01-09 23:43:14 +00001275 assert(DataSection.isWasmData());
Sam Clegg7c395942017-09-14 23:07:53 +00001276
Sam Clegg6c899ba2018-02-23 05:08:34 +00001277 // For each data symbol, export it in the symtab as a reference to the
1278 // corresponding Wasm data segment.
1279 wasm::WasmDataReference Ref = wasm::WasmDataReference{
1280 DataSection.getSegmentIndex(),
1281 static_cast<uint32_t>(Layout.getSymbolOffset(WS)),
1282 static_cast<uint32_t>(Size)};
1283 DataLocations[&WS] = Ref;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001284 LLVM_DEBUG(dbgs() << " -> segment index: " << Ref.Segment << "\n");
Sam Clegga165f2d2018-04-30 19:40:57 +00001285 } else if (WS.isGlobal()) {
Sam Clegg6c899ba2018-02-23 05:08:34 +00001286 // A "true" Wasm global (currently just __stack_pointer)
Eric Christopher545932b2018-02-23 21:14:47 +00001287 if (WS.isDefined())
Sam Clegg6c899ba2018-02-23 05:08:34 +00001288 report_fatal_error("don't yet support defined globals");
Sam Clegg6c899ba2018-02-23 05:08:34 +00001289
Eric Christopher545932b2018-02-23 21:14:47 +00001290 // An import; the index was assigned above
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001291 LLVM_DEBUG(dbgs() << " -> global index: "
1292 << WasmIndices.find(&WS)->second << "\n");
Sam Clegga165f2d2018-04-30 19:40:57 +00001293 } else {
1294 assert(WS.isSection());
Dan Gohmand934cb82017-02-24 23:18:00 +00001295 }
1296 }
1297
Nicholas Wilson586320c2018-02-28 17:19:48 +00001298 // Populate WasmIndices and DataLocations for aliased symbols. We need to
1299 // process these in a separate pass because we need to have processed the
1300 // target of the alias before the alias itself and the symbols are not
1301 // necessarily ordered in this way.
Sam Cleggb7787fd2017-06-20 04:04:59 +00001302 for (const MCSymbol &S : Asm.symbols()) {
1303 if (!S.isVariable())
1304 continue;
Sam Clegg31a2c802017-09-20 21:17:04 +00001305
Sam Cleggcd65f692018-01-11 23:59:16 +00001306 assert(S.isDefined());
Sam Cleggb7787fd2017-06-20 04:04:59 +00001307
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001308 // Find the target symbol of this weak alias and export that index
Sam Cleggaff1c4d2017-09-15 19:22:01 +00001309 const auto &WS = static_cast<const MCSymbolWasm &>(S);
1310 const MCSymbolWasm *ResolvedSym = ResolveSymbol(WS);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001311 LLVM_DEBUG(dbgs() << WS.getName() << ": weak alias of '" << *ResolvedSym
1312 << "'\n");
Sam Cleggb7787fd2017-06-20 04:04:59 +00001313
Sam Clegg6c899ba2018-02-23 05:08:34 +00001314 if (WS.isFunction()) {
1315 assert(WasmIndices.count(ResolvedSym) > 0);
1316 uint32_t WasmIndex = WasmIndices.find(ResolvedSym)->second;
1317 WasmIndices[&WS] = WasmIndex;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001318 LLVM_DEBUG(dbgs() << " -> index:" << WasmIndex << "\n");
Sam Clegg6c899ba2018-02-23 05:08:34 +00001319 } else if (WS.isData()) {
1320 assert(DataLocations.count(ResolvedSym) > 0);
1321 const wasm::WasmDataReference &Ref =
1322 DataLocations.find(ResolvedSym)->second;
1323 DataLocations[&WS] = Ref;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001324 LLVM_DEBUG(dbgs() << " -> index:" << Ref.Segment << "\n");
Sam Clegg6c899ba2018-02-23 05:08:34 +00001325 } else {
1326 report_fatal_error("don't yet support global aliases");
1327 }
Nicholas Wilson586320c2018-02-28 17:19:48 +00001328 }
Sam Clegg31a2c802017-09-20 21:17:04 +00001329
Nicholas Wilson586320c2018-02-28 17:19:48 +00001330 // Finally, populate the symbol table itself, in its "natural" order.
1331 for (const MCSymbol &S : Asm.symbols()) {
1332 const auto &WS = static_cast<const MCSymbolWasm &>(S);
Sam Clegg25d8e682018-05-08 00:08:21 +00001333 if (!isInSymtab(WS)) {
1334 WS.setIndex(INVALID_INDEX);
Nicholas Wilson586320c2018-02-28 17:19:48 +00001335 continue;
Sam Clegg25d8e682018-05-08 00:08:21 +00001336 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001337 LLVM_DEBUG(dbgs() << "adding to symtab: " << WS << "\n");
Nicholas Wilson586320c2018-02-28 17:19:48 +00001338
1339 uint32_t Flags = 0;
1340 if (WS.isWeak())
1341 Flags |= wasm::WASM_SYMBOL_BINDING_WEAK;
1342 if (WS.isHidden())
1343 Flags |= wasm::WASM_SYMBOL_VISIBILITY_HIDDEN;
1344 if (!WS.isExternal() && WS.isDefined())
1345 Flags |= wasm::WASM_SYMBOL_BINDING_LOCAL;
1346 if (WS.isUndefined())
1347 Flags |= wasm::WASM_SYMBOL_UNDEFINED;
1348
1349 wasm::WasmSymbolInfo Info;
1350 Info.Name = WS.getName();
1351 Info.Kind = WS.getType();
1352 Info.Flags = Flags;
Sam Clegg4d57fbd2018-05-02 23:11:38 +00001353 if (!WS.isData()) {
1354 assert(WasmIndices.count(&WS) > 0);
Nicholas Wilson586320c2018-02-28 17:19:48 +00001355 Info.ElementIndex = WasmIndices.find(&WS)->second;
Sam Clegg4d57fbd2018-05-02 23:11:38 +00001356 } else if (WS.isDefined()) {
1357 assert(DataLocations.count(&WS) > 0);
Nicholas Wilson586320c2018-02-28 17:19:48 +00001358 Info.DataRef = DataLocations.find(&WS)->second;
Sam Clegg4d57fbd2018-05-02 23:11:38 +00001359 }
Sam Clegg25d8e682018-05-08 00:08:21 +00001360 WS.setIndex(SymbolInfos.size());
Nicholas Wilson586320c2018-02-28 17:19:48 +00001361 SymbolInfos.emplace_back(Info);
Sam Cleggb7787fd2017-06-20 04:04:59 +00001362 }
1363
Sam Clegg6006e092017-12-22 20:31:39 +00001364 {
1365 auto HandleReloc = [&](const WasmRelocationEntry &Rel) {
Sam Cleggf9edbe92018-01-31 19:28:47 +00001366 // Functions referenced by a relocation need to put in the table. This is
1367 // purely to make the object file's provisional values readable, and is
1368 // ignored by the linker, which re-calculates the relocations itself.
1369 if (Rel.Type != wasm::R_WEBASSEMBLY_TABLE_INDEX_I32 &&
1370 Rel.Type != wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB)
1371 return;
1372 assert(Rel.Symbol->isFunction());
1373 const MCSymbolWasm &WS = *ResolveSymbol(*Rel.Symbol);
Sam Clegg6c899ba2018-02-23 05:08:34 +00001374 uint32_t FunctionIndex = WasmIndices.find(&WS)->second;
Sam Cleggf9edbe92018-01-31 19:28:47 +00001375 uint32_t TableIndex = TableElems.size() + kInitialTableOffset;
1376 if (TableIndices.try_emplace(&WS, TableIndex).second) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001377 LLVM_DEBUG(dbgs() << " -> adding " << WS.getName()
1378 << " to table: " << TableIndex << "\n");
Sam Clegg6c899ba2018-02-23 05:08:34 +00001379 TableElems.push_back(FunctionIndex);
Sam Cleggf9edbe92018-01-31 19:28:47 +00001380 registerFunctionType(WS);
Sam Clegg6006e092017-12-22 20:31:39 +00001381 }
1382 };
Dan Gohman970d02c2017-03-30 23:58:19 +00001383
Sam Clegg6006e092017-12-22 20:31:39 +00001384 for (const WasmRelocationEntry &RelEntry : CodeRelocations)
1385 HandleReloc(RelEntry);
1386 for (const WasmRelocationEntry &RelEntry : DataRelocations)
1387 HandleReloc(RelEntry);
Dan Gohmand934cb82017-02-24 23:18:00 +00001388 }
1389
Sam Cleggbafe6902017-12-15 00:17:10 +00001390 // Translate .init_array section contents into start functions.
1391 for (const MCSection &S : Asm) {
1392 const auto &WS = static_cast<const MCSectionWasm &>(S);
1393 if (WS.getSectionName().startswith(".fini_array"))
1394 report_fatal_error(".fini_array sections are unsupported");
1395 if (!WS.getSectionName().startswith(".init_array"))
1396 continue;
1397 if (WS.getFragmentList().empty())
1398 continue;
Sam Cleggb210c642018-05-10 17:38:35 +00001399
1400 // init_array is expected to contain a single non-empty data fragment
1401 if (WS.getFragmentList().size() != 3)
Sam Cleggbafe6902017-12-15 00:17:10 +00001402 report_fatal_error("only one .init_array section fragment supported");
Sam Cleggb210c642018-05-10 17:38:35 +00001403
1404 auto IT = WS.begin();
1405 const MCFragment &EmptyFrag = *IT;
1406 if (EmptyFrag.getKind() != MCFragment::FT_Data)
1407 report_fatal_error(".init_array section should be aligned");
1408
1409 IT = std::next(IT);
1410 const MCFragment &AlignFrag = *IT;
Sam Cleggbafe6902017-12-15 00:17:10 +00001411 if (AlignFrag.getKind() != MCFragment::FT_Align)
1412 report_fatal_error(".init_array section should be aligned");
1413 if (cast<MCAlignFragment>(AlignFrag).getAlignment() != (is64Bit() ? 8 : 4))
1414 report_fatal_error(".init_array section should be aligned for pointers");
Sam Cleggb210c642018-05-10 17:38:35 +00001415
1416 const MCFragment &Frag = *std::next(IT);
Sam Cleggbafe6902017-12-15 00:17:10 +00001417 if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
1418 report_fatal_error("only data supported in .init_array section");
Sam Cleggb210c642018-05-10 17:38:35 +00001419
Sam Cleggbafe6902017-12-15 00:17:10 +00001420 uint16_t Priority = UINT16_MAX;
Sam Clegg4d57fbd2018-05-02 23:11:38 +00001421 unsigned PrefixLength = strlen(".init_array");
1422 if (WS.getSectionName().size() > PrefixLength) {
1423 if (WS.getSectionName()[PrefixLength] != '.')
Sam Cleggbafe6902017-12-15 00:17:10 +00001424 report_fatal_error(".init_array section priority should start with '.'");
Sam Clegg4d57fbd2018-05-02 23:11:38 +00001425 if (WS.getSectionName()
1426 .substr(PrefixLength + 1)
1427 .getAsInteger(10, Priority))
Sam Cleggbafe6902017-12-15 00:17:10 +00001428 report_fatal_error("invalid .init_array section priority");
1429 }
1430 const auto &DataFrag = cast<MCDataFragment>(Frag);
1431 const SmallVectorImpl<char> &Contents = DataFrag.getContents();
1432 for (const uint8_t *p = (const uint8_t *)Contents.data(),
1433 *end = (const uint8_t *)Contents.data() + Contents.size();
1434 p != end; ++p) {
1435 if (*p != 0)
1436 report_fatal_error("non-symbolic data in .init_array section");
1437 }
1438 for (const MCFixup &Fixup : DataFrag.getFixups()) {
1439 assert(Fixup.getKind() == MCFixup::getKindForSize(is64Bit() ? 8 : 4, false));
1440 const MCExpr *Expr = Fixup.getValue();
1441 auto *Sym = dyn_cast<MCSymbolRefExpr>(Expr);
1442 if (!Sym)
1443 report_fatal_error("fixups in .init_array should be symbol references");
1444 if (Sym->getKind() != MCSymbolRefExpr::VK_WebAssembly_FUNCTION)
1445 report_fatal_error("symbols in .init_array should be for functions");
Sam Clegg25d8e682018-05-08 00:08:21 +00001446 if (Sym->getSymbol().getIndex() == INVALID_INDEX)
1447 report_fatal_error("symbols in .init_array should exist in symbtab");
1448 InitFuncs.push_back(
1449 std::make_pair(Priority, Sym->getSymbol().getIndex()));
Sam Cleggbafe6902017-12-15 00:17:10 +00001450 }
1451 }
1452
Dan Gohman18eafb62017-02-22 01:23:18 +00001453 // Write out the Wasm header.
1454 writeHeader(Asm);
1455
Sam Clegg9e15f352017-06-03 02:01:24 +00001456 writeTypeSection(FunctionTypes);
Sam Cleggf950b242017-12-11 23:03:38 +00001457 writeImportSection(Imports, DataSize, TableElems.size());
Sam Clegg9e15f352017-06-03 02:01:24 +00001458 writeFunctionSection(Functions);
Sam Cleggf950b242017-12-11 23:03:38 +00001459 // Skip the "table" section; we import the table instead.
1460 // Skip the "memory" section; we import the memory instead.
Sam Clegg7c395942017-09-14 23:07:53 +00001461 writeGlobalSection();
Sam Clegg9e15f352017-06-03 02:01:24 +00001462 writeExportSection(Exports);
Sam Clegg9e15f352017-06-03 02:01:24 +00001463 writeElemSection(TableElems);
Sam Cleggacd7d2b2017-06-06 19:15:05 +00001464 writeCodeSection(Asm, Layout, Functions);
Sam Clegg6c899ba2018-02-23 05:08:34 +00001465 writeDataSection();
Sam Clegg6a31a0d2018-04-26 19:27:28 +00001466 writeCustomSections(Asm, Layout);
Nicholas Wilsonc22bfb62018-03-05 12:59:03 +00001467 writeLinkingMetaDataSection(SymbolInfos, InitFuncs, Comdats);
Sam Clegg6f08c842018-04-24 18:11:36 +00001468 writeRelocSection(CodeSectionIndex, "CODE", CodeRelocations);
1469 writeRelocSection(DataSectionIndex, "DATA", DataRelocations);
Sam Clegg6a31a0d2018-04-26 19:27:28 +00001470 writeCustomRelocSections();
Dan Gohman970d02c2017-03-30 23:58:19 +00001471
Dan Gohmand934cb82017-02-24 23:18:00 +00001472 // TODO: Translate the .comment section to the output.
Dan Gohman18eafb62017-02-22 01:23:18 +00001473}
1474
Lang Hames60fbc7c2017-10-10 16:28:07 +00001475std::unique_ptr<MCObjectWriter>
Lang Hames1301a872017-10-10 01:15:10 +00001476llvm::createWasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
1477 raw_pwrite_stream &OS) {
Dan Gohman0917c9e2018-01-15 17:06:23 +00001478 return llvm::make_unique<WasmObjectWriter>(std::move(MOTW), OS);
Dan Gohman18eafb62017-02-22 01:23:18 +00001479}