]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elfnn-aarch64.c
766d38c52c31e7f368747c89f2922de189b7c0c8
[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(cur_bfd) \
2933 elf_elfheader(cur_bfd)->e_flags & EF_AARCH64_CHERI_PURECAP ? 32 : (ARCH_SIZE/8)*2
2934
2935 struct elf_aarch64_local_symbol
2936 {
2937 unsigned int got_type;
2938 bfd_signed_vma got_refcount;
2939 bfd_vma got_offset;
2940
2941 /* Offset of the GOTPLT entry reserved for the TLS descriptor. The
2942 offset is from the end of the jump table and reserved entries
2943 within the PLTGOT.
2944
2945 The magic value (bfd_vma) -1 indicates that an offset has not be
2946 allocated. */
2947 bfd_vma tlsdesc_got_jump_table_offset;
2948 };
2949
2950 struct elf_aarch64_obj_tdata
2951 {
2952 struct elf_obj_tdata root;
2953
2954 /* local symbol descriptors */
2955 struct elf_aarch64_local_symbol *locals;
2956
2957 /* Zero to warn when linking objects with incompatible enum sizes. */
2958 int no_enum_size_warning;
2959
2960 /* Zero to warn when linking objects with incompatible wchar_t sizes. */
2961 int no_wchar_size_warning;
2962
2963 /* All GNU_PROPERTY_AARCH64_FEATURE_1_AND properties. */
2964 uint32_t gnu_and_prop;
2965
2966 /* Zero to warn when linking objects with incompatible
2967 GNU_PROPERTY_AARCH64_FEATURE_1_BTI. */
2968 int no_bti_warn;
2969
2970 /* PLT type based on security. */
2971 aarch64_plt_type plt_type;
2972
2973 /* Flag to check if section maps have been initialised for all sections in
2974 this object. */
2975 bfd_boolean secmaps_initialised;
2976 };
2977
2978 #define elf_aarch64_tdata(bfd) \
2979 ((struct elf_aarch64_obj_tdata *) (bfd)->tdata.any)
2980
2981 #define elf_aarch64_locals(bfd) (elf_aarch64_tdata (bfd)->locals)
2982
2983 #define is_aarch64_elf(bfd) \
2984 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2985 && elf_tdata (bfd) != NULL \
2986 && elf_object_id (bfd) == AARCH64_ELF_DATA)
2987
2988 static bfd_boolean
2989 elfNN_aarch64_mkobject (bfd *abfd)
2990 {
2991 return bfd_elf_allocate_object (abfd, sizeof (struct elf_aarch64_obj_tdata),
2992 AARCH64_ELF_DATA);
2993 }
2994
2995 #define elf_aarch64_hash_entry(ent) \
2996 ((struct elf_aarch64_link_hash_entry *)(ent))
2997
2998 #define GOT_UNKNOWN 0
2999 #define GOT_NORMAL 1
3000 #define GOT_TLS_GD 2
3001 #define GOT_TLS_IE 4
3002 #define GOT_TLSDESC_GD 8
3003 #define GOT_CAP 16
3004
3005 #define GOT_TLS_GD_ANY_P(type) ((type & GOT_TLS_GD) || (type & GOT_TLSDESC_GD))
3006
3007 /* AArch64 ELF linker hash entry. */
3008 struct elf_aarch64_link_hash_entry
3009 {
3010 struct elf_link_hash_entry root;
3011
3012 /* Since PLT entries have variable size, we need to record the
3013 index into .got.plt instead of recomputing it from the PLT
3014 offset. */
3015 bfd_signed_vma plt_got_offset;
3016
3017 /* Bit mask representing the type of GOT entry(s) if any required by
3018 this symbol. */
3019 unsigned int got_type;
3020
3021 /* A pointer to the most recently used stub hash entry against this
3022 symbol. */
3023 struct elf_aarch64_stub_hash_entry *stub_cache;
3024
3025 /* Offset of the GOTPLT entry reserved for the TLS descriptor. The offset
3026 is from the end of the jump table and reserved entries within the PLTGOT.
3027
3028 The magic value (bfd_vma) -1 indicates that an offset has not
3029 be allocated. */
3030 bfd_vma tlsdesc_got_jump_table_offset;
3031 };
3032
3033 static unsigned int
3034 elfNN_aarch64_symbol_got_type (struct elf_link_hash_entry *h,
3035 bfd *abfd,
3036 unsigned long r_symndx)
3037 {
3038 if (h)
3039 return elf_aarch64_hash_entry (h)->got_type;
3040
3041 if (! elf_aarch64_locals (abfd))
3042 return GOT_UNKNOWN;
3043
3044 return elf_aarch64_locals (abfd)[r_symndx].got_type;
3045 }
3046
3047 /* Get the AArch64 elf linker hash table from a link_info structure. */
3048 #define elf_aarch64_hash_table(info) \
3049 ((struct elf_aarch64_link_hash_table *) ((info)->hash))
3050
3051 #define aarch64_stub_hash_lookup(table, string, create, copy) \
3052 ((struct elf_aarch64_stub_hash_entry *) \
3053 bfd_hash_lookup ((table), (string), (create), (copy)))
3054
3055 /* AArch64 ELF linker hash table. */
3056 struct elf_aarch64_link_hash_table
3057 {
3058 /* The main hash table. */
3059 struct elf_link_hash_table root;
3060
3061 /* Nonzero to force PIC branch veneers. */
3062 int pic_veneer;
3063
3064 /* Fix erratum 835769. */
3065 int fix_erratum_835769;
3066
3067 /* Fix erratum 843419. */
3068 erratum_84319_opts fix_erratum_843419;
3069
3070 /* Don't apply link-time values for dynamic relocations. */
3071 int no_apply_dynamic_relocs;
3072
3073 /* The number of bytes in the initial entry in the PLT. */
3074 bfd_size_type plt_header_size;
3075
3076 /* The bytes of the initial PLT entry. */
3077 const bfd_byte *plt0_entry;
3078
3079 /* The number of bytes in the subsequent PLT entries. */
3080 bfd_size_type plt_entry_size;
3081
3082 /* The bytes of the subsequent PLT entry. */
3083 const bfd_byte *plt_entry;
3084
3085 /* For convenience in allocate_dynrelocs. */
3086 bfd *obfd;
3087
3088 /* The amount of space used by the reserved portion of the sgotplt
3089 section, plus whatever space is used by the jump slots. */
3090 bfd_vma sgotplt_jump_table_size;
3091
3092 /* The stub hash table. */
3093 struct bfd_hash_table stub_hash_table;
3094
3095 /* Linker stub bfd. */
3096 bfd *stub_bfd;
3097
3098 /* Linker call-backs. */
3099 asection *(*add_stub_section) (const char *, asection *);
3100 void (*layout_sections_again) (void);
3101
3102 /* Array to keep track of which stub sections have been created, and
3103 information on stub grouping. */
3104 struct map_stub
3105 {
3106 /* This is the section to which stubs in the group will be
3107 attached. */
3108 asection *link_sec;
3109 /* The stub section. */
3110 asection *stub_sec;
3111 } *stub_group;
3112
3113 /* Assorted information used by elfNN_aarch64_size_stubs. */
3114 unsigned int bfd_count;
3115 unsigned int top_index;
3116 asection **input_list;
3117
3118 /* JUMP_SLOT relocs for variant PCS symbols may be present. */
3119 int variant_pcs;
3120
3121 /* The number of bytes in the PLT enty for the TLS descriptor. */
3122 bfd_size_type tlsdesc_plt_entry_size;
3123
3124 /* Used by local STT_GNU_IFUNC symbols. */
3125 htab_t loc_hash_table;
3126 void * loc_hash_memory;
3127
3128 /* Used for capability relocations. */
3129 asection *srelcaps;
3130 int c64_rel;
3131 bfd_boolean c64_output;
3132 };
3133
3134 /* Create an entry in an AArch64 ELF linker hash table. */
3135
3136 static struct bfd_hash_entry *
3137 elfNN_aarch64_link_hash_newfunc (struct bfd_hash_entry *entry,
3138 struct bfd_hash_table *table,
3139 const char *string)
3140 {
3141 struct elf_aarch64_link_hash_entry *ret =
3142 (struct elf_aarch64_link_hash_entry *) entry;
3143
3144 /* Allocate the structure if it has not already been allocated by a
3145 subclass. */
3146 if (ret == NULL)
3147 ret = bfd_hash_allocate (table,
3148 sizeof (struct elf_aarch64_link_hash_entry));
3149 if (ret == NULL)
3150 return (struct bfd_hash_entry *) ret;
3151
3152 /* Call the allocation method of the superclass. */
3153 ret = ((struct elf_aarch64_link_hash_entry *)
3154 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3155 table, string));
3156 if (ret != NULL)
3157 {
3158 ret->got_type = GOT_UNKNOWN;
3159 ret->plt_got_offset = (bfd_vma) - 1;
3160 ret->stub_cache = NULL;
3161 ret->tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
3162 }
3163
3164 return (struct bfd_hash_entry *) ret;
3165 }
3166
3167 /* Initialize an entry in the stub hash table. */
3168
3169 static struct bfd_hash_entry *
3170 stub_hash_newfunc (struct bfd_hash_entry *entry,
3171 struct bfd_hash_table *table, const char *string)
3172 {
3173 /* Allocate the structure if it has not already been allocated by a
3174 subclass. */
3175 if (entry == NULL)
3176 {
3177 entry = bfd_hash_allocate (table,
3178 sizeof (struct
3179 elf_aarch64_stub_hash_entry));
3180 if (entry == NULL)
3181 return entry;
3182 }
3183
3184 /* Call the allocation method of the superclass. */
3185 entry = bfd_hash_newfunc (entry, table, string);
3186 if (entry != NULL)
3187 {
3188 struct elf_aarch64_stub_hash_entry *eh;
3189
3190 /* Initialize the local fields. */
3191 eh = (struct elf_aarch64_stub_hash_entry *) entry;
3192 eh->adrp_offset = 0;
3193 eh->stub_sec = NULL;
3194 eh->stub_offset = 0;
3195 eh->target_value = 0;
3196 eh->target_section = NULL;
3197 eh->stub_type = aarch64_stub_none;
3198 eh->h = NULL;
3199 eh->id_sec = NULL;
3200 }
3201
3202 return entry;
3203 }
3204
3205 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
3206 for local symbol so that we can handle local STT_GNU_IFUNC symbols
3207 as global symbol. We reuse indx and dynstr_index for local symbol
3208 hash since they aren't used by global symbols in this backend. */
3209
3210 static hashval_t
3211 elfNN_aarch64_local_htab_hash (const void *ptr)
3212 {
3213 struct elf_link_hash_entry *h
3214 = (struct elf_link_hash_entry *) ptr;
3215 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
3216 }
3217
3218 /* Compare local hash entries. */
3219
3220 static int
3221 elfNN_aarch64_local_htab_eq (const void *ptr1, const void *ptr2)
3222 {
3223 struct elf_link_hash_entry *h1
3224 = (struct elf_link_hash_entry *) ptr1;
3225 struct elf_link_hash_entry *h2
3226 = (struct elf_link_hash_entry *) ptr2;
3227
3228 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
3229 }
3230
3231 /* Find and/or create a hash entry for local symbol. */
3232
3233 static struct elf_link_hash_entry *
3234 elfNN_aarch64_get_local_sym_hash (struct elf_aarch64_link_hash_table *htab,
3235 bfd *abfd, const Elf_Internal_Rela *rel,
3236 bfd_boolean create)
3237 {
3238 struct elf_aarch64_link_hash_entry e, *ret;
3239 asection *sec = abfd->sections;
3240 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
3241 ELFNN_R_SYM (rel->r_info));
3242 void **slot;
3243
3244 e.root.indx = sec->id;
3245 e.root.dynstr_index = ELFNN_R_SYM (rel->r_info);
3246 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
3247 create ? INSERT : NO_INSERT);
3248
3249 if (!slot)
3250 return NULL;
3251
3252 if (*slot)
3253 {
3254 ret = (struct elf_aarch64_link_hash_entry *) *slot;
3255 return &ret->root;
3256 }
3257
3258 ret = (struct elf_aarch64_link_hash_entry *)
3259 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
3260 sizeof (struct elf_aarch64_link_hash_entry));
3261 if (ret)
3262 {
3263 memset (ret, 0, sizeof (*ret));
3264 ret->root.indx = sec->id;
3265 ret->root.dynstr_index = ELFNN_R_SYM (rel->r_info);
3266 ret->root.dynindx = -1;
3267 *slot = ret;
3268 }
3269 return &ret->root;
3270 }
3271
3272 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3273
3274 static void
3275 elfNN_aarch64_copy_indirect_symbol (struct bfd_link_info *info,
3276 struct elf_link_hash_entry *dir,
3277 struct elf_link_hash_entry *ind)
3278 {
3279 struct elf_aarch64_link_hash_entry *edir, *eind;
3280
3281 edir = (struct elf_aarch64_link_hash_entry *) dir;
3282 eind = (struct elf_aarch64_link_hash_entry *) ind;
3283
3284 if (ind->root.type == bfd_link_hash_indirect)
3285 {
3286 /* Copy over PLT info. */
3287 if (dir->got.refcount <= 0)
3288 {
3289 edir->got_type = eind->got_type;
3290 eind->got_type = GOT_UNKNOWN;
3291 }
3292 }
3293
3294 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3295 }
3296
3297 /* Merge non-visibility st_other attributes. */
3298
3299 static void
3300 elfNN_aarch64_merge_symbol_attribute (struct elf_link_hash_entry *h,
3301 const Elf_Internal_Sym *isym,
3302 bfd_boolean definition ATTRIBUTE_UNUSED,
3303 bfd_boolean dynamic ATTRIBUTE_UNUSED)
3304 {
3305 unsigned int isym_sto = isym->st_other & ~ELF_ST_VISIBILITY (-1);
3306 unsigned int h_sto = h->other & ~ELF_ST_VISIBILITY (-1);
3307
3308 if (isym_sto == h_sto)
3309 return;
3310
3311 if (isym_sto & ~STO_AARCH64_VARIANT_PCS)
3312 /* Not fatal, this callback cannot fail. */
3313 _bfd_error_handler (_("unknown attribute for symbol `%s': 0x%02x"),
3314 h->root.root.string, isym_sto);
3315
3316 /* Note: Ideally we would warn about any attribute mismatch, but
3317 this api does not allow that without substantial changes. */
3318 if (isym_sto & STO_AARCH64_VARIANT_PCS)
3319 h->other |= STO_AARCH64_VARIANT_PCS;
3320 }
3321
3322 /* Destroy an AArch64 elf linker hash table. */
3323
3324 static void
3325 elfNN_aarch64_link_hash_table_free (bfd *obfd)
3326 {
3327 struct elf_aarch64_link_hash_table *ret
3328 = (struct elf_aarch64_link_hash_table *) obfd->link.hash;
3329
3330 if (ret->loc_hash_table)
3331 htab_delete (ret->loc_hash_table);
3332 if (ret->loc_hash_memory)
3333 objalloc_free ((struct objalloc *) ret->loc_hash_memory);
3334
3335 bfd_hash_table_free (&ret->stub_hash_table);
3336 _bfd_elf_link_hash_table_free (obfd);
3337 }
3338
3339 /* Create an AArch64 elf linker hash table. */
3340
3341 static struct bfd_link_hash_table *
3342 elfNN_aarch64_link_hash_table_create (bfd *abfd)
3343 {
3344 struct elf_aarch64_link_hash_table *ret;
3345 size_t amt = sizeof (struct elf_aarch64_link_hash_table);
3346
3347 ret = bfd_zmalloc (amt);
3348 if (ret == NULL)
3349 return NULL;
3350
3351 if (!_bfd_elf_link_hash_table_init
3352 (&ret->root, abfd, elfNN_aarch64_link_hash_newfunc,
3353 sizeof (struct elf_aarch64_link_hash_entry), AARCH64_ELF_DATA))
3354 {
3355 free (ret);
3356 return NULL;
3357 }
3358
3359 ret->plt_header_size = PLT_ENTRY_SIZE;
3360 ret->plt0_entry = elfNN_aarch64_small_plt0_entry;
3361 ret->plt_entry_size = PLT_SMALL_ENTRY_SIZE;
3362 ret->plt_entry = elfNN_aarch64_small_plt_entry;
3363 ret->tlsdesc_plt_entry_size = PLT_TLSDESC_ENTRY_SIZE;
3364 ret->obfd = abfd;
3365 ret->root.tlsdesc_got = (bfd_vma) - 1;
3366
3367 if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
3368 sizeof (struct elf_aarch64_stub_hash_entry)))
3369 {
3370 _bfd_elf_link_hash_table_free (abfd);
3371 return NULL;
3372 }
3373
3374 ret->loc_hash_table = htab_try_create (1024,
3375 elfNN_aarch64_local_htab_hash,
3376 elfNN_aarch64_local_htab_eq,
3377 NULL);
3378 ret->loc_hash_memory = objalloc_create ();
3379 if (!ret->loc_hash_table || !ret->loc_hash_memory)
3380 {
3381 elfNN_aarch64_link_hash_table_free (abfd);
3382 return NULL;
3383 }
3384 ret->root.root.hash_table_free = elfNN_aarch64_link_hash_table_free;
3385
3386 return &ret->root.root;
3387 }
3388
3389 /* Perform relocation R_TYPE. Returns TRUE upon success, FALSE otherwise. */
3390
3391 static bfd_boolean
3392 aarch64_relocate (unsigned int r_type, bfd *input_bfd, asection *input_section,
3393 bfd_vma offset, bfd_vma value)
3394 {
3395 reloc_howto_type *howto;
3396 bfd_vma place;
3397
3398 howto = elfNN_aarch64_howto_from_type (input_bfd, r_type);
3399 place = (input_section->output_section->vma + input_section->output_offset
3400 + offset);
3401
3402 r_type = elfNN_aarch64_bfd_reloc_from_type (input_bfd, r_type);
3403 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, r_type, place,
3404 value, 0, FALSE);
3405 return _bfd_aarch64_elf_put_addend (input_bfd,
3406 input_section->contents + offset, r_type,
3407 howto, value) == bfd_reloc_ok;
3408 }
3409
3410 /* Return interworking stub for a relocation. */
3411
3412 static enum elf_aarch64_stub_type
3413 aarch64_interwork_stub (unsigned int r_type,
3414 bfd_boolean branch_to_c64)
3415 {
3416 switch (r_type)
3417 {
3418 case MORELLO_R (JUMP26):
3419 case MORELLO_R (CALL26):
3420 if (!branch_to_c64)
3421 return c64_stub_branch_aarch64;
3422 break;
3423 case AARCH64_R (JUMP26):
3424 case AARCH64_R (CALL26):
3425 if (branch_to_c64)
3426 return aarch64_stub_branch_c64;
3427 break;
3428 default:
3429 break;
3430 }
3431
3432 return aarch64_stub_none;
3433 }
3434
3435 static enum elf_aarch64_stub_type
3436 aarch64_select_branch_stub (bfd_vma value, bfd_vma place)
3437 {
3438 if (aarch64_valid_for_adrp_p (value, place))
3439 return aarch64_stub_adrp_branch;
3440 return aarch64_stub_long_branch;
3441 }
3442
3443 /* Determine the type of stub needed, if any, for a call. */
3444
3445 static enum elf_aarch64_stub_type
3446 aarch64_type_of_stub (asection *input_sec,
3447 const Elf_Internal_Rela *rel,
3448 asection *sym_sec,
3449 unsigned char st_type,
3450 bfd_vma destination)
3451 {
3452 bfd_vma location;
3453 bfd_signed_vma branch_offset;
3454 unsigned int r_type = ELFNN_R_TYPE (rel->r_info);
3455 enum elf_aarch64_stub_type stub_type = aarch64_stub_none;
3456
3457 if (st_type != STT_FUNC
3458 && (sym_sec == input_sec))
3459 return stub_type;
3460
3461 /* Determine where the call point is. */
3462 location = (input_sec->output_offset
3463 + input_sec->output_section->vma + rel->r_offset);
3464
3465 branch_offset = (bfd_signed_vma) (destination - location);
3466
3467 /* For A64 <-> C64 branches we only come here for jumps to PLT. Treat them
3468 as regular branches and leave the interworking to PLT. */
3469 if (branch_offset > AARCH64_MAX_FWD_BRANCH_OFFSET
3470 || branch_offset < AARCH64_MAX_BWD_BRANCH_OFFSET)
3471 {
3472 switch (r_type)
3473 {
3474 /* We don't want to redirect any old unconditional jump in this way,
3475 only one which is being used for a sibcall, where it is
3476 acceptable for the IP0 and IP1 registers to be clobbered. */
3477 case AARCH64_R (CALL26):
3478 case AARCH64_R (JUMP26):
3479 return aarch64_stub_long_branch;
3480 case MORELLO_R (CALL26):
3481 case MORELLO_R (JUMP26):
3482 return c64_stub_branch_c64;
3483 default:
3484 break;
3485 }
3486 }
3487
3488 return aarch64_stub_none;
3489 }
3490
3491 /* Return a string to add as suffix to a veneer name. */
3492
3493 static const char *
3494 aarch64_lookup_stub_type_suffix (enum elf_aarch64_stub_type stub_type)
3495 {
3496 switch (stub_type)
3497 {
3498 case aarch64_stub_branch_c64:
3499 return "_a64c64";
3500 case c64_stub_branch_aarch64:
3501 return "_c64a64";
3502 break;
3503 default:
3504 return "";
3505 }
3506 }
3507
3508 /* Build a name for an entry in the stub hash table. */
3509
3510 static char *
3511 elfNN_aarch64_stub_name (const asection *input_section,
3512 const asection *sym_sec,
3513 const struct elf_aarch64_link_hash_entry *hash,
3514 const Elf_Internal_Rela *rel,
3515 enum elf_aarch64_stub_type stub_type)
3516 {
3517 char *stub_name;
3518 bfd_size_type len;
3519 const char *suffix = aarch64_lookup_stub_type_suffix (stub_type);;
3520
3521 if (hash)
3522 {
3523 len = 8 + 1 + strlen (hash->root.root.root.string) + 1 + 16 + 1;
3524 stub_name = bfd_malloc (len);
3525 if (stub_name != NULL)
3526 snprintf (stub_name, len, "%08x_%s%s+%" BFD_VMA_FMT "x",
3527 (unsigned int) input_section->id,
3528 hash->root.root.root.string,
3529 suffix, rel->r_addend);
3530 }
3531 else
3532 {
3533 len = 8 + 1 + 8 + 1 + 8 + 1 + 16 + 1;
3534 stub_name = bfd_malloc (len);
3535 if (stub_name != NULL)
3536 snprintf (stub_name, len, "%08x_%x:%x%s+%" BFD_VMA_FMT "x",
3537 (unsigned int) input_section->id,
3538 (unsigned int) sym_sec->id,
3539 (unsigned int) ELFNN_R_SYM (rel->r_info),
3540 suffix, rel->r_addend);
3541 }
3542
3543 return stub_name;
3544 }
3545
3546 /* Return TRUE if symbol H should be hashed in the `.gnu.hash' section. For
3547 executable PLT slots where the executable never takes the address of those
3548 functions, the function symbols are not added to the hash table. */
3549
3550 static bfd_boolean
3551 elf_aarch64_hash_symbol (struct elf_link_hash_entry *h)
3552 {
3553 if (h->plt.offset != (bfd_vma) -1
3554 && !h->def_regular
3555 && !h->pointer_equality_needed)
3556 return FALSE;
3557
3558 return _bfd_elf_hash_symbol (h);
3559 }
3560
3561 /* Look up an entry in the stub hash. Stub entries are cached because
3562 creating the stub name takes a bit of time. */
3563
3564 static struct elf_aarch64_stub_hash_entry *
3565 elfNN_aarch64_get_stub_entry (const asection *input_section,
3566 const asection *sym_sec,
3567 struct elf_link_hash_entry *hash,
3568 const Elf_Internal_Rela *rel,
3569 struct elf_aarch64_link_hash_table *htab,
3570 enum elf_aarch64_stub_type stub_type)
3571 {
3572 struct elf_aarch64_stub_hash_entry *stub_entry;
3573 struct elf_aarch64_link_hash_entry *h =
3574 (struct elf_aarch64_link_hash_entry *) hash;
3575 const asection *id_sec;
3576
3577 if ((input_section->flags & SEC_CODE) == 0)
3578 return NULL;
3579
3580 /* If this input section is part of a group of sections sharing one
3581 stub section, then use the id of the first section in the group.
3582 Stub names need to include a section id, as there may well be
3583 more than one stub used to reach say, printf, and we need to
3584 distinguish between them. */
3585 id_sec = htab->stub_group[input_section->id].link_sec;
3586
3587 if (h != NULL && h->stub_cache != NULL
3588 && h->stub_cache->h == h && h->stub_cache->id_sec == id_sec)
3589 {
3590 stub_entry = h->stub_cache;
3591 }
3592 else
3593 {
3594 char *stub_name;
3595
3596 stub_name = elfNN_aarch64_stub_name (id_sec, sym_sec, h, rel, stub_type);
3597 if (stub_name == NULL)
3598 return NULL;
3599
3600 stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table,
3601 stub_name, FALSE, FALSE);
3602 if (h != NULL)
3603 h->stub_cache = stub_entry;
3604
3605 free (stub_name);
3606 }
3607
3608 return stub_entry;
3609 }
3610
3611
3612 /* Create a stub section. */
3613
3614 static asection *
3615 _bfd_aarch64_create_stub_section (asection *section,
3616 struct elf_aarch64_link_hash_table *htab)
3617 {
3618 size_t namelen;
3619 bfd_size_type len;
3620 char *s_name;
3621
3622 namelen = strlen (section->name);
3623 len = namelen + sizeof (STUB_SUFFIX);
3624 s_name = bfd_alloc (htab->stub_bfd, len);
3625 if (s_name == NULL)
3626 return NULL;
3627
3628 memcpy (s_name, section->name, namelen);
3629 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3630 return (*htab->add_stub_section) (s_name, section);
3631 }
3632
3633
3634 /* Find or create a stub section for a link section.
3635
3636 Fix or create the stub section used to collect stubs attached to
3637 the specified link section. */
3638
3639 static asection *
3640 _bfd_aarch64_get_stub_for_link_section (asection *link_section,
3641 struct elf_aarch64_link_hash_table *htab)
3642 {
3643 if (htab->stub_group[link_section->id].stub_sec == NULL)
3644 htab->stub_group[link_section->id].stub_sec
3645 = _bfd_aarch64_create_stub_section (link_section, htab);
3646 return htab->stub_group[link_section->id].stub_sec;
3647 }
3648
3649
3650 /* Find or create a stub section in the stub group for an input
3651 section. */
3652
3653 static asection *
3654 _bfd_aarch64_create_or_find_stub_sec (asection *section,
3655 struct elf_aarch64_link_hash_table *htab)
3656 {
3657 asection *link_sec = htab->stub_group[section->id].link_sec;
3658 return _bfd_aarch64_get_stub_for_link_section (link_sec, htab);
3659 }
3660
3661
3662 /* Add a new stub entry in the stub group associated with an input
3663 section to the stub hash. Not all fields of the new stub entry are
3664 initialised. */
3665
3666 static struct elf_aarch64_stub_hash_entry *
3667 _bfd_aarch64_add_stub_entry_in_group (const char *stub_name,
3668 asection *section,
3669 struct elf_aarch64_link_hash_table *htab)
3670 {
3671 asection *link_sec;
3672 asection *stub_sec;
3673 struct elf_aarch64_stub_hash_entry *stub_entry;
3674
3675 link_sec = htab->stub_group[section->id].link_sec;
3676 stub_sec = _bfd_aarch64_create_or_find_stub_sec (section, htab);
3677
3678 /* Enter this entry into the linker stub hash table. */
3679 stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3680 TRUE, FALSE);
3681 if (stub_entry == NULL)
3682 {
3683 /* xgettext:c-format */
3684 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3685 section->owner, stub_name);
3686 return NULL;
3687 }
3688
3689 stub_entry->stub_sec = stub_sec;
3690 stub_entry->stub_offset = 0;
3691 stub_entry->id_sec = link_sec;
3692
3693 return stub_entry;
3694 }
3695
3696 /* Add a new stub entry in the final stub section to the stub hash.
3697 Not all fields of the new stub entry are initialised. */
3698
3699 static struct elf_aarch64_stub_hash_entry *
3700 _bfd_aarch64_add_stub_entry_after (const char *stub_name,
3701 asection *link_section,
3702 struct elf_aarch64_link_hash_table *htab)
3703 {
3704 asection *stub_sec;
3705 struct elf_aarch64_stub_hash_entry *stub_entry;
3706
3707 stub_sec = NULL;
3708 /* Only create the actual stub if we will end up needing it. */
3709 if (htab->fix_erratum_843419 & ERRAT_ADRP)
3710 stub_sec = _bfd_aarch64_get_stub_for_link_section (link_section, htab);
3711 stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3712 TRUE, FALSE);
3713 if (stub_entry == NULL)
3714 {
3715 _bfd_error_handler (_("cannot create stub entry %s"), stub_name);
3716 return NULL;
3717 }
3718
3719 stub_entry->stub_sec = stub_sec;
3720 stub_entry->stub_offset = 0;
3721 stub_entry->id_sec = link_section;
3722
3723 return stub_entry;
3724 }
3725
3726
3727 static bfd_boolean
3728 aarch64_build_one_stub (struct bfd_hash_entry *gen_entry,
3729 void *in_arg)
3730 {
3731 struct elf_aarch64_stub_hash_entry *stub_entry;
3732 asection *stub_sec;
3733 bfd *stub_bfd;
3734 bfd_byte *loc;
3735 bfd_vma sym_value;
3736 bfd_vma veneered_insn_loc;
3737 bfd_vma veneer_entry_loc;
3738 bfd_signed_vma branch_offset = 0;
3739 unsigned int template_size;
3740 const uint32_t *template;
3741 unsigned int i;
3742 struct bfd_link_info *info;
3743
3744 /* Massage our args to the form they really have. */
3745 stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
3746
3747 info = (struct bfd_link_info *) in_arg;
3748
3749 /* Fail if the target section could not be assigned to an output
3750 section. The user should fix his linker script. */
3751 if (stub_entry->target_section->output_section == NULL
3752 && info->non_contiguous_regions)
3753 info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
3754 "Retry without "
3755 "--enable-non-contiguous-regions.\n"),
3756 stub_entry->target_section);
3757
3758 stub_sec = stub_entry->stub_sec;
3759
3760 /* Make a note of the offset within the stubs for this entry. */
3761 stub_entry->stub_offset = stub_sec->size;
3762 loc = stub_sec->contents + stub_entry->stub_offset;
3763
3764 stub_bfd = stub_sec->owner;
3765
3766 /* This is the address of the stub destination. */
3767 sym_value = (stub_entry->target_value
3768 + stub_entry->target_section->output_offset
3769 + stub_entry->target_section->output_section->vma);
3770
3771 bfd_vma place = (stub_entry->stub_offset + stub_sec->output_section->vma
3772 + stub_sec->output_offset);
3773
3774 if (stub_entry->stub_type == aarch64_stub_long_branch)
3775 {
3776 /* See if we can relax the stub. */
3777 if (aarch64_valid_for_adrp_p (sym_value, place))
3778 stub_entry->stub_type = aarch64_select_branch_stub (sym_value, place);
3779 }
3780
3781 if ((stub_entry->stub_type == aarch64_stub_branch_c64
3782 || stub_entry->stub_type == c64_stub_branch_aarch64
3783 || stub_entry->stub_type == c64_stub_branch_c64)
3784 && !c64_valid_for_adrp_p (sym_value, place))
3785 {
3786 _bfd_error_handler
3787 (_("%s: stub target out of range for %s branch"),
3788 stub_entry->output_name,
3789 (stub_entry->stub_type == aarch64_stub_branch_c64
3790 ? "A64 to C64" : "C64 to A64"));
3791 bfd_set_error (bfd_error_bad_value);
3792 return FALSE;
3793 }
3794
3795 switch (stub_entry->stub_type)
3796 {
3797 case aarch64_stub_adrp_branch:
3798 template = aarch64_adrp_branch_stub;
3799 template_size = sizeof (aarch64_adrp_branch_stub);
3800 break;
3801 case aarch64_stub_long_branch:
3802 template = aarch64_long_branch_stub;
3803 template_size = sizeof (aarch64_long_branch_stub);
3804 break;
3805 case aarch64_stub_erratum_835769_veneer:
3806 template = aarch64_erratum_835769_stub;
3807 template_size = sizeof (aarch64_erratum_835769_stub);
3808 break;
3809 case aarch64_stub_erratum_843419_veneer:
3810 template = aarch64_erratum_843419_stub;
3811 template_size = sizeof (aarch64_erratum_843419_stub);
3812 break;
3813 case aarch64_stub_branch_c64:
3814 template = aarch64_c64_branch_stub;
3815 template_size = sizeof (aarch64_c64_branch_stub);
3816 break;
3817 case c64_stub_branch_aarch64:
3818 case c64_stub_branch_c64:
3819 template = c64_aarch64_branch_stub;
3820 template_size = sizeof (c64_aarch64_branch_stub);
3821 break;
3822 default:
3823 abort ();
3824 }
3825
3826 for (i = 0; i < (template_size / sizeof template[0]); i++)
3827 {
3828 bfd_putl32 (template[i], loc);
3829 loc += 4;
3830 }
3831
3832 template_size = (template_size + 7) & ~7;
3833 stub_sec->size += template_size;
3834
3835 bfd_vma stub_offset = stub_entry->stub_offset;
3836
3837 switch (stub_entry->stub_type)
3838 {
3839 case aarch64_stub_adrp_branch:
3840 if (!aarch64_relocate (AARCH64_R (ADR_PREL_PG_HI21), stub_bfd, stub_sec,
3841 stub_entry->stub_offset, sym_value))
3842 /* The stub would not have been relaxed if the offset was out
3843 of range. */
3844 BFD_FAIL ();
3845
3846 if (!aarch64_relocate (AARCH64_R (ADD_ABS_LO12_NC), stub_bfd, stub_sec,
3847 stub_entry->stub_offset + 4, sym_value))
3848 BFD_FAIL ();
3849 break;
3850
3851 case aarch64_stub_long_branch:
3852 /* We want the value relative to the address 12 bytes back from the
3853 value itself. */
3854 if (!aarch64_relocate (AARCH64_R (PRELNN), stub_bfd, stub_sec,
3855 stub_entry->stub_offset + 16, sym_value + 12))
3856 BFD_FAIL ();
3857 break;
3858
3859 case aarch64_stub_erratum_835769_veneer:
3860 veneered_insn_loc = stub_entry->target_section->output_section->vma
3861 + stub_entry->target_section->output_offset
3862 + stub_entry->target_value;
3863 veneer_entry_loc = stub_entry->stub_sec->output_section->vma
3864 + stub_entry->stub_sec->output_offset
3865 + stub_entry->stub_offset;
3866 branch_offset = veneered_insn_loc - veneer_entry_loc;
3867 branch_offset >>= 2;
3868 branch_offset &= 0x3ffffff;
3869 bfd_putl32 (stub_entry->veneered_insn,
3870 stub_sec->contents + stub_entry->stub_offset);
3871 bfd_putl32 (template[1] | branch_offset,
3872 stub_sec->contents + stub_entry->stub_offset + 4);
3873 break;
3874
3875 case aarch64_stub_erratum_843419_veneer:
3876 if (!aarch64_relocate (AARCH64_R (JUMP26), stub_bfd, stub_sec,
3877 stub_entry->stub_offset + 4, sym_value + 4))
3878 BFD_FAIL ();
3879 break;
3880
3881 case aarch64_stub_branch_c64:
3882 stub_offset += 4;
3883 /* Fall through. */
3884 case c64_stub_branch_aarch64:
3885 case c64_stub_branch_c64:
3886 if (!aarch64_relocate (R_MORELLO_ADR_PREL_PG_HI20, stub_bfd, stub_sec,
3887 stub_offset, sym_value))
3888 /* We fail early if offset is out of range. */
3889 BFD_FAIL ();
3890
3891 if (!aarch64_relocate (AARCH64_R (ADD_ABS_LO12_NC), stub_bfd, stub_sec,
3892 stub_offset + 4, sym_value))
3893 BFD_FAIL ();
3894 break;
3895
3896 default:
3897 abort ();
3898 }
3899
3900 return TRUE;
3901 }
3902
3903 /* As above, but don't actually build the stub. Just bump offset so
3904 we know stub section sizes. */
3905
3906 static bfd_boolean
3907 aarch64_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
3908 {
3909 struct elf_aarch64_stub_hash_entry *stub_entry;
3910 struct elf_aarch64_link_hash_table *htab;
3911 int size;
3912
3913 /* Massage our args to the form they really have. */
3914 stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
3915 htab = (struct elf_aarch64_link_hash_table *) in_arg;
3916
3917 switch (stub_entry->stub_type)
3918 {
3919 case aarch64_stub_adrp_branch:
3920 size = sizeof (aarch64_adrp_branch_stub);
3921 break;
3922 case aarch64_stub_long_branch:
3923 size = sizeof (aarch64_long_branch_stub);
3924 break;
3925 case aarch64_stub_erratum_835769_veneer:
3926 size = sizeof (aarch64_erratum_835769_stub);
3927 break;
3928 case aarch64_stub_erratum_843419_veneer:
3929 {
3930 if (htab->fix_erratum_843419 == ERRAT_ADR)
3931 return TRUE;
3932 size = sizeof (aarch64_erratum_843419_stub);
3933 }
3934 break;
3935 case aarch64_stub_branch_c64:
3936 size = sizeof (aarch64_c64_branch_stub);
3937 break;
3938 case c64_stub_branch_aarch64:
3939 case c64_stub_branch_c64:
3940 size = sizeof (c64_aarch64_branch_stub);
3941 break;
3942 default:
3943 abort ();
3944 }
3945
3946 size = (size + 7) & ~7;
3947 stub_entry->stub_sec->size += size;
3948 return TRUE;
3949 }
3950
3951 /* External entry points for sizing and building linker stubs. */
3952
3953 /* Set up various things so that we can make a list of input sections
3954 for each output section included in the link. Returns -1 on error,
3955 0 when no stubs will be needed, and 1 on success. */
3956
3957 int
3958 elfNN_aarch64_setup_section_lists (bfd *output_bfd,
3959 struct bfd_link_info *info)
3960 {
3961 bfd *input_bfd;
3962 unsigned int bfd_count;
3963 unsigned int top_id, top_index;
3964 asection *section;
3965 asection **input_list, **list;
3966 size_t amt;
3967 struct elf_aarch64_link_hash_table *htab =
3968 elf_aarch64_hash_table (info);
3969
3970 if (!is_elf_hash_table (htab))
3971 return 0;
3972
3973 /* Count the number of input BFDs and find the top input section id. */
3974 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
3975 input_bfd != NULL; input_bfd = input_bfd->link.next)
3976 {
3977 bfd_count += 1;
3978 for (section = input_bfd->sections;
3979 section != NULL; section = section->next)
3980 {
3981 if (top_id < section->id)
3982 top_id = section->id;
3983 }
3984 }
3985 htab->bfd_count = bfd_count;
3986
3987 amt = sizeof (struct map_stub) * (top_id + 1);
3988 htab->stub_group = bfd_zmalloc (amt);
3989 if (htab->stub_group == NULL)
3990 return -1;
3991
3992 /* We can't use output_bfd->section_count here to find the top output
3993 section index as some sections may have been removed, and
3994 _bfd_strip_section_from_output doesn't renumber the indices. */
3995 for (section = output_bfd->sections, top_index = 0;
3996 section != NULL; section = section->next)
3997 {
3998 if (top_index < section->index)
3999 top_index = section->index;
4000 }
4001
4002 htab->top_index = top_index;
4003 amt = sizeof (asection *) * (top_index + 1);
4004 input_list = bfd_malloc (amt);
4005 htab->input_list = input_list;
4006 if (input_list == NULL)
4007 return -1;
4008
4009 /* For sections we aren't interested in, mark their entries with a
4010 value we can check later. */
4011 list = input_list + top_index;
4012 do
4013 *list = bfd_abs_section_ptr;
4014 while (list-- != input_list);
4015
4016 for (section = output_bfd->sections;
4017 section != NULL; section = section->next)
4018 {
4019 if ((section->flags & SEC_CODE) != 0)
4020 input_list[section->index] = NULL;
4021 }
4022
4023 return 1;
4024 }
4025
4026 /* Used by elfNN_aarch64_next_input_section and group_sections. */
4027 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
4028
4029 /* The linker repeatedly calls this function for each input section,
4030 in the order that input sections are linked into output sections.
4031 Build lists of input sections to determine groupings between which
4032 we may insert linker stubs. */
4033
4034 void
4035 elfNN_aarch64_next_input_section (struct bfd_link_info *info, asection *isec)
4036 {
4037 struct elf_aarch64_link_hash_table *htab =
4038 elf_aarch64_hash_table (info);
4039
4040 if (isec->output_section->index <= htab->top_index)
4041 {
4042 asection **list = htab->input_list + isec->output_section->index;
4043
4044 if (*list != bfd_abs_section_ptr && (isec->flags & SEC_CODE) != 0)
4045 {
4046 /* Steal the link_sec pointer for our list. */
4047 /* This happens to make the list in reverse order,
4048 which is what we want. */
4049 PREV_SEC (isec) = *list;
4050 *list = isec;
4051 }
4052 }
4053 }
4054
4055 /* See whether we can group stub sections together. Grouping stub
4056 sections may result in fewer stubs. More importantly, we need to
4057 put all .init* and .fini* stubs at the beginning of the .init or
4058 .fini output sections respectively, because glibc splits the
4059 _init and _fini functions into multiple parts. Putting a stub in
4060 the middle of a function is not a good idea. */
4061
4062 static void
4063 group_sections (struct elf_aarch64_link_hash_table *htab,
4064 bfd_size_type stub_group_size,
4065 bfd_boolean stubs_always_after_branch)
4066 {
4067 asection **list = htab->input_list;
4068
4069 do
4070 {
4071 asection *tail = *list;
4072 asection *head;
4073
4074 if (tail == bfd_abs_section_ptr)
4075 continue;
4076
4077 /* Reverse the list: we must avoid placing stubs at the
4078 beginning of the section because the beginning of the text
4079 section may be required for an interrupt vector in bare metal
4080 code. */
4081 #define NEXT_SEC PREV_SEC
4082 head = NULL;
4083 while (tail != NULL)
4084 {
4085 /* Pop from tail. */
4086 asection *item = tail;
4087 tail = PREV_SEC (item);
4088
4089 /* Push on head. */
4090 NEXT_SEC (item) = head;
4091 head = item;
4092 }
4093
4094 while (head != NULL)
4095 {
4096 asection *curr;
4097 asection *next;
4098 bfd_vma stub_group_start = head->output_offset;
4099 bfd_vma end_of_next;
4100
4101 curr = head;
4102 while (NEXT_SEC (curr) != NULL)
4103 {
4104 next = NEXT_SEC (curr);
4105 end_of_next = next->output_offset + next->size;
4106 if (end_of_next - stub_group_start >= stub_group_size)
4107 /* End of NEXT is too far from start, so stop. */
4108 break;
4109 /* Add NEXT to the group. */
4110 curr = next;
4111 }
4112
4113 /* OK, the size from the start to the start of CURR is less
4114 than stub_group_size and thus can be handled by one stub
4115 section. (Or the head section is itself larger than
4116 stub_group_size, in which case we may be toast.)
4117 We should really be keeping track of the total size of
4118 stubs added here, as stubs contribute to the final output
4119 section size. */
4120 do
4121 {
4122 next = NEXT_SEC (head);
4123 /* Set up this stub group. */
4124 htab->stub_group[head->id].link_sec = curr;
4125 }
4126 while (head != curr && (head = next) != NULL);
4127
4128 /* But wait, there's more! Input sections up to stub_group_size
4129 bytes after the stub section can be handled by it too. */
4130 if (!stubs_always_after_branch)
4131 {
4132 stub_group_start = curr->output_offset + curr->size;
4133
4134 while (next != NULL)
4135 {
4136 end_of_next = next->output_offset + next->size;
4137 if (end_of_next - stub_group_start >= stub_group_size)
4138 /* End of NEXT is too far from stubs, so stop. */
4139 break;
4140 /* Add NEXT to the stub group. */
4141 head = next;
4142 next = NEXT_SEC (head);
4143 htab->stub_group[head->id].link_sec = curr;
4144 }
4145 }
4146 head = next;
4147 }
4148 }
4149 while (list++ != htab->input_list + htab->top_index);
4150
4151 free (htab->input_list);
4152 }
4153
4154 #undef PREV_SEC
4155 #undef PREV_SEC
4156
4157 #define AARCH64_BITS(x, pos, n) (((x) >> (pos)) & ((1 << (n)) - 1))
4158
4159 #define AARCH64_RT(insn) AARCH64_BITS (insn, 0, 5)
4160 #define AARCH64_RT2(insn) AARCH64_BITS (insn, 10, 5)
4161 #define AARCH64_RA(insn) AARCH64_BITS (insn, 10, 5)
4162 #define AARCH64_RD(insn) AARCH64_BITS (insn, 0, 5)
4163 #define AARCH64_RN(insn) AARCH64_BITS (insn, 5, 5)
4164 #define AARCH64_RM(insn) AARCH64_BITS (insn, 16, 5)
4165
4166 #define AARCH64_MAC(insn) (((insn) & 0xff000000) == 0x9b000000)
4167 #define AARCH64_BIT(insn, n) AARCH64_BITS (insn, n, 1)
4168 #define AARCH64_OP31(insn) AARCH64_BITS (insn, 21, 3)
4169 #define AARCH64_ZR 0x1f
4170
4171 /* All ld/st ops. See C4-182 of the ARM ARM. The encoding space for
4172 LD_PCREL, LDST_RO, LDST_UI and LDST_UIMM cover prefetch ops. */
4173
4174 #define AARCH64_LD(insn) (AARCH64_BIT (insn, 22) == 1)
4175 #define AARCH64_LDST(insn) (((insn) & 0x0a000000) == 0x08000000)
4176 #define AARCH64_LDST_EX(insn) (((insn) & 0x3f000000) == 0x08000000)
4177 #define AARCH64_LDST_PCREL(insn) (((insn) & 0x3b000000) == 0x18000000)
4178 #define AARCH64_LDST_NAP(insn) (((insn) & 0x3b800000) == 0x28000000)
4179 #define AARCH64_LDSTP_PI(insn) (((insn) & 0x3b800000) == 0x28800000)
4180 #define AARCH64_LDSTP_O(insn) (((insn) & 0x3b800000) == 0x29000000)
4181 #define AARCH64_LDSTP_PRE(insn) (((insn) & 0x3b800000) == 0x29800000)
4182 #define AARCH64_LDST_UI(insn) (((insn) & 0x3b200c00) == 0x38000000)
4183 #define AARCH64_LDST_PIIMM(insn) (((insn) & 0x3b200c00) == 0x38000400)
4184 #define AARCH64_LDST_U(insn) (((insn) & 0x3b200c00) == 0x38000800)
4185 #define AARCH64_LDST_PREIMM(insn) (((insn) & 0x3b200c00) == 0x38000c00)
4186 #define AARCH64_LDST_RO(insn) (((insn) & 0x3b200c00) == 0x38200800)
4187 #define AARCH64_LDST_UIMM(insn) (((insn) & 0x3b000000) == 0x39000000)
4188 #define AARCH64_LDST_SIMD_M(insn) (((insn) & 0xbfbf0000) == 0x0c000000)
4189 #define AARCH64_LDST_SIMD_M_PI(insn) (((insn) & 0xbfa00000) == 0x0c800000)
4190 #define AARCH64_LDST_SIMD_S(insn) (((insn) & 0xbf9f0000) == 0x0d000000)
4191 #define AARCH64_LDST_SIMD_S_PI(insn) (((insn) & 0xbf800000) == 0x0d800000)
4192
4193 /* Classify an INSN if it is indeed a load/store.
4194
4195 Return TRUE if INSN is a LD/ST instruction otherwise return FALSE.
4196
4197 For scalar LD/ST instructions PAIR is FALSE, RT is returned and RT2
4198 is set equal to RT.
4199
4200 For LD/ST pair instructions PAIR is TRUE, RT and RT2 are returned. */
4201
4202 static bfd_boolean
4203 aarch64_mem_op_p (uint32_t insn, unsigned int *rt, unsigned int *rt2,
4204 bfd_boolean *pair, bfd_boolean *load)
4205 {
4206 uint32_t opcode;
4207 unsigned int r;
4208 uint32_t opc = 0;
4209 uint32_t v = 0;
4210 uint32_t opc_v = 0;
4211
4212 /* Bail out quickly if INSN doesn't fall into the load-store
4213 encoding space. */
4214 if (!AARCH64_LDST (insn))
4215 return FALSE;
4216
4217 *pair = FALSE;
4218 *load = FALSE;
4219 if (AARCH64_LDST_EX (insn))
4220 {
4221 *rt = AARCH64_RT (insn);
4222 *rt2 = *rt;
4223 if (AARCH64_BIT (insn, 21) == 1)
4224 {
4225 *pair = TRUE;
4226 *rt2 = AARCH64_RT2 (insn);
4227 }
4228 *load = AARCH64_LD (insn);
4229 return TRUE;
4230 }
4231 else if (AARCH64_LDST_NAP (insn)
4232 || AARCH64_LDSTP_PI (insn)
4233 || AARCH64_LDSTP_O (insn)
4234 || AARCH64_LDSTP_PRE (insn))
4235 {
4236 *pair = TRUE;
4237 *rt = AARCH64_RT (insn);
4238 *rt2 = AARCH64_RT2 (insn);
4239 *load = AARCH64_LD (insn);
4240 return TRUE;
4241 }
4242 else if (AARCH64_LDST_PCREL (insn)
4243 || AARCH64_LDST_UI (insn)
4244 || AARCH64_LDST_PIIMM (insn)
4245 || AARCH64_LDST_U (insn)
4246 || AARCH64_LDST_PREIMM (insn)
4247 || AARCH64_LDST_RO (insn)
4248 || AARCH64_LDST_UIMM (insn))
4249 {
4250 *rt = AARCH64_RT (insn);
4251 *rt2 = *rt;
4252 if (AARCH64_LDST_PCREL (insn))
4253 *load = TRUE;
4254 opc = AARCH64_BITS (insn, 22, 2);
4255 v = AARCH64_BIT (insn, 26);
4256 opc_v = opc | (v << 2);
4257 *load = (opc_v == 1 || opc_v == 2 || opc_v == 3
4258 || opc_v == 5 || opc_v == 7);
4259 return TRUE;
4260 }
4261 else if (AARCH64_LDST_SIMD_M (insn)
4262 || AARCH64_LDST_SIMD_M_PI (insn))
4263 {
4264 *rt = AARCH64_RT (insn);
4265 *load = AARCH64_BIT (insn, 22);
4266 opcode = (insn >> 12) & 0xf;
4267 switch (opcode)
4268 {
4269 case 0:
4270 case 2:
4271 *rt2 = *rt + 3;
4272 break;
4273
4274 case 4:
4275 case 6:
4276 *rt2 = *rt + 2;
4277 break;
4278
4279 case 7:
4280 *rt2 = *rt;
4281 break;
4282
4283 case 8:
4284 case 10:
4285 *rt2 = *rt + 1;
4286 break;
4287
4288 default:
4289 return FALSE;
4290 }
4291 return TRUE;
4292 }
4293 else if (AARCH64_LDST_SIMD_S (insn)
4294 || AARCH64_LDST_SIMD_S_PI (insn))
4295 {
4296 *rt = AARCH64_RT (insn);
4297 r = (insn >> 21) & 1;
4298 *load = AARCH64_BIT (insn, 22);
4299 opcode = (insn >> 13) & 0x7;
4300 switch (opcode)
4301 {
4302 case 0:
4303 case 2:
4304 case 4:
4305 *rt2 = *rt + r;
4306 break;
4307
4308 case 1:
4309 case 3:
4310 case 5:
4311 *rt2 = *rt + (r == 0 ? 2 : 3);
4312 break;
4313
4314 case 6:
4315 *rt2 = *rt + r;
4316 break;
4317
4318 case 7:
4319 *rt2 = *rt + (r == 0 ? 2 : 3);
4320 break;
4321
4322 default:
4323 return FALSE;
4324 }
4325 return TRUE;
4326 }
4327
4328 return FALSE;
4329 }
4330
4331 /* Return TRUE if INSN is multiply-accumulate. */
4332
4333 static bfd_boolean
4334 aarch64_mlxl_p (uint32_t insn)
4335 {
4336 uint32_t op31 = AARCH64_OP31 (insn);
4337
4338 if (AARCH64_MAC (insn)
4339 && (op31 == 0 || op31 == 1 || op31 == 5)
4340 /* Exclude MUL instructions which are encoded as a multiple accumulate
4341 with RA = XZR. */
4342 && AARCH64_RA (insn) != AARCH64_ZR)
4343 return TRUE;
4344
4345 return FALSE;
4346 }
4347
4348 /* Some early revisions of the Cortex-A53 have an erratum (835769) whereby
4349 it is possible for a 64-bit multiply-accumulate instruction to generate an
4350 incorrect result. The details are quite complex and hard to
4351 determine statically, since branches in the code may exist in some
4352 circumstances, but all cases end with a memory (load, store, or
4353 prefetch) instruction followed immediately by the multiply-accumulate
4354 operation. We employ a linker patching technique, by moving the potentially
4355 affected multiply-accumulate instruction into a patch region and replacing
4356 the original instruction with a branch to the patch. This function checks
4357 if INSN_1 is the memory operation followed by a multiply-accumulate
4358 operation (INSN_2). Return TRUE if an erratum sequence is found, FALSE
4359 if INSN_1 and INSN_2 are safe. */
4360
4361 static bfd_boolean
4362 aarch64_erratum_sequence (uint32_t insn_1, uint32_t insn_2)
4363 {
4364 uint32_t rt;
4365 uint32_t rt2;
4366 uint32_t rn;
4367 uint32_t rm;
4368 uint32_t ra;
4369 bfd_boolean pair;
4370 bfd_boolean load;
4371
4372 if (aarch64_mlxl_p (insn_2)
4373 && aarch64_mem_op_p (insn_1, &rt, &rt2, &pair, &load))
4374 {
4375 /* Any SIMD memory op is independent of the subsequent MLA
4376 by definition of the erratum. */
4377 if (AARCH64_BIT (insn_1, 26))
4378 return TRUE;
4379
4380 /* If not SIMD, check for integer memory ops and MLA relationship. */
4381 rn = AARCH64_RN (insn_2);
4382 ra = AARCH64_RA (insn_2);
4383 rm = AARCH64_RM (insn_2);
4384
4385 /* If this is a load and there's a true(RAW) dependency, we are safe
4386 and this is not an erratum sequence. */
4387 if (load &&
4388 (rt == rn || rt == rm || rt == ra
4389 || (pair && (rt2 == rn || rt2 == rm || rt2 == ra))))
4390 return FALSE;
4391
4392 /* We conservatively put out stubs for all other cases (including
4393 writebacks). */
4394 return TRUE;
4395 }
4396
4397 return FALSE;
4398 }
4399
4400
4401 static char *
4402 _bfd_aarch64_erratum_835769_stub_name (unsigned num_fixes)
4403 {
4404 char *stub_name = (char *) bfd_malloc
4405 (strlen ("__erratum_835769_veneer_") + 16);
4406 if (stub_name != NULL)
4407 sprintf (stub_name,"__erratum_835769_veneer_%d", num_fixes);
4408 return stub_name;
4409 }
4410
4411 /* Scan for Cortex-A53 erratum 835769 sequence.
4412
4413 Return TRUE else FALSE on abnormal termination. */
4414
4415 static bfd_boolean
4416 _bfd_aarch64_erratum_835769_scan (bfd *input_bfd,
4417 struct bfd_link_info *info,
4418 unsigned int *num_fixes_p)
4419 {
4420 asection *section;
4421 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
4422 unsigned int num_fixes = *num_fixes_p;
4423
4424 if (htab == NULL)
4425 return TRUE;
4426
4427 for (section = input_bfd->sections;
4428 section != NULL;
4429 section = section->next)
4430 {
4431 bfd_byte *contents = NULL;
4432 struct _aarch64_elf_section_data *sec_data;
4433 unsigned int span;
4434
4435 if (elf_section_type (section) != SHT_PROGBITS
4436 || (elf_section_flags (section) & SHF_EXECINSTR) == 0
4437 || (section->flags & SEC_EXCLUDE) != 0
4438 || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4439 || (section->output_section == bfd_abs_section_ptr))
4440 continue;
4441
4442 if (elf_section_data (section)->this_hdr.contents != NULL)
4443 contents = elf_section_data (section)->this_hdr.contents;
4444 else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
4445 return FALSE;
4446
4447 sec_data = elf_aarch64_section_data (section);
4448
4449 if (sec_data->mapcount)
4450 qsort (sec_data->map, sec_data->mapcount,
4451 sizeof (elf_aarch64_section_map), elf_aarch64_compare_mapping);
4452
4453 for (span = 0; span < sec_data->mapcount; span++)
4454 {
4455 unsigned int span_start = sec_data->map[span].vma;
4456 unsigned int span_end = ((span == sec_data->mapcount - 1)
4457 ? sec_data->map[0].vma + section->size
4458 : sec_data->map[span + 1].vma);
4459 unsigned int i;
4460 char span_type = sec_data->map[span].type;
4461
4462 if (span_type == 'd')
4463 continue;
4464
4465 for (i = span_start; i + 4 < span_end; i += 4)
4466 {
4467 uint32_t insn_1 = bfd_getl32 (contents + i);
4468 uint32_t insn_2 = bfd_getl32 (contents + i + 4);
4469
4470 if (aarch64_erratum_sequence (insn_1, insn_2))
4471 {
4472 struct elf_aarch64_stub_hash_entry *stub_entry;
4473 char *stub_name = _bfd_aarch64_erratum_835769_stub_name (num_fixes);
4474 if (! stub_name)
4475 return FALSE;
4476
4477 stub_entry = _bfd_aarch64_add_stub_entry_in_group (stub_name,
4478 section,
4479 htab);
4480 if (! stub_entry)
4481 return FALSE;
4482
4483 stub_entry->stub_type = aarch64_stub_erratum_835769_veneer;
4484 stub_entry->target_section = section;
4485 stub_entry->target_value = i + 4;
4486 stub_entry->veneered_insn = insn_2;
4487 stub_entry->output_name = stub_name;
4488 num_fixes++;
4489 }
4490 }
4491 }
4492 if (elf_section_data (section)->this_hdr.contents == NULL)
4493 free (contents);
4494 }
4495
4496 *num_fixes_p = num_fixes;
4497
4498 return TRUE;
4499 }
4500
4501
4502 /* Test if instruction INSN is ADRP. */
4503
4504 static bfd_boolean
4505 _bfd_aarch64_adrp_p (uint32_t insn)
4506 {
4507 return ((insn & AARCH64_ADRP_OP_MASK) == AARCH64_ADRP_OP);
4508 }
4509
4510
4511 /* Helper predicate to look for cortex-a53 erratum 843419 sequence 1. */
4512
4513 static bfd_boolean
4514 _bfd_aarch64_erratum_843419_sequence_p (uint32_t insn_1, uint32_t insn_2,
4515 uint32_t insn_3)
4516 {
4517 uint32_t rt;
4518 uint32_t rt2;
4519 bfd_boolean pair;
4520 bfd_boolean load;
4521
4522 return (aarch64_mem_op_p (insn_2, &rt, &rt2, &pair, &load)
4523 && (!pair
4524 || (pair && !load))
4525 && AARCH64_LDST_UIMM (insn_3)
4526 && AARCH64_RN (insn_3) == AARCH64_RD (insn_1));
4527 }
4528
4529
4530 /* Test for the presence of Cortex-A53 erratum 843419 instruction sequence.
4531
4532 Return TRUE if section CONTENTS at offset I contains one of the
4533 erratum 843419 sequences, otherwise return FALSE. If a sequence is
4534 seen set P_VENEER_I to the offset of the final LOAD/STORE
4535 instruction in the sequence.
4536 */
4537
4538 static bfd_boolean
4539 _bfd_aarch64_erratum_843419_p (bfd_byte *contents, bfd_vma vma,
4540 bfd_vma i, bfd_vma span_end,
4541 bfd_vma *p_veneer_i)
4542 {
4543 uint32_t insn_1 = bfd_getl32 (contents + i);
4544
4545 if (!_bfd_aarch64_adrp_p (insn_1))
4546 return FALSE;
4547
4548 if (span_end < i + 12)
4549 return FALSE;
4550
4551 uint32_t insn_2 = bfd_getl32 (contents + i + 4);
4552 uint32_t insn_3 = bfd_getl32 (contents + i + 8);
4553
4554 if ((vma & 0xfff) != 0xff8 && (vma & 0xfff) != 0xffc)
4555 return FALSE;
4556
4557 if (_bfd_aarch64_erratum_843419_sequence_p (insn_1, insn_2, insn_3))
4558 {
4559 *p_veneer_i = i + 8;
4560 return TRUE;
4561 }
4562
4563 if (span_end < i + 16)
4564 return FALSE;
4565
4566 uint32_t insn_4 = bfd_getl32 (contents + i + 12);
4567
4568 if (_bfd_aarch64_erratum_843419_sequence_p (insn_1, insn_2, insn_4))
4569 {
4570 *p_veneer_i = i + 12;
4571 return TRUE;
4572 }
4573
4574 return FALSE;
4575 }
4576
4577
4578 /* Resize all stub sections. */
4579
4580 static void
4581 _bfd_aarch64_resize_stubs (struct elf_aarch64_link_hash_table *htab)
4582 {
4583 asection *section;
4584
4585 /* OK, we've added some stubs. Find out the new size of the
4586 stub sections. */
4587 for (section = htab->stub_bfd->sections;
4588 section != NULL; section = section->next)
4589 {
4590 /* Ignore non-stub sections. */
4591 if (!strstr (section->name, STUB_SUFFIX))
4592 continue;
4593 section->size = 0;
4594 }
4595
4596 bfd_hash_traverse (&htab->stub_hash_table, aarch64_size_one_stub, htab);
4597
4598 for (section = htab->stub_bfd->sections;
4599 section != NULL; section = section->next)
4600 {
4601 if (!strstr (section->name, STUB_SUFFIX))
4602 continue;
4603
4604 /* Add space for a branch. Add 8 bytes to keep section 8 byte aligned,
4605 as long branch stubs contain a 64-bit address. */
4606 if (section->size)
4607 section->size += 8;
4608
4609 /* Ensure all stub sections have a size which is a multiple of
4610 4096. This is important in order to ensure that the insertion
4611 of stub sections does not in itself move existing code around
4612 in such a way that new errata sequences are created. We only do this
4613 when the ADRP workaround is enabled. If only the ADR workaround is
4614 enabled then the stubs workaround won't ever be used. */
4615 if (htab->fix_erratum_843419 & ERRAT_ADRP)
4616 if (section->size)
4617 section->size = BFD_ALIGN (section->size, 0x1000);
4618 }
4619 }
4620
4621 /* Construct an erratum 843419 workaround stub name. */
4622
4623 static char *
4624 _bfd_aarch64_erratum_843419_stub_name (asection *input_section,
4625 bfd_vma offset)
4626 {
4627 const bfd_size_type len = 8 + 4 + 1 + 8 + 1 + 16 + 1;
4628 char *stub_name = bfd_malloc (len);
4629
4630 if (stub_name != NULL)
4631 snprintf (stub_name, len, "e843419@%04x_%08x_%" BFD_VMA_FMT "x",
4632 input_section->owner->id,
4633 input_section->id,
4634 offset);
4635 return stub_name;
4636 }
4637
4638 /* Build a stub_entry structure describing an 843419 fixup.
4639
4640 The stub_entry constructed is populated with the bit pattern INSN
4641 of the instruction located at OFFSET within input SECTION.
4642
4643 Returns TRUE on success. */
4644
4645 static bfd_boolean
4646 _bfd_aarch64_erratum_843419_fixup (uint32_t insn,
4647 bfd_vma adrp_offset,
4648 bfd_vma ldst_offset,
4649 asection *section,
4650 struct bfd_link_info *info)
4651 {
4652 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
4653 char *stub_name;
4654 struct elf_aarch64_stub_hash_entry *stub_entry;
4655
4656 stub_name = _bfd_aarch64_erratum_843419_stub_name (section, ldst_offset);
4657 if (stub_name == NULL)
4658 return FALSE;
4659 stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4660 FALSE, FALSE);
4661 if (stub_entry)
4662 {
4663 free (stub_name);
4664 return TRUE;
4665 }
4666
4667 /* We always place an 843419 workaround veneer in the stub section
4668 attached to the input section in which an erratum sequence has
4669 been found. This ensures that later in the link process (in
4670 elfNN_aarch64_write_section) when we copy the veneered
4671 instruction from the input section into the stub section the
4672 copied instruction will have had any relocations applied to it.
4673 If we placed workaround veneers in any other stub section then we
4674 could not assume that all relocations have been processed on the
4675 corresponding input section at the point we output the stub
4676 section. */
4677
4678 stub_entry = _bfd_aarch64_add_stub_entry_after (stub_name, section, htab);
4679 if (stub_entry == NULL)
4680 {
4681 free (stub_name);
4682 return FALSE;
4683 }
4684
4685 stub_entry->adrp_offset = adrp_offset;
4686 stub_entry->target_value = ldst_offset;
4687 stub_entry->target_section = section;
4688 stub_entry->stub_type = aarch64_stub_erratum_843419_veneer;
4689 stub_entry->veneered_insn = insn;
4690 stub_entry->output_name = stub_name;
4691
4692 return TRUE;
4693 }
4694
4695
4696 /* Scan an input section looking for the signature of erratum 843419.
4697
4698 Scans input SECTION in INPUT_BFD looking for erratum 843419
4699 signatures, for each signature found a stub_entry is created
4700 describing the location of the erratum for subsequent fixup.
4701
4702 Return TRUE on successful scan, FALSE on failure to scan.
4703 */
4704
4705 static bfd_boolean
4706 _bfd_aarch64_erratum_843419_scan (bfd *input_bfd, asection *section,
4707 struct bfd_link_info *info)
4708 {
4709 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
4710
4711 if (htab == NULL)
4712 return TRUE;
4713
4714 if (elf_section_type (section) != SHT_PROGBITS
4715 || (elf_section_flags (section) & SHF_EXECINSTR) == 0
4716 || (section->flags & SEC_EXCLUDE) != 0
4717 || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4718 || (section->output_section == bfd_abs_section_ptr))
4719 return TRUE;
4720
4721 do
4722 {
4723 bfd_byte *contents = NULL;
4724 struct _aarch64_elf_section_data *sec_data;
4725 unsigned int span;
4726
4727 if (elf_section_data (section)->this_hdr.contents != NULL)
4728 contents = elf_section_data (section)->this_hdr.contents;
4729 else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
4730 return FALSE;
4731
4732 sec_data = elf_aarch64_section_data (section);
4733
4734 if (sec_data->mapcount)
4735 qsort (sec_data->map, sec_data->mapcount,
4736 sizeof (elf_aarch64_section_map), elf_aarch64_compare_mapping);
4737
4738 for (span = 0; span < sec_data->mapcount; span++)
4739 {
4740 unsigned int span_start = sec_data->map[span].vma;
4741 unsigned int span_end = ((span == sec_data->mapcount - 1)
4742 ? sec_data->map[0].vma + section->size
4743 : sec_data->map[span + 1].vma);
4744 unsigned int i;
4745 char span_type = sec_data->map[span].type;
4746
4747 if (span_type == 'd')
4748 continue;
4749
4750 for (i = span_start; i + 8 < span_end; i += 4)
4751 {
4752 bfd_vma vma = (section->output_section->vma
4753 + section->output_offset
4754 + i);
4755 bfd_vma veneer_i;
4756
4757 if (_bfd_aarch64_erratum_843419_p
4758 (contents, vma, i, span_end, &veneer_i))
4759 {
4760 uint32_t insn = bfd_getl32 (contents + veneer_i);
4761
4762 if (!_bfd_aarch64_erratum_843419_fixup (insn, i, veneer_i,
4763 section, info))
4764 return FALSE;
4765 }
4766 }
4767 }
4768
4769 if (elf_section_data (section)->this_hdr.contents == NULL)
4770 free (contents);
4771 }
4772 while (0);
4773
4774 return TRUE;
4775 }
4776
4777 static bfd_boolean
4778 section_start_symbol (bfd *abfd ATTRIBUTE_UNUSED, asection *section,
4779 void *valp)
4780 {
4781 return section->vma == *(bfd_vma *)valp;
4782 }
4783
4784 /* Capability format functions. */
4785
4786 static unsigned
4787 exponent (uint64_t len)
4788 {
4789 #define CAP_MAX_EXPONENT 50
4790 /* Size is a 65 bit value, so there's an implicit 0 MSB. */
4791 unsigned zeroes = __builtin_clzl (len) + 1;
4792
4793 /* All bits up to and including CAP_MW - 2 are zero. */
4794 if (CAP_MAX_EXPONENT < zeroes)
4795 return (unsigned) -1;
4796 else
4797 return CAP_MAX_EXPONENT - zeroes;
4798 #undef CAP_MAX_EXPONENT
4799 }
4800
4801 #define ONES(x) ((1ULL << (x)) - 1)
4802 #define ALIGN_UP(x, a) (((x) + ONES (a)) & (~ONES (a)))
4803
4804 static bfd_boolean
4805 c64_valid_cap_range (bfd_vma *basep, bfd_vma *limitp, unsigned *alignmentp)
4806 {
4807 bfd_vma base = *basep, size = *limitp - *basep;
4808
4809 unsigned e, old_e;
4810 *alignmentp = 1;
4811
4812 if ((e = exponent (size)) == (unsigned) -1)
4813 return TRUE;
4814
4815 size = ALIGN_UP (size, e + 3);
4816 old_e = e;
4817 e = exponent (size);
4818 if (old_e != e)
4819 size = ALIGN_UP (size, e + 3);
4820
4821 base = ALIGN_UP (base, e + 3);
4822
4823 *alignmentp = e+3;
4824 if (base == *basep && *limitp == base + size)
4825 return TRUE;
4826
4827 *basep = base;
4828 *limitp = base + size;
4829 return FALSE;
4830 }
4831
4832 /* Check if the bounds of section SEC will get rounded off in the Morello
4833 capability format and if it would, adjust the section to ensure any
4834 capability spanning this section would have its bounds precise. */
4835 static inline void
4836 ensure_precisely_bounded_section (asection *sec,
4837 struct elf_aarch64_link_hash_table *htab,
4838 void (*c64_pad_section) (asection *, bfd_vma))
4839 {
4840 bfd_vma low = sec->vma;
4841 bfd_vma high = sec->vma + sec->size;
4842 unsigned alignment;
4843
4844 bfd_boolean did_change = FALSE;
4845 if (!c64_valid_cap_range (&low, &high, &alignment))
4846 {
4847 bfd_vma padding = high - low - sec->size;
4848 c64_pad_section (sec, padding);
4849 did_change = TRUE;
4850 }
4851 if (sec->alignment_power < alignment)
4852 {
4853 sec->alignment_power = alignment;
4854 did_change = TRUE;
4855 }
4856
4857 if (did_change)
4858 (*htab->layout_sections_again) ();
4859 }
4860
4861 /* Make sure that all capabilities that refer to sections have bounds that
4862 won't overlap with neighbouring sections. This is needed in two specific
4863 cases. The first case is that of PCC, which needs to span across all
4864 readonly sections as well as the GOT and PLT sections in the output binary.
4865 The second case is that of linker and ldscript defined symbols that indicate
4866 start and/or end of sections and/or zero-sized symbols.
4867
4868 In both cases, overlap of capability bounds are avoided by aligning the base
4869 of the section and if necessary, adding a pad at the end of the section so
4870 that the section following it starts only after the pad. */
4871
4872 static bfd_vma pcc_low;
4873 static bfd_vma pcc_high;
4874 void
4875 elfNN_c64_resize_sections (bfd *output_bfd, struct bfd_link_info *info,
4876 void (*c64_pad_section) (asection *, bfd_vma),
4877 void (*layout_sections_again) (void))
4878 {
4879 asection *sec;
4880 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
4881 bfd *input_bfd;
4882 unsigned align = 0;
4883
4884 htab->layout_sections_again = layout_sections_again;
4885
4886 /* If this is not a PURECAP binary, and has no C64 code in it, then this is
4887 just a stock AArch64 binary and the section padding is not necessary.
4888 We can have PURECAP shared libraries that are data-only, so just checking
4889 if there is C64 code in this executable is not enough. We can have HYBRID
4890 binaries, so just checking for PURECAP is not enough. */
4891 if (!(htab->c64_output
4892 || (elf_elfheader (output_bfd)->e_flags & EF_AARCH64_CHERI_PURECAP)))
4893 return;
4894
4895 /* First, walk through all the relocations to find those referring to linker
4896 defined and ldscript defined symbols since we set their range to their
4897 output sections. */
4898 for (input_bfd = info->input_bfds;
4899 /* No point iterating over all relocations to ensure each section that
4900 needs to give the bounds for a capability is padded accordingly if
4901 there are no capability relocations in the GOT and there are no
4902 CAPINIT relocations.
4903 N.b. It is possible that when creating a dynamic object a
4904 section-spanning symbol could be used by some other executable
4905 linking to it when we don't have a relocation to it in the given
4906 dynamic library. Rather than pad every section which has a linker
4907 defined symbol pointing into it we choose to allow such use-cases to
4908 have sizes which bleed into another section. */
4909 (htab->c64_rel || htab->srelcaps) && input_bfd != NULL;
4910 input_bfd = input_bfd->link.next)
4911 {
4912 Elf_Internal_Shdr *symtab_hdr;
4913
4914 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4915 if (symtab_hdr->sh_info == 0)
4916 continue;
4917
4918 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
4919 {
4920 Elf_Internal_Rela *irelaend, *irela;
4921
4922 /* If there aren't any relocs, then there's nothing more to do. */
4923 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
4924 continue;
4925
4926 irela = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
4927 info->keep_memory);
4928 if (irela == NULL)
4929 continue;
4930
4931 /* Now examine each relocation. */
4932 irelaend = irela + sec->reloc_count;
4933 for (; irela < irelaend; irela++)
4934 {
4935 unsigned int r_indx;
4936 struct elf_link_hash_entry *h;
4937 int e_indx;
4938 asection *os;
4939
4940 r_indx = ELFNN_R_SYM (irela->r_info);
4941
4942 /* Linker defined or linker script defined symbols are always in
4943 the symbol hash. */
4944 if (r_indx < symtab_hdr->sh_info)
4945 continue;
4946
4947 e_indx = r_indx - symtab_hdr->sh_info;
4948 h = elf_sym_hashes (input_bfd)[e_indx];
4949
4950 /* XXX Does this ever happen? */
4951 if (h == NULL)
4952 continue;
4953
4954 os = h->root.u.def.section->output_section;
4955
4956 if (h->root.linker_def)
4957 ensure_precisely_bounded_section (os, htab, c64_pad_section);
4958 else if (h->root.ldscript_def)
4959 {
4960 const char *name = h->root.root.string;
4961 size_t len = strlen (name);
4962 asection *altos = NULL;
4963 bfd_vma value = os->vma + os->size;
4964
4965 if (len > 8 && name[0] == '_' && name[1] == '_'
4966 && (!strncmp (name + 2, "start_", 6)
4967 || !strcmp (name + len - 6, "_start"))
4968 && ((altos = bfd_sections_find_if
4969 (info->output_bfd, section_start_symbol, &value))
4970 != NULL))
4971 ensure_precisely_bounded_section (altos, htab,
4972 c64_pad_section);
4973 /* XXX We're overfitting here because the offset of H within
4974 the output section is not yet resolved and ldscript
4975 defined symbols do not have input section information. */
4976 ensure_precisely_bounded_section (os, htab, c64_pad_section);
4977 }
4978 }
4979 }
4980 }
4981
4982 /* Next, walk through output sections to find the PCC span and add a padding
4983 at the end to ensure that PCC bounds don't bleed into neighbouring
4984 sections. For now PCC needs to encompass all code sections, .got, .plt
4985 and .got.plt.
4986 If there is no C64 code in this binary, then we do not need to care about
4987 PCC bounds, hence skip this bit.
4988 It's tempting to also avoid padding the PCC range when we have no static
4989 relocations in this binary, since it would seem that we can never end up
4990 trying to access something "outside" the PCC bounds (any PCC bounded
4991 capability provided to an outside dynamic object would be sealed by the
4992 runtime, and hence can't be offset). Unfortunately it is still possible,
4993 since an `adr c0, 0` gives an unsealed capability to the current code
4994 which could then be offset by some other means.
4995 While that seems unlikely to happen, having no relocations in a file also
4996 seems quite unlikely, so we may as well play it safe. */
4997 if (!htab->c64_output)
4998 return;
4999
5000 bfd_vma low = (bfd_vma) -1, high = 0;
5001 asection *pcc_low_sec = NULL, *pcc_high_sec = NULL;
5002 for (sec = output_bfd->sections; sec != NULL; sec = sec->next)
5003 {
5004 /* XXX This is a good place to figure out if there are any readable or
5005 writable sections in the PCC range that are not in the list of
5006 sections we want the PCC to span and then warn the user of it. */
5007
5008 #define NOT_OP_SECTION(s) ((s) == NULL || (s)->output_section != sec)
5009
5010 if ((sec->flags & SEC_READONLY) == 0
5011 && NOT_OP_SECTION (htab->root.sgotplt)
5012 && NOT_OP_SECTION (htab->root.igotplt)
5013 && NOT_OP_SECTION (htab->root.sgot)
5014 && NOT_OP_SECTION (htab->root.splt)
5015 && NOT_OP_SECTION (htab->root.iplt)
5016 && (sec->vma < info->relro_start
5017 || sec->vma >= info->relro_end))
5018 continue;
5019 if ((sec->flags & SEC_ALLOC) == 0)
5020 continue;
5021
5022 if (sec->vma < low)
5023 {
5024 low = sec->vma;
5025 pcc_low_sec = sec;
5026 }
5027 if (sec->vma + sec->size > high)
5028 {
5029 high = sec->vma + sec->size;
5030 pcc_high_sec = sec;
5031 }
5032
5033 #undef NOT_OP_SECTION
5034 }
5035
5036 /* Set the PCC range to have precise bounds to ensure that PCC relative loads
5037 can not access outside of their given range. */
5038 if (pcc_low_sec != NULL)
5039 {
5040 BFD_ASSERT (pcc_high_sec);
5041
5042 bfd_vma pcc_low_tmp;
5043 bfd_vma pcc_high_tmp;
5044
5045 /* We have to be a little careful about the padding we introduce. The
5046 padding we could calculate here may not be the padding that we would
5047 want after the very first section in the PCC bounds has been aligned
5048 properly. That change in the start address propagated through a few
5049 different sections with their own alignment requirements can easily
5050 change the length of the region we want the PCC to span.
5051 Also, that change in length could change the alignment we want. We
5052 don't proove that the alignment requirement converges, but believe
5053 that it should (there is only so much space that existing alignment
5054 requirements could trigger to be added -- a section with an alignment
5055 requirement of 16 can only really add 15 bytes to the length). */
5056 bfd_boolean valid_range = FALSE;
5057 while (TRUE) {
5058 pcc_low_tmp = pcc_low_sec->vma;
5059 pcc_high_tmp = pcc_high_sec->vma + pcc_high_sec->size;
5060 valid_range =
5061 c64_valid_cap_range (&pcc_low_tmp, &pcc_high_tmp, &align);
5062 if (pcc_low_sec->alignment_power >= align)
5063 break;
5064 pcc_low_sec->alignment_power = align;
5065 (*htab->layout_sections_again) ();
5066 }
5067
5068 /* We have calculated the bottom and top address that we want in the
5069 above call to c64_valid_cap_range. We have also aligned the lowest
5070 section in the PCC range to where we want it. Just have to add the
5071 padding remaining if needs be. */
5072 if (!valid_range)
5073 {
5074 BFD_ASSERT (pcc_low_tmp == pcc_low_sec->vma);
5075 bfd_vma current_length =
5076 (pcc_high_sec->vma + pcc_high_sec->size) - pcc_low_sec->vma;
5077 bfd_vma desired_length = (pcc_high_tmp - pcc_low_tmp);
5078 bfd_vma padding = desired_length - current_length;
5079 c64_pad_section (pcc_high_sec, padding);
5080 (*htab->layout_sections_again) ();
5081 }
5082
5083 pcc_low = pcc_low_sec->vma;
5084 pcc_high = pcc_high_sec->vma + pcc_high_sec->size;
5085 }
5086 }
5087
5088 /* Determine and set the size of the stub section for a final link.
5089
5090 The basic idea here is to examine all the relocations looking for
5091 PC-relative calls to a target that either needs a PE state change (A64 to
5092 C64 or vice versa) or in case of unconditional branches (B/BL), is
5093 unreachable. */
5094
5095 bfd_boolean
5096 elfNN_aarch64_size_stubs (bfd *output_bfd,
5097 bfd *stub_bfd,
5098 struct bfd_link_info *info,
5099 bfd_signed_vma group_size,
5100 asection * (*add_stub_section) (const char *,
5101 asection *),
5102 void (*layout_sections_again) (void))
5103 {
5104 bfd_size_type stub_group_size;
5105 bfd_boolean stubs_always_before_branch;
5106 bfd_boolean stub_changed = FALSE;
5107 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
5108 unsigned int num_erratum_835769_fixes = 0;
5109
5110 /* Propagate mach to stub bfd, because it may not have been
5111 finalized when we created stub_bfd. */
5112 bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
5113 bfd_get_mach (output_bfd));
5114
5115 /* Stash our params away. */
5116 htab->stub_bfd = stub_bfd;
5117 htab->add_stub_section = add_stub_section;
5118 htab->layout_sections_again = layout_sections_again;
5119 stubs_always_before_branch = group_size < 0;
5120 if (group_size < 0)
5121 stub_group_size = -group_size;
5122 else
5123 stub_group_size = group_size;
5124
5125 if (stub_group_size == 1)
5126 {
5127 /* Default values. */
5128 /* AArch64 branch range is +-128MB. The value used is 1MB less. */
5129 stub_group_size = 127 * 1024 * 1024;
5130 }
5131
5132 group_sections (htab, stub_group_size, stubs_always_before_branch);
5133
5134 (*htab->layout_sections_again) ();
5135
5136 if (htab->fix_erratum_835769)
5137 {
5138 bfd *input_bfd;
5139
5140 for (input_bfd = info->input_bfds;
5141 input_bfd != NULL; input_bfd = input_bfd->link.next)
5142 {
5143 if (!is_aarch64_elf (input_bfd)
5144 || (input_bfd->flags & BFD_LINKER_CREATED) != 0)
5145 continue;
5146
5147 if (!_bfd_aarch64_erratum_835769_scan (input_bfd, info,
5148 &num_erratum_835769_fixes))
5149 return FALSE;
5150 }
5151
5152 _bfd_aarch64_resize_stubs (htab);
5153 (*htab->layout_sections_again) ();
5154 }
5155
5156 if (htab->fix_erratum_843419 != ERRAT_NONE)
5157 {
5158 bfd *input_bfd;
5159
5160 for (input_bfd = info->input_bfds;
5161 input_bfd != NULL;
5162 input_bfd = input_bfd->link.next)
5163 {
5164 asection *section;
5165
5166 if (!is_aarch64_elf (input_bfd)
5167 || (input_bfd->flags & BFD_LINKER_CREATED) != 0)
5168 continue;
5169
5170 for (section = input_bfd->sections;
5171 section != NULL;
5172 section = section->next)
5173 if (!_bfd_aarch64_erratum_843419_scan (input_bfd, section, info))
5174 return FALSE;
5175 }
5176
5177 _bfd_aarch64_resize_stubs (htab);
5178 (*htab->layout_sections_again) ();
5179 }
5180
5181 while (1)
5182 {
5183 bfd *input_bfd;
5184
5185 for (input_bfd = info->input_bfds;
5186 input_bfd != NULL; input_bfd = input_bfd->link.next)
5187 {
5188 Elf_Internal_Shdr *symtab_hdr;
5189 asection *section;
5190
5191 if (!is_aarch64_elf (input_bfd)
5192 || (input_bfd->flags & BFD_LINKER_CREATED) != 0)
5193 continue;
5194
5195 /* We'll need the symbol table in a second. */
5196 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5197 if (symtab_hdr->sh_info == 0)
5198 continue;
5199
5200 /* Walk over each section attached to the input bfd. */
5201 for (section = input_bfd->sections;
5202 section != NULL; section = section->next)
5203 {
5204 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
5205
5206 /* If there aren't any relocs, then there's nothing more
5207 to do. */
5208 if ((section->flags & SEC_RELOC) == 0
5209 || section->reloc_count == 0
5210 || (section->flags & SEC_CODE) == 0)
5211 continue;
5212
5213 /* If this section is a link-once section that will be
5214 discarded, then don't create any stubs. */
5215 if (section->output_section == NULL
5216 || section->output_section->owner != output_bfd)
5217 continue;
5218
5219 /* Get the relocs. */
5220 internal_relocs
5221 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
5222 NULL, info->keep_memory);
5223 if (internal_relocs == NULL)
5224 goto error_ret_free_local;
5225
5226 /* Now examine each relocation. */
5227 irela = internal_relocs;
5228 irelaend = irela + section->reloc_count;
5229 for (; irela < irelaend; irela++)
5230 {
5231 unsigned int r_type, r_indx;
5232 enum elf_aarch64_stub_type stub_type = aarch64_stub_none;
5233 struct elf_aarch64_stub_hash_entry *stub_entry;
5234 asection *sym_sec;
5235 bfd_vma sym_value;
5236 bfd_vma destination;
5237 struct elf_aarch64_link_hash_entry *hash;
5238 const char *sym_name;
5239 char *stub_name;
5240 const asection *id_sec;
5241 unsigned char st_type;
5242 bfd_size_type len;
5243 unsigned branch_to_c64 = FALSE;
5244 const char *suffix;
5245
5246 r_type = ELFNN_R_TYPE (irela->r_info);
5247 r_indx = ELFNN_R_SYM (irela->r_info);
5248
5249 if (r_type >= (unsigned int) R_AARCH64_end)
5250 {
5251 bfd_set_error (bfd_error_bad_value);
5252 error_ret_free_internal:
5253 if (elf_section_data (section)->relocs == NULL)
5254 free (internal_relocs);
5255 goto error_ret_free_local;
5256 }
5257
5258 /* Only look for stubs on unconditional branch and
5259 branch and link instructions. */
5260 if (!aarch64_branch_reloc_p (r_type))
5261 continue;
5262
5263 /* Now determine the call target, its name, value,
5264 section. */
5265 sym_sec = NULL;
5266 sym_value = 0;
5267 destination = 0;
5268 hash = NULL;
5269 sym_name = NULL;
5270 if (r_indx < symtab_hdr->sh_info)
5271 {
5272 /* It's a local symbol. */
5273 Elf_Internal_Sym *sym =
5274 bfd_sym_from_r_symndx (&htab->root.sym_cache,
5275 input_bfd, r_indx);
5276 if (sym == NULL)
5277 goto error_ret_free_internal;
5278
5279 branch_to_c64 |= (sym->st_target_internal
5280 & ST_BRANCH_TO_C64);
5281
5282 Elf_Internal_Shdr *hdr =
5283 elf_elfsections (input_bfd)[sym->st_shndx];
5284
5285 sym_sec = hdr->bfd_section;
5286 if (!sym_sec)
5287 /* This is an undefined symbol. It can never
5288 be resolved. */
5289 continue;
5290
5291 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5292 sym_value = sym->st_value;
5293 destination = (sym_value + irela->r_addend
5294 + sym_sec->output_offset
5295 + sym_sec->output_section->vma);
5296 st_type = ELF_ST_TYPE (sym->st_info);
5297 sym_name
5298 = bfd_elf_string_from_elf_section (input_bfd,
5299 symtab_hdr->sh_link,
5300 sym->st_name);
5301
5302 /* Get the interworking stub if needed. */
5303 stub_type = aarch64_interwork_stub (r_type,
5304 branch_to_c64);
5305 }
5306 else
5307 {
5308 int e_indx;
5309 struct elf_aarch64_link_hash_table *globals =
5310 elf_aarch64_hash_table (info);
5311
5312 e_indx = r_indx - symtab_hdr->sh_info;
5313 hash = ((struct elf_aarch64_link_hash_entry *)
5314 elf_sym_hashes (input_bfd)[e_indx]);
5315
5316 while (hash->root.root.type == bfd_link_hash_indirect
5317 || hash->root.root.type == bfd_link_hash_warning)
5318 hash = ((struct elf_aarch64_link_hash_entry *)
5319 hash->root.root.u.i.link);
5320
5321 /* Static executable. */
5322 if (globals->root.splt == NULL || hash == NULL
5323 || hash->root.plt.offset == (bfd_vma) - 1)
5324 {
5325 branch_to_c64 |= (hash->root.target_internal
5326 & ST_BRANCH_TO_C64);
5327 stub_type = aarch64_interwork_stub (r_type,
5328 branch_to_c64);
5329 }
5330
5331 if (hash->root.root.type == bfd_link_hash_defined
5332 || hash->root.root.type == bfd_link_hash_defweak)
5333 {
5334 sym_sec = hash->root.root.u.def.section;
5335 sym_value = hash->root.root.u.def.value;
5336 /* For a destination in a shared library,
5337 use the PLT stub as target address to
5338 decide whether a branch stub is
5339 needed. */
5340 if (globals->root.splt != NULL && hash != NULL
5341 && hash->root.plt.offset != (bfd_vma) - 1)
5342 {
5343 sym_sec = globals->root.splt;
5344 sym_value = hash->root.plt.offset;
5345 if (sym_sec->output_section != NULL)
5346 destination = (sym_value
5347 + sym_sec->output_offset
5348 +
5349 sym_sec->output_section->vma);
5350 }
5351 else if (sym_sec->output_section != NULL)
5352 destination = (sym_value + irela->r_addend
5353 + sym_sec->output_offset
5354 + sym_sec->output_section->vma);
5355 }
5356 else if (hash->root.root.type == bfd_link_hash_undefined
5357 || (hash->root.root.type
5358 == bfd_link_hash_undefweak))
5359 {
5360 /* For a shared library, use the PLT stub as
5361 target address to decide whether a long
5362 branch stub is needed.
5363 For absolute code, they cannot be handled. */
5364
5365 if (globals->root.splt != NULL && hash != NULL
5366 && hash->root.plt.offset != (bfd_vma) - 1)
5367 {
5368 sym_sec = globals->root.splt;
5369 sym_value = hash->root.plt.offset;
5370 if (sym_sec->output_section != NULL)
5371 destination = (sym_value
5372 + sym_sec->output_offset
5373 +
5374 sym_sec->output_section->vma);
5375 }
5376 else
5377 continue;
5378 }
5379 else
5380 {
5381 bfd_set_error (bfd_error_bad_value);
5382 goto error_ret_free_internal;
5383 }
5384 st_type = ELF_ST_TYPE (hash->root.type);
5385 sym_name = hash->root.root.root.string;
5386 }
5387
5388 /* Determine what (if any) linker stub is needed. */
5389 if (stub_type == aarch64_stub_none)
5390 stub_type = aarch64_type_of_stub (section, irela, sym_sec,
5391 st_type, destination);
5392
5393 if (stub_type == aarch64_stub_none)
5394 continue;
5395
5396 /* Support for grouping stub sections. */
5397 id_sec = htab->stub_group[section->id].link_sec;
5398
5399 /* Get the name of this stub. */
5400 stub_name = elfNN_aarch64_stub_name (id_sec, sym_sec, hash,
5401 irela, stub_type);
5402 if (!stub_name)
5403 goto error_ret_free_internal;
5404
5405 stub_entry =
5406 aarch64_stub_hash_lookup (&htab->stub_hash_table,
5407 stub_name, FALSE, FALSE);
5408 if (stub_entry != NULL)
5409 {
5410 /* The proper stub has already been created. */
5411 free (stub_name);
5412 /* Always update this stub's target since it may have
5413 changed after layout. */
5414 stub_entry->target_value = sym_value + irela->r_addend;
5415
5416 /* Set LSB for A64 to C64 branch. */
5417 if (branch_to_c64)
5418 stub_entry->target_value |= 1;
5419
5420 continue;
5421 }
5422
5423 stub_entry = _bfd_aarch64_add_stub_entry_in_group
5424 (stub_name, section, htab);
5425 if (stub_entry == NULL)
5426 {
5427 free (stub_name);
5428 goto error_ret_free_internal;
5429 }
5430
5431 stub_entry->target_value = sym_value + irela->r_addend;
5432 /* Set LSB for A64 to C64 branch. */
5433 if (branch_to_c64)
5434 stub_entry->target_value |= 1;
5435
5436 stub_entry->target_section = sym_sec;
5437 stub_entry->stub_type = stub_type;
5438 stub_entry->h = hash;
5439 stub_entry->st_type = st_type;
5440
5441 suffix = aarch64_lookup_stub_type_suffix (stub_type);
5442
5443 if (sym_name == NULL)
5444 sym_name = "unnamed";
5445 len = (sizeof (STUB_ENTRY_NAME) + strlen (sym_name)
5446 + strlen (suffix));
5447 stub_entry->output_name = bfd_alloc (htab->stub_bfd, len);
5448 if (stub_entry->output_name == NULL)
5449 {
5450 free (stub_name);
5451 goto error_ret_free_internal;
5452 }
5453
5454 snprintf (stub_entry->output_name, len, STUB_ENTRY_NAME,
5455 sym_name, suffix);
5456
5457 stub_changed = TRUE;
5458 }
5459
5460 /* We're done with the internal relocs, free them. */
5461 if (elf_section_data (section)->relocs == NULL)
5462 free (internal_relocs);
5463 }
5464 }
5465
5466 if (!stub_changed)
5467 break;
5468
5469 _bfd_aarch64_resize_stubs (htab);
5470
5471 /* Ask the linker to do its stuff. */
5472 (*htab->layout_sections_again) ();
5473 stub_changed = FALSE;
5474 }
5475
5476 return TRUE;
5477
5478 error_ret_free_local:
5479 return FALSE;
5480 }
5481
5482 /* Build all the stubs associated with the current output file. The
5483 stubs are kept in a hash table attached to the main linker hash
5484 table. We also set up the .plt entries for statically linked PIC
5485 functions here. This function is called via aarch64_elf_finish in the
5486 linker. */
5487
5488 bfd_boolean
5489 elfNN_aarch64_build_stubs (struct bfd_link_info *info)
5490 {
5491 asection *stub_sec;
5492 struct bfd_hash_table *table;
5493 struct elf_aarch64_link_hash_table *htab;
5494
5495 htab = elf_aarch64_hash_table (info);
5496
5497 for (stub_sec = htab->stub_bfd->sections;
5498 stub_sec != NULL; stub_sec = stub_sec->next)
5499 {
5500 bfd_size_type size;
5501
5502 /* Ignore non-stub sections. */
5503 if (!strstr (stub_sec->name, STUB_SUFFIX))
5504 continue;
5505
5506 /* Allocate memory to hold the linker stubs. */
5507 size = stub_sec->size;
5508 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
5509 if (stub_sec->contents == NULL && size != 0)
5510 return FALSE;
5511 stub_sec->size = 0;
5512
5513 /* Add a branch around the stub section, and a nop, to keep it 8 byte
5514 aligned, as long branch stubs contain a 64-bit address. */
5515 bfd_putl32 (0x14000000 | (size >> 2), stub_sec->contents);
5516 bfd_putl32 (INSN_NOP, stub_sec->contents + 4);
5517 stub_sec->size += 8;
5518 }
5519
5520 /* Build the stubs as directed by the stub hash table. */
5521 table = &htab->stub_hash_table;
5522
5523 bfd_error_type save_error = bfd_get_error ();
5524 bfd_set_error (bfd_error_no_error);
5525 bfd_hash_traverse (table, aarch64_build_one_stub, info);
5526
5527 if (bfd_get_error () != bfd_error_no_error)
5528 return FALSE;
5529
5530 bfd_set_error (save_error);
5531
5532 return TRUE;
5533 }
5534
5535
5536 /* Add an entry to the code/data map for section SEC. */
5537
5538 static void
5539 elfNN_aarch64_section_map_add (bfd *abfd, asection *sec, char type,
5540 bfd_vma vma)
5541 {
5542 struct _aarch64_elf_section_data *sec_data =
5543 elf_aarch64_section_data (sec);
5544 unsigned int newidx;
5545
5546 /* The aarch64 section hook was not called for this section. */
5547 if (!sec_data->elf.is_target_section_data)
5548 {
5549 struct _aarch64_elf_section_data *newdata =
5550 bfd_zalloc (abfd, sizeof (*newdata));
5551
5552 if (newdata == NULL)
5553 return;
5554
5555 newdata->elf = sec_data->elf;
5556 newdata->elf.is_target_section_data = TRUE;
5557 free (sec_data);
5558 sec->used_by_bfd = sec_data = newdata;
5559 }
5560
5561 if (sec_data->map == NULL)
5562 {
5563 sec_data->map = bfd_malloc (sizeof (elf_aarch64_section_map));
5564 sec_data->mapcount = 0;
5565 sec_data->mapsize = 1;
5566 }
5567
5568 newidx = sec_data->mapcount++;
5569
5570 if (sec_data->mapcount > sec_data->mapsize)
5571 {
5572 sec_data->mapsize *= 2;
5573 sec_data->map = bfd_realloc_or_free
5574 (sec_data->map, sec_data->mapsize * sizeof (elf_aarch64_section_map));
5575 }
5576
5577 if (sec_data->map)
5578 {
5579 sec_data->map[newidx].vma = vma;
5580 sec_data->map[newidx].type = type;
5581 }
5582 }
5583
5584
5585 /* Initialise maps of insn/data for input BFDs. */
5586 void
5587 bfd_elfNN_aarch64_init_maps (bfd *abfd, struct bfd_link_info *info)
5588 {
5589 Elf_Internal_Sym *isymbuf;
5590 Elf_Internal_Shdr *hdr;
5591 unsigned int i, localsyms;
5592
5593 /* Make sure that we are dealing with an AArch64 elf binary. */
5594 if (!is_aarch64_elf (abfd))
5595 return;
5596
5597 if (elf_aarch64_tdata (abfd)->secmaps_initialised)
5598 return;
5599
5600 if ((abfd->flags & DYNAMIC) != 0)
5601 return;
5602
5603 hdr = &elf_symtab_hdr (abfd);
5604 localsyms = hdr->sh_info;
5605
5606 /* Obtain a buffer full of symbols for this BFD. The hdr->sh_info field
5607 should contain the number of local symbols, which should come before any
5608 global symbols. Mapping symbols are always local. */
5609 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, localsyms, 0, NULL, NULL, NULL);
5610
5611 /* No internal symbols read? Skip this BFD. */
5612 if (isymbuf == NULL)
5613 return;
5614
5615 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table ((info));
5616
5617 for (i = 0; i < localsyms; i++)
5618 {
5619 Elf_Internal_Sym *isym = &isymbuf[i];
5620 asection *sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
5621 const char *name;
5622
5623 if (sec != NULL && ELF_ST_BIND (isym->st_info) == STB_LOCAL)
5624 {
5625 name = bfd_elf_string_from_elf_section (abfd,
5626 hdr->sh_link,
5627 isym->st_name);
5628
5629 if (bfd_is_aarch64_special_symbol_name
5630 (name, BFD_AARCH64_SPECIAL_SYM_TYPE_MAP))
5631 {
5632 elfNN_aarch64_section_map_add (abfd, sec, name[1],
5633 isym->st_value);
5634 if (!htab->c64_output && name[1] == 'c')
5635 htab->c64_output = TRUE;
5636 }
5637 }
5638 }
5639 elf_aarch64_tdata (abfd)->secmaps_initialised = TRUE;
5640 }
5641
5642 static void
5643 setup_plt_values (struct bfd_link_info *link_info,
5644 aarch64_plt_type plt_type)
5645 {
5646 struct elf_aarch64_link_hash_table *globals;
5647 globals = elf_aarch64_hash_table (link_info);
5648
5649 /* Set up plt stubs in case we need C64 PLT. Override BTI/PAC since they're
5650 not compatible. PLT stub sizes are the same as the default ones. */
5651 if (globals->c64_rel)
5652 {
5653 if (plt_type != PLT_NORMAL)
5654 _bfd_error_handler
5655 (_("ignoring C64-incompatible extensions: %s"),
5656 (plt_type == PLT_BTI_PAC ? "BTI, PAC"
5657 : plt_type == PLT_BTI ? "BTI" : "PAC"));
5658
5659 globals->plt0_entry = elfNN_c64_small_plt0_entry;
5660 globals->plt_entry = elfNN_c64_small_plt_entry;
5661 return;
5662 }
5663
5664 if (plt_type == PLT_BTI_PAC)
5665 {
5666 globals->plt0_entry = elfNN_aarch64_small_plt0_bti_entry;
5667
5668 /* Only in ET_EXEC we need PLTn with BTI. */
5669 if (bfd_link_pde (link_info))
5670 {
5671 globals->plt_entry_size = PLT_BTI_PAC_SMALL_ENTRY_SIZE;
5672 globals->plt_entry = elfNN_aarch64_small_plt_bti_pac_entry;
5673 }
5674 else
5675 {
5676 globals->plt_entry_size = PLT_PAC_SMALL_ENTRY_SIZE;
5677 globals->plt_entry = elfNN_aarch64_small_plt_pac_entry;
5678 }
5679 }
5680 else if (plt_type == PLT_BTI)
5681 {
5682 globals->plt0_entry = elfNN_aarch64_small_plt0_bti_entry;
5683
5684 /* Only in ET_EXEC we need PLTn with BTI. */
5685 if (bfd_link_pde (link_info))
5686 {
5687 globals->plt_entry_size = PLT_BTI_SMALL_ENTRY_SIZE;
5688 globals->plt_entry = elfNN_aarch64_small_plt_bti_entry;
5689 }
5690 }
5691 else if (plt_type == PLT_PAC)
5692 {
5693 globals->plt_entry_size = PLT_PAC_SMALL_ENTRY_SIZE;
5694 globals->plt_entry = elfNN_aarch64_small_plt_pac_entry;
5695 }
5696 }
5697
5698 /* Set option values needed during linking. */
5699 void
5700 bfd_elfNN_aarch64_set_options (struct bfd *output_bfd,
5701 struct bfd_link_info *link_info,
5702 int no_enum_warn,
5703 int no_wchar_warn, int pic_veneer,
5704 int fix_erratum_835769,
5705 erratum_84319_opts fix_erratum_843419,
5706 int no_apply_dynamic_relocs,
5707 aarch64_bti_pac_info bp_info)
5708 {
5709 struct elf_aarch64_link_hash_table *globals;
5710
5711 globals = elf_aarch64_hash_table (link_info);
5712 globals->pic_veneer = pic_veneer;
5713 globals->fix_erratum_835769 = fix_erratum_835769;
5714 /* If the default options are used, then ERRAT_ADR will be set by default
5715 which will enable the ADRP->ADR workaround for the erratum 843419
5716 workaround. */
5717 globals->fix_erratum_843419 = fix_erratum_843419;
5718 globals->no_apply_dynamic_relocs = no_apply_dynamic_relocs;
5719 globals->c64_rel = 0;
5720
5721 BFD_ASSERT (is_aarch64_elf (output_bfd));
5722 elf_aarch64_tdata (output_bfd)->no_enum_size_warning = no_enum_warn;
5723 elf_aarch64_tdata (output_bfd)->no_wchar_size_warning = no_wchar_warn;
5724
5725 switch (bp_info.bti_type)
5726 {
5727 case BTI_WARN:
5728 elf_aarch64_tdata (output_bfd)->no_bti_warn = 0;
5729 elf_aarch64_tdata (output_bfd)->gnu_and_prop
5730 |= GNU_PROPERTY_AARCH64_FEATURE_1_BTI;
5731 break;
5732
5733 default:
5734 break;
5735 }
5736 elf_aarch64_tdata (output_bfd)->plt_type = bp_info.plt_type;
5737 elf_aarch64_tdata (output_bfd)->secmaps_initialised = FALSE;
5738 }
5739
5740 static bfd_vma
5741 aarch64_calculate_got_entry_vma (struct elf_link_hash_entry *h,
5742 struct elf_aarch64_link_hash_table
5743 *globals, struct bfd_link_info *info,
5744 bfd_vma value, bfd *output_bfd,
5745 bfd_boolean *unresolved_reloc_p)
5746 {
5747 bfd_vma off = (bfd_vma) - 1;
5748 asection *basegot = globals->root.sgot;
5749 bfd_boolean dyn = globals->root.dynamic_sections_created;
5750
5751 if (h != NULL)
5752 {
5753 BFD_ASSERT (basegot != NULL);
5754 off = h->got.offset;
5755 BFD_ASSERT (off != (bfd_vma) - 1);
5756 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5757 || (bfd_link_pic (info)
5758 && SYMBOL_REFERENCES_LOCAL (info, h))
5759 || (ELF_ST_VISIBILITY (h->other)
5760 && h->root.type == bfd_link_hash_undefweak))
5761 {
5762 /* This is actually a static link, or it is a -Bsymbolic link
5763 and the symbol is defined locally. We must initialize this
5764 entry in the global offset table. Since the offset must
5765 always be a multiple of 8 (4 in the case of ILP32), we use
5766 the least significant bit to record whether we have
5767 initialized it already.
5768 When doing a dynamic link, we create a .rel(a).got relocation
5769 entry to initialize the value. This is done in the
5770 finish_dynamic_symbol routine. */
5771 if ((off & 1) != 0)
5772 off &= ~1;
5773 else
5774 {
5775 bfd_put_NN (output_bfd, value, basegot->contents + off);
5776 h->got.offset |= 1;
5777 }
5778 }
5779 else
5780 *unresolved_reloc_p = FALSE;
5781
5782 off = off + basegot->output_section->vma + basegot->output_offset;
5783 }
5784
5785 return off;
5786 }
5787
5788 /* Change R_TYPE to a more efficient access model where possible,
5789 return the new reloc type. */
5790
5791 static bfd_reloc_code_real_type
5792 aarch64_tls_transition_without_check (bfd_reloc_code_real_type r_type,
5793 struct bfd_link_info *info,
5794 struct elf_link_hash_entry *h,
5795 bfd_boolean morello_reloc)
5796 {
5797 bfd_boolean is_local = h == NULL;
5798
5799 switch (r_type)
5800 {
5801 case BFD_RELOC_MORELLO_TLSDESC_ADR_PAGE20:
5802 return (is_local || !bfd_link_pic (info)
5803 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1
5804 : r_type);
5805
5806 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
5807 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
5808 return (is_local
5809 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1
5810 : BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21);
5811
5812 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
5813 return (is_local
5814 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
5815 : r_type);
5816
5817 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
5818 return (is_local
5819 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1
5820 : BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19);
5821
5822 case BFD_RELOC_AARCH64_TLSDESC_LDR:
5823 return (is_local
5824 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
5825 : BFD_RELOC_AARCH64_NONE);
5826
5827 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
5828 return (is_local
5829 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC
5830 : BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC);
5831
5832 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
5833 return (is_local
5834 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2
5835 : BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1);
5836
5837 case BFD_RELOC_MORELLO_TLSDESC_LD128_LO12:
5838 return ((is_local || !bfd_link_pie (info)
5839 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC : r_type));
5840
5841 case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
5842 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
5843 return (is_local
5844 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
5845 : BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC);
5846
5847 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5848 return is_local ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1 : r_type;
5849
5850 case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
5851 return is_local ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC : r_type;
5852
5853 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
5854 return r_type;
5855
5856 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
5857 return (is_local
5858 ? BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12
5859 : BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19);
5860
5861 case BFD_RELOC_MORELLO_TLSDESC_CALL:
5862 return ((is_local || !bfd_link_pie (info))
5863 ? BFD_RELOC_AARCH64_NONE : r_type);
5864
5865 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
5866 if (morello_reloc && !is_local && bfd_link_pie (info))
5867 return r_type;
5868 /* Fall through. */
5869 case BFD_RELOC_AARCH64_TLSDESC_ADD:
5870 case BFD_RELOC_AARCH64_TLSDESC_CALL:
5871 /* Instructions with these relocations will be fully resolved during the
5872 transition into either a NOP in the A64 case or movk and add in
5873 C64. */
5874 return BFD_RELOC_AARCH64_NONE;
5875
5876 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
5877 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
5878 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
5879 return is_local ? BFD_RELOC_AARCH64_NONE : r_type;
5880
5881 #if ARCH_SIZE == 64
5882 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
5883 return is_local
5884 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC
5885 : BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC;
5886
5887 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
5888 return is_local
5889 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2
5890 : BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1;
5891 #endif
5892
5893 default:
5894 break;
5895 }
5896
5897 return r_type;
5898 }
5899
5900 static unsigned int
5901 aarch64_reloc_got_type (bfd_reloc_code_real_type r_type)
5902 {
5903 switch (r_type)
5904 {
5905 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
5906 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
5907 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
5908 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
5909 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
5910 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
5911 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
5912 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
5913 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
5914 return GOT_NORMAL;
5915
5916 case BFD_RELOC_MORELLO_ADR_GOT_PAGE:
5917 case BFD_RELOC_MORELLO_LD128_GOT_LO12_NC:
5918 return GOT_CAP;
5919
5920 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
5921 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
5922 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
5923 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
5924 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
5925 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
5926 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
5927 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
5928 return GOT_TLS_GD;
5929
5930 case BFD_RELOC_MORELLO_TLSDESC_ADR_PAGE20:
5931 case BFD_RELOC_MORELLO_TLSDESC_CALL:
5932 case BFD_RELOC_MORELLO_TLSDESC_LD128_LO12:
5933 return GOT_TLSDESC_GD | GOT_CAP;
5934
5935 case BFD_RELOC_AARCH64_TLSDESC_ADD:
5936 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
5937 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
5938 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
5939 case BFD_RELOC_AARCH64_TLSDESC_CALL:
5940 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
5941 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12:
5942 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
5943 case BFD_RELOC_AARCH64_TLSDESC_LDR:
5944 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
5945 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
5946 return GOT_TLSDESC_GD;
5947
5948 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5949 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
5950 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5951 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
5952 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
5953 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
5954 return GOT_TLS_IE;
5955
5956 default:
5957 break;
5958 }
5959 return GOT_UNKNOWN;
5960 }
5961
5962 static bfd_boolean
5963 aarch64_can_relax_tls (bfd *input_bfd,
5964 struct bfd_link_info *info,
5965 const Elf_Internal_Rela *rel,
5966 struct elf_link_hash_entry *h,
5967 unsigned long r_symndx)
5968 {
5969 unsigned int symbol_got_type;
5970 unsigned int reloc_got_type;
5971
5972 bfd_reloc_code_real_type bfd_r_type
5973 = elfNN_aarch64_bfd_reloc_from_type (input_bfd,
5974 ELFNN_R_TYPE (rel->r_info));
5975
5976 if (! IS_AARCH64_TLS_RELAX_RELOC (bfd_r_type))
5977 return FALSE;
5978
5979 symbol_got_type = elfNN_aarch64_symbol_got_type (h, input_bfd, r_symndx);
5980 reloc_got_type = aarch64_reloc_got_type (bfd_r_type);
5981
5982 if (symbol_got_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (reloc_got_type))
5983 return TRUE;
5984
5985 if (!bfd_link_executable (info))
5986 return FALSE;
5987
5988 if (h && h->root.type == bfd_link_hash_undefweak)
5989 return FALSE;
5990
5991 return TRUE;
5992 }
5993
5994 /* Given the relocation code R_TYPE, return the relaxed bfd reloc
5995 enumerator. */
5996
5997 static bfd_reloc_code_real_type
5998 aarch64_tls_transition (bfd *input_bfd,
5999 struct bfd_link_info *info,
6000 const Elf_Internal_Rela *rel,
6001 struct elf_link_hash_entry *h,
6002 unsigned long r_symndx)
6003 {
6004 bfd_reloc_code_real_type bfd_r_type
6005 = elfNN_aarch64_bfd_reloc_from_type (input_bfd,
6006 ELFNN_R_TYPE (rel->r_info));
6007
6008 if (! aarch64_can_relax_tls (input_bfd, info, rel, h, r_symndx))
6009 return bfd_r_type;
6010
6011 bfd_boolean morello_reloc = (bfd_r_type == BFD_RELOC_AARCH64_TLSDESC_ADD_LO12
6012 && (ELFNN_R_TYPE (rel[1].r_info)
6013 == MORELLO_R (TLSDESC_CALL)));
6014
6015 /* GD -> IE is not supported for Morello TLSDESC yet. We do however allow
6016 lowering of GD -> LE for static non-pie executables. XXX It ought to be
6017 safe to do this for A64 as well but it is not implemented yet. */
6018 if (h != NULL && morello_reloc && bfd_link_pie (info))
6019 return bfd_r_type;
6020
6021 return aarch64_tls_transition_without_check (bfd_r_type, info, h,
6022 morello_reloc);
6023 }
6024
6025 /* Return the base VMA address which should be subtracted from real addresses
6026 when resolving R_AARCH64_TLS_DTPREL relocation. */
6027
6028 static bfd_vma
6029 dtpoff_base (struct bfd_link_info *info)
6030 {
6031 /* If tls_sec is NULL, we should have signalled an error already. */
6032 BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
6033 return elf_hash_table (info)->tls_sec->vma;
6034 }
6035
6036 /* Return the base VMA address which should be subtracted from real addresses
6037 when resolving R_AARCH64_TLS_GOTTPREL64 relocations. */
6038
6039 static bfd_vma
6040 tpoff_base (struct bfd_link_info *info)
6041 {
6042 struct elf_link_hash_table *htab = elf_hash_table (info);
6043
6044 /* If tls_sec is NULL, we should have signalled an error already. */
6045 BFD_ASSERT (htab->tls_sec != NULL);
6046
6047 bfd_vma base = align_power ((bfd_vma) TCB_SIZE (info->output_bfd),
6048 htab->tls_sec->alignment_power);
6049 return htab->tls_sec->vma - base;
6050 }
6051
6052 static bfd_vma *
6053 symbol_got_offset_ref (bfd *input_bfd, struct elf_link_hash_entry *h,
6054 unsigned long r_symndx)
6055 {
6056 /* Calculate the address of the GOT entry for symbol
6057 referred to in h. */
6058 if (h != NULL)
6059 return &h->got.offset;
6060 else
6061 {
6062 /* local symbol */
6063 struct elf_aarch64_local_symbol *l;
6064
6065 l = elf_aarch64_locals (input_bfd);
6066 return &l[r_symndx].got_offset;
6067 }
6068 }
6069
6070 static void
6071 symbol_got_offset_mark (bfd *input_bfd, struct elf_link_hash_entry *h,
6072 unsigned long r_symndx)
6073 {
6074 bfd_vma *p;
6075 p = symbol_got_offset_ref (input_bfd, h, r_symndx);
6076 *p |= 1;
6077 }
6078
6079 static int
6080 symbol_got_offset_mark_p (bfd *input_bfd, struct elf_link_hash_entry *h,
6081 unsigned long r_symndx)
6082 {
6083 bfd_vma value;
6084 value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
6085 return value & 1;
6086 }
6087
6088 static bfd_vma
6089 symbol_got_offset (bfd *input_bfd, struct elf_link_hash_entry *h,
6090 unsigned long r_symndx)
6091 {
6092 bfd_vma value;
6093 value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
6094 value &= ~1;
6095 return value;
6096 }
6097
6098 static bfd_vma *
6099 symbol_tlsdesc_got_offset_ref (bfd *input_bfd, struct elf_link_hash_entry *h,
6100 unsigned long r_symndx)
6101 {
6102 /* Calculate the address of the GOT entry for symbol
6103 referred to in h. */
6104 if (h != NULL)
6105 {
6106 struct elf_aarch64_link_hash_entry *eh;
6107 eh = (struct elf_aarch64_link_hash_entry *) h;
6108 return &eh->tlsdesc_got_jump_table_offset;
6109 }
6110 else
6111 {
6112 /* local symbol */
6113 struct elf_aarch64_local_symbol *l;
6114
6115 l = elf_aarch64_locals (input_bfd);
6116 return &l[r_symndx].tlsdesc_got_jump_table_offset;
6117 }
6118 }
6119
6120 static void
6121 symbol_tlsdesc_got_offset_mark (bfd *input_bfd, struct elf_link_hash_entry *h,
6122 unsigned long r_symndx)
6123 {
6124 bfd_vma *p;
6125 p = symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
6126 *p |= 1;
6127 }
6128
6129 static int
6130 symbol_tlsdesc_got_offset_mark_p (bfd *input_bfd,
6131 struct elf_link_hash_entry *h,
6132 unsigned long r_symndx)
6133 {
6134 bfd_vma value;
6135 value = * symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
6136 return value & 1;
6137 }
6138
6139 static bfd_vma
6140 symbol_tlsdesc_got_offset (bfd *input_bfd, struct elf_link_hash_entry *h,
6141 unsigned long r_symndx)
6142 {
6143 bfd_vma value;
6144 value = * symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
6145 value &= ~1;
6146 return value;
6147 }
6148
6149 /* Data for make_branch_to_erratum_835769_stub(). */
6150
6151 struct erratum_835769_branch_to_stub_data
6152 {
6153 struct bfd_link_info *info;
6154 asection *output_section;
6155 bfd_byte *contents;
6156 };
6157
6158 /* Helper to insert branches to erratum 835769 stubs in the right
6159 places for a particular section. */
6160
6161 static bfd_boolean
6162 make_branch_to_erratum_835769_stub (struct bfd_hash_entry *gen_entry,
6163 void *in_arg)
6164 {
6165 struct elf_aarch64_stub_hash_entry *stub_entry;
6166 struct erratum_835769_branch_to_stub_data *data;
6167 bfd_byte *contents;
6168 unsigned long branch_insn = 0;
6169 bfd_vma veneered_insn_loc, veneer_entry_loc;
6170 bfd_signed_vma branch_offset;
6171 unsigned int target;
6172 bfd *abfd;
6173
6174 stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
6175 data = (struct erratum_835769_branch_to_stub_data *) in_arg;
6176
6177 if (stub_entry->target_section != data->output_section
6178 || stub_entry->stub_type != aarch64_stub_erratum_835769_veneer)
6179 return TRUE;
6180
6181 contents = data->contents;
6182 veneered_insn_loc = stub_entry->target_section->output_section->vma
6183 + stub_entry->target_section->output_offset
6184 + stub_entry->target_value;
6185 veneer_entry_loc = stub_entry->stub_sec->output_section->vma
6186 + stub_entry->stub_sec->output_offset
6187 + stub_entry->stub_offset;
6188 branch_offset = veneer_entry_loc - veneered_insn_loc;
6189
6190 abfd = stub_entry->target_section->owner;
6191 if (!aarch64_valid_branch_p (veneer_entry_loc, veneered_insn_loc))
6192 _bfd_error_handler
6193 (_("%pB: error: erratum 835769 stub out "
6194 "of range (input file too large)"), abfd);
6195
6196 target = stub_entry->target_value;
6197 branch_insn = 0x14000000;
6198 branch_offset >>= 2;
6199 branch_offset &= 0x3ffffff;
6200 branch_insn |= branch_offset;
6201 bfd_putl32 (branch_insn, &contents[target]);
6202
6203 return TRUE;
6204 }
6205
6206
6207 static bfd_boolean
6208 _bfd_aarch64_erratum_843419_branch_to_stub (struct bfd_hash_entry *gen_entry,
6209 void *in_arg)
6210 {
6211 struct elf_aarch64_stub_hash_entry *stub_entry
6212 = (struct elf_aarch64_stub_hash_entry *) gen_entry;
6213 struct erratum_835769_branch_to_stub_data *data
6214 = (struct erratum_835769_branch_to_stub_data *) in_arg;
6215 struct bfd_link_info *info;
6216 struct elf_aarch64_link_hash_table *htab;
6217 bfd_byte *contents;
6218 asection *section;
6219 bfd *abfd;
6220 bfd_vma place;
6221 uint32_t insn;
6222
6223 info = data->info;
6224 contents = data->contents;
6225 section = data->output_section;
6226
6227 htab = elf_aarch64_hash_table (info);
6228
6229 if (stub_entry->target_section != section
6230 || stub_entry->stub_type != aarch64_stub_erratum_843419_veneer)
6231 return TRUE;
6232
6233 BFD_ASSERT (((htab->fix_erratum_843419 & ERRAT_ADRP) && stub_entry->stub_sec)
6234 || (htab->fix_erratum_843419 & ERRAT_ADR));
6235
6236 /* Only update the stub section if we have one. We should always have one if
6237 we're allowed to use the ADRP errata workaround, otherwise it is not
6238 required. */
6239 if (stub_entry->stub_sec)
6240 {
6241 insn = bfd_getl32 (contents + stub_entry->target_value);
6242 bfd_putl32 (insn,
6243 stub_entry->stub_sec->contents + stub_entry->stub_offset);
6244 }
6245
6246 place = (section->output_section->vma + section->output_offset
6247 + stub_entry->adrp_offset);
6248 insn = bfd_getl32 (contents + stub_entry->adrp_offset);
6249
6250 if (!_bfd_aarch64_adrp_p (insn))
6251 abort ();
6252
6253 bfd_signed_vma imm =
6254 (_bfd_aarch64_sign_extend
6255 ((bfd_vma) _bfd_aarch64_decode_adrp_imm (insn) << 12, 33)
6256 - (place & 0xfff));
6257
6258 if ((htab->fix_erratum_843419 & ERRAT_ADR)
6259 && (imm >= AARCH64_MIN_ADRP_IMM && imm <= AARCH64_MAX_ADRP_IMM))
6260 {
6261 insn = (_bfd_aarch64_reencode_adr_imm (AARCH64_ADR_OP, imm, 0)
6262 | AARCH64_RT (insn));
6263 bfd_putl32 (insn, contents + stub_entry->adrp_offset);
6264 /* Stub is not needed, don't map it out. */
6265 stub_entry->stub_type = aarch64_stub_none;
6266 }
6267 else if (htab->fix_erratum_843419 & ERRAT_ADRP)
6268 {
6269 bfd_vma veneered_insn_loc;
6270 bfd_vma veneer_entry_loc;
6271 bfd_signed_vma branch_offset;
6272 uint32_t branch_insn;
6273
6274 veneered_insn_loc = stub_entry->target_section->output_section->vma
6275 + stub_entry->target_section->output_offset
6276 + stub_entry->target_value;
6277 veneer_entry_loc = stub_entry->stub_sec->output_section->vma
6278 + stub_entry->stub_sec->output_offset
6279 + stub_entry->stub_offset;
6280 branch_offset = veneer_entry_loc - veneered_insn_loc;
6281
6282 abfd = stub_entry->target_section->owner;
6283 if (!aarch64_valid_branch_p (veneer_entry_loc, veneered_insn_loc))
6284 _bfd_error_handler
6285 (_("%pB: error: erratum 843419 stub out "
6286 "of range (input file too large)"), abfd);
6287
6288 branch_insn = 0x14000000;
6289 branch_offset >>= 2;
6290 branch_offset &= 0x3ffffff;
6291 branch_insn |= branch_offset;
6292 bfd_putl32 (branch_insn, contents + stub_entry->target_value);
6293 }
6294 else
6295 {
6296 abfd = stub_entry->target_section->owner;
6297 _bfd_error_handler
6298 (_("%pB: error: erratum 843419 immediate 0x%" BFD_VMA_FMT "x "
6299 "out of range for ADR (input file too large) and "
6300 "--fix-cortex-a53-843419=adr used. Run the linker with "
6301 "--fix-cortex-a53-843419=full instead"), abfd, imm);
6302 bfd_set_error (bfd_error_bad_value);
6303 /* This function is called inside a hashtable traversal and the error
6304 handlers called above turn into non-fatal errors. Which means this
6305 case ld returns an exit code 0 and also produces a broken object file.
6306 To prevent this, issue a hard abort. */
6307 BFD_FAIL ();
6308 }
6309 return TRUE;
6310 }
6311
6312
6313 static bfd_boolean
6314 elfNN_aarch64_write_section (bfd *output_bfd ATTRIBUTE_UNUSED,
6315 struct bfd_link_info *link_info,
6316 asection *sec,
6317 bfd_byte *contents)
6318
6319 {
6320 struct elf_aarch64_link_hash_table *globals =
6321 elf_aarch64_hash_table (link_info);
6322
6323 if (globals == NULL)
6324 return FALSE;
6325
6326 /* Fix code to point to erratum 835769 stubs. */
6327 if (globals->fix_erratum_835769)
6328 {
6329 struct erratum_835769_branch_to_stub_data data;
6330
6331 data.info = link_info;
6332 data.output_section = sec;
6333 data.contents = contents;
6334 bfd_hash_traverse (&globals->stub_hash_table,
6335 make_branch_to_erratum_835769_stub, &data);
6336 }
6337
6338 if (globals->fix_erratum_843419)
6339 {
6340 struct erratum_835769_branch_to_stub_data data;
6341
6342 data.info = link_info;
6343 data.output_section = sec;
6344 data.contents = contents;
6345 bfd_hash_traverse (&globals->stub_hash_table,
6346 _bfd_aarch64_erratum_843419_branch_to_stub, &data);
6347 }
6348
6349 return FALSE;
6350 }
6351
6352 /* Return TRUE if RELOC is a relocation against the base of GOT table. */
6353
6354 static bfd_boolean
6355 aarch64_relocation_aginst_gp_p (bfd_reloc_code_real_type reloc)
6356 {
6357 return (reloc == BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14
6358 || reloc == BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15
6359 || reloc == BFD_RELOC_AARCH64_LD64_GOTOFF_LO15
6360 || reloc == BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC
6361 || reloc == BFD_RELOC_AARCH64_MOVW_GOTOFF_G1);
6362 }
6363
6364 /* Build capability meta data, i.e. size and permissions for a capability. */
6365
6366 static bfd_vma
6367 cap_meta (size_t size, const asection *sec, bfd_boolean *guessed)
6368 {
6369
6370 if (size >= (1ULL << 56))
6371 return (bfd_vma) -1;
6372
6373 /* N.b. We are only ever using this function for Morello.
6374 Morello is little-endian.
6375 We are returning a 64bit sized integer.
6376 The format this metadata is supposed to fit is
6377 | 56 bit length | 8 bit permissions |
6378 This means that (in little endian layout) we need to put the 56 bit size
6379 in the *lower* bits of the uint64_t. */
6380 uint64_t flags = 0;
6381 if (sec->flags & SEC_CODE)
6382 flags = 4;
6383 else if (sec->flags & SEC_READONLY
6384 || sec->flags & SEC_ROM)
6385 flags = 1;
6386 else if (sec->flags & SEC_ALLOC)
6387 flags = 2;
6388
6389 /* We should usually be able to derive a valid set of permissions
6390 from the section flags. We know that when a relocation is against an
6391 SHN_ABS symbol the section has no associated flags and we must guess.
6392
6393 As it stands we don't know of any other instances where we do not have
6394 permission flags on a section. We choose to allow instances that we do
6395 not know of rather than abort on them so that if the guess is correct we
6396 don't hamper anyone progressing. */
6397 if (flags == 0)
6398 {
6399 flags = 2;
6400 *guessed = TRUE;
6401 }
6402
6403 return size | (flags << 56);
6404 }
6405
6406 enum c64_section_perm_type {
6407 C64_SYM_UNKNOWN = 0,
6408 C64_SYM_STANDARD,
6409 C64_SYM_LINKER_DEF,
6410 C64_SYM_LDSCRIPT_DEF,
6411 C64_SYM_LDSCRIPT_START,
6412 };
6413
6414 static enum c64_section_perm_type
6415 c64_symbol_section_adjustment (struct elf_link_hash_entry *h, bfd_vma value,
6416 asection *sym_sec, asection **ret_sec,
6417 struct bfd_link_info *info)
6418 {
6419 if (!sym_sec)
6420 return C64_SYM_UNKNOWN;
6421
6422 *ret_sec = sym_sec;
6423 if (!h)
6424 return C64_SYM_STANDARD;
6425
6426 /* Linker defined symbols are always at the start of the section they
6427 track. */
6428 if (h->root.linker_def)
6429 return C64_SYM_LINKER_DEF;
6430 else if (h->root.ldscript_def)
6431 {
6432 const char *name = h->root.root.string;
6433 size_t len = strlen (name);
6434
6435 bfd_vma size = sym_sec->size - (value - sym_sec->vma);
6436 /* The special case: the symbol is at the end of the section.
6437 This could either mean that it is an end symbol or it is the
6438 start of the output section following the symbol. We try to
6439 guess if it is a start of the next section by reading its
6440 name. This is a compatibility hack, ideally linker scripts
6441 should be written such that start symbols are defined within
6442 the output section it intends to track. */
6443 if (size == 0
6444 && (len > 8 && name[0] == '_' && name[1] == '_'
6445 && (!strncmp (name + 2, "start_", 6)
6446 || !strcmp (name + len - 6, "_start"))))
6447 {
6448 asection *s = bfd_sections_find_if (info->output_bfd,
6449 section_start_symbol,
6450 &value);
6451 if (s != NULL)
6452 {
6453 *ret_sec = s;
6454 return C64_SYM_LDSCRIPT_START;
6455 }
6456 }
6457 return C64_SYM_LDSCRIPT_DEF;
6458 }
6459
6460 if (h->start_stop)
6461 {
6462 asection *s = h->u2.start_stop_section->output_section;
6463 BFD_ASSERT (s != NULL);
6464 *ret_sec = s;
6465 return C64_SYM_LDSCRIPT_DEF;
6466 }
6467
6468 return C64_SYM_STANDARD;
6469 }
6470
6471 static bfd_reloc_status_type
6472 c64_fixup_frag (bfd *input_bfd, struct bfd_link_info *info,
6473 bfd_reloc_code_real_type bfd_r_type, Elf_Internal_Sym *sym,
6474 struct elf_link_hash_entry *h, asection *sym_sec,
6475 asection *reloc_sec, bfd_byte *frag_loc, bfd_vma value,
6476 bfd_signed_vma addend, bfd_vma r_offset)
6477 {
6478 BFD_ASSERT (h || sym);
6479 bfd_vma size = sym ? sym->st_size : h->size;
6480 asection *perm_sec = sym_sec;
6481 bfd_boolean bounds_ok = FALSE;
6482
6483 const int aarch64_reloc_idx = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
6484 const char *reloc_name = elfNN_aarch64_howto_table[aarch64_reloc_idx].name;
6485 const char *sym_name;
6486
6487 if (sym)
6488 {
6489 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_bfd);
6490 sym_name = (bfd_elf_string_from_elf_section (input_bfd,
6491 symtab_hdr->sh_link,
6492 sym->st_name));
6493 }
6494 else
6495 sym_name = h->root.root.string;
6496
6497 if (size == 0 && sym_sec)
6498 {
6499 bounds_ok = TRUE;
6500 enum c64_section_perm_type type
6501 = c64_symbol_section_adjustment (h, value, sym_sec, &perm_sec, info);
6502
6503 switch (type)
6504 {
6505 case C64_SYM_STANDARD:
6506 break;
6507 case C64_SYM_LINKER_DEF:
6508 size = perm_sec->output_section->size;
6509 break;
6510 case C64_SYM_LDSCRIPT_DEF:
6511 size = perm_sec->size - (value - perm_sec->vma);
6512 break;
6513 case C64_SYM_LDSCRIPT_START:
6514 size = perm_sec->size;
6515 break;
6516 default:
6517 abort ();
6518 }
6519 }
6520
6521 /* Negative addends are not allowed for capability symbols. */
6522 if (addend < 0 || (bfd_vma) addend > size)
6523 return bfd_reloc_outofrange;
6524
6525 bfd_vma base = value, limit = value + size;
6526 unsigned align = 0;
6527
6528 if (!bounds_ok && !c64_valid_cap_range (&base, &limit, &align))
6529 {
6530 /* Just warn about this. It's not a requirement that bounds on
6531 objects should be precise, so there's no reason to error out on
6532 such an object. */
6533 /* xgettext:c-format */
6534 _bfd_error_handler
6535 (_("%pB: capability range for '%s' may exceed object bounds"),
6536 input_bfd, sym_name);
6537 }
6538
6539 if (perm_sec && perm_sec->flags & SEC_CODE)
6540 {
6541 /* Any symbol pointing into an executable section gets bounds according
6542 to PCC. In this case the relocation is set up so that the value is
6543 the base of the PCC, the addend is the offset from the PCC base to the
6544 VA that we want, and the size is the length of the PCC range.
6545 In this function we only use `value` to check the bounds make sense,
6546 which is somewhat superfluous when we're using pcc_high and pcc_low
6547 since we already enforced that in elfNN_c64_resize_sections. No harm
6548 in instead checking that the bounds on the object that were requested
6549 made sense even if they were overridden because this symbol points
6550 into an executable section.
6551
6552 `size` on the other hand is part of the fragment that we output to and
6553 we need to change it in order to have functions that can access global
6554 data or jump to other functions. */
6555 size = pcc_high - pcc_low;
6556 }
6557
6558 if (perm_sec != NULL)
6559 {
6560 bfd_boolean permissions_guessed = FALSE;
6561 bfd_vma frag = cap_meta (size, perm_sec, &permissions_guessed);
6562
6563 if (frag == (bfd_vma) -1)
6564 return bfd_reloc_outofrange;
6565
6566 if (permissions_guessed)
6567 {
6568 _bfd_error_handler (_("%pB(%pA+%#" PRIx64 "): "
6569 "warning: relocation %s against symbol '%s' in "
6570 "section without permission flags '%s'. "
6571 "Assuming Read-Write."),
6572 input_bfd, reloc_sec, r_offset, reloc_name,
6573 sym_name, perm_sec->name);
6574 }
6575
6576 bfd_put_64 (input_bfd, frag, frag_loc);
6577 }
6578
6579 return bfd_reloc_continue;
6580 }
6581
6582 /* Given either a local symbol SYM or global symbol H, do we need to adjust
6583 capability relocations against the symbol due to the fact that it points to
6584 a code section? */
6585 static bfd_boolean
6586 c64_symbol_adjust (struct elf_link_hash_entry *h,
6587 bfd_vma value, asection *sym_sec, struct bfd_link_info *info,
6588 bfd_vma *adjust_addr)
6589 {
6590 asection *tmp_sec;
6591 enum c64_section_perm_type type
6592 = c64_symbol_section_adjustment (h, value, sym_sec, &tmp_sec, info);
6593
6594 if (type == C64_SYM_UNKNOWN)
6595 return FALSE;
6596
6597 if (tmp_sec->flags & SEC_CODE)
6598 {
6599 *adjust_addr = pcc_low;
6600 return TRUE;
6601 }
6602
6603 return FALSE;
6604 }
6605
6606 /* Perform a relocation as part of a final link. The input relocation type
6607 should be TLS relaxed. */
6608
6609 static bfd_reloc_status_type
6610 elfNN_aarch64_final_link_relocate (reloc_howto_type *howto,
6611 bfd *input_bfd,
6612 bfd *output_bfd,
6613 asection *input_section,
6614 bfd_byte *contents,
6615 Elf_Internal_Rela *rel,
6616 bfd_vma value,
6617 struct bfd_link_info *info,
6618 asection *sym_sec,
6619 struct elf_link_hash_entry *h,
6620 bfd_boolean *unresolved_reloc_p,
6621 bfd_boolean save_addend,
6622 bfd_vma *saved_addend,
6623 Elf_Internal_Sym *sym)
6624 {
6625 Elf_Internal_Shdr *symtab_hdr;
6626 unsigned int r_type = howto->type;
6627 bfd_reloc_code_real_type bfd_r_type
6628 = elfNN_aarch64_bfd_reloc_from_howto (howto);
6629 unsigned long r_symndx;
6630 bfd_byte *hit_data = contents + rel->r_offset;
6631 bfd_vma place, off, got_entry_addr = 0;
6632 bfd_signed_vma signed_addend;
6633 struct elf_aarch64_link_hash_table *globals;
6634 bfd_boolean weak_undef_p;
6635 bfd_boolean relative_reloc;
6636 bfd_boolean c64_needs_frag_fixup;
6637 asection *base_got;
6638 bfd_vma orig_value = value;
6639 bfd_boolean resolved_to_zero;
6640 bfd_boolean abs_symbol_p;
6641 Elf_Internal_Sym *isym = NULL;
6642 bfd_boolean c64_rtype = FALSE;
6643 bfd_boolean to_c64 = FALSE;
6644
6645 globals = elf_aarch64_hash_table (info);
6646
6647 symtab_hdr = &elf_symtab_hdr (input_bfd);
6648
6649 BFD_ASSERT (is_aarch64_elf (input_bfd));
6650
6651 r_symndx = ELFNN_R_SYM (rel->r_info);
6652
6653 place = input_section->output_section->vma
6654 + input_section->output_offset + rel->r_offset;
6655
6656 /* Get addend, accumulating the addend for consecutive relocs
6657 which refer to the same offset. */
6658 signed_addend = saved_addend ? *saved_addend : 0;
6659 signed_addend += rel->r_addend;
6660
6661 weak_undef_p = (h ? h->root.type == bfd_link_hash_undefweak
6662 : bfd_is_und_section (sym_sec));
6663 abs_symbol_p = h != NULL && bfd_is_abs_symbol (&h->root);
6664
6665 if (sym)
6666 {
6667 isym = bfd_sym_from_r_symndx (&globals->root.sym_cache, input_bfd,
6668 r_symndx);
6669 BFD_ASSERT (isym != NULL);
6670 to_c64 = (isym->st_target_internal & ST_BRANCH_TO_C64) != 0;
6671 }
6672 else
6673 to_c64 = (h->target_internal & ST_BRANCH_TO_C64) != 0;
6674
6675
6676 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
6677 it here if it is defined in a non-shared object. */
6678 if (h != NULL
6679 && h->type == STT_GNU_IFUNC
6680 && h->def_regular)
6681 {
6682 asection *plt;
6683 const char *name;
6684 bfd_vma addend = 0;
6685
6686 if ((input_section->flags & SEC_ALLOC) == 0)
6687 {
6688 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
6689 STT_GNU_IFUNC symbol as STT_FUNC. */
6690 if (elf_section_type (input_section) == SHT_NOTE)
6691 goto skip_ifunc;
6692
6693 /* Dynamic relocs are not propagated for SEC_DEBUGGING
6694 sections because such sections are not SEC_ALLOC and
6695 thus ld.so will not process them. */
6696 if ((input_section->flags & SEC_DEBUGGING) != 0)
6697 return bfd_reloc_ok;
6698
6699 if (h->root.root.string)
6700 name = h->root.root.string;
6701 else
6702 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, NULL);
6703 _bfd_error_handler
6704 /* xgettext:c-format */
6705 (_("%pB(%pA+%#" PRIx64 "): "
6706 "unresolvable %s relocation against symbol `%s'"),
6707 input_bfd, input_section, (uint64_t) rel->r_offset,
6708 howto->name, name);
6709 bfd_set_error (bfd_error_bad_value);
6710 return bfd_reloc_notsupported;
6711 }
6712 else if (h->plt.offset == (bfd_vma) -1)
6713 goto bad_ifunc_reloc;
6714
6715 /* STT_GNU_IFUNC symbol must go through PLT. */
6716 plt = globals->root.splt ? globals->root.splt : globals->root.iplt;
6717 value = (plt->output_section->vma + plt->output_offset + h->plt.offset);
6718
6719 switch (bfd_r_type)
6720 {
6721 default:
6722 bad_ifunc_reloc:
6723 if (h->root.root.string)
6724 name = h->root.root.string;
6725 else
6726 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
6727 NULL);
6728 _bfd_error_handler
6729 /* xgettext:c-format */
6730 (_("%pB: relocation %s against STT_GNU_IFUNC "
6731 "symbol `%s' isn't handled by %s"), input_bfd,
6732 howto->name, name, __FUNCTION__);
6733 bfd_set_error (bfd_error_bad_value);
6734 return bfd_reloc_notsupported;
6735
6736 case BFD_RELOC_AARCH64_NN:
6737 if (rel->r_addend != 0)
6738 {
6739 if (h->root.root.string)
6740 name = h->root.root.string;
6741 else
6742 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
6743 sym, NULL);
6744 _bfd_error_handler
6745 /* xgettext:c-format */
6746 (_("%pB: relocation %s against STT_GNU_IFUNC "
6747 "symbol `%s' has non-zero addend: %" PRId64),
6748 input_bfd, howto->name, name, (int64_t) rel->r_addend);
6749 bfd_set_error (bfd_error_bad_value);
6750 return bfd_reloc_notsupported;
6751 }
6752
6753 /* Generate dynamic relocation only when there is a
6754 non-GOT reference in a shared object. */
6755 if (bfd_link_pic (info) && h->non_got_ref)
6756 {
6757 Elf_Internal_Rela outrel;
6758 asection *sreloc;
6759
6760 /* Need a dynamic relocation to get the real function
6761 address. */
6762 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
6763 info,
6764 input_section,
6765 rel->r_offset);
6766 if (outrel.r_offset == (bfd_vma) -1
6767 || outrel.r_offset == (bfd_vma) -2)
6768 abort ();
6769
6770 outrel.r_offset += (input_section->output_section->vma
6771 + input_section->output_offset);
6772
6773 if (h->dynindx == -1
6774 || h->forced_local
6775 || bfd_link_executable (info))
6776 {
6777 /* This symbol is resolved locally. */
6778 outrel.r_info = (elf_aarch64_hash_entry (h)->got_type
6779 == GOT_CAP
6780 ? ELFNN_R_INFO (0, MORELLO_R (IRELATIVE))
6781 : ELFNN_R_INFO (0, AARCH64_R (IRELATIVE)));
6782 outrel.r_addend = (h->root.u.def.value
6783 + h->root.u.def.section->output_section->vma
6784 + h->root.u.def.section->output_offset);
6785 }
6786 else
6787 {
6788 outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
6789 outrel.r_addend = 0;
6790 }
6791
6792 sreloc = globals->root.irelifunc;
6793 elf_append_rela (output_bfd, sreloc, &outrel);
6794
6795 /* If this reloc is against an external symbol, we
6796 do not want to fiddle with the addend. Otherwise,
6797 we need to include the symbol value so that it
6798 becomes an addend for the dynamic reloc. For an
6799 internal symbol, we have updated addend. */
6800 return bfd_reloc_ok;
6801 }
6802 /* FALLTHROUGH */
6803 case BFD_RELOC_MORELLO_CALL26:
6804 case BFD_RELOC_MORELLO_JUMP26:
6805 case BFD_RELOC_AARCH64_CALL26:
6806 case BFD_RELOC_AARCH64_JUMP26:
6807 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
6808 place, value,
6809 signed_addend,
6810 weak_undef_p);
6811 return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type,
6812 howto, value);
6813 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
6814 case BFD_RELOC_MORELLO_ADR_GOT_PAGE:
6815 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
6816 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
6817 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
6818 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
6819 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
6820 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
6821 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
6822 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
6823 case BFD_RELOC_MORELLO_LD128_GOT_LO12_NC:
6824 base_got = globals->root.sgot;
6825 off = h->got.offset;
6826
6827 if (base_got == NULL)
6828 abort ();
6829
6830 if (off == (bfd_vma) -1)
6831 {
6832 bfd_vma plt_index;
6833
6834 /* We can't use h->got.offset here to save state, or
6835 even just remember the offset, as finish_dynamic_symbol
6836 would use that as offset into .got. */
6837
6838 if (globals->root.splt != NULL)
6839 {
6840 plt_index = ((h->plt.offset - globals->plt_header_size) /
6841 globals->plt_entry_size);
6842 off = (plt_index + 3) * GOT_ENTRY_SIZE (globals);
6843 base_got = globals->root.sgotplt;
6844 }
6845 else
6846 {
6847 plt_index = h->plt.offset / globals->plt_entry_size;
6848 off = plt_index * GOT_ENTRY_SIZE (globals);
6849 base_got = globals->root.igotplt;
6850 }
6851
6852 if (h->dynindx == -1
6853 || h->forced_local
6854 || info->symbolic)
6855 {
6856 /* This references the local definition. We must
6857 initialize this entry in the global offset table.
6858 Since the offset must always be a multiple of 8,
6859 we use the least significant bit to record
6860 whether we have initialized it already.
6861
6862 When doing a dynamic link, we create a .rela.got
6863 relocation entry to initialize the value. This
6864 is done in the finish_dynamic_symbol routine. */
6865 if ((off & 1) != 0)
6866 off &= ~1;
6867 else
6868 {
6869 bfd_put_NN (output_bfd, value,
6870 base_got->contents + off);
6871 /* Note that this is harmless as -1 | 1 still is -1. */
6872 h->got.offset |= 1;
6873 }
6874 }
6875 value = (base_got->output_section->vma
6876 + base_got->output_offset + off);
6877 }
6878 else
6879 value = aarch64_calculate_got_entry_vma (h, globals, info,
6880 value, output_bfd,
6881 unresolved_reloc_p);
6882
6883 if (aarch64_relocation_aginst_gp_p (bfd_r_type))
6884 addend = (globals->root.sgot->output_section->vma
6885 + globals->root.sgot->output_offset);
6886
6887 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
6888 place, value,
6889 addend, weak_undef_p);
6890 return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type, howto, value);
6891 case BFD_RELOC_AARCH64_ADD_LO12:
6892 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
6893 case BFD_RELOC_MORELLO_ADR_HI20_PCREL:
6894 break;
6895 }
6896 }
6897
6898 skip_ifunc:
6899 resolved_to_zero = (h != NULL
6900 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6901
6902 switch (bfd_r_type)
6903 {
6904 case BFD_RELOC_AARCH64_NONE:
6905 case BFD_RELOC_AARCH64_TLSDESC_ADD:
6906 case BFD_RELOC_AARCH64_TLSDESC_CALL:
6907 case BFD_RELOC_AARCH64_TLSDESC_LDR:
6908 case BFD_RELOC_MORELLO_TLSDESC_CALL:
6909 *unresolved_reloc_p = FALSE;
6910 return bfd_reloc_ok;
6911
6912 case BFD_RELOC_AARCH64_NN:
6913 /* If we are relocating against a C64 symbol, then the value can't
6914 already have the LSB set (since STT_FUNC symbols are code labels and
6915 they will be aligned). Hence it's safe just to or-equal in order
6916 to ensure the LSB is set in that case. */
6917 value |= to_c64 ? 1 : 0;
6918
6919 /* When generating a shared object or relocatable executable, these
6920 relocations are copied into the output file to be resolved at
6921 run time. */
6922 if (((bfd_link_pic (info)
6923 || globals->root.is_relocatable_executable)
6924 && (input_section->flags & SEC_ALLOC)
6925 && (h == NULL
6926 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6927 && !resolved_to_zero)
6928 || h->root.type != bfd_link_hash_undefweak))
6929 /* Or we are creating an executable, we may need to keep relocations
6930 for symbols satisfied by a dynamic library if we manage to avoid
6931 copy relocs for the symbol. */
6932 || (ELIMINATE_COPY_RELOCS
6933 && !bfd_link_pic (info)
6934 && h != NULL
6935 && (input_section->flags & SEC_ALLOC)
6936 && h->dynindx != -1
6937 && !h->non_got_ref
6938 && ((h->def_dynamic
6939 && !h->def_regular)
6940 || h->root.type == bfd_link_hash_undefweak
6941 || h->root.type == bfd_link_hash_undefined)))
6942 {
6943 Elf_Internal_Rela outrel;
6944 bfd_byte *loc;
6945 bfd_boolean skip, relocate;
6946 asection *sreloc;
6947
6948 *unresolved_reloc_p = FALSE;
6949
6950 skip = FALSE;
6951 relocate = FALSE;
6952
6953 outrel.r_addend = signed_addend;
6954 outrel.r_offset =
6955 _bfd_elf_section_offset (output_bfd, info, input_section,
6956 rel->r_offset);
6957 if (outrel.r_offset == (bfd_vma) - 1)
6958 skip = TRUE;
6959 else if (outrel.r_offset == (bfd_vma) - 2)
6960 {
6961 skip = TRUE;
6962 relocate = TRUE;
6963 }
6964 else if (abs_symbol_p)
6965 {
6966 /* Local absolute symbol. */
6967 skip = (h->forced_local || (h->dynindx == -1));
6968 relocate = skip;
6969 }
6970
6971 outrel.r_offset += (input_section->output_section->vma
6972 + input_section->output_offset);
6973
6974 if (skip)
6975 memset (&outrel, 0, sizeof outrel);
6976 else if (h != NULL
6977 && h->dynindx != -1
6978 && (!bfd_link_pic (info)
6979 || !(bfd_link_pie (info) || SYMBOLIC_BIND (info, h))
6980 || !h->def_regular))
6981 outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
6982 else
6983 {
6984 int symbol;
6985
6986 /* On SVR4-ish systems, the dynamic loader cannot
6987 relocate the text and data segments independently,
6988 so the symbol does not matter. */
6989 symbol = 0;
6990 relocate = globals->no_apply_dynamic_relocs ? FALSE : TRUE;
6991 outrel.r_info = ELFNN_R_INFO (symbol, AARCH64_R (RELATIVE));
6992 outrel.r_addend += value;
6993 }
6994
6995 sreloc = elf_section_data (input_section)->sreloc;
6996 if (sreloc == NULL || sreloc->contents == NULL)
6997 return bfd_reloc_notsupported;
6998
6999 loc = sreloc->contents + sreloc->reloc_count++ * RELOC_SIZE (globals);
7000 bfd_elfNN_swap_reloca_out (output_bfd, &outrel, loc);
7001
7002 if (sreloc->reloc_count * RELOC_SIZE (globals) > sreloc->size)
7003 {
7004 /* Sanity to check that we have previously allocated
7005 sufficient space in the relocation section for the
7006 number of relocations we actually want to emit. */
7007 abort ();
7008 }
7009
7010 /* If this reloc is against an external symbol, we do not want to
7011 fiddle with the addend. Otherwise, we need to include the symbol
7012 value so that it becomes an addend for the dynamic reloc. */
7013 if (!relocate)
7014 return bfd_reloc_ok;
7015
7016 return _bfd_final_link_relocate (howto, input_bfd, input_section,
7017 contents, rel->r_offset, value,
7018 signed_addend);
7019 }
7020 else
7021 value += signed_addend;
7022 break;
7023
7024 case BFD_RELOC_MORELLO_CALL26:
7025 case BFD_RELOC_MORELLO_JUMP26:
7026 case BFD_RELOC_AARCH64_CALL26:
7027 case BFD_RELOC_AARCH64_JUMP26:
7028 {
7029 asection *splt = globals->root.splt;
7030 bfd_boolean via_plt_p =
7031 splt != NULL && h != NULL && h->plt.offset != (bfd_vma) - 1;
7032
7033 /* A call to an undefined weak symbol is converted to a jump to
7034 the next instruction unless a PLT entry will be created.
7035 The jump to the next instruction is optimized as a NOP.
7036 Do the same for local undefined symbols. */
7037 if (weak_undef_p && ! via_plt_p)
7038 {
7039 bfd_putl32 (INSN_NOP, hit_data);
7040 return bfd_reloc_ok;
7041 }
7042
7043 /* If the call goes through a PLT entry, make sure to
7044 check distance to the right destination address. */
7045 if (via_plt_p)
7046 value = (splt->output_section->vma
7047 + splt->output_offset + h->plt.offset);
7048
7049 /* Check if a stub has to be inserted because the destination
7050 is too far away. */
7051 struct elf_aarch64_stub_hash_entry *stub_entry = NULL;
7052
7053 enum elf_aarch64_stub_type c64_stub = aarch64_stub_none;
7054
7055 /* Figure out if we need an interworking stub and if yes, what
7056 kind. */
7057 if (!via_plt_p)
7058 c64_stub = aarch64_interwork_stub (r_type, to_c64);
7059
7060 /* If the branch destination is directed to plt stub, "value" will be
7061 the final destination, otherwise we should plus signed_addend, it may
7062 contain non-zero value, for example call to local function symbol
7063 which are turned into "sec_sym + sec_off", and sec_off is kept in
7064 signed_addend. */
7065 if (c64_stub != aarch64_stub_none
7066 || (aarch64_branch_reloc_p (r_type)
7067 && !aarch64_valid_branch_p ((via_plt_p ? value
7068 : value + signed_addend), place)))
7069 {
7070 /* The target is out of reach, so redirect the branch to
7071 the local stub for this function. */
7072 stub_entry = elfNN_aarch64_get_stub_entry (input_section, sym_sec,
7073 h, rel, globals,
7074 c64_stub);
7075 }
7076
7077 if (stub_entry != NULL)
7078 {
7079 value = (stub_entry->stub_offset
7080 + stub_entry->stub_sec->output_offset
7081 + stub_entry->stub_sec->output_section->vma);
7082
7083 /* We have redirected the destination to stub entry address,
7084 so ignore any addend record in the original rela entry. */
7085 signed_addend = 0;
7086 }
7087 }
7088 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
7089 place, value,
7090 signed_addend, weak_undef_p);
7091 *unresolved_reloc_p = FALSE;
7092 break;
7093
7094 case BFD_RELOC_AARCH64_16_PCREL:
7095 case BFD_RELOC_AARCH64_32_PCREL:
7096 case BFD_RELOC_AARCH64_64_PCREL:
7097 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
7098 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
7099 case BFD_RELOC_MORELLO_ADR_HI20_NC_PCREL:
7100 case BFD_RELOC_MORELLO_ADR_HI20_PCREL:
7101 case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
7102 case BFD_RELOC_AARCH64_LD_LO19_PCREL:
7103 case BFD_RELOC_MORELLO_LD_LO17_PCREL:
7104 case BFD_RELOC_AARCH64_MOVW_PREL_G0:
7105 case BFD_RELOC_AARCH64_MOVW_PREL_G0_NC:
7106 case BFD_RELOC_AARCH64_MOVW_PREL_G1:
7107 case BFD_RELOC_AARCH64_MOVW_PREL_G1_NC:
7108 case BFD_RELOC_AARCH64_MOVW_PREL_G2:
7109 case BFD_RELOC_AARCH64_MOVW_PREL_G2_NC:
7110 case BFD_RELOC_AARCH64_MOVW_PREL_G3:
7111 if (bfd_link_pic (info)
7112 && (input_section->flags & SEC_ALLOC) != 0
7113 && (input_section->flags & SEC_READONLY) != 0
7114 && !SYMBOL_REFERENCES_LOCAL (info, h))
7115 {
7116 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
7117
7118 _bfd_error_handler
7119 /* xgettext:c-format */
7120 (_("%pB: relocation %s against symbol `%s' which may bind "
7121 "externally can not be used when making a shared object; "
7122 "recompile with -fPIC"),
7123 input_bfd, elfNN_aarch64_howto_table[howto_index].name,
7124 h->root.root.string);
7125 bfd_set_error (bfd_error_bad_value);
7126 return bfd_reloc_notsupported;
7127 }
7128 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
7129 place, value,
7130 signed_addend,
7131 weak_undef_p);
7132
7133 if (bfd_r_type == BFD_RELOC_AARCH64_ADR_LO21_PCREL && to_c64)
7134 value |= 1;
7135 break;
7136
7137 case BFD_RELOC_MORELLO_BRANCH19:
7138 case BFD_RELOC_MORELLO_TSTBR14:
7139 c64_rtype = TRUE;
7140 /* Fall through. */
7141 case BFD_RELOC_AARCH64_BRANCH19:
7142 case BFD_RELOC_AARCH64_TSTBR14:
7143 if (h && h->root.type == bfd_link_hash_undefined)
7144 {
7145 _bfd_error_handler
7146 /* xgettext:c-format */
7147 (_("%pB: conditional branch to undefined symbol `%s' "
7148 "not allowed"), input_bfd, h->root.root.string);
7149 bfd_set_error (bfd_error_bad_value);
7150 return bfd_reloc_notsupported;
7151 }
7152 {
7153 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
7154
7155 if ((c64_rtype && !to_c64) || (!c64_rtype && to_c64))
7156 {
7157 _bfd_error_handler
7158 /* xgettext:c-format */
7159 (_("%pB: interworking not supported on relocation %s"),
7160 input_bfd, elfNN_aarch64_howto_table[howto_index].name);
7161 return bfd_reloc_notsupported;
7162 }
7163 }
7164 /* Fall through. */
7165
7166 case BFD_RELOC_AARCH64_16:
7167 #if ARCH_SIZE == 64
7168 case BFD_RELOC_AARCH64_32:
7169 #endif
7170 case BFD_RELOC_AARCH64_ADD_LO12:
7171 case BFD_RELOC_AARCH64_LDST128_LO12:
7172 case BFD_RELOC_AARCH64_LDST16_LO12:
7173 case BFD_RELOC_AARCH64_LDST32_LO12:
7174 case BFD_RELOC_AARCH64_LDST64_LO12:
7175 case BFD_RELOC_AARCH64_LDST8_LO12:
7176 case BFD_RELOC_AARCH64_MOVW_G0:
7177 case BFD_RELOC_AARCH64_MOVW_G0_NC:
7178 case BFD_RELOC_AARCH64_MOVW_G0_S:
7179 case BFD_RELOC_AARCH64_MOVW_G1:
7180 case BFD_RELOC_AARCH64_MOVW_G1_NC:
7181 case BFD_RELOC_AARCH64_MOVW_G1_S:
7182 case BFD_RELOC_AARCH64_MOVW_G2:
7183 case BFD_RELOC_AARCH64_MOVW_G2_NC:
7184 case BFD_RELOC_AARCH64_MOVW_G2_S:
7185 case BFD_RELOC_AARCH64_MOVW_G3:
7186 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
7187 place, value,
7188 signed_addend, weak_undef_p);
7189 if ((bfd_r_type == BFD_RELOC_AARCH64_ADD_LO12
7190 || bfd_r_type == BFD_RELOC_AARCH64_MOVW_G0
7191 || bfd_r_type == BFD_RELOC_AARCH64_MOVW_G0_S
7192 || bfd_r_type == BFD_RELOC_AARCH64_MOVW_G0_NC
7193 || bfd_r_type == BFD_RELOC_AARCH64_32
7194 || bfd_r_type == BFD_RELOC_AARCH64_16)
7195 && to_c64)
7196 value |= 1;
7197
7198 break;
7199
7200 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
7201 case BFD_RELOC_MORELLO_ADR_GOT_PAGE:
7202 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
7203 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
7204 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
7205 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
7206 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
7207 case BFD_RELOC_MORELLO_LD128_GOT_LO12_NC:
7208 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
7209 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
7210 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
7211 off = symbol_got_offset (input_bfd, h, r_symndx);
7212 base_got = globals->root.sgot;
7213
7214 bfd_boolean c64_reloc =
7215 (bfd_r_type == BFD_RELOC_MORELLO_LD128_GOT_LO12_NC
7216 || bfd_r_type == BFD_RELOC_MORELLO_ADR_GOT_PAGE);
7217
7218 if (signed_addend != 0)
7219 {
7220 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
7221 _bfd_error_handler
7222 /* xgettext:c-format */
7223 (_("%pB: symbol plus addend can not be placed into the GOT "
7224 "for relocation %s"),
7225 input_bfd, elfNN_aarch64_howto_table[howto_index].name);
7226 abort ();
7227 }
7228
7229 if (base_got == NULL)
7230 BFD_ASSERT (h != NULL);
7231
7232 relative_reloc = FALSE;
7233 c64_needs_frag_fixup = FALSE;
7234 if (h != NULL)
7235 {
7236 bfd_vma addend = 0;
7237 bfd_vma frag_value;
7238 bfd_boolean is_dynamic
7239 = elf_hash_table (info)->dynamic_sections_created;
7240
7241 /* If a symbol is not dynamic and is not undefined weak, bind it
7242 locally and generate a RELATIVE relocation under PIC mode.
7243
7244 NOTE: one symbol may be referenced by several relocations, we
7245 should only generate one RELATIVE relocation for that symbol.
7246 Therefore, check GOT offset mark first.
7247
7248 NOTE2: Symbol references via GOT in C64 static binaries without
7249 PIC should always have relative relocations, so we do that here
7250 early. */
7251 if (((h->dynindx == -1
7252 && !h->forced_local
7253 && h->root.type != bfd_link_hash_undefweak
7254 && bfd_link_pic (info))
7255 || (!bfd_link_pic (info) && bfd_link_executable (info)
7256 && c64_reloc))
7257 && !symbol_got_offset_mark_p (input_bfd, h, r_symndx))
7258 {
7259 relative_reloc = TRUE;
7260 c64_needs_frag_fixup = c64_reloc ? TRUE : FALSE;
7261 }
7262 /* If this is a dynamic symbol that binds locally then the generic
7263 code and elfNN_aarch64_finish_dynamic_symbol will already handle
7264 creating the RELATIVE reloc pointing into the GOT for this symbol.
7265 That means that this function does not need to handle *creating*
7266 such a relocation. This function does already handle setting the
7267 base value as the fragment for that relocation, hence we should
7268 ensure that we set the fragment correctly for C64 code (i.e.
7269 including the required permissions and bounds). */
7270 else if (c64_reloc
7271 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (is_dynamic,
7272 bfd_link_pic (info), h)
7273 && bfd_link_pic (info)
7274 && SYMBOL_REFERENCES_LOCAL (info, h))
7275 {
7276 /* We believe that if `h` were undefined weak it would not have
7277 SYMBOL_REFERENCES_LOCAL return true. However this is not 100%
7278 clear based purely on the members that we check in the code.
7279 The reason it matters is if we could have a
7280 SYMBOL_REFERENCES_LOCAL symbol which is also
7281 !UNDEFWEAK_NO_DYNAMIC_RELOC then the check above would
7282 determine that we need to fix up the fragment for the RELATIVE
7283 relocation that elfNN_aarch64_finish_dynamic_symbol will
7284 create, but in actual fact elfNN_aarch64_finish_dynamic_symbol
7285 would not create that relocation. */
7286 BFD_ASSERT (!UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7287 c64_needs_frag_fixup = TRUE;
7288 }
7289
7290 if (c64_reloc
7291 && c64_symbol_adjust (h, value, sym_sec, info, &frag_value))
7292 signed_addend = (value | h->target_internal) - frag_value;
7293 else
7294 frag_value = value | h->target_internal;
7295
7296 value = aarch64_calculate_got_entry_vma (h, globals, info,
7297 frag_value,
7298 output_bfd,
7299 unresolved_reloc_p);
7300 /* Record the GOT entry address which will be used when generating
7301 RELATIVE relocation. */
7302 if (relative_reloc)
7303 got_entry_addr = value;
7304
7305 if (aarch64_relocation_aginst_gp_p (bfd_r_type))
7306 addend = (globals->root.sgot->output_section->vma
7307 + globals->root.sgot->output_offset);
7308 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
7309 place, value,
7310 addend, weak_undef_p);
7311 }
7312 else
7313 {
7314 bfd_vma addend = 0;
7315 struct elf_aarch64_local_symbol *locals
7316 = elf_aarch64_locals (input_bfd);
7317
7318 if (locals == NULL)
7319 {
7320 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
7321 _bfd_error_handler
7322 /* xgettext:c-format */
7323 (_("%pB: local symbol descriptor table be NULL when applying "
7324 "relocation %s against local symbol"),
7325 input_bfd, elfNN_aarch64_howto_table[howto_index].name);
7326 abort ();
7327 }
7328
7329 got_entry_addr = (base_got->output_section->vma
7330 + base_got->output_offset + off);
7331
7332 if (!symbol_got_offset_mark_p (input_bfd, h, r_symndx))
7333 {
7334 bfd_vma frag_value;
7335
7336 if (c64_reloc
7337 && c64_symbol_adjust (h, value, sym_sec, info, &frag_value))
7338 signed_addend = (value | sym->st_target_internal) - frag_value;
7339 else
7340 frag_value = value | sym->st_target_internal;
7341
7342 bfd_put_64 (output_bfd, frag_value, base_got->contents + off);
7343
7344 /* For local symbol, we have done absolute relocation in static
7345 linking stage. While for shared library, we need to update the
7346 content of GOT entry according to the shared object's runtime
7347 base address. So, we need to generate a R_AARCH64_RELATIVE reloc
7348 for dynamic linker.
7349
7350 For any C64 binary we need to ensure there are RELATIVE
7351 relocations to initialise the capabilities. The only case when
7352 we would not want to emit such relocations is when the producing
7353 a relocatable object file (since such files should not have
7354 dynamic relocations). */
7355 if (bfd_link_pic (info)
7356 || (!bfd_link_pic (info) && bfd_link_executable (info)
7357 && c64_reloc))
7358 {
7359 relative_reloc = TRUE;
7360 c64_needs_frag_fixup = c64_reloc ? TRUE : FALSE;
7361 }
7362
7363 symbol_got_offset_mark (input_bfd, h, r_symndx);
7364 }
7365
7366 /* Update the relocation value to GOT entry addr as we have transformed
7367 the direct data access into indirect data access through GOT. */
7368 value = got_entry_addr;
7369
7370 if (aarch64_relocation_aginst_gp_p (bfd_r_type))
7371 addend = base_got->output_section->vma + base_got->output_offset;
7372
7373 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
7374 place, value,
7375 addend, weak_undef_p);
7376 }
7377
7378 if (c64_needs_frag_fixup)
7379 {
7380 BFD_ASSERT (c64_reloc);
7381 /* For a C64 relative relocation, also add size and permissions into
7382 the frag. */
7383 bfd_reloc_status_type ret;
7384
7385 ret = c64_fixup_frag (input_bfd, info, bfd_r_type, sym, h,
7386 sym_sec, globals->root.srelgot,
7387 base_got->contents + off + 8,
7388 orig_value, 0, off);
7389
7390 if (ret != bfd_reloc_continue)
7391 return ret;
7392 }
7393
7394 if (relative_reloc)
7395 {
7396 asection *s;
7397 Elf_Internal_Rela outrel;
7398
7399 enum elf_aarch64_reloc_type rtype = AARCH64_R (RELATIVE);
7400
7401 s = globals->root.srelgot;
7402
7403 if (c64_reloc)
7404 {
7405 rtype = MORELLO_R (RELATIVE);
7406
7407 if (bfd_link_executable (info) && !bfd_link_pic (info))
7408 s = globals->srelcaps;
7409
7410 outrel.r_addend = signed_addend;
7411 }
7412 else
7413 outrel.r_addend = orig_value;
7414
7415 if (s == NULL)
7416 abort ();
7417
7418 outrel.r_offset = got_entry_addr;
7419 outrel.r_info = ELFNN_R_INFO (0, rtype);
7420 elf_append_rela (output_bfd, s, &outrel);
7421 }
7422 break;
7423
7424 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
7425 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
7426 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
7427 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
7428 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
7429 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
7430 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
7431 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
7432 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
7433 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
7434 if (globals->root.sgot == NULL)
7435 return bfd_reloc_notsupported;
7436
7437 value = (symbol_got_offset (input_bfd, h, r_symndx)
7438 + globals->root.sgot->output_section->vma
7439 + globals->root.sgot->output_offset);
7440
7441 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
7442 place, value,
7443 0, weak_undef_p);
7444 *unresolved_reloc_p = FALSE;
7445 break;
7446
7447 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
7448 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
7449 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
7450 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
7451 if (globals->root.sgot == NULL)
7452 return bfd_reloc_notsupported;
7453
7454 value = symbol_got_offset (input_bfd, h, r_symndx);
7455 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
7456 place, value,
7457 0, weak_undef_p);
7458 *unresolved_reloc_p = FALSE;
7459 break;
7460
7461 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12:
7462 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12:
7463 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
7464 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12:
7465 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC:
7466 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12:
7467 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC:
7468 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12:
7469 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC:
7470 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12:
7471 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC:
7472 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0:
7473 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
7474 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1:
7475 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC:
7476 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2:
7477 {
7478 if (!(weak_undef_p || elf_hash_table (info)->tls_sec))
7479 {
7480 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
7481 _bfd_error_handler
7482 /* xgettext:c-format */
7483 (_("%pB: TLS relocation %s against undefined symbol `%s'"),
7484 input_bfd, elfNN_aarch64_howto_table[howto_index].name,
7485 h->root.root.string);
7486 bfd_set_error (bfd_error_bad_value);
7487 return bfd_reloc_notsupported;
7488 }
7489
7490 bfd_vma def_value
7491 = weak_undef_p ? 0 : signed_addend - dtpoff_base (info);
7492 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
7493 place, value,
7494 def_value, weak_undef_p);
7495 break;
7496 }
7497
7498 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
7499 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
7500 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
7501 case BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12:
7502 case BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
7503 case BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12:
7504 case BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
7505 case BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12:
7506 case BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
7507 case BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12:
7508 case BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
7509 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
7510 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
7511 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
7512 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
7513 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
7514 {
7515 if (!(weak_undef_p || elf_hash_table (info)->tls_sec))
7516 {
7517 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
7518 _bfd_error_handler
7519 /* xgettext:c-format */
7520 (_("%pB: TLS relocation %s against undefined symbol `%s'"),
7521 input_bfd, elfNN_aarch64_howto_table[howto_index].name,
7522 h->root.root.string);
7523 bfd_set_error (bfd_error_bad_value);
7524 return bfd_reloc_notsupported;
7525 }
7526
7527 bfd_vma def_value
7528 = weak_undef_p ? 0 : signed_addend - tpoff_base (info);
7529 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
7530 place, value,
7531 def_value, weak_undef_p);
7532 *unresolved_reloc_p = FALSE;
7533 break;
7534 }
7535
7536 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
7537 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
7538 case BFD_RELOC_MORELLO_TLSDESC_ADR_PAGE20:
7539 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
7540 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
7541 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12:
7542 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
7543 case BFD_RELOC_MORELLO_TLSDESC_LD128_LO12:
7544 if (globals->root.sgot == NULL)
7545 return bfd_reloc_notsupported;
7546 value = (symbol_tlsdesc_got_offset (input_bfd, h, r_symndx)
7547 + globals->root.sgotplt->output_section->vma
7548 + globals->root.sgotplt->output_offset
7549 + globals->sgotplt_jump_table_size);
7550
7551 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
7552 place, value,
7553 0, weak_undef_p);
7554 *unresolved_reloc_p = FALSE;
7555 break;
7556
7557 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
7558 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
7559 if (globals->root.sgot == NULL)
7560 return bfd_reloc_notsupported;
7561
7562 value = (symbol_tlsdesc_got_offset (input_bfd, h, r_symndx)
7563 + globals->root.sgotplt->output_section->vma
7564 + globals->root.sgotplt->output_offset
7565 + globals->sgotplt_jump_table_size);
7566
7567 value -= (globals->root.sgot->output_section->vma
7568 + globals->root.sgot->output_offset);
7569
7570 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
7571 place, value,
7572 0, weak_undef_p);
7573 *unresolved_reloc_p = FALSE;
7574 break;
7575
7576 case BFD_RELOC_MORELLO_CAPINIT:
7577 {
7578 Elf_Internal_Rela outrel;
7579
7580 if (input_section->flags & SEC_READONLY)
7581 {
7582 _bfd_error_handler
7583 /* xgettext:c-format */
7584 (_("%pB: capability relocation section must be writable"),
7585 input_bfd);
7586 bfd_set_error (bfd_error_bad_value);
7587 return bfd_reloc_notsupported;
7588 }
7589
7590 outrel.r_offset = _bfd_elf_section_offset (output_bfd, info,
7591 input_section,
7592 rel->r_offset);
7593
7594 outrel.r_offset += (input_section->output_section->vma
7595 + input_section->output_offset);
7596
7597 /* Capability-aligned. */
7598 if (outrel.r_offset & 0xf)
7599 return bfd_reloc_overflow;
7600
7601 bfd_reloc_status_type ret;
7602
7603 ret = c64_fixup_frag (input_bfd, info, bfd_r_type, sym, h, sym_sec,
7604 input_section, hit_data + 8, value,
7605 signed_addend, rel->r_offset);
7606
7607 if (ret != bfd_reloc_continue)
7608 return ret;
7609
7610 outrel.r_addend = signed_addend;
7611 value |= (h != NULL ? h->target_internal : sym->st_target_internal);
7612
7613 /* Emit a dynamic relocation if we are building PIC. */
7614 if (h != NULL
7615 && h->dynindx != -1
7616 && bfd_link_pic (info)
7617 && !SYMBOL_REFERENCES_LOCAL (info, h))
7618 outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
7619 else
7620 outrel.r_info = ELFNN_R_INFO (0, MORELLO_R (RELATIVE));
7621
7622 /* Symbols without size information get bounds to the
7623 whole section: adjust the base of the capability to the
7624 start of the section and set the addend to obtain the
7625 correct address for the symbol. */
7626 bfd_vma new_value;
7627 if (c64_symbol_adjust (h, value, sym_sec, info, &new_value))
7628 {
7629 outrel.r_addend += (value - new_value);
7630 value = new_value;
7631 }
7632
7633 asection *s = globals->srelcaps;
7634
7635 elf_append_rela (output_bfd, s, &outrel);
7636 *unresolved_reloc_p = FALSE;
7637 }
7638 break;
7639
7640 default:
7641 return bfd_reloc_notsupported;
7642 }
7643
7644 if (saved_addend)
7645 *saved_addend = value;
7646
7647 /* Only apply the final relocation in a sequence. */
7648 if (save_addend)
7649 return bfd_reloc_continue;
7650
7651 return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type,
7652 howto, value);
7653 }
7654
7655 /* LP64 and ILP32 operates on x- and w-registers respectively.
7656 Next definitions take into account the difference between
7657 corresponding machine codes. R means x-register if the target
7658 arch is LP64, and w-register if the target is ILP32. */
7659
7660 #if ARCH_SIZE == 64
7661 # define add_R0_R0 (0x91000000)
7662 # define add_R0_R0_R1 (0x8b000020)
7663 # define add_R0_R1 (0x91400020)
7664 # define ldr_R0 (0x58000000)
7665 # define ldr_R0_mask(i) (i & 0xffffffe0)
7666 # define ldr_R0_x0 (0xf9400000)
7667 # define ldr_hw_R0 (0xf2a00000)
7668 # define movk_R0 (0xf2800000)
7669 # define movz_R0 (0xd2a00000)
7670 # define movz_hw_R0 (0xd2c00000)
7671 #else /*ARCH_SIZE == 32 */
7672 # define add_R0_R0 (0x11000000)
7673 # define add_R0_R0_R1 (0x0b000020)
7674 # define add_R0_R1 (0x11400020)
7675 # define ldr_R0 (0x18000000)
7676 # define ldr_R0_mask(i) (i & 0xbfffffe0)
7677 # define ldr_R0_x0 (0xb9400000)
7678 # define ldr_hw_R0 (0x72a00000)
7679 # define movk_R0 (0x72800000)
7680 # define movz_R0 (0x52a00000)
7681 # define movz_hw_R0 (0x52c00000)
7682 #endif
7683
7684 /* Structure to hold payload for _bfd_aarch64_erratum_843419_clear_stub,
7685 it is used to identify the stub information to reset. */
7686
7687 struct erratum_843419_branch_to_stub_clear_data
7688 {
7689 bfd_vma adrp_offset;
7690 asection *output_section;
7691 };
7692
7693 /* Clear the erratum information for GEN_ENTRY if the ADRP_OFFSET and
7694 section inside IN_ARG matches. The clearing is done by setting the
7695 stub_type to none. */
7696
7697 static bfd_boolean
7698 _bfd_aarch64_erratum_843419_clear_stub (struct bfd_hash_entry *gen_entry,
7699 void *in_arg)
7700 {
7701 struct elf_aarch64_stub_hash_entry *stub_entry
7702 = (struct elf_aarch64_stub_hash_entry *) gen_entry;
7703 struct erratum_843419_branch_to_stub_clear_data *data
7704 = (struct erratum_843419_branch_to_stub_clear_data *) in_arg;
7705
7706 if (stub_entry->target_section != data->output_section
7707 || stub_entry->stub_type != aarch64_stub_erratum_843419_veneer
7708 || stub_entry->adrp_offset != data->adrp_offset)
7709 return TRUE;
7710
7711 /* Change the stub type instead of removing the entry, removing from the hash
7712 table would be slower and we have already reserved the memory for the entry
7713 so there wouldn't be much gain. Changing the stub also keeps around a
7714 record of what was there before. */
7715 stub_entry->stub_type = aarch64_stub_none;
7716
7717 /* We're done and there could have been only one matching stub at that
7718 particular offset, so abort further traversal. */
7719 return FALSE;
7720 }
7721
7722 /* TLS Relaxations may relax an adrp sequence that matches the erratum 843419
7723 sequence. In this case the erratum no longer applies and we need to remove
7724 the entry from the pending stub generation. This clears matching adrp insn
7725 at ADRP_OFFSET in INPUT_SECTION in the stub table defined in GLOBALS. */
7726
7727 static void
7728 clear_erratum_843419_entry (struct elf_aarch64_link_hash_table *globals,
7729 bfd_vma adrp_offset, asection *input_section)
7730 {
7731 if (globals->fix_erratum_843419 & ERRAT_ADRP)
7732 {
7733 struct erratum_843419_branch_to_stub_clear_data data;
7734 data.adrp_offset = adrp_offset;
7735 data.output_section = input_section;
7736
7737 bfd_hash_traverse (&globals->stub_hash_table,
7738 _bfd_aarch64_erratum_843419_clear_stub, &data);
7739 }
7740 }
7741
7742 #define BUILD_MOVZ(_reg, _imm) (movz_R0 \
7743 | ((((_imm) >> 16) & 0xffff) << 5) \
7744 | (_reg))
7745 #define BUILD_MOVK(_reg, _imm) (movk_R0 | (((_imm) & 0xffff) << 5) | (_reg))
7746
7747 /* Handle TLS relaxations. Relaxing is possible for symbols that use
7748 R_AARCH64_TLSDESC_ADR_{PAGE, LD64_LO12_NC, ADD_LO12_NC} during a static
7749 link.
7750
7751 Return bfd_reloc_ok if we're done, bfd_reloc_continue if the caller
7752 is to then call final_link_relocate. Return other values in the
7753 case of error. */
7754
7755 static bfd_reloc_status_type
7756 elfNN_aarch64_tls_relax (bfd *input_bfd, struct bfd_link_info *info,
7757 asection *input_section,
7758 bfd_byte *contents, Elf_Internal_Rela *rel,
7759 struct elf_link_hash_entry *h, unsigned long r_symndx)
7760 {
7761 bfd_boolean is_local = h == NULL;
7762
7763 unsigned int r_type = ELFNN_R_TYPE (rel->r_info);
7764 unsigned long insn;
7765 bfd_vma sym_size = 0;
7766 struct elf_aarch64_link_hash_table *globals = elf_aarch64_hash_table (info);
7767
7768 BFD_ASSERT (globals && input_bfd && contents && rel);
7769
7770 if (is_local || !bfd_link_pic (info))
7771 {
7772 if (h != NULL)
7773 sym_size = h->size;
7774 else
7775 {
7776 Elf_Internal_Sym *sym;
7777
7778 sym = bfd_sym_from_r_symndx (&globals->root.sym_cache, input_bfd,
7779 r_symndx);
7780 BFD_ASSERT (sym != NULL);
7781 sym_size = sym->st_size;
7782 }
7783 }
7784
7785 switch (elfNN_aarch64_bfd_reloc_from_type (input_bfd, r_type))
7786 {
7787 case BFD_RELOC_MORELLO_TLSDESC_ADR_PAGE20:
7788 if (is_local || !bfd_link_pic (info))
7789 {
7790 /* GD->LE relaxation:
7791 nop => movz x1, objsize_hi16
7792 adrp x0, :tlsdesc:var => movz x0, :tprel_g1:var */
7793 bfd_putl32 (BUILD_MOVZ(1, sym_size), contents + rel->r_offset - 4);
7794 bfd_putl32 (movz_R0, contents + rel->r_offset);
7795
7796 /* We have relaxed the adrp into a mov, we may have to clear any
7797 pending erratum fixes. */
7798 clear_erratum_843419_entry (globals, rel->r_offset, input_section);
7799 return bfd_reloc_continue;
7800 }
7801 else
7802 {
7803 /* GD->IE relaxation: Not implemented. */
7804 return bfd_reloc_continue;
7805 }
7806 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
7807 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
7808 if (is_local)
7809 {
7810 /* GD->LE relaxation:
7811 adrp x0, :tlsgd:var => movz R0, :tprel_g1:var
7812 or
7813 adrp x0, :tlsdesc:var => movz R0, :tprel_g1:var
7814
7815 Where R is x for LP64, and w for ILP32. */
7816 bfd_putl32 (movz_R0, contents + rel->r_offset);
7817 /* We have relaxed the adrp into a mov, we may have to clear any
7818 pending erratum fixes. */
7819 clear_erratum_843419_entry (globals, rel->r_offset, input_section);
7820 return bfd_reloc_continue;
7821 }
7822 else
7823 {
7824 /* GD->IE relaxation:
7825 adrp x0, :tlsgd:var => adrp x0, :gottprel:var
7826 or
7827 adrp x0, :tlsdesc:var => adrp x0, :gottprel:var
7828 */
7829 return bfd_reloc_continue;
7830 }
7831
7832 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
7833 BFD_ASSERT (0);
7834 break;
7835
7836 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
7837 if (is_local)
7838 {
7839 /* Tiny TLSDESC->LE relaxation:
7840 ldr x1, :tlsdesc:var => movz R0, #:tprel_g1:var
7841 adr x0, :tlsdesc:var => movk R0, #:tprel_g0_nc:var
7842 .tlsdesccall var
7843 blr x1 => nop
7844
7845 Where R is x for LP64, and w for ILP32. */
7846 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (TLSDESC_ADR_PREL21));
7847 BFD_ASSERT (ELFNN_R_TYPE (rel[2].r_info) == AARCH64_R (TLSDESC_CALL));
7848
7849 rel[1].r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
7850 AARCH64_R (TLSLE_MOVW_TPREL_G0_NC));
7851 rel[2].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
7852
7853 bfd_putl32 (movz_R0, contents + rel->r_offset);
7854 bfd_putl32 (movk_R0, contents + rel->r_offset + 4);
7855 bfd_putl32 (INSN_NOP, contents + rel->r_offset + 8);
7856 return bfd_reloc_continue;
7857 }
7858 else
7859 {
7860 /* Tiny TLSDESC->IE relaxation:
7861 ldr x1, :tlsdesc:var => ldr x0, :gottprel:var
7862 adr x0, :tlsdesc:var => nop
7863 .tlsdesccall var
7864 blr x1 => nop
7865 */
7866 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (TLSDESC_ADR_PREL21));
7867 BFD_ASSERT (ELFNN_R_TYPE (rel[2].r_info) == AARCH64_R (TLSDESC_CALL));
7868
7869 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
7870 rel[2].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
7871
7872 bfd_putl32 (ldr_R0, contents + rel->r_offset);
7873 bfd_putl32 (INSN_NOP, contents + rel->r_offset + 4);
7874 bfd_putl32 (INSN_NOP, contents + rel->r_offset + 8);
7875 return bfd_reloc_continue;
7876 }
7877
7878 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
7879 if (is_local)
7880 {
7881 /* Tiny GD->LE relaxation:
7882 adr x0, :tlsgd:var => mrs x1, tpidr_el0
7883 bl __tls_get_addr => add R0, R1, #:tprel_hi12:x, lsl #12
7884 nop => add R0, R0, #:tprel_lo12_nc:x
7885
7886 Where R is x for LP64, and x for Ilp32. */
7887
7888 /* First kill the tls_get_addr reloc on the bl instruction. */
7889 BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
7890
7891 bfd_putl32 (0xd53bd041, contents + rel->r_offset + 0);
7892 bfd_putl32 (add_R0_R1, contents + rel->r_offset + 4);
7893 bfd_putl32 (add_R0_R0, contents + rel->r_offset + 8);
7894
7895 rel[1].r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
7896 AARCH64_R (TLSLE_ADD_TPREL_LO12_NC));
7897 rel[1].r_offset = rel->r_offset + 8;
7898
7899 /* Move the current relocation to the second instruction in
7900 the sequence. */
7901 rel->r_offset += 4;
7902 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
7903 AARCH64_R (TLSLE_ADD_TPREL_HI12));
7904 return bfd_reloc_continue;
7905 }
7906 else
7907 {
7908 /* Tiny GD->IE relaxation:
7909 adr x0, :tlsgd:var => ldr R0, :gottprel:var
7910 bl __tls_get_addr => mrs x1, tpidr_el0
7911 nop => add R0, R0, R1
7912
7913 Where R is x for LP64, and w for Ilp32. */
7914
7915 /* First kill the tls_get_addr reloc on the bl instruction. */
7916 BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
7917 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
7918
7919 bfd_putl32 (ldr_R0, contents + rel->r_offset);
7920 bfd_putl32 (0xd53bd041, contents + rel->r_offset + 4);
7921 bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 8);
7922 return bfd_reloc_continue;
7923 }
7924
7925 #if ARCH_SIZE == 64
7926 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
7927 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (TLSGD_MOVW_G0_NC));
7928 BFD_ASSERT (rel->r_offset + 12 == rel[2].r_offset);
7929 BFD_ASSERT (ELFNN_R_TYPE (rel[2].r_info) == AARCH64_R (CALL26));
7930
7931 if (is_local)
7932 {
7933 /* Large GD->LE relaxation:
7934 movz x0, #:tlsgd_g1:var => movz x0, #:tprel_g2:var, lsl #32
7935 movk x0, #:tlsgd_g0_nc:var => movk x0, #:tprel_g1_nc:var, lsl #16
7936 add x0, gp, x0 => movk x0, #:tprel_g0_nc:var
7937 bl __tls_get_addr => mrs x1, tpidr_el0
7938 nop => add x0, x0, x1
7939 */
7940 rel[2].r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
7941 AARCH64_R (TLSLE_MOVW_TPREL_G0_NC));
7942 rel[2].r_offset = rel->r_offset + 8;
7943
7944 bfd_putl32 (movz_hw_R0, contents + rel->r_offset + 0);
7945 bfd_putl32 (ldr_hw_R0, contents + rel->r_offset + 4);
7946 bfd_putl32 (movk_R0, contents + rel->r_offset + 8);
7947 bfd_putl32 (0xd53bd041, contents + rel->r_offset + 12);
7948 bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 16);
7949 }
7950 else
7951 {
7952 /* Large GD->IE relaxation:
7953 movz x0, #:tlsgd_g1:var => movz x0, #:gottprel_g1:var, lsl #16
7954 movk x0, #:tlsgd_g0_nc:var => movk x0, #:gottprel_g0_nc:var
7955 add x0, gp, x0 => ldr x0, [gp, x0]
7956 bl __tls_get_addr => mrs x1, tpidr_el0
7957 nop => add x0, x0, x1
7958 */
7959 rel[2].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
7960 bfd_putl32 (0xd2a80000, contents + rel->r_offset + 0);
7961 bfd_putl32 (ldr_R0, contents + rel->r_offset + 8);
7962 bfd_putl32 (0xd53bd041, contents + rel->r_offset + 12);
7963 bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 16);
7964 }
7965 return bfd_reloc_continue;
7966
7967 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
7968 return bfd_reloc_continue;
7969 #endif
7970
7971 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
7972 return bfd_reloc_continue;
7973
7974 case BFD_RELOC_MORELLO_TLSDESC_LD128_LO12:
7975 if (is_local || !bfd_link_pic (info))
7976 {
7977 /* GD->LE relaxation:
7978 ldr xd, [x0, #:tlsdesc_lo12:var] => movk x0, :tprel_g0_nc:var */
7979 bfd_putl32 (movk_R0, contents + rel->r_offset);
7980 return bfd_reloc_continue;
7981 }
7982 else
7983 {
7984 /* GD->IE relaxation: not implemented. */
7985 return bfd_reloc_continue;
7986 }
7987 case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
7988 if (is_local)
7989 {
7990 /* GD->LE relaxation:
7991 ldr xd, [x0, #:tlsdesc_lo12:var] => movk x0, :tprel_g0_nc:var
7992
7993 Where R is x for lp64 mode, and w for ILP32 mode. */
7994 bfd_putl32 (movk_R0, contents + rel->r_offset);
7995 return bfd_reloc_continue;
7996 }
7997 else
7998 {
7999 /* GD->IE relaxation:
8000 ldr xd, [x0, #:tlsdesc_lo12:var] => ldr R0, [x0, #:gottprel_lo12:var]
8001
8002 Where R is x for lp64 mode, and w for ILP32 mode. */
8003 insn = bfd_getl32 (contents + rel->r_offset);
8004 bfd_putl32 (ldr_R0_mask (insn), contents + rel->r_offset);
8005 return bfd_reloc_continue;
8006 }
8007
8008 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
8009 if (is_local)
8010 {
8011 /* GD->LE relaxation
8012 add x0, #:tlsgd_lo12:var => movk R0, :tprel_g0_nc:var
8013 bl __tls_get_addr => mrs x1, tpidr_el0
8014 nop => add R0, R1, R0
8015
8016 Where R is x for lp64 mode, and w for ILP32 mode. */
8017
8018 /* First kill the tls_get_addr reloc on the bl instruction. */
8019 BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
8020 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
8021
8022 bfd_putl32 (movk_R0, contents + rel->r_offset);
8023 bfd_putl32 (0xd53bd041, contents + rel->r_offset + 4);
8024 bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 8);
8025 return bfd_reloc_continue;
8026 }
8027 else
8028 {
8029 /* GD->IE relaxation
8030 ADD x0, #:tlsgd_lo12:var => ldr R0, [x0, #:gottprel_lo12:var]
8031 BL __tls_get_addr => mrs x1, tpidr_el0
8032 R_AARCH64_CALL26
8033 NOP => add R0, R1, R0
8034
8035 Where R is x for lp64 mode, and w for ilp32 mode. */
8036
8037 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
8038
8039 /* Remove the relocation on the BL instruction. */
8040 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
8041
8042 /* We choose to fixup the BL and NOP instructions using the
8043 offset from the second relocation to allow flexibility in
8044 scheduling instructions between the ADD and BL. */
8045 bfd_putl32 (ldr_R0_x0, contents + rel->r_offset);
8046 bfd_putl32 (0xd53bd041, contents + rel[1].r_offset);
8047 bfd_putl32 (add_R0_R0_R1, contents + rel[1].r_offset + 4);
8048 return bfd_reloc_continue;
8049 }
8050
8051 case BFD_RELOC_MORELLO_TLSDESC_CALL:
8052 /* GD->LE relaxation:
8053 blr cd => add c0, c2, x0 */
8054 if (is_local || !bfd_link_pic (info))
8055 {
8056 bfd_putl32 (0xc2a06040, contents + rel->r_offset);
8057 return bfd_reloc_ok;
8058 }
8059 else
8060 goto set_nop;
8061
8062 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
8063 /* GD->LE relaxation:
8064 ldr cd, [c0, #:tlsdesc_lo12:var] => movk x1, objsize_lo16 */
8065 if ((is_local || !bfd_link_pic (info))
8066 && ELFNN_R_TYPE (rel[1].r_info) == MORELLO_R (TLSDESC_CALL))
8067 {
8068 bfd_putl32 (BUILD_MOVK(1, sym_size), contents + rel->r_offset);
8069 return bfd_reloc_continue;
8070 }
8071
8072 /* Fall through. */
8073 case BFD_RELOC_AARCH64_TLSDESC_ADD:
8074 case BFD_RELOC_AARCH64_TLSDESC_CALL:
8075 /* GD->IE/LE relaxation:
8076 add x0, x0, #:tlsdesc_lo12:var => nop
8077 blr xd => nop
8078 */
8079 set_nop:
8080 bfd_putl32 (INSN_NOP, contents + rel->r_offset);
8081 return bfd_reloc_ok;
8082
8083 case BFD_RELOC_AARCH64_TLSDESC_LDR:
8084 if (is_local)
8085 {
8086 /* GD->LE relaxation:
8087 ldr xd, [gp, xn] => movk R0, #:tprel_g0_nc:var
8088
8089 Where R is x for lp64 mode, and w for ILP32 mode. */
8090 bfd_putl32 (movk_R0, contents + rel->r_offset);
8091 return bfd_reloc_continue;
8092 }
8093 else
8094 {
8095 /* GD->IE relaxation:
8096 ldr xd, [gp, xn] => ldr R0, [gp, xn]
8097
8098 Where R is x for lp64 mode, and w for ILP32 mode. */
8099 insn = bfd_getl32 (contents + rel->r_offset);
8100 bfd_putl32 (ldr_R0_mask (insn), contents + rel->r_offset);
8101 return bfd_reloc_ok;
8102 }
8103
8104 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
8105 /* GD->LE relaxation:
8106 movk xd, #:tlsdesc_off_g0_nc:var => movk R0, #:tprel_g1_nc:var, lsl #16
8107 GD->IE relaxation:
8108 movk xd, #:tlsdesc_off_g0_nc:var => movk Rd, #:gottprel_g0_nc:var
8109
8110 Where R is x for lp64 mode, and w for ILP32 mode. */
8111 if (is_local)
8112 bfd_putl32 (ldr_hw_R0, contents + rel->r_offset);
8113 return bfd_reloc_continue;
8114
8115 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
8116 if (is_local)
8117 {
8118 /* GD->LE relaxation:
8119 movz xd, #:tlsdesc_off_g1:var => movz R0, #:tprel_g2:var, lsl #32
8120
8121 Where R is x for lp64 mode, and w for ILP32 mode. */
8122 bfd_putl32 (movz_hw_R0, contents + rel->r_offset);
8123 return bfd_reloc_continue;
8124 }
8125 else
8126 {
8127 /* GD->IE relaxation:
8128 movz xd, #:tlsdesc_off_g1:var => movz Rd, #:gottprel_g1:var, lsl #16
8129
8130 Where R is x for lp64 mode, and w for ILP32 mode. */
8131 insn = bfd_getl32 (contents + rel->r_offset);
8132 bfd_putl32 (movz_R0 | (insn & 0x1f), contents + rel->r_offset);
8133 return bfd_reloc_continue;
8134 }
8135
8136 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
8137 /* IE->LE relaxation:
8138 adrp xd, :gottprel:var => movz Rd, :tprel_g1:var
8139
8140 Where R is x for lp64 mode, and w for ILP32 mode. */
8141 if (is_local)
8142 {
8143 insn = bfd_getl32 (contents + rel->r_offset);
8144 bfd_putl32 (movz_R0 | (insn & 0x1f), contents + rel->r_offset);
8145 /* We have relaxed the adrp into a mov, we may have to clear any
8146 pending erratum fixes. */
8147 clear_erratum_843419_entry (globals, rel->r_offset, input_section);
8148 }
8149 return bfd_reloc_continue;
8150
8151 case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
8152 /* IE->LE relaxation:
8153 ldr xd, [xm, #:gottprel_lo12:var] => movk Rd, :tprel_g0_nc:var
8154
8155 Where R is x for lp64 mode, and w for ILP32 mode. */
8156 if (is_local)
8157 {
8158 insn = bfd_getl32 (contents + rel->r_offset);
8159 bfd_putl32 (movk_R0 | (insn & 0x1f), contents + rel->r_offset);
8160 }
8161 return bfd_reloc_continue;
8162
8163 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
8164 /* LD->LE relaxation (tiny):
8165 adr x0, :tlsldm:x => mrs x0, tpidr_el0
8166 bl __tls_get_addr => add R0, R0, TCB_SIZE
8167
8168 Where R is x for lp64 mode, and w for ilp32 mode. */
8169 if (is_local)
8170 {
8171 BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
8172 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
8173 /* No need of CALL26 relocation for tls_get_addr. */
8174 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
8175 bfd_putl32 (0xd53bd040, contents + rel->r_offset + 0);
8176 bfd_putl32 (add_R0_R0 | (TCB_SIZE (input_bfd) << 10),
8177 contents + rel->r_offset + 4);
8178 return bfd_reloc_ok;
8179 }
8180 return bfd_reloc_continue;
8181
8182 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
8183 /* LD->LE relaxation (small):
8184 adrp x0, :tlsldm:x => mrs x0, tpidr_el0
8185 */
8186 if (is_local)
8187 {
8188 bfd_putl32 (0xd53bd040, contents + rel->r_offset);
8189 return bfd_reloc_ok;
8190 }
8191 return bfd_reloc_continue;
8192
8193 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
8194 /* LD->LE relaxation (small):
8195 add x0, #:tlsldm_lo12:x => add R0, R0, TCB_SIZE
8196 bl __tls_get_addr => nop
8197
8198 Where R is x for lp64 mode, and w for ilp32 mode. */
8199 if (is_local)
8200 {
8201 BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
8202 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
8203 /* No need of CALL26 relocation for tls_get_addr. */
8204 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
8205 bfd_putl32 (add_R0_R0 | (TCB_SIZE (input_bfd) << 10),
8206 contents + rel->r_offset + 0);
8207 bfd_putl32 (INSN_NOP, contents + rel->r_offset + 4);
8208 return bfd_reloc_ok;
8209 }
8210 return bfd_reloc_continue;
8211
8212 default:
8213 return bfd_reloc_continue;
8214 }
8215
8216 return bfd_reloc_ok;
8217 }
8218
8219 /* Relocate an AArch64 ELF section. */
8220
8221 static bfd_boolean
8222 elfNN_aarch64_relocate_section (bfd *output_bfd,
8223 struct bfd_link_info *info,
8224 bfd *input_bfd,
8225 asection *input_section,
8226 bfd_byte *contents,
8227 Elf_Internal_Rela *relocs,
8228 Elf_Internal_Sym *local_syms,
8229 asection **local_sections)
8230 {
8231 Elf_Internal_Shdr *symtab_hdr;
8232 struct elf_link_hash_entry **sym_hashes;
8233 Elf_Internal_Rela *rel;
8234 Elf_Internal_Rela *relend;
8235 const char *name;
8236 struct elf_aarch64_link_hash_table *globals;
8237 bfd_boolean save_addend = FALSE;
8238 bfd_vma addend = 0;
8239
8240 globals = elf_aarch64_hash_table (info);
8241
8242 symtab_hdr = &elf_symtab_hdr (input_bfd);
8243 sym_hashes = elf_sym_hashes (input_bfd);
8244
8245 rel = relocs;
8246 relend = relocs + input_section->reloc_count;
8247 for (; rel < relend; rel++)
8248 {
8249 unsigned int r_type;
8250 bfd_reloc_code_real_type bfd_r_type;
8251 bfd_reloc_code_real_type relaxed_bfd_r_type;
8252 reloc_howto_type *howto;
8253 unsigned long r_symndx;
8254 Elf_Internal_Sym *sym;
8255 asection *sec;
8256 struct elf_link_hash_entry *h;
8257 bfd_vma relocation;
8258 bfd_reloc_status_type r;
8259 arelent bfd_reloc;
8260 char sym_type;
8261 bfd_boolean unresolved_reloc = FALSE;
8262 char *error_message = NULL;
8263
8264 r_symndx = ELFNN_R_SYM (rel->r_info);
8265 r_type = ELFNN_R_TYPE (rel->r_info);
8266
8267 bfd_reloc.howto = elfNN_aarch64_howto_from_type (input_bfd, r_type);
8268 howto = bfd_reloc.howto;
8269
8270 if (howto == NULL)
8271 return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
8272
8273 bfd_r_type = elfNN_aarch64_bfd_reloc_from_howto (howto);
8274
8275 h = NULL;
8276 sym = NULL;
8277 sec = NULL;
8278
8279 if (r_symndx < symtab_hdr->sh_info)
8280 {
8281 sym = local_syms + r_symndx;
8282 sym_type = ELFNN_ST_TYPE (sym->st_info);
8283 sec = local_sections[r_symndx];
8284
8285 /* An object file might have a reference to a local
8286 undefined symbol. This is a daft object file, but we
8287 should at least do something about it. */
8288 if (r_type != R_AARCH64_NONE && r_type != R_AARCH64_NULL
8289 && bfd_is_und_section (sec)
8290 && ELF_ST_BIND (sym->st_info) != STB_WEAK)
8291 (*info->callbacks->undefined_symbol)
8292 (info, bfd_elf_string_from_elf_section
8293 (input_bfd, symtab_hdr->sh_link, sym->st_name),
8294 input_bfd, input_section, rel->r_offset, TRUE);
8295
8296 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8297
8298 /* Relocate against local STT_GNU_IFUNC symbol. */
8299 if (!bfd_link_relocatable (info)
8300 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8301 {
8302 h = elfNN_aarch64_get_local_sym_hash (globals, input_bfd,
8303 rel, FALSE);
8304 if (h == NULL)
8305 abort ();
8306
8307 /* Set STT_GNU_IFUNC symbol value. */
8308 h->root.u.def.value = sym->st_value;
8309 h->root.u.def.section = sec;
8310 }
8311 }
8312 else
8313 {
8314 bfd_boolean warned, ignored;
8315
8316 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
8317 r_symndx, symtab_hdr, sym_hashes,
8318 h, sec, relocation,
8319 unresolved_reloc, warned, ignored);
8320
8321 sym_type = h->type;
8322 }
8323
8324 if (sec != NULL && discarded_section (sec))
8325 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
8326 rel, 1, relend, howto, 0, contents);
8327
8328 if (bfd_link_relocatable (info))
8329 continue;
8330
8331 if (h != NULL)
8332 name = h->root.root.string;
8333 else
8334 {
8335 name = (bfd_elf_string_from_elf_section
8336 (input_bfd, symtab_hdr->sh_link, sym->st_name));
8337 if (name == NULL || *name == '\0')
8338 name = bfd_section_name (sec);
8339 }
8340
8341 if (r_symndx != 0
8342 && r_type != R_AARCH64_NONE
8343 && r_type != R_AARCH64_NULL
8344 && (h == NULL
8345 || h->root.type == bfd_link_hash_defined
8346 || h->root.type == bfd_link_hash_defweak)
8347 && IS_AARCH64_TLS_RELOC (bfd_r_type) != (sym_type == STT_TLS))
8348 {
8349 _bfd_error_handler
8350 ((sym_type == STT_TLS
8351 /* xgettext:c-format */
8352 ? _("%pB(%pA+%#" PRIx64 "): %s used with TLS symbol %s")
8353 /* xgettext:c-format */
8354 : _("%pB(%pA+%#" PRIx64 "): %s used with non-TLS symbol %s")),
8355 input_bfd,
8356 input_section, (uint64_t) rel->r_offset, howto->name, name);
8357 }
8358
8359 if (r_symndx
8360 && h
8361 && IS_AARCH64_TLS_RELOC (bfd_r_type)
8362 && h->root.type == bfd_link_hash_undefweak)
8363 /* We have already warned about these in aarch64_check_relocs,
8364 so just skip over them. */
8365 continue;
8366
8367 /* We relax only if we can see that there can be a valid transition
8368 from a reloc type to another.
8369 We call elfNN_aarch64_final_link_relocate unless we're completely
8370 done, i.e., the relaxation produced the final output we want. */
8371
8372 relaxed_bfd_r_type = aarch64_tls_transition (input_bfd, info, rel,
8373 h, r_symndx);
8374 if (relaxed_bfd_r_type != bfd_r_type)
8375 {
8376 bfd_r_type = relaxed_bfd_r_type;
8377 howto = elfNN_aarch64_howto_from_bfd_reloc (bfd_r_type);
8378 BFD_ASSERT (howto != NULL);
8379 r_type = howto->type;
8380 r = elfNN_aarch64_tls_relax (input_bfd, info, input_section,
8381 contents, rel, h, r_symndx);
8382 unresolved_reloc = 0;
8383 }
8384 else
8385 r = bfd_reloc_continue;
8386
8387 /* There may be multiple consecutive relocations for the
8388 same offset. In that case we are supposed to treat the
8389 output of each relocation as the addend for the next. */
8390 if (rel + 1 < relend
8391 && rel->r_offset == rel[1].r_offset
8392 && ELFNN_R_TYPE (rel[1].r_info) != R_AARCH64_NONE
8393 && ELFNN_R_TYPE (rel[1].r_info) != R_AARCH64_NULL)
8394 save_addend = TRUE;
8395 else
8396 save_addend = FALSE;
8397
8398 if (r == bfd_reloc_continue)
8399 r = elfNN_aarch64_final_link_relocate (howto, input_bfd, output_bfd,
8400 input_section, contents, rel,
8401 relocation, info, sec,
8402 h, &unresolved_reloc,
8403 save_addend, &addend, sym);
8404
8405 bfd_boolean c64_rtype = FALSE;
8406
8407 switch (elfNN_aarch64_bfd_reloc_from_type (input_bfd, r_type))
8408 {
8409 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
8410 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
8411 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
8412 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
8413 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
8414 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
8415 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
8416 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
8417 if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
8418 {
8419 bfd_boolean need_relocs = FALSE;
8420 bfd_byte *loc;
8421 int indx;
8422 bfd_vma off;
8423
8424 off = symbol_got_offset (input_bfd, h, r_symndx);
8425 indx = h && h->dynindx != -1 ? h->dynindx : 0;
8426
8427 need_relocs =
8428 (!bfd_link_executable (info) || indx != 0) &&
8429 (h == NULL
8430 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8431 || h->root.type != bfd_link_hash_undefweak);
8432
8433 BFD_ASSERT (globals->root.srelgot != NULL);
8434
8435 if (need_relocs)
8436 {
8437 Elf_Internal_Rela rela;
8438 rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLS_DTPMOD));
8439 rela.r_addend = 0;
8440 rela.r_offset = globals->root.sgot->output_section->vma +
8441 globals->root.sgot->output_offset + off;
8442
8443
8444 loc = globals->root.srelgot->contents;
8445 loc += globals->root.srelgot->reloc_count++
8446 * RELOC_SIZE (htab);
8447 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
8448
8449 bfd_reloc_code_real_type real_type =
8450 elfNN_aarch64_bfd_reloc_from_type (input_bfd, r_type);
8451
8452 if (real_type == BFD_RELOC_AARCH64_TLSLD_ADR_PREL21
8453 || real_type == BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21
8454 || real_type == BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC)
8455 {
8456 /* For local dynamic, don't generate DTPREL in any case.
8457 Initialize the DTPREL slot into zero, so we get module
8458 base address when invoke runtime TLS resolver. */
8459 bfd_put_NN (output_bfd, 0,
8460 globals->root.sgot->contents + off
8461 + GOT_ENTRY_SIZE (globals));
8462 }
8463 else if (indx == 0)
8464 {
8465 bfd_put_NN (output_bfd,
8466 relocation - dtpoff_base (info),
8467 globals->root.sgot->contents + off
8468 + GOT_ENTRY_SIZE (globals));
8469 }
8470 else
8471 {
8472 /* This TLS symbol is global. We emit a
8473 relocation to fixup the tls offset at load
8474 time. */
8475 rela.r_info =
8476 ELFNN_R_INFO (indx, AARCH64_R (TLS_DTPREL));
8477 rela.r_addend = 0;
8478 rela.r_offset =
8479 (globals->root.sgot->output_section->vma
8480 + globals->root.sgot->output_offset + off
8481 + GOT_ENTRY_SIZE (globals));
8482
8483 loc = globals->root.srelgot->contents;
8484 loc += globals->root.srelgot->reloc_count++
8485 * RELOC_SIZE (globals);
8486 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
8487 bfd_put_NN (output_bfd, (bfd_vma) 0,
8488 globals->root.sgot->contents + off
8489 + GOT_ENTRY_SIZE (globals));
8490 }
8491 }
8492 else
8493 {
8494 bfd_put_NN (output_bfd, (bfd_vma) 1,
8495 globals->root.sgot->contents + off);
8496 bfd_put_NN (output_bfd,
8497 relocation - dtpoff_base (info),
8498 globals->root.sgot->contents + off
8499 + GOT_ENTRY_SIZE (globals));
8500 }
8501
8502 symbol_got_offset_mark (input_bfd, h, r_symndx);
8503 }
8504 break;
8505
8506 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
8507 case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
8508 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
8509 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
8510 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
8511 if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
8512 {
8513 bfd_boolean need_relocs = FALSE;
8514 bfd_byte *loc;
8515 int indx;
8516 bfd_vma off;
8517
8518 off = symbol_got_offset (input_bfd, h, r_symndx);
8519
8520 indx = h && h->dynindx != -1 ? h->dynindx : 0;
8521
8522 need_relocs =
8523 (!bfd_link_executable (info) || indx != 0) &&
8524 (h == NULL
8525 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8526 || h->root.type != bfd_link_hash_undefweak);
8527
8528 BFD_ASSERT (globals->root.srelgot != NULL);
8529
8530 if (need_relocs)
8531 {
8532 Elf_Internal_Rela rela;
8533
8534 if (indx == 0)
8535 rela.r_addend = relocation - dtpoff_base (info);
8536 else
8537 rela.r_addend = 0;
8538
8539 rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLS_TPREL));
8540 rela.r_offset = globals->root.sgot->output_section->vma +
8541 globals->root.sgot->output_offset + off;
8542
8543 loc = globals->root.srelgot->contents;
8544 loc += globals->root.srelgot->reloc_count++
8545 * RELOC_SIZE (htab);
8546
8547 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
8548
8549 bfd_put_NN (output_bfd, rela.r_addend,
8550 globals->root.sgot->contents + off);
8551 }
8552 else
8553 bfd_put_NN (output_bfd, relocation - tpoff_base (info),
8554 globals->root.sgot->contents + off);
8555
8556 symbol_got_offset_mark (input_bfd, h, r_symndx);
8557 }
8558 break;
8559
8560 case BFD_RELOC_MORELLO_TLSDESC_ADR_PAGE20:
8561 case BFD_RELOC_MORELLO_TLSDESC_LD128_LO12:
8562 c64_rtype = TRUE;
8563 /* Fall through. */
8564
8565 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
8566 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
8567 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
8568 case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
8569 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
8570 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
8571 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
8572 if (! symbol_tlsdesc_got_offset_mark_p (input_bfd, h, r_symndx))
8573 {
8574 bfd_boolean need_relocs = FALSE;
8575 int indx = h && h->dynindx != -1 ? h->dynindx : 0;
8576 bfd_vma off = symbol_tlsdesc_got_offset (input_bfd, h, r_symndx);
8577
8578 need_relocs = (h == NULL
8579 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8580 || h->root.type != bfd_link_hash_undefweak);
8581
8582 BFD_ASSERT (globals->root.srelgot != NULL);
8583 BFD_ASSERT (globals->root.sgot != NULL);
8584
8585 if (need_relocs)
8586 {
8587 bfd_byte *loc;
8588 Elf_Internal_Rela rela;
8589
8590 rela.r_info = ELFNN_R_INFO (indx,
8591 (c64_rtype ? MORELLO_R (TLSDESC)
8592 : AARCH64_R (TLSDESC)));
8593
8594 rela.r_addend = 0;
8595 rela.r_offset = (globals->root.sgotplt->output_section->vma
8596 + globals->root.sgotplt->output_offset
8597 + off + globals->sgotplt_jump_table_size);
8598
8599 if (indx == 0)
8600 rela.r_addend = relocation - dtpoff_base (info);
8601
8602 /* Allocate the next available slot in the PLT reloc
8603 section to hold our R_AARCH64_TLSDESC, the next
8604 available slot is determined from reloc_count,
8605 which we step. But note, reloc_count was
8606 artifically moved down while allocating slots for
8607 real PLT relocs such that all of the PLT relocs
8608 will fit above the initial reloc_count and the
8609 extra stuff will fit below. */
8610 loc = globals->root.srelplt->contents;
8611 loc += globals->root.srelplt->reloc_count++
8612 * RELOC_SIZE (globals);
8613
8614 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
8615
8616 bfd_put_NN (output_bfd, (bfd_vma) 0,
8617 globals->root.sgotplt->contents + off +
8618 globals->sgotplt_jump_table_size);
8619 bfd_put_NN (output_bfd, (bfd_vma) 0,
8620 globals->root.sgotplt->contents + off +
8621 globals->sgotplt_jump_table_size +
8622 GOT_ENTRY_SIZE (globals));
8623 }
8624
8625 symbol_tlsdesc_got_offset_mark (input_bfd, h, r_symndx);
8626 }
8627 break;
8628 default:
8629 break;
8630 }
8631
8632 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
8633 because such sections are not SEC_ALLOC and thus ld.so will
8634 not process them. */
8635 if (unresolved_reloc
8636 && !((input_section->flags & SEC_DEBUGGING) != 0
8637 && h->def_dynamic)
8638 && _bfd_elf_section_offset (output_bfd, info, input_section,
8639 +rel->r_offset) != (bfd_vma) - 1)
8640 {
8641 _bfd_error_handler
8642 /* xgettext:c-format */
8643 (_("%pB(%pA+%#" PRIx64 "): "
8644 "unresolvable %s relocation against symbol `%s'"),
8645 input_bfd, input_section, (uint64_t) rel->r_offset, howto->name,
8646 h->root.root.string);
8647 return FALSE;
8648 }
8649
8650 if (r != bfd_reloc_ok && r != bfd_reloc_continue)
8651 {
8652 bfd_reloc_code_real_type real_r_type
8653 = elfNN_aarch64_bfd_reloc_from_type (input_bfd, r_type);
8654
8655 switch (r)
8656 {
8657 case bfd_reloc_overflow:
8658 (*info->callbacks->reloc_overflow)
8659 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
8660 input_bfd, input_section, rel->r_offset);
8661 if (real_r_type == BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15
8662 || real_r_type == BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14)
8663 {
8664 (*info->callbacks->warning)
8665 (info,
8666 _("too many GOT entries for -fpic, "
8667 "please recompile with -fPIC"),
8668 name, input_bfd, input_section, rel->r_offset);
8669 return FALSE;
8670 }
8671 /* Overflow can occur when a variable is referenced with a type
8672 that has a larger alignment than the type with which it was
8673 declared. eg:
8674 file1.c: extern int foo; int a (void) { return foo; }
8675 file2.c: char bar, foo, baz;
8676 If the variable is placed into a data section at an offset
8677 that is incompatible with the larger alignment requirement
8678 overflow will occur. (Strictly speaking this is not overflow
8679 but rather an alignment problem, but the bfd_reloc_ error
8680 enum does not have a value to cover that situation).
8681
8682 Try to catch this situation here and provide a more helpful
8683 error message to the user. */
8684 if (addend & (((bfd_vma) 1 << howto->rightshift) - 1)
8685 /* FIXME: Are we testing all of the appropriate reloc
8686 types here ? */
8687 && (real_r_type == BFD_RELOC_AARCH64_LD_LO19_PCREL
8688 || real_r_type == BFD_RELOC_AARCH64_LDST16_LO12
8689 || real_r_type == BFD_RELOC_AARCH64_LDST32_LO12
8690 || real_r_type == BFD_RELOC_AARCH64_LDST64_LO12
8691 || real_r_type == BFD_RELOC_AARCH64_LDST128_LO12))
8692 {
8693 info->callbacks->warning
8694 (info, _("one possible cause of this error is that the \
8695 symbol is being referenced in the indicated code as if it had a larger \
8696 alignment than was declared where it was defined"),
8697 name, input_bfd, input_section, rel->r_offset);
8698 }
8699
8700 if (real_r_type == BFD_RELOC_MORELLO_CAPINIT)
8701 info->callbacks->warning
8702 (info, _("relocation offset must be capability aligned"),
8703 name, input_bfd, input_section, rel->r_offset);
8704 break;
8705
8706 case bfd_reloc_undefined:
8707 (*info->callbacks->undefined_symbol)
8708 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
8709 break;
8710
8711 case bfd_reloc_outofrange:
8712 error_message = _("out of range");
8713 goto common_error;
8714
8715 case bfd_reloc_notsupported:
8716 error_message = _("unsupported relocation");
8717 goto common_error;
8718
8719 case bfd_reloc_dangerous:
8720 /* error_message should already be set. */
8721 goto common_error;
8722
8723 default:
8724 error_message = _("unknown error");
8725 /* Fall through. */
8726
8727 common_error:
8728 BFD_ASSERT (error_message != NULL);
8729 (*info->callbacks->reloc_dangerous)
8730 (info, error_message, input_bfd, input_section, rel->r_offset);
8731 break;
8732 }
8733 }
8734
8735 if (!save_addend)
8736 addend = 0;
8737 }
8738
8739 return TRUE;
8740 }
8741
8742 /* Set the right machine number. */
8743
8744 static bfd_boolean
8745 elfNN_aarch64_object_p (bfd *abfd)
8746 {
8747 #if ARCH_SIZE == 32
8748 bfd_default_set_arch_mach (abfd, bfd_arch_aarch64, bfd_mach_aarch64_ilp32);
8749 #else
8750 bfd_default_set_arch_mach (abfd, bfd_arch_aarch64, bfd_mach_aarch64);
8751 #endif
8752 return TRUE;
8753 }
8754
8755 /* Function to keep AArch64 specific flags in the ELF header. */
8756
8757 static bfd_boolean
8758 elfNN_aarch64_set_private_flags (bfd *abfd, flagword flags)
8759 {
8760 if (elf_flags_init (abfd) && elf_elfheader (abfd)->e_flags != flags)
8761 {
8762 }
8763 else
8764 {
8765 elf_elfheader (abfd)->e_flags = flags;
8766 elf_flags_init (abfd) = TRUE;
8767 }
8768
8769 return TRUE;
8770 }
8771
8772 /* Merge backend specific data from an object file to the output
8773 object file when linking. */
8774
8775 static bfd_boolean
8776 elfNN_aarch64_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
8777 {
8778 bfd *obfd = info->output_bfd;
8779 flagword out_flags;
8780 flagword in_flags;
8781 bfd_boolean flags_compatible = FALSE;
8782 asection *sec;
8783
8784 /* Check if we have the same endianess. */
8785 if (!_bfd_generic_verify_endian_match (ibfd, info))
8786 return FALSE;
8787
8788 if (!is_aarch64_elf (ibfd) || !is_aarch64_elf (obfd))
8789 return TRUE;
8790
8791 /* The input BFD must have had its flags initialised. */
8792 /* The following seems bogus to me -- The flags are initialized in
8793 the assembler but I don't think an elf_flags_init field is
8794 written into the object. */
8795 /* BFD_ASSERT (elf_flags_init (ibfd)); */
8796
8797 in_flags = elf_elfheader (ibfd)->e_flags;
8798 out_flags = elf_elfheader (obfd)->e_flags;
8799
8800 if (!elf_flags_init (obfd))
8801 {
8802 elf_flags_init (obfd) = TRUE;
8803
8804 /* If the input is the default architecture and had the default
8805 flags then do not bother setting the flags for the output
8806 architecture, instead allow future merges to do this. If no
8807 future merges ever set these flags then they will retain their
8808 uninitialised values, which surprise surprise, correspond
8809 to the default values. */
8810 if (bfd_get_arch_info (ibfd)->the_default
8811 && elf_elfheader (ibfd)->e_flags == 0)
8812 return TRUE;
8813
8814 elf_elfheader (obfd)->e_flags = in_flags;
8815
8816 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
8817 && bfd_get_arch_info (obfd)->the_default)
8818 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
8819 bfd_get_mach (ibfd));
8820
8821 return TRUE;
8822 }
8823
8824 /* Identical flags must be compatible. */
8825 if (in_flags == out_flags)
8826 return TRUE;
8827
8828 /* Check to see if the input BFD actually contains any sections. If
8829 not, its flags may not have been initialised either, but it
8830 cannot actually cause any incompatiblity. Do not short-circuit
8831 dynamic objects; their section list may be emptied by
8832 elf_link_add_object_symbols.
8833
8834 Also check to see if there are no code sections in the input.
8835 In this case there is no need to check for code specific flags.
8836 XXX - do we need to worry about floating-point format compatability
8837 in data sections ?
8838
8839 We definitely need to check for data sections if one set of flags is
8840 targetting the PURECAP abi and another is not. Pointers being
8841 capabilities in data sections can not be glossed over. */
8842 if (!(ibfd->flags & DYNAMIC))
8843 {
8844 bfd_boolean null_input_bfd = TRUE;
8845 bfd_boolean only_data_sections
8846 = !(in_flags & EF_AARCH64_CHERI_PURECAP
8847 || out_flags & EF_AARCH64_CHERI_PURECAP);
8848
8849 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8850 {
8851 if ((bfd_section_flags (sec)
8852 & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
8853 == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
8854 only_data_sections = FALSE;
8855
8856 null_input_bfd = FALSE;
8857 break;
8858 }
8859
8860 if (null_input_bfd || only_data_sections)
8861 return TRUE;
8862 }
8863
8864 return flags_compatible;
8865 }
8866
8867 /* Display the flags field. */
8868
8869 static bfd_boolean
8870 elfNN_aarch64_print_private_bfd_data (bfd *abfd, void *ptr)
8871 {
8872 FILE *file = (FILE *) ptr;
8873 unsigned long flags;
8874
8875 BFD_ASSERT (abfd != NULL && ptr != NULL);
8876
8877 /* Print normal ELF private data. */
8878 _bfd_elf_print_private_bfd_data (abfd, ptr);
8879
8880 flags = elf_elfheader (abfd)->e_flags;
8881 /* Ignore init flag - it may not be set, despite the flags field
8882 containing valid data. */
8883
8884 /* xgettext:c-format */
8885 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
8886
8887 if (flags)
8888 fprintf (file, _("<Unrecognised flag bits set>"));
8889
8890 fputc ('\n', file);
8891
8892 return TRUE;
8893 }
8894
8895 /* Return true if we need copy relocation against EH. */
8896
8897 static bfd_boolean
8898 need_copy_relocation_p (struct elf_aarch64_link_hash_entry *eh)
8899 {
8900 struct elf_dyn_relocs *p;
8901 asection *s;
8902
8903 for (p = eh->root.dyn_relocs; p != NULL; p = p->next)
8904 {
8905 /* If there is any pc-relative reference, we need to keep copy relocation
8906 to avoid propagating the relocation into runtime that current glibc
8907 does not support. */
8908 if (p->pc_count)
8909 return TRUE;
8910
8911 s = p->sec->output_section;
8912 /* Need copy relocation if it's against read-only section. */
8913 if (s != NULL && (s->flags & SEC_READONLY) != 0)
8914 return TRUE;
8915 }
8916
8917 return FALSE;
8918 }
8919
8920 /* Adjust a symbol defined by a dynamic object and referenced by a
8921 regular object. The current definition is in some section of the
8922 dynamic object, but we're not including those sections. We have to
8923 change the definition to something the rest of the link can
8924 understand. */
8925
8926 static bfd_boolean
8927 elfNN_aarch64_adjust_dynamic_symbol (struct bfd_link_info *info,
8928 struct elf_link_hash_entry *h)
8929 {
8930 struct elf_aarch64_link_hash_table *htab;
8931 asection *s, *srel;
8932
8933 /* If this is a function, put it in the procedure linkage table. We
8934 will fill in the contents of the procedure linkage table later,
8935 when we know the address of the .got section. */
8936 if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
8937 {
8938 if (h->plt.refcount <= 0
8939 || (h->type != STT_GNU_IFUNC
8940 && (SYMBOL_CALLS_LOCAL (info, h)
8941 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8942 && h->root.type == bfd_link_hash_undefweak))))
8943 {
8944 /* This case can occur if we saw a CALL26 reloc in
8945 an input file, but the symbol wasn't referred to
8946 by a dynamic object or all references were
8947 garbage collected. In which case we can end up
8948 resolving. */
8949 h->plt.offset = (bfd_vma) - 1;
8950 h->needs_plt = 0;
8951 }
8952
8953 return TRUE;
8954 }
8955 else
8956 /* Otherwise, reset to -1. */
8957 h->plt.offset = (bfd_vma) - 1;
8958
8959
8960 /* If this is a weak symbol, and there is a real definition, the
8961 processor independent code will have arranged for us to see the
8962 real definition first, and we can just use the same value. */
8963 if (h->is_weakalias)
8964 {
8965 struct elf_link_hash_entry *def = weakdef (h);
8966 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
8967 h->root.u.def.section = def->root.u.def.section;
8968 h->root.u.def.value = def->root.u.def.value;
8969 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
8970 h->non_got_ref = def->non_got_ref;
8971 return TRUE;
8972 }
8973
8974 /* If we are creating a shared library, we must presume that the
8975 only references to the symbol are via the global offset table.
8976 For such cases we need not do anything here; the relocations will
8977 be handled correctly by relocate_section. */
8978 if (bfd_link_pic (info))
8979 return TRUE;
8980
8981 /* If there are no references to this symbol that do not use the
8982 GOT, we don't need to generate a copy reloc. */
8983 if (!h->non_got_ref)
8984 return TRUE;
8985
8986 /* If -z nocopyreloc was given, we won't generate them either. */
8987 if (info->nocopyreloc)
8988 {
8989 h->non_got_ref = 0;
8990 return TRUE;
8991 }
8992
8993 if (ELIMINATE_COPY_RELOCS)
8994 {
8995 struct elf_aarch64_link_hash_entry *eh;
8996 /* If we don't find any dynamic relocs in read-only sections, then
8997 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
8998 eh = (struct elf_aarch64_link_hash_entry *) h;
8999 if (!need_copy_relocation_p (eh))
9000 {
9001 h->non_got_ref = 0;
9002 return TRUE;
9003 }
9004 }
9005
9006 /* We must allocate the symbol in our .dynbss section, which will
9007 become part of the .bss section of the executable. There will be
9008 an entry for this symbol in the .dynsym section. The dynamic
9009 object will contain position independent code, so all references
9010 from the dynamic object to this symbol will go through the global
9011 offset table. The dynamic linker will use the .dynsym entry to
9012 determine the address it must put in the global offset table, so
9013 both the dynamic object and the regular object will refer to the
9014 same memory location for the variable. */
9015
9016 htab = elf_aarch64_hash_table (info);
9017
9018 /* We must generate a R_AARCH64_COPY reloc to tell the dynamic linker
9019 to copy the initial value out of the dynamic object and into the
9020 runtime process image. */
9021 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
9022 {
9023 s = htab->root.sdynrelro;
9024 srel = htab->root.sreldynrelro;
9025 }
9026 else
9027 {
9028 s = htab->root.sdynbss;
9029 srel = htab->root.srelbss;
9030 }
9031 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
9032 {
9033 srel->size += RELOC_SIZE (htab);
9034 h->needs_copy = 1;
9035 }
9036
9037 return _bfd_elf_adjust_dynamic_copy (info, h, s);
9038
9039 }
9040
9041 static bfd_boolean
9042 elfNN_aarch64_allocate_local_symbols (bfd *abfd, unsigned number)
9043 {
9044 struct elf_aarch64_local_symbol *locals;
9045 locals = elf_aarch64_locals (abfd);
9046 if (locals == NULL)
9047 {
9048 locals = (struct elf_aarch64_local_symbol *)
9049 bfd_zalloc (abfd, number * sizeof (struct elf_aarch64_local_symbol));
9050 if (locals == NULL)
9051 return FALSE;
9052 elf_aarch64_locals (abfd) = locals;
9053 }
9054 return TRUE;
9055 }
9056
9057 /* Initialise the .got section to hold the global offset table. */
9058
9059 static void
9060 aarch64_elf_init_got_section (bfd *abfd, struct bfd_link_info *info)
9061 {
9062 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9063 asection *s;
9064 struct elf_aarch64_link_hash_table *globals = elf_aarch64_hash_table (info);
9065 unsigned int align = bed->s->log_file_align + globals->c64_rel;
9066
9067 if (globals->root.sgot != NULL)
9068 {
9069 bfd_set_section_alignment (globals->root.srelgot,
9070 bed->s->log_file_align);
9071 bfd_set_section_alignment (globals->root.sgot, align);
9072 globals->root.sgot->size += GOT_ENTRY_SIZE (globals);
9073 }
9074
9075 /* Track capability initialisation for static non-PIE binaries. */
9076 if (bfd_link_executable (info) && !bfd_link_pic (info)
9077 && globals->srelcaps == NULL)
9078 globals->srelcaps = globals->root.srelgot;
9079
9080 if (globals->root.igotplt != NULL)
9081 bfd_set_section_alignment (globals->root.igotplt, align);
9082
9083 s = globals->root.sgot;
9084
9085 if (globals->root.sgotplt != NULL)
9086 {
9087 bfd_set_section_alignment (globals->root.sgotplt, align);
9088 s = globals->root.sgotplt;
9089 }
9090
9091 /* The first bit of the global offset table is the header. */
9092 if (s != NULL)
9093 s->size += bed->got_header_size (info);
9094 }
9095
9096 /* Create the .got section to hold the global offset table. */
9097
9098 static bfd_boolean
9099 aarch64_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
9100 {
9101 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9102 flagword flags;
9103 asection *s;
9104 struct elf_link_hash_entry *h;
9105 struct elf_link_hash_table *htab = elf_hash_table (info);
9106
9107 /* This function may be called more than once. */
9108 if (htab->sgot != NULL)
9109 return TRUE;
9110
9111 flags = bed->dynamic_sec_flags;
9112
9113 s = bfd_make_section_anyway_with_flags (abfd,
9114 (bed->rela_plts_and_copies_p
9115 ? ".rela.got" : ".rel.got"),
9116 (bed->dynamic_sec_flags
9117 | SEC_READONLY));
9118 if (s == NULL)
9119 return FALSE;
9120 htab->srelgot = s;
9121
9122 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
9123 if (s == NULL)
9124 return FALSE;
9125 htab->sgot = s;
9126
9127 if (bed->want_got_sym)
9128 {
9129 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
9130 (or .got.plt) section. We don't do this in the linker script
9131 because we don't want to define the symbol if we are not creating
9132 a global offset table. */
9133 h = _bfd_elf_define_linkage_sym (abfd, info, s,
9134 "_GLOBAL_OFFSET_TABLE_");
9135 elf_hash_table (info)->hgot = h;
9136 if (h == NULL)
9137 return FALSE;
9138 }
9139
9140 if (bed->want_got_plt)
9141 {
9142 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
9143 if (s == NULL)
9144 return FALSE;
9145 htab->sgotplt = s;
9146 }
9147
9148 return TRUE;
9149 }
9150
9151 /* Look through the relocs for a section during the first phase. */
9152
9153 static bfd_boolean
9154 elfNN_aarch64_check_relocs (bfd *abfd, struct bfd_link_info *info,
9155 asection *sec, const Elf_Internal_Rela *relocs)
9156 {
9157 Elf_Internal_Shdr *symtab_hdr;
9158 struct elf_link_hash_entry **sym_hashes;
9159 const Elf_Internal_Rela *rel;
9160 const Elf_Internal_Rela *rel_end;
9161 asection *sreloc;
9162
9163 struct elf_aarch64_link_hash_table *htab;
9164
9165 if (bfd_link_relocatable (info))
9166 return TRUE;
9167
9168 BFD_ASSERT (is_aarch64_elf (abfd));
9169
9170 htab = elf_aarch64_hash_table (info);
9171 sreloc = NULL;
9172
9173 symtab_hdr = &elf_symtab_hdr (abfd);
9174 sym_hashes = elf_sym_hashes (abfd);
9175
9176 bfd_elfNN_aarch64_init_maps (abfd, info);
9177
9178 rel_end = relocs + sec->reloc_count;
9179 for (rel = relocs; rel < rel_end; rel++)
9180 {
9181 struct elf_link_hash_entry *h;
9182 unsigned int r_symndx, r_type;
9183 bfd_reloc_code_real_type bfd_r_type;
9184 Elf_Internal_Sym *isym;
9185
9186 r_symndx = ELFNN_R_SYM (rel->r_info);
9187 r_type = ELFNN_R_TYPE (rel->r_info);
9188 bfd_r_type = elfNN_aarch64_bfd_reloc_from_type (abfd, r_type);
9189
9190 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
9191 {
9192 /* xgettext:c-format */
9193 _bfd_error_handler (_("%pB: bad symbol index: %d"), abfd, r_symndx);
9194 return FALSE;
9195 }
9196
9197 if (r_symndx < symtab_hdr->sh_info)
9198 {
9199 /* A local symbol. */
9200 isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
9201 abfd, r_symndx);
9202 if (isym == NULL)
9203 return FALSE;
9204
9205 /* Check relocation against local STT_GNU_IFUNC symbol. */
9206 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
9207 {
9208 h = elfNN_aarch64_get_local_sym_hash (htab, abfd, rel,
9209 TRUE);
9210 if (h == NULL)
9211 return FALSE;
9212
9213 /* Fake a STT_GNU_IFUNC symbol. */
9214 h->type = STT_GNU_IFUNC;
9215 h->def_regular = 1;
9216 h->ref_regular = 1;
9217 h->forced_local = 1;
9218 h->root.type = bfd_link_hash_defined;
9219 }
9220 else
9221 h = NULL;
9222 }
9223 else
9224 {
9225 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
9226 while (h->root.type == bfd_link_hash_indirect
9227 || h->root.type == bfd_link_hash_warning)
9228 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9229 }
9230
9231 /* Ignore TLS relocations against weak undef symbols and warn about them.
9232 The behaviour of weak TLS variables is not well defined. Since making
9233 these well behaved is not a priority for Morello, we simply ignore
9234 TLS relocations against such symbols here to avoid the linker crashing
9235 on these and to enable making progress in other areas. */
9236 if (r_symndx
9237 && h
9238 && IS_AARCH64_TLS_RELOC (bfd_r_type)
9239 && h->root.type == bfd_link_hash_undefweak)
9240 {
9241 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
9242 _bfd_error_handler (_("%pB(%pA+%#" PRIx64 "): ignoring TLS relocation "
9243 "%s against undef weak symbol %s"),
9244 abfd, sec,
9245 (uint64_t) rel->r_offset,
9246 elfNN_aarch64_howto_table[howto_index].name,
9247 h->root.root.string);
9248 continue;
9249 }
9250
9251 /* Could be done earlier, if h were already available. */
9252 bfd_r_type = aarch64_tls_transition (abfd, info, rel, h, r_symndx);
9253
9254 if (h != NULL)
9255 {
9256 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
9257 This shows up in particular in an R_AARCH64_PREL64 in large model
9258 when calculating the pc-relative address to .got section which is
9259 used to initialize the gp register. */
9260 if (h->root.root.string
9261 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
9262 {
9263 if (htab->root.dynobj == NULL)
9264 htab->root.dynobj = abfd;
9265
9266 if (! aarch64_elf_create_got_section (htab->root.dynobj, info))
9267 return FALSE;
9268
9269 BFD_ASSERT (h == htab->root.hgot);
9270 }
9271
9272 /* Create the ifunc sections for static executables. If we
9273 never see an indirect function symbol nor we are building
9274 a static executable, those sections will be empty and
9275 won't appear in output. */
9276 switch (bfd_r_type)
9277 {
9278 default:
9279 break;
9280
9281 case BFD_RELOC_MORELLO_CALL26:
9282 case BFD_RELOC_MORELLO_JUMP26:
9283 /* For dynamic symbols record caller information so that we can
9284 decide what kind of PLT stubs to emit. */
9285 if (h != NULL)
9286 elf_aarch64_hash_entry (h)->got_type = GOT_CAP;
9287 /* Fall through. */
9288
9289 case BFD_RELOC_AARCH64_ADD_LO12:
9290 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
9291 case BFD_RELOC_MORELLO_ADR_GOT_PAGE:
9292 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
9293 case BFD_RELOC_MORELLO_ADR_HI20_PCREL:
9294 case BFD_RELOC_AARCH64_CALL26:
9295 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
9296 case BFD_RELOC_AARCH64_JUMP26:
9297 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
9298 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
9299 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
9300 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
9301 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
9302 case BFD_RELOC_MORELLO_LD128_GOT_LO12_NC:
9303 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
9304 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
9305 case BFD_RELOC_AARCH64_NN:
9306 if (htab->root.dynobj == NULL)
9307 htab->root.dynobj = abfd;
9308 if (!_bfd_elf_create_ifunc_sections (htab->root.dynobj, info))
9309 return FALSE;
9310 break;
9311 }
9312
9313 /* It is referenced by a non-shared object. */
9314 h->ref_regular = 1;
9315 }
9316
9317 switch (bfd_r_type)
9318 {
9319 case BFD_RELOC_AARCH64_16:
9320 #if ARCH_SIZE == 64
9321 case BFD_RELOC_AARCH64_32:
9322 #endif
9323 if (bfd_link_pic (info) && (sec->flags & SEC_ALLOC) != 0)
9324 {
9325 if (h != NULL
9326 /* This is an absolute symbol. It represents a value instead
9327 of an address. */
9328 && (bfd_is_abs_symbol (&h->root)
9329 /* This is an undefined symbol. */
9330 || h->root.type == bfd_link_hash_undefined))
9331 break;
9332
9333 /* For local symbols, defined global symbols in a non-ABS section,
9334 it is assumed that the value is an address. */
9335 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
9336 _bfd_error_handler
9337 /* xgettext:c-format */
9338 (_("%pB: relocation %s against `%s' can not be used when making "
9339 "a shared object"),
9340 abfd, elfNN_aarch64_howto_table[howto_index].name,
9341 (h) ? h->root.root.string : "a local symbol");
9342 bfd_set_error (bfd_error_bad_value);
9343 return FALSE;
9344 }
9345 else
9346 break;
9347
9348 case BFD_RELOC_AARCH64_MOVW_G0_NC:
9349 case BFD_RELOC_AARCH64_MOVW_G1_NC:
9350 case BFD_RELOC_AARCH64_MOVW_G2_NC:
9351 case BFD_RELOC_AARCH64_MOVW_G3:
9352 if (bfd_link_pic (info))
9353 {
9354 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
9355 _bfd_error_handler
9356 /* xgettext:c-format */
9357 (_("%pB: relocation %s against `%s' can not be used when making "
9358 "a shared object; recompile with -fPIC"),
9359 abfd, elfNN_aarch64_howto_table[howto_index].name,
9360 (h) ? h->root.root.string : "a local symbol");
9361 bfd_set_error (bfd_error_bad_value);
9362 return FALSE;
9363 }
9364 /* Fall through. */
9365
9366 case BFD_RELOC_AARCH64_16_PCREL:
9367 case BFD_RELOC_AARCH64_32_PCREL:
9368 case BFD_RELOC_AARCH64_64_PCREL:
9369 case BFD_RELOC_AARCH64_ADD_LO12:
9370 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
9371 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
9372 case BFD_RELOC_MORELLO_ADR_HI20_NC_PCREL:
9373 case BFD_RELOC_MORELLO_ADR_HI20_PCREL:
9374 case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
9375 case BFD_RELOC_AARCH64_LDST128_LO12:
9376 case BFD_RELOC_AARCH64_LDST16_LO12:
9377 case BFD_RELOC_AARCH64_LDST32_LO12:
9378 case BFD_RELOC_AARCH64_LDST64_LO12:
9379 case BFD_RELOC_AARCH64_LDST8_LO12:
9380 case BFD_RELOC_AARCH64_LD_LO19_PCREL:
9381 case BFD_RELOC_MORELLO_LD_LO17_PCREL:
9382 if (h == NULL || bfd_link_pic (info))
9383 break;
9384 /* Fall through. */
9385
9386 case BFD_RELOC_AARCH64_NN:
9387
9388 /* We don't need to handle relocs into sections not going into
9389 the "real" output. */
9390 if ((sec->flags & SEC_ALLOC) == 0)
9391 break;
9392
9393 if (h != NULL)
9394 {
9395 if (!bfd_link_pic (info))
9396 h->non_got_ref = 1;
9397
9398 h->plt.refcount += 1;
9399 h->pointer_equality_needed = 1;
9400 }
9401
9402 /* No need to do anything if we're not creating a shared
9403 object. */
9404 if (!(bfd_link_pic (info)
9405 /* If on the other hand, we are creating an executable, we
9406 may need to keep relocations for symbols satisfied by a
9407 dynamic library if we manage to avoid copy relocs for the
9408 symbol.
9409
9410 NOTE: Currently, there is no support of copy relocs
9411 elimination on pc-relative relocation types, because there is
9412 no dynamic relocation support for them in glibc. We still
9413 record the dynamic symbol reference for them. This is
9414 because one symbol may be referenced by both absolute
9415 relocation (for example, BFD_RELOC_AARCH64_NN) and
9416 pc-relative relocation. We need full symbol reference
9417 information to make correct decision later in
9418 elfNN_aarch64_adjust_dynamic_symbol. */
9419 || (ELIMINATE_COPY_RELOCS
9420 && !bfd_link_pic (info)
9421 && h != NULL
9422 && (h->root.type == bfd_link_hash_defweak
9423 || !h->def_regular))))
9424 break;
9425
9426 {
9427 struct elf_dyn_relocs *p;
9428 struct elf_dyn_relocs **head;
9429 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
9430
9431 /* We must copy these reloc types into the output file.
9432 Create a reloc section in dynobj and make room for
9433 this reloc. */
9434 if (sreloc == NULL)
9435 {
9436 if (htab->root.dynobj == NULL)
9437 htab->root.dynobj = abfd;
9438
9439 sreloc = _bfd_elf_make_dynamic_reloc_section
9440 (sec, htab->root.dynobj, LOG_FILE_ALIGN, abfd, /*rela? */ TRUE);
9441
9442 if (sreloc == NULL)
9443 return FALSE;
9444 }
9445
9446 /* If this is a global symbol, we count the number of
9447 relocations we need for this symbol. */
9448 if (h != NULL)
9449 {
9450 head = &h->dyn_relocs;
9451 }
9452 else
9453 {
9454 /* Track dynamic relocs needed for local syms too.
9455 We really need local syms available to do this
9456 easily. Oh well. */
9457
9458 asection *s;
9459 void **vpp;
9460
9461 isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
9462 abfd, r_symndx);
9463 if (isym == NULL)
9464 return FALSE;
9465
9466 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
9467 if (s == NULL)
9468 s = sec;
9469
9470 /* Beware of type punned pointers vs strict aliasing
9471 rules. */
9472 vpp = &(elf_section_data (s)->local_dynrel);
9473 head = (struct elf_dyn_relocs **) vpp;
9474 }
9475
9476 p = *head;
9477 if (p == NULL || p->sec != sec)
9478 {
9479 size_t amt = sizeof *p;
9480 p = ((struct elf_dyn_relocs *)
9481 bfd_zalloc (htab->root.dynobj, amt));
9482 if (p == NULL)
9483 return FALSE;
9484 p->next = *head;
9485 *head = p;
9486 p->sec = sec;
9487 }
9488
9489 p->count += 1;
9490
9491 if (elfNN_aarch64_howto_table[howto_index].pc_relative)
9492 p->pc_count += 1;
9493 }
9494 break;
9495
9496 /* RR: We probably want to keep a consistency check that
9497 there are no dangling GOT_PAGE relocs. */
9498 case BFD_RELOC_MORELLO_ADR_GOT_PAGE:
9499 case BFD_RELOC_MORELLO_LD128_GOT_LO12_NC:
9500 case BFD_RELOC_MORELLO_TLSDESC_ADR_PAGE20:
9501 case BFD_RELOC_MORELLO_TLSDESC_LD128_LO12:
9502 htab->c64_rel = 1;
9503 /* Fall through. */
9504
9505 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
9506 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
9507 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
9508 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
9509 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
9510 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
9511 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
9512 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
9513 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
9514 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
9515 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
9516 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
9517 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
9518 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12:
9519 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
9520 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
9521 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
9522 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
9523 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
9524 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
9525 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
9526 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
9527 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
9528 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
9529 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
9530 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
9531 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
9532 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
9533 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
9534 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
9535 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
9536 {
9537 unsigned got_type;
9538 unsigned old_got_type;
9539
9540 got_type = aarch64_reloc_got_type (bfd_r_type);
9541
9542 if (h)
9543 {
9544 h->got.refcount += 1;
9545 old_got_type = elf_aarch64_hash_entry (h)->got_type;
9546 }
9547 else
9548 {
9549 struct elf_aarch64_local_symbol *locals;
9550
9551 if (!elfNN_aarch64_allocate_local_symbols
9552 (abfd, symtab_hdr->sh_info))
9553 return FALSE;
9554
9555 locals = elf_aarch64_locals (abfd);
9556 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
9557 locals[r_symndx].got_refcount += 1;
9558 old_got_type = locals[r_symndx].got_type;
9559 }
9560
9561 /* If a variable is accessed with both general dynamic TLS
9562 methods, two slots may be created. */
9563 if (GOT_TLS_GD_ANY_P (old_got_type) && GOT_TLS_GD_ANY_P (got_type))
9564 got_type |= old_got_type;
9565
9566 /* We will already have issued an error message if there
9567 is a TLS/non-TLS mismatch, based on the symbol type.
9568 So just combine any TLS types needed. */
9569 if (old_got_type != GOT_UNKNOWN && old_got_type != GOT_NORMAL
9570 && got_type != GOT_NORMAL && old_got_type != GOT_CAP
9571 && got_type != GOT_CAP)
9572 got_type |= old_got_type;
9573
9574 /* If the symbol is accessed by both IE and GD methods, we
9575 are able to relax. Turn off the GD flag, without
9576 messing up with any other kind of TLS types that may be
9577 involved. */
9578 if ((got_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (got_type))
9579 got_type &= ~ (GOT_TLSDESC_GD | GOT_TLS_GD);
9580
9581 /* Prefer the capability reference. */
9582 if ((old_got_type & GOT_CAP) && (got_type & GOT_NORMAL))
9583 {
9584 got_type &= ~GOT_NORMAL;
9585 got_type |= GOT_CAP;
9586 }
9587
9588 if (old_got_type != got_type)
9589 {
9590 if (h != NULL)
9591 elf_aarch64_hash_entry (h)->got_type = got_type;
9592 else
9593 {
9594 struct elf_aarch64_local_symbol *locals;
9595 locals = elf_aarch64_locals (abfd);
9596 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
9597 locals[r_symndx].got_type = got_type;
9598 }
9599 }
9600
9601 if (htab->root.dynobj == NULL)
9602 htab->root.dynobj = abfd;
9603 if (! aarch64_elf_create_got_section (htab->root.dynobj, info))
9604 return FALSE;
9605 break;
9606 }
9607
9608 case BFD_RELOC_MORELLO_CALL26:
9609 case BFD_RELOC_MORELLO_JUMP26:
9610 htab->c64_rel = 1;
9611 if (h != NULL)
9612 elf_aarch64_hash_entry (h)->got_type = GOT_CAP;
9613
9614 /* Fall through. */
9615 case BFD_RELOC_AARCH64_CALL26:
9616 case BFD_RELOC_AARCH64_JUMP26:
9617 if (h == NULL)
9618 {
9619 isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, abfd,
9620 r_symndx);
9621 if (isym == NULL)
9622 return FALSE;
9623
9624 asection *s = bfd_section_from_elf_index (abfd, isym->st_shndx);
9625
9626 if (s == NULL)
9627 s = sec;
9628
9629 if (c64_value_p (s, isym->st_value))
9630 isym->st_target_internal |= ST_BRANCH_TO_C64;
9631
9632 /* If this is a local symbol then we resolve it
9633 directly without creating a PLT entry. */
9634 continue;
9635 }
9636
9637 if (h->root.type == bfd_link_hash_defined
9638 || h->root.type == bfd_link_hash_defweak)
9639 {
9640 asection *sym_sec = h->root.u.def.section;
9641 bfd_vma sym_value = h->root.u.def.value;
9642
9643 if (sym_sec != NULL && c64_value_p (sym_sec, sym_value))
9644 h->target_internal |= ST_BRANCH_TO_C64;
9645 }
9646
9647 h->needs_plt = 1;
9648 if (h->plt.refcount <= 0)
9649 h->plt.refcount = 1;
9650 else
9651 h->plt.refcount += 1;
9652 break;
9653
9654 case BFD_RELOC_MORELLO_CAPINIT:
9655 if (htab->srelcaps == NULL)
9656 {
9657 if (htab->root.dynobj == NULL)
9658 htab->root.dynobj = abfd;
9659
9660 sreloc = _bfd_elf_make_dynamic_reloc_section
9661 (sec, htab->root.dynobj, LOG_FILE_ALIGN,
9662 abfd, /*rela? */ TRUE);
9663
9664 if (sreloc == NULL)
9665 return FALSE;
9666
9667 htab->srelcaps = sreloc;
9668 }
9669 htab->srelcaps->size += RELOC_SIZE (htab);
9670
9671 break;
9672
9673 default:
9674 break;
9675 }
9676 }
9677
9678 return TRUE;
9679 }
9680
9681 /* Treat mapping symbols as special target symbols. */
9682
9683 static bfd_boolean
9684 elfNN_aarch64_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED,
9685 asymbol *sym)
9686 {
9687 return bfd_is_aarch64_special_symbol_name (sym->name,
9688 BFD_AARCH64_SPECIAL_SYM_TYPE_ANY);
9689 }
9690
9691 /* If the ELF symbol SYM might be a function in SEC, return the
9692 function size and set *CODE_OFF to the function's entry point,
9693 otherwise return zero. */
9694
9695 static bfd_size_type
9696 elfNN_aarch64_maybe_function_sym (const asymbol *sym, asection *sec,
9697 bfd_vma *code_off)
9698 {
9699 bfd_size_type size;
9700
9701 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
9702 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
9703 || sym->section != sec)
9704 return 0;
9705
9706 if (!(sym->flags & BSF_SYNTHETIC))
9707 switch (ELF_ST_TYPE (((elf_symbol_type *) sym)->internal_elf_sym.st_info))
9708 {
9709 case STT_FUNC:
9710 case STT_NOTYPE:
9711 break;
9712 default:
9713 return 0;
9714 }
9715
9716 if ((sym->flags & BSF_LOCAL)
9717 && bfd_is_aarch64_special_symbol_name (sym->name,
9718 BFD_AARCH64_SPECIAL_SYM_TYPE_ANY))
9719 return 0;
9720
9721 *code_off = sym->value;
9722 size = 0;
9723 if (!(sym->flags & BSF_SYNTHETIC))
9724 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
9725 if (size == 0)
9726 size = 1;
9727 return size;
9728 }
9729
9730 static bfd_boolean
9731 elfNN_aarch64_find_inliner_info (bfd *abfd,
9732 const char **filename_ptr,
9733 const char **functionname_ptr,
9734 unsigned int *line_ptr)
9735 {
9736 bfd_boolean found;
9737 found = _bfd_dwarf2_find_inliner_info
9738 (abfd, filename_ptr,
9739 functionname_ptr, line_ptr, &elf_tdata (abfd)->dwarf2_find_line_info);
9740 return found;
9741 }
9742
9743
9744 static bfd_boolean
9745 elfNN_aarch64_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
9746 {
9747 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
9748
9749 if (!_bfd_elf_init_file_header (abfd, link_info))
9750 return FALSE;
9751
9752 i_ehdrp = elf_elfheader (abfd);
9753 i_ehdrp->e_ident[EI_ABIVERSION] = AARCH64_ELF_ABI_VERSION;
9754 return TRUE;
9755 }
9756
9757 static enum elf_reloc_type_class
9758 elfNN_aarch64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
9759 const asection *rel_sec ATTRIBUTE_UNUSED,
9760 const Elf_Internal_Rela *rela)
9761 {
9762 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
9763
9764 if (htab->root.dynsym != NULL
9765 && htab->root.dynsym->contents != NULL)
9766 {
9767 /* Check relocation against STT_GNU_IFUNC symbol if there are
9768 dynamic symbols. */
9769 bfd *abfd = info->output_bfd;
9770 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9771 unsigned long r_symndx = ELFNN_R_SYM (rela->r_info);
9772 if (r_symndx != STN_UNDEF)
9773 {
9774 Elf_Internal_Sym sym;
9775 if (!bed->s->swap_symbol_in (abfd,
9776 (htab->root.dynsym->contents
9777 + r_symndx * bed->s->sizeof_sym),
9778 0, &sym))
9779 {
9780 /* xgettext:c-format */
9781 _bfd_error_handler (_("%pB symbol number %lu references"
9782 " nonexistent SHT_SYMTAB_SHNDX section"),
9783 abfd, r_symndx);
9784 /* Ideally an error class should be returned here. */
9785 }
9786 else if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
9787 return reloc_class_ifunc;
9788 }
9789 }
9790
9791 switch ((int) ELFNN_R_TYPE (rela->r_info))
9792 {
9793 case AARCH64_R (IRELATIVE):
9794 case MORELLO_R (IRELATIVE):
9795 return reloc_class_ifunc;
9796 case AARCH64_R (RELATIVE):
9797 case MORELLO_R (RELATIVE):
9798 return reloc_class_relative;
9799 case AARCH64_R (JUMP_SLOT):
9800 case MORELLO_R (JUMP_SLOT):
9801 return reloc_class_plt;
9802 case AARCH64_R (COPY):
9803 return reloc_class_copy;
9804 default:
9805 return reloc_class_normal;
9806 }
9807 }
9808
9809 /* Handle an AArch64 specific section when reading an object file. This is
9810 called when bfd_section_from_shdr finds a section with an unknown
9811 type. */
9812
9813 static bfd_boolean
9814 elfNN_aarch64_section_from_shdr (bfd *abfd,
9815 Elf_Internal_Shdr *hdr,
9816 const char *name, int shindex)
9817 {
9818 /* There ought to be a place to keep ELF backend specific flags, but
9819 at the moment there isn't one. We just keep track of the
9820 sections by their name, instead. Fortunately, the ABI gives
9821 names for all the AArch64 specific sections, so we will probably get
9822 away with this. */
9823 switch (hdr->sh_type)
9824 {
9825 case SHT_AARCH64_ATTRIBUTES:
9826 break;
9827
9828 default:
9829 return FALSE;
9830 }
9831
9832 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
9833 return FALSE;
9834
9835 return TRUE;
9836 }
9837
9838 typedef struct
9839 {
9840 void *finfo;
9841 struct bfd_link_info *info;
9842 asection *sec;
9843 int sec_shndx;
9844 int (*func) (void *, const char *, Elf_Internal_Sym *,
9845 asection *, struct elf_link_hash_entry *);
9846 } output_arch_syminfo;
9847
9848 enum map_symbol_type
9849 {
9850 AARCH64_MAP_INSN,
9851 AARCH64_MAP_DATA,
9852 AARCH64_MAP_C64,
9853 };
9854
9855
9856 /* Output a single mapping symbol. */
9857
9858 static bfd_boolean
9859 elfNN_aarch64_output_map_sym (output_arch_syminfo *osi,
9860 enum map_symbol_type type, bfd_vma offset)
9861 {
9862 static const char *names[3] = { "$x", "$d", "$c" };
9863 Elf_Internal_Sym sym;
9864
9865 sym.st_value = (osi->sec->output_section->vma
9866 + osi->sec->output_offset + offset);
9867 sym.st_size = 0;
9868 sym.st_other = 0;
9869 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
9870 sym.st_shndx = osi->sec_shndx;
9871 sym.st_target_internal = 0;
9872 return osi->func (osi->finfo, names[type], &sym, osi->sec, NULL) == 1;
9873 }
9874
9875 /* Output a single local symbol for a generated stub. */
9876
9877 static bfd_boolean
9878 elfNN_aarch64_output_stub_sym (output_arch_syminfo *osi, const char *name,
9879 bfd_vma offset, bfd_vma size)
9880 {
9881 Elf_Internal_Sym sym;
9882
9883 sym.st_value = (osi->sec->output_section->vma
9884 + osi->sec->output_offset + offset);
9885 sym.st_size = size;
9886 sym.st_other = 0;
9887 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
9888 sym.st_shndx = osi->sec_shndx;
9889 sym.st_target_internal = 0;
9890 return osi->func (osi->finfo, name, &sym, osi->sec, NULL) == 1;
9891 }
9892
9893 static bfd_boolean
9894 aarch64_map_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
9895 {
9896 struct elf_aarch64_stub_hash_entry *stub_entry;
9897 asection *stub_sec;
9898 bfd_vma addr;
9899 char *stub_name;
9900 output_arch_syminfo *osi;
9901
9902 /* Massage our args to the form they really have. */
9903 stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
9904 osi = (output_arch_syminfo *) in_arg;
9905
9906 stub_sec = stub_entry->stub_sec;
9907
9908 /* Ensure this stub is attached to the current section being
9909 processed. */
9910 if (stub_sec != osi->sec)
9911 return TRUE;
9912
9913 addr = (bfd_vma) stub_entry->stub_offset;
9914
9915 stub_name = stub_entry->output_name;
9916
9917 switch (stub_entry->stub_type)
9918 {
9919 case aarch64_stub_adrp_branch:
9920 if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
9921 sizeof (aarch64_adrp_branch_stub)))
9922 return FALSE;
9923 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
9924 return FALSE;
9925 break;
9926 case aarch64_stub_long_branch:
9927 if (!elfNN_aarch64_output_stub_sym
9928 (osi, stub_name, addr, sizeof (aarch64_long_branch_stub)))
9929 return FALSE;
9930 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
9931 return FALSE;
9932 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_DATA, addr + 16))
9933 return FALSE;
9934 break;
9935 case aarch64_stub_erratum_835769_veneer:
9936 if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
9937 sizeof (aarch64_erratum_835769_stub)))
9938 return FALSE;
9939 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
9940 return FALSE;
9941 break;
9942 case aarch64_stub_erratum_843419_veneer:
9943 if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
9944 sizeof (aarch64_erratum_843419_stub)))
9945 return FALSE;
9946 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
9947 return FALSE;
9948 break;
9949 case aarch64_stub_branch_c64:
9950 if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
9951 sizeof (aarch64_c64_branch_stub)))
9952 return FALSE;
9953 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_C64, addr))
9954 return FALSE;
9955 break;
9956 case c64_stub_branch_aarch64:
9957 case c64_stub_branch_c64:
9958 if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
9959 sizeof (c64_aarch64_branch_stub)))
9960 return FALSE;
9961 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_C64, addr))
9962 return FALSE;
9963 break;
9964 case aarch64_stub_none:
9965 break;
9966
9967 default:
9968 abort ();
9969 }
9970
9971 return TRUE;
9972 }
9973
9974 /* Output mapping symbols for linker generated sections. */
9975
9976 static bfd_boolean
9977 elfNN_aarch64_output_arch_local_syms (bfd *output_bfd,
9978 struct bfd_link_info *info,
9979 void *finfo,
9980 int (*func) (void *, const char *,
9981 Elf_Internal_Sym *,
9982 asection *,
9983 struct elf_link_hash_entry
9984 *))
9985 {
9986 output_arch_syminfo osi;
9987 struct elf_aarch64_link_hash_table *htab;
9988
9989 htab = elf_aarch64_hash_table (info);
9990
9991 osi.finfo = finfo;
9992 osi.info = info;
9993 osi.func = func;
9994
9995 /* Long calls stubs. */
9996 if (htab->stub_bfd && htab->stub_bfd->sections)
9997 {
9998 asection *stub_sec;
9999
10000 for (stub_sec = htab->stub_bfd->sections;
10001 stub_sec != NULL; stub_sec = stub_sec->next)
10002 {
10003 /* Ignore non-stub sections. */
10004 if (!strstr (stub_sec->name, STUB_SUFFIX))
10005 continue;
10006
10007 osi.sec = stub_sec;
10008
10009 osi.sec_shndx = _bfd_elf_section_from_bfd_section
10010 (output_bfd, osi.sec->output_section);
10011
10012 /* The first instruction in a stub is always a branch. */
10013 if (!elfNN_aarch64_output_map_sym (&osi, AARCH64_MAP_INSN, 0))
10014 return FALSE;
10015
10016 bfd_hash_traverse (&htab->stub_hash_table, aarch64_map_one_stub,
10017 &osi);
10018 }
10019 }
10020
10021 /* Finally, output mapping symbols for the PLT. */
10022 if (!htab->root.splt || htab->root.splt->size == 0)
10023 return TRUE;
10024
10025 osi.sec_shndx = _bfd_elf_section_from_bfd_section
10026 (output_bfd, htab->root.splt->output_section);
10027 osi.sec = htab->root.splt;
10028
10029 elfNN_aarch64_output_map_sym (&osi, (htab->c64_rel ? AARCH64_MAP_C64
10030 : AARCH64_MAP_INSN), 0);
10031
10032 return TRUE;
10033
10034 }
10035
10036 /* Allocate target specific section data. */
10037
10038 static bfd_boolean
10039 elfNN_aarch64_new_section_hook (bfd *abfd, asection *sec)
10040 {
10041 if (!sec->used_by_bfd)
10042 {
10043 _aarch64_elf_section_data *sdata;
10044 size_t amt = sizeof (*sdata);
10045
10046 sdata = bfd_zalloc (abfd, amt);
10047 if (sdata == NULL)
10048 return FALSE;
10049 sdata->elf.is_target_section_data = TRUE;
10050 sec->used_by_bfd = sdata;
10051 }
10052
10053 return _bfd_elf_new_section_hook (abfd, sec);
10054 }
10055
10056
10057 /* Create dynamic sections. This is different from the ARM backend in that
10058 the got, plt, gotplt and their relocation sections are all created in the
10059 standard part of the bfd elf backend. */
10060
10061 static bfd_boolean
10062 elfNN_aarch64_create_dynamic_sections (bfd *dynobj,
10063 struct bfd_link_info *info)
10064 {
10065 /* We need to create .got section. */
10066 if (!aarch64_elf_create_got_section (dynobj, info))
10067 return FALSE;
10068
10069 return _bfd_elf_create_dynamic_sections (dynobj, info);
10070 }
10071
10072
10073 /* Allocate space in .plt, .got and associated reloc sections for
10074 dynamic relocs. */
10075
10076 static bfd_boolean
10077 elfNN_aarch64_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
10078 {
10079 struct bfd_link_info *info;
10080 struct elf_aarch64_link_hash_table *htab;
10081 struct elf_aarch64_link_hash_entry *eh;
10082 struct elf_dyn_relocs *p;
10083
10084 /* An example of a bfd_link_hash_indirect symbol is versioned
10085 symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
10086 -> __gxx_personality_v0(bfd_link_hash_defined)
10087
10088 There is no need to process bfd_link_hash_indirect symbols here
10089 because we will also be presented with the concrete instance of
10090 the symbol and elfNN_aarch64_copy_indirect_symbol () will have been
10091 called to copy all relevant data from the generic to the concrete
10092 symbol instance. */
10093 if (h->root.type == bfd_link_hash_indirect)
10094 return TRUE;
10095
10096 if (h->root.type == bfd_link_hash_warning)
10097 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10098
10099 info = (struct bfd_link_info *) inf;
10100 htab = elf_aarch64_hash_table (info);
10101
10102 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
10103 here if it is defined and referenced in a non-shared object. */
10104 if (h->type == STT_GNU_IFUNC
10105 && h->def_regular)
10106 return TRUE;
10107 else if (htab->root.dynamic_sections_created && h->plt.refcount > 0)
10108 {
10109 /* Make sure this symbol is output as a dynamic symbol.
10110 Undefined weak syms won't yet be marked as dynamic. */
10111 if (h->dynindx == -1 && !h->forced_local
10112 && h->root.type == bfd_link_hash_undefweak)
10113 {
10114 if (!bfd_elf_link_record_dynamic_symbol (info, h))
10115 return FALSE;
10116 }
10117
10118 if (bfd_link_pic (info) || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
10119 {
10120 asection *s = htab->root.splt;
10121
10122 /* If this is the first .plt entry, make room for the special
10123 first entry. */
10124 if (s->size == 0)
10125 s->size += htab->plt_header_size;
10126
10127 h->plt.offset = s->size;
10128
10129 /* If this symbol is not defined in a regular file, and we are
10130 not generating a shared library, then set the symbol to this
10131 location in the .plt. This is required to make function
10132 pointers compare as equal between the normal executable and
10133 the shared library. */
10134 if (!bfd_link_pic (info) && !h->def_regular)
10135 {
10136 h->root.u.def.section = s;
10137 h->root.u.def.value = h->plt.offset;
10138 }
10139
10140 /* Make room for this entry. For now we only create the
10141 small model PLT entries. We later need to find a way
10142 of relaxing into these from the large model PLT entries. */
10143 s->size += htab->plt_entry_size;
10144
10145 /* We also need to make an entry in the .got.plt section, which
10146 will be placed in the .got section by the linker script. */
10147 htab->root.sgotplt->size += GOT_ENTRY_SIZE (htab);
10148
10149 /* We also need to make an entry in the .rela.plt section. */
10150 htab->root.srelplt->size += RELOC_SIZE (htab);
10151
10152 /* We need to ensure that all GOT entries that serve the PLT
10153 are consecutive with the special GOT slots [0] [1] and
10154 [2]. Any addtional relocations, such as
10155 R_AARCH64_TLSDESC, must be placed after the PLT related
10156 entries. We abuse the reloc_count such that during
10157 sizing we adjust reloc_count to indicate the number of
10158 PLT related reserved entries. In subsequent phases when
10159 filling in the contents of the reloc entries, PLT related
10160 entries are placed by computing their PLT index (0
10161 .. reloc_count). While other none PLT relocs are placed
10162 at the slot indicated by reloc_count and reloc_count is
10163 updated. */
10164
10165 htab->root.srelplt->reloc_count++;
10166
10167 /* Mark the DSO in case R_<CLS>_JUMP_SLOT relocs against
10168 variant PCS symbols are present. */
10169 if (h->other & STO_AARCH64_VARIANT_PCS)
10170 htab->variant_pcs = 1;
10171
10172 }
10173 else
10174 {
10175 h->plt.offset = (bfd_vma) - 1;
10176 h->needs_plt = 0;
10177 }
10178 }
10179 else
10180 {
10181 h->plt.offset = (bfd_vma) - 1;
10182 h->needs_plt = 0;
10183 }
10184
10185 eh = (struct elf_aarch64_link_hash_entry *) h;
10186 eh->tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
10187
10188 if (h->got.refcount > 0)
10189 {
10190 bfd_boolean dyn;
10191 unsigned got_type = elf_aarch64_hash_entry (h)->got_type;
10192
10193 h->got.offset = (bfd_vma) - 1;
10194
10195 dyn = htab->root.dynamic_sections_created;
10196
10197 /* Make sure this symbol is output as a dynamic symbol.
10198 Undefined weak syms won't yet be marked as dynamic. */
10199 if (dyn && h->dynindx == -1 && !h->forced_local
10200 && h->root.type == bfd_link_hash_undefweak)
10201 {
10202 if (!bfd_elf_link_record_dynamic_symbol (info, h))
10203 return FALSE;
10204 }
10205
10206 if (got_type == GOT_UNKNOWN)
10207 {
10208 }
10209 else if (got_type == GOT_NORMAL
10210 || got_type == GOT_CAP)
10211 {
10212 h->got.offset = htab->root.sgot->size;
10213 htab->root.sgot->size += GOT_ENTRY_SIZE (htab);
10214 if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10215 || h->root.type != bfd_link_hash_undefweak)
10216 && (bfd_link_pic (info)
10217 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
10218 /* Undefined weak symbol in static PIE resolves to 0 without
10219 any dynamic relocations. */
10220 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
10221 {
10222 htab->root.srelgot->size += RELOC_SIZE (htab);
10223 }
10224 else if (bfd_link_executable (info) && !bfd_link_pic (info))
10225 htab->srelcaps->size += RELOC_SIZE (htab);
10226 }
10227 else
10228 {
10229 int indx;
10230 if (got_type & GOT_TLSDESC_GD)
10231 {
10232 eh->tlsdesc_got_jump_table_offset =
10233 (htab->root.sgotplt->size
10234 - aarch64_compute_jump_table_size (htab));
10235 htab->root.sgotplt->size += GOT_ENTRY_SIZE (htab) * 2;
10236 h->got.offset = (bfd_vma) - 2;
10237 }
10238
10239 if (got_type & GOT_TLS_GD)
10240 {
10241 h->got.offset = htab->root.sgot->size;
10242 htab->root.sgot->size += GOT_ENTRY_SIZE (htab) * 2;
10243 }
10244
10245 if (got_type & GOT_TLS_IE)
10246 {
10247 h->got.offset = htab->root.sgot->size;
10248 htab->root.sgot->size += GOT_ENTRY_SIZE (htab);
10249 }
10250
10251 indx = h && h->dynindx != -1 ? h->dynindx : 0;
10252 if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10253 || h->root.type != bfd_link_hash_undefweak)
10254 && (!bfd_link_executable (info)
10255 || indx != 0
10256 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)
10257 /* On Morello support only TLSDESC_GD to TLSLE relaxation;
10258 for everything else we must emit a dynamic relocation. */
10259 || got_type & GOT_CAP))
10260 {
10261 if (got_type & GOT_TLSDESC_GD)
10262 {
10263 htab->root.srelplt->size += RELOC_SIZE (htab);
10264 /* Note reloc_count not incremented here! We have
10265 already adjusted reloc_count for this relocation
10266 type. */
10267
10268 /* TLSDESC PLT is now needed, but not yet determined. */
10269 htab->root.tlsdesc_plt = (bfd_vma) - 1;
10270 }
10271
10272 if (got_type & GOT_TLS_GD)
10273 htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
10274
10275 if (got_type & GOT_TLS_IE)
10276 htab->root.srelgot->size += RELOC_SIZE (htab);
10277 }
10278 }
10279 }
10280 else
10281 {
10282 h->got.offset = (bfd_vma) - 1;
10283 }
10284
10285 if (h->dyn_relocs == NULL)
10286 return TRUE;
10287
10288 /* In the shared -Bsymbolic case, discard space allocated for
10289 dynamic pc-relative relocs against symbols which turn out to be
10290 defined in regular objects. For the normal shared case, discard
10291 space for pc-relative relocs that have become local due to symbol
10292 visibility changes. */
10293
10294 if (bfd_link_pic (info))
10295 {
10296 /* Relocs that use pc_count are those that appear on a call
10297 insn, or certain REL relocs that can generated via assembly.
10298 We want calls to protected symbols to resolve directly to the
10299 function rather than going via the plt. If people want
10300 function pointer comparisons to work as expected then they
10301 should avoid writing weird assembly. */
10302 if (SYMBOL_CALLS_LOCAL (info, h))
10303 {
10304 struct elf_dyn_relocs **pp;
10305
10306 for (pp = &h->dyn_relocs; (p = *pp) != NULL;)
10307 {
10308 p->count -= p->pc_count;
10309 p->pc_count = 0;
10310 if (p->count == 0)
10311 *pp = p->next;
10312 else
10313 pp = &p->next;
10314 }
10315 }
10316
10317 /* Also discard relocs on undefined weak syms with non-default
10318 visibility. */
10319 if (h->dyn_relocs != NULL && h->root.type == bfd_link_hash_undefweak)
10320 {
10321 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
10322 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
10323 h->dyn_relocs = NULL;
10324
10325 /* Make sure undefined weak symbols are output as a dynamic
10326 symbol in PIEs. */
10327 else if (h->dynindx == -1
10328 && !h->forced_local
10329 && h->root.type == bfd_link_hash_undefweak
10330 && !bfd_elf_link_record_dynamic_symbol (info, h))
10331 return FALSE;
10332 }
10333
10334 }
10335 else if (ELIMINATE_COPY_RELOCS)
10336 {
10337 /* For the non-shared case, discard space for relocs against
10338 symbols which turn out to need copy relocs or are not
10339 dynamic. */
10340
10341 if (!h->non_got_ref
10342 && ((h->def_dynamic
10343 && !h->def_regular)
10344 || (htab->root.dynamic_sections_created
10345 && (h->root.type == bfd_link_hash_undefweak
10346 || h->root.type == bfd_link_hash_undefined))))
10347 {
10348 /* Make sure this symbol is output as a dynamic symbol.
10349 Undefined weak syms won't yet be marked as dynamic. */
10350 if (h->dynindx == -1
10351 && !h->forced_local
10352 && h->root.type == bfd_link_hash_undefweak
10353 && !bfd_elf_link_record_dynamic_symbol (info, h))
10354 return FALSE;
10355
10356 /* If that succeeded, we know we'll be keeping all the
10357 relocs. */
10358 if (h->dynindx != -1)
10359 goto keep;
10360 }
10361
10362 h->dyn_relocs = NULL;
10363
10364 keep:;
10365 }
10366
10367 /* Finally, allocate space. */
10368 for (p = h->dyn_relocs; p != NULL; p = p->next)
10369 {
10370 asection *sreloc;
10371
10372 sreloc = elf_section_data (p->sec)->sreloc;
10373
10374 BFD_ASSERT (sreloc != NULL);
10375
10376 sreloc->size += p->count * RELOC_SIZE (htab);
10377 }
10378
10379 return TRUE;
10380 }
10381
10382 /* Allocate space in .plt, .got and associated reloc sections for
10383 ifunc dynamic relocs. */
10384
10385 static bfd_boolean
10386 elfNN_aarch64_allocate_ifunc_dynrelocs (struct elf_link_hash_entry *h,
10387 void *inf)
10388 {
10389 struct bfd_link_info *info;
10390 struct elf_aarch64_link_hash_table *htab;
10391
10392 /* An example of a bfd_link_hash_indirect symbol is versioned
10393 symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
10394 -> __gxx_personality_v0(bfd_link_hash_defined)
10395
10396 There is no need to process bfd_link_hash_indirect symbols here
10397 because we will also be presented with the concrete instance of
10398 the symbol and elfNN_aarch64_copy_indirect_symbol () will have been
10399 called to copy all relevant data from the generic to the concrete
10400 symbol instance. */
10401 if (h->root.type == bfd_link_hash_indirect)
10402 return TRUE;
10403
10404 if (h->root.type == bfd_link_hash_warning)
10405 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10406
10407 info = (struct bfd_link_info *) inf;
10408 htab = elf_aarch64_hash_table (info);
10409
10410 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
10411 here if it is defined and referenced in a non-shared object. */
10412 if (h->type == STT_GNU_IFUNC
10413 && h->def_regular)
10414 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
10415 &h->dyn_relocs,
10416 htab->plt_entry_size,
10417 htab->plt_header_size,
10418 GOT_ENTRY_SIZE (htab),
10419 FALSE);
10420 return TRUE;
10421 }
10422
10423 /* Allocate space in .plt, .got and associated reloc sections for
10424 local ifunc dynamic relocs. */
10425
10426 static bfd_boolean
10427 elfNN_aarch64_allocate_local_ifunc_dynrelocs (void **slot, void *inf)
10428 {
10429 struct elf_link_hash_entry *h
10430 = (struct elf_link_hash_entry *) *slot;
10431
10432 if (h->type != STT_GNU_IFUNC
10433 || !h->def_regular
10434 || !h->ref_regular
10435 || !h->forced_local
10436 || h->root.type != bfd_link_hash_defined)
10437 abort ();
10438
10439 return elfNN_aarch64_allocate_ifunc_dynrelocs (h, inf);
10440 }
10441
10442 /* This is the most important function of all . Innocuosly named
10443 though ! */
10444
10445 static bfd_boolean
10446 elfNN_aarch64_size_dynamic_sections (bfd *output_bfd,
10447 struct bfd_link_info *info)
10448 {
10449 struct elf_aarch64_link_hash_table *htab;
10450 bfd *dynobj;
10451 asection *s;
10452 bfd_boolean relocs;
10453 bfd *ibfd;
10454
10455 htab = elf_aarch64_hash_table ((info));
10456 dynobj = htab->root.dynobj;
10457
10458 BFD_ASSERT (dynobj != NULL);
10459
10460 if (htab->root.dynamic_sections_created)
10461 {
10462 if (bfd_link_executable (info) && !info->nointerp)
10463 {
10464 s = bfd_get_linker_section (dynobj, ".interp");
10465 if (s == NULL)
10466 abort ();
10467 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10468 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10469 }
10470 }
10471
10472 aarch64_elf_init_got_section (output_bfd, info);
10473
10474 setup_plt_values (info, elf_aarch64_tdata (output_bfd)->plt_type);
10475
10476 /* Set up .got offsets for local syms, and space for local dynamic
10477 relocs. */
10478 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10479 {
10480 struct elf_aarch64_local_symbol *locals = NULL;
10481 Elf_Internal_Shdr *symtab_hdr;
10482 asection *srel;
10483 unsigned int i;
10484
10485 if (!is_aarch64_elf (ibfd))
10486 continue;
10487
10488 for (s = ibfd->sections; s != NULL; s = s->next)
10489 {
10490 struct elf_dyn_relocs *p;
10491
10492 for (p = (struct elf_dyn_relocs *)
10493 (elf_section_data (s)->local_dynrel); p != NULL; p = p->next)
10494 {
10495 if (!bfd_is_abs_section (p->sec)
10496 && bfd_is_abs_section (p->sec->output_section))
10497 {
10498 /* Input section has been discarded, either because
10499 it is a copy of a linkonce section or due to
10500 linker script /DISCARD/, so we'll be discarding
10501 the relocs too. */
10502 }
10503 else if (p->count != 0)
10504 {
10505 srel = elf_section_data (p->sec)->sreloc;
10506 srel->size += p->count * RELOC_SIZE (htab);
10507 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10508 info->flags |= DF_TEXTREL;
10509 }
10510 }
10511 }
10512
10513 locals = elf_aarch64_locals (ibfd);
10514 if (!locals)
10515 continue;
10516
10517 symtab_hdr = &elf_symtab_hdr (ibfd);
10518 srel = htab->root.srelgot;
10519 for (i = 0; i < symtab_hdr->sh_info; i++)
10520 {
10521 locals[i].got_offset = (bfd_vma) - 1;
10522 locals[i].tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
10523 if (locals[i].got_refcount > 0)
10524 {
10525 unsigned got_type = locals[i].got_type;
10526 if (got_type & GOT_TLSDESC_GD)
10527 {
10528 locals[i].tlsdesc_got_jump_table_offset =
10529 (htab->root.sgotplt->size
10530 - aarch64_compute_jump_table_size (htab));
10531 htab->root.sgotplt->size += GOT_ENTRY_SIZE (htab) * 2;
10532 locals[i].got_offset = (bfd_vma) - 2;
10533 }
10534
10535 if (got_type & GOT_TLS_GD)
10536 {
10537 locals[i].got_offset = htab->root.sgot->size;
10538 htab->root.sgot->size += GOT_ENTRY_SIZE (htab) * 2;
10539 }
10540
10541 if (got_type & GOT_TLS_IE
10542 || got_type & GOT_NORMAL
10543 || got_type & GOT_CAP)
10544 {
10545 locals[i].got_offset = htab->root.sgot->size;
10546 htab->root.sgot->size += GOT_ENTRY_SIZE (htab);
10547 }
10548
10549 if (got_type == GOT_UNKNOWN)
10550 {
10551 }
10552
10553 if (bfd_link_pic (info))
10554 {
10555 if (got_type & GOT_TLSDESC_GD)
10556 {
10557 htab->root.srelplt->size += RELOC_SIZE (htab);
10558 /* Note RELOC_COUNT not incremented here! */
10559 htab->root.tlsdesc_plt = (bfd_vma) - 1;
10560 }
10561
10562 if (got_type & GOT_TLS_GD)
10563 htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
10564
10565 if (got_type & GOT_TLS_IE
10566 || got_type & GOT_NORMAL
10567 || got_type & GOT_CAP)
10568 htab->root.srelgot->size += RELOC_SIZE (htab);
10569 }
10570 /* Static binary; put relocs into srelcaps. */
10571 else if (bfd_link_executable (info) && (got_type & GOT_CAP))
10572 htab->srelcaps->size += RELOC_SIZE (htab);
10573 }
10574 else
10575 {
10576 locals[i].got_refcount = (bfd_vma) - 1;
10577 }
10578 }
10579 }
10580
10581
10582 /* Allocate global sym .plt and .got entries, and space for global
10583 sym dynamic relocs. */
10584 elf_link_hash_traverse (&htab->root, elfNN_aarch64_allocate_dynrelocs,
10585 info);
10586
10587 /* Allocate global ifunc sym .plt and .got entries, and space for global
10588 ifunc sym dynamic relocs. */
10589 elf_link_hash_traverse (&htab->root, elfNN_aarch64_allocate_ifunc_dynrelocs,
10590 info);
10591
10592 /* Allocate .plt and .got entries, and space for local ifunc symbols. */
10593 htab_traverse (htab->loc_hash_table,
10594 elfNN_aarch64_allocate_local_ifunc_dynrelocs,
10595 info);
10596
10597 if (bfd_link_executable (info)
10598 && !bfd_link_pic (info)
10599 && htab->srelcaps
10600 && htab->srelcaps->size > 0)
10601 {
10602 struct elf_link_hash_entry *h;
10603
10604 h = _bfd_elf_define_linkage_sym (output_bfd, info,
10605 htab->srelcaps,
10606 "__rela_dyn_start");
10607 h = _bfd_elf_define_linkage_sym (output_bfd, info,
10608 htab->srelcaps,
10609 "__rela_dyn_end");
10610
10611 h->root.u.def.value = htab->srelcaps->vma + htab->srelcaps->size;
10612 }
10613
10614 /* For every jump slot reserved in the sgotplt, reloc_count is
10615 incremented. However, when we reserve space for TLS descriptors,
10616 it's not incremented, so in order to compute the space reserved
10617 for them, it suffices to multiply the reloc count by the jump
10618 slot size. */
10619
10620 if (htab->root.srelplt)
10621 htab->sgotplt_jump_table_size = aarch64_compute_jump_table_size (htab);
10622
10623 if (htab->root.tlsdesc_plt)
10624 {
10625 if (htab->root.splt->size == 0)
10626 htab->root.splt->size += htab->plt_header_size;
10627
10628 /* If we're not using lazy TLS relocations, don't generate the
10629 GOT and PLT entry required. */
10630 if ((info->flags & DF_BIND_NOW))
10631 htab->root.tlsdesc_plt = 0;
10632 else
10633 {
10634 htab->root.tlsdesc_plt = htab->root.splt->size;
10635 htab->root.splt->size += htab->tlsdesc_plt_entry_size;
10636
10637 htab->root.tlsdesc_got = htab->root.sgot->size;
10638 htab->root.sgot->size += GOT_ENTRY_SIZE (htab);
10639 }
10640 }
10641
10642 /* Init mapping symbols information to use later to distingush between
10643 code and data while scanning for errata. */
10644 if (htab->fix_erratum_835769 || htab->fix_erratum_843419)
10645 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10646 {
10647 if (!is_aarch64_elf (ibfd))
10648 continue;
10649 bfd_elfNN_aarch64_init_maps (ibfd, info);
10650 }
10651
10652 /* We now have determined the sizes of the various dynamic sections.
10653 Allocate memory for them. */
10654 relocs = FALSE;
10655 for (s = dynobj->sections; s != NULL; s = s->next)
10656 {
10657 if ((s->flags & SEC_LINKER_CREATED) == 0)
10658 continue;
10659
10660 if (s == htab->root.splt
10661 || s == htab->root.sgot
10662 || s == htab->root.sgotplt
10663 || s == htab->root.iplt
10664 || s == htab->root.igotplt
10665 || s == htab->root.sdynbss
10666 || s == htab->root.sdynrelro)
10667 {
10668 /* Strip this section if we don't need it; see the
10669 comment below. */
10670 }
10671 else if (CONST_STRNEQ (bfd_section_name (s), ".rela"))
10672 {
10673 if (s->size != 0 && s != htab->root.srelplt)
10674 relocs = TRUE;
10675
10676 /* We use the reloc_count field as a counter if we need
10677 to copy relocs into the output file. */
10678 if (s != htab->root.srelplt)
10679 s->reloc_count = 0;
10680 }
10681 else
10682 {
10683 /* It's not one of our sections, so don't allocate space. */
10684 continue;
10685 }
10686
10687 if (s->size == 0)
10688 {
10689 /* If we don't need this section, strip it from the
10690 output file. This is mostly to handle .rela.bss and
10691 .rela.plt. We must create both sections in
10692 create_dynamic_sections, because they must be created
10693 before the linker maps input sections to output
10694 sections. The linker does that before
10695 adjust_dynamic_symbol is called, and it is that
10696 function which decides whether anything needs to go
10697 into these sections. */
10698 s->flags |= SEC_EXCLUDE;
10699 continue;
10700 }
10701
10702 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10703 continue;
10704
10705 /* Allocate memory for the section contents. We use bfd_zalloc
10706 here in case unused entries are not reclaimed before the
10707 section's contents are written out. This should not happen,
10708 but this way if it does, we get a R_AARCH64_NONE reloc instead
10709 of garbage. */
10710 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
10711 if (s->contents == NULL)
10712 return FALSE;
10713 }
10714
10715 if (htab->root.dynamic_sections_created)
10716 {
10717 /* Add some entries to the .dynamic section. We fill in the
10718 values later, in elfNN_aarch64_finish_dynamic_sections, but we
10719 must add the entries now so that we get the correct size for
10720 the .dynamic section. The DT_DEBUG entry is filled in by the
10721 dynamic linker and used by the debugger. */
10722 #define add_dynamic_entry(TAG, VAL) \
10723 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10724
10725 if (!_bfd_elf_add_dynamic_tags (output_bfd, info, relocs))
10726 return FALSE;
10727
10728 if (htab->root.splt->size != 0)
10729 {
10730 if (htab->variant_pcs
10731 && !add_dynamic_entry (DT_AARCH64_VARIANT_PCS, 0))
10732 return FALSE;
10733
10734 if ((elf_aarch64_tdata (output_bfd)->plt_type == PLT_BTI_PAC)
10735 && (!add_dynamic_entry (DT_AARCH64_BTI_PLT, 0)
10736 || !add_dynamic_entry (DT_AARCH64_PAC_PLT, 0)))
10737 return FALSE;
10738
10739 else if ((elf_aarch64_tdata (output_bfd)->plt_type == PLT_BTI)
10740 && !add_dynamic_entry (DT_AARCH64_BTI_PLT, 0))
10741 return FALSE;
10742
10743 else if ((elf_aarch64_tdata (output_bfd)->plt_type == PLT_PAC)
10744 && !add_dynamic_entry (DT_AARCH64_PAC_PLT, 0))
10745 return FALSE;
10746 }
10747 }
10748 #undef add_dynamic_entry
10749
10750 return TRUE;
10751 }
10752
10753 static inline void
10754 elf_aarch64_update_plt_entry (bfd *output_bfd,
10755 bfd_reloc_code_real_type r_type,
10756 bfd_byte *plt_entry, bfd_vma value)
10757 {
10758 reloc_howto_type *howto = elfNN_aarch64_howto_from_bfd_reloc (r_type);
10759
10760 /* FIXME: We should check the return value from this function call. */
10761 (void) _bfd_aarch64_elf_put_addend (output_bfd, plt_entry, r_type, howto, value);
10762 }
10763
10764 static void
10765 aarch64_update_c64_plt_entry (bfd *output_bfd, bfd_byte *plt_entry,
10766 bfd_vma plt_base, bfd_vma plt_got_ent)
10767 {
10768 /* Fill in the top 20 bits for this: ADRP c16, PLT_GOT + n * 16.
10769 ADRP: ((PG(S+A)-PG(P)) >> 12) & 0xfffff */
10770 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_MORELLO_ADR_HI20_PCREL,
10771 plt_entry,
10772 PG (plt_got_ent) - PG (plt_base));
10773
10774 elf_aarch64_update_plt_entry (output_bfd,
10775 BFD_RELOC_AARCH64_LDST128_LO12,
10776 plt_entry + 4,
10777 PG_OFFSET (plt_got_ent));
10778
10779 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADD_LO12,
10780 plt_entry + 8,
10781 PG_OFFSET (plt_got_ent));
10782 }
10783
10784 static void
10785 elfNN_aarch64_create_small_pltn_entry (struct elf_link_hash_entry *h,
10786 struct elf_aarch64_link_hash_table
10787 *htab, bfd *output_bfd,
10788 struct bfd_link_info *info)
10789 {
10790 bfd_byte *plt_entry;
10791 bfd_vma plt_index;
10792 bfd_vma got_offset;
10793 bfd_vma gotplt_entry_address;
10794 bfd_vma plt_entry_address;
10795 Elf_Internal_Rela rela;
10796 bfd_byte *loc;
10797 asection *plt, *gotplt, *relplt;
10798
10799 /* When building a static executable, use .iplt, .igot.plt and
10800 .rela.iplt sections for STT_GNU_IFUNC symbols. */
10801 if (htab->root.splt != NULL)
10802 {
10803 plt = htab->root.splt;
10804 gotplt = htab->root.sgotplt;
10805 relplt = htab->root.srelplt;
10806 }
10807 else
10808 {
10809 plt = htab->root.iplt;
10810 gotplt = htab->root.igotplt;
10811 relplt = htab->root.irelplt;
10812 }
10813
10814 /* Get the index in the procedure linkage table which
10815 corresponds to this symbol. This is the index of this symbol
10816 in all the symbols for which we are making plt entries. The
10817 first entry in the procedure linkage table is reserved.
10818
10819 Get the offset into the .got table of the entry that
10820 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
10821 bytes. The first three are reserved for the dynamic linker.
10822
10823 For static executables, we don't reserve anything. */
10824
10825 if (plt == htab->root.splt)
10826 {
10827 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
10828 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE (htab);
10829 }
10830 else
10831 {
10832 plt_index = h->plt.offset / htab->plt_entry_size;
10833 got_offset = plt_index * GOT_ENTRY_SIZE (htab);
10834 }
10835
10836 plt_entry = plt->contents + h->plt.offset;
10837 plt_entry_address = plt->output_section->vma
10838 + plt->output_offset + h->plt.offset;
10839 gotplt_entry_address = gotplt->output_section->vma +
10840 gotplt->output_offset + got_offset;
10841
10842 /* Copy in the boiler-plate for the PLTn entry. */
10843 memcpy (plt_entry, htab->plt_entry, htab->plt_entry_size);
10844
10845 if (htab->c64_rel)
10846 aarch64_update_c64_plt_entry (output_bfd, plt_entry, plt_entry_address,
10847 gotplt_entry_address);
10848 else
10849 {
10850
10851 /* First instruction in BTI enabled PLT stub is a BTI
10852 instruction so skip it. */
10853 if (elf_aarch64_tdata (output_bfd)->plt_type & PLT_BTI
10854 && elf_elfheader (output_bfd)->e_type == ET_EXEC)
10855 plt_entry = plt_entry + 4;
10856
10857 /* Fill in the top 21 bits for this: ADRP x16, PLT_GOT + n * 8.
10858 ADRP: ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
10859 elf_aarch64_update_plt_entry (output_bfd,
10860 BFD_RELOC_AARCH64_ADR_HI21_PCREL,
10861 plt_entry,
10862 PG (gotplt_entry_address) -
10863 PG (plt_entry_address));
10864
10865 /* Fill in the lo12 bits for the load from the pltgot. */
10866 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_LDSTNN_LO12,
10867 plt_entry + 4,
10868 PG_OFFSET (gotplt_entry_address));
10869
10870 /* Fill in the lo12 bits for the add from the pltgot entry. */
10871 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADD_LO12,
10872 plt_entry + 8,
10873 PG_OFFSET (gotplt_entry_address));
10874 }
10875
10876 /* All the GOTPLT Entries are essentially initialized to PLT0. Set LSB if
10877 the PLT is C64. */
10878 bfd_vma plt0 = ((plt->output_section->vma + plt->output_offset)
10879 | htab->c64_rel);
10880 bfd_put_NN (output_bfd, plt0, gotplt->contents + got_offset);
10881
10882 rela.r_offset = gotplt_entry_address;
10883
10884 if (h->dynindx == -1
10885 || ((bfd_link_executable (info)
10886 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
10887 && h->def_regular
10888 && h->type == STT_GNU_IFUNC))
10889 {
10890 /* If an STT_GNU_IFUNC symbol is locally defined, generate
10891 R_AARCH64_IRELATIVE instead of R_AARCH64_JUMP_SLOT. */
10892 rela.r_info = (elf_aarch64_hash_entry (h)->got_type == GOT_CAP
10893 ? ELFNN_R_INFO (0, MORELLO_R (IRELATIVE))
10894 : ELFNN_R_INFO (0, AARCH64_R (IRELATIVE)));
10895 rela.r_addend = (h->root.u.def.value
10896 + h->root.u.def.section->output_section->vma
10897 + h->root.u.def.section->output_offset);
10898 }
10899 else
10900 {
10901 /* Fill in the entry in the .rela.plt section. */
10902 rela.r_info = (elf_aarch64_hash_entry (h)->got_type == GOT_CAP
10903 ? ELFNN_R_INFO (h->dynindx, MORELLO_R (JUMP_SLOT))
10904 : ELFNN_R_INFO (h->dynindx, AARCH64_R (JUMP_SLOT)));
10905 rela.r_addend = 0;
10906 }
10907
10908 /* Compute the relocation entry to used based on PLT index and do
10909 not adjust reloc_count. The reloc_count has already been adjusted
10910 to account for this entry. */
10911 loc = relplt->contents + plt_index * RELOC_SIZE (htab);
10912 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
10913 }
10914
10915 /* Size sections even though they're not dynamic. We use it to setup
10916 _TLS_MODULE_BASE_, if needed. */
10917
10918 static bfd_boolean
10919 elfNN_aarch64_always_size_sections (bfd *output_bfd,
10920 struct bfd_link_info *info)
10921 {
10922 asection *tls_sec;
10923
10924 if (bfd_link_relocatable (info))
10925 return TRUE;
10926
10927 tls_sec = elf_hash_table (info)->tls_sec;
10928
10929 if (tls_sec)
10930 {
10931 struct elf_link_hash_entry *tlsbase;
10932
10933 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
10934 "_TLS_MODULE_BASE_", TRUE, TRUE, FALSE);
10935
10936 if (tlsbase)
10937 {
10938 struct bfd_link_hash_entry *h = NULL;
10939 const struct elf_backend_data *bed =
10940 get_elf_backend_data (output_bfd);
10941
10942 if (!(_bfd_generic_link_add_one_symbol
10943 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
10944 tls_sec, 0, NULL, FALSE, bed->collect, &h)))
10945 return FALSE;
10946
10947 tlsbase->type = STT_TLS;
10948 tlsbase = (struct elf_link_hash_entry *) h;
10949 tlsbase->def_regular = 1;
10950 tlsbase->other = STV_HIDDEN;
10951 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
10952 }
10953 }
10954
10955 return TRUE;
10956 }
10957
10958 /* Finish up dynamic symbol handling. We set the contents of various
10959 dynamic sections here. */
10960
10961 static bfd_boolean
10962 elfNN_aarch64_finish_dynamic_symbol (bfd *output_bfd,
10963 struct bfd_link_info *info,
10964 struct elf_link_hash_entry *h,
10965 Elf_Internal_Sym *sym)
10966 {
10967 struct elf_aarch64_link_hash_table *htab;
10968 htab = elf_aarch64_hash_table (info);
10969
10970 if (h->plt.offset != (bfd_vma) - 1)
10971 {
10972 asection *plt, *gotplt, *relplt;
10973
10974 /* This symbol has an entry in the procedure linkage table. Set
10975 it up. */
10976
10977 /* When building a static executable, use .iplt, .igot.plt and
10978 .rela.iplt sections for STT_GNU_IFUNC symbols. */
10979 if (htab->root.splt != NULL)
10980 {
10981 plt = htab->root.splt;
10982 gotplt = htab->root.sgotplt;
10983 relplt = htab->root.srelplt;
10984 }
10985 else
10986 {
10987 plt = htab->root.iplt;
10988 gotplt = htab->root.igotplt;
10989 relplt = htab->root.irelplt;
10990 }
10991
10992 /* This symbol has an entry in the procedure linkage table. Set
10993 it up. */
10994 if ((h->dynindx == -1
10995 && !((h->forced_local || bfd_link_executable (info))
10996 && h->def_regular
10997 && h->type == STT_GNU_IFUNC))
10998 || plt == NULL
10999 || gotplt == NULL
11000 || relplt == NULL)
11001 return FALSE;
11002
11003 elfNN_aarch64_create_small_pltn_entry (h, htab, output_bfd, info);
11004 if (!h->def_regular)
11005 {
11006 /* Mark the symbol as undefined, rather than as defined in
11007 the .plt section. */
11008 sym->st_shndx = SHN_UNDEF;
11009 /* If the symbol is weak we need to clear the value.
11010 Otherwise, the PLT entry would provide a definition for
11011 the symbol even if the symbol wasn't defined anywhere,
11012 and so the symbol would never be NULL. Leave the value if
11013 there were any relocations where pointer equality matters
11014 (this is a clue for the dynamic linker, to make function
11015 pointer comparisons work between an application and shared
11016 library). */
11017 if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
11018 sym->st_value = 0;
11019 }
11020 }
11021
11022 bfd_boolean is_c64 = elf_aarch64_hash_entry (h)->got_type == GOT_CAP;
11023
11024 if (h->got.offset != (bfd_vma) - 1
11025 && (elf_aarch64_hash_entry (h)->got_type == GOT_NORMAL
11026 || elf_aarch64_hash_entry (h)->got_type == GOT_CAP)
11027 /* Undefined weak symbol in static PIE resolves to 0 without
11028 any dynamic relocations. */
11029 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
11030 {
11031 Elf_Internal_Rela rela;
11032 bfd_byte *loc;
11033
11034 /* This symbol has an entry in the global offset table. Set it
11035 up. */
11036 if (htab->root.sgot == NULL || htab->root.srelgot == NULL)
11037 abort ();
11038
11039 rela.r_offset = (htab->root.sgot->output_section->vma
11040 + htab->root.sgot->output_offset
11041 + (h->got.offset & ~(bfd_vma) 1));
11042
11043 if (h->def_regular
11044 && h->type == STT_GNU_IFUNC)
11045 {
11046 if (bfd_link_pic (info))
11047 {
11048 /* Generate R_AARCH64_GLOB_DAT. */
11049 goto do_glob_dat;
11050 }
11051 else
11052 {
11053 asection *plt;
11054
11055 if (!h->pointer_equality_needed)
11056 abort ();
11057
11058 /* For non-shared object, we can't use .got.plt, which
11059 contains the real function address if we need pointer
11060 equality. We load the GOT entry with the PLT entry. */
11061 plt = htab->root.splt ? htab->root.splt : htab->root.iplt;
11062 bfd_put_NN (output_bfd, (plt->output_section->vma
11063 + plt->output_offset
11064 + h->plt.offset),
11065 htab->root.sgot->contents
11066 + (h->got.offset & ~(bfd_vma) 1));
11067 return TRUE;
11068 }
11069 }
11070 else if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
11071 {
11072 if (!(h->def_regular || ELF_COMMON_DEF_P (h)))
11073 return FALSE;
11074
11075 BFD_ASSERT ((h->got.offset & 1) != 0);
11076 bfd_vma value = h->root.u.def.value
11077 + h->root.u.def.section->output_section->vma
11078 + h->root.u.def.section->output_offset;
11079 if (is_c64)
11080 {
11081 rela.r_info = ELFNN_R_INFO (0, MORELLO_R (RELATIVE));
11082 bfd_vma base_value = 0;
11083 if (c64_symbol_adjust (h, value, h->root.u.def.section, info,
11084 &base_value))
11085 rela.r_addend = (value | h->target_internal) - base_value;
11086 else
11087 rela.r_addend = 0;
11088 }
11089 else
11090 {
11091 rela.r_info = ELFNN_R_INFO (0, AARCH64_R (RELATIVE));
11092 rela.r_addend = value;
11093 }
11094 }
11095 else
11096 {
11097 do_glob_dat:
11098 BFD_ASSERT ((h->got.offset & 1) == 0);
11099 bfd_put_NN (output_bfd, (bfd_vma) 0,
11100 htab->root.sgot->contents + h->got.offset);
11101 rela.r_info = ELFNN_R_INFO (h->dynindx,
11102 (is_c64 ? MORELLO_R (GLOB_DAT)
11103 : AARCH64_R (GLOB_DAT)));
11104 rela.r_addend = 0;
11105 }
11106
11107 loc = htab->root.srelgot->contents;
11108 loc += htab->root.srelgot->reloc_count++ * RELOC_SIZE (htab);
11109 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
11110 }
11111
11112 if (h->needs_copy)
11113 {
11114 Elf_Internal_Rela rela;
11115 asection *s;
11116 bfd_byte *loc;
11117
11118 /* This symbol needs a copy reloc. Set it up. */
11119 if (h->dynindx == -1
11120 || (h->root.type != bfd_link_hash_defined
11121 && h->root.type != bfd_link_hash_defweak)
11122 || htab->root.srelbss == NULL)
11123 abort ();
11124
11125 rela.r_offset = (h->root.u.def.value
11126 + h->root.u.def.section->output_section->vma
11127 + h->root.u.def.section->output_offset);
11128 rela.r_info = ELFNN_R_INFO (h->dynindx, AARCH64_R (COPY));
11129 rela.r_addend = 0;
11130 if (h->root.u.def.section == htab->root.sdynrelro)
11131 s = htab->root.sreldynrelro;
11132 else
11133 s = htab->root.srelbss;
11134 loc = s->contents + s->reloc_count++ * RELOC_SIZE (htab);
11135 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
11136 }
11137
11138 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. SYM may
11139 be NULL for local symbols. */
11140 if (sym != NULL
11141 && (h == elf_hash_table (info)->hdynamic
11142 || h == elf_hash_table (info)->hgot))
11143 sym->st_shndx = SHN_ABS;
11144
11145 return TRUE;
11146 }
11147
11148 /* Finish up local dynamic symbol handling. We set the contents of
11149 various dynamic sections here. */
11150
11151 static bfd_boolean
11152 elfNN_aarch64_finish_local_dynamic_symbol (void **slot, void *inf)
11153 {
11154 struct elf_link_hash_entry *h
11155 = (struct elf_link_hash_entry *) *slot;
11156 struct bfd_link_info *info
11157 = (struct bfd_link_info *) inf;
11158
11159 return elfNN_aarch64_finish_dynamic_symbol (info->output_bfd,
11160 info, h, NULL);
11161 }
11162
11163 static void
11164 elfNN_aarch64_init_small_plt0_entry (bfd *output_bfd ATTRIBUTE_UNUSED,
11165 struct elf_aarch64_link_hash_table
11166 *htab)
11167 {
11168 /* Fill in PLT0. Fixme:RR Note this doesn't distinguish between
11169 small and large plts and at the minute just generates
11170 the small PLT. */
11171
11172 /* PLT0 of the small PLT looks like this in ELF64 -
11173 stp x16, x30, [sp, #-16]! // Save the reloc and lr on stack.
11174 adrp x16, PLT_GOT + 16 // Get the page base of the GOTPLT
11175 ldr x17, [x16, #:lo12:PLT_GOT+16] // Load the address of the
11176 // symbol resolver
11177 add x16, x16, #:lo12:PLT_GOT+16 // Load the lo12 bits of the
11178 // GOTPLT entry for this.
11179 br x17
11180 PLT0 will be slightly different in ELF32 due to different got entry
11181 size. */
11182 bfd_vma plt_got_2nd_ent; /* Address of GOT[2]. */
11183 bfd_vma plt_base;
11184
11185
11186 memcpy (htab->root.splt->contents, htab->plt0_entry,
11187 htab->plt_header_size);
11188
11189 /* PR 26312: Explicitly set the sh_entsize to 0 so that
11190 consumers do not think that the section contains fixed
11191 sized objects. */
11192 elf_section_data (htab->root.splt->output_section)->this_hdr.sh_entsize = 0;
11193
11194 plt_got_2nd_ent = (htab->root.sgotplt->output_section->vma
11195 + htab->root.sgotplt->output_offset
11196 + GOT_ENTRY_SIZE (htab) * 2);
11197
11198 plt_base = htab->root.splt->output_section->vma +
11199 htab->root.splt->output_offset;
11200
11201 bfd_byte *plt0_entry = htab->root.splt->contents;
11202
11203 if (htab->c64_rel)
11204 {
11205 aarch64_update_c64_plt_entry (output_bfd, plt0_entry + 4,
11206 plt_base + 4, plt_got_2nd_ent);
11207 return;
11208 }
11209
11210 /* First instruction in BTI enabled PLT stub is a BTI
11211 instruction so skip it. */
11212 if (elf_aarch64_tdata (output_bfd)->plt_type & PLT_BTI)
11213 plt0_entry = plt0_entry + 4;
11214
11215 /* Fill in the top 21 bits for this: ADRP x16, PLT_GOT + n * 8.
11216 ADRP: ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
11217 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADR_HI21_PCREL,
11218 plt0_entry + 4,
11219 PG (plt_got_2nd_ent) - PG (plt_base + 4));
11220
11221 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_LDSTNN_LO12,
11222 plt0_entry + 8,
11223 PG_OFFSET (plt_got_2nd_ent));
11224
11225 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADD_LO12,
11226 plt0_entry + 12,
11227 PG_OFFSET (plt_got_2nd_ent));
11228 }
11229
11230 static bfd_boolean
11231 elfNN_aarch64_finish_dynamic_sections (bfd *output_bfd,
11232 struct bfd_link_info *info)
11233 {
11234 struct elf_aarch64_link_hash_table *htab;
11235 bfd *dynobj;
11236 asection *sdyn;
11237
11238 htab = elf_aarch64_hash_table (info);
11239 dynobj = htab->root.dynobj;
11240 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
11241
11242 if (htab->root.dynamic_sections_created)
11243 {
11244 ElfNN_External_Dyn *dyncon, *dynconend;
11245
11246 if (sdyn == NULL || htab->root.sgot == NULL)
11247 abort ();
11248
11249 dyncon = (ElfNN_External_Dyn *) sdyn->contents;
11250 dynconend = (ElfNN_External_Dyn *) (sdyn->contents + sdyn->size);
11251 for (; dyncon < dynconend; dyncon++)
11252 {
11253 Elf_Internal_Dyn dyn;
11254 asection *s;
11255
11256 bfd_elfNN_swap_dyn_in (dynobj, dyncon, &dyn);
11257
11258 switch (dyn.d_tag)
11259 {
11260 default:
11261 continue;
11262
11263 case DT_PLTGOT:
11264 s = htab->root.sgotplt;
11265 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11266 break;
11267
11268 case DT_JMPREL:
11269 s = htab->root.srelplt;
11270 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11271 break;
11272
11273 case DT_PLTRELSZ:
11274 s = htab->root.srelplt;
11275 dyn.d_un.d_val = s->size;
11276 break;
11277
11278 case DT_TLSDESC_PLT:
11279 s = htab->root.splt;
11280 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
11281 + htab->root.tlsdesc_plt;
11282 break;
11283
11284 case DT_TLSDESC_GOT:
11285 s = htab->root.sgot;
11286 BFD_ASSERT (htab->root.tlsdesc_got != (bfd_vma)-1);
11287 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
11288 + htab->root.tlsdesc_got;
11289 break;
11290 }
11291
11292 bfd_elfNN_swap_dyn_out (output_bfd, &dyn, dyncon);
11293 }
11294
11295 }
11296
11297 /* Fill in the special first entry in the procedure linkage table. */
11298 if (htab->root.splt && htab->root.splt->size > 0)
11299 {
11300 elfNN_aarch64_init_small_plt0_entry (output_bfd, htab);
11301
11302 if (htab->root.tlsdesc_plt && !(info->flags & DF_BIND_NOW))
11303 {
11304 BFD_ASSERT (htab->root.tlsdesc_got != (bfd_vma)-1);
11305 bfd_put_NN (output_bfd, (bfd_vma) 0,
11306 htab->root.sgot->contents + htab->root.tlsdesc_got);
11307
11308 const bfd_byte *entry = elfNN_aarch64_tlsdesc_small_plt_entry;
11309 htab->tlsdesc_plt_entry_size = PLT_TLSDESC_ENTRY_SIZE;
11310
11311 unsigned adrp_rtype = BFD_RELOC_AARCH64_ADR_HI21_PCREL;
11312 unsigned ldr_rtype = BFD_RELOC_AARCH64_LDSTNN_LO12;
11313
11314 aarch64_plt_type type = elf_aarch64_tdata (output_bfd)->plt_type;
11315 if (htab->c64_rel)
11316 {
11317 entry = elfNN_aarch64_tlsdesc_small_plt_c64_entry;
11318 adrp_rtype = BFD_RELOC_MORELLO_ADR_HI20_PCREL;
11319 ldr_rtype = BFD_RELOC_AARCH64_LDST128_LO12;
11320 }
11321 else if (type == PLT_BTI || type == PLT_BTI_PAC)
11322 {
11323 entry = elfNN_aarch64_tlsdesc_small_plt_bti_entry;
11324 }
11325
11326 memcpy (htab->root.splt->contents + htab->root.tlsdesc_plt,
11327 entry, htab->tlsdesc_plt_entry_size);
11328
11329 {
11330 bfd_vma adrp1_addr =
11331 htab->root.splt->output_section->vma
11332 + htab->root.splt->output_offset
11333 + htab->root.tlsdesc_plt + 4;
11334
11335 bfd_vma adrp2_addr = adrp1_addr + 4;
11336
11337 bfd_vma got_addr =
11338 htab->root.sgot->output_section->vma
11339 + htab->root.sgot->output_offset;
11340
11341 bfd_vma pltgot_addr =
11342 htab->root.sgotplt->output_section->vma
11343 + htab->root.sgotplt->output_offset;
11344
11345 bfd_vma dt_tlsdesc_got = got_addr + htab->root.tlsdesc_got;
11346
11347 bfd_byte *plt_entry =
11348 htab->root.splt->contents + htab->root.tlsdesc_plt;
11349
11350 /* First instruction in BTI enabled PLT stub is a BTI
11351 instruction so skip it. */
11352 if (type & PLT_BTI)
11353 {
11354 plt_entry = plt_entry + 4;
11355 adrp1_addr = adrp1_addr + 4;
11356 adrp2_addr = adrp2_addr + 4;
11357 }
11358
11359 /* adrp x2, DT_TLSDESC_GOT */
11360 elf_aarch64_update_plt_entry (output_bfd,
11361 adrp_rtype,
11362 plt_entry + 4,
11363 (PG (dt_tlsdesc_got)
11364 - PG (adrp1_addr)));
11365
11366 /* adrp x3, 0 */
11367 elf_aarch64_update_plt_entry (output_bfd,
11368 adrp_rtype,
11369 plt_entry + 8,
11370 (PG (pltgot_addr)
11371 - PG (adrp2_addr)));
11372
11373 /* ldr x2, [x2, #0] */
11374 elf_aarch64_update_plt_entry (output_bfd,
11375 ldr_rtype,
11376 plt_entry + 12,
11377 PG_OFFSET (dt_tlsdesc_got));
11378
11379 /* add x3, x3, 0 */
11380 elf_aarch64_update_plt_entry (output_bfd,
11381 BFD_RELOC_AARCH64_ADD_LO12,
11382 plt_entry + 16,
11383 PG_OFFSET (pltgot_addr));
11384 }
11385 }
11386 }
11387
11388 if (htab->root.sgotplt)
11389 {
11390 if (bfd_is_abs_section (htab->root.sgotplt->output_section))
11391 {
11392 _bfd_error_handler
11393 (_("discarded output section: `%pA'"), htab->root.sgotplt);
11394 return FALSE;
11395 }
11396
11397 /* Fill in the first three entries in the global offset table. */
11398 if (htab->root.sgotplt->size > 0)
11399 {
11400 bfd_put_NN (output_bfd, (bfd_vma) 0, htab->root.sgotplt->contents);
11401
11402 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
11403 bfd_put_NN (output_bfd,
11404 (bfd_vma) 0,
11405 htab->root.sgotplt->contents + GOT_ENTRY_SIZE (htab));
11406 bfd_put_NN (output_bfd,
11407 (bfd_vma) 0,
11408 (htab->root.sgotplt->contents
11409 + GOT_ENTRY_SIZE (htab) * 2));
11410 }
11411
11412 if (htab->root.sgot)
11413 {
11414 if (htab->root.sgot->size > 0)
11415 {
11416 bfd_vma addr =
11417 sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0;
11418 bfd_put_NN (output_bfd, addr, htab->root.sgot->contents);
11419 }
11420 }
11421
11422 elf_section_data (htab->root.sgotplt->output_section)->
11423 this_hdr.sh_entsize = GOT_ENTRY_SIZE (htab);
11424 }
11425
11426 if (htab->root.sgot && htab->root.sgot->size > 0)
11427 elf_section_data (htab->root.sgot->output_section)->this_hdr.sh_entsize
11428 = GOT_ENTRY_SIZE (htab);
11429
11430 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
11431 htab_traverse (htab->loc_hash_table,
11432 elfNN_aarch64_finish_local_dynamic_symbol,
11433 info);
11434
11435 return TRUE;
11436 }
11437
11438 /* Check if BTI enabled PLTs are needed. Returns the type needed. */
11439 static aarch64_plt_type
11440 get_plt_type (bfd *abfd)
11441 {
11442 aarch64_plt_type ret = PLT_NORMAL;
11443 bfd_byte *contents, *extdyn, *extdynend;
11444 asection *sec = bfd_get_section_by_name (abfd, ".dynamic");
11445 if (!sec || !bfd_malloc_and_get_section (abfd, sec, &contents))
11446 return ret;
11447 extdyn = contents;
11448 extdynend = contents + sec->size;
11449 for (; extdyn < extdynend; extdyn += sizeof (ElfNN_External_Dyn))
11450 {
11451 Elf_Internal_Dyn dyn;
11452 bfd_elfNN_swap_dyn_in (abfd, extdyn, &dyn);
11453
11454 /* Let's check the processor specific dynamic array tags. */
11455 bfd_vma tag = dyn.d_tag;
11456 if (tag < DT_LOPROC || tag > DT_HIPROC)
11457 continue;
11458
11459 switch (tag)
11460 {
11461 case DT_AARCH64_BTI_PLT:
11462 ret |= PLT_BTI;
11463 break;
11464
11465 case DT_AARCH64_PAC_PLT:
11466 ret |= PLT_PAC;
11467 break;
11468
11469 default: break;
11470 }
11471 }
11472 free (contents);
11473 return ret;
11474 }
11475
11476 static long
11477 elfNN_aarch64_get_synthetic_symtab (bfd *abfd,
11478 long symcount,
11479 asymbol **syms,
11480 long dynsymcount,
11481 asymbol **dynsyms,
11482 asymbol **ret)
11483 {
11484 elf_aarch64_tdata (abfd)->plt_type = get_plt_type (abfd);
11485 return _bfd_elf_get_synthetic_symtab (abfd, symcount, syms,
11486 dynsymcount, dynsyms, ret);
11487 }
11488
11489 /* Return address for Ith PLT stub in section PLT, for relocation REL
11490 or (bfd_vma) -1 if it should not be included. */
11491
11492 static bfd_vma
11493 elfNN_aarch64_plt_sym_val (bfd_vma i, const asection *plt,
11494 const arelent *rel ATTRIBUTE_UNUSED)
11495 {
11496 size_t plt0_size = PLT_ENTRY_SIZE;
11497 size_t pltn_size = PLT_SMALL_ENTRY_SIZE;
11498
11499 if (elf_aarch64_tdata (plt->owner)->plt_type == PLT_BTI_PAC)
11500 {
11501 if (elf_elfheader (plt->owner)->e_type == ET_EXEC)
11502 pltn_size = PLT_BTI_PAC_SMALL_ENTRY_SIZE;
11503 else
11504 pltn_size = PLT_PAC_SMALL_ENTRY_SIZE;
11505 }
11506 else if (elf_aarch64_tdata (plt->owner)->plt_type == PLT_BTI)
11507 {
11508 if (elf_elfheader (plt->owner)->e_type == ET_EXEC)
11509 pltn_size = PLT_BTI_SMALL_ENTRY_SIZE;
11510 }
11511 else if (elf_aarch64_tdata (plt->owner)->plt_type == PLT_PAC)
11512 {
11513 pltn_size = PLT_PAC_SMALL_ENTRY_SIZE;
11514 }
11515
11516 return plt->vma + plt0_size + i * pltn_size;
11517 }
11518
11519 /* Returns TRUE if NAME is an AArch64 mapping symbol.
11520 The ARM ELF standard defines $x (for A64 code) and $d (for data).
11521 It also allows a period initiated suffix to be added to the symbol, ie:
11522 "$[adtx]\.[:sym_char]+". */
11523
11524 static bfd_boolean
11525 is_aarch64_mapping_symbol (const char * name)
11526 {
11527 return name != NULL /* Paranoia. */
11528 && name[0] == '$' /* Note: if objcopy --prefix-symbols has been used then
11529 the mapping symbols could have acquired a prefix.
11530 We do not support this here, since such symbols no
11531 longer conform to the ARM ELF ABI. */
11532 && (name[1] == 'd' || name[1] == 'x' || name[1] == 'c')
11533 && (name[2] == 0 || name[2] == '.');
11534 /* FIXME: Strictly speaking the symbol is only a valid mapping symbol if
11535 any characters that follow the period are legal characters for the body
11536 of a symbol's name. For now we just assume that this is the case. */
11537 }
11538
11539 /* Make sure that mapping symbols in object files are not removed via the
11540 "strip --strip-unneeded" tool. These symbols might needed in order to
11541 correctly generate linked files. Once an object file has been linked,
11542 it should be safe to remove them. */
11543
11544 static void
11545 elfNN_aarch64_backend_symbol_processing (bfd *abfd, asymbol *sym)
11546 {
11547 if (((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
11548 && sym->section != bfd_abs_section_ptr
11549 && is_aarch64_mapping_symbol (sym->name))
11550 sym->flags |= BSF_KEEP;
11551 }
11552
11553 /* Implement elf_backend_setup_gnu_properties for AArch64. It serves as a
11554 wrapper function for _bfd_aarch64_elf_link_setup_gnu_properties to account
11555 for the effect of GNU properties of the output_bfd. */
11556 static bfd *
11557 elfNN_aarch64_link_setup_gnu_properties (struct bfd_link_info *info)
11558 {
11559 uint32_t prop = elf_aarch64_tdata (info->output_bfd)->gnu_and_prop;
11560 bfd *pbfd = _bfd_aarch64_elf_link_setup_gnu_properties (info, &prop);
11561 elf_aarch64_tdata (info->output_bfd)->gnu_and_prop = prop;
11562 elf_aarch64_tdata (info->output_bfd)->plt_type
11563 |= (prop & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) ? PLT_BTI : 0;
11564 return pbfd;
11565 }
11566
11567 /* Implement elf_backend_merge_gnu_properties for AArch64. It serves as a
11568 wrapper function for _bfd_aarch64_elf_merge_gnu_properties to account
11569 for the effect of GNU properties of the output_bfd. */
11570 static bfd_boolean
11571 elfNN_aarch64_merge_gnu_properties (struct bfd_link_info *info,
11572 bfd *abfd, bfd *bbfd,
11573 elf_property *aprop,
11574 elf_property *bprop)
11575 {
11576 uint32_t prop
11577 = elf_aarch64_tdata (info->output_bfd)->gnu_and_prop;
11578
11579 /* If output has been marked with BTI using command line argument, give out
11580 warning if necessary. */
11581 /* Properties are merged per type, hence only check for warnings when merging
11582 GNU_PROPERTY_AARCH64_FEATURE_1_AND. */
11583 if (((aprop && aprop->pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
11584 || (bprop && bprop->pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND))
11585 && (prop & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
11586 && (!elf_aarch64_tdata (info->output_bfd)->no_bti_warn))
11587 {
11588 if ((aprop && !(aprop->u.number & GNU_PROPERTY_AARCH64_FEATURE_1_BTI))
11589 || !aprop)
11590 {
11591 _bfd_error_handler (_("%pB: warning: BTI turned on by -z force-bti when "
11592 "all inputs do not have BTI in NOTE section."),
11593 abfd);
11594 }
11595 if ((bprop && !(bprop->u.number & GNU_PROPERTY_AARCH64_FEATURE_1_BTI))
11596 || !bprop)
11597 {
11598 _bfd_error_handler (_("%pB: warning: BTI turned on by -z force-bti when "
11599 "all inputs do not have BTI in NOTE section."),
11600 bbfd);
11601 }
11602 }
11603
11604 return _bfd_aarch64_elf_merge_gnu_properties (info, abfd, aprop,
11605 bprop, prop);
11606 }
11607
11608 /* Demangle c64 function symbols as we read them in. */
11609
11610 static bfd_boolean
11611 aarch64_elfNN_swap_symbol_in (bfd * abfd,
11612 const void *psrc,
11613 const void *pshn,
11614 Elf_Internal_Sym *dst)
11615 {
11616 if (!bfd_elfNN_swap_symbol_in (abfd, psrc, pshn, dst))
11617 return FALSE;
11618
11619 dst->st_target_internal = 0;
11620
11621 if (ELF_ST_TYPE (dst->st_info) == STT_FUNC
11622 || ELF_ST_TYPE (dst->st_info) == STT_GNU_IFUNC)
11623 {
11624 dst->st_target_internal = dst->st_value & ST_BRANCH_TO_C64;
11625 dst->st_value &= ~(bfd_vma) ST_BRANCH_TO_C64;
11626 }
11627
11628 return TRUE;
11629 }
11630
11631
11632 /* Mangle c64 function symbols as we write them out. */
11633
11634 static void
11635 aarch64_elfNN_swap_symbol_out (bfd *abfd,
11636 const Elf_Internal_Sym *src,
11637 void *cdst,
11638 void *shndx)
11639 {
11640 Elf_Internal_Sym newsym = *src;
11641
11642 if ((ELF_ST_TYPE (newsym.st_info) == STT_FUNC
11643 || ELF_ST_TYPE (newsym.st_info) == STT_GNU_IFUNC)
11644 && newsym.st_shndx != SHN_UNDEF)
11645 newsym.st_value |= newsym.st_target_internal;
11646
11647 bfd_elfNN_swap_symbol_out (abfd, &newsym, cdst, shndx);
11648 }
11649
11650 /* Define the size of a GOT element for the generic mid-end. */
11651
11652 static bfd_vma
11653 elfNN_aarch64_got_elt_size (bfd *abfd ATTRIBUTE_UNUSED,
11654 struct bfd_link_info *info,
11655 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
11656 bfd *ibfd ATTRIBUTE_UNUSED,
11657 unsigned long symndx ATTRIBUTE_UNUSED)
11658 {
11659 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
11660
11661 return GOT_ENTRY_SIZE (htab);
11662 }
11663
11664 /* Define the size of a GOT header, which is the minimum size of the GOT section
11665 when one is needed. */
11666
11667 static bfd_vma
11668 elfNN_aarch64_got_header_size (struct bfd_link_info *info)
11669 {
11670 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
11671
11672 return GOT_ENTRY_SIZE (htab) * GOT_RESERVED_HEADER_SLOTS;
11673 }
11674
11675 /* Identify the 'C' in the CIE augmentation string. */
11676
11677 static bfd_boolean
11678 elf64_aarch64_eh_frame_augmentation_char (const char aug)
11679 {
11680 return aug == 'C';
11681 }
11682
11683 /* We use this so we can override certain functions
11684 (though currently we don't). */
11685
11686 const struct elf_size_info elfNN_aarch64_size_info =
11687 {
11688 sizeof (ElfNN_External_Ehdr),
11689 sizeof (ElfNN_External_Phdr),
11690 sizeof (ElfNN_External_Shdr),
11691 sizeof (ElfNN_External_Rel),
11692 sizeof (ElfNN_External_Rela),
11693 sizeof (ElfNN_External_Sym),
11694 sizeof (ElfNN_External_Dyn),
11695 sizeof (Elf_External_Note),
11696 4, /* Hash table entry size. */
11697 1, /* Internal relocs per external relocs. */
11698 ARCH_SIZE, /* Arch size. */
11699 LOG_FILE_ALIGN, /* Log_file_align. */
11700 ELFCLASSNN, EV_CURRENT,
11701 bfd_elfNN_write_out_phdrs,
11702 bfd_elfNN_write_shdrs_and_ehdr,
11703 bfd_elfNN_checksum_contents,
11704 bfd_elfNN_write_relocs,
11705 aarch64_elfNN_swap_symbol_in,
11706 aarch64_elfNN_swap_symbol_out,
11707 bfd_elfNN_slurp_reloc_table,
11708 bfd_elfNN_slurp_symbol_table,
11709 bfd_elfNN_swap_dyn_in,
11710 bfd_elfNN_swap_dyn_out,
11711 bfd_elfNN_swap_reloc_in,
11712 bfd_elfNN_swap_reloc_out,
11713 bfd_elfNN_swap_reloca_in,
11714 bfd_elfNN_swap_reloca_out
11715 };
11716
11717 #define ELF_ARCH bfd_arch_aarch64
11718 #define ELF_MACHINE_CODE EM_AARCH64
11719 #define ELF_MAXPAGESIZE 0x10000
11720 #define ELF_MINPAGESIZE 0x1000
11721 #define ELF_COMMONPAGESIZE 0x1000
11722
11723 #define bfd_elfNN_bfd_is_target_special_symbol \
11724 elfNN_aarch64_is_target_special_symbol
11725
11726 #define bfd_elfNN_bfd_link_hash_table_create \
11727 elfNN_aarch64_link_hash_table_create
11728
11729 #define bfd_elfNN_bfd_merge_private_bfd_data \
11730 elfNN_aarch64_merge_private_bfd_data
11731
11732 #define bfd_elfNN_bfd_print_private_bfd_data \
11733 elfNN_aarch64_print_private_bfd_data
11734
11735 #define bfd_elfNN_bfd_reloc_type_lookup \
11736 elfNN_aarch64_reloc_type_lookup
11737
11738 #define bfd_elfNN_bfd_reloc_name_lookup \
11739 elfNN_aarch64_reloc_name_lookup
11740
11741 #define bfd_elfNN_bfd_set_private_flags \
11742 elfNN_aarch64_set_private_flags
11743
11744 #define bfd_elfNN_find_inliner_info \
11745 elfNN_aarch64_find_inliner_info
11746
11747 #define bfd_elfNN_get_synthetic_symtab \
11748 elfNN_aarch64_get_synthetic_symtab
11749
11750 #define bfd_elfNN_mkobject \
11751 elfNN_aarch64_mkobject
11752
11753 #define bfd_elfNN_new_section_hook \
11754 elfNN_aarch64_new_section_hook
11755
11756 #define elf_backend_adjust_dynamic_symbol \
11757 elfNN_aarch64_adjust_dynamic_symbol
11758
11759 #define elf_backend_always_size_sections \
11760 elfNN_aarch64_always_size_sections
11761
11762 #define elf_backend_check_relocs \
11763 elfNN_aarch64_check_relocs
11764
11765 #define elf_backend_copy_indirect_symbol \
11766 elfNN_aarch64_copy_indirect_symbol
11767
11768 #define elf_backend_merge_symbol_attribute \
11769 elfNN_aarch64_merge_symbol_attribute
11770
11771 /* Create .dynbss, and .rela.bss sections in DYNOBJ, and set up shortcuts
11772 to them in our hash. */
11773 #define elf_backend_create_dynamic_sections \
11774 elfNN_aarch64_create_dynamic_sections
11775
11776 #define elf_backend_init_index_section \
11777 _bfd_elf_init_2_index_sections
11778
11779 #define elf_backend_finish_dynamic_sections \
11780 elfNN_aarch64_finish_dynamic_sections
11781
11782 #define elf_backend_finish_dynamic_symbol \
11783 elfNN_aarch64_finish_dynamic_symbol
11784
11785 #define elf_backend_object_p \
11786 elfNN_aarch64_object_p
11787
11788 #define elf_backend_output_arch_local_syms \
11789 elfNN_aarch64_output_arch_local_syms
11790
11791 #define elf_backend_maybe_function_sym \
11792 elfNN_aarch64_maybe_function_sym
11793
11794 #define elf_backend_plt_sym_val \
11795 elfNN_aarch64_plt_sym_val
11796
11797 #define elf_backend_init_file_header \
11798 elfNN_aarch64_init_file_header
11799
11800 #define elf_backend_relocate_section \
11801 elfNN_aarch64_relocate_section
11802
11803 #define elf_backend_reloc_type_class \
11804 elfNN_aarch64_reloc_type_class
11805
11806 #define elf_backend_section_from_shdr \
11807 elfNN_aarch64_section_from_shdr
11808
11809 #define elf_backend_size_dynamic_sections \
11810 elfNN_aarch64_size_dynamic_sections
11811
11812 #define elf_backend_size_info \
11813 elfNN_aarch64_size_info
11814
11815 #define elf_backend_write_section \
11816 elfNN_aarch64_write_section
11817
11818 #define elf_backend_symbol_processing \
11819 elfNN_aarch64_backend_symbol_processing
11820
11821 #define elf_backend_setup_gnu_properties \
11822 elfNN_aarch64_link_setup_gnu_properties
11823
11824 #define elf_backend_merge_gnu_properties \
11825 elfNN_aarch64_merge_gnu_properties
11826
11827 #define elf_backend_got_header_size \
11828 elfNN_aarch64_got_header_size
11829
11830 #define elf_backend_got_elt_size \
11831 elfNN_aarch64_got_elt_size
11832
11833 #define elf_backend_eh_frame_augmentation_char \
11834 elf64_aarch64_eh_frame_augmentation_char
11835
11836 #define elf_backend_can_refcount 1
11837 #define elf_backend_can_gc_sections 1
11838 #define elf_backend_plt_readonly 1
11839 #define elf_backend_want_got_plt 1
11840 #define elf_backend_want_plt_sym 0
11841 #define elf_backend_want_dynrelro 1
11842 #define elf_backend_may_use_rel_p 0
11843 #define elf_backend_may_use_rela_p 1
11844 #define elf_backend_default_use_rela_p 1
11845 #define elf_backend_rela_normal 1
11846 #define elf_backend_dtrel_excludes_plt 1
11847 #define elf_backend_default_execstack 0
11848 #define elf_backend_extern_protected_data 1
11849 #define elf_backend_hash_symbol elf_aarch64_hash_symbol
11850
11851 #undef elf_backend_obj_attrs_section
11852 #define elf_backend_obj_attrs_section ".ARM.attributes"
11853
11854 #include "elfNN-target.h"
11855
11856 /* CloudABI support. */
11857
11858 #undef TARGET_LITTLE_SYM
11859 #define TARGET_LITTLE_SYM aarch64_elfNN_le_cloudabi_vec
11860 #undef TARGET_LITTLE_NAME
11861 #define TARGET_LITTLE_NAME "elfNN-littleaarch64-cloudabi"
11862 #undef TARGET_BIG_SYM
11863 #define TARGET_BIG_SYM aarch64_elfNN_be_cloudabi_vec
11864 #undef TARGET_BIG_NAME
11865 #define TARGET_BIG_NAME "elfNN-bigaarch64-cloudabi"
11866
11867 #undef ELF_OSABI
11868 #define ELF_OSABI ELFOSABI_CLOUDABI
11869
11870 #undef elfNN_bed
11871 #define elfNN_bed elfNN_aarch64_cloudabi_bed
11872
11873 #include "elfNN-target.h"