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