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