Coverage Report

Created: 2018-10-20 06:24

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