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