[mlir][py] Enable loading only specified dialects during creation. (#121421)
[llvm-project.git] / lld / COFF / SymbolTable.h
blob5443815172dfd9dd5f1d72d36e81a0c8270b7fcb
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,
51 llvm::COFF::MachineTypes machine = IMAGE_FILE_MACHINE_UNKNOWN)
52 : ctx(c), machine(machine) {}
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 COFFLinkerContext &ctx;
123 llvm::COFF::MachineTypes machine;
125 bool isEC() const { return machine == ARM64EC; }
127 // A list of chunks which to be added to .rdata.
128 std::vector<Chunk *> localImportChunks;
130 // A list of EC EXP+ symbols.
131 std::vector<Symbol *> expSymbols;
133 // Iterates symbols in non-determinstic hash table order.
134 template <typename T> void forEachSymbol(T callback) {
135 for (auto &pair : symMap)
136 callback(pair.second);
139 DefinedRegular *loadConfigSym = nullptr;
140 uint32_t loadConfigSize = 0;
141 void initializeLoadConfig();
143 private:
144 /// Given a name without "__imp_" prefix, returns a defined symbol
145 /// with the "__imp_" prefix, if it exists.
146 Defined *impSymbol(StringRef name);
147 /// Inserts symbol if not already present.
148 std::pair<Symbol *, bool> insert(StringRef name);
149 /// Same as insert(Name), but also sets isUsedInRegularObj.
150 std::pair<Symbol *, bool> insert(StringRef name, InputFile *f);
152 std::vector<Symbol *> getSymsWithPrefix(StringRef prefix);
154 llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> symMap;
155 std::unique_ptr<BitcodeCompiler> lto;
156 std::vector<std::pair<Symbol *, Symbol *>> entryThunks;
157 llvm::DenseMap<Symbol *, Symbol *> exitThunks;
160 std::vector<std::string> getSymbolLocations(ObjFile *file, uint32_t symIndex);
162 StringRef ltrim1(StringRef s, const char *chars);
164 } // namespace lld::coff
166 #endif