[DAGCombiner] Add target hook function to decide folding (mul (add x, c1), c2)
[llvm-project.git] / lld / COFF / SymbolTable.h
blobe88002c88310139f895046d378831009aa1ed1ab
1 //===- SymbolTable.h --------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_COFF_SYMBOL_TABLE_H
10 #define LLD_COFF_SYMBOL_TABLE_H
12 #include "InputFiles.h"
13 #include "LTO.h"
14 #include "llvm/ADT/CachedHashString.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/DenseMapInfo.h"
17 #include "llvm/Support/raw_ostream.h"
19 namespace llvm {
20 struct LTOCodeGenerator;
23 namespace lld {
24 namespace coff {
26 class Chunk;
27 class CommonChunk;
28 class Defined;
29 class DefinedAbsolute;
30 class DefinedRegular;
31 class DefinedRelative;
32 class LazyArchive;
33 class SectionChunk;
34 class Symbol;
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).
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);
52 // Emit errors for symbols that cannot be resolved.
53 void reportUnresolvable();
55 // Try to resolve any undefined symbols and update the symbol table
56 // accordingly, then print an error message for any remaining undefined
57 // symbols and warn about imported local symbols.
58 void resolveRemainingUndefines();
60 // Load lazy objects that are needed for MinGW automatic import and for
61 // doing stdcall fixups.
62 void loadMinGWSymbols();
63 bool handleMinGWAutomaticImport(Symbol *sym, StringRef name);
65 // Returns a list of chunks of selected symbols.
66 std::vector<Chunk *> getChunks();
68 // Returns a symbol for a given name. Returns a nullptr if not found.
69 Symbol *find(StringRef name);
70 Symbol *findUnderscore(StringRef name);
72 // Occasionally we have to resolve an undefined symbol to its
73 // mangled symbol. This function tries to find a mangled name
74 // for U from the symbol table, and if found, set the symbol as
75 // a weak alias for U.
76 Symbol *findMangle(StringRef name);
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();
83 // Creates an Undefined symbol for a given name.
84 Symbol *addUndefined(StringRef name);
86 Symbol *addSynthetic(StringRef n, Chunk *c);
87 Symbol *addAbsolute(StringRef n, uint64_t va);
89 Symbol *addUndefined(StringRef name, InputFile *f, bool isWeakAlias);
90 void addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym);
91 void addLazyObject(LazyObjFile *f, StringRef n);
92 void addLazyDLLSymbol(DLLFile *f, DLLFile::Symbol *sym, StringRef n);
93 Symbol *addAbsolute(StringRef n, COFFSymbolRef s);
94 Symbol *addRegular(InputFile *f, StringRef n,
95 const llvm::object::coff_symbol_generic *s = nullptr,
96 SectionChunk *c = nullptr, uint32_t sectionOffset = 0);
97 std::pair<DefinedRegular *, bool>
98 addComdat(InputFile *f, StringRef n,
99 const llvm::object::coff_symbol_generic *s = nullptr);
100 Symbol *addCommon(InputFile *f, StringRef n, uint64_t size,
101 const llvm::object::coff_symbol_generic *s = nullptr,
102 CommonChunk *c = nullptr);
103 Symbol *addImportData(StringRef n, ImportFile *f);
104 Symbol *addImportThunk(StringRef name, DefinedImportData *s,
105 uint16_t machine);
106 void addLibcall(StringRef name);
108 void reportDuplicate(Symbol *existing, InputFile *newFile,
109 SectionChunk *newSc = nullptr,
110 uint32_t newSectionOffset = 0);
112 // A list of chunks which to be added to .rdata.
113 std::vector<Chunk *> localImportChunks;
115 // Iterates symbols in non-determinstic hash table order.
116 template <typename T> void forEachSymbol(T callback) {
117 for (auto &pair : symMap)
118 callback(pair.second);
121 private:
122 /// Given a name without "__imp_" prefix, returns a defined symbol
123 /// with the "__imp_" prefix, if it exists.
124 Defined *impSymbol(StringRef name);
125 /// Inserts symbol if not already present.
126 std::pair<Symbol *, bool> insert(StringRef name);
127 /// Same as insert(Name), but also sets isUsedInRegularObj.
128 std::pair<Symbol *, bool> insert(StringRef name, InputFile *f);
130 std::vector<Symbol *> getSymsWithPrefix(StringRef prefix);
132 llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> symMap;
133 std::unique_ptr<BitcodeCompiler> lto;
136 extern SymbolTable *symtab;
138 std::vector<std::string> getSymbolLocations(ObjFile *file, uint32_t symIndex);
140 StringRef ltrim1(StringRef s, const char *chars);
142 } // namespace coff
143 } // namespace lld
145 #endif