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