blob: 5047a85710986d3654f08c09595738db33907f6c [file] [log] [blame]
Rafael Espindola01205f72015-09-22 18:19:46 +00001//===- Target.cpp ---------------------------------------------------------===//
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//===----------------------------------------------------------------------===//
Rui Ueyama34f29242015-10-13 19:51:57 +00009//
Rui Ueyama66072272015-10-15 19:52:27 +000010// Machine-specific things, such as applying relocations, creation of
11// GOT or PLT entries, etc., are handled in this file.
12//
13// Refer the ELF spec for the single letter varaibles, S, A or P, used
14// in this file. SA is S+A.
Rui Ueyama34f29242015-10-13 19:51:57 +000015//
16//===----------------------------------------------------------------------===//
Rafael Espindola01205f72015-09-22 18:19:46 +000017
18#include "Target.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000019#include "Error.h"
Rui Ueyamaaf21d922015-10-08 20:06:07 +000020#include "OutputSections.h"
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000021#include "Symbols.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000022
23#include "llvm/ADT/ArrayRef.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000024#include "llvm/Object/ELF.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000025#include "llvm/Support/Endian.h"
26#include "llvm/Support/ELF.h"
27
28using namespace llvm;
Rafael Espindolac4010882015-09-22 20:54:08 +000029using namespace llvm::object;
Rafael Espindola0872ea32015-09-24 14:16:02 +000030using namespace llvm::support::endian;
Rafael Espindola01205f72015-09-22 18:19:46 +000031using namespace llvm::ELF;
32
33namespace lld {
34namespace elf2 {
35
36std::unique_ptr<TargetInfo> Target;
37
Rafael Espindolae7e57b22015-11-09 21:43:00 +000038template <endianness E> static void add32(void *P, int32_t V) {
39 write32<E>(P, read32<E>(P) + V);
40}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000041
Rafael Espindolae7e57b22015-11-09 21:43:00 +000042static void add32le(uint8_t *P, int32_t V) { add32<support::little>(P, V); }
43static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
Rui Ueyamaefc23de2015-10-14 21:30:32 +000044
45namespace {
46class X86TargetInfo final : public TargetInfo {
47public:
48 X86TargetInfo();
George Rimar21c0a712015-11-25 21:37:59 +000049 void writeGotPltHeaderEntries(uint8_t *Buf) const override;
George Rimard23970f2015-11-25 20:41:53 +000050 unsigned getDynReloc(unsigned Type) const override;
51 bool isTlsDynReloc(unsigned Type) const override;
George Rimar648a2c32015-10-20 08:54:27 +000052 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
53 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
54 uint64_t PltEntryAddr) const override;
George Rimar21c0a712015-11-25 21:37:59 +000055 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
56 uint64_t PltEntryAddr, int32_t Index,
57 unsigned RelOff) const override;
George Rimar70e25082015-11-25 11:27:40 +000058 bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000059 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000060 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000061 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +000062 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000063};
64
65class X86_64TargetInfo final : public TargetInfo {
66public:
67 X86_64TargetInfo();
Igor Kudrine7ad0932015-11-17 17:47:53 +000068 unsigned getPltRefReloc(unsigned Type) const override;
George Rimard23970f2015-11-25 20:41:53 +000069 bool isTlsDynReloc(unsigned Type) const override;
Igor Kudrin351b41d2015-11-16 17:44:08 +000070 void writeGotPltHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +000071 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
72 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
73 uint64_t PltEntryAddr) const override;
George Rimar21c0a712015-11-25 21:37:59 +000074 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
75 uint64_t PltEntryAddr, int32_t Index,
76 unsigned RelOff) const override;
George Rimarbc590fe2015-10-28 16:48:58 +000077 bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000078 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
79 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000080 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +000081 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000082 bool isRelRelative(uint32_t Type) const override;
George Rimar77d1cb12015-11-24 09:00:06 +000083 bool isTlsOptimized(unsigned Type, const SymbolBody &S) const override;
84 void relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
85 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000086};
87
88class PPC64TargetInfo final : public TargetInfo {
89public:
90 PPC64TargetInfo();
George Rimar648a2c32015-10-20 08:54:27 +000091 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
92 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
93 uint64_t PltEntryAddr) const override;
George Rimar21c0a712015-11-25 21:37:59 +000094 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
95 uint64_t PltEntryAddr, int32_t Index,
96 unsigned RelOff) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000097 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
98 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000099 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +0000100 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000101 bool isRelRelative(uint32_t Type) const override;
102};
103
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000104class AArch64TargetInfo final : public TargetInfo {
105public:
106 AArch64TargetInfo();
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000107 unsigned getGotRefReloc(unsigned Type) const override;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000108 unsigned getPltRefReloc(unsigned Type) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000109 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
110 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
111 uint64_t PltEntryAddr) const override;
George Rimar21c0a712015-11-25 21:37:59 +0000112 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
113 uint64_t PltEntryAddr, int32_t Index,
114 unsigned RelOff) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000115 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
116 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000117 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +0000118 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000119};
120
121template <class ELFT> class MipsTargetInfo final : public TargetInfo {
122public:
123 MipsTargetInfo();
Simon Atanasyan96306bb2015-11-25 20:58:52 +0000124 unsigned getGotRefReloc(unsigned Type) const override;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000125 void writeGotHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000126 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
127 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
128 uint64_t PltEntryAddr) const override;
George Rimar21c0a712015-11-25 21:37:59 +0000129 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
130 uint64_t PltEntryAddr, int32_t Index,
131 unsigned RelOff) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000132 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
133 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000134 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +0000135 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000136};
137} // anonymous namespace
138
Rui Ueyama91004392015-10-13 16:08:15 +0000139TargetInfo *createTarget() {
140 switch (Config->EMachine) {
141 case EM_386:
142 return new X86TargetInfo();
143 case EM_AARCH64:
144 return new AArch64TargetInfo();
145 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000146 switch (Config->EKind) {
147 case ELF32LEKind:
148 return new MipsTargetInfo<ELF32LE>();
149 case ELF32BEKind:
150 return new MipsTargetInfo<ELF32BE>();
151 default:
152 error("Unsupported MIPS target");
153 }
Rui Ueyama91004392015-10-13 16:08:15 +0000154 case EM_PPC64:
155 return new PPC64TargetInfo();
156 case EM_X86_64:
157 return new X86_64TargetInfo();
158 }
159 error("Unknown target machine");
160}
161
Rafael Espindola01205f72015-09-22 18:19:46 +0000162TargetInfo::~TargetInfo() {}
163
George Rimar77d1cb12015-11-24 09:00:06 +0000164bool TargetInfo::isTlsOptimized(unsigned Type, const SymbolBody &S) const {
165 return false;
166}
167
Igor Kudrinf6f45472015-11-10 08:39:27 +0000168uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
169
George Rimarbc590fe2015-10-28 16:48:58 +0000170bool TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const {
171 return false;
172}
173
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000174unsigned TargetInfo::getGotRefReloc(unsigned Type) const { return GotRefReloc; }
175
Igor Kudrine7ad0932015-11-17 17:47:53 +0000176unsigned TargetInfo::getPltRefReloc(unsigned Type) const { return PCRelReloc; }
Rafael Espindola227556e2015-10-14 16:15:46 +0000177
Rafael Espindolaae244002015-10-05 19:30:12 +0000178bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
179
George Rimar77d1cb12015-11-24 09:00:06 +0000180void TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
181 uint64_t SA) const {}
182
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000183void TargetInfo::writeGotHeaderEntries(uint8_t *Buf) const {}
184
Igor Kudrin351b41d2015-11-16 17:44:08 +0000185void TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {}
186
Rafael Espindola7f074422015-09-22 21:35:51 +0000187X86TargetInfo::X86TargetInfo() {
George Rimar70e25082015-11-25 11:27:40 +0000188 CopyReloc = R_386_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000189 PCRelReloc = R_386_PC32;
190 GotReloc = R_386_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000191 GotRefReloc = R_386_GOT32;
George Rimar648a2c32015-10-20 08:54:27 +0000192 PltReloc = R_386_JUMP_SLOT;
George Rimar21c0a712015-11-25 21:37:59 +0000193 LazyRelocations = true;
194 PltEntrySize = 16;
195 PltZeroEntrySize = 16;
196}
197
198void X86TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
199 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
200}
201
202void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
203 // Skip 6 bytes of "pushl (GOT+4)"
204 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000205}
Rafael Espindola01205f72015-09-22 18:19:46 +0000206
George Rimard23970f2015-11-25 20:41:53 +0000207unsigned X86TargetInfo::getDynReloc(unsigned Type) const {
208 if (Type == R_386_TLS_LE)
209 return R_386_TLS_TPOFF;
210 if (Type == R_386_TLS_LE_32)
211 return R_386_TLS_TPOFF32;
212 return Type;
213}
214
215bool X86TargetInfo::isTlsDynReloc(unsigned Type) const {
216 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32)
217 return Config->Shared;
218 return false;
219}
220
George Rimar648a2c32015-10-20 08:54:27 +0000221void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar21c0a712015-11-25 21:37:59 +0000222 uint64_t PltEntryAddr) const {
223 // Executable files and shared object files have
224 // separate procedure linkage tables.
225 if (Config->Shared) {
226 const uint8_t V[] = {
227 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx
228 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
229 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
230 };
231 memcpy(Buf, V, sizeof(V));
232 return;
233 }
George Rimar648a2c32015-10-20 08:54:27 +0000234
George Rimar21c0a712015-11-25 21:37:59 +0000235 const uint8_t PltData[] = {
236 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
237 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
238 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
239 };
240 memcpy(Buf, PltData, sizeof(PltData));
241 write32le(Buf + 2, GotEntryAddr + 4); // GOT+4
242 write32le(Buf + 8, GotEntryAddr + 8); // GOT+8
243}
244
245void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
246 uint64_t GotEntryAddr, uint64_t PltEntryAddr,
247 int32_t Index, unsigned RelOff) const {
248 const uint8_t Inst[] = {
249 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
250 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
251 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
252 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000253 memcpy(Buf, Inst, sizeof(Inst));
George Rimar21c0a712015-11-25 21:37:59 +0000254 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
255 Buf[1] = Config->Shared ? 0xa3 : 0x25;
256 write32le(Buf + 2, Config->Shared ? (GotEntryAddr - GotAddr) : GotEntryAddr);
257 write32le(Buf + 7, RelOff);
258 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000259}
260
George Rimar70e25082015-11-25 11:27:40 +0000261bool X86TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const {
262 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
263 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
264 return SS->Sym.getType() == STT_OBJECT;
265 return false;
266}
267
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000268bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +0000269 return Type == R_386_GOT32 || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000270}
271
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000272bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimar730c2782015-10-07 18:46:13 +0000273 return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000274}
275
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000276void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
277 uint64_t P, uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000278 switch (Type) {
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000279 case R_386_GOT32:
Rui Ueyama66072272015-10-15 19:52:27 +0000280 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000281 break;
George Rimar13934772015-11-25 20:20:31 +0000282 case R_386_GOTPC:
283 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
284 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000285 case R_386_PC32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000286 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000287 break;
288 case R_386_32:
Rui Ueyama66072272015-10-15 19:52:27 +0000289 add32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000290 break;
George Rimard23970f2015-11-25 20:41:53 +0000291 case R_386_TLS_LE:
292 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
293 break;
294 case R_386_TLS_LE_32:
295 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
296 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000297 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000298 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000299 }
300}
301
Rafael Espindola7f074422015-09-22 21:35:51 +0000302X86_64TargetInfo::X86_64TargetInfo() {
George Rimarbc590fe2015-10-28 16:48:58 +0000303 CopyReloc = R_X86_64_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000304 PCRelReloc = R_X86_64_PC32;
305 GotReloc = R_X86_64_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000306 GotRefReloc = R_X86_64_PC32;
George Rimar648a2c32015-10-20 08:54:27 +0000307 PltReloc = R_X86_64_JUMP_SLOT;
Rafael Espindolaae244002015-10-05 19:30:12 +0000308 RelativeReloc = R_X86_64_RELATIVE;
George Rimar687138c2015-11-13 16:28:53 +0000309 TlsGotReloc = R_X86_64_TPOFF64;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000310 TlsLocalDynamicReloc = R_X86_64_TLSLD;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000311 TlsGlobalDynamicReloc = R_X86_64_TLSGD;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000312 TlsModuleIndexReloc = R_X86_64_DTPMOD64;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000313 TlsOffsetReloc = R_X86_64_DTPOFF64;
George Rimar648a2c32015-10-20 08:54:27 +0000314 LazyRelocations = true;
315 PltEntrySize = 16;
316 PltZeroEntrySize = 16;
317}
318
Igor Kudrin351b41d2015-11-16 17:44:08 +0000319void X86_64TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
320 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
321}
322
George Rimar648a2c32015-10-20 08:54:27 +0000323void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
324 // Skip 6 bytes of "jmpq *got(%rip)"
325 write32le(Buf, Plt + 6);
326}
327
328void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
329 uint64_t PltEntryAddr) const {
330 const uint8_t PltData[] = {
331 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
332 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
333 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
334 };
335 memcpy(Buf, PltData, sizeof(PltData));
336 write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8
337 write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000338}
Rafael Espindola01205f72015-09-22 18:19:46 +0000339
George Rimar21c0a712015-11-25 21:37:59 +0000340void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
341 uint64_t GotEntryAddr,
342 uint64_t PltEntryAddr, int32_t Index,
343 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000344 const uint8_t Inst[] = {
345 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
346 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
347 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
348 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000349 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000350
George Rimar648a2c32015-10-20 08:54:27 +0000351 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
352 write32le(Buf + 7, Index);
353 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000354}
355
George Rimarbc590fe2015-10-28 16:48:58 +0000356bool X86_64TargetInfo::relocNeedsCopy(uint32_t Type,
357 const SymbolBody &S) const {
358 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
359 Type == R_X86_64_64)
360 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
361 return SS->Sym.getType() == STT_OBJECT;
362 return false;
363}
364
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000365bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000366 if (Type == R_X86_64_GOTTPOFF)
367 return !isTlsOptimized(Type, S);
George Rimar687138c2015-11-13 16:28:53 +0000368 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_GOTPCREL ||
369 relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000370}
371
George Rimard23970f2015-11-25 20:41:53 +0000372bool X86_64TargetInfo::isTlsDynReloc(unsigned Type) const {
373 return Type == R_X86_64_GOTTPOFF;
374}
375
Igor Kudrine7ad0932015-11-17 17:47:53 +0000376unsigned X86_64TargetInfo::getPltRefReloc(unsigned Type) const {
George Rimar52687212015-10-28 18:33:08 +0000377 if (Type == R_X86_64_PLT32)
Rafael Espindola227556e2015-10-14 16:15:46 +0000378 return R_X86_64_PC32;
George Rimar52687212015-10-28 18:33:08 +0000379 return Type;
Rafael Espindola227556e2015-10-14 16:15:46 +0000380}
381
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000382bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000383 if (relocNeedsCopy(Type, S))
384 return false;
385
Rafael Espindola01205f72015-09-22 18:19:46 +0000386 switch (Type) {
387 default:
388 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000389 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000390 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000391 case R_X86_64_PC32:
392 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000393 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000394 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000395 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000396 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000397 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
398 // libc.so.
399 //
400 // The general idea on how to handle such cases is to create a PLT entry
401 // and use that as the function value.
402 //
403 // For the static linking part, we just return true and everything else
404 // will use the the PLT entry as the address.
405 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000406 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000407 // still works. We need the help of the dynamic linker for that. We
408 // let it know that we have a direct reference to a so symbol by creating
409 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000410 // dynamic linker resolves the symbol to the value of the symbol we created.
411 // This is true even for got entries, so pointer equality is maintained.
412 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000413 // real function is a dedicated got entry used by the plt. That is
414 // identified by special relocation types (R_X86_64_JUMP_SLOT,
415 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000416 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000417 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000418 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000419 }
420}
Rafael Espindolac4010882015-09-22 20:54:08 +0000421
Rafael Espindolaae244002015-10-05 19:30:12 +0000422bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
423 switch (Type) {
424 default:
425 return false;
426 case R_X86_64_PC64:
427 case R_X86_64_PC32:
428 case R_X86_64_PC16:
429 case R_X86_64_PC8:
Rafael Espindola69535df2015-10-19 05:20:01 +0000430 case R_X86_64_PLT32:
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000431 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000432 case R_X86_64_DTPOFF64:
Rafael Espindolaae244002015-10-05 19:30:12 +0000433 return true;
434 }
435}
436
George Rimar77d1cb12015-11-24 09:00:06 +0000437bool X86_64TargetInfo::isTlsOptimized(unsigned Type,
438 const SymbolBody &S) const {
439 if (Config->Shared || !S.isTLS())
440 return false;
441 return Type == R_X86_64_GOTTPOFF && !canBePreempted(&S, true);
442}
443
444// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
445// R_X86_64_TPOFF32 so that R_X86_64_TPOFF32 so that it does not use GOT.
446// This function does that. Read "ELF Handling For Thread-Local Storage,
447// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
448// by Ulrich Drepper for details.
449void X86_64TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
450 uint64_t P, uint64_t SA) const {
451 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
452 // used in MOVQ or ADDQ instructions only.
453 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
454 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
455 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
456 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
457 uint8_t *Prefix = Loc - 3;
458 uint8_t *Inst = Loc - 2;
459 uint8_t *RegSlot = Loc - 1;
460 uint8_t Reg = Loc[-1] >> 3;
461 bool IsMov = *Inst == 0x8b;
462 bool RspAdd = !IsMov && Reg == 4;
463 // r12 and rsp registers requires special handling.
464 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
465 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
466 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
467 // The same true for rsp. So we convert to addq for them, saving 1 byte that
468 // we dont have.
469 if (RspAdd)
470 *Inst = 0x81;
471 else
472 *Inst = IsMov ? 0xc7 : 0x8d;
473 if (*Prefix == 0x4c)
474 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
475 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
476 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
477}
478
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000479void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
480 uint64_t P, uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000481 switch (Type) {
482 case R_X86_64_PC32:
Rafael Espindolacdfecff2015-09-23 20:08:25 +0000483 case R_X86_64_GOTPCREL:
Rafael Espindola5045e442015-10-18 03:13:46 +0000484 case R_X86_64_PLT32:
Michael J. Spencer1e225612015-11-11 01:00:24 +0000485 case R_X86_64_TLSLD:
Michael J. Spencer627ae702015-11-13 00:28:34 +0000486 case R_X86_64_TLSGD:
George Rimar687138c2015-11-13 16:28:53 +0000487 case R_X86_64_TPOFF64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000488 write32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000489 break;
490 case R_X86_64_64:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000491 case R_X86_64_DTPOFF64:
Rui Ueyama66072272015-10-15 19:52:27 +0000492 write64le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000493 break;
Rui Ueyama3835b492015-10-23 16:13:27 +0000494 case R_X86_64_32:
Rafael Espindolac4010882015-09-22 20:54:08 +0000495 case R_X86_64_32S:
Rui Ueyama66072272015-10-15 19:52:27 +0000496 if (Type == R_X86_64_32 && !isUInt<32>(SA))
Rafael Espindolac4010882015-09-22 20:54:08 +0000497 error("R_X86_64_32 out of range");
Rui Ueyama66072272015-10-15 19:52:27 +0000498 else if (!isInt<32>(SA))
Rafael Espindolac4010882015-09-22 20:54:08 +0000499 error("R_X86_64_32S out of range");
Rui Ueyama66072272015-10-15 19:52:27 +0000500 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000501 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000502 case R_X86_64_DTPOFF32:
503 write32le(Loc, SA);
504 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000505 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000506 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000507 if (!isInt<32>(Val))
508 error("R_X86_64_TPOFF32 out of range");
509 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000510 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000511 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000512 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000513 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000514 }
515}
516
Hal Finkel3c8cc672015-10-12 20:56:18 +0000517// Relocation masks following the #lo(value), #hi(value), #ha(value),
518// #higher(value), #highera(value), #highest(value), and #highesta(value)
519// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
520// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000521static uint16_t applyPPCLo(uint64_t V) { return V; }
522static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
523static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
524static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
525static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000526static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000527static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
528
Rafael Espindolac4010882015-09-22 20:54:08 +0000529PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000530 PCRelReloc = R_PPC64_REL24;
531 GotReloc = R_PPC64_GLOB_DAT;
532 GotRefReloc = R_PPC64_REL64;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000533 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000534 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000535
536 // We need 64K pages (at least under glibc/Linux, the loader won't
537 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000538 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000539
540 // The PPC64 ELF ABI v1 spec, says:
541 //
542 // It is normally desirable to put segments with different characteristics
543 // in separate 256 Mbyte portions of the address space, to give the
544 // operating system full paging flexibility in the 64-bit address space.
545 //
546 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
547 // use 0x10000000 as the starting address.
548 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000549}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000550
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000551uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000552 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
553 // order. The TOC starts where the first of these sections starts.
554
555 // FIXME: This obviously does not do the right thing when there is no .got
556 // section, but there is a .toc or .tocbss section.
557 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
558 if (!TocVA)
559 TocVA = Out<ELF64BE>::Plt->getVA();
560
561 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
562 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
563 // code (crt1.o) assumes that you can get from the TOC base to the
564 // start of the .toc section with only a single (signed) 16-bit relocation.
565 return TocVA + 0x8000;
566}
567
George Rimar648a2c32015-10-20 08:54:27 +0000568void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
569void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
570 uint64_t PltEntryAddr) const {}
George Rimar21c0a712015-11-25 21:37:59 +0000571void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
572 uint64_t GotEntryAddr,
573 uint64_t PltEntryAddr, int32_t Index,
574 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000575 uint64_t Off = GotEntryAddr - getPPC64TocBase();
576
577 // FIXME: What we should do, in theory, is get the offset of the function
578 // descriptor in the .opd section, and use that as the offset from %r2 (the
579 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
580 // be a pointer to the function descriptor in the .opd section. Using
581 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
582
Hal Finkelfa92f682015-10-13 21:47:34 +0000583 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000584 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
585 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
586 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
587 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
588 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
589 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
590 write32be(Buf + 28, 0x4e800420); // bctr
591}
592
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000593bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000594 if (relocNeedsPlt(Type, S))
595 return true;
596
597 switch (Type) {
598 default: return false;
599 case R_PPC64_GOT16:
600 case R_PPC64_GOT16_LO:
601 case R_PPC64_GOT16_HI:
602 case R_PPC64_GOT16_HA:
603 case R_PPC64_GOT16_DS:
604 case R_PPC64_GOT16_LO_DS:
605 return true;
606 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000607}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000608
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000609bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000610 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +0000611 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000612}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000613
Hal Finkelbe0823d2015-10-12 20:58:52 +0000614bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
615 switch (Type) {
616 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +0000617 return true;
Hal Finkel00918622015-10-16 19:01:50 +0000618 case R_PPC64_TOC:
619 case R_PPC64_ADDR64:
620 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000621 }
622}
623
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000624void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
625 uint64_t P, uint64_t SA) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000626 uint64_t TB = getPPC64TocBase();
627
Hal Finkel3c8cc672015-10-12 20:56:18 +0000628 // For a TOC-relative relocation, adjust the addend and proceed in terms of
629 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000630 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +0000631 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
632 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
633 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
634 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
635 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
636 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000637 default: break;
638 }
639
Hal Finkel3c8cc672015-10-12 20:56:18 +0000640 switch (Type) {
641 case R_PPC64_ADDR16:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000642 if (!isInt<16>(SA))
Hal Finkel33e17a72015-10-15 16:17:30 +0000643 error("Relocation R_PPC64_ADDR16 overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000644 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000645 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000646 case R_PPC64_ADDR16_DS:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000647 if (!isInt<16>(SA))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000648 error("Relocation R_PPC64_ADDR16_DS overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000649 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000650 break;
651 case R_PPC64_ADDR16_LO:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000652 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000653 break;
654 case R_PPC64_ADDR16_LO_DS:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000655 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000656 break;
657 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000658 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000659 break;
660 case R_PPC64_ADDR16_HA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000661 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000662 break;
663 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000664 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000665 break;
666 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000667 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000668 break;
669 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000670 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000671 break;
672 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000673 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000674 break;
675 case R_PPC64_ADDR14: {
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000676 if ((SA & 3) != 0)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000677 error("Improper alignment for relocation R_PPC64_ADDR14");
678
679 // Preserve the AA/LK bits in the branch instruction
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000680 uint8_t AALK = Loc[3];
681 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000682 break;
683 }
684 case R_PPC64_REL16_LO:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000685 write16be(Loc, applyPPCLo(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000686 break;
687 case R_PPC64_REL16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000688 write16be(Loc, applyPPCHi(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000689 break;
690 case R_PPC64_REL16_HA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000691 write16be(Loc, applyPPCHa(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000692 break;
693 case R_PPC64_ADDR32:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000694 if (!isInt<32>(SA))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000695 error("Relocation R_PPC64_ADDR32 overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000696 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000697 break;
698 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +0000699 // If we have an undefined weak symbol, we might get here with a symbol
700 // address of zero. That could overflow, but the code must be unreachable,
701 // so don't bother doing anything at all.
702 if (!SA)
703 break;
704
Hal Finkeldaedc122015-10-12 23:16:53 +0000705 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
706 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000707 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000708
709 if (!InPlt && Out<ELF64BE>::Opd) {
710 // If this is a local call, and we currently have the address of a
711 // function-descriptor, get the underlying code address instead.
712 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
713 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000714 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000715
716 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000717 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +0000718 }
719
Hal Finkel3c8cc672015-10-12 20:56:18 +0000720 uint32_t Mask = 0x03FFFFFC;
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000721 if (!isInt<24>(SA - P))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000722 error("Relocation R_PPC64_REL24 overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000723 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000724
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000725 uint32_t Nop = 0x60000000;
726 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
727 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000728 break;
729 }
730 case R_PPC64_REL32:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000731 if (!isInt<32>(SA - P))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000732 error("Relocation R_PPC64_REL32 overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000733 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000734 break;
735 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000736 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000737 break;
738 case R_PPC64_ADDR64:
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000739 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000740 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000741 break;
742 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000743 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000744 }
745}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000746
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000747AArch64TargetInfo::AArch64TargetInfo() {
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000748 GotReloc = R_AARCH64_GLOB_DAT;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000749 PltReloc = R_AARCH64_JUMP_SLOT;
750 LazyRelocations = true;
751 PltEntrySize = 16;
752 PltZeroEntrySize = 32;
753}
George Rimar648a2c32015-10-20 08:54:27 +0000754
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000755unsigned AArch64TargetInfo::getGotRefReloc(unsigned Type) const { return Type; }
756
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000757unsigned AArch64TargetInfo::getPltRefReloc(unsigned Type) const { return Type; }
758
759void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
760 write64le(Buf, Out<ELF64LE>::Plt->getVA());
761}
762
George Rimar648a2c32015-10-20 08:54:27 +0000763void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000764 uint64_t PltEntryAddr) const {
765 const uint8_t PltData[] = {
766 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
767 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
768 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
769 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
770 0x20, 0x02, 0x1f, 0xd6, // br x17
771 0x1f, 0x20, 0x03, 0xd5, // nop
772 0x1f, 0x20, 0x03, 0xd5, // nop
773 0x1f, 0x20, 0x03, 0xd5 // nop
774 };
775 memcpy(Buf, PltData, sizeof(PltData));
776
777 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr + 4,
778 GotEntryAddr + 16);
779 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 8,
780 GotEntryAddr + 16);
781 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 12,
782 GotEntryAddr + 16);
783}
784
George Rimar21c0a712015-11-25 21:37:59 +0000785void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
786 uint64_t GotEntryAddr,
787 uint64_t PltEntryAddr, int32_t Index,
788 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000789 const uint8_t Inst[] = {
790 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
791 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
792 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
793 0x20, 0x02, 0x1f, 0xd6 // br x17
794 };
795 memcpy(Buf, Inst, sizeof(Inst));
796
797 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
798 GotEntryAddr);
799 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
800 GotEntryAddr);
801 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
802 GotEntryAddr);
803}
804
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000805bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
806 const SymbolBody &S) const {
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000807 return Type == R_AARCH64_ADR_GOT_PAGE || Type == R_AARCH64_LD64_GOT_LO12_NC ||
808 relocNeedsPlt(Type, S);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000809}
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000810
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000811bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
812 const SymbolBody &S) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000813 switch (Type) {
814 default:
815 return false;
816 case R_AARCH64_JUMP26:
817 case R_AARCH64_CALL26:
818 return canBePreempted(&S, true);
819 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000820}
Davide Italiano1d750a62015-09-27 08:45:38 +0000821
Davide Italianoef4be6b2015-10-06 19:01:32 +0000822static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000823 uint32_t ImmLo = (Imm & 0x3) << 29;
824 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
825 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000826 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000827}
828
Davide Italiano318ca222015-10-02 22:13:51 +0000829// Page(Expr) is the page address of the expression Expr, defined
830// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +0000831// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +0000832static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +0000833 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000834}
835
Igor Kudrinee252de2015-11-23 17:16:09 +0000836template <unsigned N>
837static void checkAArch64OutOfRange(int64_t X, uint32_t Type) {
838 if (!isInt<N>(X))
839 error("Relocation " + getELFRelocationTypeName(EM_AARCH64, Type) +
840 " out of range");
841}
842
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000843void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
844 uint32_t Type, uint64_t P,
845 uint64_t SA) const {
Davide Italiano1d750a62015-09-27 08:45:38 +0000846 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +0000847 case R_AARCH64_ABS16:
Igor Kudrinee252de2015-11-23 17:16:09 +0000848 checkAArch64OutOfRange<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000849 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000850 break;
851 case R_AARCH64_ABS32:
Igor Kudrinee252de2015-11-23 17:16:09 +0000852 checkAArch64OutOfRange<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000853 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000854 break;
855 case R_AARCH64_ABS64:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000856 // No overflow check needed.
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000857 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000858 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +0000859 case R_AARCH64_ADD_ABS_LO12_NC:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000860 // No overflow check needed.
Davide Italianoa7165742015-10-16 21:06:55 +0000861 // This relocation stores 12 bits and there's no instruction
862 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000863 // of r_addend bitwise-or'ed Loc. This assumes that the addend
864 // bits in Loc are zero.
865 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +0000866 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000867 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000868 uint64_t X = SA - P;
Igor Kudrinee252de2015-11-23 17:16:09 +0000869 checkAArch64OutOfRange<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000870 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +0000871 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000872 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000873 case R_AARCH64_ADR_GOT_PAGE:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000874 case R_AARCH64_ADR_PREL_PG_HI21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000875 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrinee252de2015-11-23 17:16:09 +0000876 checkAArch64OutOfRange<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000877 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000878 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000879 }
Igor Kudrinb34115b2015-11-13 03:26:59 +0000880 case R_AARCH64_JUMP26:
881 case R_AARCH64_CALL26: {
882 uint64_t X = SA - P;
Igor Kudrinee252de2015-11-23 17:16:09 +0000883 checkAArch64OutOfRange<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +0000884 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
885 break;
886 }
Davide Italianodc67f9b2015-11-20 21:35:38 +0000887 case R_AARCH64_LDST32_ABS_LO12_NC:
888 // No overflow check needed.
889 or32le(Loc, (SA & 0xFFC) << 8);
890 break;
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000891 case R_AARCH64_LD64_GOT_LO12_NC:
892 if (SA & 0x7)
893 error("Relocation R_AARCH64_LD64_GOT_LO12_NC not aligned");
894 // No overflow check needed.
895 or32le(Loc, (SA & 0xFF8) << 7);
896 break;
Davide Italianobbcc7f62015-11-08 04:45:26 +0000897 case R_AARCH64_LDST64_ABS_LO12_NC:
898 // No overflow check needed.
899 or32le(Loc, (SA & 0xFF8) << 7);
900 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +0000901 case R_AARCH64_LDST8_ABS_LO12_NC:
902 // No overflow check needed.
903 or32le(Loc, (SA & 0xFFF) << 10);
904 break;
Davide Italiano3300b792015-10-29 19:55:59 +0000905 case R_AARCH64_PREL16:
Igor Kudrinee252de2015-11-23 17:16:09 +0000906 checkAArch64OutOfRange<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +0000907 write16le(Loc, SA - P);
908 break;
909 case R_AARCH64_PREL32:
Igor Kudrinee252de2015-11-23 17:16:09 +0000910 checkAArch64OutOfRange<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +0000911 write32le(Loc, SA - P);
912 break;
Davide Italianob12d6682015-10-28 16:14:18 +0000913 case R_AARCH64_PREL64:
914 // No overflow check needed.
915 write64le(Loc, SA - P);
916 break;
Davide Italiano1d750a62015-09-27 08:45:38 +0000917 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000918 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +0000919 }
920}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000921
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000922template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +0000923 PageSize = 65536;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000924 GotRefReloc = R_MIPS_GOT16;
925 GotHeaderEntriesNum = 2;
926}
927
928template <class ELFT>
Simon Atanasyan96306bb2015-11-25 20:58:52 +0000929unsigned MipsTargetInfo<ELFT>::getGotRefReloc(unsigned Type) const {
930 return Type;
931}
932
933template <class ELFT>
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000934void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const {
935 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
936 auto *P = reinterpret_cast<Elf_Off *>(Buf);
937 // Module pointer
938 P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000;
Simon Atanasyan49829a12015-09-29 05:34:03 +0000939}
940
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000941template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000942void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
943template <class ELFT>
944void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
945 uint64_t PltEntryAddr) const {}
946template <class ELFT>
George Rimar21c0a712015-11-25 21:37:59 +0000947void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
948 uint64_t GotEntryAddr,
949 uint64_t PltEntryAddr, int32_t Index,
950 unsigned RelOff) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000951
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000952template <class ELFT>
953bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type,
954 const SymbolBody &S) const {
Simon Atanasyan96306bb2015-11-25 20:58:52 +0000955 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000956}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000957
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000958template <class ELFT>
959bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type,
960 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000961 return false;
962}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000963
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000964template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000965void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
966 uint32_t Type, uint64_t P,
967 uint64_t SA) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +0000968 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000969 switch (Type) {
970 case R_MIPS_32:
Rafael Espindolae7e57b22015-11-09 21:43:00 +0000971 add32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000972 break;
Simon Atanasyan96306bb2015-11-25 20:58:52 +0000973 case R_MIPS_CALL16:
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000974 case R_MIPS_GOT16: {
975 int64_t V = SA - getMipsGpAddr<ELFT>();
Simon Atanasyan96306bb2015-11-25 20:58:52 +0000976 if (Type == R_MIPS_GOT16 && !isInt<16>(V))
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000977 error("Relocation R_MIPS_GOT16 out of range");
Rafael Espindolae7e57b22015-11-09 21:43:00 +0000978 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000979 break;
980 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000981 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000982 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000983 }
984}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000985
986template <class ELFT>
987typename llvm::object::ELFFile<ELFT>::uintX_t getMipsGpAddr() {
988 const unsigned GPOffset = 0x7ff0;
989 return Out<ELFT>::Got->getVA() ? (Out<ELFT>::Got->getVA() + GPOffset) : 0;
990}
991
992template uint32_t getMipsGpAddr<ELF32LE>();
993template uint32_t getMipsGpAddr<ELF32BE>();
994template uint64_t getMipsGpAddr<ELF64LE>();
995template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +0000996}
997}