Coverage Report

Created: 2018-08-19 21:11

/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.83k
  virtual uint32_t calcEFlags() const { return 0; }
29
12
  virtual RelType getDynRel(RelType Type) const { return Type; }
30
42
  virtual void writeGotPltHeader(uint8_t *Buf) const {}
31
101
  virtual void writeGotHeader(uint8_t *Buf) const {}
32
0
  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
187
  virtual void addPltHeaderSymbols(InputSection &IS) const {}
45
267
  virtual void addPltSymbols(InputSection &IS, uint64_t Off) const {}
46
47
768
  unsigned getPltEntryOffset(unsigned Index) const {
48
768
    return Index * PltEntrySize + PltHeaderSize;
49
768
  }
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
  // The function with a prologue starting at Loc was compiled with
65
  // -fsplit-stack and it calls a function compiled without. Adjust the prologue
66
  // to do the right thing. See https://gcc.gnu.org/wiki/SplitStacks.
67
  virtual bool adjustPrologueForCrossSplitStack(uint8_t *Loc,
68
                                                uint8_t *End) const;
69
70
  // Return true if we can reach Dst from Src with Relocation RelocType
71
  virtual bool inBranchRange(RelType Type, uint64_t Src,
72
                             uint64_t Dst) const;
73
  virtual RelExpr getRelExpr(RelType Type, const Symbol &S,
74
                             const uint8_t *Loc) const = 0;
75
76
  virtual void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const = 0;
77
78
  virtual ~TargetInfo();
79
80
  unsigned TlsGdRelaxSkip = 1;
81
  unsigned PageSize = 4096;
82
  unsigned DefaultMaxPageSize = 4096;
83
84
  uint64_t getImageBase();
85
86
  // Offset of _GLOBAL_OFFSET_TABLE_ from base of .got or .got.plt section.
87
  uint64_t GotBaseSymOff = 0;
88
  // True if _GLOBAL_OFFSET_TABLE_ is relative to .got.plt, false if .got.
89
  bool GotBaseSymInGotPlt = true;
90
91
  // On systems with range extensions we place collections of Thunks at
92
  // regular spacings that enable the majority of branches reach the Thunks.
93
  uint32_t ThunkSectionSpacing = 0;
94
95
  RelType CopyRel;
96
  RelType GotRel;
97
  RelType PltRel;
98
  RelType RelativeRel;
99
  RelType IRelativeRel;
100
  RelType TlsDescRel;
101
  RelType TlsGotRel;
102
  RelType TlsModuleIndexRel;
103
  RelType TlsOffsetRel;
104
  unsigned GotEntrySize = 0;
105
  unsigned GotPltEntrySize = 0;
106
  unsigned PltEntrySize;
107
  unsigned PltHeaderSize;
108
109
  // At least on x86_64 positions 1 and 2 are used by the first plt entry
110
  // to support lazy loading.
111
  unsigned GotPltHeaderEntriesNum = 3;
112
113
  // On PPC ELF V2 abi, the first entry in the .got is the .TOC.
114
  unsigned GotHeaderEntriesNum = 0;
115
116
  // For TLS variant 1, the TCB is a fixed size specified by the Target.
117
  // For variant 2, the TCB is an unspecified size.
118
  // Set to 0 for variant 2.
119
  unsigned TcbSize = 0;
120
121
  // Set to the offset (in bytes) that the thread pointer is initialized to
122
  // point to, relative to the start of the thread local storage.
123
  unsigned TlsTpOffset = 0;
124
125
  bool NeedsThunks = false;
126
127
  // A 4-byte field corresponding to one or more trap instructions, used to pad
128
  // executable OutputSections.
129
  uint32_t TrapInstr = 0;
130
131
  virtual RelExpr adjustRelaxExpr(RelType Type, const uint8_t *Data,
132
                                  RelExpr Expr) const;
133
  virtual void relaxGot(uint8_t *Loc, uint64_t Val) const;
134
  virtual void relaxTlsGdToIe(uint8_t *Loc, RelType Type, uint64_t Val) const;
135
  virtual void relaxTlsGdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const;
136
  virtual void relaxTlsIeToLe(uint8_t *Loc, RelType Type, uint64_t Val) const;
137
  virtual void relaxTlsLdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const;
138
139
protected:
140
  // On FreeBSD x86_64 the first page cannot be mmaped.
141
  // On Linux that is controled by vm.mmap_min_addr. At least on some x86_64
142
  // installs that is 65536, so the first 15 pages cannot be used.
143
  // Given that, the smallest value that can be used in here is 0x10000.
144
  uint64_t DefaultImageBase = 0x10000;
145
};
146
147
TargetInfo *getAArch64TargetInfo();
148
TargetInfo *getAMDGPUTargetInfo();
149
TargetInfo *getARMTargetInfo();
150
TargetInfo *getAVRTargetInfo();
151
TargetInfo *getHexagonTargetInfo();
152
TargetInfo *getPPC64TargetInfo();
153
TargetInfo *getPPCTargetInfo();
154
TargetInfo *getRISCVTargetInfo();
155
TargetInfo *getSPARCV9TargetInfo();
156
TargetInfo *getX32TargetInfo();
157
TargetInfo *getX86TargetInfo();
158
TargetInfo *getX86_64TargetInfo();
159
template <class ELFT> TargetInfo *getMipsTargetInfo();
160
161
struct ErrorPlace {
162
  InputSectionBase *IS;
163
  std::string Loc;
164
};
165
166
// Returns input section and corresponding source string for the given location.
167
ErrorPlace getErrorPlace(const uint8_t *Loc);
168
169
14
static inline std::string getErrorLocation(const uint8_t *Loc) {
170
14
  return getErrorPlace(Loc).Loc;
171
14
}
Unexecuted instantiation: AArch64ErrataFix.cpp:lld::elf::getErrorLocation(unsigned char const*)
AArch64.cpp:lld::elf::getErrorLocation(unsigned char const*)
Line
Count
Source
169
6
static inline std::string getErrorLocation(const uint8_t *Loc) {
170
6
  return getErrorPlace(Loc).Loc;
171
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: 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
169
4
static inline std::string getErrorLocation(const uint8_t *Loc) {
170
4
  return getErrorPlace(Loc).Loc;
171
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
169
4
static inline std::string getErrorLocation(const uint8_t *Loc) {
170
4
  return getErrorPlace(Loc).Loc;
171
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*)
172
173
uint64_t getPPC64TocBase();
174
uint64_t getAArch64Page(uint64_t Expr);
175
176
extern TargetInfo *Target;
177
TargetInfo *getTarget();
178
179
template <class ELFT> bool isMipsPIC(const Defined *Sym);
180
181
static inline void reportRangeError(uint8_t *Loc, RelType Type, const Twine &V,
182
1.83k
                                    int64_t Min, uint64_t Max) {
183
1.83k
  ErrorPlace ErrPlace = getErrorPlace(Loc);
184
1.83k
  StringRef Hint;
185
1.83k
  if (ErrPlace.IS && ErrPlace.IS->Name.startswith(".debug"))
186
1
    Hint = "; consider recompiling with -fdebug-types-section to reduce size "
187
1
           "of debug sections";
188
1.83k
189
1.83k
  error(ErrPlace.Loc + "relocation " + lld::toString(Type) +
190
1.83k
        " out of range: " + V.str() + " is not in [" + Twine(Min).str() + ", " +
191
1.83k
        Twine(Max).str() + "]" + Hint);
192
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
182
1.82k
                                    int64_t Min, uint64_t Max) {
183
1.82k
  ErrorPlace ErrPlace = getErrorPlace(Loc);
184
1.82k
  StringRef Hint;
185
1.82k
  if (ErrPlace.IS && ErrPlace.IS->Name.startswith(".debug"))
186
0
    Hint = "; consider recompiling with -fdebug-types-section to reduce size "
187
0
           "of debug sections";
188
1.82k
189
1.82k
  error(ErrPlace.Loc + "relocation " + lld::toString(Type) +
190
1.82k
        " out of range: " + V.str() + " is not in [" + Twine(Min).str() + ", " +
191
1.82k
        Twine(Max).str() + "]" + Hint);
192
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
182
3
                                    int64_t Min, uint64_t Max) {
183
3
  ErrorPlace ErrPlace = getErrorPlace(Loc);
184
3
  StringRef Hint;
185
3
  if (ErrPlace.IS && ErrPlace.IS->Name.startswith(".debug"))
186
0
    Hint = "; consider recompiling with -fdebug-types-section to reduce size "
187
0
           "of debug sections";
188
3
189
3
  error(ErrPlace.Loc + "relocation " + lld::toString(Type) +
190
3
        " out of range: " + V.str() + " is not in [" + Twine(Min).str() + ", " +
191
3
        Twine(Max).str() + "]" + Hint);
192
3
}
X86_64.cpp:lld::elf::reportRangeError(unsigned char*, unsigned int, llvm::Twine const&, long long, unsigned long long)
Line
Count
Source
182
11
                                    int64_t Min, uint64_t Max) {
183
11
  ErrorPlace ErrPlace = getErrorPlace(Loc);
184
11
  StringRef Hint;
185
11
  if (ErrPlace.IS && ErrPlace.IS->Name.startswith(".debug"))
186
1
    Hint = "; consider recompiling with -fdebug-types-section to reduce size "
187
1
           "of debug sections";
188
11
189
11
  error(ErrPlace.Loc + "relocation " + lld::toString(Type) +
190
11
        " out of range: " + V.str() + " is not in [" + Twine(Min).str() + ", " +
191
11
        Twine(Max).str() + "]" + Hint);
192
11
}
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)
193
194
// Make sure that V can be represented as an N bit signed integer.
195
11.8k
inline void checkInt(uint8_t *Loc, int64_t V, int N, RelType Type) {
196
11.8k
  if (V != llvm::SignExtend64(V, N))
197
1.82k
    reportRangeError(Loc, Type, Twine(V), llvm::minIntN(N), llvm::maxIntN(N));
198
11.8k
}
199
200
// Make sure that V can be represented as an N bit unsigned integer.
201
138
inline void checkUInt(uint8_t *Loc, uint64_t V, int N, RelType Type) {
202
138
  if ((V >> N) != 0)
203
6
    reportRangeError(Loc, Type, Twine(V), 0, llvm::maxUIntN(N));
204
138
}
205
206
// Make sure that V can be represented as an N bit signed or unsigned integer.
207
40
inline void checkIntUInt(uint8_t *Loc, uint64_t V, int N, RelType Type) {
208
40
  // For the error message we should cast V to a signed integer so that error
209
40
  // messages show a small negative value rather than an extremely large one
210
40
  if (V != (uint64_t)llvm::SignExtend64(V, N) && 
(V >> N) != 019
)
211
10
    reportRangeError(Loc, Type, Twine((int64_t)V), llvm::minIntN(N),
212
10
                     llvm::maxIntN(N));
213
40
}
214
215
149
inline void checkAlignment(uint8_t *Loc, uint64_t V, int N, RelType Type) {
216
149
  if ((V & (N - 1)) != 0)
217
6
    error(getErrorLocation(Loc) + "improper alignment for relocation " +
218
6
          lld::toString(Type) + ": 0x" + llvm::utohexstr(V) +
219
6
          " is not aligned to " + Twine(N) + " bytes");
220
149
}
221
222
// Endianness-aware read/write.
223
32
inline uint16_t read16(const void *P) {
224
32
  return llvm::support::endian::read16(P, Config->Endianness);
225
32
}
226
227
596
inline uint32_t read32(const void *P) {
228
596
  return llvm::support::endian::read32(P, Config->Endianness);
229
596
}
230
231
3
inline uint64_t read64(const void *P) {
232
3
  return llvm::support::endian::read64(P, Config->Endianness);
233
3
}
234
235
279
inline void write16(void *P, uint16_t V) {
236
279
  llvm::support::endian::write16(P, V, Config->Endianness);
237
279
}
238
239
10.1k
inline void write32(void *P, uint32_t V) {
240
10.1k
  llvm::support::endian::write32(P, V, Config->Endianness);
241
10.1k
}
242
243
75
inline void write64(void *P, uint64_t V) {
244
75
  llvm::support::endian::write64(P, V, Config->Endianness);
245
75
}
246
} // namespace elf
247
} // namespace lld
248
249
#endif