Coverage Report

Created: 2019-07-24 05:18

/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
Defined *ElfSym::riscvGlobalPointer;
43
Defined *ElfSym::tlsModuleBase;
44
45
// Returns a symbol for an error message.
46
319
static std::string demangle(StringRef symName) {
47
319
  if (config->demangle)
48
310
    if (Optional<std::string> s = demangleItanium(symName))
49
19
      return *s;
50
300
  return symName;
51
300
}
52
namespace lld {
53
318
std::string toString(const Symbol &b) { return demangle(b.getName()); }
54
0
std::string toELFString(const Archive::Symbol &b) {
55
0
  return demangle(b.getName());
56
0
}
57
} // namespace lld
58
59
105k
static uint64_t getSymVA(const Symbol &sym, int64_t &addend) {
60
105k
  switch (sym.kind()) {
61
105k
  case Symbol::DefinedKind: {
62
103k
    auto &d = cast<Defined>(sym);
63
103k
    SectionBase *isec = d.section;
64
103k
65
103k
    // This is an absolute symbol.
66
103k
    if (!isec)
67
1.11k
      return d.value;
68
102k
69
102k
    assert(isec != &InputSection::discarded);
70
102k
    isec = isec->repl;
71
102k
72
102k
    uint64_t offset = d.value;
73
102k
74
102k
    // An object in an SHF_MERGE section might be referenced via a
75
102k
    // section symbol (as a hack for reducing the number of local
76
102k
    // symbols).
77
102k
    // Depending on the addend, the reference via a section symbol
78
102k
    // refers to a different object in the merge section.
79
102k
    // Since the objects in the merge section are not necessarily
80
102k
    // contiguous in the output, the addend can thus affect the final
81
102k
    // VA in a non-linear way.
82
102k
    // To make this work, we incorporate the addend into the section
83
102k
    // offset (and zero out the addend for later processing) so that
84
102k
    // we find the right object in the section.
85
102k
    if (d.isSection()) {
86
66.7k
      offset += addend;
87
66.7k
      addend = 0;
88
66.7k
    }
89
102k
90
102k
    // In the typical case, this is actually very simple and boils
91
102k
    // down to adding together 3 numbers:
92
102k
    // 1. The address of the output section.
93
102k
    // 2. The offset of the input section within the output section.
94
102k
    // 3. The offset within the input section (this addition happens
95
102k
    //    inside InputSection::getOffset).
96
102k
    //
97
102k
    // If you understand the data structures involved with this next
98
102k
    // line (and how they get built), then you have a pretty good
99
102k
    // understanding of the linker.
100
102k
    uint64_t va = isec->getVA(offset);
101
102k
102
102k
    // MIPS relocatable files can mix regular and microMIPS code.
103
102k
    // Linker needs to distinguish such code. To do so microMIPS
104
102k
    // symbols has the `STO_MIPS_MICROMIPS` flag in the `st_other`
105
102k
    // field. Unfortunately, the `MIPS::relocateOne()` method has
106
102k
    // a symbol value only. To pass type of the symbol (regular/microMIPS)
107
102k
    // to that routine as well as other places where we write
108
102k
    // a symbol value as-is (.dynamic section, `Elf_Ehdr::e_entry`
109
102k
    // field etc) do the same trick as compiler uses to mark microMIPS
110
102k
    // for CPU - set the less-significant bit.
111
102k
    if (config->emachine == EM_MIPS && 
isMicroMips()21.8k
&&
112
102k
        
(236
(sym.stOther & STO_MIPS_MICROMIPS)236
||
sym.needsPltAddr142
))
113
98
      va |= 1;
114
102k
115
102k
    if (d.isTls() && 
!config->relocatable771
) {
116
770
      // Use the address of the TLS segment's first section rather than the
117
770
      // segment's address, because segment addresses aren't initialized until
118
770
      // after sections are finalized. (e.g. Measuring the size of .rela.dyn
119
770
      // for Android relocation packing requires knowing TLS symbol addresses
120
770
      // during section finalization.)
121
770
      if (!Out::tlsPhdr || !Out::tlsPhdr->firstSec)
122
0
        fatal(toString(d.file) +
123
0
              " has an STT_TLS symbol but doesn't have an SHF_TLS section");
124
770
      return va - Out::tlsPhdr->firstSec->addr;
125
770
    }
126
101k
    return va;
127
101k
  }
128
101k
  case Symbol::SharedKind:
129
1.28k
  case Symbol::UndefinedKind:
130
1.28k
    return 0;
131
1.28k
  case Symbol::LazyArchiveKind:
132
35
  case Symbol::LazyObjectKind:
133
35
    assert(sym.isUsedInRegularObj && "lazy symbol reached writer");
134
35
    return 0;
135
35
  case Symbol::CommonKind:
136
0
    llvm_unreachable("common symbol reached writer");
137
35
  case Symbol::PlaceholderKind:
138
0
    llvm_unreachable("placeholder symbol reached writer");
139
0
  }
140
0
  llvm_unreachable("invalid symbol kind");
141
0
}
142
143
105k
uint64_t Symbol::getVA(int64_t addend) const {
144
105k
  uint64_t outVA = getSymVA(*this, addend);
145
105k
  return outVA + addend;
146
105k
}
147
148
280
uint64_t Symbol::getGotVA() const {
149
280
  if (gotInIgot)
150
12
    return in.igotPlt->getVA() + getGotPltOffset();
151
268
  return in.got->getVA() + getGotOffset();
152
268
}
153
154
566
uint64_t Symbol::getGotOffset() const { return gotIndex * config->wordsize; }
155
156
448
uint64_t Symbol::getGotPltVA() const {
157
448
  if (isInIplt)
158
66
    return in.igotPlt->getVA() + getGotPltOffset();
159
382
  return in.gotPlt->getVA() + getGotPltOffset();
160
382
}
161
162
881
uint64_t Symbol::getGotPltOffset() const {
163
881
  if (isInIplt)
164
140
    return pltIndex * config->wordsize;
165
741
  return (pltIndex + target->gotPltHeaderEntriesNum) * config->wordsize;
166
741
}
167
168
1
uint64_t Symbol::getPPC64LongBranchOffset() const {
169
1
  assert(ppc64BranchltIndex != 0xffff);
170
1
  return ppc64BranchltIndex * config->wordsize;
171
1
}
172
173
731
uint64_t Symbol::getPltVA() const {
174
731
  PltSection *plt = isInIplt ? 
in.iplt79
:
in.plt652
;
175
731
  uint64_t outVA =
176
731
      plt->getVA() + plt->headerSize + pltIndex * target->pltEntrySize;
177
731
  // While linking microMIPS code PLT code are always microMIPS
178
731
  // code. Set the less-significant bit to track that fact.
179
731
  // See detailed comment in the `getSymVA` function.
180
731
  if (config->emachine == EM_MIPS && 
isMicroMips()14
)
181
6
    outVA |= 1;
182
731
  return outVA;
183
731
}
184
185
9
uint64_t Symbol::getPPC64LongBranchTableVA() const {
186
9
  assert(ppc64BranchltIndex != 0xffff);
187
9
  return in.ppc64LongBranchTarget->getVA() +
188
9
         ppc64BranchltIndex * config->wordsize;
189
9
}
190
191
86.8k
uint64_t Symbol::getSize() const {
192
86.8k
  if (const auto *dr = dyn_cast<Defined>(this))
193
86.7k
    return dr->size;
194
53
  return cast<SharedSymbol>(this)->size;
195
53
}
196
197
152k
OutputSection *Symbol::getOutputSection() const {
198
152k
  if (auto *s = dyn_cast<Defined>(this)) {
199
152k
    if (auto *sec = s->section)
200
152k
      return sec->repl->getOutputSection();
201
728
    return nullptr;
202
728
  }
203
6
  return nullptr;
204
6
}
205
206
// If a symbol name contains '@', the characters after that is
207
// a symbol version name. This function parses that.
208
17.1k
void Symbol::parseSymbolVersion() {
209
17.1k
  StringRef s = getName();
210
17.1k
  size_t pos = s.find('@');
211
17.1k
  if (pos == 0 || pos == StringRef::npos)
212
17.1k
    return;
213
91
  StringRef verstr = s.substr(pos + 1);
214
91
  if (verstr.empty())
215
1
    return;
216
90
217
90
  // Truncate the symbol name so that it doesn't include the version string.
218
90
  nameSize = pos;
219
90
220
90
  // If this is not in this DSO, it is not a definition.
221
90
  if (!isDefined())
222
43
    return;
223
47
224
47
  // '@@' in a symbol name means the default version.
225
47
  // It is usually the most recent one.
226
47
  bool isDefault = (verstr[0] == '@');
227
47
  if (isDefault)
228
22
    verstr = verstr.substr(1);
229
47
230
61
  for (VersionDefinition &ver : config->versionDefinitions) {
231
61
    if (ver.name != verstr)
232
23
      continue;
233
38
234
38
    if (isDefault)
235
16
      versionId = ver.id;
236
22
    else
237
22
      versionId = ver.id | VERSYM_HIDDEN;
238
38
    return;
239
38
  }
240
47
241
47
  // It is an error if the specified version is not defined.
242
47
  // Usually version script is not provided when linking executable,
243
47
  // but we may still want to override a versioned symbol from DSO,
244
47
  // so we do not report error in this case. We also do not error
245
47
  // if the symbol has a local version as it won't be in the dynamic
246
47
  // symbol table.
247
47
  
if (9
config->shared9
&&
versionId != VER_NDX_LOCAL5
)
248
3
    error(toString(file) + ": symbol " + s + " has undefined version " +
249
3
          verstr);
250
9
}
251
252
119
void Symbol::fetch() const {
253
119
  if (auto *sym = dyn_cast<LazyArchive>(this)) {
254
100
    cast<ArchiveFile>(sym->file)->fetch(sym->sym);
255
100
    return;
256
100
  }
257
19
258
19
  if (auto *sym = dyn_cast<LazyObject>(this)) {
259
19
    dyn_cast<LazyObjFile>(sym->file)->fetch();
260
19
    return;
261
19
  }
262
0
263
0
  llvm_unreachable("Symbol::fetch() is called on a non-lazy symbol");
264
0
}
265
266
2
MemoryBufferRef LazyArchive::getMemberBuffer() {
267
2
  Archive::Child c =
268
2
      CHECK(sym.getMember(),
269
2
            "could not get the member for symbol " + toELFString(sym));
270
2
271
2
  return CHECK(c.getMemoryBufferRef(),
272
2
               "could not get the buffer for the member defining symbol " +
273
2
                   toELFString(sym));
274
2
}
275
276
47.1k
uint8_t Symbol::computeBinding() const {
277
47.1k
  if (config->relocatable)
278
300
    return binding;
279
46.8k
  if (visibility != STV_DEFAULT && 
visibility != STV_PROTECTED7.05k
)
280
6.85k
    return STB_LOCAL;
281
39.9k
  if (versionId == VER_NDX_LOCAL && 
isDefined()478
&&
!isPreemptible378
)
282
377
    return STB_LOCAL;
283
39.5k
  if (!config->gnuUnique && 
binding == STB_GNU_UNIQUE10
)
284
5
    return STB_GLOBAL;
285
39.5k
  return binding;
286
39.5k
}
287
288
36.9k
bool Symbol::includeInDynsym() const {
289
36.9k
  if (!config->hasDynSymTab)
290
27.1k
    return false;
291
9.73k
  if (computeBinding() == STB_LOCAL)
292
3.52k
    return false;
293
6.20k
294
6.20k
  // If a PIE binary was not linked against any shared libraries, then we can
295
6.20k
  // safely drop weak undef symbols from .dynsym.
296
6.20k
  if (isUndefWeak() && 
config->pie127
&&
sharedFiles.empty()10
)
297
4
    return false;
298
6.19k
299
6.19k
  return isUndefined() || 
isShared()5.59k
||
exportDynamic4.47k
;
300
6.19k
}
301
302
// Print out a log message for --trace-symbol.
303
62
void elf::printTraceSymbol(const Symbol *sym) {
304
62
  std::string s;
305
62
  if (sym->isUndefined())
306
19
    s = ": reference to ";
307
43
  else if (sym->isLazy())
308
2
    s = ": lazy definition of ";
309
41
  else if (sym->isShared())
310
9
    s = ": shared definition of ";
311
32
  else if (sym->isCommon())
312
5
    s = ": common definition of ";
313
27
  else
314
27
    s = ": definition of ";
315
62
316
62
  message(toString(sym->file) + s + sym->getName());
317
62
}
318
319
173
void elf::maybeWarnUnorderableSymbol(const Symbol *sym) {
320
173
  if (!config->warnSymbolOrdering)
321
7
    return;
322
166
323
166
  // If UnresolvedPolicy::Ignore is used, no "undefined symbol" error/warning
324
166
  // is emitted. It makes sense to not warn on undefined symbols.
325
166
  //
326
166
  // Note, ld.bfd --symbol-ordering-file= does not warn on undefined symbols,
327
166
  // but we don't have to be compatible here.
328
166
  if (sym->isUndefined() &&
329
166
      
config->unresolvedSymbols == UnresolvedPolicy::Ignore3
)
330
1
    return;
331
165
332
165
  const InputFile *file = sym->file;
333
165
  auto *d = dyn_cast<Defined>(sym);
334
165
335
165
  auto report = [&](StringRef s) 
{ warn(toString(file) + s + sym->getName()); }18
;
336
165
337
165
  if (sym->isUndefined())
338
2
    report(": unable to order undefined symbol: ");
339
163
  else if (sym->isShared())
340
2
    report(": unable to order shared symbol: ");
341
161
  else if (d && !d->section)
342
7
    report(": unable to order absolute symbol: ");
343
154
  else if (d && isa<OutputSection>(d->section))
344
2
    report(": unable to order synthetic symbol: ");
345
152
  else if (d && !d->section->repl->isLive())
346
5
    report(": unable to order discarded symbol: ");
347
165
}
348
349
20.3k
static uint8_t getMinVisibility(uint8_t va, uint8_t vb) {
350
20.3k
  if (va == STV_DEFAULT)
351
20.2k
    return vb;
352
85
  if (vb == STV_DEFAULT)
353
38
    return va;
354
47
  return std::min(va, vb);
355
47
}
356
357
// Merge symbol properties.
358
//
359
// When we have many symbols of the same name, we choose one of them,
360
// and that's the result of symbol resolution. However, symbols that
361
// were not chosen still affect some symbol properties.
362
21.2k
void Symbol::mergeProperties(const Symbol &other) {
363
21.2k
  if (other.exportDynamic)
364
4.59k
    exportDynamic = true;
365
21.2k
  if (other.isUsedInRegularObj)
366
19.6k
    isUsedInRegularObj = true;
367
21.2k
368
21.2k
  // DSO symbols do not affect visibility in the output.
369
21.2k
  if (!other.isShared())
370
20.3k
    visibility = getMinVisibility(visibility, other.visibility);
371
21.2k
}
372
373
20.4k
void Symbol::resolve(const Symbol &other) {
374
20.4k
  mergeProperties(other);
375
20.4k
376
20.4k
  if (isPlaceholder()) {
377
18.3k
    replace(other);
378
18.3k
    return;
379
18.3k
  }
380
2.04k
381
2.04k
  switch (other.kind()) {
382
2.04k
  case Symbol::UndefinedKind:
383
446
    resolveUndefined(cast<Undefined>(other));
384
446
    break;
385
2.04k
  case Symbol::CommonKind:
386
31
    resolveCommon(cast<CommonSymbol>(other));
387
31
    break;
388
2.04k
  case Symbol::DefinedKind:
389
1.00k
    resolveDefined(cast<Defined>(other));
390
1.00k
    break;
391
2.04k
  case Symbol::LazyArchiveKind:
392
101
    resolveLazy(cast<LazyArchive>(other));
393
101
    break;
394
2.04k
  case Symbol::LazyObjectKind:
395
11
    resolveLazy(cast<LazyObject>(other));
396
11
    break;
397
2.04k
  case Symbol::SharedKind:
398
455
    resolveShared(cast<SharedSymbol>(other));
399
455
    break;
400
2.04k
  case Symbol::PlaceholderKind:
401
0
    llvm_unreachable("bad symbol kind");
402
2.04k
  }
403
2.04k
}
404
405
446
void Symbol::resolveUndefined(const Undefined &other) {
406
446
  // An undefined symbol with non default visibility must be satisfied
407
446
  // in the same DSO.
408
446
  //
409
446
  // If this is a non-weak defined symbol in a discarded section, override the
410
446
  // existing undefined symbol for better error message later.
411
446
  if ((isShared() && 
other.visibility != STV_DEFAULT55
) ||
412
446
      
(444
isUndefined()444
&&
other.binding != STB_WEAK92
&&
other.discardedSecIdx89
)) {
413
2
    replace(other);
414
2
    return;
415
2
  }
416
444
417
444
  if (traced)
418
1
    printTraceSymbol(&other);
419
444
420
444
  if (isLazy()) {
421
31
    // An undefined weak will not fetch archive members. See comment on Lazy in
422
31
    // Symbols.h for the details.
423
31
    if (other.binding == STB_WEAK) {
424
3
      binding = STB_WEAK;
425
3
      type = other.type;
426
3
      return;
427
3
    }
428
28
429
28
    // Do extra check for --warn-backrefs.
430
28
    //
431
28
    // --warn-backrefs is an option to prevent an undefined reference from
432
28
    // fetching an archive member written earlier in the command line. It can be
433
28
    // used to keep compatibility with GNU linkers to some degree.
434
28
    // I'll explain the feature and why you may find it useful in this comment.
435
28
    //
436
28
    // lld's symbol resolution semantics is more relaxed than traditional Unix
437
28
    // linkers. For example,
438
28
    //
439
28
    //   ld.lld foo.a bar.o
440
28
    //
441
28
    // succeeds even if bar.o contains an undefined symbol that has to be
442
28
    // resolved by some object file in foo.a. Traditional Unix linkers don't
443
28
    // allow this kind of backward reference, as they visit each file only once
444
28
    // from left to right in the command line while resolving all undefined
445
28
    // symbols at the moment of visiting.
446
28
    //
447
28
    // In the above case, since there's no undefined symbol when a linker visits
448
28
    // foo.a, no files are pulled out from foo.a, and because the linker forgets
449
28
    // about foo.a after visiting, it can't resolve undefined symbols in bar.o
450
28
    // that could have been resolved otherwise.
451
28
    //
452
28
    // That lld accepts more relaxed form means that (besides it'd make more
453
28
    // sense) you can accidentally write a command line or a build file that
454
28
    // works only with lld, even if you have a plan to distribute it to wider
455
28
    // users who may be using GNU linkers. With --warn-backrefs, you can detect
456
28
    // a library order that doesn't work with other Unix linkers.
457
28
    //
458
28
    // The option is also useful to detect cyclic dependencies between static
459
28
    // archives. Again, lld accepts
460
28
    //
461
28
    //   ld.lld foo.a bar.a
462
28
    //
463
28
    // even if foo.a and bar.a depend on each other. With --warn-backrefs, it is
464
28
    // handled as an error.
465
28
    //
466
28
    // Here is how the option works. We assign a group ID to each file. A file
467
28
    // with a smaller group ID can pull out object files from an archive file
468
28
    // with an equal or greater group ID. Otherwise, it is a reverse dependency
469
28
    // and an error.
470
28
    //
471
28
    // A file outside --{start,end}-group gets a fresh ID when instantiated. All
472
28
    // files within the same --{start,end}-group get the same group ID. E.g.
473
28
    //
474
28
    //   ld.lld A B --start-group C D --end-group E
475
28
    //
476
28
    // A forms group 0. B form group 1. C and D (including their member object
477
28
    // files) form group 2. E forms group 3. I think that you can see how this
478
28
    // group assignment rule simulates the traditional linker's semantics.
479
28
    bool backref = config->warnBackrefs && 
other.file10
&&
480
28
                   
file->groupId < other.file->groupId10
;
481
28
    fetch();
482
28
483
28
    // We don't report backward references to weak symbols as they can be
484
28
    // overridden later.
485
28
    if (backref && 
!isWeak()5
)
486
4
      warn("backward reference detected: " + other.getName() + " in " +
487
4
           toString(other.file) + " refers to " + toString(file));
488
28
    return;
489
28
  }
490
413
491
413
  // Undefined symbols in a SharedFile do not change the binding.
492
413
  if (dyn_cast_or_null<SharedFile>(other.file))
493
13
    return;
494
400
495
400
  if (isUndefined()) {
496
91
    // The binding may "upgrade" from weak to non-weak.
497
91
    if (other.binding != STB_WEAK)
498
88
      binding = other.binding;
499
309
  } else if (auto *s = dyn_cast<SharedSymbol>(this)) {
500
48
    // The binding of a SharedSymbol will be weak if there is at least one
501
48
    // reference and all are weak. The binding has one opportunity to change to
502
48
    // weak: if the first reference is weak.
503
48
    if (other.binding != STB_WEAK || 
!s->referenced7
)
504
45
      binding = other.binding;
505
48
    s->referenced = true;
506
48
  }
507
400
}
508
509
// Using .symver foo,foo@@VER unfortunately creates two symbols: foo and
510
// foo@@VER. We want to effectively ignore foo, so give precedence to
511
// foo@@VER.
512
// FIXME: If users can transition to using
513
// .symver foo,foo@@@VER
514
// we can delete this hack.
515
101
static int compareVersion(StringRef a, StringRef b) {
516
101
  bool x = a.contains("@@");
517
101
  bool y = b.contains("@@");
518
101
  if (!x && 
y100
)
519
1
    return 1;
520
100
  if (x && 
!y1
)
521
1
    return -1;
522
99
  return 0;
523
99
}
524
525
// Compare two symbols. Return 1 if the new symbol should win, -1 if
526
// the new symbol should lose, or 0 if there is a conflict.
527
1.03k
int Symbol::compare(const Symbol *other) const {
528
1.03k
  assert(other->isDefined() || other->isCommon());
529
1.03k
530
1.03k
  if (!isDefined() && 
!isCommon()954
)
531
932
    return 1;
532
101
533
101
  if (int cmp = compareVersion(getName(), other->getName()))
534
2
    return cmp;
535
99
536
99
  if (other->isWeak())
537
18
    return -1;
538
81
539
81
  if (isWeak())
540
23
    return 1;
541
58
542
58
  if (isCommon() && 
other->isCommon()16
) {
543
14
    if (config->warnCommon)
544
3
      warn("multiple common of " + getName());
545
14
    return 0;
546
14
  }
547
44
548
44
  if (isCommon()) {
549
2
    if (config->warnCommon)
550
1
      warn("common " + getName() + " is overridden");
551
2
    return 1;
552
2
  }
553
42
554
42
  if (other->isCommon()) {
555
2
    if (config->warnCommon)
556
1
      warn("common " + getName() + " is overridden");
557
2
    return -1;
558
2
  }
559
40
560
40
  auto *oldSym = cast<Defined>(this);
561
40
  auto *newSym = cast<Defined>(other);
562
40
563
40
  if (other->file && 
isa<BitcodeFile>(other->file)35
)
564
1
    return 0;
565
39
566
39
  if (!oldSym->section && 
!newSym->section5
&&
oldSym->value == newSym->value5
&&
567
39
      
newSym->binding == STB_GLOBAL4
)
568
4
    return -1;
569
35
570
35
  return 0;
571
35
}
572
573
static void reportDuplicate(Symbol *sym, InputFile *newFile,
574
36
                            InputSectionBase *errSec, uint64_t errOffset) {
575
36
  if (config->allowMultipleDefinition)
576
4
    return;
577
32
578
32
  Defined *d = cast<Defined>(sym);
579
32
  if (!d->section || 
!errSec30
) {
580
2
    error("duplicate symbol: " + toString(*sym) + "\n>>> defined in " +
581
2
          toString(sym->file) + "\n>>> defined in " + toString(newFile));
582
2
    return;
583
2
  }
584
30
585
30
  // Construct and print an error message in the form of:
586
30
  //
587
30
  //   ld.lld: error: duplicate symbol: foo
588
30
  //   >>> defined at bar.c:30
589
30
  //   >>>            bar.o (/home/alice/src/bar.o)
590
30
  //   >>> defined at baz.c:563
591
30
  //   >>>            baz.o in archive libbaz.a
592
30
  auto *sec1 = cast<InputSectionBase>(d->section);
593
30
  std::string src1 = sec1->getSrcMsg(*sym, d->value);
594
30
  std::string obj1 = sec1->getObjMsg(d->value);
595
30
  std::string src2 = errSec->getSrcMsg(*sym, errOffset);
596
30
  std::string obj2 = errSec->getObjMsg(errOffset);
597
30
598
30
  std::string msg = "duplicate symbol: " + toString(*sym) + "\n>>> defined at ";
599
30
  if (!src1.empty())
600
12
    msg += src1 + "\n>>>            ";
601
30
  msg += obj1 + "\n>>> defined at ";
602
30
  if (!src2.empty())
603
12
    msg += src2 + "\n>>>            ";
604
30
  msg += obj2;
605
30
  error(msg);
606
30
}
607
608
31
void Symbol::resolveCommon(const CommonSymbol &other) {
609
31
  int cmp = compare(&other);
610
31
  if (cmp < 0)
611
6
    return;
612
25
613
25
  if (cmp > 0) {
614
11
    replace(other);
615
11
    return;
616
11
  }
617
14
618
14
  CommonSymbol *oldSym = cast<CommonSymbol>(this);
619
14
620
14
  oldSym->alignment = std::max(oldSym->alignment, other.alignment);
621
14
  if (oldSym->size < other.size) {
622
12
    oldSym->file = other.file;
623
12
    oldSym->size = other.size;
624
12
  }
625
14
}
626
627
1.00k
void Symbol::resolveDefined(const Defined &other) {
628
1.00k
  int cmp = compare(&other);
629
1.00k
  if (cmp > 0)
630
947
    replace(other);
631
55
  else if (cmp == 0)
632
36
    reportDuplicate(this, other.file,
633
36
                    dyn_cast_or_null<InputSectionBase>(other.section),
634
36
                    other.value);
635
1.00k
}
636
637
112
template <class LazyT> void Symbol::resolveLazy(const LazyT &other) {
638
112
  if (!isUndefined())
639
32
    return;
640
80
641
80
  // An undefined weak will not fetch archive members. See comment on Lazy in
642
80
  // Symbols.h for the details.
643
80
  if (isWeak()) {
644
15
    uint8_t ty = type;
645
15
    replace(other);
646
15
    type = ty;
647
15
    binding = STB_WEAK;
648
15
    return;
649
15
  }
650
65
651
65
  other.fetch();
652
65
}
void lld::elf::Symbol::resolveLazy<lld::elf::LazyArchive>(lld::elf::LazyArchive const&)
Line
Count
Source
637
101
template <class LazyT> void Symbol::resolveLazy(const LazyT &other) {
638
101
  if (!isUndefined())
639
32
    return;
640
69
641
69
  // An undefined weak will not fetch archive members. See comment on Lazy in
642
69
  // Symbols.h for the details.
643
69
  if (isWeak()) {
644
14
    uint8_t ty = type;
645
14
    replace(other);
646
14
    type = ty;
647
14
    binding = STB_WEAK;
648
14
    return;
649
14
  }
650
55
651
55
  other.fetch();
652
55
}
void lld::elf::Symbol::resolveLazy<lld::elf::LazyObject>(lld::elf::LazyObject const&)
Line
Count
Source
637
11
template <class LazyT> void Symbol::resolveLazy(const LazyT &other) {
638
11
  if (!isUndefined())
639
0
    return;
640
11
641
11
  // An undefined weak will not fetch archive members. See comment on Lazy in
642
11
  // Symbols.h for the details.
643
11
  if (isWeak()) {
644
1
    uint8_t ty = type;
645
1
    replace(other);
646
1
    type = ty;
647
1
    binding = STB_WEAK;
648
1
    return;
649
1
  }
650
10
651
10
  other.fetch();
652
10
}
653
654
455
void Symbol::resolveShared(const SharedSymbol &other) {
655
455
  if (visibility == STV_DEFAULT && 
(450
isUndefined()450
||
isLazy()45
)) {
656
407
    // An undefined symbol with non default visibility must be satisfied
657
407
    // in the same DSO.
658
407
    uint8_t bind = binding;
659
407
    replace(other);
660
407
    binding = bind;
661
407
    cast<SharedSymbol>(this)->referenced = true;
662
407
  }
663
455
}