150
|
1 //===- SymbolTable.h --------------------------------------------*- C++ -*-===//
|
|
2 //
|
|
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
4 // See https://llvm.org/LICENSE.txt for license information.
|
|
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
6 //
|
|
7 //===----------------------------------------------------------------------===//
|
|
8
|
|
9 #ifndef LLD_WASM_SYMBOL_TABLE_H
|
|
10 #define LLD_WASM_SYMBOL_TABLE_H
|
|
11
|
|
12 #include "InputFiles.h"
|
|
13 #include "LTO.h"
|
|
14 #include "Symbols.h"
|
|
15 #include "lld/Common/LLVM.h"
|
|
16 #include "llvm/ADT/CachedHashString.h"
|
|
17 #include "llvm/ADT/DenseSet.h"
|
221
|
18 #include "llvm/BinaryFormat/WasmTraits.h"
|
252
|
19 #include <optional>
|
150
|
20
|
252
|
21 namespace lld::wasm {
|
150
|
22
|
|
23 class InputSegment;
|
|
24
|
|
25 // SymbolTable is a bucket of all known symbols, including defined,
|
|
26 // undefined, or lazy symbols (the last one is symbols in archive
|
|
27 // files whose archive members are not yet loaded).
|
|
28 //
|
|
29 // We put all symbols of all files to a SymbolTable, and the
|
|
30 // SymbolTable selects the "best" symbols if there are name
|
|
31 // conflicts. For example, obviously, a defined symbol is better than
|
|
32 // an undefined symbol. Or, if there's a conflict between a lazy and a
|
|
33 // undefined, it'll read an archive member to read a real definition
|
|
34 // to replace the lazy symbol. The logic is implemented in the
|
|
35 // add*() functions, which are called by input files as they are parsed.
|
|
36 // There is one add* function per symbol type.
|
|
37 class SymbolTable {
|
|
38 public:
|
236
|
39 ArrayRef<Symbol *> symbols() const { return symVector; }
|
|
40
|
150
|
41 void wrap(Symbol *sym, Symbol *real, Symbol *wrap);
|
|
42
|
|
43 void addFile(InputFile *file);
|
|
44
|
236
|
45 void compileBitcodeFiles();
|
150
|
46
|
|
47 Symbol *find(StringRef name);
|
|
48
|
|
49 void replace(StringRef name, Symbol* sym);
|
|
50
|
|
51 void trace(StringRef name);
|
|
52
|
|
53 Symbol *addDefinedFunction(StringRef name, uint32_t flags, InputFile *file,
|
|
54 InputFunction *function);
|
|
55 Symbol *addDefinedData(StringRef name, uint32_t flags, InputFile *file,
|
221
|
56 InputChunk *segment, uint64_t address, uint64_t size);
|
150
|
57 Symbol *addDefinedGlobal(StringRef name, uint32_t flags, InputFile *file,
|
|
58 InputGlobal *g);
|
223
|
59 Symbol *addDefinedTag(StringRef name, uint32_t flags, InputFile *file,
|
|
60 InputTag *t);
|
221
|
61 Symbol *addDefinedTable(StringRef name, uint32_t flags, InputFile *file,
|
|
62 InputTable *t);
|
150
|
63
|
173
|
64 Symbol *addUndefinedFunction(StringRef name,
|
252
|
65 std::optional<StringRef> importName,
|
|
66 std::optional<StringRef> importModule,
|
173
|
67 uint32_t flags, InputFile *file,
|
|
68 const WasmSignature *signature,
|
150
|
69 bool isCalledDirectly);
|
|
70 Symbol *addUndefinedData(StringRef name, uint32_t flags, InputFile *file);
|
173
|
71 Symbol *addUndefinedGlobal(StringRef name,
|
252
|
72 std::optional<StringRef> importName,
|
|
73 std::optional<StringRef> importModule,
|
173
|
74 uint32_t flags, InputFile *file,
|
|
75 const WasmGlobalType *type);
|
252
|
76 Symbol *addUndefinedTable(StringRef name, std::optional<StringRef> importName,
|
|
77 std::optional<StringRef> importModule,
|
221
|
78 uint32_t flags, InputFile *file,
|
|
79 const WasmTableType *type);
|
252
|
80 Symbol *addUndefinedTag(StringRef name, std::optional<StringRef> importName,
|
|
81 std::optional<StringRef> importModule, uint32_t flags,
|
|
82 InputFile *file, const WasmSignature *sig);
|
221
|
83
|
|
84 TableSymbol *resolveIndirectFunctionTable(bool required);
|
150
|
85
|
|
86 void addLazy(ArchiveFile *f, const llvm::object::Archive::Symbol *sym);
|
|
87
|
|
88 bool addComdat(StringRef name);
|
|
89
|
|
90 DefinedData *addSyntheticDataSymbol(StringRef name, uint32_t flags);
|
|
91 DefinedGlobal *addSyntheticGlobal(StringRef name, uint32_t flags,
|
|
92 InputGlobal *global);
|
|
93 DefinedFunction *addSyntheticFunction(StringRef name, uint32_t flags,
|
|
94 InputFunction *function);
|
221
|
95 DefinedData *addOptionalDataSymbol(StringRef name, uint64_t value = 0);
|
|
96 DefinedGlobal *addOptionalGlobalSymbol(StringRef name, InputGlobal *global);
|
|
97 DefinedTable *addSyntheticTable(StringRef name, uint32_t flags,
|
|
98 InputTable *global);
|
150
|
99
|
|
100 void handleSymbolVariants();
|
|
101 void handleWeakUndefines();
|
221
|
102 DefinedFunction *createUndefinedStub(const WasmSignature &sig);
|
150
|
103
|
|
104 std::vector<ObjFile *> objectFiles;
|
252
|
105 std::vector<StubFile *> stubFiles;
|
150
|
106 std::vector<SharedFile *> sharedFiles;
|
|
107 std::vector<BitcodeFile *> bitcodeFiles;
|
|
108 std::vector<InputFunction *> syntheticFunctions;
|
|
109 std::vector<InputGlobal *> syntheticGlobals;
|
221
|
110 std::vector<InputTable *> syntheticTables;
|
150
|
111
|
|
112 private:
|
|
113 std::pair<Symbol *, bool> insert(StringRef name, const InputFile *file);
|
|
114 std::pair<Symbol *, bool> insertName(StringRef name);
|
|
115
|
|
116 bool getFunctionVariant(Symbol* sym, const WasmSignature *sig,
|
|
117 const InputFile *file, Symbol **out);
|
|
118 InputFunction *replaceWithUnreachable(Symbol *sym, const WasmSignature &sig,
|
|
119 StringRef debugName);
|
221
|
120 void replaceWithUndefined(Symbol *sym);
|
|
121
|
|
122 TableSymbol *createDefinedIndirectFunctionTable(StringRef name);
|
|
123 TableSymbol *createUndefinedIndirectFunctionTable(StringRef name);
|
150
|
124
|
|
125 // Maps symbol names to index into the symVector. -1 means that symbols
|
|
126 // is to not yet in the vector but it should have tracing enabled if it is
|
|
127 // ever added.
|
|
128 llvm::DenseMap<llvm::CachedHashStringRef, int> symMap;
|
|
129 std::vector<Symbol *> symVector;
|
|
130
|
173
|
131 // For certain symbols types, e.g. function symbols, we allow for multiple
|
150
|
132 // variants of the same symbol with different signatures.
|
|
133 llvm::DenseMap<llvm::CachedHashStringRef, std::vector<Symbol *>> symVariants;
|
221
|
134 llvm::DenseMap<WasmSignature, DefinedFunction *> stubFunctions;
|
150
|
135
|
|
136 // Comdat groups define "link once" sections. If two comdat groups have the
|
|
137 // same name, only one of them is linked, and the other is ignored. This set
|
|
138 // is used to uniquify them.
|
|
139 llvm::DenseSet<llvm::CachedHashStringRef> comdatGroups;
|
|
140
|
|
141 // For LTO.
|
|
142 std::unique_ptr<BitcodeCompiler> lto;
|
|
143 };
|
|
144
|
|
145 extern SymbolTable *symtab;
|
|
146
|
252
|
147 } // namespace lld::wasm
|
150
|
148
|
|
149 #endif
|