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 23*bdd1243dSDimitry Andric namespace lld::coff { 240b57cec5SDimitry Andric 250b57cec5SDimitry Andric class Chunk; 260b57cec5SDimitry Andric class CommonChunk; 27349cc55cSDimitry Andric class COFFLinkerContext; 280b57cec5SDimitry Andric class Defined; 290b57cec5SDimitry Andric class DefinedAbsolute; 300b57cec5SDimitry Andric class DefinedRegular; 310b57cec5SDimitry Andric class DefinedRelative; 3285868e8aSDimitry 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: 50*bdd1243dSDimitry Andric SymbolTable(COFFLinkerContext &c) : ctx(c) {} 51349cc55cSDimitry Andric 520b57cec5SDimitry Andric void addFile(InputFile *file); 530b57cec5SDimitry Andric 5485868e8aSDimitry Andric // Emit errors for symbols that cannot be resolved. 5585868e8aSDimitry Andric void reportUnresolvable(); 5685868e8aSDimitry Andric 570b57cec5SDimitry Andric // Try to resolve any undefined symbols and update the symbol table 580b57cec5SDimitry Andric // accordingly, then print an error message for any remaining undefined 5985868e8aSDimitry Andric // symbols and warn about imported local symbols. 6085868e8aSDimitry Andric void resolveRemainingUndefines(); 610b57cec5SDimitry Andric 62fe6060f1SDimitry Andric // Load lazy objects that are needed for MinGW automatic import and for 63fe6060f1SDimitry Andric // doing stdcall fixups. 64fe6060f1SDimitry Andric void loadMinGWSymbols(); 650b57cec5SDimitry Andric bool handleMinGWAutomaticImport(Symbol *sym, StringRef name); 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric // Returns a list of chunks of selected symbols. 68349cc55cSDimitry Andric std::vector<Chunk *> getChunks() const; 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric // Returns a symbol for a given name. Returns a nullptr if not found. 71349cc55cSDimitry Andric Symbol *find(StringRef name) const; 72349cc55cSDimitry Andric Symbol *findUnderscore(StringRef name) const; 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric // Occasionally we have to resolve an undefined symbol to its 750b57cec5SDimitry Andric // mangled symbol. This function tries to find a mangled name 760b57cec5SDimitry Andric // for U from the symbol table, and if found, set the symbol as 770b57cec5SDimitry Andric // a weak alias for U. 780b57cec5SDimitry Andric Symbol *findMangle(StringRef name); 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric // Build a set of COFF objects representing the combined contents of 810b57cec5SDimitry Andric // BitcodeFiles and add them to the symbol table. Called after all files are 820b57cec5SDimitry Andric // added and before the writer writes results to a file. 83349cc55cSDimitry Andric void compileBitcodeFiles(); 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric // Creates an Undefined symbol for a given name. 860b57cec5SDimitry Andric Symbol *addUndefined(StringRef name); 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric Symbol *addSynthetic(StringRef n, Chunk *c); 890b57cec5SDimitry Andric Symbol *addAbsolute(StringRef n, uint64_t va); 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric Symbol *addUndefined(StringRef name, InputFile *f, bool isWeakAlias); 9285868e8aSDimitry Andric void addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym); 9304eeddc0SDimitry Andric void addLazyObject(InputFile *f, StringRef n); 94fe6060f1SDimitry Andric void addLazyDLLSymbol(DLLFile *f, DLLFile::Symbol *sym, StringRef n); 950b57cec5SDimitry Andric Symbol *addAbsolute(StringRef n, COFFSymbolRef s); 960b57cec5SDimitry Andric Symbol *addRegular(InputFile *f, StringRef n, 970b57cec5SDimitry Andric const llvm::object::coff_symbol_generic *s = nullptr, 98*bdd1243dSDimitry Andric SectionChunk *c = nullptr, uint32_t sectionOffset = 0, 99*bdd1243dSDimitry Andric bool isWeak = false); 1000b57cec5SDimitry Andric std::pair<DefinedRegular *, bool> 1010b57cec5SDimitry Andric addComdat(InputFile *f, StringRef n, 1020b57cec5SDimitry Andric const llvm::object::coff_symbol_generic *s = nullptr); 1030b57cec5SDimitry Andric Symbol *addCommon(InputFile *f, StringRef n, uint64_t size, 1040b57cec5SDimitry Andric const llvm::object::coff_symbol_generic *s = nullptr, 1050b57cec5SDimitry Andric CommonChunk *c = nullptr); 1060b57cec5SDimitry Andric Symbol *addImportData(StringRef n, ImportFile *f); 1070b57cec5SDimitry Andric Symbol *addImportThunk(StringRef name, DefinedImportData *s, 1080b57cec5SDimitry Andric uint16_t machine); 1090b57cec5SDimitry Andric void addLibcall(StringRef name); 1100b57cec5SDimitry Andric 11185868e8aSDimitry Andric void reportDuplicate(Symbol *existing, InputFile *newFile, 11285868e8aSDimitry Andric SectionChunk *newSc = nullptr, 11385868e8aSDimitry Andric uint32_t newSectionOffset = 0); 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric // A list of chunks which to be added to .rdata. 1160b57cec5SDimitry Andric std::vector<Chunk *> localImportChunks; 1170b57cec5SDimitry Andric 1180b57cec5SDimitry Andric // Iterates symbols in non-determinstic hash table order. 1190b57cec5SDimitry Andric template <typename T> void forEachSymbol(T callback) { 1200b57cec5SDimitry Andric for (auto &pair : symMap) 1210b57cec5SDimitry Andric callback(pair.second); 1220b57cec5SDimitry Andric } 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric private: 12585868e8aSDimitry Andric /// Given a name without "__imp_" prefix, returns a defined symbol 12685868e8aSDimitry Andric /// with the "__imp_" prefix, if it exists. 12785868e8aSDimitry Andric Defined *impSymbol(StringRef name); 1280b57cec5SDimitry Andric /// Inserts symbol if not already present. 1290b57cec5SDimitry Andric std::pair<Symbol *, bool> insert(StringRef name); 1300b57cec5SDimitry Andric /// Same as insert(Name), but also sets isUsedInRegularObj. 1310b57cec5SDimitry Andric std::pair<Symbol *, bool> insert(StringRef name, InputFile *f); 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric std::vector<Symbol *> getSymsWithPrefix(StringRef prefix); 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> symMap; 1360b57cec5SDimitry Andric std::unique_ptr<BitcodeCompiler> lto; 1370b57cec5SDimitry Andric 138349cc55cSDimitry Andric COFFLinkerContext &ctx; 139349cc55cSDimitry Andric }; 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric std::vector<std::string> getSymbolLocations(ObjFile *file, uint32_t symIndex); 1420b57cec5SDimitry Andric 143fe6060f1SDimitry Andric StringRef ltrim1(StringRef s, const char *chars); 144fe6060f1SDimitry Andric 145*bdd1243dSDimitry Andric } // namespace lld::coff 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric #endif 148