Coverage Report

Created: 2018-08-19 14:04

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/SymbolTable.cpp
Line
Count
Source
1
//===- SymbolTable.cpp ----------------------------------------------------===//
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
// Symbol table is a bag of all known symbols. We put all symbols of
11
// all input files to the symbol table. The symbol table is basically
12
// a hash table with the logic to resolve symbol name conflicts using
13
// the symbol types.
14
//
15
//===----------------------------------------------------------------------===//
16
17
#include "SymbolTable.h"
18
#include "Config.h"
19
#include "LinkerScript.h"
20
#include "Symbols.h"
21
#include "SyntheticSections.h"
22
#include "lld/Common/ErrorHandler.h"
23
#include "lld/Common/Memory.h"
24
#include "lld/Common/Strings.h"
25
#include "llvm/ADT/STLExtras.h"
26
27
using namespace llvm;
28
using namespace llvm::object;
29
using namespace llvm::ELF;
30
31
using namespace lld;
32
using namespace lld::elf;
33
34
SymbolTable *elf::Symtab;
35
36
9
static InputFile *getFirstElf() {
37
9
  if (!ObjectFiles.empty())
38
6
    return ObjectFiles[0];
39
3
  if (!SharedFiles.empty())
40
2
    return SharedFiles[0];
41
1
  return BitcodeFiles[0];
42
1
}
43
44
// All input object files must be for the same architecture
45
// (e.g. it does not make sense to link x86 object files with
46
// MIPS object files.) This function checks for that error.
47
3.31k
static bool isCompatible(InputFile *F) {
48
3.31k
  if (!F->isElf() && 
!isa<BitcodeFile>(F)374
)
49
135
    return true;
50
3.18k
51
3.18k
  if (F->EKind == Config->EKind && 
F->EMachine == Config->EMachine3.16k
) {
52
3.16k
    if (Config->EMachine != EM_MIPS)
53
2.89k
      return true;
54
270
    if (isMipsN32Abi(F) == Config->MipsN32Abi)
55
269
      return true;
56
15
  }
57
15
58
15
  if (!Config->Emulation.empty())
59
6
    error(toString(F) + " is incompatible with " + Config->Emulation);
60
9
  else
61
9
    error(toString(F) + " is incompatible with " + toString(getFirstElf()));
62
15
  return false;
63
15
}
64
65
// Add symbols in File to the symbol table.
66
3.31k
template <class ELFT> void SymbolTable::addFile(InputFile *File) {
67
3.31k
  if (!isCompatible(File))
68
15
    return;
69
3.30k
70
3.30k
  // Binary file
71
3.30k
  if (auto *F = dyn_cast<BinaryFile>(File)) {
72
6
    BinaryFiles.push_back(F);
73
6
    F->parse();
74
6
    return;
75
6
  }
76
3.29k
77
3.29k
  // .a file
78
3.29k
  if (auto *F = dyn_cast<ArchiveFile>(File)) {
79
100
    F->parse<ELFT>();
80
100
    return;
81
100
  }
82
3.19k
83
3.19k
  // Lazy object file
84
3.19k
  if (auto *F = dyn_cast<LazyObjFile>(File)) {
85
29
    LazyObjFiles.push_back(F);
86
29
    F->parse<ELFT>();
87
29
    return;
88
29
  }
89
3.16k
90
3.16k
  if (Config->Trace)
91
7
    message(toString(File));
92
3.16k
93
3.16k
  // .so file
94
3.16k
  if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
95
336
    // DSOs are uniquified not by filename but by soname.
96
336
    F->parseSoName();
97
336
    if (errorCount() || 
!SoNames.insert(F->SoName).second334
)
98
4
      return;
99
332
    SharedFiles.push_back(F);
100
332
    F->parseRest();
101
332
    return;
102
332
  }
103
2.82k
104
2.82k
  // LLVM bitcode file
105
2.82k
  if (auto *F = dyn_cast<BitcodeFile>(File)) {
106
237
    BitcodeFiles.push_back(F);
107
237
    F->parse<ELFT>(ComdatGroups);
108
237
    return;
109
237
  }
110
2.59k
111
2.59k
  // Regular object file
112
2.59k
  ObjectFiles.push_back(File);
113
2.59k
  cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
114
2.59k
}
void lld::elf::SymbolTable::addFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::InputFile*)
Line
Count
Source
66
411
template <class ELFT> void SymbolTable::addFile(InputFile *File) {
67
411
  if (!isCompatible(File))
68
8
    return;
69
403
70
403
  // Binary file
71
403
  if (auto *F = dyn_cast<BinaryFile>(File)) {
72
0
    BinaryFiles.push_back(F);
73
0
    F->parse();
74
0
    return;
75
0
  }
76
403
77
403
  // .a file
78
403
  if (auto *F = dyn_cast<ArchiveFile>(File)) {
79
3
    F->parse<ELFT>();
80
3
    return;
81
3
  }
82
400
83
400
  // Lazy object file
84
400
  if (auto *F = dyn_cast<LazyObjFile>(File)) {
85
0
    LazyObjFiles.push_back(F);
86
0
    F->parse<ELFT>();
87
0
    return;
88
0
  }
89
400
90
400
  if (Config->Trace)
91
0
    message(toString(File));
92
400
93
400
  // .so file
94
400
  if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
95
58
    // DSOs are uniquified not by filename but by soname.
96
58
    F->parseSoName();
97
58
    if (errorCount() || 
!SoNames.insert(F->SoName).second57
)
98
2
      return;
99
56
    SharedFiles.push_back(F);
100
56
    F->parseRest();
101
56
    return;
102
56
  }
103
342
104
342
  // LLVM bitcode file
105
342
  if (auto *F = dyn_cast<BitcodeFile>(File)) {
106
1
    BitcodeFiles.push_back(F);
107
1
    F->parse<ELFT>(ComdatGroups);
108
1
    return;
109
1
  }
110
341
111
341
  // Regular object file
112
341
  ObjectFiles.push_back(File);
113
341
  cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
114
341
}
void lld::elf::SymbolTable::addFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::InputFile*)
Line
Count
Source
66
182
template <class ELFT> void SymbolTable::addFile(InputFile *File) {
67
182
  if (!isCompatible(File))
68
1
    return;
69
181
70
181
  // Binary file
71
181
  if (auto *F = dyn_cast<BinaryFile>(File)) {
72
0
    BinaryFiles.push_back(F);
73
0
    F->parse();
74
0
    return;
75
0
  }
76
181
77
181
  // .a file
78
181
  if (auto *F = dyn_cast<ArchiveFile>(File)) {
79
0
    F->parse<ELFT>();
80
0
    return;
81
0
  }
82
181
83
181
  // Lazy object file
84
181
  if (auto *F = dyn_cast<LazyObjFile>(File)) {
85
0
    LazyObjFiles.push_back(F);
86
0
    F->parse<ELFT>();
87
0
    return;
88
0
  }
89
181
90
181
  if (Config->Trace)
91
0
    message(toString(File));
92
181
93
181
  // .so file
94
181
  if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
95
24
    // DSOs are uniquified not by filename but by soname.
96
24
    F->parseSoName();
97
24
    if (errorCount() || !SoNames.insert(F->SoName).second)
98
0
      return;
99
24
    SharedFiles.push_back(F);
100
24
    F->parseRest();
101
24
    return;
102
24
  }
103
157
104
157
  // LLVM bitcode file
105
157
  if (auto *F = dyn_cast<BitcodeFile>(File)) {
106
0
    BitcodeFiles.push_back(F);
107
0
    F->parse<ELFT>(ComdatGroups);
108
0
    return;
109
0
  }
110
157
111
157
  // Regular object file
112
157
  ObjectFiles.push_back(File);
113
157
  cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
114
157
}
void lld::elf::SymbolTable::addFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::InputFile*)
Line
Count
Source
66
2.59k
template <class ELFT> void SymbolTable::addFile(InputFile *File) {
67
2.59k
  if (!isCompatible(File))
68
3
    return;
69
2.59k
70
2.59k
  // Binary file
71
2.59k
  if (auto *F = dyn_cast<BinaryFile>(File)) {
72
6
    BinaryFiles.push_back(F);
73
6
    F->parse();
74
6
    return;
75
6
  }
76
2.58k
77
2.58k
  // .a file
78
2.58k
  if (auto *F = dyn_cast<ArchiveFile>(File)) {
79
96
    F->parse<ELFT>();
80
96
    return;
81
96
  }
82
2.49k
83
2.49k
  // Lazy object file
84
2.49k
  if (auto *F = dyn_cast<LazyObjFile>(File)) {
85
29
    LazyObjFiles.push_back(F);
86
29
    F->parse<ELFT>();
87
29
    return;
88
29
  }
89
2.46k
90
2.46k
  if (Config->Trace)
91
7
    message(toString(File));
92
2.46k
93
2.46k
  // .so file
94
2.46k
  if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
95
237
    // DSOs are uniquified not by filename but by soname.
96
237
    F->parseSoName();
97
237
    if (errorCount() || 
!SoNames.insert(F->SoName).second236
)
98
2
      return;
99
235
    SharedFiles.push_back(F);
100
235
    F->parseRest();
101
235
    return;
102
235
  }
103
2.22k
104
2.22k
  // LLVM bitcode file
105
2.22k
  if (auto *F = dyn_cast<BitcodeFile>(File)) {
106
236
    BitcodeFiles.push_back(F);
107
236
    F->parse<ELFT>(ComdatGroups);
108
236
    return;
109
236
  }
110
1.99k
111
1.99k
  // Regular object file
112
1.99k
  ObjectFiles.push_back(File);
113
1.99k
  cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
114
1.99k
}
void lld::elf::SymbolTable::addFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::InputFile*)
Line
Count
Source
66
125
template <class ELFT> void SymbolTable::addFile(InputFile *File) {
67
125
  if (!isCompatible(File))
68
3
    return;
69
122
70
122
  // Binary file
71
122
  if (auto *F = dyn_cast<BinaryFile>(File)) {
72
0
    BinaryFiles.push_back(F);
73
0
    F->parse();
74
0
    return;
75
0
  }
76
122
77
122
  // .a file
78
122
  if (auto *F = dyn_cast<ArchiveFile>(File)) {
79
1
    F->parse<ELFT>();
80
1
    return;
81
1
  }
82
121
83
121
  // Lazy object file
84
121
  if (auto *F = dyn_cast<LazyObjFile>(File)) {
85
0
    LazyObjFiles.push_back(F);
86
0
    F->parse<ELFT>();
87
0
    return;
88
0
  }
89
121
90
121
  if (Config->Trace)
91
0
    message(toString(File));
92
121
93
121
  // .so file
94
121
  if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
95
17
    // DSOs are uniquified not by filename but by soname.
96
17
    F->parseSoName();
97
17
    if (errorCount() || !SoNames.insert(F->SoName).second)
98
0
      return;
99
17
    SharedFiles.push_back(F);
100
17
    F->parseRest();
101
17
    return;
102
17
  }
103
104
104
104
  // LLVM bitcode file
105
104
  if (auto *F = dyn_cast<BitcodeFile>(File)) {
106
0
    BitcodeFiles.push_back(F);
107
0
    F->parse<ELFT>(ComdatGroups);
108
0
    return;
109
0
  }
110
104
111
104
  // Regular object file
112
104
  ObjectFiles.push_back(File);
113
104
  cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
114
104
}
115
116
// This function is where all the optimizations of link-time
117
// optimization happens. When LTO is in use, some input files are
118
// not in native object file format but in the LLVM bitcode format.
119
// This function compiles bitcode files into a few big native files
120
// using LLVM functions and replaces bitcode symbols with the results.
121
// Because all bitcode files that the program consists of are passed
122
// to the compiler at once, it can do whole-program optimization.
123
2.25k
template <class ELFT> void SymbolTable::addCombinedLTOObject() {
124
2.25k
  if (BitcodeFiles.empty())
125
2.09k
    return;
126
162
127
162
  // Compile bitcode files and replace bitcode symbols.
128
162
  LTO.reset(new BitcodeCompiler);
129
162
  for (BitcodeFile *F : BitcodeFiles)
130
234
    LTO->add(*F);
131
162
132
195
  for (InputFile *File : LTO->compile()) {
133
195
    DenseSet<CachedHashStringRef> DummyGroups;
134
195
    auto *Obj = cast<ObjFile<ELFT>>(File);
135
195
    Obj->parse(DummyGroups);
136
195
    for (Symbol *Sym : Obj->getGlobalSymbols())
137
245
      Sym->parseSymbolVersion();
138
195
    ObjectFiles.push_back(File);
139
195
  }
140
162
}
void lld::elf::SymbolTable::addCombinedLTOObject<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
123
269
template <class ELFT> void SymbolTable::addCombinedLTOObject() {
124
269
  if (BitcodeFiles.empty())
125
269
    return;
126
0
127
0
  // Compile bitcode files and replace bitcode symbols.
128
0
  LTO.reset(new BitcodeCompiler);
129
0
  for (BitcodeFile *F : BitcodeFiles)
130
0
    LTO->add(*F);
131
0
132
0
  for (InputFile *File : LTO->compile()) {
133
0
    DenseSet<CachedHashStringRef> DummyGroups;
134
0
    auto *Obj = cast<ObjFile<ELFT>>(File);
135
0
    Obj->parse(DummyGroups);
136
0
    for (Symbol *Sym : Obj->getGlobalSymbols())
137
0
      Sym->parseSymbolVersion();
138
0
    ObjectFiles.push_back(File);
139
0
  }
140
0
}
void lld::elf::SymbolTable::addCombinedLTOObject<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
123
119
template <class ELFT> void SymbolTable::addCombinedLTOObject() {
124
119
  if (BitcodeFiles.empty())
125
119
    return;
126
0
127
0
  // Compile bitcode files and replace bitcode symbols.
128
0
  LTO.reset(new BitcodeCompiler);
129
0
  for (BitcodeFile *F : BitcodeFiles)
130
0
    LTO->add(*F);
131
0
132
0
  for (InputFile *File : LTO->compile()) {
133
0
    DenseSet<CachedHashStringRef> DummyGroups;
134
0
    auto *Obj = cast<ObjFile<ELFT>>(File);
135
0
    Obj->parse(DummyGroups);
136
0
    for (Symbol *Sym : Obj->getGlobalSymbols())
137
0
      Sym->parseSymbolVersion();
138
0
    ObjectFiles.push_back(File);
139
0
  }
140
0
}
void lld::elf::SymbolTable::addCombinedLTOObject<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
123
1.78k
template <class ELFT> void SymbolTable::addCombinedLTOObject() {
124
1.78k
  if (BitcodeFiles.empty())
125
1.62k
    return;
126
162
127
162
  // Compile bitcode files and replace bitcode symbols.
128
162
  LTO.reset(new BitcodeCompiler);
129
162
  for (BitcodeFile *F : BitcodeFiles)
130
234
    LTO->add(*F);
131
162
132
195
  for (InputFile *File : LTO->compile()) {
133
195
    DenseSet<CachedHashStringRef> DummyGroups;
134
195
    auto *Obj = cast<ObjFile<ELFT>>(File);
135
195
    Obj->parse(DummyGroups);
136
195
    for (Symbol *Sym : Obj->getGlobalSymbols())
137
245
      Sym->parseSymbolVersion();
138
195
    ObjectFiles.push_back(File);
139
195
  }
140
162
}
void lld::elf::SymbolTable::addCombinedLTOObject<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
123
85
template <class ELFT> void SymbolTable::addCombinedLTOObject() {
124
85
  if (BitcodeFiles.empty())
125
85
    return;
126
0
127
0
  // Compile bitcode files and replace bitcode symbols.
128
0
  LTO.reset(new BitcodeCompiler);
129
0
  for (BitcodeFile *F : BitcodeFiles)
130
0
    LTO->add(*F);
131
0
132
0
  for (InputFile *File : LTO->compile()) {
133
0
    DenseSet<CachedHashStringRef> DummyGroups;
134
0
    auto *Obj = cast<ObjFile<ELFT>>(File);
135
0
    Obj->parse(DummyGroups);
136
0
    for (Symbol *Sym : Obj->getGlobalSymbols())
137
0
      Sym->parseSymbolVersion();
138
0
    ObjectFiles.push_back(File);
139
0
  }
140
0
}
141
142
Defined *SymbolTable::addAbsolute(StringRef Name, uint8_t Visibility,
143
177
                                  uint8_t Binding) {
144
177
  Symbol *Sym =
145
177
      addRegular(Name, Visibility, STT_NOTYPE, 0, 0, Binding, nullptr, nullptr);
146
177
  return cast<Defined>(Sym);
147
177
}
148
149
// Set a flag for --trace-symbol so that we can print out a log message
150
// if a new symbol with the same name is inserted into the symbol table.
151
34
void SymbolTable::trace(StringRef Name) {
152
34
  SymMap.insert({CachedHashStringRef(Name), -1});
153
34
}
154
155
// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
156
// Used to implement --wrap.
157
14
template <class ELFT> void SymbolTable::addSymbolWrap(StringRef Name) {
158
14
  Symbol *Sym = find(Name);
159
14
  if (!Sym)
160
3
    return;
161
11
162
11
  // Do not wrap the same symbol twice.
163
11
  for (const WrappedSymbol &S : WrappedSymbols)
164
1
    if (S.Sym == Sym)
165
1
      return;
166
11
167
11
  Symbol *Real = addUndefined<ELFT>(Saver.save("__real_" + Name));
168
10
  Symbol *Wrap = addUndefined<ELFT>(Saver.save("__wrap_" + Name));
169
10
  WrappedSymbols.push_back({Sym, Real, Wrap});
170
10
171
10
  // We want to tell LTO not to inline symbols to be overwritten
172
10
  // because LTO doesn't know the final symbol contents after renaming.
173
10
  Real->CanInline = false;
174
10
  Sym->CanInline = false;
175
10
176
10
  // Tell LTO not to eliminate these symbols.
177
10
  Sym->IsUsedInRegularObj = true;
178
10
  Wrap->IsUsedInRegularObj = true;
179
10
}
Unexecuted instantiation: void lld::elf::SymbolTable::addSymbolWrap<llvm::object::ELFType<(llvm::support::endianness)1, false> >(llvm::StringRef)
Unexecuted instantiation: void lld::elf::SymbolTable::addSymbolWrap<llvm::object::ELFType<(llvm::support::endianness)0, false> >(llvm::StringRef)
void lld::elf::SymbolTable::addSymbolWrap<llvm::object::ELFType<(llvm::support::endianness)1, true> >(llvm::StringRef)
Line
Count
Source
157
14
template <class ELFT> void SymbolTable::addSymbolWrap(StringRef Name) {
158
14
  Symbol *Sym = find(Name);
159
14
  if (!Sym)
160
3
    return;
161
11
162
11
  // Do not wrap the same symbol twice.
163
11
  for (const WrappedSymbol &S : WrappedSymbols)
164
1
    if (S.Sym == Sym)
165
1
      return;
166
11
167
11
  Symbol *Real = addUndefined<ELFT>(Saver.save("__real_" + Name));
168
10
  Symbol *Wrap = addUndefined<ELFT>(Saver.save("__wrap_" + Name));
169
10
  WrappedSymbols.push_back({Sym, Real, Wrap});
170
10
171
10
  // We want to tell LTO not to inline symbols to be overwritten
172
10
  // because LTO doesn't know the final symbol contents after renaming.
173
10
  Real->CanInline = false;
174
10
  Sym->CanInline = false;
175
10
176
10
  // Tell LTO not to eliminate these symbols.
177
10
  Sym->IsUsedInRegularObj = true;
178
10
  Wrap->IsUsedInRegularObj = true;
179
10
}
Unexecuted instantiation: void lld::elf::SymbolTable::addSymbolWrap<llvm::object::ELFType<(llvm::support::endianness)0, true> >(llvm::StringRef)
180
181
// Apply symbol renames created by -wrap. The renames are created
182
// before LTO in addSymbolWrap() to have a chance to inform LTO (if
183
// LTO is running) not to include these symbols in IPO. Now that the
184
// symbols are finalized, we can perform the replacement.
185
2.23k
void SymbolTable::applySymbolWrap() {
186
2.23k
  // This function rotates 3 symbols:
187
2.23k
  //
188
2.23k
  // __real_sym becomes sym
189
2.23k
  // sym        becomes __wrap_sym
190
2.23k
  // __wrap_sym becomes __real_sym
191
2.23k
  //
192
2.23k
  // The last part is special in that we don't want to change what references to
193
2.23k
  // __wrap_sym point to, we just want have __real_sym in the symbol table.
194
2.23k
195
2.23k
  for (WrappedSymbol &W : WrappedSymbols) {
196
10
    // First, make a copy of __real_sym.
197
10
    Symbol *Real = nullptr;
198
10
    if (W.Real->isDefined()) {
199
7
      Real = reinterpret_cast<Symbol *>(make<SymbolUnion>());
200
7
      memcpy(Real, W.Real, sizeof(SymbolUnion));
201
7
    }
202
10
203
10
    // Replace __real_sym with sym and sym with __wrap_sym.
204
10
    memcpy(W.Real, W.Sym, sizeof(SymbolUnion));
205
10
    memcpy(W.Sym, W.Wrap, sizeof(SymbolUnion));
206
10
207
10
    // We now have two copies of __wrap_sym. Drop one.
208
10
    W.Wrap->IsUsedInRegularObj = false;
209
10
210
10
    if (Real)
211
7
      SymVector.push_back(Real);
212
10
  }
213
2.23k
}
214
215
18.9k
static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
216
18.9k
  if (VA == STV_DEFAULT)
217
18.8k
    return VB;
218
90
  if (VB == STV_DEFAULT)
219
43
    return VA;
220
47
  return std::min(VA, VB);
221
47
}
222
223
// Find an existing symbol or create and insert a new one.
224
19.2k
std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {
225
19.2k
  // <name>@@<version> means the symbol is the default version. In that
226
19.2k
  // case <name>@@<version> will be used to resolve references to <name>.
227
19.2k
  //
228
19.2k
  // Since this is a hot path, the following string search code is
229
19.2k
  // optimized for speed. StringRef::find(char) is much faster than
230
19.2k
  // StringRef::find(StringRef).
231
19.2k
  size_t Pos = Name.find('@');
232
19.2k
  if (Pos != StringRef::npos && 
Pos + 1 < Name.size()103
&&
Name[Pos + 1] == '@'102
)
233
22
    Name = Name.take_front(Pos);
234
19.2k
235
19.2k
  auto P = SymMap.insert({CachedHashStringRef(Name), (int)SymVector.size()});
236
19.2k
  int &SymIndex = P.first->second;
237
19.2k
  bool IsNew = P.second;
238
19.2k
  bool Traced = false;
239
19.2k
240
19.2k
  if (SymIndex == -1) {
241
28
    SymIndex = SymVector.size();
242
28
    IsNew = Traced = true;
243
28
  }
244
19.2k
245
19.2k
  Symbol *Sym;
246
19.2k
  if (IsNew) {
247
17.2k
    Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
248
17.2k
    Sym->Visibility = STV_DEFAULT;
249
17.2k
    Sym->IsUsedInRegularObj = false;
250
17.2k
    Sym->ExportDynamic = false;
251
17.2k
    Sym->CanInline = true;
252
17.2k
    Sym->Traced = Traced;
253
17.2k
    Sym->VersionId = Config->DefaultSymbolVersion;
254
17.2k
    SymVector.push_back(Sym);
255
17.2k
  } else {
256
1.98k
    Sym = SymVector[SymIndex];
257
1.98k
  }
258
19.2k
  return {Sym, IsNew};
259
19.2k
}
260
261
// Find an existing symbol or create and insert a new one, then apply the given
262
// attributes.
263
std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, uint8_t Type,
264
                                              uint8_t Visibility,
265
                                              bool CanOmitFromDynSym,
266
18.9k
                                              InputFile *File) {
267
18.9k
  Symbol *S;
268
18.9k
  bool WasInserted;
269
18.9k
  std::tie(S, WasInserted) = insert(Name);
270
18.9k
271
18.9k
  // Merge in the new symbol's visibility.
272
18.9k
  S->Visibility = getMinVisibility(S->Visibility, Visibility);
273
18.9k
274
18.9k
  if (!CanOmitFromDynSym && 
(18.1k
Config->Shared18.1k
||
Config->ExportDynamic15.0k
))
275
3.19k
    S->ExportDynamic = true;
276
18.9k
277
18.9k
  if (!File || 
File->kind() == InputFile::ObjKind16.8k
)
278
17.7k
    S->IsUsedInRegularObj = true;
279
18.9k
280
18.9k
  if (!WasInserted && 
S->Type != Symbol::UnknownType1.88k
&&
281
18.9k
      
((Type == STT_TLS) != S->isTls())1.79k
) {
282
1
    error("TLS attribute mismatch: " + toString(*S) + "\n>>> defined in " +
283
1
          toString(S->File) + "\n>>> defined in " + toString(File));
284
1
  }
285
18.9k
286
18.9k
  return {S, WasInserted};
287
18.9k
}
288
289
95
template <class ELFT> Symbol *SymbolTable::addUndefined(StringRef Name) {
290
95
  return addUndefined<ELFT>(Name, STB_GLOBAL, STV_DEFAULT,
291
95
                            /*Type*/ 0,
292
95
                            /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
293
95
}
Unexecuted instantiation: lld::elf::Symbol* lld::elf::SymbolTable::addUndefined<llvm::object::ELFType<(llvm::support::endianness)1, false> >(llvm::StringRef)
Unexecuted instantiation: lld::elf::Symbol* lld::elf::SymbolTable::addUndefined<llvm::object::ELFType<(llvm::support::endianness)0, false> >(llvm::StringRef)
lld::elf::Symbol* lld::elf::SymbolTable::addUndefined<llvm::object::ELFType<(llvm::support::endianness)1, true> >(llvm::StringRef)
Line
Count
Source
289
91
template <class ELFT> Symbol *SymbolTable::addUndefined(StringRef Name) {
290
91
  return addUndefined<ELFT>(Name, STB_GLOBAL, STV_DEFAULT,
291
91
                            /*Type*/ 0,
292
91
                            /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
293
91
}
lld::elf::Symbol* lld::elf::SymbolTable::addUndefined<llvm::object::ELFType<(llvm::support::endianness)0, true> >(llvm::StringRef)
Line
Count
Source
289
4
template <class ELFT> Symbol *SymbolTable::addUndefined(StringRef Name) {
290
4
  return addUndefined<ELFT>(Name, STB_GLOBAL, STV_DEFAULT,
291
4
                            /*Type*/ 0,
292
4
                            /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
293
4
}
294
295
17.5k
static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; }
296
297
template <class ELFT>
298
Symbol *SymbolTable::addUndefined(StringRef Name, uint8_t Binding,
299
                                  uint8_t StOther, uint8_t Type,
300
1.57k
                                  bool CanOmitFromDynSym, InputFile *File) {
301
1.57k
  Symbol *S;
302
1.57k
  bool WasInserted;
303
1.57k
  uint8_t Visibility = getVisibility(StOther);
304
1.57k
  std::tie(S, WasInserted) =
305
1.57k
      insert(Name, Type, Visibility, CanOmitFromDynSym, File);
306
1.57k
307
1.57k
  // An undefined symbol with non default visibility must be satisfied
308
1.57k
  // in the same DSO.
309
1.57k
  if (WasInserted || 
(293
isa<SharedSymbol>(S)293
&&
Visibility != STV_DEFAULT35
)) {
310
1.28k
    replaceSymbol<Undefined>(S, File, Name, Binding, StOther, Type);
311
1.28k
    return S;
312
1.28k
  }
313
291
314
291
  if (S->isShared() || 
S->isLazy()258
||
(232
S->isUndefined()232
&&
Binding != STB_WEAK58
))
315
115
    S->Binding = Binding;
316
291
317
291
  if (!Config->GcSections && 
Binding != STB_WEAK281
)
318
266
    if (auto *SS = dyn_cast<SharedSymbol>(S))
319
27
      SS->getFile<ELFT>().IsNeeded = true;
320
291
321
291
  if (S->isLazy()) {
322
26
    // An undefined weak will not fetch archive members. See comment on Lazy in
323
26
    // Symbols.h for the details.
324
26
    if (Binding == STB_WEAK) {
325
3
      S->Type = Type;
326
3
      return S;
327
3
    }
328
23
329
23
    // Do extra check for --warn-backrefs.
330
23
    //
331
23
    // --warn-backrefs is an option to prevent an undefined reference from
332
23
    // fetching an archive member written earlier in the command line. It can be
333
23
    // used to keep compatibility with GNU linkers to some degree.
334
23
    // I'll explain the feature and why you may find it useful in this comment.
335
23
    //
336
23
    // lld's symbol resolution semantics is more relaxed than traditional Unix
337
23
    // linkers. For example,
338
23
    //
339
23
    //   ld.lld foo.a bar.o
340
23
    //
341
23
    // succeeds even if bar.o contains an undefined symbol that has to be
342
23
    // resolved by some object file in foo.a. Traditional Unix linkers don't
343
23
    // allow this kind of backward reference, as they visit each file only once
344
23
    // from left to right in the command line while resolving all undefined
345
23
    // symbols at the moment of visiting.
346
23
    //
347
23
    // In the above case, since there's no undefined symbol when a linker visits
348
23
    // foo.a, no files are pulled out from foo.a, and because the linker forgets
349
23
    // about foo.a after visiting, it can't resolve undefined symbols in bar.o
350
23
    // that could have been resolved otherwise.
351
23
    //
352
23
    // That lld accepts more relaxed form means that (besides it'd make more
353
23
    // sense) you can accidentally write a command line or a build file that
354
23
    // works only with lld, even if you have a plan to distribute it to wider
355
23
    // users who may be using GNU linkers. With --warn-backrefs, you can detect
356
23
    // a library order that doesn't work with other Unix linkers.
357
23
    //
358
23
    // The option is also useful to detect cyclic dependencies between static
359
23
    // archives. Again, lld accepts
360
23
    //
361
23
    //   ld.lld foo.a bar.a
362
23
    //
363
23
    // even if foo.a and bar.a depend on each other. With --warn-backrefs, it is
364
23
    // handled as an error.
365
23
    //
366
23
    // Here is how the option works. We assign a group ID to each file. A file
367
23
    // with a smaller group ID can pull out object files from an archive file
368
23
    // with an equal or greater group ID. Otherwise, it is a reverse dependency
369
23
    // and an error.
370
23
    //
371
23
    // A file outside --{start,end}-group gets a fresh ID when instantiated. All
372
23
    // files within the same --{start,end}-group get the same group ID. E.g.
373
23
    //
374
23
    //   ld.lld A B --start-group C D --end-group E
375
23
    //
376
23
    // A forms group 0. B form group 1. C and D (including their member object
377
23
    // files) form group 2. E forms group 3. I think that you can see how this
378
23
    // group assignment rule simulates the traditional linker's semantics.
379
23
    bool Backref =
380
23
        Config->WarnBackrefs && 
File10
&&
S->File->GroupId < File->GroupId10
;
381
23
    fetchLazy<ELFT>(S);
382
23
383
23
    // We don't report backward references to weak symbols as they can be
384
23
    // overridden later.
385
23
    if (Backref && 
S->Binding != STB_WEAK5
)
386
4
      warn("backward reference detected: " + Name + " in " + toString(File) +
387
4
           " refers to " + toString(S->File));
388
23
  }
389
291
  
return S288
;
390
291
}
lld::elf::Symbol* lld::elf::SymbolTable::addUndefined<llvm::object::ELFType<(llvm::support::endianness)1, false> >(llvm::StringRef, unsigned char, unsigned char, unsigned char, bool, lld::elf::InputFile*)
Line
Count
Source
300
185
                                  bool CanOmitFromDynSym, InputFile *File) {
301
185
  Symbol *S;
302
185
  bool WasInserted;
303
185
  uint8_t Visibility = getVisibility(StOther);
304
185
  std::tie(S, WasInserted) =
305
185
      insert(Name, Type, Visibility, CanOmitFromDynSym, File);
306
185
307
185
  // An undefined symbol with non default visibility must be satisfied
308
185
  // in the same DSO.
309
185
  if (WasInserted || 
(44
isa<SharedSymbol>(S)44
&&
Visibility != STV_DEFAULT12
)) {
310
141
    replaceSymbol<Undefined>(S, File, Name, Binding, StOther, Type);
311
141
    return S;
312
141
  }
313
44
314
44
  if (S->isShared() || 
S->isLazy()32
||
(31
S->isUndefined()31
&&
Binding != STB_WEAK0
))
315
13
    S->Binding = Binding;
316
44
317
44
  if (!Config->GcSections && Binding != STB_WEAK)
318
43
    if (auto *SS = dyn_cast<SharedSymbol>(S))
319
11
      SS->getFile<ELFT>().IsNeeded = true;
320
44
321
44
  if (S->isLazy()) {
322
1
    // An undefined weak will not fetch archive members. See comment on Lazy in
323
1
    // Symbols.h for the details.
324
1
    if (Binding == STB_WEAK) {
325
0
      S->Type = Type;
326
0
      return S;
327
0
    }
328
1
329
1
    // Do extra check for --warn-backrefs.
330
1
    //
331
1
    // --warn-backrefs is an option to prevent an undefined reference from
332
1
    // fetching an archive member written earlier in the command line. It can be
333
1
    // used to keep compatibility with GNU linkers to some degree.
334
1
    // I'll explain the feature and why you may find it useful in this comment.
335
1
    //
336
1
    // lld's symbol resolution semantics is more relaxed than traditional Unix
337
1
    // linkers. For example,
338
1
    //
339
1
    //   ld.lld foo.a bar.o
340
1
    //
341
1
    // succeeds even if bar.o contains an undefined symbol that has to be
342
1
    // resolved by some object file in foo.a. Traditional Unix linkers don't
343
1
    // allow this kind of backward reference, as they visit each file only once
344
1
    // from left to right in the command line while resolving all undefined
345
1
    // symbols at the moment of visiting.
346
1
    //
347
1
    // In the above case, since there's no undefined symbol when a linker visits
348
1
    // foo.a, no files are pulled out from foo.a, and because the linker forgets
349
1
    // about foo.a after visiting, it can't resolve undefined symbols in bar.o
350
1
    // that could have been resolved otherwise.
351
1
    //
352
1
    // That lld accepts more relaxed form means that (besides it'd make more
353
1
    // sense) you can accidentally write a command line or a build file that
354
1
    // works only with lld, even if you have a plan to distribute it to wider
355
1
    // users who may be using GNU linkers. With --warn-backrefs, you can detect
356
1
    // a library order that doesn't work with other Unix linkers.
357
1
    //
358
1
    // The option is also useful to detect cyclic dependencies between static
359
1
    // archives. Again, lld accepts
360
1
    //
361
1
    //   ld.lld foo.a bar.a
362
1
    //
363
1
    // even if foo.a and bar.a depend on each other. With --warn-backrefs, it is
364
1
    // handled as an error.
365
1
    //
366
1
    // Here is how the option works. We assign a group ID to each file. A file
367
1
    // with a smaller group ID can pull out object files from an archive file
368
1
    // with an equal or greater group ID. Otherwise, it is a reverse dependency
369
1
    // and an error.
370
1
    //
371
1
    // A file outside --{start,end}-group gets a fresh ID when instantiated. All
372
1
    // files within the same --{start,end}-group get the same group ID. E.g.
373
1
    //
374
1
    //   ld.lld A B --start-group C D --end-group E
375
1
    //
376
1
    // A forms group 0. B form group 1. C and D (including their member object
377
1
    // files) form group 2. E forms group 3. I think that you can see how this
378
1
    // group assignment rule simulates the traditional linker's semantics.
379
1
    bool Backref =
380
1
        Config->WarnBackrefs && 
File0
&&
S->File->GroupId < File->GroupId0
;
381
1
    fetchLazy<ELFT>(S);
382
1
383
1
    // We don't report backward references to weak symbols as they can be
384
1
    // overridden later.
385
1
    if (Backref && 
S->Binding != STB_WEAK0
)
386
0
      warn("backward reference detected: " + Name + " in " + toString(File) +
387
0
           " refers to " + toString(S->File));
388
1
  }
389
44
  return S;
390
44
}
lld::elf::Symbol* lld::elf::SymbolTable::addUndefined<llvm::object::ELFType<(llvm::support::endianness)0, false> >(llvm::StringRef, unsigned char, unsigned char, unsigned char, bool, lld::elf::InputFile*)
Line
Count
Source
300
94
                                  bool CanOmitFromDynSym, InputFile *File) {
301
94
  Symbol *S;
302
94
  bool WasInserted;
303
94
  uint8_t Visibility = getVisibility(StOther);
304
94
  std::tie(S, WasInserted) =
305
94
      insert(Name, Type, Visibility, CanOmitFromDynSym, File);
306
94
307
94
  // An undefined symbol with non default visibility must be satisfied
308
94
  // in the same DSO.
309
94
  if (WasInserted || 
(12
isa<SharedSymbol>(S)12
&&
Visibility != STV_DEFAULT0
)) {
310
82
    replaceSymbol<Undefined>(S, File, Name, Binding, StOther, Type);
311
82
    return S;
312
82
  }
313
12
314
12
  if (S->isShared() || S->isLazy() || (S->isUndefined() && 
Binding != STB_WEAK0
))
315
0
    S->Binding = Binding;
316
12
317
12
  if (!Config->GcSections && Binding != STB_WEAK)
318
12
    if (auto *SS = dyn_cast<SharedSymbol>(S))
319
0
      SS->getFile<ELFT>().IsNeeded = true;
320
12
321
12
  if (S->isLazy()) {
322
0
    // An undefined weak will not fetch archive members. See comment on Lazy in
323
0
    // Symbols.h for the details.
324
0
    if (Binding == STB_WEAK) {
325
0
      S->Type = Type;
326
0
      return S;
327
0
    }
328
0
329
0
    // Do extra check for --warn-backrefs.
330
0
    //
331
0
    // --warn-backrefs is an option to prevent an undefined reference from
332
0
    // fetching an archive member written earlier in the command line. It can be
333
0
    // used to keep compatibility with GNU linkers to some degree.
334
0
    // I'll explain the feature and why you may find it useful in this comment.
335
0
    //
336
0
    // lld's symbol resolution semantics is more relaxed than traditional Unix
337
0
    // linkers. For example,
338
0
    //
339
0
    //   ld.lld foo.a bar.o
340
0
    //
341
0
    // succeeds even if bar.o contains an undefined symbol that has to be
342
0
    // resolved by some object file in foo.a. Traditional Unix linkers don't
343
0
    // allow this kind of backward reference, as they visit each file only once
344
0
    // from left to right in the command line while resolving all undefined
345
0
    // symbols at the moment of visiting.
346
0
    //
347
0
    // In the above case, since there's no undefined symbol when a linker visits
348
0
    // foo.a, no files are pulled out from foo.a, and because the linker forgets
349
0
    // about foo.a after visiting, it can't resolve undefined symbols in bar.o
350
0
    // that could have been resolved otherwise.
351
0
    //
352
0
    // That lld accepts more relaxed form means that (besides it'd make more
353
0
    // sense) you can accidentally write a command line or a build file that
354
0
    // works only with lld, even if you have a plan to distribute it to wider
355
0
    // users who may be using GNU linkers. With --warn-backrefs, you can detect
356
0
    // a library order that doesn't work with other Unix linkers.
357
0
    //
358
0
    // The option is also useful to detect cyclic dependencies between static
359
0
    // archives. Again, lld accepts
360
0
    //
361
0
    //   ld.lld foo.a bar.a
362
0
    //
363
0
    // even if foo.a and bar.a depend on each other. With --warn-backrefs, it is
364
0
    // handled as an error.
365
0
    //
366
0
    // Here is how the option works. We assign a group ID to each file. A file
367
0
    // with a smaller group ID can pull out object files from an archive file
368
0
    // with an equal or greater group ID. Otherwise, it is a reverse dependency
369
0
    // and an error.
370
0
    //
371
0
    // A file outside --{start,end}-group gets a fresh ID when instantiated. All
372
0
    // files within the same --{start,end}-group get the same group ID. E.g.
373
0
    //
374
0
    //   ld.lld A B --start-group C D --end-group E
375
0
    //
376
0
    // A forms group 0. B form group 1. C and D (including their member object
377
0
    // files) form group 2. E forms group 3. I think that you can see how this
378
0
    // group assignment rule simulates the traditional linker's semantics.
379
0
    bool Backref =
380
0
        Config->WarnBackrefs && File && S->File->GroupId < File->GroupId;
381
0
    fetchLazy<ELFT>(S);
382
0
383
0
    // We don't report backward references to weak symbols as they can be
384
0
    // overridden later.
385
0
    if (Backref && S->Binding != STB_WEAK)
386
0
      warn("backward reference detected: " + Name + " in " + toString(File) +
387
0
           " refers to " + toString(S->File));
388
0
  }
389
12
  return S;
390
12
}
lld::elf::Symbol* lld::elf::SymbolTable::addUndefined<llvm::object::ELFType<(llvm::support::endianness)1, true> >(llvm::StringRef, unsigned char, unsigned char, unsigned char, bool, lld::elf::InputFile*)
Line
Count
Source
300
1.22k
                                  bool CanOmitFromDynSym, InputFile *File) {
301
1.22k
  Symbol *S;
302
1.22k
  bool WasInserted;
303
1.22k
  uint8_t Visibility = getVisibility(StOther);
304
1.22k
  std::tie(S, WasInserted) =
305
1.22k
      insert(Name, Type, Visibility, CanOmitFromDynSym, File);
306
1.22k
307
1.22k
  // An undefined symbol with non default visibility must be satisfied
308
1.22k
  // in the same DSO.
309
1.22k
  if (WasInserted || 
(232
isa<SharedSymbol>(S)232
&&
Visibility != STV_DEFAULT23
)) {
310
997
    replaceSymbol<Undefined>(S, File, Name, Binding, StOther, Type);
311
997
    return S;
312
997
  }
313
230
314
230
  if (S->isShared() || 
S->isLazy()209
||
(184
S->isUndefined()184
&&
Binding != STB_WEAK57
))
315
101
    S->Binding = Binding;
316
230
317
230
  if (!Config->GcSections && 
Binding != STB_WEAK220
)
318
206
    if (auto *SS = dyn_cast<SharedSymbol>(S))
319
16
      SS->getFile<ELFT>().IsNeeded = true;
320
230
321
230
  if (S->isLazy()) {
322
25
    // An undefined weak will not fetch archive members. See comment on Lazy in
323
25
    // Symbols.h for the details.
324
25
    if (Binding == STB_WEAK) {
325
3
      S->Type = Type;
326
3
      return S;
327
3
    }
328
22
329
22
    // Do extra check for --warn-backrefs.
330
22
    //
331
22
    // --warn-backrefs is an option to prevent an undefined reference from
332
22
    // fetching an archive member written earlier in the command line. It can be
333
22
    // used to keep compatibility with GNU linkers to some degree.
334
22
    // I'll explain the feature and why you may find it useful in this comment.
335
22
    //
336
22
    // lld's symbol resolution semantics is more relaxed than traditional Unix
337
22
    // linkers. For example,
338
22
    //
339
22
    //   ld.lld foo.a bar.o
340
22
    //
341
22
    // succeeds even if bar.o contains an undefined symbol that has to be
342
22
    // resolved by some object file in foo.a. Traditional Unix linkers don't
343
22
    // allow this kind of backward reference, as they visit each file only once
344
22
    // from left to right in the command line while resolving all undefined
345
22
    // symbols at the moment of visiting.
346
22
    //
347
22
    // In the above case, since there's no undefined symbol when a linker visits
348
22
    // foo.a, no files are pulled out from foo.a, and because the linker forgets
349
22
    // about foo.a after visiting, it can't resolve undefined symbols in bar.o
350
22
    // that could have been resolved otherwise.
351
22
    //
352
22
    // That lld accepts more relaxed form means that (besides it'd make more
353
22
    // sense) you can accidentally write a command line or a build file that
354
22
    // works only with lld, even if you have a plan to distribute it to wider
355
22
    // users who may be using GNU linkers. With --warn-backrefs, you can detect
356
22
    // a library order that doesn't work with other Unix linkers.
357
22
    //
358
22
    // The option is also useful to detect cyclic dependencies between static
359
22
    // archives. Again, lld accepts
360
22
    //
361
22
    //   ld.lld foo.a bar.a
362
22
    //
363
22
    // even if foo.a and bar.a depend on each other. With --warn-backrefs, it is
364
22
    // handled as an error.
365
22
    //
366
22
    // Here is how the option works. We assign a group ID to each file. A file
367
22
    // with a smaller group ID can pull out object files from an archive file
368
22
    // with an equal or greater group ID. Otherwise, it is a reverse dependency
369
22
    // and an error.
370
22
    //
371
22
    // A file outside --{start,end}-group gets a fresh ID when instantiated. All
372
22
    // files within the same --{start,end}-group get the same group ID. E.g.
373
22
    //
374
22
    //   ld.lld A B --start-group C D --end-group E
375
22
    //
376
22
    // A forms group 0. B form group 1. C and D (including their member object
377
22
    // files) form group 2. E forms group 3. I think that you can see how this
378
22
    // group assignment rule simulates the traditional linker's semantics.
379
22
    bool Backref =
380
22
        Config->WarnBackrefs && 
File10
&&
S->File->GroupId < File->GroupId10
;
381
22
    fetchLazy<ELFT>(S);
382
22
383
22
    // We don't report backward references to weak symbols as they can be
384
22
    // overridden later.
385
22
    if (Backref && 
S->Binding != STB_WEAK5
)
386
4
      warn("backward reference detected: " + Name + " in " + toString(File) +
387
4
           " refers to " + toString(S->File));
388
22
  }
389
230
  
return S227
;
390
230
}
lld::elf::Symbol* lld::elf::SymbolTable::addUndefined<llvm::object::ELFType<(llvm::support::endianness)0, true> >(llvm::StringRef, unsigned char, unsigned char, unsigned char, bool, lld::elf::InputFile*)
Line
Count
Source
300
68
                                  bool CanOmitFromDynSym, InputFile *File) {
301
68
  Symbol *S;
302
68
  bool WasInserted;
303
68
  uint8_t Visibility = getVisibility(StOther);
304
68
  std::tie(S, WasInserted) =
305
68
      insert(Name, Type, Visibility, CanOmitFromDynSym, File);
306
68
307
68
  // An undefined symbol with non default visibility must be satisfied
308
68
  // in the same DSO.
309
68
  if (WasInserted || 
(5
isa<SharedSymbol>(S)5
&&
Visibility != STV_DEFAULT0
)) {
310
63
    replaceSymbol<Undefined>(S, File, Name, Binding, StOther, Type);
311
63
    return S;
312
63
  }
313
5
314
5
  if (S->isShared() || S->isLazy() || (S->isUndefined() && 
Binding != STB_WEAK1
))
315
1
    S->Binding = Binding;
316
5
317
5
  if (!Config->GcSections && Binding != STB_WEAK)
318
5
    if (auto *SS = dyn_cast<SharedSymbol>(S))
319
0
      SS->getFile<ELFT>().IsNeeded = true;
320
5
321
5
  if (S->isLazy()) {
322
0
    // An undefined weak will not fetch archive members. See comment on Lazy in
323
0
    // Symbols.h for the details.
324
0
    if (Binding == STB_WEAK) {
325
0
      S->Type = Type;
326
0
      return S;
327
0
    }
328
0
329
0
    // Do extra check for --warn-backrefs.
330
0
    //
331
0
    // --warn-backrefs is an option to prevent an undefined reference from
332
0
    // fetching an archive member written earlier in the command line. It can be
333
0
    // used to keep compatibility with GNU linkers to some degree.
334
0
    // I'll explain the feature and why you may find it useful in this comment.
335
0
    //
336
0
    // lld's symbol resolution semantics is more relaxed than traditional Unix
337
0
    // linkers. For example,
338
0
    //
339
0
    //   ld.lld foo.a bar.o
340
0
    //
341
0
    // succeeds even if bar.o contains an undefined symbol that has to be
342
0
    // resolved by some object file in foo.a. Traditional Unix linkers don't
343
0
    // allow this kind of backward reference, as they visit each file only once
344
0
    // from left to right in the command line while resolving all undefined
345
0
    // symbols at the moment of visiting.
346
0
    //
347
0
    // In the above case, since there's no undefined symbol when a linker visits
348
0
    // foo.a, no files are pulled out from foo.a, and because the linker forgets
349
0
    // about foo.a after visiting, it can't resolve undefined symbols in bar.o
350
0
    // that could have been resolved otherwise.
351
0
    //
352
0
    // That lld accepts more relaxed form means that (besides it'd make more
353
0
    // sense) you can accidentally write a command line or a build file that
354
0
    // works only with lld, even if you have a plan to distribute it to wider
355
0
    // users who may be using GNU linkers. With --warn-backrefs, you can detect
356
0
    // a library order that doesn't work with other Unix linkers.
357
0
    //
358
0
    // The option is also useful to detect cyclic dependencies between static
359
0
    // archives. Again, lld accepts
360
0
    //
361
0
    //   ld.lld foo.a bar.a
362
0
    //
363
0
    // even if foo.a and bar.a depend on each other. With --warn-backrefs, it is
364
0
    // handled as an error.
365
0
    //
366
0
    // Here is how the option works. We assign a group ID to each file. A file
367
0
    // with a smaller group ID can pull out object files from an archive file
368
0
    // with an equal or greater group ID. Otherwise, it is a reverse dependency
369
0
    // and an error.
370
0
    //
371
0
    // A file outside --{start,end}-group gets a fresh ID when instantiated. All
372
0
    // files within the same --{start,end}-group get the same group ID. E.g.
373
0
    //
374
0
    //   ld.lld A B --start-group C D --end-group E
375
0
    //
376
0
    // A forms group 0. B form group 1. C and D (including their member object
377
0
    // files) form group 2. E forms group 3. I think that you can see how this
378
0
    // group assignment rule simulates the traditional linker's semantics.
379
0
    bool Backref =
380
0
        Config->WarnBackrefs && File && S->File->GroupId < File->GroupId;
381
0
    fetchLazy<ELFT>(S);
382
0
383
0
    // We don't report backward references to weak symbols as they can be
384
0
    // overridden later.
385
0
    if (Backref && S->Binding != STB_WEAK)
386
0
      warn("backward reference detected: " + Name + " in " + toString(File) +
387
0
           " refers to " + toString(S->File));
388
0
  }
389
5
  return S;
390
5
}
391
392
// Using .symver foo,foo@@VER unfortunately creates two symbols: foo and
393
// foo@@VER. We want to effectively ignore foo, so give precedence to
394
// foo@@VER.
395
// FIXME: If users can transition to using
396
// .symver foo,foo@@@VER
397
// we can delete this hack.
398
95
static int compareVersion(Symbol *S, StringRef Name) {
399
95
  bool A = Name.contains("@@");
400
95
  bool B = S->getName().contains("@@");
401
95
  if (A && 
!B1
)
402
1
    return 1;
403
94
  if (!A && B)
404
1
    return -1;
405
93
  return 0;
406
93
}
407
408
// We have a new defined symbol with the specified binding. Return 1 if the new
409
// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
410
// strong defined symbols.
411
static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding,
412
15.9k
                          StringRef Name) {
413
15.9k
  if (WasInserted)
414
15.1k
    return 1;
415
820
  if (!S->isDefined())
416
725
    return 1;
417
95
  if (int R = compareVersion(S, Name))
418
2
    return R;
419
93
  if (Binding == STB_WEAK)
420
18
    return -1;
421
75
  if (S->isWeak())
422
21
    return 1;
423
54
  return 0;
424
54
}
425
426
// We have a new non-common defined symbol with the specified binding. Return 1
427
// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
428
// is a conflict. If the new symbol wins, also update the binding.
429
static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding,
430
                                   bool IsAbsolute, uint64_t Value,
431
15.8k
                                   StringRef Name) {
432
15.8k
  if (int Cmp = compareDefined(S, WasInserted, Binding, Name))
433
15.8k
    return Cmp;
434
38
  if (auto *R = dyn_cast<Defined>(S)) {
435
38
    if (R->Section && 
isa<BssSection>(R->Section)32
) {
436
2
      // Non-common symbols take precedence over common symbols.
437
2
      if (Config->WarnCommon)
438
1
        warn("common " + S->getName() + " is overridden");
439
2
      return 1;
440
2
    }
441
36
    if (R->Section == nullptr && 
Binding == STB_GLOBAL6
&&
IsAbsolute6
&&
442
36
        
R->Value == Value5
)
443
4
      return -1;
444
32
  }
445
32
  return 0;
446
32
}
447
448
Symbol *SymbolTable::addCommon(StringRef N, uint64_t Size, uint32_t Alignment,
449
                               uint8_t Binding, uint8_t StOther, uint8_t Type,
450
120
                               InputFile &File) {
451
120
  Symbol *S;
452
120
  bool WasInserted;
453
120
  std::tie(S, WasInserted) = insert(N, Type, getVisibility(StOther),
454
120
                                    /*CanOmitFromDynSym*/ false, &File);
455
120
456
120
  int Cmp = compareDefined(S, WasInserted, Binding, N);
457
120
  if (Cmp < 0)
458
4
    return S;
459
116
460
116
  if (Cmp > 0) {
461
100
    auto *Bss = make<BssSection>("COMMON", Size, Alignment);
462
100
    Bss->File = &File;
463
100
    Bss->Live = !Config->GcSections;
464
100
    InputSections.push_back(Bss);
465
100
466
100
    replaceSymbol<Defined>(S, &File, N, Binding, StOther, Type, 0, Size, Bss);
467
100
    return S;
468
100
  }
469
16
470
16
  auto *D = cast<Defined>(S);
471
16
  auto *Bss = dyn_cast_or_null<BssSection>(D->Section);
472
16
  if (!Bss) {
473
2
    // Non-common symbols take precedence over common symbols.
474
2
    if (Config->WarnCommon)
475
1
      warn("common " + S->getName() + " is overridden");
476
2
    return S;
477
2
  }
478
14
479
14
  if (Config->WarnCommon)
480
3
    warn("multiple common of " + D->getName());
481
14
482
14
  Bss->Alignment = std::max(Bss->Alignment, Alignment);
483
14
  if (Size > Bss->Size) {
484
12
    D->File = Bss->File = &File;
485
12
    D->Size = Bss->Size = Size;
486
12
  }
487
14
  return S;
488
14
}
489
490
2
static void reportDuplicate(Symbol *Sym, InputFile *NewFile) {
491
2
  if (!Config->AllowMultipleDefinition)
492
2
    error("duplicate symbol: " + toString(*Sym) + "\n>>> defined in " +
493
2
          toString(Sym->File) + "\n>>> defined in " + toString(NewFile));
494
2
}
495
496
static void reportDuplicate(Symbol *Sym, InputFile *NewFile,
497
31
                            InputSectionBase *ErrSec, uint64_t ErrOffset) {
498
31
  if (Config->AllowMultipleDefinition)
499
4
    return;
500
27
501
27
  Defined *D = cast<Defined>(Sym);
502
27
  if (!D->Section || 
!ErrSec26
) {
503
1
    reportDuplicate(Sym, NewFile);
504
1
    return;
505
1
  }
506
26
507
26
  // Construct and print an error message in the form of:
508
26
  //
509
26
  //   ld.lld: error: duplicate symbol: foo
510
26
  //   >>> defined at bar.c:30
511
26
  //   >>>            bar.o (/home/alice/src/bar.o)
512
26
  //   >>> defined at baz.c:563
513
26
  //   >>>            baz.o in archive libbaz.a
514
26
  auto *Sec1 = cast<InputSectionBase>(D->Section);
515
26
  std::string Src1 = Sec1->getSrcMsg(*Sym, D->Value);
516
26
  std::string Obj1 = Sec1->getObjMsg(D->Value);
517
26
  std::string Src2 = ErrSec->getSrcMsg(*Sym, ErrOffset);
518
26
  std::string Obj2 = ErrSec->getObjMsg(ErrOffset);
519
26
520
26
  std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at ";
521
26
  if (!Src1.empty())
522
10
    Msg += Src1 + "\n>>>            ";
523
26
  Msg += Obj1 + "\n>>> defined at ";
524
26
  if (!Src2.empty())
525
10
    Msg += Src2 + "\n>>>            ";
526
26
  Msg += Obj2;
527
26
  error(Msg);
528
26
}
529
530
Symbol *SymbolTable::addRegular(StringRef Name, uint8_t StOther, uint8_t Type,
531
                                uint64_t Value, uint64_t Size, uint8_t Binding,
532
15.5k
                                SectionBase *Section, InputFile *File) {
533
15.5k
  Symbol *S;
534
15.5k
  bool WasInserted;
535
15.5k
  std::tie(S, WasInserted) = insert(Name, Type, getVisibility(StOther),
536
15.5k
                                    /*CanOmitFromDynSym*/ false, File);
537
15.5k
  int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, Section == nullptr,
538
15.5k
                                    Value, Name);
539
15.5k
  if (Cmp > 0)
540
15.5k
    replaceSymbol<Defined>(S, File, Name, Binding, StOther, Type, Value, Size,
541
15.5k
                           Section);
542
43
  else if (Cmp == 0)
543
31
    reportDuplicate(S, File, dyn_cast_or_null<InputSectionBase>(Section),
544
31
                    Value);
545
15.5k
  return S;
546
15.5k
}
547
548
template <typename ELFT>
549
void SymbolTable::addShared(StringRef Name, SharedFile<ELFT> &File,
550
                            const typename ELFT::Sym &Sym, uint32_t Alignment,
551
853
                            uint32_t VerdefIndex) {
552
853
  // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
553
853
  // as the visibility, which will leave the visibility in the symbol table
554
853
  // unchanged.
555
853
  Symbol *S;
556
853
  bool WasInserted;
557
853
  std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT,
558
853
                                    /*CanOmitFromDynSym*/ true, &File);
559
853
  // Make sure we preempt DSO symbols with default visibility.
560
853
  if (Sym.getVisibility() == STV_DEFAULT)
561
841
    S->ExportDynamic = true;
562
853
563
853
  // An undefined symbol with non default visibility must be satisfied
564
853
  // in the same DSO.
565
853
  if (WasInserted ||
566
853
      
(394
(394
S->isUndefined()394
||
S->isLazy()38
) &&
S->Visibility == STV_DEFAULT358
)) {
567
813
    uint8_t Binding = S->Binding;
568
813
    bool WasUndefined = S->isUndefined();
569
813
    replaceSymbol<SharedSymbol>(S, File, Name, Sym.getBinding(), Sym.st_other,
570
813
                                Sym.getType(), Sym.st_value, Sym.st_size,
571
813
                                Alignment, VerdefIndex);
572
813
    if (!WasInserted) {
573
354
      S->Binding = Binding;
574
354
      if (!S->isWeak() && 
!Config->GcSections339
&&
WasUndefined332
)
575
330
        File.IsNeeded = true;
576
354
    }
577
813
  }
578
853
}
void lld::elf::SymbolTable::addShared<llvm::object::ELFType<(llvm::support::endianness)1, false> >(llvm::StringRef, lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >&, llvm::object::ELFType<(llvm::support::endianness)1, false>::Sym const&, unsigned int, unsigned int)
Line
Count
Source
551
119
                            uint32_t VerdefIndex) {
552
119
  // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
553
119
  // as the visibility, which will leave the visibility in the symbol table
554
119
  // unchanged.
555
119
  Symbol *S;
556
119
  bool WasInserted;
557
119
  std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT,
558
119
                                    /*CanOmitFromDynSym*/ true, &File);
559
119
  // Make sure we preempt DSO symbols with default visibility.
560
119
  if (Sym.getVisibility() == STV_DEFAULT)
561
119
    S->ExportDynamic = true;
562
119
563
119
  // An undefined symbol with non default visibility must be satisfied
564
119
  // in the same DSO.
565
119
  if (WasInserted ||
566
119
      
(67
(67
S->isUndefined()67
||
S->isLazy()6
) &&
S->Visibility == STV_DEFAULT61
)) {
567
113
    uint8_t Binding = S->Binding;
568
113
    bool WasUndefined = S->isUndefined();
569
113
    replaceSymbol<SharedSymbol>(S, File, Name, Sym.getBinding(), Sym.st_other,
570
113
                                Sym.getType(), Sym.st_value, Sym.st_size,
571
113
                                Alignment, VerdefIndex);
572
113
    if (!WasInserted) {
573
61
      S->Binding = Binding;
574
61
      if (!S->isWeak() && 
!Config->GcSections56
&&
WasUndefined56
)
575
56
        File.IsNeeded = true;
576
61
    }
577
113
  }
578
119
}
void lld::elf::SymbolTable::addShared<llvm::object::ELFType<(llvm::support::endianness)0, false> >(llvm::StringRef, lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >&, llvm::object::ELFType<(llvm::support::endianness)0, false>::Sym const&, unsigned int, unsigned int)
Line
Count
Source
551
100
                            uint32_t VerdefIndex) {
552
100
  // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
553
100
  // as the visibility, which will leave the visibility in the symbol table
554
100
  // unchanged.
555
100
  Symbol *S;
556
100
  bool WasInserted;
557
100
  std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT,
558
100
                                    /*CanOmitFromDynSym*/ true, &File);
559
100
  // Make sure we preempt DSO symbols with default visibility.
560
100
  if (Sym.getVisibility() == STV_DEFAULT)
561
100
    S->ExportDynamic = true;
562
100
563
100
  // An undefined symbol with non default visibility must be satisfied
564
100
  // in the same DSO.
565
100
  if (WasInserted ||
566
100
      
(28
(28
S->isUndefined()28
||
S->isLazy()0
) &&
S->Visibility == STV_DEFAULT28
)) {
567
100
    uint8_t Binding = S->Binding;
568
100
    bool WasUndefined = S->isUndefined();
569
100
    replaceSymbol<SharedSymbol>(S, File, Name, Sym.getBinding(), Sym.st_other,
570
100
                                Sym.getType(), Sym.st_value, Sym.st_size,
571
100
                                Alignment, VerdefIndex);
572
100
    if (!WasInserted) {
573
28
      S->Binding = Binding;
574
28
      if (!S->isWeak() && !Config->GcSections && WasUndefined)
575
28
        File.IsNeeded = true;
576
28
    }
577
100
  }
578
100
}
void lld::elf::SymbolTable::addShared<llvm::object::ELFType<(llvm::support::endianness)1, true> >(llvm::StringRef, lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >&, llvm::object::ELFType<(llvm::support::endianness)1, true>::Sym const&, unsigned int, unsigned int)
Line
Count
Source
551
525
                            uint32_t VerdefIndex) {
552
525
  // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
553
525
  // as the visibility, which will leave the visibility in the symbol table
554
525
  // unchanged.
555
525
  Symbol *S;
556
525
  bool WasInserted;
557
525
  std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT,
558
525
                                    /*CanOmitFromDynSym*/ true, &File);
559
525
  // Make sure we preempt DSO symbols with default visibility.
560
525
  if (Sym.getVisibility() == STV_DEFAULT)
561
513
    S->ExportDynamic = true;
562
525
563
525
  // An undefined symbol with non default visibility must be satisfied
564
525
  // in the same DSO.
565
525
  if (WasInserted ||
566
525
      
(278
(278
S->isUndefined()278
||
S->isLazy()32
) &&
S->Visibility == STV_DEFAULT248
)) {
567
491
    uint8_t Binding = S->Binding;
568
491
    bool WasUndefined = S->isUndefined();
569
491
    replaceSymbol<SharedSymbol>(S, File, Name, Sym.getBinding(), Sym.st_other,
570
491
                                Sym.getType(), Sym.st_value, Sym.st_size,
571
491
                                Alignment, VerdefIndex);
572
491
    if (!WasInserted) {
573
244
      S->Binding = Binding;
574
244
      if (!S->isWeak() && 
!Config->GcSections234
&&
WasUndefined227
)
575
225
        File.IsNeeded = true;
576
244
    }
577
491
  }
578
525
}
void lld::elf::SymbolTable::addShared<llvm::object::ELFType<(llvm::support::endianness)0, true> >(llvm::StringRef, lld::elf::SharedFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >&, llvm::object::ELFType<(llvm::support::endianness)0, true>::Sym const&, unsigned int, unsigned int)
Line
Count
Source
551
109
                            uint32_t VerdefIndex) {
552
109
  // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
553
109
  // as the visibility, which will leave the visibility in the symbol table
554
109
  // unchanged.
555
109
  Symbol *S;
556
109
  bool WasInserted;
557
109
  std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT,
558
109
                                    /*CanOmitFromDynSym*/ true, &File);
559
109
  // Make sure we preempt DSO symbols with default visibility.
560
109
  if (Sym.getVisibility() == STV_DEFAULT)
561
109
    S->ExportDynamic = true;
562
109
563
109
  // An undefined symbol with non default visibility must be satisfied
564
109
  // in the same DSO.
565
109
  if (WasInserted ||
566
109
      
(21
(21
S->isUndefined()21
||
S->isLazy()0
) &&
S->Visibility == STV_DEFAULT21
)) {
567
109
    uint8_t Binding = S->Binding;
568
109
    bool WasUndefined = S->isUndefined();
569
109
    replaceSymbol<SharedSymbol>(S, File, Name, Sym.getBinding(), Sym.st_other,
570
109
                                Sym.getType(), Sym.st_value, Sym.st_size,
571
109
                                Alignment, VerdefIndex);
572
109
    if (!WasInserted) {
573
21
      S->Binding = Binding;
574
21
      if (!S->isWeak() && !Config->GcSections && WasUndefined)
575
21
        File.IsNeeded = true;
576
21
    }
577
109
  }
578
109
}
579
580
Symbol *SymbolTable::addBitcode(StringRef Name, uint8_t Binding,
581
                                uint8_t StOther, uint8_t Type,
582
272
                                bool CanOmitFromDynSym, BitcodeFile &F) {
583
272
  Symbol *S;
584
272
  bool WasInserted;
585
272
  std::tie(S, WasInserted) =
586
272
      insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, &F);
587
272
  int Cmp = compareDefinedNonCommon(S, WasInserted, Binding,
588
272
                                    /*IsAbs*/ false, /*Value*/ 0, Name);
589
272
  if (Cmp > 0)
590
264
    replaceSymbol<Defined>(S, &F, Name, Binding, StOther, Type, 0, 0, nullptr);
591
8
  else if (Cmp == 0)
592
1
    reportDuplicate(S, &F);
593
272
  return S;
594
272
}
595
596
645k
Symbol *SymbolTable::find(StringRef Name) {
597
645k
  auto It = SymMap.find(CachedHashStringRef(Name));
598
645k
  if (It == SymMap.end())
599
642k
    return nullptr;
600
2.91k
  if (It->second == -1)
601
1
    return nullptr;
602
2.91k
  return SymVector[It->second];
603
2.91k
}
604
605
// This is used to handle lazy symbols. May replace existent
606
// symbol with lazy version or request to Fetch it.
607
template <class ELFT, typename LazyT, typename... ArgT>
608
static void replaceOrFetchLazy(StringRef Name, InputFile &File,
609
                               llvm::function_ref<InputFile *()> Fetch,
610
217
                               ArgT &&... Arg) {
611
217
  Symbol *S;
612
217
  bool WasInserted;
613
217
  std::tie(S, WasInserted) = Symtab->insert(Name);
614
217
  if (WasInserted) {
615
110
    replaceSymbol<LazyT>(S, File, Symbol::UnknownType,
616
110
                         std::forward<ArgT>(Arg)...);
617
110
    return;
618
110
  }
619
107
  if (!S->isUndefined())
620
34
    return;
621
73
622
73
  // An undefined weak will not fetch archive members. See comment on Lazy in
623
73
  // Symbols.h for the details.
624
73
  if (S->isWeak()) {
625
15
    replaceSymbol<LazyT>(S, File, S->Type, std::forward<ArgT>(Arg)...);
626
15
    S->Binding = STB_WEAK;
627
15
    return;
628
15
  }
629
58
630
58
  if (InputFile *F = Fetch())
631
57
    Symtab->addFile<ELFT>(F);
632
58
}
SymbolTable.cpp:void replaceOrFetchLazy<llvm::object::ELFType<(llvm::support::endianness)1, false>, lld::elf::LazyArchive, llvm::object::Archive::Symbol const&>(llvm::StringRef, lld::elf::InputFile&, llvm::function_ref<lld::elf::InputFile* ()>, llvm::object::Archive::Symbol const&&&)
Line
Count
Source
610
4
                               ArgT &&... Arg) {
611
4
  Symbol *S;
612
4
  bool WasInserted;
613
4
  std::tie(S, WasInserted) = Symtab->insert(Name);
614
4
  if (WasInserted) {
615
3
    replaceSymbol<LazyT>(S, File, Symbol::UnknownType,
616
3
                         std::forward<ArgT>(Arg)...);
617
3
    return;
618
3
  }
619
1
  if (!S->isUndefined())
620
0
    return;
621
1
622
1
  // An undefined weak will not fetch archive members. See comment on Lazy in
623
1
  // Symbols.h for the details.
624
1
  if (S->isWeak()) {
625
1
    replaceSymbol<LazyT>(S, File, S->Type, std::forward<ArgT>(Arg)...);
626
1
    S->Binding = STB_WEAK;
627
1
    return;
628
1
  }
629
0
630
0
  if (InputFile *F = Fetch())
631
0
    Symtab->addFile<ELFT>(F);
632
0
}
Unexecuted instantiation: SymbolTable.cpp:void replaceOrFetchLazy<llvm::object::ELFType<(llvm::support::endianness)0, false>, lld::elf::LazyArchive, llvm::object::Archive::Symbol const&>(llvm::StringRef, lld::elf::InputFile&, llvm::function_ref<lld::elf::InputFile* ()>, llvm::object::Archive::Symbol const&&&)
SymbolTable.cpp:void replaceOrFetchLazy<llvm::object::ELFType<(llvm::support::endianness)1, true>, lld::elf::LazyArchive, llvm::object::Archive::Symbol const&>(llvm::StringRef, lld::elf::InputFile&, llvm::function_ref<lld::elf::InputFile* ()>, llvm::object::Archive::Symbol const&&&)
Line
Count
Source
610
177
                               ArgT &&... Arg) {
611
177
  Symbol *S;
612
177
  bool WasInserted;
613
177
  std::tie(S, WasInserted) = Symtab->insert(Name);
614
177
  if (WasInserted) {
615
87
    replaceSymbol<LazyT>(S, File, Symbol::UnknownType,
616
87
                         std::forward<ArgT>(Arg)...);
617
87
    return;
618
87
  }
619
90
  if (!S->isUndefined())
620
31
    return;
621
59
622
59
  // An undefined weak will not fetch archive members. See comment on Lazy in
623
59
  // Symbols.h for the details.
624
59
  if (S->isWeak()) {
625
12
    replaceSymbol<LazyT>(S, File, S->Type, std::forward<ArgT>(Arg)...);
626
12
    S->Binding = STB_WEAK;
627
12
    return;
628
12
  }
629
47
630
47
  if (InputFile *F = Fetch())
631
47
    Symtab->addFile<ELFT>(F);
632
47
}
SymbolTable.cpp:void replaceOrFetchLazy<llvm::object::ELFType<(llvm::support::endianness)0, true>, lld::elf::LazyArchive, llvm::object::Archive::Symbol const&>(llvm::StringRef, lld::elf::InputFile&, llvm::function_ref<lld::elf::InputFile* ()>, llvm::object::Archive::Symbol const&&&)
Line
Count
Source
610
1
                               ArgT &&... Arg) {
611
1
  Symbol *S;
612
1
  bool WasInserted;
613
1
  std::tie(S, WasInserted) = Symtab->insert(Name);
614
1
  if (WasInserted) {
615
0
    replaceSymbol<LazyT>(S, File, Symbol::UnknownType,
616
0
                         std::forward<ArgT>(Arg)...);
617
0
    return;
618
0
  }
619
1
  if (!S->isUndefined())
620
0
    return;
621
1
622
1
  // An undefined weak will not fetch archive members. See comment on Lazy in
623
1
  // Symbols.h for the details.
624
1
  if (S->isWeak()) {
625
1
    replaceSymbol<LazyT>(S, File, S->Type, std::forward<ArgT>(Arg)...);
626
1
    S->Binding = STB_WEAK;
627
1
    return;
628
1
  }
629
0
630
0
  if (InputFile *F = Fetch())
631
0
    Symtab->addFile<ELFT>(F);
632
0
}
Unexecuted instantiation: SymbolTable.cpp:void replaceOrFetchLazy<llvm::object::ELFType<(llvm::support::endianness)1, false>, lld::elf::LazyObject, llvm::StringRef&>(llvm::StringRef, lld::elf::InputFile&, llvm::function_ref<lld::elf::InputFile* ()>, llvm::StringRef&&&)
Unexecuted instantiation: SymbolTable.cpp:void replaceOrFetchLazy<llvm::object::ELFType<(llvm::support::endianness)0, false>, lld::elf::LazyObject, llvm::StringRef&>(llvm::StringRef, lld::elf::InputFile&, llvm::function_ref<lld::elf::InputFile* ()>, llvm::StringRef&&&)
SymbolTable.cpp:void replaceOrFetchLazy<llvm::object::ELFType<(llvm::support::endianness)1, true>, lld::elf::LazyObject, llvm::StringRef&>(llvm::StringRef, lld::elf::InputFile&, llvm::function_ref<lld::elf::InputFile* ()>, llvm::StringRef&&&)
Line
Count
Source
610
35
                               ArgT &&... Arg) {
611
35
  Symbol *S;
612
35
  bool WasInserted;
613
35
  std::tie(S, WasInserted) = Symtab->insert(Name);
614
35
  if (WasInserted) {
615
20
    replaceSymbol<LazyT>(S, File, Symbol::UnknownType,
616
20
                         std::forward<ArgT>(Arg)...);
617
20
    return;
618
20
  }
619
15
  if (!S->isUndefined())
620
3
    return;
621
12
622
12
  // An undefined weak will not fetch archive members. See comment on Lazy in
623
12
  // Symbols.h for the details.
624
12
  if (S->isWeak()) {
625
1
    replaceSymbol<LazyT>(S, File, S->Type, std::forward<ArgT>(Arg)...);
626
1
    S->Binding = STB_WEAK;
627
1
    return;
628
1
  }
629
11
630
11
  if (InputFile *F = Fetch())
631
10
    Symtab->addFile<ELFT>(F);
632
11
}
Unexecuted instantiation: SymbolTable.cpp:void replaceOrFetchLazy<llvm::object::ELFType<(llvm::support::endianness)0, true>, lld::elf::LazyObject, llvm::StringRef&>(llvm::StringRef, lld::elf::InputFile&, llvm::function_ref<lld::elf::InputFile* ()>, llvm::StringRef&&&)
633
634
template <class ELFT>
635
void SymbolTable::addLazyArchive(StringRef Name, ArchiveFile &F,
636
182
                                 const object::Archive::Symbol Sym) {
637
182
  replaceOrFetchLazy<ELFT, LazyArchive>(Name, F, [&]() 
{ return F.fetch(Sym); }47
,
Unexecuted instantiation: void lld::elf::SymbolTable::addLazyArchive<llvm::object::ELFType<(llvm::support::endianness)1, false> >(llvm::StringRef, lld::elf::ArchiveFile&, llvm::object::Archive::Symbol)::'lambda'()::operator()() const
Unexecuted instantiation: void lld::elf::SymbolTable::addLazyArchive<llvm::object::ELFType<(llvm::support::endianness)0, false> >(llvm::StringRef, lld::elf::ArchiveFile&, llvm::object::Archive::Symbol)::'lambda'()::operator()() const
void lld::elf::SymbolTable::addLazyArchive<llvm::object::ELFType<(llvm::support::endianness)1, true> >(llvm::StringRef, lld::elf::ArchiveFile&, llvm::object::Archive::Symbol)::'lambda'()::operator()() const
Line
Count
Source
637
47
  replaceOrFetchLazy<ELFT, LazyArchive>(Name, F, [&]() { return F.fetch(Sym); },
Unexecuted instantiation: void lld::elf::SymbolTable::addLazyArchive<llvm::object::ELFType<(llvm::support::endianness)0, true> >(llvm::StringRef, lld::elf::ArchiveFile&, llvm::object::Archive::Symbol)::'lambda'()::operator()() const
638
182
                                        Sym);
639
182
}
void lld::elf::SymbolTable::addLazyArchive<llvm::object::ELFType<(llvm::support::endianness)1, false> >(llvm::StringRef, lld::elf::ArchiveFile&, llvm::object::Archive::Symbol)
Line
Count
Source
636
4
                                 const object::Archive::Symbol Sym) {
637
4
  replaceOrFetchLazy<ELFT, LazyArchive>(Name, F, [&]() { return F.fetch(Sym); },
638
4
                                        Sym);
639
4
}
Unexecuted instantiation: void lld::elf::SymbolTable::addLazyArchive<llvm::object::ELFType<(llvm::support::endianness)0, false> >(llvm::StringRef, lld::elf::ArchiveFile&, llvm::object::Archive::Symbol)
void lld::elf::SymbolTable::addLazyArchive<llvm::object::ELFType<(llvm::support::endianness)1, true> >(llvm::StringRef, lld::elf::ArchiveFile&, llvm::object::Archive::Symbol)
Line
Count
Source
636
177
                                 const object::Archive::Symbol Sym) {
637
177
  replaceOrFetchLazy<ELFT, LazyArchive>(Name, F, [&]() { return F.fetch(Sym); },
638
177
                                        Sym);
639
177
}
void lld::elf::SymbolTable::addLazyArchive<llvm::object::ELFType<(llvm::support::endianness)0, true> >(llvm::StringRef, lld::elf::ArchiveFile&, llvm::object::Archive::Symbol)
Line
Count
Source
636
1
                                 const object::Archive::Symbol Sym) {
637
1
  replaceOrFetchLazy<ELFT, LazyArchive>(Name, F, [&]() { return F.fetch(Sym); },
638
1
                                        Sym);
639
1
}
640
641
template <class ELFT>
642
35
void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &Obj) {
643
35
  replaceOrFetchLazy<ELFT, LazyObject>(Name, Obj, [&]() 
{ return Obj.fetch(); }11
,
Unexecuted instantiation: void lld::elf::SymbolTable::addLazyObject<llvm::object::ELFType<(llvm::support::endianness)1, false> >(llvm::StringRef, lld::elf::LazyObjFile&)::'lambda'()::operator()() const
Unexecuted instantiation: void lld::elf::SymbolTable::addLazyObject<llvm::object::ELFType<(llvm::support::endianness)0, false> >(llvm::StringRef, lld::elf::LazyObjFile&)::'lambda'()::operator()() const
void lld::elf::SymbolTable::addLazyObject<llvm::object::ELFType<(llvm::support::endianness)1, true> >(llvm::StringRef, lld::elf::LazyObjFile&)::'lambda'()::operator()() const
Line
Count
Source
643
11
  replaceOrFetchLazy<ELFT, LazyObject>(Name, Obj, [&]() { return Obj.fetch(); },
Unexecuted instantiation: void lld::elf::SymbolTable::addLazyObject<llvm::object::ELFType<(llvm::support::endianness)0, true> >(llvm::StringRef, lld::elf::LazyObjFile&)::'lambda'()::operator()() const
644
35
                                       Name);
645
35
}
Unexecuted instantiation: void lld::elf::SymbolTable::addLazyObject<llvm::object::ELFType<(llvm::support::endianness)1, false> >(llvm::StringRef, lld::elf::LazyObjFile&)
Unexecuted instantiation: void lld::elf::SymbolTable::addLazyObject<llvm::object::ELFType<(llvm::support::endianness)0, false> >(llvm::StringRef, lld::elf::LazyObjFile&)
void lld::elf::SymbolTable::addLazyObject<llvm::object::ELFType<(llvm::support::endianness)1, true> >(llvm::StringRef, lld::elf::LazyObjFile&)
Line
Count
Source
642
35
void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &Obj) {
643
35
  replaceOrFetchLazy<ELFT, LazyObject>(Name, Obj, [&]() { return Obj.fetch(); },
644
35
                                       Name);
645
35
}
Unexecuted instantiation: void lld::elf::SymbolTable::addLazyObject<llvm::object::ELFType<(llvm::support::endianness)0, true> >(llvm::StringRef, lld::elf::LazyObjFile&)
646
647
41
template <class ELFT> void SymbolTable::fetchLazy(Symbol *Sym) {
648
41
  if (auto *S = dyn_cast<LazyArchive>(Sym)) {
649
32
    if (InputFile *File = S->fetch())
650
31
      addFile<ELFT>(File);
651
32
    return;
652
32
  }
653
9
654
9
  auto *S = cast<LazyObject>(Sym);
655
9
  if (InputFile *File = cast<LazyObjFile>(S->File)->fetch())
656
8
    addFile<ELFT>(File);
657
9
}
void lld::elf::SymbolTable::fetchLazy<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::Symbol*)
Line
Count
Source
647
2
template <class ELFT> void SymbolTable::fetchLazy(Symbol *Sym) {
648
2
  if (auto *S = dyn_cast<LazyArchive>(Sym)) {
649
2
    if (InputFile *File = S->fetch())
650
2
      addFile<ELFT>(File);
651
2
    return;
652
2
  }
653
0
654
0
  auto *S = cast<LazyObject>(Sym);
655
0
  if (InputFile *File = cast<LazyObjFile>(S->File)->fetch())
656
0
    addFile<ELFT>(File);
657
0
}
Unexecuted instantiation: void lld::elf::SymbolTable::fetchLazy<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::Symbol*)
void lld::elf::SymbolTable::fetchLazy<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::Symbol*)
Line
Count
Source
647
39
template <class ELFT> void SymbolTable::fetchLazy(Symbol *Sym) {
648
39
  if (auto *S = dyn_cast<LazyArchive>(Sym)) {
649
30
    if (InputFile *File = S->fetch())
650
29
      addFile<ELFT>(File);
651
30
    return;
652
30
  }
653
9
654
9
  auto *S = cast<LazyObject>(Sym);
655
9
  if (InputFile *File = cast<LazyObjFile>(S->File)->fetch())
656
8
    addFile<ELFT>(File);
657
9
}
Unexecuted instantiation: void lld::elf::SymbolTable::fetchLazy<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::Symbol*)
658
659
// Initialize DemangledSyms with a map from demangled symbols to symbol
660
// objects. Used to handle "extern C++" directive in version scripts.
661
//
662
// The map will contain all demangled symbols. That can be very large,
663
// and in LLD we generally want to avoid do anything for each symbol.
664
// Then, why are we doing this? Here's why.
665
//
666
// Users can use "extern C++ {}" directive to match against demangled
667
// C++ symbols. For example, you can write a pattern such as
668
// "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
669
// other than trying to match a pattern against all demangled symbols.
670
// So, if "extern C++" feature is used, we need to demangle all known
671
// symbols.
672
29
StringMap<std::vector<Symbol *>> &SymbolTable::getDemangledSyms() {
673
29
  if (!DemangledSyms) {
674
22
    DemangledSyms.emplace();
675
45
    for (Symbol *Sym : SymVector) {
676
45
      if (!Sym->isDefined())
677
1
        continue;
678
44
      if (Optional<std::string> S = demangleItanium(Sym->getName()))
679
43
        (*DemangledSyms)[*S].push_back(Sym);
680
1
      else
681
1
        (*DemangledSyms)[Sym->getName()].push_back(Sym);
682
44
    }
683
22
  }
684
29
  return *DemangledSyms;
685
29
}
686
687
99
std::vector<Symbol *> SymbolTable::findByVersion(SymbolVersion Ver) {
688
99
  if (Ver.IsExternCpp)
689
12
    return getDemangledSyms().lookup(Ver.Name);
690
87
  if (Symbol *B = find(Ver.Name))
691
77
    if (B->isDefined())
692
74
      return {B};
693
13
  return {};
694
13
}
695
696
27
std::vector<Symbol *> SymbolTable::findAllByVersion(SymbolVersion Ver) {
697
27
  std::vector<Symbol *> Res;
698
27
  StringMatcher M(Ver.Name);
699
27
700
27
  if (Ver.IsExternCpp) {
701
17
    for (auto &P : getDemangledSyms())
702
42
      if (M.match(P.first()))
703
19
        Res.insert(Res.end(), P.second.begin(), P.second.end());
704
17
    return Res;
705
17
  }
706
10
707
10
  for (Symbol *Sym : SymVector)
708
35
    if (Sym->isDefined() && M.match(Sym->getName()))
709
23
      Res.push_back(Sym);
710
10
  return Res;
711
10
}
712
713
// If there's only one anonymous version definition in a version
714
// script file, the script does not actually define any symbol version,
715
// but just specifies symbols visibilities.
716
2.18k
void SymbolTable::handleAnonymousVersion() {
717
2.18k
  for (SymbolVersion &Ver : Config->VersionScriptGlobals)
718
24
    assignExactVersion(Ver, VER_NDX_GLOBAL, "global");
719
2.18k
  for (SymbolVersion &Ver : Config->VersionScriptGlobals)
720
24
    assignWildcardVersion(Ver, VER_NDX_GLOBAL);
721
2.18k
  for (SymbolVersion &Ver : Config->VersionScriptLocals)
722
8
    assignExactVersion(Ver, VER_NDX_LOCAL, "local");
723
2.18k
  for (SymbolVersion &Ver : Config->VersionScriptLocals)
724
8
    assignWildcardVersion(Ver, VER_NDX_LOCAL);
725
2.18k
}
726
727
// Handles -dynamic-list.
728
2.18k
void SymbolTable::handleDynamicList() {
729
2.18k
  for (SymbolVersion &Ver : Config->DynamicList) {
730
26
    std::vector<Symbol *> Syms;
731
26
    if (Ver.HasWildcard)
732
1
      Syms = findAllByVersion(Ver);
733
25
    else
734
25
      Syms = findByVersion(Ver);
735
26
736
26
    for (Symbol *B : Syms) {
737
21
      if (!Config->Shared)
738
18
        B->ExportDynamic = true;
739
3
      else if (B->includeInDynsym())
740
2
        B->IsPreemptible = true;
741
21
    }
742
26
  }
743
2.18k
}
744
745
// Set symbol versions to symbols. This function handles patterns
746
// containing no wildcard characters.
747
void SymbolTable::assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
748
100
                                     StringRef VersionName) {
749
100
  if (Ver.HasWildcard)
750
26
    return;
751
74
752
74
  // Get a list of symbols which we need to assign the version to.
753
74
  std::vector<Symbol *> Syms = findByVersion(Ver);
754
74
  if (Syms.empty()) {
755
13
    if (!Config->UndefinedVersion)
756
3
      error("version script assignment of '" + VersionName + "' to symbol '" +
757
3
            Ver.Name + "' failed: symbol not defined");
758
13
    return;
759
13
  }
760
61
761
61
  // Assign the version.
762
62
  
for (Symbol *Sym : Syms)61
{
763
62
    // Skip symbols containing version info because symbol versions
764
62
    // specified by symbol names take precedence over version scripts.
765
62
    // See parseSymbolVersion().
766
62
    if (Sym->getName().contains('@'))
767
4
      continue;
768
58
769
58
    if (Sym->VersionId != Config->DefaultSymbolVersion &&
770
58
        
Sym->VersionId != VersionId2
)
771
1
      error("duplicate symbol '" + Ver.Name + "' in version script");
772
58
    Sym->VersionId = VersionId;
773
58
  }
774
61
}
775
776
100
void SymbolTable::assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId) {
777
100
  if (!Ver.HasWildcard)
778
74
    return;
779
26
780
26
  // Exact matching takes precendence over fuzzy matching,
781
26
  // so we set a version to a symbol only if no version has been assigned
782
26
  // to the symbol. This behavior is compatible with GNU.
783
26
  for (Symbol *B : findAllByVersion(Ver))
784
40
    if (B->VersionId == Config->DefaultSymbolVersion)
785
36
      B->VersionId = VersionId;
786
26
}
787
788
// This function processes version scripts by updating VersionId
789
// member of symbols.
790
2.18k
void SymbolTable::scanVersionScript() {
791
2.18k
  // Handle edge cases first.
792
2.18k
  handleAnonymousVersion();
793
2.18k
  handleDynamicList();
794
2.18k
795
2.18k
  // Now we have version definitions, so we need to set version ids to symbols.
796
2.18k
  // Each version definition has a glob pattern, and all symbols that match
797
2.18k
  // with the pattern get that version.
798
2.18k
799
2.18k
  // First, we assign versions to exact matching symbols,
800
2.18k
  // i.e. version definitions not containing any glob meta-characters.
801
2.18k
  for (VersionDefinition &V : Config->VersionDefinitions)
802
87
    for (SymbolVersion &Ver : V.Globals)
803
68
      assignExactVersion(Ver, V.Id, V.Name);
804
2.18k
805
2.18k
  // Next, we assign versions to fuzzy matching symbols,
806
2.18k
  // i.e. version definitions containing glob meta-characters.
807
2.18k
  // Note that because the last match takes precedence over previous matches,
808
2.18k
  // we iterate over the definitions in the reverse order.
809
2.18k
  for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
810
87
    for (SymbolVersion &Ver : V.Globals)
811
68
      assignWildcardVersion(Ver, V.Id);
812
2.18k
813
2.18k
  // Symbol themselves might know their versions because symbols
814
2.18k
  // can contain versions in the form of <name>@<version>.
815
2.18k
  // Let them parse and update their names to exclude version suffix.
816
2.18k
  for (Symbol *Sym : SymVector)
817
15.8k
    Sym->parseSymbolVersion();
818
2.18k
}
819
820
template void SymbolTable::addFile<ELF32LE>(InputFile *);
821
template void SymbolTable::addFile<ELF32BE>(InputFile *);
822
template void SymbolTable::addFile<ELF64LE>(InputFile *);
823
template void SymbolTable::addFile<ELF64BE>(InputFile *);
824
825
template void SymbolTable::addSymbolWrap<ELF32LE>(StringRef);
826
template void SymbolTable::addSymbolWrap<ELF32BE>(StringRef);
827
template void SymbolTable::addSymbolWrap<ELF64LE>(StringRef);
828
template void SymbolTable::addSymbolWrap<ELF64BE>(StringRef);
829
830
template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef);
831
template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef);
832
template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef);
833
template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef);
834
835
template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef, uint8_t, uint8_t,
836
                                                    uint8_t, bool, InputFile *);
837
template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef, uint8_t, uint8_t,
838
                                                    uint8_t, bool, InputFile *);
839
template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef, uint8_t, uint8_t,
840
                                                    uint8_t, bool, InputFile *);
841
template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef, uint8_t, uint8_t,
842
                                                    uint8_t, bool, InputFile *);
843
844
template void SymbolTable::addCombinedLTOObject<ELF32LE>();
845
template void SymbolTable::addCombinedLTOObject<ELF32BE>();
846
template void SymbolTable::addCombinedLTOObject<ELF64LE>();
847
template void SymbolTable::addCombinedLTOObject<ELF64BE>();
848
849
template void
850
SymbolTable::addLazyArchive<ELF32LE>(StringRef, ArchiveFile &,
851
                                     const object::Archive::Symbol);
852
template void
853
SymbolTable::addLazyArchive<ELF32BE>(StringRef, ArchiveFile &,
854
                                     const object::Archive::Symbol);
855
template void
856
SymbolTable::addLazyArchive<ELF64LE>(StringRef, ArchiveFile &,
857
                                     const object::Archive::Symbol);
858
template void
859
SymbolTable::addLazyArchive<ELF64BE>(StringRef, ArchiveFile &,
860
                                     const object::Archive::Symbol);
861
862
template void SymbolTable::addLazyObject<ELF32LE>(StringRef, LazyObjFile &);
863
template void SymbolTable::addLazyObject<ELF32BE>(StringRef, LazyObjFile &);
864
template void SymbolTable::addLazyObject<ELF64LE>(StringRef, LazyObjFile &);
865
template void SymbolTable::addLazyObject<ELF64BE>(StringRef, LazyObjFile &);
866
867
template void SymbolTable::fetchLazy<ELF32LE>(Symbol *);
868
template void SymbolTable::fetchLazy<ELF32BE>(Symbol *);
869
template void SymbolTable::fetchLazy<ELF64LE>(Symbol *);
870
template void SymbolTable::fetchLazy<ELF64BE>(Symbol *);
871
872
template void SymbolTable::addShared<ELF32LE>(StringRef, SharedFile<ELF32LE> &,
873
                                              const typename ELF32LE::Sym &,
874
                                              uint32_t Alignment, uint32_t);
875
template void SymbolTable::addShared<ELF32BE>(StringRef, SharedFile<ELF32BE> &,
876
                                              const typename ELF32BE::Sym &,
877
                                              uint32_t Alignment, uint32_t);
878
template void SymbolTable::addShared<ELF64LE>(StringRef, SharedFile<ELF64LE> &,
879
                                              const typename ELF64LE::Sym &,
880
                                              uint32_t Alignment, uint32_t);
881
template void SymbolTable::addShared<ELF64BE>(StringRef, SharedFile<ELF64BE> &,
882
                                              const typename ELF64BE::Sym &,
883
                                              uint32_t Alignment, uint32_t);