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