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