Coverage Report

Created: 2018-06-25 02:00

/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::RelaIpltEnd;
42
43
34.6k
static uint64_t getSymVA(const Symbol &Sym, int64_t &Addend) {
44
34.6k
  switch (Sym.kind()) {
45
34.6k
  case Symbol::DefinedKind: {
46
33.5k
    auto &D = cast<Defined>(Sym);
47
33.5k
    SectionBase *IS = D.Section;
48
33.5k
49
33.5k
    // According to the ELF spec reference to a local symbol from outside
50
33.5k
    // the group are not allowed. Unfortunately .eh_frame breaks that rule
51
33.5k
    // and must be treated specially. For now we just replace the symbol with
52
33.5k
    // 0.
53
33.5k
    if (IS == &InputSection::Discarded)
54
2
      return 0;
55
33.5k
56
33.5k
    // This is an absolute symbol.
57
33.5k
    if (!IS)
58
784
      return D.Value;
59
32.8k
60
32.8k
    IS = IS->Repl;
61
32.8k
62
32.8k
    uint64_t Offset = D.Value;
63
32.8k
64
32.8k
    // An object in an SHF_MERGE section might be referenced via a
65
32.8k
    // section symbol (as a hack for reducing the number of local
66
32.8k
    // symbols).
67
32.8k
    // Depending on the addend, the reference via a section symbol
68
32.8k
    // refers to a different object in the merge section.
69
32.8k
    // Since the objects in the merge section are not necessarily
70
32.8k
    // contiguous in the output, the addend can thus affect the final
71
32.8k
    // VA in a non-linear way.
72
32.8k
    // To make this work, we incorporate the addend into the section
73
32.8k
    // offset (and zero out the addend for later processing) so that
74
32.8k
    // we find the right object in the section.
75
32.8k
    if (D.isSection()) {
76
834
      Offset += Addend;
77
834
      Addend = 0;
78
834
    }
79
32.8k
80
32.8k
    // In the typical case, this is actually very simple and boils
81
32.8k
    // down to adding together 3 numbers:
82
32.8k
    // 1. The address of the output section.
83
32.8k
    // 2. The offset of the input section within the output section.
84
32.8k
    // 3. The offset within the input section (this addition happens
85
32.8k
    //    inside InputSection::getOffset).
86
32.8k
    //
87
32.8k
    // If you understand the data structures involved with this next
88
32.8k
    // line (and how they get built), then you have a pretty good
89
32.8k
    // understanding of the linker.
90
32.8k
    uint64_t VA = IS->getVA(Offset);
91
32.8k
92
32.8k
    if (D.isTls() && 
!Config->Relocatable408
) {
93
407
      if (!Out::TlsPhdr)
94
0
        fatal(toString(D.File) +
95
0
              " has an STT_TLS symbol but doesn't have an SHF_TLS section");
96
407
      return VA - Out::TlsPhdr->p_vaddr;
97
407
    }
98
32.3k
    return VA;
99
32.3k
  }
100
32.3k
  case Symbol::SharedKind:
101
1.05k
  case Symbol::UndefinedKind:
102
1.05k
    return 0;
103
1.05k
  case Symbol::LazyArchiveKind:
104
0
  case Symbol::LazyObjectKind:
105
0
    llvm_unreachable("lazy symbol reached writer");
106
0
  }
107
0
  llvm_unreachable("invalid symbol kind");
108
0
}
109
110
34.6k
uint64_t Symbol::getVA(int64_t Addend) const {
111
34.6k
  uint64_t OutVA = getSymVA(*this, Addend);
112
34.6k
  return OutVA + Addend;
113
34.6k
}
114
115
182
uint64_t Symbol::getGotVA() const { return InX::Got->getVA() + getGotOffset(); }
116
117
356
uint64_t Symbol::getGotOffset() const {
118
356
  return GotIndex * Target->GotEntrySize;
119
356
}
120
121
331
uint64_t Symbol::getGotPltVA() const {
122
331
  if (this->IsInIgot)
123
34
    return InX::IgotPlt->getVA() + getGotPltOffset();
124
297
  return InX::GotPlt->getVA() + getGotPltOffset();
125
297
}
126
127
642
uint64_t Symbol::getGotPltOffset() const {
128
642
  if (IsInIgot)
129
66
    return PltIndex * Target->GotPltEntrySize;
130
576
  return (PltIndex + Target->GotPltHeaderEntriesNum) * Target->GotPltEntrySize;
131
576
}
132
133
566
uint64_t Symbol::getPltVA() const {
134
566
  if (this->IsInIplt)
135
54
    return InX::Iplt->getVA() + PltIndex * Target->PltEntrySize;
136
512
  return InX::Plt->getVA() + Target->getPltEntryOffset(PltIndex);
137
512
}
138
139
42
uint64_t Symbol::getPltOffset() const {
140
42
  assert(!this->IsInIplt);
141
42
  return Target->getPltEntryOffset(PltIndex);
142
42
}
143
144
18.9k
uint64_t Symbol::getSize() const {
145
18.9k
  if (const auto *DR = dyn_cast<Defined>(this))
146
18.8k
    return DR->Size;
147
50
  if (const auto *S = dyn_cast<SharedSymbol>(this))
148
50
    return S->Size;
149
0
  return 0;
150
0
}
151
152
20.0k
OutputSection *Symbol::getOutputSection() const {
153
20.0k
  if (auto *S = dyn_cast<Defined>(this)) {
154
19.1k
    if (auto *Sec = S->Section)
155
18.5k
      return Sec->Repl->getOutputSection();
156
602
    return nullptr;
157
602
  }
158
963
  return nullptr;
159
963
}
160
161
// If a symbol name contains '@', the characters after that is
162
// a symbol version name. This function parses that.
163
15.7k
void Symbol::parseSymbolVersion() {
164
15.7k
  StringRef S = getName();
165
15.7k
  size_t Pos = S.find('@');
166
15.7k
  if (Pos == 0 || Pos == StringRef::npos)
167
15.6k
    return;
168
78
  StringRef Verstr = S.substr(Pos + 1);
169
78
  if (Verstr.empty())
170
0
    return;
171
78
172
78
  // Truncate the symbol name so that it doesn't include the version string.
173
78
  NameSize = Pos;
174
78
175
78
  // If this is not in this DSO, it is not a definition.
176
78
  if (!isDefined())
177
37
    return;
178
41
179
41
  // '@@' in a symbol name means the default version.
180
41
  // It is usually the most recent one.
181
41
  bool IsDefault = (Verstr[0] == '@');
182
41
  if (IsDefault)
183
20
    Verstr = Verstr.substr(1);
184
41
185
51
  for (VersionDefinition &Ver : Config->VersionDefinitions) {
186
51
    if (Ver.Name != Verstr)
187
19
      continue;
188
32
189
32
    if (IsDefault)
190
14
      VersionId = Ver.Id;
191
18
    else
192
18
      VersionId = Ver.Id | VERSYM_HIDDEN;
193
32
    return;
194
32
  }
195
41
196
41
  // It is an error if the specified version is not defined.
197
41
  // Usually version script is not provided when linking executable,
198
41
  // but we may still want to override a versioned symbol from DSO,
199
41
  // so we do not report error in this case. We also do not error
200
41
  // if the symbol has a local version as it won't be in the dynamic
201
41
  // symbol table.
202
41
  
if (9
Config->Shared9
&&
VersionId != VER_NDX_LOCAL5
)
203
3
    error(toString(File) + ": symbol " + S + " has undefined version " +
204
3
          Verstr);
205
9
}
206
207
28
InputFile *LazyArchive::fetch() { return cast<ArchiveFile>(File)->fetch(Sym); }
208
209
41.8k
uint8_t Symbol::computeBinding() const {
210
41.8k
  if (Config->Relocatable)
211
252
    return Binding;
212
41.6k
  if (Visibility != STV_DEFAULT && 
Visibility != STV_PROTECTED5.77k
)
213
5.60k
    return STB_LOCAL;
214
36.0k
  if (VersionId == VER_NDX_LOCAL && 
isDefined()409
)
215
316
    return STB_LOCAL;
216
35.7k
  if (!Config->GnuUnique && 
Binding == STB_GNU_UNIQUE10
)
217
5
    return STB_GLOBAL;
218
35.7k
  return Binding;
219
35.7k
}
220
221
20.7k
bool Symbol::includeInDynsym() const {
222
20.7k
  if (!Config->HasDynSymTab)
223
12.7k
    return false;
224
7.99k
  if (computeBinding() == STB_LOCAL)
225
2.90k
    return false;
226
5.08k
  if (!isDefined())
227
1.47k
    return true;
228
3.61k
  return ExportDynamic;
229
3.61k
}
230
231
// Print out a log message for --trace-symbol.
232
54
void elf::printTraceSymbol(Symbol *Sym) {
233
54
  std::string S;
234
54
  if (Sym->isUndefined())
235
18
    S = ": reference to ";
236
36
  else if (Sym->isLazy())
237
2
    S = ": lazy definition of ";
238
34
  else if (Sym->isShared())
239
9
    S = ": shared definition of ";
240
25
  else if (dyn_cast_or_null<BssSection>(cast<Defined>(Sym)->Section))
241
5
    S = ": common definition of ";
242
20
  else
243
20
    S = ": definition of ";
244
54
245
54
  message(toString(Sym->File) + S + Sym->getName());
246
54
}
247
248
136
void elf::warnUnorderableSymbol(const Symbol *Sym) {
249
136
  if (!Config->WarnSymbolOrdering)
250
0
    return;
251
136
252
136
  const InputFile *File = Sym->File;
253
136
  auto *D = dyn_cast<Defined>(Sym);
254
136
255
136
  auto Warn = [&](StringRef S) 
{ warn(toString(File) + S + Sym->getName()); }20
;
256
136
257
136
  if (Sym->isUndefined())
258
4
    Warn(": unable to order undefined symbol: ");
259
132
  else if (Sym->isShared())
260
2
    Warn(": unable to order shared symbol: ");
261
130
  else if (D && !D->Section)
262
7
    Warn(": unable to order absolute symbol: ");
263
123
  else if (D && isa<OutputSection>(D->Section))
264
2
    Warn(": unable to order synthetic symbol: ");
265
121
  else if (D && !D->Section->Repl->Live)
266
5
    Warn(": unable to order discarded symbol: ");
267
136
}
268
269
// Returns a symbol for an error message.
270
226
std::string lld::toString(const Symbol &B) {
271
226
  if (Config->Demangle)
272
218
    if (Optional<std::string> S = demangleItanium(B.getName()))
273
13
      return *S;
274
213
  return B.getName();
275
213
}