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