• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ADT/Optional.h"
19 #include "llvm/BinaryFormat/WasmTraits.h"
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:
40   void wrap(Symbol *sym, Symbol *real, Symbol *wrap);
41 
42   void addFile(InputFile *file);
43 
44   void addCombinedLTOObject();
45 
getSymbols()46   ArrayRef<Symbol *> getSymbols() const { return symVector; }
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                          InputSegment *segment, uint64_t address,
58                          uint64_t size);
59   Symbol *addDefinedGlobal(StringRef name, uint32_t flags, InputFile *file,
60                            InputGlobal *g);
61   Symbol *addDefinedEvent(StringRef name, uint32_t flags, InputFile *file,
62                           InputEvent *e);
63 
64   Symbol *addUndefinedFunction(StringRef name,
65                                llvm::Optional<StringRef> importName,
66                                llvm::Optional<StringRef> importModule,
67                                uint32_t flags, InputFile *file,
68                                const WasmSignature *signature,
69                                bool isCalledDirectly);
70   Symbol *addUndefinedData(StringRef name, uint32_t flags, InputFile *file);
71   Symbol *addUndefinedGlobal(StringRef name,
72                              llvm::Optional<StringRef> importName,
73                              llvm::Optional<StringRef> importModule,
74                              uint32_t flags, InputFile *file,
75                              const WasmGlobalType *type);
76 
77   void addLazy(ArchiveFile *f, const llvm::object::Archive::Symbol *sym);
78 
79   bool addComdat(StringRef name);
80 
81   DefinedData *addSyntheticDataSymbol(StringRef name, uint32_t flags);
82   DefinedGlobal *addSyntheticGlobal(StringRef name, uint32_t flags,
83                                     InputGlobal *global);
84   DefinedFunction *addSyntheticFunction(StringRef name, uint32_t flags,
85                                         InputFunction *function);
86   DefinedData *addOptionalDataSymbol(StringRef name, uint64_t value = 0);
87   DefinedGlobal *addOptionalGlobalSymbols(StringRef name, uint32_t flags,
88                                           InputGlobal *global);
89 
90   void handleSymbolVariants();
91   void handleWeakUndefines();
92   DefinedFunction *createUndefinedStub(const WasmSignature &sig);
93 
94   std::vector<ObjFile *> objectFiles;
95   std::vector<SharedFile *> sharedFiles;
96   std::vector<BitcodeFile *> bitcodeFiles;
97   std::vector<InputFunction *> syntheticFunctions;
98   std::vector<InputGlobal *> syntheticGlobals;
99 
100 private:
101   std::pair<Symbol *, bool> insert(StringRef name, const InputFile *file);
102   std::pair<Symbol *, bool> insertName(StringRef name);
103 
104   bool getFunctionVariant(Symbol* sym, const WasmSignature *sig,
105                           const InputFile *file, Symbol **out);
106   InputFunction *replaceWithUnreachable(Symbol *sym, const WasmSignature &sig,
107                                         StringRef debugName);
108   void replaceWithUndefined(Symbol *sym);
109 
110   // Maps symbol names to index into the symVector.  -1 means that symbols
111   // is to not yet in the vector but it should have tracing enabled if it is
112   // ever added.
113   llvm::DenseMap<llvm::CachedHashStringRef, int> symMap;
114   std::vector<Symbol *> symVector;
115 
116   // For certain symbols types, e.g. function symbols, we allow for multiple
117   // variants of the same symbol with different signatures.
118   llvm::DenseMap<llvm::CachedHashStringRef, std::vector<Symbol *>> symVariants;
119   llvm::DenseMap<WasmSignature, DefinedFunction *> stubFunctions;
120 
121   // Comdat groups define "link once" sections. If two comdat groups have the
122   // same name, only one of them is linked, and the other is ignored. This set
123   // is used to uniquify them.
124   llvm::DenseSet<llvm::CachedHashStringRef> comdatGroups;
125 
126   // For LTO.
127   std::unique_ptr<BitcodeCompiler> lto;
128 };
129 
130 extern SymbolTable *symtab;
131 
132 } // namespace wasm
133 } // namespace lld
134 
135 #endif
136