1 //===- SymbolTable.h --------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_ELF_SYMBOL_TABLE_H
10 #define LLD_ELF_SYMBOL_TABLE_H
13 #include "llvm/ADT/CachedHashString.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/Support/Compiler.h"
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).
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.
36 ArrayRef
<Symbol
*> getSymbols() const { return symVector
; }
38 void wrap(Symbol
*sym
, Symbol
*real
, Symbol
*wrap
);
40 Symbol
*insert(StringRef name
);
42 template <typename T
> Symbol
*addSymbol(const T
&newSym
) {
43 Symbol
*sym
= insert(newSym
.getName());
47 Symbol
*addAndCheckDuplicate(const Defined
&newSym
);
49 void scanVersionScript();
51 Symbol
*find(StringRef name
);
53 void handleDynamicList();
55 // Set of .so files to not link the same shared object file more than once.
56 llvm::DenseMap
<llvm::CachedHashStringRef
, SharedFile
*> soNames
;
58 // Comdat groups define "link once" sections. If two comdat groups have the
59 // same name, only one of them is linked, and the other is ignored. This map
60 // is used to uniquify them.
61 llvm::DenseMap
<llvm::CachedHashStringRef
, const InputFile
*> comdatGroups
;
64 SmallVector
<Symbol
*, 0> findByVersion(SymbolVersion ver
);
65 SmallVector
<Symbol
*, 0> findAllByVersion(SymbolVersion ver
,
66 bool includeNonDefault
);
68 llvm::StringMap
<SmallVector
<Symbol
*, 0>> &getDemangledSyms();
69 bool assignExactVersion(SymbolVersion ver
, uint16_t versionId
,
70 StringRef versionName
, bool includeNonDefault
);
71 void assignWildcardVersion(SymbolVersion ver
, uint16_t versionId
,
72 bool includeNonDefault
);
74 // The order the global symbols are in is not defined. We can use an arbitrary
75 // order, but it has to be reproducible. That is true even when cross linking.
76 // The default hashing of StringRef produces different results on 32 and 64
77 // bit systems so we use a map to a vector. That is arbitrary, deterministic
78 // but a bit inefficient.
79 // FIXME: Experiment with passing in a custom hashing or sorting the symbols
80 // once symbol resolution is finished.
81 llvm::DenseMap
<llvm::CachedHashStringRef
, int> symMap
;
82 SmallVector
<Symbol
*, 0> symVector
;
84 // A map from demangled symbol names to their symbol objects.
85 // This mapping is 1:N because two symbols with different versions
86 // can have the same name. We use this map to handle "extern C++ {}"
87 // directive in version scripts.
88 std::optional
<llvm::StringMap
<SmallVector
<Symbol
*, 0>>> demangledSyms
;
91 LLVM_LIBRARY_VISIBILITY
extern SymbolTable symtab
;
93 } // namespace lld::elf