summaryrefslogtreecommitdiffstats
path: root/lld/ELF/Symbols.cpp
blob: 212fe1d18d6478f375628739c36f9ccaff68d100 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//===- Symbols.cpp --------------------------------------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "Error.h"
#include "InputFiles.h"
#include "Symbols.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"

using namespace llvm::object;
using llvm::sys::fs::identify_magic;
using llvm::sys::fs::file_magic;

using namespace lld;
using namespace lld::elfv2;

// Returns 1, 0 or -1 if this symbol should take precedence
// over the Other, tie or lose, respectively.
template <class ELFT> int DefinedRegular<ELFT>::compare(SymbolBody *Other) {
  if (Other->kind() < kind())
    return -Other->compare(this);
  auto *R = dyn_cast<DefinedRegular>(Other);
  if (!R)
    return 1;

  // Common symbols are weaker than other types of defined symbols.
  if (isCommon() && R->isCommon())
    return (getCommonSize() < R->getCommonSize()) ? -1 : 1;
  // TODO: we are not sure if regular defined symbol and common
  // symbols are allowed to have the same name.
  if (isCommon())
    return -1;
  if (R->isCommon())
    return 1;
  return 0;
}

int DefinedBitcode::compare(SymbolBody *Other) {
  assert(Other->kind() >= kind());
  if (!isa<Defined>(Other))
    return 1;

  if (auto *B = dyn_cast<DefinedBitcode>(Other)) {
    if (!Replaceable && !B->Replaceable)
      return 0;
    // Non-replaceable symbols win.
    return Replaceable ? -1 : 1;
  }

  // As an approximation, regular symbols win over bitcode symbols,
  // but we definitely have a conflict if the regular symbol is not
  // replaceable and neither is the bitcode symbol. We do not
  // replicate the rest of the symbol resolution logic here; symbol
  // resolution will be done accurately after lowering bitcode symbols
  // to regular symbols in addCombinedLTOObject().
  if (auto *R = dyn_cast<DefinedRegular<llvm::object::ELF64LE>>(Other)) {
    if (!R->isCommon() && !Replaceable)
      return 0;
    return -1;
  }
  return 0;
}

int Defined::compare(SymbolBody *Other) {
  if (Other->kind() < kind())
    return -Other->compare(this);
  if (isa<Defined>(Other))
    return 0;
  return 1;
}

int Lazy::compare(SymbolBody *Other) {
  if (Other->kind() < kind())
    return -Other->compare(this);

  // Undefined symbols with weak aliases will turn into defined
  // symbols if they remain undefined, so we don't need to resolve
  // such symbols.
  if (auto *U = dyn_cast<Undefined>(Other))
    if (U->getWeakAlias())
      return -1;
  return 1;
}

int Undefined::compare(SymbolBody *Other) {
  if (Other->kind() < kind())
    return -Other->compare(this);
  if (cast<Undefined>(Other)->getWeakAlias())
    return -1;
  return 1;
}

template <class ELFT> StringRef DefinedRegular<ELFT>::getName() {
  // DefinedSymbol's name is read lazily for a performance reason.
  // Non-external symbol names are never used by the linker
  // except for logging or debugging.
  // Their internal references are resolved not by name but by symbol index.
  // And because they are not external, no one can refer them by name.
  // Object files contain lots of non-external symbols, and creating
  // StringRefs for them (which involves lots of strlen() on the string table)
  // is a waste of time.
  if (Name.empty())
    Name = *File->getStaticSymbolName(Sym);
  return Name;
}

ErrorOr<std::unique_ptr<InputFile>> Lazy::getMember() {
  auto MBRefOrErr = File->getMember(&Sym);
  if (auto EC = MBRefOrErr.getError())
    return EC;
  MemoryBufferRef MBRef = MBRefOrErr.get();

  // getMember returns an empty buffer if the member was already
  // read from the library.
  if (MBRef.getBuffer().empty())
    return std::unique_ptr<InputFile>(nullptr);

  file_magic Magic = identify_magic(MBRef.getBuffer());
  if (Magic == file_magic::bitcode)
    return std::unique_ptr<InputFile>(new BitcodeFile(MBRef));
  if (Magic != file_magic::elf_relocatable) {
    llvm::errs() << File->getName() << ": unknown file type\n";
    return make_error_code(LLDError::InvalidFile);
  }

  std::unique_ptr<InputFile> Obj(new ObjectFile<llvm::object::ELF64LE>(MBRef));
  Obj->setParentName(File->getName());
  return std::move(Obj);
}

template class DefinedRegular<llvm::object::ELF32LE>;
template class DefinedRegular<llvm::object::ELF32BE>;
template class DefinedRegular<llvm::object::ELF64LE>;
template class DefinedRegular<llvm::object::ELF64BE>;
OpenPOWER on IntegriCloud