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