10b57cec5SDimitry Andric //===- SymbolTable.h --------------------------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #ifndef LLD_COFF_SYMBOL_TABLE_H 100b57cec5SDimitry Andric #define LLD_COFF_SYMBOL_TABLE_H 110b57cec5SDimitry Andric 120b57cec5SDimitry Andric #include "InputFiles.h" 130b57cec5SDimitry Andric #include "LTO.h" 140b57cec5SDimitry Andric #include "llvm/ADT/CachedHashString.h" 150b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 160b57cec5SDimitry Andric #include "llvm/ADT/DenseMapInfo.h" 170b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric namespace llvm { 200b57cec5SDimitry Andric struct LTOCodeGenerator; 210b57cec5SDimitry Andric } 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric namespace lld { 240b57cec5SDimitry Andric namespace coff { 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric class Chunk; 270b57cec5SDimitry Andric class CommonChunk; 280b57cec5SDimitry Andric class Defined; 290b57cec5SDimitry Andric class DefinedAbsolute; 300b57cec5SDimitry Andric class DefinedRegular; 310b57cec5SDimitry Andric class DefinedRelative; 32*85868e8aSDimitry Andric class LazyArchive; 330b57cec5SDimitry Andric class SectionChunk; 340b57cec5SDimitry Andric class Symbol; 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric // SymbolTable is a bucket of all known symbols, including defined, 370b57cec5SDimitry Andric // undefined, or lazy symbols (the last one is symbols in archive 380b57cec5SDimitry Andric // files whose archive members are not yet loaded). 390b57cec5SDimitry Andric // 400b57cec5SDimitry Andric // We put all symbols of all files to a SymbolTable, and the 410b57cec5SDimitry Andric // SymbolTable selects the "best" symbols if there are name 420b57cec5SDimitry Andric // conflicts. For example, obviously, a defined symbol is better than 430b57cec5SDimitry Andric // an undefined symbol. Or, if there's a conflict between a lazy and a 440b57cec5SDimitry Andric // undefined, it'll read an archive member to read a real definition 450b57cec5SDimitry Andric // to replace the lazy symbol. The logic is implemented in the 460b57cec5SDimitry Andric // add*() functions, which are called by input files as they are parsed. 470b57cec5SDimitry Andric // There is one add* function per symbol type. 480b57cec5SDimitry Andric class SymbolTable { 490b57cec5SDimitry Andric public: 500b57cec5SDimitry Andric void addFile(InputFile *file); 510b57cec5SDimitry Andric 52*85868e8aSDimitry Andric // Emit errors for symbols that cannot be resolved. 53*85868e8aSDimitry Andric void reportUnresolvable(); 54*85868e8aSDimitry Andric 550b57cec5SDimitry Andric // Try to resolve any undefined symbols and update the symbol table 560b57cec5SDimitry Andric // accordingly, then print an error message for any remaining undefined 57*85868e8aSDimitry Andric // symbols and warn about imported local symbols. 58*85868e8aSDimitry Andric void resolveRemainingUndefines(); 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric void loadMinGWAutomaticImports(); 610b57cec5SDimitry Andric bool handleMinGWAutomaticImport(Symbol *sym, StringRef name); 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric // Returns a list of chunks of selected symbols. 640b57cec5SDimitry Andric std::vector<Chunk *> getChunks(); 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric // Returns a symbol for a given name. Returns a nullptr if not found. 670b57cec5SDimitry Andric Symbol *find(StringRef name); 680b57cec5SDimitry Andric Symbol *findUnderscore(StringRef name); 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric // Occasionally we have to resolve an undefined symbol to its 710b57cec5SDimitry Andric // mangled symbol. This function tries to find a mangled name 720b57cec5SDimitry Andric // for U from the symbol table, and if found, set the symbol as 730b57cec5SDimitry Andric // a weak alias for U. 740b57cec5SDimitry Andric Symbol *findMangle(StringRef name); 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric // Build a set of COFF objects representing the combined contents of 770b57cec5SDimitry Andric // BitcodeFiles and add them to the symbol table. Called after all files are 780b57cec5SDimitry Andric // added and before the writer writes results to a file. 790b57cec5SDimitry Andric void addCombinedLTOObjects(); 800b57cec5SDimitry Andric std::vector<StringRef> compileBitcodeFiles(); 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric // Creates an Undefined symbol for a given name. 830b57cec5SDimitry Andric Symbol *addUndefined(StringRef name); 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric Symbol *addSynthetic(StringRef n, Chunk *c); 860b57cec5SDimitry Andric Symbol *addAbsolute(StringRef n, uint64_t va); 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric Symbol *addUndefined(StringRef name, InputFile *f, bool isWeakAlias); 89*85868e8aSDimitry Andric void addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym); 90*85868e8aSDimitry Andric void addLazyObject(LazyObjFile *f, StringRef n); 910b57cec5SDimitry Andric Symbol *addAbsolute(StringRef n, COFFSymbolRef s); 920b57cec5SDimitry Andric Symbol *addRegular(InputFile *f, StringRef n, 930b57cec5SDimitry Andric const llvm::object::coff_symbol_generic *s = nullptr, 94*85868e8aSDimitry Andric SectionChunk *c = nullptr, uint32_t sectionOffset = 0); 950b57cec5SDimitry Andric std::pair<DefinedRegular *, bool> 960b57cec5SDimitry Andric addComdat(InputFile *f, StringRef n, 970b57cec5SDimitry Andric const llvm::object::coff_symbol_generic *s = nullptr); 980b57cec5SDimitry Andric Symbol *addCommon(InputFile *f, StringRef n, uint64_t size, 990b57cec5SDimitry Andric const llvm::object::coff_symbol_generic *s = nullptr, 1000b57cec5SDimitry Andric CommonChunk *c = nullptr); 1010b57cec5SDimitry Andric Symbol *addImportData(StringRef n, ImportFile *f); 1020b57cec5SDimitry Andric Symbol *addImportThunk(StringRef name, DefinedImportData *s, 1030b57cec5SDimitry Andric uint16_t machine); 1040b57cec5SDimitry Andric void addLibcall(StringRef name); 1050b57cec5SDimitry Andric 106*85868e8aSDimitry Andric void reportDuplicate(Symbol *existing, InputFile *newFile, 107*85868e8aSDimitry Andric SectionChunk *newSc = nullptr, 108*85868e8aSDimitry Andric uint32_t newSectionOffset = 0); 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric // A list of chunks which to be added to .rdata. 1110b57cec5SDimitry Andric std::vector<Chunk *> localImportChunks; 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric // Iterates symbols in non-determinstic hash table order. 1140b57cec5SDimitry Andric template <typename T> void forEachSymbol(T callback) { 1150b57cec5SDimitry Andric for (auto &pair : symMap) 1160b57cec5SDimitry Andric callback(pair.second); 1170b57cec5SDimitry Andric } 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric private: 120*85868e8aSDimitry Andric /// Given a name without "__imp_" prefix, returns a defined symbol 121*85868e8aSDimitry Andric /// with the "__imp_" prefix, if it exists. 122*85868e8aSDimitry Andric Defined *impSymbol(StringRef name); 1230b57cec5SDimitry Andric /// Inserts symbol if not already present. 1240b57cec5SDimitry Andric std::pair<Symbol *, bool> insert(StringRef name); 1250b57cec5SDimitry Andric /// Same as insert(Name), but also sets isUsedInRegularObj. 1260b57cec5SDimitry Andric std::pair<Symbol *, bool> insert(StringRef name, InputFile *f); 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric std::vector<Symbol *> getSymsWithPrefix(StringRef prefix); 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> symMap; 1310b57cec5SDimitry Andric std::unique_ptr<BitcodeCompiler> lto; 1320b57cec5SDimitry Andric }; 1330b57cec5SDimitry Andric 1340b57cec5SDimitry Andric extern SymbolTable *symtab; 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric std::vector<std::string> getSymbolLocations(ObjFile *file, uint32_t symIndex); 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric } // namespace coff 1390b57cec5SDimitry Andric } // namespace lld 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric #endif 142