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 // 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 Symbol *findMangle(StringRef name);
73 // Build a set of COFF objects representing the combined contents of
74 // BitcodeFiles and add them to the symbol table. Called after all files are
75 // added and before the writer writes results to a file.
76 void addCombinedLTOObjects();
77 std::vector<StringRef> compileBitcodeFiles();
79 // Creates an Undefined symbol for a given name.
80 Symbol *addUndefined(StringRef name);
82 Symbol *addSynthetic(StringRef n, Chunk *c);
83 Symbol *addAbsolute(StringRef n, uint64_t va);
85 Symbol *addUndefined(StringRef name, InputFile *f, bool isWeakAlias);
86 void addLazy(ArchiveFile *f, const Archive::Symbol &sym);
87 Symbol *addAbsolute(StringRef n, COFFSymbolRef s);
88 Symbol *addRegular(InputFile *f, StringRef n,
89 const llvm::object::coff_symbol_generic *s = nullptr,
90 SectionChunk *c = nullptr);
91 std::pair<DefinedRegular *, bool>
92 addComdat(InputFile *f, StringRef n,
93 const llvm::object::coff_symbol_generic *s = nullptr);
94 Symbol *addCommon(InputFile *f, StringRef n, uint64_t size,
95 const llvm::object::coff_symbol_generic *s = nullptr,
96 CommonChunk *c = nullptr);
97 Symbol *addImportData(StringRef n, ImportFile *f);
98 Symbol *addImportThunk(StringRef name, DefinedImportData *s,
100 void addLibcall(StringRef name);
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);
119 std::vector<Symbol *> getSymsWithPrefix(StringRef prefix);
121 llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> symMap;
122 std::unique_ptr<BitcodeCompiler> lto;
125 extern SymbolTable *symtab;
127 std::vector<std::string> getSymbolLocations(ObjFile *file, uint32_t symIndex);