[AArch64][3/8] LD support BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21
[deliverable/binutils-gdb.git] / bfd / elfnn-aarch64.c
1 /* AArch64-specific support for NN-bit ELF.
2 Copyright (C) 2009-2015 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 "objalloc.h"
146 #include "elf/aarch64.h"
147 #include "elfxx-aarch64.h"
148
149 #define ARCH_SIZE NN
150
151 #if ARCH_SIZE == 64
152 #define AARCH64_R(NAME) R_AARCH64_ ## NAME
153 #define AARCH64_R_STR(NAME) "R_AARCH64_" #NAME
154 #define HOWTO64(...) HOWTO (__VA_ARGS__)
155 #define HOWTO32(...) EMPTY_HOWTO (0)
156 #define LOG_FILE_ALIGN 3
157 #endif
158
159 #if ARCH_SIZE == 32
160 #define AARCH64_R(NAME) R_AARCH64_P32_ ## NAME
161 #define AARCH64_R_STR(NAME) "R_AARCH64_P32_" #NAME
162 #define HOWTO64(...) EMPTY_HOWTO (0)
163 #define HOWTO32(...) HOWTO (__VA_ARGS__)
164 #define LOG_FILE_ALIGN 2
165 #endif
166
167 #define IS_AARCH64_TLS_RELOC(R_TYPE) \
168 ((R_TYPE) == BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC \
169 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21 \
170 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_ADR_PREL21 \
171 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 \
172 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC \
173 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC \
174 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19 \
175 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC \
176 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1 \
177 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21 \
178 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_ADR_PREL21 \
179 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12 \
180 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12 \
181 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC \
182 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0 \
183 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC \
184 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1 \
185 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC \
186 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2 \
187 || (R_TYPE) == BFD_RELOC_AARCH64_TLS_DTPMOD \
188 || (R_TYPE) == BFD_RELOC_AARCH64_TLS_DTPREL \
189 || (R_TYPE) == BFD_RELOC_AARCH64_TLS_TPREL \
190 || IS_AARCH64_TLSDESC_RELOC ((R_TYPE)))
191
192 #define IS_AARCH64_TLSDESC_RELOC(R_TYPE) \
193 ((R_TYPE) == BFD_RELOC_AARCH64_TLSDESC \
194 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADD \
195 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC \
196 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21 \
197 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21 \
198 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_CALL \
199 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC \
200 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC \
201 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LDR \
202 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LD_PREL19 \
203 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC \
204 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_OFF_G1)
205
206 #define ELIMINATE_COPY_RELOCS 0
207
208 /* Return size of a relocation entry. HTAB is the bfd's
209 elf_aarch64_link_hash_entry. */
210 #define RELOC_SIZE(HTAB) (sizeof (ElfNN_External_Rela))
211
212 /* GOT Entry size - 8 bytes in ELF64 and 4 bytes in ELF32. */
213 #define GOT_ENTRY_SIZE (ARCH_SIZE / 8)
214 #define PLT_ENTRY_SIZE (32)
215 #define PLT_SMALL_ENTRY_SIZE (16)
216 #define PLT_TLSDESC_ENTRY_SIZE (32)
217
218 /* Encoding of the nop instruction */
219 #define INSN_NOP 0xd503201f
220
221 #define aarch64_compute_jump_table_size(htab) \
222 (((htab)->root.srelplt == NULL) ? 0 \
223 : (htab)->root.srelplt->reloc_count * GOT_ENTRY_SIZE)
224
225 /* The first entry in a procedure linkage table looks like this
226 if the distance between the PLTGOT and the PLT is < 4GB use
227 these PLT entries. Note that the dynamic linker gets &PLTGOT[2]
228 in x16 and needs to work out PLTGOT[1] by using an address of
229 [x16,#-GOT_ENTRY_SIZE]. */
230 static const bfd_byte elfNN_aarch64_small_plt0_entry[PLT_ENTRY_SIZE] =
231 {
232 0xf0, 0x7b, 0xbf, 0xa9, /* stp x16, x30, [sp, #-16]! */
233 0x10, 0x00, 0x00, 0x90, /* adrp x16, (GOT+16) */
234 #if ARCH_SIZE == 64
235 0x11, 0x0A, 0x40, 0xf9, /* ldr x17, [x16, #PLT_GOT+0x10] */
236 0x10, 0x42, 0x00, 0x91, /* add x16, x16,#PLT_GOT+0x10 */
237 #else
238 0x11, 0x0A, 0x40, 0xb9, /* ldr w17, [x16, #PLT_GOT+0x8] */
239 0x10, 0x22, 0x00, 0x11, /* add w16, w16,#PLT_GOT+0x8 */
240 #endif
241 0x20, 0x02, 0x1f, 0xd6, /* br x17 */
242 0x1f, 0x20, 0x03, 0xd5, /* nop */
243 0x1f, 0x20, 0x03, 0xd5, /* nop */
244 0x1f, 0x20, 0x03, 0xd5, /* nop */
245 };
246
247 /* Per function entry in a procedure linkage table looks like this
248 if the distance between the PLTGOT and the PLT is < 4GB use
249 these PLT entries. */
250 static const bfd_byte elfNN_aarch64_small_plt_entry[PLT_SMALL_ENTRY_SIZE] =
251 {
252 0x10, 0x00, 0x00, 0x90, /* adrp x16, PLTGOT + n * 8 */
253 #if ARCH_SIZE == 64
254 0x11, 0x02, 0x40, 0xf9, /* ldr x17, [x16, PLTGOT + n * 8] */
255 0x10, 0x02, 0x00, 0x91, /* add x16, x16, :lo12:PLTGOT + n * 8 */
256 #else
257 0x11, 0x02, 0x40, 0xb9, /* ldr w17, [x16, PLTGOT + n * 4] */
258 0x10, 0x02, 0x00, 0x11, /* add w16, w16, :lo12:PLTGOT + n * 4 */
259 #endif
260 0x20, 0x02, 0x1f, 0xd6, /* br x17. */
261 };
262
263 static const bfd_byte
264 elfNN_aarch64_tlsdesc_small_plt_entry[PLT_TLSDESC_ENTRY_SIZE] =
265 {
266 0xe2, 0x0f, 0xbf, 0xa9, /* stp x2, x3, [sp, #-16]! */
267 0x02, 0x00, 0x00, 0x90, /* adrp x2, 0 */
268 0x03, 0x00, 0x00, 0x90, /* adrp x3, 0 */
269 #if ARCH_SIZE == 64
270 0x42, 0x00, 0x40, 0xf9, /* ldr x2, [x2, #0] */
271 0x63, 0x00, 0x00, 0x91, /* add x3, x3, 0 */
272 #else
273 0x42, 0x00, 0x40, 0xb9, /* ldr w2, [x2, #0] */
274 0x63, 0x00, 0x00, 0x11, /* add w3, w3, 0 */
275 #endif
276 0x40, 0x00, 0x1f, 0xd6, /* br x2 */
277 0x1f, 0x20, 0x03, 0xd5, /* nop */
278 0x1f, 0x20, 0x03, 0xd5, /* nop */
279 };
280
281 #define elf_info_to_howto elfNN_aarch64_info_to_howto
282 #define elf_info_to_howto_rel elfNN_aarch64_info_to_howto
283
284 #define AARCH64_ELF_ABI_VERSION 0
285
286 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
287 #define ALL_ONES (~ (bfd_vma) 0)
288
289 /* Indexed by the bfd interal reloc enumerators.
290 Therefore, the table needs to be synced with BFD_RELOC_AARCH64_*
291 in reloc.c. */
292
293 static reloc_howto_type elfNN_aarch64_howto_table[] =
294 {
295 EMPTY_HOWTO (0),
296
297 /* Basic data relocations. */
298
299 #if ARCH_SIZE == 64
300 HOWTO (R_AARCH64_NULL, /* type */
301 0, /* rightshift */
302 3, /* size (0 = byte, 1 = short, 2 = long) */
303 0, /* bitsize */
304 FALSE, /* pc_relative */
305 0, /* bitpos */
306 complain_overflow_dont, /* complain_on_overflow */
307 bfd_elf_generic_reloc, /* special_function */
308 "R_AARCH64_NULL", /* name */
309 FALSE, /* partial_inplace */
310 0, /* src_mask */
311 0, /* dst_mask */
312 FALSE), /* pcrel_offset */
313 #else
314 HOWTO (R_AARCH64_NONE, /* type */
315 0, /* rightshift */
316 3, /* size (0 = byte, 1 = short, 2 = long) */
317 0, /* bitsize */
318 FALSE, /* pc_relative */
319 0, /* bitpos */
320 complain_overflow_dont, /* complain_on_overflow */
321 bfd_elf_generic_reloc, /* special_function */
322 "R_AARCH64_NONE", /* name */
323 FALSE, /* partial_inplace */
324 0, /* src_mask */
325 0, /* dst_mask */
326 FALSE), /* pcrel_offset */
327 #endif
328
329 /* .xword: (S+A) */
330 HOWTO64 (AARCH64_R (ABS64), /* type */
331 0, /* rightshift */
332 4, /* size (4 = long long) */
333 64, /* bitsize */
334 FALSE, /* pc_relative */
335 0, /* bitpos */
336 complain_overflow_unsigned, /* complain_on_overflow */
337 bfd_elf_generic_reloc, /* special_function */
338 AARCH64_R_STR (ABS64), /* name */
339 FALSE, /* partial_inplace */
340 ALL_ONES, /* src_mask */
341 ALL_ONES, /* dst_mask */
342 FALSE), /* pcrel_offset */
343
344 /* .word: (S+A) */
345 HOWTO (AARCH64_R (ABS32), /* type */
346 0, /* rightshift */
347 2, /* size (0 = byte, 1 = short, 2 = long) */
348 32, /* bitsize */
349 FALSE, /* pc_relative */
350 0, /* bitpos */
351 complain_overflow_unsigned, /* complain_on_overflow */
352 bfd_elf_generic_reloc, /* special_function */
353 AARCH64_R_STR (ABS32), /* name */
354 FALSE, /* partial_inplace */
355 0xffffffff, /* src_mask */
356 0xffffffff, /* dst_mask */
357 FALSE), /* pcrel_offset */
358
359 /* .half: (S+A) */
360 HOWTO (AARCH64_R (ABS16), /* type */
361 0, /* rightshift */
362 1, /* size (0 = byte, 1 = short, 2 = long) */
363 16, /* bitsize */
364 FALSE, /* pc_relative */
365 0, /* bitpos */
366 complain_overflow_unsigned, /* complain_on_overflow */
367 bfd_elf_generic_reloc, /* special_function */
368 AARCH64_R_STR (ABS16), /* name */
369 FALSE, /* partial_inplace */
370 0xffff, /* src_mask */
371 0xffff, /* dst_mask */
372 FALSE), /* pcrel_offset */
373
374 /* .xword: (S+A-P) */
375 HOWTO64 (AARCH64_R (PREL64), /* type */
376 0, /* rightshift */
377 4, /* size (4 = long long) */
378 64, /* bitsize */
379 TRUE, /* pc_relative */
380 0, /* bitpos */
381 complain_overflow_signed, /* complain_on_overflow */
382 bfd_elf_generic_reloc, /* special_function */
383 AARCH64_R_STR (PREL64), /* name */
384 FALSE, /* partial_inplace */
385 ALL_ONES, /* src_mask */
386 ALL_ONES, /* dst_mask */
387 TRUE), /* pcrel_offset */
388
389 /* .word: (S+A-P) */
390 HOWTO (AARCH64_R (PREL32), /* type */
391 0, /* rightshift */
392 2, /* size (0 = byte, 1 = short, 2 = long) */
393 32, /* bitsize */
394 TRUE, /* pc_relative */
395 0, /* bitpos */
396 complain_overflow_signed, /* complain_on_overflow */
397 bfd_elf_generic_reloc, /* special_function */
398 AARCH64_R_STR (PREL32), /* name */
399 FALSE, /* partial_inplace */
400 0xffffffff, /* src_mask */
401 0xffffffff, /* dst_mask */
402 TRUE), /* pcrel_offset */
403
404 /* .half: (S+A-P) */
405 HOWTO (AARCH64_R (PREL16), /* type */
406 0, /* rightshift */
407 1, /* size (0 = byte, 1 = short, 2 = long) */
408 16, /* bitsize */
409 TRUE, /* pc_relative */
410 0, /* bitpos */
411 complain_overflow_signed, /* complain_on_overflow */
412 bfd_elf_generic_reloc, /* special_function */
413 AARCH64_R_STR (PREL16), /* name */
414 FALSE, /* partial_inplace */
415 0xffff, /* src_mask */
416 0xffff, /* dst_mask */
417 TRUE), /* pcrel_offset */
418
419 /* Group relocations to create a 16, 32, 48 or 64 bit
420 unsigned data or abs address inline. */
421
422 /* MOVZ: ((S+A) >> 0) & 0xffff */
423 HOWTO (AARCH64_R (MOVW_UABS_G0), /* type */
424 0, /* rightshift */
425 2, /* size (0 = byte, 1 = short, 2 = long) */
426 16, /* bitsize */
427 FALSE, /* pc_relative */
428 0, /* bitpos */
429 complain_overflow_unsigned, /* complain_on_overflow */
430 bfd_elf_generic_reloc, /* special_function */
431 AARCH64_R_STR (MOVW_UABS_G0), /* name */
432 FALSE, /* partial_inplace */
433 0xffff, /* src_mask */
434 0xffff, /* dst_mask */
435 FALSE), /* pcrel_offset */
436
437 /* MOVK: ((S+A) >> 0) & 0xffff [no overflow check] */
438 HOWTO (AARCH64_R (MOVW_UABS_G0_NC), /* type */
439 0, /* rightshift */
440 2, /* size (0 = byte, 1 = short, 2 = long) */
441 16, /* bitsize */
442 FALSE, /* pc_relative */
443 0, /* bitpos */
444 complain_overflow_dont, /* complain_on_overflow */
445 bfd_elf_generic_reloc, /* special_function */
446 AARCH64_R_STR (MOVW_UABS_G0_NC), /* name */
447 FALSE, /* partial_inplace */
448 0xffff, /* src_mask */
449 0xffff, /* dst_mask */
450 FALSE), /* pcrel_offset */
451
452 /* MOVZ: ((S+A) >> 16) & 0xffff */
453 HOWTO (AARCH64_R (MOVW_UABS_G1), /* type */
454 16, /* rightshift */
455 2, /* size (0 = byte, 1 = short, 2 = long) */
456 16, /* bitsize */
457 FALSE, /* pc_relative */
458 0, /* bitpos */
459 complain_overflow_unsigned, /* complain_on_overflow */
460 bfd_elf_generic_reloc, /* special_function */
461 AARCH64_R_STR (MOVW_UABS_G1), /* name */
462 FALSE, /* partial_inplace */
463 0xffff, /* src_mask */
464 0xffff, /* dst_mask */
465 FALSE), /* pcrel_offset */
466
467 /* MOVK: ((S+A) >> 16) & 0xffff [no overflow check] */
468 HOWTO64 (AARCH64_R (MOVW_UABS_G1_NC), /* type */
469 16, /* rightshift */
470 2, /* size (0 = byte, 1 = short, 2 = long) */
471 16, /* bitsize */
472 FALSE, /* pc_relative */
473 0, /* bitpos */
474 complain_overflow_dont, /* complain_on_overflow */
475 bfd_elf_generic_reloc, /* special_function */
476 AARCH64_R_STR (MOVW_UABS_G1_NC), /* name */
477 FALSE, /* partial_inplace */
478 0xffff, /* src_mask */
479 0xffff, /* dst_mask */
480 FALSE), /* pcrel_offset */
481
482 /* MOVZ: ((S+A) >> 32) & 0xffff */
483 HOWTO64 (AARCH64_R (MOVW_UABS_G2), /* type */
484 32, /* rightshift */
485 2, /* size (0 = byte, 1 = short, 2 = long) */
486 16, /* bitsize */
487 FALSE, /* pc_relative */
488 0, /* bitpos */
489 complain_overflow_unsigned, /* complain_on_overflow */
490 bfd_elf_generic_reloc, /* special_function */
491 AARCH64_R_STR (MOVW_UABS_G2), /* name */
492 FALSE, /* partial_inplace */
493 0xffff, /* src_mask */
494 0xffff, /* dst_mask */
495 FALSE), /* pcrel_offset */
496
497 /* MOVK: ((S+A) >> 32) & 0xffff [no overflow check] */
498 HOWTO64 (AARCH64_R (MOVW_UABS_G2_NC), /* type */
499 32, /* rightshift */
500 2, /* size (0 = byte, 1 = short, 2 = long) */
501 16, /* bitsize */
502 FALSE, /* pc_relative */
503 0, /* bitpos */
504 complain_overflow_dont, /* complain_on_overflow */
505 bfd_elf_generic_reloc, /* special_function */
506 AARCH64_R_STR (MOVW_UABS_G2_NC), /* name */
507 FALSE, /* partial_inplace */
508 0xffff, /* src_mask */
509 0xffff, /* dst_mask */
510 FALSE), /* pcrel_offset */
511
512 /* MOVZ: ((S+A) >> 48) & 0xffff */
513 HOWTO64 (AARCH64_R (MOVW_UABS_G3), /* type */
514 48, /* rightshift */
515 2, /* size (0 = byte, 1 = short, 2 = long) */
516 16, /* bitsize */
517 FALSE, /* pc_relative */
518 0, /* bitpos */
519 complain_overflow_unsigned, /* complain_on_overflow */
520 bfd_elf_generic_reloc, /* special_function */
521 AARCH64_R_STR (MOVW_UABS_G3), /* name */
522 FALSE, /* partial_inplace */
523 0xffff, /* src_mask */
524 0xffff, /* dst_mask */
525 FALSE), /* pcrel_offset */
526
527 /* Group relocations to create high part of a 16, 32, 48 or 64 bit
528 signed data or abs address inline. Will change instruction
529 to MOVN or MOVZ depending on sign of calculated value. */
530
531 /* MOV[ZN]: ((S+A) >> 0) & 0xffff */
532 HOWTO (AARCH64_R (MOVW_SABS_G0), /* type */
533 0, /* rightshift */
534 2, /* size (0 = byte, 1 = short, 2 = long) */
535 16, /* bitsize */
536 FALSE, /* pc_relative */
537 0, /* bitpos */
538 complain_overflow_signed, /* complain_on_overflow */
539 bfd_elf_generic_reloc, /* special_function */
540 AARCH64_R_STR (MOVW_SABS_G0), /* name */
541 FALSE, /* partial_inplace */
542 0xffff, /* src_mask */
543 0xffff, /* dst_mask */
544 FALSE), /* pcrel_offset */
545
546 /* MOV[ZN]: ((S+A) >> 16) & 0xffff */
547 HOWTO64 (AARCH64_R (MOVW_SABS_G1), /* type */
548 16, /* rightshift */
549 2, /* size (0 = byte, 1 = short, 2 = long) */
550 16, /* bitsize */
551 FALSE, /* pc_relative */
552 0, /* bitpos */
553 complain_overflow_signed, /* complain_on_overflow */
554 bfd_elf_generic_reloc, /* special_function */
555 AARCH64_R_STR (MOVW_SABS_G1), /* name */
556 FALSE, /* partial_inplace */
557 0xffff, /* src_mask */
558 0xffff, /* dst_mask */
559 FALSE), /* pcrel_offset */
560
561 /* MOV[ZN]: ((S+A) >> 32) & 0xffff */
562 HOWTO64 (AARCH64_R (MOVW_SABS_G2), /* type */
563 32, /* rightshift */
564 2, /* size (0 = byte, 1 = short, 2 = long) */
565 16, /* bitsize */
566 FALSE, /* pc_relative */
567 0, /* bitpos */
568 complain_overflow_signed, /* complain_on_overflow */
569 bfd_elf_generic_reloc, /* special_function */
570 AARCH64_R_STR (MOVW_SABS_G2), /* name */
571 FALSE, /* partial_inplace */
572 0xffff, /* src_mask */
573 0xffff, /* dst_mask */
574 FALSE), /* pcrel_offset */
575
576 /* Relocations to generate 19, 21 and 33 bit PC-relative load/store
577 addresses: PG(x) is (x & ~0xfff). */
578
579 /* LD-lit: ((S+A-P) >> 2) & 0x7ffff */
580 HOWTO (AARCH64_R (LD_PREL_LO19), /* type */
581 2, /* rightshift */
582 2, /* size (0 = byte, 1 = short, 2 = long) */
583 19, /* bitsize */
584 TRUE, /* pc_relative */
585 0, /* bitpos */
586 complain_overflow_signed, /* complain_on_overflow */
587 bfd_elf_generic_reloc, /* special_function */
588 AARCH64_R_STR (LD_PREL_LO19), /* name */
589 FALSE, /* partial_inplace */
590 0x7ffff, /* src_mask */
591 0x7ffff, /* dst_mask */
592 TRUE), /* pcrel_offset */
593
594 /* ADR: (S+A-P) & 0x1fffff */
595 HOWTO (AARCH64_R (ADR_PREL_LO21), /* type */
596 0, /* rightshift */
597 2, /* size (0 = byte, 1 = short, 2 = long) */
598 21, /* bitsize */
599 TRUE, /* pc_relative */
600 0, /* bitpos */
601 complain_overflow_signed, /* complain_on_overflow */
602 bfd_elf_generic_reloc, /* special_function */
603 AARCH64_R_STR (ADR_PREL_LO21), /* name */
604 FALSE, /* partial_inplace */
605 0x1fffff, /* src_mask */
606 0x1fffff, /* dst_mask */
607 TRUE), /* pcrel_offset */
608
609 /* ADRP: ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
610 HOWTO (AARCH64_R (ADR_PREL_PG_HI21), /* type */
611 12, /* rightshift */
612 2, /* size (0 = byte, 1 = short, 2 = long) */
613 21, /* bitsize */
614 TRUE, /* pc_relative */
615 0, /* bitpos */
616 complain_overflow_signed, /* complain_on_overflow */
617 bfd_elf_generic_reloc, /* special_function */
618 AARCH64_R_STR (ADR_PREL_PG_HI21), /* name */
619 FALSE, /* partial_inplace */
620 0x1fffff, /* src_mask */
621 0x1fffff, /* dst_mask */
622 TRUE), /* pcrel_offset */
623
624 /* ADRP: ((PG(S+A)-PG(P)) >> 12) & 0x1fffff [no overflow check] */
625 HOWTO64 (AARCH64_R (ADR_PREL_PG_HI21_NC), /* type */
626 12, /* rightshift */
627 2, /* size (0 = byte, 1 = short, 2 = long) */
628 21, /* bitsize */
629 TRUE, /* pc_relative */
630 0, /* bitpos */
631 complain_overflow_dont, /* complain_on_overflow */
632 bfd_elf_generic_reloc, /* special_function */
633 AARCH64_R_STR (ADR_PREL_PG_HI21_NC), /* name */
634 FALSE, /* partial_inplace */
635 0x1fffff, /* src_mask */
636 0x1fffff, /* dst_mask */
637 TRUE), /* pcrel_offset */
638
639 /* ADD: (S+A) & 0xfff [no overflow check] */
640 HOWTO (AARCH64_R (ADD_ABS_LO12_NC), /* type */
641 0, /* rightshift */
642 2, /* size (0 = byte, 1 = short, 2 = long) */
643 12, /* bitsize */
644 FALSE, /* pc_relative */
645 10, /* bitpos */
646 complain_overflow_dont, /* complain_on_overflow */
647 bfd_elf_generic_reloc, /* special_function */
648 AARCH64_R_STR (ADD_ABS_LO12_NC), /* name */
649 FALSE, /* partial_inplace */
650 0x3ffc00, /* src_mask */
651 0x3ffc00, /* dst_mask */
652 FALSE), /* pcrel_offset */
653
654 /* LD/ST8: (S+A) & 0xfff */
655 HOWTO (AARCH64_R (LDST8_ABS_LO12_NC), /* type */
656 0, /* rightshift */
657 2, /* size (0 = byte, 1 = short, 2 = long) */
658 12, /* bitsize */
659 FALSE, /* pc_relative */
660 0, /* bitpos */
661 complain_overflow_dont, /* complain_on_overflow */
662 bfd_elf_generic_reloc, /* special_function */
663 AARCH64_R_STR (LDST8_ABS_LO12_NC), /* name */
664 FALSE, /* partial_inplace */
665 0xfff, /* src_mask */
666 0xfff, /* dst_mask */
667 FALSE), /* pcrel_offset */
668
669 /* Relocations for control-flow instructions. */
670
671 /* TBZ/NZ: ((S+A-P) >> 2) & 0x3fff */
672 HOWTO (AARCH64_R (TSTBR14), /* type */
673 2, /* rightshift */
674 2, /* size (0 = byte, 1 = short, 2 = long) */
675 14, /* bitsize */
676 TRUE, /* pc_relative */
677 0, /* bitpos */
678 complain_overflow_signed, /* complain_on_overflow */
679 bfd_elf_generic_reloc, /* special_function */
680 AARCH64_R_STR (TSTBR14), /* name */
681 FALSE, /* partial_inplace */
682 0x3fff, /* src_mask */
683 0x3fff, /* dst_mask */
684 TRUE), /* pcrel_offset */
685
686 /* B.cond: ((S+A-P) >> 2) & 0x7ffff */
687 HOWTO (AARCH64_R (CONDBR19), /* type */
688 2, /* rightshift */
689 2, /* size (0 = byte, 1 = short, 2 = long) */
690 19, /* bitsize */
691 TRUE, /* pc_relative */
692 0, /* bitpos */
693 complain_overflow_signed, /* complain_on_overflow */
694 bfd_elf_generic_reloc, /* special_function */
695 AARCH64_R_STR (CONDBR19), /* name */
696 FALSE, /* partial_inplace */
697 0x7ffff, /* src_mask */
698 0x7ffff, /* dst_mask */
699 TRUE), /* pcrel_offset */
700
701 /* B: ((S+A-P) >> 2) & 0x3ffffff */
702 HOWTO (AARCH64_R (JUMP26), /* type */
703 2, /* rightshift */
704 2, /* size (0 = byte, 1 = short, 2 = long) */
705 26, /* bitsize */
706 TRUE, /* pc_relative */
707 0, /* bitpos */
708 complain_overflow_signed, /* complain_on_overflow */
709 bfd_elf_generic_reloc, /* special_function */
710 AARCH64_R_STR (JUMP26), /* name */
711 FALSE, /* partial_inplace */
712 0x3ffffff, /* src_mask */
713 0x3ffffff, /* dst_mask */
714 TRUE), /* pcrel_offset */
715
716 /* BL: ((S+A-P) >> 2) & 0x3ffffff */
717 HOWTO (AARCH64_R (CALL26), /* type */
718 2, /* rightshift */
719 2, /* size (0 = byte, 1 = short, 2 = long) */
720 26, /* bitsize */
721 TRUE, /* pc_relative */
722 0, /* bitpos */
723 complain_overflow_signed, /* complain_on_overflow */
724 bfd_elf_generic_reloc, /* special_function */
725 AARCH64_R_STR (CALL26), /* name */
726 FALSE, /* partial_inplace */
727 0x3ffffff, /* src_mask */
728 0x3ffffff, /* dst_mask */
729 TRUE), /* pcrel_offset */
730
731 /* LD/ST16: (S+A) & 0xffe */
732 HOWTO (AARCH64_R (LDST16_ABS_LO12_NC), /* type */
733 1, /* rightshift */
734 2, /* size (0 = byte, 1 = short, 2 = long) */
735 12, /* bitsize */
736 FALSE, /* pc_relative */
737 0, /* bitpos */
738 complain_overflow_dont, /* complain_on_overflow */
739 bfd_elf_generic_reloc, /* special_function */
740 AARCH64_R_STR (LDST16_ABS_LO12_NC), /* name */
741 FALSE, /* partial_inplace */
742 0xffe, /* src_mask */
743 0xffe, /* dst_mask */
744 FALSE), /* pcrel_offset */
745
746 /* LD/ST32: (S+A) & 0xffc */
747 HOWTO (AARCH64_R (LDST32_ABS_LO12_NC), /* type */
748 2, /* rightshift */
749 2, /* size (0 = byte, 1 = short, 2 = long) */
750 12, /* bitsize */
751 FALSE, /* pc_relative */
752 0, /* bitpos */
753 complain_overflow_dont, /* complain_on_overflow */
754 bfd_elf_generic_reloc, /* special_function */
755 AARCH64_R_STR (LDST32_ABS_LO12_NC), /* name */
756 FALSE, /* partial_inplace */
757 0xffc, /* src_mask */
758 0xffc, /* dst_mask */
759 FALSE), /* pcrel_offset */
760
761 /* LD/ST64: (S+A) & 0xff8 */
762 HOWTO (AARCH64_R (LDST64_ABS_LO12_NC), /* type */
763 3, /* rightshift */
764 2, /* size (0 = byte, 1 = short, 2 = long) */
765 12, /* bitsize */
766 FALSE, /* pc_relative */
767 0, /* bitpos */
768 complain_overflow_dont, /* complain_on_overflow */
769 bfd_elf_generic_reloc, /* special_function */
770 AARCH64_R_STR (LDST64_ABS_LO12_NC), /* name */
771 FALSE, /* partial_inplace */
772 0xff8, /* src_mask */
773 0xff8, /* dst_mask */
774 FALSE), /* pcrel_offset */
775
776 /* LD/ST128: (S+A) & 0xff0 */
777 HOWTO (AARCH64_R (LDST128_ABS_LO12_NC), /* type */
778 4, /* rightshift */
779 2, /* size (0 = byte, 1 = short, 2 = long) */
780 12, /* bitsize */
781 FALSE, /* pc_relative */
782 0, /* bitpos */
783 complain_overflow_dont, /* complain_on_overflow */
784 bfd_elf_generic_reloc, /* special_function */
785 AARCH64_R_STR (LDST128_ABS_LO12_NC), /* name */
786 FALSE, /* partial_inplace */
787 0xff0, /* src_mask */
788 0xff0, /* dst_mask */
789 FALSE), /* pcrel_offset */
790
791 /* Set a load-literal immediate field to bits
792 0x1FFFFC of G(S)-P */
793 HOWTO (AARCH64_R (GOT_LD_PREL19), /* type */
794 2, /* rightshift */
795 2, /* size (0 = byte,1 = short,2 = long) */
796 19, /* bitsize */
797 TRUE, /* pc_relative */
798 0, /* bitpos */
799 complain_overflow_signed, /* complain_on_overflow */
800 bfd_elf_generic_reloc, /* special_function */
801 AARCH64_R_STR (GOT_LD_PREL19), /* name */
802 FALSE, /* partial_inplace */
803 0xffffe0, /* src_mask */
804 0xffffe0, /* dst_mask */
805 TRUE), /* pcrel_offset */
806
807 /* Get to the page for the GOT entry for the symbol
808 (G(S) - P) using an ADRP instruction. */
809 HOWTO (AARCH64_R (ADR_GOT_PAGE), /* type */
810 12, /* rightshift */
811 2, /* size (0 = byte, 1 = short, 2 = long) */
812 21, /* bitsize */
813 TRUE, /* pc_relative */
814 0, /* bitpos */
815 complain_overflow_dont, /* complain_on_overflow */
816 bfd_elf_generic_reloc, /* special_function */
817 AARCH64_R_STR (ADR_GOT_PAGE), /* name */
818 FALSE, /* partial_inplace */
819 0x1fffff, /* src_mask */
820 0x1fffff, /* dst_mask */
821 TRUE), /* pcrel_offset */
822
823 /* LD64: GOT offset G(S) & 0xff8 */
824 HOWTO64 (AARCH64_R (LD64_GOT_LO12_NC), /* type */
825 3, /* rightshift */
826 2, /* size (0 = byte, 1 = short, 2 = long) */
827 12, /* bitsize */
828 FALSE, /* pc_relative */
829 0, /* bitpos */
830 complain_overflow_dont, /* complain_on_overflow */
831 bfd_elf_generic_reloc, /* special_function */
832 AARCH64_R_STR (LD64_GOT_LO12_NC), /* name */
833 FALSE, /* partial_inplace */
834 0xff8, /* src_mask */
835 0xff8, /* dst_mask */
836 FALSE), /* pcrel_offset */
837
838 /* LD32: GOT offset G(S) & 0xffc */
839 HOWTO32 (AARCH64_R (LD32_GOT_LO12_NC), /* type */
840 2, /* rightshift */
841 2, /* size (0 = byte, 1 = short, 2 = long) */
842 12, /* bitsize */
843 FALSE, /* pc_relative */
844 0, /* bitpos */
845 complain_overflow_dont, /* complain_on_overflow */
846 bfd_elf_generic_reloc, /* special_function */
847 AARCH64_R_STR (LD32_GOT_LO12_NC), /* name */
848 FALSE, /* partial_inplace */
849 0xffc, /* src_mask */
850 0xffc, /* dst_mask */
851 FALSE), /* pcrel_offset */
852
853 /* LD64: GOT offset for the symbol. */
854 HOWTO64 (AARCH64_R (LD64_GOTOFF_LO15), /* type */
855 3, /* rightshift */
856 2, /* size (0 = byte, 1 = short, 2 = long) */
857 12, /* bitsize */
858 FALSE, /* pc_relative */
859 0, /* bitpos */
860 complain_overflow_unsigned, /* complain_on_overflow */
861 bfd_elf_generic_reloc, /* special_function */
862 AARCH64_R_STR (LD64_GOTOFF_LO15), /* name */
863 FALSE, /* partial_inplace */
864 0x7ff8, /* src_mask */
865 0x7ff8, /* dst_mask */
866 FALSE), /* pcrel_offset */
867
868 /* LD32: GOT offset to the page address of GOT table.
869 (G(S) - PAGE (_GLOBAL_OFFSET_TABLE_)) & 0x5ffc. */
870 HOWTO32 (AARCH64_R (LD32_GOTPAGE_LO14), /* type */
871 2, /* rightshift */
872 2, /* size (0 = byte, 1 = short, 2 = long) */
873 12, /* bitsize */
874 FALSE, /* pc_relative */
875 0, /* bitpos */
876 complain_overflow_unsigned, /* complain_on_overflow */
877 bfd_elf_generic_reloc, /* special_function */
878 AARCH64_R_STR (LD32_GOTPAGE_LO14), /* name */
879 FALSE, /* partial_inplace */
880 0x5ffc, /* src_mask */
881 0x5ffc, /* dst_mask */
882 FALSE), /* pcrel_offset */
883
884 /* LD64: GOT offset to the page address of GOT table.
885 (G(S) - PAGE (_GLOBAL_OFFSET_TABLE_)) & 0x7ff8. */
886 HOWTO64 (AARCH64_R (LD64_GOTPAGE_LO15), /* type */
887 3, /* rightshift */
888 2, /* size (0 = byte, 1 = short, 2 = long) */
889 12, /* bitsize */
890 FALSE, /* pc_relative */
891 0, /* bitpos */
892 complain_overflow_unsigned, /* complain_on_overflow */
893 bfd_elf_generic_reloc, /* special_function */
894 AARCH64_R_STR (LD64_GOTPAGE_LO15), /* name */
895 FALSE, /* partial_inplace */
896 0x7ff8, /* src_mask */
897 0x7ff8, /* dst_mask */
898 FALSE), /* pcrel_offset */
899
900 /* Get to the page for the GOT entry for the symbol
901 (G(S) - P) using an ADRP instruction. */
902 HOWTO (AARCH64_R (TLSGD_ADR_PAGE21), /* type */
903 12, /* rightshift */
904 2, /* size (0 = byte, 1 = short, 2 = long) */
905 21, /* bitsize */
906 TRUE, /* pc_relative */
907 0, /* bitpos */
908 complain_overflow_dont, /* complain_on_overflow */
909 bfd_elf_generic_reloc, /* special_function */
910 AARCH64_R_STR (TLSGD_ADR_PAGE21), /* name */
911 FALSE, /* partial_inplace */
912 0x1fffff, /* src_mask */
913 0x1fffff, /* dst_mask */
914 TRUE), /* pcrel_offset */
915
916 HOWTO (AARCH64_R (TLSGD_ADR_PREL21), /* type */
917 0, /* rightshift */
918 2, /* size (0 = byte, 1 = short, 2 = long) */
919 21, /* bitsize */
920 TRUE, /* pc_relative */
921 0, /* bitpos */
922 complain_overflow_dont, /* complain_on_overflow */
923 bfd_elf_generic_reloc, /* special_function */
924 AARCH64_R_STR (TLSGD_ADR_PREL21), /* name */
925 FALSE, /* partial_inplace */
926 0x1fffff, /* src_mask */
927 0x1fffff, /* dst_mask */
928 TRUE), /* pcrel_offset */
929
930 /* ADD: GOT offset G(S) & 0xff8 [no overflow check] */
931 HOWTO (AARCH64_R (TLSGD_ADD_LO12_NC), /* type */
932 0, /* rightshift */
933 2, /* size (0 = byte, 1 = short, 2 = long) */
934 12, /* bitsize */
935 FALSE, /* pc_relative */
936 0, /* bitpos */
937 complain_overflow_dont, /* complain_on_overflow */
938 bfd_elf_generic_reloc, /* special_function */
939 AARCH64_R_STR (TLSGD_ADD_LO12_NC), /* name */
940 FALSE, /* partial_inplace */
941 0xfff, /* src_mask */
942 0xfff, /* dst_mask */
943 FALSE), /* pcrel_offset */
944
945 HOWTO64 (AARCH64_R (TLSIE_MOVW_GOTTPREL_G1), /* type */
946 16, /* rightshift */
947 2, /* size (0 = byte, 1 = short, 2 = long) */
948 16, /* bitsize */
949 FALSE, /* pc_relative */
950 0, /* bitpos */
951 complain_overflow_dont, /* complain_on_overflow */
952 bfd_elf_generic_reloc, /* special_function */
953 AARCH64_R_STR (TLSIE_MOVW_GOTTPREL_G1), /* name */
954 FALSE, /* partial_inplace */
955 0xffff, /* src_mask */
956 0xffff, /* dst_mask */
957 FALSE), /* pcrel_offset */
958
959 HOWTO64 (AARCH64_R (TLSIE_MOVW_GOTTPREL_G0_NC), /* type */
960 0, /* rightshift */
961 2, /* size (0 = byte, 1 = short, 2 = long) */
962 16, /* bitsize */
963 FALSE, /* pc_relative */
964 0, /* bitpos */
965 complain_overflow_dont, /* complain_on_overflow */
966 bfd_elf_generic_reloc, /* special_function */
967 AARCH64_R_STR (TLSIE_MOVW_GOTTPREL_G0_NC), /* name */
968 FALSE, /* partial_inplace */
969 0xffff, /* src_mask */
970 0xffff, /* dst_mask */
971 FALSE), /* pcrel_offset */
972
973 HOWTO (AARCH64_R (TLSIE_ADR_GOTTPREL_PAGE21), /* type */
974 12, /* rightshift */
975 2, /* size (0 = byte, 1 = short, 2 = long) */
976 21, /* bitsize */
977 FALSE, /* pc_relative */
978 0, /* bitpos */
979 complain_overflow_dont, /* complain_on_overflow */
980 bfd_elf_generic_reloc, /* special_function */
981 AARCH64_R_STR (TLSIE_ADR_GOTTPREL_PAGE21), /* name */
982 FALSE, /* partial_inplace */
983 0x1fffff, /* src_mask */
984 0x1fffff, /* dst_mask */
985 FALSE), /* pcrel_offset */
986
987 HOWTO64 (AARCH64_R (TLSIE_LD64_GOTTPREL_LO12_NC), /* type */
988 3, /* rightshift */
989 2, /* size (0 = byte, 1 = short, 2 = long) */
990 12, /* bitsize */
991 FALSE, /* pc_relative */
992 0, /* bitpos */
993 complain_overflow_dont, /* complain_on_overflow */
994 bfd_elf_generic_reloc, /* special_function */
995 AARCH64_R_STR (TLSIE_LD64_GOTTPREL_LO12_NC), /* name */
996 FALSE, /* partial_inplace */
997 0xff8, /* src_mask */
998 0xff8, /* dst_mask */
999 FALSE), /* pcrel_offset */
1000
1001 HOWTO32 (AARCH64_R (TLSIE_LD32_GOTTPREL_LO12_NC), /* type */
1002 2, /* rightshift */
1003 2, /* size (0 = byte, 1 = short, 2 = long) */
1004 12, /* bitsize */
1005 FALSE, /* pc_relative */
1006 0, /* bitpos */
1007 complain_overflow_dont, /* complain_on_overflow */
1008 bfd_elf_generic_reloc, /* special_function */
1009 AARCH64_R_STR (TLSIE_LD32_GOTTPREL_LO12_NC), /* name */
1010 FALSE, /* partial_inplace */
1011 0xffc, /* src_mask */
1012 0xffc, /* dst_mask */
1013 FALSE), /* pcrel_offset */
1014
1015 HOWTO (AARCH64_R (TLSIE_LD_GOTTPREL_PREL19), /* type */
1016 2, /* rightshift */
1017 2, /* size (0 = byte, 1 = short, 2 = long) */
1018 19, /* bitsize */
1019 FALSE, /* pc_relative */
1020 0, /* bitpos */
1021 complain_overflow_dont, /* complain_on_overflow */
1022 bfd_elf_generic_reloc, /* special_function */
1023 AARCH64_R_STR (TLSIE_LD_GOTTPREL_PREL19), /* name */
1024 FALSE, /* partial_inplace */
1025 0x1ffffc, /* src_mask */
1026 0x1ffffc, /* dst_mask */
1027 FALSE), /* pcrel_offset */
1028
1029 /* Get to the page for the GOT entry for the symbol
1030 (G(S) - P) using an ADRP instruction. */
1031 HOWTO (AARCH64_R (TLSLD_ADR_PAGE21), /* type */
1032 12, /* rightshift */
1033 2, /* size (0 = byte, 1 = short, 2 = long) */
1034 21, /* bitsize */
1035 TRUE, /* pc_relative */
1036 0, /* bitpos */
1037 complain_overflow_signed, /* complain_on_overflow */
1038 bfd_elf_generic_reloc, /* special_function */
1039 AARCH64_R_STR (TLSLD_ADR_PAGE21), /* name */
1040 FALSE, /* partial_inplace */
1041 0x1fffff, /* src_mask */
1042 0x1fffff, /* dst_mask */
1043 TRUE), /* pcrel_offset */
1044
1045 HOWTO (AARCH64_R (TLSLD_ADR_PREL21), /* type */
1046 0, /* rightshift */
1047 2, /* size (0 = byte, 1 = short, 2 = long) */
1048 21, /* bitsize */
1049 TRUE, /* pc_relative */
1050 0, /* bitpos */
1051 complain_overflow_signed, /* complain_on_overflow */
1052 bfd_elf_generic_reloc, /* special_function */
1053 AARCH64_R_STR (TLSLD_ADR_PREL21), /* name */
1054 FALSE, /* partial_inplace */
1055 0x1fffff, /* src_mask */
1056 0x1fffff, /* dst_mask */
1057 TRUE), /* pcrel_offset */
1058
1059 HOWTO64 (AARCH64_R (TLSLE_MOVW_TPREL_G2), /* type */
1060 32, /* rightshift */
1061 2, /* size (0 = byte, 1 = short, 2 = long) */
1062 16, /* bitsize */
1063 FALSE, /* pc_relative */
1064 0, /* bitpos */
1065 complain_overflow_unsigned, /* complain_on_overflow */
1066 bfd_elf_generic_reloc, /* special_function */
1067 AARCH64_R_STR (TLSLE_MOVW_TPREL_G2), /* name */
1068 FALSE, /* partial_inplace */
1069 0xffff, /* src_mask */
1070 0xffff, /* dst_mask */
1071 FALSE), /* pcrel_offset */
1072
1073 HOWTO (AARCH64_R (TLSLE_MOVW_TPREL_G1), /* type */
1074 16, /* rightshift */
1075 2, /* size (0 = byte, 1 = short, 2 = long) */
1076 16, /* bitsize */
1077 FALSE, /* pc_relative */
1078 0, /* bitpos */
1079 complain_overflow_dont, /* complain_on_overflow */
1080 bfd_elf_generic_reloc, /* special_function */
1081 AARCH64_R_STR (TLSLE_MOVW_TPREL_G1), /* name */
1082 FALSE, /* partial_inplace */
1083 0xffff, /* src_mask */
1084 0xffff, /* dst_mask */
1085 FALSE), /* pcrel_offset */
1086
1087 HOWTO64 (AARCH64_R (TLSLE_MOVW_TPREL_G1_NC), /* type */
1088 16, /* rightshift */
1089 2, /* size (0 = byte, 1 = short, 2 = long) */
1090 16, /* bitsize */
1091 FALSE, /* pc_relative */
1092 0, /* bitpos */
1093 complain_overflow_dont, /* complain_on_overflow */
1094 bfd_elf_generic_reloc, /* special_function */
1095 AARCH64_R_STR (TLSLE_MOVW_TPREL_G1_NC), /* name */
1096 FALSE, /* partial_inplace */
1097 0xffff, /* src_mask */
1098 0xffff, /* dst_mask */
1099 FALSE), /* pcrel_offset */
1100
1101 HOWTO (AARCH64_R (TLSLE_MOVW_TPREL_G0), /* type */
1102 0, /* rightshift */
1103 2, /* size (0 = byte, 1 = short, 2 = long) */
1104 16, /* bitsize */
1105 FALSE, /* pc_relative */
1106 0, /* bitpos */
1107 complain_overflow_dont, /* complain_on_overflow */
1108 bfd_elf_generic_reloc, /* special_function */
1109 AARCH64_R_STR (TLSLE_MOVW_TPREL_G0), /* name */
1110 FALSE, /* partial_inplace */
1111 0xffff, /* src_mask */
1112 0xffff, /* dst_mask */
1113 FALSE), /* pcrel_offset */
1114
1115 HOWTO (AARCH64_R (TLSLE_MOVW_TPREL_G0_NC), /* type */
1116 0, /* rightshift */
1117 2, /* size (0 = byte, 1 = short, 2 = long) */
1118 16, /* bitsize */
1119 FALSE, /* pc_relative */
1120 0, /* bitpos */
1121 complain_overflow_dont, /* complain_on_overflow */
1122 bfd_elf_generic_reloc, /* special_function */
1123 AARCH64_R_STR (TLSLE_MOVW_TPREL_G0_NC), /* name */
1124 FALSE, /* partial_inplace */
1125 0xffff, /* src_mask */
1126 0xffff, /* dst_mask */
1127 FALSE), /* pcrel_offset */
1128
1129 HOWTO (AARCH64_R (TLSLE_ADD_TPREL_HI12), /* type */
1130 12, /* rightshift */
1131 2, /* size (0 = byte, 1 = short, 2 = long) */
1132 12, /* bitsize */
1133 FALSE, /* pc_relative */
1134 0, /* bitpos */
1135 complain_overflow_unsigned, /* complain_on_overflow */
1136 bfd_elf_generic_reloc, /* special_function */
1137 AARCH64_R_STR (TLSLE_ADD_TPREL_HI12), /* name */
1138 FALSE, /* partial_inplace */
1139 0xfff, /* src_mask */
1140 0xfff, /* dst_mask */
1141 FALSE), /* pcrel_offset */
1142
1143 HOWTO (AARCH64_R (TLSLE_ADD_TPREL_LO12), /* type */
1144 0, /* rightshift */
1145 2, /* size (0 = byte, 1 = short, 2 = long) */
1146 12, /* bitsize */
1147 FALSE, /* pc_relative */
1148 0, /* bitpos */
1149 complain_overflow_unsigned, /* complain_on_overflow */
1150 bfd_elf_generic_reloc, /* special_function */
1151 AARCH64_R_STR (TLSLE_ADD_TPREL_LO12), /* name */
1152 FALSE, /* partial_inplace */
1153 0xfff, /* src_mask */
1154 0xfff, /* dst_mask */
1155 FALSE), /* pcrel_offset */
1156
1157 HOWTO (AARCH64_R (TLSLE_ADD_TPREL_LO12_NC), /* type */
1158 0, /* rightshift */
1159 2, /* size (0 = byte, 1 = short, 2 = long) */
1160 12, /* bitsize */
1161 FALSE, /* pc_relative */
1162 0, /* bitpos */
1163 complain_overflow_dont, /* complain_on_overflow */
1164 bfd_elf_generic_reloc, /* special_function */
1165 AARCH64_R_STR (TLSLE_ADD_TPREL_LO12_NC), /* name */
1166 FALSE, /* partial_inplace */
1167 0xfff, /* src_mask */
1168 0xfff, /* dst_mask */
1169 FALSE), /* pcrel_offset */
1170
1171 HOWTO (AARCH64_R (TLSDESC_LD_PREL19), /* type */
1172 2, /* rightshift */
1173 2, /* size (0 = byte, 1 = short, 2 = long) */
1174 19, /* bitsize */
1175 TRUE, /* pc_relative */
1176 0, /* bitpos */
1177 complain_overflow_dont, /* complain_on_overflow */
1178 bfd_elf_generic_reloc, /* special_function */
1179 AARCH64_R_STR (TLSDESC_LD_PREL19), /* name */
1180 FALSE, /* partial_inplace */
1181 0x0ffffe0, /* src_mask */
1182 0x0ffffe0, /* dst_mask */
1183 TRUE), /* pcrel_offset */
1184
1185 HOWTO (AARCH64_R (TLSDESC_ADR_PREL21), /* type */
1186 0, /* rightshift */
1187 2, /* size (0 = byte, 1 = short, 2 = long) */
1188 21, /* bitsize */
1189 TRUE, /* pc_relative */
1190 0, /* bitpos */
1191 complain_overflow_dont, /* complain_on_overflow */
1192 bfd_elf_generic_reloc, /* special_function */
1193 AARCH64_R_STR (TLSDESC_ADR_PREL21), /* name */
1194 FALSE, /* partial_inplace */
1195 0x1fffff, /* src_mask */
1196 0x1fffff, /* dst_mask */
1197 TRUE), /* pcrel_offset */
1198
1199 /* Get to the page for the GOT entry for the symbol
1200 (G(S) - P) using an ADRP instruction. */
1201 HOWTO (AARCH64_R (TLSDESC_ADR_PAGE21), /* type */
1202 12, /* rightshift */
1203 2, /* size (0 = byte, 1 = short, 2 = long) */
1204 21, /* bitsize */
1205 TRUE, /* pc_relative */
1206 0, /* bitpos */
1207 complain_overflow_dont, /* complain_on_overflow */
1208 bfd_elf_generic_reloc, /* special_function */
1209 AARCH64_R_STR (TLSDESC_ADR_PAGE21), /* name */
1210 FALSE, /* partial_inplace */
1211 0x1fffff, /* src_mask */
1212 0x1fffff, /* dst_mask */
1213 TRUE), /* pcrel_offset */
1214
1215 /* LD64: GOT offset G(S) & 0xff8. */
1216 HOWTO64 (AARCH64_R (TLSDESC_LD64_LO12_NC), /* type */
1217 3, /* rightshift */
1218 2, /* size (0 = byte, 1 = short, 2 = long) */
1219 12, /* bitsize */
1220 FALSE, /* pc_relative */
1221 0, /* bitpos */
1222 complain_overflow_dont, /* complain_on_overflow */
1223 bfd_elf_generic_reloc, /* special_function */
1224 AARCH64_R_STR (TLSDESC_LD64_LO12_NC), /* name */
1225 FALSE, /* partial_inplace */
1226 0xff8, /* src_mask */
1227 0xff8, /* dst_mask */
1228 FALSE), /* pcrel_offset */
1229
1230 /* LD32: GOT offset G(S) & 0xffc. */
1231 HOWTO32 (AARCH64_R (TLSDESC_LD32_LO12_NC), /* type */
1232 2, /* rightshift */
1233 2, /* size (0 = byte, 1 = short, 2 = long) */
1234 12, /* bitsize */
1235 FALSE, /* pc_relative */
1236 0, /* bitpos */
1237 complain_overflow_dont, /* complain_on_overflow */
1238 bfd_elf_generic_reloc, /* special_function */
1239 AARCH64_R_STR (TLSDESC_LD32_LO12_NC), /* name */
1240 FALSE, /* partial_inplace */
1241 0xffc, /* src_mask */
1242 0xffc, /* dst_mask */
1243 FALSE), /* pcrel_offset */
1244
1245 /* ADD: GOT offset G(S) & 0xfff. */
1246 HOWTO (AARCH64_R (TLSDESC_ADD_LO12_NC), /* type */
1247 0, /* rightshift */
1248 2, /* size (0 = byte, 1 = short, 2 = long) */
1249 12, /* bitsize */
1250 FALSE, /* pc_relative */
1251 0, /* bitpos */
1252 complain_overflow_dont, /* complain_on_overflow */
1253 bfd_elf_generic_reloc, /* special_function */
1254 AARCH64_R_STR (TLSDESC_ADD_LO12_NC), /* name */
1255 FALSE, /* partial_inplace */
1256 0xfff, /* src_mask */
1257 0xfff, /* dst_mask */
1258 FALSE), /* pcrel_offset */
1259
1260 HOWTO64 (AARCH64_R (TLSDESC_OFF_G1), /* type */
1261 16, /* rightshift */
1262 2, /* size (0 = byte, 1 = short, 2 = long) */
1263 12, /* bitsize */
1264 FALSE, /* pc_relative */
1265 0, /* bitpos */
1266 complain_overflow_dont, /* complain_on_overflow */
1267 bfd_elf_generic_reloc, /* special_function */
1268 AARCH64_R_STR (TLSDESC_OFF_G1), /* name */
1269 FALSE, /* partial_inplace */
1270 0xffff, /* src_mask */
1271 0xffff, /* dst_mask */
1272 FALSE), /* pcrel_offset */
1273
1274 HOWTO64 (AARCH64_R (TLSDESC_OFF_G0_NC), /* type */
1275 0, /* rightshift */
1276 2, /* size (0 = byte, 1 = short, 2 = long) */
1277 12, /* bitsize */
1278 FALSE, /* pc_relative */
1279 0, /* bitpos */
1280 complain_overflow_dont, /* complain_on_overflow */
1281 bfd_elf_generic_reloc, /* special_function */
1282 AARCH64_R_STR (TLSDESC_OFF_G0_NC), /* name */
1283 FALSE, /* partial_inplace */
1284 0xffff, /* src_mask */
1285 0xffff, /* dst_mask */
1286 FALSE), /* pcrel_offset */
1287
1288 HOWTO64 (AARCH64_R (TLSDESC_LDR), /* type */
1289 0, /* rightshift */
1290 2, /* size (0 = byte, 1 = short, 2 = long) */
1291 12, /* bitsize */
1292 FALSE, /* pc_relative */
1293 0, /* bitpos */
1294 complain_overflow_dont, /* complain_on_overflow */
1295 bfd_elf_generic_reloc, /* special_function */
1296 AARCH64_R_STR (TLSDESC_LDR), /* name */
1297 FALSE, /* partial_inplace */
1298 0x0, /* src_mask */
1299 0x0, /* dst_mask */
1300 FALSE), /* pcrel_offset */
1301
1302 HOWTO64 (AARCH64_R (TLSDESC_ADD), /* type */
1303 0, /* rightshift */
1304 2, /* size (0 = byte, 1 = short, 2 = long) */
1305 12, /* bitsize */
1306 FALSE, /* pc_relative */
1307 0, /* bitpos */
1308 complain_overflow_dont, /* complain_on_overflow */
1309 bfd_elf_generic_reloc, /* special_function */
1310 AARCH64_R_STR (TLSDESC_ADD), /* name */
1311 FALSE, /* partial_inplace */
1312 0x0, /* src_mask */
1313 0x0, /* dst_mask */
1314 FALSE), /* pcrel_offset */
1315
1316 HOWTO (AARCH64_R (TLSDESC_CALL), /* type */
1317 0, /* rightshift */
1318 2, /* size (0 = byte, 1 = short, 2 = long) */
1319 0, /* bitsize */
1320 FALSE, /* pc_relative */
1321 0, /* bitpos */
1322 complain_overflow_dont, /* complain_on_overflow */
1323 bfd_elf_generic_reloc, /* special_function */
1324 AARCH64_R_STR (TLSDESC_CALL), /* name */
1325 FALSE, /* partial_inplace */
1326 0x0, /* src_mask */
1327 0x0, /* dst_mask */
1328 FALSE), /* pcrel_offset */
1329
1330 HOWTO (AARCH64_R (COPY), /* type */
1331 0, /* rightshift */
1332 2, /* size (0 = byte, 1 = short, 2 = long) */
1333 64, /* bitsize */
1334 FALSE, /* pc_relative */
1335 0, /* bitpos */
1336 complain_overflow_bitfield, /* complain_on_overflow */
1337 bfd_elf_generic_reloc, /* special_function */
1338 AARCH64_R_STR (COPY), /* name */
1339 TRUE, /* partial_inplace */
1340 0xffffffff, /* src_mask */
1341 0xffffffff, /* dst_mask */
1342 FALSE), /* pcrel_offset */
1343
1344 HOWTO (AARCH64_R (GLOB_DAT), /* type */
1345 0, /* rightshift */
1346 2, /* size (0 = byte, 1 = short, 2 = long) */
1347 64, /* bitsize */
1348 FALSE, /* pc_relative */
1349 0, /* bitpos */
1350 complain_overflow_bitfield, /* complain_on_overflow */
1351 bfd_elf_generic_reloc, /* special_function */
1352 AARCH64_R_STR (GLOB_DAT), /* name */
1353 TRUE, /* partial_inplace */
1354 0xffffffff, /* src_mask */
1355 0xffffffff, /* dst_mask */
1356 FALSE), /* pcrel_offset */
1357
1358 HOWTO (AARCH64_R (JUMP_SLOT), /* type */
1359 0, /* rightshift */
1360 2, /* size (0 = byte, 1 = short, 2 = long) */
1361 64, /* bitsize */
1362 FALSE, /* pc_relative */
1363 0, /* bitpos */
1364 complain_overflow_bitfield, /* complain_on_overflow */
1365 bfd_elf_generic_reloc, /* special_function */
1366 AARCH64_R_STR (JUMP_SLOT), /* name */
1367 TRUE, /* partial_inplace */
1368 0xffffffff, /* src_mask */
1369 0xffffffff, /* dst_mask */
1370 FALSE), /* pcrel_offset */
1371
1372 HOWTO (AARCH64_R (RELATIVE), /* type */
1373 0, /* rightshift */
1374 2, /* size (0 = byte, 1 = short, 2 = long) */
1375 64, /* bitsize */
1376 FALSE, /* pc_relative */
1377 0, /* bitpos */
1378 complain_overflow_bitfield, /* complain_on_overflow */
1379 bfd_elf_generic_reloc, /* special_function */
1380 AARCH64_R_STR (RELATIVE), /* name */
1381 TRUE, /* partial_inplace */
1382 ALL_ONES, /* src_mask */
1383 ALL_ONES, /* dst_mask */
1384 FALSE), /* pcrel_offset */
1385
1386 HOWTO (AARCH64_R (TLS_DTPMOD), /* type */
1387 0, /* rightshift */
1388 2, /* size (0 = byte, 1 = short, 2 = long) */
1389 64, /* bitsize */
1390 FALSE, /* pc_relative */
1391 0, /* bitpos */
1392 complain_overflow_dont, /* complain_on_overflow */
1393 bfd_elf_generic_reloc, /* special_function */
1394 #if ARCH_SIZE == 64
1395 AARCH64_R_STR (TLS_DTPMOD64), /* name */
1396 #else
1397 AARCH64_R_STR (TLS_DTPMOD), /* name */
1398 #endif
1399 FALSE, /* partial_inplace */
1400 0, /* src_mask */
1401 ALL_ONES, /* dst_mask */
1402 FALSE), /* pc_reloffset */
1403
1404 HOWTO (AARCH64_R (TLS_DTPREL), /* type */
1405 0, /* rightshift */
1406 2, /* size (0 = byte, 1 = short, 2 = long) */
1407 64, /* bitsize */
1408 FALSE, /* pc_relative */
1409 0, /* bitpos */
1410 complain_overflow_dont, /* complain_on_overflow */
1411 bfd_elf_generic_reloc, /* special_function */
1412 #if ARCH_SIZE == 64
1413 AARCH64_R_STR (TLS_DTPREL64), /* name */
1414 #else
1415 AARCH64_R_STR (TLS_DTPREL), /* name */
1416 #endif
1417 FALSE, /* partial_inplace */
1418 0, /* src_mask */
1419 ALL_ONES, /* dst_mask */
1420 FALSE), /* pcrel_offset */
1421
1422 HOWTO (AARCH64_R (TLS_TPREL), /* type */
1423 0, /* rightshift */
1424 2, /* size (0 = byte, 1 = short, 2 = long) */
1425 64, /* bitsize */
1426 FALSE, /* pc_relative */
1427 0, /* bitpos */
1428 complain_overflow_dont, /* complain_on_overflow */
1429 bfd_elf_generic_reloc, /* special_function */
1430 #if ARCH_SIZE == 64
1431 AARCH64_R_STR (TLS_TPREL64), /* name */
1432 #else
1433 AARCH64_R_STR (TLS_TPREL), /* name */
1434 #endif
1435 FALSE, /* partial_inplace */
1436 0, /* src_mask */
1437 ALL_ONES, /* dst_mask */
1438 FALSE), /* pcrel_offset */
1439
1440 HOWTO (AARCH64_R (TLSDESC), /* type */
1441 0, /* rightshift */
1442 2, /* size (0 = byte, 1 = short, 2 = long) */
1443 64, /* bitsize */
1444 FALSE, /* pc_relative */
1445 0, /* bitpos */
1446 complain_overflow_dont, /* complain_on_overflow */
1447 bfd_elf_generic_reloc, /* special_function */
1448 AARCH64_R_STR (TLSDESC), /* name */
1449 FALSE, /* partial_inplace */
1450 0, /* src_mask */
1451 ALL_ONES, /* dst_mask */
1452 FALSE), /* pcrel_offset */
1453
1454 HOWTO (AARCH64_R (IRELATIVE), /* type */
1455 0, /* rightshift */
1456 2, /* size (0 = byte, 1 = short, 2 = long) */
1457 64, /* bitsize */
1458 FALSE, /* pc_relative */
1459 0, /* bitpos */
1460 complain_overflow_bitfield, /* complain_on_overflow */
1461 bfd_elf_generic_reloc, /* special_function */
1462 AARCH64_R_STR (IRELATIVE), /* name */
1463 FALSE, /* partial_inplace */
1464 0, /* src_mask */
1465 ALL_ONES, /* dst_mask */
1466 FALSE), /* pcrel_offset */
1467
1468 EMPTY_HOWTO (0),
1469 };
1470
1471 static reloc_howto_type elfNN_aarch64_howto_none =
1472 HOWTO (R_AARCH64_NONE, /* type */
1473 0, /* rightshift */
1474 3, /* size (0 = byte, 1 = short, 2 = long) */
1475 0, /* bitsize */
1476 FALSE, /* pc_relative */
1477 0, /* bitpos */
1478 complain_overflow_dont,/* complain_on_overflow */
1479 bfd_elf_generic_reloc, /* special_function */
1480 "R_AARCH64_NONE", /* name */
1481 FALSE, /* partial_inplace */
1482 0, /* src_mask */
1483 0, /* dst_mask */
1484 FALSE); /* pcrel_offset */
1485
1486 /* Given HOWTO, return the bfd internal relocation enumerator. */
1487
1488 static bfd_reloc_code_real_type
1489 elfNN_aarch64_bfd_reloc_from_howto (reloc_howto_type *howto)
1490 {
1491 const int size
1492 = (int) ARRAY_SIZE (elfNN_aarch64_howto_table);
1493 const ptrdiff_t offset
1494 = howto - elfNN_aarch64_howto_table;
1495
1496 if (offset > 0 && offset < size - 1)
1497 return BFD_RELOC_AARCH64_RELOC_START + offset;
1498
1499 if (howto == &elfNN_aarch64_howto_none)
1500 return BFD_RELOC_AARCH64_NONE;
1501
1502 return BFD_RELOC_AARCH64_RELOC_START;
1503 }
1504
1505 /* Given R_TYPE, return the bfd internal relocation enumerator. */
1506
1507 static bfd_reloc_code_real_type
1508 elfNN_aarch64_bfd_reloc_from_type (unsigned int r_type)
1509 {
1510 static bfd_boolean initialized_p = FALSE;
1511 /* Indexed by R_TYPE, values are offsets in the howto_table. */
1512 static unsigned int offsets[R_AARCH64_end];
1513
1514 if (initialized_p == FALSE)
1515 {
1516 unsigned int i;
1517
1518 for (i = 1; i < ARRAY_SIZE (elfNN_aarch64_howto_table) - 1; ++i)
1519 if (elfNN_aarch64_howto_table[i].type != 0)
1520 offsets[elfNN_aarch64_howto_table[i].type] = i;
1521
1522 initialized_p = TRUE;
1523 }
1524
1525 if (r_type == R_AARCH64_NONE || r_type == R_AARCH64_NULL)
1526 return BFD_RELOC_AARCH64_NONE;
1527
1528 /* PR 17512: file: b371e70a. */
1529 if (r_type >= R_AARCH64_end)
1530 {
1531 _bfd_error_handler (_("Invalid AArch64 reloc number: %d"), r_type);
1532 bfd_set_error (bfd_error_bad_value);
1533 return BFD_RELOC_AARCH64_NONE;
1534 }
1535
1536 return BFD_RELOC_AARCH64_RELOC_START + offsets[r_type];
1537 }
1538
1539 struct elf_aarch64_reloc_map
1540 {
1541 bfd_reloc_code_real_type from;
1542 bfd_reloc_code_real_type to;
1543 };
1544
1545 /* Map bfd generic reloc to AArch64-specific reloc. */
1546 static const struct elf_aarch64_reloc_map elf_aarch64_reloc_map[] =
1547 {
1548 {BFD_RELOC_NONE, BFD_RELOC_AARCH64_NONE},
1549
1550 /* Basic data relocations. */
1551 {BFD_RELOC_CTOR, BFD_RELOC_AARCH64_NN},
1552 {BFD_RELOC_64, BFD_RELOC_AARCH64_64},
1553 {BFD_RELOC_32, BFD_RELOC_AARCH64_32},
1554 {BFD_RELOC_16, BFD_RELOC_AARCH64_16},
1555 {BFD_RELOC_64_PCREL, BFD_RELOC_AARCH64_64_PCREL},
1556 {BFD_RELOC_32_PCREL, BFD_RELOC_AARCH64_32_PCREL},
1557 {BFD_RELOC_16_PCREL, BFD_RELOC_AARCH64_16_PCREL},
1558 };
1559
1560 /* Given the bfd internal relocation enumerator in CODE, return the
1561 corresponding howto entry. */
1562
1563 static reloc_howto_type *
1564 elfNN_aarch64_howto_from_bfd_reloc (bfd_reloc_code_real_type code)
1565 {
1566 unsigned int i;
1567
1568 /* Convert bfd generic reloc to AArch64-specific reloc. */
1569 if (code < BFD_RELOC_AARCH64_RELOC_START
1570 || code > BFD_RELOC_AARCH64_RELOC_END)
1571 for (i = 0; i < ARRAY_SIZE (elf_aarch64_reloc_map); i++)
1572 if (elf_aarch64_reloc_map[i].from == code)
1573 {
1574 code = elf_aarch64_reloc_map[i].to;
1575 break;
1576 }
1577
1578 if (code > BFD_RELOC_AARCH64_RELOC_START
1579 && code < BFD_RELOC_AARCH64_RELOC_END)
1580 if (elfNN_aarch64_howto_table[code - BFD_RELOC_AARCH64_RELOC_START].type)
1581 return &elfNN_aarch64_howto_table[code - BFD_RELOC_AARCH64_RELOC_START];
1582
1583 if (code == BFD_RELOC_AARCH64_NONE)
1584 return &elfNN_aarch64_howto_none;
1585
1586 return NULL;
1587 }
1588
1589 static reloc_howto_type *
1590 elfNN_aarch64_howto_from_type (unsigned int r_type)
1591 {
1592 bfd_reloc_code_real_type val;
1593 reloc_howto_type *howto;
1594
1595 #if ARCH_SIZE == 32
1596 if (r_type > 256)
1597 {
1598 bfd_set_error (bfd_error_bad_value);
1599 return NULL;
1600 }
1601 #endif
1602
1603 if (r_type == R_AARCH64_NONE)
1604 return &elfNN_aarch64_howto_none;
1605
1606 val = elfNN_aarch64_bfd_reloc_from_type (r_type);
1607 howto = elfNN_aarch64_howto_from_bfd_reloc (val);
1608
1609 if (howto != NULL)
1610 return howto;
1611
1612 bfd_set_error (bfd_error_bad_value);
1613 return NULL;
1614 }
1615
1616 static void
1617 elfNN_aarch64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc,
1618 Elf_Internal_Rela *elf_reloc)
1619 {
1620 unsigned int r_type;
1621
1622 r_type = ELFNN_R_TYPE (elf_reloc->r_info);
1623 bfd_reloc->howto = elfNN_aarch64_howto_from_type (r_type);
1624 }
1625
1626 static reloc_howto_type *
1627 elfNN_aarch64_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1628 bfd_reloc_code_real_type code)
1629 {
1630 reloc_howto_type *howto = elfNN_aarch64_howto_from_bfd_reloc (code);
1631
1632 if (howto != NULL)
1633 return howto;
1634
1635 bfd_set_error (bfd_error_bad_value);
1636 return NULL;
1637 }
1638
1639 static reloc_howto_type *
1640 elfNN_aarch64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1641 const char *r_name)
1642 {
1643 unsigned int i;
1644
1645 for (i = 1; i < ARRAY_SIZE (elfNN_aarch64_howto_table) - 1; ++i)
1646 if (elfNN_aarch64_howto_table[i].name != NULL
1647 && strcasecmp (elfNN_aarch64_howto_table[i].name, r_name) == 0)
1648 return &elfNN_aarch64_howto_table[i];
1649
1650 return NULL;
1651 }
1652
1653 #define TARGET_LITTLE_SYM aarch64_elfNN_le_vec
1654 #define TARGET_LITTLE_NAME "elfNN-littleaarch64"
1655 #define TARGET_BIG_SYM aarch64_elfNN_be_vec
1656 #define TARGET_BIG_NAME "elfNN-bigaarch64"
1657
1658 /* The linker script knows the section names for placement.
1659 The entry_names are used to do simple name mangling on the stubs.
1660 Given a function name, and its type, the stub can be found. The
1661 name can be changed. The only requirement is the %s be present. */
1662 #define STUB_ENTRY_NAME "__%s_veneer"
1663
1664 /* The name of the dynamic interpreter. This is put in the .interp
1665 section. */
1666 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
1667
1668 #define AARCH64_MAX_FWD_BRANCH_OFFSET \
1669 (((1 << 25) - 1) << 2)
1670 #define AARCH64_MAX_BWD_BRANCH_OFFSET \
1671 (-((1 << 25) << 2))
1672
1673 #define AARCH64_MAX_ADRP_IMM ((1 << 20) - 1)
1674 #define AARCH64_MIN_ADRP_IMM (-(1 << 20))
1675
1676 static int
1677 aarch64_valid_for_adrp_p (bfd_vma value, bfd_vma place)
1678 {
1679 bfd_signed_vma offset = (bfd_signed_vma) (PG (value) - PG (place)) >> 12;
1680 return offset <= AARCH64_MAX_ADRP_IMM && offset >= AARCH64_MIN_ADRP_IMM;
1681 }
1682
1683 static int
1684 aarch64_valid_branch_p (bfd_vma value, bfd_vma place)
1685 {
1686 bfd_signed_vma offset = (bfd_signed_vma) (value - place);
1687 return (offset <= AARCH64_MAX_FWD_BRANCH_OFFSET
1688 && offset >= AARCH64_MAX_BWD_BRANCH_OFFSET);
1689 }
1690
1691 static const uint32_t aarch64_adrp_branch_stub [] =
1692 {
1693 0x90000010, /* adrp ip0, X */
1694 /* R_AARCH64_ADR_HI21_PCREL(X) */
1695 0x91000210, /* add ip0, ip0, :lo12:X */
1696 /* R_AARCH64_ADD_ABS_LO12_NC(X) */
1697 0xd61f0200, /* br ip0 */
1698 };
1699
1700 static const uint32_t aarch64_long_branch_stub[] =
1701 {
1702 #if ARCH_SIZE == 64
1703 0x58000090, /* ldr ip0, 1f */
1704 #else
1705 0x18000090, /* ldr wip0, 1f */
1706 #endif
1707 0x10000011, /* adr ip1, #0 */
1708 0x8b110210, /* add ip0, ip0, ip1 */
1709 0xd61f0200, /* br ip0 */
1710 0x00000000, /* 1: .xword or .word
1711 R_AARCH64_PRELNN(X) + 12
1712 */
1713 0x00000000,
1714 };
1715
1716 static const uint32_t aarch64_erratum_835769_stub[] =
1717 {
1718 0x00000000, /* Placeholder for multiply accumulate. */
1719 0x14000000, /* b <label> */
1720 };
1721
1722 static const uint32_t aarch64_erratum_843419_stub[] =
1723 {
1724 0x00000000, /* Placeholder for LDR instruction. */
1725 0x14000000, /* b <label> */
1726 };
1727
1728 /* Section name for stubs is the associated section name plus this
1729 string. */
1730 #define STUB_SUFFIX ".stub"
1731
1732 enum elf_aarch64_stub_type
1733 {
1734 aarch64_stub_none,
1735 aarch64_stub_adrp_branch,
1736 aarch64_stub_long_branch,
1737 aarch64_stub_erratum_835769_veneer,
1738 aarch64_stub_erratum_843419_veneer,
1739 };
1740
1741 struct elf_aarch64_stub_hash_entry
1742 {
1743 /* Base hash table entry structure. */
1744 struct bfd_hash_entry root;
1745
1746 /* The stub section. */
1747 asection *stub_sec;
1748
1749 /* Offset within stub_sec of the beginning of this stub. */
1750 bfd_vma stub_offset;
1751
1752 /* Given the symbol's value and its section we can determine its final
1753 value when building the stubs (so the stub knows where to jump). */
1754 bfd_vma target_value;
1755 asection *target_section;
1756
1757 enum elf_aarch64_stub_type stub_type;
1758
1759 /* The symbol table entry, if any, that this was derived from. */
1760 struct elf_aarch64_link_hash_entry *h;
1761
1762 /* Destination symbol type */
1763 unsigned char st_type;
1764
1765 /* Where this stub is being called from, or, in the case of combined
1766 stub sections, the first input section in the group. */
1767 asection *id_sec;
1768
1769 /* The name for the local symbol at the start of this stub. The
1770 stub name in the hash table has to be unique; this does not, so
1771 it can be friendlier. */
1772 char *output_name;
1773
1774 /* The instruction which caused this stub to be generated (only valid for
1775 erratum 835769 workaround stubs at present). */
1776 uint32_t veneered_insn;
1777
1778 /* In an erratum 843419 workaround stub, the ADRP instruction offset. */
1779 bfd_vma adrp_offset;
1780 };
1781
1782 /* Used to build a map of a section. This is required for mixed-endian
1783 code/data. */
1784
1785 typedef struct elf_elf_section_map
1786 {
1787 bfd_vma vma;
1788 char type;
1789 }
1790 elf_aarch64_section_map;
1791
1792
1793 typedef struct _aarch64_elf_section_data
1794 {
1795 struct bfd_elf_section_data elf;
1796 unsigned int mapcount;
1797 unsigned int mapsize;
1798 elf_aarch64_section_map *map;
1799 }
1800 _aarch64_elf_section_data;
1801
1802 #define elf_aarch64_section_data(sec) \
1803 ((_aarch64_elf_section_data *) elf_section_data (sec))
1804
1805 /* The size of the thread control block which is defined to be two pointers. */
1806 #define TCB_SIZE (ARCH_SIZE/8)*2
1807
1808 struct elf_aarch64_local_symbol
1809 {
1810 unsigned int got_type;
1811 bfd_signed_vma got_refcount;
1812 bfd_vma got_offset;
1813
1814 /* Offset of the GOTPLT entry reserved for the TLS descriptor. The
1815 offset is from the end of the jump table and reserved entries
1816 within the PLTGOT.
1817
1818 The magic value (bfd_vma) -1 indicates that an offset has not be
1819 allocated. */
1820 bfd_vma tlsdesc_got_jump_table_offset;
1821 };
1822
1823 struct elf_aarch64_obj_tdata
1824 {
1825 struct elf_obj_tdata root;
1826
1827 /* local symbol descriptors */
1828 struct elf_aarch64_local_symbol *locals;
1829
1830 /* Zero to warn when linking objects with incompatible enum sizes. */
1831 int no_enum_size_warning;
1832
1833 /* Zero to warn when linking objects with incompatible wchar_t sizes. */
1834 int no_wchar_size_warning;
1835 };
1836
1837 #define elf_aarch64_tdata(bfd) \
1838 ((struct elf_aarch64_obj_tdata *) (bfd)->tdata.any)
1839
1840 #define elf_aarch64_locals(bfd) (elf_aarch64_tdata (bfd)->locals)
1841
1842 #define is_aarch64_elf(bfd) \
1843 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1844 && elf_tdata (bfd) != NULL \
1845 && elf_object_id (bfd) == AARCH64_ELF_DATA)
1846
1847 static bfd_boolean
1848 elfNN_aarch64_mkobject (bfd *abfd)
1849 {
1850 return bfd_elf_allocate_object (abfd, sizeof (struct elf_aarch64_obj_tdata),
1851 AARCH64_ELF_DATA);
1852 }
1853
1854 #define elf_aarch64_hash_entry(ent) \
1855 ((struct elf_aarch64_link_hash_entry *)(ent))
1856
1857 #define GOT_UNKNOWN 0
1858 #define GOT_NORMAL 1
1859 #define GOT_TLS_GD 2
1860 #define GOT_TLS_IE 4
1861 #define GOT_TLSDESC_GD 8
1862
1863 #define GOT_TLS_GD_ANY_P(type) ((type & GOT_TLS_GD) || (type & GOT_TLSDESC_GD))
1864
1865 /* AArch64 ELF linker hash entry. */
1866 struct elf_aarch64_link_hash_entry
1867 {
1868 struct elf_link_hash_entry root;
1869
1870 /* Track dynamic relocs copied for this symbol. */
1871 struct elf_dyn_relocs *dyn_relocs;
1872
1873 /* Since PLT entries have variable size, we need to record the
1874 index into .got.plt instead of recomputing it from the PLT
1875 offset. */
1876 bfd_signed_vma plt_got_offset;
1877
1878 /* Bit mask representing the type of GOT entry(s) if any required by
1879 this symbol. */
1880 unsigned int got_type;
1881
1882 /* A pointer to the most recently used stub hash entry against this
1883 symbol. */
1884 struct elf_aarch64_stub_hash_entry *stub_cache;
1885
1886 /* Offset of the GOTPLT entry reserved for the TLS descriptor. The offset
1887 is from the end of the jump table and reserved entries within the PLTGOT.
1888
1889 The magic value (bfd_vma) -1 indicates that an offset has not
1890 be allocated. */
1891 bfd_vma tlsdesc_got_jump_table_offset;
1892 };
1893
1894 static unsigned int
1895 elfNN_aarch64_symbol_got_type (struct elf_link_hash_entry *h,
1896 bfd *abfd,
1897 unsigned long r_symndx)
1898 {
1899 if (h)
1900 return elf_aarch64_hash_entry (h)->got_type;
1901
1902 if (! elf_aarch64_locals (abfd))
1903 return GOT_UNKNOWN;
1904
1905 return elf_aarch64_locals (abfd)[r_symndx].got_type;
1906 }
1907
1908 /* Get the AArch64 elf linker hash table from a link_info structure. */
1909 #define elf_aarch64_hash_table(info) \
1910 ((struct elf_aarch64_link_hash_table *) ((info)->hash))
1911
1912 #define aarch64_stub_hash_lookup(table, string, create, copy) \
1913 ((struct elf_aarch64_stub_hash_entry *) \
1914 bfd_hash_lookup ((table), (string), (create), (copy)))
1915
1916 /* AArch64 ELF linker hash table. */
1917 struct elf_aarch64_link_hash_table
1918 {
1919 /* The main hash table. */
1920 struct elf_link_hash_table root;
1921
1922 /* Nonzero to force PIC branch veneers. */
1923 int pic_veneer;
1924
1925 /* Fix erratum 835769. */
1926 int fix_erratum_835769;
1927
1928 /* Fix erratum 843419. */
1929 int fix_erratum_843419;
1930
1931 /* Enable ADRP->ADR rewrite for erratum 843419 workaround. */
1932 int fix_erratum_843419_adr;
1933
1934 /* The number of bytes in the initial entry in the PLT. */
1935 bfd_size_type plt_header_size;
1936
1937 /* The number of bytes in the subsequent PLT etries. */
1938 bfd_size_type plt_entry_size;
1939
1940 /* Short-cuts to get to dynamic linker sections. */
1941 asection *sdynbss;
1942 asection *srelbss;
1943
1944 /* Small local sym cache. */
1945 struct sym_cache sym_cache;
1946
1947 /* For convenience in allocate_dynrelocs. */
1948 bfd *obfd;
1949
1950 /* The amount of space used by the reserved portion of the sgotplt
1951 section, plus whatever space is used by the jump slots. */
1952 bfd_vma sgotplt_jump_table_size;
1953
1954 /* The stub hash table. */
1955 struct bfd_hash_table stub_hash_table;
1956
1957 /* Linker stub bfd. */
1958 bfd *stub_bfd;
1959
1960 /* Linker call-backs. */
1961 asection *(*add_stub_section) (const char *, asection *);
1962 void (*layout_sections_again) (void);
1963
1964 /* Array to keep track of which stub sections have been created, and
1965 information on stub grouping. */
1966 struct map_stub
1967 {
1968 /* This is the section to which stubs in the group will be
1969 attached. */
1970 asection *link_sec;
1971 /* The stub section. */
1972 asection *stub_sec;
1973 } *stub_group;
1974
1975 /* Assorted information used by elfNN_aarch64_size_stubs. */
1976 unsigned int bfd_count;
1977 int top_index;
1978 asection **input_list;
1979
1980 /* The offset into splt of the PLT entry for the TLS descriptor
1981 resolver. Special values are 0, if not necessary (or not found
1982 to be necessary yet), and -1 if needed but not determined
1983 yet. */
1984 bfd_vma tlsdesc_plt;
1985
1986 /* The GOT offset for the lazy trampoline. Communicated to the
1987 loader via DT_TLSDESC_GOT. The magic value (bfd_vma) -1
1988 indicates an offset is not allocated. */
1989 bfd_vma dt_tlsdesc_got;
1990
1991 /* Used by local STT_GNU_IFUNC symbols. */
1992 htab_t loc_hash_table;
1993 void * loc_hash_memory;
1994 };
1995
1996 /* Create an entry in an AArch64 ELF linker hash table. */
1997
1998 static struct bfd_hash_entry *
1999 elfNN_aarch64_link_hash_newfunc (struct bfd_hash_entry *entry,
2000 struct bfd_hash_table *table,
2001 const char *string)
2002 {
2003 struct elf_aarch64_link_hash_entry *ret =
2004 (struct elf_aarch64_link_hash_entry *) entry;
2005
2006 /* Allocate the structure if it has not already been allocated by a
2007 subclass. */
2008 if (ret == NULL)
2009 ret = bfd_hash_allocate (table,
2010 sizeof (struct elf_aarch64_link_hash_entry));
2011 if (ret == NULL)
2012 return (struct bfd_hash_entry *) ret;
2013
2014 /* Call the allocation method of the superclass. */
2015 ret = ((struct elf_aarch64_link_hash_entry *)
2016 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2017 table, string));
2018 if (ret != NULL)
2019 {
2020 ret->dyn_relocs = NULL;
2021 ret->got_type = GOT_UNKNOWN;
2022 ret->plt_got_offset = (bfd_vma) - 1;
2023 ret->stub_cache = NULL;
2024 ret->tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
2025 }
2026
2027 return (struct bfd_hash_entry *) ret;
2028 }
2029
2030 /* Initialize an entry in the stub hash table. */
2031
2032 static struct bfd_hash_entry *
2033 stub_hash_newfunc (struct bfd_hash_entry *entry,
2034 struct bfd_hash_table *table, const char *string)
2035 {
2036 /* Allocate the structure if it has not already been allocated by a
2037 subclass. */
2038 if (entry == NULL)
2039 {
2040 entry = bfd_hash_allocate (table,
2041 sizeof (struct
2042 elf_aarch64_stub_hash_entry));
2043 if (entry == NULL)
2044 return entry;
2045 }
2046
2047 /* Call the allocation method of the superclass. */
2048 entry = bfd_hash_newfunc (entry, table, string);
2049 if (entry != NULL)
2050 {
2051 struct elf_aarch64_stub_hash_entry *eh;
2052
2053 /* Initialize the local fields. */
2054 eh = (struct elf_aarch64_stub_hash_entry *) entry;
2055 eh->adrp_offset = 0;
2056 eh->stub_sec = NULL;
2057 eh->stub_offset = 0;
2058 eh->target_value = 0;
2059 eh->target_section = NULL;
2060 eh->stub_type = aarch64_stub_none;
2061 eh->h = NULL;
2062 eh->id_sec = NULL;
2063 }
2064
2065 return entry;
2066 }
2067
2068 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
2069 for local symbol so that we can handle local STT_GNU_IFUNC symbols
2070 as global symbol. We reuse indx and dynstr_index for local symbol
2071 hash since they aren't used by global symbols in this backend. */
2072
2073 static hashval_t
2074 elfNN_aarch64_local_htab_hash (const void *ptr)
2075 {
2076 struct elf_link_hash_entry *h
2077 = (struct elf_link_hash_entry *) ptr;
2078 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
2079 }
2080
2081 /* Compare local hash entries. */
2082
2083 static int
2084 elfNN_aarch64_local_htab_eq (const void *ptr1, const void *ptr2)
2085 {
2086 struct elf_link_hash_entry *h1
2087 = (struct elf_link_hash_entry *) ptr1;
2088 struct elf_link_hash_entry *h2
2089 = (struct elf_link_hash_entry *) ptr2;
2090
2091 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
2092 }
2093
2094 /* Find and/or create a hash entry for local symbol. */
2095
2096 static struct elf_link_hash_entry *
2097 elfNN_aarch64_get_local_sym_hash (struct elf_aarch64_link_hash_table *htab,
2098 bfd *abfd, const Elf_Internal_Rela *rel,
2099 bfd_boolean create)
2100 {
2101 struct elf_aarch64_link_hash_entry e, *ret;
2102 asection *sec = abfd->sections;
2103 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
2104 ELFNN_R_SYM (rel->r_info));
2105 void **slot;
2106
2107 e.root.indx = sec->id;
2108 e.root.dynstr_index = ELFNN_R_SYM (rel->r_info);
2109 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
2110 create ? INSERT : NO_INSERT);
2111
2112 if (!slot)
2113 return NULL;
2114
2115 if (*slot)
2116 {
2117 ret = (struct elf_aarch64_link_hash_entry *) *slot;
2118 return &ret->root;
2119 }
2120
2121 ret = (struct elf_aarch64_link_hash_entry *)
2122 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
2123 sizeof (struct elf_aarch64_link_hash_entry));
2124 if (ret)
2125 {
2126 memset (ret, 0, sizeof (*ret));
2127 ret->root.indx = sec->id;
2128 ret->root.dynstr_index = ELFNN_R_SYM (rel->r_info);
2129 ret->root.dynindx = -1;
2130 *slot = ret;
2131 }
2132 return &ret->root;
2133 }
2134
2135 /* Copy the extra info we tack onto an elf_link_hash_entry. */
2136
2137 static void
2138 elfNN_aarch64_copy_indirect_symbol (struct bfd_link_info *info,
2139 struct elf_link_hash_entry *dir,
2140 struct elf_link_hash_entry *ind)
2141 {
2142 struct elf_aarch64_link_hash_entry *edir, *eind;
2143
2144 edir = (struct elf_aarch64_link_hash_entry *) dir;
2145 eind = (struct elf_aarch64_link_hash_entry *) ind;
2146
2147 if (eind->dyn_relocs != NULL)
2148 {
2149 if (edir->dyn_relocs != NULL)
2150 {
2151 struct elf_dyn_relocs **pp;
2152 struct elf_dyn_relocs *p;
2153
2154 /* Add reloc counts against the indirect sym to the direct sym
2155 list. Merge any entries against the same section. */
2156 for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
2157 {
2158 struct elf_dyn_relocs *q;
2159
2160 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2161 if (q->sec == p->sec)
2162 {
2163 q->pc_count += p->pc_count;
2164 q->count += p->count;
2165 *pp = p->next;
2166 break;
2167 }
2168 if (q == NULL)
2169 pp = &p->next;
2170 }
2171 *pp = edir->dyn_relocs;
2172 }
2173
2174 edir->dyn_relocs = eind->dyn_relocs;
2175 eind->dyn_relocs = NULL;
2176 }
2177
2178 if (ind->root.type == bfd_link_hash_indirect)
2179 {
2180 /* Copy over PLT info. */
2181 if (dir->got.refcount <= 0)
2182 {
2183 edir->got_type = eind->got_type;
2184 eind->got_type = GOT_UNKNOWN;
2185 }
2186 }
2187
2188 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2189 }
2190
2191 /* Destroy an AArch64 elf linker hash table. */
2192
2193 static void
2194 elfNN_aarch64_link_hash_table_free (bfd *obfd)
2195 {
2196 struct elf_aarch64_link_hash_table *ret
2197 = (struct elf_aarch64_link_hash_table *) obfd->link.hash;
2198
2199 if (ret->loc_hash_table)
2200 htab_delete (ret->loc_hash_table);
2201 if (ret->loc_hash_memory)
2202 objalloc_free ((struct objalloc *) ret->loc_hash_memory);
2203
2204 bfd_hash_table_free (&ret->stub_hash_table);
2205 _bfd_elf_link_hash_table_free (obfd);
2206 }
2207
2208 /* Create an AArch64 elf linker hash table. */
2209
2210 static struct bfd_link_hash_table *
2211 elfNN_aarch64_link_hash_table_create (bfd *abfd)
2212 {
2213 struct elf_aarch64_link_hash_table *ret;
2214 bfd_size_type amt = sizeof (struct elf_aarch64_link_hash_table);
2215
2216 ret = bfd_zmalloc (amt);
2217 if (ret == NULL)
2218 return NULL;
2219
2220 if (!_bfd_elf_link_hash_table_init
2221 (&ret->root, abfd, elfNN_aarch64_link_hash_newfunc,
2222 sizeof (struct elf_aarch64_link_hash_entry), AARCH64_ELF_DATA))
2223 {
2224 free (ret);
2225 return NULL;
2226 }
2227
2228 ret->plt_header_size = PLT_ENTRY_SIZE;
2229 ret->plt_entry_size = PLT_SMALL_ENTRY_SIZE;
2230 ret->obfd = abfd;
2231 ret->dt_tlsdesc_got = (bfd_vma) - 1;
2232
2233 if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
2234 sizeof (struct elf_aarch64_stub_hash_entry)))
2235 {
2236 _bfd_elf_link_hash_table_free (abfd);
2237 return NULL;
2238 }
2239
2240 ret->loc_hash_table = htab_try_create (1024,
2241 elfNN_aarch64_local_htab_hash,
2242 elfNN_aarch64_local_htab_eq,
2243 NULL);
2244 ret->loc_hash_memory = objalloc_create ();
2245 if (!ret->loc_hash_table || !ret->loc_hash_memory)
2246 {
2247 elfNN_aarch64_link_hash_table_free (abfd);
2248 return NULL;
2249 }
2250 ret->root.root.hash_table_free = elfNN_aarch64_link_hash_table_free;
2251
2252 return &ret->root.root;
2253 }
2254
2255 static bfd_boolean
2256 aarch64_relocate (unsigned int r_type, bfd *input_bfd, asection *input_section,
2257 bfd_vma offset, bfd_vma value)
2258 {
2259 reloc_howto_type *howto;
2260 bfd_vma place;
2261
2262 howto = elfNN_aarch64_howto_from_type (r_type);
2263 place = (input_section->output_section->vma + input_section->output_offset
2264 + offset);
2265
2266 r_type = elfNN_aarch64_bfd_reloc_from_type (r_type);
2267 value = _bfd_aarch64_elf_resolve_relocation (r_type, place, value, 0, FALSE);
2268 return _bfd_aarch64_elf_put_addend (input_bfd,
2269 input_section->contents + offset, r_type,
2270 howto, value);
2271 }
2272
2273 static enum elf_aarch64_stub_type
2274 aarch64_select_branch_stub (bfd_vma value, bfd_vma place)
2275 {
2276 if (aarch64_valid_for_adrp_p (value, place))
2277 return aarch64_stub_adrp_branch;
2278 return aarch64_stub_long_branch;
2279 }
2280
2281 /* Determine the type of stub needed, if any, for a call. */
2282
2283 static enum elf_aarch64_stub_type
2284 aarch64_type_of_stub (struct bfd_link_info *info,
2285 asection *input_sec,
2286 const Elf_Internal_Rela *rel,
2287 unsigned char st_type,
2288 struct elf_aarch64_link_hash_entry *hash,
2289 bfd_vma destination)
2290 {
2291 bfd_vma location;
2292 bfd_signed_vma branch_offset;
2293 unsigned int r_type;
2294 struct elf_aarch64_link_hash_table *globals;
2295 enum elf_aarch64_stub_type stub_type = aarch64_stub_none;
2296 bfd_boolean via_plt_p;
2297
2298 if (st_type != STT_FUNC)
2299 return stub_type;
2300
2301 globals = elf_aarch64_hash_table (info);
2302 via_plt_p = (globals->root.splt != NULL && hash != NULL
2303 && hash->root.plt.offset != (bfd_vma) - 1);
2304
2305 if (via_plt_p)
2306 return stub_type;
2307
2308 /* Determine where the call point is. */
2309 location = (input_sec->output_offset
2310 + input_sec->output_section->vma + rel->r_offset);
2311
2312 branch_offset = (bfd_signed_vma) (destination - location);
2313
2314 r_type = ELFNN_R_TYPE (rel->r_info);
2315
2316 /* We don't want to redirect any old unconditional jump in this way,
2317 only one which is being used for a sibcall, where it is
2318 acceptable for the IP0 and IP1 registers to be clobbered. */
2319 if ((r_type == AARCH64_R (CALL26) || r_type == AARCH64_R (JUMP26))
2320 && (branch_offset > AARCH64_MAX_FWD_BRANCH_OFFSET
2321 || branch_offset < AARCH64_MAX_BWD_BRANCH_OFFSET))
2322 {
2323 stub_type = aarch64_stub_long_branch;
2324 }
2325
2326 return stub_type;
2327 }
2328
2329 /* Build a name for an entry in the stub hash table. */
2330
2331 static char *
2332 elfNN_aarch64_stub_name (const asection *input_section,
2333 const asection *sym_sec,
2334 const struct elf_aarch64_link_hash_entry *hash,
2335 const Elf_Internal_Rela *rel)
2336 {
2337 char *stub_name;
2338 bfd_size_type len;
2339
2340 if (hash)
2341 {
2342 len = 8 + 1 + strlen (hash->root.root.root.string) + 1 + 16 + 1;
2343 stub_name = bfd_malloc (len);
2344 if (stub_name != NULL)
2345 snprintf (stub_name, len, "%08x_%s+%" BFD_VMA_FMT "x",
2346 (unsigned int) input_section->id,
2347 hash->root.root.root.string,
2348 rel->r_addend);
2349 }
2350 else
2351 {
2352 len = 8 + 1 + 8 + 1 + 8 + 1 + 16 + 1;
2353 stub_name = bfd_malloc (len);
2354 if (stub_name != NULL)
2355 snprintf (stub_name, len, "%08x_%x:%x+%" BFD_VMA_FMT "x",
2356 (unsigned int) input_section->id,
2357 (unsigned int) sym_sec->id,
2358 (unsigned int) ELFNN_R_SYM (rel->r_info),
2359 rel->r_addend);
2360 }
2361
2362 return stub_name;
2363 }
2364
2365 /* Look up an entry in the stub hash. Stub entries are cached because
2366 creating the stub name takes a bit of time. */
2367
2368 static struct elf_aarch64_stub_hash_entry *
2369 elfNN_aarch64_get_stub_entry (const asection *input_section,
2370 const asection *sym_sec,
2371 struct elf_link_hash_entry *hash,
2372 const Elf_Internal_Rela *rel,
2373 struct elf_aarch64_link_hash_table *htab)
2374 {
2375 struct elf_aarch64_stub_hash_entry *stub_entry;
2376 struct elf_aarch64_link_hash_entry *h =
2377 (struct elf_aarch64_link_hash_entry *) hash;
2378 const asection *id_sec;
2379
2380 if ((input_section->flags & SEC_CODE) == 0)
2381 return NULL;
2382
2383 /* If this input section is part of a group of sections sharing one
2384 stub section, then use the id of the first section in the group.
2385 Stub names need to include a section id, as there may well be
2386 more than one stub used to reach say, printf, and we need to
2387 distinguish between them. */
2388 id_sec = htab->stub_group[input_section->id].link_sec;
2389
2390 if (h != NULL && h->stub_cache != NULL
2391 && h->stub_cache->h == h && h->stub_cache->id_sec == id_sec)
2392 {
2393 stub_entry = h->stub_cache;
2394 }
2395 else
2396 {
2397 char *stub_name;
2398
2399 stub_name = elfNN_aarch64_stub_name (id_sec, sym_sec, h, rel);
2400 if (stub_name == NULL)
2401 return NULL;
2402
2403 stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table,
2404 stub_name, FALSE, FALSE);
2405 if (h != NULL)
2406 h->stub_cache = stub_entry;
2407
2408 free (stub_name);
2409 }
2410
2411 return stub_entry;
2412 }
2413
2414
2415 /* Create a stub section. */
2416
2417 static asection *
2418 _bfd_aarch64_create_stub_section (asection *section,
2419 struct elf_aarch64_link_hash_table *htab)
2420 {
2421 size_t namelen;
2422 bfd_size_type len;
2423 char *s_name;
2424
2425 namelen = strlen (section->name);
2426 len = namelen + sizeof (STUB_SUFFIX);
2427 s_name = bfd_alloc (htab->stub_bfd, len);
2428 if (s_name == NULL)
2429 return NULL;
2430
2431 memcpy (s_name, section->name, namelen);
2432 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
2433 return (*htab->add_stub_section) (s_name, section);
2434 }
2435
2436
2437 /* Find or create a stub section for a link section.
2438
2439 Fix or create the stub section used to collect stubs attached to
2440 the specified link section. */
2441
2442 static asection *
2443 _bfd_aarch64_get_stub_for_link_section (asection *link_section,
2444 struct elf_aarch64_link_hash_table *htab)
2445 {
2446 if (htab->stub_group[link_section->id].stub_sec == NULL)
2447 htab->stub_group[link_section->id].stub_sec
2448 = _bfd_aarch64_create_stub_section (link_section, htab);
2449 return htab->stub_group[link_section->id].stub_sec;
2450 }
2451
2452
2453 /* Find or create a stub section in the stub group for an input
2454 section. */
2455
2456 static asection *
2457 _bfd_aarch64_create_or_find_stub_sec (asection *section,
2458 struct elf_aarch64_link_hash_table *htab)
2459 {
2460 asection *link_sec = htab->stub_group[section->id].link_sec;
2461 return _bfd_aarch64_get_stub_for_link_section (link_sec, htab);
2462 }
2463
2464
2465 /* Add a new stub entry in the stub group associated with an input
2466 section to the stub hash. Not all fields of the new stub entry are
2467 initialised. */
2468
2469 static struct elf_aarch64_stub_hash_entry *
2470 _bfd_aarch64_add_stub_entry_in_group (const char *stub_name,
2471 asection *section,
2472 struct elf_aarch64_link_hash_table *htab)
2473 {
2474 asection *link_sec;
2475 asection *stub_sec;
2476 struct elf_aarch64_stub_hash_entry *stub_entry;
2477
2478 link_sec = htab->stub_group[section->id].link_sec;
2479 stub_sec = _bfd_aarch64_create_or_find_stub_sec (section, htab);
2480
2481 /* Enter this entry into the linker stub hash table. */
2482 stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
2483 TRUE, FALSE);
2484 if (stub_entry == NULL)
2485 {
2486 (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
2487 section->owner, stub_name);
2488 return NULL;
2489 }
2490
2491 stub_entry->stub_sec = stub_sec;
2492 stub_entry->stub_offset = 0;
2493 stub_entry->id_sec = link_sec;
2494
2495 return stub_entry;
2496 }
2497
2498 /* Add a new stub entry in the final stub section to the stub hash.
2499 Not all fields of the new stub entry are initialised. */
2500
2501 static struct elf_aarch64_stub_hash_entry *
2502 _bfd_aarch64_add_stub_entry_after (const char *stub_name,
2503 asection *link_section,
2504 struct elf_aarch64_link_hash_table *htab)
2505 {
2506 asection *stub_sec;
2507 struct elf_aarch64_stub_hash_entry *stub_entry;
2508
2509 stub_sec = _bfd_aarch64_get_stub_for_link_section (link_section, htab);
2510 stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
2511 TRUE, FALSE);
2512 if (stub_entry == NULL)
2513 {
2514 (*_bfd_error_handler) (_("cannot create stub entry %s"), stub_name);
2515 return NULL;
2516 }
2517
2518 stub_entry->stub_sec = stub_sec;
2519 stub_entry->stub_offset = 0;
2520 stub_entry->id_sec = link_section;
2521
2522 return stub_entry;
2523 }
2524
2525
2526 static bfd_boolean
2527 aarch64_build_one_stub (struct bfd_hash_entry *gen_entry,
2528 void *in_arg ATTRIBUTE_UNUSED)
2529 {
2530 struct elf_aarch64_stub_hash_entry *stub_entry;
2531 asection *stub_sec;
2532 bfd *stub_bfd;
2533 bfd_byte *loc;
2534 bfd_vma sym_value;
2535 bfd_vma veneered_insn_loc;
2536 bfd_vma veneer_entry_loc;
2537 bfd_signed_vma branch_offset = 0;
2538 unsigned int template_size;
2539 const uint32_t *template;
2540 unsigned int i;
2541
2542 /* Massage our args to the form they really have. */
2543 stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
2544
2545 stub_sec = stub_entry->stub_sec;
2546
2547 /* Make a note of the offset within the stubs for this entry. */
2548 stub_entry->stub_offset = stub_sec->size;
2549 loc = stub_sec->contents + stub_entry->stub_offset;
2550
2551 stub_bfd = stub_sec->owner;
2552
2553 /* This is the address of the stub destination. */
2554 sym_value = (stub_entry->target_value
2555 + stub_entry->target_section->output_offset
2556 + stub_entry->target_section->output_section->vma);
2557
2558 if (stub_entry->stub_type == aarch64_stub_long_branch)
2559 {
2560 bfd_vma place = (stub_entry->stub_offset + stub_sec->output_section->vma
2561 + stub_sec->output_offset);
2562
2563 /* See if we can relax the stub. */
2564 if (aarch64_valid_for_adrp_p (sym_value, place))
2565 stub_entry->stub_type = aarch64_select_branch_stub (sym_value, place);
2566 }
2567
2568 switch (stub_entry->stub_type)
2569 {
2570 case aarch64_stub_adrp_branch:
2571 template = aarch64_adrp_branch_stub;
2572 template_size = sizeof (aarch64_adrp_branch_stub);
2573 break;
2574 case aarch64_stub_long_branch:
2575 template = aarch64_long_branch_stub;
2576 template_size = sizeof (aarch64_long_branch_stub);
2577 break;
2578 case aarch64_stub_erratum_835769_veneer:
2579 template = aarch64_erratum_835769_stub;
2580 template_size = sizeof (aarch64_erratum_835769_stub);
2581 break;
2582 case aarch64_stub_erratum_843419_veneer:
2583 template = aarch64_erratum_843419_stub;
2584 template_size = sizeof (aarch64_erratum_843419_stub);
2585 break;
2586 default:
2587 abort ();
2588 }
2589
2590 for (i = 0; i < (template_size / sizeof template[0]); i++)
2591 {
2592 bfd_putl32 (template[i], loc);
2593 loc += 4;
2594 }
2595
2596 template_size = (template_size + 7) & ~7;
2597 stub_sec->size += template_size;
2598
2599 switch (stub_entry->stub_type)
2600 {
2601 case aarch64_stub_adrp_branch:
2602 if (aarch64_relocate (AARCH64_R (ADR_PREL_PG_HI21), stub_bfd, stub_sec,
2603 stub_entry->stub_offset, sym_value))
2604 /* The stub would not have been relaxed if the offset was out
2605 of range. */
2606 BFD_FAIL ();
2607
2608 if (aarch64_relocate (AARCH64_R (ADD_ABS_LO12_NC), stub_bfd, stub_sec,
2609 stub_entry->stub_offset + 4, sym_value))
2610 BFD_FAIL ();
2611 break;
2612
2613 case aarch64_stub_long_branch:
2614 /* We want the value relative to the address 12 bytes back from the
2615 value itself. */
2616 if (aarch64_relocate (AARCH64_R (PRELNN), stub_bfd, stub_sec,
2617 stub_entry->stub_offset + 16, sym_value + 12))
2618 BFD_FAIL ();
2619 break;
2620
2621 case aarch64_stub_erratum_835769_veneer:
2622 veneered_insn_loc = stub_entry->target_section->output_section->vma
2623 + stub_entry->target_section->output_offset
2624 + stub_entry->target_value;
2625 veneer_entry_loc = stub_entry->stub_sec->output_section->vma
2626 + stub_entry->stub_sec->output_offset
2627 + stub_entry->stub_offset;
2628 branch_offset = veneered_insn_loc - veneer_entry_loc;
2629 branch_offset >>= 2;
2630 branch_offset &= 0x3ffffff;
2631 bfd_putl32 (stub_entry->veneered_insn,
2632 stub_sec->contents + stub_entry->stub_offset);
2633 bfd_putl32 (template[1] | branch_offset,
2634 stub_sec->contents + stub_entry->stub_offset + 4);
2635 break;
2636
2637 case aarch64_stub_erratum_843419_veneer:
2638 if (aarch64_relocate (AARCH64_R (JUMP26), stub_bfd, stub_sec,
2639 stub_entry->stub_offset + 4, sym_value + 4))
2640 BFD_FAIL ();
2641 break;
2642
2643 default:
2644 abort ();
2645 }
2646
2647 return TRUE;
2648 }
2649
2650 /* As above, but don't actually build the stub. Just bump offset so
2651 we know stub section sizes. */
2652
2653 static bfd_boolean
2654 aarch64_size_one_stub (struct bfd_hash_entry *gen_entry,
2655 void *in_arg ATTRIBUTE_UNUSED)
2656 {
2657 struct elf_aarch64_stub_hash_entry *stub_entry;
2658 int size;
2659
2660 /* Massage our args to the form they really have. */
2661 stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
2662
2663 switch (stub_entry->stub_type)
2664 {
2665 case aarch64_stub_adrp_branch:
2666 size = sizeof (aarch64_adrp_branch_stub);
2667 break;
2668 case aarch64_stub_long_branch:
2669 size = sizeof (aarch64_long_branch_stub);
2670 break;
2671 case aarch64_stub_erratum_835769_veneer:
2672 size = sizeof (aarch64_erratum_835769_stub);
2673 break;
2674 case aarch64_stub_erratum_843419_veneer:
2675 size = sizeof (aarch64_erratum_843419_stub);
2676 break;
2677 default:
2678 abort ();
2679 }
2680
2681 size = (size + 7) & ~7;
2682 stub_entry->stub_sec->size += size;
2683 return TRUE;
2684 }
2685
2686 /* External entry points for sizing and building linker stubs. */
2687
2688 /* Set up various things so that we can make a list of input sections
2689 for each output section included in the link. Returns -1 on error,
2690 0 when no stubs will be needed, and 1 on success. */
2691
2692 int
2693 elfNN_aarch64_setup_section_lists (bfd *output_bfd,
2694 struct bfd_link_info *info)
2695 {
2696 bfd *input_bfd;
2697 unsigned int bfd_count;
2698 int top_id, top_index;
2699 asection *section;
2700 asection **input_list, **list;
2701 bfd_size_type amt;
2702 struct elf_aarch64_link_hash_table *htab =
2703 elf_aarch64_hash_table (info);
2704
2705 if (!is_elf_hash_table (htab))
2706 return 0;
2707
2708 /* Count the number of input BFDs and find the top input section id. */
2709 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2710 input_bfd != NULL; input_bfd = input_bfd->link.next)
2711 {
2712 bfd_count += 1;
2713 for (section = input_bfd->sections;
2714 section != NULL; section = section->next)
2715 {
2716 if (top_id < section->id)
2717 top_id = section->id;
2718 }
2719 }
2720 htab->bfd_count = bfd_count;
2721
2722 amt = sizeof (struct map_stub) * (top_id + 1);
2723 htab->stub_group = bfd_zmalloc (amt);
2724 if (htab->stub_group == NULL)
2725 return -1;
2726
2727 /* We can't use output_bfd->section_count here to find the top output
2728 section index as some sections may have been removed, and
2729 _bfd_strip_section_from_output doesn't renumber the indices. */
2730 for (section = output_bfd->sections, top_index = 0;
2731 section != NULL; section = section->next)
2732 {
2733 if (top_index < section->index)
2734 top_index = section->index;
2735 }
2736
2737 htab->top_index = top_index;
2738 amt = sizeof (asection *) * (top_index + 1);
2739 input_list = bfd_malloc (amt);
2740 htab->input_list = input_list;
2741 if (input_list == NULL)
2742 return -1;
2743
2744 /* For sections we aren't interested in, mark their entries with a
2745 value we can check later. */
2746 list = input_list + top_index;
2747 do
2748 *list = bfd_abs_section_ptr;
2749 while (list-- != input_list);
2750
2751 for (section = output_bfd->sections;
2752 section != NULL; section = section->next)
2753 {
2754 if ((section->flags & SEC_CODE) != 0)
2755 input_list[section->index] = NULL;
2756 }
2757
2758 return 1;
2759 }
2760
2761 /* Used by elfNN_aarch64_next_input_section and group_sections. */
2762 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
2763
2764 /* The linker repeatedly calls this function for each input section,
2765 in the order that input sections are linked into output sections.
2766 Build lists of input sections to determine groupings between which
2767 we may insert linker stubs. */
2768
2769 void
2770 elfNN_aarch64_next_input_section (struct bfd_link_info *info, asection *isec)
2771 {
2772 struct elf_aarch64_link_hash_table *htab =
2773 elf_aarch64_hash_table (info);
2774
2775 if (isec->output_section->index <= htab->top_index)
2776 {
2777 asection **list = htab->input_list + isec->output_section->index;
2778
2779 if (*list != bfd_abs_section_ptr)
2780 {
2781 /* Steal the link_sec pointer for our list. */
2782 /* This happens to make the list in reverse order,
2783 which is what we want. */
2784 PREV_SEC (isec) = *list;
2785 *list = isec;
2786 }
2787 }
2788 }
2789
2790 /* See whether we can group stub sections together. Grouping stub
2791 sections may result in fewer stubs. More importantly, we need to
2792 put all .init* and .fini* stubs at the beginning of the .init or
2793 .fini output sections respectively, because glibc splits the
2794 _init and _fini functions into multiple parts. Putting a stub in
2795 the middle of a function is not a good idea. */
2796
2797 static void
2798 group_sections (struct elf_aarch64_link_hash_table *htab,
2799 bfd_size_type stub_group_size,
2800 bfd_boolean stubs_always_before_branch)
2801 {
2802 asection **list = htab->input_list + htab->top_index;
2803
2804 do
2805 {
2806 asection *tail = *list;
2807
2808 if (tail == bfd_abs_section_ptr)
2809 continue;
2810
2811 while (tail != NULL)
2812 {
2813 asection *curr;
2814 asection *prev;
2815 bfd_size_type total;
2816
2817 curr = tail;
2818 total = tail->size;
2819 while ((prev = PREV_SEC (curr)) != NULL
2820 && ((total += curr->output_offset - prev->output_offset)
2821 < stub_group_size))
2822 curr = prev;
2823
2824 /* OK, the size from the start of CURR to the end is less
2825 than stub_group_size and thus can be handled by one stub
2826 section. (Or the tail section is itself larger than
2827 stub_group_size, in which case we may be toast.)
2828 We should really be keeping track of the total size of
2829 stubs added here, as stubs contribute to the final output
2830 section size. */
2831 do
2832 {
2833 prev = PREV_SEC (tail);
2834 /* Set up this stub group. */
2835 htab->stub_group[tail->id].link_sec = curr;
2836 }
2837 while (tail != curr && (tail = prev) != NULL);
2838
2839 /* But wait, there's more! Input sections up to stub_group_size
2840 bytes before the stub section can be handled by it too. */
2841 if (!stubs_always_before_branch)
2842 {
2843 total = 0;
2844 while (prev != NULL
2845 && ((total += tail->output_offset - prev->output_offset)
2846 < stub_group_size))
2847 {
2848 tail = prev;
2849 prev = PREV_SEC (tail);
2850 htab->stub_group[tail->id].link_sec = curr;
2851 }
2852 }
2853 tail = prev;
2854 }
2855 }
2856 while (list-- != htab->input_list);
2857
2858 free (htab->input_list);
2859 }
2860
2861 #undef PREV_SEC
2862
2863 #define AARCH64_BITS(x, pos, n) (((x) >> (pos)) & ((1 << (n)) - 1))
2864
2865 #define AARCH64_RT(insn) AARCH64_BITS (insn, 0, 5)
2866 #define AARCH64_RT2(insn) AARCH64_BITS (insn, 10, 5)
2867 #define AARCH64_RA(insn) AARCH64_BITS (insn, 10, 5)
2868 #define AARCH64_RD(insn) AARCH64_BITS (insn, 0, 5)
2869 #define AARCH64_RN(insn) AARCH64_BITS (insn, 5, 5)
2870 #define AARCH64_RM(insn) AARCH64_BITS (insn, 16, 5)
2871
2872 #define AARCH64_MAC(insn) (((insn) & 0xff000000) == 0x9b000000)
2873 #define AARCH64_BIT(insn, n) AARCH64_BITS (insn, n, 1)
2874 #define AARCH64_OP31(insn) AARCH64_BITS (insn, 21, 3)
2875 #define AARCH64_ZR 0x1f
2876
2877 /* All ld/st ops. See C4-182 of the ARM ARM. The encoding space for
2878 LD_PCREL, LDST_RO, LDST_UI and LDST_UIMM cover prefetch ops. */
2879
2880 #define AARCH64_LD(insn) (AARCH64_BIT (insn, 22) == 1)
2881 #define AARCH64_LDST(insn) (((insn) & 0x0a000000) == 0x08000000)
2882 #define AARCH64_LDST_EX(insn) (((insn) & 0x3f000000) == 0x08000000)
2883 #define AARCH64_LDST_PCREL(insn) (((insn) & 0x3b000000) == 0x18000000)
2884 #define AARCH64_LDST_NAP(insn) (((insn) & 0x3b800000) == 0x28000000)
2885 #define AARCH64_LDSTP_PI(insn) (((insn) & 0x3b800000) == 0x28800000)
2886 #define AARCH64_LDSTP_O(insn) (((insn) & 0x3b800000) == 0x29000000)
2887 #define AARCH64_LDSTP_PRE(insn) (((insn) & 0x3b800000) == 0x29800000)
2888 #define AARCH64_LDST_UI(insn) (((insn) & 0x3b200c00) == 0x38000000)
2889 #define AARCH64_LDST_PIIMM(insn) (((insn) & 0x3b200c00) == 0x38000400)
2890 #define AARCH64_LDST_U(insn) (((insn) & 0x3b200c00) == 0x38000800)
2891 #define AARCH64_LDST_PREIMM(insn) (((insn) & 0x3b200c00) == 0x38000c00)
2892 #define AARCH64_LDST_RO(insn) (((insn) & 0x3b200c00) == 0x38200800)
2893 #define AARCH64_LDST_UIMM(insn) (((insn) & 0x3b000000) == 0x39000000)
2894 #define AARCH64_LDST_SIMD_M(insn) (((insn) & 0xbfbf0000) == 0x0c000000)
2895 #define AARCH64_LDST_SIMD_M_PI(insn) (((insn) & 0xbfa00000) == 0x0c800000)
2896 #define AARCH64_LDST_SIMD_S(insn) (((insn) & 0xbf9f0000) == 0x0d000000)
2897 #define AARCH64_LDST_SIMD_S_PI(insn) (((insn) & 0xbf800000) == 0x0d800000)
2898
2899 /* Classify an INSN if it is indeed a load/store.
2900
2901 Return TRUE if INSN is a LD/ST instruction otherwise return FALSE.
2902
2903 For scalar LD/ST instructions PAIR is FALSE, RT is returned and RT2
2904 is set equal to RT.
2905
2906 For LD/ST pair instructions PAIR is TRUE, RT and RT2 are returned.
2907
2908 */
2909
2910 static bfd_boolean
2911 aarch64_mem_op_p (uint32_t insn, unsigned int *rt, unsigned int *rt2,
2912 bfd_boolean *pair, bfd_boolean *load)
2913 {
2914 uint32_t opcode;
2915 unsigned int r;
2916 uint32_t opc = 0;
2917 uint32_t v = 0;
2918 uint32_t opc_v = 0;
2919
2920 /* Bail out quickly if INSN doesn't fall into the the load-store
2921 encoding space. */
2922 if (!AARCH64_LDST (insn))
2923 return FALSE;
2924
2925 *pair = FALSE;
2926 *load = FALSE;
2927 if (AARCH64_LDST_EX (insn))
2928 {
2929 *rt = AARCH64_RT (insn);
2930 *rt2 = *rt;
2931 if (AARCH64_BIT (insn, 21) == 1)
2932 {
2933 *pair = TRUE;
2934 *rt2 = AARCH64_RT2 (insn);
2935 }
2936 *load = AARCH64_LD (insn);
2937 return TRUE;
2938 }
2939 else if (AARCH64_LDST_NAP (insn)
2940 || AARCH64_LDSTP_PI (insn)
2941 || AARCH64_LDSTP_O (insn)
2942 || AARCH64_LDSTP_PRE (insn))
2943 {
2944 *pair = TRUE;
2945 *rt = AARCH64_RT (insn);
2946 *rt2 = AARCH64_RT2 (insn);
2947 *load = AARCH64_LD (insn);
2948 return TRUE;
2949 }
2950 else if (AARCH64_LDST_PCREL (insn)
2951 || AARCH64_LDST_UI (insn)
2952 || AARCH64_LDST_PIIMM (insn)
2953 || AARCH64_LDST_U (insn)
2954 || AARCH64_LDST_PREIMM (insn)
2955 || AARCH64_LDST_RO (insn)
2956 || AARCH64_LDST_UIMM (insn))
2957 {
2958 *rt = AARCH64_RT (insn);
2959 *rt2 = *rt;
2960 if (AARCH64_LDST_PCREL (insn))
2961 *load = TRUE;
2962 opc = AARCH64_BITS (insn, 22, 2);
2963 v = AARCH64_BIT (insn, 26);
2964 opc_v = opc | (v << 2);
2965 *load = (opc_v == 1 || opc_v == 2 || opc_v == 3
2966 || opc_v == 5 || opc_v == 7);
2967 return TRUE;
2968 }
2969 else if (AARCH64_LDST_SIMD_M (insn)
2970 || AARCH64_LDST_SIMD_M_PI (insn))
2971 {
2972 *rt = AARCH64_RT (insn);
2973 *load = AARCH64_BIT (insn, 22);
2974 opcode = (insn >> 12) & 0xf;
2975 switch (opcode)
2976 {
2977 case 0:
2978 case 2:
2979 *rt2 = *rt + 3;
2980 break;
2981
2982 case 4:
2983 case 6:
2984 *rt2 = *rt + 2;
2985 break;
2986
2987 case 7:
2988 *rt2 = *rt;
2989 break;
2990
2991 case 8:
2992 case 10:
2993 *rt2 = *rt + 1;
2994 break;
2995
2996 default:
2997 return FALSE;
2998 }
2999 return TRUE;
3000 }
3001 else if (AARCH64_LDST_SIMD_S (insn)
3002 || AARCH64_LDST_SIMD_S_PI (insn))
3003 {
3004 *rt = AARCH64_RT (insn);
3005 r = (insn >> 21) & 1;
3006 *load = AARCH64_BIT (insn, 22);
3007 opcode = (insn >> 13) & 0x7;
3008 switch (opcode)
3009 {
3010 case 0:
3011 case 2:
3012 case 4:
3013 *rt2 = *rt + r;
3014 break;
3015
3016 case 1:
3017 case 3:
3018 case 5:
3019 *rt2 = *rt + (r == 0 ? 2 : 3);
3020 break;
3021
3022 case 6:
3023 *rt2 = *rt + r;
3024 break;
3025
3026 case 7:
3027 *rt2 = *rt + (r == 0 ? 2 : 3);
3028 break;
3029
3030 default:
3031 return FALSE;
3032 }
3033 return TRUE;
3034 }
3035
3036 return FALSE;
3037 }
3038
3039 /* Return TRUE if INSN is multiply-accumulate. */
3040
3041 static bfd_boolean
3042 aarch64_mlxl_p (uint32_t insn)
3043 {
3044 uint32_t op31 = AARCH64_OP31 (insn);
3045
3046 if (AARCH64_MAC (insn)
3047 && (op31 == 0 || op31 == 1 || op31 == 5)
3048 /* Exclude MUL instructions which are encoded as a multiple accumulate
3049 with RA = XZR. */
3050 && AARCH64_RA (insn) != AARCH64_ZR)
3051 return TRUE;
3052
3053 return FALSE;
3054 }
3055
3056 /* Some early revisions of the Cortex-A53 have an erratum (835769) whereby
3057 it is possible for a 64-bit multiply-accumulate instruction to generate an
3058 incorrect result. The details are quite complex and hard to
3059 determine statically, since branches in the code may exist in some
3060 circumstances, but all cases end with a memory (load, store, or
3061 prefetch) instruction followed immediately by the multiply-accumulate
3062 operation. We employ a linker patching technique, by moving the potentially
3063 affected multiply-accumulate instruction into a patch region and replacing
3064 the original instruction with a branch to the patch. This function checks
3065 if INSN_1 is the memory operation followed by a multiply-accumulate
3066 operation (INSN_2). Return TRUE if an erratum sequence is found, FALSE
3067 if INSN_1 and INSN_2 are safe. */
3068
3069 static bfd_boolean
3070 aarch64_erratum_sequence (uint32_t insn_1, uint32_t insn_2)
3071 {
3072 uint32_t rt;
3073 uint32_t rt2;
3074 uint32_t rn;
3075 uint32_t rm;
3076 uint32_t ra;
3077 bfd_boolean pair;
3078 bfd_boolean load;
3079
3080 if (aarch64_mlxl_p (insn_2)
3081 && aarch64_mem_op_p (insn_1, &rt, &rt2, &pair, &load))
3082 {
3083 /* Any SIMD memory op is independent of the subsequent MLA
3084 by definition of the erratum. */
3085 if (AARCH64_BIT (insn_1, 26))
3086 return TRUE;
3087
3088 /* If not SIMD, check for integer memory ops and MLA relationship. */
3089 rn = AARCH64_RN (insn_2);
3090 ra = AARCH64_RA (insn_2);
3091 rm = AARCH64_RM (insn_2);
3092
3093 /* If this is a load and there's a true(RAW) dependency, we are safe
3094 and this is not an erratum sequence. */
3095 if (load &&
3096 (rt == rn || rt == rm || rt == ra
3097 || (pair && (rt2 == rn || rt2 == rm || rt2 == ra))))
3098 return FALSE;
3099
3100 /* We conservatively put out stubs for all other cases (including
3101 writebacks). */
3102 return TRUE;
3103 }
3104
3105 return FALSE;
3106 }
3107
3108 /* Used to order a list of mapping symbols by address. */
3109
3110 static int
3111 elf_aarch64_compare_mapping (const void *a, const void *b)
3112 {
3113 const elf_aarch64_section_map *amap = (const elf_aarch64_section_map *) a;
3114 const elf_aarch64_section_map *bmap = (const elf_aarch64_section_map *) b;
3115
3116 if (amap->vma > bmap->vma)
3117 return 1;
3118 else if (amap->vma < bmap->vma)
3119 return -1;
3120 else if (amap->type > bmap->type)
3121 /* Ensure results do not depend on the host qsort for objects with
3122 multiple mapping symbols at the same address by sorting on type
3123 after vma. */
3124 return 1;
3125 else if (amap->type < bmap->type)
3126 return -1;
3127 else
3128 return 0;
3129 }
3130
3131
3132 static char *
3133 _bfd_aarch64_erratum_835769_stub_name (unsigned num_fixes)
3134 {
3135 char *stub_name = (char *) bfd_malloc
3136 (strlen ("__erratum_835769_veneer_") + 16);
3137 sprintf (stub_name,"__erratum_835769_veneer_%d", num_fixes);
3138 return stub_name;
3139 }
3140
3141 /* Scan for Cortex-A53 erratum 835769 sequence.
3142
3143 Return TRUE else FALSE on abnormal termination. */
3144
3145 static bfd_boolean
3146 _bfd_aarch64_erratum_835769_scan (bfd *input_bfd,
3147 struct bfd_link_info *info,
3148 unsigned int *num_fixes_p)
3149 {
3150 asection *section;
3151 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
3152 unsigned int num_fixes = *num_fixes_p;
3153
3154 if (htab == NULL)
3155 return TRUE;
3156
3157 for (section = input_bfd->sections;
3158 section != NULL;
3159 section = section->next)
3160 {
3161 bfd_byte *contents = NULL;
3162 struct _aarch64_elf_section_data *sec_data;
3163 unsigned int span;
3164
3165 if (elf_section_type (section) != SHT_PROGBITS
3166 || (elf_section_flags (section) & SHF_EXECINSTR) == 0
3167 || (section->flags & SEC_EXCLUDE) != 0
3168 || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
3169 || (section->output_section == bfd_abs_section_ptr))
3170 continue;
3171
3172 if (elf_section_data (section)->this_hdr.contents != NULL)
3173 contents = elf_section_data (section)->this_hdr.contents;
3174 else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
3175 return FALSE;
3176
3177 sec_data = elf_aarch64_section_data (section);
3178
3179 qsort (sec_data->map, sec_data->mapcount,
3180 sizeof (elf_aarch64_section_map), elf_aarch64_compare_mapping);
3181
3182 for (span = 0; span < sec_data->mapcount; span++)
3183 {
3184 unsigned int span_start = sec_data->map[span].vma;
3185 unsigned int span_end = ((span == sec_data->mapcount - 1)
3186 ? sec_data->map[0].vma + section->size
3187 : sec_data->map[span + 1].vma);
3188 unsigned int i;
3189 char span_type = sec_data->map[span].type;
3190
3191 if (span_type == 'd')
3192 continue;
3193
3194 for (i = span_start; i + 4 < span_end; i += 4)
3195 {
3196 uint32_t insn_1 = bfd_getl32 (contents + i);
3197 uint32_t insn_2 = bfd_getl32 (contents + i + 4);
3198
3199 if (aarch64_erratum_sequence (insn_1, insn_2))
3200 {
3201 struct elf_aarch64_stub_hash_entry *stub_entry;
3202 char *stub_name = _bfd_aarch64_erratum_835769_stub_name (num_fixes);
3203 if (! stub_name)
3204 return FALSE;
3205
3206 stub_entry = _bfd_aarch64_add_stub_entry_in_group (stub_name,
3207 section,
3208 htab);
3209 if (! stub_entry)
3210 return FALSE;
3211
3212 stub_entry->stub_type = aarch64_stub_erratum_835769_veneer;
3213 stub_entry->target_section = section;
3214 stub_entry->target_value = i + 4;
3215 stub_entry->veneered_insn = insn_2;
3216 stub_entry->output_name = stub_name;
3217 num_fixes++;
3218 }
3219 }
3220 }
3221 if (elf_section_data (section)->this_hdr.contents == NULL)
3222 free (contents);
3223 }
3224
3225 *num_fixes_p = num_fixes;
3226
3227 return TRUE;
3228 }
3229
3230
3231 /* Test if instruction INSN is ADRP. */
3232
3233 static bfd_boolean
3234 _bfd_aarch64_adrp_p (uint32_t insn)
3235 {
3236 return ((insn & 0x9f000000) == 0x90000000);
3237 }
3238
3239
3240 /* Helper predicate to look for cortex-a53 erratum 843419 sequence 1. */
3241
3242 static bfd_boolean
3243 _bfd_aarch64_erratum_843419_sequence_p (uint32_t insn_1, uint32_t insn_2,
3244 uint32_t insn_3)
3245 {
3246 uint32_t rt;
3247 uint32_t rt2;
3248 bfd_boolean pair;
3249 bfd_boolean load;
3250
3251 return (aarch64_mem_op_p (insn_2, &rt, &rt2, &pair, &load)
3252 && (!pair
3253 || (pair && !load))
3254 && AARCH64_LDST_UIMM (insn_3)
3255 && AARCH64_RN (insn_3) == AARCH64_RD (insn_1));
3256 }
3257
3258
3259 /* Test for the presence of Cortex-A53 erratum 843419 instruction sequence.
3260
3261 Return TRUE if section CONTENTS at offset I contains one of the
3262 erratum 843419 sequences, otherwise return FALSE. If a sequence is
3263 seen set P_VENEER_I to the offset of the final LOAD/STORE
3264 instruction in the sequence.
3265 */
3266
3267 static bfd_boolean
3268 _bfd_aarch64_erratum_843419_p (bfd_byte *contents, bfd_vma vma,
3269 bfd_vma i, bfd_vma span_end,
3270 bfd_vma *p_veneer_i)
3271 {
3272 uint32_t insn_1 = bfd_getl32 (contents + i);
3273
3274 if (!_bfd_aarch64_adrp_p (insn_1))
3275 return FALSE;
3276
3277 if (span_end < i + 12)
3278 return FALSE;
3279
3280 uint32_t insn_2 = bfd_getl32 (contents + i + 4);
3281 uint32_t insn_3 = bfd_getl32 (contents + i + 8);
3282
3283 if ((vma & 0xfff) != 0xff8 && (vma & 0xfff) != 0xffc)
3284 return FALSE;
3285
3286 if (_bfd_aarch64_erratum_843419_sequence_p (insn_1, insn_2, insn_3))
3287 {
3288 *p_veneer_i = i + 8;
3289 return TRUE;
3290 }
3291
3292 if (span_end < i + 16)
3293 return FALSE;
3294
3295 uint32_t insn_4 = bfd_getl32 (contents + i + 12);
3296
3297 if (_bfd_aarch64_erratum_843419_sequence_p (insn_1, insn_2, insn_4))
3298 {
3299 *p_veneer_i = i + 12;
3300 return TRUE;
3301 }
3302
3303 return FALSE;
3304 }
3305
3306
3307 /* Resize all stub sections. */
3308
3309 static void
3310 _bfd_aarch64_resize_stubs (struct elf_aarch64_link_hash_table *htab)
3311 {
3312 asection *section;
3313
3314 /* OK, we've added some stubs. Find out the new size of the
3315 stub sections. */
3316 for (section = htab->stub_bfd->sections;
3317 section != NULL; section = section->next)
3318 {
3319 /* Ignore non-stub sections. */
3320 if (!strstr (section->name, STUB_SUFFIX))
3321 continue;
3322 section->size = 0;
3323 }
3324
3325 bfd_hash_traverse (&htab->stub_hash_table, aarch64_size_one_stub, htab);
3326
3327 for (section = htab->stub_bfd->sections;
3328 section != NULL; section = section->next)
3329 {
3330 if (!strstr (section->name, STUB_SUFFIX))
3331 continue;
3332
3333 if (section->size)
3334 section->size += 4;
3335
3336 /* Ensure all stub sections have a size which is a multiple of
3337 4096. This is important in order to ensure that the insertion
3338 of stub sections does not in itself move existing code around
3339 in such a way that new errata sequences are created. */
3340 if (htab->fix_erratum_843419)
3341 if (section->size)
3342 section->size = BFD_ALIGN (section->size, 0x1000);
3343 }
3344 }
3345
3346
3347 /* Construct an erratum 843419 workaround stub name.
3348 */
3349
3350 static char *
3351 _bfd_aarch64_erratum_843419_stub_name (asection *input_section,
3352 bfd_vma offset)
3353 {
3354 const bfd_size_type len = 8 + 4 + 1 + 8 + 1 + 16 + 1;
3355 char *stub_name = bfd_malloc (len);
3356
3357 if (stub_name != NULL)
3358 snprintf (stub_name, len, "e843419@%04x_%08x_%" BFD_VMA_FMT "x",
3359 input_section->owner->id,
3360 input_section->id,
3361 offset);
3362 return stub_name;
3363 }
3364
3365 /* Build a stub_entry structure describing an 843419 fixup.
3366
3367 The stub_entry constructed is populated with the bit pattern INSN
3368 of the instruction located at OFFSET within input SECTION.
3369
3370 Returns TRUE on success. */
3371
3372 static bfd_boolean
3373 _bfd_aarch64_erratum_843419_fixup (uint32_t insn,
3374 bfd_vma adrp_offset,
3375 bfd_vma ldst_offset,
3376 asection *section,
3377 struct bfd_link_info *info)
3378 {
3379 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
3380 char *stub_name;
3381 struct elf_aarch64_stub_hash_entry *stub_entry;
3382
3383 stub_name = _bfd_aarch64_erratum_843419_stub_name (section, ldst_offset);
3384 stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3385 FALSE, FALSE);
3386 if (stub_entry)
3387 {
3388 free (stub_name);
3389 return TRUE;
3390 }
3391
3392 /* We always place an 843419 workaround veneer in the stub section
3393 attached to the input section in which an erratum sequence has
3394 been found. This ensures that later in the link process (in
3395 elfNN_aarch64_write_section) when we copy the veneered
3396 instruction from the input section into the stub section the
3397 copied instruction will have had any relocations applied to it.
3398 If we placed workaround veneers in any other stub section then we
3399 could not assume that all relocations have been processed on the
3400 corresponding input section at the point we output the stub
3401 section.
3402 */
3403
3404 stub_entry = _bfd_aarch64_add_stub_entry_after (stub_name, section, htab);
3405 if (stub_entry == NULL)
3406 {
3407 free (stub_name);
3408 return FALSE;
3409 }
3410
3411 stub_entry->adrp_offset = adrp_offset;
3412 stub_entry->target_value = ldst_offset;
3413 stub_entry->target_section = section;
3414 stub_entry->stub_type = aarch64_stub_erratum_843419_veneer;
3415 stub_entry->veneered_insn = insn;
3416 stub_entry->output_name = stub_name;
3417
3418 return TRUE;
3419 }
3420
3421
3422 /* Scan an input section looking for the signature of erratum 843419.
3423
3424 Scans input SECTION in INPUT_BFD looking for erratum 843419
3425 signatures, for each signature found a stub_entry is created
3426 describing the location of the erratum for subsequent fixup.
3427
3428 Return TRUE on successful scan, FALSE on failure to scan.
3429 */
3430
3431 static bfd_boolean
3432 _bfd_aarch64_erratum_843419_scan (bfd *input_bfd, asection *section,
3433 struct bfd_link_info *info)
3434 {
3435 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
3436
3437 if (htab == NULL)
3438 return TRUE;
3439
3440 if (elf_section_type (section) != SHT_PROGBITS
3441 || (elf_section_flags (section) & SHF_EXECINSTR) == 0
3442 || (section->flags & SEC_EXCLUDE) != 0
3443 || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
3444 || (section->output_section == bfd_abs_section_ptr))
3445 return TRUE;
3446
3447 do
3448 {
3449 bfd_byte *contents = NULL;
3450 struct _aarch64_elf_section_data *sec_data;
3451 unsigned int span;
3452
3453 if (elf_section_data (section)->this_hdr.contents != NULL)
3454 contents = elf_section_data (section)->this_hdr.contents;
3455 else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
3456 return FALSE;
3457
3458 sec_data = elf_aarch64_section_data (section);
3459
3460 qsort (sec_data->map, sec_data->mapcount,
3461 sizeof (elf_aarch64_section_map), elf_aarch64_compare_mapping);
3462
3463 for (span = 0; span < sec_data->mapcount; span++)
3464 {
3465 unsigned int span_start = sec_data->map[span].vma;
3466 unsigned int span_end = ((span == sec_data->mapcount - 1)
3467 ? sec_data->map[0].vma + section->size
3468 : sec_data->map[span + 1].vma);
3469 unsigned int i;
3470 char span_type = sec_data->map[span].type;
3471
3472 if (span_type == 'd')
3473 continue;
3474
3475 for (i = span_start; i + 8 < span_end; i += 4)
3476 {
3477 bfd_vma vma = (section->output_section->vma
3478 + section->output_offset
3479 + i);
3480 bfd_vma veneer_i;
3481
3482 if (_bfd_aarch64_erratum_843419_p
3483 (contents, vma, i, span_end, &veneer_i))
3484 {
3485 uint32_t insn = bfd_getl32 (contents + veneer_i);
3486
3487 if (!_bfd_aarch64_erratum_843419_fixup (insn, i, veneer_i,
3488 section, info))
3489 return FALSE;
3490 }
3491 }
3492 }
3493
3494 if (elf_section_data (section)->this_hdr.contents == NULL)
3495 free (contents);
3496 }
3497 while (0);
3498
3499 return TRUE;
3500 }
3501
3502
3503 /* Determine and set the size of the stub section for a final link.
3504
3505 The basic idea here is to examine all the relocations looking for
3506 PC-relative calls to a target that is unreachable with a "bl"
3507 instruction. */
3508
3509 bfd_boolean
3510 elfNN_aarch64_size_stubs (bfd *output_bfd,
3511 bfd *stub_bfd,
3512 struct bfd_link_info *info,
3513 bfd_signed_vma group_size,
3514 asection * (*add_stub_section) (const char *,
3515 asection *),
3516 void (*layout_sections_again) (void))
3517 {
3518 bfd_size_type stub_group_size;
3519 bfd_boolean stubs_always_before_branch;
3520 bfd_boolean stub_changed = FALSE;
3521 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
3522 unsigned int num_erratum_835769_fixes = 0;
3523
3524 /* Propagate mach to stub bfd, because it may not have been
3525 finalized when we created stub_bfd. */
3526 bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
3527 bfd_get_mach (output_bfd));
3528
3529 /* Stash our params away. */
3530 htab->stub_bfd = stub_bfd;
3531 htab->add_stub_section = add_stub_section;
3532 htab->layout_sections_again = layout_sections_again;
3533 stubs_always_before_branch = group_size < 0;
3534 if (group_size < 0)
3535 stub_group_size = -group_size;
3536 else
3537 stub_group_size = group_size;
3538
3539 if (stub_group_size == 1)
3540 {
3541 /* Default values. */
3542 /* AArch64 branch range is +-128MB. The value used is 1MB less. */
3543 stub_group_size = 127 * 1024 * 1024;
3544 }
3545
3546 group_sections (htab, stub_group_size, stubs_always_before_branch);
3547
3548 (*htab->layout_sections_again) ();
3549
3550 if (htab->fix_erratum_835769)
3551 {
3552 bfd *input_bfd;
3553
3554 for (input_bfd = info->input_bfds;
3555 input_bfd != NULL; input_bfd = input_bfd->link.next)
3556 if (!_bfd_aarch64_erratum_835769_scan (input_bfd, info,
3557 &num_erratum_835769_fixes))
3558 return FALSE;
3559
3560 _bfd_aarch64_resize_stubs (htab);
3561 (*htab->layout_sections_again) ();
3562 }
3563
3564 if (htab->fix_erratum_843419)
3565 {
3566 bfd *input_bfd;
3567
3568 for (input_bfd = info->input_bfds;
3569 input_bfd != NULL;
3570 input_bfd = input_bfd->link.next)
3571 {
3572 asection *section;
3573
3574 for (section = input_bfd->sections;
3575 section != NULL;
3576 section = section->next)
3577 if (!_bfd_aarch64_erratum_843419_scan (input_bfd, section, info))
3578 return FALSE;
3579 }
3580
3581 _bfd_aarch64_resize_stubs (htab);
3582 (*htab->layout_sections_again) ();
3583 }
3584
3585 while (1)
3586 {
3587 bfd *input_bfd;
3588
3589 for (input_bfd = info->input_bfds;
3590 input_bfd != NULL; input_bfd = input_bfd->link.next)
3591 {
3592 Elf_Internal_Shdr *symtab_hdr;
3593 asection *section;
3594 Elf_Internal_Sym *local_syms = NULL;
3595
3596 /* We'll need the symbol table in a second. */
3597 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3598 if (symtab_hdr->sh_info == 0)
3599 continue;
3600
3601 /* Walk over each section attached to the input bfd. */
3602 for (section = input_bfd->sections;
3603 section != NULL; section = section->next)
3604 {
3605 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
3606
3607 /* If there aren't any relocs, then there's nothing more
3608 to do. */
3609 if ((section->flags & SEC_RELOC) == 0
3610 || section->reloc_count == 0
3611 || (section->flags & SEC_CODE) == 0)
3612 continue;
3613
3614 /* If this section is a link-once section that will be
3615 discarded, then don't create any stubs. */
3616 if (section->output_section == NULL
3617 || section->output_section->owner != output_bfd)
3618 continue;
3619
3620 /* Get the relocs. */
3621 internal_relocs
3622 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
3623 NULL, info->keep_memory);
3624 if (internal_relocs == NULL)
3625 goto error_ret_free_local;
3626
3627 /* Now examine each relocation. */
3628 irela = internal_relocs;
3629 irelaend = irela + section->reloc_count;
3630 for (; irela < irelaend; irela++)
3631 {
3632 unsigned int r_type, r_indx;
3633 enum elf_aarch64_stub_type stub_type;
3634 struct elf_aarch64_stub_hash_entry *stub_entry;
3635 asection *sym_sec;
3636 bfd_vma sym_value;
3637 bfd_vma destination;
3638 struct elf_aarch64_link_hash_entry *hash;
3639 const char *sym_name;
3640 char *stub_name;
3641 const asection *id_sec;
3642 unsigned char st_type;
3643 bfd_size_type len;
3644
3645 r_type = ELFNN_R_TYPE (irela->r_info);
3646 r_indx = ELFNN_R_SYM (irela->r_info);
3647
3648 if (r_type >= (unsigned int) R_AARCH64_end)
3649 {
3650 bfd_set_error (bfd_error_bad_value);
3651 error_ret_free_internal:
3652 if (elf_section_data (section)->relocs == NULL)
3653 free (internal_relocs);
3654 goto error_ret_free_local;
3655 }
3656
3657 /* Only look for stubs on unconditional branch and
3658 branch and link instructions. */
3659 if (r_type != (unsigned int) AARCH64_R (CALL26)
3660 && r_type != (unsigned int) AARCH64_R (JUMP26))
3661 continue;
3662
3663 /* Now determine the call target, its name, value,
3664 section. */
3665 sym_sec = NULL;
3666 sym_value = 0;
3667 destination = 0;
3668 hash = NULL;
3669 sym_name = NULL;
3670 if (r_indx < symtab_hdr->sh_info)
3671 {
3672 /* It's a local symbol. */
3673 Elf_Internal_Sym *sym;
3674 Elf_Internal_Shdr *hdr;
3675
3676 if (local_syms == NULL)
3677 {
3678 local_syms
3679 = (Elf_Internal_Sym *) symtab_hdr->contents;
3680 if (local_syms == NULL)
3681 local_syms
3682 = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3683 symtab_hdr->sh_info, 0,
3684 NULL, NULL, NULL);
3685 if (local_syms == NULL)
3686 goto error_ret_free_internal;
3687 }
3688
3689 sym = local_syms + r_indx;
3690 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
3691 sym_sec = hdr->bfd_section;
3692 if (!sym_sec)
3693 /* This is an undefined symbol. It can never
3694 be resolved. */
3695 continue;
3696
3697 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
3698 sym_value = sym->st_value;
3699 destination = (sym_value + irela->r_addend
3700 + sym_sec->output_offset
3701 + sym_sec->output_section->vma);
3702 st_type = ELF_ST_TYPE (sym->st_info);
3703 sym_name
3704 = bfd_elf_string_from_elf_section (input_bfd,
3705 symtab_hdr->sh_link,
3706 sym->st_name);
3707 }
3708 else
3709 {
3710 int e_indx;
3711
3712 e_indx = r_indx - symtab_hdr->sh_info;
3713 hash = ((struct elf_aarch64_link_hash_entry *)
3714 elf_sym_hashes (input_bfd)[e_indx]);
3715
3716 while (hash->root.root.type == bfd_link_hash_indirect
3717 || hash->root.root.type == bfd_link_hash_warning)
3718 hash = ((struct elf_aarch64_link_hash_entry *)
3719 hash->root.root.u.i.link);
3720
3721 if (hash->root.root.type == bfd_link_hash_defined
3722 || hash->root.root.type == bfd_link_hash_defweak)
3723 {
3724 struct elf_aarch64_link_hash_table *globals =
3725 elf_aarch64_hash_table (info);
3726 sym_sec = hash->root.root.u.def.section;
3727 sym_value = hash->root.root.u.def.value;
3728 /* For a destination in a shared library,
3729 use the PLT stub as target address to
3730 decide whether a branch stub is
3731 needed. */
3732 if (globals->root.splt != NULL && hash != NULL
3733 && hash->root.plt.offset != (bfd_vma) - 1)
3734 {
3735 sym_sec = globals->root.splt;
3736 sym_value = hash->root.plt.offset;
3737 if (sym_sec->output_section != NULL)
3738 destination = (sym_value
3739 + sym_sec->output_offset
3740 +
3741 sym_sec->output_section->vma);
3742 }
3743 else if (sym_sec->output_section != NULL)
3744 destination = (sym_value + irela->r_addend
3745 + sym_sec->output_offset
3746 + sym_sec->output_section->vma);
3747 }
3748 else if (hash->root.root.type == bfd_link_hash_undefined
3749 || (hash->root.root.type
3750 == bfd_link_hash_undefweak))
3751 {
3752 /* For a shared library, use the PLT stub as
3753 target address to decide whether a long
3754 branch stub is needed.
3755 For absolute code, they cannot be handled. */
3756 struct elf_aarch64_link_hash_table *globals =
3757 elf_aarch64_hash_table (info);
3758
3759 if (globals->root.splt != NULL && hash != NULL
3760 && hash->root.plt.offset != (bfd_vma) - 1)
3761 {
3762 sym_sec = globals->root.splt;
3763 sym_value = hash->root.plt.offset;
3764 if (sym_sec->output_section != NULL)
3765 destination = (sym_value
3766 + sym_sec->output_offset
3767 +
3768 sym_sec->output_section->vma);
3769 }
3770 else
3771 continue;
3772 }
3773 else
3774 {
3775 bfd_set_error (bfd_error_bad_value);
3776 goto error_ret_free_internal;
3777 }
3778 st_type = ELF_ST_TYPE (hash->root.type);
3779 sym_name = hash->root.root.root.string;
3780 }
3781
3782 /* Determine what (if any) linker stub is needed. */
3783 stub_type = aarch64_type_of_stub
3784 (info, section, irela, st_type, hash, destination);
3785 if (stub_type == aarch64_stub_none)
3786 continue;
3787
3788 /* Support for grouping stub sections. */
3789 id_sec = htab->stub_group[section->id].link_sec;
3790
3791 /* Get the name of this stub. */
3792 stub_name = elfNN_aarch64_stub_name (id_sec, sym_sec, hash,
3793 irela);
3794 if (!stub_name)
3795 goto error_ret_free_internal;
3796
3797 stub_entry =
3798 aarch64_stub_hash_lookup (&htab->stub_hash_table,
3799 stub_name, FALSE, FALSE);
3800 if (stub_entry != NULL)
3801 {
3802 /* The proper stub has already been created. */
3803 free (stub_name);
3804 continue;
3805 }
3806
3807 stub_entry = _bfd_aarch64_add_stub_entry_in_group
3808 (stub_name, section, htab);
3809 if (stub_entry == NULL)
3810 {
3811 free (stub_name);
3812 goto error_ret_free_internal;
3813 }
3814
3815 stub_entry->target_value = sym_value;
3816 stub_entry->target_section = sym_sec;
3817 stub_entry->stub_type = stub_type;
3818 stub_entry->h = hash;
3819 stub_entry->st_type = st_type;
3820
3821 if (sym_name == NULL)
3822 sym_name = "unnamed";
3823 len = sizeof (STUB_ENTRY_NAME) + strlen (sym_name);
3824 stub_entry->output_name = bfd_alloc (htab->stub_bfd, len);
3825 if (stub_entry->output_name == NULL)
3826 {
3827 free (stub_name);
3828 goto error_ret_free_internal;
3829 }
3830
3831 snprintf (stub_entry->output_name, len, STUB_ENTRY_NAME,
3832 sym_name);
3833
3834 stub_changed = TRUE;
3835 }
3836
3837 /* We're done with the internal relocs, free them. */
3838 if (elf_section_data (section)->relocs == NULL)
3839 free (internal_relocs);
3840 }
3841 }
3842
3843 if (!stub_changed)
3844 break;
3845
3846 _bfd_aarch64_resize_stubs (htab);
3847
3848 /* Ask the linker to do its stuff. */
3849 (*htab->layout_sections_again) ();
3850 stub_changed = FALSE;
3851 }
3852
3853 return TRUE;
3854
3855 error_ret_free_local:
3856 return FALSE;
3857 }
3858
3859 /* Build all the stubs associated with the current output file. The
3860 stubs are kept in a hash table attached to the main linker hash
3861 table. We also set up the .plt entries for statically linked PIC
3862 functions here. This function is called via aarch64_elf_finish in the
3863 linker. */
3864
3865 bfd_boolean
3866 elfNN_aarch64_build_stubs (struct bfd_link_info *info)
3867 {
3868 asection *stub_sec;
3869 struct bfd_hash_table *table;
3870 struct elf_aarch64_link_hash_table *htab;
3871
3872 htab = elf_aarch64_hash_table (info);
3873
3874 for (stub_sec = htab->stub_bfd->sections;
3875 stub_sec != NULL; stub_sec = stub_sec->next)
3876 {
3877 bfd_size_type size;
3878
3879 /* Ignore non-stub sections. */
3880 if (!strstr (stub_sec->name, STUB_SUFFIX))
3881 continue;
3882
3883 /* Allocate memory to hold the linker stubs. */
3884 size = stub_sec->size;
3885 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
3886 if (stub_sec->contents == NULL && size != 0)
3887 return FALSE;
3888 stub_sec->size = 0;
3889
3890 bfd_putl32 (0x14000000 | (size >> 2), stub_sec->contents);
3891 stub_sec->size += 4;
3892 }
3893
3894 /* Build the stubs as directed by the stub hash table. */
3895 table = &htab->stub_hash_table;
3896 bfd_hash_traverse (table, aarch64_build_one_stub, info);
3897
3898 return TRUE;
3899 }
3900
3901
3902 /* Add an entry to the code/data map for section SEC. */
3903
3904 static void
3905 elfNN_aarch64_section_map_add (asection *sec, char type, bfd_vma vma)
3906 {
3907 struct _aarch64_elf_section_data *sec_data =
3908 elf_aarch64_section_data (sec);
3909 unsigned int newidx;
3910
3911 if (sec_data->map == NULL)
3912 {
3913 sec_data->map = bfd_malloc (sizeof (elf_aarch64_section_map));
3914 sec_data->mapcount = 0;
3915 sec_data->mapsize = 1;
3916 }
3917
3918 newidx = sec_data->mapcount++;
3919
3920 if (sec_data->mapcount > sec_data->mapsize)
3921 {
3922 sec_data->mapsize *= 2;
3923 sec_data->map = bfd_realloc_or_free
3924 (sec_data->map, sec_data->mapsize * sizeof (elf_aarch64_section_map));
3925 }
3926
3927 if (sec_data->map)
3928 {
3929 sec_data->map[newidx].vma = vma;
3930 sec_data->map[newidx].type = type;
3931 }
3932 }
3933
3934
3935 /* Initialise maps of insn/data for input BFDs. */
3936 void
3937 bfd_elfNN_aarch64_init_maps (bfd *abfd)
3938 {
3939 Elf_Internal_Sym *isymbuf;
3940 Elf_Internal_Shdr *hdr;
3941 unsigned int i, localsyms;
3942
3943 /* Make sure that we are dealing with an AArch64 elf binary. */
3944 if (!is_aarch64_elf (abfd))
3945 return;
3946
3947 if ((abfd->flags & DYNAMIC) != 0)
3948 return;
3949
3950 hdr = &elf_symtab_hdr (abfd);
3951 localsyms = hdr->sh_info;
3952
3953 /* Obtain a buffer full of symbols for this BFD. The hdr->sh_info field
3954 should contain the number of local symbols, which should come before any
3955 global symbols. Mapping symbols are always local. */
3956 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, localsyms, 0, NULL, NULL, NULL);
3957
3958 /* No internal symbols read? Skip this BFD. */
3959 if (isymbuf == NULL)
3960 return;
3961
3962 for (i = 0; i < localsyms; i++)
3963 {
3964 Elf_Internal_Sym *isym = &isymbuf[i];
3965 asection *sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3966 const char *name;
3967
3968 if (sec != NULL && ELF_ST_BIND (isym->st_info) == STB_LOCAL)
3969 {
3970 name = bfd_elf_string_from_elf_section (abfd,
3971 hdr->sh_link,
3972 isym->st_name);
3973
3974 if (bfd_is_aarch64_special_symbol_name
3975 (name, BFD_AARCH64_SPECIAL_SYM_TYPE_MAP))
3976 elfNN_aarch64_section_map_add (sec, name[1], isym->st_value);
3977 }
3978 }
3979 }
3980
3981 /* Set option values needed during linking. */
3982 void
3983 bfd_elfNN_aarch64_set_options (struct bfd *output_bfd,
3984 struct bfd_link_info *link_info,
3985 int no_enum_warn,
3986 int no_wchar_warn, int pic_veneer,
3987 int fix_erratum_835769,
3988 int fix_erratum_843419)
3989 {
3990 struct elf_aarch64_link_hash_table *globals;
3991
3992 globals = elf_aarch64_hash_table (link_info);
3993 globals->pic_veneer = pic_veneer;
3994 globals->fix_erratum_835769 = fix_erratum_835769;
3995 globals->fix_erratum_843419 = fix_erratum_843419;
3996 globals->fix_erratum_843419_adr = TRUE;
3997
3998 BFD_ASSERT (is_aarch64_elf (output_bfd));
3999 elf_aarch64_tdata (output_bfd)->no_enum_size_warning = no_enum_warn;
4000 elf_aarch64_tdata (output_bfd)->no_wchar_size_warning = no_wchar_warn;
4001 }
4002
4003 static bfd_vma
4004 aarch64_calculate_got_entry_vma (struct elf_link_hash_entry *h,
4005 struct elf_aarch64_link_hash_table
4006 *globals, struct bfd_link_info *info,
4007 bfd_vma value, bfd *output_bfd,
4008 bfd_boolean *unresolved_reloc_p)
4009 {
4010 bfd_vma off = (bfd_vma) - 1;
4011 asection *basegot = globals->root.sgot;
4012 bfd_boolean dyn = globals->root.dynamic_sections_created;
4013
4014 if (h != NULL)
4015 {
4016 BFD_ASSERT (basegot != NULL);
4017 off = h->got.offset;
4018 BFD_ASSERT (off != (bfd_vma) - 1);
4019 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
4020 || (info->shared
4021 && SYMBOL_REFERENCES_LOCAL (info, h))
4022 || (ELF_ST_VISIBILITY (h->other)
4023 && h->root.type == bfd_link_hash_undefweak))
4024 {
4025 /* This is actually a static link, or it is a -Bsymbolic link
4026 and the symbol is defined locally. We must initialize this
4027 entry in the global offset table. Since the offset must
4028 always be a multiple of 8 (4 in the case of ILP32), we use
4029 the least significant bit to record whether we have
4030 initialized it already.
4031 When doing a dynamic link, we create a .rel(a).got relocation
4032 entry to initialize the value. This is done in the
4033 finish_dynamic_symbol routine. */
4034 if ((off & 1) != 0)
4035 off &= ~1;
4036 else
4037 {
4038 bfd_put_NN (output_bfd, value, basegot->contents + off);
4039 h->got.offset |= 1;
4040 }
4041 }
4042 else
4043 *unresolved_reloc_p = FALSE;
4044
4045 off = off + basegot->output_section->vma + basegot->output_offset;
4046 }
4047
4048 return off;
4049 }
4050
4051 /* Change R_TYPE to a more efficient access model where possible,
4052 return the new reloc type. */
4053
4054 static bfd_reloc_code_real_type
4055 aarch64_tls_transition_without_check (bfd_reloc_code_real_type r_type,
4056 struct elf_link_hash_entry *h)
4057 {
4058 bfd_boolean is_local = h == NULL;
4059
4060 switch (r_type)
4061 {
4062 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
4063 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
4064 return (is_local
4065 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1
4066 : BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21);
4067
4068 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
4069 return (is_local
4070 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
4071 : r_type);
4072
4073 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
4074 return (is_local
4075 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1
4076 : BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19);
4077
4078 case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
4079 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
4080 return (is_local
4081 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
4082 : BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC);
4083
4084 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
4085 return is_local ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1 : r_type;
4086
4087 case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
4088 return is_local ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC : r_type;
4089
4090 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
4091 return r_type;
4092
4093 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
4094 return (is_local
4095 ? BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12
4096 : BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19);
4097
4098 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
4099 case BFD_RELOC_AARCH64_TLSDESC_CALL:
4100 /* Instructions with these relocations will become NOPs. */
4101 return BFD_RELOC_AARCH64_NONE;
4102
4103 default:
4104 break;
4105 }
4106
4107 return r_type;
4108 }
4109
4110 static unsigned int
4111 aarch64_reloc_got_type (bfd_reloc_code_real_type r_type)
4112 {
4113 switch (r_type)
4114 {
4115 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
4116 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
4117 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
4118 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
4119 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
4120 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
4121 return GOT_NORMAL;
4122
4123 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
4124 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
4125 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
4126 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
4127 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
4128 return GOT_TLS_GD;
4129
4130 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
4131 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
4132 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
4133 case BFD_RELOC_AARCH64_TLSDESC_CALL:
4134 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
4135 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
4136 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
4137 return GOT_TLSDESC_GD;
4138
4139 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
4140 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
4141 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
4142 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
4143 return GOT_TLS_IE;
4144
4145 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
4146 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
4147 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
4148 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
4149 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
4150 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
4151 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
4152 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
4153 return GOT_UNKNOWN;
4154
4155 default:
4156 break;
4157 }
4158 return GOT_UNKNOWN;
4159 }
4160
4161 static bfd_boolean
4162 aarch64_can_relax_tls (bfd *input_bfd,
4163 struct bfd_link_info *info,
4164 bfd_reloc_code_real_type r_type,
4165 struct elf_link_hash_entry *h,
4166 unsigned long r_symndx)
4167 {
4168 unsigned int symbol_got_type;
4169 unsigned int reloc_got_type;
4170
4171 if (! IS_AARCH64_TLS_RELOC (r_type))
4172 return FALSE;
4173
4174 symbol_got_type = elfNN_aarch64_symbol_got_type (h, input_bfd, r_symndx);
4175 reloc_got_type = aarch64_reloc_got_type (r_type);
4176
4177 if (symbol_got_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (reloc_got_type))
4178 return TRUE;
4179
4180 if (info->shared)
4181 return FALSE;
4182
4183 if (h && h->root.type == bfd_link_hash_undefweak)
4184 return FALSE;
4185
4186 return TRUE;
4187 }
4188
4189 /* Given the relocation code R_TYPE, return the relaxed bfd reloc
4190 enumerator. */
4191
4192 static bfd_reloc_code_real_type
4193 aarch64_tls_transition (bfd *input_bfd,
4194 struct bfd_link_info *info,
4195 unsigned int r_type,
4196 struct elf_link_hash_entry *h,
4197 unsigned long r_symndx)
4198 {
4199 bfd_reloc_code_real_type bfd_r_type
4200 = elfNN_aarch64_bfd_reloc_from_type (r_type);
4201
4202 if (! aarch64_can_relax_tls (input_bfd, info, bfd_r_type, h, r_symndx))
4203 return bfd_r_type;
4204
4205 return aarch64_tls_transition_without_check (bfd_r_type, h);
4206 }
4207
4208 /* Return the base VMA address which should be subtracted from real addresses
4209 when resolving R_AARCH64_TLS_DTPREL relocation. */
4210
4211 static bfd_vma
4212 dtpoff_base (struct bfd_link_info *info)
4213 {
4214 /* If tls_sec is NULL, we should have signalled an error already. */
4215 BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
4216 return elf_hash_table (info)->tls_sec->vma;
4217 }
4218
4219 /* Return the base VMA address which should be subtracted from real addresses
4220 when resolving R_AARCH64_TLS_GOTTPREL64 relocations. */
4221
4222 static bfd_vma
4223 tpoff_base (struct bfd_link_info *info)
4224 {
4225 struct elf_link_hash_table *htab = elf_hash_table (info);
4226
4227 /* If tls_sec is NULL, we should have signalled an error already. */
4228 BFD_ASSERT (htab->tls_sec != NULL);
4229
4230 bfd_vma base = align_power ((bfd_vma) TCB_SIZE,
4231 htab->tls_sec->alignment_power);
4232 return htab->tls_sec->vma - base;
4233 }
4234
4235 static bfd_vma *
4236 symbol_got_offset_ref (bfd *input_bfd, struct elf_link_hash_entry *h,
4237 unsigned long r_symndx)
4238 {
4239 /* Calculate the address of the GOT entry for symbol
4240 referred to in h. */
4241 if (h != NULL)
4242 return &h->got.offset;
4243 else
4244 {
4245 /* local symbol */
4246 struct elf_aarch64_local_symbol *l;
4247
4248 l = elf_aarch64_locals (input_bfd);
4249 return &l[r_symndx].got_offset;
4250 }
4251 }
4252
4253 static void
4254 symbol_got_offset_mark (bfd *input_bfd, struct elf_link_hash_entry *h,
4255 unsigned long r_symndx)
4256 {
4257 bfd_vma *p;
4258 p = symbol_got_offset_ref (input_bfd, h, r_symndx);
4259 *p |= 1;
4260 }
4261
4262 static int
4263 symbol_got_offset_mark_p (bfd *input_bfd, struct elf_link_hash_entry *h,
4264 unsigned long r_symndx)
4265 {
4266 bfd_vma value;
4267 value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
4268 return value & 1;
4269 }
4270
4271 static bfd_vma
4272 symbol_got_offset (bfd *input_bfd, struct elf_link_hash_entry *h,
4273 unsigned long r_symndx)
4274 {
4275 bfd_vma value;
4276 value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
4277 value &= ~1;
4278 return value;
4279 }
4280
4281 static bfd_vma *
4282 symbol_tlsdesc_got_offset_ref (bfd *input_bfd, struct elf_link_hash_entry *h,
4283 unsigned long r_symndx)
4284 {
4285 /* Calculate the address of the GOT entry for symbol
4286 referred to in h. */
4287 if (h != NULL)
4288 {
4289 struct elf_aarch64_link_hash_entry *eh;
4290 eh = (struct elf_aarch64_link_hash_entry *) h;
4291 return &eh->tlsdesc_got_jump_table_offset;
4292 }
4293 else
4294 {
4295 /* local symbol */
4296 struct elf_aarch64_local_symbol *l;
4297
4298 l = elf_aarch64_locals (input_bfd);
4299 return &l[r_symndx].tlsdesc_got_jump_table_offset;
4300 }
4301 }
4302
4303 static void
4304 symbol_tlsdesc_got_offset_mark (bfd *input_bfd, struct elf_link_hash_entry *h,
4305 unsigned long r_symndx)
4306 {
4307 bfd_vma *p;
4308 p = symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
4309 *p |= 1;
4310 }
4311
4312 static int
4313 symbol_tlsdesc_got_offset_mark_p (bfd *input_bfd,
4314 struct elf_link_hash_entry *h,
4315 unsigned long r_symndx)
4316 {
4317 bfd_vma value;
4318 value = * symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
4319 return value & 1;
4320 }
4321
4322 static bfd_vma
4323 symbol_tlsdesc_got_offset (bfd *input_bfd, struct elf_link_hash_entry *h,
4324 unsigned long r_symndx)
4325 {
4326 bfd_vma value;
4327 value = * symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
4328 value &= ~1;
4329 return value;
4330 }
4331
4332 /* Data for make_branch_to_erratum_835769_stub(). */
4333
4334 struct erratum_835769_branch_to_stub_data
4335 {
4336 struct bfd_link_info *info;
4337 asection *output_section;
4338 bfd_byte *contents;
4339 };
4340
4341 /* Helper to insert branches to erratum 835769 stubs in the right
4342 places for a particular section. */
4343
4344 static bfd_boolean
4345 make_branch_to_erratum_835769_stub (struct bfd_hash_entry *gen_entry,
4346 void *in_arg)
4347 {
4348 struct elf_aarch64_stub_hash_entry *stub_entry;
4349 struct erratum_835769_branch_to_stub_data *data;
4350 bfd_byte *contents;
4351 unsigned long branch_insn = 0;
4352 bfd_vma veneered_insn_loc, veneer_entry_loc;
4353 bfd_signed_vma branch_offset;
4354 unsigned int target;
4355 bfd *abfd;
4356
4357 stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
4358 data = (struct erratum_835769_branch_to_stub_data *) in_arg;
4359
4360 if (stub_entry->target_section != data->output_section
4361 || stub_entry->stub_type != aarch64_stub_erratum_835769_veneer)
4362 return TRUE;
4363
4364 contents = data->contents;
4365 veneered_insn_loc = stub_entry->target_section->output_section->vma
4366 + stub_entry->target_section->output_offset
4367 + stub_entry->target_value;
4368 veneer_entry_loc = stub_entry->stub_sec->output_section->vma
4369 + stub_entry->stub_sec->output_offset
4370 + stub_entry->stub_offset;
4371 branch_offset = veneer_entry_loc - veneered_insn_loc;
4372
4373 abfd = stub_entry->target_section->owner;
4374 if (!aarch64_valid_branch_p (veneer_entry_loc, veneered_insn_loc))
4375 (*_bfd_error_handler)
4376 (_("%B: error: Erratum 835769 stub out "
4377 "of range (input file too large)"), abfd);
4378
4379 target = stub_entry->target_value;
4380 branch_insn = 0x14000000;
4381 branch_offset >>= 2;
4382 branch_offset &= 0x3ffffff;
4383 branch_insn |= branch_offset;
4384 bfd_putl32 (branch_insn, &contents[target]);
4385
4386 return TRUE;
4387 }
4388
4389
4390 static bfd_boolean
4391 _bfd_aarch64_erratum_843419_branch_to_stub (struct bfd_hash_entry *gen_entry,
4392 void *in_arg)
4393 {
4394 struct elf_aarch64_stub_hash_entry *stub_entry
4395 = (struct elf_aarch64_stub_hash_entry *) gen_entry;
4396 struct erratum_835769_branch_to_stub_data *data
4397 = (struct erratum_835769_branch_to_stub_data *) in_arg;
4398 struct bfd_link_info *info;
4399 struct elf_aarch64_link_hash_table *htab;
4400 bfd_byte *contents;
4401 asection *section;
4402 bfd *abfd;
4403 bfd_vma place;
4404 uint32_t insn;
4405
4406 info = data->info;
4407 contents = data->contents;
4408 section = data->output_section;
4409
4410 htab = elf_aarch64_hash_table (info);
4411
4412 if (stub_entry->target_section != section
4413 || stub_entry->stub_type != aarch64_stub_erratum_843419_veneer)
4414 return TRUE;
4415
4416 insn = bfd_getl32 (contents + stub_entry->target_value);
4417 bfd_putl32 (insn,
4418 stub_entry->stub_sec->contents + stub_entry->stub_offset);
4419
4420 place = (section->output_section->vma + section->output_offset
4421 + stub_entry->adrp_offset);
4422 insn = bfd_getl32 (contents + stub_entry->adrp_offset);
4423
4424 if ((insn & AARCH64_ADRP_OP_MASK) != AARCH64_ADRP_OP)
4425 abort ();
4426
4427 bfd_signed_vma imm =
4428 (_bfd_aarch64_sign_extend
4429 ((bfd_vma) _bfd_aarch64_decode_adrp_imm (insn) << 12, 33)
4430 - (place & 0xfff));
4431
4432 if (htab->fix_erratum_843419_adr
4433 && (imm >= AARCH64_MIN_ADRP_IMM && imm <= AARCH64_MAX_ADRP_IMM))
4434 {
4435 insn = (_bfd_aarch64_reencode_adr_imm (AARCH64_ADR_OP, imm)
4436 | AARCH64_RT (insn));
4437 bfd_putl32 (insn, contents + stub_entry->adrp_offset);
4438 }
4439 else
4440 {
4441 bfd_vma veneered_insn_loc;
4442 bfd_vma veneer_entry_loc;
4443 bfd_signed_vma branch_offset;
4444 uint32_t branch_insn;
4445
4446 veneered_insn_loc = stub_entry->target_section->output_section->vma
4447 + stub_entry->target_section->output_offset
4448 + stub_entry->target_value;
4449 veneer_entry_loc = stub_entry->stub_sec->output_section->vma
4450 + stub_entry->stub_sec->output_offset
4451 + stub_entry->stub_offset;
4452 branch_offset = veneer_entry_loc - veneered_insn_loc;
4453
4454 abfd = stub_entry->target_section->owner;
4455 if (!aarch64_valid_branch_p (veneer_entry_loc, veneered_insn_loc))
4456 (*_bfd_error_handler)
4457 (_("%B: error: Erratum 843419 stub out "
4458 "of range (input file too large)"), abfd);
4459
4460 branch_insn = 0x14000000;
4461 branch_offset >>= 2;
4462 branch_offset &= 0x3ffffff;
4463 branch_insn |= branch_offset;
4464 bfd_putl32 (branch_insn, contents + stub_entry->target_value);
4465 }
4466 return TRUE;
4467 }
4468
4469
4470 static bfd_boolean
4471 elfNN_aarch64_write_section (bfd *output_bfd ATTRIBUTE_UNUSED,
4472 struct bfd_link_info *link_info,
4473 asection *sec,
4474 bfd_byte *contents)
4475
4476 {
4477 struct elf_aarch64_link_hash_table *globals =
4478 elf_aarch64_hash_table (link_info);
4479
4480 if (globals == NULL)
4481 return FALSE;
4482
4483 /* Fix code to point to erratum 835769 stubs. */
4484 if (globals->fix_erratum_835769)
4485 {
4486 struct erratum_835769_branch_to_stub_data data;
4487
4488 data.info = link_info;
4489 data.output_section = sec;
4490 data.contents = contents;
4491 bfd_hash_traverse (&globals->stub_hash_table,
4492 make_branch_to_erratum_835769_stub, &data);
4493 }
4494
4495 if (globals->fix_erratum_843419)
4496 {
4497 struct erratum_835769_branch_to_stub_data data;
4498
4499 data.info = link_info;
4500 data.output_section = sec;
4501 data.contents = contents;
4502 bfd_hash_traverse (&globals->stub_hash_table,
4503 _bfd_aarch64_erratum_843419_branch_to_stub, &data);
4504 }
4505
4506 return FALSE;
4507 }
4508
4509 /* Perform a relocation as part of a final link. */
4510 static bfd_reloc_status_type
4511 elfNN_aarch64_final_link_relocate (reloc_howto_type *howto,
4512 bfd *input_bfd,
4513 bfd *output_bfd,
4514 asection *input_section,
4515 bfd_byte *contents,
4516 Elf_Internal_Rela *rel,
4517 bfd_vma value,
4518 struct bfd_link_info *info,
4519 asection *sym_sec,
4520 struct elf_link_hash_entry *h,
4521 bfd_boolean *unresolved_reloc_p,
4522 bfd_boolean save_addend,
4523 bfd_vma *saved_addend,
4524 Elf_Internal_Sym *sym)
4525 {
4526 Elf_Internal_Shdr *symtab_hdr;
4527 unsigned int r_type = howto->type;
4528 bfd_reloc_code_real_type bfd_r_type
4529 = elfNN_aarch64_bfd_reloc_from_howto (howto);
4530 bfd_reloc_code_real_type new_bfd_r_type;
4531 unsigned long r_symndx;
4532 bfd_byte *hit_data = contents + rel->r_offset;
4533 bfd_vma place, off;
4534 bfd_signed_vma signed_addend;
4535 struct elf_aarch64_link_hash_table *globals;
4536 bfd_boolean weak_undef_p;
4537 asection *base_got;
4538
4539 globals = elf_aarch64_hash_table (info);
4540
4541 symtab_hdr = &elf_symtab_hdr (input_bfd);
4542
4543 BFD_ASSERT (is_aarch64_elf (input_bfd));
4544
4545 r_symndx = ELFNN_R_SYM (rel->r_info);
4546
4547 /* It is possible to have linker relaxations on some TLS access
4548 models. Update our information here. */
4549 new_bfd_r_type = aarch64_tls_transition (input_bfd, info, r_type, h, r_symndx);
4550 if (new_bfd_r_type != bfd_r_type)
4551 {
4552 bfd_r_type = new_bfd_r_type;
4553 howto = elfNN_aarch64_howto_from_bfd_reloc (bfd_r_type);
4554 BFD_ASSERT (howto != NULL);
4555 r_type = howto->type;
4556 }
4557
4558 place = input_section->output_section->vma
4559 + input_section->output_offset + rel->r_offset;
4560
4561 /* Get addend, accumulating the addend for consecutive relocs
4562 which refer to the same offset. */
4563 signed_addend = saved_addend ? *saved_addend : 0;
4564 signed_addend += rel->r_addend;
4565
4566 weak_undef_p = (h ? h->root.type == bfd_link_hash_undefweak
4567 : bfd_is_und_section (sym_sec));
4568
4569 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
4570 it here if it is defined in a non-shared object. */
4571 if (h != NULL
4572 && h->type == STT_GNU_IFUNC
4573 && h->def_regular)
4574 {
4575 asection *plt;
4576 const char *name;
4577 bfd_vma addend = 0;
4578
4579 if ((input_section->flags & SEC_ALLOC) == 0
4580 || h->plt.offset == (bfd_vma) -1)
4581 abort ();
4582
4583 /* STT_GNU_IFUNC symbol must go through PLT. */
4584 plt = globals->root.splt ? globals->root.splt : globals->root.iplt;
4585 value = (plt->output_section->vma + plt->output_offset + h->plt.offset);
4586
4587 switch (bfd_r_type)
4588 {
4589 default:
4590 if (h->root.root.string)
4591 name = h->root.root.string;
4592 else
4593 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4594 NULL);
4595 (*_bfd_error_handler)
4596 (_("%B: relocation %s against STT_GNU_IFUNC "
4597 "symbol `%s' isn't handled by %s"), input_bfd,
4598 howto->name, name, __FUNCTION__);
4599 bfd_set_error (bfd_error_bad_value);
4600 return FALSE;
4601
4602 case BFD_RELOC_AARCH64_NN:
4603 if (rel->r_addend != 0)
4604 {
4605 if (h->root.root.string)
4606 name = h->root.root.string;
4607 else
4608 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
4609 sym, NULL);
4610 (*_bfd_error_handler)
4611 (_("%B: relocation %s against STT_GNU_IFUNC "
4612 "symbol `%s' has non-zero addend: %d"),
4613 input_bfd, howto->name, name, rel->r_addend);
4614 bfd_set_error (bfd_error_bad_value);
4615 return FALSE;
4616 }
4617
4618 /* Generate dynamic relocation only when there is a
4619 non-GOT reference in a shared object. */
4620 if (info->shared && h->non_got_ref)
4621 {
4622 Elf_Internal_Rela outrel;
4623 asection *sreloc;
4624
4625 /* Need a dynamic relocation to get the real function
4626 address. */
4627 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
4628 info,
4629 input_section,
4630 rel->r_offset);
4631 if (outrel.r_offset == (bfd_vma) -1
4632 || outrel.r_offset == (bfd_vma) -2)
4633 abort ();
4634
4635 outrel.r_offset += (input_section->output_section->vma
4636 + input_section->output_offset);
4637
4638 if (h->dynindx == -1
4639 || h->forced_local
4640 || info->executable)
4641 {
4642 /* This symbol is resolved locally. */
4643 outrel.r_info = ELFNN_R_INFO (0, AARCH64_R (IRELATIVE));
4644 outrel.r_addend = (h->root.u.def.value
4645 + h->root.u.def.section->output_section->vma
4646 + h->root.u.def.section->output_offset);
4647 }
4648 else
4649 {
4650 outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
4651 outrel.r_addend = 0;
4652 }
4653
4654 sreloc = globals->root.irelifunc;
4655 elf_append_rela (output_bfd, sreloc, &outrel);
4656
4657 /* If this reloc is against an external symbol, we
4658 do not want to fiddle with the addend. Otherwise,
4659 we need to include the symbol value so that it
4660 becomes an addend for the dynamic reloc. For an
4661 internal symbol, we have updated addend. */
4662 return bfd_reloc_ok;
4663 }
4664 /* FALLTHROUGH */
4665 case BFD_RELOC_AARCH64_CALL26:
4666 case BFD_RELOC_AARCH64_JUMP26:
4667 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
4668 signed_addend,
4669 weak_undef_p);
4670 return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type,
4671 howto, value);
4672 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
4673 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
4674 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
4675 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
4676 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
4677 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
4678 base_got = globals->root.sgot;
4679 off = h->got.offset;
4680
4681 if (base_got == NULL)
4682 abort ();
4683
4684 if (off == (bfd_vma) -1)
4685 {
4686 bfd_vma plt_index;
4687
4688 /* We can't use h->got.offset here to save state, or
4689 even just remember the offset, as finish_dynamic_symbol
4690 would use that as offset into .got. */
4691
4692 if (globals->root.splt != NULL)
4693 {
4694 plt_index = ((h->plt.offset - globals->plt_header_size) /
4695 globals->plt_entry_size);
4696 off = (plt_index + 3) * GOT_ENTRY_SIZE;
4697 base_got = globals->root.sgotplt;
4698 }
4699 else
4700 {
4701 plt_index = h->plt.offset / globals->plt_entry_size;
4702 off = plt_index * GOT_ENTRY_SIZE;
4703 base_got = globals->root.igotplt;
4704 }
4705
4706 if (h->dynindx == -1
4707 || h->forced_local
4708 || info->symbolic)
4709 {
4710 /* This references the local definition. We must
4711 initialize this entry in the global offset table.
4712 Since the offset must always be a multiple of 8,
4713 we use the least significant bit to record
4714 whether we have initialized it already.
4715
4716 When doing a dynamic link, we create a .rela.got
4717 relocation entry to initialize the value. This
4718 is done in the finish_dynamic_symbol routine. */
4719 if ((off & 1) != 0)
4720 off &= ~1;
4721 else
4722 {
4723 bfd_put_NN (output_bfd, value,
4724 base_got->contents + off);
4725 /* Note that this is harmless as -1 | 1 still is -1. */
4726 h->got.offset |= 1;
4727 }
4728 }
4729 value = (base_got->output_section->vma
4730 + base_got->output_offset + off);
4731 }
4732 else
4733 value = aarch64_calculate_got_entry_vma (h, globals, info,
4734 value, output_bfd,
4735 unresolved_reloc_p);
4736 if (bfd_r_type == BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15
4737 || bfd_r_type == BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14)
4738 addend = (globals->root.sgot->output_section->vma
4739 + globals->root.sgot->output_offset);
4740 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
4741 addend, weak_undef_p);
4742 return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type, howto, value);
4743 case BFD_RELOC_AARCH64_ADD_LO12:
4744 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
4745 break;
4746 }
4747 }
4748
4749 switch (bfd_r_type)
4750 {
4751 case BFD_RELOC_AARCH64_NONE:
4752 case BFD_RELOC_AARCH64_TLSDESC_CALL:
4753 *unresolved_reloc_p = FALSE;
4754 return bfd_reloc_ok;
4755
4756 case BFD_RELOC_AARCH64_NN:
4757
4758 /* When generating a shared object or relocatable executable, these
4759 relocations are copied into the output file to be resolved at
4760 run time. */
4761 if (((info->shared == TRUE) || globals->root.is_relocatable_executable)
4762 && (input_section->flags & SEC_ALLOC)
4763 && (h == NULL
4764 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4765 || h->root.type != bfd_link_hash_undefweak))
4766 {
4767 Elf_Internal_Rela outrel;
4768 bfd_byte *loc;
4769 bfd_boolean skip, relocate;
4770 asection *sreloc;
4771
4772 *unresolved_reloc_p = FALSE;
4773
4774 skip = FALSE;
4775 relocate = FALSE;
4776
4777 outrel.r_addend = signed_addend;
4778 outrel.r_offset =
4779 _bfd_elf_section_offset (output_bfd, info, input_section,
4780 rel->r_offset);
4781 if (outrel.r_offset == (bfd_vma) - 1)
4782 skip = TRUE;
4783 else if (outrel.r_offset == (bfd_vma) - 2)
4784 {
4785 skip = TRUE;
4786 relocate = TRUE;
4787 }
4788
4789 outrel.r_offset += (input_section->output_section->vma
4790 + input_section->output_offset);
4791
4792 if (skip)
4793 memset (&outrel, 0, sizeof outrel);
4794 else if (h != NULL
4795 && h->dynindx != -1
4796 && (!info->shared || !SYMBOLIC_BIND (info, h) || !h->def_regular))
4797 outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
4798 else
4799 {
4800 int symbol;
4801
4802 /* On SVR4-ish systems, the dynamic loader cannot
4803 relocate the text and data segments independently,
4804 so the symbol does not matter. */
4805 symbol = 0;
4806 outrel.r_info = ELFNN_R_INFO (symbol, AARCH64_R (RELATIVE));
4807 outrel.r_addend += value;
4808 }
4809
4810 sreloc = elf_section_data (input_section)->sreloc;
4811 if (sreloc == NULL || sreloc->contents == NULL)
4812 return bfd_reloc_notsupported;
4813
4814 loc = sreloc->contents + sreloc->reloc_count++ * RELOC_SIZE (globals);
4815 bfd_elfNN_swap_reloca_out (output_bfd, &outrel, loc);
4816
4817 if (sreloc->reloc_count * RELOC_SIZE (globals) > sreloc->size)
4818 {
4819 /* Sanity to check that we have previously allocated
4820 sufficient space in the relocation section for the
4821 number of relocations we actually want to emit. */
4822 abort ();
4823 }
4824
4825 /* If this reloc is against an external symbol, we do not want to
4826 fiddle with the addend. Otherwise, we need to include the symbol
4827 value so that it becomes an addend for the dynamic reloc. */
4828 if (!relocate)
4829 return bfd_reloc_ok;
4830
4831 return _bfd_final_link_relocate (howto, input_bfd, input_section,
4832 contents, rel->r_offset, value,
4833 signed_addend);
4834 }
4835 else
4836 value += signed_addend;
4837 break;
4838
4839 case BFD_RELOC_AARCH64_CALL26:
4840 case BFD_RELOC_AARCH64_JUMP26:
4841 {
4842 asection *splt = globals->root.splt;
4843 bfd_boolean via_plt_p =
4844 splt != NULL && h != NULL && h->plt.offset != (bfd_vma) - 1;
4845
4846 /* A call to an undefined weak symbol is converted to a jump to
4847 the next instruction unless a PLT entry will be created.
4848 The jump to the next instruction is optimized as a NOP.
4849 Do the same for local undefined symbols. */
4850 if (weak_undef_p && ! via_plt_p)
4851 {
4852 bfd_putl32 (INSN_NOP, hit_data);
4853 return bfd_reloc_ok;
4854 }
4855
4856 /* If the call goes through a PLT entry, make sure to
4857 check distance to the right destination address. */
4858 if (via_plt_p)
4859 {
4860 value = (splt->output_section->vma
4861 + splt->output_offset + h->plt.offset);
4862 *unresolved_reloc_p = FALSE;
4863 }
4864
4865 /* If the target symbol is global and marked as a function the
4866 relocation applies a function call or a tail call. In this
4867 situation we can veneer out of range branches. The veneers
4868 use IP0 and IP1 hence cannot be used arbitrary out of range
4869 branches that occur within the body of a function. */
4870 if (h && h->type == STT_FUNC)
4871 {
4872 /* Check if a stub has to be inserted because the destination
4873 is too far away. */
4874 if (! aarch64_valid_branch_p (value, place))
4875 {
4876 /* The target is out of reach, so redirect the branch to
4877 the local stub for this function. */
4878 struct elf_aarch64_stub_hash_entry *stub_entry;
4879 stub_entry = elfNN_aarch64_get_stub_entry (input_section,
4880 sym_sec, h,
4881 rel, globals);
4882 if (stub_entry != NULL)
4883 value = (stub_entry->stub_offset
4884 + stub_entry->stub_sec->output_offset
4885 + stub_entry->stub_sec->output_section->vma);
4886 }
4887 }
4888 }
4889 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
4890 signed_addend, weak_undef_p);
4891 break;
4892
4893 case BFD_RELOC_AARCH64_16_PCREL:
4894 case BFD_RELOC_AARCH64_32_PCREL:
4895 case BFD_RELOC_AARCH64_64_PCREL:
4896 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
4897 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
4898 case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
4899 case BFD_RELOC_AARCH64_LD_LO19_PCREL:
4900 if (info->shared
4901 && (input_section->flags & SEC_ALLOC) != 0
4902 && (input_section->flags & SEC_READONLY) != 0
4903 && h != NULL
4904 && !h->def_regular)
4905 {
4906 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
4907
4908 (*_bfd_error_handler)
4909 (_("%B: relocation %s against external symbol `%s' can not be used"
4910 " when making a shared object; recompile with -fPIC"),
4911 input_bfd, elfNN_aarch64_howto_table[howto_index].name,
4912 h->root.root.string);
4913 bfd_set_error (bfd_error_bad_value);
4914 return FALSE;
4915 }
4916
4917 case BFD_RELOC_AARCH64_16:
4918 #if ARCH_SIZE == 64
4919 case BFD_RELOC_AARCH64_32:
4920 #endif
4921 case BFD_RELOC_AARCH64_ADD_LO12:
4922 case BFD_RELOC_AARCH64_BRANCH19:
4923 case BFD_RELOC_AARCH64_LDST128_LO12:
4924 case BFD_RELOC_AARCH64_LDST16_LO12:
4925 case BFD_RELOC_AARCH64_LDST32_LO12:
4926 case BFD_RELOC_AARCH64_LDST64_LO12:
4927 case BFD_RELOC_AARCH64_LDST8_LO12:
4928 case BFD_RELOC_AARCH64_MOVW_G0:
4929 case BFD_RELOC_AARCH64_MOVW_G0_NC:
4930 case BFD_RELOC_AARCH64_MOVW_G0_S:
4931 case BFD_RELOC_AARCH64_MOVW_G1:
4932 case BFD_RELOC_AARCH64_MOVW_G1_NC:
4933 case BFD_RELOC_AARCH64_MOVW_G1_S:
4934 case BFD_RELOC_AARCH64_MOVW_G2:
4935 case BFD_RELOC_AARCH64_MOVW_G2_NC:
4936 case BFD_RELOC_AARCH64_MOVW_G2_S:
4937 case BFD_RELOC_AARCH64_MOVW_G3:
4938 case BFD_RELOC_AARCH64_TSTBR14:
4939 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
4940 signed_addend, weak_undef_p);
4941 break;
4942
4943 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
4944 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
4945 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
4946 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
4947 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
4948 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
4949 if (globals->root.sgot == NULL)
4950 BFD_ASSERT (h != NULL);
4951
4952 if (h != NULL)
4953 {
4954 bfd_vma addend = 0;
4955 value = aarch64_calculate_got_entry_vma (h, globals, info, value,
4956 output_bfd,
4957 unresolved_reloc_p);
4958 if (bfd_r_type == BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15
4959 || bfd_r_type == BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14)
4960 addend = (globals->root.sgot->output_section->vma
4961 + globals->root.sgot->output_offset);
4962 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
4963 addend, weak_undef_p);
4964 }
4965 else
4966 {
4967 bfd_vma addend = 0;
4968 struct elf_aarch64_local_symbol *locals
4969 = elf_aarch64_locals (input_bfd);
4970
4971 if (locals == NULL)
4972 {
4973 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
4974 (*_bfd_error_handler)
4975 (_("%B: Local symbol descriptor table be NULL when applying "
4976 "relocation %s against local symbol"),
4977 input_bfd, elfNN_aarch64_howto_table[howto_index].name);
4978 abort ();
4979 }
4980
4981 off = symbol_got_offset (input_bfd, h, r_symndx);
4982 base_got = globals->root.sgot;
4983 bfd_vma got_entry_addr = (base_got->output_section->vma
4984 + base_got->output_offset + off);
4985
4986 if (!symbol_got_offset_mark_p (input_bfd, h, r_symndx))
4987 {
4988 bfd_put_64 (output_bfd, value, base_got->contents + off);
4989
4990 if (info->shared)
4991 {
4992 asection *s;
4993 Elf_Internal_Rela outrel;
4994
4995 /* For local symbol, we have done absolute relocation in static
4996 linking stageh. While for share library, we need to update
4997 the content of GOT entry according to the share objects
4998 loading base address. So we need to generate a
4999 R_AARCH64_RELATIVE reloc for dynamic linker. */
5000 s = globals->root.srelgot;
5001 if (s == NULL)
5002 abort ();
5003
5004 outrel.r_offset = got_entry_addr;
5005 outrel.r_info = ELFNN_R_INFO (0, AARCH64_R (RELATIVE));
5006 outrel.r_addend = value;
5007 elf_append_rela (output_bfd, s, &outrel);
5008 }
5009
5010 symbol_got_offset_mark (input_bfd, h, r_symndx);
5011 }
5012
5013 /* Update the relocation value to GOT entry addr as we have transformed
5014 the direct data access into indirect data access through GOT. */
5015 value = got_entry_addr;
5016
5017 if (bfd_r_type == BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15
5018 || bfd_r_type == BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14)
5019 addend = base_got->output_section->vma + base_got->output_offset;
5020
5021 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5022 addend, weak_undef_p);
5023 }
5024
5025 break;
5026
5027 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
5028 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
5029 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
5030 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5031 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
5032 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5033 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
5034 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
5035 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
5036 if (globals->root.sgot == NULL)
5037 return bfd_reloc_notsupported;
5038
5039 value = (symbol_got_offset (input_bfd, h, r_symndx)
5040 + globals->root.sgot->output_section->vma
5041 + globals->root.sgot->output_offset);
5042
5043 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5044 0, weak_undef_p);
5045 *unresolved_reloc_p = FALSE;
5046 break;
5047
5048 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
5049 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
5050 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5051 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
5052 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5053 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
5054 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5055 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
5056 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5057 signed_addend - tpoff_base (info),
5058 weak_undef_p);
5059 *unresolved_reloc_p = FALSE;
5060 break;
5061
5062 case BFD_RELOC_AARCH64_TLSDESC_ADD:
5063 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
5064 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
5065 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
5066 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
5067 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
5068 case BFD_RELOC_AARCH64_TLSDESC_LDR:
5069 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
5070 if (globals->root.sgot == NULL)
5071 return bfd_reloc_notsupported;
5072 value = (symbol_tlsdesc_got_offset (input_bfd, h, r_symndx)
5073 + globals->root.sgotplt->output_section->vma
5074 + globals->root.sgotplt->output_offset
5075 + globals->sgotplt_jump_table_size);
5076
5077 value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5078 0, weak_undef_p);
5079 *unresolved_reloc_p = FALSE;
5080 break;
5081
5082 default:
5083 return bfd_reloc_notsupported;
5084 }
5085
5086 if (saved_addend)
5087 *saved_addend = value;
5088
5089 /* Only apply the final relocation in a sequence. */
5090 if (save_addend)
5091 return bfd_reloc_continue;
5092
5093 return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type,
5094 howto, value);
5095 }
5096
5097 /* Handle TLS relaxations. Relaxing is possible for symbols that use
5098 R_AARCH64_TLSDESC_ADR_{PAGE, LD64_LO12_NC, ADD_LO12_NC} during a static
5099 link.
5100
5101 Return bfd_reloc_ok if we're done, bfd_reloc_continue if the caller
5102 is to then call final_link_relocate. Return other values in the
5103 case of error. */
5104
5105 static bfd_reloc_status_type
5106 elfNN_aarch64_tls_relax (struct elf_aarch64_link_hash_table *globals,
5107 bfd *input_bfd, bfd_byte *contents,
5108 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h)
5109 {
5110 bfd_boolean is_local = h == NULL;
5111 unsigned int r_type = ELFNN_R_TYPE (rel->r_info);
5112 unsigned long insn;
5113
5114 BFD_ASSERT (globals && input_bfd && contents && rel);
5115
5116 switch (elfNN_aarch64_bfd_reloc_from_type (r_type))
5117 {
5118 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
5119 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
5120 if (is_local)
5121 {
5122 /* GD->LE relaxation:
5123 adrp x0, :tlsgd:var => movz x0, :tprel_g1:var
5124 or
5125 adrp x0, :tlsdesc:var => movz x0, :tprel_g1:var
5126 */
5127 bfd_putl32 (0xd2a00000, contents + rel->r_offset);
5128 return bfd_reloc_continue;
5129 }
5130 else
5131 {
5132 /* GD->IE relaxation:
5133 adrp x0, :tlsgd:var => adrp x0, :gottprel:var
5134 or
5135 adrp x0, :tlsdesc:var => adrp x0, :gottprel:var
5136 */
5137 return bfd_reloc_continue;
5138 }
5139
5140 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
5141 BFD_ASSERT (0);
5142 break;
5143
5144 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
5145 if (is_local)
5146 {
5147 /* Tiny TLSDESC->LE relaxation:
5148 ldr x1, :tlsdesc:var => movz x0, #:tprel_g1:var
5149 adr x0, :tlsdesc:var => movk x0, #:tprel_g0_nc:var
5150 .tlsdesccall var
5151 blr x1 => nop
5152 */
5153 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (TLSDESC_ADR_PREL21));
5154 BFD_ASSERT (ELFNN_R_TYPE (rel[2].r_info) == AARCH64_R (TLSDESC_CALL));
5155
5156 rel[1].r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
5157 AARCH64_R (TLSLE_MOVW_TPREL_G0_NC));
5158 rel[2].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5159
5160 bfd_putl32 (0xd2a00000, contents + rel->r_offset);
5161 bfd_putl32 (0xf2800000, contents + rel->r_offset + 4);
5162 bfd_putl32 (INSN_NOP, contents + rel->r_offset + 8);
5163 return bfd_reloc_continue;
5164 }
5165 else
5166 {
5167 /* Tiny TLSDESC->IE relaxation:
5168 ldr x1, :tlsdesc:var => ldr x0, :gottprel:var
5169 adr x0, :tlsdesc:var => nop
5170 .tlsdesccall var
5171 blr x1 => nop
5172 */
5173 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (TLSDESC_ADR_PREL21));
5174 BFD_ASSERT (ELFNN_R_TYPE (rel[2].r_info) == AARCH64_R (TLSDESC_CALL));
5175
5176 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5177 rel[2].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5178
5179 bfd_putl32 (0x58000000, contents + rel->r_offset);
5180 bfd_putl32 (INSN_NOP, contents + rel->r_offset + 4);
5181 bfd_putl32 (INSN_NOP, contents + rel->r_offset + 8);
5182 return bfd_reloc_continue;
5183 }
5184
5185 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
5186 if (is_local)
5187 {
5188 /* Tiny GD->LE relaxation:
5189 adr x0, :tlsgd:var => mrs x1, tpidr_el0
5190 bl __tls_get_addr => add x0, x1, #:tprel_hi12:x, lsl #12
5191 nop => add x0, x0, #:tprel_lo12_nc:x
5192 */
5193
5194 /* First kill the tls_get_addr reloc on the bl instruction. */
5195 BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
5196
5197 bfd_putl32 (0xd53bd041, contents + rel->r_offset + 0);
5198 bfd_putl32 (0x91400020, contents + rel->r_offset + 4);
5199 bfd_putl32 (0x91000000, contents + rel->r_offset + 8);
5200
5201 rel[1].r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
5202 AARCH64_R (TLSLE_ADD_TPREL_LO12_NC));
5203 rel[1].r_offset = rel->r_offset + 8;
5204
5205 /* Move the current relocation to the second instruction in
5206 the sequence. */
5207 rel->r_offset += 4;
5208 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
5209 AARCH64_R (TLSLE_ADD_TPREL_HI12));
5210 return bfd_reloc_continue;
5211 }
5212 else
5213 {
5214 /* Tiny GD->IE relaxation:
5215 adr x0, :tlsgd:var => ldr x0, :gottprel:var
5216 bl __tls_get_addr => mrs x1, tpidr_el0
5217 nop => add x0, x0, x1
5218 */
5219
5220 /* First kill the tls_get_addr reloc on the bl instruction. */
5221 BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
5222 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5223
5224 bfd_putl32 (0x58000000, contents + rel->r_offset);
5225 bfd_putl32 (0xd53bd041, contents + rel->r_offset + 4);
5226 bfd_putl32 (0x8b000020, contents + rel->r_offset + 8);
5227 return bfd_reloc_continue;
5228 }
5229
5230 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
5231 return bfd_reloc_continue;
5232
5233 case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
5234 if (is_local)
5235 {
5236 /* GD->LE relaxation:
5237 ldr xd, [x0, #:tlsdesc_lo12:var] => movk x0, :tprel_g0_nc:var
5238 */
5239 bfd_putl32 (0xf2800000, contents + rel->r_offset);
5240 return bfd_reloc_continue;
5241 }
5242 else
5243 {
5244 /* GD->IE relaxation:
5245 ldr xd, [x0, #:tlsdesc_lo12:var] => ldr x0, [x0, #:gottprel_lo12:var]
5246 */
5247 insn = bfd_getl32 (contents + rel->r_offset);
5248 insn &= 0xffffffe0;
5249 bfd_putl32 (insn, contents + rel->r_offset);
5250 return bfd_reloc_continue;
5251 }
5252
5253 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
5254 if (is_local)
5255 {
5256 /* GD->LE relaxation
5257 add x0, #:tlsgd_lo12:var => movk x0, :tprel_g0_nc:var
5258 bl __tls_get_addr => mrs x1, tpidr_el0
5259 nop => add x0, x1, x0
5260 */
5261
5262 /* First kill the tls_get_addr reloc on the bl instruction. */
5263 BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
5264 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5265
5266 bfd_putl32 (0xf2800000, contents + rel->r_offset);
5267 bfd_putl32 (0xd53bd041, contents + rel->r_offset + 4);
5268 bfd_putl32 (0x8b000020, contents + rel->r_offset + 8);
5269 return bfd_reloc_continue;
5270 }
5271 else
5272 {
5273 /* GD->IE relaxation
5274 ADD x0, #:tlsgd_lo12:var => ldr x0, [x0, #:gottprel_lo12:var]
5275 BL __tls_get_addr => mrs x1, tpidr_el0
5276 R_AARCH64_CALL26
5277 NOP => add x0, x1, x0
5278 */
5279
5280 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
5281
5282 /* Remove the relocation on the BL instruction. */
5283 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5284
5285 bfd_putl32 (0xf9400000, contents + rel->r_offset);
5286
5287 /* We choose to fixup the BL and NOP instructions using the
5288 offset from the second relocation to allow flexibility in
5289 scheduling instructions between the ADD and BL. */
5290 bfd_putl32 (0xd53bd041, contents + rel[1].r_offset);
5291 bfd_putl32 (0x8b000020, contents + rel[1].r_offset + 4);
5292 return bfd_reloc_continue;
5293 }
5294
5295 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
5296 case BFD_RELOC_AARCH64_TLSDESC_CALL:
5297 /* GD->IE/LE relaxation:
5298 add x0, x0, #:tlsdesc_lo12:var => nop
5299 blr xd => nop
5300 */
5301 bfd_putl32 (INSN_NOP, contents + rel->r_offset);
5302 return bfd_reloc_ok;
5303
5304 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5305 /* IE->LE relaxation:
5306 adrp xd, :gottprel:var => movz xd, :tprel_g1:var
5307 */
5308 if (is_local)
5309 {
5310 insn = bfd_getl32 (contents + rel->r_offset);
5311 bfd_putl32 (0xd2a00000 | (insn & 0x1f), contents + rel->r_offset);
5312 }
5313 return bfd_reloc_continue;
5314
5315 case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
5316 /* IE->LE relaxation:
5317 ldr xd, [xm, #:gottprel_lo12:var] => movk xd, :tprel_g0_nc:var
5318 */
5319 if (is_local)
5320 {
5321 insn = bfd_getl32 (contents + rel->r_offset);
5322 bfd_putl32 (0xf2800000 | (insn & 0x1f), contents + rel->r_offset);
5323 }
5324 return bfd_reloc_continue;
5325
5326 default:
5327 return bfd_reloc_continue;
5328 }
5329
5330 return bfd_reloc_ok;
5331 }
5332
5333 /* Relocate an AArch64 ELF section. */
5334
5335 static bfd_boolean
5336 elfNN_aarch64_relocate_section (bfd *output_bfd,
5337 struct bfd_link_info *info,
5338 bfd *input_bfd,
5339 asection *input_section,
5340 bfd_byte *contents,
5341 Elf_Internal_Rela *relocs,
5342 Elf_Internal_Sym *local_syms,
5343 asection **local_sections)
5344 {
5345 Elf_Internal_Shdr *symtab_hdr;
5346 struct elf_link_hash_entry **sym_hashes;
5347 Elf_Internal_Rela *rel;
5348 Elf_Internal_Rela *relend;
5349 const char *name;
5350 struct elf_aarch64_link_hash_table *globals;
5351 bfd_boolean save_addend = FALSE;
5352 bfd_vma addend = 0;
5353
5354 globals = elf_aarch64_hash_table (info);
5355
5356 symtab_hdr = &elf_symtab_hdr (input_bfd);
5357 sym_hashes = elf_sym_hashes (input_bfd);
5358
5359 rel = relocs;
5360 relend = relocs + input_section->reloc_count;
5361 for (; rel < relend; rel++)
5362 {
5363 unsigned int r_type;
5364 bfd_reloc_code_real_type bfd_r_type;
5365 bfd_reloc_code_real_type relaxed_bfd_r_type;
5366 reloc_howto_type *howto;
5367 unsigned long r_symndx;
5368 Elf_Internal_Sym *sym;
5369 asection *sec;
5370 struct elf_link_hash_entry *h;
5371 bfd_vma relocation;
5372 bfd_reloc_status_type r;
5373 arelent bfd_reloc;
5374 char sym_type;
5375 bfd_boolean unresolved_reloc = FALSE;
5376 char *error_message = NULL;
5377
5378 r_symndx = ELFNN_R_SYM (rel->r_info);
5379 r_type = ELFNN_R_TYPE (rel->r_info);
5380
5381 bfd_reloc.howto = elfNN_aarch64_howto_from_type (r_type);
5382 howto = bfd_reloc.howto;
5383
5384 if (howto == NULL)
5385 {
5386 (*_bfd_error_handler)
5387 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
5388 input_bfd, input_section, r_type);
5389 return FALSE;
5390 }
5391 bfd_r_type = elfNN_aarch64_bfd_reloc_from_howto (howto);
5392
5393 h = NULL;
5394 sym = NULL;
5395 sec = NULL;
5396
5397 if (r_symndx < symtab_hdr->sh_info)
5398 {
5399 sym = local_syms + r_symndx;
5400 sym_type = ELFNN_ST_TYPE (sym->st_info);
5401 sec = local_sections[r_symndx];
5402
5403 /* An object file might have a reference to a local
5404 undefined symbol. This is a daft object file, but we
5405 should at least do something about it. */
5406 if (r_type != R_AARCH64_NONE && r_type != R_AARCH64_NULL
5407 && bfd_is_und_section (sec)
5408 && ELF_ST_BIND (sym->st_info) != STB_WEAK)
5409 {
5410 if (!info->callbacks->undefined_symbol
5411 (info, bfd_elf_string_from_elf_section
5412 (input_bfd, symtab_hdr->sh_link, sym->st_name),
5413 input_bfd, input_section, rel->r_offset, TRUE))
5414 return FALSE;
5415 }
5416
5417 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
5418
5419 /* Relocate against local STT_GNU_IFUNC symbol. */
5420 if (!info->relocatable
5421 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
5422 {
5423 h = elfNN_aarch64_get_local_sym_hash (globals, input_bfd,
5424 rel, FALSE);
5425 if (h == NULL)
5426 abort ();
5427
5428 /* Set STT_GNU_IFUNC symbol value. */
5429 h->root.u.def.value = sym->st_value;
5430 h->root.u.def.section = sec;
5431 }
5432 }
5433 else
5434 {
5435 bfd_boolean warned, ignored;
5436
5437 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5438 r_symndx, symtab_hdr, sym_hashes,
5439 h, sec, relocation,
5440 unresolved_reloc, warned, ignored);
5441
5442 sym_type = h->type;
5443 }
5444
5445 if (sec != NULL && discarded_section (sec))
5446 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
5447 rel, 1, relend, howto, 0, contents);
5448
5449 if (info->relocatable)
5450 continue;
5451
5452 if (h != NULL)
5453 name = h->root.root.string;
5454 else
5455 {
5456 name = (bfd_elf_string_from_elf_section
5457 (input_bfd, symtab_hdr->sh_link, sym->st_name));
5458 if (name == NULL || *name == '\0')
5459 name = bfd_section_name (input_bfd, sec);
5460 }
5461
5462 if (r_symndx != 0
5463 && r_type != R_AARCH64_NONE
5464 && r_type != R_AARCH64_NULL
5465 && (h == NULL
5466 || h->root.type == bfd_link_hash_defined
5467 || h->root.type == bfd_link_hash_defweak)
5468 && IS_AARCH64_TLS_RELOC (bfd_r_type) != (sym_type == STT_TLS))
5469 {
5470 (*_bfd_error_handler)
5471 ((sym_type == STT_TLS
5472 ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
5473 : _("%B(%A+0x%lx): %s used with non-TLS symbol %s")),
5474 input_bfd,
5475 input_section, (long) rel->r_offset, howto->name, name);
5476 }
5477
5478 /* We relax only if we can see that there can be a valid transition
5479 from a reloc type to another.
5480 We call elfNN_aarch64_final_link_relocate unless we're completely
5481 done, i.e., the relaxation produced the final output we want. */
5482
5483 relaxed_bfd_r_type = aarch64_tls_transition (input_bfd, info, r_type,
5484 h, r_symndx);
5485 if (relaxed_bfd_r_type != bfd_r_type)
5486 {
5487 bfd_r_type = relaxed_bfd_r_type;
5488 howto = elfNN_aarch64_howto_from_bfd_reloc (bfd_r_type);
5489 BFD_ASSERT (howto != NULL);
5490 r_type = howto->type;
5491 r = elfNN_aarch64_tls_relax (globals, input_bfd, contents, rel, h);
5492 unresolved_reloc = 0;
5493 }
5494 else
5495 r = bfd_reloc_continue;
5496
5497 /* There may be multiple consecutive relocations for the
5498 same offset. In that case we are supposed to treat the
5499 output of each relocation as the addend for the next. */
5500 if (rel + 1 < relend
5501 && rel->r_offset == rel[1].r_offset
5502 && ELFNN_R_TYPE (rel[1].r_info) != R_AARCH64_NONE
5503 && ELFNN_R_TYPE (rel[1].r_info) != R_AARCH64_NULL)
5504 save_addend = TRUE;
5505 else
5506 save_addend = FALSE;
5507
5508 if (r == bfd_reloc_continue)
5509 r = elfNN_aarch64_final_link_relocate (howto, input_bfd, output_bfd,
5510 input_section, contents, rel,
5511 relocation, info, sec,
5512 h, &unresolved_reloc,
5513 save_addend, &addend, sym);
5514
5515 switch (elfNN_aarch64_bfd_reloc_from_type (r_type))
5516 {
5517 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
5518 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
5519 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
5520 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
5521 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
5522 if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
5523 {
5524 bfd_boolean need_relocs = FALSE;
5525 bfd_byte *loc;
5526 int indx;
5527 bfd_vma off;
5528
5529 off = symbol_got_offset (input_bfd, h, r_symndx);
5530 indx = h && h->dynindx != -1 ? h->dynindx : 0;
5531
5532 need_relocs =
5533 (info->shared || indx != 0) &&
5534 (h == NULL
5535 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5536 || h->root.type != bfd_link_hash_undefweak);
5537
5538 BFD_ASSERT (globals->root.srelgot != NULL);
5539
5540 if (need_relocs)
5541 {
5542 Elf_Internal_Rela rela;
5543 rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLS_DTPMOD));
5544 rela.r_addend = 0;
5545 rela.r_offset = globals->root.sgot->output_section->vma +
5546 globals->root.sgot->output_offset + off;
5547
5548
5549 loc = globals->root.srelgot->contents;
5550 loc += globals->root.srelgot->reloc_count++
5551 * RELOC_SIZE (htab);
5552 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
5553
5554 bfd_reloc_code_real_type real_type =
5555 elfNN_aarch64_bfd_reloc_from_type (r_type);
5556
5557 if (real_type == BFD_RELOC_AARCH64_TLSLD_ADR_PREL21
5558 || real_type == BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21)
5559 {
5560 /* For local dynamic, don't generate DTPREL in any case.
5561 Initialize the DTPREL slot into zero, so we get module
5562 base address when invoke runtime TLS resolver. */
5563 bfd_put_NN (output_bfd, 0,
5564 globals->root.sgot->contents + off
5565 + GOT_ENTRY_SIZE);
5566 }
5567 else if (indx == 0)
5568 {
5569 bfd_put_NN (output_bfd,
5570 relocation - dtpoff_base (info),
5571 globals->root.sgot->contents + off
5572 + GOT_ENTRY_SIZE);
5573 }
5574 else
5575 {
5576 /* This TLS symbol is global. We emit a
5577 relocation to fixup the tls offset at load
5578 time. */
5579 rela.r_info =
5580 ELFNN_R_INFO (indx, AARCH64_R (TLS_DTPREL));
5581 rela.r_addend = 0;
5582 rela.r_offset =
5583 (globals->root.sgot->output_section->vma
5584 + globals->root.sgot->output_offset + off
5585 + GOT_ENTRY_SIZE);
5586
5587 loc = globals->root.srelgot->contents;
5588 loc += globals->root.srelgot->reloc_count++
5589 * RELOC_SIZE (globals);
5590 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
5591 bfd_put_NN (output_bfd, (bfd_vma) 0,
5592 globals->root.sgot->contents + off
5593 + GOT_ENTRY_SIZE);
5594 }
5595 }
5596 else
5597 {
5598 bfd_put_NN (output_bfd, (bfd_vma) 1,
5599 globals->root.sgot->contents + off);
5600 bfd_put_NN (output_bfd,
5601 relocation - dtpoff_base (info),
5602 globals->root.sgot->contents + off
5603 + GOT_ENTRY_SIZE);
5604 }
5605
5606 symbol_got_offset_mark (input_bfd, h, r_symndx);
5607 }
5608 break;
5609
5610 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5611 case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
5612 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
5613 if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
5614 {
5615 bfd_boolean need_relocs = FALSE;
5616 bfd_byte *loc;
5617 int indx;
5618 bfd_vma off;
5619
5620 off = symbol_got_offset (input_bfd, h, r_symndx);
5621
5622 indx = h && h->dynindx != -1 ? h->dynindx : 0;
5623
5624 need_relocs =
5625 (info->shared || indx != 0) &&
5626 (h == NULL
5627 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5628 || h->root.type != bfd_link_hash_undefweak);
5629
5630 BFD_ASSERT (globals->root.srelgot != NULL);
5631
5632 if (need_relocs)
5633 {
5634 Elf_Internal_Rela rela;
5635
5636 if (indx == 0)
5637 rela.r_addend = relocation - dtpoff_base (info);
5638 else
5639 rela.r_addend = 0;
5640
5641 rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLS_TPREL));
5642 rela.r_offset = globals->root.sgot->output_section->vma +
5643 globals->root.sgot->output_offset + off;
5644
5645 loc = globals->root.srelgot->contents;
5646 loc += globals->root.srelgot->reloc_count++
5647 * RELOC_SIZE (htab);
5648
5649 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
5650
5651 bfd_put_NN (output_bfd, rela.r_addend,
5652 globals->root.sgot->contents + off);
5653 }
5654 else
5655 bfd_put_NN (output_bfd, relocation - tpoff_base (info),
5656 globals->root.sgot->contents + off);
5657
5658 symbol_got_offset_mark (input_bfd, h, r_symndx);
5659 }
5660 break;
5661
5662 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
5663 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
5664 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5665 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
5666 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5667 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
5668 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5669 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
5670 break;
5671
5672 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
5673 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
5674 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
5675 case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
5676 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
5677 if (! symbol_tlsdesc_got_offset_mark_p (input_bfd, h, r_symndx))
5678 {
5679 bfd_boolean need_relocs = FALSE;
5680 int indx = h && h->dynindx != -1 ? h->dynindx : 0;
5681 bfd_vma off = symbol_tlsdesc_got_offset (input_bfd, h, r_symndx);
5682
5683 need_relocs = (h == NULL
5684 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5685 || h->root.type != bfd_link_hash_undefweak);
5686
5687 BFD_ASSERT (globals->root.srelgot != NULL);
5688 BFD_ASSERT (globals->root.sgot != NULL);
5689
5690 if (need_relocs)
5691 {
5692 bfd_byte *loc;
5693 Elf_Internal_Rela rela;
5694 rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLSDESC));
5695
5696 rela.r_addend = 0;
5697 rela.r_offset = (globals->root.sgotplt->output_section->vma
5698 + globals->root.sgotplt->output_offset
5699 + off + globals->sgotplt_jump_table_size);
5700
5701 if (indx == 0)
5702 rela.r_addend = relocation - dtpoff_base (info);
5703
5704 /* Allocate the next available slot in the PLT reloc
5705 section to hold our R_AARCH64_TLSDESC, the next
5706 available slot is determined from reloc_count,
5707 which we step. But note, reloc_count was
5708 artifically moved down while allocating slots for
5709 real PLT relocs such that all of the PLT relocs
5710 will fit above the initial reloc_count and the
5711 extra stuff will fit below. */
5712 loc = globals->root.srelplt->contents;
5713 loc += globals->root.srelplt->reloc_count++
5714 * RELOC_SIZE (globals);
5715
5716 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
5717
5718 bfd_put_NN (output_bfd, (bfd_vma) 0,
5719 globals->root.sgotplt->contents + off +
5720 globals->sgotplt_jump_table_size);
5721 bfd_put_NN (output_bfd, (bfd_vma) 0,
5722 globals->root.sgotplt->contents + off +
5723 globals->sgotplt_jump_table_size +
5724 GOT_ENTRY_SIZE);
5725 }
5726
5727 symbol_tlsdesc_got_offset_mark (input_bfd, h, r_symndx);
5728 }
5729 break;
5730 default:
5731 break;
5732 }
5733
5734 if (!save_addend)
5735 addend = 0;
5736
5737
5738 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
5739 because such sections are not SEC_ALLOC and thus ld.so will
5740 not process them. */
5741 if (unresolved_reloc
5742 && !((input_section->flags & SEC_DEBUGGING) != 0
5743 && h->def_dynamic)
5744 && _bfd_elf_section_offset (output_bfd, info, input_section,
5745 +rel->r_offset) != (bfd_vma) - 1)
5746 {
5747 (*_bfd_error_handler)
5748 (_
5749 ("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
5750 input_bfd, input_section, (long) rel->r_offset, howto->name,
5751 h->root.root.string);
5752 return FALSE;
5753 }
5754
5755 if (r != bfd_reloc_ok && r != bfd_reloc_continue)
5756 {
5757 switch (r)
5758 {
5759 case bfd_reloc_overflow:
5760 if (!(*info->callbacks->reloc_overflow)
5761 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
5762 input_bfd, input_section, rel->r_offset))
5763 return FALSE;
5764 break;
5765
5766 case bfd_reloc_undefined:
5767 if (!((*info->callbacks->undefined_symbol)
5768 (info, name, input_bfd, input_section,
5769 rel->r_offset, TRUE)))
5770 return FALSE;
5771 break;
5772
5773 case bfd_reloc_outofrange:
5774 error_message = _("out of range");
5775 goto common_error;
5776
5777 case bfd_reloc_notsupported:
5778 error_message = _("unsupported relocation");
5779 goto common_error;
5780
5781 case bfd_reloc_dangerous:
5782 /* error_message should already be set. */
5783 goto common_error;
5784
5785 default:
5786 error_message = _("unknown error");
5787 /* Fall through. */
5788
5789 common_error:
5790 BFD_ASSERT (error_message != NULL);
5791 if (!((*info->callbacks->reloc_dangerous)
5792 (info, error_message, input_bfd, input_section,
5793 rel->r_offset)))
5794 return FALSE;
5795 break;
5796 }
5797 }
5798 }
5799
5800 return TRUE;
5801 }
5802
5803 /* Set the right machine number. */
5804
5805 static bfd_boolean
5806 elfNN_aarch64_object_p (bfd *abfd)
5807 {
5808 #if ARCH_SIZE == 32
5809 bfd_default_set_arch_mach (abfd, bfd_arch_aarch64, bfd_mach_aarch64_ilp32);
5810 #else
5811 bfd_default_set_arch_mach (abfd, bfd_arch_aarch64, bfd_mach_aarch64);
5812 #endif
5813 return TRUE;
5814 }
5815
5816 /* Function to keep AArch64 specific flags in the ELF header. */
5817
5818 static bfd_boolean
5819 elfNN_aarch64_set_private_flags (bfd *abfd, flagword flags)
5820 {
5821 if (elf_flags_init (abfd) && elf_elfheader (abfd)->e_flags != flags)
5822 {
5823 }
5824 else
5825 {
5826 elf_elfheader (abfd)->e_flags = flags;
5827 elf_flags_init (abfd) = TRUE;
5828 }
5829
5830 return TRUE;
5831 }
5832
5833 /* Merge backend specific data from an object file to the output
5834 object file when linking. */
5835
5836 static bfd_boolean
5837 elfNN_aarch64_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5838 {
5839 flagword out_flags;
5840 flagword in_flags;
5841 bfd_boolean flags_compatible = TRUE;
5842 asection *sec;
5843
5844 /* Check if we have the same endianess. */
5845 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
5846 return FALSE;
5847
5848 if (!is_aarch64_elf (ibfd) || !is_aarch64_elf (obfd))
5849 return TRUE;
5850
5851 /* The input BFD must have had its flags initialised. */
5852 /* The following seems bogus to me -- The flags are initialized in
5853 the assembler but I don't think an elf_flags_init field is
5854 written into the object. */
5855 /* BFD_ASSERT (elf_flags_init (ibfd)); */
5856
5857 in_flags = elf_elfheader (ibfd)->e_flags;
5858 out_flags = elf_elfheader (obfd)->e_flags;
5859
5860 if (!elf_flags_init (obfd))
5861 {
5862 /* If the input is the default architecture and had the default
5863 flags then do not bother setting the flags for the output
5864 architecture, instead allow future merges to do this. If no
5865 future merges ever set these flags then they will retain their
5866 uninitialised values, which surprise surprise, correspond
5867 to the default values. */
5868 if (bfd_get_arch_info (ibfd)->the_default
5869 && elf_elfheader (ibfd)->e_flags == 0)
5870 return TRUE;
5871
5872 elf_flags_init (obfd) = TRUE;
5873 elf_elfheader (obfd)->e_flags = in_flags;
5874
5875 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
5876 && bfd_get_arch_info (obfd)->the_default)
5877 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
5878 bfd_get_mach (ibfd));
5879
5880 return TRUE;
5881 }
5882
5883 /* Identical flags must be compatible. */
5884 if (in_flags == out_flags)
5885 return TRUE;
5886
5887 /* Check to see if the input BFD actually contains any sections. If
5888 not, its flags may not have been initialised either, but it
5889 cannot actually cause any incompatiblity. Do not short-circuit
5890 dynamic objects; their section list may be emptied by
5891 elf_link_add_object_symbols.
5892
5893 Also check to see if there are no code sections in the input.
5894 In this case there is no need to check for code specific flags.
5895 XXX - do we need to worry about floating-point format compatability
5896 in data sections ? */
5897 if (!(ibfd->flags & DYNAMIC))
5898 {
5899 bfd_boolean null_input_bfd = TRUE;
5900 bfd_boolean only_data_sections = TRUE;
5901
5902 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
5903 {
5904 if ((bfd_get_section_flags (ibfd, sec)
5905 & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
5906 == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
5907 only_data_sections = FALSE;
5908
5909 null_input_bfd = FALSE;
5910 break;
5911 }
5912
5913 if (null_input_bfd || only_data_sections)
5914 return TRUE;
5915 }
5916
5917 return flags_compatible;
5918 }
5919
5920 /* Display the flags field. */
5921
5922 static bfd_boolean
5923 elfNN_aarch64_print_private_bfd_data (bfd *abfd, void *ptr)
5924 {
5925 FILE *file = (FILE *) ptr;
5926 unsigned long flags;
5927
5928 BFD_ASSERT (abfd != NULL && ptr != NULL);
5929
5930 /* Print normal ELF private data. */
5931 _bfd_elf_print_private_bfd_data (abfd, ptr);
5932
5933 flags = elf_elfheader (abfd)->e_flags;
5934 /* Ignore init flag - it may not be set, despite the flags field
5935 containing valid data. */
5936
5937 /* xgettext:c-format */
5938 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
5939
5940 if (flags)
5941 fprintf (file, _("<Unrecognised flag bits set>"));
5942
5943 fputc ('\n', file);
5944
5945 return TRUE;
5946 }
5947
5948 /* Update the got entry reference counts for the section being removed. */
5949
5950 static bfd_boolean
5951 elfNN_aarch64_gc_sweep_hook (bfd *abfd,
5952 struct bfd_link_info *info,
5953 asection *sec,
5954 const Elf_Internal_Rela * relocs)
5955 {
5956 struct elf_aarch64_link_hash_table *htab;
5957 Elf_Internal_Shdr *symtab_hdr;
5958 struct elf_link_hash_entry **sym_hashes;
5959 struct elf_aarch64_local_symbol *locals;
5960 const Elf_Internal_Rela *rel, *relend;
5961
5962 if (info->relocatable)
5963 return TRUE;
5964
5965 htab = elf_aarch64_hash_table (info);
5966
5967 if (htab == NULL)
5968 return FALSE;
5969
5970 elf_section_data (sec)->local_dynrel = NULL;
5971
5972 symtab_hdr = &elf_symtab_hdr (abfd);
5973 sym_hashes = elf_sym_hashes (abfd);
5974
5975 locals = elf_aarch64_locals (abfd);
5976
5977 relend = relocs + sec->reloc_count;
5978 for (rel = relocs; rel < relend; rel++)
5979 {
5980 unsigned long r_symndx;
5981 unsigned int r_type;
5982 struct elf_link_hash_entry *h = NULL;
5983
5984 r_symndx = ELFNN_R_SYM (rel->r_info);
5985
5986 if (r_symndx >= symtab_hdr->sh_info)
5987 {
5988
5989 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5990 while (h->root.type == bfd_link_hash_indirect
5991 || h->root.type == bfd_link_hash_warning)
5992 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5993 }
5994 else
5995 {
5996 Elf_Internal_Sym *isym;
5997
5998 /* A local symbol. */
5999 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6000 abfd, r_symndx);
6001
6002 /* Check relocation against local STT_GNU_IFUNC symbol. */
6003 if (isym != NULL
6004 && ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
6005 {
6006 h = elfNN_aarch64_get_local_sym_hash (htab, abfd, rel, FALSE);
6007 if (h == NULL)
6008 abort ();
6009 }
6010 }
6011
6012 if (h)
6013 {
6014 struct elf_aarch64_link_hash_entry *eh;
6015 struct elf_dyn_relocs **pp;
6016 struct elf_dyn_relocs *p;
6017
6018 eh = (struct elf_aarch64_link_hash_entry *) h;
6019
6020 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6021 if (p->sec == sec)
6022 {
6023 /* Everything must go for SEC. */
6024 *pp = p->next;
6025 break;
6026 }
6027 }
6028
6029 r_type = ELFNN_R_TYPE (rel->r_info);
6030 switch (aarch64_tls_transition (abfd,info, r_type, h ,r_symndx))
6031 {
6032 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
6033 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
6034 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
6035 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
6036 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
6037 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
6038 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
6039 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
6040 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
6041 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
6042 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
6043 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
6044 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
6045 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
6046 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
6047 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6048 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
6049 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
6050 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
6051 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
6052 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
6053 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
6054 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
6055 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
6056 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
6057 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
6058 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
6059 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6060 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
6061 if (h != NULL)
6062 {
6063 if (h->got.refcount > 0)
6064 h->got.refcount -= 1;
6065
6066 if (h->type == STT_GNU_IFUNC)
6067 {
6068 if (h->plt.refcount > 0)
6069 h->plt.refcount -= 1;
6070 }
6071 }
6072 else if (locals != NULL)
6073 {
6074 if (locals[r_symndx].got_refcount > 0)
6075 locals[r_symndx].got_refcount -= 1;
6076 }
6077 break;
6078
6079 case BFD_RELOC_AARCH64_CALL26:
6080 case BFD_RELOC_AARCH64_JUMP26:
6081 /* If this is a local symbol then we resolve it
6082 directly without creating a PLT entry. */
6083 if (h == NULL)
6084 continue;
6085
6086 if (h->plt.refcount > 0)
6087 h->plt.refcount -= 1;
6088 break;
6089
6090 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
6091 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
6092 case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
6093 case BFD_RELOC_AARCH64_MOVW_G0_NC:
6094 case BFD_RELOC_AARCH64_MOVW_G1_NC:
6095 case BFD_RELOC_AARCH64_MOVW_G2_NC:
6096 case BFD_RELOC_AARCH64_MOVW_G3:
6097 case BFD_RELOC_AARCH64_NN:
6098 if (h != NULL && info->executable)
6099 {
6100 if (h->plt.refcount > 0)
6101 h->plt.refcount -= 1;
6102 }
6103 break;
6104
6105 default:
6106 break;
6107 }
6108 }
6109
6110 return TRUE;
6111 }
6112
6113 /* Adjust a symbol defined by a dynamic object and referenced by a
6114 regular object. The current definition is in some section of the
6115 dynamic object, but we're not including those sections. We have to
6116 change the definition to something the rest of the link can
6117 understand. */
6118
6119 static bfd_boolean
6120 elfNN_aarch64_adjust_dynamic_symbol (struct bfd_link_info *info,
6121 struct elf_link_hash_entry *h)
6122 {
6123 struct elf_aarch64_link_hash_table *htab;
6124 asection *s;
6125
6126 /* If this is a function, put it in the procedure linkage table. We
6127 will fill in the contents of the procedure linkage table later,
6128 when we know the address of the .got section. */
6129 if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
6130 {
6131 if (h->plt.refcount <= 0
6132 || (h->type != STT_GNU_IFUNC
6133 && (SYMBOL_CALLS_LOCAL (info, h)
6134 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6135 && h->root.type == bfd_link_hash_undefweak))))
6136 {
6137 /* This case can occur if we saw a CALL26 reloc in
6138 an input file, but the symbol wasn't referred to
6139 by a dynamic object or all references were
6140 garbage collected. In which case we can end up
6141 resolving. */
6142 h->plt.offset = (bfd_vma) - 1;
6143 h->needs_plt = 0;
6144 }
6145
6146 return TRUE;
6147 }
6148 else
6149 /* Otherwise, reset to -1. */
6150 h->plt.offset = (bfd_vma) - 1;
6151
6152
6153 /* If this is a weak symbol, and there is a real definition, the
6154 processor independent code will have arranged for us to see the
6155 real definition first, and we can just use the same value. */
6156 if (h->u.weakdef != NULL)
6157 {
6158 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6159 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6160 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6161 h->root.u.def.value = h->u.weakdef->root.u.def.value;
6162 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
6163 h->non_got_ref = h->u.weakdef->non_got_ref;
6164 return TRUE;
6165 }
6166
6167 /* If we are creating a shared library, we must presume that the
6168 only references to the symbol are via the global offset table.
6169 For such cases we need not do anything here; the relocations will
6170 be handled correctly by relocate_section. */
6171 if (info->shared)
6172 return TRUE;
6173
6174 /* If there are no references to this symbol that do not use the
6175 GOT, we don't need to generate a copy reloc. */
6176 if (!h->non_got_ref)
6177 return TRUE;
6178
6179 /* If -z nocopyreloc was given, we won't generate them either. */
6180 if (info->nocopyreloc)
6181 {
6182 h->non_got_ref = 0;
6183 return TRUE;
6184 }
6185
6186 /* We must allocate the symbol in our .dynbss section, which will
6187 become part of the .bss section of the executable. There will be
6188 an entry for this symbol in the .dynsym section. The dynamic
6189 object will contain position independent code, so all references
6190 from the dynamic object to this symbol will go through the global
6191 offset table. The dynamic linker will use the .dynsym entry to
6192 determine the address it must put in the global offset table, so
6193 both the dynamic object and the regular object will refer to the
6194 same memory location for the variable. */
6195
6196 htab = elf_aarch64_hash_table (info);
6197
6198 /* We must generate a R_AARCH64_COPY reloc to tell the dynamic linker
6199 to copy the initial value out of the dynamic object and into the
6200 runtime process image. */
6201 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6202 {
6203 htab->srelbss->size += RELOC_SIZE (htab);
6204 h->needs_copy = 1;
6205 }
6206
6207 s = htab->sdynbss;
6208
6209 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6210
6211 }
6212
6213 static bfd_boolean
6214 elfNN_aarch64_allocate_local_symbols (bfd *abfd, unsigned number)
6215 {
6216 struct elf_aarch64_local_symbol *locals;
6217 locals = elf_aarch64_locals (abfd);
6218 if (locals == NULL)
6219 {
6220 locals = (struct elf_aarch64_local_symbol *)
6221 bfd_zalloc (abfd, number * sizeof (struct elf_aarch64_local_symbol));
6222 if (locals == NULL)
6223 return FALSE;
6224 elf_aarch64_locals (abfd) = locals;
6225 }
6226 return TRUE;
6227 }
6228
6229 /* Create the .got section to hold the global offset table. */
6230
6231 static bfd_boolean
6232 aarch64_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
6233 {
6234 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6235 flagword flags;
6236 asection *s;
6237 struct elf_link_hash_entry *h;
6238 struct elf_link_hash_table *htab = elf_hash_table (info);
6239
6240 /* This function may be called more than once. */
6241 s = bfd_get_linker_section (abfd, ".got");
6242 if (s != NULL)
6243 return TRUE;
6244
6245 flags = bed->dynamic_sec_flags;
6246
6247 s = bfd_make_section_anyway_with_flags (abfd,
6248 (bed->rela_plts_and_copies_p
6249 ? ".rela.got" : ".rel.got"),
6250 (bed->dynamic_sec_flags
6251 | SEC_READONLY));
6252 if (s == NULL
6253 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
6254 return FALSE;
6255 htab->srelgot = s;
6256
6257 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
6258 if (s == NULL
6259 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
6260 return FALSE;
6261 htab->sgot = s;
6262 htab->sgot->size += GOT_ENTRY_SIZE;
6263
6264 if (bed->want_got_sym)
6265 {
6266 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
6267 (or .got.plt) section. We don't do this in the linker script
6268 because we don't want to define the symbol if we are not creating
6269 a global offset table. */
6270 h = _bfd_elf_define_linkage_sym (abfd, info, s,
6271 "_GLOBAL_OFFSET_TABLE_");
6272 elf_hash_table (info)->hgot = h;
6273 if (h == NULL)
6274 return FALSE;
6275 }
6276
6277 if (bed->want_got_plt)
6278 {
6279 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
6280 if (s == NULL
6281 || !bfd_set_section_alignment (abfd, s,
6282 bed->s->log_file_align))
6283 return FALSE;
6284 htab->sgotplt = s;
6285 }
6286
6287 /* The first bit of the global offset table is the header. */
6288 s->size += bed->got_header_size;
6289
6290 return TRUE;
6291 }
6292
6293 /* Look through the relocs for a section during the first phase. */
6294
6295 static bfd_boolean
6296 elfNN_aarch64_check_relocs (bfd *abfd, struct bfd_link_info *info,
6297 asection *sec, const Elf_Internal_Rela *relocs)
6298 {
6299 Elf_Internal_Shdr *symtab_hdr;
6300 struct elf_link_hash_entry **sym_hashes;
6301 const Elf_Internal_Rela *rel;
6302 const Elf_Internal_Rela *rel_end;
6303 asection *sreloc;
6304
6305 struct elf_aarch64_link_hash_table *htab;
6306
6307 if (info->relocatable)
6308 return TRUE;
6309
6310 BFD_ASSERT (is_aarch64_elf (abfd));
6311
6312 htab = elf_aarch64_hash_table (info);
6313 sreloc = NULL;
6314
6315 symtab_hdr = &elf_symtab_hdr (abfd);
6316 sym_hashes = elf_sym_hashes (abfd);
6317
6318 rel_end = relocs + sec->reloc_count;
6319 for (rel = relocs; rel < rel_end; rel++)
6320 {
6321 struct elf_link_hash_entry *h;
6322 unsigned long r_symndx;
6323 unsigned int r_type;
6324 bfd_reloc_code_real_type bfd_r_type;
6325 Elf_Internal_Sym *isym;
6326
6327 r_symndx = ELFNN_R_SYM (rel->r_info);
6328 r_type = ELFNN_R_TYPE (rel->r_info);
6329
6330 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
6331 {
6332 (*_bfd_error_handler) (_("%B: bad symbol index: %d"), abfd,
6333 r_symndx);
6334 return FALSE;
6335 }
6336
6337 if (r_symndx < symtab_hdr->sh_info)
6338 {
6339 /* A local symbol. */
6340 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6341 abfd, r_symndx);
6342 if (isym == NULL)
6343 return FALSE;
6344
6345 /* Check relocation against local STT_GNU_IFUNC symbol. */
6346 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
6347 {
6348 h = elfNN_aarch64_get_local_sym_hash (htab, abfd, rel,
6349 TRUE);
6350 if (h == NULL)
6351 return FALSE;
6352
6353 /* Fake a STT_GNU_IFUNC symbol. */
6354 h->type = STT_GNU_IFUNC;
6355 h->def_regular = 1;
6356 h->ref_regular = 1;
6357 h->forced_local = 1;
6358 h->root.type = bfd_link_hash_defined;
6359 }
6360 else
6361 h = NULL;
6362 }
6363 else
6364 {
6365 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6366 while (h->root.type == bfd_link_hash_indirect
6367 || h->root.type == bfd_link_hash_warning)
6368 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6369
6370 /* PR15323, ref flags aren't set for references in the same
6371 object. */
6372 h->root.non_ir_ref = 1;
6373 }
6374
6375 /* Could be done earlier, if h were already available. */
6376 bfd_r_type = aarch64_tls_transition (abfd, info, r_type, h, r_symndx);
6377
6378 if (h != NULL)
6379 {
6380 /* Create the ifunc sections for static executables. If we
6381 never see an indirect function symbol nor we are building
6382 a static executable, those sections will be empty and
6383 won't appear in output. */
6384 switch (bfd_r_type)
6385 {
6386 default:
6387 break;
6388
6389 case BFD_RELOC_AARCH64_ADD_LO12:
6390 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
6391 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
6392 case BFD_RELOC_AARCH64_CALL26:
6393 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
6394 case BFD_RELOC_AARCH64_JUMP26:
6395 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
6396 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
6397 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
6398 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
6399 case BFD_RELOC_AARCH64_NN:
6400 if (htab->root.dynobj == NULL)
6401 htab->root.dynobj = abfd;
6402 if (!_bfd_elf_create_ifunc_sections (htab->root.dynobj, info))
6403 return FALSE;
6404 break;
6405 }
6406
6407 /* It is referenced by a non-shared object. */
6408 h->ref_regular = 1;
6409 h->root.non_ir_ref = 1;
6410 }
6411
6412 switch (bfd_r_type)
6413 {
6414 case BFD_RELOC_AARCH64_NN:
6415
6416 /* We don't need to handle relocs into sections not going into
6417 the "real" output. */
6418 if ((sec->flags & SEC_ALLOC) == 0)
6419 break;
6420
6421 if (h != NULL)
6422 {
6423 if (!info->shared)
6424 h->non_got_ref = 1;
6425
6426 h->plt.refcount += 1;
6427 h->pointer_equality_needed = 1;
6428 }
6429
6430 /* No need to do anything if we're not creating a shared
6431 object. */
6432 if (! info->shared)
6433 break;
6434
6435 {
6436 struct elf_dyn_relocs *p;
6437 struct elf_dyn_relocs **head;
6438
6439 /* We must copy these reloc types into the output file.
6440 Create a reloc section in dynobj and make room for
6441 this reloc. */
6442 if (sreloc == NULL)
6443 {
6444 if (htab->root.dynobj == NULL)
6445 htab->root.dynobj = abfd;
6446
6447 sreloc = _bfd_elf_make_dynamic_reloc_section
6448 (sec, htab->root.dynobj, LOG_FILE_ALIGN, abfd, /*rela? */ TRUE);
6449
6450 if (sreloc == NULL)
6451 return FALSE;
6452 }
6453
6454 /* If this is a global symbol, we count the number of
6455 relocations we need for this symbol. */
6456 if (h != NULL)
6457 {
6458 struct elf_aarch64_link_hash_entry *eh;
6459 eh = (struct elf_aarch64_link_hash_entry *) h;
6460 head = &eh->dyn_relocs;
6461 }
6462 else
6463 {
6464 /* Track dynamic relocs needed for local syms too.
6465 We really need local syms available to do this
6466 easily. Oh well. */
6467
6468 asection *s;
6469 void **vpp;
6470
6471 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6472 abfd, r_symndx);
6473 if (isym == NULL)
6474 return FALSE;
6475
6476 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6477 if (s == NULL)
6478 s = sec;
6479
6480 /* Beware of type punned pointers vs strict aliasing
6481 rules. */
6482 vpp = &(elf_section_data (s)->local_dynrel);
6483 head = (struct elf_dyn_relocs **) vpp;
6484 }
6485
6486 p = *head;
6487 if (p == NULL || p->sec != sec)
6488 {
6489 bfd_size_type amt = sizeof *p;
6490 p = ((struct elf_dyn_relocs *)
6491 bfd_zalloc (htab->root.dynobj, amt));
6492 if (p == NULL)
6493 return FALSE;
6494 p->next = *head;
6495 *head = p;
6496 p->sec = sec;
6497 }
6498
6499 p->count += 1;
6500
6501 }
6502 break;
6503
6504 /* RR: We probably want to keep a consistency check that
6505 there are no dangling GOT_PAGE relocs. */
6506 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
6507 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
6508 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
6509 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
6510 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
6511 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
6512 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
6513 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
6514 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
6515 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
6516 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
6517 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
6518 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
6519 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
6520 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
6521 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6522 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
6523 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
6524 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
6525 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
6526 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
6527 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
6528 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
6529 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
6530 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
6531 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
6532 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
6533 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6534 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
6535 {
6536 unsigned got_type;
6537 unsigned old_got_type;
6538
6539 got_type = aarch64_reloc_got_type (bfd_r_type);
6540
6541 if (h)
6542 {
6543 h->got.refcount += 1;
6544 old_got_type = elf_aarch64_hash_entry (h)->got_type;
6545 }
6546 else
6547 {
6548 struct elf_aarch64_local_symbol *locals;
6549
6550 if (!elfNN_aarch64_allocate_local_symbols
6551 (abfd, symtab_hdr->sh_info))
6552 return FALSE;
6553
6554 locals = elf_aarch64_locals (abfd);
6555 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
6556 locals[r_symndx].got_refcount += 1;
6557 old_got_type = locals[r_symndx].got_type;
6558 }
6559
6560 /* If a variable is accessed with both general dynamic TLS
6561 methods, two slots may be created. */
6562 if (GOT_TLS_GD_ANY_P (old_got_type) && GOT_TLS_GD_ANY_P (got_type))
6563 got_type |= old_got_type;
6564
6565 /* We will already have issued an error message if there
6566 is a TLS/non-TLS mismatch, based on the symbol type.
6567 So just combine any TLS types needed. */
6568 if (old_got_type != GOT_UNKNOWN && old_got_type != GOT_NORMAL
6569 && got_type != GOT_NORMAL)
6570 got_type |= old_got_type;
6571
6572 /* If the symbol is accessed by both IE and GD methods, we
6573 are able to relax. Turn off the GD flag, without
6574 messing up with any other kind of TLS types that may be
6575 involved. */
6576 if ((got_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (got_type))
6577 got_type &= ~ (GOT_TLSDESC_GD | GOT_TLS_GD);
6578
6579 if (old_got_type != got_type)
6580 {
6581 if (h != NULL)
6582 elf_aarch64_hash_entry (h)->got_type = got_type;
6583 else
6584 {
6585 struct elf_aarch64_local_symbol *locals;
6586 locals = elf_aarch64_locals (abfd);
6587 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
6588 locals[r_symndx].got_type = got_type;
6589 }
6590 }
6591
6592 if (htab->root.dynobj == NULL)
6593 htab->root.dynobj = abfd;
6594 if (! aarch64_elf_create_got_section (htab->root.dynobj, info))
6595 return FALSE;
6596 break;
6597 }
6598
6599 case BFD_RELOC_AARCH64_MOVW_G0_NC:
6600 case BFD_RELOC_AARCH64_MOVW_G1_NC:
6601 case BFD_RELOC_AARCH64_MOVW_G2_NC:
6602 case BFD_RELOC_AARCH64_MOVW_G3:
6603 if (info->shared)
6604 {
6605 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
6606 (*_bfd_error_handler)
6607 (_("%B: relocation %s against `%s' can not be used when making "
6608 "a shared object; recompile with -fPIC"),
6609 abfd, elfNN_aarch64_howto_table[howto_index].name,
6610 (h) ? h->root.root.string : "a local symbol");
6611 bfd_set_error (bfd_error_bad_value);
6612 return FALSE;
6613 }
6614
6615 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
6616 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
6617 case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
6618 if (h != NULL && info->executable)
6619 {
6620 /* If this reloc is in a read-only section, we might
6621 need a copy reloc. We can't check reliably at this
6622 stage whether the section is read-only, as input
6623 sections have not yet been mapped to output sections.
6624 Tentatively set the flag for now, and correct in
6625 adjust_dynamic_symbol. */
6626 h->non_got_ref = 1;
6627 h->plt.refcount += 1;
6628 h->pointer_equality_needed = 1;
6629 }
6630 /* FIXME:: RR need to handle these in shared libraries
6631 and essentially bomb out as these being non-PIC
6632 relocations in shared libraries. */
6633 break;
6634
6635 case BFD_RELOC_AARCH64_CALL26:
6636 case BFD_RELOC_AARCH64_JUMP26:
6637 /* If this is a local symbol then we resolve it
6638 directly without creating a PLT entry. */
6639 if (h == NULL)
6640 continue;
6641
6642 h->needs_plt = 1;
6643 if (h->plt.refcount <= 0)
6644 h->plt.refcount = 1;
6645 else
6646 h->plt.refcount += 1;
6647 break;
6648
6649 default:
6650 break;
6651 }
6652 }
6653
6654 return TRUE;
6655 }
6656
6657 /* Treat mapping symbols as special target symbols. */
6658
6659 static bfd_boolean
6660 elfNN_aarch64_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED,
6661 asymbol *sym)
6662 {
6663 return bfd_is_aarch64_special_symbol_name (sym->name,
6664 BFD_AARCH64_SPECIAL_SYM_TYPE_ANY);
6665 }
6666
6667 /* This is a copy of elf_find_function () from elf.c except that
6668 AArch64 mapping symbols are ignored when looking for function names. */
6669
6670 static bfd_boolean
6671 aarch64_elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
6672 asymbol **symbols,
6673 asection *section,
6674 bfd_vma offset,
6675 const char **filename_ptr,
6676 const char **functionname_ptr)
6677 {
6678 const char *filename = NULL;
6679 asymbol *func = NULL;
6680 bfd_vma low_func = 0;
6681 asymbol **p;
6682
6683 for (p = symbols; *p != NULL; p++)
6684 {
6685 elf_symbol_type *q;
6686
6687 q = (elf_symbol_type *) * p;
6688
6689 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
6690 {
6691 default:
6692 break;
6693 case STT_FILE:
6694 filename = bfd_asymbol_name (&q->symbol);
6695 break;
6696 case STT_FUNC:
6697 case STT_NOTYPE:
6698 /* Skip mapping symbols. */
6699 if ((q->symbol.flags & BSF_LOCAL)
6700 && (bfd_is_aarch64_special_symbol_name
6701 (q->symbol.name, BFD_AARCH64_SPECIAL_SYM_TYPE_ANY)))
6702 continue;
6703 /* Fall through. */
6704 if (bfd_get_section (&q->symbol) == section
6705 && q->symbol.value >= low_func && q->symbol.value <= offset)
6706 {
6707 func = (asymbol *) q;
6708 low_func = q->symbol.value;
6709 }
6710 break;
6711 }
6712 }
6713
6714 if (func == NULL)
6715 return FALSE;
6716
6717 if (filename_ptr)
6718 *filename_ptr = filename;
6719 if (functionname_ptr)
6720 *functionname_ptr = bfd_asymbol_name (func);
6721
6722 return TRUE;
6723 }
6724
6725
6726 /* Find the nearest line to a particular section and offset, for error
6727 reporting. This code is a duplicate of the code in elf.c, except
6728 that it uses aarch64_elf_find_function. */
6729
6730 static bfd_boolean
6731 elfNN_aarch64_find_nearest_line (bfd *abfd,
6732 asymbol **symbols,
6733 asection *section,
6734 bfd_vma offset,
6735 const char **filename_ptr,
6736 const char **functionname_ptr,
6737 unsigned int *line_ptr,
6738 unsigned int *discriminator_ptr)
6739 {
6740 bfd_boolean found = FALSE;
6741
6742 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
6743 filename_ptr, functionname_ptr,
6744 line_ptr, discriminator_ptr,
6745 dwarf_debug_sections, 0,
6746 &elf_tdata (abfd)->dwarf2_find_line_info))
6747 {
6748 if (!*functionname_ptr)
6749 aarch64_elf_find_function (abfd, symbols, section, offset,
6750 *filename_ptr ? NULL : filename_ptr,
6751 functionname_ptr);
6752
6753 return TRUE;
6754 }
6755
6756 /* Skip _bfd_dwarf1_find_nearest_line since no known AArch64
6757 toolchain uses DWARF1. */
6758
6759 if (!_bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
6760 &found, filename_ptr,
6761 functionname_ptr, line_ptr,
6762 &elf_tdata (abfd)->line_info))
6763 return FALSE;
6764
6765 if (found && (*functionname_ptr || *line_ptr))
6766 return TRUE;
6767
6768 if (symbols == NULL)
6769 return FALSE;
6770
6771 if (!aarch64_elf_find_function (abfd, symbols, section, offset,
6772 filename_ptr, functionname_ptr))
6773 return FALSE;
6774
6775 *line_ptr = 0;
6776 return TRUE;
6777 }
6778
6779 static bfd_boolean
6780 elfNN_aarch64_find_inliner_info (bfd *abfd,
6781 const char **filename_ptr,
6782 const char **functionname_ptr,
6783 unsigned int *line_ptr)
6784 {
6785 bfd_boolean found;
6786 found = _bfd_dwarf2_find_inliner_info
6787 (abfd, filename_ptr,
6788 functionname_ptr, line_ptr, &elf_tdata (abfd)->dwarf2_find_line_info);
6789 return found;
6790 }
6791
6792
6793 static void
6794 elfNN_aarch64_post_process_headers (bfd *abfd,
6795 struct bfd_link_info *link_info)
6796 {
6797 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
6798
6799 i_ehdrp = elf_elfheader (abfd);
6800 i_ehdrp->e_ident[EI_ABIVERSION] = AARCH64_ELF_ABI_VERSION;
6801
6802 _bfd_elf_post_process_headers (abfd, link_info);
6803 }
6804
6805 static enum elf_reloc_type_class
6806 elfNN_aarch64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
6807 const asection *rel_sec ATTRIBUTE_UNUSED,
6808 const Elf_Internal_Rela *rela)
6809 {
6810 switch ((int) ELFNN_R_TYPE (rela->r_info))
6811 {
6812 case AARCH64_R (RELATIVE):
6813 return reloc_class_relative;
6814 case AARCH64_R (JUMP_SLOT):
6815 return reloc_class_plt;
6816 case AARCH64_R (COPY):
6817 return reloc_class_copy;
6818 default:
6819 return reloc_class_normal;
6820 }
6821 }
6822
6823 /* Handle an AArch64 specific section when reading an object file. This is
6824 called when bfd_section_from_shdr finds a section with an unknown
6825 type. */
6826
6827 static bfd_boolean
6828 elfNN_aarch64_section_from_shdr (bfd *abfd,
6829 Elf_Internal_Shdr *hdr,
6830 const char *name, int shindex)
6831 {
6832 /* There ought to be a place to keep ELF backend specific flags, but
6833 at the moment there isn't one. We just keep track of the
6834 sections by their name, instead. Fortunately, the ABI gives
6835 names for all the AArch64 specific sections, so we will probably get
6836 away with this. */
6837 switch (hdr->sh_type)
6838 {
6839 case SHT_AARCH64_ATTRIBUTES:
6840 break;
6841
6842 default:
6843 return FALSE;
6844 }
6845
6846 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
6847 return FALSE;
6848
6849 return TRUE;
6850 }
6851
6852 /* A structure used to record a list of sections, independently
6853 of the next and prev fields in the asection structure. */
6854 typedef struct section_list
6855 {
6856 asection *sec;
6857 struct section_list *next;
6858 struct section_list *prev;
6859 }
6860 section_list;
6861
6862 /* Unfortunately we need to keep a list of sections for which
6863 an _aarch64_elf_section_data structure has been allocated. This
6864 is because it is possible for functions like elfNN_aarch64_write_section
6865 to be called on a section which has had an elf_data_structure
6866 allocated for it (and so the used_by_bfd field is valid) but
6867 for which the AArch64 extended version of this structure - the
6868 _aarch64_elf_section_data structure - has not been allocated. */
6869 static section_list *sections_with_aarch64_elf_section_data = NULL;
6870
6871 static void
6872 record_section_with_aarch64_elf_section_data (asection *sec)
6873 {
6874 struct section_list *entry;
6875
6876 entry = bfd_malloc (sizeof (*entry));
6877 if (entry == NULL)
6878 return;
6879 entry->sec = sec;
6880 entry->next = sections_with_aarch64_elf_section_data;
6881 entry->prev = NULL;
6882 if (entry->next != NULL)
6883 entry->next->prev = entry;
6884 sections_with_aarch64_elf_section_data = entry;
6885 }
6886
6887 static struct section_list *
6888 find_aarch64_elf_section_entry (asection *sec)
6889 {
6890 struct section_list *entry;
6891 static struct section_list *last_entry = NULL;
6892
6893 /* This is a short cut for the typical case where the sections are added
6894 to the sections_with_aarch64_elf_section_data list in forward order and
6895 then looked up here in backwards order. This makes a real difference
6896 to the ld-srec/sec64k.exp linker test. */
6897 entry = sections_with_aarch64_elf_section_data;
6898 if (last_entry != NULL)
6899 {
6900 if (last_entry->sec == sec)
6901 entry = last_entry;
6902 else if (last_entry->next != NULL && last_entry->next->sec == sec)
6903 entry = last_entry->next;
6904 }
6905
6906 for (; entry; entry = entry->next)
6907 if (entry->sec == sec)
6908 break;
6909
6910 if (entry)
6911 /* Record the entry prior to this one - it is the entry we are
6912 most likely to want to locate next time. Also this way if we
6913 have been called from
6914 unrecord_section_with_aarch64_elf_section_data () we will not
6915 be caching a pointer that is about to be freed. */
6916 last_entry = entry->prev;
6917
6918 return entry;
6919 }
6920
6921 static void
6922 unrecord_section_with_aarch64_elf_section_data (asection *sec)
6923 {
6924 struct section_list *entry;
6925
6926 entry = find_aarch64_elf_section_entry (sec);
6927
6928 if (entry)
6929 {
6930 if (entry->prev != NULL)
6931 entry->prev->next = entry->next;
6932 if (entry->next != NULL)
6933 entry->next->prev = entry->prev;
6934 if (entry == sections_with_aarch64_elf_section_data)
6935 sections_with_aarch64_elf_section_data = entry->next;
6936 free (entry);
6937 }
6938 }
6939
6940
6941 typedef struct
6942 {
6943 void *finfo;
6944 struct bfd_link_info *info;
6945 asection *sec;
6946 int sec_shndx;
6947 int (*func) (void *, const char *, Elf_Internal_Sym *,
6948 asection *, struct elf_link_hash_entry *);
6949 } output_arch_syminfo;
6950
6951 enum map_symbol_type
6952 {
6953 AARCH64_MAP_INSN,
6954 AARCH64_MAP_DATA
6955 };
6956
6957
6958 /* Output a single mapping symbol. */
6959
6960 static bfd_boolean
6961 elfNN_aarch64_output_map_sym (output_arch_syminfo *osi,
6962 enum map_symbol_type type, bfd_vma offset)
6963 {
6964 static const char *names[2] = { "$x", "$d" };
6965 Elf_Internal_Sym sym;
6966
6967 sym.st_value = (osi->sec->output_section->vma
6968 + osi->sec->output_offset + offset);
6969 sym.st_size = 0;
6970 sym.st_other = 0;
6971 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
6972 sym.st_shndx = osi->sec_shndx;
6973 return osi->func (osi->finfo, names[type], &sym, osi->sec, NULL) == 1;
6974 }
6975
6976
6977
6978 /* Output mapping symbols for PLT entries associated with H. */
6979
6980 static bfd_boolean
6981 elfNN_aarch64_output_plt_map (struct elf_link_hash_entry *h, void *inf)
6982 {
6983 output_arch_syminfo *osi = (output_arch_syminfo *) inf;
6984 bfd_vma addr;
6985
6986 if (h->root.type == bfd_link_hash_indirect)
6987 return TRUE;
6988
6989 if (h->root.type == bfd_link_hash_warning)
6990 /* When warning symbols are created, they **replace** the "real"
6991 entry in the hash table, thus we never get to see the real
6992 symbol in a hash traversal. So look at it now. */
6993 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6994
6995 if (h->plt.offset == (bfd_vma) - 1)
6996 return TRUE;
6997
6998 addr = h->plt.offset;
6999 if (addr == 32)
7000 {
7001 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
7002 return FALSE;
7003 }
7004 return TRUE;
7005 }
7006
7007
7008 /* Output a single local symbol for a generated stub. */
7009
7010 static bfd_boolean
7011 elfNN_aarch64_output_stub_sym (output_arch_syminfo *osi, const char *name,
7012 bfd_vma offset, bfd_vma size)
7013 {
7014 Elf_Internal_Sym sym;
7015
7016 sym.st_value = (osi->sec->output_section->vma
7017 + osi->sec->output_offset + offset);
7018 sym.st_size = size;
7019 sym.st_other = 0;
7020 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7021 sym.st_shndx = osi->sec_shndx;
7022 return osi->func (osi->finfo, name, &sym, osi->sec, NULL) == 1;
7023 }
7024
7025 static bfd_boolean
7026 aarch64_map_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
7027 {
7028 struct elf_aarch64_stub_hash_entry *stub_entry;
7029 asection *stub_sec;
7030 bfd_vma addr;
7031 char *stub_name;
7032 output_arch_syminfo *osi;
7033
7034 /* Massage our args to the form they really have. */
7035 stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
7036 osi = (output_arch_syminfo *) in_arg;
7037
7038 stub_sec = stub_entry->stub_sec;
7039
7040 /* Ensure this stub is attached to the current section being
7041 processed. */
7042 if (stub_sec != osi->sec)
7043 return TRUE;
7044
7045 addr = (bfd_vma) stub_entry->stub_offset;
7046
7047 stub_name = stub_entry->output_name;
7048
7049 switch (stub_entry->stub_type)
7050 {
7051 case aarch64_stub_adrp_branch:
7052 if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
7053 sizeof (aarch64_adrp_branch_stub)))
7054 return FALSE;
7055 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
7056 return FALSE;
7057 break;
7058 case aarch64_stub_long_branch:
7059 if (!elfNN_aarch64_output_stub_sym
7060 (osi, stub_name, addr, sizeof (aarch64_long_branch_stub)))
7061 return FALSE;
7062 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
7063 return FALSE;
7064 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_DATA, addr + 16))
7065 return FALSE;
7066 break;
7067 case aarch64_stub_erratum_835769_veneer:
7068 if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
7069 sizeof (aarch64_erratum_835769_stub)))
7070 return FALSE;
7071 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
7072 return FALSE;
7073 break;
7074 case aarch64_stub_erratum_843419_veneer:
7075 if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
7076 sizeof (aarch64_erratum_843419_stub)))
7077 return FALSE;
7078 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
7079 return FALSE;
7080 break;
7081
7082 default:
7083 abort ();
7084 }
7085
7086 return TRUE;
7087 }
7088
7089 /* Output mapping symbols for linker generated sections. */
7090
7091 static bfd_boolean
7092 elfNN_aarch64_output_arch_local_syms (bfd *output_bfd,
7093 struct bfd_link_info *info,
7094 void *finfo,
7095 int (*func) (void *, const char *,
7096 Elf_Internal_Sym *,
7097 asection *,
7098 struct elf_link_hash_entry
7099 *))
7100 {
7101 output_arch_syminfo osi;
7102 struct elf_aarch64_link_hash_table *htab;
7103
7104 htab = elf_aarch64_hash_table (info);
7105
7106 osi.finfo = finfo;
7107 osi.info = info;
7108 osi.func = func;
7109
7110 /* Long calls stubs. */
7111 if (htab->stub_bfd && htab->stub_bfd->sections)
7112 {
7113 asection *stub_sec;
7114
7115 for (stub_sec = htab->stub_bfd->sections;
7116 stub_sec != NULL; stub_sec = stub_sec->next)
7117 {
7118 /* Ignore non-stub sections. */
7119 if (!strstr (stub_sec->name, STUB_SUFFIX))
7120 continue;
7121
7122 osi.sec = stub_sec;
7123
7124 osi.sec_shndx = _bfd_elf_section_from_bfd_section
7125 (output_bfd, osi.sec->output_section);
7126
7127 /* The first instruction in a stub is always a branch. */
7128 if (!elfNN_aarch64_output_map_sym (&osi, AARCH64_MAP_INSN, 0))
7129 return FALSE;
7130
7131 bfd_hash_traverse (&htab->stub_hash_table, aarch64_map_one_stub,
7132 &osi);
7133 }
7134 }
7135
7136 /* Finally, output mapping symbols for the PLT. */
7137 if (!htab->root.splt || htab->root.splt->size == 0)
7138 return TRUE;
7139
7140 /* For now live without mapping symbols for the plt. */
7141 osi.sec_shndx = _bfd_elf_section_from_bfd_section
7142 (output_bfd, htab->root.splt->output_section);
7143 osi.sec = htab->root.splt;
7144
7145 elf_link_hash_traverse (&htab->root, elfNN_aarch64_output_plt_map,
7146 (void *) &osi);
7147
7148 return TRUE;
7149
7150 }
7151
7152 /* Allocate target specific section data. */
7153
7154 static bfd_boolean
7155 elfNN_aarch64_new_section_hook (bfd *abfd, asection *sec)
7156 {
7157 if (!sec->used_by_bfd)
7158 {
7159 _aarch64_elf_section_data *sdata;
7160 bfd_size_type amt = sizeof (*sdata);
7161
7162 sdata = bfd_zalloc (abfd, amt);
7163 if (sdata == NULL)
7164 return FALSE;
7165 sec->used_by_bfd = sdata;
7166 }
7167
7168 record_section_with_aarch64_elf_section_data (sec);
7169
7170 return _bfd_elf_new_section_hook (abfd, sec);
7171 }
7172
7173
7174 static void
7175 unrecord_section_via_map_over_sections (bfd *abfd ATTRIBUTE_UNUSED,
7176 asection *sec,
7177 void *ignore ATTRIBUTE_UNUSED)
7178 {
7179 unrecord_section_with_aarch64_elf_section_data (sec);
7180 }
7181
7182 static bfd_boolean
7183 elfNN_aarch64_close_and_cleanup (bfd *abfd)
7184 {
7185 if (abfd->sections)
7186 bfd_map_over_sections (abfd,
7187 unrecord_section_via_map_over_sections, NULL);
7188
7189 return _bfd_elf_close_and_cleanup (abfd);
7190 }
7191
7192 static bfd_boolean
7193 elfNN_aarch64_bfd_free_cached_info (bfd *abfd)
7194 {
7195 if (abfd->sections)
7196 bfd_map_over_sections (abfd,
7197 unrecord_section_via_map_over_sections, NULL);
7198
7199 return _bfd_free_cached_info (abfd);
7200 }
7201
7202 /* Create dynamic sections. This is different from the ARM backend in that
7203 the got, plt, gotplt and their relocation sections are all created in the
7204 standard part of the bfd elf backend. */
7205
7206 static bfd_boolean
7207 elfNN_aarch64_create_dynamic_sections (bfd *dynobj,
7208 struct bfd_link_info *info)
7209 {
7210 struct elf_aarch64_link_hash_table *htab;
7211
7212 /* We need to create .got section. */
7213 if (!aarch64_elf_create_got_section (dynobj, info))
7214 return FALSE;
7215
7216 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
7217 return FALSE;
7218
7219 htab = elf_aarch64_hash_table (info);
7220 htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
7221 if (!info->shared)
7222 htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss");
7223
7224 if (!htab->sdynbss || (!info->shared && !htab->srelbss))
7225 abort ();
7226
7227 return TRUE;
7228 }
7229
7230
7231 /* Allocate space in .plt, .got and associated reloc sections for
7232 dynamic relocs. */
7233
7234 static bfd_boolean
7235 elfNN_aarch64_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
7236 {
7237 struct bfd_link_info *info;
7238 struct elf_aarch64_link_hash_table *htab;
7239 struct elf_aarch64_link_hash_entry *eh;
7240 struct elf_dyn_relocs *p;
7241
7242 /* An example of a bfd_link_hash_indirect symbol is versioned
7243 symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
7244 -> __gxx_personality_v0(bfd_link_hash_defined)
7245
7246 There is no need to process bfd_link_hash_indirect symbols here
7247 because we will also be presented with the concrete instance of
7248 the symbol and elfNN_aarch64_copy_indirect_symbol () will have been
7249 called to copy all relevant data from the generic to the concrete
7250 symbol instance.
7251 */
7252 if (h->root.type == bfd_link_hash_indirect)
7253 return TRUE;
7254
7255 if (h->root.type == bfd_link_hash_warning)
7256 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7257
7258 info = (struct bfd_link_info *) inf;
7259 htab = elf_aarch64_hash_table (info);
7260
7261 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
7262 here if it is defined and referenced in a non-shared object. */
7263 if (h->type == STT_GNU_IFUNC
7264 && h->def_regular)
7265 return TRUE;
7266 else if (htab->root.dynamic_sections_created && h->plt.refcount > 0)
7267 {
7268 /* Make sure this symbol is output as a dynamic symbol.
7269 Undefined weak syms won't yet be marked as dynamic. */
7270 if (h->dynindx == -1 && !h->forced_local)
7271 {
7272 if (!bfd_elf_link_record_dynamic_symbol (info, h))
7273 return FALSE;
7274 }
7275
7276 if (info->shared || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
7277 {
7278 asection *s = htab->root.splt;
7279
7280 /* If this is the first .plt entry, make room for the special
7281 first entry. */
7282 if (s->size == 0)
7283 s->size += htab->plt_header_size;
7284
7285 h->plt.offset = s->size;
7286
7287 /* If this symbol is not defined in a regular file, and we are
7288 not generating a shared library, then set the symbol to this
7289 location in the .plt. This is required to make function
7290 pointers compare as equal between the normal executable and
7291 the shared library. */
7292 if (!info->shared && !h->def_regular)
7293 {
7294 h->root.u.def.section = s;
7295 h->root.u.def.value = h->plt.offset;
7296 }
7297
7298 /* Make room for this entry. For now we only create the
7299 small model PLT entries. We later need to find a way
7300 of relaxing into these from the large model PLT entries. */
7301 s->size += PLT_SMALL_ENTRY_SIZE;
7302
7303 /* We also need to make an entry in the .got.plt section, which
7304 will be placed in the .got section by the linker script. */
7305 htab->root.sgotplt->size += GOT_ENTRY_SIZE;
7306
7307 /* We also need to make an entry in the .rela.plt section. */
7308 htab->root.srelplt->size += RELOC_SIZE (htab);
7309
7310 /* We need to ensure that all GOT entries that serve the PLT
7311 are consecutive with the special GOT slots [0] [1] and
7312 [2]. Any addtional relocations, such as
7313 R_AARCH64_TLSDESC, must be placed after the PLT related
7314 entries. We abuse the reloc_count such that during
7315 sizing we adjust reloc_count to indicate the number of
7316 PLT related reserved entries. In subsequent phases when
7317 filling in the contents of the reloc entries, PLT related
7318 entries are placed by computing their PLT index (0
7319 .. reloc_count). While other none PLT relocs are placed
7320 at the slot indicated by reloc_count and reloc_count is
7321 updated. */
7322
7323 htab->root.srelplt->reloc_count++;
7324 }
7325 else
7326 {
7327 h->plt.offset = (bfd_vma) - 1;
7328 h->needs_plt = 0;
7329 }
7330 }
7331 else
7332 {
7333 h->plt.offset = (bfd_vma) - 1;
7334 h->needs_plt = 0;
7335 }
7336
7337 eh = (struct elf_aarch64_link_hash_entry *) h;
7338 eh->tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
7339
7340 if (h->got.refcount > 0)
7341 {
7342 bfd_boolean dyn;
7343 unsigned got_type = elf_aarch64_hash_entry (h)->got_type;
7344
7345 h->got.offset = (bfd_vma) - 1;
7346
7347 dyn = htab->root.dynamic_sections_created;
7348
7349 /* Make sure this symbol is output as a dynamic symbol.
7350 Undefined weak syms won't yet be marked as dynamic. */
7351 if (dyn && h->dynindx == -1 && !h->forced_local)
7352 {
7353 if (!bfd_elf_link_record_dynamic_symbol (info, h))
7354 return FALSE;
7355 }
7356
7357 if (got_type == GOT_UNKNOWN)
7358 {
7359 }
7360 else if (got_type == GOT_NORMAL)
7361 {
7362 h->got.offset = htab->root.sgot->size;
7363 htab->root.sgot->size += GOT_ENTRY_SIZE;
7364 if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7365 || h->root.type != bfd_link_hash_undefweak)
7366 && (info->shared
7367 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
7368 {
7369 htab->root.srelgot->size += RELOC_SIZE (htab);
7370 }
7371 }
7372 else
7373 {
7374 int indx;
7375 if (got_type & GOT_TLSDESC_GD)
7376 {
7377 eh->tlsdesc_got_jump_table_offset =
7378 (htab->root.sgotplt->size
7379 - aarch64_compute_jump_table_size (htab));
7380 htab->root.sgotplt->size += GOT_ENTRY_SIZE * 2;
7381 h->got.offset = (bfd_vma) - 2;
7382 }
7383
7384 if (got_type & GOT_TLS_GD)
7385 {
7386 h->got.offset = htab->root.sgot->size;
7387 htab->root.sgot->size += GOT_ENTRY_SIZE * 2;
7388 }
7389
7390 if (got_type & GOT_TLS_IE)
7391 {
7392 h->got.offset = htab->root.sgot->size;
7393 htab->root.sgot->size += GOT_ENTRY_SIZE;
7394 }
7395
7396 indx = h && h->dynindx != -1 ? h->dynindx : 0;
7397 if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7398 || h->root.type != bfd_link_hash_undefweak)
7399 && (info->shared
7400 || indx != 0
7401 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
7402 {
7403 if (got_type & GOT_TLSDESC_GD)
7404 {
7405 htab->root.srelplt->size += RELOC_SIZE (htab);
7406 /* Note reloc_count not incremented here! We have
7407 already adjusted reloc_count for this relocation
7408 type. */
7409
7410 /* TLSDESC PLT is now needed, but not yet determined. */
7411 htab->tlsdesc_plt = (bfd_vma) - 1;
7412 }
7413
7414 if (got_type & GOT_TLS_GD)
7415 htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
7416
7417 if (got_type & GOT_TLS_IE)
7418 htab->root.srelgot->size += RELOC_SIZE (htab);
7419 }
7420 }
7421 }
7422 else
7423 {
7424 h->got.offset = (bfd_vma) - 1;
7425 }
7426
7427 if (eh->dyn_relocs == NULL)
7428 return TRUE;
7429
7430 /* In the shared -Bsymbolic case, discard space allocated for
7431 dynamic pc-relative relocs against symbols which turn out to be
7432 defined in regular objects. For the normal shared case, discard
7433 space for pc-relative relocs that have become local due to symbol
7434 visibility changes. */
7435
7436 if (info->shared)
7437 {
7438 /* Relocs that use pc_count are those that appear on a call
7439 insn, or certain REL relocs that can generated via assembly.
7440 We want calls to protected symbols to resolve directly to the
7441 function rather than going via the plt. If people want
7442 function pointer comparisons to work as expected then they
7443 should avoid writing weird assembly. */
7444 if (SYMBOL_CALLS_LOCAL (info, h))
7445 {
7446 struct elf_dyn_relocs **pp;
7447
7448 for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
7449 {
7450 p->count -= p->pc_count;
7451 p->pc_count = 0;
7452 if (p->count == 0)
7453 *pp = p->next;
7454 else
7455 pp = &p->next;
7456 }
7457 }
7458
7459 /* Also discard relocs on undefined weak syms with non-default
7460 visibility. */
7461 if (eh->dyn_relocs != NULL && h->root.type == bfd_link_hash_undefweak)
7462 {
7463 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
7464 eh->dyn_relocs = NULL;
7465
7466 /* Make sure undefined weak symbols are output as a dynamic
7467 symbol in PIEs. */
7468 else if (h->dynindx == -1
7469 && !h->forced_local
7470 && !bfd_elf_link_record_dynamic_symbol (info, h))
7471 return FALSE;
7472 }
7473
7474 }
7475 else if (ELIMINATE_COPY_RELOCS)
7476 {
7477 /* For the non-shared case, discard space for relocs against
7478 symbols which turn out to need copy relocs or are not
7479 dynamic. */
7480
7481 if (!h->non_got_ref
7482 && ((h->def_dynamic
7483 && !h->def_regular)
7484 || (htab->root.dynamic_sections_created
7485 && (h->root.type == bfd_link_hash_undefweak
7486 || h->root.type == bfd_link_hash_undefined))))
7487 {
7488 /* Make sure this symbol is output as a dynamic symbol.
7489 Undefined weak syms won't yet be marked as dynamic. */
7490 if (h->dynindx == -1
7491 && !h->forced_local
7492 && !bfd_elf_link_record_dynamic_symbol (info, h))
7493 return FALSE;
7494
7495 /* If that succeeded, we know we'll be keeping all the
7496 relocs. */
7497 if (h->dynindx != -1)
7498 goto keep;
7499 }
7500
7501 eh->dyn_relocs = NULL;
7502
7503 keep:;
7504 }
7505
7506 /* Finally, allocate space. */
7507 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7508 {
7509 asection *sreloc;
7510
7511 sreloc = elf_section_data (p->sec)->sreloc;
7512
7513 BFD_ASSERT (sreloc != NULL);
7514
7515 sreloc->size += p->count * RELOC_SIZE (htab);
7516 }
7517
7518 return TRUE;
7519 }
7520
7521 /* Allocate space in .plt, .got and associated reloc sections for
7522 ifunc dynamic relocs. */
7523
7524 static bfd_boolean
7525 elfNN_aarch64_allocate_ifunc_dynrelocs (struct elf_link_hash_entry *h,
7526 void *inf)
7527 {
7528 struct bfd_link_info *info;
7529 struct elf_aarch64_link_hash_table *htab;
7530 struct elf_aarch64_link_hash_entry *eh;
7531
7532 /* An example of a bfd_link_hash_indirect symbol is versioned
7533 symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
7534 -> __gxx_personality_v0(bfd_link_hash_defined)
7535
7536 There is no need to process bfd_link_hash_indirect symbols here
7537 because we will also be presented with the concrete instance of
7538 the symbol and elfNN_aarch64_copy_indirect_symbol () will have been
7539 called to copy all relevant data from the generic to the concrete
7540 symbol instance.
7541 */
7542 if (h->root.type == bfd_link_hash_indirect)
7543 return TRUE;
7544
7545 if (h->root.type == bfd_link_hash_warning)
7546 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7547
7548 info = (struct bfd_link_info *) inf;
7549 htab = elf_aarch64_hash_table (info);
7550
7551 eh = (struct elf_aarch64_link_hash_entry *) h;
7552
7553 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
7554 here if it is defined and referenced in a non-shared object. */
7555 if (h->type == STT_GNU_IFUNC
7556 && h->def_regular)
7557 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
7558 &eh->dyn_relocs,
7559 htab->plt_entry_size,
7560 htab->plt_header_size,
7561 GOT_ENTRY_SIZE);
7562 return TRUE;
7563 }
7564
7565 /* Allocate space in .plt, .got and associated reloc sections for
7566 local dynamic relocs. */
7567
7568 static bfd_boolean
7569 elfNN_aarch64_allocate_local_dynrelocs (void **slot, void *inf)
7570 {
7571 struct elf_link_hash_entry *h
7572 = (struct elf_link_hash_entry *) *slot;
7573
7574 if (h->type != STT_GNU_IFUNC
7575 || !h->def_regular
7576 || !h->ref_regular
7577 || !h->forced_local
7578 || h->root.type != bfd_link_hash_defined)
7579 abort ();
7580
7581 return elfNN_aarch64_allocate_dynrelocs (h, inf);
7582 }
7583
7584 /* Allocate space in .plt, .got and associated reloc sections for
7585 local ifunc dynamic relocs. */
7586
7587 static bfd_boolean
7588 elfNN_aarch64_allocate_local_ifunc_dynrelocs (void **slot, void *inf)
7589 {
7590 struct elf_link_hash_entry *h
7591 = (struct elf_link_hash_entry *) *slot;
7592
7593 if (h->type != STT_GNU_IFUNC
7594 || !h->def_regular
7595 || !h->ref_regular
7596 || !h->forced_local
7597 || h->root.type != bfd_link_hash_defined)
7598 abort ();
7599
7600 return elfNN_aarch64_allocate_ifunc_dynrelocs (h, inf);
7601 }
7602
7603 /* Find any dynamic relocs that apply to read-only sections. */
7604
7605 static bfd_boolean
7606 aarch64_readonly_dynrelocs (struct elf_link_hash_entry * h, void * inf)
7607 {
7608 struct elf_aarch64_link_hash_entry * eh;
7609 struct elf_dyn_relocs * p;
7610
7611 eh = (struct elf_aarch64_link_hash_entry *) h;
7612 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7613 {
7614 asection *s = p->sec;
7615
7616 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7617 {
7618 struct bfd_link_info *info = (struct bfd_link_info *) inf;
7619
7620 info->flags |= DF_TEXTREL;
7621
7622 /* Not an error, just cut short the traversal. */
7623 return FALSE;
7624 }
7625 }
7626 return TRUE;
7627 }
7628
7629 /* This is the most important function of all . Innocuosly named
7630 though ! */
7631 static bfd_boolean
7632 elfNN_aarch64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
7633 struct bfd_link_info *info)
7634 {
7635 struct elf_aarch64_link_hash_table *htab;
7636 bfd *dynobj;
7637 asection *s;
7638 bfd_boolean relocs;
7639 bfd *ibfd;
7640
7641 htab = elf_aarch64_hash_table ((info));
7642 dynobj = htab->root.dynobj;
7643
7644 BFD_ASSERT (dynobj != NULL);
7645
7646 if (htab->root.dynamic_sections_created)
7647 {
7648 if (info->executable)
7649 {
7650 s = bfd_get_linker_section (dynobj, ".interp");
7651 if (s == NULL)
7652 abort ();
7653 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
7654 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
7655 }
7656 }
7657
7658 /* Set up .got offsets for local syms, and space for local dynamic
7659 relocs. */
7660 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7661 {
7662 struct elf_aarch64_local_symbol *locals = NULL;
7663 Elf_Internal_Shdr *symtab_hdr;
7664 asection *srel;
7665 unsigned int i;
7666
7667 if (!is_aarch64_elf (ibfd))
7668 continue;
7669
7670 for (s = ibfd->sections; s != NULL; s = s->next)
7671 {
7672 struct elf_dyn_relocs *p;
7673
7674 for (p = (struct elf_dyn_relocs *)
7675 (elf_section_data (s)->local_dynrel); p != NULL; p = p->next)
7676 {
7677 if (!bfd_is_abs_section (p->sec)
7678 && bfd_is_abs_section (p->sec->output_section))
7679 {
7680 /* Input section has been discarded, either because
7681 it is a copy of a linkonce section or due to
7682 linker script /DISCARD/, so we'll be discarding
7683 the relocs too. */
7684 }
7685 else if (p->count != 0)
7686 {
7687 srel = elf_section_data (p->sec)->sreloc;
7688 srel->size += p->count * RELOC_SIZE (htab);
7689 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
7690 info->flags |= DF_TEXTREL;
7691 }
7692 }
7693 }
7694
7695 locals = elf_aarch64_locals (ibfd);
7696 if (!locals)
7697 continue;
7698
7699 symtab_hdr = &elf_symtab_hdr (ibfd);
7700 srel = htab->root.srelgot;
7701 for (i = 0; i < symtab_hdr->sh_info; i++)
7702 {
7703 locals[i].got_offset = (bfd_vma) - 1;
7704 locals[i].tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
7705 if (locals[i].got_refcount > 0)
7706 {
7707 unsigned got_type = locals[i].got_type;
7708 if (got_type & GOT_TLSDESC_GD)
7709 {
7710 locals[i].tlsdesc_got_jump_table_offset =
7711 (htab->root.sgotplt->size
7712 - aarch64_compute_jump_table_size (htab));
7713 htab->root.sgotplt->size += GOT_ENTRY_SIZE * 2;
7714 locals[i].got_offset = (bfd_vma) - 2;
7715 }
7716
7717 if (got_type & GOT_TLS_GD)
7718 {
7719 locals[i].got_offset = htab->root.sgot->size;
7720 htab->root.sgot->size += GOT_ENTRY_SIZE * 2;
7721 }
7722
7723 if (got_type & GOT_TLS_IE
7724 || got_type & GOT_NORMAL)
7725 {
7726 locals[i].got_offset = htab->root.sgot->size;
7727 htab->root.sgot->size += GOT_ENTRY_SIZE;
7728 }
7729
7730 if (got_type == GOT_UNKNOWN)
7731 {
7732 }
7733
7734 if (info->shared)
7735 {
7736 if (got_type & GOT_TLSDESC_GD)
7737 {
7738 htab->root.srelplt->size += RELOC_SIZE (htab);
7739 /* Note RELOC_COUNT not incremented here! */
7740 htab->tlsdesc_plt = (bfd_vma) - 1;
7741 }
7742
7743 if (got_type & GOT_TLS_GD)
7744 htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
7745
7746 if (got_type & GOT_TLS_IE
7747 || got_type & GOT_NORMAL)
7748 htab->root.srelgot->size += RELOC_SIZE (htab);
7749 }
7750 }
7751 else
7752 {
7753 locals[i].got_refcount = (bfd_vma) - 1;
7754 }
7755 }
7756 }
7757
7758
7759 /* Allocate global sym .plt and .got entries, and space for global
7760 sym dynamic relocs. */
7761 elf_link_hash_traverse (&htab->root, elfNN_aarch64_allocate_dynrelocs,
7762 info);
7763
7764 /* Allocate global ifunc sym .plt and .got entries, and space for global
7765 ifunc sym dynamic relocs. */
7766 elf_link_hash_traverse (&htab->root, elfNN_aarch64_allocate_ifunc_dynrelocs,
7767 info);
7768
7769 /* Allocate .plt and .got entries, and space for local symbols. */
7770 htab_traverse (htab->loc_hash_table,
7771 elfNN_aarch64_allocate_local_dynrelocs,
7772 info);
7773
7774 /* Allocate .plt and .got entries, and space for local ifunc symbols. */
7775 htab_traverse (htab->loc_hash_table,
7776 elfNN_aarch64_allocate_local_ifunc_dynrelocs,
7777 info);
7778
7779 /* For every jump slot reserved in the sgotplt, reloc_count is
7780 incremented. However, when we reserve space for TLS descriptors,
7781 it's not incremented, so in order to compute the space reserved
7782 for them, it suffices to multiply the reloc count by the jump
7783 slot size. */
7784
7785 if (htab->root.srelplt)
7786 htab->sgotplt_jump_table_size = aarch64_compute_jump_table_size (htab);
7787
7788 if (htab->tlsdesc_plt)
7789 {
7790 if (htab->root.splt->size == 0)
7791 htab->root.splt->size += PLT_ENTRY_SIZE;
7792
7793 htab->tlsdesc_plt = htab->root.splt->size;
7794 htab->root.splt->size += PLT_TLSDESC_ENTRY_SIZE;
7795
7796 /* If we're not using lazy TLS relocations, don't generate the
7797 GOT entry required. */
7798 if (!(info->flags & DF_BIND_NOW))
7799 {
7800 htab->dt_tlsdesc_got = htab->root.sgot->size;
7801 htab->root.sgot->size += GOT_ENTRY_SIZE;
7802 }
7803 }
7804
7805 /* Init mapping symbols information to use later to distingush between
7806 code and data while scanning for errata. */
7807 if (htab->fix_erratum_835769 || htab->fix_erratum_843419)
7808 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7809 {
7810 if (!is_aarch64_elf (ibfd))
7811 continue;
7812 bfd_elfNN_aarch64_init_maps (ibfd);
7813 }
7814
7815 /* We now have determined the sizes of the various dynamic sections.
7816 Allocate memory for them. */
7817 relocs = FALSE;
7818 for (s = dynobj->sections; s != NULL; s = s->next)
7819 {
7820 if ((s->flags & SEC_LINKER_CREATED) == 0)
7821 continue;
7822
7823 if (s == htab->root.splt
7824 || s == htab->root.sgot
7825 || s == htab->root.sgotplt
7826 || s == htab->root.iplt
7827 || s == htab->root.igotplt || s == htab->sdynbss)
7828 {
7829 /* Strip this section if we don't need it; see the
7830 comment below. */
7831 }
7832 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
7833 {
7834 if (s->size != 0 && s != htab->root.srelplt)
7835 relocs = TRUE;
7836
7837 /* We use the reloc_count field as a counter if we need
7838 to copy relocs into the output file. */
7839 if (s != htab->root.srelplt)
7840 s->reloc_count = 0;
7841 }
7842 else
7843 {
7844 /* It's not one of our sections, so don't allocate space. */
7845 continue;
7846 }
7847
7848 if (s->size == 0)
7849 {
7850 /* If we don't need this section, strip it from the
7851 output file. This is mostly to handle .rela.bss and
7852 .rela.plt. We must create both sections in
7853 create_dynamic_sections, because they must be created
7854 before the linker maps input sections to output
7855 sections. The linker does that before
7856 adjust_dynamic_symbol is called, and it is that
7857 function which decides whether anything needs to go
7858 into these sections. */
7859
7860 s->flags |= SEC_EXCLUDE;
7861 continue;
7862 }
7863
7864 if ((s->flags & SEC_HAS_CONTENTS) == 0)
7865 continue;
7866
7867 /* Allocate memory for the section contents. We use bfd_zalloc
7868 here in case unused entries are not reclaimed before the
7869 section's contents are written out. This should not happen,
7870 but this way if it does, we get a R_AARCH64_NONE reloc instead
7871 of garbage. */
7872 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
7873 if (s->contents == NULL)
7874 return FALSE;
7875 }
7876
7877 if (htab->root.dynamic_sections_created)
7878 {
7879 /* Add some entries to the .dynamic section. We fill in the
7880 values later, in elfNN_aarch64_finish_dynamic_sections, but we
7881 must add the entries now so that we get the correct size for
7882 the .dynamic section. The DT_DEBUG entry is filled in by the
7883 dynamic linker and used by the debugger. */
7884 #define add_dynamic_entry(TAG, VAL) \
7885 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
7886
7887 if (info->executable)
7888 {
7889 if (!add_dynamic_entry (DT_DEBUG, 0))
7890 return FALSE;
7891 }
7892
7893 if (htab->root.splt->size != 0)
7894 {
7895 if (!add_dynamic_entry (DT_PLTGOT, 0)
7896 || !add_dynamic_entry (DT_PLTRELSZ, 0)
7897 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
7898 || !add_dynamic_entry (DT_JMPREL, 0))
7899 return FALSE;
7900
7901 if (htab->tlsdesc_plt
7902 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
7903 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
7904 return FALSE;
7905 }
7906
7907 if (relocs)
7908 {
7909 if (!add_dynamic_entry (DT_RELA, 0)
7910 || !add_dynamic_entry (DT_RELASZ, 0)
7911 || !add_dynamic_entry (DT_RELAENT, RELOC_SIZE (htab)))
7912 return FALSE;
7913
7914 /* If any dynamic relocs apply to a read-only section,
7915 then we need a DT_TEXTREL entry. */
7916 if ((info->flags & DF_TEXTREL) == 0)
7917 elf_link_hash_traverse (& htab->root, aarch64_readonly_dynrelocs,
7918 info);
7919
7920 if ((info->flags & DF_TEXTREL) != 0)
7921 {
7922 if (!add_dynamic_entry (DT_TEXTREL, 0))
7923 return FALSE;
7924 }
7925 }
7926 }
7927 #undef add_dynamic_entry
7928
7929 return TRUE;
7930 }
7931
7932 static inline void
7933 elf_aarch64_update_plt_entry (bfd *output_bfd,
7934 bfd_reloc_code_real_type r_type,
7935 bfd_byte *plt_entry, bfd_vma value)
7936 {
7937 reloc_howto_type *howto = elfNN_aarch64_howto_from_bfd_reloc (r_type);
7938
7939 _bfd_aarch64_elf_put_addend (output_bfd, plt_entry, r_type, howto, value);
7940 }
7941
7942 static void
7943 elfNN_aarch64_create_small_pltn_entry (struct elf_link_hash_entry *h,
7944 struct elf_aarch64_link_hash_table
7945 *htab, bfd *output_bfd,
7946 struct bfd_link_info *info)
7947 {
7948 bfd_byte *plt_entry;
7949 bfd_vma plt_index;
7950 bfd_vma got_offset;
7951 bfd_vma gotplt_entry_address;
7952 bfd_vma plt_entry_address;
7953 Elf_Internal_Rela rela;
7954 bfd_byte *loc;
7955 asection *plt, *gotplt, *relplt;
7956
7957 /* When building a static executable, use .iplt, .igot.plt and
7958 .rela.iplt sections for STT_GNU_IFUNC symbols. */
7959 if (htab->root.splt != NULL)
7960 {
7961 plt = htab->root.splt;
7962 gotplt = htab->root.sgotplt;
7963 relplt = htab->root.srelplt;
7964 }
7965 else
7966 {
7967 plt = htab->root.iplt;
7968 gotplt = htab->root.igotplt;
7969 relplt = htab->root.irelplt;
7970 }
7971
7972 /* Get the index in the procedure linkage table which
7973 corresponds to this symbol. This is the index of this symbol
7974 in all the symbols for which we are making plt entries. The
7975 first entry in the procedure linkage table is reserved.
7976
7977 Get the offset into the .got table of the entry that
7978 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
7979 bytes. The first three are reserved for the dynamic linker.
7980
7981 For static executables, we don't reserve anything. */
7982
7983 if (plt == htab->root.splt)
7984 {
7985 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
7986 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
7987 }
7988 else
7989 {
7990 plt_index = h->plt.offset / htab->plt_entry_size;
7991 got_offset = plt_index * GOT_ENTRY_SIZE;
7992 }
7993
7994 plt_entry = plt->contents + h->plt.offset;
7995 plt_entry_address = plt->output_section->vma
7996 + plt->output_offset + h->plt.offset;
7997 gotplt_entry_address = gotplt->output_section->vma +
7998 gotplt->output_offset + got_offset;
7999
8000 /* Copy in the boiler-plate for the PLTn entry. */
8001 memcpy (plt_entry, elfNN_aarch64_small_plt_entry, PLT_SMALL_ENTRY_SIZE);
8002
8003 /* Fill in the top 21 bits for this: ADRP x16, PLT_GOT + n * 8.
8004 ADRP: ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
8005 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADR_HI21_PCREL,
8006 plt_entry,
8007 PG (gotplt_entry_address) -
8008 PG (plt_entry_address));
8009
8010 /* Fill in the lo12 bits for the load from the pltgot. */
8011 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_LDSTNN_LO12,
8012 plt_entry + 4,
8013 PG_OFFSET (gotplt_entry_address));
8014
8015 /* Fill in the lo12 bits for the add from the pltgot entry. */
8016 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADD_LO12,
8017 plt_entry + 8,
8018 PG_OFFSET (gotplt_entry_address));
8019
8020 /* All the GOTPLT Entries are essentially initialized to PLT0. */
8021 bfd_put_NN (output_bfd,
8022 plt->output_section->vma + plt->output_offset,
8023 gotplt->contents + got_offset);
8024
8025 rela.r_offset = gotplt_entry_address;
8026
8027 if (h->dynindx == -1
8028 || ((info->executable
8029 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8030 && h->def_regular
8031 && h->type == STT_GNU_IFUNC))
8032 {
8033 /* If an STT_GNU_IFUNC symbol is locally defined, generate
8034 R_AARCH64_IRELATIVE instead of R_AARCH64_JUMP_SLOT. */
8035 rela.r_info = ELFNN_R_INFO (0, AARCH64_R (IRELATIVE));
8036 rela.r_addend = (h->root.u.def.value
8037 + h->root.u.def.section->output_section->vma
8038 + h->root.u.def.section->output_offset);
8039 }
8040 else
8041 {
8042 /* Fill in the entry in the .rela.plt section. */
8043 rela.r_info = ELFNN_R_INFO (h->dynindx, AARCH64_R (JUMP_SLOT));
8044 rela.r_addend = 0;
8045 }
8046
8047 /* Compute the relocation entry to used based on PLT index and do
8048 not adjust reloc_count. The reloc_count has already been adjusted
8049 to account for this entry. */
8050 loc = relplt->contents + plt_index * RELOC_SIZE (htab);
8051 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
8052 }
8053
8054 /* Size sections even though they're not dynamic. We use it to setup
8055 _TLS_MODULE_BASE_, if needed. */
8056
8057 static bfd_boolean
8058 elfNN_aarch64_always_size_sections (bfd *output_bfd,
8059 struct bfd_link_info *info)
8060 {
8061 asection *tls_sec;
8062
8063 if (info->relocatable)
8064 return TRUE;
8065
8066 tls_sec = elf_hash_table (info)->tls_sec;
8067
8068 if (tls_sec)
8069 {
8070 struct elf_link_hash_entry *tlsbase;
8071
8072 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
8073 "_TLS_MODULE_BASE_", TRUE, TRUE, FALSE);
8074
8075 if (tlsbase)
8076 {
8077 struct bfd_link_hash_entry *h = NULL;
8078 const struct elf_backend_data *bed =
8079 get_elf_backend_data (output_bfd);
8080
8081 if (!(_bfd_generic_link_add_one_symbol
8082 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
8083 tls_sec, 0, NULL, FALSE, bed->collect, &h)))
8084 return FALSE;
8085
8086 tlsbase->type = STT_TLS;
8087 tlsbase = (struct elf_link_hash_entry *) h;
8088 tlsbase->def_regular = 1;
8089 tlsbase->other = STV_HIDDEN;
8090 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
8091 }
8092 }
8093
8094 return TRUE;
8095 }
8096
8097 /* Finish up dynamic symbol handling. We set the contents of various
8098 dynamic sections here. */
8099 static bfd_boolean
8100 elfNN_aarch64_finish_dynamic_symbol (bfd *output_bfd,
8101 struct bfd_link_info *info,
8102 struct elf_link_hash_entry *h,
8103 Elf_Internal_Sym *sym)
8104 {
8105 struct elf_aarch64_link_hash_table *htab;
8106 htab = elf_aarch64_hash_table (info);
8107
8108 if (h->plt.offset != (bfd_vma) - 1)
8109 {
8110 asection *plt, *gotplt, *relplt;
8111
8112 /* This symbol has an entry in the procedure linkage table. Set
8113 it up. */
8114
8115 /* When building a static executable, use .iplt, .igot.plt and
8116 .rela.iplt sections for STT_GNU_IFUNC symbols. */
8117 if (htab->root.splt != NULL)
8118 {
8119 plt = htab->root.splt;
8120 gotplt = htab->root.sgotplt;
8121 relplt = htab->root.srelplt;
8122 }
8123 else
8124 {
8125 plt = htab->root.iplt;
8126 gotplt = htab->root.igotplt;
8127 relplt = htab->root.irelplt;
8128 }
8129
8130 /* This symbol has an entry in the procedure linkage table. Set
8131 it up. */
8132 if ((h->dynindx == -1
8133 && !((h->forced_local || info->executable)
8134 && h->def_regular
8135 && h->type == STT_GNU_IFUNC))
8136 || plt == NULL
8137 || gotplt == NULL
8138 || relplt == NULL)
8139 abort ();
8140
8141 elfNN_aarch64_create_small_pltn_entry (h, htab, output_bfd, info);
8142 if (!h->def_regular)
8143 {
8144 /* Mark the symbol as undefined, rather than as defined in
8145 the .plt section. */
8146 sym->st_shndx = SHN_UNDEF;
8147 /* If the symbol is weak we need to clear the value.
8148 Otherwise, the PLT entry would provide a definition for
8149 the symbol even if the symbol wasn't defined anywhere,
8150 and so the symbol would never be NULL. Leave the value if
8151 there were any relocations where pointer equality matters
8152 (this is a clue for the dynamic linker, to make function
8153 pointer comparisons work between an application and shared
8154 library). */
8155 if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
8156 sym->st_value = 0;
8157 }
8158 }
8159
8160 if (h->got.offset != (bfd_vma) - 1
8161 && elf_aarch64_hash_entry (h)->got_type == GOT_NORMAL)
8162 {
8163 Elf_Internal_Rela rela;
8164 bfd_byte *loc;
8165
8166 /* This symbol has an entry in the global offset table. Set it
8167 up. */
8168 if (htab->root.sgot == NULL || htab->root.srelgot == NULL)
8169 abort ();
8170
8171 rela.r_offset = (htab->root.sgot->output_section->vma
8172 + htab->root.sgot->output_offset
8173 + (h->got.offset & ~(bfd_vma) 1));
8174
8175 if (h->def_regular
8176 && h->type == STT_GNU_IFUNC)
8177 {
8178 if (info->shared)
8179 {
8180 /* Generate R_AARCH64_GLOB_DAT. */
8181 goto do_glob_dat;
8182 }
8183 else
8184 {
8185 asection *plt;
8186
8187 if (!h->pointer_equality_needed)
8188 abort ();
8189
8190 /* For non-shared object, we can't use .got.plt, which
8191 contains the real function address if we need pointer
8192 equality. We load the GOT entry with the PLT entry. */
8193 plt = htab->root.splt ? htab->root.splt : htab->root.iplt;
8194 bfd_put_NN (output_bfd, (plt->output_section->vma
8195 + plt->output_offset
8196 + h->plt.offset),
8197 htab->root.sgot->contents
8198 + (h->got.offset & ~(bfd_vma) 1));
8199 return TRUE;
8200 }
8201 }
8202 else if (info->shared && SYMBOL_REFERENCES_LOCAL (info, h))
8203 {
8204 if (!h->def_regular)
8205 return FALSE;
8206
8207 BFD_ASSERT ((h->got.offset & 1) != 0);
8208 rela.r_info = ELFNN_R_INFO (0, AARCH64_R (RELATIVE));
8209 rela.r_addend = (h->root.u.def.value
8210 + h->root.u.def.section->output_section->vma
8211 + h->root.u.def.section->output_offset);
8212 }
8213 else
8214 {
8215 do_glob_dat:
8216 BFD_ASSERT ((h->got.offset & 1) == 0);
8217 bfd_put_NN (output_bfd, (bfd_vma) 0,
8218 htab->root.sgot->contents + h->got.offset);
8219 rela.r_info = ELFNN_R_INFO (h->dynindx, AARCH64_R (GLOB_DAT));
8220 rela.r_addend = 0;
8221 }
8222
8223 loc = htab->root.srelgot->contents;
8224 loc += htab->root.srelgot->reloc_count++ * RELOC_SIZE (htab);
8225 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
8226 }
8227
8228 if (h->needs_copy)
8229 {
8230 Elf_Internal_Rela rela;
8231 bfd_byte *loc;
8232
8233 /* This symbol needs a copy reloc. Set it up. */
8234
8235 if (h->dynindx == -1
8236 || (h->root.type != bfd_link_hash_defined
8237 && h->root.type != bfd_link_hash_defweak)
8238 || htab->srelbss == NULL)
8239 abort ();
8240
8241 rela.r_offset = (h->root.u.def.value
8242 + h->root.u.def.section->output_section->vma
8243 + h->root.u.def.section->output_offset);
8244 rela.r_info = ELFNN_R_INFO (h->dynindx, AARCH64_R (COPY));
8245 rela.r_addend = 0;
8246 loc = htab->srelbss->contents;
8247 loc += htab->srelbss->reloc_count++ * RELOC_SIZE (htab);
8248 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
8249 }
8250
8251 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. SYM may
8252 be NULL for local symbols. */
8253 if (sym != NULL
8254 && (h == elf_hash_table (info)->hdynamic
8255 || h == elf_hash_table (info)->hgot))
8256 sym->st_shndx = SHN_ABS;
8257
8258 return TRUE;
8259 }
8260
8261 /* Finish up local dynamic symbol handling. We set the contents of
8262 various dynamic sections here. */
8263
8264 static bfd_boolean
8265 elfNN_aarch64_finish_local_dynamic_symbol (void **slot, void *inf)
8266 {
8267 struct elf_link_hash_entry *h
8268 = (struct elf_link_hash_entry *) *slot;
8269 struct bfd_link_info *info
8270 = (struct bfd_link_info *) inf;
8271
8272 return elfNN_aarch64_finish_dynamic_symbol (info->output_bfd,
8273 info, h, NULL);
8274 }
8275
8276 static void
8277 elfNN_aarch64_init_small_plt0_entry (bfd *output_bfd ATTRIBUTE_UNUSED,
8278 struct elf_aarch64_link_hash_table
8279 *htab)
8280 {
8281 /* Fill in PLT0. Fixme:RR Note this doesn't distinguish between
8282 small and large plts and at the minute just generates
8283 the small PLT. */
8284
8285 /* PLT0 of the small PLT looks like this in ELF64 -
8286 stp x16, x30, [sp, #-16]! // Save the reloc and lr on stack.
8287 adrp x16, PLT_GOT + 16 // Get the page base of the GOTPLT
8288 ldr x17, [x16, #:lo12:PLT_GOT+16] // Load the address of the
8289 // symbol resolver
8290 add x16, x16, #:lo12:PLT_GOT+16 // Load the lo12 bits of the
8291 // GOTPLT entry for this.
8292 br x17
8293 PLT0 will be slightly different in ELF32 due to different got entry
8294 size.
8295 */
8296 bfd_vma plt_got_2nd_ent; /* Address of GOT[2]. */
8297 bfd_vma plt_base;
8298
8299
8300 memcpy (htab->root.splt->contents, elfNN_aarch64_small_plt0_entry,
8301 PLT_ENTRY_SIZE);
8302 elf_section_data (htab->root.splt->output_section)->this_hdr.sh_entsize =
8303 PLT_ENTRY_SIZE;
8304
8305 plt_got_2nd_ent = (htab->root.sgotplt->output_section->vma
8306 + htab->root.sgotplt->output_offset
8307 + GOT_ENTRY_SIZE * 2);
8308
8309 plt_base = htab->root.splt->output_section->vma +
8310 htab->root.splt->output_offset;
8311
8312 /* Fill in the top 21 bits for this: ADRP x16, PLT_GOT + n * 8.
8313 ADRP: ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
8314 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADR_HI21_PCREL,
8315 htab->root.splt->contents + 4,
8316 PG (plt_got_2nd_ent) - PG (plt_base + 4));
8317
8318 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_LDSTNN_LO12,
8319 htab->root.splt->contents + 8,
8320 PG_OFFSET (plt_got_2nd_ent));
8321
8322 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADD_LO12,
8323 htab->root.splt->contents + 12,
8324 PG_OFFSET (plt_got_2nd_ent));
8325 }
8326
8327 static bfd_boolean
8328 elfNN_aarch64_finish_dynamic_sections (bfd *output_bfd,
8329 struct bfd_link_info *info)
8330 {
8331 struct elf_aarch64_link_hash_table *htab;
8332 bfd *dynobj;
8333 asection *sdyn;
8334
8335 htab = elf_aarch64_hash_table (info);
8336 dynobj = htab->root.dynobj;
8337 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
8338
8339 if (htab->root.dynamic_sections_created)
8340 {
8341 ElfNN_External_Dyn *dyncon, *dynconend;
8342
8343 if (sdyn == NULL || htab->root.sgot == NULL)
8344 abort ();
8345
8346 dyncon = (ElfNN_External_Dyn *) sdyn->contents;
8347 dynconend = (ElfNN_External_Dyn *) (sdyn->contents + sdyn->size);
8348 for (; dyncon < dynconend; dyncon++)
8349 {
8350 Elf_Internal_Dyn dyn;
8351 asection *s;
8352
8353 bfd_elfNN_swap_dyn_in (dynobj, dyncon, &dyn);
8354
8355 switch (dyn.d_tag)
8356 {
8357 default:
8358 continue;
8359
8360 case DT_PLTGOT:
8361 s = htab->root.sgotplt;
8362 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8363 break;
8364
8365 case DT_JMPREL:
8366 dyn.d_un.d_ptr = htab->root.srelplt->output_section->vma;
8367 break;
8368
8369 case DT_PLTRELSZ:
8370 s = htab->root.srelplt;
8371 dyn.d_un.d_val = s->size;
8372 break;
8373
8374 case DT_RELASZ:
8375 /* The procedure linkage table relocs (DT_JMPREL) should
8376 not be included in the overall relocs (DT_RELA).
8377 Therefore, we override the DT_RELASZ entry here to
8378 make it not include the JMPREL relocs. Since the
8379 linker script arranges for .rela.plt to follow all
8380 other relocation sections, we don't have to worry
8381 about changing the DT_RELA entry. */
8382 if (htab->root.srelplt != NULL)
8383 {
8384 s = htab->root.srelplt;
8385 dyn.d_un.d_val -= s->size;
8386 }
8387 break;
8388
8389 case DT_TLSDESC_PLT:
8390 s = htab->root.splt;
8391 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
8392 + htab->tlsdesc_plt;
8393 break;
8394
8395 case DT_TLSDESC_GOT:
8396 s = htab->root.sgot;
8397 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
8398 + htab->dt_tlsdesc_got;
8399 break;
8400 }
8401
8402 bfd_elfNN_swap_dyn_out (output_bfd, &dyn, dyncon);
8403 }
8404
8405 }
8406
8407 /* Fill in the special first entry in the procedure linkage table. */
8408 if (htab->root.splt && htab->root.splt->size > 0)
8409 {
8410 elfNN_aarch64_init_small_plt0_entry (output_bfd, htab);
8411
8412 elf_section_data (htab->root.splt->output_section)->
8413 this_hdr.sh_entsize = htab->plt_entry_size;
8414
8415
8416 if (htab->tlsdesc_plt)
8417 {
8418 bfd_put_NN (output_bfd, (bfd_vma) 0,
8419 htab->root.sgot->contents + htab->dt_tlsdesc_got);
8420
8421 memcpy (htab->root.splt->contents + htab->tlsdesc_plt,
8422 elfNN_aarch64_tlsdesc_small_plt_entry,
8423 sizeof (elfNN_aarch64_tlsdesc_small_plt_entry));
8424
8425 {
8426 bfd_vma adrp1_addr =
8427 htab->root.splt->output_section->vma
8428 + htab->root.splt->output_offset + htab->tlsdesc_plt + 4;
8429
8430 bfd_vma adrp2_addr = adrp1_addr + 4;
8431
8432 bfd_vma got_addr =
8433 htab->root.sgot->output_section->vma
8434 + htab->root.sgot->output_offset;
8435
8436 bfd_vma pltgot_addr =
8437 htab->root.sgotplt->output_section->vma
8438 + htab->root.sgotplt->output_offset;
8439
8440 bfd_vma dt_tlsdesc_got = got_addr + htab->dt_tlsdesc_got;
8441
8442 bfd_byte *plt_entry =
8443 htab->root.splt->contents + htab->tlsdesc_plt;
8444
8445 /* adrp x2, DT_TLSDESC_GOT */
8446 elf_aarch64_update_plt_entry (output_bfd,
8447 BFD_RELOC_AARCH64_ADR_HI21_PCREL,
8448 plt_entry + 4,
8449 (PG (dt_tlsdesc_got)
8450 - PG (adrp1_addr)));
8451
8452 /* adrp x3, 0 */
8453 elf_aarch64_update_plt_entry (output_bfd,
8454 BFD_RELOC_AARCH64_ADR_HI21_PCREL,
8455 plt_entry + 8,
8456 (PG (pltgot_addr)
8457 - PG (adrp2_addr)));
8458
8459 /* ldr x2, [x2, #0] */
8460 elf_aarch64_update_plt_entry (output_bfd,
8461 BFD_RELOC_AARCH64_LDSTNN_LO12,
8462 plt_entry + 12,
8463 PG_OFFSET (dt_tlsdesc_got));
8464
8465 /* add x3, x3, 0 */
8466 elf_aarch64_update_plt_entry (output_bfd,
8467 BFD_RELOC_AARCH64_ADD_LO12,
8468 plt_entry + 16,
8469 PG_OFFSET (pltgot_addr));
8470 }
8471 }
8472 }
8473
8474 if (htab->root.sgotplt)
8475 {
8476 if (bfd_is_abs_section (htab->root.sgotplt->output_section))
8477 {
8478 (*_bfd_error_handler)
8479 (_("discarded output section: `%A'"), htab->root.sgotplt);
8480 return FALSE;
8481 }
8482
8483 /* Fill in the first three entries in the global offset table. */
8484 if (htab->root.sgotplt->size > 0)
8485 {
8486 bfd_put_NN (output_bfd, (bfd_vma) 0, htab->root.sgotplt->contents);
8487
8488 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
8489 bfd_put_NN (output_bfd,
8490 (bfd_vma) 0,
8491 htab->root.sgotplt->contents + GOT_ENTRY_SIZE);
8492 bfd_put_NN (output_bfd,
8493 (bfd_vma) 0,
8494 htab->root.sgotplt->contents + GOT_ENTRY_SIZE * 2);
8495 }
8496
8497 if (htab->root.sgot)
8498 {
8499 if (htab->root.sgot->size > 0)
8500 {
8501 bfd_vma addr =
8502 sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0;
8503 bfd_put_NN (output_bfd, addr, htab->root.sgot->contents);
8504 }
8505 }
8506
8507 elf_section_data (htab->root.sgotplt->output_section)->
8508 this_hdr.sh_entsize = GOT_ENTRY_SIZE;
8509 }
8510
8511 if (htab->root.sgot && htab->root.sgot->size > 0)
8512 elf_section_data (htab->root.sgot->output_section)->this_hdr.sh_entsize
8513 = GOT_ENTRY_SIZE;
8514
8515 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
8516 htab_traverse (htab->loc_hash_table,
8517 elfNN_aarch64_finish_local_dynamic_symbol,
8518 info);
8519
8520 return TRUE;
8521 }
8522
8523 /* Return address for Ith PLT stub in section PLT, for relocation REL
8524 or (bfd_vma) -1 if it should not be included. */
8525
8526 static bfd_vma
8527 elfNN_aarch64_plt_sym_val (bfd_vma i, const asection *plt,
8528 const arelent *rel ATTRIBUTE_UNUSED)
8529 {
8530 return plt->vma + PLT_ENTRY_SIZE + i * PLT_SMALL_ENTRY_SIZE;
8531 }
8532
8533
8534 /* We use this so we can override certain functions
8535 (though currently we don't). */
8536
8537 const struct elf_size_info elfNN_aarch64_size_info =
8538 {
8539 sizeof (ElfNN_External_Ehdr),
8540 sizeof (ElfNN_External_Phdr),
8541 sizeof (ElfNN_External_Shdr),
8542 sizeof (ElfNN_External_Rel),
8543 sizeof (ElfNN_External_Rela),
8544 sizeof (ElfNN_External_Sym),
8545 sizeof (ElfNN_External_Dyn),
8546 sizeof (Elf_External_Note),
8547 4, /* Hash table entry size. */
8548 1, /* Internal relocs per external relocs. */
8549 ARCH_SIZE, /* Arch size. */
8550 LOG_FILE_ALIGN, /* Log_file_align. */
8551 ELFCLASSNN, EV_CURRENT,
8552 bfd_elfNN_write_out_phdrs,
8553 bfd_elfNN_write_shdrs_and_ehdr,
8554 bfd_elfNN_checksum_contents,
8555 bfd_elfNN_write_relocs,
8556 bfd_elfNN_swap_symbol_in,
8557 bfd_elfNN_swap_symbol_out,
8558 bfd_elfNN_slurp_reloc_table,
8559 bfd_elfNN_slurp_symbol_table,
8560 bfd_elfNN_swap_dyn_in,
8561 bfd_elfNN_swap_dyn_out,
8562 bfd_elfNN_swap_reloc_in,
8563 bfd_elfNN_swap_reloc_out,
8564 bfd_elfNN_swap_reloca_in,
8565 bfd_elfNN_swap_reloca_out
8566 };
8567
8568 #define ELF_ARCH bfd_arch_aarch64
8569 #define ELF_MACHINE_CODE EM_AARCH64
8570 #define ELF_MAXPAGESIZE 0x10000
8571 #define ELF_MINPAGESIZE 0x1000
8572 #define ELF_COMMONPAGESIZE 0x1000
8573
8574 #define bfd_elfNN_close_and_cleanup \
8575 elfNN_aarch64_close_and_cleanup
8576
8577 #define bfd_elfNN_bfd_free_cached_info \
8578 elfNN_aarch64_bfd_free_cached_info
8579
8580 #define bfd_elfNN_bfd_is_target_special_symbol \
8581 elfNN_aarch64_is_target_special_symbol
8582
8583 #define bfd_elfNN_bfd_link_hash_table_create \
8584 elfNN_aarch64_link_hash_table_create
8585
8586 #define bfd_elfNN_bfd_merge_private_bfd_data \
8587 elfNN_aarch64_merge_private_bfd_data
8588
8589 #define bfd_elfNN_bfd_print_private_bfd_data \
8590 elfNN_aarch64_print_private_bfd_data
8591
8592 #define bfd_elfNN_bfd_reloc_type_lookup \
8593 elfNN_aarch64_reloc_type_lookup
8594
8595 #define bfd_elfNN_bfd_reloc_name_lookup \
8596 elfNN_aarch64_reloc_name_lookup
8597
8598 #define bfd_elfNN_bfd_set_private_flags \
8599 elfNN_aarch64_set_private_flags
8600
8601 #define bfd_elfNN_find_inliner_info \
8602 elfNN_aarch64_find_inliner_info
8603
8604 #define bfd_elfNN_find_nearest_line \
8605 elfNN_aarch64_find_nearest_line
8606
8607 #define bfd_elfNN_mkobject \
8608 elfNN_aarch64_mkobject
8609
8610 #define bfd_elfNN_new_section_hook \
8611 elfNN_aarch64_new_section_hook
8612
8613 #define elf_backend_adjust_dynamic_symbol \
8614 elfNN_aarch64_adjust_dynamic_symbol
8615
8616 #define elf_backend_always_size_sections \
8617 elfNN_aarch64_always_size_sections
8618
8619 #define elf_backend_check_relocs \
8620 elfNN_aarch64_check_relocs
8621
8622 #define elf_backend_copy_indirect_symbol \
8623 elfNN_aarch64_copy_indirect_symbol
8624
8625 /* Create .dynbss, and .rela.bss sections in DYNOBJ, and set up shortcuts
8626 to them in our hash. */
8627 #define elf_backend_create_dynamic_sections \
8628 elfNN_aarch64_create_dynamic_sections
8629
8630 #define elf_backend_init_index_section \
8631 _bfd_elf_init_2_index_sections
8632
8633 #define elf_backend_finish_dynamic_sections \
8634 elfNN_aarch64_finish_dynamic_sections
8635
8636 #define elf_backend_finish_dynamic_symbol \
8637 elfNN_aarch64_finish_dynamic_symbol
8638
8639 #define elf_backend_gc_sweep_hook \
8640 elfNN_aarch64_gc_sweep_hook
8641
8642 #define elf_backend_object_p \
8643 elfNN_aarch64_object_p
8644
8645 #define elf_backend_output_arch_local_syms \
8646 elfNN_aarch64_output_arch_local_syms
8647
8648 #define elf_backend_plt_sym_val \
8649 elfNN_aarch64_plt_sym_val
8650
8651 #define elf_backend_post_process_headers \
8652 elfNN_aarch64_post_process_headers
8653
8654 #define elf_backend_relocate_section \
8655 elfNN_aarch64_relocate_section
8656
8657 #define elf_backend_reloc_type_class \
8658 elfNN_aarch64_reloc_type_class
8659
8660 #define elf_backend_section_from_shdr \
8661 elfNN_aarch64_section_from_shdr
8662
8663 #define elf_backend_size_dynamic_sections \
8664 elfNN_aarch64_size_dynamic_sections
8665
8666 #define elf_backend_size_info \
8667 elfNN_aarch64_size_info
8668
8669 #define elf_backend_write_section \
8670 elfNN_aarch64_write_section
8671
8672 #define elf_backend_can_refcount 1
8673 #define elf_backend_can_gc_sections 1
8674 #define elf_backend_plt_readonly 1
8675 #define elf_backend_want_got_plt 1
8676 #define elf_backend_want_plt_sym 0
8677 #define elf_backend_may_use_rel_p 0
8678 #define elf_backend_may_use_rela_p 1
8679 #define elf_backend_default_use_rela_p 1
8680 #define elf_backend_rela_normal 1
8681 #define elf_backend_got_header_size (GOT_ENTRY_SIZE * 3)
8682 #define elf_backend_default_execstack 0
8683 #define elf_backend_extern_protected_data 1
8684
8685 #undef elf_backend_obj_attrs_section
8686 #define elf_backend_obj_attrs_section ".ARM.attributes"
8687
8688 #include "elfNN-target.h"
This page took 0.224272 seconds and 5 git commands to generate.