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