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