blob: 2a6120574475a60b89c1f1f3e47263b4c659f771 [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//
Nico Weberd08aa5c2016-08-24 16:36:41 +000013// Refer the ELF spec for the single letter variables, S, A or P, used
Rafael Espindola22ef9562016-04-13 01:40:19 +000014// in this file.
Rui Ueyama34f29242015-10-13 19:51:57 +000015//
Rui Ueyama55274e32016-04-23 01:10:15 +000016// Some functions defined in this file has "relaxTls" as part of their names.
17// They do peephole optimization for TLS variables by rewriting instructions.
18// They are not part of the ABI but optional optimization, so you can skip
19// them if you are not interested in how TLS variables are optimized.
20// See the following paper for the details.
21//
22// Ulrich Drepper, ELF Handling For Thread-Local Storage
23// http://www.akkadia.org/drepper/tls.pdf
24//
Rui Ueyama34f29242015-10-13 19:51:57 +000025//===----------------------------------------------------------------------===//
Rafael Espindola01205f72015-09-22 18:19:46 +000026
27#include "Target.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000028#include "Error.h"
Simon Atanasyan13f6da12016-03-31 21:26:23 +000029#include "InputFiles.h"
Rui Ueyamaaf21d922015-10-08 20:06:07 +000030#include "OutputSections.h"
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000031#include "Symbols.h"
Eugene Leviant41ca3272016-11-10 09:48:29 +000032#include "SyntheticSections.h"
Peter Smithfb05cd92016-07-08 16:10:27 +000033#include "Thunks.h"
Simon Atanasyan9e0297b2016-11-05 22:58:01 +000034#include "Writer.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000035
36#include "llvm/ADT/ArrayRef.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000037#include "llvm/Object/ELF.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000038#include "llvm/Support/Endian.h"
39#include "llvm/Support/ELF.h"
40
41using namespace llvm;
Rafael Espindolac4010882015-09-22 20:54:08 +000042using namespace llvm::object;
Rafael Espindola0872ea32015-09-24 14:16:02 +000043using namespace llvm::support::endian;
Rafael Espindola01205f72015-09-22 18:19:46 +000044using namespace llvm::ELF;
45
46namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000047namespace elf {
Rafael Espindola01205f72015-09-22 18:19:46 +000048
Rui Ueyamac1c282a2016-02-11 21:18:01 +000049TargetInfo *Target;
Rafael Espindola01205f72015-09-22 18:19:46 +000050
Rafael Espindolae7e57b22015-11-09 21:43:00 +000051static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
Rui Ueyama7fd5c84f2016-11-01 18:30:26 +000052static void or32be(uint8_t *P, int32_t V) { write32be(P, read32be(P) | V); }
Rui Ueyamaefc23de2015-10-14 21:30:32 +000053
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +000054std::string toString(uint32_t Type) {
Rui Ueyama12ebff22016-06-07 18:10:12 +000055 return getELFRelocationTypeName(Config->EMachine, Type);
56}
57
Eugene Leviant84569e62016-11-29 08:05:44 +000058template <unsigned N>
59static void checkInt(uint8_t *Loc, int64_t V, uint32_t Type) {
Rui Ueyamabebf4892016-06-16 23:28:03 +000060 if (!isInt<N>(V))
Eugene Leviant84569e62016-11-29 08:05:44 +000061 error(getErrorLocation(Loc) + "relocation " + toString(Type) +
62 " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000063}
64
Eugene Leviant84569e62016-11-29 08:05:44 +000065template <unsigned N>
66static void checkUInt(uint8_t *Loc, uint64_t V, uint32_t Type) {
Rui Ueyamabebf4892016-06-16 23:28:03 +000067 if (!isUInt<N>(V))
Eugene Leviant84569e62016-11-29 08:05:44 +000068 error(getErrorLocation(Loc) + "relocation " + toString(Type) +
69 " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000070}
71
Eugene Leviant84569e62016-11-29 08:05:44 +000072template <unsigned N>
73static void checkIntUInt(uint8_t *Loc, uint64_t V, uint32_t Type) {
Rui Ueyamabebf4892016-06-16 23:28:03 +000074 if (!isInt<N>(V) && !isUInt<N>(V))
Eugene Leviant84569e62016-11-29 08:05:44 +000075 error(getErrorLocation(Loc) + "relocation " + toString(Type) +
76 " out of range");
Igor Kudrinfea8ed52015-11-26 10:05:24 +000077}
78
Eugene Leviant84569e62016-11-29 08:05:44 +000079template <unsigned N>
80static void checkAlignment(uint8_t *Loc, uint64_t V, uint32_t Type) {
Rui Ueyamabebf4892016-06-16 23:28:03 +000081 if ((V & (N - 1)) != 0)
Eugene Leviant84569e62016-11-29 08:05:44 +000082 error(getErrorLocation(Loc) + "improper alignment for relocation " +
83 toString(Type));
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000084}
85
Rui Ueyamaefc23de2015-10-14 21:30:32 +000086namespace {
87class X86TargetInfo final : public TargetInfo {
88public:
89 X86TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +000090 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola666625b2016-04-01 14:36:09 +000091 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000092 void writeGotPltHeader(uint8_t *Buf) const override;
George Rimar98b060d2016-03-06 06:01:07 +000093 uint32_t getDynRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +000094 bool isTlsLocalDynamicRel(uint32_t Type) const override;
95 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
96 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac9fee5f2016-06-16 16:14:50 +000097 void writeGotPlt(uint8_t *Buf, const SymbolBody &S) const override;
Rui Ueyama4a90f572016-06-16 16:28:50 +000098 void writePltHeader(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000099 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
100 int32_t Index, unsigned RelOff) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000101 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000102
Rafael Espindola69f54022016-06-04 23:22:34 +0000103 RelExpr adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
104 RelExpr Expr) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000105 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
106 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
107 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
108 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000109};
110
Rui Ueyama46626e12016-07-12 23:28:31 +0000111template <class ELFT> class X86_64TargetInfo final : public TargetInfo {
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000112public:
113 X86_64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000114 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Eugene Leviantab024a32016-11-25 08:56:36 +0000115 bool isPicRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000116 bool isTlsLocalDynamicRel(uint32_t Type) const override;
117 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
118 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000119 void writeGotPltHeader(uint8_t *Buf) const override;
Rui Ueyamac9fee5f2016-06-16 16:14:50 +0000120 void writeGotPlt(uint8_t *Buf, const SymbolBody &S) const override;
Rui Ueyama4a90f572016-06-16 16:28:50 +0000121 void writePltHeader(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000122 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
123 int32_t Index, unsigned RelOff) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000124 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000125
Rafael Espindola5c66b822016-06-04 22:58:54 +0000126 RelExpr adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
127 RelExpr Expr) const override;
George Rimar5c33b912016-05-25 14:31:37 +0000128 void relaxGot(uint8_t *Loc, uint64_t Val) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000129 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
130 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
131 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
132 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
George Rimarb7204302016-06-02 09:22:00 +0000133
134private:
135 void relaxGotNoPic(uint8_t *Loc, uint64_t Val, uint8_t Op,
136 uint8_t ModRm) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000137};
138
Davide Italiano8c3444362016-01-11 19:45:33 +0000139class PPCTargetInfo final : public TargetInfo {
140public:
141 PPCTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000142 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000143 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000144};
145
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000146class PPC64TargetInfo final : public TargetInfo {
147public:
148 PPC64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000149 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000150 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
151 int32_t Index, unsigned RelOff) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000152 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000153};
154
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000155class AArch64TargetInfo final : public TargetInfo {
156public:
157 AArch64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000158 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Eugene Leviantab024a32016-11-25 08:56:36 +0000159 bool isPicRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000160 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac9fee5f2016-06-16 16:14:50 +0000161 void writeGotPlt(uint8_t *Buf, const SymbolBody &S) const override;
Rui Ueyama4a90f572016-06-16 16:28:50 +0000162 void writePltHeader(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000163 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
164 int32_t Index, unsigned RelOff) const override;
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000165 bool usesOnlyLowPageBits(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000166 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindolae1979ae2016-06-04 23:33:31 +0000167 RelExpr adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
168 RelExpr Expr) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000169 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindolae1979ae2016-06-04 23:33:31 +0000170 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000171 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000172};
173
Tom Stellard80efb162016-01-07 03:59:08 +0000174class AMDGPUTargetInfo final : public TargetInfo {
175public:
Tom Stellard391e3a82016-07-04 19:19:07 +0000176 AMDGPUTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000177 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
178 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Tom Stellard80efb162016-01-07 03:59:08 +0000179};
180
Peter Smith8646ced2016-06-07 09:31:52 +0000181class ARMTargetInfo final : public TargetInfo {
182public:
183 ARMTargetInfo();
184 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Eugene Leviantab024a32016-11-25 08:56:36 +0000185 bool isPicRel(uint32_t Type) const override;
Peter Smith8646ced2016-06-07 09:31:52 +0000186 uint32_t getDynRel(uint32_t Type) const override;
187 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
Peter Smith441cf5d2016-07-20 14:56:26 +0000188 bool isTlsLocalDynamicRel(uint32_t Type) const override;
Peter Smith9d450252016-07-20 08:52:27 +0000189 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
190 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac9fee5f2016-06-16 16:14:50 +0000191 void writeGotPlt(uint8_t *Buf, const SymbolBody &S) const override;
Rui Ueyama4a90f572016-06-16 16:28:50 +0000192 void writePltHeader(uint8_t *Buf) const override;
Peter Smith8646ced2016-06-07 09:31:52 +0000193 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
194 int32_t Index, unsigned RelOff) const override;
George Rimara4c7e742016-10-20 08:36:42 +0000195 RelExpr getThunkExpr(RelExpr Expr, uint32_t RelocType, const InputFile &File,
Peter Smithfb05cd92016-07-08 16:10:27 +0000196 const SymbolBody &S) const override;
Peter Smith8646ced2016-06-07 09:31:52 +0000197 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
198};
199
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000200template <class ELFT> class MipsTargetInfo final : public TargetInfo {
201public:
202 MipsTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000203 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola666625b2016-04-01 14:36:09 +0000204 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
Eugene Leviantab024a32016-11-25 08:56:36 +0000205 bool isPicRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000206 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan002e2442016-06-23 15:26:31 +0000207 bool isTlsLocalDynamicRel(uint32_t Type) const override;
208 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
Rui Ueyamac9fee5f2016-06-16 16:14:50 +0000209 void writeGotPlt(uint8_t *Buf, const SymbolBody &S) const override;
Rui Ueyama4a90f572016-06-16 16:28:50 +0000210 void writePltHeader(uint8_t *Buf) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000211 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
212 int32_t Index, unsigned RelOff) const override;
George Rimara4c7e742016-10-20 08:36:42 +0000213 RelExpr getThunkExpr(RelExpr Expr, uint32_t RelocType, const InputFile &File,
Peter Smithfb05cd92016-07-08 16:10:27 +0000214 const SymbolBody &S) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000215 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000216 bool usesOnlyLowPageBits(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000217};
218} // anonymous namespace
219
Rui Ueyama91004392015-10-13 16:08:15 +0000220TargetInfo *createTarget() {
221 switch (Config->EMachine) {
222 case EM_386:
Rui Ueyama6c509902016-08-03 20:15:56 +0000223 case EM_IAMCU:
Rui Ueyama91004392015-10-13 16:08:15 +0000224 return new X86TargetInfo();
225 case EM_AARCH64:
226 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000227 case EM_AMDGPU:
228 return new AMDGPUTargetInfo();
Peter Smith8646ced2016-06-07 09:31:52 +0000229 case EM_ARM:
230 return new ARMTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000231 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000232 switch (Config->EKind) {
233 case ELF32LEKind:
234 return new MipsTargetInfo<ELF32LE>();
235 case ELF32BEKind:
236 return new MipsTargetInfo<ELF32BE>();
Simon Atanasyanae77ab72016-04-29 10:39:17 +0000237 case ELF64LEKind:
238 return new MipsTargetInfo<ELF64LE>();
239 case ELF64BEKind:
240 return new MipsTargetInfo<ELF64BE>();
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000241 default:
George Rimar777f9632016-03-12 08:31:34 +0000242 fatal("unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000243 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000244 case EM_PPC:
245 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000246 case EM_PPC64:
247 return new PPC64TargetInfo();
248 case EM_X86_64:
Rui Ueyama46626e12016-07-12 23:28:31 +0000249 if (Config->EKind == ELF32LEKind)
250 return new X86_64TargetInfo<ELF32LE>();
251 return new X86_64TargetInfo<ELF64LE>();
Rui Ueyama91004392015-10-13 16:08:15 +0000252 }
George Rimar777f9632016-03-12 08:31:34 +0000253 fatal("unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000254}
255
Rafael Espindola01205f72015-09-22 18:19:46 +0000256TargetInfo::~TargetInfo() {}
257
Rafael Espindola666625b2016-04-01 14:36:09 +0000258uint64_t TargetInfo::getImplicitAddend(const uint8_t *Buf,
259 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000260 return 0;
261}
262
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000263bool TargetInfo::usesOnlyLowPageBits(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000264
Peter Smithfb05cd92016-07-08 16:10:27 +0000265RelExpr TargetInfo::getThunkExpr(RelExpr Expr, uint32_t RelocType,
266 const InputFile &File,
267 const SymbolBody &S) const {
268 return Expr;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000269}
270
George Rimar98b060d2016-03-06 06:01:07 +0000271bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000272
George Rimar98b060d2016-03-06 06:01:07 +0000273bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000274
George Rimara4c7e742016-10-20 08:36:42 +0000275bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const { return false; }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000276
Rafael Espindola5c66b822016-06-04 22:58:54 +0000277RelExpr TargetInfo::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
278 RelExpr Expr) const {
George Rimarf10c8292016-06-01 16:45:30 +0000279 return Expr;
George Rimar5c33b912016-05-25 14:31:37 +0000280}
281
282void TargetInfo::relaxGot(uint8_t *Loc, uint64_t Val) const {
283 llvm_unreachable("Should not have claimed to be relaxable");
284}
285
Rafael Espindola22ef9562016-04-13 01:40:19 +0000286void TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
287 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000288 llvm_unreachable("Should not have claimed to be relaxable");
289}
290
Rafael Espindola22ef9562016-04-13 01:40:19 +0000291void TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
292 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000293 llvm_unreachable("Should not have claimed to be relaxable");
294}
295
Rafael Espindola22ef9562016-04-13 01:40:19 +0000296void TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
297 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000298 llvm_unreachable("Should not have claimed to be relaxable");
299}
300
Rafael Espindola22ef9562016-04-13 01:40:19 +0000301void TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
302 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000303 llvm_unreachable("Should not have claimed to be relaxable");
George Rimar6713cf82015-11-25 21:46:05 +0000304}
George Rimar77d1cb12015-11-24 09:00:06 +0000305
Rafael Espindola7f074422015-09-22 21:35:51 +0000306X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000307 CopyRel = R_386_COPY;
308 GotRel = R_386_GLOB_DAT;
309 PltRel = R_386_JUMP_SLOT;
310 IRelativeRel = R_386_IRELATIVE;
311 RelativeRel = R_386_RELATIVE;
312 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000313 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
314 TlsOffsetRel = R_386_TLS_DTPOFF32;
Rui Ueyama803b1202016-07-13 18:55:14 +0000315 GotEntrySize = 4;
316 GotPltEntrySize = 4;
George Rimar77b77792015-11-25 22:15:01 +0000317 PltEntrySize = 16;
Rui Ueyama4a90f572016-06-16 16:28:50 +0000318 PltHeaderSize = 16;
Rafael Espindolaf807d472016-06-04 23:04:39 +0000319 TlsGdRelaxSkip = 2;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000320}
321
322RelExpr X86TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
323 switch (Type) {
324 default:
325 return R_ABS;
Rafael Espindoladf172772016-04-18 01:29:15 +0000326 case R_386_TLS_GD:
327 return R_TLSGD;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000328 case R_386_TLS_LDM:
329 return R_TLSLD;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000330 case R_386_PLT32:
Rafael Espindolab312a742016-04-21 17:30:24 +0000331 return R_PLT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000332 case R_386_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000333 return R_PC;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000334 case R_386_GOTPC:
Rafael Espindola79202c32016-08-31 23:24:11 +0000335 return R_GOTONLY_PC_FROM_END;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000336 case R_386_TLS_IE:
337 return R_GOT;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000338 case R_386_GOT32:
Rafael Espindolad03e6592016-07-06 21:41:39 +0000339 case R_386_GOT32X:
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000340 case R_386_TLS_GOTIE:
341 return R_GOT_FROM_END;
342 case R_386_GOTOFF:
Rafael Espindola79202c32016-08-31 23:24:11 +0000343 return R_GOTREL_FROM_END;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000344 case R_386_TLS_LE:
345 return R_TLS;
346 case R_386_TLS_LE_32:
347 return R_NEG_TLS;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000348 }
George Rimar77b77792015-11-25 22:15:01 +0000349}
350
Rafael Espindola69f54022016-06-04 23:22:34 +0000351RelExpr X86TargetInfo::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
352 RelExpr Expr) const {
353 switch (Expr) {
354 default:
355 return Expr;
356 case R_RELAX_TLS_GD_TO_IE:
357 return R_RELAX_TLS_GD_TO_IE_END;
358 case R_RELAX_TLS_GD_TO_LE:
359 return R_RELAX_TLS_GD_TO_LE_NEG;
360 }
361}
362
Rui Ueyamac516ae12016-01-29 02:33:45 +0000363void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Eugene Leviant6380ce22016-11-15 12:26:55 +0000364 write32le(Buf, In<ELF32LE>::Dynamic->getVA());
George Rimar77b77792015-11-25 22:15:01 +0000365}
366
Rui Ueyamac9fee5f2016-06-16 16:14:50 +0000367void X86TargetInfo::writeGotPlt(uint8_t *Buf, const SymbolBody &S) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000368 // Entries in .got.plt initially points back to the corresponding
369 // PLT entries with a fixed offset to skip the first instruction.
Rui Ueyamac9fee5f2016-06-16 16:14:50 +0000370 write32le(Buf, S.getPltVA<ELF32LE>() + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000371}
Rafael Espindola01205f72015-09-22 18:19:46 +0000372
George Rimar98b060d2016-03-06 06:01:07 +0000373uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000374 if (Type == R_386_TLS_LE)
375 return R_386_TLS_TPOFF;
376 if (Type == R_386_TLS_LE_32)
377 return R_386_TLS_TPOFF32;
378 return Type;
379}
380
George Rimar98b060d2016-03-06 06:01:07 +0000381bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000382 return Type == R_386_TLS_GD;
383}
384
George Rimar98b060d2016-03-06 06:01:07 +0000385bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000386 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
387}
388
George Rimar98b060d2016-03-06 06:01:07 +0000389bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000390 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
391}
392
Rui Ueyama4a90f572016-06-16 16:28:50 +0000393void X86TargetInfo::writePltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000394 // Executable files and shared object files have
395 // separate procedure linkage tables.
George Rimar786e8662016-03-17 05:57:33 +0000396 if (Config->Pic) {
George Rimar77b77792015-11-25 22:15:01 +0000397 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000398 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000399 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
400 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000401 };
402 memcpy(Buf, V, sizeof(V));
403 return;
404 }
George Rimar648a2c32015-10-20 08:54:27 +0000405
George Rimar77b77792015-11-25 22:15:01 +0000406 const uint8_t PltData[] = {
407 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000408 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
409 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000410 };
411 memcpy(Buf, PltData, sizeof(PltData));
Eugene Leviant41ca3272016-11-10 09:48:29 +0000412 uint32_t Got = In<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000413 write32le(Buf + 2, Got + 4);
414 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000415}
416
Rui Ueyama9398f862016-01-29 04:15:02 +0000417void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
418 uint64_t PltEntryAddr, int32_t Index,
419 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000420 const uint8_t Inst[] = {
421 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
422 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
423 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
424 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000425 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000426
George Rimar77b77792015-11-25 22:15:01 +0000427 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
George Rimar786e8662016-03-17 05:57:33 +0000428 Buf[1] = Config->Pic ? 0xa3 : 0x25;
Eugene Leviant41ca3272016-11-10 09:48:29 +0000429 uint32_t Got = In<ELF32LE>::GotPlt->getVA();
Rui Ueyama9398f862016-01-29 04:15:02 +0000430 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000431 write32le(Buf + 7, RelOff);
Rui Ueyama4a90f572016-06-16 16:28:50 +0000432 write32le(Buf + 12, -Index * PltEntrySize - PltHeaderSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000433}
434
Rafael Espindola666625b2016-04-01 14:36:09 +0000435uint64_t X86TargetInfo::getImplicitAddend(const uint8_t *Buf,
436 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000437 switch (Type) {
438 default:
439 return 0;
440 case R_386_32:
441 case R_386_GOT32:
Rafael Espindola9639ec12016-07-06 21:48:50 +0000442 case R_386_GOT32X:
Rafael Espindolada99df32016-03-30 12:40:38 +0000443 case R_386_GOTOFF:
444 case R_386_GOTPC:
445 case R_386_PC32:
446 case R_386_PLT32:
Ed Schouten21483f52016-08-20 10:54:51 +0000447 case R_386_TLS_LE:
Rafael Espindolada99df32016-03-30 12:40:38 +0000448 return read32le(Buf);
449 }
450}
451
Rafael Espindola22ef9562016-04-13 01:40:19 +0000452void X86TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
453 uint64_t Val) const {
Eugene Leviant84569e62016-11-29 08:05:44 +0000454 checkInt<32>(Loc, Val, Type);
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000455 write32le(Loc, Val);
Rafael Espindolac4010882015-09-22 20:54:08 +0000456}
457
Rafael Espindola22ef9562016-04-13 01:40:19 +0000458void X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
459 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000460 // Convert
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000461 // leal x@tlsgd(, %ebx, 1),
462 // call __tls_get_addr@plt
Rui Ueyama55274e32016-04-23 01:10:15 +0000463 // to
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000464 // movl %gs:0,%eax
Rui Ueyama55274e32016-04-23 01:10:15 +0000465 // subl $x@ntpoff,%eax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000466 const uint8_t Inst[] = {
467 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
468 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
469 };
470 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindolaebed1fe2016-05-20 21:23:52 +0000471 relocateOne(Loc + 5, R_386_32, Val);
George Rimar2558e122015-12-09 09:55:54 +0000472}
473
Rafael Espindola22ef9562016-04-13 01:40:19 +0000474void X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
475 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000476 // Convert
477 // leal x@tlsgd(, %ebx, 1),
478 // call __tls_get_addr@plt
479 // to
480 // movl %gs:0, %eax
481 // addl x@gotntpoff(%ebx), %eax
George Rimar2558e122015-12-09 09:55:54 +0000482 const uint8_t Inst[] = {
483 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
484 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
485 };
486 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola74f3dbe2016-05-20 20:09:35 +0000487 relocateOne(Loc + 5, R_386_32, Val);
George Rimar2558e122015-12-09 09:55:54 +0000488}
489
George Rimar6f17e092015-12-17 09:32:21 +0000490// In some conditions, relocations can be optimized to avoid using GOT.
491// This function does that for Initial Exec to Local Exec case.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000492void X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
493 uint64_t Val) const {
George Rimar6f17e092015-12-17 09:32:21 +0000494 // Ulrich's document section 6.2 says that @gotntpoff can
495 // be used with MOVL or ADDL instructions.
496 // @indntpoff is similar to @gotntpoff, but for use in
497 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000498 uint8_t Reg = (Loc[-1] >> 3) & 7;
Rui Ueyamab319ae22016-06-21 05:44:14 +0000499
George Rimar6f17e092015-12-17 09:32:21 +0000500 if (Type == R_386_TLS_IE) {
Rui Ueyamab319ae22016-06-21 05:44:14 +0000501 if (Loc[-1] == 0xa1) {
502 // "movl foo@indntpoff,%eax" -> "movl $foo,%eax"
503 // This case is different from the generic case below because
504 // this is a 5 byte instruction while below is 6 bytes.
505 Loc[-1] = 0xb8;
506 } else if (Loc[-2] == 0x8b) {
507 // "movl foo@indntpoff,%reg" -> "movl $foo,%reg"
508 Loc[-2] = 0xc7;
509 Loc[-1] = 0xc0 | Reg;
George Rimar6f17e092015-12-17 09:32:21 +0000510 } else {
Rui Ueyamab319ae22016-06-21 05:44:14 +0000511 // "addl foo@indntpoff,%reg" -> "addl $foo,%reg"
512 Loc[-2] = 0x81;
513 Loc[-1] = 0xc0 | Reg;
George Rimar6f17e092015-12-17 09:32:21 +0000514 }
515 } else {
Rui Ueyamab319ae22016-06-21 05:44:14 +0000516 assert(Type == R_386_TLS_GOTIE);
517 if (Loc[-2] == 0x8b) {
518 // "movl foo@gottpoff(%rip),%reg" -> "movl $foo,%reg"
519 Loc[-2] = 0xc7;
520 Loc[-1] = 0xc0 | Reg;
521 } else {
522 // "addl foo@gotntpoff(%rip),%reg" -> "leal foo(%reg),%reg"
523 Loc[-2] = 0x8d;
524 Loc[-1] = 0x80 | (Reg << 3) | Reg;
525 }
George Rimar6f17e092015-12-17 09:32:21 +0000526 }
Rafael Espindola8818ca62016-05-20 17:41:09 +0000527 relocateOne(Loc, R_386_TLS_LE, Val);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000528}
529
Rafael Espindola22ef9562016-04-13 01:40:19 +0000530void X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
531 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000532 if (Type == R_386_TLS_LDO_32) {
Rafael Espindola8818ca62016-05-20 17:41:09 +0000533 relocateOne(Loc, R_386_TLS_LE, Val);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000534 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000535 }
536
Rui Ueyama55274e32016-04-23 01:10:15 +0000537 // Convert
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000538 // leal foo(%reg),%eax
539 // call ___tls_get_addr
Rui Ueyama55274e32016-04-23 01:10:15 +0000540 // to
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000541 // movl %gs:0,%eax
542 // nop
543 // leal 0(%esi,1),%esi
544 const uint8_t Inst[] = {
545 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
546 0x90, // nop
547 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
548 };
549 memcpy(Loc - 2, Inst, sizeof(Inst));
George Rimar2558e122015-12-09 09:55:54 +0000550}
551
Rui Ueyama46626e12016-07-12 23:28:31 +0000552template <class ELFT> X86_64TargetInfo<ELFT>::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000553 CopyRel = R_X86_64_COPY;
554 GotRel = R_X86_64_GLOB_DAT;
555 PltRel = R_X86_64_JUMP_SLOT;
556 RelativeRel = R_X86_64_RELATIVE;
557 IRelativeRel = R_X86_64_IRELATIVE;
558 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000559 TlsModuleIndexRel = R_X86_64_DTPMOD64;
560 TlsOffsetRel = R_X86_64_DTPOFF64;
Rui Ueyama803b1202016-07-13 18:55:14 +0000561 GotEntrySize = 8;
562 GotPltEntrySize = 8;
George Rimar648a2c32015-10-20 08:54:27 +0000563 PltEntrySize = 16;
Rui Ueyama4a90f572016-06-16 16:28:50 +0000564 PltHeaderSize = 16;
Rafael Espindolaf807d472016-06-04 23:04:39 +0000565 TlsGdRelaxSkip = 2;
Ed Maste8fd01962016-11-23 17:44:02 +0000566 // Align to the large page size (known as a superpage or huge page).
567 // FreeBSD automatically promotes large, superpage-aligned allocations.
Rui Ueyama835bd722016-11-23 22:10:46 +0000568 DefaultImageBase = 0x200000;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000569}
570
Rui Ueyama46626e12016-07-12 23:28:31 +0000571template <class ELFT>
572RelExpr X86_64TargetInfo<ELFT>::getRelExpr(uint32_t Type,
573 const SymbolBody &S) const {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000574 switch (Type) {
575 default:
576 return R_ABS;
Rafael Espindolaece62b92016-04-18 12:44:33 +0000577 case R_X86_64_TPOFF32:
578 return R_TLS;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000579 case R_X86_64_TLSLD:
580 return R_TLSLD_PC;
Rafael Espindoladf172772016-04-18 01:29:15 +0000581 case R_X86_64_TLSGD:
582 return R_TLSGD_PC;
Rafael Espindola3151d892016-04-14 18:39:44 +0000583 case R_X86_64_SIZE32:
584 case R_X86_64_SIZE64:
585 return R_SIZE;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000586 case R_X86_64_PLT32:
Rafael Espindolab312a742016-04-21 17:30:24 +0000587 return R_PLT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000588 case R_X86_64_PC32:
Rafael Espindola926bff82016-04-25 14:05:44 +0000589 case R_X86_64_PC64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000590 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000591 case R_X86_64_GOT32:
Rafael Espindolaf4c1cd42016-04-18 12:58:59 +0000592 return R_GOT_FROM_END;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000593 case R_X86_64_GOTPCREL:
Rafael Espindoladba64b82016-05-24 11:53:15 +0000594 case R_X86_64_GOTPCRELX:
595 case R_X86_64_REX_GOTPCRELX:
Rafael Espindolafe3a2f12016-05-24 12:12:06 +0000596 case R_X86_64_GOTTPOFF:
597 return R_GOT_PC;
Rafael Espindola5708b2f2016-12-02 08:00:09 +0000598 case R_X86_64_NONE:
599 return R_HINT;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000600 }
George Rimar648a2c32015-10-20 08:54:27 +0000601}
602
Rui Ueyama46626e12016-07-12 23:28:31 +0000603template <class ELFT>
604void X86_64TargetInfo<ELFT>::writeGotPltHeader(uint8_t *Buf) const {
Rafael Espindola4ee6cb32016-05-09 18:12:15 +0000605 // The first entry holds the value of _DYNAMIC. It is not clear why that is
606 // required, but it is documented in the psabi and the glibc dynamic linker
Rafael Espindolae5027512016-05-10 16:23:46 +0000607 // seems to use it (note that this is relevant for linking ld.so, not any
Rafael Espindola4ee6cb32016-05-09 18:12:15 +0000608 // other program).
Eugene Leviant6380ce22016-11-15 12:26:55 +0000609 write64le(Buf, In<ELFT>::Dynamic->getVA());
Igor Kudrin351b41d2015-11-16 17:44:08 +0000610}
611
Rui Ueyama46626e12016-07-12 23:28:31 +0000612template <class ELFT>
613void X86_64TargetInfo<ELFT>::writeGotPlt(uint8_t *Buf,
614 const SymbolBody &S) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000615 // See comments in X86TargetInfo::writeGotPlt.
Rui Ueyama46626e12016-07-12 23:28:31 +0000616 write32le(Buf, S.getPltVA<ELFT>() + 6);
George Rimar648a2c32015-10-20 08:54:27 +0000617}
618
Rui Ueyama46626e12016-07-12 23:28:31 +0000619template <class ELFT>
620void X86_64TargetInfo<ELFT>::writePltHeader(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000621 const uint8_t PltData[] = {
622 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
623 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
624 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
625 };
626 memcpy(Buf, PltData, sizeof(PltData));
Eugene Leviant41ca3272016-11-10 09:48:29 +0000627 uint64_t Got = In<ELFT>::GotPlt->getVA();
Eugene Leviantff23d3e2016-11-18 14:35:03 +0000628 uint64_t Plt = In<ELFT>::Plt->getVA();
Rui Ueyama900e2d22016-01-29 03:51:49 +0000629 write32le(Buf + 2, Got - Plt + 2); // GOT+8
630 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000631}
Rafael Espindola01205f72015-09-22 18:19:46 +0000632
Rui Ueyama46626e12016-07-12 23:28:31 +0000633template <class ELFT>
634void X86_64TargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
635 uint64_t PltEntryAddr, int32_t Index,
636 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000637 const uint8_t Inst[] = {
638 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
639 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
640 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
641 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000642 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000643
George Rimar648a2c32015-10-20 08:54:27 +0000644 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
645 write32le(Buf + 7, Index);
Rui Ueyama4a90f572016-06-16 16:28:50 +0000646 write32le(Buf + 12, -Index * PltEntrySize - PltHeaderSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000647}
648
Rui Ueyama46626e12016-07-12 23:28:31 +0000649template <class ELFT>
Eugene Leviantab024a32016-11-25 08:56:36 +0000650bool X86_64TargetInfo<ELFT>::isPicRel(uint32_t Type) const {
651 return Type != R_X86_64_PC32 && Type != R_X86_64_32;
George Rimar86971052016-03-29 08:35:42 +0000652}
653
Rui Ueyama46626e12016-07-12 23:28:31 +0000654template <class ELFT>
655bool X86_64TargetInfo<ELFT>::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000656 return Type == R_X86_64_GOTTPOFF;
657}
658
Rui Ueyama46626e12016-07-12 23:28:31 +0000659template <class ELFT>
660bool X86_64TargetInfo<ELFT>::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000661 return Type == R_X86_64_TLSGD;
662}
663
Rui Ueyama46626e12016-07-12 23:28:31 +0000664template <class ELFT>
665bool X86_64TargetInfo<ELFT>::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000666 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
667 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000668}
669
Rui Ueyama46626e12016-07-12 23:28:31 +0000670template <class ELFT>
671void X86_64TargetInfo<ELFT>::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
672 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000673 // Convert
674 // .byte 0x66
675 // leaq x@tlsgd(%rip), %rdi
676 // .word 0x6666
677 // rex64
678 // call __tls_get_addr@plt
679 // to
680 // mov %fs:0x0,%rax
681 // lea x@tpoff,%rax
George Rimar6713cf82015-11-25 21:46:05 +0000682 const uint8_t Inst[] = {
683 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
684 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
685 };
686 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola91e9fc02016-05-20 21:09:59 +0000687 // The original code used a pc relative relocation and so we have to
688 // compensate for the -4 in had in the addend.
Rafael Espindola8818ca62016-05-20 17:41:09 +0000689 relocateOne(Loc + 8, R_X86_64_TPOFF32, Val + 4);
George Rimar77d1cb12015-11-24 09:00:06 +0000690}
691
Rui Ueyama46626e12016-07-12 23:28:31 +0000692template <class ELFT>
693void X86_64TargetInfo<ELFT>::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
694 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000695 // Convert
696 // .byte 0x66
697 // leaq x@tlsgd(%rip), %rdi
698 // .word 0x6666
699 // rex64
700 // call __tls_get_addr@plt
701 // to
702 // mov %fs:0x0,%rax
703 // addq x@tpoff,%rax
George Rimar25411f252015-12-04 11:20:13 +0000704 const uint8_t Inst[] = {
705 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
706 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
707 };
708 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola91e9fc02016-05-20 21:09:59 +0000709 // Both code sequences are PC relatives, but since we are moving the constant
710 // forward by 8 bytes we have to subtract the value by 8.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000711 relocateOne(Loc + 8, R_X86_64_PC32, Val - 8);
George Rimar25411f252015-12-04 11:20:13 +0000712}
713
George Rimar77d1cb12015-11-24 09:00:06 +0000714// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000715// R_X86_64_TPOFF32 so that it does not use GOT.
Rui Ueyama46626e12016-07-12 23:28:31 +0000716template <class ELFT>
717void X86_64TargetInfo<ELFT>::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
718 uint64_t Val) const {
Rui Ueyama55a9def2016-06-21 03:42:32 +0000719 uint8_t *Inst = Loc - 3;
George Rimar77d1cb12015-11-24 09:00:06 +0000720 uint8_t Reg = Loc[-1] >> 3;
Rui Ueyama3f5dd142016-06-21 05:01:31 +0000721 uint8_t *RegSlot = Loc - 1;
Rui Ueyama55274e32016-04-23 01:10:15 +0000722
Rui Ueyama73575c42016-06-21 05:09:39 +0000723 // Note that ADD with RSP or R12 is converted to ADD instead of LEA
Rui Ueyama55a9def2016-06-21 03:42:32 +0000724 // because LEA with these registers needs 4 bytes to encode and thus
725 // wouldn't fit the space.
726
727 if (memcmp(Inst, "\x48\x03\x25", 3) == 0) {
728 // "addq foo@gottpoff(%rip),%rsp" -> "addq $foo,%rsp"
729 memcpy(Inst, "\x48\x81\xc4", 3);
730 } else if (memcmp(Inst, "\x4c\x03\x25", 3) == 0) {
731 // "addq foo@gottpoff(%rip),%r12" -> "addq $foo,%r12"
732 memcpy(Inst, "\x49\x81\xc4", 3);
733 } else if (memcmp(Inst, "\x4c\x03", 2) == 0) {
734 // "addq foo@gottpoff(%rip),%r[8-15]" -> "leaq foo(%r[8-15]),%r[8-15]"
735 memcpy(Inst, "\x4d\x8d", 2);
736 *RegSlot = 0x80 | (Reg << 3) | Reg;
737 } else if (memcmp(Inst, "\x48\x03", 2) == 0) {
738 // "addq foo@gottpoff(%rip),%reg -> "leaq foo(%reg),%reg"
739 memcpy(Inst, "\x48\x8d", 2);
740 *RegSlot = 0x80 | (Reg << 3) | Reg;
741 } else if (memcmp(Inst, "\x4c\x8b", 2) == 0) {
742 // "movq foo@gottpoff(%rip),%r[8-15]" -> "movq $foo,%r[8-15]"
743 memcpy(Inst, "\x49\xc7", 2);
744 *RegSlot = 0xc0 | Reg;
745 } else if (memcmp(Inst, "\x48\x8b", 2) == 0) {
746 // "movq foo@gottpoff(%rip),%reg" -> "movq $foo,%reg"
747 memcpy(Inst, "\x48\xc7", 2);
748 *RegSlot = 0xc0 | Reg;
Rui Ueyama03a6cec2016-06-21 06:03:28 +0000749 } else {
Eugene Leviant84569e62016-11-29 08:05:44 +0000750 fatal(getErrorLocation(Loc - 3) +
751 "R_X86_64_GOTTPOFF must be used in MOVQ or ADDQ instructions only");
Rui Ueyama55a9def2016-06-21 03:42:32 +0000752 }
753
754 // The original code used a PC relative relocation.
755 // Need to compensate for the -4 it had in the addend.
Rafael Espindola8818ca62016-05-20 17:41:09 +0000756 relocateOne(Loc, R_X86_64_TPOFF32, Val + 4);
George Rimar77d1cb12015-11-24 09:00:06 +0000757}
758
Rui Ueyama46626e12016-07-12 23:28:31 +0000759template <class ELFT>
760void X86_64TargetInfo<ELFT>::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
761 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000762 // Convert
763 // leaq bar@tlsld(%rip), %rdi
764 // callq __tls_get_addr@PLT
765 // leaq bar@dtpoff(%rax), %rcx
766 // to
767 // .word 0x6666
768 // .byte 0x66
769 // mov %fs:0,%rax
770 // leaq bar@tpoff(%rax), %rcx
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000771 if (Type == R_X86_64_DTPOFF64) {
Rafael Espindola8818ca62016-05-20 17:41:09 +0000772 write64le(Loc, Val);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000773 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000774 }
775 if (Type == R_X86_64_DTPOFF32) {
Rafael Espindola8818ca62016-05-20 17:41:09 +0000776 relocateOne(Loc, R_X86_64_TPOFF32, Val);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000777 return;
George Rimar25411f252015-12-04 11:20:13 +0000778 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000779
780 const uint8_t Inst[] = {
Rui Ueyama02fcf112016-05-25 04:29:53 +0000781 0x66, 0x66, // .word 0x6666
782 0x66, // .byte 0x66
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000783 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
784 };
785 memcpy(Loc - 3, Inst, sizeof(Inst));
George Rimar6713cf82015-11-25 21:46:05 +0000786}
787
Rui Ueyama46626e12016-07-12 23:28:31 +0000788template <class ELFT>
789void X86_64TargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type,
790 uint64_t Val) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000791 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000792 case R_X86_64_32:
Eugene Leviant84569e62016-11-29 08:05:44 +0000793 checkUInt<32>(Loc, Val, Type);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000794 write32le(Loc, Val);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000795 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000796 case R_X86_64_32S:
Rafael Espindolaece62b92016-04-18 12:44:33 +0000797 case R_X86_64_TPOFF32:
Rafael Espindolaf4c1cd42016-04-18 12:58:59 +0000798 case R_X86_64_GOT32:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000799 case R_X86_64_GOTPCREL:
George Rimar9f8f4e32016-03-22 12:15:26 +0000800 case R_X86_64_GOTPCRELX:
801 case R_X86_64_REX_GOTPCRELX:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000802 case R_X86_64_PC32:
Rafael Espindola38bd2172016-05-04 15:51:23 +0000803 case R_X86_64_GOTTPOFF:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000804 case R_X86_64_PLT32:
805 case R_X86_64_TLSGD:
806 case R_X86_64_TLSLD:
Rafael Espindola3c20fb42016-04-18 11:53:42 +0000807 case R_X86_64_DTPOFF32:
George Rimar48651482015-12-11 08:59:37 +0000808 case R_X86_64_SIZE32:
Eugene Leviant84569e62016-11-29 08:05:44 +0000809 checkInt<32>(Loc, Val, Type);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000810 write32le(Loc, Val);
George Rimar48651482015-12-11 08:59:37 +0000811 break;
Rui Ueyamae66f45c2016-05-25 04:10:14 +0000812 case R_X86_64_64:
813 case R_X86_64_DTPOFF64:
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000814 case R_X86_64_GLOB_DAT:
Rui Ueyamae66f45c2016-05-25 04:10:14 +0000815 case R_X86_64_PC64:
Rafael Espindolad3b32df2016-11-29 03:36:30 +0000816 case R_X86_64_SIZE64:
Rui Ueyamae66f45c2016-05-25 04:10:14 +0000817 write64le(Loc, Val);
818 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000819 default:
Eugene Leviant84569e62016-11-29 08:05:44 +0000820 fatal(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000821 }
822}
823
Rui Ueyama46626e12016-07-12 23:28:31 +0000824template <class ELFT>
825RelExpr X86_64TargetInfo<ELFT>::adjustRelaxExpr(uint32_t Type,
826 const uint8_t *Data,
827 RelExpr RelExpr) const {
George Rimar5c33b912016-05-25 14:31:37 +0000828 if (Type != R_X86_64_GOTPCRELX && Type != R_X86_64_REX_GOTPCRELX)
George Rimarf10c8292016-06-01 16:45:30 +0000829 return RelExpr;
George Rimara8f9cf12016-05-26 13:37:12 +0000830 const uint8_t Op = Data[-2];
831 const uint8_t ModRm = Data[-1];
George Rimarf10c8292016-06-01 16:45:30 +0000832 // FIXME: When PIC is disabled and foo is defined locally in the
833 // lower 32 bit address space, memory operand in mov can be converted into
834 // immediate operand. Otherwise, mov must be changed to lea. We support only
835 // latter relaxation at this moment.
George Rimar95433df2016-05-25 16:51:08 +0000836 if (Op == 0x8b)
George Rimarf10c8292016-06-01 16:45:30 +0000837 return R_RELAX_GOT_PC;
George Rimar95433df2016-05-25 16:51:08 +0000838 // Relax call and jmp.
George Rimarf10c8292016-06-01 16:45:30 +0000839 if (Op == 0xff && (ModRm == 0x15 || ModRm == 0x25))
840 return R_RELAX_GOT_PC;
841
842 // Relaxation of test, adc, add, and, cmp, or, sbb, sub, xor.
843 // If PIC then no relaxation is available.
844 // We also don't relax test/binop instructions without REX byte,
845 // they are 32bit operations and not common to have.
846 assert(Type == R_X86_64_REX_GOTPCRELX);
847 return Config->Pic ? RelExpr : R_RELAX_GOT_PC_NOPIC;
George Rimar5c33b912016-05-25 14:31:37 +0000848}
849
George Rimarb7204302016-06-02 09:22:00 +0000850// A subset of relaxations can only be applied for no-PIC. This method
851// handles such relaxations. Instructions encoding information was taken from:
852// "Intel 64 and IA-32 Architectures Software Developer's Manual V2"
853// (http://www.intel.com/content/dam/www/public/us/en/documents/manuals/
854// 64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf)
Rui Ueyama46626e12016-07-12 23:28:31 +0000855template <class ELFT>
856void X86_64TargetInfo<ELFT>::relaxGotNoPic(uint8_t *Loc, uint64_t Val,
857 uint8_t Op, uint8_t ModRm) const {
George Rimarf10c8292016-06-01 16:45:30 +0000858 const uint8_t Rex = Loc[-3];
859 // Convert "test %reg, foo@GOTPCREL(%rip)" to "test $foo, %reg".
860 if (Op == 0x85) {
861 // See "TEST-Logical Compare" (4-428 Vol. 2B),
862 // TEST r/m64, r64 uses "full" ModR / M byte (no opcode extension).
863
864 // ModR/M byte has form XX YYY ZZZ, where
865 // YYY is MODRM.reg(register 2), ZZZ is MODRM.rm(register 1).
866 // XX has different meanings:
867 // 00: The operand's memory address is in reg1.
868 // 01: The operand's memory address is reg1 + a byte-sized displacement.
869 // 10: The operand's memory address is reg1 + a word-sized displacement.
870 // 11: The operand is reg1 itself.
871 // If an instruction requires only one operand, the unused reg2 field
872 // holds extra opcode bits rather than a register code
873 // 0xC0 == 11 000 000 binary.
874 // 0x38 == 00 111 000 binary.
875 // We transfer reg2 to reg1 here as operand.
876 // See "2.1.3 ModR/M and SIB Bytes" (Vol. 2A 2-3).
Rui Ueyama595bc5d2016-06-16 19:48:07 +0000877 Loc[-1] = 0xc0 | (ModRm & 0x38) >> 3; // ModR/M byte.
George Rimarf10c8292016-06-01 16:45:30 +0000878
879 // Change opcode from TEST r/m64, r64 to TEST r/m64, imm32
880 // See "TEST-Logical Compare" (4-428 Vol. 2B).
Rui Ueyama595bc5d2016-06-16 19:48:07 +0000881 Loc[-2] = 0xf7;
George Rimarf10c8292016-06-01 16:45:30 +0000882
883 // Move R bit to the B bit in REX byte.
884 // REX byte is encoded as 0100WRXB, where
885 // 0100 is 4bit fixed pattern.
886 // REX.W When 1, a 64-bit operand size is used. Otherwise, when 0, the
887 // default operand size is used (which is 32-bit for most but not all
888 // instructions).
889 // REX.R This 1-bit value is an extension to the MODRM.reg field.
890 // REX.X This 1-bit value is an extension to the SIB.index field.
891 // REX.B This 1-bit value is an extension to the MODRM.rm field or the
892 // SIB.base field.
893 // See "2.2.1.2 More on REX Prefix Fields " (2-8 Vol. 2A).
Rui Ueyama595bc5d2016-06-16 19:48:07 +0000894 Loc[-3] = (Rex & ~0x4) | (Rex & 0x4) >> 2;
George Rimarf10c8292016-06-01 16:45:30 +0000895 relocateOne(Loc, R_X86_64_PC32, Val);
896 return;
897 }
898
899 // If we are here then we need to relax the adc, add, and, cmp, or, sbb, sub
900 // or xor operations.
901
902 // Convert "binop foo@GOTPCREL(%rip), %reg" to "binop $foo, %reg".
903 // Logic is close to one for test instruction above, but we also
904 // write opcode extension here, see below for details.
Rui Ueyama595bc5d2016-06-16 19:48:07 +0000905 Loc[-1] = 0xc0 | (ModRm & 0x38) >> 3 | (Op & 0x3c); // ModR/M byte.
George Rimarf10c8292016-06-01 16:45:30 +0000906
907 // Primary opcode is 0x81, opcode extension is one of:
908 // 000b = ADD, 001b is OR, 010b is ADC, 011b is SBB,
909 // 100b is AND, 101b is SUB, 110b is XOR, 111b is CMP.
910 // This value was wrote to MODRM.reg in a line above.
911 // See "3.2 INSTRUCTIONS (A-M)" (Vol. 2A 3-15),
912 // "INSTRUCTION SET REFERENCE, N-Z" (Vol. 2B 4-1) for
913 // descriptions about each operation.
Rui Ueyama595bc5d2016-06-16 19:48:07 +0000914 Loc[-2] = 0x81;
915 Loc[-3] = (Rex & ~0x4) | (Rex & 0x4) >> 2;
George Rimar5c33b912016-05-25 14:31:37 +0000916 relocateOne(Loc, R_X86_64_PC32, Val);
917}
918
Rui Ueyama46626e12016-07-12 23:28:31 +0000919template <class ELFT>
920void X86_64TargetInfo<ELFT>::relaxGot(uint8_t *Loc, uint64_t Val) const {
George Rimarb7204302016-06-02 09:22:00 +0000921 const uint8_t Op = Loc[-2];
922 const uint8_t ModRm = Loc[-1];
923
Rui Ueyamaa71ba432016-06-16 23:28:05 +0000924 // Convert "mov foo@GOTPCREL(%rip),%reg" to "lea foo(%rip),%reg".
George Rimarb7204302016-06-02 09:22:00 +0000925 if (Op == 0x8b) {
Rui Ueyama595bc5d2016-06-16 19:48:07 +0000926 Loc[-2] = 0x8d;
George Rimarb7204302016-06-02 09:22:00 +0000927 relocateOne(Loc, R_X86_64_PC32, Val);
928 return;
929 }
930
Rui Ueyamaa71ba432016-06-16 23:28:05 +0000931 if (Op != 0xff) {
932 // We are relaxing a rip relative to an absolute, so compensate
933 // for the old -4 addend.
934 assert(!Config->Pic);
935 relaxGotNoPic(Loc, Val + 4, Op, ModRm);
936 return;
937 }
938
George Rimarb7204302016-06-02 09:22:00 +0000939 // Convert call/jmp instructions.
Rui Ueyamaa71ba432016-06-16 23:28:05 +0000940 if (ModRm == 0x15) {
941 // ABI says we can convert "call *foo@GOTPCREL(%rip)" to "nop; call foo".
942 // Instead we convert to "addr32 call foo" where addr32 is an instruction
943 // prefix. That makes result expression to be a single instruction.
944 Loc[-2] = 0x67; // addr32 prefix
945 Loc[-1] = 0xe8; // call
George Rimarb7204302016-06-02 09:22:00 +0000946 relocateOne(Loc, R_X86_64_PC32, Val);
947 return;
948 }
949
Rui Ueyamaa71ba432016-06-16 23:28:05 +0000950 // Convert "jmp *foo@GOTPCREL(%rip)" to "jmp foo; nop".
951 // jmp doesn't return, so it is fine to use nop here, it is just a stub.
952 assert(ModRm == 0x25);
953 Loc[-2] = 0xe9; // jmp
954 Loc[3] = 0x90; // nop
955 relocateOne(Loc - 1, R_X86_64_PC32, Val + 1);
George Rimarb7204302016-06-02 09:22:00 +0000956}
957
Hal Finkel3c8cc672015-10-12 20:56:18 +0000958// Relocation masks following the #lo(value), #hi(value), #ha(value),
959// #higher(value), #highera(value), #highest(value), and #highesta(value)
960// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
961// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000962static uint16_t applyPPCLo(uint64_t V) { return V; }
963static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
964static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
965static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
966static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000967static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000968static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
969
Davide Italiano8c3444362016-01-11 19:45:33 +0000970PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000971
Rafael Espindola22ef9562016-04-13 01:40:19 +0000972void PPCTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
973 uint64_t Val) const {
Davide Italiano8c3444362016-01-11 19:45:33 +0000974 switch (Type) {
975 case R_PPC_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000976 write16be(Loc, applyPPCHa(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000977 break;
978 case R_PPC_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000979 write16be(Loc, applyPPCLo(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000980 break;
Rui Ueyama035c4f12016-11-01 18:30:28 +0000981 case R_PPC_ADDR32:
Rui Ueyama7fd5c84f2016-11-01 18:30:26 +0000982 case R_PPC_REL32:
983 write32be(Loc, Val);
984 break;
Rui Ueyama035c4f12016-11-01 18:30:28 +0000985 case R_PPC_REL24:
986 or32be(Loc, Val & 0x3FFFFFC);
987 break;
Davide Italiano8c3444362016-01-11 19:45:33 +0000988 default:
Eugene Leviant84569e62016-11-29 08:05:44 +0000989 fatal(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000990 }
991}
992
Rafael Espindola22ef9562016-04-13 01:40:19 +0000993RelExpr PPCTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama7fd5c84f2016-11-01 18:30:26 +0000994 switch (Type) {
995 case R_PPC_REL24:
996 case R_PPC_REL32:
997 return R_PC;
998 default:
999 return R_ABS;
1000 }
Rafael Espindola22ef9562016-04-13 01:40:19 +00001001}
1002
Rafael Espindolac4010882015-09-22 20:54:08 +00001003PPC64TargetInfo::PPC64TargetInfo() {
Rafael Espindolae4c86d832016-05-18 21:03:36 +00001004 PltRel = GotRel = R_PPC64_GLOB_DAT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001005 RelativeRel = R_PPC64_RELATIVE;
Rui Ueyama803b1202016-07-13 18:55:14 +00001006 GotEntrySize = 8;
1007 GotPltEntrySize = 8;
Hal Finkel6c2a3b82015-10-08 21:51:31 +00001008 PltEntrySize = 32;
Rui Ueyamac737ef52016-06-16 23:50:25 +00001009 PltHeaderSize = 0;
Hal Finkelc848b322015-10-12 19:34:29 +00001010
1011 // We need 64K pages (at least under glibc/Linux, the loader won't
1012 // set different permissions on a finer granularity than that).
Petr Hosek5d98fef72016-09-28 00:09:20 +00001013 MaxPageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +00001014
1015 // The PPC64 ELF ABI v1 spec, says:
1016 //
1017 // It is normally desirable to put segments with different characteristics
1018 // in separate 256 Mbyte portions of the address space, to give the
1019 // operating system full paging flexibility in the 64-bit address space.
1020 //
1021 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
1022 // use 0x10000000 as the starting address.
Rui Ueyama941faa72016-07-14 17:43:28 +00001023 DefaultImageBase = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +00001024}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001025
Rafael Espindola15cec292016-04-27 12:25:22 +00001026static uint64_t PPC64TocOffset = 0x8000;
1027
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001028uint64_t getPPC64TocBase() {
Rafael Espindola520ed3a2016-04-27 12:21:27 +00001029 // The TOC consists of sections .got, .toc, .tocbss, .plt in that order. The
1030 // TOC starts where the first of these sections starts. We always create a
1031 // .got when we see a relocation that uses it, so for us the start is always
1032 // the .got.
Eugene Leviantad4439e2016-11-11 11:33:32 +00001033 uint64_t TocVA = In<ELF64BE>::Got->getVA();
Hal Finkel3c8cc672015-10-12 20:56:18 +00001034
1035 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
1036 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
1037 // code (crt1.o) assumes that you can get from the TOC base to the
1038 // start of the .toc section with only a single (signed) 16-bit relocation.
Rafael Espindola15cec292016-04-27 12:25:22 +00001039 return TocVA + PPC64TocOffset;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001040}
1041
Rafael Espindola22ef9562016-04-13 01:40:19 +00001042RelExpr PPC64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
1043 switch (Type) {
1044 default:
1045 return R_ABS;
Rafael Espindola15cec292016-04-27 12:25:22 +00001046 case R_PPC64_TOC16:
1047 case R_PPC64_TOC16_DS:
1048 case R_PPC64_TOC16_HA:
1049 case R_PPC64_TOC16_HI:
1050 case R_PPC64_TOC16_LO:
1051 case R_PPC64_TOC16_LO_DS:
1052 return R_GOTREL;
Rafael Espindola365e5f62016-04-27 11:54:07 +00001053 case R_PPC64_TOC:
1054 return R_PPC_TOC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001055 case R_PPC64_REL24:
Rafael Espindolab312a742016-04-21 17:30:24 +00001056 return R_PPC_PLT_OPD;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001057 }
1058}
1059
Rui Ueyama9398f862016-01-29 04:15:02 +00001060void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1061 uint64_t PltEntryAddr, int32_t Index,
1062 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001063 uint64_t Off = GotEntryAddr - getPPC64TocBase();
1064
1065 // FIXME: What we should do, in theory, is get the offset of the function
1066 // descriptor in the .opd section, and use that as the offset from %r2 (the
1067 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1068 // be a pointer to the function descriptor in the .opd section. Using
1069 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1070
George Rimara4c7e742016-10-20 08:36:42 +00001071 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
1072 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1073 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1074 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1075 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1076 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1077 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1078 write32be(Buf + 28, 0x4e800420); // bctr
Hal Finkel3c8cc672015-10-12 20:56:18 +00001079}
1080
Rui Ueyama2f524fb2016-06-16 23:28:08 +00001081static std::pair<uint32_t, uint64_t> toAddr16Rel(uint32_t Type, uint64_t Val) {
1082 uint64_t V = Val - PPC64TocOffset;
1083 switch (Type) {
George Rimara4c7e742016-10-20 08:36:42 +00001084 case R_PPC64_TOC16:
1085 return {R_PPC64_ADDR16, V};
1086 case R_PPC64_TOC16_DS:
1087 return {R_PPC64_ADDR16_DS, V};
1088 case R_PPC64_TOC16_HA:
1089 return {R_PPC64_ADDR16_HA, V};
1090 case R_PPC64_TOC16_HI:
1091 return {R_PPC64_ADDR16_HI, V};
1092 case R_PPC64_TOC16_LO:
1093 return {R_PPC64_ADDR16_LO, V};
1094 case R_PPC64_TOC16_LO_DS:
1095 return {R_PPC64_ADDR16_LO_DS, V};
1096 default:
1097 return {Type, Val};
Rui Ueyama2f524fb2016-06-16 23:28:08 +00001098 }
1099}
1100
Rafael Espindola22ef9562016-04-13 01:40:19 +00001101void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1102 uint64_t Val) const {
Rui Ueyama2f524fb2016-06-16 23:28:08 +00001103 // For a TOC-relative relocation, proceed in terms of the corresponding
Rafael Espindola15cec292016-04-27 12:25:22 +00001104 // ADDR16 relocation type.
Rui Ueyama2f524fb2016-06-16 23:28:08 +00001105 std::tie(Type, Val) = toAddr16Rel(Type, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001106
Hal Finkel3c8cc672015-10-12 20:56:18 +00001107 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001108 case R_PPC64_ADDR14: {
Eugene Leviant84569e62016-11-29 08:05:44 +00001109 checkAlignment<4>(Loc, Val, Type);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001110 // Preserve the AA/LK bits in the branch instruction
1111 uint8_t AALK = Loc[3];
Rafael Espindola22ef9562016-04-13 01:40:19 +00001112 write16be(Loc + 2, (AALK & 3) | (Val & 0xfffc));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001113 break;
1114 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001115 case R_PPC64_ADDR16:
Eugene Leviant84569e62016-11-29 08:05:44 +00001116 checkInt<16>(Loc, Val, Type);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001117 write16be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001118 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001119 case R_PPC64_ADDR16_DS:
Eugene Leviant84569e62016-11-29 08:05:44 +00001120 checkInt<16>(Loc, Val, Type);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001121 write16be(Loc, (read16be(Loc) & 3) | (Val & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001122 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001123 case R_PPC64_ADDR16_HA:
Rui Ueyamae991a492016-06-16 23:28:06 +00001124 case R_PPC64_REL16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001125 write16be(Loc, applyPPCHa(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001126 break;
1127 case R_PPC64_ADDR16_HI:
Rui Ueyamae991a492016-06-16 23:28:06 +00001128 case R_PPC64_REL16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001129 write16be(Loc, applyPPCHi(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001130 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001131 case R_PPC64_ADDR16_HIGHER:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001132 write16be(Loc, applyPPCHigher(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001133 break;
1134 case R_PPC64_ADDR16_HIGHERA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001135 write16be(Loc, applyPPCHighera(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001136 break;
1137 case R_PPC64_ADDR16_HIGHEST:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001138 write16be(Loc, applyPPCHighest(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001139 break;
1140 case R_PPC64_ADDR16_HIGHESTA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001141 write16be(Loc, applyPPCHighesta(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001142 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001143 case R_PPC64_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001144 write16be(Loc, applyPPCLo(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001145 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001146 case R_PPC64_ADDR16_LO_DS:
Rui Ueyamae991a492016-06-16 23:28:06 +00001147 case R_PPC64_REL16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001148 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(Val) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001149 break;
1150 case R_PPC64_ADDR32:
Rui Ueyamae991a492016-06-16 23:28:06 +00001151 case R_PPC64_REL32:
Eugene Leviant84569e62016-11-29 08:05:44 +00001152 checkInt<32>(Loc, Val, Type);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001153 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001154 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001155 case R_PPC64_ADDR64:
Rui Ueyamae991a492016-06-16 23:28:06 +00001156 case R_PPC64_REL64:
1157 case R_PPC64_TOC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001158 write64be(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001159 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001160 case R_PPC64_REL24: {
1161 uint32_t Mask = 0x03FFFFFC;
Eugene Leviant84569e62016-11-29 08:05:44 +00001162 checkInt<24>(Loc, Val, Type);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001163 write32be(Loc, (read32be(Loc) & ~Mask) | (Val & Mask));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001164 break;
1165 }
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001166 default:
Eugene Leviant84569e62016-11-29 08:05:44 +00001167 fatal(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001168 }
1169}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001170
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001171AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001172 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001173 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001174 IRelativeRel = R_AARCH64_IRELATIVE;
1175 GotRel = R_AARCH64_GLOB_DAT;
1176 PltRel = R_AARCH64_JUMP_SLOT;
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001177 TlsDescRel = R_AARCH64_TLSDESC;
Rui Ueyama724d6252016-01-29 01:49:32 +00001178 TlsGotRel = R_AARCH64_TLS_TPREL64;
Rui Ueyama803b1202016-07-13 18:55:14 +00001179 GotEntrySize = 8;
1180 GotPltEntrySize = 8;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001181 PltEntrySize = 16;
Rui Ueyama4a90f572016-06-16 16:28:50 +00001182 PltHeaderSize = 32;
Eugene Leviantee8dcfb2016-10-04 08:58:55 +00001183 MaxPageSize = 65536;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001184
1185 // It doesn't seem to be documented anywhere, but tls on aarch64 uses variant
1186 // 1 of the tls structures and the tcb size is 16.
1187 TcbSize = 16;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001188}
George Rimar648a2c32015-10-20 08:54:27 +00001189
Rafael Espindola22ef9562016-04-13 01:40:19 +00001190RelExpr AArch64TargetInfo::getRelExpr(uint32_t Type,
1191 const SymbolBody &S) const {
1192 switch (Type) {
1193 default:
1194 return R_ABS;
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001195 case R_AARCH64_TLSDESC_ADR_PAGE21:
1196 return R_TLSDESC_PAGE;
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001197 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1198 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1199 return R_TLSDESC;
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001200 case R_AARCH64_TLSDESC_CALL:
Peter Smithd6486032016-10-20 09:59:26 +00001201 return R_TLSDESC_CALL;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001202 case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1203 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1204 return R_TLS;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001205 case R_AARCH64_CALL26:
Rafael Espindolab312a742016-04-21 17:30:24 +00001206 case R_AARCH64_CONDBR19:
1207 case R_AARCH64_JUMP26:
1208 case R_AARCH64_TSTBR14:
1209 return R_PLT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001210 case R_AARCH64_PREL16:
1211 case R_AARCH64_PREL32:
1212 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001213 case R_AARCH64_ADR_PREL_LO21:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001214 return R_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001215 case R_AARCH64_ADR_PREL_PG_HI21:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001216 return R_PAGE_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +00001217 case R_AARCH64_LD64_GOT_LO12_NC:
1218 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1219 return R_GOT;
1220 case R_AARCH64_ADR_GOT_PAGE:
1221 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1222 return R_GOT_PAGE_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001223 }
1224}
1225
Rafael Espindolae1979ae2016-06-04 23:33:31 +00001226RelExpr AArch64TargetInfo::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
1227 RelExpr Expr) const {
1228 if (Expr == R_RELAX_TLS_GD_TO_IE) {
1229 if (Type == R_AARCH64_TLSDESC_ADR_PAGE21)
1230 return R_RELAX_TLS_GD_TO_IE_PAGE_PC;
1231 return R_RELAX_TLS_GD_TO_IE_ABS;
1232 }
1233 return Expr;
1234}
1235
Rafael Espindolab8ff59a2016-04-28 14:34:39 +00001236bool AArch64TargetInfo::usesOnlyLowPageBits(uint32_t Type) const {
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001237 switch (Type) {
1238 default:
1239 return false;
Ed Schouten39aca422016-04-06 18:21:07 +00001240 case R_AARCH64_ADD_ABS_LO12_NC:
Rafael Espindola53d0a9f2016-06-02 15:24:52 +00001241 case R_AARCH64_LD64_GOT_LO12_NC:
1242 case R_AARCH64_LDST128_ABS_LO12_NC:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001243 case R_AARCH64_LDST16_ABS_LO12_NC:
1244 case R_AARCH64_LDST32_ABS_LO12_NC:
1245 case R_AARCH64_LDST64_ABS_LO12_NC:
Rafael Espindola53d0a9f2016-06-02 15:24:52 +00001246 case R_AARCH64_LDST8_ABS_LO12_NC:
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001247 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1248 case R_AARCH64_TLSDESC_LD64_LO12_NC:
Rafael Espindolade17d282016-05-04 21:40:07 +00001249 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001250 return true;
1251 }
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001252}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001253
George Rimar98b060d2016-03-06 06:01:07 +00001254bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001255 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1256 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1257}
1258
Eugene Leviantab024a32016-11-25 08:56:36 +00001259bool AArch64TargetInfo::isPicRel(uint32_t Type) const {
1260 return Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001261}
1262
Rui Ueyamac9fee5f2016-06-16 16:14:50 +00001263void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, const SymbolBody &) const {
Eugene Leviantff23d3e2016-11-18 14:35:03 +00001264 write64le(Buf, In<ELF64LE>::Plt->getVA());
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001265}
1266
Rafael Espindola22ef9562016-04-13 01:40:19 +00001267static uint64_t getAArch64Page(uint64_t Expr) {
1268 return Expr & (~static_cast<uint64_t>(0xFFF));
1269}
1270
Rui Ueyama4a90f572016-06-16 16:28:50 +00001271void AArch64TargetInfo::writePltHeader(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001272 const uint8_t PltData[] = {
1273 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1274 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1275 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1276 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1277 0x20, 0x02, 0x1f, 0xd6, // br x17
1278 0x1f, 0x20, 0x03, 0xd5, // nop
1279 0x1f, 0x20, 0x03, 0xd5, // nop
1280 0x1f, 0x20, 0x03, 0xd5 // nop
1281 };
1282 memcpy(Buf, PltData, sizeof(PltData));
1283
Eugene Leviant41ca3272016-11-10 09:48:29 +00001284 uint64_t Got = In<ELF64LE>::GotPlt->getVA();
Eugene Leviantff23d3e2016-11-18 14:35:03 +00001285 uint64_t Plt = In<ELF64LE>::Plt->getVA();
Rafael Espindola22ef9562016-04-13 01:40:19 +00001286 relocateOne(Buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
1287 getAArch64Page(Got + 16) - getAArch64Page(Plt + 4));
1288 relocateOne(Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, Got + 16);
1289 relocateOne(Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001290}
1291
Rui Ueyama9398f862016-01-29 04:15:02 +00001292void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1293 uint64_t PltEntryAddr, int32_t Index,
1294 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001295 const uint8_t Inst[] = {
1296 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1297 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1298 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1299 0x20, 0x02, 0x1f, 0xd6 // br x17
1300 };
1301 memcpy(Buf, Inst, sizeof(Inst));
1302
Rafael Espindola22ef9562016-04-13 01:40:19 +00001303 relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21,
1304 getAArch64Page(GotEntryAddr) - getAArch64Page(PltEntryAddr));
1305 relocateOne(Buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, GotEntryAddr);
1306 relocateOne(Buf + 8, R_AARCH64_ADD_ABS_LO12_NC, GotEntryAddr);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001307}
1308
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001309static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001310 uint32_t ImmLo = (Imm & 0x3) << 29;
Rafael Espindola1c0eb972016-06-02 16:00:25 +00001311 uint32_t ImmHi = (Imm & 0x1FFFFC) << 3;
1312 uint64_t Mask = (0x3 << 29) | (0x1FFFFC << 3);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001313 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001314}
1315
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001316static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1317 or32le(L, (Imm & 0xFFF) << 10);
1318}
1319
Rafael Espindola22ef9562016-04-13 01:40:19 +00001320void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1321 uint64_t Val) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001322 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001323 case R_AARCH64_ABS16:
Rui Ueyamae66f45c2016-05-25 04:10:14 +00001324 case R_AARCH64_PREL16:
Eugene Leviant84569e62016-11-29 08:05:44 +00001325 checkIntUInt<16>(Loc, Val, Type);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001326 write16le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001327 break;
1328 case R_AARCH64_ABS32:
Rui Ueyamae66f45c2016-05-25 04:10:14 +00001329 case R_AARCH64_PREL32:
Eugene Leviant84569e62016-11-29 08:05:44 +00001330 checkIntUInt<32>(Loc, Val, Type);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001331 write32le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001332 break;
1333 case R_AARCH64_ABS64:
Rafael Espindolaf1e24532016-11-29 03:45:36 +00001334 case R_AARCH64_GLOB_DAT:
Rui Ueyamae66f45c2016-05-25 04:10:14 +00001335 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001336 write64le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001337 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001338 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001339 // This relocation stores 12 bits and there's no instruction
1340 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001341 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1342 // bits in Loc are zero.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001343 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001344 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001345 case R_AARCH64_ADR_GOT_PAGE:
Rui Ueyamae66f45c2016-05-25 04:10:14 +00001346 case R_AARCH64_ADR_PREL_PG_HI21:
1347 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001348 case R_AARCH64_TLSDESC_ADR_PAGE21:
Eugene Leviant84569e62016-11-29 08:05:44 +00001349 checkInt<33>(Loc, Val, Type);
Rafael Espindola1c0eb972016-06-02 16:00:25 +00001350 updateAArch64Addr(Loc, Val >> 12);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001351 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001352 case R_AARCH64_ADR_PREL_LO21:
Eugene Leviant84569e62016-11-29 08:05:44 +00001353 checkInt<21>(Loc, Val, Type);
Rafael Espindola1c0eb972016-06-02 16:00:25 +00001354 updateAArch64Addr(Loc, Val);
Davide Italiano1d750a62015-09-27 08:45:38 +00001355 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001356 case R_AARCH64_CALL26:
Rafael Espindolad79073d2016-04-25 12:32:19 +00001357 case R_AARCH64_JUMP26:
Eugene Leviant84569e62016-11-29 08:05:44 +00001358 checkInt<28>(Loc, Val, Type);
Rafael Espindolad79073d2016-04-25 12:32:19 +00001359 or32le(Loc, (Val & 0x0FFFFFFC) >> 2);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001360 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001361 case R_AARCH64_CONDBR19:
Eugene Leviant84569e62016-11-29 08:05:44 +00001362 checkInt<21>(Loc, Val, Type);
Rafael Espindolad79073d2016-04-25 12:32:19 +00001363 or32le(Loc, (Val & 0x1FFFFC) << 3);
George Rimar4102bfb2016-01-11 14:22:00 +00001364 break;
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001365 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001366 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001367 case R_AARCH64_TLSDESC_LD64_LO12_NC:
Eugene Leviant84569e62016-11-29 08:05:44 +00001368 checkAlignment<8>(Loc, Val, Type);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001369 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001370 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001371 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001372 or32le(Loc, (Val & 0x0FF8) << 6);
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001373 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001374 case R_AARCH64_LDST16_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001375 or32le(Loc, (Val & 0x0FFC) << 9);
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001376 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001377 case R_AARCH64_LDST8_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001378 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italianodc67f9b2015-11-20 21:35:38 +00001379 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001380 case R_AARCH64_LDST32_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001381 or32le(Loc, (Val & 0xFFC) << 8);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001382 break;
1383 case R_AARCH64_LDST64_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001384 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001385 break;
Eugene Leviant99da7522016-09-12 10:02:41 +00001386 case R_AARCH64_MOVW_UABS_G0_NC:
1387 or32le(Loc, (Val & 0xFFFF) << 5);
1388 break;
1389 case R_AARCH64_MOVW_UABS_G1_NC:
1390 or32le(Loc, (Val & 0xFFFF0000) >> 11);
1391 break;
1392 case R_AARCH64_MOVW_UABS_G2_NC:
1393 or32le(Loc, (Val & 0xFFFF00000000) >> 27);
1394 break;
1395 case R_AARCH64_MOVW_UABS_G3:
1396 or32le(Loc, (Val & 0xFFFF000000000000) >> 43);
1397 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001398 case R_AARCH64_TSTBR14:
Eugene Leviant84569e62016-11-29 08:05:44 +00001399 checkInt<16>(Loc, Val, Type);
Rafael Espindolad79073d2016-04-25 12:32:19 +00001400 or32le(Loc, (Val & 0xFFFC) << 3);
George Rimar1395dbd2016-01-11 14:27:05 +00001401 break;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001402 case R_AARCH64_TLSLE_ADD_TPREL_HI12:
Eugene Leviant84569e62016-11-29 08:05:44 +00001403 checkInt<24>(Loc, Val, Type);
Rafael Espindola1016f192016-06-02 15:51:40 +00001404 updateAArch64Add(Loc, Val >> 12);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001405 break;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001406 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001407 case R_AARCH64_TLSDESC_ADD_LO12_NC:
Rafael Espindola1016f192016-06-02 15:51:40 +00001408 updateAArch64Add(Loc, Val);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001409 break;
Davide Italiano1d750a62015-09-27 08:45:38 +00001410 default:
Eugene Leviant84569e62016-11-29 08:05:44 +00001411 fatal(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001412 }
1413}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001414
Rafael Espindola22ef9562016-04-13 01:40:19 +00001415void AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
1416 uint64_t Val) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001417 // TLSDESC Global-Dynamic relocation are in the form:
1418 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1419 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1420 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1421 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
Rafael Espindolae1979ae2016-06-04 23:33:31 +00001422 // blr x1
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001423 // And it can optimized to:
1424 // movz x0, #0x0, lsl #16
1425 // movk x0, #0x10
1426 // nop
1427 // nop
Eugene Leviant84569e62016-11-29 08:05:44 +00001428 checkUInt<32>(Loc, Val, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001429
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001430 switch (Type) {
1431 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1432 case R_AARCH64_TLSDESC_CALL:
Rui Ueyamaf9d56202016-06-16 16:44:52 +00001433 write32le(Loc, 0xd503201f); // nop
1434 return;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001435 case R_AARCH64_TLSDESC_ADR_PAGE21:
Rui Ueyamaf9d56202016-06-16 16:44:52 +00001436 write32le(Loc, 0xd2a00000 | (((Val >> 16) & 0xffff) << 5)); // movz
1437 return;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001438 case R_AARCH64_TLSDESC_LD64_LO12_NC:
Rui Ueyamaf9d56202016-06-16 16:44:52 +00001439 write32le(Loc, 0xf2800000 | ((Val & 0xffff) << 5)); // movk
1440 return;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001441 default:
Rui Ueyamaf9d56202016-06-16 16:44:52 +00001442 llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001443 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001444}
1445
Rafael Espindolae1979ae2016-06-04 23:33:31 +00001446void AArch64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
1447 uint64_t Val) const {
1448 // TLSDESC Global-Dynamic relocation are in the form:
1449 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1450 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1451 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1452 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1453 // blr x1
1454 // And it can optimized to:
1455 // adrp x0, :gottprel:v
1456 // ldr x0, [x0, :gottprel_lo12:v]
1457 // nop
1458 // nop
1459
1460 switch (Type) {
1461 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1462 case R_AARCH64_TLSDESC_CALL:
1463 write32le(Loc, 0xd503201f); // nop
1464 break;
1465 case R_AARCH64_TLSDESC_ADR_PAGE21:
1466 write32le(Loc, 0x90000000); // adrp
1467 relocateOne(Loc, R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21, Val);
1468 break;
1469 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1470 write32le(Loc, 0xf9400000); // ldr
1471 relocateOne(Loc, R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC, Val);
1472 break;
1473 default:
Rui Ueyamaf9d56202016-06-16 16:44:52 +00001474 llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
Rafael Espindolae1979ae2016-06-04 23:33:31 +00001475 }
1476}
1477
Rafael Espindola22ef9562016-04-13 01:40:19 +00001478void AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
1479 uint64_t Val) const {
Eugene Leviant84569e62016-11-29 08:05:44 +00001480 checkUInt<32>(Loc, Val, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001481
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001482 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
Rui Ueyamad089a432016-06-16 16:40:36 +00001483 // Generate MOVZ.
1484 uint32_t RegNo = read32le(Loc) & 0x1f;
1485 write32le(Loc, (0xd2a00000 | RegNo) | (((Val >> 16) & 0xffff) << 5));
1486 return;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001487 }
Rui Ueyamad089a432016-06-16 16:40:36 +00001488 if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1489 // Generate MOVK.
1490 uint32_t RegNo = read32le(Loc) & 0x1f;
1491 write32le(Loc, (0xf2800000 | RegNo) | ((Val & 0xffff) << 5));
1492 return;
1493 }
1494 llvm_unreachable("invalid relocation for TLS IE to LE relaxation");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001495}
1496
Rui Ueyama0fad6ea2016-07-14 05:46:22 +00001497AMDGPUTargetInfo::AMDGPUTargetInfo() {
Rui Ueyama7caf48c2016-08-31 21:04:25 +00001498 RelativeRel = R_AMDGPU_REL64;
Rui Ueyama0fad6ea2016-07-14 05:46:22 +00001499 GotRel = R_AMDGPU_ABS64;
1500 GotEntrySize = 8;
1501}
Tom Stellard391e3a82016-07-04 19:19:07 +00001502
Rafael Espindola22ef9562016-04-13 01:40:19 +00001503void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1504 uint64_t Val) const {
Tom Stellard391e3a82016-07-04 19:19:07 +00001505 switch (Type) {
Konstantin Zhuravlyov667e245e2016-07-21 15:30:13 +00001506 case R_AMDGPU_ABS32:
Tom Stellard391e3a82016-07-04 19:19:07 +00001507 case R_AMDGPU_GOTPCREL:
Konstantin Zhuravlyovd4327e92016-10-14 04:51:43 +00001508 case R_AMDGPU_GOTPCREL32_LO:
Tom Stellard391e3a82016-07-04 19:19:07 +00001509 case R_AMDGPU_REL32:
Konstantin Zhuravlyovd4327e92016-10-14 04:51:43 +00001510 case R_AMDGPU_REL32_LO:
Tom Stellard391e3a82016-07-04 19:19:07 +00001511 write32le(Loc, Val);
1512 break;
Konstantin Zhuravlyovb625d172016-10-20 18:34:58 +00001513 case R_AMDGPU_ABS64:
1514 write64le(Loc, Val);
1515 break;
Konstantin Zhuravlyovd4327e92016-10-14 04:51:43 +00001516 case R_AMDGPU_GOTPCREL32_HI:
1517 case R_AMDGPU_REL32_HI:
1518 write32le(Loc, Val >> 32);
1519 break;
Tom Stellard391e3a82016-07-04 19:19:07 +00001520 default:
Eugene Leviant84569e62016-11-29 08:05:44 +00001521 fatal(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
Tom Stellard391e3a82016-07-04 19:19:07 +00001522 }
Rafael Espindola22ef9562016-04-13 01:40:19 +00001523}
1524
1525RelExpr AMDGPUTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
Tom Stellard391e3a82016-07-04 19:19:07 +00001526 switch (Type) {
Konstantin Zhuravlyov667e245e2016-07-21 15:30:13 +00001527 case R_AMDGPU_ABS32:
Konstantin Zhuravlyovb625d172016-10-20 18:34:58 +00001528 case R_AMDGPU_ABS64:
Konstantin Zhuravlyov667e245e2016-07-21 15:30:13 +00001529 return R_ABS;
Tom Stellard391e3a82016-07-04 19:19:07 +00001530 case R_AMDGPU_REL32:
Konstantin Zhuravlyovd4327e92016-10-14 04:51:43 +00001531 case R_AMDGPU_REL32_LO:
1532 case R_AMDGPU_REL32_HI:
Tom Stellard391e3a82016-07-04 19:19:07 +00001533 return R_PC;
1534 case R_AMDGPU_GOTPCREL:
Konstantin Zhuravlyovd4327e92016-10-14 04:51:43 +00001535 case R_AMDGPU_GOTPCREL32_LO:
1536 case R_AMDGPU_GOTPCREL32_HI:
Tom Stellard391e3a82016-07-04 19:19:07 +00001537 return R_GOT_PC;
1538 default:
1539 fatal("do not know how to handle relocation " + Twine(Type));
1540 }
Tom Stellard80efb162016-01-07 03:59:08 +00001541}
1542
Peter Smith8646ced2016-06-07 09:31:52 +00001543ARMTargetInfo::ARMTargetInfo() {
1544 CopyRel = R_ARM_COPY;
1545 RelativeRel = R_ARM_RELATIVE;
1546 IRelativeRel = R_ARM_IRELATIVE;
1547 GotRel = R_ARM_GLOB_DAT;
1548 PltRel = R_ARM_JUMP_SLOT;
1549 TlsGotRel = R_ARM_TLS_TPOFF32;
1550 TlsModuleIndexRel = R_ARM_TLS_DTPMOD32;
1551 TlsOffsetRel = R_ARM_TLS_DTPOFF32;
Rui Ueyama803b1202016-07-13 18:55:14 +00001552 GotEntrySize = 4;
1553 GotPltEntrySize = 4;
Peter Smith8646ced2016-06-07 09:31:52 +00001554 PltEntrySize = 16;
Rui Ueyama4a90f572016-06-16 16:28:50 +00001555 PltHeaderSize = 20;
Peter Smith9d450252016-07-20 08:52:27 +00001556 // ARM uses Variant 1 TLS
1557 TcbSize = 8;
Rafael Espindola0f7ceda2016-07-20 17:58:07 +00001558 NeedsThunks = true;
Peter Smith8646ced2016-06-07 09:31:52 +00001559}
1560
1561RelExpr ARMTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
1562 switch (Type) {
1563 default:
1564 return R_ABS;
Peter Smithfa4d90d2016-06-16 09:53:46 +00001565 case R_ARM_THM_JUMP11:
1566 return R_PC;
Peter Smith8646ced2016-06-07 09:31:52 +00001567 case R_ARM_CALL:
1568 case R_ARM_JUMP24:
1569 case R_ARM_PC24:
1570 case R_ARM_PLT32:
Peter Smithd6486032016-10-20 09:59:26 +00001571 case R_ARM_PREL31:
Peter Smithfa4d90d2016-06-16 09:53:46 +00001572 case R_ARM_THM_JUMP19:
1573 case R_ARM_THM_JUMP24:
1574 case R_ARM_THM_CALL:
Peter Smith8646ced2016-06-07 09:31:52 +00001575 return R_PLT_PC;
1576 case R_ARM_GOTOFF32:
1577 // (S + A) - GOT_ORG
1578 return R_GOTREL;
1579 case R_ARM_GOT_BREL:
1580 // GOT(S) + A - GOT_ORG
1581 return R_GOT_OFF;
1582 case R_ARM_GOT_PREL:
Peter Smith9d450252016-07-20 08:52:27 +00001583 case R_ARM_TLS_IE32:
1584 // GOT(S) + A - P
Peter Smith8646ced2016-06-07 09:31:52 +00001585 return R_GOT_PC;
Davide Italiano38115ff2016-08-01 19:28:13 +00001586 case R_ARM_TARGET1:
1587 return Config->Target1Rel ? R_PC : R_ABS;
Peter Smith9bbd4e22016-10-17 18:12:24 +00001588 case R_ARM_TARGET2:
1589 if (Config->Target2 == Target2Policy::Rel)
1590 return R_PC;
1591 if (Config->Target2 == Target2Policy::Abs)
1592 return R_ABS;
1593 return R_GOT_PC;
Peter Smith9d450252016-07-20 08:52:27 +00001594 case R_ARM_TLS_GD32:
1595 return R_TLSGD_PC;
Peter Smith441cf5d2016-07-20 14:56:26 +00001596 case R_ARM_TLS_LDM32:
1597 return R_TLSLD_PC;
Peter Smith8646ced2016-06-07 09:31:52 +00001598 case R_ARM_BASE_PREL:
1599 // B(S) + A - P
1600 // FIXME: currently B(S) assumed to be .got, this may not hold for all
1601 // platforms.
1602 return R_GOTONLY_PC;
Peter Smithfb05cd92016-07-08 16:10:27 +00001603 case R_ARM_MOVW_PREL_NC:
1604 case R_ARM_MOVT_PREL:
Peter Smith8646ced2016-06-07 09:31:52 +00001605 case R_ARM_REL32:
Peter Smithfb05cd92016-07-08 16:10:27 +00001606 case R_ARM_THM_MOVW_PREL_NC:
1607 case R_ARM_THM_MOVT_PREL:
Peter Smith8646ced2016-06-07 09:31:52 +00001608 return R_PC;
Peter Smithd6486032016-10-20 09:59:26 +00001609 case R_ARM_NONE:
1610 return R_HINT;
Peter Smith9d450252016-07-20 08:52:27 +00001611 case R_ARM_TLS_LE32:
1612 return R_TLS;
Peter Smith8646ced2016-06-07 09:31:52 +00001613 }
1614}
1615
Eugene Leviantab024a32016-11-25 08:56:36 +00001616bool ARMTargetInfo::isPicRel(uint32_t Type) const {
1617 return (Type == R_ARM_TARGET1 && !Config->Target1Rel) ||
1618 (Type == R_ARM_ABS32);
1619}
1620
Peter Smith8646ced2016-06-07 09:31:52 +00001621uint32_t ARMTargetInfo::getDynRel(uint32_t Type) const {
Davide Italiano38115ff2016-08-01 19:28:13 +00001622 if (Type == R_ARM_TARGET1 && !Config->Target1Rel)
1623 return R_ARM_ABS32;
Peter Smith8646ced2016-06-07 09:31:52 +00001624 if (Type == R_ARM_ABS32)
1625 return Type;
Peter Smith8646ced2016-06-07 09:31:52 +00001626 // Keep it going with a dummy value so that we can find more reloc errors.
1627 return R_ARM_ABS32;
1628}
1629
Rui Ueyamac9fee5f2016-06-16 16:14:50 +00001630void ARMTargetInfo::writeGotPlt(uint8_t *Buf, const SymbolBody &) const {
Eugene Leviantff23d3e2016-11-18 14:35:03 +00001631 write32le(Buf, In<ELF32LE>::Plt->getVA());
Peter Smith8646ced2016-06-07 09:31:52 +00001632}
1633
Rui Ueyama4a90f572016-06-16 16:28:50 +00001634void ARMTargetInfo::writePltHeader(uint8_t *Buf) const {
Peter Smith8646ced2016-06-07 09:31:52 +00001635 const uint8_t PltData[] = {
1636 0x04, 0xe0, 0x2d, 0xe5, // str lr, [sp,#-4]!
1637 0x04, 0xe0, 0x9f, 0xe5, // ldr lr, L2
1638 0x0e, 0xe0, 0x8f, 0xe0, // L1: add lr, pc, lr
1639 0x08, 0xf0, 0xbe, 0xe5, // ldr pc, [lr, #8]
1640 0x00, 0x00, 0x00, 0x00, // L2: .word &(.got.plt) - L1 - 8
1641 };
1642 memcpy(Buf, PltData, sizeof(PltData));
Eugene Leviant41ca3272016-11-10 09:48:29 +00001643 uint64_t GotPlt = In<ELF32LE>::GotPlt->getVA();
Eugene Leviantff23d3e2016-11-18 14:35:03 +00001644 uint64_t L1 = In<ELF32LE>::Plt->getVA() + 8;
Peter Smith8646ced2016-06-07 09:31:52 +00001645 write32le(Buf + 16, GotPlt - L1 - 8);
1646}
1647
1648void ARMTargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1649 uint64_t PltEntryAddr, int32_t Index,
1650 unsigned RelOff) const {
1651 // FIXME: Using simple code sequence with simple relocations.
1652 // There is a more optimal sequence but it requires support for the group
1653 // relocations. See ELF for the ARM Architecture Appendix A.3
1654 const uint8_t PltData[] = {
1655 0x04, 0xc0, 0x9f, 0xe5, // ldr ip, L2
1656 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
1657 0x00, 0xf0, 0x9c, 0xe5, // ldr pc, [ip]
1658 0x00, 0x00, 0x00, 0x00, // L2: .word Offset(&(.plt.got) - L1 - 8
1659 };
1660 memcpy(Buf, PltData, sizeof(PltData));
1661 uint64_t L1 = PltEntryAddr + 4;
1662 write32le(Buf + 12, GotEntryAddr - L1 - 8);
1663}
1664
Peter Smithfb05cd92016-07-08 16:10:27 +00001665RelExpr ARMTargetInfo::getThunkExpr(RelExpr Expr, uint32_t RelocType,
1666 const InputFile &File,
1667 const SymbolBody &S) const {
Peter Smith2227c7f2016-11-03 11:49:23 +00001668 // If S is an undefined weak symbol we don't need a Thunk
1669 if (S.isUndefined())
1670 return Expr;
Peter Smithfb05cd92016-07-08 16:10:27 +00001671 // A state change from ARM to Thumb and vice versa must go through an
1672 // interworking thunk if the relocation type is not R_ARM_CALL or
1673 // R_ARM_THM_CALL.
1674 switch (RelocType) {
1675 case R_ARM_PC24:
1676 case R_ARM_PLT32:
1677 case R_ARM_JUMP24:
1678 // Source is ARM, all PLT entries are ARM so no interworking required.
1679 // Otherwise we need to interwork if Symbol has bit 0 set (Thumb).
1680 if (Expr == R_PC && ((S.getVA<ELF32LE>() & 1) == 1))
1681 return R_THUNK_PC;
1682 break;
1683 case R_ARM_THM_JUMP19:
1684 case R_ARM_THM_JUMP24:
1685 // Source is Thumb, all PLT entries are ARM so interworking is required.
1686 // Otherwise we need to interwork if Symbol has bit 0 clear (ARM).
1687 if (Expr == R_PLT_PC)
1688 return R_THUNK_PLT_PC;
1689 if ((S.getVA<ELF32LE>() & 1) == 0)
1690 return R_THUNK_PC;
1691 break;
1692 }
1693 return Expr;
1694}
1695
Peter Smith8646ced2016-06-07 09:31:52 +00001696void ARMTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1697 uint64_t Val) const {
1698 switch (Type) {
Peter Smith8646ced2016-06-07 09:31:52 +00001699 case R_ARM_ABS32:
1700 case R_ARM_BASE_PREL:
Rafael Espindolaf1e24532016-11-29 03:45:36 +00001701 case R_ARM_GLOB_DAT:
Peter Smith8646ced2016-06-07 09:31:52 +00001702 case R_ARM_GOTOFF32:
1703 case R_ARM_GOT_BREL:
1704 case R_ARM_GOT_PREL:
1705 case R_ARM_REL32:
Davide Italiano38115ff2016-08-01 19:28:13 +00001706 case R_ARM_TARGET1:
Peter Smith9bbd4e22016-10-17 18:12:24 +00001707 case R_ARM_TARGET2:
Peter Smith9d450252016-07-20 08:52:27 +00001708 case R_ARM_TLS_GD32:
1709 case R_ARM_TLS_IE32:
Peter Smith441cf5d2016-07-20 14:56:26 +00001710 case R_ARM_TLS_LDM32:
1711 case R_ARM_TLS_LDO32:
Peter Smith9d450252016-07-20 08:52:27 +00001712 case R_ARM_TLS_LE32:
Rafael Espindolaf1e24532016-11-29 03:45:36 +00001713 case R_ARM_TLS_TPOFF32:
Peter Smith8646ced2016-06-07 09:31:52 +00001714 write32le(Loc, Val);
1715 break;
Peter Smithde3e7382016-11-29 16:23:50 +00001716 case R_ARM_TLS_DTPMOD32:
1717 write32le(Loc, 1);
1718 break;
Peter Smith8646ced2016-06-07 09:31:52 +00001719 case R_ARM_PREL31:
Eugene Leviant84569e62016-11-29 08:05:44 +00001720 checkInt<31>(Loc, Val, Type);
Peter Smith8646ced2016-06-07 09:31:52 +00001721 write32le(Loc, (read32le(Loc) & 0x80000000) | (Val & ~0x80000000));
1722 break;
1723 case R_ARM_CALL:
Peter Smithfa4d90d2016-06-16 09:53:46 +00001724 // R_ARM_CALL is used for BL and BLX instructions, depending on the
1725 // value of bit 0 of Val, we must select a BL or BLX instruction
1726 if (Val & 1) {
1727 // If bit 0 of Val is 1 the target is Thumb, we must select a BLX.
1728 // The BLX encoding is 0xfa:H:imm24 where Val = imm24:H:'1'
Eugene Leviant84569e62016-11-29 08:05:44 +00001729 checkInt<26>(Loc, Val, Type);
Peter Smithfa4d90d2016-06-16 09:53:46 +00001730 write32le(Loc, 0xfa000000 | // opcode
1731 ((Val & 2) << 23) | // H
1732 ((Val >> 2) & 0x00ffffff)); // imm24
1733 break;
1734 }
1735 if ((read32le(Loc) & 0xfe000000) == 0xfa000000)
1736 // BLX (always unconditional) instruction to an ARM Target, select an
1737 // unconditional BL.
1738 write32le(Loc, 0xeb000000 | (read32le(Loc) & 0x00ffffff));
George Rimara4c7e742016-10-20 08:36:42 +00001739 // fall through as BL encoding is shared with B
Peter Smith8646ced2016-06-07 09:31:52 +00001740 case R_ARM_JUMP24:
1741 case R_ARM_PC24:
1742 case R_ARM_PLT32:
Eugene Leviant84569e62016-11-29 08:05:44 +00001743 checkInt<26>(Loc, Val, Type);
Peter Smith8646ced2016-06-07 09:31:52 +00001744 write32le(Loc, (read32le(Loc) & ~0x00ffffff) | ((Val >> 2) & 0x00ffffff));
1745 break;
Peter Smithfa4d90d2016-06-16 09:53:46 +00001746 case R_ARM_THM_JUMP11:
Eugene Leviant84569e62016-11-29 08:05:44 +00001747 checkInt<12>(Loc, Val, Type);
Peter Smithfa4d90d2016-06-16 09:53:46 +00001748 write16le(Loc, (read32le(Loc) & 0xf800) | ((Val >> 1) & 0x07ff));
1749 break;
1750 case R_ARM_THM_JUMP19:
1751 // Encoding T3: Val = S:J2:J1:imm6:imm11:0
Eugene Leviant84569e62016-11-29 08:05:44 +00001752 checkInt<21>(Loc, Val, Type);
Peter Smithfa4d90d2016-06-16 09:53:46 +00001753 write16le(Loc,
1754 (read16le(Loc) & 0xfbc0) | // opcode cond
1755 ((Val >> 10) & 0x0400) | // S
1756 ((Val >> 12) & 0x003f)); // imm6
1757 write16le(Loc + 2,
1758 0x8000 | // opcode
1759 ((Val >> 8) & 0x0800) | // J2
1760 ((Val >> 5) & 0x2000) | // J1
1761 ((Val >> 1) & 0x07ff)); // imm11
1762 break;
1763 case R_ARM_THM_CALL:
1764 // R_ARM_THM_CALL is used for BL and BLX instructions, depending on the
1765 // value of bit 0 of Val, we must select a BL or BLX instruction
1766 if ((Val & 1) == 0) {
1767 // Ensure BLX destination is 4-byte aligned. As BLX instruction may
1768 // only be two byte aligned. This must be done before overflow check
1769 Val = alignTo(Val, 4);
1770 }
1771 // Bit 12 is 0 for BLX, 1 for BL
1772 write16le(Loc + 2, (read16le(Loc + 2) & ~0x1000) | (Val & 1) << 12);
George Rimara4c7e742016-10-20 08:36:42 +00001773 // Fall through as rest of encoding is the same as B.W
Peter Smithfa4d90d2016-06-16 09:53:46 +00001774 case R_ARM_THM_JUMP24:
1775 // Encoding B T4, BL T1, BLX T2: Val = S:I1:I2:imm10:imm11:0
1776 // FIXME: Use of I1 and I2 require v6T2ops
Eugene Leviant84569e62016-11-29 08:05:44 +00001777 checkInt<25>(Loc, Val, Type);
Peter Smithfa4d90d2016-06-16 09:53:46 +00001778 write16le(Loc,
1779 0xf000 | // opcode
1780 ((Val >> 14) & 0x0400) | // S
1781 ((Val >> 12) & 0x03ff)); // imm10
1782 write16le(Loc + 2,
1783 (read16le(Loc + 2) & 0xd000) | // opcode
1784 (((~(Val >> 10)) ^ (Val >> 11)) & 0x2000) | // J1
1785 (((~(Val >> 11)) ^ (Val >> 13)) & 0x0800) | // J2
1786 ((Val >> 1) & 0x07ff)); // imm11
1787 break;
Peter Smith8646ced2016-06-07 09:31:52 +00001788 case R_ARM_MOVW_ABS_NC:
Peter Smithfb05cd92016-07-08 16:10:27 +00001789 case R_ARM_MOVW_PREL_NC:
Peter Smith8646ced2016-06-07 09:31:52 +00001790 write32le(Loc, (read32le(Loc) & ~0x000f0fff) | ((Val & 0xf000) << 4) |
1791 (Val & 0x0fff));
1792 break;
1793 case R_ARM_MOVT_ABS:
Peter Smithfb05cd92016-07-08 16:10:27 +00001794 case R_ARM_MOVT_PREL:
Eugene Leviant84569e62016-11-29 08:05:44 +00001795 checkInt<32>(Loc, Val, Type);
Peter Smith8646ced2016-06-07 09:31:52 +00001796 write32le(Loc, (read32le(Loc) & ~0x000f0fff) |
1797 (((Val >> 16) & 0xf000) << 4) | ((Val >> 16) & 0xfff));
1798 break;
Peter Smithfa4d90d2016-06-16 09:53:46 +00001799 case R_ARM_THM_MOVT_ABS:
Peter Smithfb05cd92016-07-08 16:10:27 +00001800 case R_ARM_THM_MOVT_PREL:
Peter Smithfa4d90d2016-06-16 09:53:46 +00001801 // Encoding T1: A = imm4:i:imm3:imm8
Eugene Leviant84569e62016-11-29 08:05:44 +00001802 checkInt<32>(Loc, Val, Type);
Peter Smithfa4d90d2016-06-16 09:53:46 +00001803 write16le(Loc,
1804 0xf2c0 | // opcode
1805 ((Val >> 17) & 0x0400) | // i
1806 ((Val >> 28) & 0x000f)); // imm4
1807 write16le(Loc + 2,
1808 (read16le(Loc + 2) & 0x8f00) | // opcode
1809 ((Val >> 12) & 0x7000) | // imm3
1810 ((Val >> 16) & 0x00ff)); // imm8
1811 break;
1812 case R_ARM_THM_MOVW_ABS_NC:
Peter Smithfb05cd92016-07-08 16:10:27 +00001813 case R_ARM_THM_MOVW_PREL_NC:
Peter Smithfa4d90d2016-06-16 09:53:46 +00001814 // Encoding T3: A = imm4:i:imm3:imm8
1815 write16le(Loc,
1816 0xf240 | // opcode
1817 ((Val >> 1) & 0x0400) | // i
1818 ((Val >> 12) & 0x000f)); // imm4
1819 write16le(Loc + 2,
1820 (read16le(Loc + 2) & 0x8f00) | // opcode
1821 ((Val << 4) & 0x7000) | // imm3
1822 (Val & 0x00ff)); // imm8
1823 break;
Peter Smith8646ced2016-06-07 09:31:52 +00001824 default:
Eugene Leviant84569e62016-11-29 08:05:44 +00001825 fatal(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
Peter Smith8646ced2016-06-07 09:31:52 +00001826 }
1827}
1828
1829uint64_t ARMTargetInfo::getImplicitAddend(const uint8_t *Buf,
1830 uint32_t Type) const {
1831 switch (Type) {
1832 default:
1833 return 0;
1834 case R_ARM_ABS32:
1835 case R_ARM_BASE_PREL:
1836 case R_ARM_GOTOFF32:
1837 case R_ARM_GOT_BREL:
1838 case R_ARM_GOT_PREL:
1839 case R_ARM_REL32:
Davide Italiano38115ff2016-08-01 19:28:13 +00001840 case R_ARM_TARGET1:
Peter Smith9bbd4e22016-10-17 18:12:24 +00001841 case R_ARM_TARGET2:
Peter Smith9d450252016-07-20 08:52:27 +00001842 case R_ARM_TLS_GD32:
Peter Smith441cf5d2016-07-20 14:56:26 +00001843 case R_ARM_TLS_LDM32:
1844 case R_ARM_TLS_LDO32:
Peter Smith9d450252016-07-20 08:52:27 +00001845 case R_ARM_TLS_IE32:
1846 case R_ARM_TLS_LE32:
Peter Smith8646ced2016-06-07 09:31:52 +00001847 return SignExtend64<32>(read32le(Buf));
Peter Smith8646ced2016-06-07 09:31:52 +00001848 case R_ARM_PREL31:
1849 return SignExtend64<31>(read32le(Buf));
Peter Smith8646ced2016-06-07 09:31:52 +00001850 case R_ARM_CALL:
1851 case R_ARM_JUMP24:
1852 case R_ARM_PC24:
1853 case R_ARM_PLT32:
Rui Ueyamabebf4892016-06-16 23:28:03 +00001854 return SignExtend64<26>(read32le(Buf) << 2);
Peter Smithfa4d90d2016-06-16 09:53:46 +00001855 case R_ARM_THM_JUMP11:
Rui Ueyamabebf4892016-06-16 23:28:03 +00001856 return SignExtend64<12>(read16le(Buf) << 1);
Peter Smithfa4d90d2016-06-16 09:53:46 +00001857 case R_ARM_THM_JUMP19: {
1858 // Encoding T3: A = S:J2:J1:imm10:imm6:0
1859 uint16_t Hi = read16le(Buf);
1860 uint16_t Lo = read16le(Buf + 2);
1861 return SignExtend64<20>(((Hi & 0x0400) << 10) | // S
1862 ((Lo & 0x0800) << 8) | // J2
1863 ((Lo & 0x2000) << 5) | // J1
1864 ((Hi & 0x003f) << 12) | // imm6
1865 ((Lo & 0x07ff) << 1)); // imm11:0
1866 }
Peter Smithfb05cd92016-07-08 16:10:27 +00001867 case R_ARM_THM_CALL:
1868 case R_ARM_THM_JUMP24: {
Peter Smithfa4d90d2016-06-16 09:53:46 +00001869 // Encoding B T4, BL T1, BLX T2: A = S:I1:I2:imm10:imm11:0
1870 // I1 = NOT(J1 EOR S), I2 = NOT(J2 EOR S)
1871 // FIXME: I1 and I2 require v6T2ops
1872 uint16_t Hi = read16le(Buf);
1873 uint16_t Lo = read16le(Buf + 2);
1874 return SignExtend64<24>(((Hi & 0x0400) << 14) | // S
1875 (~((Lo ^ (Hi << 3)) << 10) & 0x00800000) | // I1
1876 (~((Lo ^ (Hi << 1)) << 11) & 0x00400000) | // I2
1877 ((Hi & 0x003ff) << 12) | // imm0
1878 ((Lo & 0x007ff) << 1)); // imm11:0
1879 }
1880 // ELF for the ARM Architecture 4.6.1.1 the implicit addend for MOVW and
1881 // MOVT is in the range -32768 <= A < 32768
Peter Smith8646ced2016-06-07 09:31:52 +00001882 case R_ARM_MOVW_ABS_NC:
Peter Smithfb05cd92016-07-08 16:10:27 +00001883 case R_ARM_MOVT_ABS:
1884 case R_ARM_MOVW_PREL_NC:
1885 case R_ARM_MOVT_PREL: {
Peter Smith8646ced2016-06-07 09:31:52 +00001886 uint64_t Val = read32le(Buf) & 0x000f0fff;
1887 return SignExtend64<16>(((Val & 0x000f0000) >> 4) | (Val & 0x00fff));
1888 }
Peter Smithfa4d90d2016-06-16 09:53:46 +00001889 case R_ARM_THM_MOVW_ABS_NC:
Peter Smithfb05cd92016-07-08 16:10:27 +00001890 case R_ARM_THM_MOVT_ABS:
1891 case R_ARM_THM_MOVW_PREL_NC:
1892 case R_ARM_THM_MOVT_PREL: {
Peter Smithfa4d90d2016-06-16 09:53:46 +00001893 // Encoding T3: A = imm4:i:imm3:imm8
1894 uint16_t Hi = read16le(Buf);
1895 uint16_t Lo = read16le(Buf + 2);
1896 return SignExtend64<16>(((Hi & 0x000f) << 12) | // imm4
1897 ((Hi & 0x0400) << 1) | // i
1898 ((Lo & 0x7000) >> 4) | // imm3
1899 (Lo & 0x00ff)); // imm8
1900 }
Peter Smith8646ced2016-06-07 09:31:52 +00001901 }
1902}
1903
Peter Smith441cf5d2016-07-20 14:56:26 +00001904bool ARMTargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
1905 return Type == R_ARM_TLS_LDO32 || Type == R_ARM_TLS_LDM32;
1906}
1907
Peter Smith9d450252016-07-20 08:52:27 +00001908bool ARMTargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
1909 return Type == R_ARM_TLS_GD32;
1910}
1911
1912bool ARMTargetInfo::isTlsInitialExecRel(uint32_t Type) const {
1913 return Type == R_ARM_TLS_IE32;
1914}
1915
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001916template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001917 GotPltHeaderEntriesNum = 2;
Petr Hosek5d98fef72016-09-28 00:09:20 +00001918 MaxPageSize = 65536;
Rui Ueyama803b1202016-07-13 18:55:14 +00001919 GotEntrySize = sizeof(typename ELFT::uint);
1920 GotPltEntrySize = sizeof(typename ELFT::uint);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001921 PltEntrySize = 16;
Rui Ueyama4a90f572016-06-16 16:28:50 +00001922 PltHeaderSize = 32;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001923 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001924 PltRel = R_MIPS_JUMP_SLOT;
Rafael Espindola0f7ceda2016-07-20 17:58:07 +00001925 NeedsThunks = true;
Simon Atanasyan002e2442016-06-23 15:26:31 +00001926 if (ELFT::Is64Bits) {
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00001927 RelativeRel = (R_MIPS_64 << 8) | R_MIPS_REL32;
Simon Atanasyan002e2442016-06-23 15:26:31 +00001928 TlsGotRel = R_MIPS_TLS_TPREL64;
1929 TlsModuleIndexRel = R_MIPS_TLS_DTPMOD64;
1930 TlsOffsetRel = R_MIPS_TLS_DTPREL64;
1931 } else {
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00001932 RelativeRel = R_MIPS_REL32;
Simon Atanasyan002e2442016-06-23 15:26:31 +00001933 TlsGotRel = R_MIPS_TLS_TPREL32;
1934 TlsModuleIndexRel = R_MIPS_TLS_DTPMOD32;
1935 TlsOffsetRel = R_MIPS_TLS_DTPREL32;
1936 }
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001937}
1938
1939template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001940RelExpr MipsTargetInfo<ELFT>::getRelExpr(uint32_t Type,
1941 const SymbolBody &S) const {
Simon Atanasyan9e0297b2016-11-05 22:58:01 +00001942 // See comment in the calculateMipsRelChain.
1943 if (ELFT::Is64Bits || Config->MipsN32Abi)
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001944 Type &= 0xff;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001945 switch (Type) {
1946 default:
1947 return R_ABS;
Rafael Espindolaebb04b92016-05-04 14:44:22 +00001948 case R_MIPS_JALR:
1949 return R_HINT;
Rafael Espindola1763dc42016-04-26 22:00:04 +00001950 case R_MIPS_GPREL16:
1951 case R_MIPS_GPREL32:
Simon Atanasyan725dc142016-11-16 21:01:02 +00001952 return R_MIPS_GOTREL;
Rafael Espindolab312a742016-04-21 17:30:24 +00001953 case R_MIPS_26:
1954 return R_PLT;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001955 case R_MIPS_HI16:
Simon Atanasyan1ca263c2016-04-14 21:10:05 +00001956 case R_MIPS_LO16:
Simon Atanasyanbe804552016-05-04 17:47:11 +00001957 case R_MIPS_GOT_OFST:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001958 // MIPS _gp_disp designates offset between start of function and 'gp'
1959 // pointer into GOT. __gnu_local_gp is equal to the current value of
1960 // the 'gp'. Therefore any relocations against them do not require
1961 // dynamic relocation.
1962 if (&S == ElfSym<ELFT>::MipsGpDisp)
1963 return R_PC;
1964 return R_ABS;
1965 case R_MIPS_PC32:
1966 case R_MIPS_PC16:
1967 case R_MIPS_PC19_S2:
1968 case R_MIPS_PC21_S2:
1969 case R_MIPS_PC26_S2:
1970 case R_MIPS_PCHI16:
1971 case R_MIPS_PCLO16:
1972 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +00001973 case R_MIPS_GOT16:
Rafael Espindola5628ee72016-04-15 19:14:18 +00001974 if (S.isLocal())
Simon Atanasyan4e3a15c2016-05-15 18:13:50 +00001975 return R_MIPS_GOT_LOCAL_PAGE;
Simon Atanasyanbe804552016-05-04 17:47:11 +00001976 // fallthrough
1977 case R_MIPS_CALL16:
1978 case R_MIPS_GOT_DISP:
Simon Atanasyan002e2442016-06-23 15:26:31 +00001979 case R_MIPS_TLS_GOTTPREL:
Simon Atanasyan41325112016-06-19 21:39:37 +00001980 return R_MIPS_GOT_OFF;
Simon Atanasyanbed04bf2016-10-21 07:22:30 +00001981 case R_MIPS_CALL_HI16:
1982 case R_MIPS_CALL_LO16:
1983 case R_MIPS_GOT_HI16:
1984 case R_MIPS_GOT_LO16:
1985 return R_MIPS_GOT_OFF32;
Simon Atanasyanbe804552016-05-04 17:47:11 +00001986 case R_MIPS_GOT_PAGE:
Simon Atanasyan4e3a15c2016-05-15 18:13:50 +00001987 return R_MIPS_GOT_LOCAL_PAGE;
Simon Atanasyan002e2442016-06-23 15:26:31 +00001988 case R_MIPS_TLS_GD:
1989 return R_MIPS_TLSGD;
1990 case R_MIPS_TLS_LDM:
1991 return R_MIPS_TLSLD;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001992 }
1993}
1994
Eugene Leviantab024a32016-11-25 08:56:36 +00001995template <class ELFT> bool MipsTargetInfo<ELFT>::isPicRel(uint32_t Type) const {
1996 return Type == R_MIPS_32 || Type == R_MIPS_64;
1997}
1998
Rafael Espindola22ef9562016-04-13 01:40:19 +00001999template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00002000uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Eugene Leviantab024a32016-11-25 08:56:36 +00002001 return RelativeRel;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00002002}
2003
2004template <class ELFT>
Simon Atanasyan002e2442016-06-23 15:26:31 +00002005bool MipsTargetInfo<ELFT>::isTlsLocalDynamicRel(uint32_t Type) const {
2006 return Type == R_MIPS_TLS_LDM;
2007}
2008
2009template <class ELFT>
2010bool MipsTargetInfo<ELFT>::isTlsGlobalDynamicRel(uint32_t Type) const {
2011 return Type == R_MIPS_TLS_GD;
2012}
2013
2014template <class ELFT>
Rui Ueyamac9fee5f2016-06-16 16:14:50 +00002015void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, const SymbolBody &) const {
Eugene Leviantff23d3e2016-11-18 14:35:03 +00002016 write32<ELFT::TargetEndianness>(Buf, In<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00002017}
Simon Atanasyan49829a12015-09-29 05:34:03 +00002018
Simon Atanasyane364e2e2016-02-04 12:31:39 +00002019template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00002020static int64_t getPcRelocAddend(const uint8_t *Loc) {
Rafael Espindola8cc68c32016-03-30 13:18:08 +00002021 uint32_t Instr = read32<E>(Loc);
2022 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
2023 return SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
2024}
2025
2026template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00002027static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t V) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00002028 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00002029 uint32_t Instr = read32<E>(Loc);
Rafael Espindola66ea7bb2016-03-31 12:09:36 +00002030 if (SHIFT > 0)
Eugene Leviant84569e62016-11-29 08:05:44 +00002031 checkAlignment<(1 << SHIFT)>(Loc, V, Type);
2032 checkInt<BSIZE + SHIFT>(Loc, V, Type);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00002033 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00002034}
2035
George Rimara4c7e742016-10-20 08:36:42 +00002036template <endianness E> static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00002037 uint32_t Instr = read32<E>(Loc);
Simon Atanasyan97519cb2016-08-31 11:47:17 +00002038 uint16_t Res = ((V + 0x8000) >> 16) & 0xffff;
2039 write32<E>(Loc, (Instr & 0xffff0000) | Res);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00002040}
2041
George Rimara4c7e742016-10-20 08:36:42 +00002042template <endianness E> static void writeMipsHigher(uint8_t *Loc, uint64_t V) {
Simon Atanasyane5532a12016-08-31 11:47:21 +00002043 uint32_t Instr = read32<E>(Loc);
2044 uint16_t Res = ((V + 0x80008000) >> 32) & 0xffff;
2045 write32<E>(Loc, (Instr & 0xffff0000) | Res);
2046}
2047
George Rimara4c7e742016-10-20 08:36:42 +00002048template <endianness E> static void writeMipsHighest(uint8_t *Loc, uint64_t V) {
Simon Atanasyane5532a12016-08-31 11:47:21 +00002049 uint32_t Instr = read32<E>(Loc);
2050 uint16_t Res = ((V + 0x800080008000) >> 48) & 0xffff;
2051 write32<E>(Loc, (Instr & 0xffff0000) | Res);
2052}
2053
George Rimara4c7e742016-10-20 08:36:42 +00002054template <endianness E> static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00002055 uint32_t Instr = read32<E>(Loc);
2056 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
2057}
2058
Simon Atanasyana088bce2016-07-20 20:15:33 +00002059template <class ELFT> static bool isMipsR6() {
2060 const auto &FirstObj = cast<ELFFileBase<ELFT>>(*Config->FirstElf);
2061 uint32_t Arch = FirstObj.getObj().getHeader()->e_flags & EF_MIPS_ARCH;
2062 return Arch == EF_MIPS_ARCH_32R6 || Arch == EF_MIPS_ARCH_64R6;
2063}
2064
Simon Atanasyan2287dc32016-02-10 19:57:19 +00002065template <class ELFT>
Rui Ueyama4a90f572016-06-16 16:28:50 +00002066void MipsTargetInfo<ELFT>::writePltHeader(uint8_t *Buf) const {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00002067 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan9e0297b2016-11-05 22:58:01 +00002068 if (Config->MipsN32Abi) {
2069 write32<E>(Buf, 0x3c0e0000); // lui $14, %hi(&GOTPLT[0])
2070 write32<E>(Buf + 4, 0x8dd90000); // lw $25, %lo(&GOTPLT[0])($14)
2071 write32<E>(Buf + 8, 0x25ce0000); // addiu $14, $14, %lo(&GOTPLT[0])
2072 write32<E>(Buf + 12, 0x030ec023); // subu $24, $24, $14
2073 } else {
2074 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
2075 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
2076 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
2077 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
2078 }
Simon Atanasyan2287dc32016-02-10 19:57:19 +00002079 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
2080 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
2081 write32<E>(Buf + 24, 0x0320f809); // jalr $25
2082 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
Eugene Leviant41ca3272016-11-10 09:48:29 +00002083 uint64_t Got = In<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00002084 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00002085 writeMipsLo16<E>(Buf + 4, Got);
2086 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00002087}
2088
2089template <class ELFT>
2090void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
2091 uint64_t PltEntryAddr, int32_t Index,
2092 unsigned RelOff) const {
2093 const endianness E = ELFT::TargetEndianness;
George Rimara4c7e742016-10-20 08:36:42 +00002094 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
2095 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
2096 // jr $25
Simon Atanasyana088bce2016-07-20 20:15:33 +00002097 write32<E>(Buf + 8, isMipsR6<ELFT>() ? 0x03200009 : 0x03200008);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00002098 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00002099 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00002100 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
2101 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00002102}
2103
2104template <class ELFT>
Peter Smithfb05cd92016-07-08 16:10:27 +00002105RelExpr MipsTargetInfo<ELFT>::getThunkExpr(RelExpr Expr, uint32_t Type,
2106 const InputFile &File,
2107 const SymbolBody &S) const {
Simon Atanasyan13f6da12016-03-31 21:26:23 +00002108 // Any MIPS PIC code function is invoked with its address in register $t9.
2109 // So if we have a branch instruction from non-PIC code to the PIC one
2110 // we cannot make the jump directly and need to create a small stubs
2111 // to save the target function address.
2112 // See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
2113 if (Type != R_MIPS_26)
Peter Smithfb05cd92016-07-08 16:10:27 +00002114 return Expr;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00002115 auto *F = dyn_cast<ELFFileBase<ELFT>>(&File);
2116 if (!F)
Peter Smithfb05cd92016-07-08 16:10:27 +00002117 return Expr;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00002118 // If current file has PIC code, LA25 stub is not required.
2119 if (F->getObj().getHeader()->e_flags & EF_MIPS_PIC)
Peter Smithfb05cd92016-07-08 16:10:27 +00002120 return Expr;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00002121 auto *D = dyn_cast<DefinedRegular<ELFT>>(&S);
Simon Atanasyan13f6da12016-03-31 21:26:23 +00002122 // LA25 is required if target file has PIC code
2123 // or target symbol is a PIC symbol.
Simon Atanasyanf967f092016-09-29 12:58:36 +00002124 return D && D->isMipsPIC() ? R_THUNK_ABS : Expr;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00002125}
2126
2127template <class ELFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00002128uint64_t MipsTargetInfo<ELFT>::getImplicitAddend(const uint8_t *Buf,
Rafael Espindola8cc68c32016-03-30 13:18:08 +00002129 uint32_t Type) const {
2130 const endianness E = ELFT::TargetEndianness;
2131 switch (Type) {
2132 default:
2133 return 0;
2134 case R_MIPS_32:
2135 case R_MIPS_GPREL32:
Simon Atanasyan875951e2016-09-05 15:42:39 +00002136 case R_MIPS_TLS_DTPREL32:
2137 case R_MIPS_TLS_TPREL32:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00002138 return read32<E>(Buf);
2139 case R_MIPS_26:
2140 // FIXME (simon): If the relocation target symbol is not a PLT entry
2141 // we should use another expression for calculation:
2142 // ((A << 2) | (P & 0xf0000000)) >> 2
Simon Atanasyand2ae3032016-07-22 05:56:43 +00002143 return SignExtend64<28>((read32<E>(Buf) & 0x3ffffff) << 2);
Rafael Espindola8cc68c32016-03-30 13:18:08 +00002144 case R_MIPS_GPREL16:
2145 case R_MIPS_LO16:
2146 case R_MIPS_PCLO16:
2147 case R_MIPS_TLS_DTPREL_HI16:
2148 case R_MIPS_TLS_DTPREL_LO16:
2149 case R_MIPS_TLS_TPREL_HI16:
2150 case R_MIPS_TLS_TPREL_LO16:
Rui Ueyamae517de62016-06-16 17:06:24 +00002151 return SignExtend64<16>(read32<E>(Buf));
Rafael Espindola8cc68c32016-03-30 13:18:08 +00002152 case R_MIPS_PC16:
2153 return getPcRelocAddend<E, 16, 2>(Buf);
2154 case R_MIPS_PC19_S2:
2155 return getPcRelocAddend<E, 19, 2>(Buf);
2156 case R_MIPS_PC21_S2:
2157 return getPcRelocAddend<E, 21, 2>(Buf);
2158 case R_MIPS_PC26_S2:
2159 return getPcRelocAddend<E, 26, 2>(Buf);
2160 case R_MIPS_PC32:
2161 return getPcRelocAddend<E, 32, 0>(Buf);
2162 }
2163}
2164
Eugene Leviant84569e62016-11-29 08:05:44 +00002165static std::pair<uint32_t, uint64_t>
2166calculateMipsRelChain(uint8_t *Loc, uint32_t Type, uint64_t Val) {
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00002167 // MIPS N64 ABI packs multiple relocations into the single relocation
2168 // record. In general, all up to three relocations can have arbitrary
2169 // types. In fact, Clang and GCC uses only a few combinations. For now,
2170 // we support two of them. That is allow to pass at least all LLVM
2171 // test suite cases.
2172 // <any relocation> / R_MIPS_SUB / R_MIPS_HI16 | R_MIPS_LO16
2173 // <any relocation> / R_MIPS_64 / R_MIPS_NONE
2174 // The first relocation is a 'real' relocation which is calculated
2175 // using the corresponding symbol's value. The second and the third
2176 // relocations used to modify result of the first one: extend it to
2177 // 64-bit, extract high or low part etc. For details, see part 2.9 Relocation
2178 // at the https://dmz-portal.mips.com/mw/images/8/82/007-4658-001.pdf
2179 uint32_t Type2 = (Type >> 8) & 0xff;
2180 uint32_t Type3 = (Type >> 16) & 0xff;
2181 if (Type2 == R_MIPS_NONE && Type3 == R_MIPS_NONE)
2182 return std::make_pair(Type, Val);
2183 if (Type2 == R_MIPS_64 && Type3 == R_MIPS_NONE)
2184 return std::make_pair(Type2, Val);
2185 if (Type2 == R_MIPS_SUB && (Type3 == R_MIPS_HI16 || Type3 == R_MIPS_LO16))
2186 return std::make_pair(Type3, -Val);
Eugene Leviant84569e62016-11-29 08:05:44 +00002187 error(getErrorLocation(Loc) + "unsupported relocations combination " +
2188 Twine(Type));
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00002189 return std::make_pair(Type & 0xff, Val);
2190}
2191
Rafael Espindola8cc68c32016-03-30 13:18:08 +00002192template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00002193void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type,
2194 uint64_t Val) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00002195 const endianness E = ELFT::TargetEndianness;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00002196 // Thread pointer and DRP offsets from the start of TLS data area.
2197 // https://www.linux-mips.org/wiki/NPTL
Simon Atanasyan875951e2016-09-05 15:42:39 +00002198 if (Type == R_MIPS_TLS_DTPREL_HI16 || Type == R_MIPS_TLS_DTPREL_LO16 ||
Simon Atanasyan643729d2016-09-05 15:42:43 +00002199 Type == R_MIPS_TLS_DTPREL32 || Type == R_MIPS_TLS_DTPREL64)
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00002200 Val -= 0x8000;
Simon Atanasyan875951e2016-09-05 15:42:39 +00002201 else if (Type == R_MIPS_TLS_TPREL_HI16 || Type == R_MIPS_TLS_TPREL_LO16 ||
Simon Atanasyan643729d2016-09-05 15:42:43 +00002202 Type == R_MIPS_TLS_TPREL32 || Type == R_MIPS_TLS_TPREL64)
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00002203 Val -= 0x7000;
Simon Atanasyan9e0297b2016-11-05 22:58:01 +00002204 if (ELFT::Is64Bits || Config->MipsN32Abi)
Eugene Leviant84569e62016-11-29 08:05:44 +00002205 std::tie(Type, Val) = calculateMipsRelChain(Loc, Type, Val);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00002206 switch (Type) {
2207 case R_MIPS_32:
Simon Atanasyan9ac81982016-05-06 15:02:50 +00002208 case R_MIPS_GPREL32:
Simon Atanasyan875951e2016-09-05 15:42:39 +00002209 case R_MIPS_TLS_DTPREL32:
2210 case R_MIPS_TLS_TPREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00002211 write32<E>(Loc, Val);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00002212 break;
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00002213 case R_MIPS_64:
Simon Atanasyan643729d2016-09-05 15:42:43 +00002214 case R_MIPS_TLS_DTPREL64:
2215 case R_MIPS_TLS_TPREL64:
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00002216 write64<E>(Loc, Val);
2217 break;
Simon Atanasyan10296c22016-05-07 07:36:47 +00002218 case R_MIPS_26:
Simon Atanasyand2ae3032016-07-22 05:56:43 +00002219 write32<E>(Loc, (read32<E>(Loc) & ~0x3ffffff) | ((Val >> 2) & 0x3ffffff));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00002220 break;
Simon Atanasyanbe804552016-05-04 17:47:11 +00002221 case R_MIPS_GOT_DISP:
2222 case R_MIPS_GOT_PAGE:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00002223 case R_MIPS_GOT16:
Simon Atanasyan9ac81982016-05-06 15:02:50 +00002224 case R_MIPS_GPREL16:
Simon Atanasyan002e2442016-06-23 15:26:31 +00002225 case R_MIPS_TLS_GD:
2226 case R_MIPS_TLS_LDM:
Eugene Leviant84569e62016-11-29 08:05:44 +00002227 checkInt<16>(Loc, Val, Type);
Rafael Espindola58cd5db2016-04-19 22:46:03 +00002228 // fallthrough
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00002229 case R_MIPS_CALL16:
Simon Atanasyane933a8e2016-08-18 19:08:36 +00002230 case R_MIPS_CALL_LO16:
Simon Atanasyan978f91c2016-08-18 19:08:41 +00002231 case R_MIPS_GOT_LO16:
Simon Atanasyanbe804552016-05-04 17:47:11 +00002232 case R_MIPS_GOT_OFST:
Simon Atanasyan5b9ac412016-05-06 15:02:54 +00002233 case R_MIPS_LO16:
2234 case R_MIPS_PCLO16:
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00002235 case R_MIPS_TLS_DTPREL_LO16:
Simon Atanasyan002e2442016-06-23 15:26:31 +00002236 case R_MIPS_TLS_GOTTPREL:
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00002237 case R_MIPS_TLS_TPREL_LO16:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00002238 writeMipsLo16<E>(Loc, Val);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00002239 break;
Simon Atanasyane933a8e2016-08-18 19:08:36 +00002240 case R_MIPS_CALL_HI16:
Simon Atanasyan978f91c2016-08-18 19:08:41 +00002241 case R_MIPS_GOT_HI16:
Simon Atanasyan3b377852016-03-04 10:55:20 +00002242 case R_MIPS_HI16:
Simon Atanasyan5b9ac412016-05-06 15:02:54 +00002243 case R_MIPS_PCHI16:
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00002244 case R_MIPS_TLS_DTPREL_HI16:
2245 case R_MIPS_TLS_TPREL_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00002246 writeMipsHi16<E>(Loc, Val);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00002247 break;
Simon Atanasyane5532a12016-08-31 11:47:21 +00002248 case R_MIPS_HIGHER:
2249 writeMipsHigher<E>(Loc, Val);
2250 break;
2251 case R_MIPS_HIGHEST:
2252 writeMipsHighest<E>(Loc, Val);
2253 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00002254 case R_MIPS_JALR:
2255 // Ignore this optimization relocation for now
2256 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00002257 case R_MIPS_PC16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00002258 applyMipsPcReloc<E, 16, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00002259 break;
2260 case R_MIPS_PC19_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00002261 applyMipsPcReloc<E, 19, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00002262 break;
2263 case R_MIPS_PC21_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00002264 applyMipsPcReloc<E, 21, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00002265 break;
2266 case R_MIPS_PC26_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00002267 applyMipsPcReloc<E, 26, 2>(Loc, Type, Val);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00002268 break;
2269 case R_MIPS_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00002270 applyMipsPcReloc<E, 32, 0>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00002271 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00002272 default:
Eugene Leviant84569e62016-11-29 08:05:44 +00002273 fatal(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00002274 }
2275}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00002276
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00002277template <class ELFT>
Rafael Espindolab8ff59a2016-04-28 14:34:39 +00002278bool MipsTargetInfo<ELFT>::usesOnlyLowPageBits(uint32_t Type) const {
Simon Atanasyanbe804552016-05-04 17:47:11 +00002279 return Type == R_MIPS_LO16 || Type == R_MIPS_GOT_OFST;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00002280}
Rafael Espindola01205f72015-09-22 18:19:46 +00002281}
2282}