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
408
409
410
411
412
413
414
415
416
417
418
419
420
|
//===- Symbols.cpp --------------------------------------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "Symbols.h"
#include "Error.h"
#include "InputFiles.h"
#include "InputSection.h"
#include "OutputSections.h"
#include "Target.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Config/config.h"
#ifdef HAVE_CXXABI_H
#include <cxxabi.h>
#endif
using namespace llvm;
using namespace llvm::object;
using namespace llvm::ELF;
using namespace lld;
using namespace lld::elf;
template <class ELFT>
static typename ELFT::uint getSymVA(const SymbolBody &Body,
typename ELFT::uint &Addend) {
typedef typename ELFT::uint uintX_t;
switch (Body.kind()) {
case SymbolBody::DefinedSyntheticKind: {
auto &D = cast<DefinedSynthetic<ELFT>>(Body);
if (D.Value == DefinedSynthetic<ELFT>::SectionEnd)
return D.Section.getVA() + D.Section.getSize();
return D.Section.getVA() + D.Value;
}
case SymbolBody::DefinedRegularKind: {
auto &D = cast<DefinedRegular<ELFT>>(Body);
InputSectionBase<ELFT> *SC = D.Section;
// According to the ELF spec reference to a local symbol from outside
// the group are not allowed. Unfortunately .eh_frame breaks that rule
// and must be treated specially. For now we just replace the symbol with
// 0.
if (SC == &InputSection<ELFT>::Discarded)
return 0;
// This is an absolute symbol.
if (!SC)
return D.Value;
uintX_t Offset = D.Value;
if (D.isSection()) {
Offset += Addend;
Addend = 0;
}
uintX_t VA = SC->OutSec->getVA() + SC->getOffset(Offset);
if (D.isTls())
return VA - Out<ELFT>::TlsPhdr->p_vaddr;
return VA;
}
case SymbolBody::DefinedCommonKind:
return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(Body).OffsetInBss;
case SymbolBody::SharedKind: {
auto &SS = cast<SharedSymbol<ELFT>>(Body);
if (!SS.NeedsCopyOrPltAddr)
return 0;
if (SS.isFunc())
return Body.getPltVA<ELFT>();
return Out<ELFT>::Bss->getVA() + SS.OffsetInBss;
}
case SymbolBody::UndefinedElfKind:
case SymbolBody::UndefinedBitcodeKind:
return 0;
case SymbolBody::LazyArchiveKind:
case SymbolBody::LazyObjectKind:
assert(Body.isUsedInRegularObj() && "lazy symbol reached writer");
return 0;
case SymbolBody::DefinedBitcodeKind:
llvm_unreachable("should have been replaced");
}
llvm_unreachable("invalid symbol kind");
}
SymbolBody::SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther,
uint8_t Type)
: SymbolKind(K), Type(Type), Binding(STB_LOCAL), StOther(StOther),
NameOffset(NameOffset) {
init();
}
SymbolBody::SymbolBody(Kind K, StringRef Name, uint8_t Binding, uint8_t StOther,
uint8_t Type)
: SymbolKind(K), Type(Type), Binding(Binding), StOther(StOther),
Name({Name.data(), Name.size()}) {
assert(!isLocal());
init();
}
void SymbolBody::init() {
Kind K = kind();
IsUsedInRegularObj = K == DefinedRegularKind || K == DefinedCommonKind ||
K == DefinedSyntheticKind || K == UndefinedElfKind;
CanKeepUndefined = false;
MustBeInDynSym = false;
CanOmitFromDynSym = false;
NeedsCopyOrPltAddr = false;
}
// Returns true if a symbol can be replaced at load-time by a symbol
// with the same name defined in other ELF executable or DSO.
bool SymbolBody::isPreemptible() const {
if (isLocal())
return false;
if (isShared())
return true;
if (getVisibility() != STV_DEFAULT)
return false;
if (isUndefined()) {
if (!isWeak())
return true;
// Ideally the static linker should see a definition for every symbol, but
// shared object are normally allowed to have undefined references that the
// static linker never sees a definition for.
if (Config->Shared)
return true;
// Otherwise, just resolve to 0.
return false;
}
if (!Config->Shared)
return false;
if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc()))
return false;
return true;
}
template <class ELFT>
typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const {
typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend);
return OutVA + Addend;
}
template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const {
return Out<ELFT>::Got->getVA() + getGotOffset<ELFT>();
}
template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const {
return (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) *
sizeof(typename ELFT::uint);
}
template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const {
return Out<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>();
}
template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const {
return GotPltIndex * sizeof(typename ELFT::uint);
}
template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const {
return Out<ELFT>::Plt->getVA() + Target->PltZeroSize +
PltIndex * Target->PltEntrySize;
}
template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const {
auto *D = cast<DefinedRegular<ELFT>>(this);
auto *S = cast<InputSection<ELFT>>(D->Section);
return S->OutSec->getVA() + S->OutSecOff + S->getThunkOff() +
ThunkIndex * Target->ThunkSize;
}
template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
if (const auto *C = dyn_cast<DefinedCommon>(this))
return C->Size;
if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
return DR->Size;
if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
return S->Sym.st_size;
if (const auto *U = dyn_cast<UndefinedElf<ELFT>>(this))
return U->Size;
return 0;
}
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 int compareCommons(DefinedCommon *A, DefinedCommon *B) {
if (Config->WarnCommon)
warning("multiple common of " + A->getName());
A->Alignment = B->Alignment = std::max(A->Alignment, B->Alignment);
return A->Size < B->Size ? -1 : 1;
}
// Returns 1, 0 or -1 if this symbol should take precedence
// over the Other, tie or lose, respectively.
int SymbolBody::compare(SymbolBody *Other) {
assert(!isLazy() && !Other->isLazy());
std::tuple<bool, bool, bool> L(isDefined(), !isShared(), !isWeak());
std::tuple<bool, bool, bool> R(Other->isDefined(), !Other->isShared(),
!Other->isWeak());
// Normalize
if (L > R)
return -Other->compare(this);
uint8_t V = getMinVisibility(getVisibility(), Other->getVisibility());
if (isShared() != Other->isShared()) {
SymbolBody *Shared = isShared() ? this : Other;
Shared->MustBeInDynSym = true;
if (Shared->getVisibility() == STV_DEFAULT &&
(V == STV_DEFAULT || V == STV_PROTECTED)) {
// We want to export all symbols that exist in the executable and are
// preemptable in DSOs, so that the symbols in the executable can
// preempt symbols in the DSO at runtime.
SymbolBody *NonShared = isShared() ? Other : this;
NonShared->MustBeInDynSym = true;
}
}
if (!isShared() && !Other->isShared()) {
setVisibility(V);
Other->setVisibility(V);
}
if (IsUsedInRegularObj || Other->IsUsedInRegularObj)
IsUsedInRegularObj = Other->IsUsedInRegularObj = true;
if (!CanOmitFromDynSym || !Other->CanOmitFromDynSym)
CanOmitFromDynSym = Other->CanOmitFromDynSym = false;
if (L != R)
return -1;
if (!isDefined() || isShared() || isWeak())
return 1;
if (!isCommon() && !Other->isCommon())
return 0;
if (isCommon() && Other->isCommon())
return compareCommons(cast<DefinedCommon>(this),
cast<DefinedCommon>(Other));
if (Config->WarnCommon)
warning("common " + this->getName() + " is overridden");
return isCommon() ? -1 : 1;
}
Defined::Defined(Kind K, StringRef Name, uint8_t Binding, uint8_t StOther,
uint8_t Type)
: SymbolBody(K, Name, Binding, StOther, Type) {}
Defined::Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type)
: SymbolBody(K, NameOffset, StOther, Type) {}
DefinedBitcode::DefinedBitcode(StringRef Name, bool IsWeak, uint8_t StOther)
: Defined(DefinedBitcodeKind, Name, IsWeak ? STB_WEAK : STB_GLOBAL,
StOther, 0 /* Type */) {}
bool DefinedBitcode::classof(const SymbolBody *S) {
return S->kind() == DefinedBitcodeKind;
}
UndefinedBitcode::UndefinedBitcode(StringRef N, bool IsWeak, uint8_t StOther)
: SymbolBody(SymbolBody::UndefinedBitcodeKind, N,
IsWeak ? STB_WEAK : STB_GLOBAL, StOther, 0 /* Type */) {}
template <typename ELFT>
UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym)
: SymbolBody(SymbolBody::UndefinedElfKind, N, Sym.getBinding(),
Sym.st_other, Sym.getType()),
Size(Sym.st_size) {}
template <typename ELFT>
UndefinedElf<ELFT>::UndefinedElf(StringRef Name, uint8_t Binding,
uint8_t StOther, uint8_t Type,
bool CanKeepUndefined)
: SymbolBody(SymbolBody::UndefinedElfKind, Name, Binding, StOther, Type) {
this->CanKeepUndefined = CanKeepUndefined;
}
template <typename ELFT>
UndefinedElf<ELFT>::UndefinedElf(const Elf_Sym &Sym)
: SymbolBody(SymbolBody::UndefinedElfKind, Sym.st_name, Sym.st_other,
Sym.getType()),
Size(Sym.st_size) {
assert(Sym.getBinding() == STB_LOCAL);
}
template <typename ELFT>
DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
OutputSectionBase<ELFT> &Section)
: Defined(SymbolBody::DefinedSyntheticKind, N, STB_GLOBAL, STV_HIDDEN,
0 /* Type */),
Value(Value), Section(Section) {}
DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
uint8_t Binding, uint8_t StOther, uint8_t Type)
: Defined(SymbolBody::DefinedCommonKind, N, Binding, StOther, Type),
Alignment(Alignment), Size(Size) {}
std::unique_ptr<InputFile> Lazy::getFile() {
if (auto *S = dyn_cast<LazyArchive>(this))
return S->getFile();
return cast<LazyObject>(this)->getFile();
}
std::unique_ptr<InputFile> LazyArchive::getFile() {
MemoryBufferRef MBRef = File->getMember(&Sym);
// getMember returns an empty buffer if the member was already
// read from the library.
if (MBRef.getBuffer().empty())
return std::unique_ptr<InputFile>(nullptr);
return createObjectFile(MBRef, File->getName());
}
std::unique_ptr<InputFile> LazyObject::getFile() {
return createObjectFile(MBRef);
}
// Returns the demangled C++ symbol name for Name.
std::string elf::demangle(StringRef Name) {
#if !defined(HAVE_CXXABI_H)
return Name;
#else
if (!Config->Demangle)
return Name;
// __cxa_demangle can be used to demangle strings other than symbol
// names which do not necessarily start with "_Z". Name can be
// either a C or C++ symbol. Don't call __cxa_demangle if the name
// does not look like a C++ symbol name to avoid getting unexpected
// result for a C symbol that happens to match a mangled type name.
if (!Name.startswith("_Z"))
return Name;
char *Buf =
abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
if (!Buf)
return Name;
std::string S(Buf);
free(Buf);
return S;
#endif
}
bool SymbolBody::includeInDynsym() const {
if (MustBeInDynSym)
return true;
uint8_t V = getVisibility();
if (V != STV_DEFAULT && V != STV_PROTECTED)
return false;
if (!Config->ExportDynamic && !Config->Shared)
return false;
return !CanOmitFromDynSym;
}
template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const;
template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const;
template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const;
template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const;
template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const;
template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const;
template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const;
template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const;
template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
template uint32_t SymbolBody::template getSize<ELF32LE>() const;
template uint32_t SymbolBody::template getSize<ELF32BE>() const;
template uint64_t SymbolBody::template getSize<ELF64LE>() const;
template uint64_t SymbolBody::template getSize<ELF64BE>() const;
template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const;
template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const;
template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const;
template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const;
template class elf::UndefinedElf<ELF32LE>;
template class elf::UndefinedElf<ELF32BE>;
template class elf::UndefinedElf<ELF64LE>;
template class elf::UndefinedElf<ELF64BE>;
template class elf::DefinedSynthetic<ELF32LE>;
template class elf::DefinedSynthetic<ELF32BE>;
template class elf::DefinedSynthetic<ELF64LE>;
template class elf::DefinedSynthetic<ELF64BE>;
|