1 //===- SymbolTable.h --------------------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_COFF_SYMBOL_TABLE_H
11 #define LLD_COFF_SYMBOL_TABLE_H
13 #include "InputFiles.h"
14 #include "llvm/ADT/CachedHashString.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/DenseMapInfo.h"
17 #include "llvm/Support/Allocator.h"
18 #include "llvm/Support/raw_ostream.h"
21 struct LTOCodeGenerator;
30 class DefinedAbsolute;
31 class DefinedRelative;
37 // SymbolTable is a bucket of all known symbols, including defined,
38 // undefined, or lazy symbols (the last one is symbols in archive
39 // files whose archive members are not yet loaded).
41 // We put all symbols of all files to a SymbolTable, and the
42 // SymbolTable selects the "best" symbols if there are name
43 // conflicts. For example, obviously, a defined symbol is better than
44 // an undefined symbol. Or, if there's a conflict between a lazy and a
45 // undefined, it'll read an archive member to read a real definition
46 // to replace the lazy symbol. The logic is implemented in the
47 // add*() functions, which are called by input files as they are parsed.
48 // There is one add* function per symbol type.
51 void addFile(InputFile *File);
53 // Try to resolve any undefined symbols and update the symbol table
54 // accordingly, then print an error message for any remaining undefined
56 void reportRemainingUndefines();
58 // Returns a list of chunks of selected symbols.
59 std::vector<Chunk *> getChunks();
61 // Returns a symbol for a given name. Returns a nullptr if not found.
62 Symbol *find(StringRef Name);
63 Symbol *findUnderscore(StringRef Name);
65 // Occasionally we have to resolve an undefined symbol to its
66 // mangled symbol. This function tries to find a mangled name
67 // for U from the symbol table, and if found, set the symbol as
68 // a weak alias for U.
69 void mangleMaybe(SymbolBody *B);
70 StringRef findMangle(StringRef Name);
72 // Print a layout map to OS.
73 void printMap(llvm::raw_ostream &OS);
75 // Build a set of COFF objects representing the combined contents of
76 // BitcodeFiles and add them to the symbol table. Called after all files are
77 // added and before the writer writes results to a file.
78 void addCombinedLTOObjects();
80 // The writer needs to handle DLL import libraries specially in
81 // order to create the import descriptor table.
82 std::vector<ImportFile *> ImportFiles;
84 // The writer needs to infer the machine type from the object files.
85 std::vector<ObjectFile *> ObjectFiles;
87 // Creates an Undefined symbol for a given name.
88 SymbolBody *addUndefined(StringRef Name);
90 Symbol *addRelative(StringRef N, uint64_t VA);
91 Symbol *addAbsolute(StringRef N, uint64_t VA);
93 Symbol *addUndefined(StringRef Name, InputFile *F, bool IsWeakAlias);
94 void addLazy(ArchiveFile *F, const Archive::Symbol Sym);
95 Symbol *addAbsolute(StringRef N, COFFSymbolRef S);
96 Symbol *addRegular(ObjectFile *F, COFFSymbolRef S, SectionChunk *C);
97 Symbol *addBitcode(BitcodeFile *F, StringRef N, bool IsReplaceable);
98 Symbol *addCommon(ObjectFile *F, COFFSymbolRef S, CommonChunk *C);
99 Symbol *addImportData(StringRef N, ImportFile *F);
100 Symbol *addImportThunk(StringRef Name, DefinedImportData *S,
103 void reportDuplicate(Symbol *Existing, InputFile *NewFile);
105 // A list of chunks which to be added to .rdata.
106 std::vector<Chunk *> LocalImportChunks;
112 std::pair<Symbol *, bool> insert(StringRef Name);
113 StringRef findByPrefix(StringRef Prefix);
115 void addCombinedLTOObject(ObjectFile *Obj);
116 std::vector<ObjectFile *> createLTOObjects(llvm::LTOCodeGenerator *CG);
118 llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> Symtab;
120 std::vector<BitcodeFile *> BitcodeFiles;
121 std::vector<SmallString<0>> Objs;
124 extern SymbolTable *Symtab;