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 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLD_ELF_SYMBOL_TABLE_H
10 #define LLD_ELF_SYMBOL_TABLE_H
11 
12 #include "Symbols.h"
13 #include "llvm/ADT/CachedHashString.h"
14 #include "llvm/ADT/DenseMap.h"
15 
16 namespace lld {
17 namespace elf {
18 
19 class InputFile;
20 class SharedFile;
21 
22 // SymbolTable is a bucket of all known symbols, including defined,
23 // undefined, or lazy symbols (the last one is symbols in archive
24 // files whose archive members are not yet loaded).
25 //
26 // We put all symbols of all files to a SymbolTable, and the
27 // SymbolTable selects the "best" symbols if there are name
28 // conflicts. For example, obviously, a defined symbol is better than
29 // an undefined symbol. Or, if there's a conflict between a lazy and a
30 // undefined, it'll read an archive member to read a real definition
31 // to replace the lazy symbol. The logic is implemented in the
32 // add*() functions, which are called by input files as they are parsed. There
33 // is one add* function per symbol type.
34 class SymbolTable {
35 public:
36   ArrayRef<Symbol *> symbols() const { return symVector; }
37 
38   void wrap(Symbol *sym, Symbol *real, Symbol *wrap);
39 
40   Symbol *insert(StringRef name);
41 
42   Symbol *addSymbol(const Symbol &newSym);
43   Symbol *addAndCheckDuplicate(const Defined &newSym);
44 
45   void scanVersionScript();
46 
47   Symbol *find(StringRef name);
48 
49   void handleDynamicList();
50 
51   // Set of .so files to not link the same shared object file more than once.
52   llvm::DenseMap<llvm::CachedHashStringRef, SharedFile *> soNames;
53 
54   // Comdat groups define "link once" sections. If two comdat groups have the
55   // same name, only one of them is linked, and the other is ignored. This map
56   // is used to uniquify them.
57   llvm::DenseMap<llvm::CachedHashStringRef, const InputFile *> comdatGroups;
58 
59 private:
60   SmallVector<Symbol *, 0> findByVersion(SymbolVersion ver);
61   SmallVector<Symbol *, 0> findAllByVersion(SymbolVersion ver,
62                                             bool includeNonDefault);
63 
64   llvm::StringMap<SmallVector<Symbol *, 0>> &getDemangledSyms();
65   bool assignExactVersion(SymbolVersion ver, uint16_t versionId,
66                           StringRef versionName, bool includeNonDefault);
67   void assignWildcardVersion(SymbolVersion ver, uint16_t versionId,
68                              bool includeNonDefault);
69 
70   // The order the global symbols are in is not defined. We can use an arbitrary
71   // order, but it has to be reproducible. That is true even when cross linking.
72   // The default hashing of StringRef produces different results on 32 and 64
73   // bit systems so we use a map to a vector. That is arbitrary, deterministic
74   // but a bit inefficient.
75   // FIXME: Experiment with passing in a custom hashing or sorting the symbols
76   // once symbol resolution is finished.
77   llvm::DenseMap<llvm::CachedHashStringRef, int> symMap;
78   SmallVector<Symbol *, 0> symVector;
79 
80   // A map from demangled symbol names to their symbol objects.
81   // This mapping is 1:N because two symbols with different versions
82   // can have the same name. We use this map to handle "extern C++ {}"
83   // directive in version scripts.
84   llvm::Optional<llvm::StringMap<SmallVector<Symbol *, 0>>> demangledSyms;
85 };
86 
87 extern std::unique_ptr<SymbolTable> symtab;
88 
89 } // namespace elf
90 } // namespace lld
91 
92 #endif
93