1 //===- SymbolTable.h --------------------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_COFF_SYMBOL_TABLE_H
11 #define LLD_COFF_SYMBOL_TABLE_H
13 #include "InputFiles.h"
15 #include "llvm/ADT/CachedHashString.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/DenseMapInfo.h"
18 #include "llvm/Support/raw_ostream.h"
21 struct LTOCodeGenerator;
30 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 // Try to resolve any undefined symbols and update the symbol table
53 // accordingly, then print an error message for any remaining undefined
55 void reportRemainingUndefines();
57 void loadMinGWAutomaticImports();
58 bool handleMinGWAutomaticImport(Symbol *Sym, StringRef Name);
60 // Returns a list of chunks of selected symbols.
61 std::vector<Chunk *> getChunks();
63 // Returns a symbol for a given name. Returns a nullptr if not found.
64 Symbol *find(StringRef Name);
65 Symbol *findUnderscore(StringRef Name);
67 // Occasionally we have to resolve an undefined symbol to its
68 // mangled symbol. This function tries to find a mangled name
69 // for U from the symbol table, and if found, set the symbol as
70 // a weak alias for U.
71 void mangleMaybe(Symbol *B);
72 StringRef findMangle(StringRef Name);
74 // Build a set of COFF objects representing the combined contents of
75 // BitcodeFiles and add them to the symbol table. Called after all files are
76 // added and before the writer writes results to a file.
77 void addCombinedLTOObjects();
78 std::vector<StringRef> compileBitcodeFiles();
80 // Creates an Undefined symbol for a given name.
81 Symbol *addUndefined(StringRef Name);
83 Symbol *addSynthetic(StringRef N, Chunk *C);
84 Symbol *addAbsolute(StringRef N, uint64_t VA);
86 Symbol *addUndefined(StringRef Name, InputFile *F, bool IsWeakAlias);
87 void addLazy(ArchiveFile *F, const Archive::Symbol Sym);
88 Symbol *addAbsolute(StringRef N, COFFSymbolRef S);
89 Symbol *addRegular(InputFile *F, StringRef N,
90 const llvm::object::coff_symbol_generic *S = nullptr,
91 SectionChunk *C = nullptr);
92 std::pair<Symbol *, bool>
93 addComdat(InputFile *F, StringRef N,
94 const llvm::object::coff_symbol_generic *S = nullptr);
95 Symbol *addCommon(InputFile *F, StringRef N, uint64_t Size,
96 const llvm::object::coff_symbol_generic *S = nullptr,
97 CommonChunk *C = nullptr);
98 Symbol *addImportData(StringRef N, ImportFile *F);
99 Symbol *addImportThunk(StringRef Name, DefinedImportData *S,
102 void reportDuplicate(Symbol *Existing, InputFile *NewFile);
104 // A list of chunks which to be added to .rdata.
105 std::vector<Chunk *> LocalImportChunks;
107 // Iterates symbols in non-determinstic hash table order.
108 template <typename T> void forEachSymbol(T Callback) {
109 for (auto &Pair : SymMap)
110 Callback(Pair.second);
114 /// Inserts symbol if not already present.
115 std::pair<Symbol *, bool> insert(StringRef Name);
116 /// Same as insert(Name), but also sets IsUsedInRegularObj.
117 std::pair<Symbol *, bool> insert(StringRef Name, InputFile *F);
118 StringRef findByPrefix(StringRef Prefix);
120 llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> SymMap;
121 std::unique_ptr<BitcodeCompiler> LTO;
124 extern SymbolTable *Symtab;
126 std::string getSymbolLocations(ObjFile *File, uint32_t SymIndex);