1 //===- SymbolTable.h --------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_COFF_SYMBOL_TABLE_H
10 #define LLD_COFF_SYMBOL_TABLE_H
12 #include "InputFiles.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"
20 struct LTOCodeGenerator;
29 class DefinedAbsolute;
31 class DefinedRelative;
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.
50 void addFile(InputFile *file);
52 // Emit errors for symbols that cannot be resolved.
53 void reportUnresolvable();
55 // Try to resolve any undefined symbols and update the symbol table
56 // accordingly, then print an error message for any remaining undefined
57 // symbols and warn about imported local symbols.
58 void resolveRemainingUndefines();
60 void loadMinGWAutomaticImports();
61 bool handleMinGWAutomaticImport(Symbol *sym, StringRef name);
63 // Returns a list of chunks of selected symbols.
64 std::vector<Chunk *> getChunks();
66 // Returns a symbol for a given name. Returns a nullptr if not found.
67 Symbol *find(StringRef name);
68 Symbol *findUnderscore(StringRef name);
70 // Occasionally we have to resolve an undefined symbol to its
71 // mangled symbol. This function tries to find a mangled name
72 // for U from the symbol table, and if found, set the symbol as
73 // a weak alias for U.
74 Symbol *findMangle(StringRef name);
76 // Build a set of COFF objects representing the combined contents of
77 // BitcodeFiles and add them to the symbol table. Called after all files are
78 // added and before the writer writes results to a file.
79 void addCombinedLTOObjects();
80 std::vector<StringRef> compileBitcodeFiles();
82 // Creates an Undefined symbol for a given name.
83 Symbol *addUndefined(StringRef name);
85 Symbol *addSynthetic(StringRef n, Chunk *c);
86 Symbol *addAbsolute(StringRef n, uint64_t va);
88 Symbol *addUndefined(StringRef name, InputFile *f, bool isWeakAlias);
89 void addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym);
90 void addLazyObject(LazyObjFile *f, StringRef n);
91 Symbol *addAbsolute(StringRef n, COFFSymbolRef s);
92 Symbol *addRegular(InputFile *f, StringRef n,
93 const llvm::object::coff_symbol_generic *s = nullptr,
94 SectionChunk *c = nullptr, uint32_t sectionOffset = 0);
95 std::pair<DefinedRegular *, bool>
96 addComdat(InputFile *f, StringRef n,
97 const llvm::object::coff_symbol_generic *s = nullptr);
98 Symbol *addCommon(InputFile *f, StringRef n, uint64_t size,
99 const llvm::object::coff_symbol_generic *s = nullptr,
100 CommonChunk *c = nullptr);
101 Symbol *addImportData(StringRef n, ImportFile *f);
102 Symbol *addImportThunk(StringRef name, DefinedImportData *s,
104 void addLibcall(StringRef name);
106 void reportDuplicate(Symbol *existing, InputFile *newFile,
107 SectionChunk *newSc = nullptr,
108 uint32_t newSectionOffset = 0);
110 // A list of chunks which to be added to .rdata.
111 std::vector<Chunk *> localImportChunks;
113 // Iterates symbols in non-determinstic hash table order.
114 template <typename T> void forEachSymbol(T callback) {
115 for (auto &pair : symMap)
116 callback(pair.second);
120 /// Given a name without "__imp_" prefix, returns a defined symbol
121 /// with the "__imp_" prefix, if it exists.
122 Defined *impSymbol(StringRef name);
123 /// Inserts symbol if not already present.
124 std::pair<Symbol *, bool> insert(StringRef name);
125 /// Same as insert(Name), but also sets isUsedInRegularObj.
126 std::pair<Symbol *, bool> insert(StringRef name, InputFile *f);
128 std::vector<Symbol *> getSymsWithPrefix(StringRef prefix);
130 llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> symMap;
131 std::unique_ptr<BitcodeCompiler> lto;
134 extern SymbolTable *symtab;
136 std::vector<std::string> getSymbolLocations(ObjFile *file, uint32_t symIndex);