annotate lib/MC/WasmObjectWriter.cpp @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents
children 3a76565eade5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1 //===- lib/MC/WasmObjectWriter.cpp - Wasm File Writer ---------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
2 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
4 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
7 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
9 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 // This file implements Wasm object file writer information.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
12 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
13
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
14 #include "llvm/ADT/STLExtras.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
15 #include "llvm/ADT/SmallPtrSet.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16 #include "llvm/BinaryFormat/Wasm.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17 #include "llvm/MC/MCAsmBackend.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18 #include "llvm/MC/MCAsmInfo.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19 #include "llvm/MC/MCAsmLayout.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20 #include "llvm/MC/MCAssembler.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21 #include "llvm/MC/MCContext.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
22 #include "llvm/MC/MCExpr.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23 #include "llvm/MC/MCFixupKindInfo.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24 #include "llvm/MC/MCObjectFileInfo.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25 #include "llvm/MC/MCObjectWriter.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26 #include "llvm/MC/MCSectionWasm.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27 #include "llvm/MC/MCSymbolWasm.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28 #include "llvm/MC/MCValue.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29 #include "llvm/MC/MCWasmObjectWriter.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30 #include "llvm/Support/Casting.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31 #include "llvm/Support/Debug.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32 #include "llvm/Support/ErrorHandling.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33 #include "llvm/Support/LEB128.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34 #include "llvm/Support/StringSaver.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35 #include <vector>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
36
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37 using namespace llvm;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39 #define DEBUG_TYPE "mc"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41 namespace {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43 // For patching purposes, we need to remember where each section starts, both
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44 // for patching up the section size field, and for patching up references to
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45 // locations within the section.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46 struct SectionBookkeeping {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47 // Where the size of the section is written.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48 uint64_t SizeOffset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
49 // Where the contents of the section starts (after the header).
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50 uint64_t ContentsOffset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 // The signature of a wasm function, in a struct capable of being used as a
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54 // DenseMap key.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
55 struct WasmFunctionType {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56 // Support empty and tombstone instances, needed by DenseMap.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
57 enum { Plain, Empty, Tombstone } State;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59 // The return types of the function.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60 SmallVector<wasm::ValType, 1> Returns;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62 // The parameter types of the function.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
63 SmallVector<wasm::ValType, 4> Params;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
64
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
65 WasmFunctionType() : State(Plain) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
67 bool operator==(const WasmFunctionType &Other) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
68 return State == Other.State && Returns == Other.Returns &&
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
69 Params == Other.Params;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
70 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
71 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
72
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
73 // Traits for using WasmFunctionType in a DenseMap.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
74 struct WasmFunctionTypeDenseMapInfo {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
75 static WasmFunctionType getEmptyKey() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
76 WasmFunctionType FuncTy;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
77 FuncTy.State = WasmFunctionType::Empty;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
78 return FuncTy;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
79 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
80 static WasmFunctionType getTombstoneKey() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
81 WasmFunctionType FuncTy;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
82 FuncTy.State = WasmFunctionType::Tombstone;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
83 return FuncTy;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
84 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
85 static unsigned getHashValue(const WasmFunctionType &FuncTy) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
86 uintptr_t Value = FuncTy.State;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
87 for (wasm::ValType Ret : FuncTy.Returns)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
88 Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Ret));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
89 for (wasm::ValType Param : FuncTy.Params)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
90 Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Param));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
91 return Value;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
92 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
93 static bool isEqual(const WasmFunctionType &LHS,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
94 const WasmFunctionType &RHS) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
95 return LHS == RHS;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
96 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
97 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
98
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
99 // A wasm data segment. A wasm binary contains only a single data section
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
100 // but that can contain many segments, each with their own virtual location
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
101 // in memory. Each MCSection data created by llvm is modeled as its own
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
102 // wasm data segment.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
103 struct WasmDataSegment {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
104 MCSectionWasm *Section;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
105 StringRef Name;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
106 uint32_t Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
107 uint32_t Alignment;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
108 uint32_t Flags;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
109 SmallVector<char, 4> Data;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
110 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
111
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
112 // A wasm import to be written into the import section.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
113 struct WasmImport {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
114 StringRef ModuleName;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
115 StringRef FieldName;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
116 unsigned Kind;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
117 int32_t Type;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
118 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
119
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
120 // A wasm function to be written into the function section.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
121 struct WasmFunction {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
122 int32_t Type;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
123 const MCSymbolWasm *Sym;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
124 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
125
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
126 // A wasm export to be written into the export section.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
127 struct WasmExport {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
128 StringRef FieldName;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
129 unsigned Kind;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
130 uint32_t Index;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
131 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
132
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
133 // A wasm global to be written into the global section.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
134 struct WasmGlobal {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
135 wasm::ValType Type;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
136 bool IsMutable;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
137 bool HasImport;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
138 uint64_t InitialValue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
139 uint32_t ImportIndex;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
140 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
141
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
142 // Information about a single relocation.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
143 struct WasmRelocationEntry {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
144 uint64_t Offset; // Where is the relocation.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
145 const MCSymbolWasm *Symbol; // The symbol to relocate with.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
146 int64_t Addend; // A value to add to the symbol.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
147 unsigned Type; // The type of the relocation.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
148 const MCSectionWasm *FixupSection;// The section the relocation is targeting.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
149
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
150 WasmRelocationEntry(uint64_t Offset, const MCSymbolWasm *Symbol,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
151 int64_t Addend, unsigned Type,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
152 const MCSectionWasm *FixupSection)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
153 : Offset(Offset), Symbol(Symbol), Addend(Addend), Type(Type),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
154 FixupSection(FixupSection) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
155
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
156 bool hasAddend() const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
157 switch (Type) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
158 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
159 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
160 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
161 return true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
162 default:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
163 return false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
164 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
165 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
166
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
167 void print(raw_ostream &Out) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
168 Out << "Off=" << Offset << ", Sym=" << *Symbol << ", Addend=" << Addend
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
169 << ", Type=" << Type
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
170 << ", FixupSection=" << FixupSection->getSectionName();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
171 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
172
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
173 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
174 LLVM_DUMP_METHOD void dump() const { print(dbgs()); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
175 #endif
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
176 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
177
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
178 #if !defined(NDEBUG)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
179 raw_ostream &operator<<(raw_ostream &OS, const WasmRelocationEntry &Rel) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
180 Rel.print(OS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
181 return OS;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
182 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
183 #endif
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
184
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
185 class WasmObjectWriter : public MCObjectWriter {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
186 /// Helper struct for containing some precomputed information on symbols.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
187 struct WasmSymbolData {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
188 const MCSymbolWasm *Symbol;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
189 StringRef Name;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
190
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
191 // Support lexicographic sorting.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
192 bool operator<(const WasmSymbolData &RHS) const { return Name < RHS.Name; }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
193 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
194
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
195 /// The target specific Wasm writer instance.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
196 std::unique_ptr<MCWasmObjectTargetWriter> TargetObjectWriter;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
197
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
198 // Relocations for fixing up references in the code section.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
199 std::vector<WasmRelocationEntry> CodeRelocations;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
200
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
201 // Relocations for fixing up references in the data section.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
202 std::vector<WasmRelocationEntry> DataRelocations;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
203
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
204 // Index values to use for fixing up call_indirect type indices.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
205 // Maps function symbols to the index of the type of the function
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
206 DenseMap<const MCSymbolWasm *, uint32_t> TypeIndices;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
207 // Maps function symbols to the table element index space. Used
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
208 // for TABLE_INDEX relocation types (i.e. address taken functions).
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
209 DenseMap<const MCSymbolWasm *, uint32_t> IndirectSymbolIndices;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
210 // Maps function/global symbols to the function/global index space.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
211 DenseMap<const MCSymbolWasm *, uint32_t> SymbolIndices;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
212
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
213 DenseMap<WasmFunctionType, int32_t, WasmFunctionTypeDenseMapInfo>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
214 FunctionTypeIndices;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
215 SmallVector<WasmFunctionType, 4> FunctionTypes;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
216 SmallVector<WasmGlobal, 4> Globals;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
217 unsigned NumGlobalImports = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
218
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
219 // TargetObjectWriter wrappers.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
220 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
221 unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
222 return TargetObjectWriter->getRelocType(Target, Fixup);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
223 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
224
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
225 void startSection(SectionBookkeeping &Section, unsigned SectionId,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
226 const char *Name = nullptr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
227 void endSection(SectionBookkeeping &Section);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
228
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
229 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
230 WasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
231 raw_pwrite_stream &OS)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
232 : MCObjectWriter(OS, /*IsLittleEndian=*/true),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
233 TargetObjectWriter(std::move(MOTW)) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
234
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
235 private:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
236 ~WasmObjectWriter() override;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
237
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
238 void reset() override {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
239 CodeRelocations.clear();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
240 DataRelocations.clear();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
241 TypeIndices.clear();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
242 SymbolIndices.clear();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
243 IndirectSymbolIndices.clear();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
244 FunctionTypeIndices.clear();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
245 FunctionTypes.clear();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
246 Globals.clear();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
247 MCObjectWriter::reset();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
248 NumGlobalImports = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
249 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
250
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
251 void writeHeader(const MCAssembler &Asm);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
252
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
253 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
254 const MCFragment *Fragment, const MCFixup &Fixup,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
255 MCValue Target, uint64_t &FixedValue) override;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
256
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
257 void executePostLayoutBinding(MCAssembler &Asm,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
258 const MCAsmLayout &Layout) override;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
259
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
260 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
261
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
262 void writeString(const StringRef Str) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
263 encodeULEB128(Str.size(), getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
264 writeBytes(Str);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
265 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
266
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
267 void writeValueType(wasm::ValType Ty) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
268 encodeSLEB128(int32_t(Ty), getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
269 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
270
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
271 void writeTypeSection(ArrayRef<WasmFunctionType> FunctionTypes);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
272 void writeImportSection(ArrayRef<WasmImport> Imports);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
273 void writeFunctionSection(ArrayRef<WasmFunction> Functions);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
274 void writeTableSection(uint32_t NumElements);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
275 void writeMemorySection(uint32_t DataSize);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
276 void writeGlobalSection();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
277 void writeExportSection(ArrayRef<WasmExport> Exports);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
278 void writeElemSection(ArrayRef<uint32_t> TableElems);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
279 void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
280 ArrayRef<WasmFunction> Functions);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
281 void writeDataSection(ArrayRef<WasmDataSegment> Segments);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
282 void writeNameSection(ArrayRef<WasmFunction> Functions,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
283 ArrayRef<WasmImport> Imports,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
284 uint32_t NumFuncImports);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
285 void writeCodeRelocSection();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
286 void writeDataRelocSection();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
287 void writeLinkingMetaDataSection(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
288 ArrayRef<WasmDataSegment> Segments, uint32_t DataSize,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
289 SmallVector<std::pair<StringRef, uint32_t>, 4> SymbolFlags,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
290 bool HasStackPointer, uint32_t StackPointerGlobal);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
291
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
292 uint32_t getProvisionalValue(const WasmRelocationEntry &RelEntry);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
293 void applyRelocations(ArrayRef<WasmRelocationEntry> Relocations,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
294 uint64_t ContentsOffset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
295
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
296 void writeRelocations(ArrayRef<WasmRelocationEntry> Relocations);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
297 uint32_t getRelocationIndexValue(const WasmRelocationEntry &RelEntry);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
298 uint32_t getFunctionType(const MCSymbolWasm& Symbol);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
299 uint32_t registerFunctionType(const MCSymbolWasm& Symbol);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
300 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
301
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
302 } // end anonymous namespace
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
303
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
304 WasmObjectWriter::~WasmObjectWriter() {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
305
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
306 // Write out a section header and a patchable section size field.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
307 void WasmObjectWriter::startSection(SectionBookkeeping &Section,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
308 unsigned SectionId,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
309 const char *Name) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
310 assert((Name != nullptr) == (SectionId == wasm::WASM_SEC_CUSTOM) &&
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
311 "Only custom sections can have names");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
312
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
313 DEBUG(dbgs() << "startSection " << SectionId << ": " << Name << "\n");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
314 encodeULEB128(SectionId, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
315
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
316 Section.SizeOffset = getStream().tell();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
317
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
318 // The section size. We don't know the size yet, so reserve enough space
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
319 // for any 32-bit value; we'll patch it later.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
320 encodeULEB128(UINT32_MAX, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
321
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
322 // The position where the section starts, for measuring its size.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
323 Section.ContentsOffset = getStream().tell();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
324
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
325 // Custom sections in wasm also have a string identifier.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
326 if (SectionId == wasm::WASM_SEC_CUSTOM) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
327 assert(Name);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
328 writeString(StringRef(Name));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
329 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
330 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
331
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
332 // Now that the section is complete and we know how big it is, patch up the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
333 // section size field at the start of the section.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
334 void WasmObjectWriter::endSection(SectionBookkeeping &Section) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
335 uint64_t Size = getStream().tell() - Section.ContentsOffset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
336 if (uint32_t(Size) != Size)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
337 report_fatal_error("section size does not fit in a uint32_t");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
338
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
339 DEBUG(dbgs() << "endSection size=" << Size << "\n");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
340
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
341 // Write the final section size to the payload_len field, which follows
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
342 // the section id byte.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
343 uint8_t Buffer[16];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
344 unsigned SizeLen = encodeULEB128(Size, Buffer, 5);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
345 assert(SizeLen == 5);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
346 getStream().pwrite((char *)Buffer, SizeLen, Section.SizeOffset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
347 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
348
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
349 // Emit the Wasm header.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
350 void WasmObjectWriter::writeHeader(const MCAssembler &Asm) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
351 writeBytes(StringRef(wasm::WasmMagic, sizeof(wasm::WasmMagic)));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
352 writeLE32(wasm::WasmVersion);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
353 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
354
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
355 void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
356 const MCAsmLayout &Layout) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
357 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
358
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
359 void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
360 const MCAsmLayout &Layout,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
361 const MCFragment *Fragment,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
362 const MCFixup &Fixup, MCValue Target,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
363 uint64_t &FixedValue) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
364 MCAsmBackend &Backend = Asm.getBackend();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
365 bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
366 MCFixupKindInfo::FKF_IsPCRel;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
367 const auto &FixupSection = cast<MCSectionWasm>(*Fragment->getParent());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
368 uint64_t C = Target.getConstant();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
369 uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
370 MCContext &Ctx = Asm.getContext();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
371
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
372 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
373 assert(RefB->getKind() == MCSymbolRefExpr::VK_None &&
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
374 "Should not have constructed this");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
375
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
376 // Let A, B and C being the components of Target and R be the location of
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
377 // the fixup. If the fixup is not pcrel, we want to compute (A - B + C).
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
378 // If it is pcrel, we want to compute (A - B + C - R).
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
379
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
380 // In general, Wasm has no relocations for -B. It can only represent (A + C)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
381 // or (A + C - R). If B = R + K and the relocation is not pcrel, we can
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
382 // replace B to implement it: (A - R - K + C)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
383 if (IsPCRel) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
384 Ctx.reportError(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
385 Fixup.getLoc(),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
386 "No relocation available to represent this relative expression");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
387 return;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
388 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
389
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
390 const auto &SymB = cast<MCSymbolWasm>(RefB->getSymbol());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
391
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
392 if (SymB.isUndefined()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
393 Ctx.reportError(Fixup.getLoc(),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
394 Twine("symbol '") + SymB.getName() +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
395 "' can not be undefined in a subtraction expression");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
396 return;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
397 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
398
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
399 assert(!SymB.isAbsolute() && "Should have been folded");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
400 const MCSection &SecB = SymB.getSection();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
401 if (&SecB != &FixupSection) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
402 Ctx.reportError(Fixup.getLoc(),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
403 "Cannot represent a difference across sections");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
404 return;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
405 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
406
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
407 uint64_t SymBOffset = Layout.getSymbolOffset(SymB);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
408 uint64_t K = SymBOffset - FixupOffset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
409 IsPCRel = true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
410 C -= K;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
411 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
412
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
413 // We either rejected the fixup or folded B into C at this point.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
414 const MCSymbolRefExpr *RefA = Target.getSymA();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
415 const auto *SymA = RefA ? cast<MCSymbolWasm>(&RefA->getSymbol()) : nullptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
416
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
417 if (SymA && SymA->isVariable()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
418 const MCExpr *Expr = SymA->getVariableValue();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
419 const auto *Inner = cast<MCSymbolRefExpr>(Expr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
420 if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
421 llvm_unreachable("weakref used in reloc not yet implemented");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
422 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
423
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
424 // Put any constant offset in an addend. Offsets can be negative, and
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
425 // LLVM expects wrapping, in contrast to wasm's immediates which can't
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
426 // be negative and don't wrap.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
427 FixedValue = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
428
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
429 if (SymA)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
430 SymA->setUsedInReloc();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
431
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
432 assert(!IsPCRel);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
433 assert(SymA);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
434
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
435 unsigned Type = getRelocType(Target, Fixup);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
436
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
437 WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
438 DEBUG(dbgs() << "WasmReloc: " << Rec << "\n");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
439
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
440 if (FixupSection.isWasmData())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
441 DataRelocations.push_back(Rec);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
442 else if (FixupSection.getKind().isText())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
443 CodeRelocations.push_back(Rec);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
444 else if (!FixupSection.getKind().isMetadata())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
445 // TODO(sbc): Add support for debug sections.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
446 llvm_unreachable("unexpected section type");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
447 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
448
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
449 // Write X as an (unsigned) LEB value at offset Offset in Stream, padded
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
450 // to allow patching.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
451 static void
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
452 WritePatchableLEB(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
453 uint8_t Buffer[5];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
454 unsigned SizeLen = encodeULEB128(X, Buffer, 5);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
455 assert(SizeLen == 5);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
456 Stream.pwrite((char *)Buffer, SizeLen, Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
457 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
458
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
459 // Write X as an signed LEB value at offset Offset in Stream, padded
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
460 // to allow patching.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
461 static void
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
462 WritePatchableSLEB(raw_pwrite_stream &Stream, int32_t X, uint64_t Offset) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
463 uint8_t Buffer[5];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
464 unsigned SizeLen = encodeSLEB128(X, Buffer, 5);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
465 assert(SizeLen == 5);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
466 Stream.pwrite((char *)Buffer, SizeLen, Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
467 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
468
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
469 // Write X as a plain integer value at offset Offset in Stream.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
470 static void WriteI32(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
471 uint8_t Buffer[4];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
472 support::endian::write32le(Buffer, X);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
473 Stream.pwrite((char *)Buffer, sizeof(Buffer), Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
474 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
475
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
476 static const MCSymbolWasm* ResolveSymbol(const MCSymbolWasm& Symbol) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
477 if (Symbol.isVariable()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
478 const MCExpr *Expr = Symbol.getVariableValue();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
479 auto *Inner = cast<MCSymbolRefExpr>(Expr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
480 return cast<MCSymbolWasm>(&Inner->getSymbol());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
481 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
482 return &Symbol;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
483 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
484
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
485 // Compute a value to write into the code at the location covered
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
486 // by RelEntry. This value isn't used by the static linker, since
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
487 // we have addends; it just serves to make the code more readable
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
488 // and to make standalone wasm modules directly usable.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
489 uint32_t
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
490 WasmObjectWriter::getProvisionalValue(const WasmRelocationEntry &RelEntry) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
491 const MCSymbolWasm *Sym = ResolveSymbol(*RelEntry.Symbol);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
492
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
493 // For undefined symbols, use a hopefully invalid value.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
494 if (!Sym->isDefined(/*SetUsed=*/false))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
495 return UINT32_MAX;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
496
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
497 uint32_t GlobalIndex = SymbolIndices[Sym];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
498 const WasmGlobal& Global = Globals[GlobalIndex - NumGlobalImports];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
499 uint64_t Address = Global.InitialValue + RelEntry.Addend;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
500
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
501 // Ignore overflow. LLVM allows address arithmetic to silently wrap.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
502 uint32_t Value = Address;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
503
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
504 return Value;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
505 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
506
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
507 static void addData(SmallVectorImpl<char> &DataBytes,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
508 MCSectionWasm &DataSection) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
509 DEBUG(errs() << "addData: " << DataSection.getSectionName() << "\n");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
510
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
511 DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment()));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
512
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
513 size_t LastFragmentSize = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
514 for (const MCFragment &Frag : DataSection) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
515 if (Frag.hasInstructions())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
516 report_fatal_error("only data supported in data sections");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
517
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
518 if (auto *Align = dyn_cast<MCAlignFragment>(&Frag)) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
519 if (Align->getValueSize() != 1)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
520 report_fatal_error("only byte values supported for alignment");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
521 // If nops are requested, use zeros, as this is the data section.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
522 uint8_t Value = Align->hasEmitNops() ? 0 : Align->getValue();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
523 uint64_t Size = std::min<uint64_t>(alignTo(DataBytes.size(),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
524 Align->getAlignment()),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
525 DataBytes.size() +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
526 Align->getMaxBytesToEmit());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
527 DataBytes.resize(Size, Value);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
528 } else if (auto *Fill = dyn_cast<MCFillFragment>(&Frag)) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
529 DataBytes.insert(DataBytes.end(), Fill->getSize(), Fill->getValue());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
530 } else {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
531 const auto &DataFrag = cast<MCDataFragment>(Frag);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
532 const SmallVectorImpl<char> &Contents = DataFrag.getContents();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
533
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
534 DataBytes.insert(DataBytes.end(), Contents.begin(), Contents.end());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
535 LastFragmentSize = Contents.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
536 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
537 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
538
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
539 // Don't allow empty segments, or segments that end with zero-sized
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
540 // fragment, otherwise the linker cannot map symbols to a unique
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
541 // data segment. This can be triggered by zero-sized structs
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
542 // See: test/MC/WebAssembly/bss.ll
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
543 if (LastFragmentSize == 0)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
544 DataBytes.resize(DataBytes.size() + 1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
545 DEBUG(dbgs() << "addData -> " << DataBytes.size() << "\n");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
546 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
547
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
548 uint32_t WasmObjectWriter::getRelocationIndexValue(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
549 const WasmRelocationEntry &RelEntry) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
550 switch (RelEntry.Type) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
551 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
552 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
553 if (!IndirectSymbolIndices.count(RelEntry.Symbol))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
554 report_fatal_error("symbol not found table index space: " +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
555 RelEntry.Symbol->getName());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
556 return IndirectSymbolIndices[RelEntry.Symbol];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
557 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
558 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
559 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
560 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
561 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
562 if (!SymbolIndices.count(RelEntry.Symbol))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
563 report_fatal_error("symbol not found function/global index space: " +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
564 RelEntry.Symbol->getName());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
565 return SymbolIndices[RelEntry.Symbol];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
566 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
567 if (!TypeIndices.count(RelEntry.Symbol))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
568 report_fatal_error("symbol not found in type index space: " +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
569 RelEntry.Symbol->getName());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
570 return TypeIndices[RelEntry.Symbol];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
571 default:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
572 llvm_unreachable("invalid relocation type");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
573 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
574 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
575
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
576 // Apply the portions of the relocation records that we can handle ourselves
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
577 // directly.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
578 void WasmObjectWriter::applyRelocations(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
579 ArrayRef<WasmRelocationEntry> Relocations, uint64_t ContentsOffset) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
580 raw_pwrite_stream &Stream = getStream();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
581 for (const WasmRelocationEntry &RelEntry : Relocations) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
582 uint64_t Offset = ContentsOffset +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
583 RelEntry.FixupSection->getSectionOffset() +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
584 RelEntry.Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
585
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
586 DEBUG(dbgs() << "applyRelocation: " << RelEntry << "\n");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
587 switch (RelEntry.Type) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
588 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
589 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
590 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
591 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB: {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
592 uint32_t Index = getRelocationIndexValue(RelEntry);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
593 WritePatchableSLEB(Stream, Index, Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
594 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
595 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
596 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32: {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
597 uint32_t Index = getRelocationIndexValue(RelEntry);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
598 WriteI32(Stream, Index, Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
599 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
600 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
601 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
602 uint32_t Value = getProvisionalValue(RelEntry);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
603 WritePatchableSLEB(Stream, Value, Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
604 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
605 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
606 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB: {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
607 uint32_t Value = getProvisionalValue(RelEntry);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
608 WritePatchableLEB(Stream, Value, Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
609 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
610 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
611 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32: {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
612 uint32_t Value = getProvisionalValue(RelEntry);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
613 WriteI32(Stream, Value, Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
614 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
615 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
616 default:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
617 llvm_unreachable("invalid relocation type");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
618 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
619 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
620 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
621
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
622 // Write out the portions of the relocation records that the linker will
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
623 // need to handle.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
624 void WasmObjectWriter::writeRelocations(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
625 ArrayRef<WasmRelocationEntry> Relocations) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
626 raw_pwrite_stream &Stream = getStream();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
627 for (const WasmRelocationEntry& RelEntry : Relocations) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
628
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
629 uint64_t Offset = RelEntry.Offset +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
630 RelEntry.FixupSection->getSectionOffset();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
631 uint32_t Index = getRelocationIndexValue(RelEntry);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
632
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
633 encodeULEB128(RelEntry.Type, Stream);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
634 encodeULEB128(Offset, Stream);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
635 encodeULEB128(Index, Stream);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
636 if (RelEntry.hasAddend())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
637 encodeSLEB128(RelEntry.Addend, Stream);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
638 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
639 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
640
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
641 void WasmObjectWriter::writeTypeSection(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
642 ArrayRef<WasmFunctionType> FunctionTypes) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
643 if (FunctionTypes.empty())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
644 return;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
645
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
646 SectionBookkeeping Section;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
647 startSection(Section, wasm::WASM_SEC_TYPE);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
648
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
649 encodeULEB128(FunctionTypes.size(), getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
650
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
651 for (const WasmFunctionType &FuncTy : FunctionTypes) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
652 encodeSLEB128(wasm::WASM_TYPE_FUNC, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
653 encodeULEB128(FuncTy.Params.size(), getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
654 for (wasm::ValType Ty : FuncTy.Params)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
655 writeValueType(Ty);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
656 encodeULEB128(FuncTy.Returns.size(), getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
657 for (wasm::ValType Ty : FuncTy.Returns)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
658 writeValueType(Ty);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
659 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
660
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
661 endSection(Section);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
662 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
663
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
664 void WasmObjectWriter::writeImportSection(ArrayRef<WasmImport> Imports) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
665 if (Imports.empty())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
666 return;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
667
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
668 SectionBookkeeping Section;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
669 startSection(Section, wasm::WASM_SEC_IMPORT);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
670
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
671 encodeULEB128(Imports.size(), getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
672 for (const WasmImport &Import : Imports) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
673 writeString(Import.ModuleName);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
674 writeString(Import.FieldName);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
675
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
676 encodeULEB128(Import.Kind, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
677
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
678 switch (Import.Kind) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
679 case wasm::WASM_EXTERNAL_FUNCTION:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
680 encodeULEB128(Import.Type, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
681 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
682 case wasm::WASM_EXTERNAL_GLOBAL:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
683 encodeSLEB128(int32_t(Import.Type), getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
684 encodeULEB128(0, getStream()); // mutability
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
685 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
686 default:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
687 llvm_unreachable("unsupported import kind");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
688 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
689 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
690
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
691 endSection(Section);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
692 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
693
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
694 void WasmObjectWriter::writeFunctionSection(ArrayRef<WasmFunction> Functions) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
695 if (Functions.empty())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
696 return;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
697
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
698 SectionBookkeeping Section;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
699 startSection(Section, wasm::WASM_SEC_FUNCTION);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
700
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
701 encodeULEB128(Functions.size(), getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
702 for (const WasmFunction &Func : Functions)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
703 encodeULEB128(Func.Type, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
704
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
705 endSection(Section);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
706 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
707
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
708 void WasmObjectWriter::writeTableSection(uint32_t NumElements) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
709 // For now, always emit the table section, since indirect calls are not
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
710 // valid without it. In the future, we could perhaps be more clever and omit
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
711 // it if there are no indirect calls.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
712
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
713 SectionBookkeeping Section;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
714 startSection(Section, wasm::WASM_SEC_TABLE);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
715
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
716 encodeULEB128(1, getStream()); // The number of tables.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
717 // Fixed to 1 for now.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
718 encodeSLEB128(wasm::WASM_TYPE_ANYFUNC, getStream()); // Type of table
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
719 encodeULEB128(0, getStream()); // flags
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
720 encodeULEB128(NumElements, getStream()); // initial
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
721
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
722 endSection(Section);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
723 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
724
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
725 void WasmObjectWriter::writeMemorySection(uint32_t DataSize) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
726 // For now, always emit the memory section, since loads and stores are not
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
727 // valid without it. In the future, we could perhaps be more clever and omit
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
728 // it if there are no loads or stores.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
729 SectionBookkeeping Section;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
730 uint32_t NumPages = (DataSize + wasm::WasmPageSize - 1) / wasm::WasmPageSize;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
731
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
732 startSection(Section, wasm::WASM_SEC_MEMORY);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
733 encodeULEB128(1, getStream()); // number of memory spaces
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
734
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
735 encodeULEB128(0, getStream()); // flags
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
736 encodeULEB128(NumPages, getStream()); // initial
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
737
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
738 endSection(Section);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
739 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
740
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
741 void WasmObjectWriter::writeGlobalSection() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
742 if (Globals.empty())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
743 return;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
744
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
745 SectionBookkeeping Section;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
746 startSection(Section, wasm::WASM_SEC_GLOBAL);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
747
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
748 encodeULEB128(Globals.size(), getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
749 for (const WasmGlobal &Global : Globals) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
750 writeValueType(Global.Type);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
751 write8(Global.IsMutable);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
752
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
753 if (Global.HasImport) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
754 assert(Global.InitialValue == 0);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
755 write8(wasm::WASM_OPCODE_GET_GLOBAL);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
756 encodeULEB128(Global.ImportIndex, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
757 } else {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
758 assert(Global.ImportIndex == 0);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
759 write8(wasm::WASM_OPCODE_I32_CONST);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
760 encodeSLEB128(Global.InitialValue, getStream()); // offset
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
761 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
762 write8(wasm::WASM_OPCODE_END);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
763 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
764
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
765 endSection(Section);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
766 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
767
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
768 void WasmObjectWriter::writeExportSection(ArrayRef<WasmExport> Exports) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
769 if (Exports.empty())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
770 return;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
771
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
772 SectionBookkeeping Section;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
773 startSection(Section, wasm::WASM_SEC_EXPORT);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
774
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
775 encodeULEB128(Exports.size(), getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
776 for (const WasmExport &Export : Exports) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
777 writeString(Export.FieldName);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
778 encodeSLEB128(Export.Kind, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
779 encodeULEB128(Export.Index, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
780 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
781
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
782 endSection(Section);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
783 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
784
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
785 void WasmObjectWriter::writeElemSection(ArrayRef<uint32_t> TableElems) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
786 if (TableElems.empty())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
787 return;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
788
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
789 SectionBookkeeping Section;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
790 startSection(Section, wasm::WASM_SEC_ELEM);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
791
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
792 encodeULEB128(1, getStream()); // number of "segments"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
793 encodeULEB128(0, getStream()); // the table index
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
794
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
795 // init expr for starting offset
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
796 write8(wasm::WASM_OPCODE_I32_CONST);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
797 encodeSLEB128(0, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
798 write8(wasm::WASM_OPCODE_END);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
799
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
800 encodeULEB128(TableElems.size(), getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
801 for (uint32_t Elem : TableElems)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
802 encodeULEB128(Elem, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
803
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
804 endSection(Section);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
805 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
806
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
807 void WasmObjectWriter::writeCodeSection(const MCAssembler &Asm,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
808 const MCAsmLayout &Layout,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
809 ArrayRef<WasmFunction> Functions) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
810 if (Functions.empty())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
811 return;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
812
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
813 SectionBookkeeping Section;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
814 startSection(Section, wasm::WASM_SEC_CODE);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
815
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
816 encodeULEB128(Functions.size(), getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
817
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
818 for (const WasmFunction &Func : Functions) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
819 auto &FuncSection = static_cast<MCSectionWasm &>(Func.Sym->getSection());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
820
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
821 int64_t Size = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
822 if (!Func.Sym->getSize()->evaluateAsAbsolute(Size, Layout))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
823 report_fatal_error(".size expression must be evaluatable");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
824
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
825 encodeULEB128(Size, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
826 FuncSection.setSectionOffset(getStream().tell() - Section.ContentsOffset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
827 Asm.writeSectionData(&FuncSection, Layout);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
828 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
829
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
830 // Apply fixups.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
831 applyRelocations(CodeRelocations, Section.ContentsOffset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
832
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
833 endSection(Section);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
834 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
835
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
836 void WasmObjectWriter::writeDataSection(ArrayRef<WasmDataSegment> Segments) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
837 if (Segments.empty())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
838 return;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
839
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
840 SectionBookkeeping Section;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
841 startSection(Section, wasm::WASM_SEC_DATA);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
842
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
843 encodeULEB128(Segments.size(), getStream()); // count
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
844
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
845 for (const WasmDataSegment & Segment : Segments) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
846 encodeULEB128(0, getStream()); // memory index
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
847 write8(wasm::WASM_OPCODE_I32_CONST);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
848 encodeSLEB128(Segment.Offset, getStream()); // offset
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
849 write8(wasm::WASM_OPCODE_END);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
850 encodeULEB128(Segment.Data.size(), getStream()); // size
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
851 Segment.Section->setSectionOffset(getStream().tell() - Section.ContentsOffset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
852 writeBytes(Segment.Data); // data
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
853 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
854
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
855 // Apply fixups.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
856 applyRelocations(DataRelocations, Section.ContentsOffset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
857
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
858 endSection(Section);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
859 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
860
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
861 void WasmObjectWriter::writeNameSection(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
862 ArrayRef<WasmFunction> Functions,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
863 ArrayRef<WasmImport> Imports,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
864 unsigned NumFuncImports) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
865 uint32_t TotalFunctions = NumFuncImports + Functions.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
866 if (TotalFunctions == 0)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
867 return;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
868
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
869 SectionBookkeeping Section;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
870 startSection(Section, wasm::WASM_SEC_CUSTOM, "name");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
871 SectionBookkeeping SubSection;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
872 startSection(SubSection, wasm::WASM_NAMES_FUNCTION);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
873
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
874 encodeULEB128(TotalFunctions, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
875 uint32_t Index = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
876 for (const WasmImport &Import : Imports) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
877 if (Import.Kind == wasm::WASM_EXTERNAL_FUNCTION) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
878 encodeULEB128(Index, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
879 writeString(Import.FieldName);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
880 ++Index;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
881 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
882 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
883 for (const WasmFunction &Func : Functions) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
884 encodeULEB128(Index, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
885 writeString(Func.Sym->getName());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
886 ++Index;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
887 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
888
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
889 endSection(SubSection);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
890 endSection(Section);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
891 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
892
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
893 void WasmObjectWriter::writeCodeRelocSection() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
894 // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
895 // for descriptions of the reloc sections.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
896
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
897 if (CodeRelocations.empty())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
898 return;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
899
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
900 SectionBookkeeping Section;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
901 startSection(Section, wasm::WASM_SEC_CUSTOM, "reloc.CODE");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
902
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
903 encodeULEB128(wasm::WASM_SEC_CODE, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
904 encodeULEB128(CodeRelocations.size(), getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
905
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
906 writeRelocations(CodeRelocations);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
907
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
908 endSection(Section);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
909 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
910
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
911 void WasmObjectWriter::writeDataRelocSection() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
912 // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
913 // for descriptions of the reloc sections.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
914
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
915 if (DataRelocations.empty())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
916 return;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
917
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
918 SectionBookkeeping Section;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
919 startSection(Section, wasm::WASM_SEC_CUSTOM, "reloc.DATA");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
920
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
921 encodeULEB128(wasm::WASM_SEC_DATA, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
922 encodeULEB128(DataRelocations.size(), getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
923
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
924 writeRelocations(DataRelocations);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
925
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
926 endSection(Section);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
927 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
928
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
929 void WasmObjectWriter::writeLinkingMetaDataSection(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
930 ArrayRef<WasmDataSegment> Segments, uint32_t DataSize,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
931 SmallVector<std::pair<StringRef, uint32_t>, 4> SymbolFlags,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
932 bool HasStackPointer, uint32_t StackPointerGlobal) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
933 SectionBookkeeping Section;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
934 startSection(Section, wasm::WASM_SEC_CUSTOM, "linking");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
935 SectionBookkeeping SubSection;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
936
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
937 if (HasStackPointer) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
938 startSection(SubSection, wasm::WASM_STACK_POINTER);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
939 encodeULEB128(StackPointerGlobal, getStream()); // id
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
940 endSection(SubSection);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
941 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
942
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
943 if (SymbolFlags.size() != 0) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
944 startSection(SubSection, wasm::WASM_SYMBOL_INFO);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
945 encodeULEB128(SymbolFlags.size(), getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
946 for (auto Pair: SymbolFlags) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
947 writeString(Pair.first);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
948 encodeULEB128(Pair.second, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
949 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
950 endSection(SubSection);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
951 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
952
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
953 if (DataSize > 0) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
954 startSection(SubSection, wasm::WASM_DATA_SIZE);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
955 encodeULEB128(DataSize, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
956 endSection(SubSection);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
957 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
958
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
959 if (Segments.size()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
960 startSection(SubSection, wasm::WASM_SEGMENT_INFO);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
961 encodeULEB128(Segments.size(), getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
962 for (const WasmDataSegment &Segment : Segments) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
963 writeString(Segment.Name);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
964 encodeULEB128(Segment.Alignment, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
965 encodeULEB128(Segment.Flags, getStream());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
966 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
967 endSection(SubSection);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
968 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
969
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
970 endSection(Section);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
971 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
972
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
973 uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm& Symbol) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
974 assert(Symbol.isFunction());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
975 assert(TypeIndices.count(&Symbol));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
976 return TypeIndices[&Symbol];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
977 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
978
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
979 uint32_t WasmObjectWriter::registerFunctionType(const MCSymbolWasm& Symbol) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
980 assert(Symbol.isFunction());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
981
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
982 WasmFunctionType F;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
983 const MCSymbolWasm* ResolvedSym = ResolveSymbol(Symbol);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
984 F.Returns = ResolvedSym->getReturns();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
985 F.Params = ResolvedSym->getParams();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
986
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
987 auto Pair =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
988 FunctionTypeIndices.insert(std::make_pair(F, FunctionTypes.size()));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
989 if (Pair.second)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
990 FunctionTypes.push_back(F);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
991 TypeIndices[&Symbol] = Pair.first->second;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
992
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
993 DEBUG(dbgs() << "registerFunctionType: " << Symbol << " new:" << Pair.second << "\n");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
994 DEBUG(dbgs() << " -> type index: " << Pair.first->second << "\n");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
995 return Pair.first->second;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
996 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
997
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
998 void WasmObjectWriter::writeObject(MCAssembler &Asm,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
999 const MCAsmLayout &Layout) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1000 DEBUG(dbgs() << "WasmObjectWriter::writeObject\n");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1001 MCContext &Ctx = Asm.getContext();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1002 wasm::ValType PtrType = is64Bit() ? wasm::ValType::I64 : wasm::ValType::I32;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1003
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1004 // Collect information from the available symbols.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1005 SmallVector<WasmFunction, 4> Functions;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1006 SmallVector<uint32_t, 4> TableElems;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1007 SmallVector<WasmImport, 4> Imports;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1008 SmallVector<WasmExport, 4> Exports;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1009 SmallVector<std::pair<StringRef, uint32_t>, 4> SymbolFlags;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1010 SmallPtrSet<const MCSymbolWasm *, 4> IsAddressTaken;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1011 unsigned NumFuncImports = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1012 SmallVector<WasmDataSegment, 4> DataSegments;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1013 uint32_t StackPointerGlobal = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1014 uint32_t DataSize = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1015 bool HasStackPointer = false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1016
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1017 // Populate the IsAddressTaken set.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1018 for (const WasmRelocationEntry &RelEntry : CodeRelocations) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1019 switch (RelEntry.Type) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1020 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1021 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1022 IsAddressTaken.insert(RelEntry.Symbol);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1023 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1024 default:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1025 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1026 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1027 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1028 for (const WasmRelocationEntry &RelEntry : DataRelocations) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1029 switch (RelEntry.Type) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1030 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1031 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1032 IsAddressTaken.insert(RelEntry.Symbol);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1033 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1034 default:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1035 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1036 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1037 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1038
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1039 // Populate FunctionTypeIndices and Imports.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1040 for (const MCSymbol &S : Asm.symbols()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1041 const auto &WS = static_cast<const MCSymbolWasm &>(S);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1042
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1043 if (WS.isTemporary())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1044 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1045
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1046 if (WS.isFunction())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1047 registerFunctionType(WS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1048
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1049 // If the symbol is not defined in this translation unit, import it.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1050 if (!WS.isDefined(/*SetUsed=*/false)) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1051 WasmImport Import;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1052 Import.ModuleName = WS.getModuleName();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1053 Import.FieldName = WS.getName();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1054
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1055 if (WS.isFunction()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1056 Import.Kind = wasm::WASM_EXTERNAL_FUNCTION;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1057 Import.Type = getFunctionType(WS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1058 SymbolIndices[&WS] = NumFuncImports;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1059 ++NumFuncImports;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1060 } else {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1061 Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1062 Import.Type = int32_t(PtrType);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1063 SymbolIndices[&WS] = NumGlobalImports;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1064 ++NumGlobalImports;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1065 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1066
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1067 Imports.push_back(Import);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1068 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1069 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1070
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1071 // In the special .global_variables section, we've encoded global
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1072 // variables used by the function. Translate them into the Globals
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1073 // list.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1074 MCSectionWasm *GlobalVars =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1075 Ctx.getWasmSection(".global_variables", SectionKind::getMetadata());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1076 if (!GlobalVars->getFragmentList().empty()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1077 if (GlobalVars->getFragmentList().size() != 1)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1078 report_fatal_error("only one .global_variables fragment supported");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1079 const MCFragment &Frag = *GlobalVars->begin();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1080 if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1081 report_fatal_error("only data supported in .global_variables");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1082 const auto &DataFrag = cast<MCDataFragment>(Frag);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1083 if (!DataFrag.getFixups().empty())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1084 report_fatal_error("fixups not supported in .global_variables");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1085 const SmallVectorImpl<char> &Contents = DataFrag.getContents();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1086 for (const uint8_t *p = (const uint8_t *)Contents.data(),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1087 *end = (const uint8_t *)Contents.data() + Contents.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1088 p != end; ) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1089 WasmGlobal G;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1090 if (end - p < 3)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1091 report_fatal_error("truncated global variable encoding");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1092 G.Type = wasm::ValType(int8_t(*p++));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1093 G.IsMutable = bool(*p++);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1094 G.HasImport = bool(*p++);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1095 if (G.HasImport) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1096 G.InitialValue = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1097
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1098 WasmImport Import;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1099 Import.ModuleName = (const char *)p;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1100 const uint8_t *nul = (const uint8_t *)memchr(p, '\0', end - p);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1101 if (!nul)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1102 report_fatal_error("global module name must be nul-terminated");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1103 p = nul + 1;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1104 nul = (const uint8_t *)memchr(p, '\0', end - p);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1105 if (!nul)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1106 report_fatal_error("global base name must be nul-terminated");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1107 Import.FieldName = (const char *)p;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1108 p = nul + 1;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1109
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1110 Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1111 Import.Type = int32_t(G.Type);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1112
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1113 G.ImportIndex = NumGlobalImports;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1114 ++NumGlobalImports;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1115
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1116 Imports.push_back(Import);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1117 } else {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1118 unsigned n;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1119 G.InitialValue = decodeSLEB128(p, &n);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1120 G.ImportIndex = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1121 if ((ptrdiff_t)n > end - p)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1122 report_fatal_error("global initial value must be valid SLEB128");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1123 p += n;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1124 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1125 Globals.push_back(G);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1126 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1127 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1128
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1129 // In the special .stack_pointer section, we've encoded the stack pointer
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1130 // index.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1131 MCSectionWasm *StackPtr =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1132 Ctx.getWasmSection(".stack_pointer", SectionKind::getMetadata());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1133 if (!StackPtr->getFragmentList().empty()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1134 if (StackPtr->getFragmentList().size() != 1)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1135 report_fatal_error("only one .stack_pointer fragment supported");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1136 const MCFragment &Frag = *StackPtr->begin();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1137 if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1138 report_fatal_error("only data supported in .stack_pointer");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1139 const auto &DataFrag = cast<MCDataFragment>(Frag);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1140 if (!DataFrag.getFixups().empty())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1141 report_fatal_error("fixups not supported in .stack_pointer");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1142 const SmallVectorImpl<char> &Contents = DataFrag.getContents();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1143 if (Contents.size() != 4)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1144 report_fatal_error("only one entry supported in .stack_pointer");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1145 HasStackPointer = true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1146 StackPointerGlobal = NumGlobalImports + *(const int32_t *)Contents.data();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1147 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1148
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1149 for (MCSection &Sec : Asm) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1150 auto &Section = static_cast<MCSectionWasm &>(Sec);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1151 if (!Section.isWasmData())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1152 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1153
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1154 DataSize = alignTo(DataSize, Section.getAlignment());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1155 DataSegments.emplace_back();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1156 WasmDataSegment &Segment = DataSegments.back();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1157 Segment.Name = Section.getSectionName();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1158 Segment.Offset = DataSize;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1159 Segment.Section = &Section;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1160 addData(Segment.Data, Section);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1161 Segment.Alignment = Section.getAlignment();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1162 Segment.Flags = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1163 DataSize += Segment.Data.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1164 Section.setMemoryOffset(Segment.Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1165 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1166
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1167 // Handle regular defined and undefined symbols.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1168 for (const MCSymbol &S : Asm.symbols()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1169 // Ignore unnamed temporary symbols, which aren't ever exported, imported,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1170 // or used in relocations.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1171 if (S.isTemporary() && S.getName().empty())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1172 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1173
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1174 const auto &WS = static_cast<const MCSymbolWasm &>(S);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1175 DEBUG(dbgs() << "MCSymbol: '" << S << "'"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1176 << " isDefined=" << S.isDefined() << " isExternal="
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1177 << S.isExternal() << " isTemporary=" << S.isTemporary()
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1178 << " isFunction=" << WS.isFunction()
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1179 << " isWeak=" << WS.isWeak()
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1180 << " isVariable=" << WS.isVariable() << "\n");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1181
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1182 if (WS.isWeak())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1183 SymbolFlags.emplace_back(WS.getName(), wasm::WASM_SYMBOL_BINDING_WEAK);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1184
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1185 if (WS.isVariable())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1186 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1187
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1188 unsigned Index;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1189
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1190 if (WS.isFunction()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1191 if (WS.isDefined(/*SetUsed=*/false)) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1192 if (WS.getOffset() != 0)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1193 report_fatal_error(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1194 "function sections must contain one function each");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1195
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1196 if (WS.getSize() == 0)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1197 report_fatal_error(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1198 "function symbols must have a size set with .size");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1199
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1200 // A definition. Take the next available index.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1201 Index = NumFuncImports + Functions.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1202
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1203 // Prepare the function.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1204 WasmFunction Func;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1205 Func.Type = getFunctionType(WS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1206 Func.Sym = &WS;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1207 SymbolIndices[&WS] = Index;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1208 Functions.push_back(Func);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1209 } else {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1210 // An import; the index was assigned above.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1211 Index = SymbolIndices.find(&WS)->second;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1212 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1213
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1214 DEBUG(dbgs() << " -> function index: " << Index << "\n");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1215
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1216 // If needed, prepare the function to be called indirectly.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1217 if (IsAddressTaken.count(&WS) != 0) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1218 IndirectSymbolIndices[&WS] = TableElems.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1219 DEBUG(dbgs() << " -> adding to table: " << TableElems.size() << "\n");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1220 TableElems.push_back(Index);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1221 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1222 } else {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1223 if (WS.isTemporary() && !WS.getSize())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1224 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1225
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1226 if (!WS.isDefined(/*SetUsed=*/false))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1227 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1228
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1229 if (!WS.getSize())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1230 report_fatal_error("data symbols must have a size set with .size: " +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1231 WS.getName());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1232
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1233 int64_t Size = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1234 if (!WS.getSize()->evaluateAsAbsolute(Size, Layout))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1235 report_fatal_error(".size expression must be evaluatable");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1236
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1237 // For each global, prepare a corresponding wasm global holding its
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1238 // address. For externals these will also be named exports.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1239 Index = NumGlobalImports + Globals.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1240 auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1241
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1242 WasmGlobal Global;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1243 Global.Type = PtrType;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1244 Global.IsMutable = false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1245 Global.HasImport = false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1246 Global.InitialValue = DataSection.getMemoryOffset() + Layout.getSymbolOffset(WS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1247 Global.ImportIndex = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1248 SymbolIndices[&WS] = Index;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1249 DEBUG(dbgs() << " -> global index: " << Index << "\n");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1250 Globals.push_back(Global);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1251 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1252
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1253 // If the symbol is visible outside this translation unit, export it.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1254 if (WS.isDefined(/*SetUsed=*/false)) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1255 WasmExport Export;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1256 Export.FieldName = WS.getName();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1257 Export.Index = Index;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1258 if (WS.isFunction())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1259 Export.Kind = wasm::WASM_EXTERNAL_FUNCTION;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1260 else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1261 Export.Kind = wasm::WASM_EXTERNAL_GLOBAL;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1262 DEBUG(dbgs() << " -> export " << Exports.size() << "\n");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1263 Exports.push_back(Export);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1264 if (!WS.isExternal())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1265 SymbolFlags.emplace_back(WS.getName(), wasm::WASM_SYMBOL_BINDING_LOCAL);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1266 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1267 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1268
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1269 // Handle weak aliases. We need to process these in a separate pass because
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1270 // we need to have processed the target of the alias before the alias itself
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1271 // and the symbols are not necessarily ordered in this way.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1272 for (const MCSymbol &S : Asm.symbols()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1273 if (!S.isVariable())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1274 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1275
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1276 assert(S.isDefined(/*SetUsed=*/false));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1277
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1278 // Find the target symbol of this weak alias and export that index
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1279 const auto &WS = static_cast<const MCSymbolWasm &>(S);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1280 const MCSymbolWasm *ResolvedSym = ResolveSymbol(WS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1281 DEBUG(dbgs() << WS.getName() << ": weak alias of '" << *ResolvedSym << "'\n");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1282 assert(SymbolIndices.count(ResolvedSym) > 0);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1283 uint32_t Index = SymbolIndices.find(ResolvedSym)->second;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1284 DEBUG(dbgs() << " -> index:" << Index << "\n");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1285
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1286 SymbolIndices[&WS] = Index;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1287 WasmExport Export;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1288 Export.FieldName = WS.getName();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1289 Export.Index = Index;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1290 if (WS.isFunction())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1291 Export.Kind = wasm::WASM_EXTERNAL_FUNCTION;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1292 else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1293 Export.Kind = wasm::WASM_EXTERNAL_GLOBAL;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1294 DEBUG(dbgs() << " -> export " << Exports.size() << "\n");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1295 Exports.push_back(Export);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1296
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1297 if (!WS.isExternal())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1298 SymbolFlags.emplace_back(WS.getName(), wasm::WASM_SYMBOL_BINDING_LOCAL);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1299 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1300
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1301 // Add types for indirect function calls.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1302 for (const WasmRelocationEntry &Fixup : CodeRelocations) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1303 if (Fixup.Type != wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1304 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1305
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1306 registerFunctionType(*Fixup.Symbol);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1307 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1308
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1309 // Write out the Wasm header.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1310 writeHeader(Asm);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1311
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1312 writeTypeSection(FunctionTypes);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1313 writeImportSection(Imports);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1314 writeFunctionSection(Functions);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1315 writeTableSection(TableElems.size());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1316 writeMemorySection(DataSize);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1317 writeGlobalSection();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1318 writeExportSection(Exports);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1319 // TODO: Start Section
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1320 writeElemSection(TableElems);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1321 writeCodeSection(Asm, Layout, Functions);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1322 writeDataSection(DataSegments);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1323 writeNameSection(Functions, Imports, NumFuncImports);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1324 writeCodeRelocSection();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1325 writeDataRelocSection();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1326 writeLinkingMetaDataSection(DataSegments, DataSize, SymbolFlags,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1327 HasStackPointer, StackPointerGlobal);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1328
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1329 // TODO: Translate the .comment section to the output.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1330 // TODO: Translate debug sections to the output.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1331 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1332
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1333 std::unique_ptr<MCObjectWriter>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1334 llvm::createWasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1335 raw_pwrite_stream &OS) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1336 // FIXME: Can't use make_unique<WasmObjectWriter>(...) as WasmObjectWriter's
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1337 // destructor is private. Is that necessary?
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1338 return std::unique_ptr<MCObjectWriter>(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1339 new WasmObjectWriter(std::move(MOTW), OS));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1340 }