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_COFF_SYMBOL_TABLE_H |
10 | #define LLD_COFF_SYMBOL_TABLE_H |
11 | |
12 | #include "InputFiles.h" |
13 | #include "LTO.h" |
14 | #include "llvm/ADT/CachedHashString.h" |
15 | #include "llvm/ADT/DenseMap.h" |
16 | #include "llvm/ADT/DenseMapInfo.h" |
17 | #include "llvm/Support/raw_ostream.h" |
18 | |
19 | namespace llvm { |
20 | struct LTOCodeGenerator; |
21 | } |
22 | |
23 | namespace lld::coff { |
24 | |
25 | class Chunk; |
26 | class CommonChunk; |
27 | class COFFLinkerContext; |
28 | class Defined; |
29 | class DefinedAbsolute; |
30 | class DefinedRegular; |
31 | class DefinedRelative; |
32 | class LazyArchive; |
33 | class SectionChunk; |
34 | class Symbol; |
35 | |
36 | // SymbolTable is a bucket of all known symbols, including defined, |
37 | // undefined, or lazy symbols (the last one is symbols in archive |
38 | // files whose archive members are not yet loaded). |
39 | // |
40 | // We put all symbols of all files to a SymbolTable, and the |
41 | // SymbolTable selects the "best" symbols if there are name |
42 | // conflicts. For example, obviously, a defined symbol is better than |
43 | // an undefined symbol. Or, if there's a conflict between a lazy and a |
44 | // undefined, it'll read an archive member to read a real definition |
45 | // to replace the lazy symbol. The logic is implemented in the |
46 | // add*() functions, which are called by input files as they are parsed. |
47 | // There is one add* function per symbol type. |
48 | class SymbolTable { |
49 | public: |
50 | SymbolTable(COFFLinkerContext &c) : ctx(c) {} |
51 | |
52 | void addFile(InputFile *file); |
53 | |
54 | // Emit errors for symbols that cannot be resolved. |
55 | void reportUnresolvable(); |
56 | |
57 | // Try to resolve any undefined symbols and update the symbol table |
58 | // accordingly, then print an error message for any remaining undefined |
59 | // symbols and warn about imported local symbols. |
60 | void resolveRemainingUndefines(); |
61 | |
62 | // Load lazy objects that are needed for MinGW automatic import and for |
63 | // doing stdcall fixups. |
64 | void loadMinGWSymbols(); |
65 | bool handleMinGWAutomaticImport(Symbol *sym, StringRef name); |
66 | |
67 | // Returns a list of chunks of selected symbols. |
68 | std::vector<Chunk *> getChunks() const; |
69 | |
70 | // Returns a symbol for a given name. Returns a nullptr if not found. |
71 | Symbol *find(StringRef name) const; |
72 | Symbol *findUnderscore(StringRef name) const; |
73 | |
74 | // Occasionally we have to resolve an undefined symbol to its |
75 | // mangled symbol. This function tries to find a mangled name |
76 | // for U from the symbol table, and if found, set the symbol as |
77 | // a weak alias for U. |
78 | Symbol *findMangle(StringRef name); |
79 | |
80 | // Build a set of COFF objects representing the combined contents of |
81 | // BitcodeFiles and add them to the symbol table. Called after all files are |
82 | // added and before the writer writes results to a file. |
83 | void compileBitcodeFiles(); |
84 | |
85 | // Creates an Undefined symbol for a given name. |
86 | Symbol *addUndefined(StringRef name); |
87 | |
88 | Symbol *addSynthetic(StringRef n, Chunk *c); |
89 | Symbol *addAbsolute(StringRef n, uint64_t va); |
90 | |
91 | Symbol *addUndefined(StringRef name, InputFile *f, bool isWeakAlias); |
92 | void addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym); |
93 | void addLazyObject(InputFile *f, StringRef n); |
94 | void addLazyDLLSymbol(DLLFile *f, DLLFile::Symbol *sym, StringRef n); |
95 | Symbol *addAbsolute(StringRef n, COFFSymbolRef s); |
96 | Symbol *addRegular(InputFile *f, StringRef n, |
97 | const llvm::object::coff_symbol_generic *s = nullptr, |
98 | SectionChunk *c = nullptr, uint32_t sectionOffset = 0, |
99 | bool isWeak = false); |
100 | std::pair<DefinedRegular *, bool> |
101 | addComdat(InputFile *f, StringRef n, |
102 | const llvm::object::coff_symbol_generic *s = nullptr); |
103 | Symbol *addCommon(InputFile *f, StringRef n, uint64_t size, |
104 | const llvm::object::coff_symbol_generic *s = nullptr, |
105 | CommonChunk *c = nullptr); |
106 | Symbol *addImportData(StringRef n, ImportFile *f); |
107 | Symbol *addImportThunk(StringRef name, DefinedImportData *s, |
108 | uint16_t machine); |
109 | void addLibcall(StringRef name); |
110 | |
111 | void reportDuplicate(Symbol *existing, InputFile *newFile, |
112 | SectionChunk *newSc = nullptr, |
113 | uint32_t newSectionOffset = 0); |
114 | |
115 | // A list of chunks which to be added to .rdata. |
116 | std::vector<Chunk *> localImportChunks; |
117 | |
118 | // Iterates symbols in non-determinstic hash table order. |
119 | template <typename T> void forEachSymbol(T callback) { |
120 | for (auto &pair : symMap) |
121 | callback(pair.second); |
122 | } |
123 | |
124 | private: |
125 | /// Given a name without "__imp_" prefix, returns a defined symbol |
126 | /// with the "__imp_" prefix, if it exists. |
127 | Defined *impSymbol(StringRef name); |
128 | /// Inserts symbol if not already present. |
129 | std::pair<Symbol *, bool> insert(StringRef name); |
130 | /// Same as insert(Name), but also sets isUsedInRegularObj. |
131 | std::pair<Symbol *, bool> insert(StringRef name, InputFile *f); |
132 | |
133 | std::vector<Symbol *> getSymsWithPrefix(StringRef prefix); |
134 | |
135 | llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> symMap; |
136 | std::unique_ptr<BitcodeCompiler> lto; |
137 | |
138 | COFFLinkerContext &ctx; |
139 | }; |
140 | |
141 | std::vector<std::string> getSymbolLocations(ObjFile *file, uint32_t symIndex); |
142 | |
143 | StringRef ltrim1(StringRef s, const char *chars); |
144 | |
145 | } // namespace lld::coff |
146 | |
147 | #endif |
148 | |