]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lld/ELF/Arch/AArch64.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lld / ELF / Arch / AArch64.cpp
1 //===- AArch64.cpp --------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "Symbols.h"
10 #include "SyntheticSections.h"
11 #include "Target.h"
12 #include "Thunks.h"
13 #include "lld/Common/ErrorHandler.h"
14 #include "llvm/Object/ELF.h"
15 #include "llvm/Support/Endian.h"
16
17 using namespace llvm;
18 using namespace llvm::support::endian;
19 using namespace llvm::ELF;
20 using namespace lld;
21 using namespace lld::elf;
22
23 // Page(Expr) is the page address of the expression Expr, defined
24 // as (Expr & ~0xFFF). (This applies even if the machine page size
25 // supported by the platform has a different value.)
26 uint64_t elf::getAArch64Page(uint64_t expr) {
27   return expr & ~static_cast<uint64_t>(0xFFF);
28 }
29
30 namespace {
31 class AArch64 : public TargetInfo {
32 public:
33   AArch64();
34   RelExpr getRelExpr(RelType type, const Symbol &s,
35                      const uint8_t *loc) const override;
36   RelType getDynRel(RelType type) const override;
37   void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
38   void writePltHeader(uint8_t *buf) const override;
39   void writePlt(uint8_t *buf, uint64_t gotPltEntryAddr, uint64_t pltEntryAddr,
40                 int32_t index, unsigned relOff) const override;
41   bool needsThunk(RelExpr expr, RelType type, const InputFile *file,
42                   uint64_t branchAddr, const Symbol &s) const override;
43   uint32_t getThunkSectionSpacing() const override;
44   bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override;
45   bool usesOnlyLowPageBits(RelType type) const override;
46   void relocateOne(uint8_t *loc, RelType type, uint64_t val) const override;
47   RelExpr adjustRelaxExpr(RelType type, const uint8_t *data,
48                           RelExpr expr) const override;
49   void relaxTlsGdToLe(uint8_t *loc, RelType type, uint64_t val) const override;
50   void relaxTlsGdToIe(uint8_t *loc, RelType type, uint64_t val) const override;
51   void relaxTlsIeToLe(uint8_t *loc, RelType type, uint64_t val) const override;
52 };
53 } // namespace
54
55 AArch64::AArch64() {
56   copyRel = R_AARCH64_COPY;
57   relativeRel = R_AARCH64_RELATIVE;
58   iRelativeRel = R_AARCH64_IRELATIVE;
59   gotRel = R_AARCH64_GLOB_DAT;
60   noneRel = R_AARCH64_NONE;
61   pltRel = R_AARCH64_JUMP_SLOT;
62   symbolicRel = R_AARCH64_ABS64;
63   tlsDescRel = R_AARCH64_TLSDESC;
64   tlsGotRel = R_AARCH64_TLS_TPREL64;
65   pltEntrySize = 16;
66   pltHeaderSize = 32;
67   defaultMaxPageSize = 65536;
68
69   // Align to the 2 MiB page size (known as a superpage or huge page).
70   // FreeBSD automatically promotes 2 MiB-aligned allocations.
71   defaultImageBase = 0x200000;
72
73   needsThunks = true;
74 }
75
76 RelExpr AArch64::getRelExpr(RelType type, const Symbol &s,
77                             const uint8_t *loc) const {
78   switch (type) {
79   case R_AARCH64_TLSDESC_ADR_PAGE21:
80     return R_AARCH64_TLSDESC_PAGE;
81   case R_AARCH64_TLSDESC_LD64_LO12:
82   case R_AARCH64_TLSDESC_ADD_LO12:
83     return R_TLSDESC;
84   case R_AARCH64_TLSDESC_CALL:
85     return R_TLSDESC_CALL;
86   case R_AARCH64_TLSLE_ADD_TPREL_HI12:
87   case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
88   case R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
89   case R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
90   case R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
91   case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
92   case R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC:
93     return R_TLS;
94   case R_AARCH64_CALL26:
95   case R_AARCH64_CONDBR19:
96   case R_AARCH64_JUMP26:
97   case R_AARCH64_TSTBR14:
98     return R_PLT_PC;
99   case R_AARCH64_PREL16:
100   case R_AARCH64_PREL32:
101   case R_AARCH64_PREL64:
102   case R_AARCH64_ADR_PREL_LO21:
103   case R_AARCH64_LD_PREL_LO19:
104     return R_PC;
105   case R_AARCH64_ADR_PREL_PG_HI21:
106   case R_AARCH64_ADR_PREL_PG_HI21_NC:
107     return R_AARCH64_PAGE_PC;
108   case R_AARCH64_LD64_GOT_LO12_NC:
109   case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
110     return R_GOT;
111   case R_AARCH64_ADR_GOT_PAGE:
112   case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
113     return R_AARCH64_GOT_PAGE_PC;
114   case R_AARCH64_NONE:
115     return R_NONE;
116   default:
117     return R_ABS;
118   }
119 }
120
121 RelExpr AArch64::adjustRelaxExpr(RelType type, const uint8_t *data,
122                                  RelExpr expr) const {
123   if (expr == R_RELAX_TLS_GD_TO_IE) {
124     if (type == R_AARCH64_TLSDESC_ADR_PAGE21)
125       return R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC;
126     return R_RELAX_TLS_GD_TO_IE_ABS;
127   }
128   return expr;
129 }
130
131 bool AArch64::usesOnlyLowPageBits(RelType type) const {
132   switch (type) {
133   default:
134     return false;
135   case R_AARCH64_ADD_ABS_LO12_NC:
136   case R_AARCH64_LD64_GOT_LO12_NC:
137   case R_AARCH64_LDST128_ABS_LO12_NC:
138   case R_AARCH64_LDST16_ABS_LO12_NC:
139   case R_AARCH64_LDST32_ABS_LO12_NC:
140   case R_AARCH64_LDST64_ABS_LO12_NC:
141   case R_AARCH64_LDST8_ABS_LO12_NC:
142   case R_AARCH64_TLSDESC_ADD_LO12:
143   case R_AARCH64_TLSDESC_LD64_LO12:
144   case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
145     return true;
146   }
147 }
148
149 RelType AArch64::getDynRel(RelType type) const {
150   if (type == R_AARCH64_ABS64)
151     return type;
152   return R_AARCH64_NONE;
153 }
154
155 void AArch64::writeGotPlt(uint8_t *buf, const Symbol &) const {
156   write64le(buf, in.plt->getVA());
157 }
158
159 void AArch64::writePltHeader(uint8_t *buf) const {
160   const uint8_t pltData[] = {
161       0xf0, 0x7b, 0xbf, 0xa9, // stp    x16, x30, [sp,#-16]!
162       0x10, 0x00, 0x00, 0x90, // adrp   x16, Page(&(.plt.got[2]))
163       0x11, 0x02, 0x40, 0xf9, // ldr    x17, [x16, Offset(&(.plt.got[2]))]
164       0x10, 0x02, 0x00, 0x91, // add    x16, x16, Offset(&(.plt.got[2]))
165       0x20, 0x02, 0x1f, 0xd6, // br     x17
166       0x1f, 0x20, 0x03, 0xd5, // nop
167       0x1f, 0x20, 0x03, 0xd5, // nop
168       0x1f, 0x20, 0x03, 0xd5  // nop
169   };
170   memcpy(buf, pltData, sizeof(pltData));
171
172   uint64_t got = in.gotPlt->getVA();
173   uint64_t plt = in.plt->getVA();
174   relocateOne(buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
175               getAArch64Page(got + 16) - getAArch64Page(plt + 4));
176   relocateOne(buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, got + 16);
177   relocateOne(buf + 12, R_AARCH64_ADD_ABS_LO12_NC, got + 16);
178 }
179
180 void AArch64::writePlt(uint8_t *buf, uint64_t gotPltEntryAddr,
181                        uint64_t pltEntryAddr, int32_t index,
182                        unsigned relOff) const {
183   const uint8_t inst[] = {
184       0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
185       0x11, 0x02, 0x40, 0xf9, // ldr  x17, [x16, Offset(&(.plt.got[n]))]
186       0x10, 0x02, 0x00, 0x91, // add  x16, x16, Offset(&(.plt.got[n]))
187       0x20, 0x02, 0x1f, 0xd6  // br   x17
188   };
189   memcpy(buf, inst, sizeof(inst));
190
191   relocateOne(buf, R_AARCH64_ADR_PREL_PG_HI21,
192               getAArch64Page(gotPltEntryAddr) - getAArch64Page(pltEntryAddr));
193   relocateOne(buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, gotPltEntryAddr);
194   relocateOne(buf + 8, R_AARCH64_ADD_ABS_LO12_NC, gotPltEntryAddr);
195 }
196
197 bool AArch64::needsThunk(RelExpr expr, RelType type, const InputFile *file,
198                          uint64_t branchAddr, const Symbol &s) const {
199   // ELF for the ARM 64-bit architecture, section Call and Jump relocations
200   // only permits range extension thunks for R_AARCH64_CALL26 and
201   // R_AARCH64_JUMP26 relocation types.
202   if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26)
203     return false;
204   uint64_t dst = (expr == R_PLT_PC) ? s.getPltVA() : s.getVA();
205   return !inBranchRange(type, branchAddr, dst);
206 }
207
208 uint32_t AArch64::getThunkSectionSpacing() const {
209   // See comment in Arch/ARM.cpp for a more detailed explanation of
210   // getThunkSectionSpacing(). For AArch64 the only branches we are permitted to
211   // Thunk have a range of +/- 128 MiB
212   return (128 * 1024 * 1024) - 0x30000;
213 }
214
215 bool AArch64::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
216   if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26)
217     return true;
218   // The AArch64 call and unconditional branch instructions have a range of
219   // +/- 128 MiB.
220   uint64_t range = 128 * 1024 * 1024;
221   if (dst > src) {
222     // Immediate of branch is signed.
223     range -= 4;
224     return dst - src <= range;
225   }
226   return src - dst <= range;
227 }
228
229 static void write32AArch64Addr(uint8_t *l, uint64_t imm) {
230   uint32_t immLo = (imm & 0x3) << 29;
231   uint32_t immHi = (imm & 0x1FFFFC) << 3;
232   uint64_t mask = (0x3 << 29) | (0x1FFFFC << 3);
233   write32le(l, (read32le(l) & ~mask) | immLo | immHi);
234 }
235
236 // Return the bits [Start, End] from Val shifted Start bits.
237 // For instance, getBits(0xF0, 4, 8) returns 0xF.
238 static uint64_t getBits(uint64_t val, int start, int end) {
239   uint64_t mask = ((uint64_t)1 << (end + 1 - start)) - 1;
240   return (val >> start) & mask;
241 }
242
243 static void or32le(uint8_t *p, int32_t v) { write32le(p, read32le(p) | v); }
244
245 // Update the immediate field in a AARCH64 ldr, str, and add instruction.
246 static void or32AArch64Imm(uint8_t *l, uint64_t imm) {
247   or32le(l, (imm & 0xFFF) << 10);
248 }
249
250 void AArch64::relocateOne(uint8_t *loc, RelType type, uint64_t val) const {
251   switch (type) {
252   case R_AARCH64_ABS16:
253   case R_AARCH64_PREL16:
254     checkIntUInt(loc, val, 16, type);
255     write16le(loc, val);
256     break;
257   case R_AARCH64_ABS32:
258   case R_AARCH64_PREL32:
259     checkIntUInt(loc, val, 32, type);
260     write32le(loc, val);
261     break;
262   case R_AARCH64_ABS64:
263   case R_AARCH64_PREL64:
264     write64le(loc, val);
265     break;
266   case R_AARCH64_ADD_ABS_LO12_NC:
267     or32AArch64Imm(loc, val);
268     break;
269   case R_AARCH64_ADR_GOT_PAGE:
270   case R_AARCH64_ADR_PREL_PG_HI21:
271   case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
272   case R_AARCH64_TLSDESC_ADR_PAGE21:
273     checkInt(loc, val, 33, type);
274     LLVM_FALLTHROUGH;
275   case R_AARCH64_ADR_PREL_PG_HI21_NC:
276     write32AArch64Addr(loc, val >> 12);
277     break;
278   case R_AARCH64_ADR_PREL_LO21:
279     checkInt(loc, val, 21, type);
280     write32AArch64Addr(loc, val);
281     break;
282   case R_AARCH64_JUMP26:
283     // Normally we would just write the bits of the immediate field, however
284     // when patching instructions for the cpu errata fix -fix-cortex-a53-843419
285     // we want to replace a non-branch instruction with a branch immediate
286     // instruction. By writing all the bits of the instruction including the
287     // opcode and the immediate (0 001 | 01 imm26) we can do this
288     // transformation by placing a R_AARCH64_JUMP26 relocation at the offset of
289     // the instruction we want to patch.
290     write32le(loc, 0x14000000);
291     LLVM_FALLTHROUGH;
292   case R_AARCH64_CALL26:
293     checkInt(loc, val, 28, type);
294     or32le(loc, (val & 0x0FFFFFFC) >> 2);
295     break;
296   case R_AARCH64_CONDBR19:
297   case R_AARCH64_LD_PREL_LO19:
298     checkAlignment(loc, val, 4, type);
299     checkInt(loc, val, 21, type);
300     or32le(loc, (val & 0x1FFFFC) << 3);
301     break;
302   case R_AARCH64_LDST8_ABS_LO12_NC:
303   case R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
304     or32AArch64Imm(loc, getBits(val, 0, 11));
305     break;
306   case R_AARCH64_LDST16_ABS_LO12_NC:
307   case R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
308     checkAlignment(loc, val, 2, type);
309     or32AArch64Imm(loc, getBits(val, 1, 11));
310     break;
311   case R_AARCH64_LDST32_ABS_LO12_NC:
312   case R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
313     checkAlignment(loc, val, 4, type);
314     or32AArch64Imm(loc, getBits(val, 2, 11));
315     break;
316   case R_AARCH64_LDST64_ABS_LO12_NC:
317   case R_AARCH64_LD64_GOT_LO12_NC:
318   case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
319   case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
320   case R_AARCH64_TLSDESC_LD64_LO12:
321     checkAlignment(loc, val, 8, type);
322     or32AArch64Imm(loc, getBits(val, 3, 11));
323     break;
324   case R_AARCH64_LDST128_ABS_LO12_NC:
325   case R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC:
326     checkAlignment(loc, val, 16, type);
327     or32AArch64Imm(loc, getBits(val, 4, 11));
328     break;
329   case R_AARCH64_MOVW_UABS_G0_NC:
330     or32le(loc, (val & 0xFFFF) << 5);
331     break;
332   case R_AARCH64_MOVW_UABS_G1_NC:
333     or32le(loc, (val & 0xFFFF0000) >> 11);
334     break;
335   case R_AARCH64_MOVW_UABS_G2_NC:
336     or32le(loc, (val & 0xFFFF00000000) >> 27);
337     break;
338   case R_AARCH64_MOVW_UABS_G3:
339     or32le(loc, (val & 0xFFFF000000000000) >> 43);
340     break;
341   case R_AARCH64_TSTBR14:
342     checkInt(loc, val, 16, type);
343     or32le(loc, (val & 0xFFFC) << 3);
344     break;
345   case R_AARCH64_TLSLE_ADD_TPREL_HI12:
346     checkUInt(loc, val, 24, type);
347     or32AArch64Imm(loc, val >> 12);
348     break;
349   case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
350   case R_AARCH64_TLSDESC_ADD_LO12:
351     or32AArch64Imm(loc, val);
352     break;
353   default:
354     error(getErrorLocation(loc) + "unrecognized relocation " + toString(type));
355   }
356 }
357
358 void AArch64::relaxTlsGdToLe(uint8_t *loc, RelType type, uint64_t val) const {
359   // TLSDESC Global-Dynamic relocation are in the form:
360   //   adrp    x0, :tlsdesc:v             [R_AARCH64_TLSDESC_ADR_PAGE21]
361   //   ldr     x1, [x0, #:tlsdesc_lo12:v  [R_AARCH64_TLSDESC_LD64_LO12]
362   //   add     x0, x0, :tlsdesc_los:v     [R_AARCH64_TLSDESC_ADD_LO12]
363   //   .tlsdesccall                       [R_AARCH64_TLSDESC_CALL]
364   //   blr     x1
365   // And it can optimized to:
366   //   movz    x0, #0x0, lsl #16
367   //   movk    x0, #0x10
368   //   nop
369   //   nop
370   checkUInt(loc, val, 32, type);
371
372   switch (type) {
373   case R_AARCH64_TLSDESC_ADD_LO12:
374   case R_AARCH64_TLSDESC_CALL:
375     write32le(loc, 0xd503201f); // nop
376     return;
377   case R_AARCH64_TLSDESC_ADR_PAGE21:
378     write32le(loc, 0xd2a00000 | (((val >> 16) & 0xffff) << 5)); // movz
379     return;
380   case R_AARCH64_TLSDESC_LD64_LO12:
381     write32le(loc, 0xf2800000 | ((val & 0xffff) << 5)); // movk
382     return;
383   default:
384     llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
385   }
386 }
387
388 void AArch64::relaxTlsGdToIe(uint8_t *loc, RelType type, uint64_t val) const {
389   // TLSDESC Global-Dynamic relocation are in the form:
390   //   adrp    x0, :tlsdesc:v             [R_AARCH64_TLSDESC_ADR_PAGE21]
391   //   ldr     x1, [x0, #:tlsdesc_lo12:v  [R_AARCH64_TLSDESC_LD64_LO12]
392   //   add     x0, x0, :tlsdesc_los:v     [R_AARCH64_TLSDESC_ADD_LO12]
393   //   .tlsdesccall                       [R_AARCH64_TLSDESC_CALL]
394   //   blr     x1
395   // And it can optimized to:
396   //   adrp    x0, :gottprel:v
397   //   ldr     x0, [x0, :gottprel_lo12:v]
398   //   nop
399   //   nop
400
401   switch (type) {
402   case R_AARCH64_TLSDESC_ADD_LO12:
403   case R_AARCH64_TLSDESC_CALL:
404     write32le(loc, 0xd503201f); // nop
405     break;
406   case R_AARCH64_TLSDESC_ADR_PAGE21:
407     write32le(loc, 0x90000000); // adrp
408     relocateOne(loc, R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21, val);
409     break;
410   case R_AARCH64_TLSDESC_LD64_LO12:
411     write32le(loc, 0xf9400000); // ldr
412     relocateOne(loc, R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC, val);
413     break;
414   default:
415     llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
416   }
417 }
418
419 void AArch64::relaxTlsIeToLe(uint8_t *loc, RelType type, uint64_t val) const {
420   checkUInt(loc, val, 32, type);
421
422   if (type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
423     // Generate MOVZ.
424     uint32_t regNo = read32le(loc) & 0x1f;
425     write32le(loc, (0xd2a00000 | regNo) | (((val >> 16) & 0xffff) << 5));
426     return;
427   }
428   if (type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
429     // Generate MOVK.
430     uint32_t regNo = read32le(loc) & 0x1f;
431     write32le(loc, (0xf2800000 | regNo) | ((val & 0xffff) << 5));
432     return;
433   }
434   llvm_unreachable("invalid relocation for TLS IE to LE relaxation");
435 }
436
437 // AArch64 may use security features in variant PLT sequences. These are:
438 // Pointer Authentication (PAC), introduced in armv8.3-a and Branch Target
439 // Indicator (BTI) introduced in armv8.5-a. The additional instructions used
440 // in the variant Plt sequences are encoded in the Hint space so they can be
441 // deployed on older architectures, which treat the instructions as a nop.
442 // PAC and BTI can be combined leading to the following combinations:
443 // writePltHeader
444 // writePltHeaderBti (no PAC Header needed)
445 // writePlt
446 // writePltBti (BTI only)
447 // writePltPac (PAC only)
448 // writePltBtiPac (BTI and PAC)
449 //
450 // When PAC is enabled the dynamic loader encrypts the address that it places
451 // in the .got.plt using the pacia1716 instruction which encrypts the value in
452 // x17 using the modifier in x16. The static linker places autia1716 before the
453 // indirect branch to x17 to authenticate the address in x17 with the modifier
454 // in x16. This makes it more difficult for an attacker to modify the value in
455 // the .got.plt.
456 //
457 // When BTI is enabled all indirect branches must land on a bti instruction.
458 // The static linker must place a bti instruction at the start of any PLT entry
459 // that may be the target of an indirect branch. As the PLT entries call the
460 // lazy resolver indirectly this must have a bti instruction at start. In
461 // general a bti instruction is not needed for a PLT entry as indirect calls
462 // are resolved to the function address and not the PLT entry for the function.
463 // There are a small number of cases where the PLT address can escape, such as
464 // taking the address of a function or ifunc via a non got-generating
465 // relocation, and a shared library refers to that symbol.
466 //
467 // We use the bti c variant of the instruction which permits indirect branches
468 // (br) via x16/x17 and indirect function calls (blr) via any register. The ABI
469 // guarantees that all indirect branches from code requiring BTI protection
470 // will go via x16/x17
471
472 namespace {
473 class AArch64BtiPac final : public AArch64 {
474 public:
475   AArch64BtiPac();
476   void writePltHeader(uint8_t *buf) const override;
477   void writePlt(uint8_t *buf, uint64_t gotPltEntryAddr, uint64_t pltEntryAddr,
478                 int32_t index, unsigned relOff) const override;
479
480 private:
481   bool btiHeader; // bti instruction needed in PLT Header
482   bool btiEntry;  // bti instruction needed in PLT Entry
483   bool pacEntry;  // autia1716 instruction needed in PLT Entry
484 };
485 } // namespace
486
487 AArch64BtiPac::AArch64BtiPac() {
488   btiHeader = (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI);
489   // A BTI (Branch Target Indicator) Plt Entry is only required if the
490   // address of the PLT entry can be taken by the program, which permits an
491   // indirect jump to the PLT entry. This can happen when the address
492   // of the PLT entry for a function is canonicalised due to the address of
493   // the function in an executable being taken by a shared library.
494   // FIXME: There is a potential optimization to omit the BTI if we detect
495   // that the address of the PLT entry isn't taken.
496   btiEntry = btiHeader && !config->shared;
497   pacEntry = (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_PAC);
498
499   if (btiEntry || pacEntry)
500     pltEntrySize = 24;
501 }
502
503 void AArch64BtiPac::writePltHeader(uint8_t *buf) const {
504   const uint8_t btiData[] = { 0x5f, 0x24, 0x03, 0xd5 }; // bti c
505   const uint8_t pltData[] = {
506       0xf0, 0x7b, 0xbf, 0xa9, // stp    x16, x30, [sp,#-16]!
507       0x10, 0x00, 0x00, 0x90, // adrp   x16, Page(&(.plt.got[2]))
508       0x11, 0x02, 0x40, 0xf9, // ldr    x17, [x16, Offset(&(.plt.got[2]))]
509       0x10, 0x02, 0x00, 0x91, // add    x16, x16, Offset(&(.plt.got[2]))
510       0x20, 0x02, 0x1f, 0xd6, // br     x17
511       0x1f, 0x20, 0x03, 0xd5, // nop
512       0x1f, 0x20, 0x03, 0xd5  // nop
513   };
514   const uint8_t nopData[] = { 0x1f, 0x20, 0x03, 0xd5 }; // nop
515
516   uint64_t got = in.gotPlt->getVA();
517   uint64_t plt = in.plt->getVA();
518
519   if (btiHeader) {
520     // PltHeader is called indirectly by plt[N]. Prefix pltData with a BTI C
521     // instruction.
522     memcpy(buf, btiData, sizeof(btiData));
523     buf += sizeof(btiData);
524     plt += sizeof(btiData);
525   }
526   memcpy(buf, pltData, sizeof(pltData));
527
528   relocateOne(buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
529               getAArch64Page(got + 16) - getAArch64Page(plt + 8));
530   relocateOne(buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, got + 16);
531   relocateOne(buf + 12, R_AARCH64_ADD_ABS_LO12_NC, got + 16);
532   if (!btiHeader)
533     // We didn't add the BTI c instruction so round out size with NOP.
534     memcpy(buf + sizeof(pltData), nopData, sizeof(nopData));
535 }
536
537 void AArch64BtiPac::writePlt(uint8_t *buf, uint64_t gotPltEntryAddr,
538                              uint64_t pltEntryAddr, int32_t index,
539                              unsigned relOff) const {
540   // The PLT entry is of the form:
541   // [btiData] addrInst (pacBr | stdBr) [nopData]
542   const uint8_t btiData[] = { 0x5f, 0x24, 0x03, 0xd5 }; // bti c
543   const uint8_t addrInst[] = {
544       0x10, 0x00, 0x00, 0x90,  // adrp x16, Page(&(.plt.got[n]))
545       0x11, 0x02, 0x40, 0xf9,  // ldr  x17, [x16, Offset(&(.plt.got[n]))]
546       0x10, 0x02, 0x00, 0x91   // add  x16, x16, Offset(&(.plt.got[n]))
547   };
548   const uint8_t pacBr[] = {
549       0x9f, 0x21, 0x03, 0xd5,  // autia1716
550       0x20, 0x02, 0x1f, 0xd6   // br   x17
551   };
552   const uint8_t stdBr[] = {
553       0x20, 0x02, 0x1f, 0xd6,  // br   x17
554       0x1f, 0x20, 0x03, 0xd5   // nop
555   };
556   const uint8_t nopData[] = { 0x1f, 0x20, 0x03, 0xd5 }; // nop
557
558   if (btiEntry) {
559     memcpy(buf, btiData, sizeof(btiData));
560     buf += sizeof(btiData);
561     pltEntryAddr += sizeof(btiData);
562   }
563
564   memcpy(buf, addrInst, sizeof(addrInst));
565   relocateOne(buf, R_AARCH64_ADR_PREL_PG_HI21,
566               getAArch64Page(gotPltEntryAddr) -
567                   getAArch64Page(pltEntryAddr));
568   relocateOne(buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, gotPltEntryAddr);
569   relocateOne(buf + 8, R_AARCH64_ADD_ABS_LO12_NC, gotPltEntryAddr);
570
571   if (pacEntry)
572     memcpy(buf + sizeof(addrInst), pacBr, sizeof(pacBr));
573   else
574     memcpy(buf + sizeof(addrInst), stdBr, sizeof(stdBr));
575   if (!btiEntry)
576     // We didn't add the BTI c instruction so round out size with NOP.
577     memcpy(buf + sizeof(addrInst) + sizeof(stdBr), nopData, sizeof(nopData));
578 }
579
580 static TargetInfo *getTargetInfo() {
581   if (config->andFeatures & (GNU_PROPERTY_AARCH64_FEATURE_1_BTI |
582                              GNU_PROPERTY_AARCH64_FEATURE_1_PAC)) {
583     static AArch64BtiPac t;
584     return &t;
585   }
586   static AArch64 t;
587   return &t;
588 }
589
590 TargetInfo *elf::getAArch64TargetInfo() { return getTargetInfo(); }