Coverage Report

Created: 2018-10-20 12:32

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