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