1 /* AArch64-specific support for ELF.
2 Copyright (C) 2009-2025 Free Software Foundation, Inc.
3 Contributed by ARM Ltd.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; see the file COPYING3. If not,
19 see <http://www.gnu.org/licenses/>. */
24 #include "elfxx-aarch64.h"
29 #define MASK(n) ((1u << (n)) - 1)
31 /* Sign-extend VALUE, which has the indicated number of BITS. */
34 _bfd_aarch64_sign_extend (bfd_vma value
, int bits
)
36 if (value
& ((bfd_vma
) 1 << (bits
- 1)))
37 /* VALUE is negative. */
38 value
|= ((bfd_vma
) - 1) << bits
;
43 /* Decode the IMM field of ADRP. */
46 _bfd_aarch64_decode_adrp_imm (uint32_t insn
)
48 return (((insn
>> 5) & MASK (19)) << 2) | ((insn
>> 29) & MASK (2));
51 /* Reencode the imm field of add immediate. */
52 static inline uint32_t
53 reencode_add_imm (uint32_t insn
, uint32_t imm
)
55 return (insn
& ~(MASK (12) << 10)) | ((imm
& MASK (12)) << 10);
58 /* Reencode the IMM field of ADR. */
61 _bfd_aarch64_reencode_adr_imm (uint32_t insn
, uint32_t imm
)
63 return (insn
& ~((MASK (2) << 29) | (MASK (19) << 5)))
64 | ((imm
& MASK (2)) << 29) | ((imm
& (MASK (19) << 2)) << 3);
67 /* Reencode the imm field of ld/st pos immediate. */
68 static inline uint32_t
69 reencode_ldst_pos_imm (uint32_t insn
, uint32_t imm
)
71 return (insn
& ~(MASK (12) << 10)) | ((imm
& MASK (12)) << 10);
74 /* Encode the 26-bit offset of unconditional branch. */
75 static inline uint32_t
76 reencode_branch_ofs_26 (uint32_t insn
, uint32_t ofs
)
78 return (insn
& ~MASK (26)) | (ofs
& MASK (26));
81 /* Encode the 19-bit offset of conditional branch and compare & branch. */
82 static inline uint32_t
83 reencode_cond_branch_ofs_19 (uint32_t insn
, uint32_t ofs
)
85 return (insn
& ~(MASK (19) << 5)) | ((ofs
& MASK (19)) << 5);
88 /* Decode the 19-bit offset of load literal. */
89 static inline uint32_t
90 reencode_ld_lit_ofs_19 (uint32_t insn
, uint32_t ofs
)
92 return (insn
& ~(MASK (19) << 5)) | ((ofs
& MASK (19)) << 5);
95 /* Encode the 14-bit offset of test & branch. */
96 static inline uint32_t
97 reencode_tst_branch_ofs_14 (uint32_t insn
, uint32_t ofs
)
99 return (insn
& ~(MASK (14) << 5)) | ((ofs
& MASK (14)) << 5);
102 /* Reencode the imm field of move wide. */
103 static inline uint32_t
104 reencode_movw_imm (uint32_t insn
, uint32_t imm
)
106 return (insn
& ~(MASK (16) << 5)) | ((imm
& MASK (16)) << 5);
109 /* Reencode mov[zn] to movz. */
110 static inline uint32_t
111 reencode_movzn_to_movz (uint32_t opcode
)
113 return opcode
| (1 << 30);
116 /* Reencode mov[zn] to movn. */
117 static inline uint32_t
118 reencode_movzn_to_movn (uint32_t opcode
)
120 return opcode
& ~(1 << 30);
123 /* Return non-zero if the indicated VALUE has overflowed the maximum
124 range expressible by a unsigned number with the indicated number of
127 static bfd_reloc_status_type
128 aarch64_unsigned_overflow (bfd_vma value
, unsigned int bits
)
131 if (bits
>= sizeof (bfd_vma
) * 8)
133 lim
= (bfd_vma
) 1 << bits
;
135 return bfd_reloc_overflow
;
139 /* Return non-zero if the indicated VALUE has overflowed the maximum
140 range expressible by an signed number with the indicated number of
143 static bfd_reloc_status_type
144 aarch64_signed_overflow (bfd_vma value
, unsigned int bits
)
146 bfd_signed_vma svalue
= (bfd_signed_vma
) value
;
149 if (bits
>= sizeof (bfd_vma
) * 8)
151 lim
= (bfd_signed_vma
) 1 << (bits
- 1);
152 if (svalue
< -lim
|| svalue
>= lim
)
153 return bfd_reloc_overflow
;
157 /* Insert the addend/value into the instruction or data object being
159 bfd_reloc_status_type
160 _bfd_aarch64_elf_put_addend (bfd
*abfd
,
161 bfd_byte
*address
, bfd_reloc_code_real_type r_type
,
162 reloc_howto_type
*howto
, bfd_signed_vma addend
)
164 bfd_reloc_status_type status
= bfd_reloc_ok
;
165 bfd_signed_vma old_addend
= addend
;
169 size
= bfd_get_reloc_size (howto
);
175 contents
= bfd_get_16 (abfd
, address
);
178 if (howto
->src_mask
!= 0xffffffff)
179 /* Must be 32-bit instruction, always little-endian. */
180 contents
= bfd_getl32 (address
);
182 /* Must be 32-bit data (endianness dependent). */
183 contents
= bfd_get_32 (abfd
, address
);
186 contents
= bfd_get_64 (abfd
, address
);
192 switch (howto
->complain_on_overflow
)
194 case complain_overflow_dont
:
196 case complain_overflow_signed
:
197 status
= aarch64_signed_overflow (addend
,
198 howto
->bitsize
+ howto
->rightshift
);
200 case complain_overflow_unsigned
:
201 status
= aarch64_unsigned_overflow (addend
,
202 howto
->bitsize
+ howto
->rightshift
);
204 case complain_overflow_bitfield
:
209 addend
>>= howto
->rightshift
;
213 case BFD_RELOC_AARCH64_CALL26
:
214 case BFD_RELOC_AARCH64_JUMP26
:
215 contents
= reencode_branch_ofs_26 (contents
, addend
);
218 case BFD_RELOC_AARCH64_BRANCH19
:
219 contents
= reencode_cond_branch_ofs_19 (contents
, addend
);
222 case BFD_RELOC_AARCH64_TSTBR14
:
223 contents
= reencode_tst_branch_ofs_14 (contents
, addend
);
226 case BFD_RELOC_AARCH64_GOT_LD_PREL19
:
227 case BFD_RELOC_AARCH64_LD_LO19_PCREL
:
228 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19
:
229 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19
:
230 if (old_addend
& ((1 << howto
->rightshift
) - 1))
231 return bfd_reloc_overflow
;
232 contents
= reencode_ld_lit_ofs_19 (contents
, addend
);
235 case BFD_RELOC_AARCH64_TLSDESC_CALL
:
238 case BFD_RELOC_AARCH64_ADR_GOT_PAGE
:
239 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL
:
240 case BFD_RELOC_AARCH64_ADR_HI21_PCREL
:
241 case BFD_RELOC_AARCH64_ADR_LO21_PCREL
:
242 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21
:
243 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21
:
244 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21
:
245 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21
:
246 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
:
247 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21
:
248 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21
:
249 contents
= _bfd_aarch64_reencode_adr_imm (contents
, addend
);
252 case BFD_RELOC_AARCH64_ADD_LO12
:
253 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12
:
254 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC
:
255 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12
:
256 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12
:
257 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC
:
258 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC
:
259 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12
:
260 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12
:
261 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC
:
262 /* Corresponds to: add rd, rn, #uimm12 to provide the low order
263 12 bits of the page offset following
264 BFD_RELOC_AARCH64_ADR_HI21_PCREL which computes the
265 (pc-relative) page base. */
266 contents
= reencode_add_imm (contents
, addend
);
269 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14
:
270 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC
:
271 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15
:
272 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15
:
273 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC
:
274 case BFD_RELOC_AARCH64_LDST128_LO12
:
275 case BFD_RELOC_AARCH64_LDST16_LO12
:
276 case BFD_RELOC_AARCH64_LDST32_LO12
:
277 case BFD_RELOC_AARCH64_LDST64_LO12
:
278 case BFD_RELOC_AARCH64_LDST8_LO12
:
279 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC
:
280 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12
:
281 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC
:
282 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
:
283 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12
:
284 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC
:
285 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12
:
286 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC
:
287 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12
:
288 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC
:
289 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12
:
290 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC
:
291 case BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12
:
292 case BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12_NC
:
293 case BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12
:
294 case BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12_NC
:
295 case BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12
:
296 case BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12_NC
:
297 case BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12
:
298 case BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12_NC
:
299 if (old_addend
& ((1 << howto
->rightshift
) - 1))
300 return bfd_reloc_overflow
;
301 /* Used for ldr*|str* rt, [rn, #uimm12] to provide the low order
302 12 bits address offset. */
303 contents
= reencode_ldst_pos_imm (contents
, addend
);
306 /* Group relocations to create high bits of a 16, 32, 48 or 64
307 bit signed data or abs address inline. Will change
308 instruction to MOVN or MOVZ depending on sign of calculated
311 case BFD_RELOC_AARCH64_MOVW_G0_S
:
312 case BFD_RELOC_AARCH64_MOVW_G1_S
:
313 case BFD_RELOC_AARCH64_MOVW_G2_S
:
314 case BFD_RELOC_AARCH64_MOVW_PREL_G0
:
315 case BFD_RELOC_AARCH64_MOVW_PREL_G1
:
316 case BFD_RELOC_AARCH64_MOVW_PREL_G2
:
317 case BFD_RELOC_AARCH64_MOVW_PREL_G3
:
318 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0
:
319 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1
:
320 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2
:
321 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0
:
322 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1
:
323 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2
:
324 /* NOTE: We can only come here with movz or movn. */
327 /* Force use of MOVN. */
329 contents
= reencode_movzn_to_movn (contents
);
333 /* Force use of MOVZ. */
334 contents
= reencode_movzn_to_movz (contents
);
338 /* Group relocations to create a 16, 32, 48 or 64 bit unsigned
339 data or abs address inline. */
341 case BFD_RELOC_AARCH64_MOVW_G0
:
342 case BFD_RELOC_AARCH64_MOVW_G0_NC
:
343 case BFD_RELOC_AARCH64_MOVW_G1
:
344 case BFD_RELOC_AARCH64_MOVW_G1_NC
:
345 case BFD_RELOC_AARCH64_MOVW_G2
:
346 case BFD_RELOC_AARCH64_MOVW_G2_NC
:
347 case BFD_RELOC_AARCH64_MOVW_G3
:
348 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC
:
349 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1
:
350 case BFD_RELOC_AARCH64_MOVW_PREL_G0_NC
:
351 case BFD_RELOC_AARCH64_MOVW_PREL_G1_NC
:
352 case BFD_RELOC_AARCH64_MOVW_PREL_G2_NC
:
353 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC
:
354 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1
:
355 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC
:
356 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1
:
357 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC
:
358 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1
:
359 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC
:
360 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC
:
361 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
:
362 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC
:
363 contents
= reencode_movw_imm (contents
, addend
);
367 /* Repack simple data */
368 if (howto
->dst_mask
& (howto
->dst_mask
+ 1))
369 return bfd_reloc_notsupported
;
371 contents
= ((contents
& ~howto
->dst_mask
) | (addend
& howto
->dst_mask
));
378 bfd_put_16 (abfd
, contents
, address
);
381 if (howto
->dst_mask
!= 0xffffffff)
382 /* must be 32-bit instruction, always little-endian */
383 bfd_putl32 (contents
, address
);
385 /* must be 32-bit data (endianness dependent) */
386 bfd_put_32 (abfd
, contents
, address
);
389 bfd_put_64 (abfd
, contents
, address
);
399 _bfd_aarch64_elf_resolve_relocation (bfd
*input_bfd
,
400 bfd_reloc_code_real_type r_type
,
401 bfd_vma place
, bfd_vma value
,
402 bfd_vma addend
, bool weak_undef_p
)
404 bool tls_reloc
= true;
407 case BFD_RELOC_AARCH64_NONE
:
408 case BFD_RELOC_AARCH64_TLSDESC_CALL
:
411 case BFD_RELOC_AARCH64_16_PCREL
:
412 case BFD_RELOC_AARCH64_32_PCREL
:
413 case BFD_RELOC_AARCH64_64_PCREL
:
414 case BFD_RELOC_AARCH64_ADR_LO21_PCREL
:
415 case BFD_RELOC_AARCH64_BRANCH19
:
416 case BFD_RELOC_AARCH64_LD_LO19_PCREL
:
417 case BFD_RELOC_AARCH64_MOVW_PREL_G0
:
418 case BFD_RELOC_AARCH64_MOVW_PREL_G0_NC
:
419 case BFD_RELOC_AARCH64_MOVW_PREL_G1
:
420 case BFD_RELOC_AARCH64_MOVW_PREL_G1_NC
:
421 case BFD_RELOC_AARCH64_MOVW_PREL_G2
:
422 case BFD_RELOC_AARCH64_MOVW_PREL_G2_NC
:
423 case BFD_RELOC_AARCH64_MOVW_PREL_G3
:
424 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21
:
425 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19
:
426 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21
:
427 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19
:
428 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21
:
429 case BFD_RELOC_AARCH64_TSTBR14
:
432 value
= value
+ addend
- place
;
435 case BFD_RELOC_AARCH64_CALL26
:
436 case BFD_RELOC_AARCH64_JUMP26
:
437 value
= value
+ addend
- place
;
440 case BFD_RELOC_AARCH64_16
:
441 case BFD_RELOC_AARCH64_32
:
442 case BFD_RELOC_AARCH64_MOVW_G0
:
443 case BFD_RELOC_AARCH64_MOVW_G0_NC
:
444 case BFD_RELOC_AARCH64_MOVW_G0_S
:
445 case BFD_RELOC_AARCH64_MOVW_G1
:
446 case BFD_RELOC_AARCH64_MOVW_G1_NC
:
447 case BFD_RELOC_AARCH64_MOVW_G1_S
:
448 case BFD_RELOC_AARCH64_MOVW_G2
:
449 case BFD_RELOC_AARCH64_MOVW_G2_NC
:
450 case BFD_RELOC_AARCH64_MOVW_G2_S
:
451 case BFD_RELOC_AARCH64_MOVW_G3
:
454 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC
:
455 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1
:
456 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC
:
457 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1
:
458 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12
:
459 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12
:
460 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC
:
461 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12
:
462 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12
:
463 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12
:
464 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12
:
465 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0
:
466 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC
:
467 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1
:
468 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC
:
469 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2
:
470 case BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12
:
471 case BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12
:
472 case BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12
:
473 case BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12
:
474 /* Weak Symbols and TLS relocations are implementation defined. For this
475 case we choose to emit 0. */
476 if (weak_undef_p
&& tls_reloc
)
478 _bfd_error_handler (_("%pB: warning: Weak TLS is implementation "
479 "defined and may not work as expected"),
483 value
= value
+ addend
;
486 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL
:
487 case BFD_RELOC_AARCH64_ADR_HI21_PCREL
:
490 value
= PG (value
+ addend
) - PG (place
);
493 case BFD_RELOC_AARCH64_GOT_LD_PREL19
:
494 value
= value
+ addend
- place
;
497 case BFD_RELOC_AARCH64_ADR_GOT_PAGE
:
498 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21
:
499 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21
:
500 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
:
501 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21
:
502 value
= PG (value
+ addend
) - PG (place
);
505 /* Caller must make sure addend is the base address of .got section. */
506 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14
:
507 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15
:
508 addend
= PG (addend
);
510 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15
:
511 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC
:
512 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1
:
513 value
= value
- addend
;
516 case BFD_RELOC_AARCH64_ADD_LO12
:
517 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC
:
518 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC
:
519 case BFD_RELOC_AARCH64_LDST128_LO12
:
520 case BFD_RELOC_AARCH64_LDST16_LO12
:
521 case BFD_RELOC_AARCH64_LDST32_LO12
:
522 case BFD_RELOC_AARCH64_LDST64_LO12
:
523 case BFD_RELOC_AARCH64_LDST8_LO12
:
524 case BFD_RELOC_AARCH64_TLSDESC_ADD
:
525 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12
:
526 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC
:
527 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12
:
528 case BFD_RELOC_AARCH64_TLSDESC_LDR
:
529 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC
:
530 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC
:
531 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
:
532 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC
:
533 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC
:
534 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC
:
535 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC
:
536 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC
:
537 case BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12_NC
:
538 case BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12_NC
:
539 case BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12_NC
:
540 case BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12_NC
:
541 value
= PG_OFFSET (value
+ addend
);
544 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12
:
545 value
= value
+ addend
;
548 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1
:
549 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1
:
550 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC
:
551 value
= (value
+ addend
) & (bfd_vma
) 0xffff0000;
553 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12
:
554 /* Mask off low 12bits, keep all other high bits, so that the later
555 generic code could check whehter there is overflow. */
556 value
= (value
+ addend
) & ~(bfd_vma
) 0xfff;
559 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC
:
560 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0
:
561 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
:
562 value
= (value
+ addend
) & (bfd_vma
) 0xffff;
565 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2
:
566 value
= (value
+ addend
) & ~(bfd_vma
) 0xffffffff;
567 value
-= place
& ~(bfd_vma
) 0xffffffff;
577 /* Support for core dump NOTE sections. */
580 _bfd_aarch64_elf_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
585 switch (note
->descsz
)
590 case 392: /* sizeof(struct elf_prstatus) on Linux/arm64. */
592 elf_tdata (abfd
)->core
->signal
593 = bfd_get_16 (abfd
, note
->descdata
+ 12);
596 elf_tdata (abfd
)->core
->lwpid
597 = bfd_get_32 (abfd
, note
->descdata
+ 32);
606 /* Make a ".reg/999" section. */
607 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
608 size
, note
->descpos
+ offset
);
612 _bfd_aarch64_elf_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
614 switch (note
->descsz
)
619 case 136: /* This is sizeof(struct elf_prpsinfo) on Linux/aarch64. */
620 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 24);
621 elf_tdata (abfd
)->core
->program
622 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 40, 16);
623 elf_tdata (abfd
)->core
->command
624 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 56, 80);
627 /* Note that for some reason, a spurious space is tacked
628 onto the end of the args in some (at least one anyway)
629 implementations, so strip it off if it exists. */
632 char *command
= elf_tdata (abfd
)->core
->command
;
633 int n
= strlen (command
);
635 if (0 < n
&& command
[n
- 1] == ' ')
636 command
[n
- 1] = '\0';
643 _bfd_aarch64_elf_write_core_note (bfd
*abfd
, char *buf
, int *bufsiz
, int note_type
,
653 char data
[136] ATTRIBUTE_NONSTRING
;
656 va_start (ap
, note_type
);
657 memset (data
, 0, sizeof (data
));
658 strncpy (data
+ 40, va_arg (ap
, const char *), 16);
659 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
661 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
662 -Wstringop-truncation:
663 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
665 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION
;
667 strncpy (data
+ 56, va_arg (ap
, const char *), 80);
668 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
673 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
674 note_type
, data
, sizeof (data
));
685 va_start (ap
, note_type
);
686 memset (data
, 0, sizeof (data
));
687 pid
= va_arg (ap
, long);
688 bfd_put_32 (abfd
, pid
, data
+ 32);
689 cursig
= va_arg (ap
, int);
690 bfd_put_16 (abfd
, cursig
, data
+ 12);
691 greg
= va_arg (ap
, const void *);
692 memcpy (data
+ 112, greg
, 272);
695 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
696 note_type
, data
, sizeof (data
));
701 /* Find the first input bfd with GNU properties.
702 If such an input is found, set found to true and return the relevant input.
703 Otherwise, return the last input of bfd inputs. */
705 _bfd_aarch64_elf_find_1st_bfd_input_with_gnu_property (
706 struct bfd_link_info
*info
,
707 bool *has_gnu_property
)
709 BFD_ASSERT (has_gnu_property
);
710 const struct elf_backend_data
*obfd
= get_elf_backend_data (info
->output_bfd
);
711 bfd
*pbfd
= info
->input_bfds
;
713 for (; pbfd
!= NULL
; pbfd
= pbfd
->link
.next
)
714 if (bfd_get_flavour (pbfd
) == bfd_target_elf_flavour
715 && bfd_count_sections (pbfd
) != 0
716 && (pbfd
->flags
& (DYNAMIC
| BFD_PLUGIN
| BFD_LINKER_CREATED
)) == 0
717 && (obfd
->elf_machine_code
718 == get_elf_backend_data (pbfd
)->elf_machine_code
)
719 && (obfd
->s
->elfclass
== get_elf_backend_data (pbfd
)->s
->elfclass
))
721 /* Does the input have a list of GNU properties ? */
722 if (elf_properties (pbfd
) != NULL
)
724 *has_gnu_property
= true;
732 /* Create a GNU property section for the given bfd input. */
734 _bfd_aarch64_elf_create_gnu_property_section (struct bfd_link_info
*info
,
738 sec
= bfd_make_section_with_flags (ebfd
,
739 NOTE_GNU_PROPERTY_SECTION_NAME
,
747 info
->callbacks
->einfo (
748 _("%F%P: failed to create GNU property section\n"));
750 unsigned align
= (bfd_get_mach (ebfd
) & bfd_mach_aarch64_ilp32
) ? 2 : 3;
751 if (!bfd_set_section_alignment (sec
, align
))
752 info
->callbacks
->einfo (_("%F%pA: failed to align section\n"),
755 elf_section_type (sec
) = SHT_NOTE
;
758 static const int GNU_PROPERTY_ISSUES_MAX
= 20;
760 /* Report a summary of the issues met during the merge of the GNU properties, if
761 the number of issues goes above GNU_PROPERTY_ISSUES_MAX. */
763 _bfd_aarch64_report_summary_merge_issues (struct bfd_link_info
*info
)
765 const struct elf_aarch64_obj_tdata
* tdata
766 = elf_aarch64_tdata (info
->output_bfd
);
768 if (tdata
->n_bti_issues
> GNU_PROPERTY_ISSUES_MAX
769 && tdata
->sw_protections
.bti_report
!= MARKING_NONE
)
772 = (tdata
->sw_protections
.bti_report
== MARKING_ERROR
)
773 ? _("%Xerror: found a total of %d inputs incompatible with "
774 "BTI requirements.\n")
775 : _("warning: found a total of %d inputs incompatible with "
776 "BTI requirements.\n");
777 info
->callbacks
->einfo (msg
, tdata
->n_bti_issues
);
780 if (tdata
->n_gcs_issues
> GNU_PROPERTY_ISSUES_MAX
781 && tdata
->sw_protections
.gcs_report
!= MARKING_NONE
)
784 = (tdata
->sw_protections
.gcs_report
== MARKING_ERROR
)
785 ? _("%Xerror: found a total of %d inputs incompatible with "
786 "GCS requirements.\n")
787 : _("warning: found a total of %d inputs incompatible with "
788 "GCS requirements.\n");
789 info
->callbacks
->einfo (msg
, tdata
->n_gcs_issues
);
792 if (tdata
->n_gcs_dynamic_issues
> GNU_PROPERTY_ISSUES_MAX
793 && tdata
->sw_protections
.gcs_report_dynamic
!= MARKING_NONE
)
796 = (tdata
->sw_protections
.gcs_report_dynamic
== MARKING_ERROR
)
797 ? _("%Xerror: found a total of %d dynamically-linked objects "
798 "incompatible with GCS requirements.\n")
799 : _("warning: found a total of %d dynamically-linked objects "
800 "incompatible with GCS requirements.\n");
801 info
->callbacks
->einfo (msg
, tdata
->n_gcs_dynamic_issues
);
805 /* Perform a look-up of a property in an unsorted list of properties. This is
806 useful when the list of properties of an object has not been sorted yet. */
808 _bfd_aarch64_prop_linear_lookup (elf_property_list
*properties
,
809 unsigned int pr_type
)
811 for (elf_property_list
*p
= properties
; p
!= NULL
; p
= p
->next
)
812 if (p
->property
.pr_type
== pr_type
)
813 return p
->property
.u
.number
;
817 /* Compare the GNU properties of the current dynamic object, with the ones of
818 the output BFD. Today, we only care about GCS feature stored in
819 GNU_PROPERTY_AARCH64_FEATURE_1. */
821 _bfd_aarch64_compare_dynamic_obj_prop_against_outprop(
822 struct bfd_link_info
*info
,
823 const uint32_t outprop
,
826 if (!(outprop
& GNU_PROPERTY_AARCH64_FEATURE_1_GCS
))
829 const uint32_t dyn_obj_aarch64_feature_prop
=
830 _bfd_aarch64_prop_linear_lookup (elf_properties (pbfd
),
831 GNU_PROPERTY_AARCH64_FEATURE_1_AND
);
832 if (!(dyn_obj_aarch64_feature_prop
& GNU_PROPERTY_AARCH64_FEATURE_1_GCS
))
833 _bfd_aarch64_elf_check_gcs_report (info
, pbfd
);
836 /* Check compatibility between the GNU properties of the ouput BFD and the
837 linked dynamic objects. */
839 _bfd_aarch64_elf_check_gnu_properties_linked_dynamic_objects (
840 struct bfd_link_info
* info
,
841 const uint32_t outprop
)
843 const struct elf_backend_data
*bed
= get_elf_backend_data (info
->output_bfd
);
844 const int elf_machine_code
= bed
->elf_machine_code
;
845 const unsigned int elfclass
= bed
->s
->elfclass
;
847 for (bfd
*pbfd
= info
->input_bfds
; pbfd
!= NULL
; pbfd
= pbfd
->link
.next
)
848 /* Ignore GNU properties from non-ELF objects or ELF objects with different
850 if ((pbfd
->flags
& DYNAMIC
) != 0
851 && (bfd_get_flavour (pbfd
) == bfd_target_elf_flavour
)
852 && (get_elf_backend_data (pbfd
)->elf_machine_code
== elf_machine_code
)
853 && (get_elf_backend_data (pbfd
)->s
->elfclass
== elfclass
))
854 _bfd_aarch64_compare_dynamic_obj_prop_against_outprop(info
, outprop
,
858 /* Find the first input bfd with GNU property and merge it with GPROP. If no
859 such input is found, add it to a new section at the last input. Update
860 GPROP accordingly. */
862 _bfd_aarch64_elf_link_setup_gnu_properties (struct bfd_link_info
*info
)
864 struct elf_aarch64_obj_tdata
*tdata
= elf_aarch64_tdata (info
->output_bfd
);
865 uint32_t outprop
= tdata
->gnu_property_aarch64_feature_1_and
;
867 bool has_gnu_property
= false;
869 _bfd_aarch64_elf_find_1st_bfd_input_with_gnu_property (info
,
872 /* If ebfd != NULL it is either an input with property note or the last input.
873 Either way if we have an output GNU property that was provided, we should
874 add it (by creating a section if needed). */
877 /* If no GNU property node was found, create the GNU property note
879 if (!has_gnu_property
)
880 _bfd_aarch64_elf_create_gnu_property_section (info
, ebfd
);
882 /* Merge the found input property with output properties. Note: if no
883 property was found, _bfd_elf_get_property will create one. */
885 _bfd_elf_get_property (ebfd
,
886 GNU_PROPERTY_AARCH64_FEATURE_1_AND
,
889 /* Check for a feature mismatch and report issue (if any) before this
890 information get lost as the value of ebfd will be overriden with
892 if ((outprop
& GNU_PROPERTY_AARCH64_FEATURE_1_BTI
)
893 && !(prop
->u
.number
& GNU_PROPERTY_AARCH64_FEATURE_1_BTI
))
894 _bfd_aarch64_elf_check_bti_report (info
, ebfd
);
896 if (tdata
->sw_protections
.gcs_type
== GCS_NEVER
)
897 prop
->u
.number
&= ~GNU_PROPERTY_AARCH64_FEATURE_1_GCS
;
898 else if ((outprop
& GNU_PROPERTY_AARCH64_FEATURE_1_GCS
)
899 && !(prop
->u
.number
& GNU_PROPERTY_AARCH64_FEATURE_1_GCS
))
900 _bfd_aarch64_elf_check_gcs_report (info
, ebfd
);
902 prop
->u
.number
|= outprop
;
903 if (prop
->u
.number
== 0)
904 prop
->pr_kind
= property_remove
;
906 prop
->pr_kind
= property_number
;
909 /* Set up generic GNU properties, and merge them with the backend-specific
910 ones (if any). pbfd points to the first relocatable ELF input with
911 GNU properties (if found). */
912 bfd
*pbfd
= _bfd_elf_link_setup_gnu_properties (info
);
914 /* If pbfd has any GNU_PROPERTY_AARCH64_FEATURE_1_AND properties, update
915 outprop accordingly. */
918 /* The property list is sorted in order of type. */
919 for (elf_property_list
*p
= elf_properties (pbfd
);
921 && (GNU_PROPERTY_AARCH64_FEATURE_1_AND
<= p
->property
.pr_type
);
924 /* This merge of features should happen only once as all the identical
925 properties are supposed to have been merged at this stage by
926 _bfd_elf_link_setup_gnu_properties(). */
927 if (p
->property
.pr_type
== GNU_PROPERTY_AARCH64_FEATURE_1_AND
)
929 outprop
= (p
->property
.u
.number
930 & (GNU_PROPERTY_AARCH64_FEATURE_1_BTI
931 | GNU_PROPERTY_AARCH64_FEATURE_1_PAC
932 | GNU_PROPERTY_AARCH64_FEATURE_1_GCS
));
938 tdata
->gnu_property_aarch64_feature_1_and
= outprop
;
940 _bfd_aarch64_elf_check_gnu_properties_linked_dynamic_objects (info
, outprop
);
942 _bfd_aarch64_report_summary_merge_issues (info
);
947 /* Define elf_backend_parse_gnu_properties for AArch64. */
948 enum elf_property_kind
949 _bfd_aarch64_elf_parse_gnu_properties (bfd
*abfd
, unsigned int type
,
950 bfd_byte
*ptr
, unsigned int datasz
)
956 case GNU_PROPERTY_AARCH64_FEATURE_1_AND
:
960 ( _("error: %pB: <corrupt AArch64 used size: 0x%x>"),
962 return property_corrupt
;
964 prop
= _bfd_elf_get_property (abfd
, type
, datasz
);
965 /* Merge AArch64 feature properties together if they are declared in
966 different AARCH64_FEATURE_1_AND properties. */
967 prop
->u
.number
|= bfd_h_get_32 (abfd
, ptr
);
968 prop
->pr_kind
= property_number
;
972 return property_ignored
;
975 return property_number
;
978 /* Merge AArch64 GNU property BPROP with APROP also accounting for OUTPROP.
979 If APROP isn't NULL, merge it with BPROP and/or OUTPROP. Vice-versa if BROP
980 isn't NULL. Return TRUE if there is any update to APROP or if BPROP should
981 be merge with ABFD. */
983 _bfd_aarch64_elf_merge_gnu_properties (struct bfd_link_info
*info
985 bfd
*abfd ATTRIBUTE_UNUSED
,
990 unsigned int orig_number
;
991 bool updated
= false;
992 unsigned int pr_type
= aprop
!= NULL
? aprop
->pr_type
: bprop
->pr_type
;
996 case GNU_PROPERTY_AARCH64_FEATURE_1_AND
:
998 aarch64_gcs_type gcs_type
999 = elf_aarch64_tdata (info
->output_bfd
)->sw_protections
.gcs_type
;
1000 /* OUTPROP does not contain GCS for GCS_NEVER. We only need to make sure
1001 that APROP does not contain GCS as well.
1003 - if BPROP contains GCS and APROP is not null, it is zeroed by the
1005 - if BPROP contains GCS and APROP is null, it is overwritten with
1006 OUTPROP as the AND with APROP would have been equivalent to zeroing
1008 if (gcs_type
== GCS_NEVER
&& aprop
!= NULL
)
1009 aprop
->u
.number
&= ~GNU_PROPERTY_AARCH64_FEATURE_1_GCS
;
1011 if (aprop
!= NULL
&& bprop
!= NULL
)
1013 orig_number
= aprop
->u
.number
;
1014 aprop
->u
.number
= (orig_number
& bprop
->u
.number
) | outprop
;
1015 updated
= orig_number
!= aprop
->u
.number
;
1016 /* Remove the property if all feature bits are cleared. */
1017 if (aprop
->u
.number
== 0)
1018 aprop
->pr_kind
= property_remove
;
1021 /* If either is NULL, the AND would be 0 so, if there is
1022 any OUTPROP, assign it to the input that is not NULL. */
1027 orig_number
= aprop
->u
.number
;
1028 aprop
->u
.number
= outprop
;
1029 updated
= orig_number
!= aprop
->u
.number
;
1033 bprop
->u
.number
= outprop
;
1037 /* No OUTPROP and BPROP is NULL, so remove APROP. */
1038 else if (aprop
!= NULL
)
1040 aprop
->pr_kind
= property_remove
;
1053 /* Fix up AArch64 GNU properties. */
1055 _bfd_aarch64_elf_link_fixup_gnu_properties
1056 (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1057 elf_property_list
**listp
)
1059 elf_property_list
*p
, *prev
;
1061 for (p
= *listp
, prev
= *listp
; p
; p
= p
->next
)
1063 unsigned int type
= p
->property
.pr_type
;
1064 if (type
== GNU_PROPERTY_AARCH64_FEATURE_1_AND
)
1066 if (p
->property
.pr_kind
== property_remove
)
1068 /* Remove empty property. */
1075 prev
->next
= p
->next
;
1080 else if (type
> GNU_PROPERTY_HIPROC
)
1082 /* The property list is sorted in order of type. */
1088 /* Check AArch64 BTI report. */
1090 _bfd_aarch64_elf_check_bti_report (struct bfd_link_info
*info
, bfd
*ebfd
)
1092 struct elf_aarch64_obj_tdata
*tdata
= elf_aarch64_tdata (info
->output_bfd
);
1094 if (tdata
->sw_protections
.bti_report
== MARKING_NONE
)
1097 ++tdata
->n_bti_issues
;
1099 if (tdata
->n_bti_issues
> GNU_PROPERTY_ISSUES_MAX
)
1103 = (tdata
->sw_protections
.bti_report
== MARKING_WARN
)
1104 ? _("%pB: warning: BTI is required by -z force-bti, but this input object "
1105 "file lacks the necessary property note.\n")
1106 : _("%X%pB: error: BTI is required by -z force-bti, but this input object "
1107 "file lacks the necessary property note.\n");
1109 info
->callbacks
->einfo (msg
, ebfd
);
1113 _bfd_aarch64_elf_check_gcs_report (struct bfd_link_info
*info
, bfd
*ebfd
)
1115 struct elf_aarch64_obj_tdata
*tdata
= elf_aarch64_tdata (info
->output_bfd
);
1116 bool dynamic_obj
= (ebfd
->flags
& DYNAMIC
) != 0;
1120 if (tdata
->sw_protections
.gcs_report_dynamic
== MARKING_NONE
)
1122 ++tdata
->n_gcs_dynamic_issues
;
1123 if (tdata
->n_gcs_dynamic_issues
> GNU_PROPERTY_ISSUES_MAX
)
1128 if (tdata
->sw_protections
.gcs_report
== MARKING_NONE
)
1130 ++tdata
->n_gcs_issues
;
1131 if (tdata
->n_gcs_issues
> GNU_PROPERTY_ISSUES_MAX
)
1137 msg
= (tdata
->sw_protections
.gcs_report_dynamic
== MARKING_WARN
)
1138 ? _("%pB: warning: GCS is required by -z gcs, but this shared library "
1139 "lacks the necessary property note. The dynamic loader might not "
1140 "enable GCS or refuse to load the program unless all the shared "
1141 "library dependencies have the GCS marking.\n")
1142 : _("%X%pB: error: GCS is required by -z gcs, but this shared library "
1143 "lacks the necessary property note. The dynamic loader might not "
1144 "enable GCS or refuse to load the program unless all the shared "
1145 "library dependencies have the GCS marking.\n");
1147 msg
= (tdata
->sw_protections
.gcs_report
== MARKING_WARN
)
1148 ? _("%pB: warning: GCS is required by -z gcs, but this input object file "
1149 "lacks the necessary property note.\n")
1150 : _("%X%pB: error: GCS is required by -z gcs, but this input object file "
1151 "lacks the necessary property note.\n");
1153 info
->callbacks
->einfo (msg
, ebfd
);