SymbolTable.h revision 360784
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//===----------------------------------------------------------------------===// 8 9#ifndef LLD_COFF_SYMBOL_TABLE_H 10#define LLD_COFF_SYMBOL_TABLE_H 11 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" 18 19namespace llvm { 20struct LTOCodeGenerator; 21} 22 23namespace lld { 24namespace coff { 25 26class Chunk; 27class CommonChunk; 28class Defined; 29class DefinedAbsolute; 30class DefinedRegular; 31class DefinedRelative; 32class LazyArchive; 33class SectionChunk; 34class Symbol; 35 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). 39// 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. 48class SymbolTable { 49public: 50 void addFile(InputFile *file); 51 52 // Emit errors for symbols that cannot be resolved. 53 void reportUnresolvable(); 54 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(); 59 60 void loadMinGWAutomaticImports(); 61 bool handleMinGWAutomaticImport(Symbol *sym, StringRef name); 62 63 // Returns a list of chunks of selected symbols. 64 std::vector<Chunk *> getChunks(); 65 66 // Returns a symbol for a given name. Returns a nullptr if not found. 67 Symbol *find(StringRef name); 68 Symbol *findUnderscore(StringRef name); 69 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); 75 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(); 81 82 // Creates an Undefined symbol for a given name. 83 Symbol *addUndefined(StringRef name); 84 85 Symbol *addSynthetic(StringRef n, Chunk *c); 86 Symbol *addAbsolute(StringRef n, uint64_t va); 87 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, 103 uint16_t machine); 104 void addLibcall(StringRef name); 105 106 void reportDuplicate(Symbol *existing, InputFile *newFile, 107 SectionChunk *newSc = nullptr, 108 uint32_t newSectionOffset = 0); 109 110 // A list of chunks which to be added to .rdata. 111 std::vector<Chunk *> localImportChunks; 112 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); 117 } 118 119private: 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); 127 128 std::vector<Symbol *> getSymsWithPrefix(StringRef prefix); 129 130 llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> symMap; 131 std::unique_ptr<BitcodeCompiler> lto; 132}; 133 134extern SymbolTable *symtab; 135 136std::vector<std::string> getSymbolLocations(ObjFile *file, uint32_t symIndex); 137 138} // namespace coff 139} // namespace lld 140 141#endif 142