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