blob: 35d9a58f6d367dcca58cede13eed437939aa63dd [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"
Dan Gohman18eafb62017-02-22 01:23:18 +000017#include "llvm/MC/MCAsmBackend.h"
18#include "llvm/MC/MCAsmInfo.h"
19#include "llvm/MC/MCAsmLayout.h"
20#include "llvm/MC/MCAssembler.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCFixupKindInfo.h"
24#include "llvm/MC/MCObjectFileInfo.h"
25#include "llvm/MC/MCObjectWriter.h"
26#include "llvm/MC/MCSectionWasm.h"
27#include "llvm/MC/MCSymbolWasm.h"
28#include "llvm/MC/MCValue.h"
29#include "llvm/MC/MCWasmObjectWriter.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000030#include "llvm/Support/Casting.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000031#include "llvm/Support/Debug.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000032#include "llvm/Support/ErrorHandling.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000033#include "llvm/Support/LEB128.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000034#include "llvm/Support/StringSaver.h"
35#include <vector>
36
37using namespace llvm;
38
Sam Clegg5e3d33a2017-07-07 02:01:29 +000039#define DEBUG_TYPE "mc"
Dan Gohman18eafb62017-02-22 01:23:18 +000040
41namespace {
Sam Clegg9e15f352017-06-03 02:01:24 +000042
Dan Gohmand934cb82017-02-24 23:18:00 +000043// For patching purposes, we need to remember where each section starts, both
44// for patching up the section size field, and for patching up references to
45// locations within the section.
46struct SectionBookkeeping {
47 // Where the size of the section is written.
48 uint64_t SizeOffset;
49 // Where the contents of the section starts (after the header).
50 uint64_t ContentsOffset;
51};
52
Sam Clegg9e15f352017-06-03 02:01:24 +000053// The signature of a wasm function, in a struct capable of being used as a
54// DenseMap key.
55struct WasmFunctionType {
56 // Support empty and tombstone instances, needed by DenseMap.
57 enum { Plain, Empty, Tombstone } State;
58
59 // The return types of the function.
60 SmallVector<wasm::ValType, 1> Returns;
61
62 // The parameter types of the function.
63 SmallVector<wasm::ValType, 4> Params;
64
65 WasmFunctionType() : State(Plain) {}
66
67 bool operator==(const WasmFunctionType &Other) const {
68 return State == Other.State && Returns == Other.Returns &&
69 Params == Other.Params;
70 }
71};
72
73// Traits for using WasmFunctionType in a DenseMap.
74struct WasmFunctionTypeDenseMapInfo {
75 static WasmFunctionType getEmptyKey() {
76 WasmFunctionType FuncTy;
77 FuncTy.State = WasmFunctionType::Empty;
78 return FuncTy;
79 }
80 static WasmFunctionType getTombstoneKey() {
81 WasmFunctionType FuncTy;
82 FuncTy.State = WasmFunctionType::Tombstone;
83 return FuncTy;
84 }
85 static unsigned getHashValue(const WasmFunctionType &FuncTy) {
86 uintptr_t Value = FuncTy.State;
87 for (wasm::ValType Ret : FuncTy.Returns)
88 Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Ret));
89 for (wasm::ValType Param : FuncTy.Params)
90 Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Param));
91 return Value;
92 }
93 static bool isEqual(const WasmFunctionType &LHS,
94 const WasmFunctionType &RHS) {
95 return LHS == RHS;
96 }
97};
98
99// A wasm import to be written into the import section.
100struct WasmImport {
101 StringRef ModuleName;
102 StringRef FieldName;
103 unsigned Kind;
104 int32_t Type;
105};
106
107// A wasm function to be written into the function section.
108struct WasmFunction {
109 int32_t Type;
110 const MCSymbolWasm *Sym;
111};
112
113// A wasm export to be written into the export section.
114struct WasmExport {
115 StringRef FieldName;
116 unsigned Kind;
117 uint32_t Index;
118};
119
120// A wasm global to be written into the global section.
121struct WasmGlobal {
122 wasm::ValType Type;
123 bool IsMutable;
124 bool HasImport;
125 uint64_t InitialValue;
126 uint32_t ImportIndex;
127};
128
Sam Clegg6dc65e92017-06-06 16:38:59 +0000129// Information about a single relocation.
130struct WasmRelocationEntry {
Sam Cleggfe6414b2017-06-21 23:46:41 +0000131 uint64_t Offset; // Where is the relocation.
132 const MCSymbolWasm *Symbol; // The symbol to relocate with.
133 int64_t Addend; // A value to add to the symbol.
134 unsigned Type; // The type of the relocation.
135 const MCSectionWasm *FixupSection;// The section the relocation is targeting.
Sam Clegg6dc65e92017-06-06 16:38:59 +0000136
137 WasmRelocationEntry(uint64_t Offset, const MCSymbolWasm *Symbol,
138 int64_t Addend, unsigned Type,
Sam Cleggfe6414b2017-06-21 23:46:41 +0000139 const MCSectionWasm *FixupSection)
Sam Clegg6dc65e92017-06-06 16:38:59 +0000140 : Offset(Offset), Symbol(Symbol), Addend(Addend), Type(Type),
141 FixupSection(FixupSection) {}
142
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000143 bool hasAddend() const {
144 switch (Type) {
Sam Clegg13a2e892017-09-01 17:32:01 +0000145 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
146 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
147 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000148 return true;
149 default:
150 return false;
151 }
152 }
153
Sam Clegg6dc65e92017-06-06 16:38:59 +0000154 void print(raw_ostream &Out) const {
Sam Clegg9bf73c02017-07-05 20:25:08 +0000155 Out << "Off=" << Offset << ", Sym=" << *Symbol << ", Addend=" << Addend
Sam Clegg6dc65e92017-06-06 16:38:59 +0000156 << ", Type=" << Type << ", FixupSection=" << FixupSection;
157 }
Sam Cleggb7787fd2017-06-20 04:04:59 +0000158
159#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
160 LLVM_DUMP_METHOD void dump() const { print(dbgs()); }
161#endif
Sam Clegg6dc65e92017-06-06 16:38:59 +0000162};
163
Sam Clegg1fb8daa2017-06-20 05:05:10 +0000164#if !defined(NDEBUG)
Sam Clegg7f055de2017-06-20 04:47:58 +0000165raw_ostream &operator<<(raw_ostream &OS, const WasmRelocationEntry &Rel) {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000166 Rel.print(OS);
167 return OS;
168}
Sam Clegg1fb8daa2017-06-20 05:05:10 +0000169#endif
Sam Cleggb7787fd2017-06-20 04:04:59 +0000170
Dan Gohman18eafb62017-02-22 01:23:18 +0000171class WasmObjectWriter : public MCObjectWriter {
172 /// Helper struct for containing some precomputed information on symbols.
173 struct WasmSymbolData {
174 const MCSymbolWasm *Symbol;
175 StringRef Name;
176
177 // Support lexicographic sorting.
178 bool operator<(const WasmSymbolData &RHS) const { return Name < RHS.Name; }
179 };
180
181 /// The target specific Wasm writer instance.
182 std::unique_ptr<MCWasmObjectTargetWriter> TargetObjectWriter;
183
Dan Gohmand934cb82017-02-24 23:18:00 +0000184 // Relocations for fixing up references in the code section.
185 std::vector<WasmRelocationEntry> CodeRelocations;
186
187 // Relocations for fixing up references in the data section.
188 std::vector<WasmRelocationEntry> DataRelocations;
189
Dan Gohmand934cb82017-02-24 23:18:00 +0000190 // Index values to use for fixing up call_indirect type indices.
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000191 // Maps function symbols to the index of the type of the function
192 DenseMap<const MCSymbolWasm *, uint32_t> TypeIndices;
Sam Cleggd99f6072017-06-12 23:52:44 +0000193 // Maps function symbols to the table element index space. Used
194 // for TABLE_INDEX relocation types (i.e. address taken functions).
195 DenseMap<const MCSymbolWasm *, uint32_t> IndirectSymbolIndices;
196 // Maps function/global symbols to the function/global index space.
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000197 DenseMap<const MCSymbolWasm *, uint32_t> SymbolIndices;
198
199 DenseMap<WasmFunctionType, int32_t, WasmFunctionTypeDenseMapInfo>
200 FunctionTypeIndices;
Sam Clegg5e3d33a2017-07-07 02:01:29 +0000201 SmallVector<WasmFunctionType, 4> FunctionTypes;
Dan Gohmand934cb82017-02-24 23:18:00 +0000202
Dan Gohman18eafb62017-02-22 01:23:18 +0000203 // TargetObjectWriter wrappers.
204 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
Sam Cleggae03c1e72017-06-13 18:51:50 +0000205 unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup) const {
206 return TargetObjectWriter->getRelocType(Target, Fixup);
Dan Gohman18eafb62017-02-22 01:23:18 +0000207 }
208
Dan Gohmand934cb82017-02-24 23:18:00 +0000209 void startSection(SectionBookkeeping &Section, unsigned SectionId,
210 const char *Name = nullptr);
211 void endSection(SectionBookkeeping &Section);
212
Dan Gohman18eafb62017-02-22 01:23:18 +0000213public:
214 WasmObjectWriter(MCWasmObjectTargetWriter *MOTW, raw_pwrite_stream &OS)
215 : MCObjectWriter(OS, /*IsLittleEndian=*/true), TargetObjectWriter(MOTW) {}
216
Dan Gohmand934cb82017-02-24 23:18:00 +0000217private:
Dan Gohman18eafb62017-02-22 01:23:18 +0000218 ~WasmObjectWriter() override;
219
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000220 void reset() override {
221 CodeRelocations.clear();
222 DataRelocations.clear();
223 TypeIndices.clear();
224 SymbolIndices.clear();
Sam Cleggd99f6072017-06-12 23:52:44 +0000225 IndirectSymbolIndices.clear();
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000226 FunctionTypeIndices.clear();
Sam Clegg5e3d33a2017-07-07 02:01:29 +0000227 FunctionTypes.clear();
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000228 MCObjectWriter::reset();
229 }
230
Dan Gohman18eafb62017-02-22 01:23:18 +0000231 void writeHeader(const MCAssembler &Asm);
232
233 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
234 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindolaceecfe5b2017-07-11 23:56:10 +0000235 MCValue Target, uint64_t &FixedValue) override;
Dan Gohman18eafb62017-02-22 01:23:18 +0000236
237 void executePostLayoutBinding(MCAssembler &Asm,
238 const MCAsmLayout &Layout) override;
239
240 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Sam Clegg9e15f352017-06-03 02:01:24 +0000241
Sam Cleggb7787fd2017-06-20 04:04:59 +0000242 void writeString(const StringRef Str) {
243 encodeULEB128(Str.size(), getStream());
244 writeBytes(Str);
245 }
246
Sam Clegg9e15f352017-06-03 02:01:24 +0000247 void writeValueType(wasm::ValType Ty) {
248 encodeSLEB128(int32_t(Ty), getStream());
249 }
250
251 void writeTypeSection(const SmallVector<WasmFunctionType, 4> &FunctionTypes);
252 void writeImportSection(const SmallVector<WasmImport, 4> &Imports);
253 void writeFunctionSection(const SmallVector<WasmFunction, 4> &Functions);
Sam Cleggd99f6072017-06-12 23:52:44 +0000254 void writeTableSection(uint32_t NumElements);
Sam Clegg9e15f352017-06-03 02:01:24 +0000255 void writeMemorySection(const SmallVector<char, 0> &DataBytes);
256 void writeGlobalSection(const SmallVector<WasmGlobal, 4> &Globals);
257 void writeExportSection(const SmallVector<WasmExport, 4> &Exports);
258 void writeElemSection(const SmallVector<uint32_t, 4> &TableElems);
259 void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
Sam Clegg9e15f352017-06-03 02:01:24 +0000260 const SmallVector<WasmFunction, 4> &Functions);
261 uint64_t
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000262 writeDataSection(const SmallVector<char, 0> &DataBytes);
Sam Clegg9e15f352017-06-03 02:01:24 +0000263 void writeNameSection(const SmallVector<WasmFunction, 4> &Functions,
264 const SmallVector<WasmImport, 4> &Imports,
265 uint32_t NumFuncImports);
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000266 void writeCodeRelocSection();
267 void writeDataRelocSection(uint64_t DataSectionHeaderSize);
Sam Clegg9e1ade92017-06-27 20:27:59 +0000268 void writeLinkingMetaDataSection(uint32_t DataSize, uint32_t DataAlignment,
269 ArrayRef<StringRef> WeakSymbols,
Sam Cleggb7787fd2017-06-20 04:04:59 +0000270 bool HasStackPointer,
Sam Clegg9e15f352017-06-03 02:01:24 +0000271 uint32_t StackPointerGlobal);
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000272
273 void applyRelocations(ArrayRef<WasmRelocationEntry> Relocations,
274 uint64_t ContentsOffset);
275
276 void writeRelocations(ArrayRef<WasmRelocationEntry> Relocations,
277 uint64_t HeaderSize);
278 uint32_t getRelocationIndexValue(const WasmRelocationEntry &RelEntry);
Sam Clegg5e3d33a2017-07-07 02:01:29 +0000279 uint32_t getFunctionType(const MCSymbolWasm& Symbol);
280 uint32_t registerFunctionType(const MCSymbolWasm& Symbol);
Dan Gohman18eafb62017-02-22 01:23:18 +0000281};
Sam Clegg9e15f352017-06-03 02:01:24 +0000282
Dan Gohman18eafb62017-02-22 01:23:18 +0000283} // end anonymous namespace
284
285WasmObjectWriter::~WasmObjectWriter() {}
286
Dan Gohmand934cb82017-02-24 23:18:00 +0000287// Return the padding size to write a 32-bit value into a 5-byte ULEB128.
288static unsigned PaddingFor5ByteULEB128(uint32_t X) {
289 return X == 0 ? 4 : (4u - (31u - countLeadingZeros(X)) / 7u);
290}
291
292// Return the padding size to write a 32-bit value into a 5-byte SLEB128.
293static unsigned PaddingFor5ByteSLEB128(int32_t X) {
294 return 5 - getSLEB128Size(X);
295}
296
297// Write out a section header and a patchable section size field.
298void WasmObjectWriter::startSection(SectionBookkeeping &Section,
299 unsigned SectionId,
300 const char *Name) {
301 assert((Name != nullptr) == (SectionId == wasm::WASM_SEC_CUSTOM) &&
302 "Only custom sections can have names");
303
Sam Cleggb7787fd2017-06-20 04:04:59 +0000304 DEBUG(dbgs() << "startSection " << SectionId << ": " << Name << "\n");
Derek Schuffe2688c42017-03-14 20:23:22 +0000305 encodeULEB128(SectionId, getStream());
Dan Gohmand934cb82017-02-24 23:18:00 +0000306
307 Section.SizeOffset = getStream().tell();
308
309 // The section size. We don't know the size yet, so reserve enough space
310 // for any 32-bit value; we'll patch it later.
311 encodeULEB128(UINT32_MAX, getStream());
312
313 // The position where the section starts, for measuring its size.
314 Section.ContentsOffset = getStream().tell();
315
316 // Custom sections in wasm also have a string identifier.
317 if (SectionId == wasm::WASM_SEC_CUSTOM) {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000318 assert(Name);
319 writeString(StringRef(Name));
Dan Gohmand934cb82017-02-24 23:18:00 +0000320 }
321}
322
323// Now that the section is complete and we know how big it is, patch up the
324// section size field at the start of the section.
325void WasmObjectWriter::endSection(SectionBookkeeping &Section) {
326 uint64_t Size = getStream().tell() - Section.ContentsOffset;
327 if (uint32_t(Size) != Size)
328 report_fatal_error("section size does not fit in a uint32_t");
329
Sam Cleggb7787fd2017-06-20 04:04:59 +0000330 DEBUG(dbgs() << "endSection size=" << Size << "\n");
Dan Gohmand934cb82017-02-24 23:18:00 +0000331 unsigned Padding = PaddingFor5ByteULEB128(Size);
332
333 // Write the final section size to the payload_len field, which follows
334 // the section id byte.
335 uint8_t Buffer[16];
336 unsigned SizeLen = encodeULEB128(Size, Buffer, Padding);
337 assert(SizeLen == 5);
338 getStream().pwrite((char *)Buffer, SizeLen, Section.SizeOffset);
339}
340
Dan Gohman18eafb62017-02-22 01:23:18 +0000341// Emit the Wasm header.
342void WasmObjectWriter::writeHeader(const MCAssembler &Asm) {
Dan Gohman7ea5adf2017-02-22 18:50:20 +0000343 writeBytes(StringRef(wasm::WasmMagic, sizeof(wasm::WasmMagic)));
344 writeLE32(wasm::WasmVersion);
Dan Gohman18eafb62017-02-22 01:23:18 +0000345}
346
347void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
348 const MCAsmLayout &Layout) {
349}
350
351void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
352 const MCAsmLayout &Layout,
353 const MCFragment *Fragment,
354 const MCFixup &Fixup, MCValue Target,
Rafael Espindolaceecfe5b2017-07-11 23:56:10 +0000355 uint64_t &FixedValue) {
356 MCAsmBackend &Backend = Asm.getBackend();
357 bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
358 MCFixupKindInfo::FKF_IsPCRel;
Sam Cleggfe6414b2017-06-21 23:46:41 +0000359 const auto &FixupSection = cast<MCSectionWasm>(*Fragment->getParent());
Dan Gohmand934cb82017-02-24 23:18:00 +0000360 uint64_t C = Target.getConstant();
361 uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
362 MCContext &Ctx = Asm.getContext();
363
364 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
365 assert(RefB->getKind() == MCSymbolRefExpr::VK_None &&
366 "Should not have constructed this");
367
368 // Let A, B and C being the components of Target and R be the location of
369 // the fixup. If the fixup is not pcrel, we want to compute (A - B + C).
370 // If it is pcrel, we want to compute (A - B + C - R).
371
372 // In general, Wasm has no relocations for -B. It can only represent (A + C)
373 // or (A + C - R). If B = R + K and the relocation is not pcrel, we can
374 // replace B to implement it: (A - R - K + C)
375 if (IsPCRel) {
376 Ctx.reportError(
377 Fixup.getLoc(),
378 "No relocation available to represent this relative expression");
379 return;
380 }
381
382 const auto &SymB = cast<MCSymbolWasm>(RefB->getSymbol());
383
384 if (SymB.isUndefined()) {
385 Ctx.reportError(Fixup.getLoc(),
386 Twine("symbol '") + SymB.getName() +
387 "' can not be undefined in a subtraction expression");
388 return;
389 }
390
391 assert(!SymB.isAbsolute() && "Should have been folded");
392 const MCSection &SecB = SymB.getSection();
393 if (&SecB != &FixupSection) {
394 Ctx.reportError(Fixup.getLoc(),
395 "Cannot represent a difference across sections");
396 return;
397 }
398
399 uint64_t SymBOffset = Layout.getSymbolOffset(SymB);
400 uint64_t K = SymBOffset - FixupOffset;
401 IsPCRel = true;
402 C -= K;
403 }
404
405 // We either rejected the fixup or folded B into C at this point.
406 const MCSymbolRefExpr *RefA = Target.getSymA();
407 const auto *SymA = RefA ? cast<MCSymbolWasm>(&RefA->getSymbol()) : nullptr;
408
Dan Gohmand934cb82017-02-24 23:18:00 +0000409 if (SymA && SymA->isVariable()) {
410 const MCExpr *Expr = SymA->getVariableValue();
Sam Clegg6ad8f192017-07-11 02:21:57 +0000411 const auto *Inner = cast<MCSymbolRefExpr>(Expr);
412 if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
413 llvm_unreachable("weakref used in reloc not yet implemented");
Dan Gohmand934cb82017-02-24 23:18:00 +0000414 }
415
416 // Put any constant offset in an addend. Offsets can be negative, and
417 // LLVM expects wrapping, in contrast to wasm's immediates which can't
418 // be negative and don't wrap.
419 FixedValue = 0;
420
Sam Clegg6ad8f192017-07-11 02:21:57 +0000421 if (SymA)
422 SymA->setUsedInReloc();
Dan Gohmand934cb82017-02-24 23:18:00 +0000423
Sam Cleggae03c1e72017-06-13 18:51:50 +0000424 assert(!IsPCRel);
Sam Clegg9d24fb72017-06-16 23:59:10 +0000425 assert(SymA);
426
Sam Cleggae03c1e72017-06-13 18:51:50 +0000427 unsigned Type = getRelocType(Target, Fixup);
428
Dan Gohmand934cb82017-02-24 23:18:00 +0000429 WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection);
Sam Cleggb7787fd2017-06-20 04:04:59 +0000430 DEBUG(dbgs() << "WasmReloc: " << Rec << "\n");
Dan Gohmand934cb82017-02-24 23:18:00 +0000431
432 if (FixupSection.hasInstructions())
433 CodeRelocations.push_back(Rec);
434 else
435 DataRelocations.push_back(Rec);
436}
437
Dan Gohmand934cb82017-02-24 23:18:00 +0000438// Write X as an (unsigned) LEB value at offset Offset in Stream, padded
439// to allow patching.
440static void
441WritePatchableLEB(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
442 uint8_t Buffer[5];
443 unsigned Padding = PaddingFor5ByteULEB128(X);
444 unsigned SizeLen = encodeULEB128(X, Buffer, Padding);
445 assert(SizeLen == 5);
446 Stream.pwrite((char *)Buffer, SizeLen, Offset);
447}
448
449// Write X as an signed LEB value at offset Offset in Stream, padded
450// to allow patching.
451static void
452WritePatchableSLEB(raw_pwrite_stream &Stream, int32_t X, uint64_t Offset) {
453 uint8_t Buffer[5];
454 unsigned Padding = PaddingFor5ByteSLEB128(X);
455 unsigned SizeLen = encodeSLEB128(X, Buffer, Padding);
456 assert(SizeLen == 5);
457 Stream.pwrite((char *)Buffer, SizeLen, Offset);
458}
459
460// Write X as a plain integer value at offset Offset in Stream.
461static void WriteI32(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
462 uint8_t Buffer[4];
463 support::endian::write32le(Buffer, X);
464 Stream.pwrite((char *)Buffer, sizeof(Buffer), Offset);
465}
466
467// Compute a value to write into the code at the location covered
468// by RelEntry. This value isn't used by the static linker, since
469// we have addends; it just serves to make the code more readable
470// and to make standalone wasm modules directly usable.
471static uint32_t ProvisionalValue(const WasmRelocationEntry &RelEntry) {
472 const MCSymbolWasm *Sym = RelEntry.Symbol;
473
474 // For undefined symbols, use a hopefully invalid value.
Sam Cleggb7787fd2017-06-20 04:04:59 +0000475 if (!Sym->isDefined(/*SetUsed=*/false))
Dan Gohmand934cb82017-02-24 23:18:00 +0000476 return UINT32_MAX;
477
Sam Cleggfe6414b2017-06-21 23:46:41 +0000478 const auto &Section = cast<MCSectionWasm>(RelEntry.Symbol->getSection(false));
Dan Gohmand934cb82017-02-24 23:18:00 +0000479 uint64_t Address = Section.getSectionOffset() + RelEntry.Addend;
480
481 // Ignore overflow. LLVM allows address arithmetic to silently wrap.
482 uint32_t Value = Address;
483
484 return Value;
485}
486
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000487uint32_t WasmObjectWriter::getRelocationIndexValue(
488 const WasmRelocationEntry &RelEntry) {
489 switch (RelEntry.Type) {
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000490 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
491 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
Sam Cleggb7787fd2017-06-20 04:04:59 +0000492 if (!IndirectSymbolIndices.count(RelEntry.Symbol))
Sam Clegg5e3d33a2017-07-07 02:01:29 +0000493 report_fatal_error("symbol not found table index space: " +
Sam Cleggb7787fd2017-06-20 04:04:59 +0000494 RelEntry.Symbol->getName());
Sam Cleggd99f6072017-06-12 23:52:44 +0000495 return IndirectSymbolIndices[RelEntry.Symbol];
496 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
Sam Clegg9d24fb72017-06-16 23:59:10 +0000497 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
Sam Clegg13a2e892017-09-01 17:32:01 +0000498 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
499 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
500 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
Sam Cleggb7787fd2017-06-20 04:04:59 +0000501 if (!SymbolIndices.count(RelEntry.Symbol))
Sam Clegg5e3d33a2017-07-07 02:01:29 +0000502 report_fatal_error("symbol not found function/global index space: " +
Sam Cleggb7787fd2017-06-20 04:04:59 +0000503 RelEntry.Symbol->getName());
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000504 return SymbolIndices[RelEntry.Symbol];
505 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
Sam Cleggb7787fd2017-06-20 04:04:59 +0000506 if (!TypeIndices.count(RelEntry.Symbol))
Sam Clegg5e3d33a2017-07-07 02:01:29 +0000507 report_fatal_error("symbol not found in type index space: " +
Sam Cleggb7787fd2017-06-20 04:04:59 +0000508 RelEntry.Symbol->getName());
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000509 return TypeIndices[RelEntry.Symbol];
510 default:
511 llvm_unreachable("invalid relocation type");
512 }
513}
514
Dan Gohmand934cb82017-02-24 23:18:00 +0000515// Apply the portions of the relocation records that we can handle ourselves
516// directly.
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000517void WasmObjectWriter::applyRelocations(
518 ArrayRef<WasmRelocationEntry> Relocations, uint64_t ContentsOffset) {
519 raw_pwrite_stream &Stream = getStream();
Dan Gohmand934cb82017-02-24 23:18:00 +0000520 for (const WasmRelocationEntry &RelEntry : Relocations) {
521 uint64_t Offset = ContentsOffset +
522 RelEntry.FixupSection->getSectionOffset() +
523 RelEntry.Offset;
Dan Gohmand934cb82017-02-24 23:18:00 +0000524
Sam Cleggb7787fd2017-06-20 04:04:59 +0000525 DEBUG(dbgs() << "applyRelocation: " << RelEntry << "\n");
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000526 switch (RelEntry.Type) {
527 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
528 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
Sam Clegg9d24fb72017-06-16 23:59:10 +0000529 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
530 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB: {
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000531 uint32_t Index = getRelocationIndexValue(RelEntry);
532 WritePatchableSLEB(Stream, Index, Offset);
Dan Gohmand934cb82017-02-24 23:18:00 +0000533 break;
534 }
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000535 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32: {
536 uint32_t Index = getRelocationIndexValue(RelEntry);
537 WriteI32(Stream, Index, Offset);
Dan Gohmand934cb82017-02-24 23:18:00 +0000538 break;
539 }
Sam Clegg13a2e892017-09-01 17:32:01 +0000540 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: {
Dan Gohmand934cb82017-02-24 23:18:00 +0000541 uint32_t Value = ProvisionalValue(RelEntry);
Dan Gohmand934cb82017-02-24 23:18:00 +0000542 WritePatchableSLEB(Stream, Value, Offset);
543 break;
544 }
Sam Clegg13a2e892017-09-01 17:32:01 +0000545 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB: {
Dan Gohmand934cb82017-02-24 23:18:00 +0000546 uint32_t Value = ProvisionalValue(RelEntry);
Dan Gohmand934cb82017-02-24 23:18:00 +0000547 WritePatchableLEB(Stream, Value, Offset);
548 break;
549 }
Sam Clegg13a2e892017-09-01 17:32:01 +0000550 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32: {
Dan Gohmand934cb82017-02-24 23:18:00 +0000551 uint32_t Value = ProvisionalValue(RelEntry);
Dan Gohmand934cb82017-02-24 23:18:00 +0000552 WriteI32(Stream, Value, Offset);
553 break;
554 }
555 default:
Sam Clegg9d24fb72017-06-16 23:59:10 +0000556 llvm_unreachable("invalid relocation type");
Dan Gohmand934cb82017-02-24 23:18:00 +0000557 }
558 }
Dan Gohman18eafb62017-02-22 01:23:18 +0000559}
560
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000561// Write out the portions of the relocation records that the linker will
Dan Gohman970d02c2017-03-30 23:58:19 +0000562// need to handle.
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000563void WasmObjectWriter::writeRelocations(
564 ArrayRef<WasmRelocationEntry> Relocations, uint64_t HeaderSize) {
565 raw_pwrite_stream &Stream = getStream();
566 for (const WasmRelocationEntry& RelEntry : Relocations) {
Dan Gohman970d02c2017-03-30 23:58:19 +0000567
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000568 uint64_t Offset = RelEntry.Offset +
569 RelEntry.FixupSection->getSectionOffset() + HeaderSize;
570 uint32_t Index = getRelocationIndexValue(RelEntry);
Dan Gohman970d02c2017-03-30 23:58:19 +0000571
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000572 encodeULEB128(RelEntry.Type, Stream);
Dan Gohman970d02c2017-03-30 23:58:19 +0000573 encodeULEB128(Offset, Stream);
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000574 encodeULEB128(Index, Stream);
575 if (RelEntry.hasAddend())
576 encodeSLEB128(RelEntry.Addend, Stream);
Dan Gohman970d02c2017-03-30 23:58:19 +0000577 }
578}
579
Sam Clegg9e15f352017-06-03 02:01:24 +0000580void WasmObjectWriter::writeTypeSection(
581 const SmallVector<WasmFunctionType, 4> &FunctionTypes) {
582 if (FunctionTypes.empty())
583 return;
584
585 SectionBookkeeping Section;
586 startSection(Section, wasm::WASM_SEC_TYPE);
587
588 encodeULEB128(FunctionTypes.size(), getStream());
589
590 for (const WasmFunctionType &FuncTy : FunctionTypes) {
591 encodeSLEB128(wasm::WASM_TYPE_FUNC, getStream());
592 encodeULEB128(FuncTy.Params.size(), getStream());
593 for (wasm::ValType Ty : FuncTy.Params)
594 writeValueType(Ty);
595 encodeULEB128(FuncTy.Returns.size(), getStream());
596 for (wasm::ValType Ty : FuncTy.Returns)
597 writeValueType(Ty);
598 }
599
600 endSection(Section);
601}
602
Sam Cleggb7787fd2017-06-20 04:04:59 +0000603
Sam Clegg9e15f352017-06-03 02:01:24 +0000604void WasmObjectWriter::writeImportSection(
605 const SmallVector<WasmImport, 4> &Imports) {
606 if (Imports.empty())
607 return;
608
609 SectionBookkeeping Section;
610 startSection(Section, wasm::WASM_SEC_IMPORT);
611
612 encodeULEB128(Imports.size(), getStream());
613 for (const WasmImport &Import : Imports) {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000614 writeString(Import.ModuleName);
615 writeString(Import.FieldName);
Sam Clegg9e15f352017-06-03 02:01:24 +0000616
617 encodeULEB128(Import.Kind, getStream());
618
619 switch (Import.Kind) {
620 case wasm::WASM_EXTERNAL_FUNCTION:
621 encodeULEB128(Import.Type, getStream());
622 break;
623 case wasm::WASM_EXTERNAL_GLOBAL:
624 encodeSLEB128(int32_t(Import.Type), getStream());
625 encodeULEB128(0, getStream()); // mutability
626 break;
627 default:
628 llvm_unreachable("unsupported import kind");
629 }
630 }
631
632 endSection(Section);
633}
634
635void WasmObjectWriter::writeFunctionSection(
636 const SmallVector<WasmFunction, 4> &Functions) {
637 if (Functions.empty())
638 return;
639
640 SectionBookkeeping Section;
641 startSection(Section, wasm::WASM_SEC_FUNCTION);
642
643 encodeULEB128(Functions.size(), getStream());
644 for (const WasmFunction &Func : Functions)
645 encodeULEB128(Func.Type, getStream());
646
647 endSection(Section);
648}
649
Sam Cleggd99f6072017-06-12 23:52:44 +0000650void WasmObjectWriter::writeTableSection(uint32_t NumElements) {
Sam Clegg9e15f352017-06-03 02:01:24 +0000651 // For now, always emit the table section, since indirect calls are not
652 // valid without it. In the future, we could perhaps be more clever and omit
653 // it if there are no indirect calls.
Sam Cleggd99f6072017-06-12 23:52:44 +0000654
Sam Clegg9e15f352017-06-03 02:01:24 +0000655 SectionBookkeeping Section;
656 startSection(Section, wasm::WASM_SEC_TABLE);
657
Sam Cleggd99f6072017-06-12 23:52:44 +0000658 encodeULEB128(1, getStream()); // The number of tables.
659 // Fixed to 1 for now.
660 encodeSLEB128(wasm::WASM_TYPE_ANYFUNC, getStream()); // Type of table
661 encodeULEB128(0, getStream()); // flags
662 encodeULEB128(NumElements, getStream()); // initial
Sam Clegg9e15f352017-06-03 02:01:24 +0000663
664 endSection(Section);
665}
666
667void WasmObjectWriter::writeMemorySection(
668 const SmallVector<char, 0> &DataBytes) {
669 // For now, always emit the memory section, since loads and stores are not
670 // valid without it. In the future, we could perhaps be more clever and omit
671 // it if there are no loads or stores.
672 SectionBookkeeping Section;
673 uint32_t NumPages =
674 (DataBytes.size() + wasm::WasmPageSize - 1) / wasm::WasmPageSize;
675
676 startSection(Section, wasm::WASM_SEC_MEMORY);
677 encodeULEB128(1, getStream()); // number of memory spaces
678
679 encodeULEB128(0, getStream()); // flags
680 encodeULEB128(NumPages, getStream()); // initial
681
682 endSection(Section);
683}
684
685void WasmObjectWriter::writeGlobalSection(
686 const SmallVector<WasmGlobal, 4> &Globals) {
687 if (Globals.empty())
688 return;
689
690 SectionBookkeeping Section;
691 startSection(Section, wasm::WASM_SEC_GLOBAL);
692
693 encodeULEB128(Globals.size(), getStream());
694 for (const WasmGlobal &Global : Globals) {
695 writeValueType(Global.Type);
696 write8(Global.IsMutable);
697
698 if (Global.HasImport) {
699 assert(Global.InitialValue == 0);
700 write8(wasm::WASM_OPCODE_GET_GLOBAL);
701 encodeULEB128(Global.ImportIndex, getStream());
702 } else {
703 assert(Global.ImportIndex == 0);
704 write8(wasm::WASM_OPCODE_I32_CONST);
705 encodeSLEB128(Global.InitialValue, getStream()); // offset
706 }
707 write8(wasm::WASM_OPCODE_END);
708 }
709
710 endSection(Section);
711}
712
713void WasmObjectWriter::writeExportSection(
714 const SmallVector<WasmExport, 4> &Exports) {
715 if (Exports.empty())
716 return;
717
718 SectionBookkeeping Section;
719 startSection(Section, wasm::WASM_SEC_EXPORT);
720
721 encodeULEB128(Exports.size(), getStream());
722 for (const WasmExport &Export : Exports) {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000723 writeString(Export.FieldName);
Sam Clegg9e15f352017-06-03 02:01:24 +0000724 encodeSLEB128(Export.Kind, getStream());
Sam Clegg9e15f352017-06-03 02:01:24 +0000725 encodeULEB128(Export.Index, getStream());
726 }
727
728 endSection(Section);
729}
730
731void WasmObjectWriter::writeElemSection(
732 const SmallVector<uint32_t, 4> &TableElems) {
733 if (TableElems.empty())
734 return;
735
736 SectionBookkeeping Section;
737 startSection(Section, wasm::WASM_SEC_ELEM);
738
739 encodeULEB128(1, getStream()); // number of "segments"
740 encodeULEB128(0, getStream()); // the table index
741
742 // init expr for starting offset
743 write8(wasm::WASM_OPCODE_I32_CONST);
744 encodeSLEB128(0, getStream());
745 write8(wasm::WASM_OPCODE_END);
746
747 encodeULEB128(TableElems.size(), getStream());
748 for (uint32_t Elem : TableElems)
749 encodeULEB128(Elem, getStream());
750
751 endSection(Section);
752}
753
754void WasmObjectWriter::writeCodeSection(
755 const MCAssembler &Asm, const MCAsmLayout &Layout,
Sam Clegg9e15f352017-06-03 02:01:24 +0000756 const SmallVector<WasmFunction, 4> &Functions) {
757 if (Functions.empty())
758 return;
759
760 SectionBookkeeping Section;
761 startSection(Section, wasm::WASM_SEC_CODE);
762
763 encodeULEB128(Functions.size(), getStream());
764
765 for (const WasmFunction &Func : Functions) {
Sam Cleggfe6414b2017-06-21 23:46:41 +0000766 auto &FuncSection = static_cast<MCSectionWasm &>(Func.Sym->getSection());
Sam Clegg9e15f352017-06-03 02:01:24 +0000767
Sam Clegg9e15f352017-06-03 02:01:24 +0000768 int64_t Size = 0;
769 if (!Func.Sym->getSize()->evaluateAsAbsolute(Size, Layout))
770 report_fatal_error(".size expression must be evaluatable");
771
772 encodeULEB128(Size, getStream());
773
Sam Cleggfe6414b2017-06-21 23:46:41 +0000774 FuncSection.setSectionOffset(getStream().tell() - Section.ContentsOffset);
Sam Clegg9e15f352017-06-03 02:01:24 +0000775
776 Asm.writeSectionData(&FuncSection, Layout);
777 }
778
Sam Clegg9e15f352017-06-03 02:01:24 +0000779 // Apply fixups.
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000780 applyRelocations(CodeRelocations, Section.ContentsOffset);
Sam Clegg9e15f352017-06-03 02:01:24 +0000781
782 endSection(Section);
783}
784
785uint64_t WasmObjectWriter::writeDataSection(
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000786 const SmallVector<char, 0> &DataBytes) {
Sam Clegg9e15f352017-06-03 02:01:24 +0000787 if (DataBytes.empty())
788 return 0;
789
790 SectionBookkeeping Section;
791 startSection(Section, wasm::WASM_SEC_DATA);
792
793 encodeULEB128(1, getStream()); // count
794 encodeULEB128(0, getStream()); // memory index
795 write8(wasm::WASM_OPCODE_I32_CONST);
796 encodeSLEB128(0, getStream()); // offset
797 write8(wasm::WASM_OPCODE_END);
798 encodeULEB128(DataBytes.size(), getStream()); // size
799 uint32_t HeaderSize = getStream().tell() - Section.ContentsOffset;
800 writeBytes(DataBytes); // data
801
802 // Apply fixups.
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000803 applyRelocations(DataRelocations, Section.ContentsOffset + HeaderSize);
Sam Clegg9e15f352017-06-03 02:01:24 +0000804
805 endSection(Section);
806 return HeaderSize;
807}
808
809void WasmObjectWriter::writeNameSection(
810 const SmallVector<WasmFunction, 4> &Functions,
811 const SmallVector<WasmImport, 4> &Imports,
812 unsigned NumFuncImports) {
813 uint32_t TotalFunctions = NumFuncImports + Functions.size();
814 if (TotalFunctions == 0)
815 return;
816
817 SectionBookkeeping Section;
818 startSection(Section, wasm::WASM_SEC_CUSTOM, "name");
819 SectionBookkeeping SubSection;
820 startSection(SubSection, wasm::WASM_NAMES_FUNCTION);
821
822 encodeULEB128(TotalFunctions, getStream());
823 uint32_t Index = 0;
824 for (const WasmImport &Import : Imports) {
825 if (Import.Kind == wasm::WASM_EXTERNAL_FUNCTION) {
826 encodeULEB128(Index, getStream());
Sam Cleggb7787fd2017-06-20 04:04:59 +0000827 writeString(Import.FieldName);
Sam Clegg9e15f352017-06-03 02:01:24 +0000828 ++Index;
829 }
830 }
831 for (const WasmFunction &Func : Functions) {
832 encodeULEB128(Index, getStream());
Sam Cleggb7787fd2017-06-20 04:04:59 +0000833 writeString(Func.Sym->getName());
Sam Clegg9e15f352017-06-03 02:01:24 +0000834 ++Index;
835 }
836
837 endSection(SubSection);
838 endSection(Section);
839}
840
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000841void WasmObjectWriter::writeCodeRelocSection() {
Sam Clegg9e15f352017-06-03 02:01:24 +0000842 // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
843 // for descriptions of the reloc sections.
844
845 if (CodeRelocations.empty())
846 return;
847
848 SectionBookkeeping Section;
849 startSection(Section, wasm::WASM_SEC_CUSTOM, "reloc.CODE");
850
851 encodeULEB128(wasm::WASM_SEC_CODE, getStream());
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000852 encodeULEB128(CodeRelocations.size(), getStream());
Sam Clegg9e15f352017-06-03 02:01:24 +0000853
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000854 writeRelocations(CodeRelocations, 0);
Sam Clegg9e15f352017-06-03 02:01:24 +0000855
856 endSection(Section);
857}
858
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000859void WasmObjectWriter::writeDataRelocSection(uint64_t DataSectionHeaderSize) {
Sam Clegg9e15f352017-06-03 02:01:24 +0000860 // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
861 // for descriptions of the reloc sections.
862
863 if (DataRelocations.empty())
864 return;
865
866 SectionBookkeeping Section;
867 startSection(Section, wasm::WASM_SEC_CUSTOM, "reloc.DATA");
868
869 encodeULEB128(wasm::WASM_SEC_DATA, getStream());
870 encodeULEB128(DataRelocations.size(), getStream());
871
Sam Cleggacd7d2b2017-06-06 19:15:05 +0000872 writeRelocations(DataRelocations, DataSectionHeaderSize);
Sam Clegg9e15f352017-06-03 02:01:24 +0000873
874 endSection(Section);
875}
876
877void WasmObjectWriter::writeLinkingMetaDataSection(
Sam Clegg9e1ade92017-06-27 20:27:59 +0000878 uint32_t DataSize, uint32_t DataAlignment, ArrayRef<StringRef> WeakSymbols,
879 bool HasStackPointer, uint32_t StackPointerGlobal) {
Sam Clegg9e15f352017-06-03 02:01:24 +0000880 SectionBookkeeping Section;
881 startSection(Section, wasm::WASM_SEC_CUSTOM, "linking");
Sam Cleggb7787fd2017-06-20 04:04:59 +0000882 SectionBookkeeping SubSection;
Sam Clegg9e15f352017-06-03 02:01:24 +0000883
Sam Cleggb7787fd2017-06-20 04:04:59 +0000884 if (HasStackPointer) {
885 startSection(SubSection, wasm::WASM_STACK_POINTER);
886 encodeULEB128(StackPointerGlobal, getStream()); // id
887 endSection(SubSection);
888 }
Sam Clegg9e15f352017-06-03 02:01:24 +0000889
Sam Cleggb7787fd2017-06-20 04:04:59 +0000890 if (WeakSymbols.size() != 0) {
891 startSection(SubSection, wasm::WASM_SYMBOL_INFO);
892 encodeULEB128(WeakSymbols.size(), getStream());
893 for (const StringRef Export: WeakSymbols) {
894 writeString(Export);
895 encodeULEB128(wasm::WASM_SYMBOL_FLAG_WEAK, getStream());
896 }
897 endSection(SubSection);
898 }
Sam Clegg9e15f352017-06-03 02:01:24 +0000899
Sam Clegg9e1ade92017-06-27 20:27:59 +0000900 if (DataSize > 0) {
901 startSection(SubSection, wasm::WASM_DATA_SIZE);
902 encodeULEB128(DataSize, getStream());
903 endSection(SubSection);
904
905 startSection(SubSection, wasm::WASM_DATA_ALIGNMENT);
906 encodeULEB128(DataAlignment, getStream());
907 endSection(SubSection);
908 }
909
Sam Clegg9e15f352017-06-03 02:01:24 +0000910 endSection(Section);
911}
912
Sam Clegg5e3d33a2017-07-07 02:01:29 +0000913uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm& Symbol) {
914 assert(Symbol.isFunction());
915 assert(TypeIndices.count(&Symbol));
916 return TypeIndices[&Symbol];
917}
918
919uint32_t WasmObjectWriter::registerFunctionType(const MCSymbolWasm& Symbol) {
920 assert(Symbol.isFunction());
921
922 WasmFunctionType F;
923 if (Symbol.isVariable()) {
924 const MCExpr *Expr = Symbol.getVariableValue();
Sam Clegg6ad8f192017-07-11 02:21:57 +0000925 auto *Inner = cast<MCSymbolRefExpr>(Expr);
Sam Clegg5e3d33a2017-07-07 02:01:29 +0000926 const auto *ResolvedSym = cast<MCSymbolWasm>(&Inner->getSymbol());
927 F.Returns = ResolvedSym->getReturns();
928 F.Params = ResolvedSym->getParams();
929 } else {
930 F.Returns = Symbol.getReturns();
931 F.Params = Symbol.getParams();
932 }
933
934 auto Pair =
935 FunctionTypeIndices.insert(std::make_pair(F, FunctionTypes.size()));
936 if (Pair.second)
937 FunctionTypes.push_back(F);
938 TypeIndices[&Symbol] = Pair.first->second;
939
940 DEBUG(dbgs() << "registerFunctionType: " << Symbol << " new:" << Pair.second << "\n");
941 DEBUG(dbgs() << " -> type index: " << Pair.first->second << "\n");
942 return Pair.first->second;
943}
944
Dan Gohman18eafb62017-02-22 01:23:18 +0000945void WasmObjectWriter::writeObject(MCAssembler &Asm,
946 const MCAsmLayout &Layout) {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000947 DEBUG(dbgs() << "WasmObjectWriter::writeObject\n");
Dan Gohman82607f52017-02-24 23:46:05 +0000948 MCContext &Ctx = Asm.getContext();
Derek Schuffb8795392017-03-16 20:49:48 +0000949 wasm::ValType PtrType = is64Bit() ? wasm::ValType::I64 : wasm::ValType::I32;
Dan Gohmand934cb82017-02-24 23:18:00 +0000950
951 // Collect information from the available symbols.
Dan Gohmand934cb82017-02-24 23:18:00 +0000952 SmallVector<WasmFunction, 4> Functions;
953 SmallVector<uint32_t, 4> TableElems;
954 SmallVector<WasmGlobal, 4> Globals;
955 SmallVector<WasmImport, 4> Imports;
956 SmallVector<WasmExport, 4> Exports;
Sam Cleggb7787fd2017-06-20 04:04:59 +0000957 SmallVector<StringRef, 4> WeakSymbols;
Dan Gohmand934cb82017-02-24 23:18:00 +0000958 SmallPtrSet<const MCSymbolWasm *, 4> IsAddressTaken;
959 unsigned NumFuncImports = 0;
960 unsigned NumGlobalImports = 0;
961 SmallVector<char, 0> DataBytes;
Sam Clegg9e1ade92017-06-27 20:27:59 +0000962 uint32_t DataAlignment = 1;
Dan Gohman970d02c2017-03-30 23:58:19 +0000963 uint32_t StackPointerGlobal = 0;
964 bool HasStackPointer = false;
Dan Gohmand934cb82017-02-24 23:18:00 +0000965
966 // Populate the IsAddressTaken set.
Sam Cleggb7787fd2017-06-20 04:04:59 +0000967 for (const WasmRelocationEntry &RelEntry : CodeRelocations) {
Dan Gohmand934cb82017-02-24 23:18:00 +0000968 switch (RelEntry.Type) {
969 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
Sam Clegg13a2e892017-09-01 17:32:01 +0000970 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
Dan Gohmand934cb82017-02-24 23:18:00 +0000971 IsAddressTaken.insert(RelEntry.Symbol);
972 break;
973 default:
974 break;
975 }
976 }
Sam Cleggb7787fd2017-06-20 04:04:59 +0000977 for (const WasmRelocationEntry &RelEntry : DataRelocations) {
Dan Gohmand934cb82017-02-24 23:18:00 +0000978 switch (RelEntry.Type) {
979 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
Sam Clegg13a2e892017-09-01 17:32:01 +0000980 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
Dan Gohmand934cb82017-02-24 23:18:00 +0000981 IsAddressTaken.insert(RelEntry.Symbol);
982 break;
983 default:
984 break;
985 }
986 }
987
988 // Populate the Imports set.
989 for (const MCSymbol &S : Asm.symbols()) {
Sam Clegg5e3d33a2017-07-07 02:01:29 +0000990 const auto &WS = static_cast<const MCSymbolWasm &>(S);
991
992 if (WS.isTemporary())
Sam Clegg8c4baa002017-07-05 20:09:26 +0000993 continue;
994
Sam Clegg5e3d33a2017-07-07 02:01:29 +0000995 if (WS.isFunction())
996 registerFunctionType(WS);
Dan Gohmand934cb82017-02-24 23:18:00 +0000997
998 // If the symbol is not defined in this translation unit, import it.
Sam Clegg5e3d33a2017-07-07 02:01:29 +0000999 if (!WS.isDefined(/*SetUsed=*/false) || WS.isVariable()) {
Dan Gohmand934cb82017-02-24 23:18:00 +00001000 WasmImport Import;
1001 Import.ModuleName = WS.getModuleName();
1002 Import.FieldName = WS.getName();
1003
1004 if (WS.isFunction()) {
1005 Import.Kind = wasm::WASM_EXTERNAL_FUNCTION;
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001006 Import.Type = getFunctionType(WS);
Dan Gohmand934cb82017-02-24 23:18:00 +00001007 SymbolIndices[&WS] = NumFuncImports;
1008 ++NumFuncImports;
1009 } else {
1010 Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001011 Import.Type = int32_t(PtrType);
Dan Gohmand934cb82017-02-24 23:18:00 +00001012 SymbolIndices[&WS] = NumGlobalImports;
1013 ++NumGlobalImports;
1014 }
1015
1016 Imports.push_back(Import);
1017 }
1018 }
1019
Dan Gohman82607f52017-02-24 23:46:05 +00001020 // In the special .global_variables section, we've encoded global
1021 // variables used by the function. Translate them into the Globals
1022 // list.
1023 MCSectionWasm *GlobalVars = Ctx.getWasmSection(".global_variables", 0, 0);
1024 if (!GlobalVars->getFragmentList().empty()) {
1025 if (GlobalVars->getFragmentList().size() != 1)
1026 report_fatal_error("only one .global_variables fragment supported");
1027 const MCFragment &Frag = *GlobalVars->begin();
1028 if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
1029 report_fatal_error("only data supported in .global_variables");
Sam Cleggfe6414b2017-06-21 23:46:41 +00001030 const auto &DataFrag = cast<MCDataFragment>(Frag);
Dan Gohman82607f52017-02-24 23:46:05 +00001031 if (!DataFrag.getFixups().empty())
1032 report_fatal_error("fixups not supported in .global_variables");
1033 const SmallVectorImpl<char> &Contents = DataFrag.getContents();
Dan Gohman970d02c2017-03-30 23:58:19 +00001034 for (const uint8_t *p = (const uint8_t *)Contents.data(),
1035 *end = (const uint8_t *)Contents.data() + Contents.size();
1036 p != end; ) {
Dan Gohman82607f52017-02-24 23:46:05 +00001037 WasmGlobal G;
Dan Gohman970d02c2017-03-30 23:58:19 +00001038 if (end - p < 3)
1039 report_fatal_error("truncated global variable encoding");
1040 G.Type = wasm::ValType(int8_t(*p++));
1041 G.IsMutable = bool(*p++);
1042 G.HasImport = bool(*p++);
1043 if (G.HasImport) {
1044 G.InitialValue = 0;
1045
1046 WasmImport Import;
1047 Import.ModuleName = (const char *)p;
1048 const uint8_t *nul = (const uint8_t *)memchr(p, '\0', end - p);
1049 if (!nul)
1050 report_fatal_error("global module name must be nul-terminated");
1051 p = nul + 1;
1052 nul = (const uint8_t *)memchr(p, '\0', end - p);
1053 if (!nul)
1054 report_fatal_error("global base name must be nul-terminated");
1055 Import.FieldName = (const char *)p;
1056 p = nul + 1;
1057
1058 Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
1059 Import.Type = int32_t(G.Type);
1060
1061 G.ImportIndex = NumGlobalImports;
1062 ++NumGlobalImports;
1063
1064 Imports.push_back(Import);
1065 } else {
1066 unsigned n;
1067 G.InitialValue = decodeSLEB128(p, &n);
1068 G.ImportIndex = 0;
Simon Pilgrimc8da0c02017-03-31 10:45:35 +00001069 if ((ptrdiff_t)n > end - p)
Dan Gohman970d02c2017-03-30 23:58:19 +00001070 report_fatal_error("global initial value must be valid SLEB128");
1071 p += n;
1072 }
Dan Gohman82607f52017-02-24 23:46:05 +00001073 Globals.push_back(G);
1074 }
1075 }
1076
Dan Gohman970d02c2017-03-30 23:58:19 +00001077 // In the special .stack_pointer section, we've encoded the stack pointer
1078 // index.
1079 MCSectionWasm *StackPtr = Ctx.getWasmSection(".stack_pointer", 0, 0);
1080 if (!StackPtr->getFragmentList().empty()) {
1081 if (StackPtr->getFragmentList().size() != 1)
1082 report_fatal_error("only one .stack_pointer fragment supported");
1083 const MCFragment &Frag = *StackPtr->begin();
1084 if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
1085 report_fatal_error("only data supported in .stack_pointer");
Sam Cleggfe6414b2017-06-21 23:46:41 +00001086 const auto &DataFrag = cast<MCDataFragment>(Frag);
Dan Gohman970d02c2017-03-30 23:58:19 +00001087 if (!DataFrag.getFixups().empty())
1088 report_fatal_error("fixups not supported in .stack_pointer");
1089 const SmallVectorImpl<char> &Contents = DataFrag.getContents();
1090 if (Contents.size() != 4)
1091 report_fatal_error("only one entry supported in .stack_pointer");
1092 HasStackPointer = true;
1093 StackPointerGlobal = NumGlobalImports + *(const int32_t *)Contents.data();
1094 }
1095
Sam Cleggb7787fd2017-06-20 04:04:59 +00001096 // Handle regular defined and undefined symbols.
Dan Gohmand934cb82017-02-24 23:18:00 +00001097 for (const MCSymbol &S : Asm.symbols()) {
1098 // Ignore unnamed temporary symbols, which aren't ever exported, imported,
1099 // or used in relocations.
1100 if (S.isTemporary() && S.getName().empty())
1101 continue;
Sam Cleggb7787fd2017-06-20 04:04:59 +00001102
Dan Gohmand934cb82017-02-24 23:18:00 +00001103 const auto &WS = static_cast<const MCSymbolWasm &>(S);
Sam Cleggb7787fd2017-06-20 04:04:59 +00001104 DEBUG(dbgs() << "MCSymbol: '" << S << "'"
1105 << " isDefined=" << S.isDefined() << " isExternal="
1106 << S.isExternal() << " isTemporary=" << S.isTemporary()
1107 << " isFunction=" << WS.isFunction()
1108 << " isWeak=" << WS.isWeak()
1109 << " isVariable=" << WS.isVariable() << "\n");
1110
1111 if (WS.isWeak())
1112 WeakSymbols.push_back(WS.getName());
1113
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001114 if (WS.isVariable())
1115 continue;
1116
Dan Gohmand934cb82017-02-24 23:18:00 +00001117 unsigned Index;
Sam Cleggb7787fd2017-06-20 04:04:59 +00001118
Dan Gohmand934cb82017-02-24 23:18:00 +00001119 if (WS.isFunction()) {
Dan Gohmand934cb82017-02-24 23:18:00 +00001120 if (WS.isDefined(/*SetUsed=*/false)) {
Sam Cleggb7787fd2017-06-20 04:04:59 +00001121 if (WS.getOffset() != 0)
1122 report_fatal_error(
1123 "function sections must contain one function each");
1124
1125 if (WS.getSize() == 0)
1126 report_fatal_error(
1127 "function symbols must have a size set with .size");
1128
Dan Gohmand934cb82017-02-24 23:18:00 +00001129 // A definition. Take the next available index.
1130 Index = NumFuncImports + Functions.size();
1131
1132 // Prepare the function.
1133 WasmFunction Func;
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001134 Func.Type = getFunctionType(WS);
Dan Gohmand934cb82017-02-24 23:18:00 +00001135 Func.Sym = &WS;
1136 SymbolIndices[&WS] = Index;
1137 Functions.push_back(Func);
1138 } else {
1139 // An import; the index was assigned above.
1140 Index = SymbolIndices.find(&WS)->second;
1141 }
1142
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001143 DEBUG(dbgs() << " -> function index: " << Index << "\n");
1144
Dan Gohmand934cb82017-02-24 23:18:00 +00001145 // If needed, prepare the function to be called indirectly.
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001146 if (IsAddressTaken.count(&WS) != 0) {
Sam Cleggd99f6072017-06-12 23:52:44 +00001147 IndirectSymbolIndices[&WS] = TableElems.size();
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001148 DEBUG(dbgs() << " -> adding to table: " << TableElems.size() << "\n");
Dan Gohmand934cb82017-02-24 23:18:00 +00001149 TableElems.push_back(Index);
Sam Cleggd99f6072017-06-12 23:52:44 +00001150 }
Dan Gohmand934cb82017-02-24 23:18:00 +00001151 } else {
Sam Cleggc38e9472017-06-02 01:05:24 +00001152 if (WS.isTemporary() && !WS.getSize())
1153 continue;
Dan Gohmand934cb82017-02-24 23:18:00 +00001154
Sam Cleggfe6414b2017-06-21 23:46:41 +00001155 if (!WS.isDefined(/*SetUsed=*/false))
1156 continue;
Sam Cleggc38e9472017-06-02 01:05:24 +00001157
Sam Cleggfe6414b2017-06-21 23:46:41 +00001158 if (WS.getOffset() != 0)
1159 report_fatal_error("data sections must contain one variable each: " +
1160 WS.getName());
1161 if (!WS.getSize())
1162 report_fatal_error("data symbols must have a size set with .size: " +
1163 WS.getName());
Sam Cleggc38e9472017-06-02 01:05:24 +00001164
Sam Cleggfe6414b2017-06-21 23:46:41 +00001165 int64_t Size = 0;
1166 if (!WS.getSize()->evaluateAsAbsolute(Size, Layout))
1167 report_fatal_error(".size expression must be evaluatable");
Dan Gohmand934cb82017-02-24 23:18:00 +00001168
Sam Cleggfe6414b2017-06-21 23:46:41 +00001169 auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection());
Dan Gohmand934cb82017-02-24 23:18:00 +00001170
Sam Cleggfe6414b2017-06-21 23:46:41 +00001171 if (uint64_t(Size) != Layout.getSectionFileSize(&DataSection))
1172 report_fatal_error("data sections must contain at most one variable");
Dan Gohmand934cb82017-02-24 23:18:00 +00001173
Sam Cleggfe6414b2017-06-21 23:46:41 +00001174 DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment()));
Sam Clegg9e1ade92017-06-27 20:27:59 +00001175 DataAlignment = std::max(DataAlignment, DataSection.getAlignment());
Dan Gohmand934cb82017-02-24 23:18:00 +00001176
Sam Cleggfe6414b2017-06-21 23:46:41 +00001177 DataSection.setSectionOffset(DataBytes.size());
Dan Gohmand934cb82017-02-24 23:18:00 +00001178
Sam Cleggfe6414b2017-06-21 23:46:41 +00001179 for (const MCFragment &Frag : DataSection) {
1180 if (Frag.hasInstructions())
1181 report_fatal_error("only data supported in data sections");
Dan Gohmand934cb82017-02-24 23:18:00 +00001182
Sam Cleggfe6414b2017-06-21 23:46:41 +00001183 if (auto *Align = dyn_cast<MCAlignFragment>(&Frag)) {
1184 if (Align->getValueSize() != 1)
1185 report_fatal_error("only byte values supported for alignment");
1186 // If nops are requested, use zeros, as this is the data section.
1187 uint8_t Value = Align->hasEmitNops() ? 0 : Align->getValue();
1188 uint64_t Size = std::min<uint64_t>(alignTo(DataBytes.size(),
1189 Align->getAlignment()),
1190 DataBytes.size() +
1191 Align->getMaxBytesToEmit());
1192 DataBytes.resize(Size, Value);
1193 } else if (auto *Fill = dyn_cast<MCFillFragment>(&Frag)) {
Sam Cleggb03c2b42017-07-10 18:36:34 +00001194 DataBytes.insert(DataBytes.end(), Fill->getSize(), Fill->getValue());
Sam Cleggfe6414b2017-06-21 23:46:41 +00001195 } else {
1196 const auto &DataFrag = cast<MCDataFragment>(Frag);
1197 const SmallVectorImpl<char> &Contents = DataFrag.getContents();
1198
1199 DataBytes.insert(DataBytes.end(), Contents.begin(), Contents.end());
Dan Gohmand934cb82017-02-24 23:18:00 +00001200 }
Dan Gohmand934cb82017-02-24 23:18:00 +00001201 }
Sam Cleggfe6414b2017-06-21 23:46:41 +00001202
1203 // For each global, prepare a corresponding wasm global holding its
1204 // address. For externals these will also be named exports.
1205 Index = NumGlobalImports + Globals.size();
1206
1207 WasmGlobal Global;
1208 Global.Type = PtrType;
1209 Global.IsMutable = false;
1210 Global.HasImport = false;
1211 Global.InitialValue = DataSection.getSectionOffset();
1212 Global.ImportIndex = 0;
1213 SymbolIndices[&WS] = Index;
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001214 DEBUG(dbgs() << " -> global index: " << Index << "\n");
Sam Cleggfe6414b2017-06-21 23:46:41 +00001215 Globals.push_back(Global);
Dan Gohmand934cb82017-02-24 23:18:00 +00001216 }
1217
1218 // If the symbol is visible outside this translation unit, export it.
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001219 if ((WS.isExternal() && WS.isDefined(/*SetUsed=*/false))) {
Dan Gohmand934cb82017-02-24 23:18:00 +00001220 WasmExport Export;
1221 Export.FieldName = WS.getName();
1222 Export.Index = Index;
Dan Gohmand934cb82017-02-24 23:18:00 +00001223 if (WS.isFunction())
1224 Export.Kind = wasm::WASM_EXTERNAL_FUNCTION;
1225 else
1226 Export.Kind = wasm::WASM_EXTERNAL_GLOBAL;
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001227 DEBUG(dbgs() << " -> export " << Exports.size() << "\n");
Dan Gohmand934cb82017-02-24 23:18:00 +00001228 Exports.push_back(Export);
1229 }
1230 }
1231
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001232 // Handle weak aliases. We need to process these in a separate pass because
1233 // we need to have processed the target of the alias before the alias itself
1234 // and the symbols are not necessarily ordered in this way.
Sam Cleggb7787fd2017-06-20 04:04:59 +00001235 for (const MCSymbol &S : Asm.symbols()) {
1236 if (!S.isVariable())
1237 continue;
Sam Cleggb7787fd2017-06-20 04:04:59 +00001238 assert(S.isDefined(/*SetUsed=*/false));
1239
1240 const auto &WS = static_cast<const MCSymbolWasm &>(S);
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001241 // Find the target symbol of this weak alias and export that index
Sam Cleggb7787fd2017-06-20 04:04:59 +00001242 const MCExpr *Expr = WS.getVariableValue();
Sam Clegg6ad8f192017-07-11 02:21:57 +00001243 auto *Inner = cast<MCSymbolRefExpr>(Expr);
Sam Cleggfe6414b2017-06-21 23:46:41 +00001244 const auto *ResolvedSym = cast<MCSymbolWasm>(&Inner->getSymbol());
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001245 DEBUG(dbgs() << WS.getName() << ": weak alias of '" << *ResolvedSym << "'\n");
1246 assert(SymbolIndices.count(ResolvedSym) > 0);
Sam Cleggb7787fd2017-06-20 04:04:59 +00001247 uint32_t Index = SymbolIndices.find(ResolvedSym)->second;
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001248 DEBUG(dbgs() << " -> index:" << Index << "\n");
Sam Cleggb7787fd2017-06-20 04:04:59 +00001249
1250 WasmExport Export;
1251 Export.FieldName = WS.getName();
1252 Export.Index = Index;
1253 if (WS.isFunction())
1254 Export.Kind = wasm::WASM_EXTERNAL_FUNCTION;
1255 else
1256 Export.Kind = wasm::WASM_EXTERNAL_GLOBAL;
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001257 DEBUG(dbgs() << " -> export " << Exports.size() << "\n");
Sam Cleggb7787fd2017-06-20 04:04:59 +00001258 Exports.push_back(Export);
1259 }
1260
Dan Gohmand934cb82017-02-24 23:18:00 +00001261 // Add types for indirect function calls.
Sam Cleggacd7d2b2017-06-06 19:15:05 +00001262 for (const WasmRelocationEntry &Fixup : CodeRelocations) {
1263 if (Fixup.Type != wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB)
1264 continue;
Dan Gohman970d02c2017-03-30 23:58:19 +00001265
Sam Clegg5e3d33a2017-07-07 02:01:29 +00001266 registerFunctionType(*Fixup.Symbol);
Dan Gohmand934cb82017-02-24 23:18:00 +00001267 }
1268
Dan Gohman18eafb62017-02-22 01:23:18 +00001269 // Write out the Wasm header.
1270 writeHeader(Asm);
1271
Sam Clegg9e15f352017-06-03 02:01:24 +00001272 writeTypeSection(FunctionTypes);
1273 writeImportSection(Imports);
1274 writeFunctionSection(Functions);
Sam Cleggd99f6072017-06-12 23:52:44 +00001275 writeTableSection(TableElems.size());
Sam Clegg9e15f352017-06-03 02:01:24 +00001276 writeMemorySection(DataBytes);
1277 writeGlobalSection(Globals);
1278 writeExportSection(Exports);
1279 // TODO: Start Section
1280 writeElemSection(TableElems);
Sam Cleggacd7d2b2017-06-06 19:15:05 +00001281 writeCodeSection(Asm, Layout, Functions);
1282 uint64_t DataSectionHeaderSize = writeDataSection(DataBytes);
Sam Clegg9e15f352017-06-03 02:01:24 +00001283 writeNameSection(Functions, Imports, NumFuncImports);
Sam Cleggacd7d2b2017-06-06 19:15:05 +00001284 writeCodeRelocSection();
1285 writeDataRelocSection(DataSectionHeaderSize);
Sam Clegg9e1ade92017-06-27 20:27:59 +00001286 writeLinkingMetaDataSection(DataBytes.size(), DataAlignment, WeakSymbols, HasStackPointer, StackPointerGlobal);
Dan Gohman970d02c2017-03-30 23:58:19 +00001287
Dan Gohmand934cb82017-02-24 23:18:00 +00001288 // TODO: Translate the .comment section to the output.
Dan Gohmand934cb82017-02-24 23:18:00 +00001289 // TODO: Translate debug sections to the output.
Dan Gohman18eafb62017-02-22 01:23:18 +00001290}
1291
1292MCObjectWriter *llvm::createWasmObjectWriter(MCWasmObjectTargetWriter *MOTW,
1293 raw_pwrite_stream &OS) {
1294 return new WasmObjectWriter(MOTW, OS);
1295}