annotate lib/MC/WasmObjectWriter.cpp @ 134:3a76565eade5 LLVM5.0.1

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