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