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