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; 28*349cc55cSDimitry Andric class COFFLinkerContext; 290b57cec5SDimitry Andric class Defined; 300b57cec5SDimitry Andric class DefinedAbsolute; 310b57cec5SDimitry Andric class DefinedRegular; 320b57cec5SDimitry Andric class DefinedRelative; 3385868e8aSDimitry Andric class LazyArchive; 340b57cec5SDimitry Andric class SectionChunk; 350b57cec5SDimitry Andric class Symbol; 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric // SymbolTable is a bucket of all known symbols, including defined, 380b57cec5SDimitry Andric // undefined, or lazy symbols (the last one is symbols in archive 390b57cec5SDimitry Andric // files whose archive members are not yet loaded). 400b57cec5SDimitry Andric // 410b57cec5SDimitry Andric // We put all symbols of all files to a SymbolTable, and the 420b57cec5SDimitry Andric // SymbolTable selects the "best" symbols if there are name 430b57cec5SDimitry Andric // conflicts. For example, obviously, a defined symbol is better than 440b57cec5SDimitry Andric // an undefined symbol. Or, if there's a conflict between a lazy and a 450b57cec5SDimitry Andric // undefined, it'll read an archive member to read a real definition 460b57cec5SDimitry Andric // to replace the lazy symbol. The logic is implemented in the 470b57cec5SDimitry Andric // add*() functions, which are called by input files as they are parsed. 480b57cec5SDimitry Andric // There is one add* function per symbol type. 490b57cec5SDimitry Andric class SymbolTable { 500b57cec5SDimitry Andric public: 51*349cc55cSDimitry Andric SymbolTable(COFFLinkerContext &ctx) : ctx(ctx) {} 52*349cc55cSDimitry Andric 530b57cec5SDimitry Andric void addFile(InputFile *file); 540b57cec5SDimitry Andric 5585868e8aSDimitry Andric // Emit errors for symbols that cannot be resolved. 5685868e8aSDimitry Andric void reportUnresolvable(); 5785868e8aSDimitry Andric 580b57cec5SDimitry Andric // Try to resolve any undefined symbols and update the symbol table 590b57cec5SDimitry Andric // accordingly, then print an error message for any remaining undefined 6085868e8aSDimitry Andric // symbols and warn about imported local symbols. 6185868e8aSDimitry Andric void resolveRemainingUndefines(); 620b57cec5SDimitry Andric 63fe6060f1SDimitry Andric // Load lazy objects that are needed for MinGW automatic import and for 64fe6060f1SDimitry Andric // doing stdcall fixups. 65fe6060f1SDimitry Andric void loadMinGWSymbols(); 660b57cec5SDimitry Andric bool handleMinGWAutomaticImport(Symbol *sym, StringRef name); 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric // Returns a list of chunks of selected symbols. 69*349cc55cSDimitry Andric std::vector<Chunk *> getChunks() const; 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric // Returns a symbol for a given name. Returns a nullptr if not found. 72*349cc55cSDimitry Andric Symbol *find(StringRef name) const; 73*349cc55cSDimitry Andric Symbol *findUnderscore(StringRef name) const; 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric // Occasionally we have to resolve an undefined symbol to its 760b57cec5SDimitry Andric // mangled symbol. This function tries to find a mangled name 770b57cec5SDimitry Andric // for U from the symbol table, and if found, set the symbol as 780b57cec5SDimitry Andric // a weak alias for U. 790b57cec5SDimitry Andric Symbol *findMangle(StringRef name); 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric // Build a set of COFF objects representing the combined contents of 820b57cec5SDimitry Andric // BitcodeFiles and add them to the symbol table. Called after all files are 830b57cec5SDimitry Andric // added and before the writer writes results to a file. 84*349cc55cSDimitry Andric void compileBitcodeFiles(); 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric // Creates an Undefined symbol for a given name. 870b57cec5SDimitry Andric Symbol *addUndefined(StringRef name); 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric Symbol *addSynthetic(StringRef n, Chunk *c); 900b57cec5SDimitry Andric Symbol *addAbsolute(StringRef n, uint64_t va); 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric Symbol *addUndefined(StringRef name, InputFile *f, bool isWeakAlias); 9385868e8aSDimitry Andric void addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym); 9485868e8aSDimitry Andric void addLazyObject(LazyObjFile *f, StringRef n); 95fe6060f1SDimitry Andric void addLazyDLLSymbol(DLLFile *f, DLLFile::Symbol *sym, StringRef n); 960b57cec5SDimitry Andric Symbol *addAbsolute(StringRef n, COFFSymbolRef s); 970b57cec5SDimitry Andric Symbol *addRegular(InputFile *f, StringRef n, 980b57cec5SDimitry Andric const llvm::object::coff_symbol_generic *s = nullptr, 9985868e8aSDimitry Andric SectionChunk *c = nullptr, uint32_t sectionOffset = 0); 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 138*349cc55cSDimitry Andric COFFLinkerContext &ctx; 139*349cc55cSDimitry 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 1450b57cec5SDimitry Andric } // namespace coff 1460b57cec5SDimitry Andric } // namespace lld 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric #endif 149