]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/SymbolTable.h
Update Apache Serf to 1.3.9 to support OpenSSL 1.1.1.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / SymbolTable.h
1 //===- SymbolTable.h --------------------------------------------*- C++ -*-===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLD_ELF_SYMBOL_TABLE_H
11 #define LLD_ELF_SYMBOL_TABLE_H
12
13 #include "InputFiles.h"
14 #include "LTO.h"
15 #include "Strings.h"
16 #include "llvm/ADT/CachedHashString.h"
17 #include "llvm/ADT/DenseMap.h"
18
19 namespace lld {
20 namespace elf {
21 class Defined;
22 class SectionBase;
23
24 // SymbolTable is a bucket of all known symbols, including defined,
25 // undefined, or lazy symbols (the last one is symbols in archive
26 // files whose archive members are not yet loaded).
27 //
28 // We put all symbols of all files to a SymbolTable, and the
29 // SymbolTable selects the "best" symbols if there are name
30 // conflicts. For example, obviously, a defined symbol is better than
31 // an undefined symbol. Or, if there's a conflict between a lazy and a
32 // undefined, it'll read an archive member to read a real definition
33 // to replace the lazy symbol. The logic is implemented in the
34 // add*() functions, which are called by input files as they are parsed. There
35 // is one add* function per symbol type.
36 class SymbolTable {
37 public:
38   template <class ELFT> void addFile(InputFile *File);
39   template <class ELFT> void addCombinedLTOObject();
40   template <class ELFT> void addSymbolWrap(StringRef Name);
41   void applySymbolWrap();
42
43   ArrayRef<Symbol *> getSymbols() const { return SymVector; }
44
45   Defined *addAbsolute(StringRef Name,
46                        uint8_t Visibility = llvm::ELF::STV_HIDDEN,
47                        uint8_t Binding = llvm::ELF::STB_GLOBAL);
48
49   template <class ELFT> Symbol *addUndefined(StringRef Name);
50   template <class ELFT>
51   Symbol *addUndefined(StringRef Name, uint8_t Binding, uint8_t StOther,
52                        uint8_t Type, bool CanOmitFromDynSym, InputFile *File);
53   Symbol *addRegular(StringRef Name, uint8_t StOther, uint8_t Type,
54                      uint64_t Value, uint64_t Size, uint8_t Binding,
55                      SectionBase *Section, InputFile *File);
56
57   template <class ELFT>
58   void addShared(StringRef Name, SharedFile<ELFT> &F,
59                  const typename ELFT::Sym &Sym, uint32_t Alignment,
60                  uint32_t VerdefIndex);
61
62   template <class ELFT>
63   Symbol *addLazyArchive(StringRef Name, ArchiveFile &F,
64                          const llvm::object::Archive::Symbol S);
65
66   template <class ELFT> void addLazyObject(StringRef Name, LazyObjFile &Obj);
67
68   Symbol *addBitcode(StringRef Name, uint8_t Binding, uint8_t StOther,
69                      uint8_t Type, bool CanOmitFromDynSym, BitcodeFile &File);
70
71   Symbol *addCommon(StringRef Name, uint64_t Size, uint32_t Alignment,
72                     uint8_t Binding, uint8_t StOther, uint8_t Type,
73                     InputFile &File);
74
75   std::pair<Symbol *, bool> insert(StringRef Name);
76   std::pair<Symbol *, bool> insert(StringRef Name, uint8_t Type,
77                                    uint8_t Visibility, bool CanOmitFromDynSym,
78                                    InputFile *File);
79
80   template <class ELFT> void fetchIfLazy(StringRef Name);
81   template <class ELFT> void scanShlibUndefined();
82   void scanVersionScript();
83
84   Symbol *find(StringRef Name);
85
86   void trace(StringRef Name);
87
88   void handleDynamicList();
89
90 private:
91   std::vector<Symbol *> findByVersion(SymbolVersion Ver);
92   std::vector<Symbol *> findAllByVersion(SymbolVersion Ver);
93   void defsym(Symbol *Dst, Symbol *Src);
94
95   llvm::StringMap<std::vector<Symbol *>> &getDemangledSyms();
96   void handleAnonymousVersion();
97   void assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
98                           StringRef VersionName);
99   void assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId);
100
101   // The order the global symbols are in is not defined. We can use an arbitrary
102   // order, but it has to be reproducible. That is true even when cross linking.
103   // The default hashing of StringRef produces different results on 32 and 64
104   // bit systems so we use a map to a vector. That is arbitrary, deterministic
105   // but a bit inefficient.
106   // FIXME: Experiment with passing in a custom hashing or sorting the symbols
107   // once symbol resolution is finished.
108   llvm::DenseMap<llvm::CachedHashStringRef, int> SymMap;
109   std::vector<Symbol *> SymVector;
110
111   // Comdat groups define "link once" sections. If two comdat groups have the
112   // same name, only one of them is linked, and the other is ignored. This set
113   // is used to uniquify them.
114   llvm::DenseSet<llvm::CachedHashStringRef> ComdatGroups;
115
116   // Set of .so files to not link the same shared object file more than once.
117   llvm::DenseSet<StringRef> SoNames;
118
119   // A map from demangled symbol names to their symbol objects.
120   // This mapping is 1:N because two symbols with different versions
121   // can have the same name. We use this map to handle "extern C++ {}"
122   // directive in version scripts.
123   llvm::Optional<llvm::StringMap<std::vector<Symbol *>>> DemangledSyms;
124
125   struct WrappedSymbol {
126     Symbol *Sym;
127     Symbol *Real;
128     Symbol *Wrap;
129   };
130
131   // For -wrap.
132   std::vector<WrappedSymbol> WrappedSymbols;
133
134   // For LTO.
135   std::unique_ptr<BitcodeCompiler> LTO;
136 };
137
138 extern SymbolTable *Symtab;
139 } // namespace elf
140 } // namespace lld
141
142 #endif