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