Coverage Report

Created: 2018-06-25 02:00

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/Target.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Target.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
// Machine-specific things, such as applying relocations, creation of
11
// GOT or PLT entries, etc., are handled in this file.
12
//
13
// Refer the ELF spec for the single letter variables, S, A or P, used
14
// in this file.
15
//
16
// Some functions defined in this file has "relaxTls" as part of their names.
17
// They do peephole optimization for TLS variables by rewriting instructions.
18
// They are not part of the ABI but optional optimization, so you can skip
19
// them if you are not interested in how TLS variables are optimized.
20
// See the following paper for the details.
21
//
22
//   Ulrich Drepper, ELF Handling For Thread-Local Storage
23
//   http://www.akkadia.org/drepper/tls.pdf
24
//
25
//===----------------------------------------------------------------------===//
26
27
#include "Target.h"
28
#include "InputFiles.h"
29
#include "OutputSections.h"
30
#include "SymbolTable.h"
31
#include "Symbols.h"
32
#include "lld/Common/ErrorHandler.h"
33
#include "llvm/Object/ELF.h"
34
35
using namespace llvm;
36
using namespace llvm::object;
37
using namespace llvm::ELF;
38
using namespace lld;
39
using namespace lld::elf;
40
41
TargetInfo *elf::Target;
42
43
1.88k
std::string lld::toString(RelType Type) {
44
1.88k
  StringRef S = getELFRelocationTypeName(elf::Config->EMachine, Type);
45
1.88k
  if (S == "Unknown")
46
1
    return ("Unknown (" + Twine(Type) + ")").str();
47
1.88k
  return S;
48
1.88k
}
49
50
2.21k
TargetInfo *elf::getTarget() {
51
2.21k
  switch (Config->EMachine) {
52
2.21k
  case EM_386:
53
123
  case EM_IAMCU:
54
123
    return getX86TargetInfo();
55
123
  case EM_AARCH64:
56
90
    return getAArch64TargetInfo();
57
123
  case EM_AMDGPU:
58
5
    return getAMDGPUTargetInfo();
59
123
  case EM_ARM:
60
120
    return getARMTargetInfo();
61
123
  case EM_AVR:
62
0
    return getAVRTargetInfo();
63
123
  case EM_HEXAGON:
64
1
    return getHexagonTargetInfo();
65
175
  case EM_MIPS:
66
175
    switch (Config->EKind) {
67
175
    case ELF32LEKind:
68
17
      return getMipsTargetInfo<ELF32LE>();
69
175
    case ELF32BEKind:
70
118
      return getMipsTargetInfo<ELF32BE>();
71
175
    case ELF64LEKind:
72
2
      return getMipsTargetInfo<ELF64LE>();
73
175
    case ELF64BEKind:
74
38
      return getMipsTargetInfo<ELF64BE>();
75
175
    default:
76
0
      fatal("unsupported MIPS target");
77
0
    }
78
3
  case EM_PPC:
79
3
    return getPPCTargetInfo();
80
76
  case EM_PPC64:
81
76
    return getPPC64TargetInfo();
82
1
  case EM_SPARCV9:
83
1
    return getSPARCV9TargetInfo();
84
1.62k
  case EM_X86_64:
85
1.62k
    if (Config->EKind == ELF32LEKind)
86
3
      return getX32TargetInfo();
87
1.61k
    return getX86_64TargetInfo();
88
0
  }
89
0
  fatal("unknown target machine");
90
0
}
91
92
1.84k
template <class ELFT> static ErrorPlace getErrPlace(const uint8_t *Loc) {
93
1.90k
  for (InputSectionBase *D : InputSections) {
94
1.90k
    auto *IS = dyn_cast<InputSection>(D);
95
1.90k
    if (!IS || !IS->getParent())
96
0
      continue;
97
1.90k
98
1.90k
    uint8_t *ISLoc = IS->getParent()->Loc + IS->OutSecOff;
99
1.90k
    if (ISLoc <= Loc && Loc < ISLoc + IS->getSize())
100
1.84k
      return {IS, IS->template getLocation<ELFT>(Loc - ISLoc) + ": "};
101
1.90k
  }
102
1.84k
  
return {}2
;
103
1.84k
}
Target.cpp:lld::elf::ErrorPlace getErrPlace<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char const*)
Line
Count
Source
92
3
template <class ELFT> static ErrorPlace getErrPlace(const uint8_t *Loc) {
93
3
  for (InputSectionBase *D : InputSections) {
94
3
    auto *IS = dyn_cast<InputSection>(D);
95
3
    if (!IS || !IS->getParent())
96
0
      continue;
97
3
98
3
    uint8_t *ISLoc = IS->getParent()->Loc + IS->OutSecOff;
99
3
    if (ISLoc <= Loc && Loc < ISLoc + IS->getSize())
100
3
      return {IS, IS->template getLocation<ELFT>(Loc - ISLoc) + ": "};
101
3
  }
102
3
  
return {}0
;
103
3
}
Target.cpp:lld::elf::ErrorPlace getErrPlace<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned char const*)
Line
Count
Source
92
1
template <class ELFT> static ErrorPlace getErrPlace(const uint8_t *Loc) {
93
1
  for (InputSectionBase *D : InputSections) {
94
1
    auto *IS = dyn_cast<InputSection>(D);
95
1
    if (!IS || !IS->getParent())
96
0
      continue;
97
1
98
1
    uint8_t *ISLoc = IS->getParent()->Loc + IS->OutSecOff;
99
1
    if (ISLoc <= Loc && Loc < ISLoc + IS->getSize())
100
1
      return {IS, IS->template getLocation<ELFT>(Loc - ISLoc) + ": "};
101
1
  }
102
1
  
return {}0
;
103
1
}
Target.cpp:lld::elf::ErrorPlace getErrPlace<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned char const*)
Line
Count
Source
92
31
template <class ELFT> static ErrorPlace getErrPlace(const uint8_t *Loc) {
93
87
  for (InputSectionBase *D : InputSections) {
94
87
    auto *IS = dyn_cast<InputSection>(D);
95
87
    if (!IS || !IS->getParent())
96
0
      continue;
97
87
98
87
    uint8_t *ISLoc = IS->getParent()->Loc + IS->OutSecOff;
99
87
    if (ISLoc <= Loc && Loc < ISLoc + IS->getSize())
100
29
      return {IS, IS->template getLocation<ELFT>(Loc - ISLoc) + ": "};
101
87
  }
102
31
  
return {}2
;
103
31
}
Target.cpp:lld::elf::ErrorPlace getErrPlace<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char const*)
Line
Count
Source
92
1.81k
template <class ELFT> static ErrorPlace getErrPlace(const uint8_t *Loc) {
93
1.81k
  for (InputSectionBase *D : InputSections) {
94
1.81k
    auto *IS = dyn_cast<InputSection>(D);
95
1.81k
    if (!IS || !IS->getParent())
96
0
      continue;
97
1.81k
98
1.81k
    uint8_t *ISLoc = IS->getParent()->Loc + IS->OutSecOff;
99
1.81k
    if (ISLoc <= Loc && Loc < ISLoc + IS->getSize())
100
1.81k
      return {IS, IS->template getLocation<ELFT>(Loc - ISLoc) + ": "};
101
1.81k
  }
102
1.81k
  
return {}0
;
103
1.81k
}
104
105
1.84k
ErrorPlace elf::getErrorPlace(const uint8_t *Loc) {
106
1.84k
  switch (Config->EKind) {
107
1.84k
  case ELF32LEKind:
108
3
    return getErrPlace<ELF32LE>(Loc);
109
1.84k
  case ELF32BEKind:
110
1
    return getErrPlace<ELF32BE>(Loc);
111
1.84k
  case ELF64LEKind:
112
31
    return getErrPlace<ELF64LE>(Loc);
113
1.84k
  case ELF64BEKind:
114
1.81k
    return getErrPlace<ELF64BE>(Loc);
115
1.84k
  default:
116
0
    llvm_unreachable("unknown ELF type");
117
1.84k
  }
118
1.84k
}
119
120
2.21k
TargetInfo::~TargetInfo() {}
121
122
2
int64_t TargetInfo::getImplicitAddend(const uint8_t *Buf, RelType Type) const {
123
2
  return 0;
124
2
}
125
126
114
bool TargetInfo::usesOnlyLowPageBits(RelType Type) const { return false; }
127
128
bool TargetInfo::needsThunk(RelExpr Expr, RelType Type, const InputFile *File,
129
0
                            uint64_t BranchAddr, const Symbol &S) const {
130
0
  return false;
131
0
}
132
133
103
bool TargetInfo::inBranchRange(RelType Type, uint64_t Src, uint64_t Dst) const {
134
103
  return true;
135
103
}
136
137
18
void TargetInfo::writeIgotPlt(uint8_t *Buf, const Symbol &S) const {
138
18
  writeGotPlt(Buf, S);
139
18
}
140
141
RelExpr TargetInfo::adjustRelaxExpr(RelType Type, const uint8_t *Data,
142
3
                                    RelExpr Expr) const {
143
3
  return Expr;
144
3
}
145
146
0
void TargetInfo::relaxGot(uint8_t *Loc, uint64_t Val) const {
147
0
  llvm_unreachable("Should not have claimed to be relaxable");
148
0
}
149
150
void TargetInfo::relaxTlsGdToLe(uint8_t *Loc, RelType Type,
151
0
                                uint64_t Val) const {
152
0
  llvm_unreachable("Should not have claimed to be relaxable");
153
0
}
154
155
void TargetInfo::relaxTlsGdToIe(uint8_t *Loc, RelType Type,
156
0
                                uint64_t Val) const {
157
0
  llvm_unreachable("Should not have claimed to be relaxable");
158
0
}
159
160
void TargetInfo::relaxTlsIeToLe(uint8_t *Loc, RelType Type,
161
0
                                uint64_t Val) const {
162
0
  llvm_unreachable("Should not have claimed to be relaxable");
163
0
}
164
165
void TargetInfo::relaxTlsLdToLe(uint8_t *Loc, RelType Type,
166
0
                                uint64_t Val) const {
167
0
  llvm_unreachable("Should not have claimed to be relaxable");
168
0
}
169
170
2.18k
uint64_t TargetInfo::getImageBase() {
171
2.18k
  // Use -image-base if set. Fall back to the target default if not.
172
2.18k
  if (Config->ImageBase)
173
11
    return *Config->ImageBase;
174
2.17k
  return Config->Pic ? 
0947
:
DefaultImageBase1.22k
;
175
2.17k
}