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