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