Coverage Report

Created: 2018-10-20 12:32

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