Coverage Report

Created: 2019-05-19 14:56

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