]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/COFF/SymbolTable.h
Merge lld trunk r321017 to contrib/llvm/tools/lld.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / COFF / SymbolTable.h
1 //===- SymbolTable.h --------------------------------------------*- C++ -*-===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLD_COFF_SYMBOL_TABLE_H
11 #define LLD_COFF_SYMBOL_TABLE_H
12
13 #include "InputFiles.h"
14 #include "LTO.h"
15 #include "llvm/ADT/CachedHashString.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/DenseMapInfo.h"
18 #include "llvm/Support/raw_ostream.h"
19
20 namespace llvm {
21 struct LTOCodeGenerator;
22 }
23
24 namespace lld {
25 namespace coff {
26
27 class Chunk;
28 class CommonChunk;
29 class Defined;
30 class DefinedAbsolute;
31 class DefinedRelative;
32 class Lazy;
33 class SectionChunk;
34 class Symbol;
35
36 // SymbolTable is a bucket of all known symbols, including defined,
37 // undefined, or lazy symbols (the last one is symbols in archive
38 // files whose archive members are not yet loaded).
39 //
40 // We put all symbols of all files to a SymbolTable, and the
41 // SymbolTable selects the "best" symbols if there are name
42 // conflicts. For example, obviously, a defined symbol is better than
43 // an undefined symbol. Or, if there's a conflict between a lazy and a
44 // undefined, it'll read an archive member to read a real definition
45 // to replace the lazy symbol. The logic is implemented in the
46 // add*() functions, which are called by input files as they are parsed.
47 // There is one add* function per symbol type.
48 class SymbolTable {
49 public:
50   void addFile(InputFile *File);
51
52   // Try to resolve any undefined symbols and update the symbol table
53   // accordingly, then print an error message for any remaining undefined
54   // symbols.
55   void reportRemainingUndefines();
56
57   // Returns a list of chunks of selected symbols.
58   std::vector<Chunk *> getChunks();
59
60   // Returns a symbol for a given name. Returns a nullptr if not found.
61   Symbol *find(StringRef Name);
62   Symbol *findUnderscore(StringRef Name);
63
64   // Occasionally we have to resolve an undefined symbol to its
65   // mangled symbol. This function tries to find a mangled name
66   // for U from the symbol table, and if found, set the symbol as
67   // a weak alias for U.
68   void mangleMaybe(Symbol *B);
69   StringRef findMangle(StringRef Name);
70
71   // Build a set of COFF objects representing the combined contents of
72   // BitcodeFiles and add them to the symbol table. Called after all files are
73   // added and before the writer writes results to a file.
74   void addCombinedLTOObjects();
75   std::vector<StringRef> compileBitcodeFiles();
76
77   // Creates an Undefined symbol for a given name.
78   Symbol *addUndefined(StringRef Name);
79
80   Symbol *addSynthetic(StringRef N, Chunk *C);
81   Symbol *addAbsolute(StringRef N, uint64_t VA);
82
83   Symbol *addUndefined(StringRef Name, InputFile *F, bool IsWeakAlias);
84   void addLazy(ArchiveFile *F, const Archive::Symbol Sym);
85   Symbol *addAbsolute(StringRef N, COFFSymbolRef S);
86   Symbol *addRegular(InputFile *F, StringRef N,
87                      const llvm::object::coff_symbol_generic *S = nullptr,
88                      SectionChunk *C = nullptr);
89   std::pair<Symbol *, bool>
90   addComdat(InputFile *F, StringRef N,
91             const llvm::object::coff_symbol_generic *S = nullptr);
92   Symbol *addCommon(InputFile *F, StringRef N, uint64_t Size,
93                     const llvm::object::coff_symbol_generic *S = nullptr,
94                     CommonChunk *C = nullptr);
95   DefinedImportData *addImportData(StringRef N, ImportFile *F);
96   DefinedImportThunk *addImportThunk(StringRef Name, DefinedImportData *S,
97                                      uint16_t Machine);
98
99   void reportDuplicate(Symbol *Existing, InputFile *NewFile);
100
101   // A list of chunks which to be added to .rdata.
102   std::vector<Chunk *> LocalImportChunks;
103
104   // Iterates symbols in non-determinstic hash table order.
105   template <typename T> void forEachSymbol(T Callback) {
106     for (auto &Pair : SymMap)
107       Callback(Pair.second);
108   }
109
110 private:
111   std::pair<Symbol *, bool> insert(StringRef Name);
112   StringRef findByPrefix(StringRef Prefix);
113
114   llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> SymMap;
115   std::unique_ptr<BitcodeCompiler> LTO;
116 };
117
118 extern SymbolTable *Symtab;
119
120 } // namespace coff
121 } // namespace lld
122
123 #endif