1 //===- SymbolTable.h --------------------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_ELF_SYMBOL_TABLE_H
11 #define LLD_ELF_SYMBOL_TABLE_H
13 #include "InputFiles.h"
15 #include "llvm/ADT/DenseMap.h"
20 template <class ELFT> class OutputSectionBase;
23 typedef llvm::CachedHash<StringRef> SymName;
25 // SymbolTable is a bucket of all known symbols, including defined,
26 // undefined, or lazy symbols (the last one is symbols in archive
27 // files whose archive members are not yet loaded).
29 // We put all symbols of all files to a SymbolTable, and the
30 // SymbolTable selects the "best" symbols if there are name
31 // conflicts. For example, obviously, a defined symbol is better than
32 // an undefined symbol. Or, if there's a conflict between a lazy and a
33 // undefined, it'll read an archive member to read a real definition
34 // to replace the lazy symbol. The logic is implemented in the
35 // add*() functions, which are called by input files as they are parsed. There
36 // is one add* function per symbol type.
37 template <class ELFT> class SymbolTable {
38 typedef typename ELFT::Sym Elf_Sym;
39 typedef typename ELFT::uint uintX_t;
42 void addFile(std::unique_ptr<InputFile> File);
43 void addCombinedLtoObject();
45 llvm::ArrayRef<Symbol *> getSymbols() const { return SymVector; }
47 const std::vector<std::unique_ptr<ObjectFile<ELFT>>> &getObjectFiles() const {
51 const std::vector<std::unique_ptr<SharedFile<ELFT>>> &getSharedFiles() const {
55 DefinedRegular<ELFT> *addAbsolute(StringRef Name,
56 uint8_t Visibility = llvm::ELF::STV_HIDDEN);
57 DefinedRegular<ELFT> *addIgnored(StringRef Name,
58 uint8_t Visibility = llvm::ELF::STV_HIDDEN);
60 Symbol *addUndefined(StringRef Name);
61 Symbol *addUndefined(StringRef Name, uint8_t Binding, uint8_t StOther,
62 uint8_t Type, bool CanOmitFromDynSym, InputFile *File);
64 Symbol *addRegular(StringRef Name, const Elf_Sym &Sym,
65 InputSectionBase<ELFT> *Section);
66 Symbol *addRegular(StringRef Name, uint8_t Binding, uint8_t StOther);
67 Symbol *addSynthetic(StringRef N, OutputSectionBase<ELFT> *Section,
69 void addShared(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym,
70 const typename ELFT::Verdef *Verdef);
72 void addLazyArchive(ArchiveFile *F, const llvm::object::Archive::Symbol S);
73 void addLazyObject(StringRef Name, LazyObjectFile &Obj);
74 Symbol *addBitcode(StringRef Name, bool IsWeak, uint8_t StOther, uint8_t Type,
75 bool CanOmitFromDynSym, BitcodeFile *File);
77 Symbol *addCommon(StringRef N, uint64_t Size, uint64_t Alignment,
78 uint8_t Binding, uint8_t StOther, uint8_t Type,
81 void scanUndefinedFlags();
82 void scanShlibUndefined();
83 void scanDynamicList();
84 void scanVersionScript();
85 void scanSymbolVersions();
87 SymbolBody *find(StringRef Name);
89 void trace(StringRef Name);
90 void wrap(StringRef Name);
93 std::vector<SymbolBody *> findAll(StringRef Pattern);
94 std::pair<Symbol *, bool> insert(StringRef Name);
95 std::pair<Symbol *, bool> insert(StringRef Name, uint8_t Type,
96 uint8_t Visibility, bool CanOmitFromDynSym,
97 bool IsUsedInRegularObj, InputFile *File);
99 std::string conflictMsg(SymbolBody *Existing, InputFile *NewFile);
100 void reportDuplicate(SymbolBody *Existing, InputFile *NewFile);
102 std::map<std::string, SymbolBody *> getDemangledSyms();
109 // The order the global symbols are in is not defined. We can use an arbitrary
110 // order, but it has to be reproducible. That is true even when cross linking.
111 // The default hashing of StringRef produces different results on 32 and 64
112 // bit systems so we use a map to a vector. That is arbitrary, deterministic
113 // but a bit inefficient.
114 // FIXME: Experiment with passing in a custom hashing or sorting the symbols
115 // once symbol resolution is finished.
116 llvm::DenseMap<SymName, SymIndex> Symtab;
117 std::vector<Symbol *> SymVector;
118 llvm::BumpPtrAllocator Alloc;
120 // Comdat groups define "link once" sections. If two comdat groups have the
121 // same name, only one of them is linked, and the other is ignored. This set
122 // is used to uniquify them.
123 llvm::DenseSet<StringRef> ComdatGroups;
125 // The symbol table owns all file objects.
126 std::vector<std::unique_ptr<ArchiveFile>> ArchiveFiles;
127 std::vector<std::unique_ptr<ObjectFile<ELFT>>> ObjectFiles;
128 std::vector<std::unique_ptr<LazyObjectFile>> LazyObjectFiles;
129 std::vector<std::unique_ptr<SharedFile<ELFT>>> SharedFiles;
130 std::vector<std::unique_ptr<BitcodeFile>> BitcodeFiles;
132 // Set of .so files to not link the same shared object file more than once.
133 llvm::DenseSet<StringRef> SoNames;
135 std::unique_ptr<BitcodeCompiler> Lto;
138 template <class ELFT> struct Symtab { static SymbolTable<ELFT> *X; };
139 template <class ELFT> SymbolTable<ELFT> *Symtab<ELFT>::X;