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