Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/lld/ELF/SymbolTable.h
Line
Count
Source
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
22
struct Symbol;
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 addSymbolAlias(StringRef Alias, StringRef Name);
41
  template <class ELFT> void addSymbolWrap(StringRef Name);
42
  void applySymbolRenames();
43
44
4.90k
  ArrayRef<Symbol *> getSymbols() const { return SymVector; }
45
46
  template <class ELFT>
47
  DefinedRegular *addAbsolute(StringRef Name,
48
                              uint8_t Visibility = llvm::ELF::STV_HIDDEN,
49
                              uint8_t Binding = llvm::ELF::STB_GLOBAL);
50
  template <class ELFT>
51
  DefinedRegular *addIgnored(StringRef Name,
52
                             uint8_t Visibility = llvm::ELF::STV_HIDDEN);
53
54
  template <class ELFT> Symbol *addUndefined(StringRef Name);
55
  template <class ELFT>
56
  Symbol *addUndefined(StringRef Name, bool IsLocal, uint8_t Binding,
57
                       uint8_t StOther, uint8_t Type, bool CanOmitFromDynSym,
58
                       InputFile *File);
59
  template <class ELFT>
60
  Symbol *addRegular(StringRef Name, uint8_t StOther, uint8_t Type,
61
                     uint64_t Value, uint64_t Size, uint8_t Binding,
62
                     SectionBase *Section, InputFile *File);
63
64
  template <class ELFT>
65
  void addShared(SharedFile<ELFT> *F, StringRef Name,
66
                 const typename ELFT::Sym &Sym,
67
                 const typename ELFT::Verdef *Verdef);
68
69
  template <class ELFT>
70
  Symbol *addLazyArchive(ArchiveFile *F, const llvm::object::Archive::Symbol S);
71
  template <class ELFT> void addLazyObject(StringRef Name, LazyObjFile &Obj);
72
73
  Symbol *addBitcode(StringRef Name, uint8_t Binding, uint8_t StOther,
74
                     uint8_t Type, bool CanOmitFromDynSym, BitcodeFile *File);
75
76
  Symbol *addCommon(StringRef N, uint64_t Size, uint32_t Alignment,
77
                    uint8_t Binding, uint8_t StOther, uint8_t Type,
78
                    InputFile *File);
79
80
  std::pair<Symbol *, bool> insert(StringRef Name);
81
  std::pair<Symbol *, bool> insert(StringRef Name, uint8_t Type,
82
                                   uint8_t Visibility, bool CanOmitFromDynSym,
83
                                   InputFile *File);
84
85
  template <class ELFT> void scanUndefinedFlags();
86
  template <class ELFT> void scanShlibUndefined();
87
  void scanVersionScript();
88
89
  SymbolBody *find(StringRef Name);
90
91
  void trace(StringRef Name);
92
93
  void handleDynamicList();
94
95
private:
96
  std::vector<SymbolBody *> findByVersion(SymbolVersion Ver);
97
  std::vector<SymbolBody *> findAllByVersion(SymbolVersion Ver);
98
  void defsym(Symbol *Dst, Symbol *Src);
99
100
  llvm::StringMap<std::vector<SymbolBody *>> &getDemangledSyms();
101
  void handleAnonymousVersion();
102
  void assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
103
                          StringRef VersionName);
104
  void assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId);
105
106
  struct SymIndex {
107
6.88k
    SymIndex(int Idx, bool Traced) : Idx(Idx), Traced(Traced) {}
108
    int Idx : 31;
109
    unsigned Traced : 1;
110
  };
111
112
  // The order the global symbols are in is not defined. We can use an arbitrary
113
  // order, but it has to be reproducible. That is true even when cross linking.
114
  // The default hashing of StringRef produces different results on 32 and 64
115
  // bit systems so we use a map to a vector. That is arbitrary, deterministic
116
  // but a bit inefficient.
117
  // FIXME: Experiment with passing in a custom hashing or sorting the symbols
118
  // once symbol resolution is finished.
119
  llvm::DenseMap<llvm::CachedHashStringRef, SymIndex> Symtab;
120
  std::vector<Symbol *> SymVector;
121
122
  // Comdat groups define "link once" sections. If two comdat groups have the
123
  // same name, only one of them is linked, and the other is ignored. This set
124
  // is used to uniquify them.
125
  llvm::DenseSet<llvm::CachedHashStringRef> ComdatGroups;
126
127
  // Set of .so files to not link the same shared object file more than once.
128
  llvm::DenseSet<StringRef> SoNames;
129
130
  // A map from demangled symbol names to their symbol objects.
131
  // This mapping is 1:N because two symbols with different versions
132
  // can have the same name. We use this map to handle "extern C++ {}"
133
  // directive in version scripts.
134
  llvm::Optional<llvm::StringMap<std::vector<SymbolBody *>>> DemangledSyms;
135
136
  struct SymbolRenaming {
137
    Symbol *Dst;
138
    Symbol *Src;
139
    uint8_t Binding;
140
  };
141
142
  // For -defsym or -wrap.
143
  std::vector<SymbolRenaming> Defsyms;
144
145
  // For LTO.
146
  std::unique_ptr<BitcodeCompiler> LTO;
147
};
148
149
extern SymbolTable *Symtab;
150
} // namespace elf
151
} // namespace lld
152
153
#endif