xref: /freebsd/contrib/llvm-project/lld/ELF/SymbolTable.h (revision 69ade1e033e478ec426cafc0ec2104d672de294a)
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 "InputFiles.h"
13 #include "Symbols.h"
14 #include "lld/Common/Strings.h"
15 #include "llvm/ADT/CachedHashString.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/STLExtras.h"
18 
19 namespace lld {
20 namespace elf {
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   struct FilterOutPlaceholder {
36     bool operator()(Symbol *S) const { return !S->isPlaceholder(); }
37   };
38   using iterator = llvm::filter_iterator<std::vector<Symbol *>::const_iterator,
39                                          FilterOutPlaceholder>;
40 
41 public:
42   llvm::iterator_range<iterator> symbols() const {
43     return llvm::make_filter_range(symVector, FilterOutPlaceholder());
44   }
45 
46   void wrap(Symbol *sym, Symbol *real, Symbol *wrap);
47 
48   Symbol *insert(StringRef name);
49 
50   Symbol *addSymbol(const Symbol &newSym);
51 
52   void scanVersionScript();
53 
54   Symbol *find(StringRef name);
55 
56   void handleDynamicList();
57 
58   // Set of .so files to not link the same shared object file more than once.
59   llvm::DenseMap<StringRef, SharedFile *> soNames;
60 
61   // Comdat groups define "link once" sections. If two comdat groups have the
62   // same name, only one of them is linked, and the other is ignored. This map
63   // is used to uniquify them.
64   llvm::DenseMap<llvm::CachedHashStringRef, const InputFile *> comdatGroups;
65 
66 private:
67   std::vector<Symbol *> findByVersion(SymbolVersion ver);
68   std::vector<Symbol *> findAllByVersion(SymbolVersion ver,
69                                          bool includeNonDefault);
70 
71   llvm::StringMap<std::vector<Symbol *>> &getDemangledSyms();
72   bool assignExactVersion(SymbolVersion ver, uint16_t versionId,
73                           StringRef versionName, bool includeNonDefault);
74   void assignWildcardVersion(SymbolVersion ver, uint16_t versionId,
75                              bool includeNonDefault);
76 
77   // The order the global symbols are in is not defined. We can use an arbitrary
78   // order, but it has to be reproducible. That is true even when cross linking.
79   // The default hashing of StringRef produces different results on 32 and 64
80   // bit systems so we use a map to a vector. That is arbitrary, deterministic
81   // but a bit inefficient.
82   // FIXME: Experiment with passing in a custom hashing or sorting the symbols
83   // once symbol resolution is finished.
84   llvm::DenseMap<llvm::CachedHashStringRef, int> symMap;
85   std::vector<Symbol *> symVector;
86 
87   // A map from demangled symbol names to their symbol objects.
88   // This mapping is 1:N because two symbols with different versions
89   // can have the same name. We use this map to handle "extern C++ {}"
90   // directive in version scripts.
91   llvm::Optional<llvm::StringMap<std::vector<Symbol *>>> demangledSyms;
92 };
93 
94 extern SymbolTable *symtab;
95 
96 } // namespace elf
97 } // namespace lld
98 
99 #endif
100