]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/COFF/SymbolTable.h
MFC r309362:
[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 "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/DenseMapInfo.h"
16 #include "llvm/Support/Allocator.h"
17 #include "llvm/Support/raw_ostream.h"
18
19 #ifdef _MSC_VER
20 // <future> depends on <eh.h> for __uncaught_exception.
21 #include <eh.h>
22 #endif
23
24 #include <future>
25
26 namespace llvm {
27 struct LTOCodeGenerator;
28 }
29
30 namespace lld {
31 namespace coff {
32
33 class Chunk;
34 class Defined;
35 class Lazy;
36 class SymbolBody;
37 struct Symbol;
38
39 // SymbolTable is a bucket of all known symbols, including defined,
40 // undefined, or lazy symbols (the last one is symbols in archive
41 // files whose archive members are not yet loaded).
42 //
43 // We put all symbols of all files to a SymbolTable, and the
44 // SymbolTable selects the "best" symbols if there are name
45 // conflicts. For example, obviously, a defined symbol is better than
46 // an undefined symbol. Or, if there's a conflict between a lazy and a
47 // undefined, it'll read an archive member to read a real definition
48 // to replace the lazy symbol. The logic is implemented in resolve().
49 class SymbolTable {
50 public:
51   void addFile(std::unique_ptr<InputFile> File);
52   std::vector<std::unique_ptr<InputFile>> &getFiles() { return Files; }
53   void step();
54   void run();
55   bool queueEmpty();
56
57   // Print an error message on undefined symbols. If Resolve is true, try to
58   // resolve any undefined symbols and update the symbol table accordingly.
59   void reportRemainingUndefines(bool Resolve);
60
61   // Returns a list of chunks of selected symbols.
62   std::vector<Chunk *> getChunks();
63
64   // Returns a symbol for a given name. Returns a nullptr if not found.
65   Symbol *find(StringRef Name);
66   Symbol *findUnderscore(StringRef Name);
67
68   // Occasionally we have to resolve an undefined symbol to its
69   // mangled symbol. This function tries to find a mangled name
70   // for U from the symbol table, and if found, set the symbol as
71   // a weak alias for U.
72   void mangleMaybe(Undefined *U);
73   StringRef findMangle(StringRef Name);
74
75   // Print a layout map to OS.
76   void printMap(llvm::raw_ostream &OS);
77
78   // Build a set of COFF objects representing the combined contents of
79   // BitcodeFiles and add them to the symbol table. Called after all files are
80   // added and before the writer writes results to a file.
81   void addCombinedLTOObjects();
82
83   // The writer needs to handle DLL import libraries specially in
84   // order to create the import descriptor table.
85   std::vector<ImportFile *> ImportFiles;
86
87   // The writer needs to infer the machine type from the object files.
88   std::vector<ObjectFile *> ObjectFiles;
89
90   // Creates an Undefined symbol for a given name.
91   Undefined *addUndefined(StringRef Name);
92   DefinedRelative *addRelative(StringRef Name, uint64_t VA);
93   DefinedAbsolute *addAbsolute(StringRef Name, uint64_t VA);
94
95   // A list of chunks which to be added to .rdata.
96   std::vector<Chunk *> LocalImportChunks;
97
98 private:
99   void readArchives();
100   void readObjects();
101
102   void addSymbol(SymbolBody *New);
103   void addLazy(Lazy *New, std::vector<Symbol *> *Accum);
104   Symbol *insert(SymbolBody *New);
105   StringRef findByPrefix(StringRef Prefix);
106
107   void addMemberFile(Lazy *Body);
108   void addCombinedLTOObject(ObjectFile *Obj);
109   std::vector<ObjectFile *> createLTOObjects(llvm::LTOCodeGenerator *CG);
110
111   llvm::DenseMap<StringRef, Symbol *> Symtab;
112
113   std::vector<std::unique_ptr<InputFile>> Files;
114   std::vector<std::future<ArchiveFile *>> ArchiveQueue;
115   std::vector<std::future<InputFile *>> ObjectQueue;
116
117   std::vector<BitcodeFile *> BitcodeFiles;
118   std::vector<SmallString<0>> Objs;
119   llvm::BumpPtrAllocator Alloc;
120 };
121
122 } // namespace coff
123 } // namespace lld
124
125 #endif