Path: blob/main/contrib/llvm-project/lld/COFF/SymbolTable.h
34870 views
//===- SymbolTable.h --------------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLD_COFF_SYMBOL_TABLE_H9#define LLD_COFF_SYMBOL_TABLE_H1011#include "InputFiles.h"12#include "LTO.h"13#include "llvm/ADT/CachedHashString.h"14#include "llvm/ADT/DenseMap.h"15#include "llvm/ADT/DenseMapInfo.h"16#include "llvm/Support/raw_ostream.h"1718namespace llvm {19struct LTOCodeGenerator;20}2122namespace lld::coff {2324class Chunk;25class CommonChunk;26class COFFLinkerContext;27class Defined;28class DefinedAbsolute;29class DefinedRegular;30class LazyArchive;31class SectionChunk;32class Symbol;3334// SymbolTable is a bucket of all known symbols, including defined,35// undefined, or lazy symbols (the last one is symbols in archive36// files whose archive members are not yet loaded).37//38// We put all symbols of all files to a SymbolTable, and the39// SymbolTable selects the "best" symbols if there are name40// conflicts. For example, obviously, a defined symbol is better than41// an undefined symbol. Or, if there's a conflict between a lazy and a42// undefined, it'll read an archive member to read a real definition43// to replace the lazy symbol. The logic is implemented in the44// add*() functions, which are called by input files as they are parsed.45// There is one add* function per symbol type.46class SymbolTable {47public:48SymbolTable(COFFLinkerContext &c) : ctx(c) {}4950void addFile(InputFile *file);5152// Emit errors for symbols that cannot be resolved.53void reportUnresolvable();5455// Try to resolve any undefined symbols and update the symbol table56// accordingly, then print an error message for any remaining undefined57// symbols and warn about imported local symbols.58void resolveRemainingUndefines();5960// Load lazy objects that are needed for MinGW automatic import and for61// doing stdcall fixups.62void loadMinGWSymbols();63bool handleMinGWAutomaticImport(Symbol *sym, StringRef name);6465// Returns a list of chunks of selected symbols.66std::vector<Chunk *> getChunks() const;6768// Returns a symbol for a given name. Returns a nullptr if not found.69Symbol *find(StringRef name) const;70Symbol *findUnderscore(StringRef name) const;7172// Occasionally we have to resolve an undefined symbol to its73// mangled symbol. This function tries to find a mangled name74// for U from the symbol table, and if found, set the symbol as75// a weak alias for U.76Symbol *findMangle(StringRef name);7778// Build a set of COFF objects representing the combined contents of79// BitcodeFiles and add them to the symbol table. Called after all files are80// added and before the writer writes results to a file.81void compileBitcodeFiles();8283// Creates an Undefined symbol for a given name.84Symbol *addUndefined(StringRef name);8586Symbol *addSynthetic(StringRef n, Chunk *c);87Symbol *addAbsolute(StringRef n, uint64_t va);8889Symbol *addUndefined(StringRef name, InputFile *f, bool isWeakAlias);90void addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym);91void addLazyObject(InputFile *f, StringRef n);92void addLazyDLLSymbol(DLLFile *f, DLLFile::Symbol *sym, StringRef n);93Symbol *addAbsolute(StringRef n, COFFSymbolRef s);94Symbol *addRegular(InputFile *f, StringRef n,95const llvm::object::coff_symbol_generic *s = nullptr,96SectionChunk *c = nullptr, uint32_t sectionOffset = 0,97bool isWeak = false);98std::pair<DefinedRegular *, bool>99addComdat(InputFile *f, StringRef n,100const llvm::object::coff_symbol_generic *s = nullptr);101Symbol *addCommon(InputFile *f, StringRef n, uint64_t size,102const llvm::object::coff_symbol_generic *s = nullptr,103CommonChunk *c = nullptr);104Symbol *addImportData(StringRef n, ImportFile *f);105Symbol *addImportThunk(StringRef name, DefinedImportData *s,106uint16_t machine);107void addLibcall(StringRef name);108void addEntryThunk(Symbol *from, Symbol *to);109void initializeEntryThunks();110111void reportDuplicate(Symbol *existing, InputFile *newFile,112SectionChunk *newSc = nullptr,113uint32_t newSectionOffset = 0);114115// A list of chunks which to be added to .rdata.116std::vector<Chunk *> localImportChunks;117118// Iterates symbols in non-determinstic hash table order.119template <typename T> void forEachSymbol(T callback) {120for (auto &pair : symMap)121callback(pair.second);122}123124private:125/// Given a name without "__imp_" prefix, returns a defined symbol126/// with the "__imp_" prefix, if it exists.127Defined *impSymbol(StringRef name);128/// Inserts symbol if not already present.129std::pair<Symbol *, bool> insert(StringRef name);130/// Same as insert(Name), but also sets isUsedInRegularObj.131std::pair<Symbol *, bool> insert(StringRef name, InputFile *f);132133std::vector<Symbol *> getSymsWithPrefix(StringRef prefix);134135llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> symMap;136std::unique_ptr<BitcodeCompiler> lto;137bool ltoCompilationDone = false;138std::vector<std::pair<Symbol *, Symbol *>> entryThunks;139140COFFLinkerContext &ctx;141};142143std::vector<std::string> getSymbolLocations(ObjFile *file, uint32_t symIndex);144145StringRef ltrim1(StringRef s, const char *chars);146147} // namespace lld::coff148149#endif150151152