Coverage Report

Created: 2019-05-19 14:56

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/Symbols.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Symbols.cpp --------------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "Symbols.h"
10
#include "InputFiles.h"
11
#include "InputSection.h"
12
#include "OutputSections.h"
13
#include "SyntheticSections.h"
14
#include "Target.h"
15
#include "Writer.h"
16
#include "lld/Common/ErrorHandler.h"
17
#include "lld/Common/Strings.h"
18
#include "llvm/ADT/STLExtras.h"
19
#include "llvm/Support/Path.h"
20
#include <cstring>
21
22
using namespace llvm;
23
using namespace llvm::object;
24
using namespace llvm::ELF;
25
26
using namespace lld;
27
using namespace lld::elf;
28
29
Defined *ElfSym::Bss;
30
Defined *ElfSym::Etext1;
31
Defined *ElfSym::Etext2;
32
Defined *ElfSym::Edata1;
33
Defined *ElfSym::Edata2;
34
Defined *ElfSym::End1;
35
Defined *ElfSym::End2;
36
Defined *ElfSym::GlobalOffsetTable;
37
Defined *ElfSym::MipsGp;
38
Defined *ElfSym::MipsGpDisp;
39
Defined *ElfSym::MipsLocalGp;
40
Defined *ElfSym::RelaIpltStart;
41
Defined *ElfSym::RelaIpltEnd;
42
43
103k
static uint64_t getSymVA(const Symbol &Sym, int64_t &Addend) {
44
103k
  switch (Sym.kind()) {
45
103k
  case Symbol::DefinedKind: {
46
102k
    auto &D = cast<Defined>(Sym);
47
102k
    SectionBase *IS = D.Section;
48
102k
49
102k
    // According to the ELF spec reference to a local symbol from outside
50
102k
    // the group are not allowed. Unfortunately .eh_frame breaks that rule
51
102k
    // and must be treated specially. For now we just replace the symbol with
52
102k
    // 0.
53
102k
    if (IS == &InputSection::Discarded)
54
3
      return 0;
55
102k
56
102k
    // This is an absolute symbol.
57
102k
    if (!IS)
58
1.00k
      return D.Value;
59
101k
60
101k
    IS = IS->Repl;
61
101k
62
101k
    uint64_t Offset = D.Value;
63
101k
64
101k
    // An object in an SHF_MERGE section might be referenced via a
65
101k
    // section symbol (as a hack for reducing the number of local
66
101k
    // symbols).
67
101k
    // Depending on the addend, the reference via a section symbol
68
101k
    // refers to a different object in the merge section.
69
101k
    // Since the objects in the merge section are not necessarily
70
101k
    // contiguous in the output, the addend can thus affect the final
71
101k
    // VA in a non-linear way.
72
101k
    // To make this work, we incorporate the addend into the section
73
101k
    // offset (and zero out the addend for later processing) so that
74
101k
    // we find the right object in the section.
75
101k
    if (D.isSection()) {
76
66.6k
      Offset += Addend;
77
66.6k
      Addend = 0;
78
66.6k
    }
79
101k
80
101k
    // In the typical case, this is actually very simple and boils
81
101k
    // down to adding together 3 numbers:
82
101k
    // 1. The address of the output section.
83
101k
    // 2. The offset of the input section within the output section.
84
101k
    // 3. The offset within the input section (this addition happens
85
101k
    //    inside InputSection::getOffset).
86
101k
    //
87
101k
    // If you understand the data structures involved with this next
88
101k
    // line (and how they get built), then you have a pretty good
89
101k
    // understanding of the linker.
90
101k
    uint64_t VA = IS->getVA(Offset);
91
101k
92
101k
    // MIPS relocatable files can mix regular and microMIPS code.
93
101k
    // Linker needs to distinguish such code. To do so microMIPS
94
101k
    // symbols has the `STO_MIPS_MICROMIPS` flag in the `st_other`
95
101k
    // field. Unfortunately, the `MIPS::relocateOne()` method has
96
101k
    // a symbol value only. To pass type of the symbol (regular/microMIPS)
97
101k
    // to that routine as well as other places where we write
98
101k
    // a symbol value as-is (.dynamic section, `Elf_Ehdr::e_entry`
99
101k
    // field etc) do the same trick as compiler uses to mark microMIPS
100
101k
    // for CPU - set the less-significant bit.
101
101k
    if (Config->EMachine == EM_MIPS && 
isMicroMips()21.8k
&&
102
101k
        
(236
(Sym.StOther & STO_MIPS_MICROMIPS)236
||
Sym.NeedsPltAddr142
))
103
98
      VA |= 1;
104
101k
105
101k
    if (D.isTls() && 
!Config->Relocatable571
) {
106
570
      // Use the address of the TLS segment's first section rather than the
107
570
      // segment's address, because segment addresses aren't initialized until
108
570
      // after sections are finalized. (e.g. Measuring the size of .rela.dyn
109
570
      // for Android relocation packing requires knowing TLS symbol addresses
110
570
      // during section finalization.)
111
570
      if (!Out::TlsPhdr || !Out::TlsPhdr->FirstSec)
112
0
        fatal(toString(D.File) +
113
0
              " has an STT_TLS symbol but doesn't have an SHF_TLS section");
114
570
      return VA - Out::TlsPhdr->FirstSec->Addr;
115
570
    }
116
100k
    return VA;
117
100k
  }
118
100k
  case Symbol::SharedKind:
119
1.16k
  case Symbol::UndefinedKind:
120
1.16k
    return 0;
121
1.16k
  case Symbol::LazyArchiveKind:
122
33
  case Symbol::LazyObjectKind:
123
33
    assert(Sym.IsUsedInRegularObj && "lazy symbol reached writer");
124
33
    return 0;
125
33
  case Symbol::CommonKind:
126
0
    llvm_unreachable("common symbol reached writer");
127
33
  case Symbol::PlaceholderKind:
128
0
    llvm_unreachable("placeholder symbol reached writer");
129
0
  }
130
0
  llvm_unreachable("invalid symbol kind");
131
0
}
132
133
103k
uint64_t Symbol::getVA(int64_t Addend) const {
134
103k
  uint64_t OutVA = getSymVA(*this, Addend);
135
103k
  return OutVA + Addend;
136
103k
}
137
138
242
uint64_t Symbol::getGotVA() const {
139
242
  if (GotInIgot)
140
12
    return In.IgotPlt->getVA() + getGotPltOffset();
141
230
  return In.Got->getVA() + getGotOffset();
142
230
}
143
144
462
uint64_t Symbol::getGotOffset() const {
145
462
  return GotIndex * Target->GotEntrySize;
146
462
}
147
148
407
uint64_t Symbol::getGotPltVA() const {
149
407
  if (IsInIplt)
150
61
    return In.IgotPlt->getVA() + getGotPltOffset();
151
346
  return In.GotPlt->getVA() + getGotPltOffset();
152
346
}
153
154
800
uint64_t Symbol::getGotPltOffset() const {
155
800
  if (IsInIplt)
156
130
    return PltIndex * Target->GotPltEntrySize;
157
670
  return (PltIndex + Target->GotPltHeaderEntriesNum) * Target->GotPltEntrySize;
158
670
}
159
160
1
uint64_t Symbol::getPPC64LongBranchOffset() const {
161
1
  assert(PPC64BranchltIndex != 0xffff);
162
1
  return PPC64BranchltIndex * Target->GotPltEntrySize;
163
1
}
164
165
677
uint64_t Symbol::getPltVA() const {
166
677
  PltSection *Plt = IsInIplt ? 
In.Iplt75
:
In.Plt602
;
167
677
  uint64_t OutVA =
168
677
      Plt->getVA() + Plt->HeaderSize + PltIndex * Target->PltEntrySize;
169
677
  // While linking microMIPS code PLT code are always microMIPS
170
677
  // code. Set the less-significant bit to track that fact.
171
677
  // See detailed comment in the `getSymVA` function.
172
677
  if (Config->EMachine == EM_MIPS && 
isMicroMips()14
)
173
6
    OutVA |= 1;
174
677
  return OutVA;
175
677
}
176
177
9
uint64_t Symbol::getPPC64LongBranchTableVA() const {
178
9
  assert(PPC64BranchltIndex != 0xffff);
179
9
  return In.PPC64LongBranchTarget->getVA() +
180
9
         PPC64BranchltIndex * Target->GotPltEntrySize;
181
9
}
182
183
86.1k
uint64_t Symbol::getSize() const {
184
86.1k
  if (const auto *DR = dyn_cast<Defined>(this))
185
86.0k
    return DR->Size;
186
51
  return cast<SharedSymbol>(this)->Size;
187
51
}
188
189
152k
OutputSection *Symbol::getOutputSection() const {
190
152k
  if (auto *S = dyn_cast<Defined>(this)) {
191
152k
    if (auto *Sec = S->Section)
192
151k
      return Sec->Repl->getOutputSection();
193
699
    return nullptr;
194
699
  }
195
3
  return nullptr;
196
3
}
197
198
// If a symbol name contains '@', the characters after that is
199
// a symbol version name. This function parses that.
200
16.7k
void Symbol::parseSymbolVersion() {
201
16.7k
  StringRef S = getName();
202
16.7k
  size_t Pos = S.find('@');
203
16.7k
  if (Pos == 0 || Pos == StringRef::npos)
204
16.6k
    return;
205
79
  StringRef Verstr = S.substr(Pos + 1);
206
79
  if (Verstr.empty())
207
1
    return;
208
78
209
78
  // Truncate the symbol name so that it doesn't include the version string.
210
78
  NameSize = Pos;
211
78
212
78
  // If this is not in this DSO, it is not a definition.
213
78
  if (!isDefined())
214
37
    return;
215
41
216
41
  // '@@' in a symbol name means the default version.
217
41
  // It is usually the most recent one.
218
41
  bool IsDefault = (Verstr[0] == '@');
219
41
  if (IsDefault)
220
20
    Verstr = Verstr.substr(1);
221
41
222
51
  for (VersionDefinition &Ver : Config->VersionDefinitions) {
223
51
    if (Ver.Name != Verstr)
224
19
      continue;
225
32
226
32
    if (IsDefault)
227
14
      VersionId = Ver.Id;
228
18
    else
229
18
      VersionId = Ver.Id | VERSYM_HIDDEN;
230
32
    return;
231
32
  }
232
41
233
41
  // It is an error if the specified version is not defined.
234
41
  // Usually version script is not provided when linking executable,
235
41
  // but we may still want to override a versioned symbol from DSO,
236
41
  // so we do not report error in this case. We also do not error
237
41
  // if the symbol has a local version as it won't be in the dynamic
238
41
  // symbol table.
239
41
  
if (9
Config->Shared9
&&
VersionId != VER_NDX_LOCAL5
)
240
3
    error(toString(File) + ": symbol " + S + " has undefined version " +
241
3
          Verstr);
242
9
}
243
244
89
InputFile *LazyArchive::fetch() const {
245
89
  return cast<ArchiveFile>(File)->fetch(Sym);
246
89
}
247
248
2
MemoryBufferRef LazyArchive::getMemberBuffer() {
249
2
  Archive::Child C = CHECK(
250
2
      Sym.getMember(), "could not get the member for symbol " + Sym.getName());
251
2
252
2
  return CHECK(C.getMemoryBufferRef(),
253
2
               "could not get the buffer for the member defining symbol " +
254
2
                   Sym.getName());
255
2
}
256
257
11
InputFile *LazyObject::fetch() const {
258
11
  return cast<LazyObjFile>(File)->fetch();
259
11
}
260
261
45.3k
uint8_t Symbol::computeBinding() const {
262
45.3k
  if (Config->Relocatable)
263
286
    return Binding;
264
45.0k
  if (Visibility != STV_DEFAULT && 
Visibility != STV_PROTECTED6.68k
)
265
6.49k
    return STB_LOCAL;
266
38.5k
  if (VersionId == VER_NDX_LOCAL && 
isDefined()435
&&
!IsPreemptible341
)
267
340
    return STB_LOCAL;
268
38.2k
  if (!Config->GnuUnique && 
Binding == STB_GNU_UNIQUE10
)
269
5
    return STB_GLOBAL;
270
38.1k
  return Binding;
271
38.1k
}
272
273
35.7k
bool Symbol::includeInDynsym() const {
274
35.7k
  if (!Config->HasDynSymTab)
275
26.7k
    return false;
276
9.03k
  if (computeBinding() == STB_LOCAL)
277
3.30k
    return false;
278
5.72k
  // If a PIE binary was not linked against any shared libraries, then we can
279
5.72k
  // safely drop weak undef symbols from .dynsym.
280
5.72k
  if (isUndefWeak() && 
Config->Pie119
&&
SharedFiles.empty()8
)
281
2
    return false;
282
5.72k
  if (!isDefined())
283
1.62k
    return true;
284
4.10k
  return ExportDynamic;
285
4.10k
}
286
287
// Print out a log message for --trace-symbol.
288
59
void elf::printTraceSymbol(Symbol *Sym) {
289
59
  std::string S;
290
59
  if (Sym->isUndefined())
291
18
    S = ": reference to ";
292
41
  else if (Sym->isLazy())
293
2
    S = ": lazy definition of ";
294
39
  else if (Sym->isShared())
295
9
    S = ": shared definition of ";
296
30
  else if (Sym->isCommon())
297
5
    S = ": common definition of ";
298
25
  else
299
25
    S = ": definition of ";
300
59
301
59
  message(toString(Sym->File) + S + Sym->getName());
302
59
}
303
304
169
void elf::maybeWarnUnorderableSymbol(const Symbol *Sym) {
305
169
  if (!Config->WarnSymbolOrdering)
306
7
    return;
307
162
308
162
  // If UnresolvedPolicy::Ignore is used, no "undefined symbol" error/warning
309
162
  // is emitted. It makes sense to not warn on undefined symbols.
310
162
  //
311
162
  // Note, ld.bfd --symbol-ordering-file= does not warn on undefined symbols,
312
162
  // but we don't have to be compatible here.
313
162
  if (Sym->isUndefined() &&
314
162
      
Config->UnresolvedSymbols == UnresolvedPolicy::Ignore3
)
315
1
    return;
316
161
317
161
  const InputFile *File = Sym->File;
318
161
  auto *D = dyn_cast<Defined>(Sym);
319
161
320
161
  auto Warn = [&](StringRef S) 
{ warn(toString(File) + S + Sym->getName()); }18
;
321
161
322
161
  if (Sym->isUndefined())
323
2
    Warn(": unable to order undefined symbol: ");
324
159
  else if (Sym->isShared())
325
2
    Warn(": unable to order shared symbol: ");
326
157
  else if (D && !D->Section)
327
7
    Warn(": unable to order absolute symbol: ");
328
150
  else if (D && isa<OutputSection>(D->Section))
329
2
    Warn(": unable to order synthetic symbol: ");
330
148
  else if (D && !D->Section->Repl->Live)
331
5
    Warn(": unable to order discarded symbol: ");
332
161
}
333
334
// Returns a symbol for an error message.
335
299
std::string lld::toString(const Symbol &B) {
336
299
  if (Config->Demangle)
337
290
    if (Optional<std::string> S = demangleItanium(B.getName()))
338
20
      return *S;
339
279
  return B.getName();
340
279
}