Coverage Report

Created: 2019-05-19 14:56

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