[WebAssembly] Fix asan issue from https://reviews.llvm.org/D121349
[llvm-project.git] / lld / COFF / SymbolTable.h
blob47f3238fd75b45173f50bf18f6478845be460de7
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 COFFLinkerContext;
29 class Defined;
30 class DefinedAbsolute;
31 class DefinedRegular;
32 class DefinedRelative;
33 class LazyArchive;
34 class SectionChunk;
35 class Symbol;
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).
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 SymbolTable(COFFLinkerContext &ctx) : ctx(ctx) {}
53 void addFile(InputFile *file);
55 // Emit errors for symbols that cannot be resolved.
56 void reportUnresolvable();
58 // Try to resolve any undefined symbols and update the symbol table
59 // accordingly, then print an error message for any remaining undefined
60 // symbols and warn about imported local symbols.
61 void resolveRemainingUndefines();
63 // Load lazy objects that are needed for MinGW automatic import and for
64 // doing stdcall fixups.
65 void loadMinGWSymbols();
66 bool handleMinGWAutomaticImport(Symbol *sym, StringRef name);
68 // Returns a list of chunks of selected symbols.
69 std::vector<Chunk *> getChunks() const;
71 // Returns a symbol for a given name. Returns a nullptr if not found.
72 Symbol *find(StringRef name) const;
73 Symbol *findUnderscore(StringRef name) const;
75 // Occasionally we have to resolve an undefined symbol to its
76 // mangled symbol. This function tries to find a mangled name
77 // for U from the symbol table, and if found, set the symbol as
78 // a weak alias for U.
79 Symbol *findMangle(StringRef name);
81 // Build a set of COFF objects representing the combined contents of
82 // BitcodeFiles and add them to the symbol table. Called after all files are
83 // added and before the writer writes results to a file.
84 void compileBitcodeFiles();
86 // Creates an Undefined symbol for a given name.
87 Symbol *addUndefined(StringRef name);
89 Symbol *addSynthetic(StringRef n, Chunk *c);
90 Symbol *addAbsolute(StringRef n, uint64_t va);
92 Symbol *addUndefined(StringRef name, InputFile *f, bool isWeakAlias);
93 void addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym);
94 void addLazyObject(InputFile *f, StringRef n);
95 void addLazyDLLSymbol(DLLFile *f, DLLFile::Symbol *sym, StringRef n);
96 Symbol *addAbsolute(StringRef n, COFFSymbolRef s);
97 Symbol *addRegular(InputFile *f, StringRef n,
98 const llvm::object::coff_symbol_generic *s = nullptr,
99 SectionChunk *c = nullptr, uint32_t sectionOffset = 0);
100 std::pair<DefinedRegular *, bool>
101 addComdat(InputFile *f, StringRef n,
102 const llvm::object::coff_symbol_generic *s = nullptr);
103 Symbol *addCommon(InputFile *f, StringRef n, uint64_t size,
104 const llvm::object::coff_symbol_generic *s = nullptr,
105 CommonChunk *c = nullptr);
106 Symbol *addImportData(StringRef n, ImportFile *f);
107 Symbol *addImportThunk(StringRef name, DefinedImportData *s,
108 uint16_t machine);
109 void addLibcall(StringRef name);
111 void reportDuplicate(Symbol *existing, InputFile *newFile,
112 SectionChunk *newSc = nullptr,
113 uint32_t newSectionOffset = 0);
115 // A list of chunks which to be added to .rdata.
116 std::vector<Chunk *> localImportChunks;
118 // Iterates symbols in non-determinstic hash table order.
119 template <typename T> void forEachSymbol(T callback) {
120 for (auto &pair : symMap)
121 callback(pair.second);
124 private:
125 /// Given a name without "__imp_" prefix, returns a defined symbol
126 /// with the "__imp_" prefix, if it exists.
127 Defined *impSymbol(StringRef name);
128 /// Inserts symbol if not already present.
129 std::pair<Symbol *, bool> insert(StringRef name);
130 /// Same as insert(Name), but also sets isUsedInRegularObj.
131 std::pair<Symbol *, bool> insert(StringRef name, InputFile *f);
133 std::vector<Symbol *> getSymsWithPrefix(StringRef prefix);
135 llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> symMap;
136 std::unique_ptr<BitcodeCompiler> lto;
138 COFFLinkerContext &ctx;
141 std::vector<std::string> getSymbolLocations(ObjFile *file, uint32_t symIndex);
143 StringRef ltrim1(StringRef s, const char *chars);
145 } // namespace coff
146 } // namespace lld
148 #endif