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