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