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