Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1 | //===- SymbolTable.h --------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef LLD_WASM_SYMBOL_TABLE_H |
| 11 | #define LLD_WASM_SYMBOL_TABLE_H |
| 12 | |
| 13 | #include "InputFiles.h" |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 14 | #include "LTO.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 15 | #include "Symbols.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/CachedHashString.h" |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseSet.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
| 19 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 20 | using llvm::wasm::WasmGlobalType; |
Nicholas Wilson | dbd90bf | 2018-03-07 13:28:16 +0000 | [diff] [blame] | 21 | using llvm::wasm::WasmSignature; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 22 | |
| 23 | namespace lld { |
| 24 | namespace wasm { |
| 25 | |
| 26 | class InputSegment; |
| 27 | |
| 28 | // SymbolTable is a bucket of all known symbols, including defined, |
| 29 | // undefined, or lazy symbols (the last one is symbols in archive |
| 30 | // files whose archive members are not yet loaded). |
| 31 | // |
| 32 | // We put all symbols of all files to a SymbolTable, and the |
| 33 | // SymbolTable selects the "best" symbols if there are name |
| 34 | // conflicts. For example, obviously, a defined symbol is better than |
| 35 | // an undefined symbol. Or, if there's a conflict between a lazy and a |
| 36 | // undefined, it'll read an archive member to read a real definition |
| 37 | // to replace the lazy symbol. The logic is implemented in the |
| 38 | // add*() functions, which are called by input files as they are parsed. |
| 39 | // There is one add* function per symbol type. |
| 40 | class SymbolTable { |
| 41 | public: |
| 42 | void addFile(InputFile *File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 43 | void addCombinedLTOObject(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 44 | |
| 45 | std::vector<ObjFile *> ObjectFiles; |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 46 | std::vector<BitcodeFile *> BitcodeFiles; |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 47 | std::vector<InputFunction *> SyntheticFunctions; |
| 48 | std::vector<InputGlobal *> SyntheticGlobals; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 49 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 50 | void reportRemainingUndefines(); |
| 51 | |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 52 | ArrayRef<Symbol *> getSymbols() const { return SymVector; } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 53 | Symbol *find(StringRef Name); |
| 54 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 55 | Symbol *addDefinedFunction(StringRef Name, uint32_t Flags, InputFile *File, |
Rui Ueyama | c61b834 | 2018-02-28 00:37:03 +0000 | [diff] [blame] | 56 | InputFunction *Function); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 57 | Symbol *addDefinedData(StringRef Name, uint32_t Flags, InputFile *File, |
Rui Ueyama | c61b834 | 2018-02-28 00:37:03 +0000 | [diff] [blame] | 58 | InputSegment *Segment, uint32_t Address, |
| 59 | uint32_t Size); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 60 | Symbol *addDefinedGlobal(StringRef Name, uint32_t Flags, InputFile *File, |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 61 | InputGlobal *G); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 62 | |
| 63 | Symbol *addUndefinedFunction(StringRef Name, uint32_t Flags, InputFile *File, |
| 64 | const WasmSignature *Signature); |
| 65 | Symbol *addUndefinedData(StringRef Name, uint32_t Flags, InputFile *File); |
| 66 | Symbol *addUndefinedGlobal(StringRef Name, uint32_t Flags, InputFile *File, |
| 67 | const WasmGlobalType *Type); |
| 68 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 69 | void addLazy(ArchiveFile *F, const Archive::Symbol *Sym); |
Rui Ueyama | dcf6234 | 2018-03-01 23:29:05 +0000 | [diff] [blame] | 70 | |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 71 | bool addComdat(StringRef Name); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 72 | |
Rui Ueyama | c61b834 | 2018-02-28 00:37:03 +0000 | [diff] [blame] | 73 | DefinedData *addSyntheticDataSymbol(StringRef Name, uint32_t Flags); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 74 | DefinedGlobal *addSyntheticGlobal(StringRef Name, uint32_t Flags, |
| 75 | InputGlobal *Global); |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 76 | DefinedFunction *addSyntheticFunction(StringRef Name, uint32_t Flags, |
| 77 | InputFunction *Function); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 78 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 79 | private: |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 80 | std::pair<Symbol *, bool> insert(StringRef Name, InputFile *File); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 81 | |
Sam Clegg | a80d94d | 2017-11-27 23:16:06 +0000 | [diff] [blame] | 82 | llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> SymMap; |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 83 | std::vector<Symbol *> SymVector; |
Rui Ueyama | dcf6234 | 2018-03-01 23:29:05 +0000 | [diff] [blame] | 84 | |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 85 | llvm::DenseSet<llvm::CachedHashStringRef> Comdats; |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 86 | |
| 87 | // For LTO. |
| 88 | std::unique_ptr<BitcodeCompiler> LTO; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | extern SymbolTable *Symtab; |
| 92 | |
| 93 | } // namespace wasm |
| 94 | } // namespace lld |
| 95 | |
| 96 | #endif |