Coverage Report

Created: 2018-06-25 02:00

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/Target.h
Line
Count
Source (jump to first uncovered line)
1
//===- Target.h -------------------------------------------------*- C++ -*-===//
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
#ifndef LLD_ELF_TARGET_H
11
#define LLD_ELF_TARGET_H
12
13
#include "InputSection.h"
14
#include "lld/Common/ErrorHandler.h"
15
#include "llvm/Object/ELF.h"
16
17
namespace lld {
18
std::string toString(elf::RelType Type);
19
20
namespace elf {
21
class Defined;
22
class InputFile;
23
class Symbol;
24
25
class TargetInfo {
26
public:
27
1.76k
  virtual uint32_t calcEFlags() const { return 0; }
28
12
  virtual RelType getDynRel(RelType Type) const { return Type; }
29
42
  virtual void writeGotPltHeader(uint8_t *Buf) const {}
30
101
  virtual void writeGotHeader(uint8_t *Buf) const {}
31
0
  virtual void writeGotPlt(uint8_t *Buf, const Symbol &S) const {};
32
  virtual void writeIgotPlt(uint8_t *Buf, const Symbol &S) const;
33
  virtual int64_t getImplicitAddend(const uint8_t *Buf, RelType Type) const;
34
35
  // If lazy binding is supported, the first entry of the PLT has code
36
  // to call the dynamic linker to resolve PLT entries the first time
37
  // they are called. This function writes that code.
38
0
  virtual void writePltHeader(uint8_t *Buf) const {}
39
40
  virtual void writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
41
                        uint64_t PltEntryAddr, int32_t Index,
42
0
                        unsigned RelOff) const {}
43
185
  virtual void addPltHeaderSymbols(InputSection &IS) const {}
44
262
  virtual void addPltSymbols(InputSection &IS, uint64_t Off) const {}
45
46
753
  unsigned getPltEntryOffset(unsigned Index) const {
47
753
    return Index * PltEntrySize + PltHeaderSize;
48
753
  }
49
50
  // Returns true if a relocation only uses the low bits of a value such that
51
  // all those bits are in the same page. For example, if the relocation
52
  // only uses the low 12 bits in a system with 4k pages. If this is true, the
53
  // bits will always have the same value at runtime and we don't have to emit
54
  // a dynamic relocation.
55
  virtual bool usesOnlyLowPageBits(RelType Type) const;
56
57
  // Decide whether a Thunk is needed for the relocation from File
58
  // targeting S.
59
  virtual bool needsThunk(RelExpr Expr, RelType RelocType,
60
                          const InputFile *File, uint64_t BranchAddr,
61
                          const Symbol &S) const;
62
  // Return true if we can reach Dst from Src with Relocation RelocType
63
  virtual bool inBranchRange(RelType Type, uint64_t Src,
64
                             uint64_t Dst) const;
65
  virtual RelExpr getRelExpr(RelType Type, const Symbol &S,
66
                             const uint8_t *Loc) const = 0;
67
68
  virtual void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const = 0;
69
70
  virtual ~TargetInfo();
71
72
  unsigned TlsGdRelaxSkip = 1;
73
  unsigned PageSize = 4096;
74
  unsigned DefaultMaxPageSize = 4096;
75
76
  uint64_t getImageBase();
77
78
  // Offset of _GLOBAL_OFFSET_TABLE_ from base of .got or .got.plt section.
79
  uint64_t GotBaseSymOff = 0;
80
  // True if _GLOBAL_OFFSET_TABLE_ is relative to .got.plt, false if .got.
81
  bool GotBaseSymInGotPlt = true;
82
83
  // On systems with range extensions we place collections of Thunks at
84
  // regular spacings that enable the majority of branches reach the Thunks.
85
  uint32_t ThunkSectionSpacing = 0;
86
87
  RelType CopyRel;
88
  RelType GotRel;
89
  RelType PltRel;
90
  RelType RelativeRel;
91
  RelType IRelativeRel;
92
  RelType TlsDescRel;
93
  RelType TlsGotRel;
94
  RelType TlsModuleIndexRel;
95
  RelType TlsOffsetRel;
96
  unsigned GotEntrySize = 0;
97
  unsigned GotPltEntrySize = 0;
98
  unsigned PltEntrySize;
99
  unsigned PltHeaderSize;
100
101
  // At least on x86_64 positions 1 and 2 are used by the first plt entry
102
  // to support lazy loading.
103
  unsigned GotPltHeaderEntriesNum = 3;
104
105
  // On PPC ELF V2 abi, the first entry in the .got is the .TOC.
106
  unsigned GotHeaderEntriesNum = 0;
107
108
  // For TLS variant 1, the TCB is a fixed size specified by the Target.
109
  // For variant 2, the TCB is an unspecified size.
110
  // Set to 0 for variant 2.
111
  unsigned TcbSize = 0;
112
113
  // Set to the offset (in bytes) that the thread pointer is initialized to
114
  // point to, relative to the start of the thread local storage.
115
  unsigned TlsTpOffset = 0;
116
117
  bool NeedsThunks = false;
118
119
  // A 4-byte field corresponding to one or more trap instructions, used to pad
120
  // executable OutputSections.
121
  uint32_t TrapInstr = 0;
122
123
  virtual RelExpr adjustRelaxExpr(RelType Type, const uint8_t *Data,
124
                                  RelExpr Expr) const;
125
  virtual void relaxGot(uint8_t *Loc, uint64_t Val) const;
126
  virtual void relaxTlsGdToIe(uint8_t *Loc, RelType Type, uint64_t Val) const;
127
  virtual void relaxTlsGdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const;
128
  virtual void relaxTlsIeToLe(uint8_t *Loc, RelType Type, uint64_t Val) const;
129
  virtual void relaxTlsLdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const;
130
131
protected:
132
  // On FreeBSD x86_64 the first page cannot be mmaped.
133
  // On Linux that is controled by vm.mmap_min_addr. At least on some x86_64
134
  // installs that is 65536, so the first 15 pages cannot be used.
135
  // Given that, the smallest value that can be used in here is 0x10000.
136
  uint64_t DefaultImageBase = 0x10000;
137
};
138
139
TargetInfo *getAArch64TargetInfo();
140
TargetInfo *getAMDGPUTargetInfo();
141
TargetInfo *getARMTargetInfo();
142
TargetInfo *getAVRTargetInfo();
143
TargetInfo *getHexagonTargetInfo();
144
TargetInfo *getPPC64TargetInfo();
145
TargetInfo *getPPCTargetInfo();
146
TargetInfo *getSPARCV9TargetInfo();
147
TargetInfo *getX32TargetInfo();
148
TargetInfo *getX86TargetInfo();
149
TargetInfo *getX86_64TargetInfo();
150
template <class ELFT> TargetInfo *getMipsTargetInfo();
151
152
struct ErrorPlace {
153
  InputSectionBase *IS;
154
  std::string Loc;
155
};
156
157
// Returns input section and corresponding source string for the given location.
158
ErrorPlace getErrorPlace(const uint8_t *Loc);
159
160
14
static inline std::string getErrorLocation(const uint8_t *Loc) {
161
14
  return getErrorPlace(Loc).Loc;
162
14
}
Unexecuted instantiation: AArch64ErrataFix.cpp:lld::elf::getErrorLocation(unsigned char const*)
AArch64.cpp:lld::elf::getErrorLocation(unsigned char const*)
Line
Count
Source
160
6
static inline std::string getErrorLocation(const uint8_t *Loc) {
161
6
  return getErrorPlace(Loc).Loc;
162
6
}
Unexecuted instantiation: AMDGPU.cpp:lld::elf::getErrorLocation(unsigned char const*)
Unexecuted instantiation: ARM.cpp:lld::elf::getErrorLocation(unsigned char const*)
Unexecuted instantiation: AVR.cpp:lld::elf::getErrorLocation(unsigned char const*)
Unexecuted instantiation: Hexagon.cpp:lld::elf::getErrorLocation(unsigned char const*)
Unexecuted instantiation: Mips.cpp:lld::elf::getErrorLocation(unsigned char const*)
Unexecuted instantiation: PPC.cpp:lld::elf::getErrorLocation(unsigned char const*)
Unexecuted instantiation: PPC64.cpp:lld::elf::getErrorLocation(unsigned char const*)
Unexecuted instantiation: SPARCV9.cpp:lld::elf::getErrorLocation(unsigned char const*)
Unexecuted instantiation: X86.cpp:lld::elf::getErrorLocation(unsigned char const*)
X86_64.cpp:lld::elf::getErrorLocation(unsigned char const*)
Line
Count
Source
160
4
static inline std::string getErrorLocation(const uint8_t *Loc) {
161
4
  return getErrorPlace(Loc).Loc;
162
4
}
Unexecuted instantiation: Driver.cpp:lld::elf::getErrorLocation(unsigned char const*)
Unexecuted instantiation: EhFrame.cpp:lld::elf::getErrorLocation(unsigned char const*)
InputSection.cpp:lld::elf::getErrorLocation(unsigned char const*)
Line
Count
Source
160
4
static inline std::string getErrorLocation(const uint8_t *Loc) {
161
4
  return getErrorPlace(Loc).Loc;
162
4
}
Unexecuted instantiation: LinkerScript.cpp:lld::elf::getErrorLocation(unsigned char const*)
Unexecuted instantiation: MarkLive.cpp:lld::elf::getErrorLocation(unsigned char const*)
Unexecuted instantiation: OutputSections.cpp:lld::elf::getErrorLocation(unsigned char const*)
Unexecuted instantiation: Relocations.cpp:lld::elf::getErrorLocation(unsigned char const*)
Unexecuted instantiation: ScriptParser.cpp:lld::elf::getErrorLocation(unsigned char const*)
Unexecuted instantiation: Symbols.cpp:lld::elf::getErrorLocation(unsigned char const*)
Unexecuted instantiation: SyntheticSections.cpp:lld::elf::getErrorLocation(unsigned char const*)
Unexecuted instantiation: Target.cpp:lld::elf::getErrorLocation(unsigned char const*)
Unexecuted instantiation: Thunks.cpp:lld::elf::getErrorLocation(unsigned char const*)
Unexecuted instantiation: Writer.cpp:lld::elf::getErrorLocation(unsigned char const*)
163
164
uint64_t getPPC64TocBase();
165
uint64_t getAArch64Page(uint64_t Expr);
166
167
extern TargetInfo *Target;
168
TargetInfo *getTarget();
169
170
template <class ELFT> bool isMipsPIC(const Defined *Sym);
171
172
static inline void reportRangeError(uint8_t *Loc, RelType Type, const Twine &V,
173
1.83k
                                    int64_t Min, uint64_t Max) {
174
1.83k
  ErrorPlace ErrPlace = getErrorPlace(Loc);
175
1.83k
  StringRef Hint;
176
1.83k
  if (ErrPlace.IS && ErrPlace.IS->Name.startswith(".debug"))
177
1
    Hint = "; consider recompiling with -fdebug-types-section to reduce size "
178
1
           "of debug sections";
179
1.83k
180
1.83k
  error(ErrPlace.Loc + "relocation " + lld::toString(Type) +
181
1.83k
        " out of range: " + V.str() + " is not in [" + Twine(Min).str() + ", " +
182
1.83k
        Twine(Max).str() + "]" + Hint);
183
1.83k
}
Unexecuted instantiation: AArch64ErrataFix.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
AArch64.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Line
Count
Source
173
1.82k
                                    int64_t Min, uint64_t Max) {
174
1.82k
  ErrorPlace ErrPlace = getErrorPlace(Loc);
175
1.82k
  StringRef Hint;
176
1.82k
  if (ErrPlace.IS && ErrPlace.IS->Name.startswith(".debug"))
177
0
    Hint = "; consider recompiling with -fdebug-types-section to reduce size "
178
0
           "of debug sections";
179
1.82k
180
1.82k
  error(ErrPlace.Loc + "relocation " + lld::toString(Type) +
181
1.82k
        " out of range: " + V.str() + " is not in [" + Twine(Min).str() + ", " +
182
1.82k
        Twine(Max).str() + "]" + Hint);
183
1.82k
}
Unexecuted instantiation: AMDGPU.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Unexecuted instantiation: ARM.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Unexecuted instantiation: AVR.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Unexecuted instantiation: Hexagon.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Unexecuted instantiation: Mips.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Unexecuted instantiation: PPC.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Unexecuted instantiation: PPC64.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Unexecuted instantiation: SPARCV9.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
X86.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Line
Count
Source
173
3
                                    int64_t Min, uint64_t Max) {
174
3
  ErrorPlace ErrPlace = getErrorPlace(Loc);
175
3
  StringRef Hint;
176
3
  if (ErrPlace.IS && ErrPlace.IS->Name.startswith(".debug"))
177
0
    Hint = "; consider recompiling with -fdebug-types-section to reduce size "
178
0
           "of debug sections";
179
3
180
3
  error(ErrPlace.Loc + "relocation " + lld::toString(Type) +
181
3
        " out of range: " + V.str() + " is not in [" + Twine(Min).str() + ", " +
182
3
        Twine(Max).str() + "]" + Hint);
183
3
}
X86_64.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Line
Count
Source
173
8
                                    int64_t Min, uint64_t Max) {
174
8
  ErrorPlace ErrPlace = getErrorPlace(Loc);
175
8
  StringRef Hint;
176
8
  if (ErrPlace.IS && ErrPlace.IS->Name.startswith(".debug"))
177
1
    Hint = "; consider recompiling with -fdebug-types-section to reduce size "
178
1
           "of debug sections";
179
8
180
8
  error(ErrPlace.Loc + "relocation " + lld::toString(Type) +
181
8
        " out of range: " + V.str() + " is not in [" + Twine(Min).str() + ", " +
182
8
        Twine(Max).str() + "]" + Hint);
183
8
}
Unexecuted instantiation: Driver.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Unexecuted instantiation: EhFrame.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Unexecuted instantiation: InputSection.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Unexecuted instantiation: LinkerScript.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Unexecuted instantiation: MarkLive.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Unexecuted instantiation: OutputSections.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Unexecuted instantiation: Relocations.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Unexecuted instantiation: ScriptParser.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Unexecuted instantiation: Symbols.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Unexecuted instantiation: SyntheticSections.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Unexecuted instantiation: Target.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Unexecuted instantiation: Thunks.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Unexecuted instantiation: Writer.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
184
185
// Sign-extend Nth bit all the way to MSB.
186
11.7k
inline int64_t signExtend(uint64_t V, int N) {
187
11.7k
  return int64_t(V << (64 - N)) >> (64 - N);
188
11.7k
}
189
190
// Make sure that V can be represented as an N bit signed integer.
191
11.7k
inline void checkInt(uint8_t *Loc, int64_t V, int N, RelType Type) {
192
11.7k
  if (V != signExtend(V, N))
193
1.82k
    reportRangeError(Loc, Type, Twine(V), llvm::minIntN(N), llvm::maxIntN(N));
194
11.7k
}
195
196
// Make sure that V can be represented as an N bit unsigned integer.
197
132
inline void checkUInt(uint8_t *Loc, uint64_t V, int N, RelType Type) {
198
132
  if ((V >> N) != 0)
199
4
    reportRangeError(Loc, Type, Twine(V), 0, llvm::maxUIntN(N));
200
132
}
201
202
// Make sure that V can be represented as an N bit signed or unsigned integer.
203
40
inline void checkIntUInt(uint8_t *Loc, uint64_t V, int N, RelType Type) {
204
40
  // For the error message we should cast V to a signed integer so that error
205
40
  // messages show a small negative value rather than an extremely large one
206
40
  if (V != (uint64_t)signExtend(V, N) && 
(V >> N) != 019
)
207
10
    reportRangeError(Loc, Type, Twine((int64_t)V), llvm::minIntN(N),
208
10
                     llvm::maxIntN(N));
209
40
}
210
211
149
inline void checkAlignment(uint8_t *Loc, uint64_t V, int N, RelType Type) {
212
149
  if ((V & (N - 1)) != 0)
213
6
    error(getErrorLocation(Loc) + "improper alignment for relocation " +
214
6
          lld::toString(Type) + ": 0x" + llvm::utohexstr(V) +
215
6
          " is not aligned to " + Twine(N) + " bytes");
216
149
}
217
218
// Endianness-aware read/write.
219
20
inline uint16_t read16(const void *P) {
220
20
  return llvm::support::endian::read16(P, Config->Endianness);
221
20
}
222
223
597
inline uint32_t read32(const void *P) {
224
597
  return llvm::support::endian::read32(P, Config->Endianness);
225
597
}
226
227
2
inline uint64_t read64(const void *P) {
228
2
  return llvm::support::endian::read64(P, Config->Endianness);
229
2
}
230
231
235
inline void write16(void *P, uint16_t V) {
232
235
  llvm::support::endian::write16(P, V, Config->Endianness);
233
235
}
234
235
9.47k
inline void write32(void *P, uint32_t V) {
236
9.47k
  llvm::support::endian::write32(P, V, Config->Endianness);
237
9.47k
}
238
239
67
inline void write64(void *P, uint64_t V) {
240
67
  llvm::support::endian::write64(P, V, Config->Endianness);
241
67
}
242
} // namespace elf
243
} // namespace lld
244
245
#endif