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