Update my e-mail address.
[binutils-gdb.git] / bfd / elfnn-aarch64.c
blob23c881c51d84aeb26b4ec22502d69734bd43d649
1 /* AArch64-specific support for NN-bit ELF.
2 Copyright (C) 2009-2017 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/>. */
21 /* Notes on implementation:
23 Thread Local Store (TLS)
25 Overview:
27 The implementation currently supports both traditional TLS and TLS
28 descriptors, but only general dynamic (GD).
30 For traditional TLS the assembler will present us with code
31 fragments of the form:
33 adrp x0, :tlsgd:foo
34 R_AARCH64_TLSGD_ADR_PAGE21(foo)
35 add x0, :tlsgd_lo12:foo
36 R_AARCH64_TLSGD_ADD_LO12_NC(foo)
37 bl __tls_get_addr
38 nop
40 For TLS descriptors the assembler will present us with code
41 fragments of the form:
43 adrp x0, :tlsdesc:foo R_AARCH64_TLSDESC_ADR_PAGE21(foo)
44 ldr x1, [x0, #:tlsdesc_lo12:foo] R_AARCH64_TLSDESC_LD64_LO12(foo)
45 add x0, x0, #:tlsdesc_lo12:foo R_AARCH64_TLSDESC_ADD_LO12(foo)
46 .tlsdesccall foo
47 blr x1 R_AARCH64_TLSDESC_CALL(foo)
49 The relocations R_AARCH64_TLSGD_{ADR_PREL21,ADD_LO12_NC} against foo
50 indicate that foo is thread local and should be accessed via the
51 traditional TLS mechanims.
53 The relocations R_AARCH64_TLSDESC_{ADR_PAGE21,LD64_LO12_NC,ADD_LO12_NC}
54 against foo indicate that 'foo' is thread local and should be accessed
55 via a TLS descriptor mechanism.
57 The precise instruction sequence is only relevant from the
58 perspective of linker relaxation which is currently not implemented.
60 The static linker must detect that 'foo' is a TLS object and
61 allocate a double GOT entry. The GOT entry must be created for both
62 global and local TLS symbols. Note that this is different to none
63 TLS local objects which do not need a GOT entry.
65 In the traditional TLS mechanism, the double GOT entry is used to
66 provide the tls_index structure, containing module and offset
67 entries. The static linker places the relocation R_AARCH64_TLS_DTPMOD
68 on the module entry. The loader will subsequently fixup this
69 relocation with the module identity.
71 For global traditional TLS symbols the static linker places an
72 R_AARCH64_TLS_DTPREL relocation on the offset entry. The loader
73 will subsequently fixup the offset. For local TLS symbols the static
74 linker fixes up offset.
76 In the TLS descriptor mechanism the double GOT entry is used to
77 provide the descriptor. The static linker places the relocation
78 R_AARCH64_TLSDESC on the first GOT slot. The loader will
79 subsequently fix this up.
81 Implementation:
83 The handling of TLS symbols is implemented across a number of
84 different backend functions. The following is a top level view of
85 what processing is performed where.
87 The TLS implementation maintains state information for each TLS
88 symbol. The state information for local and global symbols is kept
89 in different places. Global symbols use generic BFD structures while
90 local symbols use backend specific structures that are allocated and
91 maintained entirely by the backend.
93 The flow:
95 elfNN_aarch64_check_relocs()
97 This function is invoked for each relocation.
99 The TLS relocations R_AARCH64_TLSGD_{ADR_PREL21,ADD_LO12_NC} and
100 R_AARCH64_TLSDESC_{ADR_PAGE21,LD64_LO12_NC,ADD_LO12_NC} are
101 spotted. One time creation of local symbol data structures are
102 created when the first local symbol is seen.
104 The reference count for a symbol is incremented. The GOT type for
105 each symbol is marked as general dynamic.
107 elfNN_aarch64_allocate_dynrelocs ()
109 For each global with positive reference count we allocate a double
110 GOT slot. For a traditional TLS symbol we allocate space for two
111 relocation entries on the GOT, for a TLS descriptor symbol we
112 allocate space for one relocation on the slot. Record the GOT offset
113 for this symbol.
115 elfNN_aarch64_size_dynamic_sections ()
117 Iterate all input BFDS, look for in the local symbol data structure
118 constructed earlier for local TLS symbols and allocate them double
119 GOT slots along with space for a single GOT relocation. Update the
120 local symbol structure to record the GOT offset allocated.
122 elfNN_aarch64_relocate_section ()
124 Calls elfNN_aarch64_final_link_relocate ()
126 Emit the relevant TLS relocations against the GOT for each TLS
127 symbol. For local TLS symbols emit the GOT offset directly. The GOT
128 relocations are emitted once the first time a TLS symbol is
129 encountered. The implementation uses the LSB of the GOT offset to
130 flag that the relevant GOT relocations for a symbol have been
131 emitted. All of the TLS code that uses the GOT offset needs to take
132 care to mask out this flag bit before using the offset.
134 elfNN_aarch64_final_link_relocate ()
136 Fixup the R_AARCH64_TLSGD_{ADR_PREL21, ADD_LO12_NC} relocations. */
138 #include "sysdep.h"
139 #include "bfd.h"
140 #include "libiberty.h"
141 #include "libbfd.h"
142 #include "bfd_stdint.h"
143 #include "elf-bfd.h"
144 #include "bfdlink.h"
145 #include "objalloc.h"
146 #include "elf/aarch64.h"
147 #include "elfxx-aarch64.h"
149 #define ARCH_SIZE NN
151 #if ARCH_SIZE == 64
152 #define AARCH64_R(NAME) R_AARCH64_ ## NAME
153 #define AARCH64_R_STR(NAME) "R_AARCH64_" #NAME
154 #define HOWTO64(...) HOWTO (__VA_ARGS__)
155 #define HOWTO32(...) EMPTY_HOWTO (0)
156 #define LOG_FILE_ALIGN 3
157 #define BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC BFD_RELOC_AARCH64_TLSDESC_LD64_LO12
158 #endif
160 #if ARCH_SIZE == 32
161 #define AARCH64_R(NAME) R_AARCH64_P32_ ## NAME
162 #define AARCH64_R_STR(NAME) "R_AARCH64_P32_" #NAME
163 #define HOWTO64(...) EMPTY_HOWTO (0)
164 #define HOWTO32(...) HOWTO (__VA_ARGS__)
165 #define LOG_FILE_ALIGN 2
166 #define BFD_RELOC_AARCH64_TLSDESC_LD32_LO12 BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC
167 #define R_AARCH64_P32_TLSDESC_ADD_LO12 R_AARCH64_P32_TLSDESC_ADD_LO12_NC
168 #endif
170 #define IS_AARCH64_TLS_RELOC(R_TYPE) \
171 ((R_TYPE) == BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC \
172 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21 \
173 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_ADR_PREL21 \
174 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC \
175 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_MOVW_G1 \
176 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 \
177 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC \
178 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC \
179 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19 \
180 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC \
181 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1 \
182 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12 \
183 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12 \
184 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC \
185 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC \
186 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21 \
187 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_ADR_PREL21 \
188 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12 \
189 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC \
190 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12 \
191 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC \
192 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12 \
193 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC \
194 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12 \
195 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC \
196 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0 \
197 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC \
198 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1 \
199 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC \
200 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2 \
201 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12 \
202 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12 \
203 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC \
204 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0 \
205 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC \
206 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1 \
207 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC \
208 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2 \
209 || (R_TYPE) == BFD_RELOC_AARCH64_TLS_DTPMOD \
210 || (R_TYPE) == BFD_RELOC_AARCH64_TLS_DTPREL \
211 || (R_TYPE) == BFD_RELOC_AARCH64_TLS_TPREL \
212 || IS_AARCH64_TLSDESC_RELOC ((R_TYPE)))
214 #define IS_AARCH64_TLS_RELAX_RELOC(R_TYPE) \
215 ((R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADD \
216 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADD_LO12 \
217 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21 \
218 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21 \
219 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_CALL \
220 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LD_PREL19 \
221 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC \
222 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LDR \
223 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC \
224 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_OFF_G1 \
225 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LDR \
226 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21 \
227 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_ADR_PREL21 \
228 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC \
229 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC \
230 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_MOVW_G1 \
231 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 \
232 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19 \
233 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC \
234 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC \
235 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21 \
236 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_ADR_PREL21)
238 #define IS_AARCH64_TLSDESC_RELOC(R_TYPE) \
239 ((R_TYPE) == BFD_RELOC_AARCH64_TLSDESC \
240 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADD \
241 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADD_LO12 \
242 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21 \
243 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21 \
244 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_CALL \
245 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC \
246 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LD64_LO12 \
247 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LDR \
248 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LD_PREL19 \
249 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC \
250 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_OFF_G1)
252 #define ELIMINATE_COPY_RELOCS 1
254 /* Return size of a relocation entry. HTAB is the bfd's
255 elf_aarch64_link_hash_entry. */
256 #define RELOC_SIZE(HTAB) (sizeof (ElfNN_External_Rela))
258 /* GOT Entry size - 8 bytes in ELF64 and 4 bytes in ELF32. */
259 #define GOT_ENTRY_SIZE (ARCH_SIZE / 8)
260 #define PLT_ENTRY_SIZE (32)
261 #define PLT_SMALL_ENTRY_SIZE (16)
262 #define PLT_TLSDESC_ENTRY_SIZE (32)
264 /* Encoding of the nop instruction. */
265 #define INSN_NOP 0xd503201f
267 #define aarch64_compute_jump_table_size(htab) \
268 (((htab)->root.srelplt == NULL) ? 0 \
269 : (htab)->root.srelplt->reloc_count * GOT_ENTRY_SIZE)
271 /* The first entry in a procedure linkage table looks like this
272 if the distance between the PLTGOT and the PLT is < 4GB use
273 these PLT entries. Note that the dynamic linker gets &PLTGOT[2]
274 in x16 and needs to work out PLTGOT[1] by using an address of
275 [x16,#-GOT_ENTRY_SIZE]. */
276 static const bfd_byte elfNN_aarch64_small_plt0_entry[PLT_ENTRY_SIZE] =
278 0xf0, 0x7b, 0xbf, 0xa9, /* stp x16, x30, [sp, #-16]! */
279 0x10, 0x00, 0x00, 0x90, /* adrp x16, (GOT+16) */
280 #if ARCH_SIZE == 64
281 0x11, 0x0A, 0x40, 0xf9, /* ldr x17, [x16, #PLT_GOT+0x10] */
282 0x10, 0x42, 0x00, 0x91, /* add x16, x16,#PLT_GOT+0x10 */
283 #else
284 0x11, 0x0A, 0x40, 0xb9, /* ldr w17, [x16, #PLT_GOT+0x8] */
285 0x10, 0x22, 0x00, 0x11, /* add w16, w16,#PLT_GOT+0x8 */
286 #endif
287 0x20, 0x02, 0x1f, 0xd6, /* br x17 */
288 0x1f, 0x20, 0x03, 0xd5, /* nop */
289 0x1f, 0x20, 0x03, 0xd5, /* nop */
290 0x1f, 0x20, 0x03, 0xd5, /* nop */
293 /* Per function entry in a procedure linkage table looks like this
294 if the distance between the PLTGOT and the PLT is < 4GB use
295 these PLT entries. */
296 static const bfd_byte elfNN_aarch64_small_plt_entry[PLT_SMALL_ENTRY_SIZE] =
298 0x10, 0x00, 0x00, 0x90, /* adrp x16, PLTGOT + n * 8 */
299 #if ARCH_SIZE == 64
300 0x11, 0x02, 0x40, 0xf9, /* ldr x17, [x16, PLTGOT + n * 8] */
301 0x10, 0x02, 0x00, 0x91, /* add x16, x16, :lo12:PLTGOT + n * 8 */
302 #else
303 0x11, 0x02, 0x40, 0xb9, /* ldr w17, [x16, PLTGOT + n * 4] */
304 0x10, 0x02, 0x00, 0x11, /* add w16, w16, :lo12:PLTGOT + n * 4 */
305 #endif
306 0x20, 0x02, 0x1f, 0xd6, /* br x17. */
309 static const bfd_byte
310 elfNN_aarch64_tlsdesc_small_plt_entry[PLT_TLSDESC_ENTRY_SIZE] =
312 0xe2, 0x0f, 0xbf, 0xa9, /* stp x2, x3, [sp, #-16]! */
313 0x02, 0x00, 0x00, 0x90, /* adrp x2, 0 */
314 0x03, 0x00, 0x00, 0x90, /* adrp x3, 0 */
315 #if ARCH_SIZE == 64
316 0x42, 0x00, 0x40, 0xf9, /* ldr x2, [x2, #0] */
317 0x63, 0x00, 0x00, 0x91, /* add x3, x3, 0 */
318 #else
319 0x42, 0x00, 0x40, 0xb9, /* ldr w2, [x2, #0] */
320 0x63, 0x00, 0x00, 0x11, /* add w3, w3, 0 */
321 #endif
322 0x40, 0x00, 0x1f, 0xd6, /* br x2 */
323 0x1f, 0x20, 0x03, 0xd5, /* nop */
324 0x1f, 0x20, 0x03, 0xd5, /* nop */
327 #define elf_info_to_howto elfNN_aarch64_info_to_howto
328 #define elf_info_to_howto_rel elfNN_aarch64_info_to_howto
330 #define AARCH64_ELF_ABI_VERSION 0
332 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
333 #define ALL_ONES (~ (bfd_vma) 0)
335 /* Indexed by the bfd interal reloc enumerators.
336 Therefore, the table needs to be synced with BFD_RELOC_AARCH64_*
337 in reloc.c. */
339 static reloc_howto_type elfNN_aarch64_howto_table[] =
341 EMPTY_HOWTO (0),
343 /* Basic data relocations. */
345 /* Deprecated, but retained for backwards compatibility. */
346 HOWTO64 (R_AARCH64_NULL, /* type */
347 0, /* rightshift */
348 3, /* size (0 = byte, 1 = short, 2 = long) */
349 0, /* bitsize */
350 FALSE, /* pc_relative */
351 0, /* bitpos */
352 complain_overflow_dont, /* complain_on_overflow */
353 bfd_elf_generic_reloc, /* special_function */
354 "R_AARCH64_NULL", /* name */
355 FALSE, /* partial_inplace */
356 0, /* src_mask */
357 0, /* dst_mask */
358 FALSE), /* pcrel_offset */
359 HOWTO (R_AARCH64_NONE, /* type */
360 0, /* rightshift */
361 3, /* size (0 = byte, 1 = short, 2 = long) */
362 0, /* bitsize */
363 FALSE, /* pc_relative */
364 0, /* bitpos */
365 complain_overflow_dont, /* complain_on_overflow */
366 bfd_elf_generic_reloc, /* special_function */
367 "R_AARCH64_NONE", /* name */
368 FALSE, /* partial_inplace */
369 0, /* src_mask */
370 0, /* dst_mask */
371 FALSE), /* pcrel_offset */
373 /* .xword: (S+A) */
374 HOWTO64 (AARCH64_R (ABS64), /* type */
375 0, /* rightshift */
376 4, /* size (4 = long long) */
377 64, /* bitsize */
378 FALSE, /* pc_relative */
379 0, /* bitpos */
380 complain_overflow_unsigned, /* complain_on_overflow */
381 bfd_elf_generic_reloc, /* special_function */
382 AARCH64_R_STR (ABS64), /* name */
383 FALSE, /* partial_inplace */
384 ALL_ONES, /* src_mask */
385 ALL_ONES, /* dst_mask */
386 FALSE), /* pcrel_offset */
388 /* .word: (S+A) */
389 HOWTO (AARCH64_R (ABS32), /* type */
390 0, /* rightshift */
391 2, /* size (0 = byte, 1 = short, 2 = long) */
392 32, /* bitsize */
393 FALSE, /* pc_relative */
394 0, /* bitpos */
395 complain_overflow_unsigned, /* complain_on_overflow */
396 bfd_elf_generic_reloc, /* special_function */
397 AARCH64_R_STR (ABS32), /* name */
398 FALSE, /* partial_inplace */
399 0xffffffff, /* src_mask */
400 0xffffffff, /* dst_mask */
401 FALSE), /* pcrel_offset */
403 /* .half: (S+A) */
404 HOWTO (AARCH64_R (ABS16), /* type */
405 0, /* rightshift */
406 1, /* size (0 = byte, 1 = short, 2 = long) */
407 16, /* bitsize */
408 FALSE, /* pc_relative */
409 0, /* bitpos */
410 complain_overflow_unsigned, /* complain_on_overflow */
411 bfd_elf_generic_reloc, /* special_function */
412 AARCH64_R_STR (ABS16), /* name */
413 FALSE, /* partial_inplace */
414 0xffff, /* src_mask */
415 0xffff, /* dst_mask */
416 FALSE), /* pcrel_offset */
418 /* .xword: (S+A-P) */
419 HOWTO64 (AARCH64_R (PREL64), /* type */
420 0, /* rightshift */
421 4, /* size (4 = long long) */
422 64, /* bitsize */
423 TRUE, /* pc_relative */
424 0, /* bitpos */
425 complain_overflow_signed, /* complain_on_overflow */
426 bfd_elf_generic_reloc, /* special_function */
427 AARCH64_R_STR (PREL64), /* name */
428 FALSE, /* partial_inplace */
429 ALL_ONES, /* src_mask */
430 ALL_ONES, /* dst_mask */
431 TRUE), /* pcrel_offset */
433 /* .word: (S+A-P) */
434 HOWTO (AARCH64_R (PREL32), /* type */
435 0, /* rightshift */
436 2, /* size (0 = byte, 1 = short, 2 = long) */
437 32, /* bitsize */
438 TRUE, /* pc_relative */
439 0, /* bitpos */
440 complain_overflow_signed, /* complain_on_overflow */
441 bfd_elf_generic_reloc, /* special_function */
442 AARCH64_R_STR (PREL32), /* name */
443 FALSE, /* partial_inplace */
444 0xffffffff, /* src_mask */
445 0xffffffff, /* dst_mask */
446 TRUE), /* pcrel_offset */
448 /* .half: (S+A-P) */
449 HOWTO (AARCH64_R (PREL16), /* type */
450 0, /* rightshift */
451 1, /* size (0 = byte, 1 = short, 2 = long) */
452 16, /* bitsize */
453 TRUE, /* pc_relative */
454 0, /* bitpos */
455 complain_overflow_signed, /* complain_on_overflow */
456 bfd_elf_generic_reloc, /* special_function */
457 AARCH64_R_STR (PREL16), /* name */
458 FALSE, /* partial_inplace */
459 0xffff, /* src_mask */
460 0xffff, /* dst_mask */
461 TRUE), /* pcrel_offset */
463 /* Group relocations to create a 16, 32, 48 or 64 bit
464 unsigned data or abs address inline. */
466 /* MOVZ: ((S+A) >> 0) & 0xffff */
467 HOWTO (AARCH64_R (MOVW_UABS_G0), /* type */
468 0, /* rightshift */
469 2, /* size (0 = byte, 1 = short, 2 = long) */
470 16, /* bitsize */
471 FALSE, /* pc_relative */
472 0, /* bitpos */
473 complain_overflow_unsigned, /* complain_on_overflow */
474 bfd_elf_generic_reloc, /* special_function */
475 AARCH64_R_STR (MOVW_UABS_G0), /* name */
476 FALSE, /* partial_inplace */
477 0xffff, /* src_mask */
478 0xffff, /* dst_mask */
479 FALSE), /* pcrel_offset */
481 /* MOVK: ((S+A) >> 0) & 0xffff [no overflow check] */
482 HOWTO (AARCH64_R (MOVW_UABS_G0_NC), /* type */
483 0, /* rightshift */
484 2, /* size (0 = byte, 1 = short, 2 = long) */
485 16, /* bitsize */
486 FALSE, /* pc_relative */
487 0, /* bitpos */
488 complain_overflow_dont, /* complain_on_overflow */
489 bfd_elf_generic_reloc, /* special_function */
490 AARCH64_R_STR (MOVW_UABS_G0_NC), /* name */
491 FALSE, /* partial_inplace */
492 0xffff, /* src_mask */
493 0xffff, /* dst_mask */
494 FALSE), /* pcrel_offset */
496 /* MOVZ: ((S+A) >> 16) & 0xffff */
497 HOWTO (AARCH64_R (MOVW_UABS_G1), /* type */
498 16, /* rightshift */
499 2, /* size (0 = byte, 1 = short, 2 = long) */
500 16, /* bitsize */
501 FALSE, /* pc_relative */
502 0, /* bitpos */
503 complain_overflow_unsigned, /* complain_on_overflow */
504 bfd_elf_generic_reloc, /* special_function */
505 AARCH64_R_STR (MOVW_UABS_G1), /* name */
506 FALSE, /* partial_inplace */
507 0xffff, /* src_mask */
508 0xffff, /* dst_mask */
509 FALSE), /* pcrel_offset */
511 /* MOVK: ((S+A) >> 16) & 0xffff [no overflow check] */
512 HOWTO64 (AARCH64_R (MOVW_UABS_G1_NC), /* type */
513 16, /* rightshift */
514 2, /* size (0 = byte, 1 = short, 2 = long) */
515 16, /* bitsize */
516 FALSE, /* pc_relative */
517 0, /* bitpos */
518 complain_overflow_dont, /* complain_on_overflow */
519 bfd_elf_generic_reloc, /* special_function */
520 AARCH64_R_STR (MOVW_UABS_G1_NC), /* name */
521 FALSE, /* partial_inplace */
522 0xffff, /* src_mask */
523 0xffff, /* dst_mask */
524 FALSE), /* pcrel_offset */
526 /* MOVZ: ((S+A) >> 32) & 0xffff */
527 HOWTO64 (AARCH64_R (MOVW_UABS_G2), /* type */
528 32, /* rightshift */
529 2, /* size (0 = byte, 1 = short, 2 = long) */
530 16, /* bitsize */
531 FALSE, /* pc_relative */
532 0, /* bitpos */
533 complain_overflow_unsigned, /* complain_on_overflow */
534 bfd_elf_generic_reloc, /* special_function */
535 AARCH64_R_STR (MOVW_UABS_G2), /* name */
536 FALSE, /* partial_inplace */
537 0xffff, /* src_mask */
538 0xffff, /* dst_mask */
539 FALSE), /* pcrel_offset */
541 /* MOVK: ((S+A) >> 32) & 0xffff [no overflow check] */
542 HOWTO64 (AARCH64_R (MOVW_UABS_G2_NC), /* type */
543 32, /* rightshift */
544 2, /* size (0 = byte, 1 = short, 2 = long) */
545 16, /* bitsize */
546 FALSE, /* pc_relative */
547 0, /* bitpos */
548 complain_overflow_dont, /* complain_on_overflow */
549 bfd_elf_generic_reloc, /* special_function */
550 AARCH64_R_STR (MOVW_UABS_G2_NC), /* name */
551 FALSE, /* partial_inplace */
552 0xffff, /* src_mask */
553 0xffff, /* dst_mask */
554 FALSE), /* pcrel_offset */
556 /* MOVZ: ((S+A) >> 48) & 0xffff */
557 HOWTO64 (AARCH64_R (MOVW_UABS_G3), /* type */
558 48, /* rightshift */
559 2, /* size (0 = byte, 1 = short, 2 = long) */
560 16, /* bitsize */
561 FALSE, /* pc_relative */
562 0, /* bitpos */
563 complain_overflow_unsigned, /* complain_on_overflow */
564 bfd_elf_generic_reloc, /* special_function */
565 AARCH64_R_STR (MOVW_UABS_G3), /* name */
566 FALSE, /* partial_inplace */
567 0xffff, /* src_mask */
568 0xffff, /* dst_mask */
569 FALSE), /* pcrel_offset */
571 /* Group relocations to create high part of a 16, 32, 48 or 64 bit
572 signed data or abs address inline. Will change instruction
573 to MOVN or MOVZ depending on sign of calculated value. */
575 /* MOV[ZN]: ((S+A) >> 0) & 0xffff */
576 HOWTO (AARCH64_R (MOVW_SABS_G0), /* type */
577 0, /* rightshift */
578 2, /* size (0 = byte, 1 = short, 2 = long) */
579 17, /* bitsize */
580 FALSE, /* pc_relative */
581 0, /* bitpos */
582 complain_overflow_signed, /* complain_on_overflow */
583 bfd_elf_generic_reloc, /* special_function */
584 AARCH64_R_STR (MOVW_SABS_G0), /* name */
585 FALSE, /* partial_inplace */
586 0xffff, /* src_mask */
587 0xffff, /* dst_mask */
588 FALSE), /* pcrel_offset */
590 /* MOV[ZN]: ((S+A) >> 16) & 0xffff */
591 HOWTO64 (AARCH64_R (MOVW_SABS_G1), /* type */
592 16, /* rightshift */
593 2, /* size (0 = byte, 1 = short, 2 = long) */
594 17, /* bitsize */
595 FALSE, /* pc_relative */
596 0, /* bitpos */
597 complain_overflow_signed, /* complain_on_overflow */
598 bfd_elf_generic_reloc, /* special_function */
599 AARCH64_R_STR (MOVW_SABS_G1), /* name */
600 FALSE, /* partial_inplace */
601 0xffff, /* src_mask */
602 0xffff, /* dst_mask */
603 FALSE), /* pcrel_offset */
605 /* MOV[ZN]: ((S+A) >> 32) & 0xffff */
606 HOWTO64 (AARCH64_R (MOVW_SABS_G2), /* type */
607 32, /* rightshift */
608 2, /* size (0 = byte, 1 = short, 2 = long) */
609 17, /* bitsize */
610 FALSE, /* pc_relative */
611 0, /* bitpos */
612 complain_overflow_signed, /* complain_on_overflow */
613 bfd_elf_generic_reloc, /* special_function */
614 AARCH64_R_STR (MOVW_SABS_G2), /* name */
615 FALSE, /* partial_inplace */
616 0xffff, /* src_mask */
617 0xffff, /* dst_mask */
618 FALSE), /* pcrel_offset */
620 /* Relocations to generate 19, 21 and 33 bit PC-relative load/store
621 addresses: PG(x) is (x & ~0xfff). */
623 /* LD-lit: ((S+A-P) >> 2) & 0x7ffff */
624 HOWTO (AARCH64_R (LD_PREL_LO19), /* type */
625 2, /* rightshift */
626 2, /* size (0 = byte, 1 = short, 2 = long) */
627 19, /* bitsize */
628 TRUE, /* pc_relative */
629 0, /* bitpos */
630 complain_overflow_signed, /* complain_on_overflow */
631 bfd_elf_generic_reloc, /* special_function */
632 AARCH64_R_STR (LD_PREL_LO19), /* name */
633 FALSE, /* partial_inplace */
634 0x7ffff, /* src_mask */
635 0x7ffff, /* dst_mask */
636 TRUE), /* pcrel_offset */
638 /* ADR: (S+A-P) & 0x1fffff */
639 HOWTO (AARCH64_R (ADR_PREL_LO21), /* type */
640 0, /* rightshift */
641 2, /* size (0 = byte, 1 = short, 2 = long) */
642 21, /* bitsize */
643 TRUE, /* pc_relative */
644 0, /* bitpos */
645 complain_overflow_signed, /* complain_on_overflow */
646 bfd_elf_generic_reloc, /* special_function */
647 AARCH64_R_STR (ADR_PREL_LO21), /* name */
648 FALSE, /* partial_inplace */
649 0x1fffff, /* src_mask */
650 0x1fffff, /* dst_mask */
651 TRUE), /* pcrel_offset */
653 /* ADRP: ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
654 HOWTO (AARCH64_R (ADR_PREL_PG_HI21), /* type */
655 12, /* rightshift */
656 2, /* size (0 = byte, 1 = short, 2 = long) */
657 21, /* bitsize */
658 TRUE, /* pc_relative */
659 0, /* bitpos */
660 complain_overflow_signed, /* complain_on_overflow */
661 bfd_elf_generic_reloc, /* special_function */
662 AARCH64_R_STR (ADR_PREL_PG_HI21), /* name */
663 FALSE, /* partial_inplace */
664 0x1fffff, /* src_mask */
665 0x1fffff, /* dst_mask */
666 TRUE), /* pcrel_offset */
668 /* ADRP: ((PG(S+A)-PG(P)) >> 12) & 0x1fffff [no overflow check] */
669 HOWTO64 (AARCH64_R (ADR_PREL_PG_HI21_NC), /* type */
670 12, /* rightshift */
671 2, /* size (0 = byte, 1 = short, 2 = long) */
672 21, /* bitsize */
673 TRUE, /* pc_relative */
674 0, /* bitpos */
675 complain_overflow_dont, /* complain_on_overflow */
676 bfd_elf_generic_reloc, /* special_function */
677 AARCH64_R_STR (ADR_PREL_PG_HI21_NC), /* name */
678 FALSE, /* partial_inplace */
679 0x1fffff, /* src_mask */
680 0x1fffff, /* dst_mask */
681 TRUE), /* pcrel_offset */
683 /* ADD: (S+A) & 0xfff [no overflow check] */
684 HOWTO (AARCH64_R (ADD_ABS_LO12_NC), /* type */
685 0, /* rightshift */
686 2, /* size (0 = byte, 1 = short, 2 = long) */
687 12, /* bitsize */
688 FALSE, /* pc_relative */
689 10, /* bitpos */
690 complain_overflow_dont, /* complain_on_overflow */
691 bfd_elf_generic_reloc, /* special_function */
692 AARCH64_R_STR (ADD_ABS_LO12_NC), /* name */
693 FALSE, /* partial_inplace */
694 0x3ffc00, /* src_mask */
695 0x3ffc00, /* dst_mask */
696 FALSE), /* pcrel_offset */
698 /* LD/ST8: (S+A) & 0xfff */
699 HOWTO (AARCH64_R (LDST8_ABS_LO12_NC), /* type */
700 0, /* rightshift */
701 2, /* size (0 = byte, 1 = short, 2 = long) */
702 12, /* bitsize */
703 FALSE, /* pc_relative */
704 0, /* bitpos */
705 complain_overflow_dont, /* complain_on_overflow */
706 bfd_elf_generic_reloc, /* special_function */
707 AARCH64_R_STR (LDST8_ABS_LO12_NC), /* name */
708 FALSE, /* partial_inplace */
709 0xfff, /* src_mask */
710 0xfff, /* dst_mask */
711 FALSE), /* pcrel_offset */
713 /* Relocations for control-flow instructions. */
715 /* TBZ/NZ: ((S+A-P) >> 2) & 0x3fff */
716 HOWTO (AARCH64_R (TSTBR14), /* type */
717 2, /* rightshift */
718 2, /* size (0 = byte, 1 = short, 2 = long) */
719 14, /* bitsize */
720 TRUE, /* pc_relative */
721 0, /* bitpos */
722 complain_overflow_signed, /* complain_on_overflow */
723 bfd_elf_generic_reloc, /* special_function */
724 AARCH64_R_STR (TSTBR14), /* name */
725 FALSE, /* partial_inplace */
726 0x3fff, /* src_mask */
727 0x3fff, /* dst_mask */
728 TRUE), /* pcrel_offset */
730 /* B.cond: ((S+A-P) >> 2) & 0x7ffff */
731 HOWTO (AARCH64_R (CONDBR19), /* type */
732 2, /* rightshift */
733 2, /* size (0 = byte, 1 = short, 2 = long) */
734 19, /* bitsize */
735 TRUE, /* pc_relative */
736 0, /* bitpos */
737 complain_overflow_signed, /* complain_on_overflow */
738 bfd_elf_generic_reloc, /* special_function */
739 AARCH64_R_STR (CONDBR19), /* name */
740 FALSE, /* partial_inplace */
741 0x7ffff, /* src_mask */
742 0x7ffff, /* dst_mask */
743 TRUE), /* pcrel_offset */
745 /* B: ((S+A-P) >> 2) & 0x3ffffff */
746 HOWTO (AARCH64_R (JUMP26), /* type */
747 2, /* rightshift */
748 2, /* size (0 = byte, 1 = short, 2 = long) */
749 26, /* bitsize */
750 TRUE, /* pc_relative */
751 0, /* bitpos */
752 complain_overflow_signed, /* complain_on_overflow */
753 bfd_elf_generic_reloc, /* special_function */
754 AARCH64_R_STR (JUMP26), /* name */
755 FALSE, /* partial_inplace */
756 0x3ffffff, /* src_mask */
757 0x3ffffff, /* dst_mask */
758 TRUE), /* pcrel_offset */
760 /* BL: ((S+A-P) >> 2) & 0x3ffffff */
761 HOWTO (AARCH64_R (CALL26), /* type */
762 2, /* rightshift */
763 2, /* size (0 = byte, 1 = short, 2 = long) */
764 26, /* bitsize */
765 TRUE, /* pc_relative */
766 0, /* bitpos */
767 complain_overflow_signed, /* complain_on_overflow */
768 bfd_elf_generic_reloc, /* special_function */
769 AARCH64_R_STR (CALL26), /* name */
770 FALSE, /* partial_inplace */
771 0x3ffffff, /* src_mask */
772 0x3ffffff, /* dst_mask */
773 TRUE), /* pcrel_offset */
775 /* LD/ST16: (S+A) & 0xffe */
776 HOWTO (AARCH64_R (LDST16_ABS_LO12_NC), /* type */
777 1, /* rightshift */
778 2, /* size (0 = byte, 1 = short, 2 = long) */
779 12, /* bitsize */
780 FALSE, /* pc_relative */
781 0, /* bitpos */
782 complain_overflow_dont, /* complain_on_overflow */
783 bfd_elf_generic_reloc, /* special_function */
784 AARCH64_R_STR (LDST16_ABS_LO12_NC), /* name */
785 FALSE, /* partial_inplace */
786 0xffe, /* src_mask */
787 0xffe, /* dst_mask */
788 FALSE), /* pcrel_offset */
790 /* LD/ST32: (S+A) & 0xffc */
791 HOWTO (AARCH64_R (LDST32_ABS_LO12_NC), /* type */
792 2, /* rightshift */
793 2, /* size (0 = byte, 1 = short, 2 = long) */
794 12, /* bitsize */
795 FALSE, /* pc_relative */
796 0, /* bitpos */
797 complain_overflow_dont, /* complain_on_overflow */
798 bfd_elf_generic_reloc, /* special_function */
799 AARCH64_R_STR (LDST32_ABS_LO12_NC), /* name */
800 FALSE, /* partial_inplace */
801 0xffc, /* src_mask */
802 0xffc, /* dst_mask */
803 FALSE), /* pcrel_offset */
805 /* LD/ST64: (S+A) & 0xff8 */
806 HOWTO (AARCH64_R (LDST64_ABS_LO12_NC), /* type */
807 3, /* rightshift */
808 2, /* size (0 = byte, 1 = short, 2 = long) */
809 12, /* bitsize */
810 FALSE, /* pc_relative */
811 0, /* bitpos */
812 complain_overflow_dont, /* complain_on_overflow */
813 bfd_elf_generic_reloc, /* special_function */
814 AARCH64_R_STR (LDST64_ABS_LO12_NC), /* name */
815 FALSE, /* partial_inplace */
816 0xff8, /* src_mask */
817 0xff8, /* dst_mask */
818 FALSE), /* pcrel_offset */
820 /* LD/ST128: (S+A) & 0xff0 */
821 HOWTO (AARCH64_R (LDST128_ABS_LO12_NC), /* type */
822 4, /* rightshift */
823 2, /* size (0 = byte, 1 = short, 2 = long) */
824 12, /* bitsize */
825 FALSE, /* pc_relative */
826 0, /* bitpos */
827 complain_overflow_dont, /* complain_on_overflow */
828 bfd_elf_generic_reloc, /* special_function */
829 AARCH64_R_STR (LDST128_ABS_LO12_NC), /* name */
830 FALSE, /* partial_inplace */
831 0xff0, /* src_mask */
832 0xff0, /* dst_mask */
833 FALSE), /* pcrel_offset */
835 /* Set a load-literal immediate field to bits
836 0x1FFFFC of G(S)-P */
837 HOWTO (AARCH64_R (GOT_LD_PREL19), /* type */
838 2, /* rightshift */
839 2, /* size (0 = byte,1 = short,2 = long) */
840 19, /* bitsize */
841 TRUE, /* pc_relative */
842 0, /* bitpos */
843 complain_overflow_signed, /* complain_on_overflow */
844 bfd_elf_generic_reloc, /* special_function */
845 AARCH64_R_STR (GOT_LD_PREL19), /* name */
846 FALSE, /* partial_inplace */
847 0xffffe0, /* src_mask */
848 0xffffe0, /* dst_mask */
849 TRUE), /* pcrel_offset */
851 /* Get to the page for the GOT entry for the symbol
852 (G(S) - P) using an ADRP instruction. */
853 HOWTO (AARCH64_R (ADR_GOT_PAGE), /* type */
854 12, /* rightshift */
855 2, /* size (0 = byte, 1 = short, 2 = long) */
856 21, /* bitsize */
857 TRUE, /* pc_relative */
858 0, /* bitpos */
859 complain_overflow_dont, /* complain_on_overflow */
860 bfd_elf_generic_reloc, /* special_function */
861 AARCH64_R_STR (ADR_GOT_PAGE), /* name */
862 FALSE, /* partial_inplace */
863 0x1fffff, /* src_mask */
864 0x1fffff, /* dst_mask */
865 TRUE), /* pcrel_offset */
867 /* LD64: GOT offset G(S) & 0xff8 */
868 HOWTO64 (AARCH64_R (LD64_GOT_LO12_NC), /* type */
869 3, /* rightshift */
870 2, /* size (0 = byte, 1 = short, 2 = long) */
871 12, /* bitsize */
872 FALSE, /* pc_relative */
873 0, /* bitpos */
874 complain_overflow_dont, /* complain_on_overflow */
875 bfd_elf_generic_reloc, /* special_function */
876 AARCH64_R_STR (LD64_GOT_LO12_NC), /* name */
877 FALSE, /* partial_inplace */
878 0xff8, /* src_mask */
879 0xff8, /* dst_mask */
880 FALSE), /* pcrel_offset */
882 /* LD32: GOT offset G(S) & 0xffc */
883 HOWTO32 (AARCH64_R (LD32_GOT_LO12_NC), /* type */
884 2, /* rightshift */
885 2, /* size (0 = byte, 1 = short, 2 = long) */
886 12, /* bitsize */
887 FALSE, /* pc_relative */
888 0, /* bitpos */
889 complain_overflow_dont, /* complain_on_overflow */
890 bfd_elf_generic_reloc, /* special_function */
891 AARCH64_R_STR (LD32_GOT_LO12_NC), /* name */
892 FALSE, /* partial_inplace */
893 0xffc, /* src_mask */
894 0xffc, /* dst_mask */
895 FALSE), /* pcrel_offset */
897 /* Lower 16 bits of GOT offset for the symbol. */
898 HOWTO64 (AARCH64_R (MOVW_GOTOFF_G0_NC), /* type */
899 0, /* rightshift */
900 2, /* size (0 = byte, 1 = short, 2 = long) */
901 16, /* bitsize */
902 FALSE, /* pc_relative */
903 0, /* bitpos */
904 complain_overflow_dont, /* complain_on_overflow */
905 bfd_elf_generic_reloc, /* special_function */
906 AARCH64_R_STR (MOVW_GOTOFF_G0_NC), /* name */
907 FALSE, /* partial_inplace */
908 0xffff, /* src_mask */
909 0xffff, /* dst_mask */
910 FALSE), /* pcrel_offset */
912 /* Higher 16 bits of GOT offset for the symbol. */
913 HOWTO64 (AARCH64_R (MOVW_GOTOFF_G1), /* type */
914 16, /* rightshift */
915 2, /* size (0 = byte, 1 = short, 2 = long) */
916 16, /* bitsize */
917 FALSE, /* pc_relative */
918 0, /* bitpos */
919 complain_overflow_unsigned, /* complain_on_overflow */
920 bfd_elf_generic_reloc, /* special_function */
921 AARCH64_R_STR (MOVW_GOTOFF_G1), /* name */
922 FALSE, /* partial_inplace */
923 0xffff, /* src_mask */
924 0xffff, /* dst_mask */
925 FALSE), /* pcrel_offset */
927 /* LD64: GOT offset for the symbol. */
928 HOWTO64 (AARCH64_R (LD64_GOTOFF_LO15), /* type */
929 3, /* rightshift */
930 2, /* size (0 = byte, 1 = short, 2 = long) */
931 12, /* bitsize */
932 FALSE, /* pc_relative */
933 0, /* bitpos */
934 complain_overflow_unsigned, /* complain_on_overflow */
935 bfd_elf_generic_reloc, /* special_function */
936 AARCH64_R_STR (LD64_GOTOFF_LO15), /* name */
937 FALSE, /* partial_inplace */
938 0x7ff8, /* src_mask */
939 0x7ff8, /* dst_mask */
940 FALSE), /* pcrel_offset */
942 /* LD32: GOT offset to the page address of GOT table.
943 (G(S) - PAGE (_GLOBAL_OFFSET_TABLE_)) & 0x5ffc. */
944 HOWTO32 (AARCH64_R (LD32_GOTPAGE_LO14), /* type */
945 2, /* rightshift */
946 2, /* size (0 = byte, 1 = short, 2 = long) */
947 12, /* bitsize */
948 FALSE, /* pc_relative */
949 0, /* bitpos */
950 complain_overflow_unsigned, /* complain_on_overflow */
951 bfd_elf_generic_reloc, /* special_function */
952 AARCH64_R_STR (LD32_GOTPAGE_LO14), /* name */
953 FALSE, /* partial_inplace */
954 0x5ffc, /* src_mask */
955 0x5ffc, /* dst_mask */
956 FALSE), /* pcrel_offset */
958 /* LD64: GOT offset to the page address of GOT table.
959 (G(S) - PAGE (_GLOBAL_OFFSET_TABLE_)) & 0x7ff8. */
960 HOWTO64 (AARCH64_R (LD64_GOTPAGE_LO15), /* type */
961 3, /* rightshift */
962 2, /* size (0 = byte, 1 = short, 2 = long) */
963 12, /* bitsize */
964 FALSE, /* pc_relative */
965 0, /* bitpos */
966 complain_overflow_unsigned, /* complain_on_overflow */
967 bfd_elf_generic_reloc, /* special_function */
968 AARCH64_R_STR (LD64_GOTPAGE_LO15), /* name */
969 FALSE, /* partial_inplace */
970 0x7ff8, /* src_mask */
971 0x7ff8, /* dst_mask */
972 FALSE), /* pcrel_offset */
974 /* Get to the page for the GOT entry for the symbol
975 (G(S) - P) using an ADRP instruction. */
976 HOWTO (AARCH64_R (TLSGD_ADR_PAGE21), /* type */
977 12, /* rightshift */
978 2, /* size (0 = byte, 1 = short, 2 = long) */
979 21, /* bitsize */
980 TRUE, /* pc_relative */
981 0, /* bitpos */
982 complain_overflow_dont, /* complain_on_overflow */
983 bfd_elf_generic_reloc, /* special_function */
984 AARCH64_R_STR (TLSGD_ADR_PAGE21), /* name */
985 FALSE, /* partial_inplace */
986 0x1fffff, /* src_mask */
987 0x1fffff, /* dst_mask */
988 TRUE), /* pcrel_offset */
990 HOWTO (AARCH64_R (TLSGD_ADR_PREL21), /* type */
991 0, /* rightshift */
992 2, /* size (0 = byte, 1 = short, 2 = long) */
993 21, /* bitsize */
994 TRUE, /* pc_relative */
995 0, /* bitpos */
996 complain_overflow_dont, /* complain_on_overflow */
997 bfd_elf_generic_reloc, /* special_function */
998 AARCH64_R_STR (TLSGD_ADR_PREL21), /* name */
999 FALSE, /* partial_inplace */
1000 0x1fffff, /* src_mask */
1001 0x1fffff, /* dst_mask */
1002 TRUE), /* pcrel_offset */
1004 /* ADD: GOT offset G(S) & 0xff8 [no overflow check] */
1005 HOWTO (AARCH64_R (TLSGD_ADD_LO12_NC), /* type */
1006 0, /* rightshift */
1007 2, /* size (0 = byte, 1 = short, 2 = long) */
1008 12, /* bitsize */
1009 FALSE, /* pc_relative */
1010 0, /* bitpos */
1011 complain_overflow_dont, /* complain_on_overflow */
1012 bfd_elf_generic_reloc, /* special_function */
1013 AARCH64_R_STR (TLSGD_ADD_LO12_NC), /* name */
1014 FALSE, /* partial_inplace */
1015 0xfff, /* src_mask */
1016 0xfff, /* dst_mask */
1017 FALSE), /* pcrel_offset */
1019 /* Lower 16 bits of GOT offset to tls_index. */
1020 HOWTO64 (AARCH64_R (TLSGD_MOVW_G0_NC), /* type */
1021 0, /* rightshift */
1022 2, /* size (0 = byte, 1 = short, 2 = long) */
1023 16, /* bitsize */
1024 FALSE, /* pc_relative */
1025 0, /* bitpos */
1026 complain_overflow_dont, /* complain_on_overflow */
1027 bfd_elf_generic_reloc, /* special_function */
1028 AARCH64_R_STR (TLSGD_MOVW_G0_NC), /* name */
1029 FALSE, /* partial_inplace */
1030 0xffff, /* src_mask */
1031 0xffff, /* dst_mask */
1032 FALSE), /* pcrel_offset */
1034 /* Higher 16 bits of GOT offset to tls_index. */
1035 HOWTO64 (AARCH64_R (TLSGD_MOVW_G1), /* type */
1036 16, /* rightshift */
1037 2, /* size (0 = byte, 1 = short, 2 = long) */
1038 16, /* bitsize */
1039 FALSE, /* pc_relative */
1040 0, /* bitpos */
1041 complain_overflow_unsigned, /* complain_on_overflow */
1042 bfd_elf_generic_reloc, /* special_function */
1043 AARCH64_R_STR (TLSGD_MOVW_G1), /* name */
1044 FALSE, /* partial_inplace */
1045 0xffff, /* src_mask */
1046 0xffff, /* dst_mask */
1047 FALSE), /* pcrel_offset */
1049 HOWTO (AARCH64_R (TLSIE_ADR_GOTTPREL_PAGE21), /* type */
1050 12, /* rightshift */
1051 2, /* size (0 = byte, 1 = short, 2 = long) */
1052 21, /* bitsize */
1053 FALSE, /* pc_relative */
1054 0, /* bitpos */
1055 complain_overflow_dont, /* complain_on_overflow */
1056 bfd_elf_generic_reloc, /* special_function */
1057 AARCH64_R_STR (TLSIE_ADR_GOTTPREL_PAGE21), /* name */
1058 FALSE, /* partial_inplace */
1059 0x1fffff, /* src_mask */
1060 0x1fffff, /* dst_mask */
1061 FALSE), /* pcrel_offset */
1063 HOWTO64 (AARCH64_R (TLSIE_LD64_GOTTPREL_LO12_NC), /* type */
1064 3, /* rightshift */
1065 2, /* size (0 = byte, 1 = short, 2 = long) */
1066 12, /* bitsize */
1067 FALSE, /* pc_relative */
1068 0, /* bitpos */
1069 complain_overflow_dont, /* complain_on_overflow */
1070 bfd_elf_generic_reloc, /* special_function */
1071 AARCH64_R_STR (TLSIE_LD64_GOTTPREL_LO12_NC), /* name */
1072 FALSE, /* partial_inplace */
1073 0xff8, /* src_mask */
1074 0xff8, /* dst_mask */
1075 FALSE), /* pcrel_offset */
1077 HOWTO32 (AARCH64_R (TLSIE_LD32_GOTTPREL_LO12_NC), /* type */
1078 2, /* rightshift */
1079 2, /* size (0 = byte, 1 = short, 2 = long) */
1080 12, /* bitsize */
1081 FALSE, /* pc_relative */
1082 0, /* bitpos */
1083 complain_overflow_dont, /* complain_on_overflow */
1084 bfd_elf_generic_reloc, /* special_function */
1085 AARCH64_R_STR (TLSIE_LD32_GOTTPREL_LO12_NC), /* name */
1086 FALSE, /* partial_inplace */
1087 0xffc, /* src_mask */
1088 0xffc, /* dst_mask */
1089 FALSE), /* pcrel_offset */
1091 HOWTO (AARCH64_R (TLSIE_LD_GOTTPREL_PREL19), /* type */
1092 2, /* rightshift */
1093 2, /* size (0 = byte, 1 = short, 2 = long) */
1094 19, /* bitsize */
1095 FALSE, /* pc_relative */
1096 0, /* bitpos */
1097 complain_overflow_dont, /* complain_on_overflow */
1098 bfd_elf_generic_reloc, /* special_function */
1099 AARCH64_R_STR (TLSIE_LD_GOTTPREL_PREL19), /* name */
1100 FALSE, /* partial_inplace */
1101 0x1ffffc, /* src_mask */
1102 0x1ffffc, /* dst_mask */
1103 FALSE), /* pcrel_offset */
1105 HOWTO64 (AARCH64_R (TLSIE_MOVW_GOTTPREL_G0_NC), /* type */
1106 0, /* rightshift */
1107 2, /* size (0 = byte, 1 = short, 2 = long) */
1108 16, /* bitsize */
1109 FALSE, /* pc_relative */
1110 0, /* bitpos */
1111 complain_overflow_dont, /* complain_on_overflow */
1112 bfd_elf_generic_reloc, /* special_function */
1113 AARCH64_R_STR (TLSIE_MOVW_GOTTPREL_G0_NC), /* name */
1114 FALSE, /* partial_inplace */
1115 0xffff, /* src_mask */
1116 0xffff, /* dst_mask */
1117 FALSE), /* pcrel_offset */
1119 HOWTO64 (AARCH64_R (TLSIE_MOVW_GOTTPREL_G1), /* type */
1120 16, /* rightshift */
1121 2, /* size (0 = byte, 1 = short, 2 = long) */
1122 16, /* bitsize */
1123 FALSE, /* pc_relative */
1124 0, /* bitpos */
1125 complain_overflow_unsigned, /* complain_on_overflow */
1126 bfd_elf_generic_reloc, /* special_function */
1127 AARCH64_R_STR (TLSIE_MOVW_GOTTPREL_G1), /* name */
1128 FALSE, /* partial_inplace */
1129 0xffff, /* src_mask */
1130 0xffff, /* dst_mask */
1131 FALSE), /* pcrel_offset */
1133 /* ADD: bit[23:12] of byte offset to module TLS base address. */
1134 HOWTO (AARCH64_R (TLSLD_ADD_DTPREL_HI12), /* type */
1135 12, /* rightshift */
1136 2, /* size (0 = byte, 1 = short, 2 = long) */
1137 12, /* bitsize */
1138 FALSE, /* pc_relative */
1139 0, /* bitpos */
1140 complain_overflow_unsigned, /* complain_on_overflow */
1141 bfd_elf_generic_reloc, /* special_function */
1142 AARCH64_R_STR (TLSLD_ADD_DTPREL_HI12), /* name */
1143 FALSE, /* partial_inplace */
1144 0xfff, /* src_mask */
1145 0xfff, /* dst_mask */
1146 FALSE), /* pcrel_offset */
1148 /* Unsigned 12 bit byte offset to module TLS base address. */
1149 HOWTO (AARCH64_R (TLSLD_ADD_DTPREL_LO12), /* type */
1150 0, /* rightshift */
1151 2, /* size (0 = byte, 1 = short, 2 = long) */
1152 12, /* bitsize */
1153 FALSE, /* pc_relative */
1154 0, /* bitpos */
1155 complain_overflow_unsigned, /* complain_on_overflow */
1156 bfd_elf_generic_reloc, /* special_function */
1157 AARCH64_R_STR (TLSLD_ADD_DTPREL_LO12), /* name */
1158 FALSE, /* partial_inplace */
1159 0xfff, /* src_mask */
1160 0xfff, /* dst_mask */
1161 FALSE), /* pcrel_offset */
1163 /* No overflow check version of BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12. */
1164 HOWTO (AARCH64_R (TLSLD_ADD_DTPREL_LO12_NC), /* type */
1165 0, /* rightshift */
1166 2, /* size (0 = byte, 1 = short, 2 = long) */
1167 12, /* bitsize */
1168 FALSE, /* pc_relative */
1169 0, /* bitpos */
1170 complain_overflow_dont, /* complain_on_overflow */
1171 bfd_elf_generic_reloc, /* special_function */
1172 AARCH64_R_STR (TLSLD_ADD_DTPREL_LO12_NC), /* name */
1173 FALSE, /* partial_inplace */
1174 0xfff, /* src_mask */
1175 0xfff, /* dst_mask */
1176 FALSE), /* pcrel_offset */
1178 /* ADD: GOT offset G(S) & 0xff8 [no overflow check] */
1179 HOWTO (AARCH64_R (TLSLD_ADD_LO12_NC), /* type */
1180 0, /* rightshift */
1181 2, /* size (0 = byte, 1 = short, 2 = long) */
1182 12, /* bitsize */
1183 FALSE, /* pc_relative */
1184 0, /* bitpos */
1185 complain_overflow_dont, /* complain_on_overflow */
1186 bfd_elf_generic_reloc, /* special_function */
1187 AARCH64_R_STR (TLSLD_ADD_LO12_NC), /* name */
1188 FALSE, /* partial_inplace */
1189 0xfff, /* src_mask */
1190 0xfff, /* dst_mask */
1191 FALSE), /* pcrel_offset */
1193 /* Get to the page for the GOT entry for the symbol
1194 (G(S) - P) using an ADRP instruction. */
1195 HOWTO (AARCH64_R (TLSLD_ADR_PAGE21), /* type */
1196 12, /* rightshift */
1197 2, /* size (0 = byte, 1 = short, 2 = long) */
1198 21, /* bitsize */
1199 TRUE, /* pc_relative */
1200 0, /* bitpos */
1201 complain_overflow_signed, /* complain_on_overflow */
1202 bfd_elf_generic_reloc, /* special_function */
1203 AARCH64_R_STR (TLSLD_ADR_PAGE21), /* name */
1204 FALSE, /* partial_inplace */
1205 0x1fffff, /* src_mask */
1206 0x1fffff, /* dst_mask */
1207 TRUE), /* pcrel_offset */
1209 HOWTO (AARCH64_R (TLSLD_ADR_PREL21), /* type */
1210 0, /* rightshift */
1211 2, /* size (0 = byte, 1 = short, 2 = long) */
1212 21, /* bitsize */
1213 TRUE, /* pc_relative */
1214 0, /* bitpos */
1215 complain_overflow_signed, /* complain_on_overflow */
1216 bfd_elf_generic_reloc, /* special_function */
1217 AARCH64_R_STR (TLSLD_ADR_PREL21), /* name */
1218 FALSE, /* partial_inplace */
1219 0x1fffff, /* src_mask */
1220 0x1fffff, /* dst_mask */
1221 TRUE), /* pcrel_offset */
1223 /* LD/ST16: bit[11:1] of byte offset to module TLS base address. */
1224 HOWTO64 (AARCH64_R (TLSLD_LDST16_DTPREL_LO12), /* type */
1225 1, /* rightshift */
1226 2, /* size (0 = byte, 1 = short, 2 = long) */
1227 11, /* bitsize */
1228 FALSE, /* pc_relative */
1229 10, /* bitpos */
1230 complain_overflow_unsigned, /* complain_on_overflow */
1231 bfd_elf_generic_reloc, /* special_function */
1232 AARCH64_R_STR (TLSLD_LDST16_DTPREL_LO12), /* name */
1233 FALSE, /* partial_inplace */
1234 0x1ffc00, /* src_mask */
1235 0x1ffc00, /* dst_mask */
1236 FALSE), /* pcrel_offset */
1238 /* Same as BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12, but no overflow check. */
1239 HOWTO64 (AARCH64_R (TLSLD_LDST16_DTPREL_LO12_NC), /* type */
1240 1, /* rightshift */
1241 2, /* size (0 = byte, 1 = short, 2 = long) */
1242 11, /* bitsize */
1243 FALSE, /* pc_relative */
1244 10, /* bitpos */
1245 complain_overflow_dont, /* complain_on_overflow */
1246 bfd_elf_generic_reloc, /* special_function */
1247 AARCH64_R_STR (TLSLD_LDST16_DTPREL_LO12_NC), /* name */
1248 FALSE, /* partial_inplace */
1249 0x1ffc00, /* src_mask */
1250 0x1ffc00, /* dst_mask */
1251 FALSE), /* pcrel_offset */
1253 /* LD/ST32: bit[11:2] of byte offset to module TLS base address. */
1254 HOWTO64 (AARCH64_R (TLSLD_LDST32_DTPREL_LO12), /* type */
1255 2, /* rightshift */
1256 2, /* size (0 = byte, 1 = short, 2 = long) */
1257 10, /* bitsize */
1258 FALSE, /* pc_relative */
1259 10, /* bitpos */
1260 complain_overflow_unsigned, /* complain_on_overflow */
1261 bfd_elf_generic_reloc, /* special_function */
1262 AARCH64_R_STR (TLSLD_LDST32_DTPREL_LO12), /* name */
1263 FALSE, /* partial_inplace */
1264 0x3ffc00, /* src_mask */
1265 0x3ffc00, /* dst_mask */
1266 FALSE), /* pcrel_offset */
1268 /* Same as BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12, but no overflow check. */
1269 HOWTO64 (AARCH64_R (TLSLD_LDST32_DTPREL_LO12_NC), /* type */
1270 2, /* rightshift */
1271 2, /* size (0 = byte, 1 = short, 2 = long) */
1272 10, /* bitsize */
1273 FALSE, /* pc_relative */
1274 10, /* bitpos */
1275 complain_overflow_dont, /* complain_on_overflow */
1276 bfd_elf_generic_reloc, /* special_function */
1277 AARCH64_R_STR (TLSLD_LDST32_DTPREL_LO12_NC), /* name */
1278 FALSE, /* partial_inplace */
1279 0xffc00, /* src_mask */
1280 0xffc00, /* dst_mask */
1281 FALSE), /* pcrel_offset */
1283 /* LD/ST64: bit[11:3] of byte offset to module TLS base address. */
1284 HOWTO64 (AARCH64_R (TLSLD_LDST64_DTPREL_LO12), /* type */
1285 3, /* rightshift */
1286 2, /* size (0 = byte, 1 = short, 2 = long) */
1287 9, /* bitsize */
1288 FALSE, /* pc_relative */
1289 10, /* bitpos */
1290 complain_overflow_unsigned, /* complain_on_overflow */
1291 bfd_elf_generic_reloc, /* special_function */
1292 AARCH64_R_STR (TLSLD_LDST64_DTPREL_LO12), /* name */
1293 FALSE, /* partial_inplace */
1294 0x3ffc00, /* src_mask */
1295 0x3ffc00, /* dst_mask */
1296 FALSE), /* pcrel_offset */
1298 /* Same as BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12, but no overflow check. */
1299 HOWTO64 (AARCH64_R (TLSLD_LDST64_DTPREL_LO12_NC), /* type */
1300 3, /* rightshift */
1301 2, /* size (0 = byte, 1 = short, 2 = long) */
1302 9, /* bitsize */
1303 FALSE, /* pc_relative */
1304 10, /* bitpos */
1305 complain_overflow_dont, /* complain_on_overflow */
1306 bfd_elf_generic_reloc, /* special_function */
1307 AARCH64_R_STR (TLSLD_LDST64_DTPREL_LO12_NC), /* name */
1308 FALSE, /* partial_inplace */
1309 0x7fc00, /* src_mask */
1310 0x7fc00, /* dst_mask */
1311 FALSE), /* pcrel_offset */
1313 /* LD/ST8: bit[11:0] of byte offset to module TLS base address. */
1314 HOWTO64 (AARCH64_R (TLSLD_LDST8_DTPREL_LO12), /* type */
1315 0, /* rightshift */
1316 2, /* size (0 = byte, 1 = short, 2 = long) */
1317 12, /* bitsize */
1318 FALSE, /* pc_relative */
1319 10, /* bitpos */
1320 complain_overflow_unsigned, /* complain_on_overflow */
1321 bfd_elf_generic_reloc, /* special_function */
1322 AARCH64_R_STR (TLSLD_LDST8_DTPREL_LO12), /* name */
1323 FALSE, /* partial_inplace */
1324 0x3ffc00, /* src_mask */
1325 0x3ffc00, /* dst_mask */
1326 FALSE), /* pcrel_offset */
1328 /* Same as BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12, but no overflow check. */
1329 HOWTO64 (AARCH64_R (TLSLD_LDST8_DTPREL_LO12_NC), /* type */
1330 0, /* rightshift */
1331 2, /* size (0 = byte, 1 = short, 2 = long) */
1332 12, /* bitsize */
1333 FALSE, /* pc_relative */
1334 10, /* bitpos */
1335 complain_overflow_dont, /* complain_on_overflow */
1336 bfd_elf_generic_reloc, /* special_function */
1337 AARCH64_R_STR (TLSLD_LDST8_DTPREL_LO12_NC), /* name */
1338 FALSE, /* partial_inplace */
1339 0x3ffc00, /* src_mask */
1340 0x3ffc00, /* dst_mask */
1341 FALSE), /* pcrel_offset */
1343 /* MOVZ: bit[15:0] of byte offset to module TLS base address. */
1344 HOWTO (AARCH64_R (TLSLD_MOVW_DTPREL_G0), /* type */
1345 0, /* rightshift */
1346 2, /* size (0 = byte, 1 = short, 2 = long) */
1347 16, /* bitsize */
1348 FALSE, /* pc_relative */
1349 0, /* bitpos */
1350 complain_overflow_unsigned, /* complain_on_overflow */
1351 bfd_elf_generic_reloc, /* special_function */
1352 AARCH64_R_STR (TLSLD_MOVW_DTPREL_G0), /* name */
1353 FALSE, /* partial_inplace */
1354 0xffff, /* src_mask */
1355 0xffff, /* dst_mask */
1356 FALSE), /* pcrel_offset */
1358 /* No overflow check version of BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0. */
1359 HOWTO (AARCH64_R (TLSLD_MOVW_DTPREL_G0_NC), /* type */
1360 0, /* rightshift */
1361 2, /* size (0 = byte, 1 = short, 2 = long) */
1362 16, /* bitsize */
1363 FALSE, /* pc_relative */
1364 0, /* bitpos */
1365 complain_overflow_dont, /* complain_on_overflow */
1366 bfd_elf_generic_reloc, /* special_function */
1367 AARCH64_R_STR (TLSLD_MOVW_DTPREL_G0_NC), /* name */
1368 FALSE, /* partial_inplace */
1369 0xffff, /* src_mask */
1370 0xffff, /* dst_mask */
1371 FALSE), /* pcrel_offset */
1373 /* MOVZ: bit[31:16] of byte offset to module TLS base address. */
1374 HOWTO (AARCH64_R (TLSLD_MOVW_DTPREL_G1), /* type */
1375 16, /* rightshift */
1376 2, /* size (0 = byte, 1 = short, 2 = long) */
1377 16, /* bitsize */
1378 FALSE, /* pc_relative */
1379 0, /* bitpos */
1380 complain_overflow_unsigned, /* complain_on_overflow */
1381 bfd_elf_generic_reloc, /* special_function */
1382 AARCH64_R_STR (TLSLD_MOVW_DTPREL_G1), /* name */
1383 FALSE, /* partial_inplace */
1384 0xffff, /* src_mask */
1385 0xffff, /* dst_mask */
1386 FALSE), /* pcrel_offset */
1388 /* No overflow check version of BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1. */
1389 HOWTO64 (AARCH64_R (TLSLD_MOVW_DTPREL_G1_NC), /* type */
1390 16, /* rightshift */
1391 2, /* size (0 = byte, 1 = short, 2 = long) */
1392 16, /* bitsize */
1393 FALSE, /* pc_relative */
1394 0, /* bitpos */
1395 complain_overflow_dont, /* complain_on_overflow */
1396 bfd_elf_generic_reloc, /* special_function */
1397 AARCH64_R_STR (TLSLD_MOVW_DTPREL_G1_NC), /* name */
1398 FALSE, /* partial_inplace */
1399 0xffff, /* src_mask */
1400 0xffff, /* dst_mask */
1401 FALSE), /* pcrel_offset */
1403 /* MOVZ: bit[47:32] of byte offset to module TLS base address. */
1404 HOWTO64 (AARCH64_R (TLSLD_MOVW_DTPREL_G2), /* type */
1405 32, /* rightshift */
1406 2, /* size (0 = byte, 1 = short, 2 = long) */
1407 16, /* bitsize */
1408 FALSE, /* pc_relative */
1409 0, /* bitpos */
1410 complain_overflow_unsigned, /* complain_on_overflow */
1411 bfd_elf_generic_reloc, /* special_function */
1412 AARCH64_R_STR (TLSLD_MOVW_DTPREL_G2), /* name */
1413 FALSE, /* partial_inplace */
1414 0xffff, /* src_mask */
1415 0xffff, /* dst_mask */
1416 FALSE), /* pcrel_offset */
1418 HOWTO64 (AARCH64_R (TLSLE_MOVW_TPREL_G2), /* type */
1419 32, /* rightshift */
1420 2, /* size (0 = byte, 1 = short, 2 = long) */
1421 16, /* bitsize */
1422 FALSE, /* pc_relative */
1423 0, /* bitpos */
1424 complain_overflow_unsigned, /* complain_on_overflow */
1425 bfd_elf_generic_reloc, /* special_function */
1426 AARCH64_R_STR (TLSLE_MOVW_TPREL_G2), /* name */
1427 FALSE, /* partial_inplace */
1428 0xffff, /* src_mask */
1429 0xffff, /* dst_mask */
1430 FALSE), /* pcrel_offset */
1432 HOWTO (AARCH64_R (TLSLE_MOVW_TPREL_G1), /* type */
1433 16, /* rightshift */
1434 2, /* size (0 = byte, 1 = short, 2 = long) */
1435 16, /* bitsize */
1436 FALSE, /* pc_relative */
1437 0, /* bitpos */
1438 complain_overflow_dont, /* complain_on_overflow */
1439 bfd_elf_generic_reloc, /* special_function */
1440 AARCH64_R_STR (TLSLE_MOVW_TPREL_G1), /* name */
1441 FALSE, /* partial_inplace */
1442 0xffff, /* src_mask */
1443 0xffff, /* dst_mask */
1444 FALSE), /* pcrel_offset */
1446 HOWTO64 (AARCH64_R (TLSLE_MOVW_TPREL_G1_NC), /* type */
1447 16, /* rightshift */
1448 2, /* size (0 = byte, 1 = short, 2 = long) */
1449 16, /* bitsize */
1450 FALSE, /* pc_relative */
1451 0, /* bitpos */
1452 complain_overflow_dont, /* complain_on_overflow */
1453 bfd_elf_generic_reloc, /* special_function */
1454 AARCH64_R_STR (TLSLE_MOVW_TPREL_G1_NC), /* name */
1455 FALSE, /* partial_inplace */
1456 0xffff, /* src_mask */
1457 0xffff, /* dst_mask */
1458 FALSE), /* pcrel_offset */
1460 HOWTO (AARCH64_R (TLSLE_MOVW_TPREL_G0), /* type */
1461 0, /* rightshift */
1462 2, /* size (0 = byte, 1 = short, 2 = long) */
1463 16, /* bitsize */
1464 FALSE, /* pc_relative */
1465 0, /* bitpos */
1466 complain_overflow_dont, /* complain_on_overflow */
1467 bfd_elf_generic_reloc, /* special_function */
1468 AARCH64_R_STR (TLSLE_MOVW_TPREL_G0), /* name */
1469 FALSE, /* partial_inplace */
1470 0xffff, /* src_mask */
1471 0xffff, /* dst_mask */
1472 FALSE), /* pcrel_offset */
1474 HOWTO (AARCH64_R (TLSLE_MOVW_TPREL_G0_NC), /* type */
1475 0, /* rightshift */
1476 2, /* size (0 = byte, 1 = short, 2 = long) */
1477 16, /* bitsize */
1478 FALSE, /* pc_relative */
1479 0, /* bitpos */
1480 complain_overflow_dont, /* complain_on_overflow */
1481 bfd_elf_generic_reloc, /* special_function */
1482 AARCH64_R_STR (TLSLE_MOVW_TPREL_G0_NC), /* name */
1483 FALSE, /* partial_inplace */
1484 0xffff, /* src_mask */
1485 0xffff, /* dst_mask */
1486 FALSE), /* pcrel_offset */
1488 HOWTO (AARCH64_R (TLSLE_ADD_TPREL_HI12), /* type */
1489 12, /* rightshift */
1490 2, /* size (0 = byte, 1 = short, 2 = long) */
1491 12, /* bitsize */
1492 FALSE, /* pc_relative */
1493 0, /* bitpos */
1494 complain_overflow_unsigned, /* complain_on_overflow */
1495 bfd_elf_generic_reloc, /* special_function */
1496 AARCH64_R_STR (TLSLE_ADD_TPREL_HI12), /* name */
1497 FALSE, /* partial_inplace */
1498 0xfff, /* src_mask */
1499 0xfff, /* dst_mask */
1500 FALSE), /* pcrel_offset */
1502 HOWTO (AARCH64_R (TLSLE_ADD_TPREL_LO12), /* type */
1503 0, /* rightshift */
1504 2, /* size (0 = byte, 1 = short, 2 = long) */
1505 12, /* bitsize */
1506 FALSE, /* pc_relative */
1507 0, /* bitpos */
1508 complain_overflow_unsigned, /* complain_on_overflow */
1509 bfd_elf_generic_reloc, /* special_function */
1510 AARCH64_R_STR (TLSLE_ADD_TPREL_LO12), /* name */
1511 FALSE, /* partial_inplace */
1512 0xfff, /* src_mask */
1513 0xfff, /* dst_mask */
1514 FALSE), /* pcrel_offset */
1516 HOWTO (AARCH64_R (TLSLE_ADD_TPREL_LO12_NC), /* type */
1517 0, /* rightshift */
1518 2, /* size (0 = byte, 1 = short, 2 = long) */
1519 12, /* bitsize */
1520 FALSE, /* pc_relative */
1521 0, /* bitpos */
1522 complain_overflow_dont, /* complain_on_overflow */
1523 bfd_elf_generic_reloc, /* special_function */
1524 AARCH64_R_STR (TLSLE_ADD_TPREL_LO12_NC), /* name */
1525 FALSE, /* partial_inplace */
1526 0xfff, /* src_mask */
1527 0xfff, /* dst_mask */
1528 FALSE), /* pcrel_offset */
1530 HOWTO (AARCH64_R (TLSDESC_LD_PREL19), /* type */
1531 2, /* rightshift */
1532 2, /* size (0 = byte, 1 = short, 2 = long) */
1533 19, /* bitsize */
1534 TRUE, /* pc_relative */
1535 0, /* bitpos */
1536 complain_overflow_dont, /* complain_on_overflow */
1537 bfd_elf_generic_reloc, /* special_function */
1538 AARCH64_R_STR (TLSDESC_LD_PREL19), /* name */
1539 FALSE, /* partial_inplace */
1540 0x0ffffe0, /* src_mask */
1541 0x0ffffe0, /* dst_mask */
1542 TRUE), /* pcrel_offset */
1544 HOWTO (AARCH64_R (TLSDESC_ADR_PREL21), /* type */
1545 0, /* rightshift */
1546 2, /* size (0 = byte, 1 = short, 2 = long) */
1547 21, /* bitsize */
1548 TRUE, /* pc_relative */
1549 0, /* bitpos */
1550 complain_overflow_dont, /* complain_on_overflow */
1551 bfd_elf_generic_reloc, /* special_function */
1552 AARCH64_R_STR (TLSDESC_ADR_PREL21), /* name */
1553 FALSE, /* partial_inplace */
1554 0x1fffff, /* src_mask */
1555 0x1fffff, /* dst_mask */
1556 TRUE), /* pcrel_offset */
1558 /* Get to the page for the GOT entry for the symbol
1559 (G(S) - P) using an ADRP instruction. */
1560 HOWTO (AARCH64_R (TLSDESC_ADR_PAGE21), /* type */
1561 12, /* rightshift */
1562 2, /* size (0 = byte, 1 = short, 2 = long) */
1563 21, /* bitsize */
1564 TRUE, /* pc_relative */
1565 0, /* bitpos */
1566 complain_overflow_dont, /* complain_on_overflow */
1567 bfd_elf_generic_reloc, /* special_function */
1568 AARCH64_R_STR (TLSDESC_ADR_PAGE21), /* name */
1569 FALSE, /* partial_inplace */
1570 0x1fffff, /* src_mask */
1571 0x1fffff, /* dst_mask */
1572 TRUE), /* pcrel_offset */
1574 /* LD64: GOT offset G(S) & 0xff8. */
1575 HOWTO64 (AARCH64_R (TLSDESC_LD64_LO12), /* type */
1576 3, /* rightshift */
1577 2, /* size (0 = byte, 1 = short, 2 = long) */
1578 12, /* bitsize */
1579 FALSE, /* pc_relative */
1580 0, /* bitpos */
1581 complain_overflow_dont, /* complain_on_overflow */
1582 bfd_elf_generic_reloc, /* special_function */
1583 AARCH64_R_STR (TLSDESC_LD64_LO12), /* name */
1584 FALSE, /* partial_inplace */
1585 0xff8, /* src_mask */
1586 0xff8, /* dst_mask */
1587 FALSE), /* pcrel_offset */
1589 /* LD32: GOT offset G(S) & 0xffc. */
1590 HOWTO32 (AARCH64_R (TLSDESC_LD32_LO12_NC), /* type */
1591 2, /* rightshift */
1592 2, /* size (0 = byte, 1 = short, 2 = long) */
1593 12, /* bitsize */
1594 FALSE, /* pc_relative */
1595 0, /* bitpos */
1596 complain_overflow_dont, /* complain_on_overflow */
1597 bfd_elf_generic_reloc, /* special_function */
1598 AARCH64_R_STR (TLSDESC_LD32_LO12_NC), /* name */
1599 FALSE, /* partial_inplace */
1600 0xffc, /* src_mask */
1601 0xffc, /* dst_mask */
1602 FALSE), /* pcrel_offset */
1604 /* ADD: GOT offset G(S) & 0xfff. */
1605 HOWTO (AARCH64_R (TLSDESC_ADD_LO12), /* type */
1606 0, /* rightshift */
1607 2, /* size (0 = byte, 1 = short, 2 = long) */
1608 12, /* bitsize */
1609 FALSE, /* pc_relative */
1610 0, /* bitpos */
1611 complain_overflow_dont,/* complain_on_overflow */
1612 bfd_elf_generic_reloc, /* special_function */
1613 AARCH64_R_STR (TLSDESC_ADD_LO12), /* name */
1614 FALSE, /* partial_inplace */
1615 0xfff, /* src_mask */
1616 0xfff, /* dst_mask */
1617 FALSE), /* pcrel_offset */
1619 HOWTO64 (AARCH64_R (TLSDESC_OFF_G1), /* type */
1620 16, /* rightshift */
1621 2, /* size (0 = byte, 1 = short, 2 = long) */
1622 12, /* bitsize */
1623 FALSE, /* pc_relative */
1624 0, /* bitpos */
1625 complain_overflow_unsigned, /* complain_on_overflow */
1626 bfd_elf_generic_reloc, /* special_function */
1627 AARCH64_R_STR (TLSDESC_OFF_G1), /* name */
1628 FALSE, /* partial_inplace */
1629 0xffff, /* src_mask */
1630 0xffff, /* dst_mask */
1631 FALSE), /* pcrel_offset */
1633 HOWTO64 (AARCH64_R (TLSDESC_OFF_G0_NC), /* type */
1634 0, /* rightshift */
1635 2, /* size (0 = byte, 1 = short, 2 = long) */
1636 12, /* bitsize */
1637 FALSE, /* pc_relative */
1638 0, /* bitpos */
1639 complain_overflow_dont, /* complain_on_overflow */
1640 bfd_elf_generic_reloc, /* special_function */
1641 AARCH64_R_STR (TLSDESC_OFF_G0_NC), /* name */
1642 FALSE, /* partial_inplace */
1643 0xffff, /* src_mask */
1644 0xffff, /* dst_mask */
1645 FALSE), /* pcrel_offset */
1647 HOWTO64 (AARCH64_R (TLSDESC_LDR), /* type */
1648 0, /* rightshift */
1649 2, /* size (0 = byte, 1 = short, 2 = long) */
1650 12, /* bitsize */
1651 FALSE, /* pc_relative */
1652 0, /* bitpos */
1653 complain_overflow_dont, /* complain_on_overflow */
1654 bfd_elf_generic_reloc, /* special_function */
1655 AARCH64_R_STR (TLSDESC_LDR), /* name */
1656 FALSE, /* partial_inplace */
1657 0x0, /* src_mask */
1658 0x0, /* dst_mask */
1659 FALSE), /* pcrel_offset */
1661 HOWTO64 (AARCH64_R (TLSDESC_ADD), /* type */
1662 0, /* rightshift */
1663 2, /* size (0 = byte, 1 = short, 2 = long) */
1664 12, /* bitsize */
1665 FALSE, /* pc_relative */
1666 0, /* bitpos */
1667 complain_overflow_dont, /* complain_on_overflow */
1668 bfd_elf_generic_reloc, /* special_function */
1669 AARCH64_R_STR (TLSDESC_ADD), /* name */
1670 FALSE, /* partial_inplace */
1671 0x0, /* src_mask */
1672 0x0, /* dst_mask */
1673 FALSE), /* pcrel_offset */
1675 HOWTO (AARCH64_R (TLSDESC_CALL), /* type */
1676 0, /* rightshift */
1677 2, /* size (0 = byte, 1 = short, 2 = long) */
1678 0, /* bitsize */
1679 FALSE, /* pc_relative */
1680 0, /* bitpos */
1681 complain_overflow_dont, /* complain_on_overflow */
1682 bfd_elf_generic_reloc, /* special_function */
1683 AARCH64_R_STR (TLSDESC_CALL), /* name */
1684 FALSE, /* partial_inplace */
1685 0x0, /* src_mask */
1686 0x0, /* dst_mask */
1687 FALSE), /* pcrel_offset */
1689 HOWTO (AARCH64_R (COPY), /* type */
1690 0, /* rightshift */
1691 2, /* size (0 = byte, 1 = short, 2 = long) */
1692 64, /* bitsize */
1693 FALSE, /* pc_relative */
1694 0, /* bitpos */
1695 complain_overflow_bitfield, /* complain_on_overflow */
1696 bfd_elf_generic_reloc, /* special_function */
1697 AARCH64_R_STR (COPY), /* name */
1698 TRUE, /* partial_inplace */
1699 0xffffffff, /* src_mask */
1700 0xffffffff, /* dst_mask */
1701 FALSE), /* pcrel_offset */
1703 HOWTO (AARCH64_R (GLOB_DAT), /* type */
1704 0, /* rightshift */
1705 2, /* size (0 = byte, 1 = short, 2 = long) */
1706 64, /* bitsize */
1707 FALSE, /* pc_relative */
1708 0, /* bitpos */
1709 complain_overflow_bitfield, /* complain_on_overflow */
1710 bfd_elf_generic_reloc, /* special_function */
1711 AARCH64_R_STR (GLOB_DAT), /* name */
1712 TRUE, /* partial_inplace */
1713 0xffffffff, /* src_mask */
1714 0xffffffff, /* dst_mask */
1715 FALSE), /* pcrel_offset */
1717 HOWTO (AARCH64_R (JUMP_SLOT), /* type */
1718 0, /* rightshift */
1719 2, /* size (0 = byte, 1 = short, 2 = long) */
1720 64, /* bitsize */
1721 FALSE, /* pc_relative */
1722 0, /* bitpos */
1723 complain_overflow_bitfield, /* complain_on_overflow */
1724 bfd_elf_generic_reloc, /* special_function */
1725 AARCH64_R_STR (JUMP_SLOT), /* name */
1726 TRUE, /* partial_inplace */
1727 0xffffffff, /* src_mask */
1728 0xffffffff, /* dst_mask */
1729 FALSE), /* pcrel_offset */
1731 HOWTO (AARCH64_R (RELATIVE), /* type */
1732 0, /* rightshift */
1733 2, /* size (0 = byte, 1 = short, 2 = long) */
1734 64, /* bitsize */
1735 FALSE, /* pc_relative */
1736 0, /* bitpos */
1737 complain_overflow_bitfield, /* complain_on_overflow */
1738 bfd_elf_generic_reloc, /* special_function */
1739 AARCH64_R_STR (RELATIVE), /* name */
1740 TRUE, /* partial_inplace */
1741 ALL_ONES, /* src_mask */
1742 ALL_ONES, /* dst_mask */
1743 FALSE), /* pcrel_offset */
1745 HOWTO (AARCH64_R (TLS_DTPMOD), /* type */
1746 0, /* rightshift */
1747 2, /* size (0 = byte, 1 = short, 2 = long) */
1748 64, /* bitsize */
1749 FALSE, /* pc_relative */
1750 0, /* bitpos */
1751 complain_overflow_dont, /* complain_on_overflow */
1752 bfd_elf_generic_reloc, /* special_function */
1753 #if ARCH_SIZE == 64
1754 AARCH64_R_STR (TLS_DTPMOD64), /* name */
1755 #else
1756 AARCH64_R_STR (TLS_DTPMOD), /* name */
1757 #endif
1758 FALSE, /* partial_inplace */
1759 0, /* src_mask */
1760 ALL_ONES, /* dst_mask */
1761 FALSE), /* pc_reloffset */
1763 HOWTO (AARCH64_R (TLS_DTPREL), /* type */
1764 0, /* rightshift */
1765 2, /* size (0 = byte, 1 = short, 2 = long) */
1766 64, /* bitsize */
1767 FALSE, /* pc_relative */
1768 0, /* bitpos */
1769 complain_overflow_dont, /* complain_on_overflow */
1770 bfd_elf_generic_reloc, /* special_function */
1771 #if ARCH_SIZE == 64
1772 AARCH64_R_STR (TLS_DTPREL64), /* name */
1773 #else
1774 AARCH64_R_STR (TLS_DTPREL), /* name */
1775 #endif
1776 FALSE, /* partial_inplace */
1777 0, /* src_mask */
1778 ALL_ONES, /* dst_mask */
1779 FALSE), /* pcrel_offset */
1781 HOWTO (AARCH64_R (TLS_TPREL), /* type */
1782 0, /* rightshift */
1783 2, /* size (0 = byte, 1 = short, 2 = long) */
1784 64, /* bitsize */
1785 FALSE, /* pc_relative */
1786 0, /* bitpos */
1787 complain_overflow_dont, /* complain_on_overflow */
1788 bfd_elf_generic_reloc, /* special_function */
1789 #if ARCH_SIZE == 64
1790 AARCH64_R_STR (TLS_TPREL64), /* name */
1791 #else
1792 AARCH64_R_STR (TLS_TPREL), /* name */
1793 #endif
1794 FALSE, /* partial_inplace */
1795 0, /* src_mask */
1796 ALL_ONES, /* dst_mask */
1797 FALSE), /* pcrel_offset */
1799 HOWTO (AARCH64_R (TLSDESC), /* type */
1800 0, /* rightshift */
1801 2, /* size (0 = byte, 1 = short, 2 = long) */
1802 64, /* bitsize */
1803 FALSE, /* pc_relative */
1804 0, /* bitpos */
1805 complain_overflow_dont, /* complain_on_overflow */
1806 bfd_elf_generic_reloc, /* special_function */
1807 AARCH64_R_STR (TLSDESC), /* name */
1808 FALSE, /* partial_inplace */
1809 0, /* src_mask */
1810 ALL_ONES, /* dst_mask */
1811 FALSE), /* pcrel_offset */
1813 HOWTO (AARCH64_R (IRELATIVE), /* type */
1814 0, /* rightshift */
1815 2, /* size (0 = byte, 1 = short, 2 = long) */
1816 64, /* bitsize */
1817 FALSE, /* pc_relative */
1818 0, /* bitpos */
1819 complain_overflow_bitfield, /* complain_on_overflow */
1820 bfd_elf_generic_reloc, /* special_function */
1821 AARCH64_R_STR (IRELATIVE), /* name */
1822 FALSE, /* partial_inplace */
1823 0, /* src_mask */
1824 ALL_ONES, /* dst_mask */
1825 FALSE), /* pcrel_offset */
1827 EMPTY_HOWTO (0),
1830 static reloc_howto_type elfNN_aarch64_howto_none =
1831 HOWTO (R_AARCH64_NONE, /* type */
1832 0, /* rightshift */
1833 3, /* size (0 = byte, 1 = short, 2 = long) */
1834 0, /* bitsize */
1835 FALSE, /* pc_relative */
1836 0, /* bitpos */
1837 complain_overflow_dont,/* complain_on_overflow */
1838 bfd_elf_generic_reloc, /* special_function */
1839 "R_AARCH64_NONE", /* name */
1840 FALSE, /* partial_inplace */
1841 0, /* src_mask */
1842 0, /* dst_mask */
1843 FALSE); /* pcrel_offset */
1845 /* Given HOWTO, return the bfd internal relocation enumerator. */
1847 static bfd_reloc_code_real_type
1848 elfNN_aarch64_bfd_reloc_from_howto (reloc_howto_type *howto)
1850 const int size
1851 = (int) ARRAY_SIZE (elfNN_aarch64_howto_table);
1852 const ptrdiff_t offset
1853 = howto - elfNN_aarch64_howto_table;
1855 if (offset > 0 && offset < size - 1)
1856 return BFD_RELOC_AARCH64_RELOC_START + offset;
1858 if (howto == &elfNN_aarch64_howto_none)
1859 return BFD_RELOC_AARCH64_NONE;
1861 return BFD_RELOC_AARCH64_RELOC_START;
1864 /* Given R_TYPE, return the bfd internal relocation enumerator. */
1866 static bfd_reloc_code_real_type
1867 elfNN_aarch64_bfd_reloc_from_type (unsigned int r_type)
1869 static bfd_boolean initialized_p = FALSE;
1870 /* Indexed by R_TYPE, values are offsets in the howto_table. */
1871 static unsigned int offsets[R_AARCH64_end];
1873 if (!initialized_p)
1875 unsigned int i;
1877 for (i = 1; i < ARRAY_SIZE (elfNN_aarch64_howto_table) - 1; ++i)
1878 if (elfNN_aarch64_howto_table[i].type != 0)
1879 offsets[elfNN_aarch64_howto_table[i].type] = i;
1881 initialized_p = TRUE;
1884 if (r_type == R_AARCH64_NONE || r_type == R_AARCH64_NULL)
1885 return BFD_RELOC_AARCH64_NONE;
1887 /* PR 17512: file: b371e70a. */
1888 if (r_type >= R_AARCH64_end)
1890 _bfd_error_handler (_("Invalid AArch64 reloc number: %d"), r_type);
1891 bfd_set_error (bfd_error_bad_value);
1892 return BFD_RELOC_AARCH64_NONE;
1895 return BFD_RELOC_AARCH64_RELOC_START + offsets[r_type];
1898 struct elf_aarch64_reloc_map
1900 bfd_reloc_code_real_type from;
1901 bfd_reloc_code_real_type to;
1904 /* Map bfd generic reloc to AArch64-specific reloc. */
1905 static const struct elf_aarch64_reloc_map elf_aarch64_reloc_map[] =
1907 {BFD_RELOC_NONE, BFD_RELOC_AARCH64_NONE},
1909 /* Basic data relocations. */
1910 {BFD_RELOC_CTOR, BFD_RELOC_AARCH64_NN},
1911 {BFD_RELOC_64, BFD_RELOC_AARCH64_64},
1912 {BFD_RELOC_32, BFD_RELOC_AARCH64_32},
1913 {BFD_RELOC_16, BFD_RELOC_AARCH64_16},
1914 {BFD_RELOC_64_PCREL, BFD_RELOC_AARCH64_64_PCREL},
1915 {BFD_RELOC_32_PCREL, BFD_RELOC_AARCH64_32_PCREL},
1916 {BFD_RELOC_16_PCREL, BFD_RELOC_AARCH64_16_PCREL},
1919 /* Given the bfd internal relocation enumerator in CODE, return the
1920 corresponding howto entry. */
1922 static reloc_howto_type *
1923 elfNN_aarch64_howto_from_bfd_reloc (bfd_reloc_code_real_type code)
1925 unsigned int i;
1927 /* Convert bfd generic reloc to AArch64-specific reloc. */
1928 if (code < BFD_RELOC_AARCH64_RELOC_START
1929 || code > BFD_RELOC_AARCH64_RELOC_END)
1930 for (i = 0; i < ARRAY_SIZE (elf_aarch64_reloc_map); i++)
1931 if (elf_aarch64_reloc_map[i].from == code)
1933 code = elf_aarch64_reloc_map[i].to;
1934 break;
1937 if (code > BFD_RELOC_AARCH64_RELOC_START
1938 && code < BFD_RELOC_AARCH64_RELOC_END)
1939 if (elfNN_aarch64_howto_table[code - BFD_RELOC_AARCH64_RELOC_START].type)
1940 return &elfNN_aarch64_howto_table[code - BFD_RELOC_AARCH64_RELOC_START];
1942 if (code == BFD_RELOC_AARCH64_NONE)
1943 return &elfNN_aarch64_howto_none;
1945 return NULL;
1948 static reloc_howto_type *
1949 elfNN_aarch64_howto_from_type (unsigned int r_type)
1951 bfd_reloc_code_real_type val;
1952 reloc_howto_type *howto;
1954 #if ARCH_SIZE == 32
1955 if (r_type > 256)
1957 bfd_set_error (bfd_error_bad_value);
1958 return NULL;
1960 #endif
1962 if (r_type == R_AARCH64_NONE)
1963 return &elfNN_aarch64_howto_none;
1965 val = elfNN_aarch64_bfd_reloc_from_type (r_type);
1966 howto = elfNN_aarch64_howto_from_bfd_reloc (val);
1968 if (howto != NULL)
1969 return howto;
1971 bfd_set_error (bfd_error_bad_value);
1972 return NULL;
1975 static void
1976 elfNN_aarch64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc,
1977 Elf_Internal_Rela *elf_reloc)
1979 unsigned int r_type;
1981 r_type = ELFNN_R_TYPE (elf_reloc->r_info);
1982 bfd_reloc->howto = elfNN_aarch64_howto_from_type (r_type);
1985 static reloc_howto_type *
1986 elfNN_aarch64_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1987 bfd_reloc_code_real_type code)
1989 reloc_howto_type *howto = elfNN_aarch64_howto_from_bfd_reloc (code);
1991 if (howto != NULL)
1992 return howto;
1994 bfd_set_error (bfd_error_bad_value);
1995 return NULL;
1998 static reloc_howto_type *
1999 elfNN_aarch64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2000 const char *r_name)
2002 unsigned int i;
2004 for (i = 1; i < ARRAY_SIZE (elfNN_aarch64_howto_table) - 1; ++i)
2005 if (elfNN_aarch64_howto_table[i].name != NULL
2006 && strcasecmp (elfNN_aarch64_howto_table[i].name, r_name) == 0)
2007 return &elfNN_aarch64_howto_table[i];
2009 return NULL;
2012 #define TARGET_LITTLE_SYM aarch64_elfNN_le_vec
2013 #define TARGET_LITTLE_NAME "elfNN-littleaarch64"
2014 #define TARGET_BIG_SYM aarch64_elfNN_be_vec
2015 #define TARGET_BIG_NAME "elfNN-bigaarch64"
2017 /* The linker script knows the section names for placement.
2018 The entry_names are used to do simple name mangling on the stubs.
2019 Given a function name, and its type, the stub can be found. The
2020 name can be changed. The only requirement is the %s be present. */
2021 #define STUB_ENTRY_NAME "__%s_veneer"
2023 /* The name of the dynamic interpreter. This is put in the .interp
2024 section. */
2025 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
2027 #define AARCH64_MAX_FWD_BRANCH_OFFSET \
2028 (((1 << 25) - 1) << 2)
2029 #define AARCH64_MAX_BWD_BRANCH_OFFSET \
2030 (-((1 << 25) << 2))
2032 #define AARCH64_MAX_ADRP_IMM ((1 << 20) - 1)
2033 #define AARCH64_MIN_ADRP_IMM (-(1 << 20))
2035 static int
2036 aarch64_valid_for_adrp_p (bfd_vma value, bfd_vma place)
2038 bfd_signed_vma offset = (bfd_signed_vma) (PG (value) - PG (place)) >> 12;
2039 return offset <= AARCH64_MAX_ADRP_IMM && offset >= AARCH64_MIN_ADRP_IMM;
2042 static int
2043 aarch64_valid_branch_p (bfd_vma value, bfd_vma place)
2045 bfd_signed_vma offset = (bfd_signed_vma) (value - place);
2046 return (offset <= AARCH64_MAX_FWD_BRANCH_OFFSET
2047 && offset >= AARCH64_MAX_BWD_BRANCH_OFFSET);
2050 static const uint32_t aarch64_adrp_branch_stub [] =
2052 0x90000010, /* adrp ip0, X */
2053 /* R_AARCH64_ADR_HI21_PCREL(X) */
2054 0x91000210, /* add ip0, ip0, :lo12:X */
2055 /* R_AARCH64_ADD_ABS_LO12_NC(X) */
2056 0xd61f0200, /* br ip0 */
2059 static const uint32_t aarch64_long_branch_stub[] =
2061 #if ARCH_SIZE == 64
2062 0x58000090, /* ldr ip0, 1f */
2063 #else
2064 0x18000090, /* ldr wip0, 1f */
2065 #endif
2066 0x10000011, /* adr ip1, #0 */
2067 0x8b110210, /* add ip0, ip0, ip1 */
2068 0xd61f0200, /* br ip0 */
2069 0x00000000, /* 1: .xword or .word
2070 R_AARCH64_PRELNN(X) + 12
2072 0x00000000,
2075 static const uint32_t aarch64_erratum_835769_stub[] =
2077 0x00000000, /* Placeholder for multiply accumulate. */
2078 0x14000000, /* b <label> */
2081 static const uint32_t aarch64_erratum_843419_stub[] =
2083 0x00000000, /* Placeholder for LDR instruction. */
2084 0x14000000, /* b <label> */
2087 /* Section name for stubs is the associated section name plus this
2088 string. */
2089 #define STUB_SUFFIX ".stub"
2091 enum elf_aarch64_stub_type
2093 aarch64_stub_none,
2094 aarch64_stub_adrp_branch,
2095 aarch64_stub_long_branch,
2096 aarch64_stub_erratum_835769_veneer,
2097 aarch64_stub_erratum_843419_veneer,
2100 struct elf_aarch64_stub_hash_entry
2102 /* Base hash table entry structure. */
2103 struct bfd_hash_entry root;
2105 /* The stub section. */
2106 asection *stub_sec;
2108 /* Offset within stub_sec of the beginning of this stub. */
2109 bfd_vma stub_offset;
2111 /* Given the symbol's value and its section we can determine its final
2112 value when building the stubs (so the stub knows where to jump). */
2113 bfd_vma target_value;
2114 asection *target_section;
2116 enum elf_aarch64_stub_type stub_type;
2118 /* The symbol table entry, if any, that this was derived from. */
2119 struct elf_aarch64_link_hash_entry *h;
2121 /* Destination symbol type */
2122 unsigned char st_type;
2124 /* Where this stub is being called from, or, in the case of combined
2125 stub sections, the first input section in the group. */
2126 asection *id_sec;
2128 /* The name for the local symbol at the start of this stub. The
2129 stub name in the hash table has to be unique; this does not, so
2130 it can be friendlier. */
2131 char *output_name;
2133 /* The instruction which caused this stub to be generated (only valid for
2134 erratum 835769 workaround stubs at present). */
2135 uint32_t veneered_insn;
2137 /* In an erratum 843419 workaround stub, the ADRP instruction offset. */
2138 bfd_vma adrp_offset;
2141 /* Used to build a map of a section. This is required for mixed-endian
2142 code/data. */
2144 typedef struct elf_elf_section_map
2146 bfd_vma vma;
2147 char type;
2149 elf_aarch64_section_map;
2152 typedef struct _aarch64_elf_section_data
2154 struct bfd_elf_section_data elf;
2155 unsigned int mapcount;
2156 unsigned int mapsize;
2157 elf_aarch64_section_map *map;
2159 _aarch64_elf_section_data;
2161 #define elf_aarch64_section_data(sec) \
2162 ((_aarch64_elf_section_data *) elf_section_data (sec))
2164 /* The size of the thread control block which is defined to be two pointers. */
2165 #define TCB_SIZE (ARCH_SIZE/8)*2
2167 struct elf_aarch64_local_symbol
2169 unsigned int got_type;
2170 bfd_signed_vma got_refcount;
2171 bfd_vma got_offset;
2173 /* Offset of the GOTPLT entry reserved for the TLS descriptor. The
2174 offset is from the end of the jump table and reserved entries
2175 within the PLTGOT.
2177 The magic value (bfd_vma) -1 indicates that an offset has not be
2178 allocated. */
2179 bfd_vma tlsdesc_got_jump_table_offset;
2182 struct elf_aarch64_obj_tdata
2184 struct elf_obj_tdata root;
2186 /* local symbol descriptors */
2187 struct elf_aarch64_local_symbol *locals;
2189 /* Zero to warn when linking objects with incompatible enum sizes. */
2190 int no_enum_size_warning;
2192 /* Zero to warn when linking objects with incompatible wchar_t sizes. */
2193 int no_wchar_size_warning;
2196 #define elf_aarch64_tdata(bfd) \
2197 ((struct elf_aarch64_obj_tdata *) (bfd)->tdata.any)
2199 #define elf_aarch64_locals(bfd) (elf_aarch64_tdata (bfd)->locals)
2201 #define is_aarch64_elf(bfd) \
2202 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2203 && elf_tdata (bfd) != NULL \
2204 && elf_object_id (bfd) == AARCH64_ELF_DATA)
2206 static bfd_boolean
2207 elfNN_aarch64_mkobject (bfd *abfd)
2209 return bfd_elf_allocate_object (abfd, sizeof (struct elf_aarch64_obj_tdata),
2210 AARCH64_ELF_DATA);
2213 #define elf_aarch64_hash_entry(ent) \
2214 ((struct elf_aarch64_link_hash_entry *)(ent))
2216 #define GOT_UNKNOWN 0
2217 #define GOT_NORMAL 1
2218 #define GOT_TLS_GD 2
2219 #define GOT_TLS_IE 4
2220 #define GOT_TLSDESC_GD 8
2222 #define GOT_TLS_GD_ANY_P(type) ((type & GOT_TLS_GD) || (type & GOT_TLSDESC_GD))
2224 /* AArch64 ELF linker hash entry. */
2225 struct elf_aarch64_link_hash_entry
2227 struct elf_link_hash_entry root;
2229 /* Track dynamic relocs copied for this symbol. */
2230 struct elf_dyn_relocs *dyn_relocs;
2232 /* Since PLT entries have variable size, we need to record the
2233 index into .got.plt instead of recomputing it from the PLT
2234 offset. */
2235 bfd_signed_vma plt_got_offset;
2237 /* Bit mask representing the type of GOT entry(s) if any required by
2238 this symbol. */
2239 unsigned int got_type;
2241 /* A pointer to the most recently used stub hash entry against this
2242 symbol. */
2243 struct elf_aarch64_stub_hash_entry *stub_cache;
2245 /* Offset of the GOTPLT entry reserved for the TLS descriptor. The offset
2246 is from the end of the jump table and reserved entries within the PLTGOT.
2248 The magic value (bfd_vma) -1 indicates that an offset has not
2249 be allocated. */
2250 bfd_vma tlsdesc_got_jump_table_offset;
2253 static unsigned int
2254 elfNN_aarch64_symbol_got_type (struct elf_link_hash_entry *h,
2255 bfd *abfd,
2256 unsigned long r_symndx)
2258 if (h)
2259 return elf_aarch64_hash_entry (h)->got_type;
2261 if (! elf_aarch64_locals (abfd))
2262 return GOT_UNKNOWN;
2264 return elf_aarch64_locals (abfd)[r_symndx].got_type;
2267 /* Get the AArch64 elf linker hash table from a link_info structure. */
2268 #define elf_aarch64_hash_table(info) \
2269 ((struct elf_aarch64_link_hash_table *) ((info)->hash))
2271 #define aarch64_stub_hash_lookup(table, string, create, copy) \
2272 ((struct elf_aarch64_stub_hash_entry *) \
2273 bfd_hash_lookup ((table), (string), (create), (copy)))
2275 /* AArch64 ELF linker hash table. */
2276 struct elf_aarch64_link_hash_table
2278 /* The main hash table. */
2279 struct elf_link_hash_table root;
2281 /* Nonzero to force PIC branch veneers. */
2282 int pic_veneer;
2284 /* Fix erratum 835769. */
2285 int fix_erratum_835769;
2287 /* Fix erratum 843419. */
2288 int fix_erratum_843419;
2290 /* Enable ADRP->ADR rewrite for erratum 843419 workaround. */
2291 int fix_erratum_843419_adr;
2293 /* Don't apply link-time values for dynamic relocations. */
2294 int no_apply_dynamic_relocs;
2296 /* The number of bytes in the initial entry in the PLT. */
2297 bfd_size_type plt_header_size;
2299 /* The number of bytes in the subsequent PLT etries. */
2300 bfd_size_type plt_entry_size;
2302 /* Small local sym cache. */
2303 struct sym_cache sym_cache;
2305 /* For convenience in allocate_dynrelocs. */
2306 bfd *obfd;
2308 /* The amount of space used by the reserved portion of the sgotplt
2309 section, plus whatever space is used by the jump slots. */
2310 bfd_vma sgotplt_jump_table_size;
2312 /* The stub hash table. */
2313 struct bfd_hash_table stub_hash_table;
2315 /* Linker stub bfd. */
2316 bfd *stub_bfd;
2318 /* Linker call-backs. */
2319 asection *(*add_stub_section) (const char *, asection *);
2320 void (*layout_sections_again) (void);
2322 /* Array to keep track of which stub sections have been created, and
2323 information on stub grouping. */
2324 struct map_stub
2326 /* This is the section to which stubs in the group will be
2327 attached. */
2328 asection *link_sec;
2329 /* The stub section. */
2330 asection *stub_sec;
2331 } *stub_group;
2333 /* Assorted information used by elfNN_aarch64_size_stubs. */
2334 unsigned int bfd_count;
2335 unsigned int top_index;
2336 asection **input_list;
2338 /* The offset into splt of the PLT entry for the TLS descriptor
2339 resolver. Special values are 0, if not necessary (or not found
2340 to be necessary yet), and -1 if needed but not determined
2341 yet. */
2342 bfd_vma tlsdesc_plt;
2344 /* The GOT offset for the lazy trampoline. Communicated to the
2345 loader via DT_TLSDESC_GOT. The magic value (bfd_vma) -1
2346 indicates an offset is not allocated. */
2347 bfd_vma dt_tlsdesc_got;
2349 /* Used by local STT_GNU_IFUNC symbols. */
2350 htab_t loc_hash_table;
2351 void * loc_hash_memory;
2354 /* Create an entry in an AArch64 ELF linker hash table. */
2356 static struct bfd_hash_entry *
2357 elfNN_aarch64_link_hash_newfunc (struct bfd_hash_entry *entry,
2358 struct bfd_hash_table *table,
2359 const char *string)
2361 struct elf_aarch64_link_hash_entry *ret =
2362 (struct elf_aarch64_link_hash_entry *) entry;
2364 /* Allocate the structure if it has not already been allocated by a
2365 subclass. */
2366 if (ret == NULL)
2367 ret = bfd_hash_allocate (table,
2368 sizeof (struct elf_aarch64_link_hash_entry));
2369 if (ret == NULL)
2370 return (struct bfd_hash_entry *) ret;
2372 /* Call the allocation method of the superclass. */
2373 ret = ((struct elf_aarch64_link_hash_entry *)
2374 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2375 table, string));
2376 if (ret != NULL)
2378 ret->dyn_relocs = NULL;
2379 ret->got_type = GOT_UNKNOWN;
2380 ret->plt_got_offset = (bfd_vma) - 1;
2381 ret->stub_cache = NULL;
2382 ret->tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
2385 return (struct bfd_hash_entry *) ret;
2388 /* Initialize an entry in the stub hash table. */
2390 static struct bfd_hash_entry *
2391 stub_hash_newfunc (struct bfd_hash_entry *entry,
2392 struct bfd_hash_table *table, const char *string)
2394 /* Allocate the structure if it has not already been allocated by a
2395 subclass. */
2396 if (entry == NULL)
2398 entry = bfd_hash_allocate (table,
2399 sizeof (struct
2400 elf_aarch64_stub_hash_entry));
2401 if (entry == NULL)
2402 return entry;
2405 /* Call the allocation method of the superclass. */
2406 entry = bfd_hash_newfunc (entry, table, string);
2407 if (entry != NULL)
2409 struct elf_aarch64_stub_hash_entry *eh;
2411 /* Initialize the local fields. */
2412 eh = (struct elf_aarch64_stub_hash_entry *) entry;
2413 eh->adrp_offset = 0;
2414 eh->stub_sec = NULL;
2415 eh->stub_offset = 0;
2416 eh->target_value = 0;
2417 eh->target_section = NULL;
2418 eh->stub_type = aarch64_stub_none;
2419 eh->h = NULL;
2420 eh->id_sec = NULL;
2423 return entry;
2426 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
2427 for local symbol so that we can handle local STT_GNU_IFUNC symbols
2428 as global symbol. We reuse indx and dynstr_index for local symbol
2429 hash since they aren't used by global symbols in this backend. */
2431 static hashval_t
2432 elfNN_aarch64_local_htab_hash (const void *ptr)
2434 struct elf_link_hash_entry *h
2435 = (struct elf_link_hash_entry *) ptr;
2436 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
2439 /* Compare local hash entries. */
2441 static int
2442 elfNN_aarch64_local_htab_eq (const void *ptr1, const void *ptr2)
2444 struct elf_link_hash_entry *h1
2445 = (struct elf_link_hash_entry *) ptr1;
2446 struct elf_link_hash_entry *h2
2447 = (struct elf_link_hash_entry *) ptr2;
2449 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
2452 /* Find and/or create a hash entry for local symbol. */
2454 static struct elf_link_hash_entry *
2455 elfNN_aarch64_get_local_sym_hash (struct elf_aarch64_link_hash_table *htab,
2456 bfd *abfd, const Elf_Internal_Rela *rel,
2457 bfd_boolean create)
2459 struct elf_aarch64_link_hash_entry e, *ret;
2460 asection *sec = abfd->sections;
2461 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
2462 ELFNN_R_SYM (rel->r_info));
2463 void **slot;
2465 e.root.indx = sec->id;
2466 e.root.dynstr_index = ELFNN_R_SYM (rel->r_info);
2467 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
2468 create ? INSERT : NO_INSERT);
2470 if (!slot)
2471 return NULL;
2473 if (*slot)
2475 ret = (struct elf_aarch64_link_hash_entry *) *slot;
2476 return &ret->root;
2479 ret = (struct elf_aarch64_link_hash_entry *)
2480 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
2481 sizeof (struct elf_aarch64_link_hash_entry));
2482 if (ret)
2484 memset (ret, 0, sizeof (*ret));
2485 ret->root.indx = sec->id;
2486 ret->root.dynstr_index = ELFNN_R_SYM (rel->r_info);
2487 ret->root.dynindx = -1;
2488 *slot = ret;
2490 return &ret->root;
2493 /* Copy the extra info we tack onto an elf_link_hash_entry. */
2495 static void
2496 elfNN_aarch64_copy_indirect_symbol (struct bfd_link_info *info,
2497 struct elf_link_hash_entry *dir,
2498 struct elf_link_hash_entry *ind)
2500 struct elf_aarch64_link_hash_entry *edir, *eind;
2502 edir = (struct elf_aarch64_link_hash_entry *) dir;
2503 eind = (struct elf_aarch64_link_hash_entry *) ind;
2505 if (eind->dyn_relocs != NULL)
2507 if (edir->dyn_relocs != NULL)
2509 struct elf_dyn_relocs **pp;
2510 struct elf_dyn_relocs *p;
2512 /* Add reloc counts against the indirect sym to the direct sym
2513 list. Merge any entries against the same section. */
2514 for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
2516 struct elf_dyn_relocs *q;
2518 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2519 if (q->sec == p->sec)
2521 q->pc_count += p->pc_count;
2522 q->count += p->count;
2523 *pp = p->next;
2524 break;
2526 if (q == NULL)
2527 pp = &p->next;
2529 *pp = edir->dyn_relocs;
2532 edir->dyn_relocs = eind->dyn_relocs;
2533 eind->dyn_relocs = NULL;
2536 if (ind->root.type == bfd_link_hash_indirect)
2538 /* Copy over PLT info. */
2539 if (dir->got.refcount <= 0)
2541 edir->got_type = eind->got_type;
2542 eind->got_type = GOT_UNKNOWN;
2546 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2549 /* Destroy an AArch64 elf linker hash table. */
2551 static void
2552 elfNN_aarch64_link_hash_table_free (bfd *obfd)
2554 struct elf_aarch64_link_hash_table *ret
2555 = (struct elf_aarch64_link_hash_table *) obfd->link.hash;
2557 if (ret->loc_hash_table)
2558 htab_delete (ret->loc_hash_table);
2559 if (ret->loc_hash_memory)
2560 objalloc_free ((struct objalloc *) ret->loc_hash_memory);
2562 bfd_hash_table_free (&ret->stub_hash_table);
2563 _bfd_elf_link_hash_table_free (obfd);
2566 /* Create an AArch64 elf linker hash table. */
2568 static struct bfd_link_hash_table *
2569 elfNN_aarch64_link_hash_table_create (bfd *abfd)
2571 struct elf_aarch64_link_hash_table *ret;
2572 bfd_size_type amt = sizeof (struct elf_aarch64_link_hash_table);
2574 ret = bfd_zmalloc (amt);
2575 if (ret == NULL)
2576 return NULL;
2578 if (!_bfd_elf_link_hash_table_init
2579 (&ret->root, abfd, elfNN_aarch64_link_hash_newfunc,
2580 sizeof (struct elf_aarch64_link_hash_entry), AARCH64_ELF_DATA))
2582 free (ret);
2583 return NULL;
2586 ret->plt_header_size = PLT_ENTRY_SIZE;
2587 ret->plt_entry_size = PLT_SMALL_ENTRY_SIZE;
2588 ret->obfd = abfd;
2589 ret->dt_tlsdesc_got = (bfd_vma) - 1;
2591 if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
2592 sizeof (struct elf_aarch64_stub_hash_entry)))
2594 _bfd_elf_link_hash_table_free (abfd);
2595 return NULL;
2598 ret->loc_hash_table = htab_try_create (1024,
2599 elfNN_aarch64_local_htab_hash,
2600 elfNN_aarch64_local_htab_eq,
2601 NULL);
2602 ret->loc_hash_memory = objalloc_create ();
2603 if (!ret->loc_hash_table || !ret->loc_hash_memory)
2605 elfNN_aarch64_link_hash_table_free (abfd);
2606 return NULL;
2608 ret->root.root.hash_table_free = elfNN_aarch64_link_hash_table_free;
2610 return &ret->root.root;
2613 /* Perform relocation R_TYPE. Returns TRUE upon success, FALSE otherwise. */
2615 static bfd_boolean
2616 aarch64_relocate (unsigned int r_type, bfd *input_bfd, asection *input_section,
2617 bfd_vma offset, bfd_vma value)
2619 reloc_howto_type *howto;
2620 bfd_vma place;
2622 howto = elfNN_aarch64_howto_from_type (r_type);
2623 place = (input_section->output_section->vma + input_section->output_offset
2624 + offset);
2626 r_type = elfNN_aarch64_bfd_reloc_from_type (r_type);
2627 value = _bfd_aarch64_elf_resolve_relocation (r_type, place, value, 0, FALSE);
2628 return _bfd_aarch64_elf_put_addend (input_bfd,
2629 input_section->contents + offset, r_type,
2630 howto, value) == bfd_reloc_ok;
2633 static enum elf_aarch64_stub_type
2634 aarch64_select_branch_stub (bfd_vma value, bfd_vma place)
2636 if (aarch64_valid_for_adrp_p (value, place))
2637 return aarch64_stub_adrp_branch;
2638 return aarch64_stub_long_branch;
2641 /* Determine the type of stub needed, if any, for a call. */
2643 static enum elf_aarch64_stub_type
2644 aarch64_type_of_stub (asection *input_sec,
2645 const Elf_Internal_Rela *rel,
2646 asection *sym_sec,
2647 unsigned char st_type,
2648 bfd_vma destination)
2650 bfd_vma location;
2651 bfd_signed_vma branch_offset;
2652 unsigned int r_type;
2653 enum elf_aarch64_stub_type stub_type = aarch64_stub_none;
2655 if (st_type != STT_FUNC
2656 && (sym_sec == input_sec))
2657 return stub_type;
2659 /* Determine where the call point is. */
2660 location = (input_sec->output_offset
2661 + input_sec->output_section->vma + rel->r_offset);
2663 branch_offset = (bfd_signed_vma) (destination - location);
2665 r_type = ELFNN_R_TYPE (rel->r_info);
2667 /* We don't want to redirect any old unconditional jump in this way,
2668 only one which is being used for a sibcall, where it is
2669 acceptable for the IP0 and IP1 registers to be clobbered. */
2670 if ((r_type == AARCH64_R (CALL26) || r_type == AARCH64_R (JUMP26))
2671 && (branch_offset > AARCH64_MAX_FWD_BRANCH_OFFSET
2672 || branch_offset < AARCH64_MAX_BWD_BRANCH_OFFSET))
2674 stub_type = aarch64_stub_long_branch;
2677 return stub_type;
2680 /* Build a name for an entry in the stub hash table. */
2682 static char *
2683 elfNN_aarch64_stub_name (const asection *input_section,
2684 const asection *sym_sec,
2685 const struct elf_aarch64_link_hash_entry *hash,
2686 const Elf_Internal_Rela *rel)
2688 char *stub_name;
2689 bfd_size_type len;
2691 if (hash)
2693 len = 8 + 1 + strlen (hash->root.root.root.string) + 1 + 16 + 1;
2694 stub_name = bfd_malloc (len);
2695 if (stub_name != NULL)
2696 snprintf (stub_name, len, "%08x_%s+%" BFD_VMA_FMT "x",
2697 (unsigned int) input_section->id,
2698 hash->root.root.root.string,
2699 rel->r_addend);
2701 else
2703 len = 8 + 1 + 8 + 1 + 8 + 1 + 16 + 1;
2704 stub_name = bfd_malloc (len);
2705 if (stub_name != NULL)
2706 snprintf (stub_name, len, "%08x_%x:%x+%" BFD_VMA_FMT "x",
2707 (unsigned int) input_section->id,
2708 (unsigned int) sym_sec->id,
2709 (unsigned int) ELFNN_R_SYM (rel->r_info),
2710 rel->r_addend);
2713 return stub_name;
2716 /* Return TRUE if symbol H should be hashed in the `.gnu.hash' section. For
2717 executable PLT slots where the executable never takes the address of those
2718 functions, the function symbols are not added to the hash table. */
2720 static bfd_boolean
2721 elf_aarch64_hash_symbol (struct elf_link_hash_entry *h)
2723 if (h->plt.offset != (bfd_vma) -1
2724 && !h->def_regular
2725 && !h->pointer_equality_needed)
2726 return FALSE;
2728 return _bfd_elf_hash_symbol (h);
2732 /* Look up an entry in the stub hash. Stub entries are cached because
2733 creating the stub name takes a bit of time. */
2735 static struct elf_aarch64_stub_hash_entry *
2736 elfNN_aarch64_get_stub_entry (const asection *input_section,
2737 const asection *sym_sec,
2738 struct elf_link_hash_entry *hash,
2739 const Elf_Internal_Rela *rel,
2740 struct elf_aarch64_link_hash_table *htab)
2742 struct elf_aarch64_stub_hash_entry *stub_entry;
2743 struct elf_aarch64_link_hash_entry *h =
2744 (struct elf_aarch64_link_hash_entry *) hash;
2745 const asection *id_sec;
2747 if ((input_section->flags & SEC_CODE) == 0)
2748 return NULL;
2750 /* If this input section is part of a group of sections sharing one
2751 stub section, then use the id of the first section in the group.
2752 Stub names need to include a section id, as there may well be
2753 more than one stub used to reach say, printf, and we need to
2754 distinguish between them. */
2755 id_sec = htab->stub_group[input_section->id].link_sec;
2757 if (h != NULL && h->stub_cache != NULL
2758 && h->stub_cache->h == h && h->stub_cache->id_sec == id_sec)
2760 stub_entry = h->stub_cache;
2762 else
2764 char *stub_name;
2766 stub_name = elfNN_aarch64_stub_name (id_sec, sym_sec, h, rel);
2767 if (stub_name == NULL)
2768 return NULL;
2770 stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table,
2771 stub_name, FALSE, FALSE);
2772 if (h != NULL)
2773 h->stub_cache = stub_entry;
2775 free (stub_name);
2778 return stub_entry;
2782 /* Create a stub section. */
2784 static asection *
2785 _bfd_aarch64_create_stub_section (asection *section,
2786 struct elf_aarch64_link_hash_table *htab)
2788 size_t namelen;
2789 bfd_size_type len;
2790 char *s_name;
2792 namelen = strlen (section->name);
2793 len = namelen + sizeof (STUB_SUFFIX);
2794 s_name = bfd_alloc (htab->stub_bfd, len);
2795 if (s_name == NULL)
2796 return NULL;
2798 memcpy (s_name, section->name, namelen);
2799 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
2800 return (*htab->add_stub_section) (s_name, section);
2804 /* Find or create a stub section for a link section.
2806 Fix or create the stub section used to collect stubs attached to
2807 the specified link section. */
2809 static asection *
2810 _bfd_aarch64_get_stub_for_link_section (asection *link_section,
2811 struct elf_aarch64_link_hash_table *htab)
2813 if (htab->stub_group[link_section->id].stub_sec == NULL)
2814 htab->stub_group[link_section->id].stub_sec
2815 = _bfd_aarch64_create_stub_section (link_section, htab);
2816 return htab->stub_group[link_section->id].stub_sec;
2820 /* Find or create a stub section in the stub group for an input
2821 section. */
2823 static asection *
2824 _bfd_aarch64_create_or_find_stub_sec (asection *section,
2825 struct elf_aarch64_link_hash_table *htab)
2827 asection *link_sec = htab->stub_group[section->id].link_sec;
2828 return _bfd_aarch64_get_stub_for_link_section (link_sec, htab);
2832 /* Add a new stub entry in the stub group associated with an input
2833 section to the stub hash. Not all fields of the new stub entry are
2834 initialised. */
2836 static struct elf_aarch64_stub_hash_entry *
2837 _bfd_aarch64_add_stub_entry_in_group (const char *stub_name,
2838 asection *section,
2839 struct elf_aarch64_link_hash_table *htab)
2841 asection *link_sec;
2842 asection *stub_sec;
2843 struct elf_aarch64_stub_hash_entry *stub_entry;
2845 link_sec = htab->stub_group[section->id].link_sec;
2846 stub_sec = _bfd_aarch64_create_or_find_stub_sec (section, htab);
2848 /* Enter this entry into the linker stub hash table. */
2849 stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
2850 TRUE, FALSE);
2851 if (stub_entry == NULL)
2853 /* xgettext:c-format */
2854 _bfd_error_handler (_("%B: cannot create stub entry %s"),
2855 section->owner, stub_name);
2856 return NULL;
2859 stub_entry->stub_sec = stub_sec;
2860 stub_entry->stub_offset = 0;
2861 stub_entry->id_sec = link_sec;
2863 return stub_entry;
2866 /* Add a new stub entry in the final stub section to the stub hash.
2867 Not all fields of the new stub entry are initialised. */
2869 static struct elf_aarch64_stub_hash_entry *
2870 _bfd_aarch64_add_stub_entry_after (const char *stub_name,
2871 asection *link_section,
2872 struct elf_aarch64_link_hash_table *htab)
2874 asection *stub_sec;
2875 struct elf_aarch64_stub_hash_entry *stub_entry;
2877 stub_sec = _bfd_aarch64_get_stub_for_link_section (link_section, htab);
2878 stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
2879 TRUE, FALSE);
2880 if (stub_entry == NULL)
2882 _bfd_error_handler (_("cannot create stub entry %s"), stub_name);
2883 return NULL;
2886 stub_entry->stub_sec = stub_sec;
2887 stub_entry->stub_offset = 0;
2888 stub_entry->id_sec = link_section;
2890 return stub_entry;
2894 static bfd_boolean
2895 aarch64_build_one_stub (struct bfd_hash_entry *gen_entry,
2896 void *in_arg ATTRIBUTE_UNUSED)
2898 struct elf_aarch64_stub_hash_entry *stub_entry;
2899 asection *stub_sec;
2900 bfd *stub_bfd;
2901 bfd_byte *loc;
2902 bfd_vma sym_value;
2903 bfd_vma veneered_insn_loc;
2904 bfd_vma veneer_entry_loc;
2905 bfd_signed_vma branch_offset = 0;
2906 unsigned int template_size;
2907 const uint32_t *template;
2908 unsigned int i;
2910 /* Massage our args to the form they really have. */
2911 stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
2913 stub_sec = stub_entry->stub_sec;
2915 /* Make a note of the offset within the stubs for this entry. */
2916 stub_entry->stub_offset = stub_sec->size;
2917 loc = stub_sec->contents + stub_entry->stub_offset;
2919 stub_bfd = stub_sec->owner;
2921 /* This is the address of the stub destination. */
2922 sym_value = (stub_entry->target_value
2923 + stub_entry->target_section->output_offset
2924 + stub_entry->target_section->output_section->vma);
2926 if (stub_entry->stub_type == aarch64_stub_long_branch)
2928 bfd_vma place = (stub_entry->stub_offset + stub_sec->output_section->vma
2929 + stub_sec->output_offset);
2931 /* See if we can relax the stub. */
2932 if (aarch64_valid_for_adrp_p (sym_value, place))
2933 stub_entry->stub_type = aarch64_select_branch_stub (sym_value, place);
2936 switch (stub_entry->stub_type)
2938 case aarch64_stub_adrp_branch:
2939 template = aarch64_adrp_branch_stub;
2940 template_size = sizeof (aarch64_adrp_branch_stub);
2941 break;
2942 case aarch64_stub_long_branch:
2943 template = aarch64_long_branch_stub;
2944 template_size = sizeof (aarch64_long_branch_stub);
2945 break;
2946 case aarch64_stub_erratum_835769_veneer:
2947 template = aarch64_erratum_835769_stub;
2948 template_size = sizeof (aarch64_erratum_835769_stub);
2949 break;
2950 case aarch64_stub_erratum_843419_veneer:
2951 template = aarch64_erratum_843419_stub;
2952 template_size = sizeof (aarch64_erratum_843419_stub);
2953 break;
2954 default:
2955 abort ();
2958 for (i = 0; i < (template_size / sizeof template[0]); i++)
2960 bfd_putl32 (template[i], loc);
2961 loc += 4;
2964 template_size = (template_size + 7) & ~7;
2965 stub_sec->size += template_size;
2967 switch (stub_entry->stub_type)
2969 case aarch64_stub_adrp_branch:
2970 if (!aarch64_relocate (AARCH64_R (ADR_PREL_PG_HI21), stub_bfd, stub_sec,
2971 stub_entry->stub_offset, sym_value))
2972 /* The stub would not have been relaxed if the offset was out
2973 of range. */
2974 BFD_FAIL ();
2976 if (!aarch64_relocate (AARCH64_R (ADD_ABS_LO12_NC), stub_bfd, stub_sec,
2977 stub_entry->stub_offset + 4, sym_value))
2978 BFD_FAIL ();
2979 break;
2981 case aarch64_stub_long_branch:
2982 /* We want the value relative to the address 12 bytes back from the
2983 value itself. */
2984 if (!aarch64_relocate (AARCH64_R (PRELNN), stub_bfd, stub_sec,
2985 stub_entry->stub_offset + 16, sym_value + 12))
2986 BFD_FAIL ();
2987 break;
2989 case aarch64_stub_erratum_835769_veneer:
2990 veneered_insn_loc = stub_entry->target_section->output_section->vma
2991 + stub_entry->target_section->output_offset
2992 + stub_entry->target_value;
2993 veneer_entry_loc = stub_entry->stub_sec->output_section->vma
2994 + stub_entry->stub_sec->output_offset
2995 + stub_entry->stub_offset;
2996 branch_offset = veneered_insn_loc - veneer_entry_loc;
2997 branch_offset >>= 2;
2998 branch_offset &= 0x3ffffff;
2999 bfd_putl32 (stub_entry->veneered_insn,
3000 stub_sec->contents + stub_entry->stub_offset);
3001 bfd_putl32 (template[1] | branch_offset,
3002 stub_sec->contents + stub_entry->stub_offset + 4);
3003 break;
3005 case aarch64_stub_erratum_843419_veneer:
3006 if (!aarch64_relocate (AARCH64_R (JUMP26), stub_bfd, stub_sec,
3007 stub_entry->stub_offset + 4, sym_value + 4))
3008 BFD_FAIL ();
3009 break;
3011 default:
3012 abort ();
3015 return TRUE;
3018 /* As above, but don't actually build the stub. Just bump offset so
3019 we know stub section sizes. */
3021 static bfd_boolean
3022 aarch64_size_one_stub (struct bfd_hash_entry *gen_entry,
3023 void *in_arg ATTRIBUTE_UNUSED)
3025 struct elf_aarch64_stub_hash_entry *stub_entry;
3026 int size;
3028 /* Massage our args to the form they really have. */
3029 stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
3031 switch (stub_entry->stub_type)
3033 case aarch64_stub_adrp_branch:
3034 size = sizeof (aarch64_adrp_branch_stub);
3035 break;
3036 case aarch64_stub_long_branch:
3037 size = sizeof (aarch64_long_branch_stub);
3038 break;
3039 case aarch64_stub_erratum_835769_veneer:
3040 size = sizeof (aarch64_erratum_835769_stub);
3041 break;
3042 case aarch64_stub_erratum_843419_veneer:
3043 size = sizeof (aarch64_erratum_843419_stub);
3044 break;
3045 default:
3046 abort ();
3049 size = (size + 7) & ~7;
3050 stub_entry->stub_sec->size += size;
3051 return TRUE;
3054 /* External entry points for sizing and building linker stubs. */
3056 /* Set up various things so that we can make a list of input sections
3057 for each output section included in the link. Returns -1 on error,
3058 0 when no stubs will be needed, and 1 on success. */
3061 elfNN_aarch64_setup_section_lists (bfd *output_bfd,
3062 struct bfd_link_info *info)
3064 bfd *input_bfd;
3065 unsigned int bfd_count;
3066 unsigned int top_id, top_index;
3067 asection *section;
3068 asection **input_list, **list;
3069 bfd_size_type amt;
3070 struct elf_aarch64_link_hash_table *htab =
3071 elf_aarch64_hash_table (info);
3073 if (!is_elf_hash_table (htab))
3074 return 0;
3076 /* Count the number of input BFDs and find the top input section id. */
3077 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
3078 input_bfd != NULL; input_bfd = input_bfd->link.next)
3080 bfd_count += 1;
3081 for (section = input_bfd->sections;
3082 section != NULL; section = section->next)
3084 if (top_id < section->id)
3085 top_id = section->id;
3088 htab->bfd_count = bfd_count;
3090 amt = sizeof (struct map_stub) * (top_id + 1);
3091 htab->stub_group = bfd_zmalloc (amt);
3092 if (htab->stub_group == NULL)
3093 return -1;
3095 /* We can't use output_bfd->section_count here to find the top output
3096 section index as some sections may have been removed, and
3097 _bfd_strip_section_from_output doesn't renumber the indices. */
3098 for (section = output_bfd->sections, top_index = 0;
3099 section != NULL; section = section->next)
3101 if (top_index < section->index)
3102 top_index = section->index;
3105 htab->top_index = top_index;
3106 amt = sizeof (asection *) * (top_index + 1);
3107 input_list = bfd_malloc (amt);
3108 htab->input_list = input_list;
3109 if (input_list == NULL)
3110 return -1;
3112 /* For sections we aren't interested in, mark their entries with a
3113 value we can check later. */
3114 list = input_list + top_index;
3116 *list = bfd_abs_section_ptr;
3117 while (list-- != input_list);
3119 for (section = output_bfd->sections;
3120 section != NULL; section = section->next)
3122 if ((section->flags & SEC_CODE) != 0)
3123 input_list[section->index] = NULL;
3126 return 1;
3129 /* Used by elfNN_aarch64_next_input_section and group_sections. */
3130 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3132 /* The linker repeatedly calls this function for each input section,
3133 in the order that input sections are linked into output sections.
3134 Build lists of input sections to determine groupings between which
3135 we may insert linker stubs. */
3137 void
3138 elfNN_aarch64_next_input_section (struct bfd_link_info *info, asection *isec)
3140 struct elf_aarch64_link_hash_table *htab =
3141 elf_aarch64_hash_table (info);
3143 if (isec->output_section->index <= htab->top_index)
3145 asection **list = htab->input_list + isec->output_section->index;
3147 if (*list != bfd_abs_section_ptr)
3149 /* Steal the link_sec pointer for our list. */
3150 /* This happens to make the list in reverse order,
3151 which is what we want. */
3152 PREV_SEC (isec) = *list;
3153 *list = isec;
3158 /* See whether we can group stub sections together. Grouping stub
3159 sections may result in fewer stubs. More importantly, we need to
3160 put all .init* and .fini* stubs at the beginning of the .init or
3161 .fini output sections respectively, because glibc splits the
3162 _init and _fini functions into multiple parts. Putting a stub in
3163 the middle of a function is not a good idea. */
3165 static void
3166 group_sections (struct elf_aarch64_link_hash_table *htab,
3167 bfd_size_type stub_group_size,
3168 bfd_boolean stubs_always_before_branch)
3170 asection **list = htab->input_list + htab->top_index;
3174 asection *tail = *list;
3176 if (tail == bfd_abs_section_ptr)
3177 continue;
3179 while (tail != NULL)
3181 asection *curr;
3182 asection *prev;
3183 bfd_size_type total;
3185 curr = tail;
3186 total = tail->size;
3187 while ((prev = PREV_SEC (curr)) != NULL
3188 && ((total += curr->output_offset - prev->output_offset)
3189 < stub_group_size))
3190 curr = prev;
3192 /* OK, the size from the start of CURR to the end is less
3193 than stub_group_size and thus can be handled by one stub
3194 section. (Or the tail section is itself larger than
3195 stub_group_size, in which case we may be toast.)
3196 We should really be keeping track of the total size of
3197 stubs added here, as stubs contribute to the final output
3198 section size. */
3201 prev = PREV_SEC (tail);
3202 /* Set up this stub group. */
3203 htab->stub_group[tail->id].link_sec = curr;
3205 while (tail != curr && (tail = prev) != NULL);
3207 /* But wait, there's more! Input sections up to stub_group_size
3208 bytes before the stub section can be handled by it too. */
3209 if (!stubs_always_before_branch)
3211 total = 0;
3212 while (prev != NULL
3213 && ((total += tail->output_offset - prev->output_offset)
3214 < stub_group_size))
3216 tail = prev;
3217 prev = PREV_SEC (tail);
3218 htab->stub_group[tail->id].link_sec = curr;
3221 tail = prev;
3224 while (list-- != htab->input_list);
3226 free (htab->input_list);
3229 #undef PREV_SEC
3231 #define AARCH64_BITS(x, pos, n) (((x) >> (pos)) & ((1 << (n)) - 1))
3233 #define AARCH64_RT(insn) AARCH64_BITS (insn, 0, 5)
3234 #define AARCH64_RT2(insn) AARCH64_BITS (insn, 10, 5)
3235 #define AARCH64_RA(insn) AARCH64_BITS (insn, 10, 5)
3236 #define AARCH64_RD(insn) AARCH64_BITS (insn, 0, 5)
3237 #define AARCH64_RN(insn) AARCH64_BITS (insn, 5, 5)
3238 #define AARCH64_RM(insn) AARCH64_BITS (insn, 16, 5)
3240 #define AARCH64_MAC(insn) (((insn) & 0xff000000) == 0x9b000000)
3241 #define AARCH64_BIT(insn, n) AARCH64_BITS (insn, n, 1)
3242 #define AARCH64_OP31(insn) AARCH64_BITS (insn, 21, 3)
3243 #define AARCH64_ZR 0x1f
3245 /* All ld/st ops. See C4-182 of the ARM ARM. The encoding space for
3246 LD_PCREL, LDST_RO, LDST_UI and LDST_UIMM cover prefetch ops. */
3248 #define AARCH64_LD(insn) (AARCH64_BIT (insn, 22) == 1)
3249 #define AARCH64_LDST(insn) (((insn) & 0x0a000000) == 0x08000000)
3250 #define AARCH64_LDST_EX(insn) (((insn) & 0x3f000000) == 0x08000000)
3251 #define AARCH64_LDST_PCREL(insn) (((insn) & 0x3b000000) == 0x18000000)
3252 #define AARCH64_LDST_NAP(insn) (((insn) & 0x3b800000) == 0x28000000)
3253 #define AARCH64_LDSTP_PI(insn) (((insn) & 0x3b800000) == 0x28800000)
3254 #define AARCH64_LDSTP_O(insn) (((insn) & 0x3b800000) == 0x29000000)
3255 #define AARCH64_LDSTP_PRE(insn) (((insn) & 0x3b800000) == 0x29800000)
3256 #define AARCH64_LDST_UI(insn) (((insn) & 0x3b200c00) == 0x38000000)
3257 #define AARCH64_LDST_PIIMM(insn) (((insn) & 0x3b200c00) == 0x38000400)
3258 #define AARCH64_LDST_U(insn) (((insn) & 0x3b200c00) == 0x38000800)
3259 #define AARCH64_LDST_PREIMM(insn) (((insn) & 0x3b200c00) == 0x38000c00)
3260 #define AARCH64_LDST_RO(insn) (((insn) & 0x3b200c00) == 0x38200800)
3261 #define AARCH64_LDST_UIMM(insn) (((insn) & 0x3b000000) == 0x39000000)
3262 #define AARCH64_LDST_SIMD_M(insn) (((insn) & 0xbfbf0000) == 0x0c000000)
3263 #define AARCH64_LDST_SIMD_M_PI(insn) (((insn) & 0xbfa00000) == 0x0c800000)
3264 #define AARCH64_LDST_SIMD_S(insn) (((insn) & 0xbf9f0000) == 0x0d000000)
3265 #define AARCH64_LDST_SIMD_S_PI(insn) (((insn) & 0xbf800000) == 0x0d800000)
3267 /* Classify an INSN if it is indeed a load/store.
3269 Return TRUE if INSN is a LD/ST instruction otherwise return FALSE.
3271 For scalar LD/ST instructions PAIR is FALSE, RT is returned and RT2
3272 is set equal to RT.
3274 For LD/ST pair instructions PAIR is TRUE, RT and RT2 are returned. */
3276 static bfd_boolean
3277 aarch64_mem_op_p (uint32_t insn, unsigned int *rt, unsigned int *rt2,
3278 bfd_boolean *pair, bfd_boolean *load)
3280 uint32_t opcode;
3281 unsigned int r;
3282 uint32_t opc = 0;
3283 uint32_t v = 0;
3284 uint32_t opc_v = 0;
3286 /* Bail out quickly if INSN doesn't fall into the load-store
3287 encoding space. */
3288 if (!AARCH64_LDST (insn))
3289 return FALSE;
3291 *pair = FALSE;
3292 *load = FALSE;
3293 if (AARCH64_LDST_EX (insn))
3295 *rt = AARCH64_RT (insn);
3296 *rt2 = *rt;
3297 if (AARCH64_BIT (insn, 21) == 1)
3299 *pair = TRUE;
3300 *rt2 = AARCH64_RT2 (insn);
3302 *load = AARCH64_LD (insn);
3303 return TRUE;
3305 else if (AARCH64_LDST_NAP (insn)
3306 || AARCH64_LDSTP_PI (insn)
3307 || AARCH64_LDSTP_O (insn)
3308 || AARCH64_LDSTP_PRE (insn))
3310 *pair = TRUE;
3311 *rt = AARCH64_RT (insn);
3312 *rt2 = AARCH64_RT2 (insn);
3313 *load = AARCH64_LD (insn);
3314 return TRUE;
3316 else if (AARCH64_LDST_PCREL (insn)
3317 || AARCH64_LDST_UI (insn)
3318 || AARCH64_LDST_PIIMM (insn)
3319 || AARCH64_LDST_U (insn)
3320 || AARCH64_LDST_PREIMM (insn)
3321 || AARCH64_LDST_RO (insn)
3322 || AARCH64_LDST_UIMM (insn))
3324 *rt = AARCH64_RT (insn);
3325 *rt2 = *rt;
3326 if (AARCH64_LDST_PCREL (insn))
3327 *load = TRUE;
3328 opc = AARCH64_BITS (insn, 22, 2);
3329 v = AARCH64_BIT (insn, 26);
3330 opc_v = opc | (v << 2);
3331 *load = (opc_v == 1 || opc_v == 2 || opc_v == 3
3332 || opc_v == 5 || opc_v == 7);
3333 return TRUE;
3335 else if (AARCH64_LDST_SIMD_M (insn)
3336 || AARCH64_LDST_SIMD_M_PI (insn))
3338 *rt = AARCH64_RT (insn);
3339 *load = AARCH64_BIT (insn, 22);
3340 opcode = (insn >> 12) & 0xf;
3341 switch (opcode)
3343 case 0:
3344 case 2:
3345 *rt2 = *rt + 3;
3346 break;
3348 case 4:
3349 case 6:
3350 *rt2 = *rt + 2;
3351 break;
3353 case 7:
3354 *rt2 = *rt;
3355 break;
3357 case 8:
3358 case 10:
3359 *rt2 = *rt + 1;
3360 break;
3362 default:
3363 return FALSE;
3365 return TRUE;
3367 else if (AARCH64_LDST_SIMD_S (insn)
3368 || AARCH64_LDST_SIMD_S_PI (insn))
3370 *rt = AARCH64_RT (insn);
3371 r = (insn >> 21) & 1;
3372 *load = AARCH64_BIT (insn, 22);
3373 opcode = (insn >> 13) & 0x7;
3374 switch (opcode)
3376 case 0:
3377 case 2:
3378 case 4:
3379 *rt2 = *rt + r;
3380 break;
3382 case 1:
3383 case 3:
3384 case 5:
3385 *rt2 = *rt + (r == 0 ? 2 : 3);
3386 break;
3388 case 6:
3389 *rt2 = *rt + r;
3390 break;
3392 case 7:
3393 *rt2 = *rt + (r == 0 ? 2 : 3);
3394 break;
3396 default:
3397 return FALSE;
3399 return TRUE;
3402 return FALSE;
3405 /* Return TRUE if INSN is multiply-accumulate. */
3407 static bfd_boolean
3408 aarch64_mlxl_p (uint32_t insn)
3410 uint32_t op31 = AARCH64_OP31 (insn);
3412 if (AARCH64_MAC (insn)
3413 && (op31 == 0 || op31 == 1 || op31 == 5)
3414 /* Exclude MUL instructions which are encoded as a multiple accumulate
3415 with RA = XZR. */
3416 && AARCH64_RA (insn) != AARCH64_ZR)
3417 return TRUE;
3419 return FALSE;
3422 /* Some early revisions of the Cortex-A53 have an erratum (835769) whereby
3423 it is possible for a 64-bit multiply-accumulate instruction to generate an
3424 incorrect result. The details are quite complex and hard to
3425 determine statically, since branches in the code may exist in some
3426 circumstances, but all cases end with a memory (load, store, or
3427 prefetch) instruction followed immediately by the multiply-accumulate
3428 operation. We employ a linker patching technique, by moving the potentially
3429 affected multiply-accumulate instruction into a patch region and replacing
3430 the original instruction with a branch to the patch. This function checks
3431 if INSN_1 is the memory operation followed by a multiply-accumulate
3432 operation (INSN_2). Return TRUE if an erratum sequence is found, FALSE
3433 if INSN_1 and INSN_2 are safe. */
3435 static bfd_boolean
3436 aarch64_erratum_sequence (uint32_t insn_1, uint32_t insn_2)
3438 uint32_t rt;
3439 uint32_t rt2;
3440 uint32_t rn;
3441 uint32_t rm;
3442 uint32_t ra;
3443 bfd_boolean pair;
3444 bfd_boolean load;
3446 if (aarch64_mlxl_p (insn_2)
3447 && aarch64_mem_op_p (insn_1, &rt, &rt2, &pair, &load))
3449 /* Any SIMD memory op is independent of the subsequent MLA
3450 by definition of the erratum. */
3451 if (AARCH64_BIT (insn_1, 26))
3452 return TRUE;
3454 /* If not SIMD, check for integer memory ops and MLA relationship. */
3455 rn = AARCH64_RN (insn_2);
3456 ra = AARCH64_RA (insn_2);
3457 rm = AARCH64_RM (insn_2);
3459 /* If this is a load and there's a true(RAW) dependency, we are safe
3460 and this is not an erratum sequence. */
3461 if (load &&
3462 (rt == rn || rt == rm || rt == ra
3463 || (pair && (rt2 == rn || rt2 == rm || rt2 == ra))))
3464 return FALSE;
3466 /* We conservatively put out stubs for all other cases (including
3467 writebacks). */
3468 return TRUE;
3471 return FALSE;
3474 /* Used to order a list of mapping symbols by address. */
3476 static int
3477 elf_aarch64_compare_mapping (const void *a, const void *b)
3479 const elf_aarch64_section_map *amap = (const elf_aarch64_section_map *) a;
3480 const elf_aarch64_section_map *bmap = (const elf_aarch64_section_map *) b;
3482 if (amap->vma > bmap->vma)
3483 return 1;
3484 else if (amap->vma < bmap->vma)
3485 return -1;
3486 else if (amap->type > bmap->type)
3487 /* Ensure results do not depend on the host qsort for objects with
3488 multiple mapping symbols at the same address by sorting on type
3489 after vma. */
3490 return 1;
3491 else if (amap->type < bmap->type)
3492 return -1;
3493 else
3494 return 0;
3498 static char *
3499 _bfd_aarch64_erratum_835769_stub_name (unsigned num_fixes)
3501 char *stub_name = (char *) bfd_malloc
3502 (strlen ("__erratum_835769_veneer_") + 16);
3503 sprintf (stub_name,"__erratum_835769_veneer_%d", num_fixes);
3504 return stub_name;
3507 /* Scan for Cortex-A53 erratum 835769 sequence.
3509 Return TRUE else FALSE on abnormal termination. */
3511 static bfd_boolean
3512 _bfd_aarch64_erratum_835769_scan (bfd *input_bfd,
3513 struct bfd_link_info *info,
3514 unsigned int *num_fixes_p)
3516 asection *section;
3517 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
3518 unsigned int num_fixes = *num_fixes_p;
3520 if (htab == NULL)
3521 return TRUE;
3523 for (section = input_bfd->sections;
3524 section != NULL;
3525 section = section->next)
3527 bfd_byte *contents = NULL;
3528 struct _aarch64_elf_section_data *sec_data;
3529 unsigned int span;
3531 if (elf_section_type (section) != SHT_PROGBITS
3532 || (elf_section_flags (section) & SHF_EXECINSTR) == 0
3533 || (section->flags & SEC_EXCLUDE) != 0
3534 || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
3535 || (section->output_section == bfd_abs_section_ptr))
3536 continue;
3538 if (elf_section_data (section)->this_hdr.contents != NULL)
3539 contents = elf_section_data (section)->this_hdr.contents;
3540 else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
3541 return FALSE;
3543 sec_data = elf_aarch64_section_data (section);
3545 qsort (sec_data->map, sec_data->mapcount,
3546 sizeof (elf_aarch64_section_map), elf_aarch64_compare_mapping);
3548 for (span = 0; span < sec_data->mapcount; span++)
3550 unsigned int span_start = sec_data->map[span].vma;
3551 unsigned int span_end = ((span == sec_data->mapcount - 1)
3552 ? sec_data->map[0].vma + section->size
3553 : sec_data->map[span + 1].vma);
3554 unsigned int i;
3555 char span_type = sec_data->map[span].type;
3557 if (span_type == 'd')
3558 continue;
3560 for (i = span_start; i + 4 < span_end; i += 4)
3562 uint32_t insn_1 = bfd_getl32 (contents + i);
3563 uint32_t insn_2 = bfd_getl32 (contents + i + 4);
3565 if (aarch64_erratum_sequence (insn_1, insn_2))
3567 struct elf_aarch64_stub_hash_entry *stub_entry;
3568 char *stub_name = _bfd_aarch64_erratum_835769_stub_name (num_fixes);
3569 if (! stub_name)
3570 return FALSE;
3572 stub_entry = _bfd_aarch64_add_stub_entry_in_group (stub_name,
3573 section,
3574 htab);
3575 if (! stub_entry)
3576 return FALSE;
3578 stub_entry->stub_type = aarch64_stub_erratum_835769_veneer;
3579 stub_entry->target_section = section;
3580 stub_entry->target_value = i + 4;
3581 stub_entry->veneered_insn = insn_2;
3582 stub_entry->output_name = stub_name;
3583 num_fixes++;
3587 if (elf_section_data (section)->this_hdr.contents == NULL)
3588 free (contents);
3591 *num_fixes_p = num_fixes;
3593 return TRUE;
3597 /* Test if instruction INSN is ADRP. */
3599 static bfd_boolean
3600 _bfd_aarch64_adrp_p (uint32_t insn)
3602 return ((insn & 0x9f000000) == 0x90000000);
3606 /* Helper predicate to look for cortex-a53 erratum 843419 sequence 1. */
3608 static bfd_boolean
3609 _bfd_aarch64_erratum_843419_sequence_p (uint32_t insn_1, uint32_t insn_2,
3610 uint32_t insn_3)
3612 uint32_t rt;
3613 uint32_t rt2;
3614 bfd_boolean pair;
3615 bfd_boolean load;
3617 return (aarch64_mem_op_p (insn_2, &rt, &rt2, &pair, &load)
3618 && (!pair
3619 || (pair && !load))
3620 && AARCH64_LDST_UIMM (insn_3)
3621 && AARCH64_RN (insn_3) == AARCH64_RD (insn_1));
3625 /* Test for the presence of Cortex-A53 erratum 843419 instruction sequence.
3627 Return TRUE if section CONTENTS at offset I contains one of the
3628 erratum 843419 sequences, otherwise return FALSE. If a sequence is
3629 seen set P_VENEER_I to the offset of the final LOAD/STORE
3630 instruction in the sequence.
3633 static bfd_boolean
3634 _bfd_aarch64_erratum_843419_p (bfd_byte *contents, bfd_vma vma,
3635 bfd_vma i, bfd_vma span_end,
3636 bfd_vma *p_veneer_i)
3638 uint32_t insn_1 = bfd_getl32 (contents + i);
3640 if (!_bfd_aarch64_adrp_p (insn_1))
3641 return FALSE;
3643 if (span_end < i + 12)
3644 return FALSE;
3646 uint32_t insn_2 = bfd_getl32 (contents + i + 4);
3647 uint32_t insn_3 = bfd_getl32 (contents + i + 8);
3649 if ((vma & 0xfff) != 0xff8 && (vma & 0xfff) != 0xffc)
3650 return FALSE;
3652 if (_bfd_aarch64_erratum_843419_sequence_p (insn_1, insn_2, insn_3))
3654 *p_veneer_i = i + 8;
3655 return TRUE;
3658 if (span_end < i + 16)
3659 return FALSE;
3661 uint32_t insn_4 = bfd_getl32 (contents + i + 12);
3663 if (_bfd_aarch64_erratum_843419_sequence_p (insn_1, insn_2, insn_4))
3665 *p_veneer_i = i + 12;
3666 return TRUE;
3669 return FALSE;
3673 /* Resize all stub sections. */
3675 static void
3676 _bfd_aarch64_resize_stubs (struct elf_aarch64_link_hash_table *htab)
3678 asection *section;
3680 /* OK, we've added some stubs. Find out the new size of the
3681 stub sections. */
3682 for (section = htab->stub_bfd->sections;
3683 section != NULL; section = section->next)
3685 /* Ignore non-stub sections. */
3686 if (!strstr (section->name, STUB_SUFFIX))
3687 continue;
3688 section->size = 0;
3691 bfd_hash_traverse (&htab->stub_hash_table, aarch64_size_one_stub, htab);
3693 for (section = htab->stub_bfd->sections;
3694 section != NULL; section = section->next)
3696 if (!strstr (section->name, STUB_SUFFIX))
3697 continue;
3699 if (section->size)
3700 section->size += 4;
3702 /* Ensure all stub sections have a size which is a multiple of
3703 4096. This is important in order to ensure that the insertion
3704 of stub sections does not in itself move existing code around
3705 in such a way that new errata sequences are created. */
3706 if (htab->fix_erratum_843419)
3707 if (section->size)
3708 section->size = BFD_ALIGN (section->size, 0x1000);
3713 /* Construct an erratum 843419 workaround stub name.
3716 static char *
3717 _bfd_aarch64_erratum_843419_stub_name (asection *input_section,
3718 bfd_vma offset)
3720 const bfd_size_type len = 8 + 4 + 1 + 8 + 1 + 16 + 1;
3721 char *stub_name = bfd_malloc (len);
3723 if (stub_name != NULL)
3724 snprintf (stub_name, len, "e843419@%04x_%08x_%" BFD_VMA_FMT "x",
3725 input_section->owner->id,
3726 input_section->id,
3727 offset);
3728 return stub_name;
3731 /* Build a stub_entry structure describing an 843419 fixup.
3733 The stub_entry constructed is populated with the bit pattern INSN
3734 of the instruction located at OFFSET within input SECTION.
3736 Returns TRUE on success. */
3738 static bfd_boolean
3739 _bfd_aarch64_erratum_843419_fixup (uint32_t insn,
3740 bfd_vma adrp_offset,
3741 bfd_vma ldst_offset,
3742 asection *section,
3743 struct bfd_link_info *info)
3745 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
3746 char *stub_name;
3747 struct elf_aarch64_stub_hash_entry *stub_entry;
3749 stub_name = _bfd_aarch64_erratum_843419_stub_name (section, ldst_offset);
3750 stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3751 FALSE, FALSE);
3752 if (stub_entry)
3754 free (stub_name);
3755 return TRUE;
3758 /* We always place an 843419 workaround veneer in the stub section
3759 attached to the input section in which an erratum sequence has
3760 been found. This ensures that later in the link process (in
3761 elfNN_aarch64_write_section) when we copy the veneered
3762 instruction from the input section into the stub section the
3763 copied instruction will have had any relocations applied to it.
3764 If we placed workaround veneers in any other stub section then we
3765 could not assume that all relocations have been processed on the
3766 corresponding input section at the point we output the stub
3767 section.
3770 stub_entry = _bfd_aarch64_add_stub_entry_after (stub_name, section, htab);
3771 if (stub_entry == NULL)
3773 free (stub_name);
3774 return FALSE;
3777 stub_entry->adrp_offset = adrp_offset;
3778 stub_entry->target_value = ldst_offset;
3779 stub_entry->target_section = section;
3780 stub_entry->stub_type = aarch64_stub_erratum_843419_veneer;
3781 stub_entry->veneered_insn = insn;
3782 stub_entry->output_name = stub_name;
3784 return TRUE;
3788 /* Scan an input section looking for the signature of erratum 843419.
3790 Scans input SECTION in INPUT_BFD looking for erratum 843419
3791 signatures, for each signature found a stub_entry is created
3792 describing the location of the erratum for subsequent fixup.
3794 Return TRUE on successful scan, FALSE on failure to scan.
3797 static bfd_boolean
3798 _bfd_aarch64_erratum_843419_scan (bfd *input_bfd, asection *section,
3799 struct bfd_link_info *info)
3801 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
3803 if (htab == NULL)
3804 return TRUE;
3806 if (elf_section_type (section) != SHT_PROGBITS
3807 || (elf_section_flags (section) & SHF_EXECINSTR) == 0
3808 || (section->flags & SEC_EXCLUDE) != 0
3809 || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
3810 || (section->output_section == bfd_abs_section_ptr))
3811 return TRUE;
3815 bfd_byte *contents = NULL;
3816 struct _aarch64_elf_section_data *sec_data;
3817 unsigned int span;
3819 if (elf_section_data (section)->this_hdr.contents != NULL)
3820 contents = elf_section_data (section)->this_hdr.contents;
3821 else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
3822 return FALSE;
3824 sec_data = elf_aarch64_section_data (section);
3826 qsort (sec_data->map, sec_data->mapcount,
3827 sizeof (elf_aarch64_section_map), elf_aarch64_compare_mapping);
3829 for (span = 0; span < sec_data->mapcount; span++)
3831 unsigned int span_start = sec_data->map[span].vma;
3832 unsigned int span_end = ((span == sec_data->mapcount - 1)
3833 ? sec_data->map[0].vma + section->size
3834 : sec_data->map[span + 1].vma);
3835 unsigned int i;
3836 char span_type = sec_data->map[span].type;
3838 if (span_type == 'd')
3839 continue;
3841 for (i = span_start; i + 8 < span_end; i += 4)
3843 bfd_vma vma = (section->output_section->vma
3844 + section->output_offset
3845 + i);
3846 bfd_vma veneer_i;
3848 if (_bfd_aarch64_erratum_843419_p
3849 (contents, vma, i, span_end, &veneer_i))
3851 uint32_t insn = bfd_getl32 (contents + veneer_i);
3853 if (!_bfd_aarch64_erratum_843419_fixup (insn, i, veneer_i,
3854 section, info))
3855 return FALSE;
3860 if (elf_section_data (section)->this_hdr.contents == NULL)
3861 free (contents);
3863 while (0);
3865 return TRUE;
3869 /* Determine and set the size of the stub section for a final link.
3871 The basic idea here is to examine all the relocations looking for
3872 PC-relative calls to a target that is unreachable with a "bl"
3873 instruction. */
3875 bfd_boolean
3876 elfNN_aarch64_size_stubs (bfd *output_bfd,
3877 bfd *stub_bfd,
3878 struct bfd_link_info *info,
3879 bfd_signed_vma group_size,
3880 asection * (*add_stub_section) (const char *,
3881 asection *),
3882 void (*layout_sections_again) (void))
3884 bfd_size_type stub_group_size;
3885 bfd_boolean stubs_always_before_branch;
3886 bfd_boolean stub_changed = FALSE;
3887 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
3888 unsigned int num_erratum_835769_fixes = 0;
3890 /* Propagate mach to stub bfd, because it may not have been
3891 finalized when we created stub_bfd. */
3892 bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
3893 bfd_get_mach (output_bfd));
3895 /* Stash our params away. */
3896 htab->stub_bfd = stub_bfd;
3897 htab->add_stub_section = add_stub_section;
3898 htab->layout_sections_again = layout_sections_again;
3899 stubs_always_before_branch = group_size < 0;
3900 if (group_size < 0)
3901 stub_group_size = -group_size;
3902 else
3903 stub_group_size = group_size;
3905 if (stub_group_size == 1)
3907 /* Default values. */
3908 /* AArch64 branch range is +-128MB. The value used is 1MB less. */
3909 stub_group_size = 127 * 1024 * 1024;
3912 group_sections (htab, stub_group_size, stubs_always_before_branch);
3914 (*htab->layout_sections_again) ();
3916 if (htab->fix_erratum_835769)
3918 bfd *input_bfd;
3920 for (input_bfd = info->input_bfds;
3921 input_bfd != NULL; input_bfd = input_bfd->link.next)
3922 if (!_bfd_aarch64_erratum_835769_scan (input_bfd, info,
3923 &num_erratum_835769_fixes))
3924 return FALSE;
3926 _bfd_aarch64_resize_stubs (htab);
3927 (*htab->layout_sections_again) ();
3930 if (htab->fix_erratum_843419)
3932 bfd *input_bfd;
3934 for (input_bfd = info->input_bfds;
3935 input_bfd != NULL;
3936 input_bfd = input_bfd->link.next)
3938 asection *section;
3940 for (section = input_bfd->sections;
3941 section != NULL;
3942 section = section->next)
3943 if (!_bfd_aarch64_erratum_843419_scan (input_bfd, section, info))
3944 return FALSE;
3947 _bfd_aarch64_resize_stubs (htab);
3948 (*htab->layout_sections_again) ();
3951 while (1)
3953 bfd *input_bfd;
3955 for (input_bfd = info->input_bfds;
3956 input_bfd != NULL; input_bfd = input_bfd->link.next)
3958 Elf_Internal_Shdr *symtab_hdr;
3959 asection *section;
3960 Elf_Internal_Sym *local_syms = NULL;
3962 /* We'll need the symbol table in a second. */
3963 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3964 if (symtab_hdr->sh_info == 0)
3965 continue;
3967 /* Walk over each section attached to the input bfd. */
3968 for (section = input_bfd->sections;
3969 section != NULL; section = section->next)
3971 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
3973 /* If there aren't any relocs, then there's nothing more
3974 to do. */
3975 if ((section->flags & SEC_RELOC) == 0
3976 || section->reloc_count == 0
3977 || (section->flags & SEC_CODE) == 0)
3978 continue;
3980 /* If this section is a link-once section that will be
3981 discarded, then don't create any stubs. */
3982 if (section->output_section == NULL
3983 || section->output_section->owner != output_bfd)
3984 continue;
3986 /* Get the relocs. */
3987 internal_relocs
3988 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
3989 NULL, info->keep_memory);
3990 if (internal_relocs == NULL)
3991 goto error_ret_free_local;
3993 /* Now examine each relocation. */
3994 irela = internal_relocs;
3995 irelaend = irela + section->reloc_count;
3996 for (; irela < irelaend; irela++)
3998 unsigned int r_type, r_indx;
3999 enum elf_aarch64_stub_type stub_type;
4000 struct elf_aarch64_stub_hash_entry *stub_entry;
4001 asection *sym_sec;
4002 bfd_vma sym_value;
4003 bfd_vma destination;
4004 struct elf_aarch64_link_hash_entry *hash;
4005 const char *sym_name;
4006 char *stub_name;
4007 const asection *id_sec;
4008 unsigned char st_type;
4009 bfd_size_type len;
4011 r_type = ELFNN_R_TYPE (irela->r_info);
4012 r_indx = ELFNN_R_SYM (irela->r_info);
4014 if (r_type >= (unsigned int) R_AARCH64_end)
4016 bfd_set_error (bfd_error_bad_value);
4017 error_ret_free_internal:
4018 if (elf_section_data (section)->relocs == NULL)
4019 free (internal_relocs);
4020 goto error_ret_free_local;
4023 /* Only look for stubs on unconditional branch and
4024 branch and link instructions. */
4025 if (r_type != (unsigned int) AARCH64_R (CALL26)
4026 && r_type != (unsigned int) AARCH64_R (JUMP26))
4027 continue;
4029 /* Now determine the call target, its name, value,
4030 section. */
4031 sym_sec = NULL;
4032 sym_value = 0;
4033 destination = 0;
4034 hash = NULL;
4035 sym_name = NULL;
4036 if (r_indx < symtab_hdr->sh_info)
4038 /* It's a local symbol. */
4039 Elf_Internal_Sym *sym;
4040 Elf_Internal_Shdr *hdr;
4042 if (local_syms == NULL)
4044 local_syms
4045 = (Elf_Internal_Sym *) symtab_hdr->contents;
4046 if (local_syms == NULL)
4047 local_syms
4048 = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
4049 symtab_hdr->sh_info, 0,
4050 NULL, NULL, NULL);
4051 if (local_syms == NULL)
4052 goto error_ret_free_internal;
4055 sym = local_syms + r_indx;
4056 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
4057 sym_sec = hdr->bfd_section;
4058 if (!sym_sec)
4059 /* This is an undefined symbol. It can never
4060 be resolved. */
4061 continue;
4063 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4064 sym_value = sym->st_value;
4065 destination = (sym_value + irela->r_addend
4066 + sym_sec->output_offset
4067 + sym_sec->output_section->vma);
4068 st_type = ELF_ST_TYPE (sym->st_info);
4069 sym_name
4070 = bfd_elf_string_from_elf_section (input_bfd,
4071 symtab_hdr->sh_link,
4072 sym->st_name);
4074 else
4076 int e_indx;
4078 e_indx = r_indx - symtab_hdr->sh_info;
4079 hash = ((struct elf_aarch64_link_hash_entry *)
4080 elf_sym_hashes (input_bfd)[e_indx]);
4082 while (hash->root.root.type == bfd_link_hash_indirect
4083 || hash->root.root.type == bfd_link_hash_warning)
4084 hash = ((struct elf_aarch64_link_hash_entry *)
4085 hash->root.root.u.i.link);
4087 if (hash->root.root.type == bfd_link_hash_defined
4088 || hash->root.root.type == bfd_link_hash_defweak)
4090 struct elf_aarch64_link_hash_table *globals =
4091 elf_aarch64_hash_table (info);
4092 sym_sec = hash->root.root.u.def.section;
4093 sym_value = hash->root.root.u.def.value;
4094 /* For a destination in a shared library,
4095 use the PLT stub as target address to
4096 decide whether a branch stub is
4097 needed. */
4098 if (globals->root.splt != NULL && hash != NULL
4099 && hash->root.plt.offset != (bfd_vma) - 1)
4101 sym_sec = globals->root.splt;
4102 sym_value = hash->root.plt.offset;
4103 if (sym_sec->output_section != NULL)
4104 destination = (sym_value
4105 + sym_sec->output_offset
4107 sym_sec->output_section->vma);
4109 else if (sym_sec->output_section != NULL)
4110 destination = (sym_value + irela->r_addend
4111 + sym_sec->output_offset
4112 + sym_sec->output_section->vma);
4114 else if (hash->root.root.type == bfd_link_hash_undefined
4115 || (hash->root.root.type
4116 == bfd_link_hash_undefweak))
4118 /* For a shared library, use the PLT stub as
4119 target address to decide whether a long
4120 branch stub is needed.
4121 For absolute code, they cannot be handled. */
4122 struct elf_aarch64_link_hash_table *globals =
4123 elf_aarch64_hash_table (info);
4125 if (globals->root.splt != NULL && hash != NULL
4126 && hash->root.plt.offset != (bfd_vma) - 1)
4128 sym_sec = globals->root.splt;
4129 sym_value = hash->root.plt.offset;
4130 if (sym_sec->output_section != NULL)
4131 destination = (sym_value
4132 + sym_sec->output_offset
4134 sym_sec->output_section->vma);
4136 else
4137 continue;
4139 else
4141 bfd_set_error (bfd_error_bad_value);
4142 goto error_ret_free_internal;
4144 st_type = ELF_ST_TYPE (hash->root.type);
4145 sym_name = hash->root.root.root.string;
4148 /* Determine what (if any) linker stub is needed. */
4149 stub_type = aarch64_type_of_stub (section, irela, sym_sec,
4150 st_type, destination);
4151 if (stub_type == aarch64_stub_none)
4152 continue;
4154 /* Support for grouping stub sections. */
4155 id_sec = htab->stub_group[section->id].link_sec;
4157 /* Get the name of this stub. */
4158 stub_name = elfNN_aarch64_stub_name (id_sec, sym_sec, hash,
4159 irela);
4160 if (!stub_name)
4161 goto error_ret_free_internal;
4163 stub_entry =
4164 aarch64_stub_hash_lookup (&htab->stub_hash_table,
4165 stub_name, FALSE, FALSE);
4166 if (stub_entry != NULL)
4168 /* The proper stub has already been created. */
4169 free (stub_name);
4170 continue;
4173 stub_entry = _bfd_aarch64_add_stub_entry_in_group
4174 (stub_name, section, htab);
4175 if (stub_entry == NULL)
4177 free (stub_name);
4178 goto error_ret_free_internal;
4181 stub_entry->target_value = sym_value + irela->r_addend;
4182 stub_entry->target_section = sym_sec;
4183 stub_entry->stub_type = stub_type;
4184 stub_entry->h = hash;
4185 stub_entry->st_type = st_type;
4187 if (sym_name == NULL)
4188 sym_name = "unnamed";
4189 len = sizeof (STUB_ENTRY_NAME) + strlen (sym_name);
4190 stub_entry->output_name = bfd_alloc (htab->stub_bfd, len);
4191 if (stub_entry->output_name == NULL)
4193 free (stub_name);
4194 goto error_ret_free_internal;
4197 snprintf (stub_entry->output_name, len, STUB_ENTRY_NAME,
4198 sym_name);
4200 stub_changed = TRUE;
4203 /* We're done with the internal relocs, free them. */
4204 if (elf_section_data (section)->relocs == NULL)
4205 free (internal_relocs);
4209 if (!stub_changed)
4210 break;
4212 _bfd_aarch64_resize_stubs (htab);
4214 /* Ask the linker to do its stuff. */
4215 (*htab->layout_sections_again) ();
4216 stub_changed = FALSE;
4219 return TRUE;
4221 error_ret_free_local:
4222 return FALSE;
4225 /* Build all the stubs associated with the current output file. The
4226 stubs are kept in a hash table attached to the main linker hash
4227 table. We also set up the .plt entries for statically linked PIC
4228 functions here. This function is called via aarch64_elf_finish in the
4229 linker. */
4231 bfd_boolean
4232 elfNN_aarch64_build_stubs (struct bfd_link_info *info)
4234 asection *stub_sec;
4235 struct bfd_hash_table *table;
4236 struct elf_aarch64_link_hash_table *htab;
4238 htab = elf_aarch64_hash_table (info);
4240 for (stub_sec = htab->stub_bfd->sections;
4241 stub_sec != NULL; stub_sec = stub_sec->next)
4243 bfd_size_type size;
4245 /* Ignore non-stub sections. */
4246 if (!strstr (stub_sec->name, STUB_SUFFIX))
4247 continue;
4249 /* Allocate memory to hold the linker stubs. */
4250 size = stub_sec->size;
4251 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
4252 if (stub_sec->contents == NULL && size != 0)
4253 return FALSE;
4254 stub_sec->size = 0;
4256 bfd_putl32 (0x14000000 | (size >> 2), stub_sec->contents);
4257 stub_sec->size += 4;
4260 /* Build the stubs as directed by the stub hash table. */
4261 table = &htab->stub_hash_table;
4262 bfd_hash_traverse (table, aarch64_build_one_stub, info);
4264 return TRUE;
4268 /* Add an entry to the code/data map for section SEC. */
4270 static void
4271 elfNN_aarch64_section_map_add (asection *sec, char type, bfd_vma vma)
4273 struct _aarch64_elf_section_data *sec_data =
4274 elf_aarch64_section_data (sec);
4275 unsigned int newidx;
4277 if (sec_data->map == NULL)
4279 sec_data->map = bfd_malloc (sizeof (elf_aarch64_section_map));
4280 sec_data->mapcount = 0;
4281 sec_data->mapsize = 1;
4284 newidx = sec_data->mapcount++;
4286 if (sec_data->mapcount > sec_data->mapsize)
4288 sec_data->mapsize *= 2;
4289 sec_data->map = bfd_realloc_or_free
4290 (sec_data->map, sec_data->mapsize * sizeof (elf_aarch64_section_map));
4293 if (sec_data->map)
4295 sec_data->map[newidx].vma = vma;
4296 sec_data->map[newidx].type = type;
4301 /* Initialise maps of insn/data for input BFDs. */
4302 void
4303 bfd_elfNN_aarch64_init_maps (bfd *abfd)
4305 Elf_Internal_Sym *isymbuf;
4306 Elf_Internal_Shdr *hdr;
4307 unsigned int i, localsyms;
4309 /* Make sure that we are dealing with an AArch64 elf binary. */
4310 if (!is_aarch64_elf (abfd))
4311 return;
4313 if ((abfd->flags & DYNAMIC) != 0)
4314 return;
4316 hdr = &elf_symtab_hdr (abfd);
4317 localsyms = hdr->sh_info;
4319 /* Obtain a buffer full of symbols for this BFD. The hdr->sh_info field
4320 should contain the number of local symbols, which should come before any
4321 global symbols. Mapping symbols are always local. */
4322 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, localsyms, 0, NULL, NULL, NULL);
4324 /* No internal symbols read? Skip this BFD. */
4325 if (isymbuf == NULL)
4326 return;
4328 for (i = 0; i < localsyms; i++)
4330 Elf_Internal_Sym *isym = &isymbuf[i];
4331 asection *sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4332 const char *name;
4334 if (sec != NULL && ELF_ST_BIND (isym->st_info) == STB_LOCAL)
4336 name = bfd_elf_string_from_elf_section (abfd,
4337 hdr->sh_link,
4338 isym->st_name);
4340 if (bfd_is_aarch64_special_symbol_name
4341 (name, BFD_AARCH64_SPECIAL_SYM_TYPE_MAP))
4342 elfNN_aarch64_section_map_add (sec, name[1], isym->st_value);
4347 /* Set option values needed during linking. */
4348 void
4349 bfd_elfNN_aarch64_set_options (struct bfd *output_bfd,
4350 struct bfd_link_info *link_info,
4351 int no_enum_warn,
4352 int no_wchar_warn, int pic_veneer,
4353 int fix_erratum_835769,
4354 int fix_erratum_843419,
4355 int no_apply_dynamic_relocs)
4357 struct elf_aarch64_link_hash_table *globals;
4359 globals = elf_aarch64_hash_table (link_info);
4360 globals->pic_veneer = pic_veneer;
4361 globals->fix_erratum_835769 = fix_erratum_835769;
4362 globals->fix_erratum_843419 = fix_erratum_843419;
4363 globals->fix_erratum_843419_adr = TRUE;
4364 globals->no_apply_dynamic_relocs = no_apply_dynamic_relocs;
4366 BFD_ASSERT (is_aarch64_elf (output_bfd));
4367 elf_aarch64_tdata (output_bfd)->no_enum_size_warning = no_enum_warn;
4368 elf_aarch64_tdata (output_bfd)->no_wchar_size_warning = no_wchar_warn;
4371 static bfd_vma
4372 aarch64_calculate_got_entry_vma (struct elf_link_hash_entry *h,
4373 struct elf_aarch64_link_hash_table
4374 *globals, struct bfd_link_info *info,
4375 bfd_vma value, bfd *output_bfd,
4376 bfd_boolean *unresolved_reloc_p)
4378 bfd_vma off = (bfd_vma) - 1;
4379 asection *basegot = globals->root.sgot;
4380 bfd_boolean dyn = globals->root.dynamic_sections_created;
4382 if (h != NULL)
4384 BFD_ASSERT (basegot != NULL);
4385 off = h->got.offset;
4386 BFD_ASSERT (off != (bfd_vma) - 1);
4387 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
4388 || (bfd_link_pic (info)
4389 && SYMBOL_REFERENCES_LOCAL (info, h))
4390 || (ELF_ST_VISIBILITY (h->other)
4391 && h->root.type == bfd_link_hash_undefweak))
4393 /* This is actually a static link, or it is a -Bsymbolic link
4394 and the symbol is defined locally. We must initialize this
4395 entry in the global offset table. Since the offset must
4396 always be a multiple of 8 (4 in the case of ILP32), we use
4397 the least significant bit to record whether we have
4398 initialized it already.
4399 When doing a dynamic link, we create a .rel(a).got relocation
4400 entry to initialize the value. This is done in the
4401 finish_dynamic_symbol routine. */
4402 if ((off & 1) != 0)
4403 off &= ~1;
4404 else
4406 bfd_put_NN (output_bfd, value, basegot->contents + off);
4407 h->got.offset |= 1;
4410 else
4411 *unresolved_reloc_p = FALSE;
4413 off = off + basegot->output_section->vma + basegot->output_offset;
4416 return off;
4419 /* Change R_TYPE to a more efficient access model where possible,
4420 return the new reloc type. */
4422 static bfd_reloc_code_real_type
4423 aarch64_tls_transition_without_check (bfd_reloc_code_real_type r_type,
4424 struct elf_link_hash_entry *h)
4426 bfd_boolean is_local = h == NULL;
4428 switch (r_type)
4430 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
4431 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
4432 return (is_local
4433 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1
4434 : BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21);
4436 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
4437 return (is_local
4438 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
4439 : r_type);
4441 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
4442 return (is_local
4443 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1
4444 : BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19);
4446 case BFD_RELOC_AARCH64_TLSDESC_LDR:
4447 return (is_local
4448 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
4449 : BFD_RELOC_AARCH64_NONE);
4451 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
4452 return (is_local
4453 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC
4454 : BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC);
4456 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
4457 return (is_local
4458 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2
4459 : BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1);
4461 case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
4462 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
4463 return (is_local
4464 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
4465 : BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC);
4467 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
4468 return is_local ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1 : r_type;
4470 case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
4471 return is_local ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC : r_type;
4473 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
4474 return r_type;
4476 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
4477 return (is_local
4478 ? BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12
4479 : BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19);
4481 case BFD_RELOC_AARCH64_TLSDESC_ADD:
4482 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
4483 case BFD_RELOC_AARCH64_TLSDESC_CALL:
4484 /* Instructions with these relocations will become NOPs. */
4485 return BFD_RELOC_AARCH64_NONE;
4487 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
4488 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
4489 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
4490 return is_local ? BFD_RELOC_AARCH64_NONE : r_type;
4492 #if ARCH_SIZE == 64
4493 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
4494 return is_local
4495 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC
4496 : BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC;
4498 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
4499 return is_local
4500 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2
4501 : BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1;
4502 #endif
4504 default:
4505 break;
4508 return r_type;
4511 static unsigned int
4512 aarch64_reloc_got_type (bfd_reloc_code_real_type r_type)
4514 switch (r_type)
4516 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
4517 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
4518 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
4519 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
4520 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
4521 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
4522 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
4523 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
4524 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
4525 return GOT_NORMAL;
4527 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
4528 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
4529 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
4530 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
4531 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
4532 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
4533 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
4534 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
4535 return GOT_TLS_GD;
4537 case BFD_RELOC_AARCH64_TLSDESC_ADD:
4538 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
4539 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
4540 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
4541 case BFD_RELOC_AARCH64_TLSDESC_CALL:
4542 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
4543 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12:
4544 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
4545 case BFD_RELOC_AARCH64_TLSDESC_LDR:
4546 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
4547 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
4548 return GOT_TLSDESC_GD;
4550 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
4551 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
4552 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
4553 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
4554 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
4555 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
4556 return GOT_TLS_IE;
4558 default:
4559 break;
4561 return GOT_UNKNOWN;
4564 static bfd_boolean
4565 aarch64_can_relax_tls (bfd *input_bfd,
4566 struct bfd_link_info *info,
4567 bfd_reloc_code_real_type r_type,
4568 struct elf_link_hash_entry *h,
4569 unsigned long r_symndx)
4571 unsigned int symbol_got_type;
4572 unsigned int reloc_got_type;
4574 if (! IS_AARCH64_TLS_RELAX_RELOC (r_type))
4575 return FALSE;
4577 symbol_got_type = elfNN_aarch64_symbol_got_type (h, input_bfd, r_symndx);
4578 reloc_got_type = aarch64_reloc_got_type (r_type);
4580 if (symbol_got_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (reloc_got_type))
4581 return TRUE;
4583 if (bfd_link_pic (info))
4584 return FALSE;
4586 if (h && h->root.type == bfd_link_hash_undefweak)
4587 return FALSE;
4589 return TRUE;
4592 /* Given the relocation code R_TYPE, return the relaxed bfd reloc
4593 enumerator. */
4595 static bfd_reloc_code_real_type
4596 aarch64_tls_transition (bfd *input_bfd,
4597 struct bfd_link_info *info,
4598 unsigned int r_type,
4599 struct elf_link_hash_entry *h,
4600 unsigned long r_symndx)
4602 bfd_reloc_code_real_type bfd_r_type
4603 = elfNN_aarch64_bfd_reloc_from_type (r_type);
4605 if (! aarch64_can_relax_tls (input_bfd, info, bfd_r_type, h, r_symndx))
4606 return bfd_r_type;
4608 return aarch64_tls_transition_without_check (bfd_r_type, h);
4611 /* Return the base VMA address which should be subtracted from real addresses
4612 when resolving R_AARCH64_TLS_DTPREL relocation. */
4614 static bfd_vma
4615 dtpoff_base (struct bfd_link_info *info)
4617 /* If tls_sec is NULL, we should have signalled an error already. */
4618 BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
4619 return elf_hash_table (info)->tls_sec->vma;
4622 /* Return the base VMA address which should be subtracted from real addresses
4623 when resolving R_AARCH64_TLS_GOTTPREL64 relocations. */
4625 static bfd_vma
4626 tpoff_base (struct bfd_link_info *info)
4628 struct elf_link_hash_table *htab = elf_hash_table (info);
4630 /* If tls_sec is NULL, we should have signalled an error already. */
4631 BFD_ASSERT (htab->tls_sec != NULL);
4633 bfd_vma base = align_power ((bfd_vma) TCB_SIZE,
4634 htab->tls_sec->alignment_power);
4635 return htab->tls_sec->vma - base;
4638 static bfd_vma *
4639 symbol_got_offset_ref (bfd *input_bfd, struct elf_link_hash_entry *h,
4640 unsigned long r_symndx)
4642 /* Calculate the address of the GOT entry for symbol
4643 referred to in h. */
4644 if (h != NULL)
4645 return &h->got.offset;
4646 else
4648 /* local symbol */
4649 struct elf_aarch64_local_symbol *l;
4651 l = elf_aarch64_locals (input_bfd);
4652 return &l[r_symndx].got_offset;
4656 static void
4657 symbol_got_offset_mark (bfd *input_bfd, struct elf_link_hash_entry *h,
4658 unsigned long r_symndx)
4660 bfd_vma *p;
4661 p = symbol_got_offset_ref (input_bfd, h, r_symndx);
4662 *p |= 1;
4665 static int
4666 symbol_got_offset_mark_p (bfd *input_bfd, struct elf_link_hash_entry *h,
4667 unsigned long r_symndx)
4669 bfd_vma value;
4670 value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
4671 return value & 1;
4674 static bfd_vma
4675 symbol_got_offset (bfd *input_bfd, struct elf_link_hash_entry *h,
4676 unsigned long r_symndx)
4678 bfd_vma value;
4679 value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
4680 value &= ~1;
4681 return value;
4684 static bfd_vma *
4685 symbol_tlsdesc_got_offset_ref (bfd *input_bfd, struct elf_link_hash_entry *h,
4686 unsigned long r_symndx)
4688 /* Calculate the address of the GOT entry for symbol
4689 referred to in h. */
4690 if (h != NULL)
4692 struct elf_aarch64_link_hash_entry *eh;
4693 eh = (struct elf_aarch64_link_hash_entry *) h;
4694 return &eh->tlsdesc_got_jump_table_offset;
4696 else
4698 /* local symbol */
4699 struct elf_aarch64_local_symbol *l;
4701 l = elf_aarch64_locals (input_bfd);
4702 return &l[r_symndx].tlsdesc_got_jump_table_offset;
4706 static void
4707 symbol_tlsdesc_got_offset_mark (bfd *input_bfd, struct elf_link_hash_entry *h,
4708 unsigned long r_symndx)
4710 bfd_vma *p;
4711 p = symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
4712 *p |= 1;
4715 static int
4716 symbol_tlsdesc_got_offset_mark_p (bfd *input_bfd,
4717 struct elf_link_hash_entry *h,
4718 unsigned long r_symndx)
4720 bfd_vma value;
4721 value = * symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
4722 return value & 1;
4725 static bfd_vma
4726 symbol_tlsdesc_got_offset (bfd *input_bfd, struct elf_link_hash_entry *h,
4727 unsigned long r_symndx)
4729 bfd_vma value;
4730 value = * symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
4731 value &= ~1;
4732 return value;
4735 /* Data for make_branch_to_erratum_835769_stub(). */
4737 struct erratum_835769_branch_to_stub_data
4739 struct bfd_link_info *info;
4740 asection *output_section;
4741 bfd_byte *contents;
4744 /* Helper to insert branches to erratum 835769 stubs in the right
4745 places for a particular section. */
4747 static bfd_boolean
4748 make_branch_to_erratum_835769_stub (struct bfd_hash_entry *gen_entry,
4749 void *in_arg)
4751 struct elf_aarch64_stub_hash_entry *stub_entry;
4752 struct erratum_835769_branch_to_stub_data *data;
4753 bfd_byte *contents;
4754 unsigned long branch_insn = 0;
4755 bfd_vma veneered_insn_loc, veneer_entry_loc;
4756 bfd_signed_vma branch_offset;
4757 unsigned int target;
4758 bfd *abfd;
4760 stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
4761 data = (struct erratum_835769_branch_to_stub_data *) in_arg;
4763 if (stub_entry->target_section != data->output_section
4764 || stub_entry->stub_type != aarch64_stub_erratum_835769_veneer)
4765 return TRUE;
4767 contents = data->contents;
4768 veneered_insn_loc = stub_entry->target_section->output_section->vma
4769 + stub_entry->target_section->output_offset
4770 + stub_entry->target_value;
4771 veneer_entry_loc = stub_entry->stub_sec->output_section->vma
4772 + stub_entry->stub_sec->output_offset
4773 + stub_entry->stub_offset;
4774 branch_offset = veneer_entry_loc - veneered_insn_loc;
4776 abfd = stub_entry->target_section->owner;
4777 if (!aarch64_valid_branch_p (veneer_entry_loc, veneered_insn_loc))
4778 _bfd_error_handler
4779 (_("%B: error: Erratum 835769 stub out "
4780 "of range (input file too large)"), abfd);
4782 target = stub_entry->target_value;
4783 branch_insn = 0x14000000;
4784 branch_offset >>= 2;
4785 branch_offset &= 0x3ffffff;
4786 branch_insn |= branch_offset;
4787 bfd_putl32 (branch_insn, &contents[target]);
4789 return TRUE;
4793 static bfd_boolean
4794 _bfd_aarch64_erratum_843419_branch_to_stub (struct bfd_hash_entry *gen_entry,
4795 void *in_arg)
4797 struct elf_aarch64_stub_hash_entry *stub_entry
4798 = (struct elf_aarch64_stub_hash_entry *) gen_entry;
4799 struct erratum_835769_branch_to_stub_data *data
4800 = (struct erratum_835769_branch_to_stub_data *) in_arg;
4801 struct bfd_link_info *info;
4802 struct elf_aarch64_link_hash_table *htab;
4803 bfd_byte *contents;
4804 asection *section;
4805 bfd *abfd;
4806 bfd_vma place;
4807 uint32_t insn;
4809 info = data->info;
4810 contents = data->contents;
4811 section = data->output_section;
4813 htab = elf_aarch64_hash_table (info);
4815 if (stub_entry->target_section != section
4816 || stub_entry->stub_type != aarch64_stub_erratum_843419_veneer)
4817 return TRUE;
4819 insn = bfd_getl32 (contents + stub_entry->target_value);
4820 bfd_putl32 (insn,
4821 stub_entry->stub_sec->contents + stub_entry->stub_offset);
4823 place = (section->output_section->vma + section->output_offset
4824 + stub_entry->adrp_offset);
4825 insn = bfd_getl32 (contents + stub_entry->adrp_offset);
4827 if ((insn & AARCH64_ADRP_OP_MASK) != AARCH64_ADRP_OP)
4828 abort ();
4830 bfd_signed_vma imm =
4831 (_bfd_aarch64_sign_extend
4832 ((bfd_vma) _bfd_aarch64_decode_adrp_imm (insn) << 12, 33)
4833 - (place & 0xfff));
4835 if (htab->fix_erratum_843419_adr
4836 && (imm >= AARCH64_MIN_ADRP_IMM && imm <= AARCH64_MAX_ADRP_IMM))
4838 insn = (_bfd_aarch64_reencode_adr_imm (AARCH64_ADR_OP, imm)
4839 | AARCH64_RT (insn));
4840 bfd_putl32 (insn, contents + stub_entry->adrp_offset);
4842 else
4844 bfd_vma veneered_insn_loc;
4845 bfd_vma veneer_entry_loc;
4846 bfd_signed_vma branch_offset;
4847 uint32_t branch_insn;
4849 veneered_insn_loc = stub_entry->target_section->output_section->vma
4850 + stub_entry->target_section->output_offset
4851 + stub_entry->target_value;
4852 veneer_entry_loc = stub_entry->stub_sec->output_section->vma
4853 + stub_entry->stub_sec->output_offset
4854 + stub_entry->stub_offset;
4855 branch_offset = veneer_entry_loc - veneered_insn_loc;
4857 abfd = stub_entry->target_section->owner;
4858 if (!aarch64_valid_branch_p (veneer_entry_loc, veneered_insn_loc))
4859 _bfd_error_handler
4860 (_("%B: error: Erratum 843419 stub out "
4861 "of range (input file too large)"), abfd);
4863 branch_insn = 0x14000000;
4864 branch_offset >>= 2;
4865 branch_offset &= 0x3ffffff;
4866 branch_insn |= branch_offset;
4867 bfd_putl32 (branch_insn, contents + stub_entry->target_value);
4869 return TRUE;
4873 static bfd_boolean
4874 elfNN_aarch64_write_section (bfd *output_bfd ATTRIBUTE_UNUSED,
4875 struct bfd_link_info *link_info,
4876 asection *sec,
4877 bfd_byte *contents)
4880 struct elf_aarch64_link_hash_table *globals =
4881 elf_aarch64_hash_table (link_info);
4883 if (globals == NULL)
4884 return FALSE;
4886 /* Fix code to point to erratum 835769 stubs. */
4887 if (globals->fix_erratum_835769)
4889 struct erratum_835769_branch_to_stub_data data;
4891 data.info = link_info;
4892 data.output_section = sec;
4893 data.contents = contents;
4894 bfd_hash_traverse (&globals->stub_hash_table,
4895 make_branch_to_erratum_835769_stub, &data);
4898 if (globals->fix_erratum_843419)
4900 struct erratum_835769_branch_to_stub_data data;
4902 data.info = link_info;
4903 data.output_section = sec;
4904 data.contents = contents;
4905 bfd_hash_traverse (&globals->stub_hash_table,
4906 _bfd_aarch64_erratum_843419_branch_to_stub, &data);
4909 return FALSE;
4912 /* Return TRUE if RELOC is a relocation against the base of GOT table. */
4914 static bfd_boolean
4915 aarch64_relocation_aginst_gp_p (bfd_reloc_code_real_type reloc)
4917 return (reloc == BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14
4918 || reloc == BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15
4919 || reloc == BFD_RELOC_AARCH64_LD64_GOTOFF_LO15
4920 || reloc == BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC
4921 || reloc == BFD_RELOC_AARCH64_MOVW_GOTOFF_G1);
4924 /* Perform a relocation as part of a final link. The input relocation type
4925 should be TLS relaxed. */
4927 static bfd_reloc_status_type
4928 elfNN_aarch64_final_link_relocate (reloc_howto_type *howto,
4929 bfd *input_bfd,
4930 bfd *output_bfd,
4931 asection *input_section,
4932 bfd_byte *contents,
4933 Elf_Internal_Rela *rel,
4934 bfd_vma value,
4935 struct bfd_link_info *info,
4936 asection *sym_sec,
4937 struct elf_link_hash_entry *h,
4938 bfd_boolean *unresolved_reloc_p,
4939 bfd_boolean save_addend,
4940 bfd_vma *saved_addend,
4941 Elf_Internal_Sym *sym)
4943 Elf_Internal_Shdr *symtab_hdr;
4944 unsigned int r_type = howto->type;
4945 bfd_reloc_code_real_type bfd_r_type
4946 = elfNN_aarch64_bfd_reloc_from_howto (howto);
4947 unsigned long r_symndx;
4948 bfd_byte *hit_data = contents + rel->r_offset;
4949 bfd_vma place, off, got_entry_addr = 0;
4950 bfd_signed_vma signed_addend;
4951 struct elf_aarch64_link_hash_table *globals;
4952 bfd_boolean weak_undef_p;
4953 bfd_boolean relative_reloc;
4954 asection *base_got;
4955 bfd_vma orig_value = value;
4956 bfd_boolean resolved_to_zero;
4958 globals = elf_aarch64_hash_table (info);
4960 symtab_hdr = &elf_symtab_hdr (input_bfd);
4962 BFD_ASSERT (is_aarch64_elf (input_bfd));
4964 r_symndx = ELFNN_R_SYM (rel->r_info);
4966 place = input_section->output_section->vma
4967 + input_section->output_offset + rel->r_offset;
4969 /* Get addend, accumulating the addend for consecutive relocs
4970 which refer to the same offset. */
4971 signed_addend = saved_addend ? *saved_addend : 0;
4972 signed_addend += rel->r_addend;
4974 weak_undef_p = (h ? h->root.type == bfd_link_hash_undefweak
4975 : bfd_is_und_section (sym_sec));
4977 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
4978 it here if it is defined in a non-shared object. */
4979 if (h != NULL
4980 && h->type == STT_GNU_IFUNC
4981 && h->def_regular)
4983 asection *plt;
4984 const char *name;
4985 bfd_vma addend = 0;
4987 if ((input_section->flags & SEC_ALLOC) == 0)
4989 /* Dynamic relocs are not propagated for SEC_DEBUGGING
4990 sections because such sections are not SEC_ALLOC and
4991 thus ld.so will not process them. */
4992 if ((input_section->flags & SEC_DEBUGGING) != 0)
4993 return bfd_reloc_ok;
4995 if (h->root.root.string)
4996 name = h->root.root.string;
4997 else
4998 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, NULL);
4999 _bfd_error_handler
5000 /* xgettext:c-format */
5001 (_("%B(%A+%#Lx): unresolvable %s relocation against symbol `%s'"),
5002 input_bfd, input_section, rel->r_offset, howto->name, name);
5003 bfd_set_error (bfd_error_bad_value);
5004 return bfd_reloc_notsupported;
5006 else if (h->plt.offset == (bfd_vma) -1)
5007 goto bad_ifunc_reloc;
5009 /* STT_GNU_IFUNC symbol must go through PLT. */
5010 plt = globals->root.splt ? globals->root.splt : globals->root.iplt;
5011 value = (plt->output_section->vma + plt->output_offset + h->plt.offset);
5013 switch (bfd_r_type)
5015 default:
5016 bad_ifunc_reloc:
5017 if (h->root.root.string)
5018 name = h->root.root.string;
5019 else
5020 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
5021 NULL);
5022 _bfd_error_handler
5023 /* xgettext:c-format */
5024 (_("%B: relocation %s against STT_GNU_IFUNC "
5025 "symbol `%s' isn't handled by %s"), input_bfd,
5026 howto->name, name, __FUNCTION__);
5027 bfd_set_error (bfd_error_bad_value);
5028 return bfd_reloc_notsupported;
5030 case BFD_RELOC_AARCH64_NN:
5031 if (rel->r_addend != 0)
5033 if (h->root.root.string)
5034 name = h->root.root.string;
5035 else
5036 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
5037 sym, NULL);
5038 _bfd_error_handler
5039 /* xgettext:c-format */
5040 (_("%B: relocation %s against STT_GNU_IFUNC "
5041 "symbol `%s' has non-zero addend: %Ld"),
5042 input_bfd, howto->name, name, rel->r_addend);
5043 bfd_set_error (bfd_error_bad_value);
5044 return bfd_reloc_notsupported;
5047 /* Generate dynamic relocation only when there is a
5048 non-GOT reference in a shared object. */
5049 if (bfd_link_pic (info) && h->non_got_ref)
5051 Elf_Internal_Rela outrel;
5052 asection *sreloc;
5054 /* Need a dynamic relocation to get the real function
5055 address. */
5056 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
5057 info,
5058 input_section,
5059 rel->r_offset);
5060 if (outrel.r_offset == (bfd_vma) -1
5061 || outrel.r_offset == (bfd_vma) -2)
5062 abort ();
5064 outrel.r_offset += (input_section->output_section->vma
5065 + input_section->output_offset);
5067 if (h->dynindx == -1
5068 || h->forced_local
5069 || bfd_link_executable (info))
5071 /* This symbol is resolved locally. */
5072 outrel.r_info = ELFNN_R_INFO (0, AARCH64_R (IRELATIVE));
5073 outrel.r_addend = (h->root.u.def.value
5074 + h->root.u.def.section->output_section->vma
5075 + h->root.u.def.section->output_offset);
5077 else
5079 outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
5080 outrel.r_addend = 0;
5083 sreloc = globals->root.irelifunc;
5084 elf_append_rela (output_bfd, sreloc, &outrel);
5086 /* If this reloc is against an external symbol, we
5087 do not want to fiddle with the addend. Otherwise,
5088 we need to include the symbol value so that it
5089 becomes an addend for the dynamic reloc. For an
5090 internal symbol, we have updated addend. */
5091 return bfd_reloc_ok;
5093 /* FALLTHROUGH */
5094 case BFD_RELOC_AARCH64_CALL26:
5095 case BFD_RELOC_AARCH64_JUMP26:
5096 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5097 signed_addend,
5098 weak_undef_p);
5099 return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type,
5100 howto, value);
5101 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
5102 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
5103 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
5104 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
5105 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
5106 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
5107 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
5108 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
5109 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
5110 base_got = globals->root.sgot;
5111 off = h->got.offset;
5113 if (base_got == NULL)
5114 abort ();
5116 if (off == (bfd_vma) -1)
5118 bfd_vma plt_index;
5120 /* We can't use h->got.offset here to save state, or
5121 even just remember the offset, as finish_dynamic_symbol
5122 would use that as offset into .got. */
5124 if (globals->root.splt != NULL)
5126 plt_index = ((h->plt.offset - globals->plt_header_size) /
5127 globals->plt_entry_size);
5128 off = (plt_index + 3) * GOT_ENTRY_SIZE;
5129 base_got = globals->root.sgotplt;
5131 else
5133 plt_index = h->plt.offset / globals->plt_entry_size;
5134 off = plt_index * GOT_ENTRY_SIZE;
5135 base_got = globals->root.igotplt;
5138 if (h->dynindx == -1
5139 || h->forced_local
5140 || info->symbolic)
5142 /* This references the local definition. We must
5143 initialize this entry in the global offset table.
5144 Since the offset must always be a multiple of 8,
5145 we use the least significant bit to record
5146 whether we have initialized it already.
5148 When doing a dynamic link, we create a .rela.got
5149 relocation entry to initialize the value. This
5150 is done in the finish_dynamic_symbol routine. */
5151 if ((off & 1) != 0)
5152 off &= ~1;
5153 else
5155 bfd_put_NN (output_bfd, value,
5156 base_got->contents + off);
5157 /* Note that this is harmless as -1 | 1 still is -1. */
5158 h->got.offset |= 1;
5161 value = (base_got->output_section->vma
5162 + base_got->output_offset + off);
5164 else
5165 value = aarch64_calculate_got_entry_vma (h, globals, info,
5166 value, output_bfd,
5167 unresolved_reloc_p);
5169 if (aarch64_relocation_aginst_gp_p (bfd_r_type))
5170 addend = (globals->root.sgot->output_section->vma
5171 + globals->root.sgot->output_offset);
5173 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5174 addend, weak_undef_p);
5175 return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type, howto, value);
5176 case BFD_RELOC_AARCH64_ADD_LO12:
5177 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
5178 break;
5182 resolved_to_zero = (h != NULL
5183 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
5185 switch (bfd_r_type)
5187 case BFD_RELOC_AARCH64_NONE:
5188 case BFD_RELOC_AARCH64_TLSDESC_ADD:
5189 case BFD_RELOC_AARCH64_TLSDESC_CALL:
5190 case BFD_RELOC_AARCH64_TLSDESC_LDR:
5191 *unresolved_reloc_p = FALSE;
5192 return bfd_reloc_ok;
5194 case BFD_RELOC_AARCH64_NN:
5196 /* When generating a shared object or relocatable executable, these
5197 relocations are copied into the output file to be resolved at
5198 run time. */
5199 if (((bfd_link_pic (info)
5200 || globals->root.is_relocatable_executable)
5201 && (input_section->flags & SEC_ALLOC)
5202 && (h == NULL
5203 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5204 && !resolved_to_zero)
5205 || h->root.type != bfd_link_hash_undefweak))
5206 /* Or we are creating an executable, we may need to keep relocations
5207 for symbols satisfied by a dynamic library if we manage to avoid
5208 copy relocs for the symbol. */
5209 || (ELIMINATE_COPY_RELOCS
5210 && !bfd_link_pic (info)
5211 && h != NULL
5212 && (input_section->flags & SEC_ALLOC)
5213 && h->dynindx != -1
5214 && !h->non_got_ref
5215 && ((h->def_dynamic
5216 && !h->def_regular)
5217 || h->root.type == bfd_link_hash_undefweak
5218 || h->root.type == bfd_link_hash_undefined)))
5220 Elf_Internal_Rela outrel;
5221 bfd_byte *loc;
5222 bfd_boolean skip, relocate;
5223 asection *sreloc;
5225 *unresolved_reloc_p = FALSE;
5227 skip = FALSE;
5228 relocate = FALSE;
5230 outrel.r_addend = signed_addend;
5231 outrel.r_offset =
5232 _bfd_elf_section_offset (output_bfd, info, input_section,
5233 rel->r_offset);
5234 if (outrel.r_offset == (bfd_vma) - 1)
5235 skip = TRUE;
5236 else if (outrel.r_offset == (bfd_vma) - 2)
5238 skip = TRUE;
5239 relocate = TRUE;
5242 outrel.r_offset += (input_section->output_section->vma
5243 + input_section->output_offset);
5245 if (skip)
5246 memset (&outrel, 0, sizeof outrel);
5247 else if (h != NULL
5248 && h->dynindx != -1
5249 && (!bfd_link_pic (info)
5250 || !(bfd_link_pie (info)
5251 || SYMBOLIC_BIND (info, h))
5252 || !h->def_regular))
5253 outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
5254 else
5256 int symbol;
5258 /* On SVR4-ish systems, the dynamic loader cannot
5259 relocate the text and data segments independently,
5260 so the symbol does not matter. */
5261 symbol = 0;
5262 relocate = globals->no_apply_dynamic_relocs ? FALSE : TRUE;
5263 outrel.r_info = ELFNN_R_INFO (symbol, AARCH64_R (RELATIVE));
5264 outrel.r_addend += value;
5267 sreloc = elf_section_data (input_section)->sreloc;
5268 if (sreloc == NULL || sreloc->contents == NULL)
5269 return bfd_reloc_notsupported;
5271 loc = sreloc->contents + sreloc->reloc_count++ * RELOC_SIZE (globals);
5272 bfd_elfNN_swap_reloca_out (output_bfd, &outrel, loc);
5274 if (sreloc->reloc_count * RELOC_SIZE (globals) > sreloc->size)
5276 /* Sanity to check that we have previously allocated
5277 sufficient space in the relocation section for the
5278 number of relocations we actually want to emit. */
5279 abort ();
5282 /* If this reloc is against an external symbol, we do not want to
5283 fiddle with the addend. Otherwise, we need to include the symbol
5284 value so that it becomes an addend for the dynamic reloc. */
5285 if (!relocate)
5286 return bfd_reloc_ok;
5288 return _bfd_final_link_relocate (howto, input_bfd, input_section,
5289 contents, rel->r_offset, value,
5290 signed_addend);
5292 else
5293 value += signed_addend;
5294 break;
5296 case BFD_RELOC_AARCH64_CALL26:
5297 case BFD_RELOC_AARCH64_JUMP26:
5299 asection *splt = globals->root.splt;
5300 bfd_boolean via_plt_p =
5301 splt != NULL && h != NULL && h->plt.offset != (bfd_vma) - 1;
5303 /* A call to an undefined weak symbol is converted to a jump to
5304 the next instruction unless a PLT entry will be created.
5305 The jump to the next instruction is optimized as a NOP.
5306 Do the same for local undefined symbols. */
5307 if (weak_undef_p && ! via_plt_p)
5309 bfd_putl32 (INSN_NOP, hit_data);
5310 return bfd_reloc_ok;
5313 /* If the call goes through a PLT entry, make sure to
5314 check distance to the right destination address. */
5315 if (via_plt_p)
5316 value = (splt->output_section->vma
5317 + splt->output_offset + h->plt.offset);
5319 /* Check if a stub has to be inserted because the destination
5320 is too far away. */
5321 struct elf_aarch64_stub_hash_entry *stub_entry = NULL;
5323 /* If the branch destination is directed to plt stub, "value" will be
5324 the final destination, otherwise we should plus signed_addend, it may
5325 contain non-zero value, for example call to local function symbol
5326 which are turned into "sec_sym + sec_off", and sec_off is kept in
5327 signed_addend. */
5328 if (! aarch64_valid_branch_p (via_plt_p ? value : value + signed_addend,
5329 place))
5330 /* The target is out of reach, so redirect the branch to
5331 the local stub for this function. */
5332 stub_entry = elfNN_aarch64_get_stub_entry (input_section, sym_sec, h,
5333 rel, globals);
5334 if (stub_entry != NULL)
5336 value = (stub_entry->stub_offset
5337 + stub_entry->stub_sec->output_offset
5338 + stub_entry->stub_sec->output_section->vma);
5340 /* We have redirected the destination to stub entry address,
5341 so ignore any addend record in the original rela entry. */
5342 signed_addend = 0;
5345 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5346 signed_addend, weak_undef_p);
5347 *unresolved_reloc_p = FALSE;
5348 break;
5350 case BFD_RELOC_AARCH64_16_PCREL:
5351 case BFD_RELOC_AARCH64_32_PCREL:
5352 case BFD_RELOC_AARCH64_64_PCREL:
5353 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
5354 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
5355 case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
5356 case BFD_RELOC_AARCH64_LD_LO19_PCREL:
5357 if (bfd_link_pic (info)
5358 && (input_section->flags & SEC_ALLOC) != 0
5359 && (input_section->flags & SEC_READONLY) != 0
5360 && !SYMBOL_REFERENCES_LOCAL (info, h))
5362 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
5364 _bfd_error_handler
5365 /* xgettext:c-format */
5366 (_("%B: relocation %s against symbol `%s' which may bind "
5367 "externally can not be used when making a shared object; "
5368 "recompile with -fPIC"),
5369 input_bfd, elfNN_aarch64_howto_table[howto_index].name,
5370 h->root.root.string);
5371 bfd_set_error (bfd_error_bad_value);
5372 return bfd_reloc_notsupported;
5374 /* Fall through. */
5376 case BFD_RELOC_AARCH64_16:
5377 #if ARCH_SIZE == 64
5378 case BFD_RELOC_AARCH64_32:
5379 #endif
5380 case BFD_RELOC_AARCH64_ADD_LO12:
5381 case BFD_RELOC_AARCH64_BRANCH19:
5382 case BFD_RELOC_AARCH64_LDST128_LO12:
5383 case BFD_RELOC_AARCH64_LDST16_LO12:
5384 case BFD_RELOC_AARCH64_LDST32_LO12:
5385 case BFD_RELOC_AARCH64_LDST64_LO12:
5386 case BFD_RELOC_AARCH64_LDST8_LO12:
5387 case BFD_RELOC_AARCH64_MOVW_G0:
5388 case BFD_RELOC_AARCH64_MOVW_G0_NC:
5389 case BFD_RELOC_AARCH64_MOVW_G0_S:
5390 case BFD_RELOC_AARCH64_MOVW_G1:
5391 case BFD_RELOC_AARCH64_MOVW_G1_NC:
5392 case BFD_RELOC_AARCH64_MOVW_G1_S:
5393 case BFD_RELOC_AARCH64_MOVW_G2:
5394 case BFD_RELOC_AARCH64_MOVW_G2_NC:
5395 case BFD_RELOC_AARCH64_MOVW_G2_S:
5396 case BFD_RELOC_AARCH64_MOVW_G3:
5397 case BFD_RELOC_AARCH64_TSTBR14:
5398 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5399 signed_addend, weak_undef_p);
5400 break;
5402 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
5403 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
5404 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
5405 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
5406 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
5407 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
5408 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
5409 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
5410 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
5411 if (globals->root.sgot == NULL)
5412 BFD_ASSERT (h != NULL);
5414 relative_reloc = FALSE;
5415 if (h != NULL)
5417 bfd_vma addend = 0;
5419 /* If a symbol is not dynamic and is not undefined weak, bind it
5420 locally and generate a RELATIVE relocation under PIC mode.
5422 NOTE: one symbol may be referenced by several relocations, we
5423 should only generate one RELATIVE relocation for that symbol.
5424 Therefore, check GOT offset mark first. */
5425 if (h->dynindx == -1
5426 && !h->forced_local
5427 && h->root.type != bfd_link_hash_undefweak
5428 && bfd_link_pic (info)
5429 && !symbol_got_offset_mark_p (input_bfd, h, r_symndx))
5430 relative_reloc = TRUE;
5432 value = aarch64_calculate_got_entry_vma (h, globals, info, value,
5433 output_bfd,
5434 unresolved_reloc_p);
5435 /* Record the GOT entry address which will be used when generating
5436 RELATIVE relocation. */
5437 if (relative_reloc)
5438 got_entry_addr = value;
5440 if (aarch64_relocation_aginst_gp_p (bfd_r_type))
5441 addend = (globals->root.sgot->output_section->vma
5442 + globals->root.sgot->output_offset);
5443 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5444 addend, weak_undef_p);
5446 else
5448 bfd_vma addend = 0;
5449 struct elf_aarch64_local_symbol *locals
5450 = elf_aarch64_locals (input_bfd);
5452 if (locals == NULL)
5454 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
5455 _bfd_error_handler
5456 /* xgettext:c-format */
5457 (_("%B: Local symbol descriptor table be NULL when applying "
5458 "relocation %s against local symbol"),
5459 input_bfd, elfNN_aarch64_howto_table[howto_index].name);
5460 abort ();
5463 off = symbol_got_offset (input_bfd, h, r_symndx);
5464 base_got = globals->root.sgot;
5465 got_entry_addr = (base_got->output_section->vma
5466 + base_got->output_offset + off);
5468 if (!symbol_got_offset_mark_p (input_bfd, h, r_symndx))
5470 bfd_put_64 (output_bfd, value, base_got->contents + off);
5472 /* For local symbol, we have done absolute relocation in static
5473 linking stage. While for shared library, we need to update the
5474 content of GOT entry according to the shared object's runtime
5475 base address. So, we need to generate a R_AARCH64_RELATIVE reloc
5476 for dynamic linker. */
5477 if (bfd_link_pic (info))
5478 relative_reloc = TRUE;
5480 symbol_got_offset_mark (input_bfd, h, r_symndx);
5483 /* Update the relocation value to GOT entry addr as we have transformed
5484 the direct data access into indirect data access through GOT. */
5485 value = got_entry_addr;
5487 if (aarch64_relocation_aginst_gp_p (bfd_r_type))
5488 addend = base_got->output_section->vma + base_got->output_offset;
5490 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5491 addend, weak_undef_p);
5494 if (relative_reloc)
5496 asection *s;
5497 Elf_Internal_Rela outrel;
5499 s = globals->root.srelgot;
5500 if (s == NULL)
5501 abort ();
5503 outrel.r_offset = got_entry_addr;
5504 outrel.r_info = ELFNN_R_INFO (0, AARCH64_R (RELATIVE));
5505 outrel.r_addend = orig_value;
5506 elf_append_rela (output_bfd, s, &outrel);
5508 break;
5510 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
5511 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
5512 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
5513 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5514 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
5515 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5516 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
5517 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
5518 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
5519 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
5520 if (globals->root.sgot == NULL)
5521 return bfd_reloc_notsupported;
5523 value = (symbol_got_offset (input_bfd, h, r_symndx)
5524 + globals->root.sgot->output_section->vma
5525 + globals->root.sgot->output_offset);
5527 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5528 0, weak_undef_p);
5529 *unresolved_reloc_p = FALSE;
5530 break;
5532 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
5533 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
5534 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
5535 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
5536 if (globals->root.sgot == NULL)
5537 return bfd_reloc_notsupported;
5539 value = symbol_got_offset (input_bfd, h, r_symndx);
5540 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5541 0, weak_undef_p);
5542 *unresolved_reloc_p = FALSE;
5543 break;
5545 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12:
5546 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12:
5547 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
5548 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12:
5549 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC:
5550 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12:
5551 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC:
5552 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12:
5553 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC:
5554 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12:
5555 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC:
5556 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0:
5557 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
5558 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1:
5559 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC:
5560 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2:
5561 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5562 signed_addend - dtpoff_base (info),
5563 weak_undef_p);
5564 break;
5566 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
5567 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
5568 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5569 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
5570 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5571 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
5572 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5573 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
5574 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5575 signed_addend - tpoff_base (info),
5576 weak_undef_p);
5577 *unresolved_reloc_p = FALSE;
5578 break;
5580 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
5581 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
5582 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
5583 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
5584 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12:
5585 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
5586 if (globals->root.sgot == NULL)
5587 return bfd_reloc_notsupported;
5588 value = (symbol_tlsdesc_got_offset (input_bfd, h, r_symndx)
5589 + globals->root.sgotplt->output_section->vma
5590 + globals->root.sgotplt->output_offset
5591 + globals->sgotplt_jump_table_size);
5593 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5594 0, weak_undef_p);
5595 *unresolved_reloc_p = FALSE;
5596 break;
5598 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
5599 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
5600 if (globals->root.sgot == NULL)
5601 return bfd_reloc_notsupported;
5603 value = (symbol_tlsdesc_got_offset (input_bfd, h, r_symndx)
5604 + globals->root.sgotplt->output_section->vma
5605 + globals->root.sgotplt->output_offset
5606 + globals->sgotplt_jump_table_size);
5608 value -= (globals->root.sgot->output_section->vma
5609 + globals->root.sgot->output_offset);
5611 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5612 0, weak_undef_p);
5613 *unresolved_reloc_p = FALSE;
5614 break;
5616 default:
5617 return bfd_reloc_notsupported;
5620 if (saved_addend)
5621 *saved_addend = value;
5623 /* Only apply the final relocation in a sequence. */
5624 if (save_addend)
5625 return bfd_reloc_continue;
5627 return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type,
5628 howto, value);
5631 /* LP64 and ILP32 operates on x- and w-registers respectively.
5632 Next definitions take into account the difference between
5633 corresponding machine codes. R means x-register if the target
5634 arch is LP64, and w-register if the target is ILP32. */
5636 #if ARCH_SIZE == 64
5637 # define add_R0_R0 (0x91000000)
5638 # define add_R0_R0_R1 (0x8b000020)
5639 # define add_R0_R1 (0x91400020)
5640 # define ldr_R0 (0x58000000)
5641 # define ldr_R0_mask(i) (i & 0xffffffe0)
5642 # define ldr_R0_x0 (0xf9400000)
5643 # define ldr_hw_R0 (0xf2a00000)
5644 # define movk_R0 (0xf2800000)
5645 # define movz_R0 (0xd2a00000)
5646 # define movz_hw_R0 (0xd2c00000)
5647 #else /*ARCH_SIZE == 32 */
5648 # define add_R0_R0 (0x11000000)
5649 # define add_R0_R0_R1 (0x0b000020)
5650 # define add_R0_R1 (0x11400020)
5651 # define ldr_R0 (0x18000000)
5652 # define ldr_R0_mask(i) (i & 0xbfffffe0)
5653 # define ldr_R0_x0 (0xb9400000)
5654 # define ldr_hw_R0 (0x72a00000)
5655 # define movk_R0 (0x72800000)
5656 # define movz_R0 (0x52a00000)
5657 # define movz_hw_R0 (0x52c00000)
5658 #endif
5660 /* Handle TLS relaxations. Relaxing is possible for symbols that use
5661 R_AARCH64_TLSDESC_ADR_{PAGE, LD64_LO12_NC, ADD_LO12_NC} during a static
5662 link.
5664 Return bfd_reloc_ok if we're done, bfd_reloc_continue if the caller
5665 is to then call final_link_relocate. Return other values in the
5666 case of error. */
5668 static bfd_reloc_status_type
5669 elfNN_aarch64_tls_relax (struct elf_aarch64_link_hash_table *globals,
5670 bfd *input_bfd, bfd_byte *contents,
5671 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h)
5673 bfd_boolean is_local = h == NULL;
5674 unsigned int r_type = ELFNN_R_TYPE (rel->r_info);
5675 unsigned long insn;
5677 BFD_ASSERT (globals && input_bfd && contents && rel);
5679 switch (elfNN_aarch64_bfd_reloc_from_type (r_type))
5681 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
5682 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
5683 if (is_local)
5685 /* GD->LE relaxation:
5686 adrp x0, :tlsgd:var => movz R0, :tprel_g1:var
5688 adrp x0, :tlsdesc:var => movz R0, :tprel_g1:var
5690 Where R is x for LP64, and w for ILP32. */
5691 bfd_putl32 (movz_R0, contents + rel->r_offset);
5692 return bfd_reloc_continue;
5694 else
5696 /* GD->IE relaxation:
5697 adrp x0, :tlsgd:var => adrp x0, :gottprel:var
5699 adrp x0, :tlsdesc:var => adrp x0, :gottprel:var
5701 return bfd_reloc_continue;
5704 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
5705 BFD_ASSERT (0);
5706 break;
5708 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
5709 if (is_local)
5711 /* Tiny TLSDESC->LE relaxation:
5712 ldr x1, :tlsdesc:var => movz R0, #:tprel_g1:var
5713 adr x0, :tlsdesc:var => movk R0, #:tprel_g0_nc:var
5714 .tlsdesccall var
5715 blr x1 => nop
5717 Where R is x for LP64, and w for ILP32. */
5718 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (TLSDESC_ADR_PREL21));
5719 BFD_ASSERT (ELFNN_R_TYPE (rel[2].r_info) == AARCH64_R (TLSDESC_CALL));
5721 rel[1].r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
5722 AARCH64_R (TLSLE_MOVW_TPREL_G0_NC));
5723 rel[2].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5725 bfd_putl32 (movz_R0, contents + rel->r_offset);
5726 bfd_putl32 (movk_R0, contents + rel->r_offset + 4);
5727 bfd_putl32 (INSN_NOP, contents + rel->r_offset + 8);
5728 return bfd_reloc_continue;
5730 else
5732 /* Tiny TLSDESC->IE relaxation:
5733 ldr x1, :tlsdesc:var => ldr x0, :gottprel:var
5734 adr x0, :tlsdesc:var => nop
5735 .tlsdesccall var
5736 blr x1 => nop
5738 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (TLSDESC_ADR_PREL21));
5739 BFD_ASSERT (ELFNN_R_TYPE (rel[2].r_info) == AARCH64_R (TLSDESC_CALL));
5741 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5742 rel[2].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5744 bfd_putl32 (ldr_R0, contents + rel->r_offset);
5745 bfd_putl32 (INSN_NOP, contents + rel->r_offset + 4);
5746 bfd_putl32 (INSN_NOP, contents + rel->r_offset + 8);
5747 return bfd_reloc_continue;
5750 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
5751 if (is_local)
5753 /* Tiny GD->LE relaxation:
5754 adr x0, :tlsgd:var => mrs x1, tpidr_el0
5755 bl __tls_get_addr => add R0, R1, #:tprel_hi12:x, lsl #12
5756 nop => add R0, R0, #:tprel_lo12_nc:x
5758 Where R is x for LP64, and x for Ilp32. */
5760 /* First kill the tls_get_addr reloc on the bl instruction. */
5761 BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
5763 bfd_putl32 (0xd53bd041, contents + rel->r_offset + 0);
5764 bfd_putl32 (add_R0_R1, contents + rel->r_offset + 4);
5765 bfd_putl32 (add_R0_R0, contents + rel->r_offset + 8);
5767 rel[1].r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
5768 AARCH64_R (TLSLE_ADD_TPREL_LO12_NC));
5769 rel[1].r_offset = rel->r_offset + 8;
5771 /* Move the current relocation to the second instruction in
5772 the sequence. */
5773 rel->r_offset += 4;
5774 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
5775 AARCH64_R (TLSLE_ADD_TPREL_HI12));
5776 return bfd_reloc_continue;
5778 else
5780 /* Tiny GD->IE relaxation:
5781 adr x0, :tlsgd:var => ldr R0, :gottprel:var
5782 bl __tls_get_addr => mrs x1, tpidr_el0
5783 nop => add R0, R0, R1
5785 Where R is x for LP64, and w for Ilp32. */
5787 /* First kill the tls_get_addr reloc on the bl instruction. */
5788 BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
5789 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5791 bfd_putl32 (ldr_R0, contents + rel->r_offset);
5792 bfd_putl32 (0xd53bd041, contents + rel->r_offset + 4);
5793 bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 8);
5794 return bfd_reloc_continue;
5797 #if ARCH_SIZE == 64
5798 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
5799 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (TLSGD_MOVW_G0_NC));
5800 BFD_ASSERT (rel->r_offset + 12 == rel[2].r_offset);
5801 BFD_ASSERT (ELFNN_R_TYPE (rel[2].r_info) == AARCH64_R (CALL26));
5803 if (is_local)
5805 /* Large GD->LE relaxation:
5806 movz x0, #:tlsgd_g1:var => movz x0, #:tprel_g2:var, lsl #32
5807 movk x0, #:tlsgd_g0_nc:var => movk x0, #:tprel_g1_nc:var, lsl #16
5808 add x0, gp, x0 => movk x0, #:tprel_g0_nc:var
5809 bl __tls_get_addr => mrs x1, tpidr_el0
5810 nop => add x0, x0, x1
5812 rel[2].r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
5813 AARCH64_R (TLSLE_MOVW_TPREL_G0_NC));
5814 rel[2].r_offset = rel->r_offset + 8;
5816 bfd_putl32 (movz_hw_R0, contents + rel->r_offset + 0);
5817 bfd_putl32 (ldr_hw_R0, contents + rel->r_offset + 4);
5818 bfd_putl32 (movk_R0, contents + rel->r_offset + 8);
5819 bfd_putl32 (0xd53bd041, contents + rel->r_offset + 12);
5820 bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 16);
5822 else
5824 /* Large GD->IE relaxation:
5825 movz x0, #:tlsgd_g1:var => movz x0, #:gottprel_g1:var, lsl #16
5826 movk x0, #:tlsgd_g0_nc:var => movk x0, #:gottprel_g0_nc:var
5827 add x0, gp, x0 => ldr x0, [gp, x0]
5828 bl __tls_get_addr => mrs x1, tpidr_el0
5829 nop => add x0, x0, x1
5831 rel[2].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5832 bfd_putl32 (0xd2a80000, contents + rel->r_offset + 0);
5833 bfd_putl32 (ldr_R0, contents + rel->r_offset + 8);
5834 bfd_putl32 (0xd53bd041, contents + rel->r_offset + 12);
5835 bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 16);
5837 return bfd_reloc_continue;
5839 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
5840 return bfd_reloc_continue;
5841 #endif
5843 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
5844 return bfd_reloc_continue;
5846 case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
5847 if (is_local)
5849 /* GD->LE relaxation:
5850 ldr xd, [x0, #:tlsdesc_lo12:var] => movk x0, :tprel_g0_nc:var
5852 Where R is x for lp64 mode, and w for ILP32 mode. */
5853 bfd_putl32 (movk_R0, contents + rel->r_offset);
5854 return bfd_reloc_continue;
5856 else
5858 /* GD->IE relaxation:
5859 ldr xd, [x0, #:tlsdesc_lo12:var] => ldr R0, [x0, #:gottprel_lo12:var]
5861 Where R is x for lp64 mode, and w for ILP32 mode. */
5862 insn = bfd_getl32 (contents + rel->r_offset);
5863 bfd_putl32 (ldr_R0_mask (insn), contents + rel->r_offset);
5864 return bfd_reloc_continue;
5867 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
5868 if (is_local)
5870 /* GD->LE relaxation
5871 add x0, #:tlsgd_lo12:var => movk R0, :tprel_g0_nc:var
5872 bl __tls_get_addr => mrs x1, tpidr_el0
5873 nop => add R0, R1, R0
5875 Where R is x for lp64 mode, and w for ILP32 mode. */
5877 /* First kill the tls_get_addr reloc on the bl instruction. */
5878 BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
5879 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5881 bfd_putl32 (movk_R0, contents + rel->r_offset);
5882 bfd_putl32 (0xd53bd041, contents + rel->r_offset + 4);
5883 bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 8);
5884 return bfd_reloc_continue;
5886 else
5888 /* GD->IE relaxation
5889 ADD x0, #:tlsgd_lo12:var => ldr R0, [x0, #:gottprel_lo12:var]
5890 BL __tls_get_addr => mrs x1, tpidr_el0
5891 R_AARCH64_CALL26
5892 NOP => add R0, R1, R0
5894 Where R is x for lp64 mode, and w for ilp32 mode. */
5896 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
5898 /* Remove the relocation on the BL instruction. */
5899 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5901 /* We choose to fixup the BL and NOP instructions using the
5902 offset from the second relocation to allow flexibility in
5903 scheduling instructions between the ADD and BL. */
5904 bfd_putl32 (ldr_R0_x0, contents + rel->r_offset);
5905 bfd_putl32 (0xd53bd041, contents + rel[1].r_offset);
5906 bfd_putl32 (add_R0_R0_R1, contents + rel[1].r_offset + 4);
5907 return bfd_reloc_continue;
5910 case BFD_RELOC_AARCH64_TLSDESC_ADD:
5911 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
5912 case BFD_RELOC_AARCH64_TLSDESC_CALL:
5913 /* GD->IE/LE relaxation:
5914 add x0, x0, #:tlsdesc_lo12:var => nop
5915 blr xd => nop
5917 bfd_putl32 (INSN_NOP, contents + rel->r_offset);
5918 return bfd_reloc_ok;
5920 case BFD_RELOC_AARCH64_TLSDESC_LDR:
5921 if (is_local)
5923 /* GD->LE relaxation:
5924 ldr xd, [gp, xn] => movk R0, #:tprel_g0_nc:var
5926 Where R is x for lp64 mode, and w for ILP32 mode. */
5927 bfd_putl32 (movk_R0, contents + rel->r_offset);
5928 return bfd_reloc_continue;
5930 else
5932 /* GD->IE relaxation:
5933 ldr xd, [gp, xn] => ldr R0, [gp, xn]
5935 Where R is x for lp64 mode, and w for ILP32 mode. */
5936 insn = bfd_getl32 (contents + rel->r_offset);
5937 bfd_putl32 (ldr_R0_mask (insn), contents + rel->r_offset);
5938 return bfd_reloc_ok;
5941 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
5942 /* GD->LE relaxation:
5943 movk xd, #:tlsdesc_off_g0_nc:var => movk R0, #:tprel_g1_nc:var, lsl #16
5944 GD->IE relaxation:
5945 movk xd, #:tlsdesc_off_g0_nc:var => movk Rd, #:gottprel_g0_nc:var
5947 Where R is x for lp64 mode, and w for ILP32 mode. */
5948 if (is_local)
5949 bfd_putl32 (ldr_hw_R0, contents + rel->r_offset);
5950 return bfd_reloc_continue;
5952 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
5953 if (is_local)
5955 /* GD->LE relaxation:
5956 movz xd, #:tlsdesc_off_g1:var => movz R0, #:tprel_g2:var, lsl #32
5958 Where R is x for lp64 mode, and w for ILP32 mode. */
5959 bfd_putl32 (movz_hw_R0, contents + rel->r_offset);
5960 return bfd_reloc_continue;
5962 else
5964 /* GD->IE relaxation:
5965 movz xd, #:tlsdesc_off_g1:var => movz Rd, #:gottprel_g1:var, lsl #16
5967 Where R is x for lp64 mode, and w for ILP32 mode. */
5968 insn = bfd_getl32 (contents + rel->r_offset);
5969 bfd_putl32 (movz_R0 | (insn & 0x1f), contents + rel->r_offset);
5970 return bfd_reloc_continue;
5973 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5974 /* IE->LE relaxation:
5975 adrp xd, :gottprel:var => movz Rd, :tprel_g1:var
5977 Where R is x for lp64 mode, and w for ILP32 mode. */
5978 if (is_local)
5980 insn = bfd_getl32 (contents + rel->r_offset);
5981 bfd_putl32 (movz_R0 | (insn & 0x1f), contents + rel->r_offset);
5983 return bfd_reloc_continue;
5985 case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
5986 /* IE->LE relaxation:
5987 ldr xd, [xm, #:gottprel_lo12:var] => movk Rd, :tprel_g0_nc:var
5989 Where R is x for lp64 mode, and w for ILP32 mode. */
5990 if (is_local)
5992 insn = bfd_getl32 (contents + rel->r_offset);
5993 bfd_putl32 (movk_R0 | (insn & 0x1f), contents + rel->r_offset);
5995 return bfd_reloc_continue;
5997 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
5998 /* LD->LE relaxation (tiny):
5999 adr x0, :tlsldm:x => mrs x0, tpidr_el0
6000 bl __tls_get_addr => add R0, R0, TCB_SIZE
6002 Where R is x for lp64 mode, and w for ilp32 mode. */
6003 if (is_local)
6005 BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
6006 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
6007 /* No need of CALL26 relocation for tls_get_addr. */
6008 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
6009 bfd_putl32 (0xd53bd040, contents + rel->r_offset + 0);
6010 bfd_putl32 (add_R0_R0 | (TCB_SIZE << 10),
6011 contents + rel->r_offset + 4);
6012 return bfd_reloc_ok;
6014 return bfd_reloc_continue;
6016 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
6017 /* LD->LE relaxation (small):
6018 adrp x0, :tlsldm:x => mrs x0, tpidr_el0
6020 if (is_local)
6022 bfd_putl32 (0xd53bd040, contents + rel->r_offset);
6023 return bfd_reloc_ok;
6025 return bfd_reloc_continue;
6027 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
6028 /* LD->LE relaxation (small):
6029 add x0, #:tlsldm_lo12:x => add R0, R0, TCB_SIZE
6030 bl __tls_get_addr => nop
6032 Where R is x for lp64 mode, and w for ilp32 mode. */
6033 if (is_local)
6035 BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
6036 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
6037 /* No need of CALL26 relocation for tls_get_addr. */
6038 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
6039 bfd_putl32 (add_R0_R0 | (TCB_SIZE << 10),
6040 contents + rel->r_offset + 0);
6041 bfd_putl32 (INSN_NOP, contents + rel->r_offset + 4);
6042 return bfd_reloc_ok;
6044 return bfd_reloc_continue;
6046 default:
6047 return bfd_reloc_continue;
6050 return bfd_reloc_ok;
6053 /* Relocate an AArch64 ELF section. */
6055 static bfd_boolean
6056 elfNN_aarch64_relocate_section (bfd *output_bfd,
6057 struct bfd_link_info *info,
6058 bfd *input_bfd,
6059 asection *input_section,
6060 bfd_byte *contents,
6061 Elf_Internal_Rela *relocs,
6062 Elf_Internal_Sym *local_syms,
6063 asection **local_sections)
6065 Elf_Internal_Shdr *symtab_hdr;
6066 struct elf_link_hash_entry **sym_hashes;
6067 Elf_Internal_Rela *rel;
6068 Elf_Internal_Rela *relend;
6069 const char *name;
6070 struct elf_aarch64_link_hash_table *globals;
6071 bfd_boolean save_addend = FALSE;
6072 bfd_vma addend = 0;
6074 globals = elf_aarch64_hash_table (info);
6076 symtab_hdr = &elf_symtab_hdr (input_bfd);
6077 sym_hashes = elf_sym_hashes (input_bfd);
6079 rel = relocs;
6080 relend = relocs + input_section->reloc_count;
6081 for (; rel < relend; rel++)
6083 unsigned int r_type;
6084 bfd_reloc_code_real_type bfd_r_type;
6085 bfd_reloc_code_real_type relaxed_bfd_r_type;
6086 reloc_howto_type *howto;
6087 unsigned long r_symndx;
6088 Elf_Internal_Sym *sym;
6089 asection *sec;
6090 struct elf_link_hash_entry *h;
6091 bfd_vma relocation;
6092 bfd_reloc_status_type r;
6093 arelent bfd_reloc;
6094 char sym_type;
6095 bfd_boolean unresolved_reloc = FALSE;
6096 char *error_message = NULL;
6098 r_symndx = ELFNN_R_SYM (rel->r_info);
6099 r_type = ELFNN_R_TYPE (rel->r_info);
6101 howto = bfd_reloc.howto = elfNN_aarch64_howto_from_type (r_type);
6103 if (howto == NULL)
6104 return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
6106 bfd_r_type = elfNN_aarch64_bfd_reloc_from_howto (howto);
6108 h = NULL;
6109 sym = NULL;
6110 sec = NULL;
6112 if (r_symndx < symtab_hdr->sh_info)
6114 sym = local_syms + r_symndx;
6115 sym_type = ELFNN_ST_TYPE (sym->st_info);
6116 sec = local_sections[r_symndx];
6118 /* An object file might have a reference to a local
6119 undefined symbol. This is a daft object file, but we
6120 should at least do something about it. */
6121 if (r_type != R_AARCH64_NONE && r_type != R_AARCH64_NULL
6122 && bfd_is_und_section (sec)
6123 && ELF_ST_BIND (sym->st_info) != STB_WEAK)
6124 (*info->callbacks->undefined_symbol)
6125 (info, bfd_elf_string_from_elf_section
6126 (input_bfd, symtab_hdr->sh_link, sym->st_name),
6127 input_bfd, input_section, rel->r_offset, TRUE);
6129 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
6131 /* Relocate against local STT_GNU_IFUNC symbol. */
6132 if (!bfd_link_relocatable (info)
6133 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
6135 h = elfNN_aarch64_get_local_sym_hash (globals, input_bfd,
6136 rel, FALSE);
6137 if (h == NULL)
6138 abort ();
6140 /* Set STT_GNU_IFUNC symbol value. */
6141 h->root.u.def.value = sym->st_value;
6142 h->root.u.def.section = sec;
6145 else
6147 bfd_boolean warned, ignored;
6149 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
6150 r_symndx, symtab_hdr, sym_hashes,
6151 h, sec, relocation,
6152 unresolved_reloc, warned, ignored);
6154 sym_type = h->type;
6157 if (sec != NULL && discarded_section (sec))
6158 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
6159 rel, 1, relend, howto, 0, contents);
6161 if (bfd_link_relocatable (info))
6162 continue;
6164 if (h != NULL)
6165 name = h->root.root.string;
6166 else
6168 name = (bfd_elf_string_from_elf_section
6169 (input_bfd, symtab_hdr->sh_link, sym->st_name));
6170 if (name == NULL || *name == '\0')
6171 name = bfd_section_name (input_bfd, sec);
6174 if (r_symndx != 0
6175 && r_type != R_AARCH64_NONE
6176 && r_type != R_AARCH64_NULL
6177 && (h == NULL
6178 || h->root.type == bfd_link_hash_defined
6179 || h->root.type == bfd_link_hash_defweak)
6180 && IS_AARCH64_TLS_RELOC (bfd_r_type) != (sym_type == STT_TLS))
6182 _bfd_error_handler
6183 ((sym_type == STT_TLS
6184 /* xgettext:c-format */
6185 ? _("%B(%A+%#Lx): %s used with TLS symbol %s")
6186 /* xgettext:c-format */
6187 : _("%B(%A+%#Lx): %s used with non-TLS symbol %s")),
6188 input_bfd,
6189 input_section, rel->r_offset, howto->name, name);
6192 /* We relax only if we can see that there can be a valid transition
6193 from a reloc type to another.
6194 We call elfNN_aarch64_final_link_relocate unless we're completely
6195 done, i.e., the relaxation produced the final output we want. */
6197 relaxed_bfd_r_type = aarch64_tls_transition (input_bfd, info, r_type,
6198 h, r_symndx);
6199 if (relaxed_bfd_r_type != bfd_r_type)
6201 bfd_r_type = relaxed_bfd_r_type;
6202 howto = elfNN_aarch64_howto_from_bfd_reloc (bfd_r_type);
6203 BFD_ASSERT (howto != NULL);
6204 r_type = howto->type;
6205 r = elfNN_aarch64_tls_relax (globals, input_bfd, contents, rel, h);
6206 unresolved_reloc = 0;
6208 else
6209 r = bfd_reloc_continue;
6211 /* There may be multiple consecutive relocations for the
6212 same offset. In that case we are supposed to treat the
6213 output of each relocation as the addend for the next. */
6214 if (rel + 1 < relend
6215 && rel->r_offset == rel[1].r_offset
6216 && ELFNN_R_TYPE (rel[1].r_info) != R_AARCH64_NONE
6217 && ELFNN_R_TYPE (rel[1].r_info) != R_AARCH64_NULL)
6218 save_addend = TRUE;
6219 else
6220 save_addend = FALSE;
6222 if (r == bfd_reloc_continue)
6223 r = elfNN_aarch64_final_link_relocate (howto, input_bfd, output_bfd,
6224 input_section, contents, rel,
6225 relocation, info, sec,
6226 h, &unresolved_reloc,
6227 save_addend, &addend, sym);
6229 switch (elfNN_aarch64_bfd_reloc_from_type (r_type))
6231 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
6232 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
6233 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
6234 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
6235 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
6236 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
6237 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
6238 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
6239 if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
6241 bfd_boolean need_relocs = FALSE;
6242 bfd_byte *loc;
6243 int indx;
6244 bfd_vma off;
6246 off = symbol_got_offset (input_bfd, h, r_symndx);
6247 indx = h && h->dynindx != -1 ? h->dynindx : 0;
6249 need_relocs =
6250 (bfd_link_pic (info) || indx != 0) &&
6251 (h == NULL
6252 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6253 || h->root.type != bfd_link_hash_undefweak);
6255 BFD_ASSERT (globals->root.srelgot != NULL);
6257 if (need_relocs)
6259 Elf_Internal_Rela rela;
6260 rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLS_DTPMOD));
6261 rela.r_addend = 0;
6262 rela.r_offset = globals->root.sgot->output_section->vma +
6263 globals->root.sgot->output_offset + off;
6266 loc = globals->root.srelgot->contents;
6267 loc += globals->root.srelgot->reloc_count++
6268 * RELOC_SIZE (htab);
6269 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
6271 bfd_reloc_code_real_type real_type =
6272 elfNN_aarch64_bfd_reloc_from_type (r_type);
6274 if (real_type == BFD_RELOC_AARCH64_TLSLD_ADR_PREL21
6275 || real_type == BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21
6276 || real_type == BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC)
6278 /* For local dynamic, don't generate DTPREL in any case.
6279 Initialize the DTPREL slot into zero, so we get module
6280 base address when invoke runtime TLS resolver. */
6281 bfd_put_NN (output_bfd, 0,
6282 globals->root.sgot->contents + off
6283 + GOT_ENTRY_SIZE);
6285 else if (indx == 0)
6287 bfd_put_NN (output_bfd,
6288 relocation - dtpoff_base (info),
6289 globals->root.sgot->contents + off
6290 + GOT_ENTRY_SIZE);
6292 else
6294 /* This TLS symbol is global. We emit a
6295 relocation to fixup the tls offset at load
6296 time. */
6297 rela.r_info =
6298 ELFNN_R_INFO (indx, AARCH64_R (TLS_DTPREL));
6299 rela.r_addend = 0;
6300 rela.r_offset =
6301 (globals->root.sgot->output_section->vma
6302 + globals->root.sgot->output_offset + off
6303 + GOT_ENTRY_SIZE);
6305 loc = globals->root.srelgot->contents;
6306 loc += globals->root.srelgot->reloc_count++
6307 * RELOC_SIZE (globals);
6308 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
6309 bfd_put_NN (output_bfd, (bfd_vma) 0,
6310 globals->root.sgot->contents + off
6311 + GOT_ENTRY_SIZE);
6314 else
6316 bfd_put_NN (output_bfd, (bfd_vma) 1,
6317 globals->root.sgot->contents + off);
6318 bfd_put_NN (output_bfd,
6319 relocation - dtpoff_base (info),
6320 globals->root.sgot->contents + off
6321 + GOT_ENTRY_SIZE);
6324 symbol_got_offset_mark (input_bfd, h, r_symndx);
6326 break;
6328 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6329 case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
6330 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
6331 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
6332 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
6333 if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
6335 bfd_boolean need_relocs = FALSE;
6336 bfd_byte *loc;
6337 int indx;
6338 bfd_vma off;
6340 off = symbol_got_offset (input_bfd, h, r_symndx);
6342 indx = h && h->dynindx != -1 ? h->dynindx : 0;
6344 need_relocs =
6345 (bfd_link_pic (info) || indx != 0) &&
6346 (h == NULL
6347 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6348 || h->root.type != bfd_link_hash_undefweak);
6350 BFD_ASSERT (globals->root.srelgot != NULL);
6352 if (need_relocs)
6354 Elf_Internal_Rela rela;
6356 if (indx == 0)
6357 rela.r_addend = relocation - dtpoff_base (info);
6358 else
6359 rela.r_addend = 0;
6361 rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLS_TPREL));
6362 rela.r_offset = globals->root.sgot->output_section->vma +
6363 globals->root.sgot->output_offset + off;
6365 loc = globals->root.srelgot->contents;
6366 loc += globals->root.srelgot->reloc_count++
6367 * RELOC_SIZE (htab);
6369 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
6371 bfd_put_NN (output_bfd, rela.r_addend,
6372 globals->root.sgot->contents + off);
6374 else
6375 bfd_put_NN (output_bfd, relocation - tpoff_base (info),
6376 globals->root.sgot->contents + off);
6378 symbol_got_offset_mark (input_bfd, h, r_symndx);
6380 break;
6382 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
6383 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
6384 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
6385 case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
6386 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
6387 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
6388 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
6389 if (! symbol_tlsdesc_got_offset_mark_p (input_bfd, h, r_symndx))
6391 bfd_boolean need_relocs = FALSE;
6392 int indx = h && h->dynindx != -1 ? h->dynindx : 0;
6393 bfd_vma off = symbol_tlsdesc_got_offset (input_bfd, h, r_symndx);
6395 need_relocs = (h == NULL
6396 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6397 || h->root.type != bfd_link_hash_undefweak);
6399 BFD_ASSERT (globals->root.srelgot != NULL);
6400 BFD_ASSERT (globals->root.sgot != NULL);
6402 if (need_relocs)
6404 bfd_byte *loc;
6405 Elf_Internal_Rela rela;
6406 rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLSDESC));
6408 rela.r_addend = 0;
6409 rela.r_offset = (globals->root.sgotplt->output_section->vma
6410 + globals->root.sgotplt->output_offset
6411 + off + globals->sgotplt_jump_table_size);
6413 if (indx == 0)
6414 rela.r_addend = relocation - dtpoff_base (info);
6416 /* Allocate the next available slot in the PLT reloc
6417 section to hold our R_AARCH64_TLSDESC, the next
6418 available slot is determined from reloc_count,
6419 which we step. But note, reloc_count was
6420 artifically moved down while allocating slots for
6421 real PLT relocs such that all of the PLT relocs
6422 will fit above the initial reloc_count and the
6423 extra stuff will fit below. */
6424 loc = globals->root.srelplt->contents;
6425 loc += globals->root.srelplt->reloc_count++
6426 * RELOC_SIZE (globals);
6428 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
6430 bfd_put_NN (output_bfd, (bfd_vma) 0,
6431 globals->root.sgotplt->contents + off +
6432 globals->sgotplt_jump_table_size);
6433 bfd_put_NN (output_bfd, (bfd_vma) 0,
6434 globals->root.sgotplt->contents + off +
6435 globals->sgotplt_jump_table_size +
6436 GOT_ENTRY_SIZE);
6439 symbol_tlsdesc_got_offset_mark (input_bfd, h, r_symndx);
6441 break;
6442 default:
6443 break;
6446 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
6447 because such sections are not SEC_ALLOC and thus ld.so will
6448 not process them. */
6449 if (unresolved_reloc
6450 && !((input_section->flags & SEC_DEBUGGING) != 0
6451 && h->def_dynamic)
6452 && _bfd_elf_section_offset (output_bfd, info, input_section,
6453 +rel->r_offset) != (bfd_vma) - 1)
6455 _bfd_error_handler
6456 /* xgettext:c-format */
6457 (_("%B(%A+%#Lx): unresolvable %s relocation against symbol `%s'"),
6458 input_bfd, input_section, rel->r_offset, howto->name,
6459 h->root.root.string);
6460 return FALSE;
6463 if (r != bfd_reloc_ok && r != bfd_reloc_continue)
6465 bfd_reloc_code_real_type real_r_type
6466 = elfNN_aarch64_bfd_reloc_from_type (r_type);
6468 switch (r)
6470 case bfd_reloc_overflow:
6471 (*info->callbacks->reloc_overflow)
6472 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
6473 input_bfd, input_section, rel->r_offset);
6474 if (real_r_type == BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15
6475 || real_r_type == BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14)
6477 (*info->callbacks->warning)
6478 (info,
6479 _("Too many GOT entries for -fpic, "
6480 "please recompile with -fPIC"),
6481 name, input_bfd, input_section, rel->r_offset);
6482 return FALSE;
6484 /* Overflow can occur when a variable is referenced with a type
6485 that has a larger alignment than the type with which it was
6486 declared. eg:
6487 file1.c: extern int foo; int a (void) { return foo; }
6488 file2.c: char bar, foo, baz;
6489 If the variable is placed into a data section at an offset
6490 that is incompatible with the larger alignment requirement
6491 overflow will occur. (Strictly speaking this is not overflow
6492 but rather an alignment problem, but the bfd_reloc_ error
6493 enum does not have a value to cover that situation).
6495 Try to catch this situation here and provide a more helpful
6496 error message to the user. */
6497 if (addend & ((1 << howto->rightshift) - 1)
6498 /* FIXME: Are we testing all of the appropriate reloc
6499 types here ? */
6500 && (real_r_type == BFD_RELOC_AARCH64_LD_LO19_PCREL
6501 || real_r_type == BFD_RELOC_AARCH64_LDST16_LO12
6502 || real_r_type == BFD_RELOC_AARCH64_LDST32_LO12
6503 || real_r_type == BFD_RELOC_AARCH64_LDST64_LO12
6504 || real_r_type == BFD_RELOC_AARCH64_LDST128_LO12))
6506 info->callbacks->warning
6507 (info, _("One possible cause of this error is that the \
6508 symbol is being referenced in the indicated code as if it had a larger \
6509 alignment than was declared where it was defined."),
6510 name, input_bfd, input_section, rel->r_offset);
6512 break;
6514 case bfd_reloc_undefined:
6515 (*info->callbacks->undefined_symbol)
6516 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
6517 break;
6519 case bfd_reloc_outofrange:
6520 error_message = _("out of range");
6521 goto common_error;
6523 case bfd_reloc_notsupported:
6524 error_message = _("unsupported relocation");
6525 goto common_error;
6527 case bfd_reloc_dangerous:
6528 /* error_message should already be set. */
6529 goto common_error;
6531 default:
6532 error_message = _("unknown error");
6533 /* Fall through. */
6535 common_error:
6536 BFD_ASSERT (error_message != NULL);
6537 (*info->callbacks->reloc_dangerous)
6538 (info, error_message, input_bfd, input_section, rel->r_offset);
6539 break;
6543 if (!save_addend)
6544 addend = 0;
6547 return TRUE;
6550 /* Set the right machine number. */
6552 static bfd_boolean
6553 elfNN_aarch64_object_p (bfd *abfd)
6555 #if ARCH_SIZE == 32
6556 bfd_default_set_arch_mach (abfd, bfd_arch_aarch64, bfd_mach_aarch64_ilp32);
6557 #else
6558 bfd_default_set_arch_mach (abfd, bfd_arch_aarch64, bfd_mach_aarch64);
6559 #endif
6560 return TRUE;
6563 /* Function to keep AArch64 specific flags in the ELF header. */
6565 static bfd_boolean
6566 elfNN_aarch64_set_private_flags (bfd *abfd, flagword flags)
6568 if (elf_flags_init (abfd) && elf_elfheader (abfd)->e_flags != flags)
6571 else
6573 elf_elfheader (abfd)->e_flags = flags;
6574 elf_flags_init (abfd) = TRUE;
6577 return TRUE;
6580 /* Merge backend specific data from an object file to the output
6581 object file when linking. */
6583 static bfd_boolean
6584 elfNN_aarch64_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6586 bfd *obfd = info->output_bfd;
6587 flagword out_flags;
6588 flagword in_flags;
6589 bfd_boolean flags_compatible = TRUE;
6590 asection *sec;
6592 /* Check if we have the same endianess. */
6593 if (!_bfd_generic_verify_endian_match (ibfd, info))
6594 return FALSE;
6596 if (!is_aarch64_elf (ibfd) || !is_aarch64_elf (obfd))
6597 return TRUE;
6599 /* The input BFD must have had its flags initialised. */
6600 /* The following seems bogus to me -- The flags are initialized in
6601 the assembler but I don't think an elf_flags_init field is
6602 written into the object. */
6603 /* BFD_ASSERT (elf_flags_init (ibfd)); */
6605 in_flags = elf_elfheader (ibfd)->e_flags;
6606 out_flags = elf_elfheader (obfd)->e_flags;
6608 if (!elf_flags_init (obfd))
6610 /* If the input is the default architecture and had the default
6611 flags then do not bother setting the flags for the output
6612 architecture, instead allow future merges to do this. If no
6613 future merges ever set these flags then they will retain their
6614 uninitialised values, which surprise surprise, correspond
6615 to the default values. */
6616 if (bfd_get_arch_info (ibfd)->the_default
6617 && elf_elfheader (ibfd)->e_flags == 0)
6618 return TRUE;
6620 elf_flags_init (obfd) = TRUE;
6621 elf_elfheader (obfd)->e_flags = in_flags;
6623 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6624 && bfd_get_arch_info (obfd)->the_default)
6625 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6626 bfd_get_mach (ibfd));
6628 return TRUE;
6631 /* Identical flags must be compatible. */
6632 if (in_flags == out_flags)
6633 return TRUE;
6635 /* Check to see if the input BFD actually contains any sections. If
6636 not, its flags may not have been initialised either, but it
6637 cannot actually cause any incompatiblity. Do not short-circuit
6638 dynamic objects; their section list may be emptied by
6639 elf_link_add_object_symbols.
6641 Also check to see if there are no code sections in the input.
6642 In this case there is no need to check for code specific flags.
6643 XXX - do we need to worry about floating-point format compatability
6644 in data sections ? */
6645 if (!(ibfd->flags & DYNAMIC))
6647 bfd_boolean null_input_bfd = TRUE;
6648 bfd_boolean only_data_sections = TRUE;
6650 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6652 if ((bfd_get_section_flags (ibfd, sec)
6653 & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
6654 == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
6655 only_data_sections = FALSE;
6657 null_input_bfd = FALSE;
6658 break;
6661 if (null_input_bfd || only_data_sections)
6662 return TRUE;
6665 return flags_compatible;
6668 /* Display the flags field. */
6670 static bfd_boolean
6671 elfNN_aarch64_print_private_bfd_data (bfd *abfd, void *ptr)
6673 FILE *file = (FILE *) ptr;
6674 unsigned long flags;
6676 BFD_ASSERT (abfd != NULL && ptr != NULL);
6678 /* Print normal ELF private data. */
6679 _bfd_elf_print_private_bfd_data (abfd, ptr);
6681 flags = elf_elfheader (abfd)->e_flags;
6682 /* Ignore init flag - it may not be set, despite the flags field
6683 containing valid data. */
6685 /* xgettext:c-format */
6686 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
6688 if (flags)
6689 fprintf (file, _("<Unrecognised flag bits set>"));
6691 fputc ('\n', file);
6693 return TRUE;
6696 /* Return true if we need copy relocation against EH. */
6698 static bfd_boolean
6699 need_copy_relocation_p (struct elf_aarch64_link_hash_entry *eh)
6701 struct elf_dyn_relocs *p;
6702 asection *s;
6704 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6706 /* If there is any pc-relative reference, we need to keep copy relocation
6707 to avoid propagating the relocation into runtime that current glibc
6708 does not support. */
6709 if (p->pc_count)
6710 return TRUE;
6712 s = p->sec->output_section;
6713 /* Need copy relocation if it's against read-only section. */
6714 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6715 return TRUE;
6718 return FALSE;
6721 /* Adjust a symbol defined by a dynamic object and referenced by a
6722 regular object. The current definition is in some section of the
6723 dynamic object, but we're not including those sections. We have to
6724 change the definition to something the rest of the link can
6725 understand. */
6727 static bfd_boolean
6728 elfNN_aarch64_adjust_dynamic_symbol (struct bfd_link_info *info,
6729 struct elf_link_hash_entry *h)
6731 struct elf_aarch64_link_hash_table *htab;
6732 asection *s, *srel;
6734 /* If this is a function, put it in the procedure linkage table. We
6735 will fill in the contents of the procedure linkage table later,
6736 when we know the address of the .got section. */
6737 if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
6739 if (h->plt.refcount <= 0
6740 || (h->type != STT_GNU_IFUNC
6741 && (SYMBOL_CALLS_LOCAL (info, h)
6742 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6743 && h->root.type == bfd_link_hash_undefweak))))
6745 /* This case can occur if we saw a CALL26 reloc in
6746 an input file, but the symbol wasn't referred to
6747 by a dynamic object or all references were
6748 garbage collected. In which case we can end up
6749 resolving. */
6750 h->plt.offset = (bfd_vma) - 1;
6751 h->needs_plt = 0;
6754 return TRUE;
6756 else
6757 /* Otherwise, reset to -1. */
6758 h->plt.offset = (bfd_vma) - 1;
6761 /* If this is a weak symbol, and there is a real definition, the
6762 processor independent code will have arranged for us to see the
6763 real definition first, and we can just use the same value. */
6764 if (h->u.weakdef != NULL)
6766 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6767 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6768 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6769 h->root.u.def.value = h->u.weakdef->root.u.def.value;
6770 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
6771 h->non_got_ref = h->u.weakdef->non_got_ref;
6772 return TRUE;
6775 /* If we are creating a shared library, we must presume that the
6776 only references to the symbol are via the global offset table.
6777 For such cases we need not do anything here; the relocations will
6778 be handled correctly by relocate_section. */
6779 if (bfd_link_pic (info))
6780 return TRUE;
6782 /* If there are no references to this symbol that do not use the
6783 GOT, we don't need to generate a copy reloc. */
6784 if (!h->non_got_ref)
6785 return TRUE;
6787 /* If -z nocopyreloc was given, we won't generate them either. */
6788 if (info->nocopyreloc)
6790 h->non_got_ref = 0;
6791 return TRUE;
6794 if (ELIMINATE_COPY_RELOCS)
6796 struct elf_aarch64_link_hash_entry *eh;
6797 /* If we didn't find any dynamic relocs in read-only sections, then
6798 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6799 eh = (struct elf_aarch64_link_hash_entry *) h;
6800 if (!need_copy_relocation_p (eh))
6802 h->non_got_ref = 0;
6803 return TRUE;
6807 /* We must allocate the symbol in our .dynbss section, which will
6808 become part of the .bss section of the executable. There will be
6809 an entry for this symbol in the .dynsym section. The dynamic
6810 object will contain position independent code, so all references
6811 from the dynamic object to this symbol will go through the global
6812 offset table. The dynamic linker will use the .dynsym entry to
6813 determine the address it must put in the global offset table, so
6814 both the dynamic object and the regular object will refer to the
6815 same memory location for the variable. */
6817 htab = elf_aarch64_hash_table (info);
6819 /* We must generate a R_AARCH64_COPY reloc to tell the dynamic linker
6820 to copy the initial value out of the dynamic object and into the
6821 runtime process image. */
6822 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6824 s = htab->root.sdynrelro;
6825 srel = htab->root.sreldynrelro;
6827 else
6829 s = htab->root.sdynbss;
6830 srel = htab->root.srelbss;
6832 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6834 srel->size += RELOC_SIZE (htab);
6835 h->needs_copy = 1;
6838 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6842 static bfd_boolean
6843 elfNN_aarch64_allocate_local_symbols (bfd *abfd, unsigned number)
6845 struct elf_aarch64_local_symbol *locals;
6846 locals = elf_aarch64_locals (abfd);
6847 if (locals == NULL)
6849 locals = (struct elf_aarch64_local_symbol *)
6850 bfd_zalloc (abfd, number * sizeof (struct elf_aarch64_local_symbol));
6851 if (locals == NULL)
6852 return FALSE;
6853 elf_aarch64_locals (abfd) = locals;
6855 return TRUE;
6858 /* Create the .got section to hold the global offset table. */
6860 static bfd_boolean
6861 aarch64_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
6863 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6864 flagword flags;
6865 asection *s;
6866 struct elf_link_hash_entry *h;
6867 struct elf_link_hash_table *htab = elf_hash_table (info);
6869 /* This function may be called more than once. */
6870 if (htab->sgot != NULL)
6871 return TRUE;
6873 flags = bed->dynamic_sec_flags;
6875 s = bfd_make_section_anyway_with_flags (abfd,
6876 (bed->rela_plts_and_copies_p
6877 ? ".rela.got" : ".rel.got"),
6878 (bed->dynamic_sec_flags
6879 | SEC_READONLY));
6880 if (s == NULL
6881 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
6882 return FALSE;
6883 htab->srelgot = s;
6885 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
6886 if (s == NULL
6887 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
6888 return FALSE;
6889 htab->sgot = s;
6890 htab->sgot->size += GOT_ENTRY_SIZE;
6892 if (bed->want_got_sym)
6894 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
6895 (or .got.plt) section. We don't do this in the linker script
6896 because we don't want to define the symbol if we are not creating
6897 a global offset table. */
6898 h = _bfd_elf_define_linkage_sym (abfd, info, s,
6899 "_GLOBAL_OFFSET_TABLE_");
6900 elf_hash_table (info)->hgot = h;
6901 if (h == NULL)
6902 return FALSE;
6905 if (bed->want_got_plt)
6907 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
6908 if (s == NULL
6909 || !bfd_set_section_alignment (abfd, s,
6910 bed->s->log_file_align))
6911 return FALSE;
6912 htab->sgotplt = s;
6915 /* The first bit of the global offset table is the header. */
6916 s->size += bed->got_header_size;
6918 return TRUE;
6921 /* Look through the relocs for a section during the first phase. */
6923 static bfd_boolean
6924 elfNN_aarch64_check_relocs (bfd *abfd, struct bfd_link_info *info,
6925 asection *sec, const Elf_Internal_Rela *relocs)
6927 Elf_Internal_Shdr *symtab_hdr;
6928 struct elf_link_hash_entry **sym_hashes;
6929 const Elf_Internal_Rela *rel;
6930 const Elf_Internal_Rela *rel_end;
6931 asection *sreloc;
6933 struct elf_aarch64_link_hash_table *htab;
6935 if (bfd_link_relocatable (info))
6936 return TRUE;
6938 BFD_ASSERT (is_aarch64_elf (abfd));
6940 htab = elf_aarch64_hash_table (info);
6941 sreloc = NULL;
6943 symtab_hdr = &elf_symtab_hdr (abfd);
6944 sym_hashes = elf_sym_hashes (abfd);
6946 rel_end = relocs + sec->reloc_count;
6947 for (rel = relocs; rel < rel_end; rel++)
6949 struct elf_link_hash_entry *h;
6950 unsigned int r_symndx;
6951 unsigned int r_type;
6952 bfd_reloc_code_real_type bfd_r_type;
6953 Elf_Internal_Sym *isym;
6955 r_symndx = ELFNN_R_SYM (rel->r_info);
6956 r_type = ELFNN_R_TYPE (rel->r_info);
6958 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
6960 /* xgettext:c-format */
6961 _bfd_error_handler (_("%B: bad symbol index: %d"), abfd, r_symndx);
6962 return FALSE;
6965 if (r_symndx < symtab_hdr->sh_info)
6967 /* A local symbol. */
6968 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6969 abfd, r_symndx);
6970 if (isym == NULL)
6971 return FALSE;
6973 /* Check relocation against local STT_GNU_IFUNC symbol. */
6974 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
6976 h = elfNN_aarch64_get_local_sym_hash (htab, abfd, rel,
6977 TRUE);
6978 if (h == NULL)
6979 return FALSE;
6981 /* Fake a STT_GNU_IFUNC symbol. */
6982 h->type = STT_GNU_IFUNC;
6983 h->def_regular = 1;
6984 h->ref_regular = 1;
6985 h->forced_local = 1;
6986 h->root.type = bfd_link_hash_defined;
6988 else
6989 h = NULL;
6991 else
6993 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6994 while (h->root.type == bfd_link_hash_indirect
6995 || h->root.type == bfd_link_hash_warning)
6996 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6998 /* PR15323, ref flags aren't set for references in the same
6999 object. */
7000 h->root.non_ir_ref_regular = 1;
7003 /* Could be done earlier, if h were already available. */
7004 bfd_r_type = aarch64_tls_transition (abfd, info, r_type, h, r_symndx);
7006 if (h != NULL)
7008 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
7009 This shows up in particular in an R_AARCH64_PREL64 in large model
7010 when calculating the pc-relative address to .got section which is
7011 used to initialize the gp register. */
7012 if (h->root.root.string
7013 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
7015 if (htab->root.dynobj == NULL)
7016 htab->root.dynobj = abfd;
7018 if (! aarch64_elf_create_got_section (htab->root.dynobj, info))
7019 return FALSE;
7021 BFD_ASSERT (h == htab->root.hgot);
7024 /* Create the ifunc sections for static executables. If we
7025 never see an indirect function symbol nor we are building
7026 a static executable, those sections will be empty and
7027 won't appear in output. */
7028 switch (bfd_r_type)
7030 default:
7031 break;
7033 case BFD_RELOC_AARCH64_ADD_LO12:
7034 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
7035 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
7036 case BFD_RELOC_AARCH64_CALL26:
7037 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
7038 case BFD_RELOC_AARCH64_JUMP26:
7039 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
7040 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
7041 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
7042 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
7043 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
7044 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
7045 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
7046 case BFD_RELOC_AARCH64_NN:
7047 if (htab->root.dynobj == NULL)
7048 htab->root.dynobj = abfd;
7049 if (!_bfd_elf_create_ifunc_sections (htab->root.dynobj, info))
7050 return FALSE;
7051 break;
7054 /* It is referenced by a non-shared object. */
7055 h->ref_regular = 1;
7056 h->root.non_ir_ref_regular = 1;
7059 switch (bfd_r_type)
7061 case BFD_RELOC_AARCH64_MOVW_G0_NC:
7062 case BFD_RELOC_AARCH64_MOVW_G1_NC:
7063 case BFD_RELOC_AARCH64_MOVW_G2_NC:
7064 case BFD_RELOC_AARCH64_MOVW_G3:
7065 if (bfd_link_pic (info))
7067 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
7068 _bfd_error_handler
7069 /* xgettext:c-format */
7070 (_("%B: relocation %s against `%s' can not be used when making "
7071 "a shared object; recompile with -fPIC"),
7072 abfd, elfNN_aarch64_howto_table[howto_index].name,
7073 (h) ? h->root.root.string : "a local symbol");
7074 bfd_set_error (bfd_error_bad_value);
7075 return FALSE;
7077 /* Fall through. */
7079 case BFD_RELOC_AARCH64_16_PCREL:
7080 case BFD_RELOC_AARCH64_32_PCREL:
7081 case BFD_RELOC_AARCH64_64_PCREL:
7082 case BFD_RELOC_AARCH64_ADD_LO12:
7083 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
7084 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
7085 case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
7086 case BFD_RELOC_AARCH64_LDST128_LO12:
7087 case BFD_RELOC_AARCH64_LDST16_LO12:
7088 case BFD_RELOC_AARCH64_LDST32_LO12:
7089 case BFD_RELOC_AARCH64_LDST64_LO12:
7090 case BFD_RELOC_AARCH64_LDST8_LO12:
7091 case BFD_RELOC_AARCH64_LD_LO19_PCREL:
7092 if (h == NULL || bfd_link_pic (info))
7093 break;
7094 /* Fall through. */
7096 case BFD_RELOC_AARCH64_NN:
7098 /* We don't need to handle relocs into sections not going into
7099 the "real" output. */
7100 if ((sec->flags & SEC_ALLOC) == 0)
7101 break;
7103 if (h != NULL)
7105 if (!bfd_link_pic (info))
7106 h->non_got_ref = 1;
7108 h->plt.refcount += 1;
7109 h->pointer_equality_needed = 1;
7112 /* No need to do anything if we're not creating a shared
7113 object. */
7114 if (!(bfd_link_pic (info)
7115 /* If on the other hand, we are creating an executable, we
7116 may need to keep relocations for symbols satisfied by a
7117 dynamic library if we manage to avoid copy relocs for the
7118 symbol.
7120 NOTE: Currently, there is no support of copy relocs
7121 elimination on pc-relative relocation types, because there is
7122 no dynamic relocation support for them in glibc. We still
7123 record the dynamic symbol reference for them. This is
7124 because one symbol may be referenced by both absolute
7125 relocation (for example, BFD_RELOC_AARCH64_NN) and
7126 pc-relative relocation. We need full symbol reference
7127 information to make correct decision later in
7128 elfNN_aarch64_adjust_dynamic_symbol. */
7129 || (ELIMINATE_COPY_RELOCS
7130 && !bfd_link_pic (info)
7131 && h != NULL
7132 && (h->root.type == bfd_link_hash_defweak
7133 || !h->def_regular))))
7134 break;
7137 struct elf_dyn_relocs *p;
7138 struct elf_dyn_relocs **head;
7139 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
7141 /* We must copy these reloc types into the output file.
7142 Create a reloc section in dynobj and make room for
7143 this reloc. */
7144 if (sreloc == NULL)
7146 if (htab->root.dynobj == NULL)
7147 htab->root.dynobj = abfd;
7149 sreloc = _bfd_elf_make_dynamic_reloc_section
7150 (sec, htab->root.dynobj, LOG_FILE_ALIGN, abfd, /*rela? */ TRUE);
7152 if (sreloc == NULL)
7153 return FALSE;
7156 /* If this is a global symbol, we count the number of
7157 relocations we need for this symbol. */
7158 if (h != NULL)
7160 struct elf_aarch64_link_hash_entry *eh;
7161 eh = (struct elf_aarch64_link_hash_entry *) h;
7162 head = &eh->dyn_relocs;
7164 else
7166 /* Track dynamic relocs needed for local syms too.
7167 We really need local syms available to do this
7168 easily. Oh well. */
7170 asection *s;
7171 void **vpp;
7173 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
7174 abfd, r_symndx);
7175 if (isym == NULL)
7176 return FALSE;
7178 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
7179 if (s == NULL)
7180 s = sec;
7182 /* Beware of type punned pointers vs strict aliasing
7183 rules. */
7184 vpp = &(elf_section_data (s)->local_dynrel);
7185 head = (struct elf_dyn_relocs **) vpp;
7188 p = *head;
7189 if (p == NULL || p->sec != sec)
7191 bfd_size_type amt = sizeof *p;
7192 p = ((struct elf_dyn_relocs *)
7193 bfd_zalloc (htab->root.dynobj, amt));
7194 if (p == NULL)
7195 return FALSE;
7196 p->next = *head;
7197 *head = p;
7198 p->sec = sec;
7201 p->count += 1;
7203 if (elfNN_aarch64_howto_table[howto_index].pc_relative)
7204 p->pc_count += 1;
7206 break;
7208 /* RR: We probably want to keep a consistency check that
7209 there are no dangling GOT_PAGE relocs. */
7210 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
7211 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
7212 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
7213 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
7214 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
7215 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
7216 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
7217 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
7218 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
7219 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
7220 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
7221 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
7222 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
7223 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12:
7224 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
7225 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
7226 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
7227 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
7228 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
7229 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
7230 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
7231 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
7232 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
7233 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
7234 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
7235 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
7236 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
7237 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
7238 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
7239 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
7240 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
7241 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
7242 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
7243 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
7245 unsigned got_type;
7246 unsigned old_got_type;
7248 got_type = aarch64_reloc_got_type (bfd_r_type);
7250 if (h)
7252 h->got.refcount += 1;
7253 old_got_type = elf_aarch64_hash_entry (h)->got_type;
7255 else
7257 struct elf_aarch64_local_symbol *locals;
7259 if (!elfNN_aarch64_allocate_local_symbols
7260 (abfd, symtab_hdr->sh_info))
7261 return FALSE;
7263 locals = elf_aarch64_locals (abfd);
7264 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7265 locals[r_symndx].got_refcount += 1;
7266 old_got_type = locals[r_symndx].got_type;
7269 /* If a variable is accessed with both general dynamic TLS
7270 methods, two slots may be created. */
7271 if (GOT_TLS_GD_ANY_P (old_got_type) && GOT_TLS_GD_ANY_P (got_type))
7272 got_type |= old_got_type;
7274 /* We will already have issued an error message if there
7275 is a TLS/non-TLS mismatch, based on the symbol type.
7276 So just combine any TLS types needed. */
7277 if (old_got_type != GOT_UNKNOWN && old_got_type != GOT_NORMAL
7278 && got_type != GOT_NORMAL)
7279 got_type |= old_got_type;
7281 /* If the symbol is accessed by both IE and GD methods, we
7282 are able to relax. Turn off the GD flag, without
7283 messing up with any other kind of TLS types that may be
7284 involved. */
7285 if ((got_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (got_type))
7286 got_type &= ~ (GOT_TLSDESC_GD | GOT_TLS_GD);
7288 if (old_got_type != got_type)
7290 if (h != NULL)
7291 elf_aarch64_hash_entry (h)->got_type = got_type;
7292 else
7294 struct elf_aarch64_local_symbol *locals;
7295 locals = elf_aarch64_locals (abfd);
7296 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7297 locals[r_symndx].got_type = got_type;
7301 if (htab->root.dynobj == NULL)
7302 htab->root.dynobj = abfd;
7303 if (! aarch64_elf_create_got_section (htab->root.dynobj, info))
7304 return FALSE;
7305 break;
7308 case BFD_RELOC_AARCH64_CALL26:
7309 case BFD_RELOC_AARCH64_JUMP26:
7310 /* If this is a local symbol then we resolve it
7311 directly without creating a PLT entry. */
7312 if (h == NULL)
7313 continue;
7315 h->needs_plt = 1;
7316 if (h->plt.refcount <= 0)
7317 h->plt.refcount = 1;
7318 else
7319 h->plt.refcount += 1;
7320 break;
7322 default:
7323 break;
7327 return TRUE;
7330 /* Treat mapping symbols as special target symbols. */
7332 static bfd_boolean
7333 elfNN_aarch64_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED,
7334 asymbol *sym)
7336 return bfd_is_aarch64_special_symbol_name (sym->name,
7337 BFD_AARCH64_SPECIAL_SYM_TYPE_ANY);
7340 /* This is a copy of elf_find_function () from elf.c except that
7341 AArch64 mapping symbols are ignored when looking for function names. */
7343 static bfd_boolean
7344 aarch64_elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
7345 asymbol **symbols,
7346 asection *section,
7347 bfd_vma offset,
7348 const char **filename_ptr,
7349 const char **functionname_ptr)
7351 const char *filename = NULL;
7352 asymbol *func = NULL;
7353 bfd_vma low_func = 0;
7354 asymbol **p;
7356 for (p = symbols; *p != NULL; p++)
7358 elf_symbol_type *q;
7360 q = (elf_symbol_type *) * p;
7362 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
7364 default:
7365 break;
7366 case STT_FILE:
7367 filename = bfd_asymbol_name (&q->symbol);
7368 break;
7369 case STT_FUNC:
7370 case STT_NOTYPE:
7371 /* Skip mapping symbols. */
7372 if ((q->symbol.flags & BSF_LOCAL)
7373 && (bfd_is_aarch64_special_symbol_name
7374 (q->symbol.name, BFD_AARCH64_SPECIAL_SYM_TYPE_ANY)))
7375 continue;
7376 /* Fall through. */
7377 if (bfd_get_section (&q->symbol) == section
7378 && q->symbol.value >= low_func && q->symbol.value <= offset)
7380 func = (asymbol *) q;
7381 low_func = q->symbol.value;
7383 break;
7387 if (func == NULL)
7388 return FALSE;
7390 if (filename_ptr)
7391 *filename_ptr = filename;
7392 if (functionname_ptr)
7393 *functionname_ptr = bfd_asymbol_name (func);
7395 return TRUE;
7399 /* Find the nearest line to a particular section and offset, for error
7400 reporting. This code is a duplicate of the code in elf.c, except
7401 that it uses aarch64_elf_find_function. */
7403 static bfd_boolean
7404 elfNN_aarch64_find_nearest_line (bfd *abfd,
7405 asymbol **symbols,
7406 asection *section,
7407 bfd_vma offset,
7408 const char **filename_ptr,
7409 const char **functionname_ptr,
7410 unsigned int *line_ptr,
7411 unsigned int *discriminator_ptr)
7413 bfd_boolean found = FALSE;
7415 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
7416 filename_ptr, functionname_ptr,
7417 line_ptr, discriminator_ptr,
7418 dwarf_debug_sections, 0,
7419 &elf_tdata (abfd)->dwarf2_find_line_info))
7421 if (!*functionname_ptr)
7422 aarch64_elf_find_function (abfd, symbols, section, offset,
7423 *filename_ptr ? NULL : filename_ptr,
7424 functionname_ptr);
7426 return TRUE;
7429 /* Skip _bfd_dwarf1_find_nearest_line since no known AArch64
7430 toolchain uses DWARF1. */
7432 if (!_bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7433 &found, filename_ptr,
7434 functionname_ptr, line_ptr,
7435 &elf_tdata (abfd)->line_info))
7436 return FALSE;
7438 if (found && (*functionname_ptr || *line_ptr))
7439 return TRUE;
7441 if (symbols == NULL)
7442 return FALSE;
7444 if (!aarch64_elf_find_function (abfd, symbols, section, offset,
7445 filename_ptr, functionname_ptr))
7446 return FALSE;
7448 *line_ptr = 0;
7449 return TRUE;
7452 static bfd_boolean
7453 elfNN_aarch64_find_inliner_info (bfd *abfd,
7454 const char **filename_ptr,
7455 const char **functionname_ptr,
7456 unsigned int *line_ptr)
7458 bfd_boolean found;
7459 found = _bfd_dwarf2_find_inliner_info
7460 (abfd, filename_ptr,
7461 functionname_ptr, line_ptr, &elf_tdata (abfd)->dwarf2_find_line_info);
7462 return found;
7466 static void
7467 elfNN_aarch64_post_process_headers (bfd *abfd,
7468 struct bfd_link_info *link_info)
7470 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
7472 i_ehdrp = elf_elfheader (abfd);
7473 i_ehdrp->e_ident[EI_ABIVERSION] = AARCH64_ELF_ABI_VERSION;
7475 _bfd_elf_post_process_headers (abfd, link_info);
7478 static enum elf_reloc_type_class
7479 elfNN_aarch64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
7480 const asection *rel_sec ATTRIBUTE_UNUSED,
7481 const Elf_Internal_Rela *rela)
7483 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
7485 if (htab->root.dynsym != NULL
7486 && htab->root.dynsym->contents != NULL)
7488 /* Check relocation against STT_GNU_IFUNC symbol if there are
7489 dynamic symbols. */
7490 bfd *abfd = info->output_bfd;
7491 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7492 unsigned long r_symndx = ELFNN_R_SYM (rela->r_info);
7493 if (r_symndx != STN_UNDEF)
7495 Elf_Internal_Sym sym;
7496 if (!bed->s->swap_symbol_in (abfd,
7497 (htab->root.dynsym->contents
7498 + r_symndx * bed->s->sizeof_sym),
7499 0, &sym))
7501 /* xgettext:c-format */
7502 _bfd_error_handler (_("%B symbol number %lu references"
7503 " nonexistent SHT_SYMTAB_SHNDX section"),
7504 abfd, r_symndx);
7505 /* Ideally an error class should be returned here. */
7507 else if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
7508 return reloc_class_ifunc;
7512 switch ((int) ELFNN_R_TYPE (rela->r_info))
7514 case AARCH64_R (IRELATIVE):
7515 return reloc_class_ifunc;
7516 case AARCH64_R (RELATIVE):
7517 return reloc_class_relative;
7518 case AARCH64_R (JUMP_SLOT):
7519 return reloc_class_plt;
7520 case AARCH64_R (COPY):
7521 return reloc_class_copy;
7522 default:
7523 return reloc_class_normal;
7527 /* Handle an AArch64 specific section when reading an object file. This is
7528 called when bfd_section_from_shdr finds a section with an unknown
7529 type. */
7531 static bfd_boolean
7532 elfNN_aarch64_section_from_shdr (bfd *abfd,
7533 Elf_Internal_Shdr *hdr,
7534 const char *name, int shindex)
7536 /* There ought to be a place to keep ELF backend specific flags, but
7537 at the moment there isn't one. We just keep track of the
7538 sections by their name, instead. Fortunately, the ABI gives
7539 names for all the AArch64 specific sections, so we will probably get
7540 away with this. */
7541 switch (hdr->sh_type)
7543 case SHT_AARCH64_ATTRIBUTES:
7544 break;
7546 default:
7547 return FALSE;
7550 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
7551 return FALSE;
7553 return TRUE;
7556 /* A structure used to record a list of sections, independently
7557 of the next and prev fields in the asection structure. */
7558 typedef struct section_list
7560 asection *sec;
7561 struct section_list *next;
7562 struct section_list *prev;
7564 section_list;
7566 /* Unfortunately we need to keep a list of sections for which
7567 an _aarch64_elf_section_data structure has been allocated. This
7568 is because it is possible for functions like elfNN_aarch64_write_section
7569 to be called on a section which has had an elf_data_structure
7570 allocated for it (and so the used_by_bfd field is valid) but
7571 for which the AArch64 extended version of this structure - the
7572 _aarch64_elf_section_data structure - has not been allocated. */
7573 static section_list *sections_with_aarch64_elf_section_data = NULL;
7575 static void
7576 record_section_with_aarch64_elf_section_data (asection *sec)
7578 struct section_list *entry;
7580 entry = bfd_malloc (sizeof (*entry));
7581 if (entry == NULL)
7582 return;
7583 entry->sec = sec;
7584 entry->next = sections_with_aarch64_elf_section_data;
7585 entry->prev = NULL;
7586 if (entry->next != NULL)
7587 entry->next->prev = entry;
7588 sections_with_aarch64_elf_section_data = entry;
7591 static struct section_list *
7592 find_aarch64_elf_section_entry (asection *sec)
7594 struct section_list *entry;
7595 static struct section_list *last_entry = NULL;
7597 /* This is a short cut for the typical case where the sections are added
7598 to the sections_with_aarch64_elf_section_data list in forward order and
7599 then looked up here in backwards order. This makes a real difference
7600 to the ld-srec/sec64k.exp linker test. */
7601 entry = sections_with_aarch64_elf_section_data;
7602 if (last_entry != NULL)
7604 if (last_entry->sec == sec)
7605 entry = last_entry;
7606 else if (last_entry->next != NULL && last_entry->next->sec == sec)
7607 entry = last_entry->next;
7610 for (; entry; entry = entry->next)
7611 if (entry->sec == sec)
7612 break;
7614 if (entry)
7615 /* Record the entry prior to this one - it is the entry we are
7616 most likely to want to locate next time. Also this way if we
7617 have been called from
7618 unrecord_section_with_aarch64_elf_section_data () we will not
7619 be caching a pointer that is about to be freed. */
7620 last_entry = entry->prev;
7622 return entry;
7625 static void
7626 unrecord_section_with_aarch64_elf_section_data (asection *sec)
7628 struct section_list *entry;
7630 entry = find_aarch64_elf_section_entry (sec);
7632 if (entry)
7634 if (entry->prev != NULL)
7635 entry->prev->next = entry->next;
7636 if (entry->next != NULL)
7637 entry->next->prev = entry->prev;
7638 if (entry == sections_with_aarch64_elf_section_data)
7639 sections_with_aarch64_elf_section_data = entry->next;
7640 free (entry);
7645 typedef struct
7647 void *finfo;
7648 struct bfd_link_info *info;
7649 asection *sec;
7650 int sec_shndx;
7651 int (*func) (void *, const char *, Elf_Internal_Sym *,
7652 asection *, struct elf_link_hash_entry *);
7653 } output_arch_syminfo;
7655 enum map_symbol_type
7657 AARCH64_MAP_INSN,
7658 AARCH64_MAP_DATA
7662 /* Output a single mapping symbol. */
7664 static bfd_boolean
7665 elfNN_aarch64_output_map_sym (output_arch_syminfo *osi,
7666 enum map_symbol_type type, bfd_vma offset)
7668 static const char *names[2] = { "$x", "$d" };
7669 Elf_Internal_Sym sym;
7671 sym.st_value = (osi->sec->output_section->vma
7672 + osi->sec->output_offset + offset);
7673 sym.st_size = 0;
7674 sym.st_other = 0;
7675 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
7676 sym.st_shndx = osi->sec_shndx;
7677 return osi->func (osi->finfo, names[type], &sym, osi->sec, NULL) == 1;
7680 /* Output a single local symbol for a generated stub. */
7682 static bfd_boolean
7683 elfNN_aarch64_output_stub_sym (output_arch_syminfo *osi, const char *name,
7684 bfd_vma offset, bfd_vma size)
7686 Elf_Internal_Sym sym;
7688 sym.st_value = (osi->sec->output_section->vma
7689 + osi->sec->output_offset + offset);
7690 sym.st_size = size;
7691 sym.st_other = 0;
7692 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7693 sym.st_shndx = osi->sec_shndx;
7694 return osi->func (osi->finfo, name, &sym, osi->sec, NULL) == 1;
7697 static bfd_boolean
7698 aarch64_map_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
7700 struct elf_aarch64_stub_hash_entry *stub_entry;
7701 asection *stub_sec;
7702 bfd_vma addr;
7703 char *stub_name;
7704 output_arch_syminfo *osi;
7706 /* Massage our args to the form they really have. */
7707 stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
7708 osi = (output_arch_syminfo *) in_arg;
7710 stub_sec = stub_entry->stub_sec;
7712 /* Ensure this stub is attached to the current section being
7713 processed. */
7714 if (stub_sec != osi->sec)
7715 return TRUE;
7717 addr = (bfd_vma) stub_entry->stub_offset;
7719 stub_name = stub_entry->output_name;
7721 switch (stub_entry->stub_type)
7723 case aarch64_stub_adrp_branch:
7724 if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
7725 sizeof (aarch64_adrp_branch_stub)))
7726 return FALSE;
7727 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
7728 return FALSE;
7729 break;
7730 case aarch64_stub_long_branch:
7731 if (!elfNN_aarch64_output_stub_sym
7732 (osi, stub_name, addr, sizeof (aarch64_long_branch_stub)))
7733 return FALSE;
7734 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
7735 return FALSE;
7736 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_DATA, addr + 16))
7737 return FALSE;
7738 break;
7739 case aarch64_stub_erratum_835769_veneer:
7740 if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
7741 sizeof (aarch64_erratum_835769_stub)))
7742 return FALSE;
7743 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
7744 return FALSE;
7745 break;
7746 case aarch64_stub_erratum_843419_veneer:
7747 if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
7748 sizeof (aarch64_erratum_843419_stub)))
7749 return FALSE;
7750 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
7751 return FALSE;
7752 break;
7754 default:
7755 abort ();
7758 return TRUE;
7761 /* Output mapping symbols for linker generated sections. */
7763 static bfd_boolean
7764 elfNN_aarch64_output_arch_local_syms (bfd *output_bfd,
7765 struct bfd_link_info *info,
7766 void *finfo,
7767 int (*func) (void *, const char *,
7768 Elf_Internal_Sym *,
7769 asection *,
7770 struct elf_link_hash_entry
7773 output_arch_syminfo osi;
7774 struct elf_aarch64_link_hash_table *htab;
7776 htab = elf_aarch64_hash_table (info);
7778 osi.finfo = finfo;
7779 osi.info = info;
7780 osi.func = func;
7782 /* Long calls stubs. */
7783 if (htab->stub_bfd && htab->stub_bfd->sections)
7785 asection *stub_sec;
7787 for (stub_sec = htab->stub_bfd->sections;
7788 stub_sec != NULL; stub_sec = stub_sec->next)
7790 /* Ignore non-stub sections. */
7791 if (!strstr (stub_sec->name, STUB_SUFFIX))
7792 continue;
7794 osi.sec = stub_sec;
7796 osi.sec_shndx = _bfd_elf_section_from_bfd_section
7797 (output_bfd, osi.sec->output_section);
7799 /* The first instruction in a stub is always a branch. */
7800 if (!elfNN_aarch64_output_map_sym (&osi, AARCH64_MAP_INSN, 0))
7801 return FALSE;
7803 bfd_hash_traverse (&htab->stub_hash_table, aarch64_map_one_stub,
7804 &osi);
7808 /* Finally, output mapping symbols for the PLT. */
7809 if (!htab->root.splt || htab->root.splt->size == 0)
7810 return TRUE;
7812 osi.sec_shndx = _bfd_elf_section_from_bfd_section
7813 (output_bfd, htab->root.splt->output_section);
7814 osi.sec = htab->root.splt;
7816 elfNN_aarch64_output_map_sym (&osi, AARCH64_MAP_INSN, 0);
7818 return TRUE;
7822 /* Allocate target specific section data. */
7824 static bfd_boolean
7825 elfNN_aarch64_new_section_hook (bfd *abfd, asection *sec)
7827 if (!sec->used_by_bfd)
7829 _aarch64_elf_section_data *sdata;
7830 bfd_size_type amt = sizeof (*sdata);
7832 sdata = bfd_zalloc (abfd, amt);
7833 if (sdata == NULL)
7834 return FALSE;
7835 sec->used_by_bfd = sdata;
7838 record_section_with_aarch64_elf_section_data (sec);
7840 return _bfd_elf_new_section_hook (abfd, sec);
7844 static void
7845 unrecord_section_via_map_over_sections (bfd *abfd ATTRIBUTE_UNUSED,
7846 asection *sec,
7847 void *ignore ATTRIBUTE_UNUSED)
7849 unrecord_section_with_aarch64_elf_section_data (sec);
7852 static bfd_boolean
7853 elfNN_aarch64_close_and_cleanup (bfd *abfd)
7855 if (abfd->sections)
7856 bfd_map_over_sections (abfd,
7857 unrecord_section_via_map_over_sections, NULL);
7859 return _bfd_elf_close_and_cleanup (abfd);
7862 static bfd_boolean
7863 elfNN_aarch64_bfd_free_cached_info (bfd *abfd)
7865 if (abfd->sections)
7866 bfd_map_over_sections (abfd,
7867 unrecord_section_via_map_over_sections, NULL);
7869 return _bfd_free_cached_info (abfd);
7872 /* Create dynamic sections. This is different from the ARM backend in that
7873 the got, plt, gotplt and their relocation sections are all created in the
7874 standard part of the bfd elf backend. */
7876 static bfd_boolean
7877 elfNN_aarch64_create_dynamic_sections (bfd *dynobj,
7878 struct bfd_link_info *info)
7880 /* We need to create .got section. */
7881 if (!aarch64_elf_create_got_section (dynobj, info))
7882 return FALSE;
7884 return _bfd_elf_create_dynamic_sections (dynobj, info);
7888 /* Allocate space in .plt, .got and associated reloc sections for
7889 dynamic relocs. */
7891 static bfd_boolean
7892 elfNN_aarch64_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
7894 struct bfd_link_info *info;
7895 struct elf_aarch64_link_hash_table *htab;
7896 struct elf_aarch64_link_hash_entry *eh;
7897 struct elf_dyn_relocs *p;
7899 /* An example of a bfd_link_hash_indirect symbol is versioned
7900 symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
7901 -> __gxx_personality_v0(bfd_link_hash_defined)
7903 There is no need to process bfd_link_hash_indirect symbols here
7904 because we will also be presented with the concrete instance of
7905 the symbol and elfNN_aarch64_copy_indirect_symbol () will have been
7906 called to copy all relevant data from the generic to the concrete
7907 symbol instance. */
7908 if (h->root.type == bfd_link_hash_indirect)
7909 return TRUE;
7911 if (h->root.type == bfd_link_hash_warning)
7912 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7914 info = (struct bfd_link_info *) inf;
7915 htab = elf_aarch64_hash_table (info);
7917 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
7918 here if it is defined and referenced in a non-shared object. */
7919 if (h->type == STT_GNU_IFUNC
7920 && h->def_regular)
7921 return TRUE;
7922 else if (htab->root.dynamic_sections_created && h->plt.refcount > 0)
7924 /* Make sure this symbol is output as a dynamic symbol.
7925 Undefined weak syms won't yet be marked as dynamic. */
7926 if (h->dynindx == -1 && !h->forced_local
7927 && h->root.type == bfd_link_hash_undefweak)
7929 if (!bfd_elf_link_record_dynamic_symbol (info, h))
7930 return FALSE;
7933 if (bfd_link_pic (info) || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
7935 asection *s = htab->root.splt;
7937 /* If this is the first .plt entry, make room for the special
7938 first entry. */
7939 if (s->size == 0)
7940 s->size += htab->plt_header_size;
7942 h->plt.offset = s->size;
7944 /* If this symbol is not defined in a regular file, and we are
7945 not generating a shared library, then set the symbol to this
7946 location in the .plt. This is required to make function
7947 pointers compare as equal between the normal executable and
7948 the shared library. */
7949 if (!bfd_link_pic (info) && !h->def_regular)
7951 h->root.u.def.section = s;
7952 h->root.u.def.value = h->plt.offset;
7955 /* Make room for this entry. For now we only create the
7956 small model PLT entries. We later need to find a way
7957 of relaxing into these from the large model PLT entries. */
7958 s->size += PLT_SMALL_ENTRY_SIZE;
7960 /* We also need to make an entry in the .got.plt section, which
7961 will be placed in the .got section by the linker script. */
7962 htab->root.sgotplt->size += GOT_ENTRY_SIZE;
7964 /* We also need to make an entry in the .rela.plt section. */
7965 htab->root.srelplt->size += RELOC_SIZE (htab);
7967 /* We need to ensure that all GOT entries that serve the PLT
7968 are consecutive with the special GOT slots [0] [1] and
7969 [2]. Any addtional relocations, such as
7970 R_AARCH64_TLSDESC, must be placed after the PLT related
7971 entries. We abuse the reloc_count such that during
7972 sizing we adjust reloc_count to indicate the number of
7973 PLT related reserved entries. In subsequent phases when
7974 filling in the contents of the reloc entries, PLT related
7975 entries are placed by computing their PLT index (0
7976 .. reloc_count). While other none PLT relocs are placed
7977 at the slot indicated by reloc_count and reloc_count is
7978 updated. */
7980 htab->root.srelplt->reloc_count++;
7982 else
7984 h->plt.offset = (bfd_vma) - 1;
7985 h->needs_plt = 0;
7988 else
7990 h->plt.offset = (bfd_vma) - 1;
7991 h->needs_plt = 0;
7994 eh = (struct elf_aarch64_link_hash_entry *) h;
7995 eh->tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
7997 if (h->got.refcount > 0)
7999 bfd_boolean dyn;
8000 unsigned got_type = elf_aarch64_hash_entry (h)->got_type;
8002 h->got.offset = (bfd_vma) - 1;
8004 dyn = htab->root.dynamic_sections_created;
8006 /* Make sure this symbol is output as a dynamic symbol.
8007 Undefined weak syms won't yet be marked as dynamic. */
8008 if (dyn && h->dynindx == -1 && !h->forced_local
8009 && h->root.type == bfd_link_hash_undefweak)
8011 if (!bfd_elf_link_record_dynamic_symbol (info, h))
8012 return FALSE;
8015 if (got_type == GOT_UNKNOWN)
8018 else if (got_type == GOT_NORMAL)
8020 h->got.offset = htab->root.sgot->size;
8021 htab->root.sgot->size += GOT_ENTRY_SIZE;
8022 if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8023 || h->root.type != bfd_link_hash_undefweak)
8024 && (bfd_link_pic (info)
8025 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
8027 htab->root.srelgot->size += RELOC_SIZE (htab);
8030 else
8032 int indx;
8033 if (got_type & GOT_TLSDESC_GD)
8035 eh->tlsdesc_got_jump_table_offset =
8036 (htab->root.sgotplt->size
8037 - aarch64_compute_jump_table_size (htab));
8038 htab->root.sgotplt->size += GOT_ENTRY_SIZE * 2;
8039 h->got.offset = (bfd_vma) - 2;
8042 if (got_type & GOT_TLS_GD)
8044 h->got.offset = htab->root.sgot->size;
8045 htab->root.sgot->size += GOT_ENTRY_SIZE * 2;
8048 if (got_type & GOT_TLS_IE)
8050 h->got.offset = htab->root.sgot->size;
8051 htab->root.sgot->size += GOT_ENTRY_SIZE;
8054 indx = h && h->dynindx != -1 ? h->dynindx : 0;
8055 if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8056 || h->root.type != bfd_link_hash_undefweak)
8057 && (bfd_link_pic (info)
8058 || indx != 0
8059 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
8061 if (got_type & GOT_TLSDESC_GD)
8063 htab->root.srelplt->size += RELOC_SIZE (htab);
8064 /* Note reloc_count not incremented here! We have
8065 already adjusted reloc_count for this relocation
8066 type. */
8068 /* TLSDESC PLT is now needed, but not yet determined. */
8069 htab->tlsdesc_plt = (bfd_vma) - 1;
8072 if (got_type & GOT_TLS_GD)
8073 htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
8075 if (got_type & GOT_TLS_IE)
8076 htab->root.srelgot->size += RELOC_SIZE (htab);
8080 else
8082 h->got.offset = (bfd_vma) - 1;
8085 if (eh->dyn_relocs == NULL)
8086 return TRUE;
8088 /* In the shared -Bsymbolic case, discard space allocated for
8089 dynamic pc-relative relocs against symbols which turn out to be
8090 defined in regular objects. For the normal shared case, discard
8091 space for pc-relative relocs that have become local due to symbol
8092 visibility changes. */
8094 if (bfd_link_pic (info))
8096 /* Relocs that use pc_count are those that appear on a call
8097 insn, or certain REL relocs that can generated via assembly.
8098 We want calls to protected symbols to resolve directly to the
8099 function rather than going via the plt. If people want
8100 function pointer comparisons to work as expected then they
8101 should avoid writing weird assembly. */
8102 if (SYMBOL_CALLS_LOCAL (info, h))
8104 struct elf_dyn_relocs **pp;
8106 for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
8108 p->count -= p->pc_count;
8109 p->pc_count = 0;
8110 if (p->count == 0)
8111 *pp = p->next;
8112 else
8113 pp = &p->next;
8117 /* Also discard relocs on undefined weak syms with non-default
8118 visibility. */
8119 if (eh->dyn_relocs != NULL && h->root.type == bfd_link_hash_undefweak)
8121 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8122 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8123 eh->dyn_relocs = NULL;
8125 /* Make sure undefined weak symbols are output as a dynamic
8126 symbol in PIEs. */
8127 else if (h->dynindx == -1
8128 && !h->forced_local
8129 && h->root.type == bfd_link_hash_undefweak
8130 && !bfd_elf_link_record_dynamic_symbol (info, h))
8131 return FALSE;
8135 else if (ELIMINATE_COPY_RELOCS)
8137 /* For the non-shared case, discard space for relocs against
8138 symbols which turn out to need copy relocs or are not
8139 dynamic. */
8141 if (!h->non_got_ref
8142 && ((h->def_dynamic
8143 && !h->def_regular)
8144 || (htab->root.dynamic_sections_created
8145 && (h->root.type == bfd_link_hash_undefweak
8146 || h->root.type == bfd_link_hash_undefined))))
8148 /* Make sure this symbol is output as a dynamic symbol.
8149 Undefined weak syms won't yet be marked as dynamic. */
8150 if (h->dynindx == -1
8151 && !h->forced_local
8152 && h->root.type == bfd_link_hash_undefweak
8153 && !bfd_elf_link_record_dynamic_symbol (info, h))
8154 return FALSE;
8156 /* If that succeeded, we know we'll be keeping all the
8157 relocs. */
8158 if (h->dynindx != -1)
8159 goto keep;
8162 eh->dyn_relocs = NULL;
8164 keep:;
8167 /* Finally, allocate space. */
8168 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8170 asection *sreloc;
8172 sreloc = elf_section_data (p->sec)->sreloc;
8174 BFD_ASSERT (sreloc != NULL);
8176 sreloc->size += p->count * RELOC_SIZE (htab);
8179 return TRUE;
8182 /* Allocate space in .plt, .got and associated reloc sections for
8183 ifunc dynamic relocs. */
8185 static bfd_boolean
8186 elfNN_aarch64_allocate_ifunc_dynrelocs (struct elf_link_hash_entry *h,
8187 void *inf)
8189 struct bfd_link_info *info;
8190 struct elf_aarch64_link_hash_table *htab;
8191 struct elf_aarch64_link_hash_entry *eh;
8193 /* An example of a bfd_link_hash_indirect symbol is versioned
8194 symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
8195 -> __gxx_personality_v0(bfd_link_hash_defined)
8197 There is no need to process bfd_link_hash_indirect symbols here
8198 because we will also be presented with the concrete instance of
8199 the symbol and elfNN_aarch64_copy_indirect_symbol () will have been
8200 called to copy all relevant data from the generic to the concrete
8201 symbol instance. */
8202 if (h->root.type == bfd_link_hash_indirect)
8203 return TRUE;
8205 if (h->root.type == bfd_link_hash_warning)
8206 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8208 info = (struct bfd_link_info *) inf;
8209 htab = elf_aarch64_hash_table (info);
8211 eh = (struct elf_aarch64_link_hash_entry *) h;
8213 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
8214 here if it is defined and referenced in a non-shared object. */
8215 if (h->type == STT_GNU_IFUNC
8216 && h->def_regular)
8217 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
8218 &eh->dyn_relocs,
8219 NULL,
8220 htab->plt_entry_size,
8221 htab->plt_header_size,
8222 GOT_ENTRY_SIZE,
8223 FALSE);
8224 return TRUE;
8227 /* Allocate space in .plt, .got and associated reloc sections for
8228 local dynamic relocs. */
8230 static bfd_boolean
8231 elfNN_aarch64_allocate_local_dynrelocs (void **slot, void *inf)
8233 struct elf_link_hash_entry *h
8234 = (struct elf_link_hash_entry *) *slot;
8236 if (h->type != STT_GNU_IFUNC
8237 || !h->def_regular
8238 || !h->ref_regular
8239 || !h->forced_local
8240 || h->root.type != bfd_link_hash_defined)
8241 abort ();
8243 return elfNN_aarch64_allocate_dynrelocs (h, inf);
8246 /* Allocate space in .plt, .got and associated reloc sections for
8247 local ifunc dynamic relocs. */
8249 static bfd_boolean
8250 elfNN_aarch64_allocate_local_ifunc_dynrelocs (void **slot, void *inf)
8252 struct elf_link_hash_entry *h
8253 = (struct elf_link_hash_entry *) *slot;
8255 if (h->type != STT_GNU_IFUNC
8256 || !h->def_regular
8257 || !h->ref_regular
8258 || !h->forced_local
8259 || h->root.type != bfd_link_hash_defined)
8260 abort ();
8262 return elfNN_aarch64_allocate_ifunc_dynrelocs (h, inf);
8265 /* Find any dynamic relocs that apply to read-only sections. */
8267 static bfd_boolean
8268 aarch64_readonly_dynrelocs (struct elf_link_hash_entry * h, void * inf)
8270 struct elf_aarch64_link_hash_entry * eh;
8271 struct elf_dyn_relocs * p;
8273 eh = (struct elf_aarch64_link_hash_entry *) h;
8274 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8276 asection *s = p->sec;
8278 if (s != NULL && (s->flags & SEC_READONLY) != 0)
8280 struct bfd_link_info *info = (struct bfd_link_info *) inf;
8282 info->flags |= DF_TEXTREL;
8284 /* Not an error, just cut short the traversal. */
8285 return FALSE;
8288 return TRUE;
8291 /* This is the most important function of all . Innocuosly named
8292 though ! */
8294 static bfd_boolean
8295 elfNN_aarch64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
8296 struct bfd_link_info *info)
8298 struct elf_aarch64_link_hash_table *htab;
8299 bfd *dynobj;
8300 asection *s;
8301 bfd_boolean relocs;
8302 bfd *ibfd;
8304 htab = elf_aarch64_hash_table ((info));
8305 dynobj = htab->root.dynobj;
8307 BFD_ASSERT (dynobj != NULL);
8309 if (htab->root.dynamic_sections_created)
8311 if (bfd_link_executable (info) && !info->nointerp)
8313 s = bfd_get_linker_section (dynobj, ".interp");
8314 if (s == NULL)
8315 abort ();
8316 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
8317 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
8321 /* Set up .got offsets for local syms, and space for local dynamic
8322 relocs. */
8323 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8325 struct elf_aarch64_local_symbol *locals = NULL;
8326 Elf_Internal_Shdr *symtab_hdr;
8327 asection *srel;
8328 unsigned int i;
8330 if (!is_aarch64_elf (ibfd))
8331 continue;
8333 for (s = ibfd->sections; s != NULL; s = s->next)
8335 struct elf_dyn_relocs *p;
8337 for (p = (struct elf_dyn_relocs *)
8338 (elf_section_data (s)->local_dynrel); p != NULL; p = p->next)
8340 if (!bfd_is_abs_section (p->sec)
8341 && bfd_is_abs_section (p->sec->output_section))
8343 /* Input section has been discarded, either because
8344 it is a copy of a linkonce section or due to
8345 linker script /DISCARD/, so we'll be discarding
8346 the relocs too. */
8348 else if (p->count != 0)
8350 srel = elf_section_data (p->sec)->sreloc;
8351 srel->size += p->count * RELOC_SIZE (htab);
8352 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
8353 info->flags |= DF_TEXTREL;
8358 locals = elf_aarch64_locals (ibfd);
8359 if (!locals)
8360 continue;
8362 symtab_hdr = &elf_symtab_hdr (ibfd);
8363 srel = htab->root.srelgot;
8364 for (i = 0; i < symtab_hdr->sh_info; i++)
8366 locals[i].got_offset = (bfd_vma) - 1;
8367 locals[i].tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
8368 if (locals[i].got_refcount > 0)
8370 unsigned got_type = locals[i].got_type;
8371 if (got_type & GOT_TLSDESC_GD)
8373 locals[i].tlsdesc_got_jump_table_offset =
8374 (htab->root.sgotplt->size
8375 - aarch64_compute_jump_table_size (htab));
8376 htab->root.sgotplt->size += GOT_ENTRY_SIZE * 2;
8377 locals[i].got_offset = (bfd_vma) - 2;
8380 if (got_type & GOT_TLS_GD)
8382 locals[i].got_offset = htab->root.sgot->size;
8383 htab->root.sgot->size += GOT_ENTRY_SIZE * 2;
8386 if (got_type & GOT_TLS_IE
8387 || got_type & GOT_NORMAL)
8389 locals[i].got_offset = htab->root.sgot->size;
8390 htab->root.sgot->size += GOT_ENTRY_SIZE;
8393 if (got_type == GOT_UNKNOWN)
8397 if (bfd_link_pic (info))
8399 if (got_type & GOT_TLSDESC_GD)
8401 htab->root.srelplt->size += RELOC_SIZE (htab);
8402 /* Note RELOC_COUNT not incremented here! */
8403 htab->tlsdesc_plt = (bfd_vma) - 1;
8406 if (got_type & GOT_TLS_GD)
8407 htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
8409 if (got_type & GOT_TLS_IE
8410 || got_type & GOT_NORMAL)
8411 htab->root.srelgot->size += RELOC_SIZE (htab);
8414 else
8416 locals[i].got_refcount = (bfd_vma) - 1;
8422 /* Allocate global sym .plt and .got entries, and space for global
8423 sym dynamic relocs. */
8424 elf_link_hash_traverse (&htab->root, elfNN_aarch64_allocate_dynrelocs,
8425 info);
8427 /* Allocate global ifunc sym .plt and .got entries, and space for global
8428 ifunc sym dynamic relocs. */
8429 elf_link_hash_traverse (&htab->root, elfNN_aarch64_allocate_ifunc_dynrelocs,
8430 info);
8432 /* Allocate .plt and .got entries, and space for local symbols. */
8433 htab_traverse (htab->loc_hash_table,
8434 elfNN_aarch64_allocate_local_dynrelocs,
8435 info);
8437 /* Allocate .plt and .got entries, and space for local ifunc symbols. */
8438 htab_traverse (htab->loc_hash_table,
8439 elfNN_aarch64_allocate_local_ifunc_dynrelocs,
8440 info);
8442 /* For every jump slot reserved in the sgotplt, reloc_count is
8443 incremented. However, when we reserve space for TLS descriptors,
8444 it's not incremented, so in order to compute the space reserved
8445 for them, it suffices to multiply the reloc count by the jump
8446 slot size. */
8448 if (htab->root.srelplt)
8449 htab->sgotplt_jump_table_size = aarch64_compute_jump_table_size (htab);
8451 if (htab->tlsdesc_plt)
8453 if (htab->root.splt->size == 0)
8454 htab->root.splt->size += PLT_ENTRY_SIZE;
8456 htab->tlsdesc_plt = htab->root.splt->size;
8457 htab->root.splt->size += PLT_TLSDESC_ENTRY_SIZE;
8459 /* If we're not using lazy TLS relocations, don't generate the
8460 GOT entry required. */
8461 if (!(info->flags & DF_BIND_NOW))
8463 htab->dt_tlsdesc_got = htab->root.sgot->size;
8464 htab->root.sgot->size += GOT_ENTRY_SIZE;
8468 /* Init mapping symbols information to use later to distingush between
8469 code and data while scanning for errata. */
8470 if (htab->fix_erratum_835769 || htab->fix_erratum_843419)
8471 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8473 if (!is_aarch64_elf (ibfd))
8474 continue;
8475 bfd_elfNN_aarch64_init_maps (ibfd);
8478 /* We now have determined the sizes of the various dynamic sections.
8479 Allocate memory for them. */
8480 relocs = FALSE;
8481 for (s = dynobj->sections; s != NULL; s = s->next)
8483 if ((s->flags & SEC_LINKER_CREATED) == 0)
8484 continue;
8486 if (s == htab->root.splt
8487 || s == htab->root.sgot
8488 || s == htab->root.sgotplt
8489 || s == htab->root.iplt
8490 || s == htab->root.igotplt
8491 || s == htab->root.sdynbss
8492 || s == htab->root.sdynrelro)
8494 /* Strip this section if we don't need it; see the
8495 comment below. */
8497 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
8499 if (s->size != 0 && s != htab->root.srelplt)
8500 relocs = TRUE;
8502 /* We use the reloc_count field as a counter if we need
8503 to copy relocs into the output file. */
8504 if (s != htab->root.srelplt)
8505 s->reloc_count = 0;
8507 else
8509 /* It's not one of our sections, so don't allocate space. */
8510 continue;
8513 if (s->size == 0)
8515 /* If we don't need this section, strip it from the
8516 output file. This is mostly to handle .rela.bss and
8517 .rela.plt. We must create both sections in
8518 create_dynamic_sections, because they must be created
8519 before the linker maps input sections to output
8520 sections. The linker does that before
8521 adjust_dynamic_symbol is called, and it is that
8522 function which decides whether anything needs to go
8523 into these sections. */
8524 s->flags |= SEC_EXCLUDE;
8525 continue;
8528 if ((s->flags & SEC_HAS_CONTENTS) == 0)
8529 continue;
8531 /* Allocate memory for the section contents. We use bfd_zalloc
8532 here in case unused entries are not reclaimed before the
8533 section's contents are written out. This should not happen,
8534 but this way if it does, we get a R_AARCH64_NONE reloc instead
8535 of garbage. */
8536 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
8537 if (s->contents == NULL)
8538 return FALSE;
8541 if (htab->root.dynamic_sections_created)
8543 /* Add some entries to the .dynamic section. We fill in the
8544 values later, in elfNN_aarch64_finish_dynamic_sections, but we
8545 must add the entries now so that we get the correct size for
8546 the .dynamic section. The DT_DEBUG entry is filled in by the
8547 dynamic linker and used by the debugger. */
8548 #define add_dynamic_entry(TAG, VAL) \
8549 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
8551 if (bfd_link_executable (info))
8553 if (!add_dynamic_entry (DT_DEBUG, 0))
8554 return FALSE;
8557 if (htab->root.splt->size != 0)
8559 if (!add_dynamic_entry (DT_PLTGOT, 0)
8560 || !add_dynamic_entry (DT_PLTRELSZ, 0)
8561 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
8562 || !add_dynamic_entry (DT_JMPREL, 0))
8563 return FALSE;
8565 if (htab->tlsdesc_plt
8566 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
8567 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
8568 return FALSE;
8571 if (relocs)
8573 if (!add_dynamic_entry (DT_RELA, 0)
8574 || !add_dynamic_entry (DT_RELASZ, 0)
8575 || !add_dynamic_entry (DT_RELAENT, RELOC_SIZE (htab)))
8576 return FALSE;
8578 /* If any dynamic relocs apply to a read-only section,
8579 then we need a DT_TEXTREL entry. */
8580 if ((info->flags & DF_TEXTREL) == 0)
8581 elf_link_hash_traverse (& htab->root, aarch64_readonly_dynrelocs,
8582 info);
8584 if ((info->flags & DF_TEXTREL) != 0)
8586 if (!add_dynamic_entry (DT_TEXTREL, 0))
8587 return FALSE;
8591 #undef add_dynamic_entry
8593 return TRUE;
8596 static inline void
8597 elf_aarch64_update_plt_entry (bfd *output_bfd,
8598 bfd_reloc_code_real_type r_type,
8599 bfd_byte *plt_entry, bfd_vma value)
8601 reloc_howto_type *howto = elfNN_aarch64_howto_from_bfd_reloc (r_type);
8603 /* FIXME: We should check the return value from this function call. */
8604 (void) _bfd_aarch64_elf_put_addend (output_bfd, plt_entry, r_type, howto, value);
8607 static void
8608 elfNN_aarch64_create_small_pltn_entry (struct elf_link_hash_entry *h,
8609 struct elf_aarch64_link_hash_table
8610 *htab, bfd *output_bfd,
8611 struct bfd_link_info *info)
8613 bfd_byte *plt_entry;
8614 bfd_vma plt_index;
8615 bfd_vma got_offset;
8616 bfd_vma gotplt_entry_address;
8617 bfd_vma plt_entry_address;
8618 Elf_Internal_Rela rela;
8619 bfd_byte *loc;
8620 asection *plt, *gotplt, *relplt;
8622 /* When building a static executable, use .iplt, .igot.plt and
8623 .rela.iplt sections for STT_GNU_IFUNC symbols. */
8624 if (htab->root.splt != NULL)
8626 plt = htab->root.splt;
8627 gotplt = htab->root.sgotplt;
8628 relplt = htab->root.srelplt;
8630 else
8632 plt = htab->root.iplt;
8633 gotplt = htab->root.igotplt;
8634 relplt = htab->root.irelplt;
8637 /* Get the index in the procedure linkage table which
8638 corresponds to this symbol. This is the index of this symbol
8639 in all the symbols for which we are making plt entries. The
8640 first entry in the procedure linkage table is reserved.
8642 Get the offset into the .got table of the entry that
8643 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
8644 bytes. The first three are reserved for the dynamic linker.
8646 For static executables, we don't reserve anything. */
8648 if (plt == htab->root.splt)
8650 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
8651 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
8653 else
8655 plt_index = h->plt.offset / htab->plt_entry_size;
8656 got_offset = plt_index * GOT_ENTRY_SIZE;
8659 plt_entry = plt->contents + h->plt.offset;
8660 plt_entry_address = plt->output_section->vma
8661 + plt->output_offset + h->plt.offset;
8662 gotplt_entry_address = gotplt->output_section->vma +
8663 gotplt->output_offset + got_offset;
8665 /* Copy in the boiler-plate for the PLTn entry. */
8666 memcpy (plt_entry, elfNN_aarch64_small_plt_entry, PLT_SMALL_ENTRY_SIZE);
8668 /* Fill in the top 21 bits for this: ADRP x16, PLT_GOT + n * 8.
8669 ADRP: ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
8670 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADR_HI21_PCREL,
8671 plt_entry,
8672 PG (gotplt_entry_address) -
8673 PG (plt_entry_address));
8675 /* Fill in the lo12 bits for the load from the pltgot. */
8676 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_LDSTNN_LO12,
8677 plt_entry + 4,
8678 PG_OFFSET (gotplt_entry_address));
8680 /* Fill in the lo12 bits for the add from the pltgot entry. */
8681 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADD_LO12,
8682 plt_entry + 8,
8683 PG_OFFSET (gotplt_entry_address));
8685 /* All the GOTPLT Entries are essentially initialized to PLT0. */
8686 bfd_put_NN (output_bfd,
8687 plt->output_section->vma + plt->output_offset,
8688 gotplt->contents + got_offset);
8690 rela.r_offset = gotplt_entry_address;
8692 if (h->dynindx == -1
8693 || ((bfd_link_executable (info)
8694 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8695 && h->def_regular
8696 && h->type == STT_GNU_IFUNC))
8698 /* If an STT_GNU_IFUNC symbol is locally defined, generate
8699 R_AARCH64_IRELATIVE instead of R_AARCH64_JUMP_SLOT. */
8700 rela.r_info = ELFNN_R_INFO (0, AARCH64_R (IRELATIVE));
8701 rela.r_addend = (h->root.u.def.value
8702 + h->root.u.def.section->output_section->vma
8703 + h->root.u.def.section->output_offset);
8705 else
8707 /* Fill in the entry in the .rela.plt section. */
8708 rela.r_info = ELFNN_R_INFO (h->dynindx, AARCH64_R (JUMP_SLOT));
8709 rela.r_addend = 0;
8712 /* Compute the relocation entry to used based on PLT index and do
8713 not adjust reloc_count. The reloc_count has already been adjusted
8714 to account for this entry. */
8715 loc = relplt->contents + plt_index * RELOC_SIZE (htab);
8716 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
8719 /* Size sections even though they're not dynamic. We use it to setup
8720 _TLS_MODULE_BASE_, if needed. */
8722 static bfd_boolean
8723 elfNN_aarch64_always_size_sections (bfd *output_bfd,
8724 struct bfd_link_info *info)
8726 asection *tls_sec;
8728 if (bfd_link_relocatable (info))
8729 return TRUE;
8731 tls_sec = elf_hash_table (info)->tls_sec;
8733 if (tls_sec)
8735 struct elf_link_hash_entry *tlsbase;
8737 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
8738 "_TLS_MODULE_BASE_", TRUE, TRUE, FALSE);
8740 if (tlsbase)
8742 struct bfd_link_hash_entry *h = NULL;
8743 const struct elf_backend_data *bed =
8744 get_elf_backend_data (output_bfd);
8746 if (!(_bfd_generic_link_add_one_symbol
8747 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
8748 tls_sec, 0, NULL, FALSE, bed->collect, &h)))
8749 return FALSE;
8751 tlsbase->type = STT_TLS;
8752 tlsbase = (struct elf_link_hash_entry *) h;
8753 tlsbase->def_regular = 1;
8754 tlsbase->other = STV_HIDDEN;
8755 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
8759 return TRUE;
8762 /* Finish up dynamic symbol handling. We set the contents of various
8763 dynamic sections here. */
8765 static bfd_boolean
8766 elfNN_aarch64_finish_dynamic_symbol (bfd *output_bfd,
8767 struct bfd_link_info *info,
8768 struct elf_link_hash_entry *h,
8769 Elf_Internal_Sym *sym)
8771 struct elf_aarch64_link_hash_table *htab;
8772 htab = elf_aarch64_hash_table (info);
8774 if (h->plt.offset != (bfd_vma) - 1)
8776 asection *plt, *gotplt, *relplt;
8778 /* This symbol has an entry in the procedure linkage table. Set
8779 it up. */
8781 /* When building a static executable, use .iplt, .igot.plt and
8782 .rela.iplt sections for STT_GNU_IFUNC symbols. */
8783 if (htab->root.splt != NULL)
8785 plt = htab->root.splt;
8786 gotplt = htab->root.sgotplt;
8787 relplt = htab->root.srelplt;
8789 else
8791 plt = htab->root.iplt;
8792 gotplt = htab->root.igotplt;
8793 relplt = htab->root.irelplt;
8796 /* This symbol has an entry in the procedure linkage table. Set
8797 it up. */
8798 if ((h->dynindx == -1
8799 && !((h->forced_local || bfd_link_executable (info))
8800 && h->def_regular
8801 && h->type == STT_GNU_IFUNC))
8802 || plt == NULL
8803 || gotplt == NULL
8804 || relplt == NULL)
8805 return FALSE;
8807 elfNN_aarch64_create_small_pltn_entry (h, htab, output_bfd, info);
8808 if (!h->def_regular)
8810 /* Mark the symbol as undefined, rather than as defined in
8811 the .plt section. */
8812 sym->st_shndx = SHN_UNDEF;
8813 /* If the symbol is weak we need to clear the value.
8814 Otherwise, the PLT entry would provide a definition for
8815 the symbol even if the symbol wasn't defined anywhere,
8816 and so the symbol would never be NULL. Leave the value if
8817 there were any relocations where pointer equality matters
8818 (this is a clue for the dynamic linker, to make function
8819 pointer comparisons work between an application and shared
8820 library). */
8821 if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
8822 sym->st_value = 0;
8826 if (h->got.offset != (bfd_vma) - 1
8827 && elf_aarch64_hash_entry (h)->got_type == GOT_NORMAL)
8829 Elf_Internal_Rela rela;
8830 bfd_byte *loc;
8832 /* This symbol has an entry in the global offset table. Set it
8833 up. */
8834 if (htab->root.sgot == NULL || htab->root.srelgot == NULL)
8835 abort ();
8837 rela.r_offset = (htab->root.sgot->output_section->vma
8838 + htab->root.sgot->output_offset
8839 + (h->got.offset & ~(bfd_vma) 1));
8841 if (h->def_regular
8842 && h->type == STT_GNU_IFUNC)
8844 if (bfd_link_pic (info))
8846 /* Generate R_AARCH64_GLOB_DAT. */
8847 goto do_glob_dat;
8849 else
8851 asection *plt;
8853 if (!h->pointer_equality_needed)
8854 abort ();
8856 /* For non-shared object, we can't use .got.plt, which
8857 contains the real function address if we need pointer
8858 equality. We load the GOT entry with the PLT entry. */
8859 plt = htab->root.splt ? htab->root.splt : htab->root.iplt;
8860 bfd_put_NN (output_bfd, (plt->output_section->vma
8861 + plt->output_offset
8862 + h->plt.offset),
8863 htab->root.sgot->contents
8864 + (h->got.offset & ~(bfd_vma) 1));
8865 return TRUE;
8868 else if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
8870 if (!(h->def_regular || ELF_COMMON_DEF_P (h)))
8871 return FALSE;
8873 BFD_ASSERT ((h->got.offset & 1) != 0);
8874 rela.r_info = ELFNN_R_INFO (0, AARCH64_R (RELATIVE));
8875 rela.r_addend = (h->root.u.def.value
8876 + h->root.u.def.section->output_section->vma
8877 + h->root.u.def.section->output_offset);
8879 else
8881 do_glob_dat:
8882 BFD_ASSERT ((h->got.offset & 1) == 0);
8883 bfd_put_NN (output_bfd, (bfd_vma) 0,
8884 htab->root.sgot->contents + h->got.offset);
8885 rela.r_info = ELFNN_R_INFO (h->dynindx, AARCH64_R (GLOB_DAT));
8886 rela.r_addend = 0;
8889 loc = htab->root.srelgot->contents;
8890 loc += htab->root.srelgot->reloc_count++ * RELOC_SIZE (htab);
8891 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
8894 if (h->needs_copy)
8896 Elf_Internal_Rela rela;
8897 asection *s;
8898 bfd_byte *loc;
8900 /* This symbol needs a copy reloc. Set it up. */
8901 if (h->dynindx == -1
8902 || (h->root.type != bfd_link_hash_defined
8903 && h->root.type != bfd_link_hash_defweak)
8904 || htab->root.srelbss == NULL)
8905 abort ();
8907 rela.r_offset = (h->root.u.def.value
8908 + h->root.u.def.section->output_section->vma
8909 + h->root.u.def.section->output_offset);
8910 rela.r_info = ELFNN_R_INFO (h->dynindx, AARCH64_R (COPY));
8911 rela.r_addend = 0;
8912 if (h->root.u.def.section == htab->root.sdynrelro)
8913 s = htab->root.sreldynrelro;
8914 else
8915 s = htab->root.srelbss;
8916 loc = s->contents + s->reloc_count++ * RELOC_SIZE (htab);
8917 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
8920 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. SYM may
8921 be NULL for local symbols. */
8922 if (sym != NULL
8923 && (h == elf_hash_table (info)->hdynamic
8924 || h == elf_hash_table (info)->hgot))
8925 sym->st_shndx = SHN_ABS;
8927 return TRUE;
8930 /* Finish up local dynamic symbol handling. We set the contents of
8931 various dynamic sections here. */
8933 static bfd_boolean
8934 elfNN_aarch64_finish_local_dynamic_symbol (void **slot, void *inf)
8936 struct elf_link_hash_entry *h
8937 = (struct elf_link_hash_entry *) *slot;
8938 struct bfd_link_info *info
8939 = (struct bfd_link_info *) inf;
8941 return elfNN_aarch64_finish_dynamic_symbol (info->output_bfd,
8942 info, h, NULL);
8945 static void
8946 elfNN_aarch64_init_small_plt0_entry (bfd *output_bfd ATTRIBUTE_UNUSED,
8947 struct elf_aarch64_link_hash_table
8948 *htab)
8950 /* Fill in PLT0. Fixme:RR Note this doesn't distinguish between
8951 small and large plts and at the minute just generates
8952 the small PLT. */
8954 /* PLT0 of the small PLT looks like this in ELF64 -
8955 stp x16, x30, [sp, #-16]! // Save the reloc and lr on stack.
8956 adrp x16, PLT_GOT + 16 // Get the page base of the GOTPLT
8957 ldr x17, [x16, #:lo12:PLT_GOT+16] // Load the address of the
8958 // symbol resolver
8959 add x16, x16, #:lo12:PLT_GOT+16 // Load the lo12 bits of the
8960 // GOTPLT entry for this.
8961 br x17
8962 PLT0 will be slightly different in ELF32 due to different got entry
8963 size. */
8964 bfd_vma plt_got_2nd_ent; /* Address of GOT[2]. */
8965 bfd_vma plt_base;
8968 memcpy (htab->root.splt->contents, elfNN_aarch64_small_plt0_entry,
8969 PLT_ENTRY_SIZE);
8970 elf_section_data (htab->root.splt->output_section)->this_hdr.sh_entsize =
8971 PLT_ENTRY_SIZE;
8973 plt_got_2nd_ent = (htab->root.sgotplt->output_section->vma
8974 + htab->root.sgotplt->output_offset
8975 + GOT_ENTRY_SIZE * 2);
8977 plt_base = htab->root.splt->output_section->vma +
8978 htab->root.splt->output_offset;
8980 /* Fill in the top 21 bits for this: ADRP x16, PLT_GOT + n * 8.
8981 ADRP: ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
8982 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADR_HI21_PCREL,
8983 htab->root.splt->contents + 4,
8984 PG (plt_got_2nd_ent) - PG (plt_base + 4));
8986 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_LDSTNN_LO12,
8987 htab->root.splt->contents + 8,
8988 PG_OFFSET (plt_got_2nd_ent));
8990 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADD_LO12,
8991 htab->root.splt->contents + 12,
8992 PG_OFFSET (plt_got_2nd_ent));
8995 static bfd_boolean
8996 elfNN_aarch64_finish_dynamic_sections (bfd *output_bfd,
8997 struct bfd_link_info *info)
8999 struct elf_aarch64_link_hash_table *htab;
9000 bfd *dynobj;
9001 asection *sdyn;
9003 htab = elf_aarch64_hash_table (info);
9004 dynobj = htab->root.dynobj;
9005 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
9007 if (htab->root.dynamic_sections_created)
9009 ElfNN_External_Dyn *dyncon, *dynconend;
9011 if (sdyn == NULL || htab->root.sgot == NULL)
9012 abort ();
9014 dyncon = (ElfNN_External_Dyn *) sdyn->contents;
9015 dynconend = (ElfNN_External_Dyn *) (sdyn->contents + sdyn->size);
9016 for (; dyncon < dynconend; dyncon++)
9018 Elf_Internal_Dyn dyn;
9019 asection *s;
9021 bfd_elfNN_swap_dyn_in (dynobj, dyncon, &dyn);
9023 switch (dyn.d_tag)
9025 default:
9026 continue;
9028 case DT_PLTGOT:
9029 s = htab->root.sgotplt;
9030 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
9031 break;
9033 case DT_JMPREL:
9034 s = htab->root.srelplt;
9035 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
9036 break;
9038 case DT_PLTRELSZ:
9039 s = htab->root.srelplt;
9040 dyn.d_un.d_val = s->size;
9041 break;
9043 case DT_TLSDESC_PLT:
9044 s = htab->root.splt;
9045 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
9046 + htab->tlsdesc_plt;
9047 break;
9049 case DT_TLSDESC_GOT:
9050 s = htab->root.sgot;
9051 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
9052 + htab->dt_tlsdesc_got;
9053 break;
9056 bfd_elfNN_swap_dyn_out (output_bfd, &dyn, dyncon);
9061 /* Fill in the special first entry in the procedure linkage table. */
9062 if (htab->root.splt && htab->root.splt->size > 0)
9064 elfNN_aarch64_init_small_plt0_entry (output_bfd, htab);
9066 elf_section_data (htab->root.splt->output_section)->
9067 this_hdr.sh_entsize = htab->plt_entry_size;
9070 if (htab->tlsdesc_plt)
9072 bfd_put_NN (output_bfd, (bfd_vma) 0,
9073 htab->root.sgot->contents + htab->dt_tlsdesc_got);
9075 memcpy (htab->root.splt->contents + htab->tlsdesc_plt,
9076 elfNN_aarch64_tlsdesc_small_plt_entry,
9077 sizeof (elfNN_aarch64_tlsdesc_small_plt_entry));
9080 bfd_vma adrp1_addr =
9081 htab->root.splt->output_section->vma
9082 + htab->root.splt->output_offset + htab->tlsdesc_plt + 4;
9084 bfd_vma adrp2_addr = adrp1_addr + 4;
9086 bfd_vma got_addr =
9087 htab->root.sgot->output_section->vma
9088 + htab->root.sgot->output_offset;
9090 bfd_vma pltgot_addr =
9091 htab->root.sgotplt->output_section->vma
9092 + htab->root.sgotplt->output_offset;
9094 bfd_vma dt_tlsdesc_got = got_addr + htab->dt_tlsdesc_got;
9096 bfd_byte *plt_entry =
9097 htab->root.splt->contents + htab->tlsdesc_plt;
9099 /* adrp x2, DT_TLSDESC_GOT */
9100 elf_aarch64_update_plt_entry (output_bfd,
9101 BFD_RELOC_AARCH64_ADR_HI21_PCREL,
9102 plt_entry + 4,
9103 (PG (dt_tlsdesc_got)
9104 - PG (adrp1_addr)));
9106 /* adrp x3, 0 */
9107 elf_aarch64_update_plt_entry (output_bfd,
9108 BFD_RELOC_AARCH64_ADR_HI21_PCREL,
9109 plt_entry + 8,
9110 (PG (pltgot_addr)
9111 - PG (adrp2_addr)));
9113 /* ldr x2, [x2, #0] */
9114 elf_aarch64_update_plt_entry (output_bfd,
9115 BFD_RELOC_AARCH64_LDSTNN_LO12,
9116 plt_entry + 12,
9117 PG_OFFSET (dt_tlsdesc_got));
9119 /* add x3, x3, 0 */
9120 elf_aarch64_update_plt_entry (output_bfd,
9121 BFD_RELOC_AARCH64_ADD_LO12,
9122 plt_entry + 16,
9123 PG_OFFSET (pltgot_addr));
9128 if (htab->root.sgotplt)
9130 if (bfd_is_abs_section (htab->root.sgotplt->output_section))
9132 _bfd_error_handler
9133 (_("discarded output section: `%A'"), htab->root.sgotplt);
9134 return FALSE;
9137 /* Fill in the first three entries in the global offset table. */
9138 if (htab->root.sgotplt->size > 0)
9140 bfd_put_NN (output_bfd, (bfd_vma) 0, htab->root.sgotplt->contents);
9142 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
9143 bfd_put_NN (output_bfd,
9144 (bfd_vma) 0,
9145 htab->root.sgotplt->contents + GOT_ENTRY_SIZE);
9146 bfd_put_NN (output_bfd,
9147 (bfd_vma) 0,
9148 htab->root.sgotplt->contents + GOT_ENTRY_SIZE * 2);
9151 if (htab->root.sgot)
9153 if (htab->root.sgot->size > 0)
9155 bfd_vma addr =
9156 sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0;
9157 bfd_put_NN (output_bfd, addr, htab->root.sgot->contents);
9161 elf_section_data (htab->root.sgotplt->output_section)->
9162 this_hdr.sh_entsize = GOT_ENTRY_SIZE;
9165 if (htab->root.sgot && htab->root.sgot->size > 0)
9166 elf_section_data (htab->root.sgot->output_section)->this_hdr.sh_entsize
9167 = GOT_ENTRY_SIZE;
9169 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
9170 htab_traverse (htab->loc_hash_table,
9171 elfNN_aarch64_finish_local_dynamic_symbol,
9172 info);
9174 return TRUE;
9177 /* Return address for Ith PLT stub in section PLT, for relocation REL
9178 or (bfd_vma) -1 if it should not be included. */
9180 static bfd_vma
9181 elfNN_aarch64_plt_sym_val (bfd_vma i, const asection *plt,
9182 const arelent *rel ATTRIBUTE_UNUSED)
9184 return plt->vma + PLT_ENTRY_SIZE + i * PLT_SMALL_ENTRY_SIZE;
9187 /* Returns TRUE if NAME is an AArch64 mapping symbol.
9188 The ARM ELF standard defines $x (for A64 code) and $d (for data).
9189 It also allows a period initiated suffix to be added to the symbol, ie:
9190 "$[adtx]\.[:sym_char]+". */
9192 static bfd_boolean
9193 is_aarch64_mapping_symbol (const char * name)
9195 return name != NULL /* Paranoia. */
9196 && name[0] == '$' /* Note: if objcopy --prefix-symbols has been used then
9197 the mapping symbols could have acquired a prefix.
9198 We do not support this here, since such symbols no
9199 longer conform to the ARM ELF ABI. */
9200 && (name[1] == 'd' || name[1] == 'x')
9201 && (name[2] == 0 || name[2] == '.');
9202 /* FIXME: Strictly speaking the symbol is only a valid mapping symbol if
9203 any characters that follow the period are legal characters for the body
9204 of a symbol's name. For now we just assume that this is the case. */
9207 /* Make sure that mapping symbols in object files are not removed via the
9208 "strip --strip-unneeded" tool. These symbols might needed in order to
9209 correctly generate linked files. Once an object file has been linked,
9210 it should be safe to remove them. */
9212 static void
9213 elfNN_aarch64_backend_symbol_processing (bfd *abfd, asymbol *sym)
9215 if (((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
9216 && sym->section != bfd_abs_section_ptr
9217 && is_aarch64_mapping_symbol (sym->name))
9218 sym->flags |= BSF_KEEP;
9222 /* We use this so we can override certain functions
9223 (though currently we don't). */
9225 const struct elf_size_info elfNN_aarch64_size_info =
9227 sizeof (ElfNN_External_Ehdr),
9228 sizeof (ElfNN_External_Phdr),
9229 sizeof (ElfNN_External_Shdr),
9230 sizeof (ElfNN_External_Rel),
9231 sizeof (ElfNN_External_Rela),
9232 sizeof (ElfNN_External_Sym),
9233 sizeof (ElfNN_External_Dyn),
9234 sizeof (Elf_External_Note),
9235 4, /* Hash table entry size. */
9236 1, /* Internal relocs per external relocs. */
9237 ARCH_SIZE, /* Arch size. */
9238 LOG_FILE_ALIGN, /* Log_file_align. */
9239 ELFCLASSNN, EV_CURRENT,
9240 bfd_elfNN_write_out_phdrs,
9241 bfd_elfNN_write_shdrs_and_ehdr,
9242 bfd_elfNN_checksum_contents,
9243 bfd_elfNN_write_relocs,
9244 bfd_elfNN_swap_symbol_in,
9245 bfd_elfNN_swap_symbol_out,
9246 bfd_elfNN_slurp_reloc_table,
9247 bfd_elfNN_slurp_symbol_table,
9248 bfd_elfNN_swap_dyn_in,
9249 bfd_elfNN_swap_dyn_out,
9250 bfd_elfNN_swap_reloc_in,
9251 bfd_elfNN_swap_reloc_out,
9252 bfd_elfNN_swap_reloca_in,
9253 bfd_elfNN_swap_reloca_out
9256 #define ELF_ARCH bfd_arch_aarch64
9257 #define ELF_MACHINE_CODE EM_AARCH64
9258 #define ELF_MAXPAGESIZE 0x10000
9259 #define ELF_MINPAGESIZE 0x1000
9260 #define ELF_COMMONPAGESIZE 0x1000
9262 #define bfd_elfNN_close_and_cleanup \
9263 elfNN_aarch64_close_and_cleanup
9265 #define bfd_elfNN_bfd_free_cached_info \
9266 elfNN_aarch64_bfd_free_cached_info
9268 #define bfd_elfNN_bfd_is_target_special_symbol \
9269 elfNN_aarch64_is_target_special_symbol
9271 #define bfd_elfNN_bfd_link_hash_table_create \
9272 elfNN_aarch64_link_hash_table_create
9274 #define bfd_elfNN_bfd_merge_private_bfd_data \
9275 elfNN_aarch64_merge_private_bfd_data
9277 #define bfd_elfNN_bfd_print_private_bfd_data \
9278 elfNN_aarch64_print_private_bfd_data
9280 #define bfd_elfNN_bfd_reloc_type_lookup \
9281 elfNN_aarch64_reloc_type_lookup
9283 #define bfd_elfNN_bfd_reloc_name_lookup \
9284 elfNN_aarch64_reloc_name_lookup
9286 #define bfd_elfNN_bfd_set_private_flags \
9287 elfNN_aarch64_set_private_flags
9289 #define bfd_elfNN_find_inliner_info \
9290 elfNN_aarch64_find_inliner_info
9292 #define bfd_elfNN_find_nearest_line \
9293 elfNN_aarch64_find_nearest_line
9295 #define bfd_elfNN_mkobject \
9296 elfNN_aarch64_mkobject
9298 #define bfd_elfNN_new_section_hook \
9299 elfNN_aarch64_new_section_hook
9301 #define elf_backend_adjust_dynamic_symbol \
9302 elfNN_aarch64_adjust_dynamic_symbol
9304 #define elf_backend_always_size_sections \
9305 elfNN_aarch64_always_size_sections
9307 #define elf_backend_check_relocs \
9308 elfNN_aarch64_check_relocs
9310 #define elf_backend_copy_indirect_symbol \
9311 elfNN_aarch64_copy_indirect_symbol
9313 /* Create .dynbss, and .rela.bss sections in DYNOBJ, and set up shortcuts
9314 to them in our hash. */
9315 #define elf_backend_create_dynamic_sections \
9316 elfNN_aarch64_create_dynamic_sections
9318 #define elf_backend_init_index_section \
9319 _bfd_elf_init_2_index_sections
9321 #define elf_backend_finish_dynamic_sections \
9322 elfNN_aarch64_finish_dynamic_sections
9324 #define elf_backend_finish_dynamic_symbol \
9325 elfNN_aarch64_finish_dynamic_symbol
9327 #define elf_backend_object_p \
9328 elfNN_aarch64_object_p
9330 #define elf_backend_output_arch_local_syms \
9331 elfNN_aarch64_output_arch_local_syms
9333 #define elf_backend_plt_sym_val \
9334 elfNN_aarch64_plt_sym_val
9336 #define elf_backend_post_process_headers \
9337 elfNN_aarch64_post_process_headers
9339 #define elf_backend_relocate_section \
9340 elfNN_aarch64_relocate_section
9342 #define elf_backend_reloc_type_class \
9343 elfNN_aarch64_reloc_type_class
9345 #define elf_backend_section_from_shdr \
9346 elfNN_aarch64_section_from_shdr
9348 #define elf_backend_size_dynamic_sections \
9349 elfNN_aarch64_size_dynamic_sections
9351 #define elf_backend_size_info \
9352 elfNN_aarch64_size_info
9354 #define elf_backend_write_section \
9355 elfNN_aarch64_write_section
9357 #define elf_backend_symbol_processing \
9358 elfNN_aarch64_backend_symbol_processing
9360 #define elf_backend_can_refcount 1
9361 #define elf_backend_can_gc_sections 1
9362 #define elf_backend_plt_readonly 1
9363 #define elf_backend_want_got_plt 1
9364 #define elf_backend_want_plt_sym 0
9365 #define elf_backend_want_dynrelro 1
9366 #define elf_backend_may_use_rel_p 0
9367 #define elf_backend_may_use_rela_p 1
9368 #define elf_backend_default_use_rela_p 1
9369 #define elf_backend_rela_normal 1
9370 #define elf_backend_dtrel_excludes_plt 1
9371 #define elf_backend_got_header_size (GOT_ENTRY_SIZE * 3)
9372 #define elf_backend_default_execstack 0
9373 #define elf_backend_extern_protected_data 1
9374 #define elf_backend_hash_symbol elf_aarch64_hash_symbol
9376 #undef elf_backend_obj_attrs_section
9377 #define elf_backend_obj_attrs_section ".ARM.attributes"
9379 #include "elfNN-target.h"
9381 /* CloudABI support. */
9383 #undef TARGET_LITTLE_SYM
9384 #define TARGET_LITTLE_SYM aarch64_elfNN_le_cloudabi_vec
9385 #undef TARGET_LITTLE_NAME
9386 #define TARGET_LITTLE_NAME "elfNN-littleaarch64-cloudabi"
9387 #undef TARGET_BIG_SYM
9388 #define TARGET_BIG_SYM aarch64_elfNN_be_cloudabi_vec
9389 #undef TARGET_BIG_NAME
9390 #define TARGET_BIG_NAME "elfNN-bigaarch64-cloudabi"
9392 #undef ELF_OSABI
9393 #define ELF_OSABI ELFOSABI_CLOUDABI
9395 #undef elfNN_bed
9396 #define elfNN_bed elfNN_aarch64_cloudabi_bed
9398 #include "elfNN-target.h"