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