[AArch64] Fix the placement of &_DYNAMIC in the GOT.
[binutils-gdb.git] / bfd / elfnn-aarch64.c
1 /* AArch64-specific support for NN-bit ELF.
2 Copyright 2009-2013 Free Software Foundation, Inc.
3 Contributed by ARM Ltd.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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/>. */
20
21 /* Notes on implementation:
22
23 Thread Local Store (TLS)
24
25 Overview:
26
27 The implementation currently supports both traditional TLS and TLS
28 descriptors, but only general dynamic (GD).
29
30 For traditional TLS the assembler will present us with code
31 fragments of the form:
32
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
39
40 For TLS descriptors the assembler will present us with code
41 fragments of the form:
42
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)
48
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.
52
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.
56
57 The precise instruction sequence is only relevant from the
58 perspective of linker relaxation which is currently not implemented.
59
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.
64
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.
70
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.
75
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.
80
81 Implementation:
82
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.
86
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.
92
93 The flow:
94
95 elfNN_aarch64_check_relocs()
96
97 This function is invoked for each relocation.
98
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.
103
104 The reference count for a symbol is incremented. The GOT type for
105 each symbol is marked as general dynamic.
106
107 elfNN_aarch64_allocate_dynrelocs ()
108
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.
114
115 elfNN_aarch64_size_dynamic_sections ()
116
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.
121
122 elfNN_aarch64_relocate_section ()
123
124 Calls elfNN_aarch64_final_link_relocate ()
125
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.
133
134 elfNN_aarch64_final_link_relocate ()
135
136 Fixup the R_AARCH64_TLSGD_{ADR_PREL21, ADD_LO12_NC} relocations. */
137
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 "elf/aarch64.h"
146 #include "elfxx-aarch64.h"
147
148 #define ARCH_SIZE NN
149
150 #if ARCH_SIZE == 64
151 #define AARCH64_R(NAME) R_AARCH64_ ## NAME
152 #define AARCH64_R_STR(NAME) "R_AARCH64_" #NAME
153 #define HOWTO64(...) HOWTO (__VA_ARGS__)
154 #define HOWTO32(...) EMPTY_HOWTO (0)
155 #define LOG_FILE_ALIGN 3
156 #endif
157
158 #if ARCH_SIZE == 32
159 #define AARCH64_R(NAME) R_AARCH64_P32_ ## NAME
160 #define AARCH64_R_STR(NAME) "R_AARCH64_P32_" #NAME
161 #define HOWTO64(...) EMPTY_HOWTO (0)
162 #define HOWTO32(...) HOWTO (__VA_ARGS__)
163 #define LOG_FILE_ALIGN 2
164 #endif
165
166 #define IS_AARCH64_TLS_RELOC(R_TYPE) \
167 ((R_TYPE) == BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21 \
168 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC \
169 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1 \
170 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC \
171 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 \
172 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC \
173 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC \
174 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19 \
175 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12 \
176 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12 \
177 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC \
178 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2 \
179 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1 \
180 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC \
181 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0 \
182 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC \
183 || (R_TYPE) == BFD_RELOC_AARCH64_TLS_DTPMOD \
184 || (R_TYPE) == BFD_RELOC_AARCH64_TLS_DTPREL \
185 || (R_TYPE) == BFD_RELOC_AARCH64_TLS_TPREL \
186 || IS_AARCH64_TLSDESC_RELOC ((R_TYPE)))
187
188 #define IS_AARCH64_TLSDESC_RELOC(R_TYPE) \
189 ((R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LD_PREL19 \
190 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21 \
191 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21 \
192 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC \
193 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC \
194 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC \
195 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_OFF_G1 \
196 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC \
197 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LDR \
198 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADD \
199 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_CALL \
200 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC)
201
202 #define ELIMINATE_COPY_RELOCS 0
203
204 /* Return size of a relocation entry. HTAB is the bfd's
205 elf_aarch64_link_hash_entry. */
206 #define RELOC_SIZE(HTAB) (sizeof (ElfNN_External_Rela))
207
208 /* GOT Entry size - 8 bytes in ELF64 and 4 bytes in ELF32. */
209 #define GOT_ENTRY_SIZE (ARCH_SIZE / 8)
210 #define PLT_ENTRY_SIZE (32)
211 #define PLT_SMALL_ENTRY_SIZE (16)
212 #define PLT_TLSDESC_ENTRY_SIZE (32)
213
214 /* Encoding of the nop instruction */
215 #define INSN_NOP 0xd503201f
216
217 #define aarch64_compute_jump_table_size(htab) \
218 (((htab)->root.srelplt == NULL) ? 0 \
219 : (htab)->root.srelplt->reloc_count * GOT_ENTRY_SIZE)
220
221 /* The first entry in a procedure linkage table looks like this
222 if the distance between the PLTGOT and the PLT is < 4GB use
223 these PLT entries. Note that the dynamic linker gets &PLTGOT[2]
224 in x16 and needs to work out PLTGOT[1] by using an address of
225 [x16,#-GOT_ENTRY_SIZE]. */
226 static const bfd_byte elfNN_aarch64_small_plt0_entry[PLT_ENTRY_SIZE] =
227 {
228 0xf0, 0x7b, 0xbf, 0xa9, /* stp x16, x30, [sp, #-16]! */
229 0x10, 0x00, 0x00, 0x90, /* adrp x16, (GOT+16) */
230 #if ARCH_SIZE == 64
231 0x11, 0x0A, 0x40, 0xf9, /* ldr x17, [x16, #PLT_GOT+0x10] */
232 0x10, 0x42, 0x00, 0x91, /* add x16, x16,#PLT_GOT+0x10 */
233 #else
234 0x11, 0x0A, 0x40, 0xb9, /* ldr w17, [x16, #PLT_GOT+0x8] */
235 0x10, 0x22, 0x00, 0x11, /* add w16, w16,#PLT_GOT+0x8 */
236 #endif
237 0x20, 0x02, 0x1f, 0xd6, /* br x17 */
238 0x1f, 0x20, 0x03, 0xd5, /* nop */
239 0x1f, 0x20, 0x03, 0xd5, /* nop */
240 0x1f, 0x20, 0x03, 0xd5, /* nop */
241 };
242
243 /* Per function entry in a procedure linkage table looks like this
244 if the distance between the PLTGOT and the PLT is < 4GB use
245 these PLT entries. */
246 static const bfd_byte elfNN_aarch64_small_plt_entry[PLT_SMALL_ENTRY_SIZE] =
247 {
248 0x10, 0x00, 0x00, 0x90, /* adrp x16, PLTGOT + n * 8 */
249 #if ARCH_SIZE == 64
250 0x11, 0x02, 0x40, 0xf9, /* ldr x17, [x16, PLTGOT + n * 8] */
251 0x10, 0x02, 0x00, 0x91, /* add x16, x16, :lo12:PLTGOT + n * 8 */
252 #else
253 0x11, 0x02, 0x40, 0xb9, /* ldr w17, [x16, PLTGOT + n * 4] */
254 0x10, 0x02, 0x00, 0x11, /* add w16, w16, :lo12:PLTGOT + n * 4 */
255 #endif
256 0x20, 0x02, 0x1f, 0xd6, /* br x17. */
257 };
258
259 static const bfd_byte
260 elfNN_aarch64_tlsdesc_small_plt_entry[PLT_TLSDESC_ENTRY_SIZE] =
261 {
262 0xe2, 0x0f, 0xbf, 0xa9, /* stp x2, x3, [sp, #-16]! */
263 0x02, 0x00, 0x00, 0x90, /* adrp x2, 0 */
264 0x03, 0x00, 0x00, 0x90, /* adrp x3, 0 */
265 #if ARCH_SIZE == 64
266 0x42, 0x00, 0x40, 0xf9, /* ldr x2, [x2, #0] */
267 0x63, 0x00, 0x00, 0x91, /* add x3, x3, 0 */
268 #else
269 0x42, 0x00, 0x40, 0xb9, /* ldr w2, [x2, #0] */
270 0x63, 0x00, 0x00, 0x11, /* add w3, w3, 0 */
271 #endif
272 0x40, 0x00, 0x1f, 0xd6, /* br x2 */
273 0x1f, 0x20, 0x03, 0xd5, /* nop */
274 0x1f, 0x20, 0x03, 0xd5, /* nop */
275 };
276
277 #define elf_info_to_howto elfNN_aarch64_info_to_howto
278 #define elf_info_to_howto_rel elfNN_aarch64_info_to_howto
279
280 #define AARCH64_ELF_ABI_VERSION 0
281
282 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
283 #define ALL_ONES (~ (bfd_vma) 0)
284
285 /* Indexed by the bfd interal reloc enumerators.
286 Therefore, the table needs to be synced with BFD_RELOC_AARCH64_*
287 in reloc.c. */
288
289 static reloc_howto_type elfNN_aarch64_howto_table[] =
290 {
291 EMPTY_HOWTO (0),
292
293 /* Basic data relocations. */
294
295 #if ARCH_SIZE == 64
296 HOWTO (R_AARCH64_NULL, /* type */
297 0, /* rightshift */
298 0, /* size (0 = byte, 1 = short, 2 = long) */
299 0, /* bitsize */
300 FALSE, /* pc_relative */
301 0, /* bitpos */
302 complain_overflow_dont, /* complain_on_overflow */
303 bfd_elf_generic_reloc, /* special_function */
304 "R_AARCH64_NULL", /* name */
305 FALSE, /* partial_inplace */
306 0, /* src_mask */
307 0, /* dst_mask */
308 FALSE), /* pcrel_offset */
309 #else
310 HOWTO (R_AARCH64_NONE, /* type */
311 0, /* rightshift */
312 0, /* size (0 = byte, 1 = short, 2 = long) */
313 0, /* bitsize */
314 FALSE, /* pc_relative */
315 0, /* bitpos */
316 complain_overflow_dont, /* complain_on_overflow */
317 bfd_elf_generic_reloc, /* special_function */
318 "R_AARCH64_NONE", /* name */
319 FALSE, /* partial_inplace */
320 0, /* src_mask */
321 0, /* dst_mask */
322 FALSE), /* pcrel_offset */
323 #endif
324
325 /* .xword: (S+A) */
326 HOWTO64 (AARCH64_R (ABS64), /* type */
327 0, /* rightshift */
328 4, /* size (4 = long long) */
329 64, /* bitsize */
330 FALSE, /* pc_relative */
331 0, /* bitpos */
332 complain_overflow_unsigned, /* complain_on_overflow */
333 bfd_elf_generic_reloc, /* special_function */
334 AARCH64_R_STR (ABS64), /* name */
335 FALSE, /* partial_inplace */
336 ALL_ONES, /* src_mask */
337 ALL_ONES, /* dst_mask */
338 FALSE), /* pcrel_offset */
339
340 /* .word: (S+A) */
341 HOWTO (AARCH64_R (ABS32), /* type */
342 0, /* rightshift */
343 2, /* size (0 = byte, 1 = short, 2 = long) */
344 32, /* bitsize */
345 FALSE, /* pc_relative */
346 0, /* bitpos */
347 complain_overflow_unsigned, /* complain_on_overflow */
348 bfd_elf_generic_reloc, /* special_function */
349 AARCH64_R_STR (ABS32), /* name */
350 FALSE, /* partial_inplace */
351 0xffffffff, /* src_mask */
352 0xffffffff, /* dst_mask */
353 FALSE), /* pcrel_offset */
354
355 /* .half: (S+A) */
356 HOWTO (AARCH64_R (ABS16), /* type */
357 0, /* rightshift */
358 1, /* size (0 = byte, 1 = short, 2 = long) */
359 16, /* bitsize */
360 FALSE, /* pc_relative */
361 0, /* bitpos */
362 complain_overflow_unsigned, /* complain_on_overflow */
363 bfd_elf_generic_reloc, /* special_function */
364 AARCH64_R_STR (ABS16), /* name */
365 FALSE, /* partial_inplace */
366 0xffff, /* src_mask */
367 0xffff, /* dst_mask */
368 FALSE), /* pcrel_offset */
369
370 /* .xword: (S+A-P) */
371 HOWTO64 (AARCH64_R (PREL64), /* type */
372 0, /* rightshift */
373 4, /* size (4 = long long) */
374 64, /* bitsize */
375 TRUE, /* pc_relative */
376 0, /* bitpos */
377 complain_overflow_signed, /* complain_on_overflow */
378 bfd_elf_generic_reloc, /* special_function */
379 AARCH64_R_STR (PREL64), /* name */
380 FALSE, /* partial_inplace */
381 ALL_ONES, /* src_mask */
382 ALL_ONES, /* dst_mask */
383 TRUE), /* pcrel_offset */
384
385 /* .word: (S+A-P) */
386 HOWTO (AARCH64_R (PREL32), /* type */
387 0, /* rightshift */
388 2, /* size (0 = byte, 1 = short, 2 = long) */
389 32, /* bitsize */
390 TRUE, /* pc_relative */
391 0, /* bitpos */
392 complain_overflow_signed, /* complain_on_overflow */
393 bfd_elf_generic_reloc, /* special_function */
394 AARCH64_R_STR (PREL32), /* name */
395 FALSE, /* partial_inplace */
396 0xffffffff, /* src_mask */
397 0xffffffff, /* dst_mask */
398 TRUE), /* pcrel_offset */
399
400 /* .half: (S+A-P) */
401 HOWTO (AARCH64_R (PREL16), /* type */
402 0, /* rightshift */
403 1, /* size (0 = byte, 1 = short, 2 = long) */
404 16, /* bitsize */
405 TRUE, /* pc_relative */
406 0, /* bitpos */
407 complain_overflow_signed, /* complain_on_overflow */
408 bfd_elf_generic_reloc, /* special_function */
409 AARCH64_R_STR (PREL16), /* name */
410 FALSE, /* partial_inplace */
411 0xffff, /* src_mask */
412 0xffff, /* dst_mask */
413 TRUE), /* pcrel_offset */
414
415 /* Group relocations to create a 16, 32, 48 or 64 bit
416 unsigned data or abs address inline. */
417
418 /* MOVZ: ((S+A) >> 0) & 0xffff */
419 HOWTO (AARCH64_R (MOVW_UABS_G0), /* type */
420 0, /* rightshift */
421 2, /* size (0 = byte, 1 = short, 2 = long) */
422 16, /* bitsize */
423 FALSE, /* pc_relative */
424 0, /* bitpos */
425 complain_overflow_unsigned, /* complain_on_overflow */
426 bfd_elf_generic_reloc, /* special_function */
427 AARCH64_R_STR (MOVW_UABS_G0), /* name */
428 FALSE, /* partial_inplace */
429 0xffff, /* src_mask */
430 0xffff, /* dst_mask */
431 FALSE), /* pcrel_offset */
432
433 /* MOVK: ((S+A) >> 0) & 0xffff [no overflow check] */
434 HOWTO (AARCH64_R (MOVW_UABS_G0_NC), /* type */
435 0, /* rightshift */
436 2, /* size (0 = byte, 1 = short, 2 = long) */
437 16, /* bitsize */
438 FALSE, /* pc_relative */
439 0, /* bitpos */
440 complain_overflow_dont, /* complain_on_overflow */
441 bfd_elf_generic_reloc, /* special_function */
442 AARCH64_R_STR (MOVW_UABS_G0_NC), /* name */
443 FALSE, /* partial_inplace */
444 0xffff, /* src_mask */
445 0xffff, /* dst_mask */
446 FALSE), /* pcrel_offset */
447
448 /* MOVZ: ((S+A) >> 16) & 0xffff */
449 HOWTO (AARCH64_R (MOVW_UABS_G1), /* type */
450 16, /* rightshift */
451 2, /* size (0 = byte, 1 = short, 2 = long) */
452 16, /* bitsize */
453 FALSE, /* pc_relative */
454 0, /* bitpos */
455 complain_overflow_unsigned, /* complain_on_overflow */
456 bfd_elf_generic_reloc, /* special_function */
457 AARCH64_R_STR (MOVW_UABS_G1), /* name */
458 FALSE, /* partial_inplace */
459 0xffff, /* src_mask */
460 0xffff, /* dst_mask */
461 FALSE), /* pcrel_offset */
462
463 /* MOVK: ((S+A) >> 16) & 0xffff [no overflow check] */
464 HOWTO64 (AARCH64_R (MOVW_UABS_G1_NC), /* type */
465 16, /* rightshift */
466 2, /* size (0 = byte, 1 = short, 2 = long) */
467 16, /* bitsize */
468 FALSE, /* pc_relative */
469 0, /* bitpos */
470 complain_overflow_dont, /* complain_on_overflow */
471 bfd_elf_generic_reloc, /* special_function */
472 AARCH64_R_STR (MOVW_UABS_G1_NC), /* name */
473 FALSE, /* partial_inplace */
474 0xffff, /* src_mask */
475 0xffff, /* dst_mask */
476 FALSE), /* pcrel_offset */
477
478 /* MOVZ: ((S+A) >> 32) & 0xffff */
479 HOWTO64 (AARCH64_R (MOVW_UABS_G2), /* type */
480 32, /* rightshift */
481 2, /* size (0 = byte, 1 = short, 2 = long) */
482 16, /* bitsize */
483 FALSE, /* pc_relative */
484 0, /* bitpos */
485 complain_overflow_unsigned, /* complain_on_overflow */
486 bfd_elf_generic_reloc, /* special_function */
487 AARCH64_R_STR (MOVW_UABS_G2), /* name */
488 FALSE, /* partial_inplace */
489 0xffff, /* src_mask */
490 0xffff, /* dst_mask */
491 FALSE), /* pcrel_offset */
492
493 /* MOVK: ((S+A) >> 32) & 0xffff [no overflow check] */
494 HOWTO64 (AARCH64_R (MOVW_UABS_G2_NC), /* type */
495 32, /* rightshift */
496 2, /* size (0 = byte, 1 = short, 2 = long) */
497 16, /* bitsize */
498 FALSE, /* pc_relative */
499 0, /* bitpos */
500 complain_overflow_dont, /* complain_on_overflow */
501 bfd_elf_generic_reloc, /* special_function */
502 AARCH64_R_STR (MOVW_UABS_G2_NC), /* name */
503 FALSE, /* partial_inplace */
504 0xffff, /* src_mask */
505 0xffff, /* dst_mask */
506 FALSE), /* pcrel_offset */
507
508 /* MOVZ: ((S+A) >> 48) & 0xffff */
509 HOWTO64 (AARCH64_R (MOVW_UABS_G3), /* type */
510 48, /* rightshift */
511 2, /* size (0 = byte, 1 = short, 2 = long) */
512 16, /* bitsize */
513 FALSE, /* pc_relative */
514 0, /* bitpos */
515 complain_overflow_unsigned, /* complain_on_overflow */
516 bfd_elf_generic_reloc, /* special_function */
517 AARCH64_R_STR (MOVW_UABS_G3), /* name */
518 FALSE, /* partial_inplace */
519 0xffff, /* src_mask */
520 0xffff, /* dst_mask */
521 FALSE), /* pcrel_offset */
522
523 /* Group relocations to create high part of a 16, 32, 48 or 64 bit
524 signed data or abs address inline. Will change instruction
525 to MOVN or MOVZ depending on sign of calculated value. */
526
527 /* MOV[ZN]: ((S+A) >> 0) & 0xffff */
528 HOWTO (AARCH64_R (MOVW_SABS_G0), /* type */
529 0, /* rightshift */
530 2, /* size (0 = byte, 1 = short, 2 = long) */
531 16, /* bitsize */
532 FALSE, /* pc_relative */
533 0, /* bitpos */
534 complain_overflow_signed, /* complain_on_overflow */
535 bfd_elf_generic_reloc, /* special_function */
536 AARCH64_R_STR (MOVW_SABS_G0), /* name */
537 FALSE, /* partial_inplace */
538 0xffff, /* src_mask */
539 0xffff, /* dst_mask */
540 FALSE), /* pcrel_offset */
541
542 /* MOV[ZN]: ((S+A) >> 16) & 0xffff */
543 HOWTO64 (AARCH64_R (MOVW_SABS_G1), /* type */
544 16, /* rightshift */
545 2, /* size (0 = byte, 1 = short, 2 = long) */
546 16, /* bitsize */
547 FALSE, /* pc_relative */
548 0, /* bitpos */
549 complain_overflow_signed, /* complain_on_overflow */
550 bfd_elf_generic_reloc, /* special_function */
551 AARCH64_R_STR (MOVW_SABS_G1), /* name */
552 FALSE, /* partial_inplace */
553 0xffff, /* src_mask */
554 0xffff, /* dst_mask */
555 FALSE), /* pcrel_offset */
556
557 /* MOV[ZN]: ((S+A) >> 32) & 0xffff */
558 HOWTO64 (AARCH64_R (MOVW_SABS_G2), /* type */
559 32, /* rightshift */
560 2, /* size (0 = byte, 1 = short, 2 = long) */
561 16, /* bitsize */
562 FALSE, /* pc_relative */
563 0, /* bitpos */
564 complain_overflow_signed, /* complain_on_overflow */
565 bfd_elf_generic_reloc, /* special_function */
566 AARCH64_R_STR (MOVW_SABS_G2), /* name */
567 FALSE, /* partial_inplace */
568 0xffff, /* src_mask */
569 0xffff, /* dst_mask */
570 FALSE), /* pcrel_offset */
571
572 /* Relocations to generate 19, 21 and 33 bit PC-relative load/store
573 addresses: PG(x) is (x & ~0xfff). */
574
575 /* LD-lit: ((S+A-P) >> 2) & 0x7ffff */
576 HOWTO (AARCH64_R (LD_PREL_LO19), /* type */
577 2, /* rightshift */
578 2, /* size (0 = byte, 1 = short, 2 = long) */
579 19, /* bitsize */
580 TRUE, /* pc_relative */
581 0, /* bitpos */
582 complain_overflow_signed, /* complain_on_overflow */
583 bfd_elf_generic_reloc, /* special_function */
584 AARCH64_R_STR (LD_PREL_LO19), /* name */
585 FALSE, /* partial_inplace */
586 0x7ffff, /* src_mask */
587 0x7ffff, /* dst_mask */
588 TRUE), /* pcrel_offset */
589
590 /* ADR: (S+A-P) & 0x1fffff */
591 HOWTO (AARCH64_R (ADR_PREL_LO21), /* type */
592 0, /* rightshift */
593 2, /* size (0 = byte, 1 = short, 2 = long) */
594 21, /* bitsize */
595 TRUE, /* pc_relative */
596 0, /* bitpos */
597 complain_overflow_signed, /* complain_on_overflow */
598 bfd_elf_generic_reloc, /* special_function */
599 AARCH64_R_STR (ADR_PREL_LO21), /* name */
600 FALSE, /* partial_inplace */
601 0x1fffff, /* src_mask */
602 0x1fffff, /* dst_mask */
603 TRUE), /* pcrel_offset */
604
605 /* ADRP: ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
606 HOWTO (AARCH64_R (ADR_PREL_PG_HI21), /* type */
607 12, /* rightshift */
608 2, /* size (0 = byte, 1 = short, 2 = long) */
609 21, /* bitsize */
610 TRUE, /* pc_relative */
611 0, /* bitpos */
612 complain_overflow_signed, /* complain_on_overflow */
613 bfd_elf_generic_reloc, /* special_function */
614 AARCH64_R_STR (ADR_PREL_PG_HI21), /* name */
615 FALSE, /* partial_inplace */
616 0x1fffff, /* src_mask */
617 0x1fffff, /* dst_mask */
618 TRUE), /* pcrel_offset */
619
620 /* ADRP: ((PG(S+A)-PG(P)) >> 12) & 0x1fffff [no overflow check] */
621 HOWTO64 (AARCH64_R (ADR_PREL_PG_HI21_NC), /* type */
622 12, /* rightshift */
623 2, /* size (0 = byte, 1 = short, 2 = long) */
624 21, /* bitsize */
625 TRUE, /* pc_relative */
626 0, /* bitpos */
627 complain_overflow_dont, /* complain_on_overflow */
628 bfd_elf_generic_reloc, /* special_function */
629 AARCH64_R_STR (ADR_PREL_PG_HI21_NC), /* name */
630 FALSE, /* partial_inplace */
631 0x1fffff, /* src_mask */
632 0x1fffff, /* dst_mask */
633 TRUE), /* pcrel_offset */
634
635 /* ADD: (S+A) & 0xfff [no overflow check] */
636 HOWTO (AARCH64_R (ADD_ABS_LO12_NC), /* type */
637 0, /* rightshift */
638 2, /* size (0 = byte, 1 = short, 2 = long) */
639 12, /* bitsize */
640 FALSE, /* pc_relative */
641 10, /* bitpos */
642 complain_overflow_dont, /* complain_on_overflow */
643 bfd_elf_generic_reloc, /* special_function */
644 AARCH64_R_STR (ADD_ABS_LO12_NC), /* name */
645 FALSE, /* partial_inplace */
646 0x3ffc00, /* src_mask */
647 0x3ffc00, /* dst_mask */
648 FALSE), /* pcrel_offset */
649
650 /* LD/ST8: (S+A) & 0xfff */
651 HOWTO (AARCH64_R (LDST8_ABS_LO12_NC), /* type */
652 0, /* rightshift */
653 2, /* size (0 = byte, 1 = short, 2 = long) */
654 12, /* bitsize */
655 FALSE, /* pc_relative */
656 0, /* bitpos */
657 complain_overflow_dont, /* complain_on_overflow */
658 bfd_elf_generic_reloc, /* special_function */
659 AARCH64_R_STR (LDST8_ABS_LO12_NC), /* name */
660 FALSE, /* partial_inplace */
661 0xfff, /* src_mask */
662 0xfff, /* dst_mask */
663 FALSE), /* pcrel_offset */
664
665 /* Relocations for control-flow instructions. */
666
667 /* TBZ/NZ: ((S+A-P) >> 2) & 0x3fff */
668 HOWTO (AARCH64_R (TSTBR14), /* type */
669 2, /* rightshift */
670 2, /* size (0 = byte, 1 = short, 2 = long) */
671 14, /* bitsize */
672 TRUE, /* pc_relative */
673 0, /* bitpos */
674 complain_overflow_signed, /* complain_on_overflow */
675 bfd_elf_generic_reloc, /* special_function */
676 AARCH64_R_STR (TSTBR14), /* name */
677 FALSE, /* partial_inplace */
678 0x3fff, /* src_mask */
679 0x3fff, /* dst_mask */
680 TRUE), /* pcrel_offset */
681
682 /* B.cond: ((S+A-P) >> 2) & 0x7ffff */
683 HOWTO (AARCH64_R (CONDBR19), /* type */
684 2, /* rightshift */
685 2, /* size (0 = byte, 1 = short, 2 = long) */
686 19, /* bitsize */
687 TRUE, /* pc_relative */
688 0, /* bitpos */
689 complain_overflow_signed, /* complain_on_overflow */
690 bfd_elf_generic_reloc, /* special_function */
691 AARCH64_R_STR (CONDBR19), /* name */
692 FALSE, /* partial_inplace */
693 0x7ffff, /* src_mask */
694 0x7ffff, /* dst_mask */
695 TRUE), /* pcrel_offset */
696
697 /* B: ((S+A-P) >> 2) & 0x3ffffff */
698 HOWTO (AARCH64_R (JUMP26), /* type */
699 2, /* rightshift */
700 2, /* size (0 = byte, 1 = short, 2 = long) */
701 26, /* bitsize */
702 TRUE, /* pc_relative */
703 0, /* bitpos */
704 complain_overflow_signed, /* complain_on_overflow */
705 bfd_elf_generic_reloc, /* special_function */
706 AARCH64_R_STR (JUMP26), /* name */
707 FALSE, /* partial_inplace */
708 0x3ffffff, /* src_mask */
709 0x3ffffff, /* dst_mask */
710 TRUE), /* pcrel_offset */
711
712 /* BL: ((S+A-P) >> 2) & 0x3ffffff */
713 HOWTO (AARCH64_R (CALL26), /* type */
714 2, /* rightshift */
715 2, /* size (0 = byte, 1 = short, 2 = long) */
716 26, /* bitsize */
717 TRUE, /* pc_relative */
718 0, /* bitpos */
719 complain_overflow_signed, /* complain_on_overflow */
720 bfd_elf_generic_reloc, /* special_function */
721 AARCH64_R_STR (CALL26), /* name */
722 FALSE, /* partial_inplace */
723 0x3ffffff, /* src_mask */
724 0x3ffffff, /* dst_mask */
725 TRUE), /* pcrel_offset */
726
727 /* LD/ST16: (S+A) & 0xffe */
728 HOWTO (AARCH64_R (LDST16_ABS_LO12_NC), /* type */
729 1, /* rightshift */
730 2, /* size (0 = byte, 1 = short, 2 = long) */
731 12, /* bitsize */
732 FALSE, /* pc_relative */
733 0, /* bitpos */
734 complain_overflow_dont, /* complain_on_overflow */
735 bfd_elf_generic_reloc, /* special_function */
736 AARCH64_R_STR (LDST16_ABS_LO12_NC), /* name */
737 FALSE, /* partial_inplace */
738 0xffe, /* src_mask */
739 0xffe, /* dst_mask */
740 FALSE), /* pcrel_offset */
741
742 /* LD/ST32: (S+A) & 0xffc */
743 HOWTO (AARCH64_R (LDST32_ABS_LO12_NC), /* type */
744 2, /* rightshift */
745 2, /* size (0 = byte, 1 = short, 2 = long) */
746 12, /* bitsize */
747 FALSE, /* pc_relative */
748 0, /* bitpos */
749 complain_overflow_dont, /* complain_on_overflow */
750 bfd_elf_generic_reloc, /* special_function */
751 AARCH64_R_STR (LDST32_ABS_LO12_NC), /* name */
752 FALSE, /* partial_inplace */
753 0xffc, /* src_mask */
754 0xffc, /* dst_mask */
755 FALSE), /* pcrel_offset */
756
757 /* LD/ST64: (S+A) & 0xff8 */
758 HOWTO (AARCH64_R (LDST64_ABS_LO12_NC), /* type */
759 3, /* rightshift */
760 2, /* size (0 = byte, 1 = short, 2 = long) */
761 12, /* bitsize */
762 FALSE, /* pc_relative */
763 0, /* bitpos */
764 complain_overflow_dont, /* complain_on_overflow */
765 bfd_elf_generic_reloc, /* special_function */
766 AARCH64_R_STR (LDST64_ABS_LO12_NC), /* name */
767 FALSE, /* partial_inplace */
768 0xff8, /* src_mask */
769 0xff8, /* dst_mask */
770 FALSE), /* pcrel_offset */
771
772 /* LD/ST128: (S+A) & 0xff0 */
773 HOWTO (AARCH64_R (LDST128_ABS_LO12_NC), /* type */
774 4, /* rightshift */
775 2, /* size (0 = byte, 1 = short, 2 = long) */
776 12, /* bitsize */
777 FALSE, /* pc_relative */
778 0, /* bitpos */
779 complain_overflow_dont, /* complain_on_overflow */
780 bfd_elf_generic_reloc, /* special_function */
781 AARCH64_R_STR (LDST128_ABS_LO12_NC), /* name */
782 FALSE, /* partial_inplace */
783 0xff0, /* src_mask */
784 0xff0, /* dst_mask */
785 FALSE), /* pcrel_offset */
786
787 /* Set a load-literal immediate field to bits
788 0x1FFFFC of G(S)-P */
789 HOWTO (AARCH64_R (GOT_LD_PREL19), /* type */
790 2, /* rightshift */
791 2, /* size (0 = byte,1 = short,2 = long) */
792 19, /* bitsize */
793 TRUE, /* pc_relative */
794 0, /* bitpos */
795 complain_overflow_signed, /* complain_on_overflow */
796 bfd_elf_generic_reloc, /* special_function */
797 AARCH64_R_STR (GOT_LD_PREL19), /* name */
798 FALSE, /* partial_inplace */
799 0xffffe0, /* src_mask */
800 0xffffe0, /* dst_mask */
801 TRUE), /* pcrel_offset */
802
803 /* Get to the page for the GOT entry for the symbol
804 (G(S) - P) using an ADRP instruction. */
805 HOWTO (AARCH64_R (ADR_GOT_PAGE), /* type */
806 12, /* rightshift */
807 2, /* size (0 = byte, 1 = short, 2 = long) */
808 21, /* bitsize */
809 TRUE, /* pc_relative */
810 0, /* bitpos */
811 complain_overflow_dont, /* complain_on_overflow */
812 bfd_elf_generic_reloc, /* special_function */
813 AARCH64_R_STR (ADR_GOT_PAGE), /* name */
814 FALSE, /* partial_inplace */
815 0x1fffff, /* src_mask */
816 0x1fffff, /* dst_mask */
817 TRUE), /* pcrel_offset */
818
819 /* LD64: GOT offset G(S) & 0xff8 */
820 HOWTO64 (AARCH64_R (LD64_GOT_LO12_NC), /* type */
821 3, /* rightshift */
822 2, /* size (0 = byte, 1 = short, 2 = long) */
823 12, /* bitsize */
824 FALSE, /* pc_relative */
825 0, /* bitpos */
826 complain_overflow_dont, /* complain_on_overflow */
827 bfd_elf_generic_reloc, /* special_function */
828 AARCH64_R_STR (LD64_GOT_LO12_NC), /* name */
829 FALSE, /* partial_inplace */
830 0xff8, /* src_mask */
831 0xff8, /* dst_mask */
832 FALSE), /* pcrel_offset */
833
834 /* LD32: GOT offset G(S) & 0xffc */
835 HOWTO32 (AARCH64_R (LD32_GOT_LO12_NC), /* type */
836 2, /* rightshift */
837 2, /* size (0 = byte, 1 = short, 2 = long) */
838 12, /* bitsize */
839 FALSE, /* pc_relative */
840 0, /* bitpos */
841 complain_overflow_dont, /* complain_on_overflow */
842 bfd_elf_generic_reloc, /* special_function */
843 AARCH64_R_STR (LD32_GOT_LO12_NC), /* name */
844 FALSE, /* partial_inplace */
845 0xffc, /* src_mask */
846 0xffc, /* dst_mask */
847 FALSE), /* pcrel_offset */
848
849 /* Get to the page for the GOT entry for the symbol
850 (G(S) - P) using an ADRP instruction. */
851 HOWTO (AARCH64_R (TLSGD_ADR_PAGE21), /* type */
852 12, /* rightshift */
853 2, /* size (0 = byte, 1 = short, 2 = long) */
854 21, /* bitsize */
855 TRUE, /* pc_relative */
856 0, /* bitpos */
857 complain_overflow_dont, /* complain_on_overflow */
858 bfd_elf_generic_reloc, /* special_function */
859 AARCH64_R_STR (TLSGD_ADR_PAGE21), /* name */
860 FALSE, /* partial_inplace */
861 0x1fffff, /* src_mask */
862 0x1fffff, /* dst_mask */
863 TRUE), /* pcrel_offset */
864
865 /* ADD: GOT offset G(S) & 0xff8 [no overflow check] */
866 HOWTO (AARCH64_R (TLSGD_ADD_LO12_NC), /* type */
867 0, /* rightshift */
868 2, /* size (0 = byte, 1 = short, 2 = long) */
869 12, /* bitsize */
870 FALSE, /* pc_relative */
871 0, /* bitpos */
872 complain_overflow_dont, /* complain_on_overflow */
873 bfd_elf_generic_reloc, /* special_function */
874 AARCH64_R_STR (TLSGD_ADD_LO12_NC), /* name */
875 FALSE, /* partial_inplace */
876 0xfff, /* src_mask */
877 0xfff, /* dst_mask */
878 FALSE), /* pcrel_offset */
879
880 HOWTO64 (AARCH64_R (TLSIE_MOVW_GOTTPREL_G1), /* type */
881 16, /* rightshift */
882 2, /* size (0 = byte, 1 = short, 2 = long) */
883 16, /* bitsize */
884 FALSE, /* pc_relative */
885 0, /* bitpos */
886 complain_overflow_dont, /* complain_on_overflow */
887 bfd_elf_generic_reloc, /* special_function */
888 AARCH64_R_STR (TLSIE_MOVW_GOTTPREL_G1), /* name */
889 FALSE, /* partial_inplace */
890 0xffff, /* src_mask */
891 0xffff, /* dst_mask */
892 FALSE), /* pcrel_offset */
893
894 HOWTO64 (AARCH64_R (TLSIE_MOVW_GOTTPREL_G0_NC), /* type */
895 0, /* rightshift */
896 2, /* size (0 = byte, 1 = short, 2 = long) */
897 32, /* bitsize */
898 FALSE, /* pc_relative */
899 0, /* bitpos */
900 complain_overflow_dont, /* complain_on_overflow */
901 bfd_elf_generic_reloc, /* special_function */
902 AARCH64_R_STR (TLSIE_MOVW_GOTTPREL_G0_NC), /* name */
903 FALSE, /* partial_inplace */
904 0xffff, /* src_mask */
905 0xffff, /* dst_mask */
906 FALSE), /* pcrel_offset */
907
908 HOWTO (AARCH64_R (TLSIE_ADR_GOTTPREL_PAGE21), /* type */
909 12, /* rightshift */
910 2, /* size (0 = byte, 1 = short, 2 = long) */
911 21, /* bitsize */
912 FALSE, /* pc_relative */
913 0, /* bitpos */
914 complain_overflow_dont, /* complain_on_overflow */
915 bfd_elf_generic_reloc, /* special_function */
916 AARCH64_R_STR (TLSIE_ADR_GOTTPREL_PAGE21), /* name */
917 FALSE, /* partial_inplace */
918 0x1fffff, /* src_mask */
919 0x1fffff, /* dst_mask */
920 FALSE), /* pcrel_offset */
921
922 HOWTO64 (AARCH64_R (TLSIE_LD64_GOTTPREL_LO12_NC), /* type */
923 3, /* rightshift */
924 2, /* size (0 = byte, 1 = short, 2 = long) */
925 12, /* bitsize */
926 FALSE, /* pc_relative */
927 0, /* bitpos */
928 complain_overflow_dont, /* complain_on_overflow */
929 bfd_elf_generic_reloc, /* special_function */
930 AARCH64_R_STR (TLSIE_LD64_GOTTPREL_LO12_NC), /* name */
931 FALSE, /* partial_inplace */
932 0xff8, /* src_mask */
933 0xff8, /* dst_mask */
934 FALSE), /* pcrel_offset */
935
936 HOWTO32 (AARCH64_R (TLSIE_LD32_GOTTPREL_LO12_NC), /* type */
937 2, /* rightshift */
938 2, /* size (0 = byte, 1 = short, 2 = long) */
939 12, /* bitsize */
940 FALSE, /* pc_relative */
941 0, /* bitpos */
942 complain_overflow_dont, /* complain_on_overflow */
943 bfd_elf_generic_reloc, /* special_function */
944 AARCH64_R_STR (TLSIE_LD32_GOTTPREL_LO12_NC), /* name */
945 FALSE, /* partial_inplace */
946 0xffc, /* src_mask */
947 0xffc, /* dst_mask */
948 FALSE), /* pcrel_offset */
949
950 HOWTO (AARCH64_R (TLSIE_LD_GOTTPREL_PREL19), /* type */
951 2, /* rightshift */
952 2, /* size (0 = byte, 1 = short, 2 = long) */
953 21, /* bitsize */
954 FALSE, /* pc_relative */
955 0, /* bitpos */
956 complain_overflow_dont, /* complain_on_overflow */
957 bfd_elf_generic_reloc, /* special_function */
958 AARCH64_R_STR (TLSIE_LD_GOTTPREL_PREL19), /* name */
959 FALSE, /* partial_inplace */
960 0x1ffffc, /* src_mask */
961 0x1ffffc, /* dst_mask */
962 FALSE), /* pcrel_offset */
963
964 HOWTO64 (AARCH64_R (TLSLE_MOVW_TPREL_G2), /* type */
965 32, /* rightshift */
966 2, /* size (0 = byte, 1 = short, 2 = long) */
967 12, /* bitsize */
968 FALSE, /* pc_relative */
969 0, /* bitpos */
970 complain_overflow_dont, /* complain_on_overflow */
971 bfd_elf_generic_reloc, /* special_function */
972 AARCH64_R_STR (TLSLE_MOVW_TPREL_G2), /* name */
973 FALSE, /* partial_inplace */
974 0xffff, /* src_mask */
975 0xffff, /* dst_mask */
976 FALSE), /* pcrel_offset */
977
978 HOWTO (AARCH64_R (TLSLE_MOVW_TPREL_G1), /* type */
979 16, /* rightshift */
980 2, /* size (0 = byte, 1 = short, 2 = long) */
981 12, /* bitsize */
982 FALSE, /* pc_relative */
983 0, /* bitpos */
984 complain_overflow_dont, /* complain_on_overflow */
985 bfd_elf_generic_reloc, /* special_function */
986 AARCH64_R_STR (TLSLE_MOVW_TPREL_G1), /* name */
987 FALSE, /* partial_inplace */
988 0xffff, /* src_mask */
989 0xffff, /* dst_mask */
990 FALSE), /* pcrel_offset */
991
992 HOWTO64 (AARCH64_R (TLSLE_MOVW_TPREL_G1_NC), /* type */
993 16, /* rightshift */
994 2, /* size (0 = byte, 1 = short, 2 = long) */
995 12, /* bitsize */
996 FALSE, /* pc_relative */
997 0, /* bitpos */
998 complain_overflow_dont, /* complain_on_overflow */
999 bfd_elf_generic_reloc, /* special_function */
1000 AARCH64_R_STR (TLSLE_MOVW_TPREL_G1_NC), /* name */
1001 FALSE, /* partial_inplace */
1002 0xffff, /* src_mask */
1003 0xffff, /* dst_mask */
1004 FALSE), /* pcrel_offset */
1005
1006 HOWTO (AARCH64_R (TLSLE_MOVW_TPREL_G0), /* type */
1007 0, /* rightshift */
1008 2, /* size (0 = byte, 1 = short, 2 = long) */
1009 12, /* bitsize */
1010 FALSE, /* pc_relative */
1011 0, /* bitpos */
1012 complain_overflow_dont, /* complain_on_overflow */
1013 bfd_elf_generic_reloc, /* special_function */
1014 AARCH64_R_STR (TLSLE_MOVW_TPREL_G0), /* name */
1015 FALSE, /* partial_inplace */
1016 0xffff, /* src_mask */
1017 0xffff, /* dst_mask */
1018 FALSE), /* pcrel_offset */
1019
1020 HOWTO (AARCH64_R (TLSLE_MOVW_TPREL_G0_NC), /* type */
1021 0, /* rightshift */
1022 2, /* size (0 = byte, 1 = short, 2 = long) */
1023 12, /* 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 (TLSLE_MOVW_TPREL_G0_NC), /* name */
1029 FALSE, /* partial_inplace */
1030 0xffff, /* src_mask */
1031 0xffff, /* dst_mask */
1032 FALSE), /* pcrel_offset */
1033
1034 HOWTO (AARCH64_R (TLSLE_ADD_TPREL_HI12), /* type */
1035 12, /* rightshift */
1036 2, /* size (0 = byte, 1 = short, 2 = long) */
1037 12, /* bitsize */
1038 FALSE, /* pc_relative */
1039 0, /* bitpos */
1040 complain_overflow_dont, /* complain_on_overflow */
1041 bfd_elf_generic_reloc, /* special_function */
1042 AARCH64_R_STR (TLSLE_ADD_TPREL_HI12), /* name */
1043 FALSE, /* partial_inplace */
1044 0xfff, /* src_mask */
1045 0xfff, /* dst_mask */
1046 FALSE), /* pcrel_offset */
1047
1048 HOWTO (AARCH64_R (TLSLE_ADD_TPREL_LO12), /* type */
1049 0, /* rightshift */
1050 2, /* size (0 = byte, 1 = short, 2 = long) */
1051 12, /* bitsize */
1052 FALSE, /* pc_relative */
1053 0, /* bitpos */
1054 complain_overflow_dont, /* complain_on_overflow */
1055 bfd_elf_generic_reloc, /* special_function */
1056 AARCH64_R_STR (TLSLE_ADD_TPREL_LO12), /* name */
1057 FALSE, /* partial_inplace */
1058 0xfff, /* src_mask */
1059 0xfff, /* dst_mask */
1060 FALSE), /* pcrel_offset */
1061
1062 HOWTO (AARCH64_R (TLSLE_ADD_TPREL_LO12_NC), /* type */
1063 0, /* rightshift */
1064 2, /* size (0 = byte, 1 = short, 2 = long) */
1065 12, /* bitsize */
1066 FALSE, /* pc_relative */
1067 0, /* bitpos */
1068 complain_overflow_dont, /* complain_on_overflow */
1069 bfd_elf_generic_reloc, /* special_function */
1070 AARCH64_R_STR (TLSLE_ADD_TPREL_LO12_NC), /* name */
1071 FALSE, /* partial_inplace */
1072 0xfff, /* src_mask */
1073 0xfff, /* dst_mask */
1074 FALSE), /* pcrel_offset */
1075
1076 HOWTO (AARCH64_R (TLSDESC_LD_PREL19), /* type */
1077 2, /* rightshift */
1078 2, /* size (0 = byte, 1 = short, 2 = long) */
1079 21, /* bitsize */
1080 TRUE, /* pc_relative */
1081 0, /* bitpos */
1082 complain_overflow_dont, /* complain_on_overflow */
1083 bfd_elf_generic_reloc, /* special_function */
1084 AARCH64_R_STR (TLSDESC_LD_PREL19), /* name */
1085 FALSE, /* partial_inplace */
1086 0x1ffffc, /* src_mask */
1087 0x1ffffc, /* dst_mask */
1088 TRUE), /* pcrel_offset */
1089
1090 HOWTO (AARCH64_R (TLSDESC_ADR_PREL21), /* type */
1091 0, /* rightshift */
1092 2, /* size (0 = byte, 1 = short, 2 = long) */
1093 21, /* bitsize */
1094 TRUE, /* pc_relative */
1095 0, /* bitpos */
1096 complain_overflow_dont, /* complain_on_overflow */
1097 bfd_elf_generic_reloc, /* special_function */
1098 AARCH64_R_STR (TLSDESC_ADR_PREL21), /* name */
1099 FALSE, /* partial_inplace */
1100 0x1fffff, /* src_mask */
1101 0x1fffff, /* dst_mask */
1102 TRUE), /* pcrel_offset */
1103
1104 /* Get to the page for the GOT entry for the symbol
1105 (G(S) - P) using an ADRP instruction. */
1106 HOWTO (AARCH64_R (TLSDESC_ADR_PAGE21), /* type */
1107 12, /* rightshift */
1108 2, /* size (0 = byte, 1 = short, 2 = long) */
1109 21, /* bitsize */
1110 TRUE, /* pc_relative */
1111 0, /* bitpos */
1112 complain_overflow_dont, /* complain_on_overflow */
1113 bfd_elf_generic_reloc, /* special_function */
1114 AARCH64_R_STR (TLSDESC_ADR_PAGE21), /* name */
1115 FALSE, /* partial_inplace */
1116 0x1fffff, /* src_mask */
1117 0x1fffff, /* dst_mask */
1118 TRUE), /* pcrel_offset */
1119
1120 /* LD64: GOT offset G(S) & 0xff8. */
1121 HOWTO64 (AARCH64_R (TLSDESC_LD64_LO12_NC), /* type */
1122 3, /* rightshift */
1123 2, /* size (0 = byte, 1 = short, 2 = long) */
1124 12, /* bitsize */
1125 FALSE, /* pc_relative */
1126 0, /* bitpos */
1127 complain_overflow_dont, /* complain_on_overflow */
1128 bfd_elf_generic_reloc, /* special_function */
1129 AARCH64_R_STR (TLSDESC_LD64_LO12_NC), /* name */
1130 FALSE, /* partial_inplace */
1131 0xff8, /* src_mask */
1132 0xff8, /* dst_mask */
1133 FALSE), /* pcrel_offset */
1134
1135 /* LD32: GOT offset G(S) & 0xffc. */
1136 HOWTO32 (AARCH64_R (TLSDESC_LD32_LO12_NC), /* type */
1137 2, /* rightshift */
1138 2, /* size (0 = byte, 1 = short, 2 = long) */
1139 12, /* bitsize */
1140 FALSE, /* pc_relative */
1141 0, /* bitpos */
1142 complain_overflow_dont, /* complain_on_overflow */
1143 bfd_elf_generic_reloc, /* special_function */
1144 AARCH64_R_STR (TLSDESC_LD32_LO12_NC), /* name */
1145 FALSE, /* partial_inplace */
1146 0xffc, /* src_mask */
1147 0xffc, /* dst_mask */
1148 FALSE), /* pcrel_offset */
1149
1150 /* ADD: GOT offset G(S) & 0xfff. */
1151 HOWTO (AARCH64_R (TLSDESC_ADD_LO12_NC), /* type */
1152 0, /* rightshift */
1153 2, /* size (0 = byte, 1 = short, 2 = long) */
1154 12, /* bitsize */
1155 FALSE, /* pc_relative */
1156 0, /* bitpos */
1157 complain_overflow_dont, /* complain_on_overflow */
1158 bfd_elf_generic_reloc, /* special_function */
1159 AARCH64_R_STR (TLSDESC_ADD_LO12_NC), /* name */
1160 FALSE, /* partial_inplace */
1161 0xfff, /* src_mask */
1162 0xfff, /* dst_mask */
1163 FALSE), /* pcrel_offset */
1164
1165 HOWTO64 (AARCH64_R (TLSDESC_OFF_G1), /* type */
1166 16, /* rightshift */
1167 2, /* size (0 = byte, 1 = short, 2 = long) */
1168 12, /* bitsize */
1169 FALSE, /* pc_relative */
1170 0, /* bitpos */
1171 complain_overflow_dont, /* complain_on_overflow */
1172 bfd_elf_generic_reloc, /* special_function */
1173 AARCH64_R_STR (TLSDESC_OFF_G1), /* name */
1174 FALSE, /* partial_inplace */
1175 0xffff, /* src_mask */
1176 0xffff, /* dst_mask */
1177 FALSE), /* pcrel_offset */
1178
1179 HOWTO64 (AARCH64_R (TLSDESC_OFF_G0_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 (TLSDESC_OFF_G0_NC), /* name */
1188 FALSE, /* partial_inplace */
1189 0xffff, /* src_mask */
1190 0xffff, /* dst_mask */
1191 FALSE), /* pcrel_offset */
1192
1193 HOWTO64 (AARCH64_R (TLSDESC_LDR), /* type */
1194 0, /* rightshift */
1195 2, /* size (0 = byte, 1 = short, 2 = long) */
1196 12, /* bitsize */
1197 FALSE, /* pc_relative */
1198 0, /* bitpos */
1199 complain_overflow_dont, /* complain_on_overflow */
1200 bfd_elf_generic_reloc, /* special_function */
1201 AARCH64_R_STR (TLSDESC_LDR), /* name */
1202 FALSE, /* partial_inplace */
1203 0x0, /* src_mask */
1204 0x0, /* dst_mask */
1205 FALSE), /* pcrel_offset */
1206
1207 HOWTO64 (AARCH64_R (TLSDESC_ADD), /* type */
1208 0, /* rightshift */
1209 2, /* size (0 = byte, 1 = short, 2 = long) */
1210 12, /* bitsize */
1211 FALSE, /* pc_relative */
1212 0, /* bitpos */
1213 complain_overflow_dont, /* complain_on_overflow */
1214 bfd_elf_generic_reloc, /* special_function */
1215 AARCH64_R_STR (TLSDESC_ADD), /* name */
1216 FALSE, /* partial_inplace */
1217 0x0, /* src_mask */
1218 0x0, /* dst_mask */
1219 FALSE), /* pcrel_offset */
1220
1221 HOWTO (AARCH64_R (TLSDESC_CALL), /* type */
1222 0, /* rightshift */
1223 2, /* size (0 = byte, 1 = short, 2 = long) */
1224 12, /* bitsize */
1225 FALSE, /* pc_relative */
1226 0, /* bitpos */
1227 complain_overflow_dont, /* complain_on_overflow */
1228 bfd_elf_generic_reloc, /* special_function */
1229 AARCH64_R_STR (TLSDESC_CALL), /* name */
1230 FALSE, /* partial_inplace */
1231 0x0, /* src_mask */
1232 0x0, /* dst_mask */
1233 FALSE), /* pcrel_offset */
1234
1235 HOWTO (AARCH64_R (COPY), /* type */
1236 0, /* rightshift */
1237 2, /* size (0 = byte, 1 = short, 2 = long) */
1238 64, /* bitsize */
1239 FALSE, /* pc_relative */
1240 0, /* bitpos */
1241 complain_overflow_bitfield, /* complain_on_overflow */
1242 bfd_elf_generic_reloc, /* special_function */
1243 AARCH64_R_STR (COPY), /* name */
1244 TRUE, /* partial_inplace */
1245 0xffffffff, /* src_mask */
1246 0xffffffff, /* dst_mask */
1247 FALSE), /* pcrel_offset */
1248
1249 HOWTO (AARCH64_R (GLOB_DAT), /* type */
1250 0, /* rightshift */
1251 2, /* size (0 = byte, 1 = short, 2 = long) */
1252 64, /* bitsize */
1253 FALSE, /* pc_relative */
1254 0, /* bitpos */
1255 complain_overflow_bitfield, /* complain_on_overflow */
1256 bfd_elf_generic_reloc, /* special_function */
1257 AARCH64_R_STR (GLOB_DAT), /* name */
1258 TRUE, /* partial_inplace */
1259 0xffffffff, /* src_mask */
1260 0xffffffff, /* dst_mask */
1261 FALSE), /* pcrel_offset */
1262
1263 HOWTO (AARCH64_R (JUMP_SLOT), /* type */
1264 0, /* rightshift */
1265 2, /* size (0 = byte, 1 = short, 2 = long) */
1266 64, /* bitsize */
1267 FALSE, /* pc_relative */
1268 0, /* bitpos */
1269 complain_overflow_bitfield, /* complain_on_overflow */
1270 bfd_elf_generic_reloc, /* special_function */
1271 AARCH64_R_STR (JUMP_SLOT), /* name */
1272 TRUE, /* partial_inplace */
1273 0xffffffff, /* src_mask */
1274 0xffffffff, /* dst_mask */
1275 FALSE), /* pcrel_offset */
1276
1277 HOWTO (AARCH64_R (RELATIVE), /* type */
1278 0, /* rightshift */
1279 2, /* size (0 = byte, 1 = short, 2 = long) */
1280 64, /* bitsize */
1281 FALSE, /* pc_relative */
1282 0, /* bitpos */
1283 complain_overflow_bitfield, /* complain_on_overflow */
1284 bfd_elf_generic_reloc, /* special_function */
1285 AARCH64_R_STR (RELATIVE), /* name */
1286 TRUE, /* partial_inplace */
1287 ALL_ONES, /* src_mask */
1288 ALL_ONES, /* dst_mask */
1289 FALSE), /* pcrel_offset */
1290
1291 HOWTO (AARCH64_R (TLS_DTPMOD), /* type */
1292 0, /* rightshift */
1293 2, /* size (0 = byte, 1 = short, 2 = long) */
1294 64, /* bitsize */
1295 FALSE, /* pc_relative */
1296 0, /* bitpos */
1297 complain_overflow_dont, /* complain_on_overflow */
1298 bfd_elf_generic_reloc, /* special_function */
1299 AARCH64_R_STR (TLS_DTPMOD), /* name */
1300 FALSE, /* partial_inplace */
1301 0, /* src_mask */
1302 ALL_ONES, /* dst_mask */
1303 FALSE), /* pc_reloffset */
1304
1305 HOWTO (AARCH64_R (TLS_DTPREL), /* type */
1306 0, /* rightshift */
1307 2, /* size (0 = byte, 1 = short, 2 = long) */
1308 64, /* bitsize */
1309 FALSE, /* pc_relative */
1310 0, /* bitpos */
1311 complain_overflow_dont, /* complain_on_overflow */
1312 bfd_elf_generic_reloc, /* special_function */
1313 AARCH64_R_STR (TLS_DTPREL), /* name */
1314 FALSE, /* partial_inplace */
1315 0, /* src_mask */
1316 ALL_ONES, /* dst_mask */
1317 FALSE), /* pcrel_offset */
1318
1319 HOWTO (AARCH64_R (TLS_TPREL), /* type */
1320 0, /* rightshift */
1321 2, /* size (0 = byte, 1 = short, 2 = long) */
1322 64, /* bitsize */
1323 FALSE, /* pc_relative */
1324 0, /* bitpos */
1325 complain_overflow_dont, /* complain_on_overflow */
1326 bfd_elf_generic_reloc, /* special_function */
1327 AARCH64_R_STR (TLS_TPREL), /* name */
1328 FALSE, /* partial_inplace */
1329 0, /* src_mask */
1330 ALL_ONES, /* dst_mask */
1331 FALSE), /* pcrel_offset */
1332
1333 HOWTO (AARCH64_R (TLSDESC), /* type */
1334 0, /* rightshift */
1335 2, /* size (0 = byte, 1 = short, 2 = long) */
1336 64, /* bitsize */
1337 FALSE, /* pc_relative */
1338 0, /* bitpos */
1339 complain_overflow_dont, /* complain_on_overflow */
1340 bfd_elf_generic_reloc, /* special_function */
1341 AARCH64_R_STR (TLSDESC), /* name */
1342 FALSE, /* partial_inplace */
1343 0, /* src_mask */
1344 ALL_ONES, /* dst_mask */
1345 FALSE), /* pcrel_offset */
1346
1347 HOWTO (AARCH64_R (IRELATIVE), /* type */
1348 0, /* rightshift */
1349 2, /* size (0 = byte, 1 = short, 2 = long) */
1350 64, /* bitsize */
1351 FALSE, /* pc_relative */
1352 0, /* bitpos */
1353 complain_overflow_bitfield, /* complain_on_overflow */
1354 bfd_elf_generic_reloc, /* special_function */
1355 AARCH64_R_STR (IRELATIVE), /* name */
1356 FALSE, /* partial_inplace */
1357 0, /* src_mask */
1358 ALL_ONES, /* dst_mask */
1359 FALSE), /* pcrel_offset */
1360
1361 EMPTY_HOWTO (0),
1362 };
1363
1364 static reloc_howto_type elfNN_aarch64_howto_none =
1365 HOWTO (R_AARCH64_NONE, /* type */
1366 0, /* rightshift */
1367 0, /* size (0 = byte, 1 = short, 2 = long) */
1368 0, /* bitsize */
1369 FALSE, /* pc_relative */
1370 0, /* bitpos */
1371 complain_overflow_dont,/* complain_on_overflow */
1372 bfd_elf_generic_reloc, /* special_function */
1373 "R_AARCH64_NONE", /* name */
1374 FALSE, /* partial_inplace */
1375 0, /* src_mask */
1376 0, /* dst_mask */
1377 FALSE); /* pcrel_offset */
1378
1379 /* Given HOWTO, return the bfd internal relocation enumerator. */
1380
1381 static bfd_reloc_code_real_type
1382 elfNN_aarch64_bfd_reloc_from_howto (reloc_howto_type *howto)
1383 {
1384 const int size
1385 = (int) ARRAY_SIZE (elfNN_aarch64_howto_table);
1386 const ptrdiff_t offset
1387 = howto - elfNN_aarch64_howto_table;
1388
1389 if (offset > 0 && offset < size - 1)
1390 return BFD_RELOC_AARCH64_RELOC_START + offset;
1391
1392 if (howto == &elfNN_aarch64_howto_none)
1393 return BFD_RELOC_AARCH64_NONE;
1394
1395 return BFD_RELOC_AARCH64_RELOC_START;
1396 }
1397
1398 /* Given R_TYPE, return the bfd internal relocation enumerator. */
1399
1400 static bfd_reloc_code_real_type
1401 elfNN_aarch64_bfd_reloc_from_type (unsigned int r_type)
1402 {
1403 static bfd_boolean initialized_p = FALSE;
1404 /* Indexed by R_TYPE, values are offsets in the howto_table. */
1405 static unsigned int offsets[R_AARCH64_end];
1406
1407 if (initialized_p == FALSE)
1408 {
1409 unsigned int i;
1410
1411 for (i = 1; i < ARRAY_SIZE (elfNN_aarch64_howto_table) - 1; ++i)
1412 if (elfNN_aarch64_howto_table[i].type != 0)
1413 offsets[elfNN_aarch64_howto_table[i].type] = i;
1414
1415 initialized_p = TRUE;
1416 }
1417
1418 if (r_type == R_AARCH64_NONE || r_type == R_AARCH64_NULL)
1419 return BFD_RELOC_AARCH64_NONE;
1420
1421 return BFD_RELOC_AARCH64_RELOC_START + offsets[r_type];
1422 }
1423
1424 struct elf_aarch64_reloc_map
1425 {
1426 bfd_reloc_code_real_type from;
1427 bfd_reloc_code_real_type to;
1428 };
1429
1430 /* Map bfd generic reloc to AArch64-specific reloc. */
1431 static const struct elf_aarch64_reloc_map elf_aarch64_reloc_map[] =
1432 {
1433 {BFD_RELOC_NONE, BFD_RELOC_AARCH64_NONE},
1434
1435 /* Basic data relocations. */
1436 {BFD_RELOC_CTOR, BFD_RELOC_AARCH64_NN},
1437 {BFD_RELOC_64, BFD_RELOC_AARCH64_64},
1438 {BFD_RELOC_32, BFD_RELOC_AARCH64_32},
1439 {BFD_RELOC_16, BFD_RELOC_AARCH64_16},
1440 {BFD_RELOC_64_PCREL, BFD_RELOC_AARCH64_64_PCREL},
1441 {BFD_RELOC_32_PCREL, BFD_RELOC_AARCH64_32_PCREL},
1442 {BFD_RELOC_16_PCREL, BFD_RELOC_AARCH64_16_PCREL},
1443 };
1444
1445 /* Given the bfd internal relocation enumerator in CODE, return the
1446 corresponding howto entry. */
1447
1448 static reloc_howto_type *
1449 elfNN_aarch64_howto_from_bfd_reloc (bfd_reloc_code_real_type code)
1450 {
1451 unsigned int i;
1452
1453 /* Convert bfd generic reloc to AArch64-specific reloc. */
1454 if (code < BFD_RELOC_AARCH64_RELOC_START
1455 || code > BFD_RELOC_AARCH64_RELOC_END)
1456 for (i = 0; i < ARRAY_SIZE (elf_aarch64_reloc_map); i++)
1457 if (elf_aarch64_reloc_map[i].from == code)
1458 {
1459 code = elf_aarch64_reloc_map[i].to;
1460 break;
1461 }
1462
1463 if (code > BFD_RELOC_AARCH64_RELOC_START
1464 && code < BFD_RELOC_AARCH64_RELOC_END)
1465 if (elfNN_aarch64_howto_table[code - BFD_RELOC_AARCH64_RELOC_START].type)
1466 return &elfNN_aarch64_howto_table[code - BFD_RELOC_AARCH64_RELOC_START];
1467
1468 return NULL;
1469 }
1470
1471 static reloc_howto_type *
1472 elfNN_aarch64_howto_from_type (unsigned int r_type)
1473 {
1474 bfd_reloc_code_real_type val;
1475 reloc_howto_type *howto;
1476
1477 #if ARCH_SIZE == 32
1478 if (r_type > 256)
1479 {
1480 bfd_set_error (bfd_error_bad_value);
1481 return NULL;
1482 }
1483 #endif
1484
1485 if (r_type == R_AARCH64_NONE)
1486 return &elfNN_aarch64_howto_none;
1487
1488 val = elfNN_aarch64_bfd_reloc_from_type (r_type);
1489 howto = elfNN_aarch64_howto_from_bfd_reloc (val);
1490
1491 if (howto != NULL)
1492 return howto;
1493
1494 bfd_set_error (bfd_error_bad_value);
1495 return NULL;
1496 }
1497
1498 static void
1499 elfNN_aarch64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc,
1500 Elf_Internal_Rela *elf_reloc)
1501 {
1502 unsigned int r_type;
1503
1504 r_type = ELFNN_R_TYPE (elf_reloc->r_info);
1505 bfd_reloc->howto = elfNN_aarch64_howto_from_type (r_type);
1506 }
1507
1508 static reloc_howto_type *
1509 elfNN_aarch64_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1510 bfd_reloc_code_real_type code)
1511 {
1512 reloc_howto_type *howto = elfNN_aarch64_howto_from_bfd_reloc (code);
1513
1514 if (howto != NULL)
1515 return howto;
1516
1517 bfd_set_error (bfd_error_bad_value);
1518 return NULL;
1519 }
1520
1521 static reloc_howto_type *
1522 elfNN_aarch64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1523 const char *r_name)
1524 {
1525 unsigned int i;
1526
1527 for (i = 1; i < ARRAY_SIZE (elfNN_aarch64_howto_table) - 1; ++i)
1528 if (elfNN_aarch64_howto_table[i].name != NULL
1529 && strcasecmp (elfNN_aarch64_howto_table[i].name, r_name) == 0)
1530 return &elfNN_aarch64_howto_table[i];
1531
1532 return NULL;
1533 }
1534
1535 #define TARGET_LITTLE_SYM bfd_elfNN_littleaarch64_vec
1536 #define TARGET_LITTLE_NAME "elfNN-littleaarch64"
1537 #define TARGET_BIG_SYM bfd_elfNN_bigaarch64_vec
1538 #define TARGET_BIG_NAME "elfNN-bigaarch64"
1539
1540 /* The linker script knows the section names for placement.
1541 The entry_names are used to do simple name mangling on the stubs.
1542 Given a function name, and its type, the stub can be found. The
1543 name can be changed. The only requirement is the %s be present. */
1544 #define STUB_ENTRY_NAME "__%s_veneer"
1545
1546 /* The name of the dynamic interpreter. This is put in the .interp
1547 section. */
1548 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
1549
1550 #define AARCH64_MAX_FWD_BRANCH_OFFSET \
1551 (((1 << 25) - 1) << 2)
1552 #define AARCH64_MAX_BWD_BRANCH_OFFSET \
1553 (-((1 << 25) << 2))
1554
1555 #define AARCH64_MAX_ADRP_IMM ((1 << 20) - 1)
1556 #define AARCH64_MIN_ADRP_IMM (-(1 << 20))
1557
1558 static int
1559 aarch64_valid_for_adrp_p (bfd_vma value, bfd_vma place)
1560 {
1561 bfd_signed_vma offset = (bfd_signed_vma) (PG (value) - PG (place)) >> 12;
1562 return offset <= AARCH64_MAX_ADRP_IMM && offset >= AARCH64_MIN_ADRP_IMM;
1563 }
1564
1565 static int
1566 aarch64_valid_branch_p (bfd_vma value, bfd_vma place)
1567 {
1568 bfd_signed_vma offset = (bfd_signed_vma) (value - place);
1569 return (offset <= AARCH64_MAX_FWD_BRANCH_OFFSET
1570 && offset >= AARCH64_MAX_BWD_BRANCH_OFFSET);
1571 }
1572
1573 static const uint32_t aarch64_adrp_branch_stub [] =
1574 {
1575 0x90000010, /* adrp ip0, X */
1576 /* R_AARCH64_ADR_HI21_PCREL(X) */
1577 0x91000210, /* add ip0, ip0, :lo12:X */
1578 /* R_AARCH64_ADD_ABS_LO12_NC(X) */
1579 0xd61f0200, /* br ip0 */
1580 };
1581
1582 static const uint32_t aarch64_long_branch_stub[] =
1583 {
1584 #if ARCH_SIZE == 64
1585 0x58000090, /* ldr ip0, 1f */
1586 #else
1587 0x18000090, /* ldr wip0, 1f */
1588 #endif
1589 0x10000011, /* adr ip1, #0 */
1590 0x8b110210, /* add ip0, ip0, ip1 */
1591 0xd61f0200, /* br ip0 */
1592 0x00000000, /* 1: .xword or .word
1593 R_AARCH64_PRELNN(X) + 12
1594 */
1595 0x00000000,
1596 };
1597
1598 /* Section name for stubs is the associated section name plus this
1599 string. */
1600 #define STUB_SUFFIX ".stub"
1601
1602 enum elf_aarch64_stub_type
1603 {
1604 aarch64_stub_none,
1605 aarch64_stub_adrp_branch,
1606 aarch64_stub_long_branch,
1607 };
1608
1609 struct elf_aarch64_stub_hash_entry
1610 {
1611 /* Base hash table entry structure. */
1612 struct bfd_hash_entry root;
1613
1614 /* The stub section. */
1615 asection *stub_sec;
1616
1617 /* Offset within stub_sec of the beginning of this stub. */
1618 bfd_vma stub_offset;
1619
1620 /* Given the symbol's value and its section we can determine its final
1621 value when building the stubs (so the stub knows where to jump). */
1622 bfd_vma target_value;
1623 asection *target_section;
1624
1625 enum elf_aarch64_stub_type stub_type;
1626
1627 /* The symbol table entry, if any, that this was derived from. */
1628 struct elf_aarch64_link_hash_entry *h;
1629
1630 /* Destination symbol type */
1631 unsigned char st_type;
1632
1633 /* Where this stub is being called from, or, in the case of combined
1634 stub sections, the first input section in the group. */
1635 asection *id_sec;
1636
1637 /* The name for the local symbol at the start of this stub. The
1638 stub name in the hash table has to be unique; this does not, so
1639 it can be friendlier. */
1640 char *output_name;
1641 };
1642
1643 /* Used to build a map of a section. This is required for mixed-endian
1644 code/data. */
1645
1646 typedef struct elf_elf_section_map
1647 {
1648 bfd_vma vma;
1649 char type;
1650 }
1651 elf_aarch64_section_map;
1652
1653
1654 typedef struct _aarch64_elf_section_data
1655 {
1656 struct bfd_elf_section_data elf;
1657 unsigned int mapcount;
1658 unsigned int mapsize;
1659 elf_aarch64_section_map *map;
1660 }
1661 _aarch64_elf_section_data;
1662
1663 #define elf_aarch64_section_data(sec) \
1664 ((_aarch64_elf_section_data *) elf_section_data (sec))
1665
1666 /* The size of the thread control block. */
1667 #define TCB_SIZE 16
1668
1669 struct elf_aarch64_local_symbol
1670 {
1671 unsigned int got_type;
1672 bfd_signed_vma got_refcount;
1673 bfd_vma got_offset;
1674
1675 /* Offset of the GOTPLT entry reserved for the TLS descriptor. The
1676 offset is from the end of the jump table and reserved entries
1677 within the PLTGOT.
1678
1679 The magic value (bfd_vma) -1 indicates that an offset has not be
1680 allocated. */
1681 bfd_vma tlsdesc_got_jump_table_offset;
1682 };
1683
1684 struct elf_aarch64_obj_tdata
1685 {
1686 struct elf_obj_tdata root;
1687
1688 /* local symbol descriptors */
1689 struct elf_aarch64_local_symbol *locals;
1690
1691 /* Zero to warn when linking objects with incompatible enum sizes. */
1692 int no_enum_size_warning;
1693
1694 /* Zero to warn when linking objects with incompatible wchar_t sizes. */
1695 int no_wchar_size_warning;
1696 };
1697
1698 #define elf_aarch64_tdata(bfd) \
1699 ((struct elf_aarch64_obj_tdata *) (bfd)->tdata.any)
1700
1701 #define elf_aarch64_locals(bfd) (elf_aarch64_tdata (bfd)->locals)
1702
1703 #define is_aarch64_elf(bfd) \
1704 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1705 && elf_tdata (bfd) != NULL \
1706 && elf_object_id (bfd) == AARCH64_ELF_DATA)
1707
1708 static bfd_boolean
1709 elfNN_aarch64_mkobject (bfd *abfd)
1710 {
1711 return bfd_elf_allocate_object (abfd, sizeof (struct elf_aarch64_obj_tdata),
1712 AARCH64_ELF_DATA);
1713 }
1714
1715 #define elf_aarch64_hash_entry(ent) \
1716 ((struct elf_aarch64_link_hash_entry *)(ent))
1717
1718 #define GOT_UNKNOWN 0
1719 #define GOT_NORMAL 1
1720 #define GOT_TLS_GD 2
1721 #define GOT_TLS_IE 4
1722 #define GOT_TLSDESC_GD 8
1723
1724 #define GOT_TLS_GD_ANY_P(type) ((type & GOT_TLS_GD) || (type & GOT_TLSDESC_GD))
1725
1726 /* AArch64 ELF linker hash entry. */
1727 struct elf_aarch64_link_hash_entry
1728 {
1729 struct elf_link_hash_entry root;
1730
1731 /* Track dynamic relocs copied for this symbol. */
1732 struct elf_dyn_relocs *dyn_relocs;
1733
1734 /* Since PLT entries have variable size, we need to record the
1735 index into .got.plt instead of recomputing it from the PLT
1736 offset. */
1737 bfd_signed_vma plt_got_offset;
1738
1739 /* Bit mask representing the type of GOT entry(s) if any required by
1740 this symbol. */
1741 unsigned int got_type;
1742
1743 /* A pointer to the most recently used stub hash entry against this
1744 symbol. */
1745 struct elf_aarch64_stub_hash_entry *stub_cache;
1746
1747 /* Offset of the GOTPLT entry reserved for the TLS descriptor. The offset
1748 is from the end of the jump table and reserved entries within the PLTGOT.
1749
1750 The magic value (bfd_vma) -1 indicates that an offset has not
1751 be allocated. */
1752 bfd_vma tlsdesc_got_jump_table_offset;
1753 };
1754
1755 static unsigned int
1756 elfNN_aarch64_symbol_got_type (struct elf_link_hash_entry *h,
1757 bfd *abfd,
1758 unsigned long r_symndx)
1759 {
1760 if (h)
1761 return elf_aarch64_hash_entry (h)->got_type;
1762
1763 if (! elf_aarch64_locals (abfd))
1764 return GOT_UNKNOWN;
1765
1766 return elf_aarch64_locals (abfd)[r_symndx].got_type;
1767 }
1768
1769 /* Get the AArch64 elf linker hash table from a link_info structure. */
1770 #define elf_aarch64_hash_table(info) \
1771 ((struct elf_aarch64_link_hash_table *) ((info)->hash))
1772
1773 #define aarch64_stub_hash_lookup(table, string, create, copy) \
1774 ((struct elf_aarch64_stub_hash_entry *) \
1775 bfd_hash_lookup ((table), (string), (create), (copy)))
1776
1777 /* AArch64 ELF linker hash table. */
1778 struct elf_aarch64_link_hash_table
1779 {
1780 /* The main hash table. */
1781 struct elf_link_hash_table root;
1782
1783 /* Nonzero to force PIC branch veneers. */
1784 int pic_veneer;
1785
1786 /* The number of bytes in the initial entry in the PLT. */
1787 bfd_size_type plt_header_size;
1788
1789 /* The number of bytes in the subsequent PLT etries. */
1790 bfd_size_type plt_entry_size;
1791
1792 /* Short-cuts to get to dynamic linker sections. */
1793 asection *sdynbss;
1794 asection *srelbss;
1795
1796 /* Small local sym cache. */
1797 struct sym_cache sym_cache;
1798
1799 /* For convenience in allocate_dynrelocs. */
1800 bfd *obfd;
1801
1802 /* The amount of space used by the reserved portion of the sgotplt
1803 section, plus whatever space is used by the jump slots. */
1804 bfd_vma sgotplt_jump_table_size;
1805
1806 /* The stub hash table. */
1807 struct bfd_hash_table stub_hash_table;
1808
1809 /* Linker stub bfd. */
1810 bfd *stub_bfd;
1811
1812 /* Linker call-backs. */
1813 asection *(*add_stub_section) (const char *, asection *);
1814 void (*layout_sections_again) (void);
1815
1816 /* Array to keep track of which stub sections have been created, and
1817 information on stub grouping. */
1818 struct map_stub
1819 {
1820 /* This is the section to which stubs in the group will be
1821 attached. */
1822 asection *link_sec;
1823 /* The stub section. */
1824 asection *stub_sec;
1825 } *stub_group;
1826
1827 /* Assorted information used by elfNN_aarch64_size_stubs. */
1828 unsigned int bfd_count;
1829 int top_index;
1830 asection **input_list;
1831
1832 /* The offset into splt of the PLT entry for the TLS descriptor
1833 resolver. Special values are 0, if not necessary (or not found
1834 to be necessary yet), and -1 if needed but not determined
1835 yet. */
1836 bfd_vma tlsdesc_plt;
1837
1838 /* The GOT offset for the lazy trampoline. Communicated to the
1839 loader via DT_TLSDESC_GOT. The magic value (bfd_vma) -1
1840 indicates an offset is not allocated. */
1841 bfd_vma dt_tlsdesc_got;
1842 };
1843
1844 /* Create an entry in an AArch64 ELF linker hash table. */
1845
1846 static struct bfd_hash_entry *
1847 elfNN_aarch64_link_hash_newfunc (struct bfd_hash_entry *entry,
1848 struct bfd_hash_table *table,
1849 const char *string)
1850 {
1851 struct elf_aarch64_link_hash_entry *ret =
1852 (struct elf_aarch64_link_hash_entry *) entry;
1853
1854 /* Allocate the structure if it has not already been allocated by a
1855 subclass. */
1856 if (ret == NULL)
1857 ret = bfd_hash_allocate (table,
1858 sizeof (struct elf_aarch64_link_hash_entry));
1859 if (ret == NULL)
1860 return (struct bfd_hash_entry *) ret;
1861
1862 /* Call the allocation method of the superclass. */
1863 ret = ((struct elf_aarch64_link_hash_entry *)
1864 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1865 table, string));
1866 if (ret != NULL)
1867 {
1868 ret->dyn_relocs = NULL;
1869 ret->got_type = GOT_UNKNOWN;
1870 ret->plt_got_offset = (bfd_vma) - 1;
1871 ret->stub_cache = NULL;
1872 ret->tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
1873 }
1874
1875 return (struct bfd_hash_entry *) ret;
1876 }
1877
1878 /* Initialize an entry in the stub hash table. */
1879
1880 static struct bfd_hash_entry *
1881 stub_hash_newfunc (struct bfd_hash_entry *entry,
1882 struct bfd_hash_table *table, const char *string)
1883 {
1884 /* Allocate the structure if it has not already been allocated by a
1885 subclass. */
1886 if (entry == NULL)
1887 {
1888 entry = bfd_hash_allocate (table,
1889 sizeof (struct
1890 elf_aarch64_stub_hash_entry));
1891 if (entry == NULL)
1892 return entry;
1893 }
1894
1895 /* Call the allocation method of the superclass. */
1896 entry = bfd_hash_newfunc (entry, table, string);
1897 if (entry != NULL)
1898 {
1899 struct elf_aarch64_stub_hash_entry *eh;
1900
1901 /* Initialize the local fields. */
1902 eh = (struct elf_aarch64_stub_hash_entry *) entry;
1903 eh->stub_sec = NULL;
1904 eh->stub_offset = 0;
1905 eh->target_value = 0;
1906 eh->target_section = NULL;
1907 eh->stub_type = aarch64_stub_none;
1908 eh->h = NULL;
1909 eh->id_sec = NULL;
1910 }
1911
1912 return entry;
1913 }
1914
1915
1916 /* Copy the extra info we tack onto an elf_link_hash_entry. */
1917
1918 static void
1919 elfNN_aarch64_copy_indirect_symbol (struct bfd_link_info *info,
1920 struct elf_link_hash_entry *dir,
1921 struct elf_link_hash_entry *ind)
1922 {
1923 struct elf_aarch64_link_hash_entry *edir, *eind;
1924
1925 edir = (struct elf_aarch64_link_hash_entry *) dir;
1926 eind = (struct elf_aarch64_link_hash_entry *) ind;
1927
1928 if (eind->dyn_relocs != NULL)
1929 {
1930 if (edir->dyn_relocs != NULL)
1931 {
1932 struct elf_dyn_relocs **pp;
1933 struct elf_dyn_relocs *p;
1934
1935 /* Add reloc counts against the indirect sym to the direct sym
1936 list. Merge any entries against the same section. */
1937 for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
1938 {
1939 struct elf_dyn_relocs *q;
1940
1941 for (q = edir->dyn_relocs; q != NULL; q = q->next)
1942 if (q->sec == p->sec)
1943 {
1944 q->pc_count += p->pc_count;
1945 q->count += p->count;
1946 *pp = p->next;
1947 break;
1948 }
1949 if (q == NULL)
1950 pp = &p->next;
1951 }
1952 *pp = edir->dyn_relocs;
1953 }
1954
1955 edir->dyn_relocs = eind->dyn_relocs;
1956 eind->dyn_relocs = NULL;
1957 }
1958
1959 if (ind->root.type == bfd_link_hash_indirect)
1960 {
1961 /* Copy over PLT info. */
1962 if (dir->got.refcount <= 0)
1963 {
1964 edir->got_type = eind->got_type;
1965 eind->got_type = GOT_UNKNOWN;
1966 }
1967 }
1968
1969 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
1970 }
1971
1972 /* Create an AArch64 elf linker hash table. */
1973
1974 static struct bfd_link_hash_table *
1975 elfNN_aarch64_link_hash_table_create (bfd *abfd)
1976 {
1977 struct elf_aarch64_link_hash_table *ret;
1978 bfd_size_type amt = sizeof (struct elf_aarch64_link_hash_table);
1979
1980 ret = bfd_zmalloc (amt);
1981 if (ret == NULL)
1982 return NULL;
1983
1984 if (!_bfd_elf_link_hash_table_init
1985 (&ret->root, abfd, elfNN_aarch64_link_hash_newfunc,
1986 sizeof (struct elf_aarch64_link_hash_entry), AARCH64_ELF_DATA))
1987 {
1988 free (ret);
1989 return NULL;
1990 }
1991
1992 ret->plt_header_size = PLT_ENTRY_SIZE;
1993 ret->plt_entry_size = PLT_SMALL_ENTRY_SIZE;
1994 ret->obfd = abfd;
1995 ret->dt_tlsdesc_got = (bfd_vma) - 1;
1996
1997 if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
1998 sizeof (struct elf_aarch64_stub_hash_entry)))
1999 {
2000 free (ret);
2001 return NULL;
2002 }
2003
2004 return &ret->root.root;
2005 }
2006
2007 /* Free the derived linker hash table. */
2008
2009 static void
2010 elfNN_aarch64_hash_table_free (struct bfd_link_hash_table *hash)
2011 {
2012 struct elf_aarch64_link_hash_table *ret
2013 = (struct elf_aarch64_link_hash_table *) hash;
2014
2015 bfd_hash_table_free (&ret->stub_hash_table);
2016 _bfd_elf_link_hash_table_free (hash);
2017 }
2018
2019 static bfd_boolean
2020 aarch64_relocate (unsigned int r_type, bfd *input_bfd, asection *input_section,
2021 bfd_vma offset, bfd_vma value)
2022 {
2023 reloc_howto_type *howto;
2024 bfd_vma place;
2025
2026 howto = elfNN_aarch64_howto_from_type (r_type);
2027 place = (input_section->output_section->vma + input_section->output_offset
2028 + offset);
2029
2030 r_type = elfNN_aarch64_bfd_reloc_from_type (r_type);
2031 value = _bfd_aarch64_elf_resolve_relocation (r_type, place, value, 0, FALSE);
2032 return _bfd_aarch64_elf_put_addend (input_bfd,
2033 input_section->contents + offset, r_type,
2034 howto, value);
2035 }
2036
2037 static enum elf_aarch64_stub_type
2038 aarch64_select_branch_stub (bfd_vma value, bfd_vma place)
2039 {
2040 if (aarch64_valid_for_adrp_p (value, place))
2041 return aarch64_stub_adrp_branch;
2042 return aarch64_stub_long_branch;
2043 }
2044
2045 /* Determine the type of stub needed, if any, for a call. */
2046
2047 static enum elf_aarch64_stub_type
2048 aarch64_type_of_stub (struct bfd_link_info *info,
2049 asection *input_sec,
2050 const Elf_Internal_Rela *rel,
2051 unsigned char st_type,
2052 struct elf_aarch64_link_hash_entry *hash,
2053 bfd_vma destination)
2054 {
2055 bfd_vma location;
2056 bfd_signed_vma branch_offset;
2057 unsigned int r_type;
2058 struct elf_aarch64_link_hash_table *globals;
2059 enum elf_aarch64_stub_type stub_type = aarch64_stub_none;
2060 bfd_boolean via_plt_p;
2061
2062 if (st_type != STT_FUNC)
2063 return stub_type;
2064
2065 globals = elf_aarch64_hash_table (info);
2066 via_plt_p = (globals->root.splt != NULL && hash != NULL
2067 && hash->root.plt.offset != (bfd_vma) - 1);
2068
2069 if (via_plt_p)
2070 return stub_type;
2071
2072 /* Determine where the call point is. */
2073 location = (input_sec->output_offset
2074 + input_sec->output_section->vma + rel->r_offset);
2075
2076 branch_offset = (bfd_signed_vma) (destination - location);
2077
2078 r_type = ELFNN_R_TYPE (rel->r_info);
2079
2080 /* We don't want to redirect any old unconditional jump in this way,
2081 only one which is being used for a sibcall, where it is
2082 acceptable for the IP0 and IP1 registers to be clobbered. */
2083 if ((r_type == AARCH64_R (CALL26) || r_type == AARCH64_R (JUMP26))
2084 && (branch_offset > AARCH64_MAX_FWD_BRANCH_OFFSET
2085 || branch_offset < AARCH64_MAX_BWD_BRANCH_OFFSET))
2086 {
2087 stub_type = aarch64_stub_long_branch;
2088 }
2089
2090 return stub_type;
2091 }
2092
2093 /* Build a name for an entry in the stub hash table. */
2094
2095 static char *
2096 elfNN_aarch64_stub_name (const asection *input_section,
2097 const asection *sym_sec,
2098 const struct elf_aarch64_link_hash_entry *hash,
2099 const Elf_Internal_Rela *rel)
2100 {
2101 char *stub_name;
2102 bfd_size_type len;
2103
2104 if (hash)
2105 {
2106 len = 8 + 1 + strlen (hash->root.root.root.string) + 1 + 16 + 1;
2107 stub_name = bfd_malloc (len);
2108 if (stub_name != NULL)
2109 snprintf (stub_name, len, "%08x_%s+%" BFD_VMA_FMT "x",
2110 (unsigned int) input_section->id,
2111 hash->root.root.root.string,
2112 rel->r_addend);
2113 }
2114 else
2115 {
2116 len = 8 + 1 + 8 + 1 + 8 + 1 + 16 + 1;
2117 stub_name = bfd_malloc (len);
2118 if (stub_name != NULL)
2119 snprintf (stub_name, len, "%08x_%x:%x+%" BFD_VMA_FMT "x",
2120 (unsigned int) input_section->id,
2121 (unsigned int) sym_sec->id,
2122 (unsigned int) ELFNN_R_SYM (rel->r_info),
2123 rel->r_addend);
2124 }
2125
2126 return stub_name;
2127 }
2128
2129 /* Look up an entry in the stub hash. Stub entries are cached because
2130 creating the stub name takes a bit of time. */
2131
2132 static struct elf_aarch64_stub_hash_entry *
2133 elfNN_aarch64_get_stub_entry (const asection *input_section,
2134 const asection *sym_sec,
2135 struct elf_link_hash_entry *hash,
2136 const Elf_Internal_Rela *rel,
2137 struct elf_aarch64_link_hash_table *htab)
2138 {
2139 struct elf_aarch64_stub_hash_entry *stub_entry;
2140 struct elf_aarch64_link_hash_entry *h =
2141 (struct elf_aarch64_link_hash_entry *) hash;
2142 const asection *id_sec;
2143
2144 if ((input_section->flags & SEC_CODE) == 0)
2145 return NULL;
2146
2147 /* If this input section is part of a group of sections sharing one
2148 stub section, then use the id of the first section in the group.
2149 Stub names need to include a section id, as there may well be
2150 more than one stub used to reach say, printf, and we need to
2151 distinguish between them. */
2152 id_sec = htab->stub_group[input_section->id].link_sec;
2153
2154 if (h != NULL && h->stub_cache != NULL
2155 && h->stub_cache->h == h && h->stub_cache->id_sec == id_sec)
2156 {
2157 stub_entry = h->stub_cache;
2158 }
2159 else
2160 {
2161 char *stub_name;
2162
2163 stub_name = elfNN_aarch64_stub_name (id_sec, sym_sec, h, rel);
2164 if (stub_name == NULL)
2165 return NULL;
2166
2167 stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table,
2168 stub_name, FALSE, FALSE);
2169 if (h != NULL)
2170 h->stub_cache = stub_entry;
2171
2172 free (stub_name);
2173 }
2174
2175 return stub_entry;
2176 }
2177
2178 /* Add a new stub entry to the stub hash. Not all fields of the new
2179 stub entry are initialised. */
2180
2181 static struct elf_aarch64_stub_hash_entry *
2182 elfNN_aarch64_add_stub (const char *stub_name,
2183 asection *section,
2184 struct elf_aarch64_link_hash_table *htab)
2185 {
2186 asection *link_sec;
2187 asection *stub_sec;
2188 struct elf_aarch64_stub_hash_entry *stub_entry;
2189
2190 link_sec = htab->stub_group[section->id].link_sec;
2191 stub_sec = htab->stub_group[section->id].stub_sec;
2192 if (stub_sec == NULL)
2193 {
2194 stub_sec = htab->stub_group[link_sec->id].stub_sec;
2195 if (stub_sec == NULL)
2196 {
2197 size_t namelen;
2198 bfd_size_type len;
2199 char *s_name;
2200
2201 namelen = strlen (link_sec->name);
2202 len = namelen + sizeof (STUB_SUFFIX);
2203 s_name = bfd_alloc (htab->stub_bfd, len);
2204 if (s_name == NULL)
2205 return NULL;
2206
2207 memcpy (s_name, link_sec->name, namelen);
2208 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
2209 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
2210 if (stub_sec == NULL)
2211 return NULL;
2212 htab->stub_group[link_sec->id].stub_sec = stub_sec;
2213 }
2214 htab->stub_group[section->id].stub_sec = stub_sec;
2215 }
2216
2217 /* Enter this entry into the linker stub hash table. */
2218 stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
2219 TRUE, FALSE);
2220 if (stub_entry == NULL)
2221 {
2222 (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
2223 section->owner, stub_name);
2224 return NULL;
2225 }
2226
2227 stub_entry->stub_sec = stub_sec;
2228 stub_entry->stub_offset = 0;
2229 stub_entry->id_sec = link_sec;
2230
2231 return stub_entry;
2232 }
2233
2234 static bfd_boolean
2235 aarch64_build_one_stub (struct bfd_hash_entry *gen_entry,
2236 void *in_arg ATTRIBUTE_UNUSED)
2237 {
2238 struct elf_aarch64_stub_hash_entry *stub_entry;
2239 asection *stub_sec;
2240 bfd *stub_bfd;
2241 bfd_byte *loc;
2242 bfd_vma sym_value;
2243 unsigned int template_size;
2244 const uint32_t *template;
2245 unsigned int i;
2246
2247 /* Massage our args to the form they really have. */
2248 stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
2249
2250 stub_sec = stub_entry->stub_sec;
2251
2252 /* Make a note of the offset within the stubs for this entry. */
2253 stub_entry->stub_offset = stub_sec->size;
2254 loc = stub_sec->contents + stub_entry->stub_offset;
2255
2256 stub_bfd = stub_sec->owner;
2257
2258 /* This is the address of the stub destination. */
2259 sym_value = (stub_entry->target_value
2260 + stub_entry->target_section->output_offset
2261 + stub_entry->target_section->output_section->vma);
2262
2263 if (stub_entry->stub_type == aarch64_stub_long_branch)
2264 {
2265 bfd_vma place = (stub_entry->stub_offset + stub_sec->output_section->vma
2266 + stub_sec->output_offset);
2267
2268 /* See if we can relax the stub. */
2269 if (aarch64_valid_for_adrp_p (sym_value, place))
2270 stub_entry->stub_type = aarch64_select_branch_stub (sym_value, place);
2271 }
2272
2273 switch (stub_entry->stub_type)
2274 {
2275 case aarch64_stub_adrp_branch:
2276 template = aarch64_adrp_branch_stub;
2277 template_size = sizeof (aarch64_adrp_branch_stub);
2278 break;
2279 case aarch64_stub_long_branch:
2280 template = aarch64_long_branch_stub;
2281 template_size = sizeof (aarch64_long_branch_stub);
2282 break;
2283 default:
2284 BFD_FAIL ();
2285 return FALSE;
2286 }
2287
2288 for (i = 0; i < (template_size / sizeof template[0]); i++)
2289 {
2290 bfd_putl32 (template[i], loc);
2291 loc += 4;
2292 }
2293
2294 template_size = (template_size + 7) & ~7;
2295 stub_sec->size += template_size;
2296
2297 switch (stub_entry->stub_type)
2298 {
2299 case aarch64_stub_adrp_branch:
2300 if (aarch64_relocate (AARCH64_R (ADR_PREL_PG_HI21), stub_bfd, stub_sec,
2301 stub_entry->stub_offset, sym_value))
2302 /* The stub would not have been relaxed if the offset was out
2303 of range. */
2304 BFD_FAIL ();
2305
2306 _bfd_final_link_relocate
2307 (elfNN_aarch64_howto_from_type (AARCH64_R (ADD_ABS_LO12_NC)),
2308 stub_bfd,
2309 stub_sec,
2310 stub_sec->contents,
2311 stub_entry->stub_offset + 4,
2312 sym_value,
2313 0);
2314 break;
2315
2316 case aarch64_stub_long_branch:
2317 /* We want the value relative to the address 12 bytes back from the
2318 value itself. */
2319 _bfd_final_link_relocate (elfNN_aarch64_howto_from_type
2320 (AARCH64_R (PRELNN)), stub_bfd, stub_sec,
2321 stub_sec->contents,
2322 stub_entry->stub_offset + 16,
2323 sym_value + 12, 0);
2324 break;
2325 default:
2326 break;
2327 }
2328
2329 return TRUE;
2330 }
2331
2332 /* As above, but don't actually build the stub. Just bump offset so
2333 we know stub section sizes. */
2334
2335 static bfd_boolean
2336 aarch64_size_one_stub (struct bfd_hash_entry *gen_entry,
2337 void *in_arg ATTRIBUTE_UNUSED)
2338 {
2339 struct elf_aarch64_stub_hash_entry *stub_entry;
2340 int size;
2341
2342 /* Massage our args to the form they really have. */
2343 stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
2344
2345 switch (stub_entry->stub_type)
2346 {
2347 case aarch64_stub_adrp_branch:
2348 size = sizeof (aarch64_adrp_branch_stub);
2349 break;
2350 case aarch64_stub_long_branch:
2351 size = sizeof (aarch64_long_branch_stub);
2352 break;
2353 default:
2354 BFD_FAIL ();
2355 return FALSE;
2356 break;
2357 }
2358
2359 size = (size + 7) & ~7;
2360 stub_entry->stub_sec->size += size;
2361 return TRUE;
2362 }
2363
2364 /* External entry points for sizing and building linker stubs. */
2365
2366 /* Set up various things so that we can make a list of input sections
2367 for each output section included in the link. Returns -1 on error,
2368 0 when no stubs will be needed, and 1 on success. */
2369
2370 int
2371 elfNN_aarch64_setup_section_lists (bfd *output_bfd,
2372 struct bfd_link_info *info)
2373 {
2374 bfd *input_bfd;
2375 unsigned int bfd_count;
2376 int top_id, top_index;
2377 asection *section;
2378 asection **input_list, **list;
2379 bfd_size_type amt;
2380 struct elf_aarch64_link_hash_table *htab =
2381 elf_aarch64_hash_table (info);
2382
2383 if (!is_elf_hash_table (htab))
2384 return 0;
2385
2386 /* Count the number of input BFDs and find the top input section id. */
2387 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2388 input_bfd != NULL; input_bfd = input_bfd->link_next)
2389 {
2390 bfd_count += 1;
2391 for (section = input_bfd->sections;
2392 section != NULL; section = section->next)
2393 {
2394 if (top_id < section->id)
2395 top_id = section->id;
2396 }
2397 }
2398 htab->bfd_count = bfd_count;
2399
2400 amt = sizeof (struct map_stub) * (top_id + 1);
2401 htab->stub_group = bfd_zmalloc (amt);
2402 if (htab->stub_group == NULL)
2403 return -1;
2404
2405 /* We can't use output_bfd->section_count here to find the top output
2406 section index as some sections may have been removed, and
2407 _bfd_strip_section_from_output doesn't renumber the indices. */
2408 for (section = output_bfd->sections, top_index = 0;
2409 section != NULL; section = section->next)
2410 {
2411 if (top_index < section->index)
2412 top_index = section->index;
2413 }
2414
2415 htab->top_index = top_index;
2416 amt = sizeof (asection *) * (top_index + 1);
2417 input_list = bfd_malloc (amt);
2418 htab->input_list = input_list;
2419 if (input_list == NULL)
2420 return -1;
2421
2422 /* For sections we aren't interested in, mark their entries with a
2423 value we can check later. */
2424 list = input_list + top_index;
2425 do
2426 *list = bfd_abs_section_ptr;
2427 while (list-- != input_list);
2428
2429 for (section = output_bfd->sections;
2430 section != NULL; section = section->next)
2431 {
2432 if ((section->flags & SEC_CODE) != 0)
2433 input_list[section->index] = NULL;
2434 }
2435
2436 return 1;
2437 }
2438
2439 /* Used by elfNN_aarch64_next_input_section and group_sections. */
2440 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
2441
2442 /* The linker repeatedly calls this function for each input section,
2443 in the order that input sections are linked into output sections.
2444 Build lists of input sections to determine groupings between which
2445 we may insert linker stubs. */
2446
2447 void
2448 elfNN_aarch64_next_input_section (struct bfd_link_info *info, asection *isec)
2449 {
2450 struct elf_aarch64_link_hash_table *htab =
2451 elf_aarch64_hash_table (info);
2452
2453 if (isec->output_section->index <= htab->top_index)
2454 {
2455 asection **list = htab->input_list + isec->output_section->index;
2456
2457 if (*list != bfd_abs_section_ptr)
2458 {
2459 /* Steal the link_sec pointer for our list. */
2460 /* This happens to make the list in reverse order,
2461 which is what we want. */
2462 PREV_SEC (isec) = *list;
2463 *list = isec;
2464 }
2465 }
2466 }
2467
2468 /* See whether we can group stub sections together. Grouping stub
2469 sections may result in fewer stubs. More importantly, we need to
2470 put all .init* and .fini* stubs at the beginning of the .init or
2471 .fini output sections respectively, because glibc splits the
2472 _init and _fini functions into multiple parts. Putting a stub in
2473 the middle of a function is not a good idea. */
2474
2475 static void
2476 group_sections (struct elf_aarch64_link_hash_table *htab,
2477 bfd_size_type stub_group_size,
2478 bfd_boolean stubs_always_before_branch)
2479 {
2480 asection **list = htab->input_list + htab->top_index;
2481
2482 do
2483 {
2484 asection *tail = *list;
2485
2486 if (tail == bfd_abs_section_ptr)
2487 continue;
2488
2489 while (tail != NULL)
2490 {
2491 asection *curr;
2492 asection *prev;
2493 bfd_size_type total;
2494
2495 curr = tail;
2496 total = tail->size;
2497 while ((prev = PREV_SEC (curr)) != NULL
2498 && ((total += curr->output_offset - prev->output_offset)
2499 < stub_group_size))
2500 curr = prev;
2501
2502 /* OK, the size from the start of CURR to the end is less
2503 than stub_group_size and thus can be handled by one stub
2504 section. (Or the tail section is itself larger than
2505 stub_group_size, in which case we may be toast.)
2506 We should really be keeping track of the total size of
2507 stubs added here, as stubs contribute to the final output
2508 section size. */
2509 do
2510 {
2511 prev = PREV_SEC (tail);
2512 /* Set up this stub group. */
2513 htab->stub_group[tail->id].link_sec = curr;
2514 }
2515 while (tail != curr && (tail = prev) != NULL);
2516
2517 /* But wait, there's more! Input sections up to stub_group_size
2518 bytes before the stub section can be handled by it too. */
2519 if (!stubs_always_before_branch)
2520 {
2521 total = 0;
2522 while (prev != NULL
2523 && ((total += tail->output_offset - prev->output_offset)
2524 < stub_group_size))
2525 {
2526 tail = prev;
2527 prev = PREV_SEC (tail);
2528 htab->stub_group[tail->id].link_sec = curr;
2529 }
2530 }
2531 tail = prev;
2532 }
2533 }
2534 while (list-- != htab->input_list);
2535
2536 free (htab->input_list);
2537 }
2538
2539 #undef PREV_SEC
2540
2541 /* Determine and set the size of the stub section for a final link.
2542
2543 The basic idea here is to examine all the relocations looking for
2544 PC-relative calls to a target that is unreachable with a "bl"
2545 instruction. */
2546
2547 bfd_boolean
2548 elfNN_aarch64_size_stubs (bfd *output_bfd,
2549 bfd *stub_bfd,
2550 struct bfd_link_info *info,
2551 bfd_signed_vma group_size,
2552 asection * (*add_stub_section) (const char *,
2553 asection *),
2554 void (*layout_sections_again) (void))
2555 {
2556 bfd_size_type stub_group_size;
2557 bfd_boolean stubs_always_before_branch;
2558 bfd_boolean stub_changed = 0;
2559 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
2560
2561 /* Propagate mach to stub bfd, because it may not have been
2562 finalized when we created stub_bfd. */
2563 bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
2564 bfd_get_mach (output_bfd));
2565
2566 /* Stash our params away. */
2567 htab->stub_bfd = stub_bfd;
2568 htab->add_stub_section = add_stub_section;
2569 htab->layout_sections_again = layout_sections_again;
2570 stubs_always_before_branch = group_size < 0;
2571 if (group_size < 0)
2572 stub_group_size = -group_size;
2573 else
2574 stub_group_size = group_size;
2575
2576 if (stub_group_size == 1)
2577 {
2578 /* Default values. */
2579 /* AArch64 branch range is +-128MB. The value used is 1MB less. */
2580 stub_group_size = 127 * 1024 * 1024;
2581 }
2582
2583 group_sections (htab, stub_group_size, stubs_always_before_branch);
2584
2585 while (1)
2586 {
2587 bfd *input_bfd;
2588 unsigned int bfd_indx;
2589 asection *stub_sec;
2590
2591 for (input_bfd = info->input_bfds, bfd_indx = 0;
2592 input_bfd != NULL; input_bfd = input_bfd->link_next, bfd_indx++)
2593 {
2594 Elf_Internal_Shdr *symtab_hdr;
2595 asection *section;
2596 Elf_Internal_Sym *local_syms = NULL;
2597
2598 /* We'll need the symbol table in a second. */
2599 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2600 if (symtab_hdr->sh_info == 0)
2601 continue;
2602
2603 /* Walk over each section attached to the input bfd. */
2604 for (section = input_bfd->sections;
2605 section != NULL; section = section->next)
2606 {
2607 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2608
2609 /* If there aren't any relocs, then there's nothing more
2610 to do. */
2611 if ((section->flags & SEC_RELOC) == 0
2612 || section->reloc_count == 0
2613 || (section->flags & SEC_CODE) == 0)
2614 continue;
2615
2616 /* If this section is a link-once section that will be
2617 discarded, then don't create any stubs. */
2618 if (section->output_section == NULL
2619 || section->output_section->owner != output_bfd)
2620 continue;
2621
2622 /* Get the relocs. */
2623 internal_relocs
2624 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
2625 NULL, info->keep_memory);
2626 if (internal_relocs == NULL)
2627 goto error_ret_free_local;
2628
2629 /* Now examine each relocation. */
2630 irela = internal_relocs;
2631 irelaend = irela + section->reloc_count;
2632 for (; irela < irelaend; irela++)
2633 {
2634 unsigned int r_type, r_indx;
2635 enum elf_aarch64_stub_type stub_type;
2636 struct elf_aarch64_stub_hash_entry *stub_entry;
2637 asection *sym_sec;
2638 bfd_vma sym_value;
2639 bfd_vma destination;
2640 struct elf_aarch64_link_hash_entry *hash;
2641 const char *sym_name;
2642 char *stub_name;
2643 const asection *id_sec;
2644 unsigned char st_type;
2645 bfd_size_type len;
2646
2647 r_type = ELFNN_R_TYPE (irela->r_info);
2648 r_indx = ELFNN_R_SYM (irela->r_info);
2649
2650 if (r_type >= (unsigned int) R_AARCH64_end)
2651 {
2652 bfd_set_error (bfd_error_bad_value);
2653 error_ret_free_internal:
2654 if (elf_section_data (section)->relocs == NULL)
2655 free (internal_relocs);
2656 goto error_ret_free_local;
2657 }
2658
2659 /* Only look for stubs on unconditional branch and
2660 branch and link instructions. */
2661 if (r_type != (unsigned int) AARCH64_R (CALL26)
2662 && r_type != (unsigned int) AARCH64_R (JUMP26))
2663 continue;
2664
2665 /* Now determine the call target, its name, value,
2666 section. */
2667 sym_sec = NULL;
2668 sym_value = 0;
2669 destination = 0;
2670 hash = NULL;
2671 sym_name = NULL;
2672 if (r_indx < symtab_hdr->sh_info)
2673 {
2674 /* It's a local symbol. */
2675 Elf_Internal_Sym *sym;
2676 Elf_Internal_Shdr *hdr;
2677
2678 if (local_syms == NULL)
2679 {
2680 local_syms
2681 = (Elf_Internal_Sym *) symtab_hdr->contents;
2682 if (local_syms == NULL)
2683 local_syms
2684 = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2685 symtab_hdr->sh_info, 0,
2686 NULL, NULL, NULL);
2687 if (local_syms == NULL)
2688 goto error_ret_free_internal;
2689 }
2690
2691 sym = local_syms + r_indx;
2692 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2693 sym_sec = hdr->bfd_section;
2694 if (!sym_sec)
2695 /* This is an undefined symbol. It can never
2696 be resolved. */
2697 continue;
2698
2699 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2700 sym_value = sym->st_value;
2701 destination = (sym_value + irela->r_addend
2702 + sym_sec->output_offset
2703 + sym_sec->output_section->vma);
2704 st_type = ELF_ST_TYPE (sym->st_info);
2705 sym_name
2706 = bfd_elf_string_from_elf_section (input_bfd,
2707 symtab_hdr->sh_link,
2708 sym->st_name);
2709 }
2710 else
2711 {
2712 int e_indx;
2713
2714 e_indx = r_indx - symtab_hdr->sh_info;
2715 hash = ((struct elf_aarch64_link_hash_entry *)
2716 elf_sym_hashes (input_bfd)[e_indx]);
2717
2718 while (hash->root.root.type == bfd_link_hash_indirect
2719 || hash->root.root.type == bfd_link_hash_warning)
2720 hash = ((struct elf_aarch64_link_hash_entry *)
2721 hash->root.root.u.i.link);
2722
2723 if (hash->root.root.type == bfd_link_hash_defined
2724 || hash->root.root.type == bfd_link_hash_defweak)
2725 {
2726 struct elf_aarch64_link_hash_table *globals =
2727 elf_aarch64_hash_table (info);
2728 sym_sec = hash->root.root.u.def.section;
2729 sym_value = hash->root.root.u.def.value;
2730 /* For a destination in a shared library,
2731 use the PLT stub as target address to
2732 decide whether a branch stub is
2733 needed. */
2734 if (globals->root.splt != NULL && hash != NULL
2735 && hash->root.plt.offset != (bfd_vma) - 1)
2736 {
2737 sym_sec = globals->root.splt;
2738 sym_value = hash->root.plt.offset;
2739 if (sym_sec->output_section != NULL)
2740 destination = (sym_value
2741 + sym_sec->output_offset
2742 +
2743 sym_sec->output_section->vma);
2744 }
2745 else if (sym_sec->output_section != NULL)
2746 destination = (sym_value + irela->r_addend
2747 + sym_sec->output_offset
2748 + sym_sec->output_section->vma);
2749 }
2750 else if (hash->root.root.type == bfd_link_hash_undefined
2751 || (hash->root.root.type
2752 == bfd_link_hash_undefweak))
2753 {
2754 /* For a shared library, use the PLT stub as
2755 target address to decide whether a long
2756 branch stub is needed.
2757 For absolute code, they cannot be handled. */
2758 struct elf_aarch64_link_hash_table *globals =
2759 elf_aarch64_hash_table (info);
2760
2761 if (globals->root.splt != NULL && hash != NULL
2762 && hash->root.plt.offset != (bfd_vma) - 1)
2763 {
2764 sym_sec = globals->root.splt;
2765 sym_value = hash->root.plt.offset;
2766 if (sym_sec->output_section != NULL)
2767 destination = (sym_value
2768 + sym_sec->output_offset
2769 +
2770 sym_sec->output_section->vma);
2771 }
2772 else
2773 continue;
2774 }
2775 else
2776 {
2777 bfd_set_error (bfd_error_bad_value);
2778 goto error_ret_free_internal;
2779 }
2780 st_type = ELF_ST_TYPE (hash->root.type);
2781 sym_name = hash->root.root.root.string;
2782 }
2783
2784 /* Determine what (if any) linker stub is needed. */
2785 stub_type = aarch64_type_of_stub
2786 (info, section, irela, st_type, hash, destination);
2787 if (stub_type == aarch64_stub_none)
2788 continue;
2789
2790 /* Support for grouping stub sections. */
2791 id_sec = htab->stub_group[section->id].link_sec;
2792
2793 /* Get the name of this stub. */
2794 stub_name = elfNN_aarch64_stub_name (id_sec, sym_sec, hash,
2795 irela);
2796 if (!stub_name)
2797 goto error_ret_free_internal;
2798
2799 stub_entry =
2800 aarch64_stub_hash_lookup (&htab->stub_hash_table,
2801 stub_name, FALSE, FALSE);
2802 if (stub_entry != NULL)
2803 {
2804 /* The proper stub has already been created. */
2805 free (stub_name);
2806 continue;
2807 }
2808
2809 stub_entry = elfNN_aarch64_add_stub (stub_name, section,
2810 htab);
2811 if (stub_entry == NULL)
2812 {
2813 free (stub_name);
2814 goto error_ret_free_internal;
2815 }
2816
2817 stub_entry->target_value = sym_value;
2818 stub_entry->target_section = sym_sec;
2819 stub_entry->stub_type = stub_type;
2820 stub_entry->h = hash;
2821 stub_entry->st_type = st_type;
2822
2823 if (sym_name == NULL)
2824 sym_name = "unnamed";
2825 len = sizeof (STUB_ENTRY_NAME) + strlen (sym_name);
2826 stub_entry->output_name = bfd_alloc (htab->stub_bfd, len);
2827 if (stub_entry->output_name == NULL)
2828 {
2829 free (stub_name);
2830 goto error_ret_free_internal;
2831 }
2832
2833 snprintf (stub_entry->output_name, len, STUB_ENTRY_NAME,
2834 sym_name);
2835
2836 stub_changed = TRUE;
2837 }
2838
2839 /* We're done with the internal relocs, free them. */
2840 if (elf_section_data (section)->relocs == NULL)
2841 free (internal_relocs);
2842 }
2843 }
2844
2845 if (!stub_changed)
2846 break;
2847
2848 /* OK, we've added some stubs. Find out the new size of the
2849 stub sections. */
2850 for (stub_sec = htab->stub_bfd->sections;
2851 stub_sec != NULL; stub_sec = stub_sec->next)
2852 stub_sec->size = 0;
2853
2854 bfd_hash_traverse (&htab->stub_hash_table, aarch64_size_one_stub, htab);
2855
2856 /* Ask the linker to do its stuff. */
2857 (*htab->layout_sections_again) ();
2858 stub_changed = FALSE;
2859 }
2860
2861 return TRUE;
2862
2863 error_ret_free_local:
2864 return FALSE;
2865 }
2866
2867 /* Build all the stubs associated with the current output file. The
2868 stubs are kept in a hash table attached to the main linker hash
2869 table. We also set up the .plt entries for statically linked PIC
2870 functions here. This function is called via aarch64_elf_finish in the
2871 linker. */
2872
2873 bfd_boolean
2874 elfNN_aarch64_build_stubs (struct bfd_link_info *info)
2875 {
2876 asection *stub_sec;
2877 struct bfd_hash_table *table;
2878 struct elf_aarch64_link_hash_table *htab;
2879
2880 htab = elf_aarch64_hash_table (info);
2881
2882 for (stub_sec = htab->stub_bfd->sections;
2883 stub_sec != NULL; stub_sec = stub_sec->next)
2884 {
2885 bfd_size_type size;
2886
2887 /* Ignore non-stub sections. */
2888 if (!strstr (stub_sec->name, STUB_SUFFIX))
2889 continue;
2890
2891 /* Allocate memory to hold the linker stubs. */
2892 size = stub_sec->size;
2893 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
2894 if (stub_sec->contents == NULL && size != 0)
2895 return FALSE;
2896 stub_sec->size = 0;
2897 }
2898
2899 /* Build the stubs as directed by the stub hash table. */
2900 table = &htab->stub_hash_table;
2901 bfd_hash_traverse (table, aarch64_build_one_stub, info);
2902
2903 return TRUE;
2904 }
2905
2906
2907 /* Add an entry to the code/data map for section SEC. */
2908
2909 static void
2910 elfNN_aarch64_section_map_add (asection *sec, char type, bfd_vma vma)
2911 {
2912 struct _aarch64_elf_section_data *sec_data =
2913 elf_aarch64_section_data (sec);
2914 unsigned int newidx;
2915
2916 if (sec_data->map == NULL)
2917 {
2918 sec_data->map = bfd_malloc (sizeof (elf_aarch64_section_map));
2919 sec_data->mapcount = 0;
2920 sec_data->mapsize = 1;
2921 }
2922
2923 newidx = sec_data->mapcount++;
2924
2925 if (sec_data->mapcount > sec_data->mapsize)
2926 {
2927 sec_data->mapsize *= 2;
2928 sec_data->map = bfd_realloc_or_free
2929 (sec_data->map, sec_data->mapsize * sizeof (elf_aarch64_section_map));
2930 }
2931
2932 if (sec_data->map)
2933 {
2934 sec_data->map[newidx].vma = vma;
2935 sec_data->map[newidx].type = type;
2936 }
2937 }
2938
2939
2940 /* Initialise maps of insn/data for input BFDs. */
2941 void
2942 bfd_elfNN_aarch64_init_maps (bfd *abfd)
2943 {
2944 Elf_Internal_Sym *isymbuf;
2945 Elf_Internal_Shdr *hdr;
2946 unsigned int i, localsyms;
2947
2948 /* Make sure that we are dealing with an AArch64 elf binary. */
2949 if (!is_aarch64_elf (abfd))
2950 return;
2951
2952 if ((abfd->flags & DYNAMIC) != 0)
2953 return;
2954
2955 hdr = &elf_symtab_hdr (abfd);
2956 localsyms = hdr->sh_info;
2957
2958 /* Obtain a buffer full of symbols for this BFD. The hdr->sh_info field
2959 should contain the number of local symbols, which should come before any
2960 global symbols. Mapping symbols are always local. */
2961 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, localsyms, 0, NULL, NULL, NULL);
2962
2963 /* No internal symbols read? Skip this BFD. */
2964 if (isymbuf == NULL)
2965 return;
2966
2967 for (i = 0; i < localsyms; i++)
2968 {
2969 Elf_Internal_Sym *isym = &isymbuf[i];
2970 asection *sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2971 const char *name;
2972
2973 if (sec != NULL && ELF_ST_BIND (isym->st_info) == STB_LOCAL)
2974 {
2975 name = bfd_elf_string_from_elf_section (abfd,
2976 hdr->sh_link,
2977 isym->st_name);
2978
2979 if (bfd_is_aarch64_special_symbol_name
2980 (name, BFD_AARCH64_SPECIAL_SYM_TYPE_MAP))
2981 elfNN_aarch64_section_map_add (sec, name[1], isym->st_value);
2982 }
2983 }
2984 }
2985
2986 /* Set option values needed during linking. */
2987 void
2988 bfd_elfNN_aarch64_set_options (struct bfd *output_bfd,
2989 struct bfd_link_info *link_info,
2990 int no_enum_warn,
2991 int no_wchar_warn, int pic_veneer)
2992 {
2993 struct elf_aarch64_link_hash_table *globals;
2994
2995 globals = elf_aarch64_hash_table (link_info);
2996 globals->pic_veneer = pic_veneer;
2997
2998 BFD_ASSERT (is_aarch64_elf (output_bfd));
2999 elf_aarch64_tdata (output_bfd)->no_enum_size_warning = no_enum_warn;
3000 elf_aarch64_tdata (output_bfd)->no_wchar_size_warning = no_wchar_warn;
3001 }
3002
3003 static bfd_vma
3004 aarch64_calculate_got_entry_vma (struct elf_link_hash_entry *h,
3005 struct elf_aarch64_link_hash_table
3006 *globals, struct bfd_link_info *info,
3007 bfd_vma value, bfd *output_bfd,
3008 bfd_boolean *unresolved_reloc_p)
3009 {
3010 bfd_vma off = (bfd_vma) - 1;
3011 asection *basegot = globals->root.sgot;
3012 bfd_boolean dyn = globals->root.dynamic_sections_created;
3013
3014 if (h != NULL)
3015 {
3016 BFD_ASSERT (basegot != NULL);
3017 off = h->got.offset;
3018 BFD_ASSERT (off != (bfd_vma) - 1);
3019 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3020 || (info->shared
3021 && SYMBOL_REFERENCES_LOCAL (info, h))
3022 || (ELF_ST_VISIBILITY (h->other)
3023 && h->root.type == bfd_link_hash_undefweak))
3024 {
3025 /* This is actually a static link, or it is a -Bsymbolic link
3026 and the symbol is defined locally. We must initialize this
3027 entry in the global offset table. Since the offset must
3028 always be a multiple of 8 (4 in the case of ILP32), we use
3029 the least significant bit to record whether we have
3030 initialized it already.
3031 When doing a dynamic link, we create a .rel(a).got relocation
3032 entry to initialize the value. This is done in the
3033 finish_dynamic_symbol routine. */
3034 if ((off & 1) != 0)
3035 off &= ~1;
3036 else
3037 {
3038 bfd_put_NN (output_bfd, value, basegot->contents + off);
3039 h->got.offset |= 1;
3040 }
3041 }
3042 else
3043 *unresolved_reloc_p = FALSE;
3044
3045 off = off + basegot->output_section->vma + basegot->output_offset;
3046 }
3047
3048 return off;
3049 }
3050
3051 /* Change R_TYPE to a more efficient access model where possible,
3052 return the new reloc type. */
3053
3054 static bfd_reloc_code_real_type
3055 aarch64_tls_transition_without_check (bfd_reloc_code_real_type r_type,
3056 struct elf_link_hash_entry *h)
3057 {
3058 bfd_boolean is_local = h == NULL;
3059
3060 switch (r_type)
3061 {
3062 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
3063 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
3064 return (is_local
3065 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1
3066 : BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21);
3067
3068 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
3069 case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
3070 return (is_local
3071 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
3072 : BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC);
3073
3074 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
3075 return is_local ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1 : r_type;
3076
3077 case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
3078 return is_local ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC : r_type;
3079
3080 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
3081 case BFD_RELOC_AARCH64_TLSDESC_CALL:
3082 /* Instructions with these relocations will become NOPs. */
3083 return BFD_RELOC_AARCH64_NONE;
3084
3085 default:
3086 break;
3087 }
3088
3089 return r_type;
3090 }
3091
3092 static unsigned int
3093 aarch64_reloc_got_type (bfd_reloc_code_real_type r_type)
3094 {
3095 switch (r_type)
3096 {
3097 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
3098 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
3099 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
3100 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
3101 return GOT_NORMAL;
3102
3103 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
3104 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
3105 return GOT_TLS_GD;
3106
3107 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
3108 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
3109 case BFD_RELOC_AARCH64_TLSDESC_CALL:
3110 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
3111 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
3112 return GOT_TLSDESC_GD;
3113
3114 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
3115 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
3116 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
3117 return GOT_TLS_IE;
3118
3119 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
3120 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
3121 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
3122 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
3123 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
3124 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
3125 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
3126 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
3127 return GOT_UNKNOWN;
3128
3129 default:
3130 break;
3131 }
3132 return GOT_UNKNOWN;
3133 }
3134
3135 static bfd_boolean
3136 aarch64_can_relax_tls (bfd *input_bfd,
3137 struct bfd_link_info *info,
3138 bfd_reloc_code_real_type r_type,
3139 struct elf_link_hash_entry *h,
3140 unsigned long r_symndx)
3141 {
3142 unsigned int symbol_got_type;
3143 unsigned int reloc_got_type;
3144
3145 if (! IS_AARCH64_TLS_RELOC (r_type))
3146 return FALSE;
3147
3148 symbol_got_type = elfNN_aarch64_symbol_got_type (h, input_bfd, r_symndx);
3149 reloc_got_type = aarch64_reloc_got_type (r_type);
3150
3151 if (symbol_got_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (reloc_got_type))
3152 return TRUE;
3153
3154 if (info->shared)
3155 return FALSE;
3156
3157 if (h && h->root.type == bfd_link_hash_undefweak)
3158 return FALSE;
3159
3160 return TRUE;
3161 }
3162
3163 /* Given the relocation code R_TYPE, return the relaxed bfd reloc
3164 enumerator. */
3165
3166 static bfd_reloc_code_real_type
3167 aarch64_tls_transition (bfd *input_bfd,
3168 struct bfd_link_info *info,
3169 unsigned int r_type,
3170 struct elf_link_hash_entry *h,
3171 unsigned long r_symndx)
3172 {
3173 bfd_reloc_code_real_type bfd_r_type
3174 = elfNN_aarch64_bfd_reloc_from_type (r_type);
3175
3176 if (! aarch64_can_relax_tls (input_bfd, info, bfd_r_type, h, r_symndx))
3177 return bfd_r_type;
3178
3179 return aarch64_tls_transition_without_check (bfd_r_type, h);
3180 }
3181
3182 /* Return the base VMA address which should be subtracted from real addresses
3183 when resolving R_AARCH64_TLS_DTPREL relocation. */
3184
3185 static bfd_vma
3186 dtpoff_base (struct bfd_link_info *info)
3187 {
3188 /* If tls_sec is NULL, we should have signalled an error already. */
3189 BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
3190 return elf_hash_table (info)->tls_sec->vma;
3191 }
3192
3193 /* Return the base VMA address which should be subtracted from real addresses
3194 when resolving R_AARCH64_TLS_GOTTPREL64 relocations. */
3195
3196 static bfd_vma
3197 tpoff_base (struct bfd_link_info *info)
3198 {
3199 struct elf_link_hash_table *htab = elf_hash_table (info);
3200
3201 /* If tls_sec is NULL, we should have signalled an error already. */
3202 if (htab->tls_sec == NULL)
3203 return 0;
3204
3205 bfd_vma base = align_power ((bfd_vma) TCB_SIZE,
3206 htab->tls_sec->alignment_power);
3207 return htab->tls_sec->vma - base;
3208 }
3209
3210 static bfd_vma *
3211 symbol_got_offset_ref (bfd *input_bfd, struct elf_link_hash_entry *h,
3212 unsigned long r_symndx)
3213 {
3214 /* Calculate the address of the GOT entry for symbol
3215 referred to in h. */
3216 if (h != NULL)
3217 return &h->got.offset;
3218 else
3219 {
3220 /* local symbol */
3221 struct elf_aarch64_local_symbol *l;
3222
3223 l = elf_aarch64_locals (input_bfd);
3224 return &l[r_symndx].got_offset;
3225 }
3226 }
3227
3228 static void
3229 symbol_got_offset_mark (bfd *input_bfd, struct elf_link_hash_entry *h,
3230 unsigned long r_symndx)
3231 {
3232 bfd_vma *p;
3233 p = symbol_got_offset_ref (input_bfd, h, r_symndx);
3234 *p |= 1;
3235 }
3236
3237 static int
3238 symbol_got_offset_mark_p (bfd *input_bfd, struct elf_link_hash_entry *h,
3239 unsigned long r_symndx)
3240 {
3241 bfd_vma value;
3242 value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
3243 return value & 1;
3244 }
3245
3246 static bfd_vma
3247 symbol_got_offset (bfd *input_bfd, struct elf_link_hash_entry *h,
3248 unsigned long r_symndx)
3249 {
3250 bfd_vma value;
3251 value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
3252 value &= ~1;
3253 return value;
3254 }
3255
3256 static bfd_vma *
3257 symbol_tlsdesc_got_offset_ref (bfd *input_bfd, struct elf_link_hash_entry *h,
3258 unsigned long r_symndx)
3259 {
3260 /* Calculate the address of the GOT entry for symbol
3261 referred to in h. */
3262 if (h != NULL)
3263 {
3264 struct elf_aarch64_link_hash_entry *eh;
3265 eh = (struct elf_aarch64_link_hash_entry *) h;
3266 return &eh->tlsdesc_got_jump_table_offset;
3267 }
3268 else
3269 {
3270 /* local symbol */
3271 struct elf_aarch64_local_symbol *l;
3272
3273 l = elf_aarch64_locals (input_bfd);
3274 return &l[r_symndx].tlsdesc_got_jump_table_offset;
3275 }
3276 }
3277
3278 static void
3279 symbol_tlsdesc_got_offset_mark (bfd *input_bfd, struct elf_link_hash_entry *h,
3280 unsigned long r_symndx)
3281 {
3282 bfd_vma *p;
3283 p = symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
3284 *p |= 1;
3285 }
3286
3287 static int
3288 symbol_tlsdesc_got_offset_mark_p (bfd *input_bfd,
3289 struct elf_link_hash_entry *h,
3290 unsigned long r_symndx)
3291 {
3292 bfd_vma value;
3293 value = * symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
3294 return value & 1;
3295 }
3296
3297 static bfd_vma
3298 symbol_tlsdesc_got_offset (bfd *input_bfd, struct elf_link_hash_entry *h,
3299 unsigned long r_symndx)
3300 {
3301 bfd_vma value;
3302 value = * symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
3303 value &= ~1;
3304 return value;
3305 }
3306
3307 /* Perform a relocation as part of a final link. */
3308 static bfd_reloc_status_type
3309 elfNN_aarch64_final_link_relocate (reloc_howto_type *howto,
3310 bfd *input_bfd,
3311 bfd *output_bfd,
3312 asection *input_section,
3313 bfd_byte *contents,
3314 Elf_Internal_Rela *rel,
3315 bfd_vma value,
3316 struct bfd_link_info *info,
3317 asection *sym_sec,
3318 struct elf_link_hash_entry *h,
3319 bfd_boolean *unresolved_reloc_p,
3320 bfd_boolean save_addend,
3321 bfd_vma *saved_addend)
3322 {
3323 unsigned int r_type = howto->type;
3324 bfd_reloc_code_real_type bfd_r_type
3325 = elfNN_aarch64_bfd_reloc_from_howto (howto);
3326 bfd_reloc_code_real_type new_bfd_r_type;
3327 unsigned long r_symndx;
3328 bfd_byte *hit_data = contents + rel->r_offset;
3329 bfd_vma place;
3330 bfd_signed_vma signed_addend;
3331 struct elf_aarch64_link_hash_table *globals;
3332 bfd_boolean weak_undef_p;
3333
3334 globals = elf_aarch64_hash_table (info);
3335
3336 BFD_ASSERT (is_aarch64_elf (input_bfd));
3337
3338 r_symndx = ELFNN_R_SYM (rel->r_info);
3339
3340 /* It is possible to have linker relaxations on some TLS access
3341 models. Update our information here. */
3342 new_bfd_r_type = aarch64_tls_transition (input_bfd, info, r_type, h, r_symndx);
3343 if (new_bfd_r_type != bfd_r_type)
3344 {
3345 bfd_r_type = new_bfd_r_type;
3346 howto = elfNN_aarch64_howto_from_bfd_reloc (bfd_r_type);
3347 BFD_ASSERT (howto != NULL);
3348 r_type = howto->type;
3349 }
3350
3351 place = input_section->output_section->vma
3352 + input_section->output_offset + rel->r_offset;
3353
3354 /* Get addend, accumulating the addend for consecutive relocs
3355 which refer to the same offset. */
3356 signed_addend = saved_addend ? *saved_addend : 0;
3357 signed_addend += rel->r_addend;
3358
3359 weak_undef_p = (h ? h->root.type == bfd_link_hash_undefweak
3360 : bfd_is_und_section (sym_sec));
3361
3362 switch (bfd_r_type)
3363 {
3364 case BFD_RELOC_AARCH64_NONE:
3365 case BFD_RELOC_AARCH64_TLSDESC_CALL:
3366 *unresolved_reloc_p = FALSE;
3367 return bfd_reloc_ok;
3368
3369 case BFD_RELOC_AARCH64_NN:
3370
3371 /* When generating a shared object or relocatable executable, these
3372 relocations are copied into the output file to be resolved at
3373 run time. */
3374 if (((info->shared == TRUE) || globals->root.is_relocatable_executable)
3375 && (input_section->flags & SEC_ALLOC)
3376 && (h == NULL
3377 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3378 || h->root.type != bfd_link_hash_undefweak))
3379 {
3380 Elf_Internal_Rela outrel;
3381 bfd_byte *loc;
3382 bfd_boolean skip, relocate;
3383 asection *sreloc;
3384
3385 *unresolved_reloc_p = FALSE;
3386
3387 sreloc = _bfd_elf_get_dynamic_reloc_section (input_bfd,
3388 input_section, 1);
3389 if (sreloc == NULL)
3390 return bfd_reloc_notsupported;
3391
3392 skip = FALSE;
3393 relocate = FALSE;
3394
3395 outrel.r_addend = signed_addend;
3396 outrel.r_offset =
3397 _bfd_elf_section_offset (output_bfd, info, input_section,
3398 rel->r_offset);
3399 if (outrel.r_offset == (bfd_vma) - 1)
3400 skip = TRUE;
3401 else if (outrel.r_offset == (bfd_vma) - 2)
3402 {
3403 skip = TRUE;
3404 relocate = TRUE;
3405 }
3406
3407 outrel.r_offset += (input_section->output_section->vma
3408 + input_section->output_offset);
3409
3410 if (skip)
3411 memset (&outrel, 0, sizeof outrel);
3412 else if (h != NULL
3413 && h->dynindx != -1
3414 && (!info->shared || !info->symbolic || !h->def_regular))
3415 outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
3416 else
3417 {
3418 int symbol;
3419
3420 /* On SVR4-ish systems, the dynamic loader cannot
3421 relocate the text and data segments independently,
3422 so the symbol does not matter. */
3423 symbol = 0;
3424 outrel.r_info = ELFNN_R_INFO (symbol, AARCH64_R (RELATIVE));
3425 outrel.r_addend += value;
3426 }
3427
3428 loc = sreloc->contents + sreloc->reloc_count++ * RELOC_SIZE (htab);
3429 bfd_elfNN_swap_reloca_out (output_bfd, &outrel, loc);
3430
3431 if (sreloc->reloc_count * RELOC_SIZE (htab) > sreloc->size)
3432 {
3433 /* Sanity to check that we have previously allocated
3434 sufficient space in the relocation section for the
3435 number of relocations we actually want to emit. */
3436 abort ();
3437 }
3438
3439 /* If this reloc is against an external symbol, we do not want to
3440 fiddle with the addend. Otherwise, we need to include the symbol
3441 value so that it becomes an addend for the dynamic reloc. */
3442 if (!relocate)
3443 return bfd_reloc_ok;
3444
3445 return _bfd_final_link_relocate (howto, input_bfd, input_section,
3446 contents, rel->r_offset, value,
3447 signed_addend);
3448 }
3449 else
3450 value += signed_addend;
3451 break;
3452
3453 case BFD_RELOC_AARCH64_JUMP26:
3454 case BFD_RELOC_AARCH64_CALL26:
3455 {
3456 asection *splt = globals->root.splt;
3457 bfd_boolean via_plt_p =
3458 splt != NULL && h != NULL && h->plt.offset != (bfd_vma) - 1;
3459
3460 /* A call to an undefined weak symbol is converted to a jump to
3461 the next instruction unless a PLT entry will be created.
3462 The jump to the next instruction is optimized as a NOP.
3463 Do the same for local undefined symbols. */
3464 if (weak_undef_p && ! via_plt_p)
3465 {
3466 bfd_putl32 (INSN_NOP, hit_data);
3467 return bfd_reloc_ok;
3468 }
3469
3470 /* If the call goes through a PLT entry, make sure to
3471 check distance to the right destination address. */
3472 if (via_plt_p)
3473 {
3474 value = (splt->output_section->vma
3475 + splt->output_offset + h->plt.offset);
3476 *unresolved_reloc_p = FALSE;
3477 }
3478
3479 /* If the target symbol is global and marked as a function the
3480 relocation applies a function call or a tail call. In this
3481 situation we can veneer out of range branches. The veneers
3482 use IP0 and IP1 hence cannot be used arbitrary out of range
3483 branches that occur within the body of a function. */
3484 if (h && h->type == STT_FUNC)
3485 {
3486 /* Check if a stub has to be inserted because the destination
3487 is too far away. */
3488 if (! aarch64_valid_branch_p (value, place))
3489 {
3490 /* The target is out of reach, so redirect the branch to
3491 the local stub for this function. */
3492 struct elf_aarch64_stub_hash_entry *stub_entry;
3493 stub_entry = elfNN_aarch64_get_stub_entry (input_section,
3494 sym_sec, h,
3495 rel, globals);
3496 if (stub_entry != NULL)
3497 value = (stub_entry->stub_offset
3498 + stub_entry->stub_sec->output_offset
3499 + stub_entry->stub_sec->output_section->vma);
3500 }
3501 }
3502 }
3503 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
3504 signed_addend, weak_undef_p);
3505 break;
3506
3507 case BFD_RELOC_AARCH64_16:
3508 #if ARCH_SIZE == 64
3509 case BFD_RELOC_AARCH64_32:
3510 #endif
3511 case BFD_RELOC_AARCH64_ADD_LO12:
3512 case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
3513 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
3514 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
3515 case BFD_RELOC_AARCH64_BRANCH19:
3516 case BFD_RELOC_AARCH64_LD_LO19_PCREL:
3517 case BFD_RELOC_AARCH64_LDST8_LO12:
3518 case BFD_RELOC_AARCH64_LDST16_LO12:
3519 case BFD_RELOC_AARCH64_LDST32_LO12:
3520 case BFD_RELOC_AARCH64_LDST64_LO12:
3521 case BFD_RELOC_AARCH64_LDST128_LO12:
3522 case BFD_RELOC_AARCH64_MOVW_G0_S:
3523 case BFD_RELOC_AARCH64_MOVW_G1_S:
3524 case BFD_RELOC_AARCH64_MOVW_G2_S:
3525 case BFD_RELOC_AARCH64_MOVW_G0:
3526 case BFD_RELOC_AARCH64_MOVW_G0_NC:
3527 case BFD_RELOC_AARCH64_MOVW_G1:
3528 case BFD_RELOC_AARCH64_MOVW_G1_NC:
3529 case BFD_RELOC_AARCH64_MOVW_G2:
3530 case BFD_RELOC_AARCH64_MOVW_G2_NC:
3531 case BFD_RELOC_AARCH64_MOVW_G3:
3532 case BFD_RELOC_AARCH64_16_PCREL:
3533 case BFD_RELOC_AARCH64_32_PCREL:
3534 case BFD_RELOC_AARCH64_64_PCREL:
3535 case BFD_RELOC_AARCH64_TSTBR14:
3536 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
3537 signed_addend, weak_undef_p);
3538 break;
3539
3540 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
3541 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
3542 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
3543 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
3544 if (globals->root.sgot == NULL)
3545 BFD_ASSERT (h != NULL);
3546
3547 if (h != NULL)
3548 {
3549 value = aarch64_calculate_got_entry_vma (h, globals, info, value,
3550 output_bfd,
3551 unresolved_reloc_p);
3552 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
3553 0, weak_undef_p);
3554 }
3555 break;
3556
3557 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
3558 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
3559 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
3560 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
3561 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
3562 if (globals->root.sgot == NULL)
3563 return bfd_reloc_notsupported;
3564
3565 value = (symbol_got_offset (input_bfd, h, r_symndx)
3566 + globals->root.sgot->output_section->vma
3567 + globals->root.sgot->output_section->output_offset);
3568
3569 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
3570 0, weak_undef_p);
3571 *unresolved_reloc_p = FALSE;
3572 break;
3573
3574 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
3575 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
3576 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
3577 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
3578 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
3579 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
3580 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
3581 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
3582 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
3583 signed_addend - tpoff_base (info),
3584 weak_undef_p);
3585 *unresolved_reloc_p = FALSE;
3586 break;
3587
3588 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
3589 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
3590 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
3591 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
3592 case BFD_RELOC_AARCH64_TLSDESC_ADD:
3593 case BFD_RELOC_AARCH64_TLSDESC_LDR:
3594 if (globals->root.sgot == NULL)
3595 return bfd_reloc_notsupported;
3596
3597 value = (symbol_tlsdesc_got_offset (input_bfd, h, r_symndx)
3598 + globals->root.sgotplt->output_section->vma
3599 + globals->root.sgotplt->output_section->output_offset
3600 + globals->sgotplt_jump_table_size);
3601
3602 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
3603 0, weak_undef_p);
3604 *unresolved_reloc_p = FALSE;
3605 break;
3606
3607 default:
3608 return bfd_reloc_notsupported;
3609 }
3610
3611 if (saved_addend)
3612 *saved_addend = value;
3613
3614 /* Only apply the final relocation in a sequence. */
3615 if (save_addend)
3616 return bfd_reloc_continue;
3617
3618 return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type,
3619 howto, value);
3620 }
3621
3622 /* Handle TLS relaxations. Relaxing is possible for symbols that use
3623 R_AARCH64_TLSDESC_ADR_{PAGE, LD64_LO12_NC, ADD_LO12_NC} during a static
3624 link.
3625
3626 Return bfd_reloc_ok if we're done, bfd_reloc_continue if the caller
3627 is to then call final_link_relocate. Return other values in the
3628 case of error. */
3629
3630 static bfd_reloc_status_type
3631 elfNN_aarch64_tls_relax (struct elf_aarch64_link_hash_table *globals,
3632 bfd *input_bfd, bfd_byte *contents,
3633 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h)
3634 {
3635 bfd_boolean is_local = h == NULL;
3636 unsigned int r_type = ELFNN_R_TYPE (rel->r_info);
3637 unsigned long insn;
3638
3639 BFD_ASSERT (globals && input_bfd && contents && rel);
3640
3641 switch (elfNN_aarch64_bfd_reloc_from_type (r_type))
3642 {
3643 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
3644 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
3645 if (is_local)
3646 {
3647 /* GD->LE relaxation:
3648 adrp x0, :tlsgd:var => movz x0, :tprel_g1:var
3649 or
3650 adrp x0, :tlsdesc:var => movz x0, :tprel_g1:var
3651 */
3652 bfd_putl32 (0xd2a00000, contents + rel->r_offset);
3653 return bfd_reloc_continue;
3654 }
3655 else
3656 {
3657 /* GD->IE relaxation:
3658 adrp x0, :tlsgd:var => adrp x0, :gottprel:var
3659 or
3660 adrp x0, :tlsdesc:var => adrp x0, :gottprel:var
3661 */
3662 insn = bfd_getl32 (contents + rel->r_offset);
3663 return bfd_reloc_continue;
3664 }
3665
3666 case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
3667 if (is_local)
3668 {
3669 /* GD->LE relaxation:
3670 ldr xd, [x0, #:tlsdesc_lo12:var] => movk x0, :tprel_g0_nc:var
3671 */
3672 bfd_putl32 (0xf2800000, contents + rel->r_offset);
3673 return bfd_reloc_continue;
3674 }
3675 else
3676 {
3677 /* GD->IE relaxation:
3678 ldr xd, [x0, #:tlsdesc_lo12:var] => ldr x0, [x0, #:gottprel_lo12:var]
3679 */
3680 insn = bfd_getl32 (contents + rel->r_offset);
3681 insn &= 0xfffffff0;
3682 bfd_putl32 (insn, contents + rel->r_offset);
3683 return bfd_reloc_continue;
3684 }
3685
3686 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
3687 if (is_local)
3688 {
3689 /* GD->LE relaxation
3690 add x0, #:tlsgd_lo12:var => movk x0, :tprel_g0_nc:var
3691 bl __tls_get_addr => mrs x1, tpidr_el0
3692 nop => add x0, x1, x0
3693 */
3694
3695 /* First kill the tls_get_addr reloc on the bl instruction. */
3696 BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
3697 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
3698
3699 bfd_putl32 (0xf2800000, contents + rel->r_offset);
3700 bfd_putl32 (0xd53bd041, contents + rel->r_offset + 4);
3701 bfd_putl32 (0x8b000020, contents + rel->r_offset + 8);
3702 return bfd_reloc_continue;
3703 }
3704 else
3705 {
3706 /* GD->IE relaxation
3707 ADD x0, #:tlsgd_lo12:var => ldr x0, [x0, #:gottprel_lo12:var]
3708 BL __tls_get_addr => mrs x1, tpidr_el0
3709 R_AARCH64_CALL26
3710 NOP => add x0, x1, x0
3711 */
3712
3713 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
3714
3715 /* Remove the relocation on the BL instruction. */
3716 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
3717
3718 bfd_putl32 (0xf9400000, contents + rel->r_offset);
3719
3720 /* We choose to fixup the BL and NOP instructions using the
3721 offset from the second relocation to allow flexibility in
3722 scheduling instructions between the ADD and BL. */
3723 bfd_putl32 (0xd53bd041, contents + rel[1].r_offset);
3724 bfd_putl32 (0x8b000020, contents + rel[1].r_offset + 4);
3725 return bfd_reloc_continue;
3726 }
3727
3728 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
3729 case BFD_RELOC_AARCH64_TLSDESC_CALL:
3730 /* GD->IE/LE relaxation:
3731 add x0, x0, #:tlsdesc_lo12:var => nop
3732 blr xd => nop
3733 */
3734 bfd_putl32 (INSN_NOP, contents + rel->r_offset);
3735 return bfd_reloc_ok;
3736
3737 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
3738 /* IE->LE relaxation:
3739 adrp xd, :gottprel:var => movz xd, :tprel_g1:var
3740 */
3741 if (is_local)
3742 {
3743 insn = bfd_getl32 (contents + rel->r_offset);
3744 bfd_putl32 (0xd2a00000 | (insn & 0x1f), contents + rel->r_offset);
3745 }
3746 return bfd_reloc_continue;
3747
3748 case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
3749 /* IE->LE relaxation:
3750 ldr xd, [xm, #:gottprel_lo12:var] => movk xd, :tprel_g0_nc:var
3751 */
3752 if (is_local)
3753 {
3754 insn = bfd_getl32 (contents + rel->r_offset);
3755 bfd_putl32 (0xf2800000 | (insn & 0x1f), contents + rel->r_offset);
3756 }
3757 return bfd_reloc_continue;
3758
3759 default:
3760 return bfd_reloc_continue;
3761 }
3762
3763 return bfd_reloc_ok;
3764 }
3765
3766 /* Relocate an AArch64 ELF section. */
3767
3768 static bfd_boolean
3769 elfNN_aarch64_relocate_section (bfd *output_bfd,
3770 struct bfd_link_info *info,
3771 bfd *input_bfd,
3772 asection *input_section,
3773 bfd_byte *contents,
3774 Elf_Internal_Rela *relocs,
3775 Elf_Internal_Sym *local_syms,
3776 asection **local_sections)
3777 {
3778 Elf_Internal_Shdr *symtab_hdr;
3779 struct elf_link_hash_entry **sym_hashes;
3780 Elf_Internal_Rela *rel;
3781 Elf_Internal_Rela *relend;
3782 const char *name;
3783 struct elf_aarch64_link_hash_table *globals;
3784 bfd_boolean save_addend = FALSE;
3785 bfd_vma addend = 0;
3786
3787 globals = elf_aarch64_hash_table (info);
3788
3789 symtab_hdr = &elf_symtab_hdr (input_bfd);
3790 sym_hashes = elf_sym_hashes (input_bfd);
3791
3792 rel = relocs;
3793 relend = relocs + input_section->reloc_count;
3794 for (; rel < relend; rel++)
3795 {
3796 unsigned int r_type;
3797 bfd_reloc_code_real_type bfd_r_type;
3798 bfd_reloc_code_real_type relaxed_bfd_r_type;
3799 reloc_howto_type *howto;
3800 unsigned long r_symndx;
3801 Elf_Internal_Sym *sym;
3802 asection *sec;
3803 struct elf_link_hash_entry *h;
3804 bfd_vma relocation;
3805 bfd_reloc_status_type r;
3806 arelent bfd_reloc;
3807 char sym_type;
3808 bfd_boolean unresolved_reloc = FALSE;
3809 char *error_message = NULL;
3810
3811 r_symndx = ELFNN_R_SYM (rel->r_info);
3812 r_type = ELFNN_R_TYPE (rel->r_info);
3813
3814 bfd_reloc.howto = elfNN_aarch64_howto_from_type (r_type);
3815 howto = bfd_reloc.howto;
3816
3817 if (howto == NULL)
3818 {
3819 (*_bfd_error_handler)
3820 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
3821 input_bfd, input_section, r_type);
3822 return FALSE;
3823 }
3824 bfd_r_type = elfNN_aarch64_bfd_reloc_from_howto (howto);
3825
3826 h = NULL;
3827 sym = NULL;
3828 sec = NULL;
3829
3830 if (r_symndx < symtab_hdr->sh_info)
3831 {
3832 sym = local_syms + r_symndx;
3833 sym_type = ELFNN_ST_TYPE (sym->st_info);
3834 sec = local_sections[r_symndx];
3835
3836 /* An object file might have a reference to a local
3837 undefined symbol. This is a daft object file, but we
3838 should at least do something about it. */
3839 if (r_type != R_AARCH64_NONE && r_type != R_AARCH64_NULL
3840 && bfd_is_und_section (sec)
3841 && ELF_ST_BIND (sym->st_info) != STB_WEAK)
3842 {
3843 if (!info->callbacks->undefined_symbol
3844 (info, bfd_elf_string_from_elf_section
3845 (input_bfd, symtab_hdr->sh_link, sym->st_name),
3846 input_bfd, input_section, rel->r_offset, TRUE))
3847 return FALSE;
3848 }
3849
3850 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
3851 }
3852 else
3853 {
3854 bfd_boolean warned;
3855
3856 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3857 r_symndx, symtab_hdr, sym_hashes,
3858 h, sec, relocation,
3859 unresolved_reloc, warned);
3860
3861 sym_type = h->type;
3862 }
3863
3864 if (sec != NULL && discarded_section (sec))
3865 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3866 rel, 1, relend, howto, 0, contents);
3867
3868 if (info->relocatable)
3869 {
3870 /* This is a relocatable link. We don't have to change
3871 anything, unless the reloc is against a section symbol,
3872 in which case we have to adjust according to where the
3873 section symbol winds up in the output section. */
3874 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3875 rel->r_addend += sec->output_offset;
3876 continue;
3877 }
3878
3879 if (h != NULL)
3880 name = h->root.root.string;
3881 else
3882 {
3883 name = (bfd_elf_string_from_elf_section
3884 (input_bfd, symtab_hdr->sh_link, sym->st_name));
3885 if (name == NULL || *name == '\0')
3886 name = bfd_section_name (input_bfd, sec);
3887 }
3888
3889 if (r_symndx != 0
3890 && r_type != R_AARCH64_NONE
3891 && r_type != R_AARCH64_NULL
3892 && (h == NULL
3893 || h->root.type == bfd_link_hash_defined
3894 || h->root.type == bfd_link_hash_defweak)
3895 && IS_AARCH64_TLS_RELOC (bfd_r_type) != (sym_type == STT_TLS))
3896 {
3897 (*_bfd_error_handler)
3898 ((sym_type == STT_TLS
3899 ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
3900 : _("%B(%A+0x%lx): %s used with non-TLS symbol %s")),
3901 input_bfd,
3902 input_section, (long) rel->r_offset, howto->name, name);
3903 }
3904
3905 /* We relax only if we can see that there can be a valid transition
3906 from a reloc type to another.
3907 We call elfNN_aarch64_final_link_relocate unless we're completely
3908 done, i.e., the relaxation produced the final output we want. */
3909
3910 relaxed_bfd_r_type = aarch64_tls_transition (input_bfd, info, r_type,
3911 h, r_symndx);
3912 if (relaxed_bfd_r_type != bfd_r_type)
3913 {
3914 bfd_r_type = relaxed_bfd_r_type;
3915 howto = elfNN_aarch64_howto_from_bfd_reloc (bfd_r_type);
3916 BFD_ASSERT (howto != NULL);
3917 r_type = howto->type;
3918 r = elfNN_aarch64_tls_relax (globals, input_bfd, contents, rel, h);
3919 unresolved_reloc = 0;
3920 }
3921 else
3922 r = bfd_reloc_continue;
3923
3924 /* There may be multiple consecutive relocations for the
3925 same offset. In that case we are supposed to treat the
3926 output of each relocation as the addend for the next. */
3927 if (rel + 1 < relend
3928 && rel->r_offset == rel[1].r_offset
3929 && ELFNN_R_TYPE (rel[1].r_info) != R_AARCH64_NONE
3930 && ELFNN_R_TYPE (rel[1].r_info) != R_AARCH64_NULL)
3931 save_addend = TRUE;
3932 else
3933 save_addend = FALSE;
3934
3935 if (r == bfd_reloc_continue)
3936 r = elfNN_aarch64_final_link_relocate (howto, input_bfd, output_bfd,
3937 input_section, contents, rel,
3938 relocation, info, sec,
3939 h, &unresolved_reloc,
3940 save_addend, &addend);
3941
3942 switch (elfNN_aarch64_bfd_reloc_from_type (r_type))
3943 {
3944 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
3945 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
3946 if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
3947 {
3948 bfd_boolean need_relocs = FALSE;
3949 bfd_byte *loc;
3950 int indx;
3951 bfd_vma off;
3952
3953 off = symbol_got_offset (input_bfd, h, r_symndx);
3954 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3955
3956 need_relocs =
3957 (info->shared || indx != 0) &&
3958 (h == NULL
3959 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3960 || h->root.type != bfd_link_hash_undefweak);
3961
3962 BFD_ASSERT (globals->root.srelgot != NULL);
3963
3964 if (need_relocs)
3965 {
3966 Elf_Internal_Rela rela;
3967 rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLS_DTPMOD));
3968 rela.r_addend = 0;
3969 rela.r_offset = globals->root.sgot->output_section->vma +
3970 globals->root.sgot->output_offset + off;
3971
3972
3973 loc = globals->root.srelgot->contents;
3974 loc += globals->root.srelgot->reloc_count++
3975 * RELOC_SIZE (htab);
3976 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
3977
3978 if (indx == 0)
3979 {
3980 bfd_put_NN (output_bfd,
3981 relocation - dtpoff_base (info),
3982 globals->root.sgot->contents + off
3983 + GOT_ENTRY_SIZE);
3984 }
3985 else
3986 {
3987 /* This TLS symbol is global. We emit a
3988 relocation to fixup the tls offset at load
3989 time. */
3990 rela.r_info =
3991 ELFNN_R_INFO (indx, AARCH64_R (TLS_DTPREL));
3992 rela.r_addend = 0;
3993 rela.r_offset =
3994 (globals->root.sgot->output_section->vma
3995 + globals->root.sgot->output_offset + off
3996 + GOT_ENTRY_SIZE);
3997
3998 loc = globals->root.srelgot->contents;
3999 loc += globals->root.srelgot->reloc_count++
4000 * RELOC_SIZE (globals);
4001 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
4002 bfd_put_NN (output_bfd, (bfd_vma) 0,
4003 globals->root.sgot->contents + off
4004 + GOT_ENTRY_SIZE);
4005 }
4006 }
4007 else
4008 {
4009 bfd_put_NN (output_bfd, (bfd_vma) 1,
4010 globals->root.sgot->contents + off);
4011 bfd_put_NN (output_bfd,
4012 relocation - dtpoff_base (info),
4013 globals->root.sgot->contents + off
4014 + GOT_ENTRY_SIZE);
4015 }
4016
4017 symbol_got_offset_mark (input_bfd, h, r_symndx);
4018 }
4019 break;
4020
4021 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
4022 case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
4023 if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
4024 {
4025 bfd_boolean need_relocs = FALSE;
4026 bfd_byte *loc;
4027 int indx;
4028 bfd_vma off;
4029
4030 off = symbol_got_offset (input_bfd, h, r_symndx);
4031
4032 indx = h && h->dynindx != -1 ? h->dynindx : 0;
4033
4034 need_relocs =
4035 (info->shared || indx != 0) &&
4036 (h == NULL
4037 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4038 || h->root.type != bfd_link_hash_undefweak);
4039
4040 BFD_ASSERT (globals->root.srelgot != NULL);
4041
4042 if (need_relocs)
4043 {
4044 Elf_Internal_Rela rela;
4045
4046 if (indx == 0)
4047 rela.r_addend = relocation - dtpoff_base (info);
4048 else
4049 rela.r_addend = 0;
4050
4051 rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLS_TPREL));
4052 rela.r_offset = globals->root.sgot->output_section->vma +
4053 globals->root.sgot->output_offset + off;
4054
4055 loc = globals->root.srelgot->contents;
4056 loc += globals->root.srelgot->reloc_count++
4057 * RELOC_SIZE (htab);
4058
4059 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
4060
4061 bfd_put_NN (output_bfd, rela.r_addend,
4062 globals->root.sgot->contents + off);
4063 }
4064 else
4065 bfd_put_NN (output_bfd, relocation - tpoff_base (info),
4066 globals->root.sgot->contents + off);
4067
4068 symbol_got_offset_mark (input_bfd, h, r_symndx);
4069 }
4070 break;
4071
4072 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
4073 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
4074 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
4075 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
4076 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
4077 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
4078 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
4079 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
4080 break;
4081
4082 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
4083 case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
4084 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
4085 if (! symbol_tlsdesc_got_offset_mark_p (input_bfd, h, r_symndx))
4086 {
4087 bfd_boolean need_relocs = FALSE;
4088 int indx = h && h->dynindx != -1 ? h->dynindx : 0;
4089 bfd_vma off = symbol_tlsdesc_got_offset (input_bfd, h, r_symndx);
4090
4091 need_relocs = (h == NULL
4092 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4093 || h->root.type != bfd_link_hash_undefweak);
4094
4095 BFD_ASSERT (globals->root.srelgot != NULL);
4096 BFD_ASSERT (globals->root.sgot != NULL);
4097
4098 if (need_relocs)
4099 {
4100 bfd_byte *loc;
4101 Elf_Internal_Rela rela;
4102 rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLSDESC));
4103
4104 rela.r_addend = 0;
4105 rela.r_offset = (globals->root.sgotplt->output_section->vma
4106 + globals->root.sgotplt->output_offset
4107 + off + globals->sgotplt_jump_table_size);
4108
4109 if (indx == 0)
4110 rela.r_addend = relocation - dtpoff_base (info);
4111
4112 /* Allocate the next available slot in the PLT reloc
4113 section to hold our R_AARCH64_TLSDESC, the next
4114 available slot is determined from reloc_count,
4115 which we step. But note, reloc_count was
4116 artifically moved down while allocating slots for
4117 real PLT relocs such that all of the PLT relocs
4118 will fit above the initial reloc_count and the
4119 extra stuff will fit below. */
4120 loc = globals->root.srelplt->contents;
4121 loc += globals->root.srelplt->reloc_count++
4122 * RELOC_SIZE (globals);
4123
4124 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
4125
4126 bfd_put_NN (output_bfd, (bfd_vma) 0,
4127 globals->root.sgotplt->contents + off +
4128 globals->sgotplt_jump_table_size);
4129 bfd_put_NN (output_bfd, (bfd_vma) 0,
4130 globals->root.sgotplt->contents + off +
4131 globals->sgotplt_jump_table_size +
4132 GOT_ENTRY_SIZE);
4133 }
4134
4135 symbol_tlsdesc_got_offset_mark (input_bfd, h, r_symndx);
4136 }
4137 break;
4138 default:
4139 break;
4140 }
4141
4142 if (!save_addend)
4143 addend = 0;
4144
4145
4146 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
4147 because such sections are not SEC_ALLOC and thus ld.so will
4148 not process them. */
4149 if (unresolved_reloc
4150 && !((input_section->flags & SEC_DEBUGGING) != 0
4151 && h->def_dynamic)
4152 && _bfd_elf_section_offset (output_bfd, info, input_section,
4153 +rel->r_offset) != (bfd_vma) - 1)
4154 {
4155 (*_bfd_error_handler)
4156 (_
4157 ("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
4158 input_bfd, input_section, (long) rel->r_offset, howto->name,
4159 h->root.root.string);
4160 return FALSE;
4161 }
4162
4163 if (r != bfd_reloc_ok && r != bfd_reloc_continue)
4164 {
4165 switch (r)
4166 {
4167 case bfd_reloc_overflow:
4168 /* If the overflowing reloc was to an undefined symbol,
4169 we have already printed one error message and there
4170 is no point complaining again. */
4171 if ((!h ||
4172 h->root.type != bfd_link_hash_undefined)
4173 && (!((*info->callbacks->reloc_overflow)
4174 (info, (h ? &h->root : NULL), name, howto->name,
4175 (bfd_vma) 0, input_bfd, input_section,
4176 rel->r_offset))))
4177 return FALSE;
4178 break;
4179
4180 case bfd_reloc_undefined:
4181 if (!((*info->callbacks->undefined_symbol)
4182 (info, name, input_bfd, input_section,
4183 rel->r_offset, TRUE)))
4184 return FALSE;
4185 break;
4186
4187 case bfd_reloc_outofrange:
4188 error_message = _("out of range");
4189 goto common_error;
4190
4191 case bfd_reloc_notsupported:
4192 error_message = _("unsupported relocation");
4193 goto common_error;
4194
4195 case bfd_reloc_dangerous:
4196 /* error_message should already be set. */
4197 goto common_error;
4198
4199 default:
4200 error_message = _("unknown error");
4201 /* Fall through. */
4202
4203 common_error:
4204 BFD_ASSERT (error_message != NULL);
4205 if (!((*info->callbacks->reloc_dangerous)
4206 (info, error_message, input_bfd, input_section,
4207 rel->r_offset)))
4208 return FALSE;
4209 break;
4210 }
4211 }
4212 }
4213
4214 return TRUE;
4215 }
4216
4217 /* Set the right machine number. */
4218
4219 static bfd_boolean
4220 elfNN_aarch64_object_p (bfd *abfd)
4221 {
4222 #if ARCH_SIZE == 32
4223 bfd_default_set_arch_mach (abfd, bfd_arch_aarch64, bfd_mach_aarch64_ilp32);
4224 #else
4225 bfd_default_set_arch_mach (abfd, bfd_arch_aarch64, bfd_mach_aarch64);
4226 #endif
4227 return TRUE;
4228 }
4229
4230 /* Function to keep AArch64 specific flags in the ELF header. */
4231
4232 static bfd_boolean
4233 elfNN_aarch64_set_private_flags (bfd *abfd, flagword flags)
4234 {
4235 if (elf_flags_init (abfd) && elf_elfheader (abfd)->e_flags != flags)
4236 {
4237 }
4238 else
4239 {
4240 elf_elfheader (abfd)->e_flags = flags;
4241 elf_flags_init (abfd) = TRUE;
4242 }
4243
4244 return TRUE;
4245 }
4246
4247 /* Copy backend specific data from one object module to another. */
4248
4249 static bfd_boolean
4250 elfNN_aarch64_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
4251 {
4252 flagword in_flags;
4253
4254 if (!is_aarch64_elf (ibfd) || !is_aarch64_elf (obfd))
4255 return TRUE;
4256
4257 in_flags = elf_elfheader (ibfd)->e_flags;
4258
4259 elf_elfheader (obfd)->e_flags = in_flags;
4260 elf_flags_init (obfd) = TRUE;
4261
4262 /* Also copy the EI_OSABI field. */
4263 elf_elfheader (obfd)->e_ident[EI_OSABI] =
4264 elf_elfheader (ibfd)->e_ident[EI_OSABI];
4265
4266 /* Copy object attributes. */
4267 _bfd_elf_copy_obj_attributes (ibfd, obfd);
4268
4269 return TRUE;
4270 }
4271
4272 /* Merge backend specific data from an object file to the output
4273 object file when linking. */
4274
4275 static bfd_boolean
4276 elfNN_aarch64_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
4277 {
4278 flagword out_flags;
4279 flagword in_flags;
4280 bfd_boolean flags_compatible = TRUE;
4281 asection *sec;
4282
4283 /* Check if we have the same endianess. */
4284 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
4285 return FALSE;
4286
4287 if (!is_aarch64_elf (ibfd) || !is_aarch64_elf (obfd))
4288 return TRUE;
4289
4290 /* The input BFD must have had its flags initialised. */
4291 /* The following seems bogus to me -- The flags are initialized in
4292 the assembler but I don't think an elf_flags_init field is
4293 written into the object. */
4294 /* BFD_ASSERT (elf_flags_init (ibfd)); */
4295
4296 in_flags = elf_elfheader (ibfd)->e_flags;
4297 out_flags = elf_elfheader (obfd)->e_flags;
4298
4299 if (!elf_flags_init (obfd))
4300 {
4301 /* If the input is the default architecture and had the default
4302 flags then do not bother setting the flags for the output
4303 architecture, instead allow future merges to do this. If no
4304 future merges ever set these flags then they will retain their
4305 uninitialised values, which surprise surprise, correspond
4306 to the default values. */
4307 if (bfd_get_arch_info (ibfd)->the_default
4308 && elf_elfheader (ibfd)->e_flags == 0)
4309 return TRUE;
4310
4311 elf_flags_init (obfd) = TRUE;
4312 elf_elfheader (obfd)->e_flags = in_flags;
4313
4314 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4315 && bfd_get_arch_info (obfd)->the_default)
4316 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
4317 bfd_get_mach (ibfd));
4318
4319 return TRUE;
4320 }
4321
4322 /* Identical flags must be compatible. */
4323 if (in_flags == out_flags)
4324 return TRUE;
4325
4326 /* Check to see if the input BFD actually contains any sections. If
4327 not, its flags may not have been initialised either, but it
4328 cannot actually cause any incompatiblity. Do not short-circuit
4329 dynamic objects; their section list may be emptied by
4330 elf_link_add_object_symbols.
4331
4332 Also check to see if there are no code sections in the input.
4333 In this case there is no need to check for code specific flags.
4334 XXX - do we need to worry about floating-point format compatability
4335 in data sections ? */
4336 if (!(ibfd->flags & DYNAMIC))
4337 {
4338 bfd_boolean null_input_bfd = TRUE;
4339 bfd_boolean only_data_sections = TRUE;
4340
4341 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
4342 {
4343 if ((bfd_get_section_flags (ibfd, sec)
4344 & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
4345 == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
4346 only_data_sections = FALSE;
4347
4348 null_input_bfd = FALSE;
4349 break;
4350 }
4351
4352 if (null_input_bfd || only_data_sections)
4353 return TRUE;
4354 }
4355
4356 return flags_compatible;
4357 }
4358
4359 /* Display the flags field. */
4360
4361 static bfd_boolean
4362 elfNN_aarch64_print_private_bfd_data (bfd *abfd, void *ptr)
4363 {
4364 FILE *file = (FILE *) ptr;
4365 unsigned long flags;
4366
4367 BFD_ASSERT (abfd != NULL && ptr != NULL);
4368
4369 /* Print normal ELF private data. */
4370 _bfd_elf_print_private_bfd_data (abfd, ptr);
4371
4372 flags = elf_elfheader (abfd)->e_flags;
4373 /* Ignore init flag - it may not be set, despite the flags field
4374 containing valid data. */
4375
4376 /* xgettext:c-format */
4377 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
4378
4379 if (flags)
4380 fprintf (file, _("<Unrecognised flag bits set>"));
4381
4382 fputc ('\n', file);
4383
4384 return TRUE;
4385 }
4386
4387 /* Update the got entry reference counts for the section being removed. */
4388
4389 static bfd_boolean
4390 elfNN_aarch64_gc_sweep_hook (bfd *abfd,
4391 struct bfd_link_info *info,
4392 asection *sec,
4393 const Elf_Internal_Rela * relocs)
4394 {
4395 struct elf_aarch64_link_hash_table *htab;
4396 Elf_Internal_Shdr *symtab_hdr;
4397 struct elf_link_hash_entry **sym_hashes;
4398 struct elf_aarch64_local_symbol *locals;
4399 const Elf_Internal_Rela *rel, *relend;
4400
4401 if (info->relocatable)
4402 return TRUE;
4403
4404 htab = elf_aarch64_hash_table (info);
4405
4406 if (htab == NULL)
4407 return FALSE;
4408
4409 elf_section_data (sec)->local_dynrel = NULL;
4410
4411 symtab_hdr = &elf_symtab_hdr (abfd);
4412 sym_hashes = elf_sym_hashes (abfd);
4413
4414 locals = elf_aarch64_locals (abfd);
4415
4416 relend = relocs + sec->reloc_count;
4417 for (rel = relocs; rel < relend; rel++)
4418 {
4419 unsigned long r_symndx;
4420 unsigned int r_type;
4421 struct elf_link_hash_entry *h = NULL;
4422
4423 r_symndx = ELFNN_R_SYM (rel->r_info);
4424
4425 if (r_symndx >= symtab_hdr->sh_info)
4426 {
4427 struct elf_aarch64_link_hash_entry *eh;
4428 struct elf_dyn_relocs **pp;
4429 struct elf_dyn_relocs *p;
4430
4431 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4432 while (h->root.type == bfd_link_hash_indirect
4433 || h->root.type == bfd_link_hash_warning)
4434 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4435 eh = (struct elf_aarch64_link_hash_entry *) h;
4436
4437 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4438 {
4439 if (p->sec == sec)
4440 {
4441 /* Everything must go for SEC. */
4442 *pp = p->next;
4443 break;
4444 }
4445 }
4446 }
4447 else
4448 {
4449 Elf_Internal_Sym *isym;
4450
4451 /* A local symbol. */
4452 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4453 abfd, r_symndx);
4454 if (isym == NULL)
4455 return FALSE;
4456 }
4457
4458 r_type = ELFNN_R_TYPE (rel->r_info);
4459 switch (aarch64_tls_transition (abfd,info, r_type, h ,r_symndx))
4460 {
4461 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
4462 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
4463 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
4464 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
4465 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
4466 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
4467 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
4468 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
4469 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
4470 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
4471 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
4472 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
4473 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
4474 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
4475 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
4476 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
4477 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
4478 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
4479 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
4480 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
4481 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
4482 if (h != NULL)
4483 {
4484 if (h->got.refcount > 0)
4485 h->got.refcount -= 1;
4486 }
4487 else if (locals != NULL)
4488 {
4489 if (locals[r_symndx].got_refcount > 0)
4490 locals[r_symndx].got_refcount -= 1;
4491 }
4492 break;
4493
4494 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
4495 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
4496 case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
4497 if (h != NULL && info->executable)
4498 {
4499 if (h->plt.refcount > 0)
4500 h->plt.refcount -= 1;
4501 }
4502 break;
4503
4504 case BFD_RELOC_AARCH64_CALL26:
4505 case BFD_RELOC_AARCH64_JUMP26:
4506 /* If this is a local symbol then we resolve it
4507 directly without creating a PLT entry. */
4508 if (h == NULL)
4509 continue;
4510
4511 if (h->plt.refcount > 0)
4512 h->plt.refcount -= 1;
4513 break;
4514
4515 case BFD_RELOC_AARCH64_NN:
4516 if (h != NULL && info->executable)
4517 {
4518 if (h->plt.refcount > 0)
4519 h->plt.refcount -= 1;
4520 }
4521 break;
4522
4523 default:
4524 break;
4525 }
4526 }
4527
4528 return TRUE;
4529 }
4530
4531 /* Adjust a symbol defined by a dynamic object and referenced by a
4532 regular object. The current definition is in some section of the
4533 dynamic object, but we're not including those sections. We have to
4534 change the definition to something the rest of the link can
4535 understand. */
4536
4537 static bfd_boolean
4538 elfNN_aarch64_adjust_dynamic_symbol (struct bfd_link_info *info,
4539 struct elf_link_hash_entry *h)
4540 {
4541 struct elf_aarch64_link_hash_table *htab;
4542 asection *s;
4543
4544 /* If this is a function, put it in the procedure linkage table. We
4545 will fill in the contents of the procedure linkage table later,
4546 when we know the address of the .got section. */
4547 if (h->type == STT_FUNC || h->needs_plt)
4548 {
4549 if (h->plt.refcount <= 0
4550 || SYMBOL_CALLS_LOCAL (info, h)
4551 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4552 && h->root.type == bfd_link_hash_undefweak))
4553 {
4554 /* This case can occur if we saw a CALL26 reloc in
4555 an input file, but the symbol wasn't referred to
4556 by a dynamic object or all references were
4557 garbage collected. In which case we can end up
4558 resolving. */
4559 h->plt.offset = (bfd_vma) - 1;
4560 h->needs_plt = 0;
4561 }
4562
4563 return TRUE;
4564 }
4565 else
4566 /* It's possible that we incorrectly decided a .plt reloc was
4567 needed for an R_X86_64_PC32 reloc to a non-function sym in
4568 check_relocs. We can't decide accurately between function and
4569 non-function syms in check-relocs; Objects loaded later in
4570 the link may change h->type. So fix it now. */
4571 h->plt.offset = (bfd_vma) - 1;
4572
4573
4574 /* If this is a weak symbol, and there is a real definition, the
4575 processor independent code will have arranged for us to see the
4576 real definition first, and we can just use the same value. */
4577 if (h->u.weakdef != NULL)
4578 {
4579 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4580 || h->u.weakdef->root.type == bfd_link_hash_defweak);
4581 h->root.u.def.section = h->u.weakdef->root.u.def.section;
4582 h->root.u.def.value = h->u.weakdef->root.u.def.value;
4583 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
4584 h->non_got_ref = h->u.weakdef->non_got_ref;
4585 return TRUE;
4586 }
4587
4588 /* If we are creating a shared library, we must presume that the
4589 only references to the symbol are via the global offset table.
4590 For such cases we need not do anything here; the relocations will
4591 be handled correctly by relocate_section. */
4592 if (info->shared)
4593 return TRUE;
4594
4595 /* If there are no references to this symbol that do not use the
4596 GOT, we don't need to generate a copy reloc. */
4597 if (!h->non_got_ref)
4598 return TRUE;
4599
4600 /* If -z nocopyreloc was given, we won't generate them either. */
4601 if (info->nocopyreloc)
4602 {
4603 h->non_got_ref = 0;
4604 return TRUE;
4605 }
4606
4607 /* We must allocate the symbol in our .dynbss section, which will
4608 become part of the .bss section of the executable. There will be
4609 an entry for this symbol in the .dynsym section. The dynamic
4610 object will contain position independent code, so all references
4611 from the dynamic object to this symbol will go through the global
4612 offset table. The dynamic linker will use the .dynsym entry to
4613 determine the address it must put in the global offset table, so
4614 both the dynamic object and the regular object will refer to the
4615 same memory location for the variable. */
4616
4617 htab = elf_aarch64_hash_table (info);
4618
4619 /* We must generate a R_AARCH64_COPY reloc to tell the dynamic linker
4620 to copy the initial value out of the dynamic object and into the
4621 runtime process image. */
4622 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
4623 {
4624 htab->srelbss->size += RELOC_SIZE (htab);
4625 h->needs_copy = 1;
4626 }
4627
4628 s = htab->sdynbss;
4629
4630 return _bfd_elf_adjust_dynamic_copy (h, s);
4631
4632 }
4633
4634 static bfd_boolean
4635 elfNN_aarch64_allocate_local_symbols (bfd *abfd, unsigned number)
4636 {
4637 struct elf_aarch64_local_symbol *locals;
4638 locals = elf_aarch64_locals (abfd);
4639 if (locals == NULL)
4640 {
4641 locals = (struct elf_aarch64_local_symbol *)
4642 bfd_zalloc (abfd, number * sizeof (struct elf_aarch64_local_symbol));
4643 if (locals == NULL)
4644 return FALSE;
4645 elf_aarch64_locals (abfd) = locals;
4646 }
4647 return TRUE;
4648 }
4649
4650 /* Look through the relocs for a section during the first phase. */
4651
4652 static bfd_boolean
4653 elfNN_aarch64_check_relocs (bfd *abfd, struct bfd_link_info *info,
4654 asection *sec, const Elf_Internal_Rela *relocs)
4655 {
4656 Elf_Internal_Shdr *symtab_hdr;
4657 struct elf_link_hash_entry **sym_hashes;
4658 const Elf_Internal_Rela *rel;
4659 const Elf_Internal_Rela *rel_end;
4660 asection *sreloc;
4661
4662 struct elf_aarch64_link_hash_table *htab;
4663
4664 if (info->relocatable)
4665 return TRUE;
4666
4667 BFD_ASSERT (is_aarch64_elf (abfd));
4668
4669 htab = elf_aarch64_hash_table (info);
4670 sreloc = NULL;
4671
4672 symtab_hdr = &elf_symtab_hdr (abfd);
4673 sym_hashes = elf_sym_hashes (abfd);
4674
4675 rel_end = relocs + sec->reloc_count;
4676 for (rel = relocs; rel < rel_end; rel++)
4677 {
4678 struct elf_link_hash_entry *h;
4679 unsigned long r_symndx;
4680 unsigned int r_type;
4681 bfd_reloc_code_real_type bfd_r_type;
4682
4683 r_symndx = ELFNN_R_SYM (rel->r_info);
4684 r_type = ELFNN_R_TYPE (rel->r_info);
4685
4686 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
4687 {
4688 (*_bfd_error_handler) (_("%B: bad symbol index: %d"), abfd,
4689 r_symndx);
4690 return FALSE;
4691 }
4692
4693 if (r_symndx < symtab_hdr->sh_info)
4694 h = NULL;
4695 else
4696 {
4697 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4698 while (h->root.type == bfd_link_hash_indirect
4699 || h->root.type == bfd_link_hash_warning)
4700 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4701
4702 /* PR15323, ref flags aren't set for references in the same
4703 object. */
4704 h->root.non_ir_ref = 1;
4705 }
4706
4707 /* Could be done earlier, if h were already available. */
4708 bfd_r_type = aarch64_tls_transition (abfd, info, r_type, h, r_symndx);
4709
4710 switch (bfd_r_type)
4711 {
4712 case BFD_RELOC_AARCH64_NN:
4713
4714 /* We don't need to handle relocs into sections not going into
4715 the "real" output. */
4716 if ((sec->flags & SEC_ALLOC) == 0)
4717 break;
4718
4719 if (h != NULL)
4720 {
4721 if (!info->shared)
4722 h->non_got_ref = 1;
4723
4724 h->plt.refcount += 1;
4725 h->pointer_equality_needed = 1;
4726 }
4727
4728 /* No need to do anything if we're not creating a shared
4729 object. */
4730 if (! info->shared)
4731 break;
4732
4733 {
4734 struct elf_dyn_relocs *p;
4735 struct elf_dyn_relocs **head;
4736
4737 /* We must copy these reloc types into the output file.
4738 Create a reloc section in dynobj and make room for
4739 this reloc. */
4740 if (sreloc == NULL)
4741 {
4742 if (htab->root.dynobj == NULL)
4743 htab->root.dynobj = abfd;
4744
4745 sreloc = _bfd_elf_make_dynamic_reloc_section
4746 (sec, htab->root.dynobj, 3, abfd, /*rela? */ TRUE);
4747
4748 if (sreloc == NULL)
4749 return FALSE;
4750 }
4751
4752 /* If this is a global symbol, we count the number of
4753 relocations we need for this symbol. */
4754 if (h != NULL)
4755 {
4756 struct elf_aarch64_link_hash_entry *eh;
4757 eh = (struct elf_aarch64_link_hash_entry *) h;
4758 head = &eh->dyn_relocs;
4759 }
4760 else
4761 {
4762 /* Track dynamic relocs needed for local syms too.
4763 We really need local syms available to do this
4764 easily. Oh well. */
4765
4766 asection *s;
4767 void **vpp;
4768 Elf_Internal_Sym *isym;
4769
4770 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4771 abfd, r_symndx);
4772 if (isym == NULL)
4773 return FALSE;
4774
4775 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
4776 if (s == NULL)
4777 s = sec;
4778
4779 /* Beware of type punned pointers vs strict aliasing
4780 rules. */
4781 vpp = &(elf_section_data (s)->local_dynrel);
4782 head = (struct elf_dyn_relocs **) vpp;
4783 }
4784
4785 p = *head;
4786 if (p == NULL || p->sec != sec)
4787 {
4788 bfd_size_type amt = sizeof *p;
4789 p = ((struct elf_dyn_relocs *)
4790 bfd_zalloc (htab->root.dynobj, amt));
4791 if (p == NULL)
4792 return FALSE;
4793 p->next = *head;
4794 *head = p;
4795 p->sec = sec;
4796 }
4797
4798 p->count += 1;
4799
4800 }
4801 break;
4802
4803 /* RR: We probably want to keep a consistency check that
4804 there are no dangling GOT_PAGE relocs. */
4805 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
4806 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
4807 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
4808 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
4809 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
4810 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
4811 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
4812 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
4813 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
4814 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
4815 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
4816 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
4817 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
4818 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
4819 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
4820 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
4821 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
4822 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
4823 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
4824 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
4825 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
4826 {
4827 unsigned got_type;
4828 unsigned old_got_type;
4829
4830 got_type = aarch64_reloc_got_type (bfd_r_type);
4831
4832 if (h)
4833 {
4834 h->got.refcount += 1;
4835 old_got_type = elf_aarch64_hash_entry (h)->got_type;
4836 }
4837 else
4838 {
4839 struct elf_aarch64_local_symbol *locals;
4840
4841 if (!elfNN_aarch64_allocate_local_symbols
4842 (abfd, symtab_hdr->sh_info))
4843 return FALSE;
4844
4845 locals = elf_aarch64_locals (abfd);
4846 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
4847 locals[r_symndx].got_refcount += 1;
4848 old_got_type = locals[r_symndx].got_type;
4849 }
4850
4851 /* If a variable is accessed with both general dynamic TLS
4852 methods, two slots may be created. */
4853 if (GOT_TLS_GD_ANY_P (old_got_type) && GOT_TLS_GD_ANY_P (got_type))
4854 got_type |= old_got_type;
4855
4856 /* We will already have issued an error message if there
4857 is a TLS/non-TLS mismatch, based on the symbol type.
4858 So just combine any TLS types needed. */
4859 if (old_got_type != GOT_UNKNOWN && old_got_type != GOT_NORMAL
4860 && got_type != GOT_NORMAL)
4861 got_type |= old_got_type;
4862
4863 /* If the symbol is accessed by both IE and GD methods, we
4864 are able to relax. Turn off the GD flag, without
4865 messing up with any other kind of TLS types that may be
4866 involved. */
4867 if ((got_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (got_type))
4868 got_type &= ~ (GOT_TLSDESC_GD | GOT_TLS_GD);
4869
4870 if (old_got_type != got_type)
4871 {
4872 if (h != NULL)
4873 elf_aarch64_hash_entry (h)->got_type = got_type;
4874 else
4875 {
4876 struct elf_aarch64_local_symbol *locals;
4877 locals = elf_aarch64_locals (abfd);
4878 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
4879 locals[r_symndx].got_type = got_type;
4880 }
4881 }
4882
4883 if (htab->root.sgot == NULL)
4884 {
4885 if (htab->root.dynobj == NULL)
4886 htab->root.dynobj = abfd;
4887 if (!_bfd_elf_create_got_section (htab->root.dynobj, info))
4888 return FALSE;
4889 htab->root.sgot->size += GOT_ENTRY_SIZE;
4890 }
4891 break;
4892 }
4893
4894 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
4895 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
4896 case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
4897 if (h != NULL && info->executable)
4898 {
4899 /* If this reloc is in a read-only section, we might
4900 need a copy reloc. We can't check reliably at this
4901 stage whether the section is read-only, as input
4902 sections have not yet been mapped to output sections.
4903 Tentatively set the flag for now, and correct in
4904 adjust_dynamic_symbol. */
4905 h->non_got_ref = 1;
4906 h->plt.refcount += 1;
4907 h->pointer_equality_needed = 1;
4908 }
4909 /* FIXME:: RR need to handle these in shared libraries
4910 and essentially bomb out as these being non-PIC
4911 relocations in shared libraries. */
4912 break;
4913
4914 case BFD_RELOC_AARCH64_CALL26:
4915 case BFD_RELOC_AARCH64_JUMP26:
4916 /* If this is a local symbol then we resolve it
4917 directly without creating a PLT entry. */
4918 if (h == NULL)
4919 continue;
4920
4921 h->needs_plt = 1;
4922 h->plt.refcount += 1;
4923 break;
4924
4925 default:
4926 break;
4927 }
4928 }
4929
4930 return TRUE;
4931 }
4932
4933 /* Treat mapping symbols as special target symbols. */
4934
4935 static bfd_boolean
4936 elfNN_aarch64_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED,
4937 asymbol *sym)
4938 {
4939 return bfd_is_aarch64_special_symbol_name (sym->name,
4940 BFD_AARCH64_SPECIAL_SYM_TYPE_ANY);
4941 }
4942
4943 /* This is a copy of elf_find_function () from elf.c except that
4944 AArch64 mapping symbols are ignored when looking for function names. */
4945
4946 static bfd_boolean
4947 aarch64_elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
4948 asection *section,
4949 asymbol **symbols,
4950 bfd_vma offset,
4951 const char **filename_ptr,
4952 const char **functionname_ptr)
4953 {
4954 const char *filename = NULL;
4955 asymbol *func = NULL;
4956 bfd_vma low_func = 0;
4957 asymbol **p;
4958
4959 for (p = symbols; *p != NULL; p++)
4960 {
4961 elf_symbol_type *q;
4962
4963 q = (elf_symbol_type *) * p;
4964
4965 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
4966 {
4967 default:
4968 break;
4969 case STT_FILE:
4970 filename = bfd_asymbol_name (&q->symbol);
4971 break;
4972 case STT_FUNC:
4973 case STT_NOTYPE:
4974 /* Skip mapping symbols. */
4975 if ((q->symbol.flags & BSF_LOCAL)
4976 && (bfd_is_aarch64_special_symbol_name
4977 (q->symbol.name, BFD_AARCH64_SPECIAL_SYM_TYPE_ANY)))
4978 continue;
4979 /* Fall through. */
4980 if (bfd_get_section (&q->symbol) == section
4981 && q->symbol.value >= low_func && q->symbol.value <= offset)
4982 {
4983 func = (asymbol *) q;
4984 low_func = q->symbol.value;
4985 }
4986 break;
4987 }
4988 }
4989
4990 if (func == NULL)
4991 return FALSE;
4992
4993 if (filename_ptr)
4994 *filename_ptr = filename;
4995 if (functionname_ptr)
4996 *functionname_ptr = bfd_asymbol_name (func);
4997
4998 return TRUE;
4999 }
5000
5001
5002 /* Find the nearest line to a particular section and offset, for error
5003 reporting. This code is a duplicate of the code in elf.c, except
5004 that it uses aarch64_elf_find_function. */
5005
5006 static bfd_boolean
5007 elfNN_aarch64_find_nearest_line (bfd *abfd,
5008 asection *section,
5009 asymbol **symbols,
5010 bfd_vma offset,
5011 const char **filename_ptr,
5012 const char **functionname_ptr,
5013 unsigned int *line_ptr)
5014 {
5015 bfd_boolean found = FALSE;
5016
5017 /* We skip _bfd_dwarf1_find_nearest_line since no known AArch64
5018 toolchain uses it. */
5019
5020 if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections,
5021 section, symbols, offset,
5022 filename_ptr, functionname_ptr,
5023 line_ptr, NULL, 0,
5024 &elf_tdata (abfd)->dwarf2_find_line_info))
5025 {
5026 if (!*functionname_ptr)
5027 aarch64_elf_find_function (abfd, section, symbols, offset,
5028 *filename_ptr ? NULL : filename_ptr,
5029 functionname_ptr);
5030
5031 return TRUE;
5032 }
5033
5034 if (!_bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
5035 &found, filename_ptr,
5036 functionname_ptr, line_ptr,
5037 &elf_tdata (abfd)->line_info))
5038 return FALSE;
5039
5040 if (found && (*functionname_ptr || *line_ptr))
5041 return TRUE;
5042
5043 if (symbols == NULL)
5044 return FALSE;
5045
5046 if (!aarch64_elf_find_function (abfd, section, symbols, offset,
5047 filename_ptr, functionname_ptr))
5048 return FALSE;
5049
5050 *line_ptr = 0;
5051 return TRUE;
5052 }
5053
5054 static bfd_boolean
5055 elfNN_aarch64_find_inliner_info (bfd *abfd,
5056 const char **filename_ptr,
5057 const char **functionname_ptr,
5058 unsigned int *line_ptr)
5059 {
5060 bfd_boolean found;
5061 found = _bfd_dwarf2_find_inliner_info
5062 (abfd, filename_ptr,
5063 functionname_ptr, line_ptr, &elf_tdata (abfd)->dwarf2_find_line_info);
5064 return found;
5065 }
5066
5067
5068 static void
5069 elfNN_aarch64_post_process_headers (bfd *abfd,
5070 struct bfd_link_info *link_info
5071 ATTRIBUTE_UNUSED)
5072 {
5073 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
5074
5075 i_ehdrp = elf_elfheader (abfd);
5076 i_ehdrp->e_ident[EI_OSABI] = 0;
5077 i_ehdrp->e_ident[EI_ABIVERSION] = AARCH64_ELF_ABI_VERSION;
5078 }
5079
5080 static enum elf_reloc_type_class
5081 elfNN_aarch64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
5082 const asection *rel_sec ATTRIBUTE_UNUSED,
5083 const Elf_Internal_Rela *rela)
5084 {
5085 switch ((int) ELFNN_R_TYPE (rela->r_info))
5086 {
5087 case AARCH64_R (RELATIVE):
5088 return reloc_class_relative;
5089 case AARCH64_R (JUMP_SLOT):
5090 return reloc_class_plt;
5091 case AARCH64_R (COPY):
5092 return reloc_class_copy;
5093 default:
5094 return reloc_class_normal;
5095 }
5096 }
5097
5098 /* Set the right machine number for an AArch64 ELF file. */
5099
5100 static bfd_boolean
5101 elfNN_aarch64_section_flags (flagword *flags, const Elf_Internal_Shdr *hdr)
5102 {
5103 if (hdr->sh_type == SHT_NOTE)
5104 *flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_CONTENTS;
5105
5106 return TRUE;
5107 }
5108
5109 /* Handle an AArch64 specific section when reading an object file. This is
5110 called when bfd_section_from_shdr finds a section with an unknown
5111 type. */
5112
5113 static bfd_boolean
5114 elfNN_aarch64_section_from_shdr (bfd *abfd,
5115 Elf_Internal_Shdr *hdr,
5116 const char *name, int shindex)
5117 {
5118 /* There ought to be a place to keep ELF backend specific flags, but
5119 at the moment there isn't one. We just keep track of the
5120 sections by their name, instead. Fortunately, the ABI gives
5121 names for all the AArch64 specific sections, so we will probably get
5122 away with this. */
5123 switch (hdr->sh_type)
5124 {
5125 case SHT_AARCH64_ATTRIBUTES:
5126 break;
5127
5128 default:
5129 return FALSE;
5130 }
5131
5132 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
5133 return FALSE;
5134
5135 return TRUE;
5136 }
5137
5138 /* A structure used to record a list of sections, independently
5139 of the next and prev fields in the asection structure. */
5140 typedef struct section_list
5141 {
5142 asection *sec;
5143 struct section_list *next;
5144 struct section_list *prev;
5145 }
5146 section_list;
5147
5148 /* Unfortunately we need to keep a list of sections for which
5149 an _aarch64_elf_section_data structure has been allocated. This
5150 is because it is possible for functions like elfNN_aarch64_write_section
5151 to be called on a section which has had an elf_data_structure
5152 allocated for it (and so the used_by_bfd field is valid) but
5153 for which the AArch64 extended version of this structure - the
5154 _aarch64_elf_section_data structure - has not been allocated. */
5155 static section_list *sections_with_aarch64_elf_section_data = NULL;
5156
5157 static void
5158 record_section_with_aarch64_elf_section_data (asection *sec)
5159 {
5160 struct section_list *entry;
5161
5162 entry = bfd_malloc (sizeof (*entry));
5163 if (entry == NULL)
5164 return;
5165 entry->sec = sec;
5166 entry->next = sections_with_aarch64_elf_section_data;
5167 entry->prev = NULL;
5168 if (entry->next != NULL)
5169 entry->next->prev = entry;
5170 sections_with_aarch64_elf_section_data = entry;
5171 }
5172
5173 static struct section_list *
5174 find_aarch64_elf_section_entry (asection *sec)
5175 {
5176 struct section_list *entry;
5177 static struct section_list *last_entry = NULL;
5178
5179 /* This is a short cut for the typical case where the sections are added
5180 to the sections_with_aarch64_elf_section_data list in forward order and
5181 then looked up here in backwards order. This makes a real difference
5182 to the ld-srec/sec64k.exp linker test. */
5183 entry = sections_with_aarch64_elf_section_data;
5184 if (last_entry != NULL)
5185 {
5186 if (last_entry->sec == sec)
5187 entry = last_entry;
5188 else if (last_entry->next != NULL && last_entry->next->sec == sec)
5189 entry = last_entry->next;
5190 }
5191
5192 for (; entry; entry = entry->next)
5193 if (entry->sec == sec)
5194 break;
5195
5196 if (entry)
5197 /* Record the entry prior to this one - it is the entry we are
5198 most likely to want to locate next time. Also this way if we
5199 have been called from
5200 unrecord_section_with_aarch64_elf_section_data () we will not
5201 be caching a pointer that is about to be freed. */
5202 last_entry = entry->prev;
5203
5204 return entry;
5205 }
5206
5207 static void
5208 unrecord_section_with_aarch64_elf_section_data (asection *sec)
5209 {
5210 struct section_list *entry;
5211
5212 entry = find_aarch64_elf_section_entry (sec);
5213
5214 if (entry)
5215 {
5216 if (entry->prev != NULL)
5217 entry->prev->next = entry->next;
5218 if (entry->next != NULL)
5219 entry->next->prev = entry->prev;
5220 if (entry == sections_with_aarch64_elf_section_data)
5221 sections_with_aarch64_elf_section_data = entry->next;
5222 free (entry);
5223 }
5224 }
5225
5226
5227 typedef struct
5228 {
5229 void *finfo;
5230 struct bfd_link_info *info;
5231 asection *sec;
5232 int sec_shndx;
5233 int (*func) (void *, const char *, Elf_Internal_Sym *,
5234 asection *, struct elf_link_hash_entry *);
5235 } output_arch_syminfo;
5236
5237 enum map_symbol_type
5238 {
5239 AARCH64_MAP_INSN,
5240 AARCH64_MAP_DATA
5241 };
5242
5243
5244 /* Output a single mapping symbol. */
5245
5246 static bfd_boolean
5247 elfNN_aarch64_output_map_sym (output_arch_syminfo *osi,
5248 enum map_symbol_type type, bfd_vma offset)
5249 {
5250 static const char *names[2] = { "$x", "$d" };
5251 Elf_Internal_Sym sym;
5252
5253 sym.st_value = (osi->sec->output_section->vma
5254 + osi->sec->output_offset + offset);
5255 sym.st_size = 0;
5256 sym.st_other = 0;
5257 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
5258 sym.st_shndx = osi->sec_shndx;
5259 return osi->func (osi->finfo, names[type], &sym, osi->sec, NULL) == 1;
5260 }
5261
5262
5263
5264 /* Output mapping symbols for PLT entries associated with H. */
5265
5266 static bfd_boolean
5267 elfNN_aarch64_output_plt_map (struct elf_link_hash_entry *h, void *inf)
5268 {
5269 output_arch_syminfo *osi = (output_arch_syminfo *) inf;
5270 bfd_vma addr;
5271
5272 if (h->root.type == bfd_link_hash_indirect)
5273 return TRUE;
5274
5275 if (h->root.type == bfd_link_hash_warning)
5276 /* When warning symbols are created, they **replace** the "real"
5277 entry in the hash table, thus we never get to see the real
5278 symbol in a hash traversal. So look at it now. */
5279 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5280
5281 if (h->plt.offset == (bfd_vma) - 1)
5282 return TRUE;
5283
5284 addr = h->plt.offset;
5285 if (addr == 32)
5286 {
5287 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
5288 return FALSE;
5289 }
5290 return TRUE;
5291 }
5292
5293
5294 /* Output a single local symbol for a generated stub. */
5295
5296 static bfd_boolean
5297 elfNN_aarch64_output_stub_sym (output_arch_syminfo *osi, const char *name,
5298 bfd_vma offset, bfd_vma size)
5299 {
5300 Elf_Internal_Sym sym;
5301
5302 sym.st_value = (osi->sec->output_section->vma
5303 + osi->sec->output_offset + offset);
5304 sym.st_size = size;
5305 sym.st_other = 0;
5306 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
5307 sym.st_shndx = osi->sec_shndx;
5308 return osi->func (osi->finfo, name, &sym, osi->sec, NULL) == 1;
5309 }
5310
5311 static bfd_boolean
5312 aarch64_map_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5313 {
5314 struct elf_aarch64_stub_hash_entry *stub_entry;
5315 asection *stub_sec;
5316 bfd_vma addr;
5317 char *stub_name;
5318 output_arch_syminfo *osi;
5319
5320 /* Massage our args to the form they really have. */
5321 stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
5322 osi = (output_arch_syminfo *) in_arg;
5323
5324 stub_sec = stub_entry->stub_sec;
5325
5326 /* Ensure this stub is attached to the current section being
5327 processed. */
5328 if (stub_sec != osi->sec)
5329 return TRUE;
5330
5331 addr = (bfd_vma) stub_entry->stub_offset;
5332
5333 stub_name = stub_entry->output_name;
5334
5335 switch (stub_entry->stub_type)
5336 {
5337 case aarch64_stub_adrp_branch:
5338 if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
5339 sizeof (aarch64_adrp_branch_stub)))
5340 return FALSE;
5341 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
5342 return FALSE;
5343 break;
5344 case aarch64_stub_long_branch:
5345 if (!elfNN_aarch64_output_stub_sym
5346 (osi, stub_name, addr, sizeof (aarch64_long_branch_stub)))
5347 return FALSE;
5348 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
5349 return FALSE;
5350 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_DATA, addr + 16))
5351 return FALSE;
5352 break;
5353 default:
5354 BFD_FAIL ();
5355 }
5356
5357 return TRUE;
5358 }
5359
5360 /* Output mapping symbols for linker generated sections. */
5361
5362 static bfd_boolean
5363 elfNN_aarch64_output_arch_local_syms (bfd *output_bfd,
5364 struct bfd_link_info *info,
5365 void *finfo,
5366 int (*func) (void *, const char *,
5367 Elf_Internal_Sym *,
5368 asection *,
5369 struct elf_link_hash_entry
5370 *))
5371 {
5372 output_arch_syminfo osi;
5373 struct elf_aarch64_link_hash_table *htab;
5374
5375 htab = elf_aarch64_hash_table (info);
5376
5377 osi.finfo = finfo;
5378 osi.info = info;
5379 osi.func = func;
5380
5381 /* Long calls stubs. */
5382 if (htab->stub_bfd && htab->stub_bfd->sections)
5383 {
5384 asection *stub_sec;
5385
5386 for (stub_sec = htab->stub_bfd->sections;
5387 stub_sec != NULL; stub_sec = stub_sec->next)
5388 {
5389 /* Ignore non-stub sections. */
5390 if (!strstr (stub_sec->name, STUB_SUFFIX))
5391 continue;
5392
5393 osi.sec = stub_sec;
5394
5395 osi.sec_shndx = _bfd_elf_section_from_bfd_section
5396 (output_bfd, osi.sec->output_section);
5397
5398 bfd_hash_traverse (&htab->stub_hash_table, aarch64_map_one_stub,
5399 &osi);
5400 }
5401 }
5402
5403 /* Finally, output mapping symbols for the PLT. */
5404 if (!htab->root.splt || htab->root.splt->size == 0)
5405 return TRUE;
5406
5407 /* For now live without mapping symbols for the plt. */
5408 osi.sec_shndx = _bfd_elf_section_from_bfd_section
5409 (output_bfd, htab->root.splt->output_section);
5410 osi.sec = htab->root.splt;
5411
5412 elf_link_hash_traverse (&htab->root, elfNN_aarch64_output_plt_map,
5413 (void *) &osi);
5414
5415 return TRUE;
5416
5417 }
5418
5419 /* Allocate target specific section data. */
5420
5421 static bfd_boolean
5422 elfNN_aarch64_new_section_hook (bfd *abfd, asection *sec)
5423 {
5424 if (!sec->used_by_bfd)
5425 {
5426 _aarch64_elf_section_data *sdata;
5427 bfd_size_type amt = sizeof (*sdata);
5428
5429 sdata = bfd_zalloc (abfd, amt);
5430 if (sdata == NULL)
5431 return FALSE;
5432 sec->used_by_bfd = sdata;
5433 }
5434
5435 record_section_with_aarch64_elf_section_data (sec);
5436
5437 return _bfd_elf_new_section_hook (abfd, sec);
5438 }
5439
5440
5441 static void
5442 unrecord_section_via_map_over_sections (bfd *abfd ATTRIBUTE_UNUSED,
5443 asection *sec,
5444 void *ignore ATTRIBUTE_UNUSED)
5445 {
5446 unrecord_section_with_aarch64_elf_section_data (sec);
5447 }
5448
5449 static bfd_boolean
5450 elfNN_aarch64_close_and_cleanup (bfd *abfd)
5451 {
5452 if (abfd->sections)
5453 bfd_map_over_sections (abfd,
5454 unrecord_section_via_map_over_sections, NULL);
5455
5456 return _bfd_elf_close_and_cleanup (abfd);
5457 }
5458
5459 static bfd_boolean
5460 elfNN_aarch64_bfd_free_cached_info (bfd *abfd)
5461 {
5462 if (abfd->sections)
5463 bfd_map_over_sections (abfd,
5464 unrecord_section_via_map_over_sections, NULL);
5465
5466 return _bfd_free_cached_info (abfd);
5467 }
5468
5469 static bfd_boolean
5470 elfNN_aarch64_is_function_type (unsigned int type)
5471 {
5472 return type == STT_FUNC;
5473 }
5474
5475 /* Create dynamic sections. This is different from the ARM backend in that
5476 the got, plt, gotplt and their relocation sections are all created in the
5477 standard part of the bfd elf backend. */
5478
5479 static bfd_boolean
5480 elfNN_aarch64_create_dynamic_sections (bfd *dynobj,
5481 struct bfd_link_info *info)
5482 {
5483 struct elf_aarch64_link_hash_table *htab;
5484 struct elf_link_hash_entry *h;
5485
5486 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
5487 return FALSE;
5488
5489 htab = elf_aarch64_hash_table (info);
5490 htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
5491 if (!info->shared)
5492 htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss");
5493
5494 if (!htab->sdynbss || (!info->shared && !htab->srelbss))
5495 abort ();
5496
5497 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the
5498 dynobj's .got section. We don't do this in the linker script
5499 because we don't want to define the symbol if we are not creating
5500 a global offset table. */
5501 h = _bfd_elf_define_linkage_sym (dynobj, info,
5502 htab->root.sgot, "_GLOBAL_OFFSET_TABLE_");
5503 elf_hash_table (info)->hgot = h;
5504 if (h == NULL)
5505 return FALSE;
5506
5507 return TRUE;
5508 }
5509
5510
5511 /* Allocate space in .plt, .got and associated reloc sections for
5512 dynamic relocs. */
5513
5514 static bfd_boolean
5515 elfNN_aarch64_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5516 {
5517 struct bfd_link_info *info;
5518 struct elf_aarch64_link_hash_table *htab;
5519 struct elf_aarch64_link_hash_entry *eh;
5520 struct elf_dyn_relocs *p;
5521
5522 /* An example of a bfd_link_hash_indirect symbol is versioned
5523 symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
5524 -> __gxx_personality_v0(bfd_link_hash_defined)
5525
5526 There is no need to process bfd_link_hash_indirect symbols here
5527 because we will also be presented with the concrete instance of
5528 the symbol and elfNN_aarch64_copy_indirect_symbol () will have been
5529 called to copy all relevant data from the generic to the concrete
5530 symbol instance.
5531 */
5532 if (h->root.type == bfd_link_hash_indirect)
5533 return TRUE;
5534
5535 if (h->root.type == bfd_link_hash_warning)
5536 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5537
5538 info = (struct bfd_link_info *) inf;
5539 htab = elf_aarch64_hash_table (info);
5540
5541 if (htab->root.dynamic_sections_created && h->plt.refcount > 0)
5542 {
5543 /* Make sure this symbol is output as a dynamic symbol.
5544 Undefined weak syms won't yet be marked as dynamic. */
5545 if (h->dynindx == -1 && !h->forced_local)
5546 {
5547 if (!bfd_elf_link_record_dynamic_symbol (info, h))
5548 return FALSE;
5549 }
5550
5551 if (info->shared || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
5552 {
5553 asection *s = htab->root.splt;
5554
5555 /* If this is the first .plt entry, make room for the special
5556 first entry. */
5557 if (s->size == 0)
5558 s->size += htab->plt_header_size;
5559
5560 h->plt.offset = s->size;
5561
5562 /* If this symbol is not defined in a regular file, and we are
5563 not generating a shared library, then set the symbol to this
5564 location in the .plt. This is required to make function
5565 pointers compare as equal between the normal executable and
5566 the shared library. */
5567 if (!info->shared && !h->def_regular)
5568 {
5569 h->root.u.def.section = s;
5570 h->root.u.def.value = h->plt.offset;
5571 }
5572
5573 /* Make room for this entry. For now we only create the
5574 small model PLT entries. We later need to find a way
5575 of relaxing into these from the large model PLT entries. */
5576 s->size += PLT_SMALL_ENTRY_SIZE;
5577
5578 /* We also need to make an entry in the .got.plt section, which
5579 will be placed in the .got section by the linker script. */
5580 htab->root.sgotplt->size += GOT_ENTRY_SIZE;
5581
5582 /* We also need to make an entry in the .rela.plt section. */
5583 htab->root.srelplt->size += RELOC_SIZE (htab);
5584
5585 /* We need to ensure that all GOT entries that serve the PLT
5586 are consecutive with the special GOT slots [0] [1] and
5587 [2]. Any addtional relocations, such as
5588 R_AARCH64_TLSDESC, must be placed after the PLT related
5589 entries. We abuse the reloc_count such that during
5590 sizing we adjust reloc_count to indicate the number of
5591 PLT related reserved entries. In subsequent phases when
5592 filling in the contents of the reloc entries, PLT related
5593 entries are placed by computing their PLT index (0
5594 .. reloc_count). While other none PLT relocs are placed
5595 at the slot indicated by reloc_count and reloc_count is
5596 updated. */
5597
5598 htab->root.srelplt->reloc_count++;
5599 }
5600 else
5601 {
5602 h->plt.offset = (bfd_vma) - 1;
5603 h->needs_plt = 0;
5604 }
5605 }
5606 else
5607 {
5608 h->plt.offset = (bfd_vma) - 1;
5609 h->needs_plt = 0;
5610 }
5611
5612 eh = (struct elf_aarch64_link_hash_entry *) h;
5613 eh->tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
5614
5615 if (h->got.refcount > 0)
5616 {
5617 bfd_boolean dyn;
5618 unsigned got_type = elf_aarch64_hash_entry (h)->got_type;
5619
5620 h->got.offset = (bfd_vma) - 1;
5621
5622 dyn = htab->root.dynamic_sections_created;
5623
5624 /* Make sure this symbol is output as a dynamic symbol.
5625 Undefined weak syms won't yet be marked as dynamic. */
5626 if (dyn && h->dynindx == -1 && !h->forced_local)
5627 {
5628 if (!bfd_elf_link_record_dynamic_symbol (info, h))
5629 return FALSE;
5630 }
5631
5632 if (got_type == GOT_UNKNOWN)
5633 {
5634 }
5635 else if (got_type == GOT_NORMAL)
5636 {
5637 h->got.offset = htab->root.sgot->size;
5638 htab->root.sgot->size += GOT_ENTRY_SIZE;
5639 if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5640 || h->root.type != bfd_link_hash_undefweak)
5641 && (info->shared
5642 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
5643 {
5644 htab->root.srelgot->size += RELOC_SIZE (htab);
5645 }
5646 }
5647 else
5648 {
5649 int indx;
5650 if (got_type & GOT_TLSDESC_GD)
5651 {
5652 eh->tlsdesc_got_jump_table_offset =
5653 (htab->root.sgotplt->size
5654 - aarch64_compute_jump_table_size (htab));
5655 htab->root.sgotplt->size += GOT_ENTRY_SIZE * 2;
5656 h->got.offset = (bfd_vma) - 2;
5657 }
5658
5659 if (got_type & GOT_TLS_GD)
5660 {
5661 h->got.offset = htab->root.sgot->size;
5662 htab->root.sgot->size += GOT_ENTRY_SIZE * 2;
5663 }
5664
5665 if (got_type & GOT_TLS_IE)
5666 {
5667 h->got.offset = htab->root.sgot->size;
5668 htab->root.sgot->size += GOT_ENTRY_SIZE;
5669 }
5670
5671 indx = h && h->dynindx != -1 ? h->dynindx : 0;
5672 if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5673 || h->root.type != bfd_link_hash_undefweak)
5674 && (info->shared
5675 || indx != 0
5676 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
5677 {
5678 if (got_type & GOT_TLSDESC_GD)
5679 {
5680 htab->root.srelplt->size += RELOC_SIZE (htab);
5681 /* Note reloc_count not incremented here! We have
5682 already adjusted reloc_count for this relocation
5683 type. */
5684
5685 /* TLSDESC PLT is now needed, but not yet determined. */
5686 htab->tlsdesc_plt = (bfd_vma) - 1;
5687 }
5688
5689 if (got_type & GOT_TLS_GD)
5690 htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
5691
5692 if (got_type & GOT_TLS_IE)
5693 htab->root.srelgot->size += RELOC_SIZE (htab);
5694 }
5695 }
5696 }
5697 else
5698 {
5699 h->got.offset = (bfd_vma) - 1;
5700 }
5701
5702 if (eh->dyn_relocs == NULL)
5703 return TRUE;
5704
5705 /* In the shared -Bsymbolic case, discard space allocated for
5706 dynamic pc-relative relocs against symbols which turn out to be
5707 defined in regular objects. For the normal shared case, discard
5708 space for pc-relative relocs that have become local due to symbol
5709 visibility changes. */
5710
5711 if (info->shared)
5712 {
5713 /* Relocs that use pc_count are those that appear on a call
5714 insn, or certain REL relocs that can generated via assembly.
5715 We want calls to protected symbols to resolve directly to the
5716 function rather than going via the plt. If people want
5717 function pointer comparisons to work as expected then they
5718 should avoid writing weird assembly. */
5719 if (SYMBOL_CALLS_LOCAL (info, h))
5720 {
5721 struct elf_dyn_relocs **pp;
5722
5723 for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
5724 {
5725 p->count -= p->pc_count;
5726 p->pc_count = 0;
5727 if (p->count == 0)
5728 *pp = p->next;
5729 else
5730 pp = &p->next;
5731 }
5732 }
5733
5734 /* Also discard relocs on undefined weak syms with non-default
5735 visibility. */
5736 if (eh->dyn_relocs != NULL && h->root.type == bfd_link_hash_undefweak)
5737 {
5738 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
5739 eh->dyn_relocs = NULL;
5740
5741 /* Make sure undefined weak symbols are output as a dynamic
5742 symbol in PIEs. */
5743 else if (h->dynindx == -1
5744 && !h->forced_local
5745 && !bfd_elf_link_record_dynamic_symbol (info, h))
5746 return FALSE;
5747 }
5748
5749 }
5750 else if (ELIMINATE_COPY_RELOCS)
5751 {
5752 /* For the non-shared case, discard space for relocs against
5753 symbols which turn out to need copy relocs or are not
5754 dynamic. */
5755
5756 if (!h->non_got_ref
5757 && ((h->def_dynamic
5758 && !h->def_regular)
5759 || (htab->root.dynamic_sections_created
5760 && (h->root.type == bfd_link_hash_undefweak
5761 || h->root.type == bfd_link_hash_undefined))))
5762 {
5763 /* Make sure this symbol is output as a dynamic symbol.
5764 Undefined weak syms won't yet be marked as dynamic. */
5765 if (h->dynindx == -1
5766 && !h->forced_local
5767 && !bfd_elf_link_record_dynamic_symbol (info, h))
5768 return FALSE;
5769
5770 /* If that succeeded, we know we'll be keeping all the
5771 relocs. */
5772 if (h->dynindx != -1)
5773 goto keep;
5774 }
5775
5776 eh->dyn_relocs = NULL;
5777
5778 keep:;
5779 }
5780
5781 /* Finally, allocate space. */
5782 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5783 {
5784 asection *sreloc;
5785
5786 sreloc = elf_section_data (p->sec)->sreloc;
5787
5788 BFD_ASSERT (sreloc != NULL);
5789
5790 sreloc->size += p->count * RELOC_SIZE (htab);
5791 }
5792
5793 return TRUE;
5794 }
5795
5796
5797 /* This is the most important function of all . Innocuosly named
5798 though ! */
5799 static bfd_boolean
5800 elfNN_aarch64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
5801 struct bfd_link_info *info)
5802 {
5803 struct elf_aarch64_link_hash_table *htab;
5804 bfd *dynobj;
5805 asection *s;
5806 bfd_boolean relocs;
5807 bfd *ibfd;
5808
5809 htab = elf_aarch64_hash_table ((info));
5810 dynobj = htab->root.dynobj;
5811
5812 BFD_ASSERT (dynobj != NULL);
5813
5814 if (htab->root.dynamic_sections_created)
5815 {
5816 if (info->executable)
5817 {
5818 s = bfd_get_linker_section (dynobj, ".interp");
5819 if (s == NULL)
5820 abort ();
5821 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5822 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5823 }
5824 }
5825
5826 /* Set up .got offsets for local syms, and space for local dynamic
5827 relocs. */
5828 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5829 {
5830 struct elf_aarch64_local_symbol *locals = NULL;
5831 Elf_Internal_Shdr *symtab_hdr;
5832 asection *srel;
5833 unsigned int i;
5834
5835 if (!is_aarch64_elf (ibfd))
5836 continue;
5837
5838 for (s = ibfd->sections; s != NULL; s = s->next)
5839 {
5840 struct elf_dyn_relocs *p;
5841
5842 for (p = (struct elf_dyn_relocs *)
5843 (elf_section_data (s)->local_dynrel); p != NULL; p = p->next)
5844 {
5845 if (!bfd_is_abs_section (p->sec)
5846 && bfd_is_abs_section (p->sec->output_section))
5847 {
5848 /* Input section has been discarded, either because
5849 it is a copy of a linkonce section or due to
5850 linker script /DISCARD/, so we'll be discarding
5851 the relocs too. */
5852 }
5853 else if (p->count != 0)
5854 {
5855 srel = elf_section_data (p->sec)->sreloc;
5856 srel->size += p->count * RELOC_SIZE (htab);
5857 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
5858 info->flags |= DF_TEXTREL;
5859 }
5860 }
5861 }
5862
5863 locals = elf_aarch64_locals (ibfd);
5864 if (!locals)
5865 continue;
5866
5867 symtab_hdr = &elf_symtab_hdr (ibfd);
5868 srel = htab->root.srelgot;
5869 for (i = 0; i < symtab_hdr->sh_info; i++)
5870 {
5871 locals[i].got_offset = (bfd_vma) - 1;
5872 locals[i].tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
5873 if (locals[i].got_refcount > 0)
5874 {
5875 unsigned got_type = locals[i].got_type;
5876 if (got_type & GOT_TLSDESC_GD)
5877 {
5878 locals[i].tlsdesc_got_jump_table_offset =
5879 (htab->root.sgotplt->size
5880 - aarch64_compute_jump_table_size (htab));
5881 htab->root.sgotplt->size += GOT_ENTRY_SIZE * 2;
5882 locals[i].got_offset = (bfd_vma) - 2;
5883 }
5884
5885 if (got_type & GOT_TLS_GD)
5886 {
5887 locals[i].got_offset = htab->root.sgot->size;
5888 htab->root.sgot->size += GOT_ENTRY_SIZE * 2;
5889 }
5890
5891 if (got_type & GOT_TLS_IE)
5892 {
5893 locals[i].got_offset = htab->root.sgot->size;
5894 htab->root.sgot->size += GOT_ENTRY_SIZE;
5895 }
5896
5897 if (got_type == GOT_UNKNOWN)
5898 {
5899 }
5900
5901 if (got_type == GOT_NORMAL)
5902 {
5903 }
5904
5905 if (info->shared)
5906 {
5907 if (got_type & GOT_TLSDESC_GD)
5908 {
5909 htab->root.srelplt->size += RELOC_SIZE (htab);
5910 /* Note RELOC_COUNT not incremented here! */
5911 htab->tlsdesc_plt = (bfd_vma) - 1;
5912 }
5913
5914 if (got_type & GOT_TLS_GD)
5915 htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
5916
5917 if (got_type & GOT_TLS_IE)
5918 htab->root.srelgot->size += RELOC_SIZE (htab);
5919 }
5920 }
5921 else
5922 {
5923 locals[i].got_refcount = (bfd_vma) - 1;
5924 }
5925 }
5926 }
5927
5928
5929 /* Allocate global sym .plt and .got entries, and space for global
5930 sym dynamic relocs. */
5931 elf_link_hash_traverse (&htab->root, elfNN_aarch64_allocate_dynrelocs,
5932 info);
5933
5934
5935 /* For every jump slot reserved in the sgotplt, reloc_count is
5936 incremented. However, when we reserve space for TLS descriptors,
5937 it's not incremented, so in order to compute the space reserved
5938 for them, it suffices to multiply the reloc count by the jump
5939 slot size. */
5940
5941 if (htab->root.srelplt)
5942 htab->sgotplt_jump_table_size = aarch64_compute_jump_table_size (htab);
5943
5944 if (htab->tlsdesc_plt)
5945 {
5946 if (htab->root.splt->size == 0)
5947 htab->root.splt->size += PLT_ENTRY_SIZE;
5948
5949 htab->tlsdesc_plt = htab->root.splt->size;
5950 htab->root.splt->size += PLT_TLSDESC_ENTRY_SIZE;
5951
5952 /* If we're not using lazy TLS relocations, don't generate the
5953 GOT entry required. */
5954 if (!(info->flags & DF_BIND_NOW))
5955 {
5956 htab->dt_tlsdesc_got = htab->root.sgot->size;
5957 htab->root.sgot->size += GOT_ENTRY_SIZE;
5958 }
5959 }
5960
5961 /* We now have determined the sizes of the various dynamic sections.
5962 Allocate memory for them. */
5963 relocs = FALSE;
5964 for (s = dynobj->sections; s != NULL; s = s->next)
5965 {
5966 if ((s->flags & SEC_LINKER_CREATED) == 0)
5967 continue;
5968
5969 if (s == htab->root.splt
5970 || s == htab->root.sgot
5971 || s == htab->root.sgotplt
5972 || s == htab->root.iplt
5973 || s == htab->root.igotplt || s == htab->sdynbss)
5974 {
5975 /* Strip this section if we don't need it; see the
5976 comment below. */
5977 }
5978 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
5979 {
5980 if (s->size != 0 && s != htab->root.srelplt)
5981 relocs = TRUE;
5982
5983 /* We use the reloc_count field as a counter if we need
5984 to copy relocs into the output file. */
5985 if (s != htab->root.srelplt)
5986 s->reloc_count = 0;
5987 }
5988 else
5989 {
5990 /* It's not one of our sections, so don't allocate space. */
5991 continue;
5992 }
5993
5994 if (s->size == 0)
5995 {
5996 /* If we don't need this section, strip it from the
5997 output file. This is mostly to handle .rela.bss and
5998 .rela.plt. We must create both sections in
5999 create_dynamic_sections, because they must be created
6000 before the linker maps input sections to output
6001 sections. The linker does that before
6002 adjust_dynamic_symbol is called, and it is that
6003 function which decides whether anything needs to go
6004 into these sections. */
6005
6006 s->flags |= SEC_EXCLUDE;
6007 continue;
6008 }
6009
6010 if ((s->flags & SEC_HAS_CONTENTS) == 0)
6011 continue;
6012
6013 /* Allocate memory for the section contents. We use bfd_zalloc
6014 here in case unused entries are not reclaimed before the
6015 section's contents are written out. This should not happen,
6016 but this way if it does, we get a R_AARCH64_NONE reloc instead
6017 of garbage. */
6018 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
6019 if (s->contents == NULL)
6020 return FALSE;
6021 }
6022
6023 if (htab->root.dynamic_sections_created)
6024 {
6025 /* Add some entries to the .dynamic section. We fill in the
6026 values later, in elfNN_aarch64_finish_dynamic_sections, but we
6027 must add the entries now so that we get the correct size for
6028 the .dynamic section. The DT_DEBUG entry is filled in by the
6029 dynamic linker and used by the debugger. */
6030 #define add_dynamic_entry(TAG, VAL) \
6031 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
6032
6033 if (info->executable)
6034 {
6035 if (!add_dynamic_entry (DT_DEBUG, 0))
6036 return FALSE;
6037 }
6038
6039 if (htab->root.splt->size != 0)
6040 {
6041 if (!add_dynamic_entry (DT_PLTGOT, 0)
6042 || !add_dynamic_entry (DT_PLTRELSZ, 0)
6043 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
6044 || !add_dynamic_entry (DT_JMPREL, 0))
6045 return FALSE;
6046
6047 if (htab->tlsdesc_plt
6048 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
6049 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
6050 return FALSE;
6051 }
6052
6053 if (relocs)
6054 {
6055 if (!add_dynamic_entry (DT_RELA, 0)
6056 || !add_dynamic_entry (DT_RELASZ, 0)
6057 || !add_dynamic_entry (DT_RELAENT, RELOC_SIZE (htab)))
6058 return FALSE;
6059
6060 /* If any dynamic relocs apply to a read-only section,
6061 then we need a DT_TEXTREL entry. */
6062 if ((info->flags & DF_TEXTREL) != 0)
6063 {
6064 if (!add_dynamic_entry (DT_TEXTREL, 0))
6065 return FALSE;
6066 }
6067 }
6068 }
6069 #undef add_dynamic_entry
6070
6071 return TRUE;
6072 }
6073
6074 static inline void
6075 elf_aarch64_update_plt_entry (bfd *output_bfd,
6076 bfd_reloc_code_real_type r_type,
6077 bfd_byte *plt_entry, bfd_vma value)
6078 {
6079 reloc_howto_type *howto = elfNN_aarch64_howto_from_bfd_reloc (r_type);
6080
6081 _bfd_aarch64_elf_put_addend (output_bfd, plt_entry, r_type, howto, value);
6082 }
6083
6084 static void
6085 elfNN_aarch64_create_small_pltn_entry (struct elf_link_hash_entry *h,
6086 struct elf_aarch64_link_hash_table
6087 *htab, bfd *output_bfd)
6088 {
6089 bfd_byte *plt_entry;
6090 bfd_vma plt_index;
6091 bfd_vma got_offset;
6092 bfd_vma gotplt_entry_address;
6093 bfd_vma plt_entry_address;
6094 Elf_Internal_Rela rela;
6095 bfd_byte *loc;
6096
6097 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
6098
6099 /* Offset in the GOT is PLT index plus got GOT headers(3)
6100 times GOT_ENTRY_SIZE. */
6101 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
6102 plt_entry = htab->root.splt->contents + h->plt.offset;
6103 plt_entry_address = htab->root.splt->output_section->vma
6104 + htab->root.splt->output_section->output_offset + h->plt.offset;
6105 gotplt_entry_address = htab->root.sgotplt->output_section->vma +
6106 htab->root.sgotplt->output_offset + got_offset;
6107
6108 /* Copy in the boiler-plate for the PLTn entry. */
6109 memcpy (plt_entry, elfNN_aarch64_small_plt_entry, PLT_SMALL_ENTRY_SIZE);
6110
6111 /* Fill in the top 21 bits for this: ADRP x16, PLT_GOT + n * 8.
6112 ADRP: ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
6113 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADR_HI21_PCREL,
6114 plt_entry,
6115 PG (gotplt_entry_address) -
6116 PG (plt_entry_address));
6117
6118 /* Fill in the lo12 bits for the load from the pltgot. */
6119 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_LDSTNN_LO12,
6120 plt_entry + 4,
6121 PG_OFFSET (gotplt_entry_address));
6122
6123 /* Fill in the the lo12 bits for the add from the pltgot entry. */
6124 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADD_LO12,
6125 plt_entry + 8,
6126 PG_OFFSET (gotplt_entry_address));
6127
6128 /* All the GOTPLT Entries are essentially initialized to PLT0. */
6129 bfd_put_NN (output_bfd,
6130 (htab->root.splt->output_section->vma
6131 + htab->root.splt->output_offset),
6132 htab->root.sgotplt->contents + got_offset);
6133
6134 /* Fill in the entry in the .rela.plt section. */
6135 rela.r_offset = gotplt_entry_address;
6136 rela.r_info = ELFNN_R_INFO (h->dynindx, AARCH64_R (JUMP_SLOT));
6137 rela.r_addend = 0;
6138
6139 /* Compute the relocation entry to used based on PLT index and do
6140 not adjust reloc_count. The reloc_count has already been adjusted
6141 to account for this entry. */
6142 loc = htab->root.srelplt->contents + plt_index * RELOC_SIZE (htab);
6143 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
6144 }
6145
6146 /* Size sections even though they're not dynamic. We use it to setup
6147 _TLS_MODULE_BASE_, if needed. */
6148
6149 static bfd_boolean
6150 elfNN_aarch64_always_size_sections (bfd *output_bfd,
6151 struct bfd_link_info *info)
6152 {
6153 asection *tls_sec;
6154
6155 if (info->relocatable)
6156 return TRUE;
6157
6158 tls_sec = elf_hash_table (info)->tls_sec;
6159
6160 if (tls_sec)
6161 {
6162 struct elf_link_hash_entry *tlsbase;
6163
6164 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
6165 "_TLS_MODULE_BASE_", TRUE, TRUE, FALSE);
6166
6167 if (tlsbase)
6168 {
6169 struct bfd_link_hash_entry *h = NULL;
6170 const struct elf_backend_data *bed =
6171 get_elf_backend_data (output_bfd);
6172
6173 if (!(_bfd_generic_link_add_one_symbol
6174 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
6175 tls_sec, 0, NULL, FALSE, bed->collect, &h)))
6176 return FALSE;
6177
6178 tlsbase->type = STT_TLS;
6179 tlsbase = (struct elf_link_hash_entry *) h;
6180 tlsbase->def_regular = 1;
6181 tlsbase->other = STV_HIDDEN;
6182 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
6183 }
6184 }
6185
6186 return TRUE;
6187 }
6188
6189 /* Finish up dynamic symbol handling. We set the contents of various
6190 dynamic sections here. */
6191 static bfd_boolean
6192 elfNN_aarch64_finish_dynamic_symbol (bfd *output_bfd,
6193 struct bfd_link_info *info,
6194 struct elf_link_hash_entry *h,
6195 Elf_Internal_Sym *sym)
6196 {
6197 struct elf_aarch64_link_hash_table *htab;
6198 htab = elf_aarch64_hash_table (info);
6199
6200 if (h->plt.offset != (bfd_vma) - 1)
6201 {
6202 /* This symbol has an entry in the procedure linkage table. Set
6203 it up. */
6204
6205 if (h->dynindx == -1
6206 || htab->root.splt == NULL
6207 || htab->root.sgotplt == NULL || htab->root.srelplt == NULL)
6208 abort ();
6209
6210 elfNN_aarch64_create_small_pltn_entry (h, htab, output_bfd);
6211 if (!h->def_regular)
6212 {
6213 /* Mark the symbol as undefined, rather than as defined in
6214 the .plt section. Leave the value alone. This is a clue
6215 for the dynamic linker, to make function pointer
6216 comparisons work between an application and shared
6217 library. */
6218 sym->st_shndx = SHN_UNDEF;
6219 }
6220 }
6221
6222 if (h->got.offset != (bfd_vma) - 1
6223 && elf_aarch64_hash_entry (h)->got_type == GOT_NORMAL)
6224 {
6225 Elf_Internal_Rela rela;
6226 bfd_byte *loc;
6227
6228 /* This symbol has an entry in the global offset table. Set it
6229 up. */
6230 if (htab->root.sgot == NULL || htab->root.srelgot == NULL)
6231 abort ();
6232
6233 rela.r_offset = (htab->root.sgot->output_section->vma
6234 + htab->root.sgot->output_offset
6235 + (h->got.offset & ~(bfd_vma) 1));
6236
6237 if (info->shared && SYMBOL_REFERENCES_LOCAL (info, h))
6238 {
6239 if (!h->def_regular)
6240 return FALSE;
6241
6242 BFD_ASSERT ((h->got.offset & 1) != 0);
6243 rela.r_info = ELFNN_R_INFO (0, AARCH64_R (RELATIVE));
6244 rela.r_addend = (h->root.u.def.value
6245 + h->root.u.def.section->output_section->vma
6246 + h->root.u.def.section->output_offset);
6247 }
6248 else
6249 {
6250 BFD_ASSERT ((h->got.offset & 1) == 0);
6251 bfd_put_NN (output_bfd, (bfd_vma) 0,
6252 htab->root.sgot->contents + h->got.offset);
6253 rela.r_info = ELFNN_R_INFO (h->dynindx, AARCH64_R (GLOB_DAT));
6254 rela.r_addend = 0;
6255 }
6256
6257 loc = htab->root.srelgot->contents;
6258 loc += htab->root.srelgot->reloc_count++ * RELOC_SIZE (htab);
6259 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
6260 }
6261
6262 if (h->needs_copy)
6263 {
6264 Elf_Internal_Rela rela;
6265 bfd_byte *loc;
6266
6267 /* This symbol needs a copy reloc. Set it up. */
6268
6269 if (h->dynindx == -1
6270 || (h->root.type != bfd_link_hash_defined
6271 && h->root.type != bfd_link_hash_defweak)
6272 || htab->srelbss == NULL)
6273 abort ();
6274
6275 rela.r_offset = (h->root.u.def.value
6276 + h->root.u.def.section->output_section->vma
6277 + h->root.u.def.section->output_offset);
6278 rela.r_info = ELFNN_R_INFO (h->dynindx, AARCH64_R (COPY));
6279 rela.r_addend = 0;
6280 loc = htab->srelbss->contents;
6281 loc += htab->srelbss->reloc_count++ * RELOC_SIZE (htab);
6282 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
6283 }
6284
6285 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. SYM may
6286 be NULL for local symbols. */
6287 if (sym != NULL
6288 && (h == elf_hash_table (info)->hdynamic
6289 || h == elf_hash_table (info)->hgot))
6290 sym->st_shndx = SHN_ABS;
6291
6292 return TRUE;
6293 }
6294
6295 static void
6296 elfNN_aarch64_init_small_plt0_entry (bfd *output_bfd ATTRIBUTE_UNUSED,
6297 struct elf_aarch64_link_hash_table
6298 *htab)
6299 {
6300 /* Fill in PLT0. Fixme:RR Note this doesn't distinguish between
6301 small and large plts and at the minute just generates
6302 the small PLT. */
6303
6304 /* PLT0 of the small PLT looks like this in ELF64 -
6305 stp x16, x30, [sp, #-16]! // Save the reloc and lr on stack.
6306 adrp x16, PLT_GOT + 16 // Get the page base of the GOTPLT
6307 ldr x17, [x16, #:lo12:PLT_GOT+16] // Load the address of the
6308 // symbol resolver
6309 add x16, x16, #:lo12:PLT_GOT+16 // Load the lo12 bits of the
6310 // GOTPLT entry for this.
6311 br x17
6312 PLT0 will be slightly different in ELF32 due to different got entry
6313 size.
6314 */
6315 bfd_vma plt_got_2nd_ent; /* Address of GOT[2]. */
6316 bfd_vma plt_base;
6317
6318
6319 memcpy (htab->root.splt->contents, elfNN_aarch64_small_plt0_entry,
6320 PLT_ENTRY_SIZE);
6321 elf_section_data (htab->root.splt->output_section)->this_hdr.sh_entsize =
6322 PLT_ENTRY_SIZE;
6323
6324 plt_got_2nd_ent = (htab->root.sgotplt->output_section->vma
6325 + htab->root.sgotplt->output_offset
6326 + GOT_ENTRY_SIZE * 2);
6327
6328 plt_base = htab->root.splt->output_section->vma +
6329 htab->root.splt->output_section->output_offset;
6330
6331 /* Fill in the top 21 bits for this: ADRP x16, PLT_GOT + n * 8.
6332 ADRP: ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
6333 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADR_HI21_PCREL,
6334 htab->root.splt->contents + 4,
6335 PG (plt_got_2nd_ent) - PG (plt_base + 4));
6336
6337 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_LDSTNN_LO12,
6338 htab->root.splt->contents + 8,
6339 PG_OFFSET (plt_got_2nd_ent));
6340
6341 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADD_LO12,
6342 htab->root.splt->contents + 12,
6343 PG_OFFSET (plt_got_2nd_ent));
6344 }
6345
6346 static bfd_boolean
6347 elfNN_aarch64_finish_dynamic_sections (bfd *output_bfd,
6348 struct bfd_link_info *info)
6349 {
6350 struct elf_aarch64_link_hash_table *htab;
6351 bfd *dynobj;
6352 asection *sdyn;
6353
6354 htab = elf_aarch64_hash_table (info);
6355 dynobj = htab->root.dynobj;
6356 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
6357
6358 if (htab->root.dynamic_sections_created)
6359 {
6360 ElfNN_External_Dyn *dyncon, *dynconend;
6361
6362 if (sdyn == NULL || htab->root.sgot == NULL)
6363 abort ();
6364
6365 dyncon = (ElfNN_External_Dyn *) sdyn->contents;
6366 dynconend = (ElfNN_External_Dyn *) (sdyn->contents + sdyn->size);
6367 for (; dyncon < dynconend; dyncon++)
6368 {
6369 Elf_Internal_Dyn dyn;
6370 asection *s;
6371
6372 bfd_elfNN_swap_dyn_in (dynobj, dyncon, &dyn);
6373
6374 switch (dyn.d_tag)
6375 {
6376 default:
6377 continue;
6378
6379 case DT_PLTGOT:
6380 s = htab->root.sgotplt;
6381 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6382 break;
6383
6384 case DT_JMPREL:
6385 dyn.d_un.d_ptr = htab->root.srelplt->output_section->vma;
6386 break;
6387
6388 case DT_PLTRELSZ:
6389 s = htab->root.srelplt->output_section;
6390 dyn.d_un.d_val = s->size;
6391 break;
6392
6393 case DT_RELASZ:
6394 /* The procedure linkage table relocs (DT_JMPREL) should
6395 not be included in the overall relocs (DT_RELA).
6396 Therefore, we override the DT_RELASZ entry here to
6397 make it not include the JMPREL relocs. Since the
6398 linker script arranges for .rela.plt to follow all
6399 other relocation sections, we don't have to worry
6400 about changing the DT_RELA entry. */
6401 if (htab->root.srelplt != NULL)
6402 {
6403 s = htab->root.srelplt->output_section;
6404 dyn.d_un.d_val -= s->size;
6405 }
6406 break;
6407
6408 case DT_TLSDESC_PLT:
6409 s = htab->root.splt;
6410 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
6411 + htab->tlsdesc_plt;
6412 break;
6413
6414 case DT_TLSDESC_GOT:
6415 s = htab->root.sgot;
6416 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
6417 + htab->dt_tlsdesc_got;
6418 break;
6419 }
6420
6421 bfd_elfNN_swap_dyn_out (output_bfd, &dyn, dyncon);
6422 }
6423
6424 }
6425
6426 /* Fill in the special first entry in the procedure linkage table. */
6427 if (htab->root.splt && htab->root.splt->size > 0)
6428 {
6429 elfNN_aarch64_init_small_plt0_entry (output_bfd, htab);
6430
6431 elf_section_data (htab->root.splt->output_section)->
6432 this_hdr.sh_entsize = htab->plt_entry_size;
6433
6434
6435 if (htab->tlsdesc_plt)
6436 {
6437 bfd_put_NN (output_bfd, (bfd_vma) 0,
6438 htab->root.sgot->contents + htab->dt_tlsdesc_got);
6439
6440 memcpy (htab->root.splt->contents + htab->tlsdesc_plt,
6441 elfNN_aarch64_tlsdesc_small_plt_entry,
6442 sizeof (elfNN_aarch64_tlsdesc_small_plt_entry));
6443
6444 {
6445 bfd_vma adrp1_addr =
6446 htab->root.splt->output_section->vma
6447 + htab->root.splt->output_offset + htab->tlsdesc_plt + 4;
6448
6449 bfd_vma adrp2_addr = adrp1_addr + 4;
6450
6451 bfd_vma got_addr =
6452 htab->root.sgot->output_section->vma
6453 + htab->root.sgot->output_offset;
6454
6455 bfd_vma pltgot_addr =
6456 htab->root.sgotplt->output_section->vma
6457 + htab->root.sgotplt->output_offset;
6458
6459 bfd_vma dt_tlsdesc_got = got_addr + htab->dt_tlsdesc_got;
6460
6461 bfd_byte *plt_entry =
6462 htab->root.splt->contents + htab->tlsdesc_plt;
6463
6464 /* adrp x2, DT_TLSDESC_GOT */
6465 elf_aarch64_update_plt_entry (output_bfd,
6466 BFD_RELOC_AARCH64_ADR_HI21_PCREL,
6467 plt_entry + 4,
6468 (PG (dt_tlsdesc_got)
6469 - PG (adrp1_addr)));
6470
6471 /* adrp x3, 0 */
6472 elf_aarch64_update_plt_entry (output_bfd,
6473 BFD_RELOC_AARCH64_ADR_HI21_PCREL,
6474 plt_entry + 8,
6475 (PG (pltgot_addr)
6476 - PG (adrp2_addr)));
6477
6478 /* ldr x2, [x2, #0] */
6479 elf_aarch64_update_plt_entry (output_bfd,
6480 BFD_RELOC_AARCH64_LDSTNN_LO12,
6481 plt_entry + 12,
6482 PG_OFFSET (dt_tlsdesc_got));
6483
6484 /* add x3, x3, 0 */
6485 elf_aarch64_update_plt_entry (output_bfd,
6486 BFD_RELOC_AARCH64_ADD_LO12,
6487 plt_entry + 16,
6488 PG_OFFSET (pltgot_addr));
6489 }
6490 }
6491 }
6492
6493 if (htab->root.sgotplt)
6494 {
6495 if (bfd_is_abs_section (htab->root.sgotplt->output_section))
6496 {
6497 (*_bfd_error_handler)
6498 (_("discarded output section: `%A'"), htab->root.sgotplt);
6499 return FALSE;
6500 }
6501
6502 /* Fill in the first three entries in the global offset table. */
6503 if (htab->root.sgotplt->size > 0)
6504 {
6505 bfd_put_NN (output_bfd, (bfd_vma) 0, htab->root.sgotplt->contents);
6506
6507 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
6508 bfd_put_NN (output_bfd,
6509 (bfd_vma) 0,
6510 htab->root.sgotplt->contents + GOT_ENTRY_SIZE);
6511 bfd_put_NN (output_bfd,
6512 (bfd_vma) 0,
6513 htab->root.sgotplt->contents + GOT_ENTRY_SIZE * 2);
6514 }
6515
6516 if (htab->root.sgot)
6517 {
6518 if (htab->root.sgot->size > 0)
6519 {
6520 bfd_vma addr =
6521 sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0;
6522 bfd_put_NN (output_bfd, addr, htab->root.sgot->contents);
6523 }
6524 }
6525
6526 elf_section_data (htab->root.sgotplt->output_section)->
6527 this_hdr.sh_entsize = GOT_ENTRY_SIZE;
6528 }
6529
6530 if (htab->root.sgot && htab->root.sgot->size > 0)
6531 elf_section_data (htab->root.sgot->output_section)->this_hdr.sh_entsize
6532 = GOT_ENTRY_SIZE;
6533
6534 return TRUE;
6535 }
6536
6537 /* Return address for Ith PLT stub in section PLT, for relocation REL
6538 or (bfd_vma) -1 if it should not be included. */
6539
6540 static bfd_vma
6541 elfNN_aarch64_plt_sym_val (bfd_vma i, const asection *plt,
6542 const arelent *rel ATTRIBUTE_UNUSED)
6543 {
6544 return plt->vma + PLT_ENTRY_SIZE + i * PLT_SMALL_ENTRY_SIZE;
6545 }
6546
6547
6548 /* We use this so we can override certain functions
6549 (though currently we don't). */
6550
6551 const struct elf_size_info elfNN_aarch64_size_info =
6552 {
6553 sizeof (ElfNN_External_Ehdr),
6554 sizeof (ElfNN_External_Phdr),
6555 sizeof (ElfNN_External_Shdr),
6556 sizeof (ElfNN_External_Rel),
6557 sizeof (ElfNN_External_Rela),
6558 sizeof (ElfNN_External_Sym),
6559 sizeof (ElfNN_External_Dyn),
6560 sizeof (Elf_External_Note),
6561 4, /* Hash table entry size. */
6562 1, /* Internal relocs per external relocs. */
6563 ARCH_SIZE, /* Arch size. */
6564 LOG_FILE_ALIGN, /* Log_file_align. */
6565 ELFCLASSNN, EV_CURRENT,
6566 bfd_elfNN_write_out_phdrs,
6567 bfd_elfNN_write_shdrs_and_ehdr,
6568 bfd_elfNN_checksum_contents,
6569 bfd_elfNN_write_relocs,
6570 bfd_elfNN_swap_symbol_in,
6571 bfd_elfNN_swap_symbol_out,
6572 bfd_elfNN_slurp_reloc_table,
6573 bfd_elfNN_slurp_symbol_table,
6574 bfd_elfNN_swap_dyn_in,
6575 bfd_elfNN_swap_dyn_out,
6576 bfd_elfNN_swap_reloc_in,
6577 bfd_elfNN_swap_reloc_out,
6578 bfd_elfNN_swap_reloca_in,
6579 bfd_elfNN_swap_reloca_out
6580 };
6581
6582 #define ELF_ARCH bfd_arch_aarch64
6583 #define ELF_MACHINE_CODE EM_AARCH64
6584 #define ELF_MAXPAGESIZE 0x10000
6585 #define ELF_MINPAGESIZE 0x1000
6586 #define ELF_COMMONPAGESIZE 0x1000
6587
6588 #define bfd_elfNN_close_and_cleanup \
6589 elfNN_aarch64_close_and_cleanup
6590
6591 #define bfd_elfNN_bfd_copy_private_bfd_data \
6592 elfNN_aarch64_copy_private_bfd_data
6593
6594 #define bfd_elfNN_bfd_free_cached_info \
6595 elfNN_aarch64_bfd_free_cached_info
6596
6597 #define bfd_elfNN_bfd_is_target_special_symbol \
6598 elfNN_aarch64_is_target_special_symbol
6599
6600 #define bfd_elfNN_bfd_link_hash_table_create \
6601 elfNN_aarch64_link_hash_table_create
6602
6603 #define bfd_elfNN_bfd_link_hash_table_free \
6604 elfNN_aarch64_hash_table_free
6605
6606 #define bfd_elfNN_bfd_merge_private_bfd_data \
6607 elfNN_aarch64_merge_private_bfd_data
6608
6609 #define bfd_elfNN_bfd_print_private_bfd_data \
6610 elfNN_aarch64_print_private_bfd_data
6611
6612 #define bfd_elfNN_bfd_reloc_type_lookup \
6613 elfNN_aarch64_reloc_type_lookup
6614
6615 #define bfd_elfNN_bfd_reloc_name_lookup \
6616 elfNN_aarch64_reloc_name_lookup
6617
6618 #define bfd_elfNN_bfd_set_private_flags \
6619 elfNN_aarch64_set_private_flags
6620
6621 #define bfd_elfNN_find_inliner_info \
6622 elfNN_aarch64_find_inliner_info
6623
6624 #define bfd_elfNN_find_nearest_line \
6625 elfNN_aarch64_find_nearest_line
6626
6627 #define bfd_elfNN_mkobject \
6628 elfNN_aarch64_mkobject
6629
6630 #define bfd_elfNN_new_section_hook \
6631 elfNN_aarch64_new_section_hook
6632
6633 #define elf_backend_adjust_dynamic_symbol \
6634 elfNN_aarch64_adjust_dynamic_symbol
6635
6636 #define elf_backend_always_size_sections \
6637 elfNN_aarch64_always_size_sections
6638
6639 #define elf_backend_check_relocs \
6640 elfNN_aarch64_check_relocs
6641
6642 #define elf_backend_copy_indirect_symbol \
6643 elfNN_aarch64_copy_indirect_symbol
6644
6645 /* Create .dynbss, and .rela.bss sections in DYNOBJ, and set up shortcuts
6646 to them in our hash. */
6647 #define elf_backend_create_dynamic_sections \
6648 elfNN_aarch64_create_dynamic_sections
6649
6650 #define elf_backend_init_index_section \
6651 _bfd_elf_init_2_index_sections
6652
6653 #define elf_backend_is_function_type \
6654 elfNN_aarch64_is_function_type
6655
6656 #define elf_backend_finish_dynamic_sections \
6657 elfNN_aarch64_finish_dynamic_sections
6658
6659 #define elf_backend_finish_dynamic_symbol \
6660 elfNN_aarch64_finish_dynamic_symbol
6661
6662 #define elf_backend_gc_sweep_hook \
6663 elfNN_aarch64_gc_sweep_hook
6664
6665 #define elf_backend_object_p \
6666 elfNN_aarch64_object_p
6667
6668 #define elf_backend_output_arch_local_syms \
6669 elfNN_aarch64_output_arch_local_syms
6670
6671 #define elf_backend_plt_sym_val \
6672 elfNN_aarch64_plt_sym_val
6673
6674 #define elf_backend_post_process_headers \
6675 elfNN_aarch64_post_process_headers
6676
6677 #define elf_backend_relocate_section \
6678 elfNN_aarch64_relocate_section
6679
6680 #define elf_backend_reloc_type_class \
6681 elfNN_aarch64_reloc_type_class
6682
6683 #define elf_backend_section_flags \
6684 elfNN_aarch64_section_flags
6685
6686 #define elf_backend_section_from_shdr \
6687 elfNN_aarch64_section_from_shdr
6688
6689 #define elf_backend_size_dynamic_sections \
6690 elfNN_aarch64_size_dynamic_sections
6691
6692 #define elf_backend_size_info \
6693 elfNN_aarch64_size_info
6694
6695 #define elf_backend_can_refcount 1
6696 #define elf_backend_can_gc_sections 1
6697 #define elf_backend_plt_readonly 1
6698 #define elf_backend_want_got_plt 1
6699 #define elf_backend_want_plt_sym 0
6700 #define elf_backend_may_use_rel_p 0
6701 #define elf_backend_may_use_rela_p 1
6702 #define elf_backend_default_use_rela_p 1
6703 #define elf_backend_got_header_size (GOT_ENTRY_SIZE * 3)
6704 #define elf_backend_default_execstack 0
6705
6706 #undef elf_backend_obj_attrs_section
6707 #define elf_backend_obj_attrs_section ".ARM.attributes"
6708
6709 #include "elfNN-target.h"