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"
15 #include "llvm/ADT/CachedHashString.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/DenseMapInfo.h"
18 #include "llvm/Support/Allocator.h"
19 #include "llvm/Support/raw_ostream.h"
22 struct LTOCodeGenerator;
31 class DefinedAbsolute;
32 class DefinedRelative;
38 // SymbolTable is a bucket of all known symbols, including defined,
39 // undefined, or lazy symbols (the last one is symbols in archive
40 // files whose archive members are not yet loaded).
42 // We put all symbols of all files to a SymbolTable, and the
43 // SymbolTable selects the "best" symbols if there are name
44 // conflicts. For example, obviously, a defined symbol is better than
45 // an undefined symbol. Or, if there's a conflict between a lazy and a
46 // undefined, it'll read an archive member to read a real definition
47 // to replace the lazy symbol. The logic is implemented in the
48 // add*() functions, which are called by input files as they are parsed.
49 // There is one add* function per symbol type.
52 void addFile(InputFile *File);
54 // Try to resolve any undefined symbols and update the symbol table
55 // accordingly, then print an error message for any remaining undefined
57 void reportRemainingUndefines();
59 // Returns a list of chunks of selected symbols.
60 std::vector<Chunk *> getChunks();
62 // Returns a symbol for a given name. Returns a nullptr if not found.
63 Symbol *find(StringRef Name);
64 Symbol *findUnderscore(StringRef Name);
66 // Occasionally we have to resolve an undefined symbol to its
67 // mangled symbol. This function tries to find a mangled name
68 // for U from the symbol table, and if found, set the symbol as
69 // a weak alias for U.
70 void mangleMaybe(SymbolBody *B);
71 StringRef findMangle(StringRef Name);
73 // Build a set of COFF objects representing the combined contents of
74 // BitcodeFiles and add them to the symbol table. Called after all files are
75 // added and before the writer writes results to a file.
76 void addCombinedLTOObjects();
77 std::vector<StringRef> compileBitcodeFiles();
79 // The writer needs to handle DLL import libraries specially in
80 // order to create the import descriptor table.
81 std::vector<ImportFile *> ImportFiles;
83 // The writer needs to infer the machine type from the object files.
84 std::vector<ObjectFile *> ObjectFiles;
86 // Creates an Undefined symbol for a given name.
87 SymbolBody *addUndefined(StringRef Name);
89 Symbol *addRelative(StringRef N, uint64_t VA);
90 Symbol *addAbsolute(StringRef N, uint64_t VA);
92 Symbol *addUndefined(StringRef Name, InputFile *F, bool IsWeakAlias);
93 void addLazy(ArchiveFile *F, const Archive::Symbol Sym);
94 Symbol *addAbsolute(StringRef N, COFFSymbolRef S);
95 Symbol *addRegular(InputFile *F, StringRef N, bool IsCOMDAT,
96 const llvm::object::coff_symbol_generic *S = nullptr,
97 SectionChunk *C = nullptr);
98 Symbol *addCommon(InputFile *F, StringRef N, uint64_t Size,
99 const llvm::object::coff_symbol_generic *S = nullptr,
100 CommonChunk *C = nullptr);
101 Symbol *addImportData(StringRef N, ImportFile *F);
102 Symbol *addImportThunk(StringRef Name, DefinedImportData *S,
105 void reportDuplicate(Symbol *Existing, InputFile *NewFile);
107 // A list of chunks which to be added to .rdata.
108 std::vector<Chunk *> LocalImportChunks;
111 std::pair<Symbol *, bool> insert(StringRef Name);
112 StringRef findByPrefix(StringRef Prefix);
114 llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> Symtab;
116 std::vector<BitcodeFile *> BitcodeFiles;
117 std::unique_ptr<BitcodeCompiler> LTO;
120 extern SymbolTable *Symtab;