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