]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/COFF/SymbolTable.h
Update lld to trunk r290819 and resolve conflicts.
[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/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"
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 SymbolBody;
35 struct Symbol;
36
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).
40 //
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.
49 class SymbolTable {
50 public:
51   void addFile(InputFile *File);
52
53   // Try to resolve any undefined symbols and update the symbol table
54   // accordingly, then print an error message for any remaining undefined
55   // symbols.
56   void reportRemainingUndefines();
57
58   // Returns a list of chunks of selected symbols.
59   std::vector<Chunk *> getChunks();
60
61   // Returns a symbol for a given name. Returns a nullptr if not found.
62   Symbol *find(StringRef Name);
63   Symbol *findUnderscore(StringRef Name);
64
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);
71
72   // Print a layout map to OS.
73   void printMap(llvm::raw_ostream &OS);
74
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();
79
80   // The writer needs to handle DLL import libraries specially in
81   // order to create the import descriptor table.
82   std::vector<ImportFile *> ImportFiles;
83
84   // The writer needs to infer the machine type from the object files.
85   std::vector<ObjectFile *> ObjectFiles;
86
87   // Creates an Undefined symbol for a given name.
88   SymbolBody *addUndefined(StringRef Name);
89
90   Symbol *addRelative(StringRef N, uint64_t VA);
91   Symbol *addAbsolute(StringRef N, uint64_t VA);
92
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,
101                          uint16_t Machine);
102
103   void reportDuplicate(Symbol *Existing, InputFile *NewFile);
104
105   // A list of chunks which to be added to .rdata.
106   std::vector<Chunk *> LocalImportChunks;
107
108 private:
109   void readArchive();
110   void readObjects();
111
112   std::pair<Symbol *, bool> insert(StringRef Name);
113   StringRef findByPrefix(StringRef Prefix);
114
115   void addCombinedLTOObject(ObjectFile *Obj);
116   std::vector<ObjectFile *> createLTOObjects(llvm::LTOCodeGenerator *CG);
117
118   llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> Symtab;
119
120   std::vector<BitcodeFile *> BitcodeFiles;
121   std::vector<SmallString<0>> Objs;
122 };
123
124 extern SymbolTable *Symtab;
125
126 } // namespace coff
127 } // namespace lld
128
129 #endif