Coverage Report

Created: 2019-01-18 03:29

/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.90k
std::string lld::toString(RelType Type) {
44
1.90k
  StringRef S = getELFRelocationTypeName(elf::Config->EMachine, Type);
45
1.90k
  if (S == "Unknown")
46
1
    return ("Unknown (" + Twine(Type) + ")").str();
47
1.90k
  return S;
48
1.90k
}
49
50
2.50k
TargetInfo *elf::getTarget() {
51
2.50k
  switch (Config->EMachine) {
52
2.50k
  case EM_386:
53
130
  case EM_IAMCU:
54
130
    return getX86TargetInfo();
55
130
  case EM_AARCH64:
56
104
    return getAArch64TargetInfo();
57
130
  case EM_AMDGPU:
58
7
    return getAMDGPUTargetInfo();
59
155
  case EM_ARM:
60
155
    return getARMTargetInfo();
61
130
  case EM_AVR:
62
0
    return getAVRTargetInfo();
63
130
  case EM_HEXAGON:
64
4
    return getHexagonTargetInfo();
65
194
  case EM_MIPS:
66
194
    switch (Config->EKind) {
67
194
    case ELF32LEKind:
68
23
      return getMipsTargetInfo<ELF32LE>();
69
194
    case ELF32BEKind:
70
122
      return getMipsTargetInfo<ELF32BE>();
71
194
    case ELF64LEKind:
72
3
      return getMipsTargetInfo<ELF64LE>();
73
194
    case ELF64BEKind:
74
46
      return getMipsTargetInfo<ELF64BE>();
75
194
    default:
76
0
      llvm_unreachable("unsupported MIPS target");
77
0
    }
78
1
  case EM_MSP430:
79
1
    return getMSP430TargetInfo();
80
3
  case EM_PPC:
81
3
    return getPPCTargetInfo();
82
145
  case EM_PPC64:
83
145
    return getPPC64TargetInfo();
84
0
  case EM_RISCV:
85
0
    return getRISCVTargetInfo();
86
1
  case EM_SPARCV9:
87
1
    return getSPARCV9TargetInfo();
88
1.75k
  case EM_X86_64:
89
1.75k
    if (Config->EKind == ELF32LEKind)
90
4
      return getX32TargetInfo();
91
1.75k
    return getX86_64TargetInfo();
92
0
  }
93
0
  llvm_unreachable("unknown target machine");
94
0
}
95
96
1.86k
template <class ELFT> static ErrorPlace getErrPlace(const uint8_t *Loc) {
97
1.93k
  for (InputSectionBase *D : InputSections) {
98
1.93k
    auto *IS = cast<InputSection>(D);
99
1.93k
    if (!IS->getParent())
100
2
      continue;
101
1.93k
102
1.93k
    uint8_t *ISLoc = IS->getParent()->Loc + IS->OutSecOff;
103
1.93k
    if (ISLoc <= Loc && Loc < ISLoc + IS->getSize())
104
1.86k
      return {IS, IS->template getLocation<ELFT>(Loc - ISLoc) + ": "};
105
1.93k
  }
106
1.86k
  
return {}2
;
107
1.86k
}
Target.cpp:lld::elf::ErrorPlace getErrPlace<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char const*)
Line
Count
Source
96
3
template <class ELFT> static ErrorPlace getErrPlace(const uint8_t *Loc) {
97
3
  for (InputSectionBase *D : InputSections) {
98
3
    auto *IS = cast<InputSection>(D);
99
3
    if (!IS->getParent())
100
0
      continue;
101
3
102
3
    uint8_t *ISLoc = IS->getParent()->Loc + IS->OutSecOff;
103
3
    if (ISLoc <= Loc && Loc < ISLoc + IS->getSize())
104
3
      return {IS, IS->template getLocation<ELFT>(Loc - ISLoc) + ": "};
105
3
  }
106
3
  
return {}0
;
107
3
}
Target.cpp:lld::elf::ErrorPlace getErrPlace<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned char const*)
Line
Count
Source
96
1
template <class ELFT> static ErrorPlace getErrPlace(const uint8_t *Loc) {
97
1
  for (InputSectionBase *D : InputSections) {
98
1
    auto *IS = cast<InputSection>(D);
99
1
    if (!IS->getParent())
100
0
      continue;
101
1
102
1
    uint8_t *ISLoc = IS->getParent()->Loc + IS->OutSecOff;
103
1
    if (ISLoc <= Loc && Loc < ISLoc + IS->getSize())
104
1
      return {IS, IS->template getLocation<ELFT>(Loc - ISLoc) + ": "};
105
1
  }
106
1
  
return {}0
;
107
1
}
Target.cpp:lld::elf::ErrorPlace getErrPlace<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned char const*)
Line
Count
Source
96
42
template <class ELFT> static ErrorPlace getErrPlace(const uint8_t *Loc) {
97
107
  for (InputSectionBase *D : InputSections) {
98
107
    auto *IS = cast<InputSection>(D);
99
107
    if (!IS->getParent())
100
2
      continue;
101
105
102
105
    uint8_t *ISLoc = IS->getParent()->Loc + IS->OutSecOff;
103
105
    if (ISLoc <= Loc && Loc < ISLoc + IS->getSize())
104
40
      return {IS, IS->template getLocation<ELFT>(Loc - ISLoc) + ": "};
105
105
  }
106
42
  
return {}2
;
107
42
}
Target.cpp:lld::elf::ErrorPlace getErrPlace<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char const*)
Line
Count
Source
96
1.82k
template <class ELFT> static ErrorPlace getErrPlace(const uint8_t *Loc) {
97
1.82k
  for (InputSectionBase *D : InputSections) {
98
1.82k
    auto *IS = cast<InputSection>(D);
99
1.82k
    if (!IS->getParent())
100
0
      continue;
101
1.82k
102
1.82k
    uint8_t *ISLoc = IS->getParent()->Loc + IS->OutSecOff;
103
1.82k
    if (ISLoc <= Loc && Loc < ISLoc + IS->getSize())
104
1.82k
      return {IS, IS->template getLocation<ELFT>(Loc - ISLoc) + ": "};
105
1.82k
  }
106
1.82k
  
return {}0
;
107
1.82k
}
108
109
1.86k
ErrorPlace elf::getErrorPlace(const uint8_t *Loc) {
110
1.86k
  switch (Config->EKind) {
111
1.86k
  case ELF32LEKind:
112
3
    return getErrPlace<ELF32LE>(Loc);
113
1.86k
  case ELF32BEKind:
114
1
    return getErrPlace<ELF32BE>(Loc);
115
1.86k
  case ELF64LEKind:
116
42
    return getErrPlace<ELF64LE>(Loc);
117
1.86k
  case ELF64BEKind:
118
1.82k
    return getErrPlace<ELF64BE>(Loc);
119
1.86k
  default:
120
0
    llvm_unreachable("unknown ELF type");
121
1.86k
  }
122
1.86k
}
123
124
2.50k
TargetInfo::~TargetInfo() {}
125
126
2
int64_t TargetInfo::getImplicitAddend(const uint8_t *Buf, RelType Type) const {
127
2
  return 0;
128
2
}
129
130
175
bool TargetInfo::usesOnlyLowPageBits(RelType Type) const { return false; }
131
132
bool TargetInfo::needsThunk(RelExpr Expr, RelType Type, const InputFile *File,
133
0
                            uint64_t BranchAddr, const Symbol &S) const {
134
0
  return false;
135
0
}
136
137
bool TargetInfo::adjustPrologueForCrossSplitStack(uint8_t *Loc, uint8_t *End,
138
0
                                                  uint8_t StOther) const {
139
0
  llvm_unreachable("Target doesn't support split stacks.");
140
0
}
141
142
47
bool TargetInfo::inBranchRange(RelType Type, uint64_t Src, uint64_t Dst) const {
143
47
  return true;
144
47
}
145
146
26
void TargetInfo::writeIgotPlt(uint8_t *Buf, const Symbol &S) const {
147
26
  writeGotPlt(Buf, S);
148
26
}
149
150
RelExpr TargetInfo::adjustRelaxExpr(RelType Type, const uint8_t *Data,
151
3
                                    RelExpr Expr) const {
152
3
  return Expr;
153
3
}
154
155
0
void TargetInfo::relaxGot(uint8_t *Loc, uint64_t Val) const {
156
0
  llvm_unreachable("Should not have claimed to be relaxable");
157
0
}
158
159
void TargetInfo::relaxTlsGdToLe(uint8_t *Loc, RelType Type,
160
0
                                uint64_t Val) const {
161
0
  llvm_unreachable("Should not have claimed to be relaxable");
162
0
}
163
164
void TargetInfo::relaxTlsGdToIe(uint8_t *Loc, RelType Type,
165
0
                                uint64_t Val) const {
166
0
  llvm_unreachable("Should not have claimed to be relaxable");
167
0
}
168
169
void TargetInfo::relaxTlsIeToLe(uint8_t *Loc, RelType Type,
170
0
                                uint64_t Val) const {
171
0
  llvm_unreachable("Should not have claimed to be relaxable");
172
0
}
173
174
void TargetInfo::relaxTlsLdToLe(uint8_t *Loc, RelType Type,
175
0
                                uint64_t Val) const {
176
0
  llvm_unreachable("Should not have claimed to be relaxable");
177
0
}
178
179
2.54k
uint64_t TargetInfo::getImageBase() {
180
2.54k
  // Use -image-base if set. Fall back to the target default if not.
181
2.54k
  if (Config->ImageBase)
182
11
    return *Config->ImageBase;
183
2.53k
  return Config->Pic ? 
01.04k
:
DefaultImageBase1.48k
;
184
2.53k
}