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 // Returns a list of chunks of selected symbols.
58 std::vector<Chunk *> getChunks();
60 // Returns a symbol for a given name. Returns a nullptr if not found.
61 Symbol *find(StringRef Name);
62 Symbol *findUnderscore(StringRef Name);
64 // Occasionally we have to resolve an undefined symbol to its
65 // mangled symbol. This function tries to find a mangled name
66 // for U from the symbol table, and if found, set the symbol as
67 // a weak alias for U.
68 void mangleMaybe(Symbol *B);
69 StringRef findMangle(StringRef Name);
71 // Build a set of COFF objects representing the combined contents of
72 // BitcodeFiles and add them to the symbol table. Called after all files are
73 // added and before the writer writes results to a file.
74 void addCombinedLTOObjects();
75 std::vector<StringRef> compileBitcodeFiles();
77 // Creates an Undefined symbol for a given name.
78 Symbol *addUndefined(StringRef Name);
80 Symbol *addSynthetic(StringRef N, Chunk *C);
81 Symbol *addAbsolute(StringRef N, uint64_t VA);
83 Symbol *addUndefined(StringRef Name, InputFile *F, bool IsWeakAlias);
84 void addLazy(ArchiveFile *F, const Archive::Symbol Sym);
85 Symbol *addAbsolute(StringRef N, COFFSymbolRef S);
86 Symbol *addRegular(InputFile *F, StringRef N,
87 const llvm::object::coff_symbol_generic *S = nullptr,
88 SectionChunk *C = nullptr);
89 std::pair<Symbol *, bool>
90 addComdat(InputFile *F, StringRef N,
91 const llvm::object::coff_symbol_generic *S = nullptr);
92 Symbol *addCommon(InputFile *F, StringRef N, uint64_t Size,
93 const llvm::object::coff_symbol_generic *S = nullptr,
94 CommonChunk *C = nullptr);
95 DefinedImportData *addImportData(StringRef N, ImportFile *F);
96 DefinedImportThunk *addImportThunk(StringRef Name, DefinedImportData *S,
99 void reportDuplicate(Symbol *Existing, InputFile *NewFile);
101 // A list of chunks which to be added to .rdata.
102 std::vector<Chunk *> LocalImportChunks;
104 // Iterates symbols in non-determinstic hash table order.
105 template <typename T> void forEachSymbol(T Callback) {
106 for (auto &Pair : SymMap)
107 Callback(Pair.second);
111 std::pair<Symbol *, bool> insert(StringRef Name);
112 StringRef findByPrefix(StringRef Prefix);
114 llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> SymMap;
115 std::unique_ptr<BitcodeCompiler> LTO;
118 extern SymbolTable *Symtab;