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