Coverage Report

Created: 2019-01-18 03:29

/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
//                             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
#include "Symbols.h"
11
#include "InputFiles.h"
12
#include "InputSection.h"
13
#include "OutputSections.h"
14
#include "SyntheticSections.h"
15
#include "Target.h"
16
#include "Writer.h"
17
#include "lld/Common/ErrorHandler.h"
18
#include "lld/Common/Strings.h"
19
#include "llvm/ADT/STLExtras.h"
20
#include "llvm/Support/Path.h"
21
#include <cstring>
22
23
using namespace llvm;
24
using namespace llvm::object;
25
using namespace llvm::ELF;
26
27
using namespace lld;
28
using namespace lld::elf;
29
30
Defined *ElfSym::Bss;
31
Defined *ElfSym::Etext1;
32
Defined *ElfSym::Etext2;
33
Defined *ElfSym::Edata1;
34
Defined *ElfSym::Edata2;
35
Defined *ElfSym::End1;
36
Defined *ElfSym::End2;
37
Defined *ElfSym::GlobalOffsetTable;
38
Defined *ElfSym::MipsGp;
39
Defined *ElfSym::MipsGpDisp;
40
Defined *ElfSym::MipsLocalGp;
41
Defined *ElfSym::RelaIpltStart;
42
Defined *ElfSym::RelaIpltEnd;
43
44
102k
static uint64_t getSymVA(const Symbol &Sym, int64_t &Addend) {
45
102k
  switch (Sym.kind()) {
46
102k
  case Symbol::DefinedKind: {
47
101k
    auto &D = cast<Defined>(Sym);
48
101k
    SectionBase *IS = D.Section;
49
101k
50
101k
    // According to the ELF spec reference to a local symbol from outside
51
101k
    // the group are not allowed. Unfortunately .eh_frame breaks that rule
52
101k
    // and must be treated specially. For now we just replace the symbol with
53
101k
    // 0.
54
101k
    if (IS == &InputSection::Discarded)
55
3
      return 0;
56
101k
57
101k
    // This is an absolute symbol.
58
101k
    if (!IS)
59
1.00k
      return D.Value;
60
100k
61
100k
    IS = IS->Repl;
62
100k
63
100k
    uint64_t Offset = D.Value;
64
100k
65
100k
    // An object in an SHF_MERGE section might be referenced via a
66
100k
    // section symbol (as a hack for reducing the number of local
67
100k
    // symbols).
68
100k
    // Depending on the addend, the reference via a section symbol
69
100k
    // refers to a different object in the merge section.
70
100k
    // Since the objects in the merge section are not necessarily
71
100k
    // contiguous in the output, the addend can thus affect the final
72
100k
    // VA in a non-linear way.
73
100k
    // To make this work, we incorporate the addend into the section
74
100k
    // offset (and zero out the addend for later processing) so that
75
100k
    // we find the right object in the section.
76
100k
    if (D.isSection()) {
77
66.5k
      Offset += Addend;
78
66.5k
      Addend = 0;
79
66.5k
    }
80
100k
81
100k
    // In the typical case, this is actually very simple and boils
82
100k
    // down to adding together 3 numbers:
83
100k
    // 1. The address of the output section.
84
100k
    // 2. The offset of the input section within the output section.
85
100k
    // 3. The offset within the input section (this addition happens
86
100k
    //    inside InputSection::getOffset).
87
100k
    //
88
100k
    // If you understand the data structures involved with this next
89
100k
    // line (and how they get built), then you have a pretty good
90
100k
    // understanding of the linker.
91
100k
    uint64_t VA = IS->getVA(Offset);
92
100k
93
100k
    if (D.isTls() && 
!Config->Relocatable541
) {
94
540
      // Use the address of the TLS segment's first section rather than the
95
540
      // segment's address, because segment addresses aren't initialized until
96
540
      // after sections are finalized. (e.g. Measuring the size of .rela.dyn
97
540
      // for Android relocation packing requires knowing TLS symbol addresses
98
540
      // during section finalization.)
99
540
      if (!Out::TlsPhdr || !Out::TlsPhdr->FirstSec)
100
0
        fatal(toString(D.File) +
101
0
              " has an STT_TLS symbol but doesn't have an SHF_TLS section");
102
540
      return VA - Out::TlsPhdr->FirstSec->Addr;
103
540
    }
104
100k
    return VA;
105
100k
  }
106
100k
  case Symbol::SharedKind:
107
1.12k
  case Symbol::UndefinedKind:
108
1.12k
    return 0;
109
1.12k
  case Symbol::LazyArchiveKind:
110
33
  case Symbol::LazyObjectKind:
111
33
    assert(Sym.IsUsedInRegularObj && "lazy symbol reached writer");
112
33
    return 0;
113
33
  case Symbol::PlaceholderKind:
114
0
    llvm_unreachable("placeholder symbol reached writer");
115
0
  }
116
0
  llvm_unreachable("invalid symbol kind");
117
0
}
118
119
102k
uint64_t Symbol::getVA(int64_t Addend) const {
120
102k
  uint64_t OutVA = getSymVA(*this, Addend);
121
102k
  return OutVA + Addend;
122
102k
}
123
124
197
uint64_t Symbol::getGotVA() const { return In.Got->getVA() + getGotOffset(); }
125
126
445
uint64_t Symbol::getGotOffset() const {
127
445
  return GotIndex * Target->GotEntrySize;
128
445
}
129
130
385
uint64_t Symbol::getGotPltVA() const {
131
385
  if (this->IsInIgot)
132
43
    return In.IgotPlt->getVA() + getGotPltOffset();
133
342
  return In.GotPlt->getVA() + getGotPltOffset();
134
342
}
135
136
740
uint64_t Symbol::getGotPltOffset() const {
137
740
  if (IsInIgot)
138
84
    return PltIndex * Target->GotPltEntrySize;
139
656
  return (PltIndex + Target->GotPltHeaderEntriesNum) * Target->GotPltEntrySize;
140
656
}
141
142
1
uint64_t Symbol::getPPC64LongBranchOffset() const {
143
1
  assert(PPC64BranchltIndex != 0xffff);
144
1
  return PPC64BranchltIndex * Target->GotPltEntrySize;
145
1
}
146
147
632
uint64_t Symbol::getPltVA() const {
148
632
  PltSection *Plt = IsInIplt ? 
In.Iplt71
:
In.Plt561
;
149
632
  return Plt->getVA() + Plt->HeaderSize + PltIndex * Target->PltEntrySize;
150
632
}
151
152
7
uint64_t Symbol::getPPC64LongBranchTableVA() const {
153
7
  assert(PPC64BranchltIndex != 0xffff);
154
7
  return In.PPC64LongBranchTarget->getVA() +
155
7
         PPC64BranchltIndex * Target->GotPltEntrySize;
156
7
}
157
158
85.6k
uint64_t Symbol::getSize() const {
159
85.6k
  if (const auto *DR = dyn_cast<Defined>(this))
160
85.6k
    return DR->Size;
161
51
  return cast<SharedSymbol>(this)->Size;
162
51
}
163
164
151k
OutputSection *Symbol::getOutputSection() const {
165
151k
  if (auto *S = dyn_cast<Defined>(this)) {
166
151k
    if (auto *Sec = S->Section)
167
151k
      return Sec->Repl->getOutputSection();
168
695
    return nullptr;
169
695
  }
170
3
  return nullptr;
171
3
}
172
173
// If a symbol name contains '@', the characters after that is
174
// a symbol version name. This function parses that.
175
16.5k
void Symbol::parseSymbolVersion() {
176
16.5k
  StringRef S = getName();
177
16.5k
  size_t Pos = S.find('@');
178
16.5k
  if (Pos == 0 || Pos == StringRef::npos)
179
16.4k
    return;
180
79
  StringRef Verstr = S.substr(Pos + 1);
181
79
  if (Verstr.empty())
182
1
    return;
183
78
184
78
  // Truncate the symbol name so that it doesn't include the version string.
185
78
  NameSize = Pos;
186
78
187
78
  // If this is not in this DSO, it is not a definition.
188
78
  if (!isDefined())
189
37
    return;
190
41
191
41
  // '@@' in a symbol name means the default version.
192
41
  // It is usually the most recent one.
193
41
  bool IsDefault = (Verstr[0] == '@');
194
41
  if (IsDefault)
195
20
    Verstr = Verstr.substr(1);
196
41
197
51
  for (VersionDefinition &Ver : Config->VersionDefinitions) {
198
51
    if (Ver.Name != Verstr)
199
19
      continue;
200
32
201
32
    if (IsDefault)
202
14
      VersionId = Ver.Id;
203
18
    else
204
18
      VersionId = Ver.Id | VERSYM_HIDDEN;
205
32
    return;
206
32
  }
207
41
208
41
  // It is an error if the specified version is not defined.
209
41
  // Usually version script is not provided when linking executable,
210
41
  // but we may still want to override a versioned symbol from DSO,
211
41
  // so we do not report error in this case. We also do not error
212
41
  // if the symbol has a local version as it won't be in the dynamic
213
41
  // symbol table.
214
41
  
if (9
Config->Shared9
&&
VersionId != VER_NDX_LOCAL5
)
215
3
    error(toString(File) + ": symbol " + S + " has undefined version " +
216
3
          Verstr);
217
9
}
218
219
34
InputFile *LazyArchive::fetch() { return cast<ArchiveFile>(File)->fetch(Sym); }
220
221
2
MemoryBufferRef LazyArchive::getMemberBuffer() {
222
2
  Archive::Child C = CHECK(
223
2
      Sym.getMember(), "could not get the member for symbol " + Sym.getName());
224
2
225
2
  return CHECK(C.getMemoryBufferRef(),
226
2
               "could not get the buffer for the member defining symbol " +
227
2
                   Sym.getName());
228
2
}
229
230
44.4k
uint8_t Symbol::computeBinding() const {
231
44.4k
  if (Config->Relocatable)
232
266
    return Binding;
233
44.2k
  if (Visibility != STV_DEFAULT && 
Visibility != STV_PROTECTED6.49k
)
234
6.29k
    return STB_LOCAL;
235
37.9k
  if (VersionId == VER_NDX_LOCAL && 
isDefined()435
&&
!IsPreemptible341
)
236
340
    return STB_LOCAL;
237
37.5k
  if (!Config->GnuUnique && 
Binding == STB_GNU_UNIQUE10
)
238
5
    return STB_GLOBAL;
239
37.5k
  return Binding;
240
37.5k
}
241
242
35.1k
bool Symbol::includeInDynsym() const {
243
35.1k
  if (!Config->HasDynSymTab)
244
26.3k
    return false;
245
8.81k
  if (computeBinding() == STB_LOCAL)
246
3.22k
    return false;
247
5.59k
  if (!isDefined())
248
1.58k
    return true;
249
4.01k
  return ExportDynamic;
250
4.01k
}
251
252
// Print out a log message for --trace-symbol.
253
54
void elf::printTraceSymbol(Symbol *Sym) {
254
54
  std::string S;
255
54
  if (Sym->isUndefined())
256
18
    S = ": reference to ";
257
36
  else if (Sym->isLazy())
258
2
    S = ": lazy definition of ";
259
34
  else if (Sym->isShared())
260
9
    S = ": shared definition of ";
261
25
  else if (dyn_cast_or_null<BssSection>(cast<Defined>(Sym)->Section))
262
5
    S = ": common definition of ";
263
20
  else
264
20
    S = ": definition of ";
265
54
266
54
  message(toString(Sym->File) + S + Sym->getName());
267
54
}
268
269
149
void elf::maybeWarnUnorderableSymbol(const Symbol *Sym) {
270
149
  if (!Config->WarnSymbolOrdering)
271
7
    return;
272
142
273
142
  // If UnresolvedPolicy::Ignore is used, no "undefined symbol" error/warning
274
142
  // is emitted. It makes sense to not warn on undefined symbols.
275
142
  //
276
142
  // Note, ld.bfd --symbol-ordering-file= does not warn on undefined symbols,
277
142
  // but we don't have to be compatible here.
278
142
  if (Sym->isUndefined() &&
279
142
      
Config->UnresolvedSymbols == UnresolvedPolicy::Ignore3
)
280
1
    return;
281
141
282
141
  const InputFile *File = Sym->File;
283
141
  auto *D = dyn_cast<Defined>(Sym);
284
141
285
141
  auto Warn = [&](StringRef S) 
{ warn(toString(File) + S + Sym->getName()); }18
;
286
141
287
141
  if (Sym->isUndefined())
288
2
    Warn(": unable to order undefined symbol: ");
289
139
  else if (Sym->isShared())
290
2
    Warn(": unable to order shared symbol: ");
291
137
  else if (D && !D->Section)
292
7
    Warn(": unable to order absolute symbol: ");
293
130
  else if (D && isa<OutputSection>(D->Section))
294
2
    Warn(": unable to order synthetic symbol: ");
295
128
  else if (D && !D->Section->Repl->Live)
296
5
    Warn(": unable to order discarded symbol: ");
297
141
}
298
299
// Returns a symbol for an error message.
300
240
std::string lld::toString(const Symbol &B) {
301
240
  if (Config->Demangle)
302
233
    if (Optional<std::string> S = demangleItanium(B.getName()))
303
16
      return *S;
304
224
  return B.getName();
305
224
}