Coverage Report

Created: 2019-01-18 03:29

/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
#include <array>
18
19
namespace lld {
20
std::string toString(elf::RelType Type);
21
22
namespace elf {
23
class Defined;
24
class InputFile;
25
class Symbol;
26
27
class TargetInfo {
28
public:
29
1.91k
  virtual uint32_t calcEFlags() const { return 0; }
30
16
  virtual RelType getDynRel(RelType Type) const { return Type; }
31
45
  virtual void writeGotPltHeader(uint8_t *Buf) const {}
32
110
  virtual void writeGotHeader(uint8_t *Buf) const {}
33
1
  virtual void writeGotPlt(uint8_t *Buf, const Symbol &S) const {};
34
  virtual void writeIgotPlt(uint8_t *Buf, const Symbol &S) const;
35
  virtual int64_t getImplicitAddend(const uint8_t *Buf, RelType Type) const;
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
210
  virtual void addPltHeaderSymbols(InputSection &IS) const {}
46
304
  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
503
  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 TlsGdRelaxSkip = 1;
85
  unsigned PageSize = 4096;
86
  unsigned DefaultMaxPageSize = 4096;
87
88
  uint64_t getImageBase();
89
90
  // Offset of _GLOBAL_OFFSET_TABLE_ from base of .got or .got.plt section.
91
  uint64_t GotBaseSymOff = 0;
92
  // True if _GLOBAL_OFFSET_TABLE_ is relative to .got.plt, false if .got.
93
  bool GotBaseSymInGotPlt = true;
94
95
  RelType CopyRel;
96
  RelType GotRel;
97
  RelType NoneRel;
98
  RelType PltRel;
99
  RelType RelativeRel;
100
  RelType IRelativeRel;
101
  RelType TlsDescRel;
102
  RelType TlsGotRel;
103
  RelType TlsModuleIndexRel;
104
  RelType TlsOffsetRel;
105
  unsigned GotEntrySize = 0;
106
  unsigned GotPltEntrySize = 0;
107
  unsigned PltEntrySize;
108
  unsigned PltHeaderSize;
109
110
  // At least on x86_64 positions 1 and 2 are used by the first plt entry
111
  // to support lazy loading.
112
  unsigned GotPltHeaderEntriesNum = 3;
113
114
  // On PPC ELF V2 abi, the first entry in the .got is the .TOC.
115
  unsigned GotHeaderEntriesNum = 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
  std::array<uint8_t, 4> TrapInstr;
122
123
  // If a target needs to rewrite calls to __morestack to instead call
124
  // __morestack_non_split when a split-stack enabled caller calls a
125
  // non-split-stack callee this will return true. Otherwise returns false.
126
  bool NeedsMoreStackNonSplit = true;
127
128
  virtual RelExpr adjustRelaxExpr(RelType Type, const uint8_t *Data,
129
                                  RelExpr Expr) const;
130
  virtual void relaxGot(uint8_t *Loc, uint64_t Val) const;
131
  virtual void relaxTlsGdToIe(uint8_t *Loc, RelType Type, uint64_t Val) const;
132
  virtual void relaxTlsGdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const;
133
  virtual void relaxTlsIeToLe(uint8_t *Loc, RelType Type, uint64_t Val) const;
134
  virtual void relaxTlsLdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const;
135
136
protected:
137
  // On FreeBSD x86_64 the first page cannot be mmaped.
138
  // On Linux that is controled by vm.mmap_min_addr. At least on some x86_64
139
  // installs that is 65536, so the first 15 pages cannot be used.
140
  // Given that, the smallest value that can be used in here is 0x10000.
141
  uint64_t DefaultImageBase = 0x10000;
142
};
143
144
TargetInfo *getAArch64TargetInfo();
145
TargetInfo *getAMDGPUTargetInfo();
146
TargetInfo *getARMTargetInfo();
147
TargetInfo *getAVRTargetInfo();
148
TargetInfo *getHexagonTargetInfo();
149
TargetInfo *getMSP430TargetInfo();
150
TargetInfo *getPPC64TargetInfo();
151
TargetInfo *getPPCTargetInfo();
152
TargetInfo *getRISCVTargetInfo();
153
TargetInfo *getSPARCV9TargetInfo();
154
TargetInfo *getX32TargetInfo();
155
TargetInfo *getX86TargetInfo();
156
TargetInfo *getX86_64TargetInfo();
157
template <class ELFT> TargetInfo *getMipsTargetInfo();
158
159
struct ErrorPlace {
160
  InputSectionBase *IS;
161
  std::string Loc;
162
};
163
164
// Returns input section and corresponding source string for the given location.
165
ErrorPlace getErrorPlace(const uint8_t *Loc);
166
167
28
static inline std::string getErrorLocation(const uint8_t *Loc) {
168
28
  return getErrorPlace(Loc).Loc;
169
28
}
Unexecuted instantiation: AArch64ErrataFix.cpp:lld::elf::getErrorLocation(unsigned char const*)
AArch64.cpp:lld::elf::getErrorLocation(unsigned char const*)
Line
Count
Source
167
14
static inline std::string getErrorLocation(const uint8_t *Loc) {
168
14
  return getErrorPlace(Loc).Loc;
169
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: 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
167
4
static inline std::string getErrorLocation(const uint8_t *Loc) {
168
4
  return getErrorPlace(Loc).Loc;
169
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
167
4
static inline std::string getErrorLocation(const uint8_t *Loc) {
168
4
  return getErrorPlace(Loc).Loc;
169
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
167
6
static inline std::string getErrorLocation(const uint8_t *Loc) {
168
6
  return getErrorPlace(Loc).Loc;
169
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*)
170
171
// In the PowerPC64 Elf V2 abi a function can have 2 entry points.  The first is
172
// a global entry point (GEP) which typically is used to intiailzie the TOC
173
// pointer in general purpose register 2.  The second is a local entry
174
// point (LEP) which bypasses the TOC pointer initialization code. The
175
// offset between GEP and LEP is encoded in a function's st_other flags.
176
// This function will return the offset (in bytes) from the global entry-point
177
// to the local entry-point.
178
unsigned getPPC64GlobalEntryToLocalEntryOffset(uint8_t StOther);
179
180
uint64_t getPPC64TocBase();
181
uint64_t getAArch64Page(uint64_t Expr);
182
183
extern TargetInfo *Target;
184
TargetInfo *getTarget();
185
186
template <class ELFT> bool isMipsPIC(const Defined *Sym);
187
188
static inline void reportRangeError(uint8_t *Loc, RelType Type, const Twine &V,
189
1.83k
                                    int64_t Min, uint64_t Max) {
190
1.83k
  ErrorPlace ErrPlace = getErrorPlace(Loc);
191
1.83k
  StringRef Hint;
192
1.83k
  if (ErrPlace.IS && ErrPlace.IS->Name.startswith(".debug"))
193
1
    Hint = "; consider recompiling with -fdebug-types-section to reduce size "
194
1
           "of debug sections";
195
1.83k
196
1.83k
  errorOrWarn(ErrPlace.Loc + "relocation " + lld::toString(Type) +
197
1.83k
              " out of range: " + V.str() + " is not in [" + Twine(Min).str() +
198
1.83k
              ", " + Twine(Max).str() + "]" + Hint);
199
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
189
1.82k
                                    int64_t Min, uint64_t Max) {
190
1.82k
  ErrorPlace ErrPlace = getErrorPlace(Loc);
191
1.82k
  StringRef Hint;
192
1.82k
  if (ErrPlace.IS && ErrPlace.IS->Name.startswith(".debug"))
193
0
    Hint = "; consider recompiling with -fdebug-types-section to reduce size "
194
0
           "of debug sections";
195
1.82k
196
1.82k
  errorOrWarn(ErrPlace.Loc + "relocation " + lld::toString(Type) +
197
1.82k
              " out of range: " + V.str() + " is not in [" + Twine(Min).str() +
198
1.82k
              ", " + Twine(Max).str() + "]" + Hint);
199
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
189
3
                                    int64_t Min, uint64_t Max) {
190
3
  ErrorPlace ErrPlace = getErrorPlace(Loc);
191
3
  StringRef Hint;
192
3
  if (ErrPlace.IS && ErrPlace.IS->Name.startswith(".debug"))
193
0
    Hint = "; consider recompiling with -fdebug-types-section to reduce size "
194
0
           "of debug sections";
195
3
196
3
  errorOrWarn(ErrPlace.Loc + "relocation " + lld::toString(Type) +
197
3
              " out of range: " + V.str() + " is not in [" + Twine(Min).str() +
198
3
              ", " + Twine(Max).str() + "]" + Hint);
199
3
}
X86_64.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Line
Count
Source
189
12
                                    int64_t Min, uint64_t Max) {
190
12
  ErrorPlace ErrPlace = getErrorPlace(Loc);
191
12
  StringRef Hint;
192
12
  if (ErrPlace.IS && ErrPlace.IS->Name.startswith(".debug"))
193
1
    Hint = "; consider recompiling with -fdebug-types-section to reduce size "
194
1
           "of debug sections";
195
12
196
12
  errorOrWarn(ErrPlace.Loc + "relocation " + lld::toString(Type) +
197
12
              " out of range: " + V.str() + " is not in [" + Twine(Min).str() +
198
12
              ", " + Twine(Max).str() + "]" + Hint);
199
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)
200
201
269
inline unsigned getPltEntryOffset(unsigned Idx) {
202
269
  return Target->PltHeaderSize + Target->PltEntrySize * Idx;
203
269
}
204
205
// Make sure that V can be represented as an N bit signed integer.
206
11.9k
inline void checkInt(uint8_t *Loc, int64_t V, int N, RelType Type) {
207
11.9k
  if (V != llvm::SignExtend64(V, N))
208
1.82k
    reportRangeError(Loc, Type, Twine(V), llvm::minIntN(N), llvm::maxIntN(N));
209
11.9k
}
210
211
// Make sure that V can be represented as an N bit unsigned integer.
212
166
inline void checkUInt(uint8_t *Loc, uint64_t V, int N, RelType Type) {
213
166
  if ((V >> N) != 0)
214
6
    reportRangeError(Loc, Type, Twine(V), 0, llvm::maxUIntN(N));
215
166
}
216
217
// Make sure that V can be represented as an N bit signed or unsigned integer.
218
45
inline void checkIntUInt(uint8_t *Loc, uint64_t V, int N, RelType Type) {
219
45
  // For the error message we should cast V to a signed integer so that error
220
45
  // messages show a small negative value rather than an extremely large one
221
45
  if (V != (uint64_t)llvm::SignExtend64(V, N) && 
(V >> N) != 021
)
222
10
    reportRangeError(Loc, Type, Twine((int64_t)V), llvm::minIntN(N),
223
10
                     llvm::maxIntN(N));
224
45
}
225
226
373
inline void checkAlignment(uint8_t *Loc, uint64_t V, int N, RelType Type) {
227
373
  if ((V & (N - 1)) != 0)
228
14
    error(getErrorLocation(Loc) + "improper alignment for relocation " +
229
14
          lld::toString(Type) + ": 0x" + llvm::utohexstr(V) +
230
14
          " is not aligned to " + Twine(N) + " bytes");
231
373
}
232
233
// Endianness-aware read/write.
234
78
inline uint16_t read16(const void *P) {
235
78
  return llvm::support::endian::read16(P, Config->Endianness);
236
78
}
237
238
350k
inline uint32_t read32(const void *P) {
239
350k
  return llvm::support::endian::read32(P, Config->Endianness);
240
350k
}
241
242
3
inline uint64_t read64(const void *P) {
243
3
  return llvm::support::endian::read64(P, Config->Endianness);
244
3
}
245
246
1.24k
inline void write16(void *P, uint16_t V) {
247
1.24k
  llvm::support::endian::write16(P, V, Config->Endianness);
248
1.24k
}
249
250
12.2k
inline void write32(void *P, uint32_t V) {
251
12.2k
  llvm::support::endian::write32(P, V, Config->Endianness);
252
12.2k
}
253
254
150
inline void write64(void *P, uint64_t V) {
255
150
  llvm::support::endian::write64(P, V, Config->Endianness);
256
150
}
257
} // namespace elf
258
} // namespace lld
259
260
#endif