Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / lld / COFF / SymbolTable.h
blob1d9e908b8b991883cde3eade9736b4cf5493ff50
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 //===----------------------------------------------------------------------===//
9 #ifndef LLD_COFF_SYMBOL_TABLE_H
10 #define LLD_COFF_SYMBOL_TABLE_H
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"
19 namespace llvm {
20 struct LTOCodeGenerator;
23 namespace lld::coff {
25 class Chunk;
26 class CommonChunk;
27 class COFFLinkerContext;
28 class Defined;
29 class DefinedAbsolute;
30 class DefinedRegular;
31 class ImportThunkChunk;
32 class LazyArchive;
33 class SectionChunk;
34 class Symbol;
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).
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) {}
52 void addFile(InputFile *file);
54 // Emit errors for symbols that cannot be resolved.
55 void reportUnresolvable();
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 // Returns whether more files might need to be linked in to resolve lazy
61 // symbols, in which case the caller is expected to call the function again
62 // after linking those files.
63 bool resolveRemainingUndefines();
65 // Load lazy objects that are needed for MinGW automatic import and for
66 // doing stdcall fixups.
67 void loadMinGWSymbols();
68 bool handleMinGWAutomaticImport(Symbol *sym, StringRef name);
70 // Returns a list of chunks of selected symbols.
71 std::vector<Chunk *> getChunks() const;
73 // Returns a symbol for a given name. Returns a nullptr if not found.
74 Symbol *find(StringRef name) const;
75 Symbol *findUnderscore(StringRef name) const;
77 // Occasionally we have to resolve an undefined symbol to its
78 // mangled symbol. This function tries to find a mangled name
79 // for U from the symbol table, and if found, set the symbol as
80 // a weak alias for U.
81 Symbol *findMangle(StringRef name);
83 // Build a set of COFF objects representing the combined contents of
84 // BitcodeFiles and add them to the symbol table. Called after all files are
85 // added and before the writer writes results to a file.
86 void compileBitcodeFiles();
88 // Creates an Undefined symbol for a given name.
89 Symbol *addUndefined(StringRef name);
91 Symbol *addSynthetic(StringRef n, Chunk *c);
92 Symbol *addAbsolute(StringRef n, uint64_t va);
94 Symbol *addUndefined(StringRef name, InputFile *f, bool overrideLazy);
95 void addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym);
96 void addLazyObject(InputFile *f, StringRef n);
97 void addLazyDLLSymbol(DLLFile *f, DLLFile::Symbol *sym, StringRef n);
98 Symbol *addAbsolute(StringRef n, COFFSymbolRef s);
99 Symbol *addRegular(InputFile *f, StringRef n,
100 const llvm::object::coff_symbol_generic *s = nullptr,
101 SectionChunk *c = nullptr, uint32_t sectionOffset = 0,
102 bool isWeak = false);
103 std::pair<DefinedRegular *, bool>
104 addComdat(InputFile *f, StringRef n,
105 const llvm::object::coff_symbol_generic *s = nullptr);
106 Symbol *addCommon(InputFile *f, StringRef n, uint64_t size,
107 const llvm::object::coff_symbol_generic *s = nullptr,
108 CommonChunk *c = nullptr);
109 DefinedImportData *addImportData(StringRef n, ImportFile *f,
110 Chunk *&location);
111 Defined *addImportThunk(StringRef name, DefinedImportData *s,
112 ImportThunkChunk *chunk);
113 void addLibcall(StringRef name);
114 void addEntryThunk(Symbol *from, Symbol *to);
115 void addExitThunk(Symbol *from, Symbol *to);
116 void initializeECThunks();
118 void reportDuplicate(Symbol *existing, InputFile *newFile,
119 SectionChunk *newSc = nullptr,
120 uint32_t newSectionOffset = 0);
122 // A list of chunks which to be added to .rdata.
123 std::vector<Chunk *> localImportChunks;
125 // A list of EC EXP+ symbols.
126 std::vector<Symbol *> expSymbols;
128 // Iterates symbols in non-determinstic hash table order.
129 template <typename T> void forEachSymbol(T callback) {
130 for (auto &pair : symMap)
131 callback(pair.second);
134 private:
135 /// Given a name without "__imp_" prefix, returns a defined symbol
136 /// with the "__imp_" prefix, if it exists.
137 Defined *impSymbol(StringRef name);
138 /// Inserts symbol if not already present.
139 std::pair<Symbol *, bool> insert(StringRef name);
140 /// Same as insert(Name), but also sets isUsedInRegularObj.
141 std::pair<Symbol *, bool> insert(StringRef name, InputFile *f);
143 std::vector<Symbol *> getSymsWithPrefix(StringRef prefix);
145 llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> symMap;
146 std::unique_ptr<BitcodeCompiler> lto;
147 bool ltoCompilationDone = false;
148 std::vector<std::pair<Symbol *, Symbol *>> entryThunks;
149 llvm::DenseMap<Symbol *, Symbol *> exitThunks;
151 COFFLinkerContext &ctx;
154 std::vector<std::string> getSymbolLocations(ObjFile *file, uint32_t symIndex);
156 StringRef ltrim1(StringRef s, const char *chars);
158 } // namespace lld::coff
160 #endif