]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/SymbolTable.h
Upgrade NetBSD tests to 01.11.2017_23.20 snapshot
[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 "llvm/ADT/DenseMap.h"
16
17 namespace lld {
18 namespace elf {
19 class Lazy;
20 template <class ELFT> class OutputSectionBase;
21 struct Symbol;
22
23 typedef llvm::CachedHash<StringRef> SymName;
24
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).
28 //
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;
40
41 public:
42   void addFile(std::unique_ptr<InputFile> File);
43   void addCombinedLtoObject();
44
45   llvm::ArrayRef<Symbol *> getSymbols() const { return SymVector; }
46
47   const std::vector<std::unique_ptr<ObjectFile<ELFT>>> &getObjectFiles() const {
48     return ObjectFiles;
49   }
50
51   const std::vector<std::unique_ptr<SharedFile<ELFT>>> &getSharedFiles() const {
52     return SharedFiles;
53   }
54
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);
59
60   Symbol *addUndefined(StringRef Name);
61   Symbol *addUndefined(StringRef Name, uint8_t Binding, uint8_t StOther,
62                        uint8_t Type, bool CanOmitFromDynSym, InputFile *File);
63
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,
68                        uintX_t Value);
69   void addShared(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym,
70                  const typename ELFT::Verdef *Verdef);
71
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);
76
77   Symbol *addCommon(StringRef N, uint64_t Size, uint64_t Alignment,
78                     uint8_t Binding, uint8_t StOther, uint8_t Type,
79                     InputFile *File);
80
81   void scanUndefinedFlags();
82   void scanShlibUndefined();
83   void scanDynamicList();
84   void scanVersionScript();
85   void scanSymbolVersions();
86
87   SymbolBody *find(StringRef Name);
88
89   void trace(StringRef Name);
90   void wrap(StringRef Name);
91
92 private:
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);
98
99   std::string conflictMsg(SymbolBody *Existing, InputFile *NewFile);
100   void reportDuplicate(SymbolBody *Existing, InputFile *NewFile);
101
102   std::map<std::string, SymbolBody *> getDemangledSyms();
103
104   struct SymIndex {
105     int Idx : 31;
106     unsigned Traced : 1;
107   };
108
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;
119
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;
124
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;
131
132   // Set of .so files to not link the same shared object file more than once.
133   llvm::DenseSet<StringRef> SoNames;
134
135   std::unique_ptr<BitcodeCompiler> Lto;
136 };
137
138 template <class ELFT> struct Symtab { static SymbolTable<ELFT> *X; };
139 template <class ELFT> SymbolTable<ELFT> *Symtab<ELFT>::X;
140
141 } // namespace elf
142 } // namespace lld
143
144 #endif