-
Notifications
You must be signed in to change notification settings - Fork 10.8k
/
SymbolTable.cpp
407 lines (365 loc) · 13.5 KB
/
SymbolTable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//===- SymbolTable.cpp ----------------------------------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Symbol table is a bag of all known symbols. We put all symbols of
// all input files to the symbol table. The symbol table is basically
// a hash table with the logic to resolve symbol name conflicts using
// the symbol types.
//
//===----------------------------------------------------------------------===//
#include "SymbolTable.h"
#include "Config.h"
#include "Error.h"
#include "Symbols.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/Support/StringSaver.h"
using namespace llvm;
using namespace llvm::object;
using namespace llvm::ELF;
using namespace lld;
using namespace lld::elf;
// All input object files must be for the same architecture
// (e.g. it does not make sense to link x86 object files with
// MIPS object files.) This function checks for that error.
template <class ELFT> static bool isCompatible(InputFile *FileP) {
auto *F = dyn_cast<ELFFileBase<ELFT>>(FileP);
if (!F)
return true;
if (F->getELFKind() == Config->EKind && F->getEMachine() == Config->EMachine)
return true;
StringRef A = F->getName();
StringRef B = Config->Emulation;
if (B.empty())
B = Config->FirstElf->getName();
error(A + " is incompatible with " + B);
return false;
}
// Returns "(internal)", "foo.a(bar.o)" or "baz.o".
static std::string getFilename(InputFile *F) {
if (!F)
return "(internal)";
if (!F->ArchiveName.empty())
return (F->ArchiveName + "(" + F->getName() + ")").str();
return F->getName();
}
// Add symbols in File to the symbol table.
template <class ELFT>
void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) {
InputFile *FileP = File.get();
if (!isCompatible<ELFT>(FileP))
return;
// .a file
if (auto *F = dyn_cast<ArchiveFile>(FileP)) {
ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release()));
F->parse();
for (Lazy &Sym : F->getLazySymbols())
addLazy(&Sym);
return;
}
// Lazy object file
if (auto *F = dyn_cast<LazyObjectFile>(FileP)) {
LazyObjectFiles.emplace_back(cast<LazyObjectFile>(File.release()));
F->parse();
for (Lazy &Sym : F->getLazySymbols())
addLazy(&Sym);
return;
}
if (Config->Trace)
llvm::outs() << getFilename(FileP) << "\n";
// .so file
if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) {
// DSOs are uniquified not by filename but by soname.
F->parseSoName();
if (!SoNames.insert(F->getSoName()).second)
return;
SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release()));
F->parseRest();
for (SharedSymbol<ELFT> &B : F->getSharedSymbols())
resolve(&B);
return;
}
// LLVM bitcode file
if (auto *F = dyn_cast<BitcodeFile>(FileP)) {
BitcodeFiles.emplace_back(cast<BitcodeFile>(File.release()));
F->parse(ComdatGroups);
for (SymbolBody *B : F->getSymbols())
if (B)
resolve(B);
return;
}
// Regular object file
auto *F = cast<ObjectFile<ELFT>>(FileP);
ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release()));
F->parse(ComdatGroups);
for (SymbolBody *B : F->getNonLocalSymbols())
resolve(B);
}
// This function is where all the optimizations of link-time
// optimization happens. When LTO is in use, some input files are
// not in native object file format but in the LLVM bitcode format.
// This function compiles bitcode files into a few big native files
// using LLVM functions and replaces bitcode symbols with the results.
// Because all bitcode files that consist of a program are passed
// to the compiler at once, it can do whole-program optimization.
template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() {
if (BitcodeFiles.empty())
return;
// Compile bitcode files.
Lto.reset(new BitcodeCompiler);
for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles)
Lto->add(*F);
std::vector<std::unique_ptr<InputFile>> IFs = Lto->compile();
// Replace bitcode symbols.
for (auto &IF : IFs) {
ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(IF.release());
llvm::DenseSet<StringRef> DummyGroups;
Obj->parse(DummyGroups);
for (SymbolBody *Body : Obj->getNonLocalSymbols()) {
Symbol *Sym = insert(Body);
if (!Sym->Body->isUndefined() && Body->isUndefined())
continue;
Sym->Body = Body;
}
ObjectFiles.emplace_back(Obj);
}
}
// Add an undefined symbol.
template <class ELFT>
SymbolBody *SymbolTable<ELFT>::addUndefined(StringRef Name) {
auto *Sym = new (Alloc)
Undefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0, /*Size*/ 0,
/*IsBitcode*/ false);
resolve(Sym);
return Sym;
}
template <class ELFT>
DefinedRegular<ELFT> *SymbolTable<ELFT>::addAbsolute(StringRef Name,
uint8_t Visibility) {
// Pass nullptr because absolute symbols have no corresponding input sections.
auto *Sym = new (Alloc) DefinedRegular<ELFT>(Name, STB_GLOBAL, Visibility);
resolve(Sym);
return Sym;
}
template <class ELFT>
SymbolBody *SymbolTable<ELFT>::addSynthetic(StringRef Name,
OutputSectionBase<ELFT> &Sec,
uintX_t Val) {
auto *Sym = new (Alloc) DefinedSynthetic<ELFT>(Name, Val, Sec);
resolve(Sym);
return Sym;
}
// Add Name as an "ignored" symbol. An ignored symbol is a regular
// linker-synthesized defined symbol, but is only defined if needed.
template <class ELFT>
DefinedRegular<ELFT> *SymbolTable<ELFT>::addIgnored(StringRef Name,
uint8_t Visibility) {
if (!find(Name))
return nullptr;
return addAbsolute(Name, Visibility);
}
// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
// Used to implement --wrap.
template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) {
if (Symtab.count(Name) == 0)
return;
StringSaver Saver(Alloc);
Symbol *Sym = addUndefined(Name)->Backref;
Symbol *Real = addUndefined(Saver.save("__real_" + Name))->Backref;
Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name))->Backref;
Real->Body = Sym->Body;
Sym->Body = Wrap->Body;
}
// Returns a file from which symbol B was created.
// If B does not belong to any file, returns a nullptr.
// This function is slow, but it's okay as it is used only for error messages.
template <class ELFT> InputFile *SymbolTable<ELFT>::findFile(SymbolBody *B) {
for (const std::unique_ptr<ObjectFile<ELFT>> &F : ObjectFiles) {
ArrayRef<SymbolBody *> Syms = F->getSymbols();
if (std::find(Syms.begin(), Syms.end(), B) != Syms.end())
return F.get();
}
for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles) {
ArrayRef<SymbolBody *> Syms = F->getSymbols();
if (std::find(Syms.begin(), Syms.end(), B) != Syms.end())
return F.get();
}
return nullptr;
}
// Construct a string in the form of "Sym in File1 and File2".
// Used to construct an error message.
template <class ELFT>
std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Old, SymbolBody *New) {
InputFile *F1 = findFile(Old);
InputFile *F2 = findFile(New);
StringRef Sym = Old->getName();
return demangle(Sym) + " in " + getFilename(F1) + " and " + getFilename(F2);
}
// This function resolves conflicts if there's an existing symbol with
// the same name. Decisions are made based on symbol type.
template <class ELFT> void SymbolTable<ELFT>::resolve(SymbolBody *New) {
Symbol *Sym = insert(New);
if (Sym->Body == New)
return;
SymbolBody *Existing = Sym->Body;
if (auto *L = dyn_cast<Lazy>(Existing)) {
Sym->Binding = New->Binding;
if (New->isUndefined()) {
addMemberFile(New, L);
return;
}
// Found a definition for something also in an archive.
// Ignore the archive definition.
Sym->Body = New;
return;
}
if (New->isTls() != Existing->isTls()) {
error("TLS attribute mismatch for symbol: " + conflictMsg(Existing, New));
return;
}
// compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
// equivalent (conflicting), or more preferable, respectively.
int Comp = Existing->compare(New);
if (Comp == 0) {
std::string S = "duplicate symbol: " + conflictMsg(Existing, New);
if (Config->AllowMultipleDefinition)
warning(S);
else
error(S);
return;
}
if (Comp < 0) {
Sym->Body = New;
if (!New->isShared())
Sym->Binding = New->Binding;
}
}
static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
if (VA == STV_DEFAULT)
return VB;
if (VB == STV_DEFAULT)
return VA;
return std::min(VA, VB);
}
static bool shouldExport(SymbolBody *B) {
if (Config->Shared || Config->ExportDynamic) {
// Export most symbols except for those that do not need to be exported.
return !B->CanOmitFromDynSym;
}
// Make sure we preempt DSO symbols with default visibility.
return B->isShared() && B->getVisibility() == STV_DEFAULT;
}
// Find an existing symbol or create and insert a new one.
template <class ELFT> Symbol *SymbolTable<ELFT>::insert(SymbolBody *New) {
StringRef Name = New->getName();
unsigned NumSyms = SymVector.size();
auto P = Symtab.insert(std::make_pair(Name, NumSyms));
Symbol *Sym;
if (P.second) {
Sym = new (Alloc) Symbol;
Sym->Body = New;
Sym->Binding = New->isShared() ? STB_GLOBAL : New->Binding;
Sym->Visibility = STV_DEFAULT;
Sym->IsUsedInRegularObj = false;
Sym->ExportDynamic = false;
Sym->VersionScriptGlobal = !Config->VersionScript;
SymVector.push_back(Sym);
} else {
Sym = SymVector[P.first->second];
}
New->Backref = Sym;
// Merge in the new symbol's visibility. DSO symbols do not affect visibility
// in the output.
if (!New->isShared())
Sym->Visibility = getMinVisibility(Sym->Visibility, New->getVisibility());
Sym->ExportDynamic = Sym->ExportDynamic || shouldExport(New);
SymbolBody::Kind K = New->kind();
if (K == SymbolBody::DefinedRegularKind ||
K == SymbolBody::DefinedCommonKind ||
K == SymbolBody::DefinedSyntheticKind ||
(K == SymbolBody::UndefinedKind && !New->IsUndefinedBitcode))
Sym->IsUsedInRegularObj = true;
return Sym;
}
template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
auto It = Symtab.find(Name);
if (It == Symtab.end())
return nullptr;
return SymVector[It->second]->Body;
}
template <class ELFT> void SymbolTable<ELFT>::addLazy(Lazy *L) {
Symbol *Sym = insert(L);
SymbolBody *Cur = Sym->Body;
if (Cur == L)
return;
if (Cur->isUndefined()) {
Sym->Body = L;
addMemberFile(Cur, L);
}
}
template <class ELFT>
void SymbolTable<ELFT>::addMemberFile(SymbolBody *Undef, Lazy *L) {
// Weak undefined symbols should not fetch members from archives.
// If we were to keep old symbol we would not know that an archive member was
// available if a strong undefined symbol shows up afterwards in the link.
// If a strong undefined symbol never shows up, this lazy symbol will
// get to the end of the link and must be treated as the weak undefined one.
// We already marked this symbol as used when we added it to the symbol table,
// but we also need to preserve its binding and type.
if (Undef->isWeak()) {
// FIXME: Consider moving these members to Symbol.
L->Type = Undef->Type;
return;
}
// Fetch a member file that has the definition for L.
// getMember returns nullptr if the member was already read from the library.
if (std::unique_ptr<InputFile> File = L->getFile())
addFile(std::move(File));
}
// Process undefined (-u) flags by loading lazy symbols named by those flags.
template <class ELFT>
void SymbolTable<ELFT>::scanUndefinedFlags() {
for (StringRef S : Config->Undefined)
if (SymbolBody *Sym = find(S))
if (auto *L = dyn_cast<Lazy>(Sym))
if (std::unique_ptr<InputFile> File = L->getFile())
addFile(std::move(File));
}
// This function takes care of the case in which shared libraries depend on
// the user program (not the other way, which is usual). Shared libraries
// may have undefined symbols, expecting that the user program provides
// the definitions for them. An example is BSD's __progname symbol.
// We need to put such symbols to the main program's .dynsym so that
// shared libraries can find them.
// Except this, we ignore undefined symbols in DSOs.
template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
for (StringRef U : File->getUndefinedSymbols())
if (SymbolBody *Sym = find(U))
if (Sym->isDefined())
Sym->Backref->ExportDynamic = true;
}
// This function process the dynamic list option by marking all the symbols
// to be exported in the dynamic table.
template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
for (StringRef S : Config->DynamicList)
if (SymbolBody *B = find(S))
B->Backref->ExportDynamic = true;
}
// This function processes the --version-script option by marking all global
// symbols with the VersionScriptGlobal flag, which acts as a filter on the
// dynamic symbol table.
template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
for (StringRef S : Config->VersionScriptGlobals)
if (SymbolBody *B = find(S))
B->Backref->VersionScriptGlobal = true;
}
template class elf::SymbolTable<ELF32LE>;
template class elf::SymbolTable<ELF32BE>;
template class elf::SymbolTable<ELF64LE>;
template class elf::SymbolTable<ELF64BE>;