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