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 #include "llvm/Support/Compiler.h" 16 17 namespace lld::elf { 18 struct Ctx; 19 class InputFile; 20 class SharedFile; 21 22 struct ArmCmseEntryFunction { 23 Symbol *acleSeSym; 24 Symbol *sym; 25 }; 26 27 // SymbolTable is a bucket of all known symbols, including defined, 28 // undefined, or lazy symbols (the last one is symbols in archive 29 // files whose archive members are not yet loaded). 30 // 31 // We put all symbols of all files to a SymbolTable, and the 32 // SymbolTable selects the "best" symbols if there are name 33 // conflicts. For example, obviously, a defined symbol is better than 34 // an undefined symbol. Or, if there's a conflict between a lazy and a 35 // undefined, it'll read an archive member to read a real definition 36 // to replace the lazy symbol. The logic is implemented in the 37 // add*() functions, which are called by input files as they are parsed. There 38 // is one add* function per symbol type. 39 class SymbolTable { 40 public: 41 SymbolTable(Ctx &ctx) : ctx(ctx) {} 42 ArrayRef<Symbol *> getSymbols() const { return symVector; } 43 44 void wrap(Symbol *sym, Symbol *real, Symbol *wrap); 45 46 Symbol *insert(StringRef name); 47 48 template <typename T> Symbol *addSymbol(const T &newSym) { 49 Symbol *sym = insert(newSym.getName()); 50 sym->resolve(ctx, newSym); 51 return sym; 52 } 53 Symbol *addAndCheckDuplicate(Ctx &, const Defined &newSym); 54 55 void scanVersionScript(); 56 57 Symbol *find(StringRef name); 58 59 void handleDynamicList(); 60 61 Symbol *addUnusedUndefined(StringRef name, 62 uint8_t binding = llvm::ELF::STB_GLOBAL); 63 64 // Set of .so files to not link the same shared object file more than once. 65 llvm::DenseMap<llvm::CachedHashStringRef, SharedFile *> soNames; 66 67 // Comdat groups define "link once" sections. If two comdat groups have the 68 // same name, only one of them is linked, and the other is ignored. This map 69 // is used to uniquify them. 70 llvm::DenseMap<llvm::CachedHashStringRef, const InputFile *> comdatGroups; 71 72 // The Map of __acle_se_<sym>, <sym> pairs found in the input objects. 73 // Key is the <sym> name. 74 llvm::SmallMapVector<StringRef, ArmCmseEntryFunction, 1> cmseSymMap; 75 76 // Map of symbols defined in the Arm CMSE import library. The linker must 77 // preserve the addresses in the output objects. 78 llvm::StringMap<Defined *> cmseImportLib; 79 80 // True if <sym> from the input Arm CMSE import library is written to the 81 // output Arm CMSE import library. 82 llvm::StringMap<bool> inCMSEOutImpLib; 83 84 private: 85 SmallVector<Symbol *, 0> findByVersion(SymbolVersion ver); 86 SmallVector<Symbol *, 0> findAllByVersion(SymbolVersion ver, 87 bool includeNonDefault); 88 89 llvm::StringMap<SmallVector<Symbol *, 0>> &getDemangledSyms(); 90 bool assignExactVersion(SymbolVersion ver, uint16_t versionId, 91 StringRef versionName, bool includeNonDefault); 92 void assignWildcardVersion(SymbolVersion ver, uint16_t versionId, 93 bool includeNonDefault); 94 95 Ctx &ctx; 96 97 // Global symbols and a map from symbol name to the index. The order is not 98 // defined. We can use an arbitrary order, but it has to be deterministic even 99 // when cross linking. 100 llvm::DenseMap<llvm::CachedHashStringRef, int> symMap; 101 SmallVector<Symbol *, 0> symVector; 102 103 // A map from demangled symbol names to their symbol objects. 104 // This mapping is 1:N because two symbols with different versions 105 // can have the same name. We use this map to handle "extern C++ {}" 106 // directive in version scripts. 107 std::optional<llvm::StringMap<SmallVector<Symbol *, 0>>> demangledSyms; 108 }; 109 110 } // namespace lld::elf 111 112 #endif 113