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