1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
//===- Symbols.h ------------------------------------------------*- C++ -*-===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLD_WASM_SYMBOLS_H
#define LLD_WASM_SYMBOLS_H
#include "lld/Common/LLVM.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/Wasm.h"
using llvm::object::Archive;
using llvm::object::WasmSymbol;
using llvm::wasm::WasmGlobal;
using llvm::wasm::WasmGlobalType;
using llvm::wasm::WasmSignature;
using llvm::wasm::WasmSymbolType;
namespace lld {
namespace wasm {
class InputFile;
class InputChunk;
class InputSegment;
class InputFunction;
class InputGlobal;
#define INVALID_INDEX UINT32_MAX
// The base class for real symbol classes.
class Symbol {
public:
enum Kind {
DefinedFunctionKind,
DefinedDataKind,
DefinedGlobalKind,
UndefinedFunctionKind,
UndefinedDataKind,
UndefinedGlobalKind,
LazyKind,
};
Kind kind() const { return SymbolKind; }
bool isDefined() const {
return SymbolKind == DefinedFunctionKind || SymbolKind == DefinedDataKind ||
SymbolKind == DefinedGlobalKind;
}
bool isUndefined() const {
return SymbolKind == UndefinedFunctionKind ||
SymbolKind == UndefinedDataKind || SymbolKind == UndefinedGlobalKind;
}
bool isLazy() const { return SymbolKind == LazyKind; }
bool isLocal() const;
bool isWeak() const;
bool isHidden() const;
// Returns the symbol name.
StringRef getName() const { return Name; }
// Returns the file from which this symbol was created.
InputFile *getFile() const { return File; }
InputChunk *getChunk() const;
// Indicates that this symbol will be included in the final image.
bool isLive() const;
void setHidden(bool IsHidden);
uint32_t getOutputIndex() const;
// Returns true if an output index has been set for this symbol
bool hasOutputIndex() const;
// Set the output index of the symbol, in the Wasm index space of the output
// object - that is, for defined symbols only, its position in the list of
// Wasm imports+code for functions, imports+globals for globals.
void setOutputIndex(uint32_t Index);
// Get/set the output symbol index, in the Symbol index space. This is
// only used for relocatable output.
uint32_t getOutputSymbolIndex() const;
void setOutputSymbolIndex(uint32_t Index);
WasmSymbolType getWasmType() const;
protected:
Symbol(StringRef Name, Kind K, uint32_t Flags, InputFile *F)
: Name(Name), SymbolKind(K), Flags(Flags), File(F) {}
StringRef Name;
Kind SymbolKind;
uint32_t Flags;
InputFile *File;
uint32_t OutputIndex = INVALID_INDEX;
uint32_t OutputSymbolIndex = INVALID_INDEX;
};
class FunctionSymbol : public Symbol {
public:
static bool classof(const Symbol *S) {
return S->kind() == DefinedFunctionKind ||
S->kind() == UndefinedFunctionKind;
}
const WasmSignature *getFunctionType() const { return FunctionType; }
uint32_t getTableIndex() const;
// Returns true if a table index has been set for this symbol
bool hasTableIndex() const;
// Set the table index of the symbol
void setTableIndex(uint32_t Index);
protected:
FunctionSymbol(StringRef Name, Kind K, uint32_t Flags, InputFile *F,
const WasmSignature *Type)
: Symbol(Name, K, Flags, F), FunctionType(Type) {}
uint32_t TableIndex = INVALID_INDEX;
const WasmSignature *FunctionType;
};
class DefinedFunction : public FunctionSymbol {
public:
// Primary constructor for file-defined functions.
DefinedFunction(StringRef Name, uint32_t Flags, InputFile *F,
InputFunction *Function);
// Second constructor used when creating synthetic functions.
DefinedFunction(StringRef Name, uint32_t Flags, const WasmSignature *Type)
: FunctionSymbol(Name, DefinedFunctionKind, Flags, nullptr, Type) {}
static bool classof(const Symbol *S) {
return S->kind() == DefinedFunctionKind;
}
InputFunction *Function;
};
class UndefinedFunction : public FunctionSymbol {
public:
UndefinedFunction(StringRef Name, uint32_t Flags, InputFile *File = nullptr,
const WasmSignature *Type = nullptr)
: FunctionSymbol(Name, UndefinedFunctionKind, Flags, File, Type) {}
static bool classof(const Symbol *S) {
return S->kind() == UndefinedFunctionKind;
}
};
class DataSymbol : public Symbol {
public:
static bool classof(const Symbol *S) {
return S->kind() == DefinedDataKind || S->kind() == UndefinedDataKind;
}
protected:
DataSymbol(StringRef Name, Kind K, uint32_t Flags, InputFile *F)
: Symbol(Name, K, Flags, F) {}
};
class DefinedData : public DataSymbol {
public:
// Constructor for for regular data symbols originating from input files.
DefinedData(StringRef Name, uint32_t Flags, InputFile *F,
InputSegment *Segment, uint32_t Offset, uint32_t Size)
: DataSymbol(Name, DefinedDataKind, Flags, F), Segment(Segment),
Offset(Offset), Size(Size) {}
// Constructor for linker synthetic data symbols.
DefinedData(StringRef Name, uint32_t Flags)
: DataSymbol(Name, DefinedDataKind, Flags, nullptr) {}
static bool classof(const Symbol *S) { return S->kind() == DefinedDataKind; }
// Returns the output virtual address of a defined data symbol.
uint32_t getVirtualAddress() const;
void setVirtualAddress(uint32_t VA);
// Returns the offset of a defined data symbol within its OutputSegment.
uint32_t getOutputSegmentOffset() const;
uint32_t getOutputSegmentIndex() const;
uint32_t getSize() const { return Size; }
InputSegment *Segment = nullptr;
protected:
uint32_t Offset = 0;
uint32_t Size = 0;
};
class UndefinedData : public DataSymbol {
public:
UndefinedData(StringRef Name, uint32_t Flags, InputFile *File = nullptr)
: DataSymbol(Name, UndefinedDataKind, Flags, File) {}
static bool classof(const Symbol *S) {
return S->kind() == UndefinedDataKind;
}
};
class GlobalSymbol : public Symbol {
public:
static bool classof(const Symbol *S) {
return S->kind() == DefinedGlobalKind || S->kind() == UndefinedGlobalKind;
}
const WasmGlobalType *getGlobalType() const { return GlobalType; }
protected:
GlobalSymbol(StringRef Name, Kind K, uint32_t Flags, InputFile *F,
const WasmGlobalType *GlobalType)
: Symbol(Name, K, Flags, F), GlobalType(GlobalType) {}
// Explicit function type, needed for undefined or synthetic functions only.
// For regular defined globals this information comes from the InputChunk.
const WasmGlobalType *GlobalType;
};
class DefinedGlobal : public GlobalSymbol {
public:
DefinedGlobal(StringRef Name, uint32_t Flags, InputFile *File,
InputGlobal *Global);
static bool classof(const Symbol *S) {
return S->kind() == DefinedGlobalKind;
}
InputGlobal *Global;
};
class UndefinedGlobal : public GlobalSymbol {
public:
UndefinedGlobal(StringRef Name, uint32_t Flags, InputFile *File = nullptr,
const WasmGlobalType *Type = nullptr)
: GlobalSymbol(Name, UndefinedGlobalKind, Flags, File, Type) {}
static bool classof(const Symbol *S) {
return S->kind() == UndefinedGlobalKind;
}
};
class LazySymbol : public Symbol {
public:
LazySymbol(StringRef Name, InputFile *File, const Archive::Symbol &Sym)
: Symbol(Name, LazyKind, 0, File), ArchiveSymbol(Sym) {}
static bool classof(const Symbol *S) { return S->kind() == LazyKind; }
void fetch();
private:
Archive::Symbol ArchiveSymbol;
};
// linker-generated symbols
struct WasmSym {
// __stack_pointer
// Global that holds the address of the top of the explicit value stack in
// linear memory.
static DefinedGlobal *StackPointer;
// __data_end
// Symbol marking the end of the data and bss.
static DefinedData *DataEnd;
// __heap_base
// Symbol marking the end of the data, bss and explicit stack. Any linear
// memory following this address is not used by the linked code and can
// therefore be used as a backing store for brk()/malloc() implementations.
static DefinedData *HeapBase;
// __wasm_call_ctors
// Function that directly calls all ctors in priority order.
static DefinedFunction *CallCtors;
// __dso_handle
// Symbol used in calls to __cxa_atexit to determine current DLL
static DefinedData *DsoHandle;
};
// A buffer class that is large enough to hold any Symbol-derived
// object. We allocate memory using this class and instantiate a symbol
// using the placement new.
union SymbolUnion {
alignas(DefinedFunction) char A[sizeof(DefinedFunction)];
alignas(DefinedData) char B[sizeof(DefinedData)];
alignas(DefinedGlobal) char C[sizeof(DefinedGlobal)];
alignas(LazySymbol) char D[sizeof(LazySymbol)];
alignas(UndefinedFunction) char E[sizeof(UndefinedFunction)];
alignas(UndefinedData) char F[sizeof(UndefinedData)];
alignas(UndefinedGlobal) char G[sizeof(UndefinedGlobal)];
};
template <typename T, typename... ArgT>
T *replaceSymbol(Symbol *S, ArgT &&... Arg) {
static_assert(std::is_trivially_destructible<T>(),
"Symbol types must be trivially destructible");
static_assert(sizeof(T) <= sizeof(SymbolUnion), "Symbol too small");
static_assert(alignof(T) <= alignof(SymbolUnion),
"SymbolUnion not aligned enough");
assert(static_cast<Symbol *>(static_cast<T *>(nullptr)) == nullptr &&
"Not a Symbol");
return new (S) T(std::forward<ArgT>(Arg)...);
}
} // namespace wasm
// Returns a symbol name for an error message.
std::string toString(const wasm::Symbol &Sym);
std::string toString(wasm::Symbol::Kind Kind);
std::string toString(WasmSymbolType Type);
} // namespace lld
#endif
|