xref: /freebsd/contrib/llvm-project/lld/COFF/SymbolTable.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 
23bdd1243dSDimitry 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;
3185868e8aSDimitry Andric class LazyArchive;
320b57cec5SDimitry Andric class SectionChunk;
330b57cec5SDimitry Andric class Symbol;
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric // SymbolTable is a bucket of all known symbols, including defined,
360b57cec5SDimitry Andric // undefined, or lazy symbols (the last one is symbols in archive
370b57cec5SDimitry Andric // files whose archive members are not yet loaded).
380b57cec5SDimitry Andric //
390b57cec5SDimitry Andric // We put all symbols of all files to a SymbolTable, and the
400b57cec5SDimitry Andric // SymbolTable selects the "best" symbols if there are name
410b57cec5SDimitry Andric // conflicts. For example, obviously, a defined symbol is better than
420b57cec5SDimitry Andric // an undefined symbol. Or, if there's a conflict between a lazy and a
430b57cec5SDimitry Andric // undefined, it'll read an archive member to read a real definition
440b57cec5SDimitry Andric // to replace the lazy symbol. The logic is implemented in the
450b57cec5SDimitry Andric // add*() functions, which are called by input files as they are parsed.
460b57cec5SDimitry Andric // There is one add* function per symbol type.
470b57cec5SDimitry Andric class SymbolTable {
480b57cec5SDimitry Andric public:
SymbolTable(COFFLinkerContext & c)49bdd1243dSDimitry Andric   SymbolTable(COFFLinkerContext &c) : ctx(c) {}
50349cc55cSDimitry Andric 
510b57cec5SDimitry Andric   void addFile(InputFile *file);
520b57cec5SDimitry Andric 
5385868e8aSDimitry Andric   // Emit errors for symbols that cannot be resolved.
5485868e8aSDimitry Andric   void reportUnresolvable();
5585868e8aSDimitry Andric 
560b57cec5SDimitry Andric   // Try to resolve any undefined symbols and update the symbol table
570b57cec5SDimitry Andric   // accordingly, then print an error message for any remaining undefined
5885868e8aSDimitry Andric   // symbols and warn about imported local symbols.
5985868e8aSDimitry Andric   void resolveRemainingUndefines();
600b57cec5SDimitry Andric 
61fe6060f1SDimitry Andric   // Load lazy objects that are needed for MinGW automatic import and for
62fe6060f1SDimitry Andric   // doing stdcall fixups.
63fe6060f1SDimitry Andric   void loadMinGWSymbols();
640b57cec5SDimitry Andric   bool handleMinGWAutomaticImport(Symbol *sym, StringRef name);
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric   // Returns a list of chunks of selected symbols.
67349cc55cSDimitry Andric   std::vector<Chunk *> getChunks() const;
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric   // Returns a symbol for a given name. Returns a nullptr if not found.
70349cc55cSDimitry Andric   Symbol *find(StringRef name) const;
71349cc55cSDimitry Andric   Symbol *findUnderscore(StringRef name) const;
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   // Occasionally we have to resolve an undefined symbol to its
740b57cec5SDimitry Andric   // mangled symbol. This function tries to find a mangled name
750b57cec5SDimitry Andric   // for U from the symbol table, and if found, set the symbol as
760b57cec5SDimitry Andric   // a weak alias for U.
770b57cec5SDimitry Andric   Symbol *findMangle(StringRef name);
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric   // Build a set of COFF objects representing the combined contents of
800b57cec5SDimitry Andric   // BitcodeFiles and add them to the symbol table. Called after all files are
810b57cec5SDimitry Andric   // added and before the writer writes results to a file.
82349cc55cSDimitry Andric   void compileBitcodeFiles();
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric   // Creates an Undefined symbol for a given name.
850b57cec5SDimitry Andric   Symbol *addUndefined(StringRef name);
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric   Symbol *addSynthetic(StringRef n, Chunk *c);
880b57cec5SDimitry Andric   Symbol *addAbsolute(StringRef n, uint64_t va);
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric   Symbol *addUndefined(StringRef name, InputFile *f, bool isWeakAlias);
9185868e8aSDimitry Andric   void addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym);
9204eeddc0SDimitry Andric   void addLazyObject(InputFile *f, StringRef n);
93fe6060f1SDimitry Andric   void addLazyDLLSymbol(DLLFile *f, DLLFile::Symbol *sym, StringRef n);
940b57cec5SDimitry Andric   Symbol *addAbsolute(StringRef n, COFFSymbolRef s);
950b57cec5SDimitry Andric   Symbol *addRegular(InputFile *f, StringRef n,
960b57cec5SDimitry Andric                      const llvm::object::coff_symbol_generic *s = nullptr,
97bdd1243dSDimitry Andric                      SectionChunk *c = nullptr, uint32_t sectionOffset = 0,
98bdd1243dSDimitry Andric                      bool isWeak = false);
990b57cec5SDimitry Andric   std::pair<DefinedRegular *, bool>
1000b57cec5SDimitry Andric   addComdat(InputFile *f, StringRef n,
1010b57cec5SDimitry Andric             const llvm::object::coff_symbol_generic *s = nullptr);
1020b57cec5SDimitry Andric   Symbol *addCommon(InputFile *f, StringRef n, uint64_t size,
1030b57cec5SDimitry Andric                     const llvm::object::coff_symbol_generic *s = nullptr,
1040b57cec5SDimitry Andric                     CommonChunk *c = nullptr);
1050b57cec5SDimitry Andric   Symbol *addImportData(StringRef n, ImportFile *f);
1060b57cec5SDimitry Andric   Symbol *addImportThunk(StringRef name, DefinedImportData *s,
1070b57cec5SDimitry Andric                          uint16_t machine);
1080b57cec5SDimitry Andric   void addLibcall(StringRef name);
109*0fca6ea1SDimitry Andric   void addEntryThunk(Symbol *from, Symbol *to);
110*0fca6ea1SDimitry Andric   void initializeEntryThunks();
1110b57cec5SDimitry Andric 
11285868e8aSDimitry Andric   void reportDuplicate(Symbol *existing, InputFile *newFile,
11385868e8aSDimitry Andric                        SectionChunk *newSc = nullptr,
11485868e8aSDimitry Andric                        uint32_t newSectionOffset = 0);
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric   // A list of chunks which to be added to .rdata.
1170b57cec5SDimitry Andric   std::vector<Chunk *> localImportChunks;
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   // Iterates symbols in non-determinstic hash table order.
forEachSymbol(T callback)1200b57cec5SDimitry Andric   template <typename T> void forEachSymbol(T callback) {
1210b57cec5SDimitry Andric     for (auto &pair : symMap)
1220b57cec5SDimitry Andric       callback(pair.second);
1230b57cec5SDimitry Andric   }
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric private:
12685868e8aSDimitry Andric   /// Given a name without "__imp_" prefix, returns a defined symbol
12785868e8aSDimitry Andric   /// with the "__imp_" prefix, if it exists.
12885868e8aSDimitry Andric   Defined *impSymbol(StringRef name);
1290b57cec5SDimitry Andric   /// Inserts symbol if not already present.
1300b57cec5SDimitry Andric   std::pair<Symbol *, bool> insert(StringRef name);
1310b57cec5SDimitry Andric   /// Same as insert(Name), but also sets isUsedInRegularObj.
1320b57cec5SDimitry Andric   std::pair<Symbol *, bool> insert(StringRef name, InputFile *f);
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric   std::vector<Symbol *> getSymsWithPrefix(StringRef prefix);
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric   llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> symMap;
1370b57cec5SDimitry Andric   std::unique_ptr<BitcodeCompiler> lto;
1385f757f3fSDimitry Andric   bool ltoCompilationDone = false;
139*0fca6ea1SDimitry Andric   std::vector<std::pair<Symbol *, Symbol *>> entryThunks;
1400b57cec5SDimitry Andric 
141349cc55cSDimitry Andric   COFFLinkerContext &ctx;
142349cc55cSDimitry Andric };
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric std::vector<std::string> getSymbolLocations(ObjFile *file, uint32_t symIndex);
1450b57cec5SDimitry Andric 
146fe6060f1SDimitry Andric StringRef ltrim1(StringRef s, const char *chars);
147fe6060f1SDimitry Andric 
148bdd1243dSDimitry Andric } // namespace lld::coff
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric #endif
151