Coverage Report

Created: 2019-01-18 03:29

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