]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/COFF/SymbolTable.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r301441, and update
[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/Allocator.h"
19 #include "llvm/Support/raw_ostream.h"
20
21 namespace llvm {
22 struct LTOCodeGenerator;
23 }
24
25 namespace lld {
26 namespace coff {
27
28 class Chunk;
29 class CommonChunk;
30 class Defined;
31 class DefinedAbsolute;
32 class DefinedRelative;
33 class Lazy;
34 class SectionChunk;
35 class SymbolBody;
36 struct Symbol;
37
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).
41 //
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.
50 class SymbolTable {
51 public:
52   void addFile(InputFile *File);
53
54   // Try to resolve any undefined symbols and update the symbol table
55   // accordingly, then print an error message for any remaining undefined
56   // symbols.
57   void reportRemainingUndefines();
58
59   // Returns a list of chunks of selected symbols.
60   std::vector<Chunk *> getChunks();
61
62   // Returns a symbol for a given name. Returns a nullptr if not found.
63   Symbol *find(StringRef Name);
64   Symbol *findUnderscore(StringRef Name);
65
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);
72
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();
78
79   // The writer needs to handle DLL import libraries specially in
80   // order to create the import descriptor table.
81   std::vector<ImportFile *> ImportFiles;
82
83   // The writer needs to infer the machine type from the object files.
84   std::vector<ObjectFile *> ObjectFiles;
85
86   // Creates an Undefined symbol for a given name.
87   SymbolBody *addUndefined(StringRef Name);
88
89   Symbol *addRelative(StringRef N, uint64_t VA);
90   Symbol *addAbsolute(StringRef N, uint64_t VA);
91
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,
103                          uint16_t Machine);
104
105   void reportDuplicate(Symbol *Existing, InputFile *NewFile);
106
107   // A list of chunks which to be added to .rdata.
108   std::vector<Chunk *> LocalImportChunks;
109
110 private:
111   std::pair<Symbol *, bool> insert(StringRef Name);
112   StringRef findByPrefix(StringRef Prefix);
113
114   llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> Symtab;
115
116   std::vector<BitcodeFile *> BitcodeFiles;
117   std::unique_ptr<BitcodeCompiler> LTO;
118 };
119
120 extern SymbolTable *Symtab;
121
122 } // namespace coff
123 } // namespace lld
124
125 #endif