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