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