56b0943037c97cd0673eff93ea63eff287c40df8
[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       _bfd_error_handler (_("%s: cannot create stub entry %s"),
2837                           section->owner, stub_name);
2838       return NULL;
2839     }
2840
2841   stub_entry->stub_sec = stub_sec;
2842   stub_entry->stub_offset = 0;
2843   stub_entry->id_sec = link_sec;
2844
2845   return stub_entry;
2846 }
2847
2848 /* Add a new stub entry in the final stub section to the stub hash.
2849    Not all fields of the new stub entry are initialised.  */
2850
2851 static struct elf_aarch64_stub_hash_entry *
2852 _bfd_aarch64_add_stub_entry_after (const char *stub_name,
2853                                    asection *link_section,
2854                                    struct elf_aarch64_link_hash_table *htab)
2855 {
2856   asection *stub_sec;
2857   struct elf_aarch64_stub_hash_entry *stub_entry;
2858
2859   stub_sec = _bfd_aarch64_get_stub_for_link_section (link_section, htab);
2860   stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
2861                                          TRUE, FALSE);
2862   if (stub_entry == NULL)
2863     {
2864       _bfd_error_handler (_("cannot create stub entry %s"), stub_name);
2865       return NULL;
2866     }
2867
2868   stub_entry->stub_sec = stub_sec;
2869   stub_entry->stub_offset = 0;
2870   stub_entry->id_sec = link_section;
2871
2872   return stub_entry;
2873 }
2874
2875
2876 static bfd_boolean
2877 aarch64_build_one_stub (struct bfd_hash_entry *gen_entry,
2878                         void *in_arg ATTRIBUTE_UNUSED)
2879 {
2880   struct elf_aarch64_stub_hash_entry *stub_entry;
2881   asection *stub_sec;
2882   bfd *stub_bfd;
2883   bfd_byte *loc;
2884   bfd_vma sym_value;
2885   bfd_vma veneered_insn_loc;
2886   bfd_vma veneer_entry_loc;
2887   bfd_signed_vma branch_offset = 0;
2888   unsigned int template_size;
2889   const uint32_t *template;
2890   unsigned int i;
2891
2892   /* Massage our args to the form they really have.  */
2893   stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
2894
2895   stub_sec = stub_entry->stub_sec;
2896
2897   /* Make a note of the offset within the stubs for this entry.  */
2898   stub_entry->stub_offset = stub_sec->size;
2899   loc = stub_sec->contents + stub_entry->stub_offset;
2900
2901   stub_bfd = stub_sec->owner;
2902
2903   /* This is the address of the stub destination.  */
2904   sym_value = (stub_entry->target_value
2905                + stub_entry->target_section->output_offset
2906                + stub_entry->target_section->output_section->vma);
2907
2908   if (stub_entry->stub_type == aarch64_stub_long_branch)
2909     {
2910       bfd_vma place = (stub_entry->stub_offset + stub_sec->output_section->vma
2911                        + stub_sec->output_offset);
2912
2913       /* See if we can relax the stub.  */
2914       if (aarch64_valid_for_adrp_p (sym_value, place))
2915         stub_entry->stub_type = aarch64_select_branch_stub (sym_value, place);
2916     }
2917
2918   switch (stub_entry->stub_type)
2919     {
2920     case aarch64_stub_adrp_branch:
2921       template = aarch64_adrp_branch_stub;
2922       template_size = sizeof (aarch64_adrp_branch_stub);
2923       break;
2924     case aarch64_stub_long_branch:
2925       template = aarch64_long_branch_stub;
2926       template_size = sizeof (aarch64_long_branch_stub);
2927       break;
2928     case aarch64_stub_erratum_835769_veneer:
2929       template = aarch64_erratum_835769_stub;
2930       template_size = sizeof (aarch64_erratum_835769_stub);
2931       break;
2932     case aarch64_stub_erratum_843419_veneer:
2933       template = aarch64_erratum_843419_stub;
2934       template_size = sizeof (aarch64_erratum_843419_stub);
2935       break;
2936     default:
2937       abort ();
2938     }
2939
2940   for (i = 0; i < (template_size / sizeof template[0]); i++)
2941     {
2942       bfd_putl32 (template[i], loc);
2943       loc += 4;
2944     }
2945
2946   template_size = (template_size + 7) & ~7;
2947   stub_sec->size += template_size;
2948
2949   switch (stub_entry->stub_type)
2950     {
2951     case aarch64_stub_adrp_branch:
2952       if (aarch64_relocate (AARCH64_R (ADR_PREL_PG_HI21), stub_bfd, stub_sec,
2953                             stub_entry->stub_offset, sym_value))
2954         /* The stub would not have been relaxed if the offset was out
2955            of range.  */
2956         BFD_FAIL ();
2957
2958       if (aarch64_relocate (AARCH64_R (ADD_ABS_LO12_NC), stub_bfd, stub_sec,
2959                             stub_entry->stub_offset + 4, sym_value))
2960         BFD_FAIL ();
2961       break;
2962
2963     case aarch64_stub_long_branch:
2964       /* We want the value relative to the address 12 bytes back from the
2965          value itself.  */
2966       if (aarch64_relocate (AARCH64_R (PRELNN), stub_bfd, stub_sec,
2967                             stub_entry->stub_offset + 16, sym_value + 12))
2968         BFD_FAIL ();
2969       break;
2970
2971     case aarch64_stub_erratum_835769_veneer:
2972       veneered_insn_loc = stub_entry->target_section->output_section->vma
2973                           + stub_entry->target_section->output_offset
2974                           + stub_entry->target_value;
2975       veneer_entry_loc = stub_entry->stub_sec->output_section->vma
2976                           + stub_entry->stub_sec->output_offset
2977                           + stub_entry->stub_offset;
2978       branch_offset = veneered_insn_loc - veneer_entry_loc;
2979       branch_offset >>= 2;
2980       branch_offset &= 0x3ffffff;
2981       bfd_putl32 (stub_entry->veneered_insn,
2982                   stub_sec->contents + stub_entry->stub_offset);
2983       bfd_putl32 (template[1] | branch_offset,
2984                   stub_sec->contents + stub_entry->stub_offset + 4);
2985       break;
2986
2987     case aarch64_stub_erratum_843419_veneer:
2988       if (aarch64_relocate (AARCH64_R (JUMP26), stub_bfd, stub_sec,
2989                             stub_entry->stub_offset + 4, sym_value + 4))
2990         BFD_FAIL ();
2991       break;
2992
2993     default:
2994       abort ();
2995     }
2996
2997   return TRUE;
2998 }
2999
3000 /* As above, but don't actually build the stub.  Just bump offset so
3001    we know stub section sizes.  */
3002
3003 static bfd_boolean
3004 aarch64_size_one_stub (struct bfd_hash_entry *gen_entry,
3005                        void *in_arg ATTRIBUTE_UNUSED)
3006 {
3007   struct elf_aarch64_stub_hash_entry *stub_entry;
3008   int size;
3009
3010   /* Massage our args to the form they really have.  */
3011   stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
3012
3013   switch (stub_entry->stub_type)
3014     {
3015     case aarch64_stub_adrp_branch:
3016       size = sizeof (aarch64_adrp_branch_stub);
3017       break;
3018     case aarch64_stub_long_branch:
3019       size = sizeof (aarch64_long_branch_stub);
3020       break;
3021     case aarch64_stub_erratum_835769_veneer:
3022       size = sizeof (aarch64_erratum_835769_stub);
3023       break;
3024     case aarch64_stub_erratum_843419_veneer:
3025       size = sizeof (aarch64_erratum_843419_stub);
3026       break;
3027     default:
3028       abort ();
3029     }
3030
3031   size = (size + 7) & ~7;
3032   stub_entry->stub_sec->size += size;
3033   return TRUE;
3034 }
3035
3036 /* External entry points for sizing and building linker stubs.  */
3037
3038 /* Set up various things so that we can make a list of input sections
3039    for each output section included in the link.  Returns -1 on error,
3040    0 when no stubs will be needed, and 1 on success.  */
3041
3042 int
3043 elfNN_aarch64_setup_section_lists (bfd *output_bfd,
3044                                    struct bfd_link_info *info)
3045 {
3046   bfd *input_bfd;
3047   unsigned int bfd_count;
3048   unsigned int top_id, top_index;
3049   asection *section;
3050   asection **input_list, **list;
3051   bfd_size_type amt;
3052   struct elf_aarch64_link_hash_table *htab =
3053     elf_aarch64_hash_table (info);
3054
3055   if (!is_elf_hash_table (htab))
3056     return 0;
3057
3058   /* Count the number of input BFDs and find the top input section id.  */
3059   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
3060        input_bfd != NULL; input_bfd = input_bfd->link.next)
3061     {
3062       bfd_count += 1;
3063       for (section = input_bfd->sections;
3064            section != NULL; section = section->next)
3065         {
3066           if (top_id < section->id)
3067             top_id = section->id;
3068         }
3069     }
3070   htab->bfd_count = bfd_count;
3071
3072   amt = sizeof (struct map_stub) * (top_id + 1);
3073   htab->stub_group = bfd_zmalloc (amt);
3074   if (htab->stub_group == NULL)
3075     return -1;
3076
3077   /* We can't use output_bfd->section_count here to find the top output
3078      section index as some sections may have been removed, and
3079      _bfd_strip_section_from_output doesn't renumber the indices.  */
3080   for (section = output_bfd->sections, top_index = 0;
3081        section != NULL; section = section->next)
3082     {
3083       if (top_index < section->index)
3084         top_index = section->index;
3085     }
3086
3087   htab->top_index = top_index;
3088   amt = sizeof (asection *) * (top_index + 1);
3089   input_list = bfd_malloc (amt);
3090   htab->input_list = input_list;
3091   if (input_list == NULL)
3092     return -1;
3093
3094   /* For sections we aren't interested in, mark their entries with a
3095      value we can check later.  */
3096   list = input_list + top_index;
3097   do
3098     *list = bfd_abs_section_ptr;
3099   while (list-- != input_list);
3100
3101   for (section = output_bfd->sections;
3102        section != NULL; section = section->next)
3103     {
3104       if ((section->flags & SEC_CODE) != 0)
3105         input_list[section->index] = NULL;
3106     }
3107
3108   return 1;
3109 }
3110
3111 /* Used by elfNN_aarch64_next_input_section and group_sections.  */
3112 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3113
3114 /* The linker repeatedly calls this function for each input section,
3115    in the order that input sections are linked into output sections.
3116    Build lists of input sections to determine groupings between which
3117    we may insert linker stubs.  */
3118
3119 void
3120 elfNN_aarch64_next_input_section (struct bfd_link_info *info, asection *isec)
3121 {
3122   struct elf_aarch64_link_hash_table *htab =
3123     elf_aarch64_hash_table (info);
3124
3125   if (isec->output_section->index <= htab->top_index)
3126     {
3127       asection **list = htab->input_list + isec->output_section->index;
3128
3129       if (*list != bfd_abs_section_ptr)
3130         {
3131           /* Steal the link_sec pointer for our list.  */
3132           /* This happens to make the list in reverse order,
3133              which is what we want.  */
3134           PREV_SEC (isec) = *list;
3135           *list = isec;
3136         }
3137     }
3138 }
3139
3140 /* See whether we can group stub sections together.  Grouping stub
3141    sections may result in fewer stubs.  More importantly, we need to
3142    put all .init* and .fini* stubs at the beginning of the .init or
3143    .fini output sections respectively, because glibc splits the
3144    _init and _fini functions into multiple parts.  Putting a stub in
3145    the middle of a function is not a good idea.  */
3146
3147 static void
3148 group_sections (struct elf_aarch64_link_hash_table *htab,
3149                 bfd_size_type stub_group_size,
3150                 bfd_boolean stubs_always_before_branch)
3151 {
3152   asection **list = htab->input_list + htab->top_index;
3153
3154   do
3155     {
3156       asection *tail = *list;
3157
3158       if (tail == bfd_abs_section_ptr)
3159         continue;
3160
3161       while (tail != NULL)
3162         {
3163           asection *curr;
3164           asection *prev;
3165           bfd_size_type total;
3166
3167           curr = tail;
3168           total = tail->size;
3169           while ((prev = PREV_SEC (curr)) != NULL
3170                  && ((total += curr->output_offset - prev->output_offset)
3171                      < stub_group_size))
3172             curr = prev;
3173
3174           /* OK, the size from the start of CURR to the end is less
3175              than stub_group_size and thus can be handled by one stub
3176              section.  (Or the tail section is itself larger than
3177              stub_group_size, in which case we may be toast.)
3178              We should really be keeping track of the total size of
3179              stubs added here, as stubs contribute to the final output
3180              section size.  */
3181           do
3182             {
3183               prev = PREV_SEC (tail);
3184               /* Set up this stub group.  */
3185               htab->stub_group[tail->id].link_sec = curr;
3186             }
3187           while (tail != curr && (tail = prev) != NULL);
3188
3189           /* But wait, there's more!  Input sections up to stub_group_size
3190              bytes before the stub section can be handled by it too.  */
3191           if (!stubs_always_before_branch)
3192             {
3193               total = 0;
3194               while (prev != NULL
3195                      && ((total += tail->output_offset - prev->output_offset)
3196                          < stub_group_size))
3197                 {
3198                   tail = prev;
3199                   prev = PREV_SEC (tail);
3200                   htab->stub_group[tail->id].link_sec = curr;
3201                 }
3202             }
3203           tail = prev;
3204         }
3205     }
3206   while (list-- != htab->input_list);
3207
3208   free (htab->input_list);
3209 }
3210
3211 #undef PREV_SEC
3212
3213 #define AARCH64_BITS(x, pos, n) (((x) >> (pos)) & ((1 << (n)) - 1))
3214
3215 #define AARCH64_RT(insn) AARCH64_BITS (insn, 0, 5)
3216 #define AARCH64_RT2(insn) AARCH64_BITS (insn, 10, 5)
3217 #define AARCH64_RA(insn) AARCH64_BITS (insn, 10, 5)
3218 #define AARCH64_RD(insn) AARCH64_BITS (insn, 0, 5)
3219 #define AARCH64_RN(insn) AARCH64_BITS (insn, 5, 5)
3220 #define AARCH64_RM(insn) AARCH64_BITS (insn, 16, 5)
3221
3222 #define AARCH64_MAC(insn) (((insn) & 0xff000000) == 0x9b000000)
3223 #define AARCH64_BIT(insn, n) AARCH64_BITS (insn, n, 1)
3224 #define AARCH64_OP31(insn) AARCH64_BITS (insn, 21, 3)
3225 #define AARCH64_ZR 0x1f
3226
3227 /* All ld/st ops.  See C4-182 of the ARM ARM.  The encoding space for
3228    LD_PCREL, LDST_RO, LDST_UI and LDST_UIMM cover prefetch ops.  */
3229
3230 #define AARCH64_LD(insn) (AARCH64_BIT (insn, 22) == 1)
3231 #define AARCH64_LDST(insn) (((insn) & 0x0a000000) == 0x08000000)
3232 #define AARCH64_LDST_EX(insn) (((insn) & 0x3f000000) == 0x08000000)
3233 #define AARCH64_LDST_PCREL(insn) (((insn) & 0x3b000000) == 0x18000000)
3234 #define AARCH64_LDST_NAP(insn) (((insn) & 0x3b800000) == 0x28000000)
3235 #define AARCH64_LDSTP_PI(insn) (((insn) & 0x3b800000) == 0x28800000)
3236 #define AARCH64_LDSTP_O(insn) (((insn) & 0x3b800000) == 0x29000000)
3237 #define AARCH64_LDSTP_PRE(insn) (((insn) & 0x3b800000) == 0x29800000)
3238 #define AARCH64_LDST_UI(insn) (((insn) & 0x3b200c00) == 0x38000000)
3239 #define AARCH64_LDST_PIIMM(insn) (((insn) & 0x3b200c00) == 0x38000400)
3240 #define AARCH64_LDST_U(insn) (((insn) & 0x3b200c00) == 0x38000800)
3241 #define AARCH64_LDST_PREIMM(insn) (((insn) & 0x3b200c00) == 0x38000c00)
3242 #define AARCH64_LDST_RO(insn) (((insn) & 0x3b200c00) == 0x38200800)
3243 #define AARCH64_LDST_UIMM(insn) (((insn) & 0x3b000000) == 0x39000000)
3244 #define AARCH64_LDST_SIMD_M(insn) (((insn) & 0xbfbf0000) == 0x0c000000)
3245 #define AARCH64_LDST_SIMD_M_PI(insn) (((insn) & 0xbfa00000) == 0x0c800000)
3246 #define AARCH64_LDST_SIMD_S(insn) (((insn) & 0xbf9f0000) == 0x0d000000)
3247 #define AARCH64_LDST_SIMD_S_PI(insn) (((insn) & 0xbf800000) == 0x0d800000)
3248
3249 /* Classify an INSN if it is indeed a load/store.
3250
3251    Return TRUE if INSN is a LD/ST instruction otherwise return FALSE.
3252
3253    For scalar LD/ST instructions PAIR is FALSE, RT is returned and RT2
3254    is set equal to RT.
3255
3256    For LD/ST pair instructions PAIR is TRUE, RT and RT2 are returned.
3257
3258  */
3259
3260 static bfd_boolean
3261 aarch64_mem_op_p (uint32_t insn, unsigned int *rt, unsigned int *rt2,
3262                   bfd_boolean *pair, bfd_boolean *load)
3263 {
3264   uint32_t opcode;
3265   unsigned int r;
3266   uint32_t opc = 0;
3267   uint32_t v = 0;
3268   uint32_t opc_v = 0;
3269
3270   /* Bail out quickly if INSN doesn't fall into the the load-store
3271      encoding space.  */
3272   if (!AARCH64_LDST (insn))
3273     return FALSE;
3274
3275   *pair = FALSE;
3276   *load = FALSE;
3277   if (AARCH64_LDST_EX (insn))
3278     {
3279       *rt = AARCH64_RT (insn);
3280       *rt2 = *rt;
3281       if (AARCH64_BIT (insn, 21) == 1)
3282         {
3283           *pair = TRUE;
3284           *rt2 = AARCH64_RT2 (insn);
3285         }
3286       *load = AARCH64_LD (insn);
3287       return TRUE;
3288     }
3289   else if (AARCH64_LDST_NAP (insn)
3290            || AARCH64_LDSTP_PI (insn)
3291            || AARCH64_LDSTP_O (insn)
3292            || AARCH64_LDSTP_PRE (insn))
3293     {
3294       *pair = TRUE;
3295       *rt = AARCH64_RT (insn);
3296       *rt2 = AARCH64_RT2 (insn);
3297       *load = AARCH64_LD (insn);
3298       return TRUE;
3299     }
3300   else if (AARCH64_LDST_PCREL (insn)
3301            || AARCH64_LDST_UI (insn)
3302            || AARCH64_LDST_PIIMM (insn)
3303            || AARCH64_LDST_U (insn)
3304            || AARCH64_LDST_PREIMM (insn)
3305            || AARCH64_LDST_RO (insn)
3306            || AARCH64_LDST_UIMM (insn))
3307    {
3308       *rt = AARCH64_RT (insn);
3309       *rt2 = *rt;
3310       if (AARCH64_LDST_PCREL (insn))
3311         *load = TRUE;
3312       opc = AARCH64_BITS (insn, 22, 2);
3313       v = AARCH64_BIT (insn, 26);
3314       opc_v = opc | (v << 2);
3315       *load =  (opc_v == 1 || opc_v == 2 || opc_v == 3
3316                 || opc_v == 5 || opc_v == 7);
3317       return TRUE;
3318    }
3319   else if (AARCH64_LDST_SIMD_M (insn)
3320            || AARCH64_LDST_SIMD_M_PI (insn))
3321     {
3322       *rt = AARCH64_RT (insn);
3323       *load = AARCH64_BIT (insn, 22);
3324       opcode = (insn >> 12) & 0xf;
3325       switch (opcode)
3326         {
3327         case 0:
3328         case 2:
3329           *rt2 = *rt + 3;
3330           break;
3331
3332         case 4:
3333         case 6:
3334           *rt2 = *rt + 2;
3335           break;
3336
3337         case 7:
3338           *rt2 = *rt;
3339           break;
3340
3341         case 8:
3342         case 10:
3343           *rt2 = *rt + 1;
3344           break;
3345
3346         default:
3347           return FALSE;
3348         }
3349       return TRUE;
3350     }
3351   else if (AARCH64_LDST_SIMD_S (insn)
3352            || AARCH64_LDST_SIMD_S_PI (insn))
3353     {
3354       *rt = AARCH64_RT (insn);
3355       r = (insn >> 21) & 1;
3356       *load = AARCH64_BIT (insn, 22);
3357       opcode = (insn >> 13) & 0x7;
3358       switch (opcode)
3359         {
3360         case 0:
3361         case 2:
3362         case 4:
3363           *rt2 = *rt + r;
3364           break;
3365
3366         case 1:
3367         case 3:
3368         case 5:
3369           *rt2 = *rt + (r == 0 ? 2 : 3);
3370           break;
3371
3372         case 6:
3373           *rt2 = *rt + r;
3374           break;
3375
3376         case 7:
3377           *rt2 = *rt + (r == 0 ? 2 : 3);
3378           break;
3379
3380         default:
3381           return FALSE;
3382         }
3383       return TRUE;
3384     }
3385
3386   return FALSE;
3387 }
3388
3389 /* Return TRUE if INSN is multiply-accumulate.  */
3390
3391 static bfd_boolean
3392 aarch64_mlxl_p (uint32_t insn)
3393 {
3394   uint32_t op31 = AARCH64_OP31 (insn);
3395
3396   if (AARCH64_MAC (insn)
3397       && (op31 == 0 || op31 == 1 || op31 == 5)
3398       /* Exclude MUL instructions which are encoded as a multiple accumulate
3399          with RA = XZR.  */
3400       && AARCH64_RA (insn) != AARCH64_ZR)
3401     return TRUE;
3402
3403   return FALSE;
3404 }
3405
3406 /* Some early revisions of the Cortex-A53 have an erratum (835769) whereby
3407    it is possible for a 64-bit multiply-accumulate instruction to generate an
3408    incorrect result.  The details are quite complex and hard to
3409    determine statically, since branches in the code may exist in some
3410    circumstances, but all cases end with a memory (load, store, or
3411    prefetch) instruction followed immediately by the multiply-accumulate
3412    operation.  We employ a linker patching technique, by moving the potentially
3413    affected multiply-accumulate instruction into a patch region and replacing
3414    the original instruction with a branch to the patch.  This function checks
3415    if INSN_1 is the memory operation followed by a multiply-accumulate
3416    operation (INSN_2).  Return TRUE if an erratum sequence is found, FALSE
3417    if INSN_1 and INSN_2 are safe.  */
3418
3419 static bfd_boolean
3420 aarch64_erratum_sequence (uint32_t insn_1, uint32_t insn_2)
3421 {
3422   uint32_t rt;
3423   uint32_t rt2;
3424   uint32_t rn;
3425   uint32_t rm;
3426   uint32_t ra;
3427   bfd_boolean pair;
3428   bfd_boolean load;
3429
3430   if (aarch64_mlxl_p (insn_2)
3431       && aarch64_mem_op_p (insn_1, &rt, &rt2, &pair, &load))
3432     {
3433       /* Any SIMD memory op is independent of the subsequent MLA
3434          by definition of the erratum.  */
3435       if (AARCH64_BIT (insn_1, 26))
3436         return TRUE;
3437
3438       /* If not SIMD, check for integer memory ops and MLA relationship.  */
3439       rn = AARCH64_RN (insn_2);
3440       ra = AARCH64_RA (insn_2);
3441       rm = AARCH64_RM (insn_2);
3442
3443       /* If this is a load and there's a true(RAW) dependency, we are safe
3444          and this is not an erratum sequence.  */
3445       if (load &&
3446           (rt == rn || rt == rm || rt == ra
3447            || (pair && (rt2 == rn || rt2 == rm || rt2 == ra))))
3448         return FALSE;
3449
3450       /* We conservatively put out stubs for all other cases (including
3451          writebacks).  */
3452       return TRUE;
3453     }
3454
3455   return FALSE;
3456 }
3457
3458 /* Used to order a list of mapping symbols by address.  */
3459
3460 static int
3461 elf_aarch64_compare_mapping (const void *a, const void *b)
3462 {
3463   const elf_aarch64_section_map *amap = (const elf_aarch64_section_map *) a;
3464   const elf_aarch64_section_map *bmap = (const elf_aarch64_section_map *) b;
3465
3466   if (amap->vma > bmap->vma)
3467     return 1;
3468   else if (amap->vma < bmap->vma)
3469     return -1;
3470   else if (amap->type > bmap->type)
3471     /* Ensure results do not depend on the host qsort for objects with
3472        multiple mapping symbols at the same address by sorting on type
3473        after vma.  */
3474     return 1;
3475   else if (amap->type < bmap->type)
3476     return -1;
3477   else
3478     return 0;
3479 }
3480
3481
3482 static char *
3483 _bfd_aarch64_erratum_835769_stub_name (unsigned num_fixes)
3484 {
3485   char *stub_name = (char *) bfd_malloc
3486     (strlen ("__erratum_835769_veneer_") + 16);
3487   sprintf (stub_name,"__erratum_835769_veneer_%d", num_fixes);
3488   return stub_name;
3489 }
3490
3491 /* Scan for Cortex-A53 erratum 835769 sequence.
3492
3493    Return TRUE else FALSE on abnormal termination.  */
3494
3495 static bfd_boolean
3496 _bfd_aarch64_erratum_835769_scan (bfd *input_bfd,
3497                                   struct bfd_link_info *info,
3498                                   unsigned int *num_fixes_p)
3499 {
3500   asection *section;
3501   struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
3502   unsigned int num_fixes = *num_fixes_p;
3503
3504   if (htab == NULL)
3505     return TRUE;
3506
3507   for (section = input_bfd->sections;
3508        section != NULL;
3509        section = section->next)
3510     {
3511       bfd_byte *contents = NULL;
3512       struct _aarch64_elf_section_data *sec_data;
3513       unsigned int span;
3514
3515       if (elf_section_type (section) != SHT_PROGBITS
3516           || (elf_section_flags (section) & SHF_EXECINSTR) == 0
3517           || (section->flags & SEC_EXCLUDE) != 0
3518           || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
3519           || (section->output_section == bfd_abs_section_ptr))
3520         continue;
3521
3522       if (elf_section_data (section)->this_hdr.contents != NULL)
3523         contents = elf_section_data (section)->this_hdr.contents;
3524       else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
3525         return FALSE;
3526
3527       sec_data = elf_aarch64_section_data (section);
3528
3529       qsort (sec_data->map, sec_data->mapcount,
3530              sizeof (elf_aarch64_section_map), elf_aarch64_compare_mapping);
3531
3532       for (span = 0; span < sec_data->mapcount; span++)
3533         {
3534           unsigned int span_start = sec_data->map[span].vma;
3535           unsigned int span_end = ((span == sec_data->mapcount - 1)
3536                                    ? sec_data->map[0].vma + section->size
3537                                    : sec_data->map[span + 1].vma);
3538           unsigned int i;
3539           char span_type = sec_data->map[span].type;
3540
3541           if (span_type == 'd')
3542             continue;
3543
3544           for (i = span_start; i + 4 < span_end; i += 4)
3545             {
3546               uint32_t insn_1 = bfd_getl32 (contents + i);
3547               uint32_t insn_2 = bfd_getl32 (contents + i + 4);
3548
3549               if (aarch64_erratum_sequence (insn_1, insn_2))
3550                 {
3551                   struct elf_aarch64_stub_hash_entry *stub_entry;
3552                   char *stub_name = _bfd_aarch64_erratum_835769_stub_name (num_fixes);
3553                   if (! stub_name)
3554                     return FALSE;
3555
3556                   stub_entry = _bfd_aarch64_add_stub_entry_in_group (stub_name,
3557                                                                      section,
3558                                                                      htab);
3559                   if (! stub_entry)
3560                     return FALSE;
3561
3562                   stub_entry->stub_type = aarch64_stub_erratum_835769_veneer;
3563                   stub_entry->target_section = section;
3564                   stub_entry->target_value = i + 4;
3565                   stub_entry->veneered_insn = insn_2;
3566                   stub_entry->output_name = stub_name;
3567                   num_fixes++;
3568                 }
3569             }
3570         }
3571       if (elf_section_data (section)->this_hdr.contents == NULL)
3572         free (contents);
3573     }
3574
3575   *num_fixes_p = num_fixes;
3576
3577   return TRUE;
3578 }
3579
3580
3581 /* Test if instruction INSN is ADRP.  */
3582
3583 static bfd_boolean
3584 _bfd_aarch64_adrp_p (uint32_t insn)
3585 {
3586   return ((insn & 0x9f000000) == 0x90000000);
3587 }
3588
3589
3590 /* Helper predicate to look for cortex-a53 erratum 843419 sequence 1.  */
3591
3592 static bfd_boolean
3593 _bfd_aarch64_erratum_843419_sequence_p (uint32_t insn_1, uint32_t insn_2,
3594                                         uint32_t insn_3)
3595 {
3596   uint32_t rt;
3597   uint32_t rt2;
3598   bfd_boolean pair;
3599   bfd_boolean load;
3600
3601   return (aarch64_mem_op_p (insn_2, &rt, &rt2, &pair, &load)
3602           && (!pair
3603               || (pair && !load))
3604           && AARCH64_LDST_UIMM (insn_3)
3605           && AARCH64_RN (insn_3) == AARCH64_RD (insn_1));
3606 }
3607
3608
3609 /* Test for the presence of Cortex-A53 erratum 843419 instruction sequence.
3610
3611    Return TRUE if section CONTENTS at offset I contains one of the
3612    erratum 843419 sequences, otherwise return FALSE.  If a sequence is
3613    seen set P_VENEER_I to the offset of the final LOAD/STORE
3614    instruction in the sequence.
3615  */
3616
3617 static bfd_boolean
3618 _bfd_aarch64_erratum_843419_p (bfd_byte *contents, bfd_vma vma,
3619                                bfd_vma i, bfd_vma span_end,
3620                                bfd_vma *p_veneer_i)
3621 {
3622   uint32_t insn_1 = bfd_getl32 (contents + i);
3623
3624   if (!_bfd_aarch64_adrp_p (insn_1))
3625     return FALSE;
3626
3627   if (span_end < i + 12)
3628     return FALSE;
3629
3630   uint32_t insn_2 = bfd_getl32 (contents + i + 4);
3631   uint32_t insn_3 = bfd_getl32 (contents + i + 8);
3632
3633   if ((vma & 0xfff) != 0xff8 && (vma & 0xfff) != 0xffc)
3634     return FALSE;
3635
3636   if (_bfd_aarch64_erratum_843419_sequence_p (insn_1, insn_2, insn_3))
3637     {
3638       *p_veneer_i = i + 8;
3639       return TRUE;
3640     }
3641
3642   if (span_end < i + 16)
3643     return FALSE;
3644
3645   uint32_t insn_4 = bfd_getl32 (contents + i + 12);
3646
3647   if (_bfd_aarch64_erratum_843419_sequence_p (insn_1, insn_2, insn_4))
3648     {
3649       *p_veneer_i = i + 12;
3650       return TRUE;
3651     }
3652
3653   return FALSE;
3654 }
3655
3656
3657 /* Resize all stub sections.  */
3658
3659 static void
3660 _bfd_aarch64_resize_stubs (struct elf_aarch64_link_hash_table *htab)
3661 {
3662   asection *section;
3663
3664   /* OK, we've added some stubs.  Find out the new size of the
3665      stub sections.  */
3666   for (section = htab->stub_bfd->sections;
3667        section != NULL; section = section->next)
3668     {
3669       /* Ignore non-stub sections.  */
3670       if (!strstr (section->name, STUB_SUFFIX))
3671         continue;
3672       section->size = 0;
3673     }
3674
3675   bfd_hash_traverse (&htab->stub_hash_table, aarch64_size_one_stub, htab);
3676
3677   for (section = htab->stub_bfd->sections;
3678        section != NULL; section = section->next)
3679     {
3680       if (!strstr (section->name, STUB_SUFFIX))
3681         continue;
3682
3683       if (section->size)
3684         section->size += 4;
3685
3686       /* Ensure all stub sections have a size which is a multiple of
3687          4096.  This is important in order to ensure that the insertion
3688          of stub sections does not in itself move existing code around
3689          in such a way that new errata sequences are created.  */
3690       if (htab->fix_erratum_843419)
3691         if (section->size)
3692           section->size = BFD_ALIGN (section->size, 0x1000);
3693     }
3694 }
3695
3696
3697 /* Construct an erratum 843419 workaround stub name.
3698  */
3699
3700 static char *
3701 _bfd_aarch64_erratum_843419_stub_name (asection *input_section,
3702                                        bfd_vma offset)
3703 {
3704   const bfd_size_type len = 8 + 4 + 1 + 8 + 1 + 16 + 1;
3705   char *stub_name = bfd_malloc (len);
3706
3707   if (stub_name != NULL)
3708     snprintf (stub_name, len, "e843419@%04x_%08x_%" BFD_VMA_FMT "x",
3709               input_section->owner->id,
3710               input_section->id,
3711               offset);
3712   return stub_name;
3713 }
3714
3715 /*  Build a stub_entry structure describing an 843419 fixup.
3716
3717     The stub_entry constructed is populated with the bit pattern INSN
3718     of the instruction located at OFFSET within input SECTION.
3719
3720     Returns TRUE on success.  */
3721
3722 static bfd_boolean
3723 _bfd_aarch64_erratum_843419_fixup (uint32_t insn,
3724                                    bfd_vma adrp_offset,
3725                                    bfd_vma ldst_offset,
3726                                    asection *section,
3727                                    struct bfd_link_info *info)
3728 {
3729   struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
3730   char *stub_name;
3731   struct elf_aarch64_stub_hash_entry *stub_entry;
3732
3733   stub_name = _bfd_aarch64_erratum_843419_stub_name (section, ldst_offset);
3734   stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3735                                          FALSE, FALSE);
3736   if (stub_entry)
3737     {
3738       free (stub_name);
3739       return TRUE;
3740     }
3741
3742   /* We always place an 843419 workaround veneer in the stub section
3743      attached to the input section in which an erratum sequence has
3744      been found.  This ensures that later in the link process (in
3745      elfNN_aarch64_write_section) when we copy the veneered
3746      instruction from the input section into the stub section the
3747      copied instruction will have had any relocations applied to it.
3748      If we placed workaround veneers in any other stub section then we
3749      could not assume that all relocations have been processed on the
3750      corresponding input section at the point we output the stub
3751      section.
3752    */
3753
3754   stub_entry = _bfd_aarch64_add_stub_entry_after (stub_name, section, htab);
3755   if (stub_entry == NULL)
3756     {
3757       free (stub_name);
3758       return FALSE;
3759     }
3760
3761   stub_entry->adrp_offset = adrp_offset;
3762   stub_entry->target_value = ldst_offset;
3763   stub_entry->target_section = section;
3764   stub_entry->stub_type = aarch64_stub_erratum_843419_veneer;
3765   stub_entry->veneered_insn = insn;
3766   stub_entry->output_name = stub_name;
3767
3768   return TRUE;
3769 }
3770
3771
3772 /* Scan an input section looking for the signature of erratum 843419.
3773
3774    Scans input SECTION in INPUT_BFD looking for erratum 843419
3775    signatures, for each signature found a stub_entry is created
3776    describing the location of the erratum for subsequent fixup.
3777
3778    Return TRUE on successful scan, FALSE on failure to scan.
3779  */
3780
3781 static bfd_boolean
3782 _bfd_aarch64_erratum_843419_scan (bfd *input_bfd, asection *section,
3783                                   struct bfd_link_info *info)
3784 {
3785   struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
3786
3787   if (htab == NULL)
3788     return TRUE;
3789
3790   if (elf_section_type (section) != SHT_PROGBITS
3791       || (elf_section_flags (section) & SHF_EXECINSTR) == 0
3792       || (section->flags & SEC_EXCLUDE) != 0
3793       || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
3794       || (section->output_section == bfd_abs_section_ptr))
3795     return TRUE;
3796
3797   do
3798     {
3799       bfd_byte *contents = NULL;
3800       struct _aarch64_elf_section_data *sec_data;
3801       unsigned int span;
3802
3803       if (elf_section_data (section)->this_hdr.contents != NULL)
3804         contents = elf_section_data (section)->this_hdr.contents;
3805       else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
3806         return FALSE;
3807
3808       sec_data = elf_aarch64_section_data (section);
3809
3810       qsort (sec_data->map, sec_data->mapcount,
3811              sizeof (elf_aarch64_section_map), elf_aarch64_compare_mapping);
3812
3813       for (span = 0; span < sec_data->mapcount; span++)
3814         {
3815           unsigned int span_start = sec_data->map[span].vma;
3816           unsigned int span_end = ((span == sec_data->mapcount - 1)
3817                                    ? sec_data->map[0].vma + section->size
3818                                    : sec_data->map[span + 1].vma);
3819           unsigned int i;
3820           char span_type = sec_data->map[span].type;
3821
3822           if (span_type == 'd')
3823             continue;
3824
3825           for (i = span_start; i + 8 < span_end; i += 4)
3826             {
3827               bfd_vma vma = (section->output_section->vma
3828                              + section->output_offset
3829                              + i);
3830               bfd_vma veneer_i;
3831
3832               if (_bfd_aarch64_erratum_843419_p
3833                   (contents, vma, i, span_end, &veneer_i))
3834                 {
3835                   uint32_t insn = bfd_getl32 (contents + veneer_i);
3836
3837                   if (!_bfd_aarch64_erratum_843419_fixup (insn, i, veneer_i,
3838                                                           section, info))
3839                     return FALSE;
3840                 }
3841             }
3842         }
3843
3844       if (elf_section_data (section)->this_hdr.contents == NULL)
3845         free (contents);
3846     }
3847   while (0);
3848
3849   return TRUE;
3850 }
3851
3852
3853 /* Determine and set the size of the stub section for a final link.
3854
3855    The basic idea here is to examine all the relocations looking for
3856    PC-relative calls to a target that is unreachable with a "bl"
3857    instruction.  */
3858
3859 bfd_boolean
3860 elfNN_aarch64_size_stubs (bfd *output_bfd,
3861                           bfd *stub_bfd,
3862                           struct bfd_link_info *info,
3863                           bfd_signed_vma group_size,
3864                           asection * (*add_stub_section) (const char *,
3865                                                           asection *),
3866                           void (*layout_sections_again) (void))
3867 {
3868   bfd_size_type stub_group_size;
3869   bfd_boolean stubs_always_before_branch;
3870   bfd_boolean stub_changed = FALSE;
3871   struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
3872   unsigned int num_erratum_835769_fixes = 0;
3873
3874   /* Propagate mach to stub bfd, because it may not have been
3875      finalized when we created stub_bfd.  */
3876   bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
3877                      bfd_get_mach (output_bfd));
3878
3879   /* Stash our params away.  */
3880   htab->stub_bfd = stub_bfd;
3881   htab->add_stub_section = add_stub_section;
3882   htab->layout_sections_again = layout_sections_again;
3883   stubs_always_before_branch = group_size < 0;
3884   if (group_size < 0)
3885     stub_group_size = -group_size;
3886   else
3887     stub_group_size = group_size;
3888
3889   if (stub_group_size == 1)
3890     {
3891       /* Default values.  */
3892       /* AArch64 branch range is +-128MB. The value used is 1MB less.  */
3893       stub_group_size = 127 * 1024 * 1024;
3894     }
3895
3896   group_sections (htab, stub_group_size, stubs_always_before_branch);
3897
3898   (*htab->layout_sections_again) ();
3899
3900   if (htab->fix_erratum_835769)
3901     {
3902       bfd *input_bfd;
3903
3904       for (input_bfd = info->input_bfds;
3905            input_bfd != NULL; input_bfd = input_bfd->link.next)
3906         if (!_bfd_aarch64_erratum_835769_scan (input_bfd, info,
3907                                                &num_erratum_835769_fixes))
3908           return FALSE;
3909
3910       _bfd_aarch64_resize_stubs (htab);
3911       (*htab->layout_sections_again) ();
3912     }
3913
3914   if (htab->fix_erratum_843419)
3915     {
3916       bfd *input_bfd;
3917
3918       for (input_bfd = info->input_bfds;
3919            input_bfd != NULL;
3920            input_bfd = input_bfd->link.next)
3921         {
3922           asection *section;
3923
3924           for (section = input_bfd->sections;
3925                section != NULL;
3926                section = section->next)
3927             if (!_bfd_aarch64_erratum_843419_scan (input_bfd, section, info))
3928               return FALSE;
3929         }
3930
3931       _bfd_aarch64_resize_stubs (htab);
3932       (*htab->layout_sections_again) ();
3933     }
3934
3935   while (1)
3936     {
3937       bfd *input_bfd;
3938
3939       for (input_bfd = info->input_bfds;
3940            input_bfd != NULL; input_bfd = input_bfd->link.next)
3941         {
3942           Elf_Internal_Shdr *symtab_hdr;
3943           asection *section;
3944           Elf_Internal_Sym *local_syms = NULL;
3945
3946           /* We'll need the symbol table in a second.  */
3947           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3948           if (symtab_hdr->sh_info == 0)
3949             continue;
3950
3951           /* Walk over each section attached to the input bfd.  */
3952           for (section = input_bfd->sections;
3953                section != NULL; section = section->next)
3954             {
3955               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
3956
3957               /* If there aren't any relocs, then there's nothing more
3958                  to do.  */
3959               if ((section->flags & SEC_RELOC) == 0
3960                   || section->reloc_count == 0
3961                   || (section->flags & SEC_CODE) == 0)
3962                 continue;
3963
3964               /* If this section is a link-once section that will be
3965                  discarded, then don't create any stubs.  */
3966               if (section->output_section == NULL
3967                   || section->output_section->owner != output_bfd)
3968                 continue;
3969
3970               /* Get the relocs.  */
3971               internal_relocs
3972                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
3973                                              NULL, info->keep_memory);
3974               if (internal_relocs == NULL)
3975                 goto error_ret_free_local;
3976
3977               /* Now examine each relocation.  */
3978               irela = internal_relocs;
3979               irelaend = irela + section->reloc_count;
3980               for (; irela < irelaend; irela++)
3981                 {
3982                   unsigned int r_type, r_indx;
3983                   enum elf_aarch64_stub_type stub_type;
3984                   struct elf_aarch64_stub_hash_entry *stub_entry;
3985                   asection *sym_sec;
3986                   bfd_vma sym_value;
3987                   bfd_vma destination;
3988                   struct elf_aarch64_link_hash_entry *hash;
3989                   const char *sym_name;
3990                   char *stub_name;
3991                   const asection *id_sec;
3992                   unsigned char st_type;
3993                   bfd_size_type len;
3994
3995                   r_type = ELFNN_R_TYPE (irela->r_info);
3996                   r_indx = ELFNN_R_SYM (irela->r_info);
3997
3998                   if (r_type >= (unsigned int) R_AARCH64_end)
3999                     {
4000                       bfd_set_error (bfd_error_bad_value);
4001                     error_ret_free_internal:
4002                       if (elf_section_data (section)->relocs == NULL)
4003                         free (internal_relocs);
4004                       goto error_ret_free_local;
4005                     }
4006
4007                   /* Only look for stubs on unconditional branch and
4008                      branch and link instructions.  */
4009                   if (r_type != (unsigned int) AARCH64_R (CALL26)
4010                       && r_type != (unsigned int) AARCH64_R (JUMP26))
4011                     continue;
4012
4013                   /* Now determine the call target, its name, value,
4014                      section.  */
4015                   sym_sec = NULL;
4016                   sym_value = 0;
4017                   destination = 0;
4018                   hash = NULL;
4019                   sym_name = NULL;
4020                   if (r_indx < symtab_hdr->sh_info)
4021                     {
4022                       /* It's a local symbol.  */
4023                       Elf_Internal_Sym *sym;
4024                       Elf_Internal_Shdr *hdr;
4025
4026                       if (local_syms == NULL)
4027                         {
4028                           local_syms
4029                             = (Elf_Internal_Sym *) symtab_hdr->contents;
4030                           if (local_syms == NULL)
4031                             local_syms
4032                               = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
4033                                                       symtab_hdr->sh_info, 0,
4034                                                       NULL, NULL, NULL);
4035                           if (local_syms == NULL)
4036                             goto error_ret_free_internal;
4037                         }
4038
4039                       sym = local_syms + r_indx;
4040                       hdr = elf_elfsections (input_bfd)[sym->st_shndx];
4041                       sym_sec = hdr->bfd_section;
4042                       if (!sym_sec)
4043                         /* This is an undefined symbol.  It can never
4044                            be resolved.  */
4045                         continue;
4046
4047                       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4048                         sym_value = sym->st_value;
4049                       destination = (sym_value + irela->r_addend
4050                                      + sym_sec->output_offset
4051                                      + sym_sec->output_section->vma);
4052                       st_type = ELF_ST_TYPE (sym->st_info);
4053                       sym_name
4054                         = bfd_elf_string_from_elf_section (input_bfd,
4055                                                            symtab_hdr->sh_link,
4056                                                            sym->st_name);
4057                     }
4058                   else
4059                     {
4060                       int e_indx;
4061
4062                       e_indx = r_indx - symtab_hdr->sh_info;
4063                       hash = ((struct elf_aarch64_link_hash_entry *)
4064                               elf_sym_hashes (input_bfd)[e_indx]);
4065
4066                       while (hash->root.root.type == bfd_link_hash_indirect
4067                              || hash->root.root.type == bfd_link_hash_warning)
4068                         hash = ((struct elf_aarch64_link_hash_entry *)
4069                                 hash->root.root.u.i.link);
4070
4071                       if (hash->root.root.type == bfd_link_hash_defined
4072                           || hash->root.root.type == bfd_link_hash_defweak)
4073                         {
4074                           struct elf_aarch64_link_hash_table *globals =
4075                             elf_aarch64_hash_table (info);
4076                           sym_sec = hash->root.root.u.def.section;
4077                           sym_value = hash->root.root.u.def.value;
4078                           /* For a destination in a shared library,
4079                              use the PLT stub as target address to
4080                              decide whether a branch stub is
4081                              needed.  */
4082                           if (globals->root.splt != NULL && hash != NULL
4083                               && hash->root.plt.offset != (bfd_vma) - 1)
4084                             {
4085                               sym_sec = globals->root.splt;
4086                               sym_value = hash->root.plt.offset;
4087                               if (sym_sec->output_section != NULL)
4088                                 destination = (sym_value
4089                                                + sym_sec->output_offset
4090                                                +
4091                                                sym_sec->output_section->vma);
4092                             }
4093                           else if (sym_sec->output_section != NULL)
4094                             destination = (sym_value + irela->r_addend
4095                                            + sym_sec->output_offset
4096                                            + sym_sec->output_section->vma);
4097                         }
4098                       else if (hash->root.root.type == bfd_link_hash_undefined
4099                                || (hash->root.root.type
4100                                    == bfd_link_hash_undefweak))
4101                         {
4102                           /* For a shared library, use the PLT stub as
4103                              target address to decide whether a long
4104                              branch stub is needed.
4105                              For absolute code, they cannot be handled.  */
4106                           struct elf_aarch64_link_hash_table *globals =
4107                             elf_aarch64_hash_table (info);
4108
4109                           if (globals->root.splt != NULL && hash != NULL
4110                               && hash->root.plt.offset != (bfd_vma) - 1)
4111                             {
4112                               sym_sec = globals->root.splt;
4113                               sym_value = hash->root.plt.offset;
4114                               if (sym_sec->output_section != NULL)
4115                                 destination = (sym_value
4116                                                + sym_sec->output_offset
4117                                                +
4118                                                sym_sec->output_section->vma);
4119                             }
4120                           else
4121                             continue;
4122                         }
4123                       else
4124                         {
4125                           bfd_set_error (bfd_error_bad_value);
4126                           goto error_ret_free_internal;
4127                         }
4128                       st_type = ELF_ST_TYPE (hash->root.type);
4129                       sym_name = hash->root.root.root.string;
4130                     }
4131
4132                   /* Determine what (if any) linker stub is needed.  */
4133                   stub_type = aarch64_type_of_stub (section, irela, sym_sec,
4134                                                     st_type, destination);
4135                   if (stub_type == aarch64_stub_none)
4136                     continue;
4137
4138                   /* Support for grouping stub sections.  */
4139                   id_sec = htab->stub_group[section->id].link_sec;
4140
4141                   /* Get the name of this stub.  */
4142                   stub_name = elfNN_aarch64_stub_name (id_sec, sym_sec, hash,
4143                                                        irela);
4144                   if (!stub_name)
4145                     goto error_ret_free_internal;
4146
4147                   stub_entry =
4148                     aarch64_stub_hash_lookup (&htab->stub_hash_table,
4149                                               stub_name, FALSE, FALSE);
4150                   if (stub_entry != NULL)
4151                     {
4152                       /* The proper stub has already been created.  */
4153                       free (stub_name);
4154                       continue;
4155                     }
4156
4157                   stub_entry = _bfd_aarch64_add_stub_entry_in_group
4158                     (stub_name, section, htab);
4159                   if (stub_entry == NULL)
4160                     {
4161                       free (stub_name);
4162                       goto error_ret_free_internal;
4163                     }
4164
4165                   stub_entry->target_value = sym_value + irela->r_addend;
4166                   stub_entry->target_section = sym_sec;
4167                   stub_entry->stub_type = stub_type;
4168                   stub_entry->h = hash;
4169                   stub_entry->st_type = st_type;
4170
4171                   if (sym_name == NULL)
4172                     sym_name = "unnamed";
4173                   len = sizeof (STUB_ENTRY_NAME) + strlen (sym_name);
4174                   stub_entry->output_name = bfd_alloc (htab->stub_bfd, len);
4175                   if (stub_entry->output_name == NULL)
4176                     {
4177                       free (stub_name);
4178                       goto error_ret_free_internal;
4179                     }
4180
4181                   snprintf (stub_entry->output_name, len, STUB_ENTRY_NAME,
4182                             sym_name);
4183
4184                   stub_changed = TRUE;
4185                 }
4186
4187               /* We're done with the internal relocs, free them.  */
4188               if (elf_section_data (section)->relocs == NULL)
4189                 free (internal_relocs);
4190             }
4191         }
4192
4193       if (!stub_changed)
4194         break;
4195
4196       _bfd_aarch64_resize_stubs (htab);
4197
4198       /* Ask the linker to do its stuff.  */
4199       (*htab->layout_sections_again) ();
4200       stub_changed = FALSE;
4201     }
4202
4203   return TRUE;
4204
4205 error_ret_free_local:
4206   return FALSE;
4207 }
4208
4209 /* Build all the stubs associated with the current output file.  The
4210    stubs are kept in a hash table attached to the main linker hash
4211    table.  We also set up the .plt entries for statically linked PIC
4212    functions here.  This function is called via aarch64_elf_finish in the
4213    linker.  */
4214
4215 bfd_boolean
4216 elfNN_aarch64_build_stubs (struct bfd_link_info *info)
4217 {
4218   asection *stub_sec;
4219   struct bfd_hash_table *table;
4220   struct elf_aarch64_link_hash_table *htab;
4221
4222   htab = elf_aarch64_hash_table (info);
4223
4224   for (stub_sec = htab->stub_bfd->sections;
4225        stub_sec != NULL; stub_sec = stub_sec->next)
4226     {
4227       bfd_size_type size;
4228
4229       /* Ignore non-stub sections.  */
4230       if (!strstr (stub_sec->name, STUB_SUFFIX))
4231         continue;
4232
4233       /* Allocate memory to hold the linker stubs.  */
4234       size = stub_sec->size;
4235       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
4236       if (stub_sec->contents == NULL && size != 0)
4237         return FALSE;
4238       stub_sec->size = 0;
4239
4240       bfd_putl32 (0x14000000 | (size >> 2), stub_sec->contents);
4241       stub_sec->size += 4;
4242     }
4243
4244   /* Build the stubs as directed by the stub hash table.  */
4245   table = &htab->stub_hash_table;
4246   bfd_hash_traverse (table, aarch64_build_one_stub, info);
4247
4248   return TRUE;
4249 }
4250
4251
4252 /* Add an entry to the code/data map for section SEC.  */
4253
4254 static void
4255 elfNN_aarch64_section_map_add (asection *sec, char type, bfd_vma vma)
4256 {
4257   struct _aarch64_elf_section_data *sec_data =
4258     elf_aarch64_section_data (sec);
4259   unsigned int newidx;
4260
4261   if (sec_data->map == NULL)
4262     {
4263       sec_data->map = bfd_malloc (sizeof (elf_aarch64_section_map));
4264       sec_data->mapcount = 0;
4265       sec_data->mapsize = 1;
4266     }
4267
4268   newidx = sec_data->mapcount++;
4269
4270   if (sec_data->mapcount > sec_data->mapsize)
4271     {
4272       sec_data->mapsize *= 2;
4273       sec_data->map = bfd_realloc_or_free
4274         (sec_data->map, sec_data->mapsize * sizeof (elf_aarch64_section_map));
4275     }
4276
4277   if (sec_data->map)
4278     {
4279       sec_data->map[newidx].vma = vma;
4280       sec_data->map[newidx].type = type;
4281     }
4282 }
4283
4284
4285 /* Initialise maps of insn/data for input BFDs.  */
4286 void
4287 bfd_elfNN_aarch64_init_maps (bfd *abfd)
4288 {
4289   Elf_Internal_Sym *isymbuf;
4290   Elf_Internal_Shdr *hdr;
4291   unsigned int i, localsyms;
4292
4293   /* Make sure that we are dealing with an AArch64 elf binary.  */
4294   if (!is_aarch64_elf (abfd))
4295     return;
4296
4297   if ((abfd->flags & DYNAMIC) != 0)
4298    return;
4299
4300   hdr = &elf_symtab_hdr (abfd);
4301   localsyms = hdr->sh_info;
4302
4303   /* Obtain a buffer full of symbols for this BFD. The hdr->sh_info field
4304      should contain the number of local symbols, which should come before any
4305      global symbols.  Mapping symbols are always local.  */
4306   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, localsyms, 0, NULL, NULL, NULL);
4307
4308   /* No internal symbols read?  Skip this BFD.  */
4309   if (isymbuf == NULL)
4310     return;
4311
4312   for (i = 0; i < localsyms; i++)
4313     {
4314       Elf_Internal_Sym *isym = &isymbuf[i];
4315       asection *sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4316       const char *name;
4317
4318       if (sec != NULL && ELF_ST_BIND (isym->st_info) == STB_LOCAL)
4319         {
4320           name = bfd_elf_string_from_elf_section (abfd,
4321                                                   hdr->sh_link,
4322                                                   isym->st_name);
4323
4324           if (bfd_is_aarch64_special_symbol_name
4325               (name, BFD_AARCH64_SPECIAL_SYM_TYPE_MAP))
4326             elfNN_aarch64_section_map_add (sec, name[1], isym->st_value);
4327         }
4328     }
4329 }
4330
4331 /* Set option values needed during linking.  */
4332 void
4333 bfd_elfNN_aarch64_set_options (struct bfd *output_bfd,
4334                                struct bfd_link_info *link_info,
4335                                int no_enum_warn,
4336                                int no_wchar_warn, int pic_veneer,
4337                                int fix_erratum_835769,
4338                                int fix_erratum_843419,
4339                                int no_apply_dynamic_relocs)
4340 {
4341   struct elf_aarch64_link_hash_table *globals;
4342
4343   globals = elf_aarch64_hash_table (link_info);
4344   globals->pic_veneer = pic_veneer;
4345   globals->fix_erratum_835769 = fix_erratum_835769;
4346   globals->fix_erratum_843419 = fix_erratum_843419;
4347   globals->fix_erratum_843419_adr = TRUE;
4348   globals->no_apply_dynamic_relocs = no_apply_dynamic_relocs;
4349
4350   BFD_ASSERT (is_aarch64_elf (output_bfd));
4351   elf_aarch64_tdata (output_bfd)->no_enum_size_warning = no_enum_warn;
4352   elf_aarch64_tdata (output_bfd)->no_wchar_size_warning = no_wchar_warn;
4353 }
4354
4355 static bfd_vma
4356 aarch64_calculate_got_entry_vma (struct elf_link_hash_entry *h,
4357                                  struct elf_aarch64_link_hash_table
4358                                  *globals, struct bfd_link_info *info,
4359                                  bfd_vma value, bfd *output_bfd,
4360                                  bfd_boolean *unresolved_reloc_p)
4361 {
4362   bfd_vma off = (bfd_vma) - 1;
4363   asection *basegot = globals->root.sgot;
4364   bfd_boolean dyn = globals->root.dynamic_sections_created;
4365
4366   if (h != NULL)
4367     {
4368       BFD_ASSERT (basegot != NULL);
4369       off = h->got.offset;
4370       BFD_ASSERT (off != (bfd_vma) - 1);
4371       if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
4372           || (bfd_link_pic (info)
4373               && SYMBOL_REFERENCES_LOCAL (info, h))
4374           || (ELF_ST_VISIBILITY (h->other)
4375               && h->root.type == bfd_link_hash_undefweak))
4376         {
4377           /* This is actually a static link, or it is a -Bsymbolic link
4378              and the symbol is defined locally.  We must initialize this
4379              entry in the global offset table.  Since the offset must
4380              always be a multiple of 8 (4 in the case of ILP32), we use
4381              the least significant bit to record whether we have
4382              initialized it already.
4383              When doing a dynamic link, we create a .rel(a).got relocation
4384              entry to initialize the value.  This is done in the
4385              finish_dynamic_symbol routine.  */
4386           if ((off & 1) != 0)
4387             off &= ~1;
4388           else
4389             {
4390               bfd_put_NN (output_bfd, value, basegot->contents + off);
4391               h->got.offset |= 1;
4392             }
4393         }
4394       else
4395         *unresolved_reloc_p = FALSE;
4396
4397       off = off + basegot->output_section->vma + basegot->output_offset;
4398     }
4399
4400   return off;
4401 }
4402
4403 /* Change R_TYPE to a more efficient access model where possible,
4404    return the new reloc type.  */
4405
4406 static bfd_reloc_code_real_type
4407 aarch64_tls_transition_without_check (bfd_reloc_code_real_type r_type,
4408                                       struct elf_link_hash_entry *h)
4409 {
4410   bfd_boolean is_local = h == NULL;
4411
4412   switch (r_type)
4413     {
4414     case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
4415     case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
4416       return (is_local
4417               ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1
4418               : BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21);
4419
4420     case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
4421       return (is_local
4422               ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
4423               : r_type);
4424
4425     case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
4426       return (is_local
4427               ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1
4428               : BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19);
4429
4430     case BFD_RELOC_AARCH64_TLSDESC_LDR:
4431       return (is_local
4432               ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
4433               : BFD_RELOC_AARCH64_NONE);
4434
4435     case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
4436       return (is_local
4437               ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC
4438               : BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC);
4439
4440     case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
4441       return (is_local
4442               ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2
4443               : BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1);
4444
4445     case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
4446     case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
4447       return (is_local
4448               ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
4449               : BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC);
4450
4451     case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
4452       return is_local ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1 : r_type;
4453
4454     case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
4455       return is_local ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC : r_type;
4456
4457     case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
4458       return r_type;
4459
4460     case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
4461       return (is_local
4462               ? BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12
4463               : BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19);
4464
4465     case BFD_RELOC_AARCH64_TLSDESC_ADD:
4466     case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
4467     case BFD_RELOC_AARCH64_TLSDESC_CALL:
4468       /* Instructions with these relocations will become NOPs.  */
4469       return BFD_RELOC_AARCH64_NONE;
4470
4471     case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
4472     case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
4473     case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
4474       return is_local ? BFD_RELOC_AARCH64_NONE : r_type;
4475
4476 #if ARCH_SIZE == 64
4477     case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
4478       return is_local
4479         ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC
4480         : BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC;
4481
4482     case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
4483       return is_local
4484         ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2
4485         : BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1;
4486 #endif
4487
4488     default:
4489       break;
4490     }
4491
4492   return r_type;
4493 }
4494
4495 static unsigned int
4496 aarch64_reloc_got_type (bfd_reloc_code_real_type r_type)
4497 {
4498   switch (r_type)
4499     {
4500     case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
4501     case BFD_RELOC_AARCH64_GOT_LD_PREL19:
4502     case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
4503     case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
4504     case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
4505     case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
4506     case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
4507     case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
4508     case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
4509       return GOT_NORMAL;
4510
4511     case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
4512     case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
4513     case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
4514     case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
4515     case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
4516     case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
4517     case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
4518     case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
4519       return GOT_TLS_GD;
4520
4521     case BFD_RELOC_AARCH64_TLSDESC_ADD:
4522     case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
4523     case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
4524     case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
4525     case BFD_RELOC_AARCH64_TLSDESC_CALL:
4526     case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
4527     case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
4528     case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
4529     case BFD_RELOC_AARCH64_TLSDESC_LDR:
4530     case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
4531     case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
4532       return GOT_TLSDESC_GD;
4533
4534     case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
4535     case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
4536     case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
4537     case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
4538     case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
4539     case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
4540       return GOT_TLS_IE;
4541
4542     default:
4543       break;
4544     }
4545   return GOT_UNKNOWN;
4546 }
4547
4548 static bfd_boolean
4549 aarch64_can_relax_tls (bfd *input_bfd,
4550                        struct bfd_link_info *info,
4551                        bfd_reloc_code_real_type r_type,
4552                        struct elf_link_hash_entry *h,
4553                        unsigned long r_symndx)
4554 {
4555   unsigned int symbol_got_type;
4556   unsigned int reloc_got_type;
4557
4558   if (! IS_AARCH64_TLS_RELAX_RELOC (r_type))
4559     return FALSE;
4560
4561   symbol_got_type = elfNN_aarch64_symbol_got_type (h, input_bfd, r_symndx);
4562   reloc_got_type = aarch64_reloc_got_type (r_type);
4563
4564   if (symbol_got_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (reloc_got_type))
4565     return TRUE;
4566
4567   if (bfd_link_pic (info))
4568     return FALSE;
4569
4570   if  (h && h->root.type == bfd_link_hash_undefweak)
4571     return FALSE;
4572
4573   return TRUE;
4574 }
4575
4576 /* Given the relocation code R_TYPE, return the relaxed bfd reloc
4577    enumerator.  */
4578
4579 static bfd_reloc_code_real_type
4580 aarch64_tls_transition (bfd *input_bfd,
4581                         struct bfd_link_info *info,
4582                         unsigned int r_type,
4583                         struct elf_link_hash_entry *h,
4584                         unsigned long r_symndx)
4585 {
4586   bfd_reloc_code_real_type bfd_r_type
4587     = elfNN_aarch64_bfd_reloc_from_type (r_type);
4588
4589   if (! aarch64_can_relax_tls (input_bfd, info, bfd_r_type, h, r_symndx))
4590     return bfd_r_type;
4591
4592   return aarch64_tls_transition_without_check (bfd_r_type, h);
4593 }
4594
4595 /* Return the base VMA address which should be subtracted from real addresses
4596    when resolving R_AARCH64_TLS_DTPREL relocation.  */
4597
4598 static bfd_vma
4599 dtpoff_base (struct bfd_link_info *info)
4600 {
4601   /* If tls_sec is NULL, we should have signalled an error already.  */
4602   BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
4603   return elf_hash_table (info)->tls_sec->vma;
4604 }
4605
4606 /* Return the base VMA address which should be subtracted from real addresses
4607    when resolving R_AARCH64_TLS_GOTTPREL64 relocations.  */
4608
4609 static bfd_vma
4610 tpoff_base (struct bfd_link_info *info)
4611 {
4612   struct elf_link_hash_table *htab = elf_hash_table (info);
4613
4614   /* If tls_sec is NULL, we should have signalled an error already.  */
4615   BFD_ASSERT (htab->tls_sec != NULL);
4616
4617   bfd_vma base = align_power ((bfd_vma) TCB_SIZE,
4618                               htab->tls_sec->alignment_power);
4619   return htab->tls_sec->vma - base;
4620 }
4621
4622 static bfd_vma *
4623 symbol_got_offset_ref (bfd *input_bfd, struct elf_link_hash_entry *h,
4624                        unsigned long r_symndx)
4625 {
4626   /* Calculate the address of the GOT entry for symbol
4627      referred to in h.  */
4628   if (h != NULL)
4629     return &h->got.offset;
4630   else
4631     {
4632       /* local symbol */
4633       struct elf_aarch64_local_symbol *l;
4634
4635       l = elf_aarch64_locals (input_bfd);
4636       return &l[r_symndx].got_offset;
4637     }
4638 }
4639
4640 static void
4641 symbol_got_offset_mark (bfd *input_bfd, struct elf_link_hash_entry *h,
4642                         unsigned long r_symndx)
4643 {
4644   bfd_vma *p;
4645   p = symbol_got_offset_ref (input_bfd, h, r_symndx);
4646   *p |= 1;
4647 }
4648
4649 static int
4650 symbol_got_offset_mark_p (bfd *input_bfd, struct elf_link_hash_entry *h,
4651                           unsigned long r_symndx)
4652 {
4653   bfd_vma value;
4654   value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
4655   return value & 1;
4656 }
4657
4658 static bfd_vma
4659 symbol_got_offset (bfd *input_bfd, struct elf_link_hash_entry *h,
4660                    unsigned long r_symndx)
4661 {
4662   bfd_vma value;
4663   value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
4664   value &= ~1;
4665   return value;
4666 }
4667
4668 static bfd_vma *
4669 symbol_tlsdesc_got_offset_ref (bfd *input_bfd, struct elf_link_hash_entry *h,
4670                                unsigned long r_symndx)
4671 {
4672   /* Calculate the address of the GOT entry for symbol
4673      referred to in h.  */
4674   if (h != NULL)
4675     {
4676       struct elf_aarch64_link_hash_entry *eh;
4677       eh = (struct elf_aarch64_link_hash_entry *) h;
4678       return &eh->tlsdesc_got_jump_table_offset;
4679     }
4680   else
4681     {
4682       /* local symbol */
4683       struct elf_aarch64_local_symbol *l;
4684
4685       l = elf_aarch64_locals (input_bfd);
4686       return &l[r_symndx].tlsdesc_got_jump_table_offset;
4687     }
4688 }
4689
4690 static void
4691 symbol_tlsdesc_got_offset_mark (bfd *input_bfd, struct elf_link_hash_entry *h,
4692                                 unsigned long r_symndx)
4693 {
4694   bfd_vma *p;
4695   p = symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
4696   *p |= 1;
4697 }
4698
4699 static int
4700 symbol_tlsdesc_got_offset_mark_p (bfd *input_bfd,
4701                                   struct elf_link_hash_entry *h,
4702                                   unsigned long r_symndx)
4703 {
4704   bfd_vma value;
4705   value = * symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
4706   return value & 1;
4707 }
4708
4709 static bfd_vma
4710 symbol_tlsdesc_got_offset (bfd *input_bfd, struct elf_link_hash_entry *h,
4711                           unsigned long r_symndx)
4712 {
4713   bfd_vma value;
4714   value = * symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
4715   value &= ~1;
4716   return value;
4717 }
4718
4719 /* Data for make_branch_to_erratum_835769_stub().  */
4720
4721 struct erratum_835769_branch_to_stub_data
4722 {
4723   struct bfd_link_info *info;
4724   asection *output_section;
4725   bfd_byte *contents;
4726 };
4727
4728 /* Helper to insert branches to erratum 835769 stubs in the right
4729    places for a particular section.  */
4730
4731 static bfd_boolean
4732 make_branch_to_erratum_835769_stub (struct bfd_hash_entry *gen_entry,
4733                                     void *in_arg)
4734 {
4735   struct elf_aarch64_stub_hash_entry *stub_entry;
4736   struct erratum_835769_branch_to_stub_data *data;
4737   bfd_byte *contents;
4738   unsigned long branch_insn = 0;
4739   bfd_vma veneered_insn_loc, veneer_entry_loc;
4740   bfd_signed_vma branch_offset;
4741   unsigned int target;
4742   bfd *abfd;
4743
4744   stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
4745   data = (struct erratum_835769_branch_to_stub_data *) in_arg;
4746
4747   if (stub_entry->target_section != data->output_section
4748       || stub_entry->stub_type != aarch64_stub_erratum_835769_veneer)
4749     return TRUE;
4750
4751   contents = data->contents;
4752   veneered_insn_loc = stub_entry->target_section->output_section->vma
4753                       + stub_entry->target_section->output_offset
4754                       + stub_entry->target_value;
4755   veneer_entry_loc = stub_entry->stub_sec->output_section->vma
4756                      + stub_entry->stub_sec->output_offset
4757                      + stub_entry->stub_offset;
4758   branch_offset = veneer_entry_loc - veneered_insn_loc;
4759
4760   abfd = stub_entry->target_section->owner;
4761   if (!aarch64_valid_branch_p (veneer_entry_loc, veneered_insn_loc))
4762     _bfd_error_handler
4763       (_("%B: error: Erratum 835769 stub out "
4764          "of range (input file too large)"), abfd);
4765
4766   target = stub_entry->target_value;
4767   branch_insn = 0x14000000;
4768   branch_offset >>= 2;
4769   branch_offset &= 0x3ffffff;
4770   branch_insn |= branch_offset;
4771   bfd_putl32 (branch_insn, &contents[target]);
4772
4773   return TRUE;
4774 }
4775
4776
4777 static bfd_boolean
4778 _bfd_aarch64_erratum_843419_branch_to_stub (struct bfd_hash_entry *gen_entry,
4779                                             void *in_arg)
4780 {
4781   struct elf_aarch64_stub_hash_entry *stub_entry
4782     = (struct elf_aarch64_stub_hash_entry *) gen_entry;
4783   struct erratum_835769_branch_to_stub_data *data
4784     = (struct erratum_835769_branch_to_stub_data *) in_arg;
4785   struct bfd_link_info *info;
4786   struct elf_aarch64_link_hash_table *htab;
4787   bfd_byte *contents;
4788   asection *section;
4789   bfd *abfd;
4790   bfd_vma place;
4791   uint32_t insn;
4792
4793   info = data->info;
4794   contents = data->contents;
4795   section = data->output_section;
4796
4797   htab = elf_aarch64_hash_table (info);
4798
4799   if (stub_entry->target_section != section
4800       || stub_entry->stub_type != aarch64_stub_erratum_843419_veneer)
4801     return TRUE;
4802
4803   insn = bfd_getl32 (contents + stub_entry->target_value);
4804   bfd_putl32 (insn,
4805               stub_entry->stub_sec->contents + stub_entry->stub_offset);
4806
4807   place = (section->output_section->vma + section->output_offset
4808            + stub_entry->adrp_offset);
4809   insn = bfd_getl32 (contents + stub_entry->adrp_offset);
4810
4811   if ((insn & AARCH64_ADRP_OP_MASK) !=  AARCH64_ADRP_OP)
4812     abort ();
4813
4814   bfd_signed_vma imm =
4815     (_bfd_aarch64_sign_extend
4816      ((bfd_vma) _bfd_aarch64_decode_adrp_imm (insn) << 12, 33)
4817      - (place & 0xfff));
4818
4819   if (htab->fix_erratum_843419_adr
4820       && (imm >= AARCH64_MIN_ADRP_IMM  && imm <= AARCH64_MAX_ADRP_IMM))
4821     {
4822       insn = (_bfd_aarch64_reencode_adr_imm (AARCH64_ADR_OP, imm)
4823               | AARCH64_RT (insn));
4824       bfd_putl32 (insn, contents + stub_entry->adrp_offset);
4825     }
4826   else
4827     {
4828       bfd_vma veneered_insn_loc;
4829       bfd_vma veneer_entry_loc;
4830       bfd_signed_vma branch_offset;
4831       uint32_t branch_insn;
4832
4833       veneered_insn_loc = stub_entry->target_section->output_section->vma
4834         + stub_entry->target_section->output_offset
4835         + stub_entry->target_value;
4836       veneer_entry_loc = stub_entry->stub_sec->output_section->vma
4837         + stub_entry->stub_sec->output_offset
4838         + stub_entry->stub_offset;
4839       branch_offset = veneer_entry_loc - veneered_insn_loc;
4840
4841       abfd = stub_entry->target_section->owner;
4842       if (!aarch64_valid_branch_p (veneer_entry_loc, veneered_insn_loc))
4843         _bfd_error_handler
4844           (_("%B: error: Erratum 843419 stub out "
4845              "of range (input file too large)"), abfd);
4846
4847       branch_insn = 0x14000000;
4848       branch_offset >>= 2;
4849       branch_offset &= 0x3ffffff;
4850       branch_insn |= branch_offset;
4851       bfd_putl32 (branch_insn, contents + stub_entry->target_value);
4852     }
4853   return TRUE;
4854 }
4855
4856
4857 static bfd_boolean
4858 elfNN_aarch64_write_section (bfd *output_bfd  ATTRIBUTE_UNUSED,
4859                              struct bfd_link_info *link_info,
4860                              asection *sec,
4861                              bfd_byte *contents)
4862
4863 {
4864   struct elf_aarch64_link_hash_table *globals =
4865     elf_aarch64_hash_table (link_info);
4866
4867   if (globals == NULL)
4868     return FALSE;
4869
4870   /* Fix code to point to erratum 835769 stubs.  */
4871   if (globals->fix_erratum_835769)
4872     {
4873       struct erratum_835769_branch_to_stub_data data;
4874
4875       data.info = link_info;
4876       data.output_section = sec;
4877       data.contents = contents;
4878       bfd_hash_traverse (&globals->stub_hash_table,
4879                          make_branch_to_erratum_835769_stub, &data);
4880     }
4881
4882   if (globals->fix_erratum_843419)
4883     {
4884       struct erratum_835769_branch_to_stub_data data;
4885
4886       data.info = link_info;
4887       data.output_section = sec;
4888       data.contents = contents;
4889       bfd_hash_traverse (&globals->stub_hash_table,
4890                          _bfd_aarch64_erratum_843419_branch_to_stub, &data);
4891     }
4892
4893   return FALSE;
4894 }
4895
4896 /* Perform a relocation as part of a final link.  The input relocation type
4897    should be TLS relaxed.  */
4898
4899 static bfd_reloc_status_type
4900 elfNN_aarch64_final_link_relocate (reloc_howto_type *howto,
4901                                    bfd *input_bfd,
4902                                    bfd *output_bfd,
4903                                    asection *input_section,
4904                                    bfd_byte *contents,
4905                                    Elf_Internal_Rela *rel,
4906                                    bfd_vma value,
4907                                    struct bfd_link_info *info,
4908                                    asection *sym_sec,
4909                                    struct elf_link_hash_entry *h,
4910                                    bfd_boolean *unresolved_reloc_p,
4911                                    bfd_boolean save_addend,
4912                                    bfd_vma *saved_addend,
4913                                    Elf_Internal_Sym *sym)
4914 {
4915   Elf_Internal_Shdr *symtab_hdr;
4916   unsigned int r_type = howto->type;
4917   bfd_reloc_code_real_type bfd_r_type
4918     = elfNN_aarch64_bfd_reloc_from_howto (howto);
4919   unsigned long r_symndx;
4920   bfd_byte *hit_data = contents + rel->r_offset;
4921   bfd_vma place, off;
4922   bfd_signed_vma signed_addend;
4923   struct elf_aarch64_link_hash_table *globals;
4924   bfd_boolean weak_undef_p;
4925   asection *base_got;
4926
4927   globals = elf_aarch64_hash_table (info);
4928
4929   symtab_hdr = &elf_symtab_hdr (input_bfd);
4930
4931   BFD_ASSERT (is_aarch64_elf (input_bfd));
4932
4933   r_symndx = ELFNN_R_SYM (rel->r_info);
4934
4935   place = input_section->output_section->vma
4936     + input_section->output_offset + rel->r_offset;
4937
4938   /* Get addend, accumulating the addend for consecutive relocs
4939      which refer to the same offset.  */
4940   signed_addend = saved_addend ? *saved_addend : 0;
4941   signed_addend += rel->r_addend;
4942
4943   weak_undef_p = (h ? h->root.type == bfd_link_hash_undefweak
4944                   : bfd_is_und_section (sym_sec));
4945
4946   /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
4947      it here if it is defined in a non-shared object.  */
4948   if (h != NULL
4949       && h->type == STT_GNU_IFUNC
4950       && h->def_regular)
4951     {
4952       asection *plt;
4953       const char *name;
4954       bfd_vma addend = 0;
4955
4956       if ((input_section->flags & SEC_ALLOC) == 0
4957           || h->plt.offset == (bfd_vma) -1)
4958         abort ();
4959
4960       /* STT_GNU_IFUNC symbol must go through PLT.  */
4961       plt = globals->root.splt ? globals->root.splt : globals->root.iplt;
4962       value = (plt->output_section->vma + plt->output_offset + h->plt.offset);
4963
4964       switch (bfd_r_type)
4965         {
4966         default:
4967           if (h->root.root.string)
4968             name = h->root.root.string;
4969           else
4970             name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4971                                      NULL);
4972           _bfd_error_handler
4973             (_("%B: relocation %s against STT_GNU_IFUNC "
4974                "symbol `%s' isn't handled by %s"), input_bfd,
4975              howto->name, name, __FUNCTION__);
4976           bfd_set_error (bfd_error_bad_value);
4977           return FALSE;
4978
4979         case BFD_RELOC_AARCH64_NN:
4980           if (rel->r_addend != 0)
4981             {
4982               if (h->root.root.string)
4983                 name = h->root.root.string;
4984               else
4985                 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
4986                                          sym, NULL);
4987               _bfd_error_handler
4988                 (_("%B: relocation %s against STT_GNU_IFUNC "
4989                    "symbol `%s' has non-zero addend: %d"),
4990                  input_bfd, howto->name, name, rel->r_addend);
4991               bfd_set_error (bfd_error_bad_value);
4992               return FALSE;
4993             }
4994
4995           /* Generate dynamic relocation only when there is a
4996              non-GOT reference in a shared object.  */
4997           if (bfd_link_pic (info) && h->non_got_ref)
4998             {
4999               Elf_Internal_Rela outrel;
5000               asection *sreloc;
5001
5002               /* Need a dynamic relocation to get the real function
5003                  address.  */
5004               outrel.r_offset = _bfd_elf_section_offset (output_bfd,
5005                                                          info,
5006                                                          input_section,
5007                                                          rel->r_offset);
5008               if (outrel.r_offset == (bfd_vma) -1
5009                   || outrel.r_offset == (bfd_vma) -2)
5010                 abort ();
5011
5012               outrel.r_offset += (input_section->output_section->vma
5013                                   + input_section->output_offset);
5014
5015               if (h->dynindx == -1
5016                   || h->forced_local
5017                   || bfd_link_executable (info))
5018                 {
5019                   /* This symbol is resolved locally.  */
5020                   outrel.r_info = ELFNN_R_INFO (0, AARCH64_R (IRELATIVE));
5021                   outrel.r_addend = (h->root.u.def.value
5022                                      + h->root.u.def.section->output_section->vma
5023                                      + h->root.u.def.section->output_offset);
5024                 }
5025               else
5026                 {
5027                   outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
5028                   outrel.r_addend = 0;
5029                 }
5030
5031               sreloc = globals->root.irelifunc;
5032               elf_append_rela (output_bfd, sreloc, &outrel);
5033
5034               /* If this reloc is against an external symbol, we
5035                  do not want to fiddle with the addend.  Otherwise,
5036                  we need to include the symbol value so that it
5037                  becomes an addend for the dynamic reloc.  For an
5038                  internal symbol, we have updated addend.  */
5039               return bfd_reloc_ok;
5040             }
5041           /* FALLTHROUGH */
5042         case BFD_RELOC_AARCH64_CALL26:
5043         case BFD_RELOC_AARCH64_JUMP26:
5044           value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5045                                                        signed_addend,
5046                                                        weak_undef_p);
5047           return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type,
5048                                               howto, value);
5049         case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
5050         case BFD_RELOC_AARCH64_GOT_LD_PREL19:
5051         case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
5052         case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
5053         case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
5054         case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
5055         case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
5056         case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
5057         case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
5058           base_got = globals->root.sgot;
5059           off = h->got.offset;
5060
5061           if (base_got == NULL)
5062             abort ();
5063
5064           if (off == (bfd_vma) -1)
5065             {
5066               bfd_vma plt_index;
5067
5068               /* We can't use h->got.offset here to save state, or
5069                  even just remember the offset, as finish_dynamic_symbol
5070                  would use that as offset into .got.  */
5071
5072               if (globals->root.splt != NULL)
5073                 {
5074                   plt_index = ((h->plt.offset - globals->plt_header_size) /
5075                                globals->plt_entry_size);
5076                   off = (plt_index + 3) * GOT_ENTRY_SIZE;
5077                   base_got = globals->root.sgotplt;
5078                 }
5079               else
5080                 {
5081                   plt_index = h->plt.offset / globals->plt_entry_size;
5082                   off = plt_index * GOT_ENTRY_SIZE;
5083                   base_got = globals->root.igotplt;
5084                 }
5085
5086               if (h->dynindx == -1
5087                   || h->forced_local
5088                   || info->symbolic)
5089                 {
5090                   /* This references the local definition.  We must
5091                      initialize this entry in the global offset table.
5092                      Since the offset must always be a multiple of 8,
5093                      we use the least significant bit to record
5094                      whether we have initialized it already.
5095
5096                      When doing a dynamic link, we create a .rela.got
5097                      relocation entry to initialize the value.  This
5098                      is done in the finish_dynamic_symbol routine.       */
5099                   if ((off & 1) != 0)
5100                     off &= ~1;
5101                   else
5102                     {
5103                       bfd_put_NN (output_bfd, value,
5104                                   base_got->contents + off);
5105                       /* Note that this is harmless as -1 | 1 still is -1.  */
5106                       h->got.offset |= 1;
5107                     }
5108                 }
5109               value = (base_got->output_section->vma
5110                        + base_got->output_offset + off);
5111             }
5112           else
5113             value = aarch64_calculate_got_entry_vma (h, globals, info,
5114                                                      value, output_bfd,
5115                                                      unresolved_reloc_p);
5116
5117           switch (bfd_r_type)
5118             {
5119             case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
5120             case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
5121               addend = (globals->root.sgot->output_section->vma
5122                         + globals->root.sgot->output_offset);
5123               break;
5124             case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
5125             case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
5126             case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
5127               value = (value - globals->root.sgot->output_section->vma
5128                        - globals->root.sgot->output_offset);
5129             default:
5130               break;
5131             }
5132
5133           value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5134                                                        addend, weak_undef_p);
5135           return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type, howto, value);
5136         case BFD_RELOC_AARCH64_ADD_LO12:
5137         case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
5138           break;
5139         }
5140     }
5141
5142   switch (bfd_r_type)
5143     {
5144     case BFD_RELOC_AARCH64_NONE:
5145     case BFD_RELOC_AARCH64_TLSDESC_ADD:
5146     case BFD_RELOC_AARCH64_TLSDESC_CALL:
5147     case BFD_RELOC_AARCH64_TLSDESC_LDR:
5148       *unresolved_reloc_p = FALSE;
5149       return bfd_reloc_ok;
5150
5151     case BFD_RELOC_AARCH64_NN:
5152
5153       /* When generating a shared object or relocatable executable, these
5154          relocations are copied into the output file to be resolved at
5155          run time.  */
5156       if (((bfd_link_pic (info) == TRUE)
5157            || globals->root.is_relocatable_executable)
5158           && (input_section->flags & SEC_ALLOC)
5159           && (h == NULL
5160               || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5161               || h->root.type != bfd_link_hash_undefweak))
5162         {
5163           Elf_Internal_Rela outrel;
5164           bfd_byte *loc;
5165           bfd_boolean skip, relocate;
5166           asection *sreloc;
5167
5168           *unresolved_reloc_p = FALSE;
5169
5170           skip = FALSE;
5171           relocate = FALSE;
5172
5173           outrel.r_addend = signed_addend;
5174           outrel.r_offset =
5175             _bfd_elf_section_offset (output_bfd, info, input_section,
5176                                      rel->r_offset);
5177           if (outrel.r_offset == (bfd_vma) - 1)
5178             skip = TRUE;
5179           else if (outrel.r_offset == (bfd_vma) - 2)
5180             {
5181               skip = TRUE;
5182               relocate = TRUE;
5183             }
5184
5185           outrel.r_offset += (input_section->output_section->vma
5186                               + input_section->output_offset);
5187
5188           if (skip)
5189             memset (&outrel, 0, sizeof outrel);
5190           else if (h != NULL
5191                    && h->dynindx != -1
5192                    && (!bfd_link_pic (info)
5193                        || !SYMBOLIC_BIND (info, h)
5194                        || !h->def_regular))
5195             outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
5196           else
5197             {
5198               int symbol;
5199
5200               /* On SVR4-ish systems, the dynamic loader cannot
5201                  relocate the text and data segments independently,
5202                  so the symbol does not matter.  */
5203               symbol = 0;
5204               relocate = globals->no_apply_dynamic_relocs ? FALSE : TRUE;
5205               outrel.r_info = ELFNN_R_INFO (symbol, AARCH64_R (RELATIVE));
5206               outrel.r_addend += value;
5207             }
5208
5209           sreloc = elf_section_data (input_section)->sreloc;
5210           if (sreloc == NULL || sreloc->contents == NULL)
5211             return bfd_reloc_notsupported;
5212
5213           loc = sreloc->contents + sreloc->reloc_count++ * RELOC_SIZE (globals);
5214           bfd_elfNN_swap_reloca_out (output_bfd, &outrel, loc);
5215
5216           if (sreloc->reloc_count * RELOC_SIZE (globals) > sreloc->size)
5217             {
5218               /* Sanity to check that we have previously allocated
5219                  sufficient space in the relocation section for the
5220                  number of relocations we actually want to emit.  */
5221               abort ();
5222             }
5223
5224           /* If this reloc is against an external symbol, we do not want to
5225              fiddle with the addend.  Otherwise, we need to include the symbol
5226              value so that it becomes an addend for the dynamic reloc.  */
5227           if (!relocate)
5228             return bfd_reloc_ok;
5229
5230           return _bfd_final_link_relocate (howto, input_bfd, input_section,
5231                                            contents, rel->r_offset, value,
5232                                            signed_addend);
5233         }
5234       else
5235         value += signed_addend;
5236       break;
5237
5238     case BFD_RELOC_AARCH64_CALL26:
5239     case BFD_RELOC_AARCH64_JUMP26:
5240       {
5241         asection *splt = globals->root.splt;
5242         bfd_boolean via_plt_p =
5243           splt != NULL && h != NULL && h->plt.offset != (bfd_vma) - 1;
5244
5245         /* A call to an undefined weak symbol is converted to a jump to
5246            the next instruction unless a PLT entry will be created.
5247            The jump to the next instruction is optimized as a NOP.
5248            Do the same for local undefined symbols.  */
5249         if (weak_undef_p && ! via_plt_p)
5250           {
5251             bfd_putl32 (INSN_NOP, hit_data);
5252             return bfd_reloc_ok;
5253           }
5254
5255         /* If the call goes through a PLT entry, make sure to
5256            check distance to the right destination address.  */
5257         if (via_plt_p)
5258           value = (splt->output_section->vma
5259                    + splt->output_offset + h->plt.offset);
5260
5261         /* Check if a stub has to be inserted because the destination
5262            is too far away.  */
5263         struct elf_aarch64_stub_hash_entry *stub_entry = NULL;
5264
5265         /* If the branch destination is directed to plt stub, "value" will be
5266            the final destination, otherwise we should plus signed_addend, it may
5267            contain non-zero value, for example call to local function symbol
5268            which are turned into "sec_sym + sec_off", and sec_off is kept in
5269            signed_addend.  */
5270         if (! aarch64_valid_branch_p (via_plt_p ? value : value + signed_addend,
5271                                       place))
5272           /* The target is out of reach, so redirect the branch to
5273              the local stub for this function.  */
5274         stub_entry = elfNN_aarch64_get_stub_entry (input_section, sym_sec, h,
5275                                                    rel, globals);
5276         if (stub_entry != NULL)
5277           {
5278             value = (stub_entry->stub_offset
5279                      + stub_entry->stub_sec->output_offset
5280                      + stub_entry->stub_sec->output_section->vma);
5281
5282             /* We have redirected the destination to stub entry address,
5283                so ignore any addend record in the original rela entry.  */
5284             signed_addend = 0;
5285           }
5286       }
5287       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5288                                                    signed_addend, weak_undef_p);
5289       *unresolved_reloc_p = FALSE;
5290       break;
5291
5292     case BFD_RELOC_AARCH64_16_PCREL:
5293     case BFD_RELOC_AARCH64_32_PCREL:
5294     case BFD_RELOC_AARCH64_64_PCREL:
5295     case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
5296     case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
5297     case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
5298     case BFD_RELOC_AARCH64_LD_LO19_PCREL:
5299       if (bfd_link_pic (info)
5300           && (input_section->flags & SEC_ALLOC) != 0
5301           && (input_section->flags & SEC_READONLY) != 0
5302           && h != NULL
5303           && !h->def_regular)
5304         {
5305           int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
5306
5307           _bfd_error_handler
5308             (_("%B: relocation %s against external symbol `%s' can not be used"
5309                " when making a shared object; recompile with -fPIC"),
5310              input_bfd, elfNN_aarch64_howto_table[howto_index].name,
5311              h->root.root.string);
5312           bfd_set_error (bfd_error_bad_value);
5313           return FALSE;
5314         }
5315       /* Fall through.  */
5316
5317     case BFD_RELOC_AARCH64_16:
5318 #if ARCH_SIZE == 64
5319     case BFD_RELOC_AARCH64_32:
5320 #endif
5321     case BFD_RELOC_AARCH64_ADD_LO12:
5322     case BFD_RELOC_AARCH64_BRANCH19:
5323     case BFD_RELOC_AARCH64_LDST128_LO12:
5324     case BFD_RELOC_AARCH64_LDST16_LO12:
5325     case BFD_RELOC_AARCH64_LDST32_LO12:
5326     case BFD_RELOC_AARCH64_LDST64_LO12:
5327     case BFD_RELOC_AARCH64_LDST8_LO12:
5328     case BFD_RELOC_AARCH64_MOVW_G0:
5329     case BFD_RELOC_AARCH64_MOVW_G0_NC:
5330     case BFD_RELOC_AARCH64_MOVW_G0_S:
5331     case BFD_RELOC_AARCH64_MOVW_G1:
5332     case BFD_RELOC_AARCH64_MOVW_G1_NC:
5333     case BFD_RELOC_AARCH64_MOVW_G1_S:
5334     case BFD_RELOC_AARCH64_MOVW_G2:
5335     case BFD_RELOC_AARCH64_MOVW_G2_NC:
5336     case BFD_RELOC_AARCH64_MOVW_G2_S:
5337     case BFD_RELOC_AARCH64_MOVW_G3:
5338     case BFD_RELOC_AARCH64_TSTBR14:
5339       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5340                                                    signed_addend, weak_undef_p);
5341       break;
5342
5343     case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
5344     case BFD_RELOC_AARCH64_GOT_LD_PREL19:
5345     case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
5346     case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
5347     case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
5348     case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
5349       if (globals->root.sgot == NULL)
5350         BFD_ASSERT (h != NULL);
5351
5352       if (h != NULL)
5353         {
5354           bfd_vma addend = 0;
5355           value = aarch64_calculate_got_entry_vma (h, globals, info, value,
5356                                                    output_bfd,
5357                                                    unresolved_reloc_p);
5358           if (bfd_r_type == BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15
5359               || bfd_r_type == BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14)
5360             addend = (globals->root.sgot->output_section->vma
5361                       + globals->root.sgot->output_offset);
5362           value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5363                                                        addend, weak_undef_p);
5364         }
5365       else
5366       {
5367         bfd_vma addend = 0;
5368         struct elf_aarch64_local_symbol *locals
5369           = elf_aarch64_locals (input_bfd);
5370
5371         if (locals == NULL)
5372           {
5373             int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
5374             _bfd_error_handler
5375               (_("%B: Local symbol descriptor table be NULL when applying "
5376                  "relocation %s against local symbol"),
5377                input_bfd, elfNN_aarch64_howto_table[howto_index].name);
5378             abort ();
5379           }
5380
5381         off = symbol_got_offset (input_bfd, h, r_symndx);
5382         base_got = globals->root.sgot;
5383         bfd_vma got_entry_addr = (base_got->output_section->vma
5384                                   + base_got->output_offset + off);
5385
5386         if (!symbol_got_offset_mark_p (input_bfd, h, r_symndx))
5387           {
5388             bfd_put_64 (output_bfd, value, base_got->contents + off);
5389
5390             if (bfd_link_pic (info))
5391               {
5392                 asection *s;
5393                 Elf_Internal_Rela outrel;
5394
5395                 /* For local symbol, we have done absolute relocation in static
5396                    linking stageh. While for share library, we need to update
5397                    the content of GOT entry according to the share objects
5398                    loading base address. So we need to generate a
5399                    R_AARCH64_RELATIVE reloc for dynamic linker.  */
5400                 s = globals->root.srelgot;
5401                 if (s == NULL)
5402                   abort ();
5403
5404                 outrel.r_offset = got_entry_addr;
5405                 outrel.r_info = ELFNN_R_INFO (0, AARCH64_R (RELATIVE));
5406                 outrel.r_addend = value;
5407                 elf_append_rela (output_bfd, s, &outrel);
5408               }
5409
5410             symbol_got_offset_mark (input_bfd, h, r_symndx);
5411           }
5412
5413         /* Update the relocation value to GOT entry addr as we have transformed
5414            the direct data access into indirect data access through GOT.  */
5415         value = got_entry_addr;
5416
5417         if (bfd_r_type == BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15
5418             || bfd_r_type == BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14)
5419           addend = base_got->output_section->vma + base_got->output_offset;
5420
5421         value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5422                                                      addend, weak_undef_p);
5423       }
5424
5425       break;
5426
5427     case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
5428     case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
5429     case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
5430       if (h != NULL)
5431           value = aarch64_calculate_got_entry_vma (h, globals, info, value,
5432                                                    output_bfd,
5433                                                    unresolved_reloc_p);
5434       else
5435         {
5436           struct elf_aarch64_local_symbol *locals
5437             = elf_aarch64_locals (input_bfd);
5438
5439           if (locals == NULL)
5440             {
5441               int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
5442               _bfd_error_handler
5443                 (_("%B: Local symbol descriptor table be NULL when applying "
5444                    "relocation %s against local symbol"),
5445                  input_bfd, elfNN_aarch64_howto_table[howto_index].name);
5446               abort ();
5447             }
5448
5449           off = symbol_got_offset (input_bfd, h, r_symndx);
5450           base_got = globals->root.sgot;
5451           if (base_got == NULL)
5452             abort ();
5453
5454           bfd_vma got_entry_addr = (base_got->output_section->vma
5455                                     + base_got->output_offset + off);
5456
5457           if (!symbol_got_offset_mark_p (input_bfd, h, r_symndx))
5458             {
5459               bfd_put_64 (output_bfd, value, base_got->contents + off);
5460
5461               if (bfd_link_pic (info))
5462                 {
5463                   asection *s;
5464                   Elf_Internal_Rela outrel;
5465
5466                   /* For local symbol, we have done absolute relocation in static
5467                      linking stage.  While for share library, we need to update
5468                      the content of GOT entry according to the share objects
5469                      loading base address.  So we need to generate a
5470                      R_AARCH64_RELATIVE reloc for dynamic linker.  */
5471                   s = globals->root.srelgot;
5472                   if (s == NULL)
5473                     abort ();
5474
5475                   outrel.r_offset = got_entry_addr;
5476                   outrel.r_info = ELFNN_R_INFO (0, AARCH64_R (RELATIVE));
5477                   outrel.r_addend = value;
5478                   elf_append_rela (output_bfd, s, &outrel);
5479                 }
5480
5481               symbol_got_offset_mark (input_bfd, h, r_symndx);
5482             }
5483         }
5484
5485       /* Update the relocation value to GOT entry addr as we have transformed
5486          the direct data access into indirect data access through GOT.  */
5487       value = symbol_got_offset (input_bfd, h, r_symndx);
5488       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5489                                                    0, weak_undef_p);
5490       *unresolved_reloc_p = FALSE;
5491       break;
5492
5493     case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
5494     case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
5495     case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
5496     case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5497     case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
5498     case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5499     case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
5500     case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
5501     case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
5502     case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
5503       if (globals->root.sgot == NULL)
5504         return bfd_reloc_notsupported;
5505
5506       value = (symbol_got_offset (input_bfd, h, r_symndx)
5507                + globals->root.sgot->output_section->vma
5508                + globals->root.sgot->output_offset);
5509
5510       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5511                                                    0, weak_undef_p);
5512       *unresolved_reloc_p = FALSE;
5513       break;
5514
5515     case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
5516     case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
5517     case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
5518     case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
5519       if (globals->root.sgot == NULL)
5520         return bfd_reloc_notsupported;
5521
5522       value = symbol_got_offset (input_bfd, h, r_symndx);
5523       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5524                                                    0, weak_undef_p);
5525       *unresolved_reloc_p = FALSE;
5526       break;
5527
5528     case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12:
5529     case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12:
5530     case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
5531     case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12:
5532     case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC:
5533     case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12:
5534     case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC:
5535     case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12:
5536     case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC:
5537     case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12:
5538     case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC:
5539     case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0:
5540     case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
5541     case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1:
5542     case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC:
5543     case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2:
5544       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5545                                                    signed_addend - dtpoff_base (info),
5546                                                    weak_undef_p);
5547       break;
5548
5549     case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
5550     case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
5551     case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5552     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
5553     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5554     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
5555     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5556     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
5557       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5558                                                    signed_addend - tpoff_base (info),
5559                                                    weak_undef_p);
5560       *unresolved_reloc_p = FALSE;
5561       break;
5562
5563     case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
5564     case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
5565     case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
5566     case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
5567     case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
5568     case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
5569       if (globals->root.sgot == NULL)
5570         return bfd_reloc_notsupported;
5571       value = (symbol_tlsdesc_got_offset (input_bfd, h, r_symndx)
5572                + globals->root.sgotplt->output_section->vma
5573                + globals->root.sgotplt->output_offset
5574                + globals->sgotplt_jump_table_size);
5575
5576       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5577                                                    0, weak_undef_p);
5578       *unresolved_reloc_p = FALSE;
5579       break;
5580
5581     case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
5582     case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
5583       if (globals->root.sgot == NULL)
5584         return bfd_reloc_notsupported;
5585
5586       value = (symbol_tlsdesc_got_offset (input_bfd, h, r_symndx)
5587                + globals->root.sgotplt->output_section->vma
5588                + globals->root.sgotplt->output_offset
5589                + globals->sgotplt_jump_table_size);
5590
5591       value -= (globals->root.sgot->output_section->vma
5592                 + globals->root.sgot->output_offset);
5593
5594       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5595                                                    0, weak_undef_p);
5596       *unresolved_reloc_p = FALSE;
5597       break;
5598
5599     default:
5600       return bfd_reloc_notsupported;
5601     }
5602
5603   if (saved_addend)
5604     *saved_addend = value;
5605
5606   /* Only apply the final relocation in a sequence.  */
5607   if (save_addend)
5608     return bfd_reloc_continue;
5609
5610   return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type,
5611                                       howto, value);
5612 }
5613
5614 /* Handle TLS relaxations.  Relaxing is possible for symbols that use
5615    R_AARCH64_TLSDESC_ADR_{PAGE, LD64_LO12_NC, ADD_LO12_NC} during a static
5616    link.
5617
5618    Return bfd_reloc_ok if we're done, bfd_reloc_continue if the caller
5619    is to then call final_link_relocate.  Return other values in the
5620    case of error.  */
5621
5622 static bfd_reloc_status_type
5623 elfNN_aarch64_tls_relax (struct elf_aarch64_link_hash_table *globals,
5624                          bfd *input_bfd, bfd_byte *contents,
5625                          Elf_Internal_Rela *rel, struct elf_link_hash_entry *h)
5626 {
5627   bfd_boolean is_local = h == NULL;
5628   unsigned int r_type = ELFNN_R_TYPE (rel->r_info);
5629   unsigned long insn;
5630
5631   BFD_ASSERT (globals && input_bfd && contents && rel);
5632
5633   switch (elfNN_aarch64_bfd_reloc_from_type (r_type))
5634     {
5635     case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
5636     case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
5637       if (is_local)
5638         {
5639           /* GD->LE relaxation:
5640              adrp x0, :tlsgd:var     =>   movz x0, :tprel_g1:var
5641              or
5642              adrp x0, :tlsdesc:var   =>   movz x0, :tprel_g1:var
5643            */
5644           bfd_putl32 (0xd2a00000, contents + rel->r_offset);
5645           return bfd_reloc_continue;
5646         }
5647       else
5648         {
5649           /* GD->IE relaxation:
5650              adrp x0, :tlsgd:var     =>   adrp x0, :gottprel:var
5651              or
5652              adrp x0, :tlsdesc:var   =>   adrp x0, :gottprel:var
5653            */
5654           return bfd_reloc_continue;
5655         }
5656
5657     case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
5658       BFD_ASSERT (0);
5659       break;
5660
5661     case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
5662       if (is_local)
5663         {
5664           /* Tiny TLSDESC->LE relaxation:
5665              ldr   x1, :tlsdesc:var      =>  movz  x0, #:tprel_g1:var
5666              adr   x0, :tlsdesc:var      =>  movk  x0, #:tprel_g0_nc:var
5667              .tlsdesccall var
5668              blr   x1                    =>  nop
5669            */
5670           BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (TLSDESC_ADR_PREL21));
5671           BFD_ASSERT (ELFNN_R_TYPE (rel[2].r_info) == AARCH64_R (TLSDESC_CALL));
5672
5673           rel[1].r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
5674                                         AARCH64_R (TLSLE_MOVW_TPREL_G0_NC));
5675           rel[2].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5676
5677           bfd_putl32 (0xd2a00000, contents + rel->r_offset);
5678           bfd_putl32 (0xf2800000, contents + rel->r_offset + 4);
5679           bfd_putl32 (INSN_NOP, contents + rel->r_offset + 8);
5680           return bfd_reloc_continue;
5681         }
5682       else
5683         {
5684           /* Tiny TLSDESC->IE relaxation:
5685              ldr   x1, :tlsdesc:var      =>  ldr   x0, :gottprel:var
5686              adr   x0, :tlsdesc:var      =>  nop
5687              .tlsdesccall var
5688              blr   x1                    =>  nop
5689            */
5690           BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (TLSDESC_ADR_PREL21));
5691           BFD_ASSERT (ELFNN_R_TYPE (rel[2].r_info) == AARCH64_R (TLSDESC_CALL));
5692
5693           rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5694           rel[2].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5695
5696           bfd_putl32 (0x58000000, contents + rel->r_offset);
5697           bfd_putl32 (INSN_NOP, contents + rel->r_offset + 4);
5698           bfd_putl32 (INSN_NOP, contents + rel->r_offset + 8);
5699           return bfd_reloc_continue;
5700         }
5701
5702     case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
5703       if (is_local)
5704         {
5705           /* Tiny GD->LE relaxation:
5706              adr x0, :tlsgd:var      =>   mrs  x1, tpidr_el0
5707              bl   __tls_get_addr     =>   add  x0, x1, #:tprel_hi12:x, lsl #12
5708              nop                     =>   add  x0, x0, #:tprel_lo12_nc:x
5709            */
5710
5711           /* First kill the tls_get_addr reloc on the bl instruction.  */
5712           BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
5713
5714           bfd_putl32 (0xd53bd041, contents + rel->r_offset + 0);
5715           bfd_putl32 (0x91400020, contents + rel->r_offset + 4);
5716           bfd_putl32 (0x91000000, contents + rel->r_offset + 8);
5717
5718           rel[1].r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
5719                                         AARCH64_R (TLSLE_ADD_TPREL_LO12_NC));
5720           rel[1].r_offset = rel->r_offset + 8;
5721
5722           /* Move the current relocation to the second instruction in
5723              the sequence.  */
5724           rel->r_offset += 4;
5725           rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
5726                                       AARCH64_R (TLSLE_ADD_TPREL_HI12));
5727           return bfd_reloc_continue;
5728         }
5729       else
5730         {
5731           /* Tiny GD->IE relaxation:
5732              adr x0, :tlsgd:var      =>   ldr  x0, :gottprel:var
5733              bl   __tls_get_addr     =>   mrs  x1, tpidr_el0
5734              nop                     =>   add  x0, x0, x1
5735            */
5736
5737           /* First kill the tls_get_addr reloc on the bl instruction.  */
5738           BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
5739           rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5740
5741           bfd_putl32 (0x58000000, contents + rel->r_offset);
5742           bfd_putl32 (0xd53bd041, contents + rel->r_offset + 4);
5743           bfd_putl32 (0x8b000020, contents + rel->r_offset + 8);
5744           return bfd_reloc_continue;
5745         }
5746
5747 #if ARCH_SIZE == 64
5748     case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
5749       BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (TLSGD_MOVW_G0_NC));
5750       BFD_ASSERT (rel->r_offset + 12 == rel[2].r_offset);
5751       BFD_ASSERT (ELFNN_R_TYPE (rel[2].r_info) == AARCH64_R (CALL26));
5752
5753       if (is_local)
5754         {
5755           /* Large GD->LE relaxation:
5756              movz x0, #:tlsgd_g1:var    => movz x0, #:tprel_g2:var, lsl #32
5757              movk x0, #:tlsgd_g0_nc:var => movk x0, #:tprel_g1_nc:var, lsl #16
5758              add x0, gp, x0             => movk x0, #:tprel_g0_nc:var
5759              bl __tls_get_addr          => mrs x1, tpidr_el0
5760              nop                        => add x0, x0, x1
5761            */
5762           rel[2].r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
5763                                         AARCH64_R (TLSLE_MOVW_TPREL_G0_NC));
5764           rel[2].r_offset = rel->r_offset + 8;
5765
5766           bfd_putl32 (0xd2c00000, contents + rel->r_offset + 0);
5767           bfd_putl32 (0xf2a00000, contents + rel->r_offset + 4);
5768           bfd_putl32 (0xf2800000, contents + rel->r_offset + 8);
5769           bfd_putl32 (0xd53bd041, contents + rel->r_offset + 12);
5770           bfd_putl32 (0x8b000020, contents + rel->r_offset + 16);
5771         }
5772       else
5773         {
5774           /* Large GD->IE relaxation:
5775              movz x0, #:tlsgd_g1:var    => movz x0, #:gottprel_g1:var, lsl #16
5776              movk x0, #:tlsgd_g0_nc:var => movk x0, #:gottprel_g0_nc:var
5777              add x0, gp, x0             => ldr x0, [gp, x0]
5778              bl __tls_get_addr          => mrs x1, tpidr_el0
5779              nop                        => add x0, x0, x1
5780            */
5781           rel[2].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5782           bfd_putl32 (0xd2a80000, contents + rel->r_offset + 0);
5783           bfd_putl32 (0x58000000, contents + rel->r_offset + 8);
5784           bfd_putl32 (0xd53bd041, contents + rel->r_offset + 12);
5785           bfd_putl32 (0x8b000020, contents + rel->r_offset + 16);
5786         }
5787       return bfd_reloc_continue;
5788
5789     case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
5790       return bfd_reloc_continue;
5791 #endif
5792
5793     case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
5794       return bfd_reloc_continue;
5795
5796     case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
5797       if (is_local)
5798         {
5799           /* GD->LE relaxation:
5800              ldr xd, [x0, #:tlsdesc_lo12:var]   =>   movk x0, :tprel_g0_nc:var
5801            */
5802           bfd_putl32 (0xf2800000, contents + rel->r_offset);
5803           return bfd_reloc_continue;
5804         }
5805       else
5806         {
5807           /* GD->IE relaxation:
5808              ldr xd, [x0, #:tlsdesc_lo12:var] => ldr x0, [x0, #:gottprel_lo12:var]
5809            */
5810           insn = bfd_getl32 (contents + rel->r_offset);
5811           insn &= 0xffffffe0;
5812           bfd_putl32 (insn, contents + rel->r_offset);
5813           return bfd_reloc_continue;
5814         }
5815
5816     case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
5817       if (is_local)
5818         {
5819           /* GD->LE relaxation
5820              add  x0, #:tlsgd_lo12:var  => movk x0, :tprel_g0_nc:var
5821              bl   __tls_get_addr        => mrs  x1, tpidr_el0
5822              nop                        => add  x0, x1, x0
5823            */
5824
5825           /* First kill the tls_get_addr reloc on the bl instruction.  */
5826           BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
5827           rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5828
5829           bfd_putl32 (0xf2800000, contents + rel->r_offset);
5830           bfd_putl32 (0xd53bd041, contents + rel->r_offset + 4);
5831           bfd_putl32 (0x8b000020, contents + rel->r_offset + 8);
5832           return bfd_reloc_continue;
5833         }
5834       else
5835         {
5836           /* GD->IE relaxation
5837              ADD  x0, #:tlsgd_lo12:var  => ldr  x0, [x0, #:gottprel_lo12:var]
5838              BL   __tls_get_addr        => mrs  x1, tpidr_el0
5839                R_AARCH64_CALL26
5840              NOP                        => add  x0, x1, x0
5841            */
5842
5843           BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
5844
5845           /* Remove the relocation on the BL instruction.  */
5846           rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5847
5848           bfd_putl32 (0xf9400000, contents + rel->r_offset);
5849
5850           /* We choose to fixup the BL and NOP instructions using the
5851              offset from the second relocation to allow flexibility in
5852              scheduling instructions between the ADD and BL.  */
5853           bfd_putl32 (0xd53bd041, contents + rel[1].r_offset);
5854           bfd_putl32 (0x8b000020, contents + rel[1].r_offset + 4);
5855           return bfd_reloc_continue;
5856         }
5857
5858     case BFD_RELOC_AARCH64_TLSDESC_ADD:
5859     case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
5860     case BFD_RELOC_AARCH64_TLSDESC_CALL:
5861       /* GD->IE/LE relaxation:
5862          add x0, x0, #:tlsdesc_lo12:var   =>   nop
5863          blr xd                           =>   nop
5864        */
5865       bfd_putl32 (INSN_NOP, contents + rel->r_offset);
5866       return bfd_reloc_ok;
5867
5868     case BFD_RELOC_AARCH64_TLSDESC_LDR:
5869       if (is_local)
5870         {
5871           /* GD->LE relaxation:
5872              ldr xd, [gp, xn]   =>   movk x0, #:tprel_g0_nc:var
5873            */
5874           bfd_putl32 (0xf2800000, contents + rel->r_offset);
5875           return bfd_reloc_continue;
5876         }
5877       else
5878         {
5879           /* GD->IE relaxation:
5880              ldr xd, [gp, xn]   =>   ldr x0, [gp, xn]
5881            */
5882           insn = bfd_getl32 (contents + rel->r_offset);
5883           insn &= 0xffffffe0;
5884           bfd_putl32 (insn, contents + rel->r_offset);
5885           return bfd_reloc_ok;
5886         }
5887
5888     case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
5889       /* GD->LE relaxation:
5890          movk xd, #:tlsdesc_off_g0_nc:var => movk x0, #:tprel_g1_nc:var, lsl #16
5891          GD->IE relaxation:
5892          movk xd, #:tlsdesc_off_g0_nc:var => movk xd, #:gottprel_g0_nc:var
5893       */
5894       if (is_local)
5895         bfd_putl32 (0xf2a00000, contents + rel->r_offset);
5896       return bfd_reloc_continue;
5897
5898     case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
5899       if (is_local)
5900         {
5901           /* GD->LE relaxation:
5902              movz xd, #:tlsdesc_off_g1:var => movz x0, #:tprel_g2:var, lsl #32
5903           */
5904           bfd_putl32 (0xd2c00000, contents + rel->r_offset);
5905           return bfd_reloc_continue;
5906         }
5907       else
5908         {
5909           /*  GD->IE relaxation:
5910               movz xd, #:tlsdesc_off_g1:var => movz xd, #:gottprel_g1:var, lsl #16
5911           */
5912           insn = bfd_getl32 (contents + rel->r_offset);
5913           bfd_putl32 (0xd2a00000 | (insn & 0x1f), contents + rel->r_offset);
5914           return bfd_reloc_continue;
5915         }
5916
5917     case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5918       /* IE->LE relaxation:
5919          adrp xd, :gottprel:var   =>   movz xd, :tprel_g1:var
5920        */
5921       if (is_local)
5922         {
5923           insn = bfd_getl32 (contents + rel->r_offset);
5924           bfd_putl32 (0xd2a00000 | (insn & 0x1f), contents + rel->r_offset);
5925         }
5926       return bfd_reloc_continue;
5927
5928     case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
5929       /* IE->LE relaxation:
5930          ldr  xd, [xm, #:gottprel_lo12:var]   =>   movk xd, :tprel_g0_nc:var
5931        */
5932       if (is_local)
5933         {
5934           insn = bfd_getl32 (contents + rel->r_offset);
5935           bfd_putl32 (0xf2800000 | (insn & 0x1f), contents + rel->r_offset);
5936         }
5937       return bfd_reloc_continue;
5938
5939     case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
5940       /* LD->LE relaxation (tiny):
5941          adr  x0, :tlsldm:x  => mrs x0, tpidr_el0
5942          bl   __tls_get_addr => add x0, x0, TCB_SIZE
5943        */
5944       if (is_local)
5945         {
5946           BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
5947           BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
5948           /* No need of CALL26 relocation for tls_get_addr.  */
5949           rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5950           bfd_putl32 (0xd53bd040, contents + rel->r_offset + 0);
5951           bfd_putl32 (0x91004000, contents + rel->r_offset + 4);
5952           return bfd_reloc_ok;
5953         }
5954       return bfd_reloc_continue;
5955
5956     case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
5957       /* LD->LE relaxation (small):
5958          adrp  x0, :tlsldm:x       => mrs x0, tpidr_el0
5959        */
5960       if (is_local)
5961         {
5962           bfd_putl32 (0xd53bd040, contents + rel->r_offset);
5963           return bfd_reloc_ok;
5964         }
5965       return bfd_reloc_continue;
5966
5967     case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
5968       /* LD->LE relaxation (small):
5969          add   x0, #:tlsldm_lo12:x => add x0, x0, TCB_SIZE
5970          bl   __tls_get_addr       => nop
5971        */
5972       if (is_local)
5973         {
5974           BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
5975           BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
5976           /* No need of CALL26 relocation for tls_get_addr.  */
5977           rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
5978           bfd_putl32 (0x91004000, contents + rel->r_offset + 0);
5979           bfd_putl32 (0xd503201f, contents + rel->r_offset + 4);
5980           return bfd_reloc_ok;
5981         }
5982       return bfd_reloc_continue;
5983
5984     default:
5985       return bfd_reloc_continue;
5986     }
5987
5988   return bfd_reloc_ok;
5989 }
5990
5991 /* Relocate an AArch64 ELF section.  */
5992
5993 static bfd_boolean
5994 elfNN_aarch64_relocate_section (bfd *output_bfd,
5995                                 struct bfd_link_info *info,
5996                                 bfd *input_bfd,
5997                                 asection *input_section,
5998                                 bfd_byte *contents,
5999                                 Elf_Internal_Rela *relocs,
6000                                 Elf_Internal_Sym *local_syms,
6001                                 asection **local_sections)
6002 {
6003   Elf_Internal_Shdr *symtab_hdr;
6004   struct elf_link_hash_entry **sym_hashes;
6005   Elf_Internal_Rela *rel;
6006   Elf_Internal_Rela *relend;
6007   const char *name;
6008   struct elf_aarch64_link_hash_table *globals;
6009   bfd_boolean save_addend = FALSE;
6010   bfd_vma addend = 0;
6011
6012   globals = elf_aarch64_hash_table (info);
6013
6014   symtab_hdr = &elf_symtab_hdr (input_bfd);
6015   sym_hashes = elf_sym_hashes (input_bfd);
6016
6017   rel = relocs;
6018   relend = relocs + input_section->reloc_count;
6019   for (; rel < relend; rel++)
6020     {
6021       unsigned int r_type;
6022       bfd_reloc_code_real_type bfd_r_type;
6023       bfd_reloc_code_real_type relaxed_bfd_r_type;
6024       reloc_howto_type *howto;
6025       unsigned long r_symndx;
6026       Elf_Internal_Sym *sym;
6027       asection *sec;
6028       struct elf_link_hash_entry *h;
6029       bfd_vma relocation;
6030       bfd_reloc_status_type r;
6031       arelent bfd_reloc;
6032       char sym_type;
6033       bfd_boolean unresolved_reloc = FALSE;
6034       char *error_message = NULL;
6035
6036       r_symndx = ELFNN_R_SYM (rel->r_info);
6037       r_type = ELFNN_R_TYPE (rel->r_info);
6038
6039       bfd_reloc.howto = elfNN_aarch64_howto_from_type (r_type);
6040       howto = bfd_reloc.howto;
6041
6042       if (howto == NULL)
6043         {
6044           _bfd_error_handler
6045             (_("%B: unrecognized relocation (0x%x) in section `%A'"),
6046              input_bfd, input_section, r_type);
6047           return FALSE;
6048         }
6049       bfd_r_type = elfNN_aarch64_bfd_reloc_from_howto (howto);
6050
6051       h = NULL;
6052       sym = NULL;
6053       sec = NULL;
6054
6055       if (r_symndx < symtab_hdr->sh_info)
6056         {
6057           sym = local_syms + r_symndx;
6058           sym_type = ELFNN_ST_TYPE (sym->st_info);
6059           sec = local_sections[r_symndx];
6060
6061           /* An object file might have a reference to a local
6062              undefined symbol.  This is a daft object file, but we
6063              should at least do something about it.  */
6064           if (r_type != R_AARCH64_NONE && r_type != R_AARCH64_NULL
6065               && bfd_is_und_section (sec)
6066               && ELF_ST_BIND (sym->st_info) != STB_WEAK)
6067             (*info->callbacks->undefined_symbol)
6068               (info, bfd_elf_string_from_elf_section
6069                (input_bfd, symtab_hdr->sh_link, sym->st_name),
6070                input_bfd, input_section, rel->r_offset, TRUE);
6071
6072           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
6073
6074           /* Relocate against local STT_GNU_IFUNC symbol.  */
6075           if (!bfd_link_relocatable (info)
6076               && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
6077             {
6078               h = elfNN_aarch64_get_local_sym_hash (globals, input_bfd,
6079                                                     rel, FALSE);
6080               if (h == NULL)
6081                 abort ();
6082
6083               /* Set STT_GNU_IFUNC symbol value.  */
6084               h->root.u.def.value = sym->st_value;
6085               h->root.u.def.section = sec;
6086             }
6087         }
6088       else
6089         {
6090           bfd_boolean warned, ignored;
6091
6092           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
6093                                    r_symndx, symtab_hdr, sym_hashes,
6094                                    h, sec, relocation,
6095                                    unresolved_reloc, warned, ignored);
6096
6097           sym_type = h->type;
6098         }
6099
6100       if (sec != NULL && discarded_section (sec))
6101         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
6102                                          rel, 1, relend, howto, 0, contents);
6103
6104       if (bfd_link_relocatable (info))
6105         continue;
6106
6107       if (h != NULL)
6108         name = h->root.root.string;
6109       else
6110         {
6111           name = (bfd_elf_string_from_elf_section
6112                   (input_bfd, symtab_hdr->sh_link, sym->st_name));
6113           if (name == NULL || *name == '\0')
6114             name = bfd_section_name (input_bfd, sec);
6115         }
6116
6117       if (r_symndx != 0
6118           && r_type != R_AARCH64_NONE
6119           && r_type != R_AARCH64_NULL
6120           && (h == NULL
6121               || h->root.type == bfd_link_hash_defined
6122               || h->root.type == bfd_link_hash_defweak)
6123           && IS_AARCH64_TLS_RELOC (bfd_r_type) != (sym_type == STT_TLS))
6124         {
6125           _bfd_error_handler
6126             ((sym_type == STT_TLS
6127               ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
6128               : _("%B(%A+0x%lx): %s used with non-TLS symbol %s")),
6129              input_bfd,
6130              input_section, (long) rel->r_offset, howto->name, name);
6131         }
6132
6133       /* We relax only if we can see that there can be a valid transition
6134          from a reloc type to another.
6135          We call elfNN_aarch64_final_link_relocate unless we're completely
6136          done, i.e., the relaxation produced the final output we want.  */
6137
6138       relaxed_bfd_r_type = aarch64_tls_transition (input_bfd, info, r_type,
6139                                                    h, r_symndx);
6140       if (relaxed_bfd_r_type != bfd_r_type)
6141         {
6142           bfd_r_type = relaxed_bfd_r_type;
6143           howto = elfNN_aarch64_howto_from_bfd_reloc (bfd_r_type);
6144           BFD_ASSERT (howto != NULL);
6145           r_type = howto->type;
6146           r = elfNN_aarch64_tls_relax (globals, input_bfd, contents, rel, h);
6147           unresolved_reloc = 0;
6148         }
6149       else
6150         r = bfd_reloc_continue;
6151
6152       /* There may be multiple consecutive relocations for the
6153          same offset.  In that case we are supposed to treat the
6154          output of each relocation as the addend for the next.  */
6155       if (rel + 1 < relend
6156           && rel->r_offset == rel[1].r_offset
6157           && ELFNN_R_TYPE (rel[1].r_info) != R_AARCH64_NONE
6158           && ELFNN_R_TYPE (rel[1].r_info) != R_AARCH64_NULL)
6159         save_addend = TRUE;
6160       else
6161         save_addend = FALSE;
6162
6163       if (r == bfd_reloc_continue)
6164         r = elfNN_aarch64_final_link_relocate (howto, input_bfd, output_bfd,
6165                                                input_section, contents, rel,
6166                                                relocation, info, sec,
6167                                                h, &unresolved_reloc,
6168                                                save_addend, &addend, sym);
6169
6170       switch (elfNN_aarch64_bfd_reloc_from_type (r_type))
6171         {
6172         case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
6173         case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
6174         case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
6175         case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
6176         case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
6177         case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
6178         case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
6179         case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
6180           if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
6181             {
6182               bfd_boolean need_relocs = FALSE;
6183               bfd_byte *loc;
6184               int indx;
6185               bfd_vma off;
6186
6187               off = symbol_got_offset (input_bfd, h, r_symndx);
6188               indx = h && h->dynindx != -1 ? h->dynindx : 0;
6189
6190               need_relocs =
6191                 (bfd_link_pic (info) || indx != 0) &&
6192                 (h == NULL
6193                  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6194                  || h->root.type != bfd_link_hash_undefweak);
6195
6196               BFD_ASSERT (globals->root.srelgot != NULL);
6197
6198               if (need_relocs)
6199                 {
6200                   Elf_Internal_Rela rela;
6201                   rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLS_DTPMOD));
6202                   rela.r_addend = 0;
6203                   rela.r_offset = globals->root.sgot->output_section->vma +
6204                     globals->root.sgot->output_offset + off;
6205
6206
6207                   loc = globals->root.srelgot->contents;
6208                   loc += globals->root.srelgot->reloc_count++
6209                     * RELOC_SIZE (htab);
6210                   bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
6211
6212                   bfd_reloc_code_real_type real_type =
6213                     elfNN_aarch64_bfd_reloc_from_type (r_type);
6214
6215                   if (real_type == BFD_RELOC_AARCH64_TLSLD_ADR_PREL21
6216                       || real_type == BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21
6217                       || real_type == BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC)
6218                     {
6219                       /* For local dynamic, don't generate DTPREL in any case.
6220                          Initialize the DTPREL slot into zero, so we get module
6221                          base address when invoke runtime TLS resolver.  */
6222                       bfd_put_NN (output_bfd, 0,
6223                                   globals->root.sgot->contents + off
6224                                   + GOT_ENTRY_SIZE);
6225                     }
6226                   else if (indx == 0)
6227                     {
6228                       bfd_put_NN (output_bfd,
6229                                   relocation - dtpoff_base (info),
6230                                   globals->root.sgot->contents + off
6231                                   + GOT_ENTRY_SIZE);
6232                     }
6233                   else
6234                     {
6235                       /* This TLS symbol is global. We emit a
6236                          relocation to fixup the tls offset at load
6237                          time.  */
6238                       rela.r_info =
6239                         ELFNN_R_INFO (indx, AARCH64_R (TLS_DTPREL));
6240                       rela.r_addend = 0;
6241                       rela.r_offset =
6242                         (globals->root.sgot->output_section->vma
6243                          + globals->root.sgot->output_offset + off
6244                          + GOT_ENTRY_SIZE);
6245
6246                       loc = globals->root.srelgot->contents;
6247                       loc += globals->root.srelgot->reloc_count++
6248                         * RELOC_SIZE (globals);
6249                       bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
6250                       bfd_put_NN (output_bfd, (bfd_vma) 0,
6251                                   globals->root.sgot->contents + off
6252                                   + GOT_ENTRY_SIZE);
6253                     }
6254                 }
6255               else
6256                 {
6257                   bfd_put_NN (output_bfd, (bfd_vma) 1,
6258                               globals->root.sgot->contents + off);
6259                   bfd_put_NN (output_bfd,
6260                               relocation - dtpoff_base (info),
6261                               globals->root.sgot->contents + off
6262                               + GOT_ENTRY_SIZE);
6263                 }
6264
6265               symbol_got_offset_mark (input_bfd, h, r_symndx);
6266             }
6267           break;
6268
6269         case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6270         case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
6271         case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
6272         case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
6273         case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
6274           if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
6275             {
6276               bfd_boolean need_relocs = FALSE;
6277               bfd_byte *loc;
6278               int indx;
6279               bfd_vma off;
6280
6281               off = symbol_got_offset (input_bfd, h, r_symndx);
6282
6283               indx = h && h->dynindx != -1 ? h->dynindx : 0;
6284
6285               need_relocs =
6286                 (bfd_link_pic (info) || indx != 0) &&
6287                 (h == NULL
6288                  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6289                  || h->root.type != bfd_link_hash_undefweak);
6290
6291               BFD_ASSERT (globals->root.srelgot != NULL);
6292
6293               if (need_relocs)
6294                 {
6295                   Elf_Internal_Rela rela;
6296
6297                   if (indx == 0)
6298                     rela.r_addend = relocation - dtpoff_base (info);
6299                   else
6300                     rela.r_addend = 0;
6301
6302                   rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLS_TPREL));
6303                   rela.r_offset = globals->root.sgot->output_section->vma +
6304                     globals->root.sgot->output_offset + off;
6305
6306                   loc = globals->root.srelgot->contents;
6307                   loc += globals->root.srelgot->reloc_count++
6308                     * RELOC_SIZE (htab);
6309
6310                   bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
6311
6312                   bfd_put_NN (output_bfd, rela.r_addend,
6313                               globals->root.sgot->contents + off);
6314                 }
6315               else
6316                 bfd_put_NN (output_bfd, relocation - tpoff_base (info),
6317                             globals->root.sgot->contents + off);
6318
6319               symbol_got_offset_mark (input_bfd, h, r_symndx);
6320             }
6321           break;
6322
6323         case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
6324         case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
6325         case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
6326         case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
6327         case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
6328         case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
6329         case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
6330           if (! symbol_tlsdesc_got_offset_mark_p (input_bfd, h, r_symndx))
6331             {
6332               bfd_boolean need_relocs = FALSE;
6333               int indx = h && h->dynindx != -1 ? h->dynindx : 0;
6334               bfd_vma off = symbol_tlsdesc_got_offset (input_bfd, h, r_symndx);
6335
6336               need_relocs = (h == NULL
6337                              || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6338                              || h->root.type != bfd_link_hash_undefweak);
6339
6340               BFD_ASSERT (globals->root.srelgot != NULL);
6341               BFD_ASSERT (globals->root.sgot != NULL);
6342
6343               if (need_relocs)
6344                 {
6345                   bfd_byte *loc;
6346                   Elf_Internal_Rela rela;
6347                   rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLSDESC));
6348
6349                   rela.r_addend = 0;
6350                   rela.r_offset = (globals->root.sgotplt->output_section->vma
6351                                    + globals->root.sgotplt->output_offset
6352                                    + off + globals->sgotplt_jump_table_size);
6353
6354                   if (indx == 0)
6355                     rela.r_addend = relocation - dtpoff_base (info);
6356
6357                   /* Allocate the next available slot in the PLT reloc
6358                      section to hold our R_AARCH64_TLSDESC, the next
6359                      available slot is determined from reloc_count,
6360                      which we step. But note, reloc_count was
6361                      artifically moved down while allocating slots for
6362                      real PLT relocs such that all of the PLT relocs
6363                      will fit above the initial reloc_count and the
6364                      extra stuff will fit below.  */
6365                   loc = globals->root.srelplt->contents;
6366                   loc += globals->root.srelplt->reloc_count++
6367                     * RELOC_SIZE (globals);
6368
6369                   bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
6370
6371                   bfd_put_NN (output_bfd, (bfd_vma) 0,
6372                               globals->root.sgotplt->contents + off +
6373                               globals->sgotplt_jump_table_size);
6374                   bfd_put_NN (output_bfd, (bfd_vma) 0,
6375                               globals->root.sgotplt->contents + off +
6376                               globals->sgotplt_jump_table_size +
6377                               GOT_ENTRY_SIZE);
6378                 }
6379
6380               symbol_tlsdesc_got_offset_mark (input_bfd, h, r_symndx);
6381             }
6382           break;
6383         default:
6384           break;
6385         }
6386
6387       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
6388          because such sections are not SEC_ALLOC and thus ld.so will
6389          not process them.  */
6390       if (unresolved_reloc
6391           && !((input_section->flags & SEC_DEBUGGING) != 0
6392                && h->def_dynamic)
6393           && _bfd_elf_section_offset (output_bfd, info, input_section,
6394                                       +rel->r_offset) != (bfd_vma) - 1)
6395         {
6396           _bfd_error_handler
6397             (_
6398              ("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
6399              input_bfd, input_section, (long) rel->r_offset, howto->name,
6400              h->root.root.string);
6401           return FALSE;
6402         }
6403
6404       if (r != bfd_reloc_ok && r != bfd_reloc_continue)
6405         {
6406           bfd_reloc_code_real_type real_r_type
6407             = elfNN_aarch64_bfd_reloc_from_type (r_type);
6408
6409           switch (r)
6410             {
6411             case bfd_reloc_overflow:
6412               (*info->callbacks->reloc_overflow)
6413                 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
6414                  input_bfd, input_section, rel->r_offset);
6415               if (real_r_type == BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15
6416                   || real_r_type == BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14)
6417                 {
6418                   (*info->callbacks->warning)
6419                     (info,
6420                      _("Too many GOT entries for -fpic, "
6421                        "please recompile with -fPIC"),
6422                      name, input_bfd, input_section, rel->r_offset);
6423                   return FALSE;
6424                 }
6425               /* Overflow can occur when a variable is referenced with a type
6426                  that has a larger alignment than the type with which it was
6427                  declared. eg:
6428                    file1.c: extern int foo; int a (void) { return foo; }
6429                    file2.c: char bar, foo, baz;
6430                  If the variable is placed into a data section at an offset
6431                  that is incompatible with the larger alignment requirement
6432                  overflow will occur.  (Strictly speaking this is not overflow
6433                  but rather an alignment problem, but the bfd_reloc_ error
6434                  enum does not have a value to cover that situation).
6435
6436                  Try to catch this situation here and provide a more helpful
6437                  error message to the user.  */
6438               if (addend & ((1 << howto->rightshift) - 1)
6439                   /* FIXME: Are we testing all of the appropriate reloc
6440                      types here ?  */
6441                   && (real_r_type == BFD_RELOC_AARCH64_LD_LO19_PCREL
6442                       || real_r_type == BFD_RELOC_AARCH64_LDST16_LO12
6443                       || real_r_type == BFD_RELOC_AARCH64_LDST32_LO12
6444                       || real_r_type == BFD_RELOC_AARCH64_LDST64_LO12
6445                       || real_r_type == BFD_RELOC_AARCH64_LDST128_LO12))
6446                 {
6447                   info->callbacks->warning
6448                     (info, _("One possible cause of this error is that the \
6449 symbol is being referenced in the indicated code as if it had a larger \
6450 alignment than was declared where it was defined."),
6451                      name, input_bfd, input_section, rel->r_offset);
6452                 }
6453               break;
6454
6455             case bfd_reloc_undefined:
6456               (*info->callbacks->undefined_symbol)
6457                 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
6458               break;
6459
6460             case bfd_reloc_outofrange:
6461               error_message = _("out of range");
6462               goto common_error;
6463
6464             case bfd_reloc_notsupported:
6465               error_message = _("unsupported relocation");
6466               goto common_error;
6467
6468             case bfd_reloc_dangerous:
6469               /* error_message should already be set.  */
6470               goto common_error;
6471
6472             default:
6473               error_message = _("unknown error");
6474               /* Fall through.  */
6475
6476             common_error:
6477               BFD_ASSERT (error_message != NULL);
6478               (*info->callbacks->reloc_dangerous)
6479                 (info, error_message, input_bfd, input_section, rel->r_offset);
6480               break;
6481             }
6482         }
6483
6484       if (!save_addend)
6485         addend = 0;
6486     }
6487
6488   return TRUE;
6489 }
6490
6491 /* Set the right machine number.  */
6492
6493 static bfd_boolean
6494 elfNN_aarch64_object_p (bfd *abfd)
6495 {
6496 #if ARCH_SIZE == 32
6497   bfd_default_set_arch_mach (abfd, bfd_arch_aarch64, bfd_mach_aarch64_ilp32);
6498 #else
6499   bfd_default_set_arch_mach (abfd, bfd_arch_aarch64, bfd_mach_aarch64);
6500 #endif
6501   return TRUE;
6502 }
6503
6504 /* Function to keep AArch64 specific flags in the ELF header.  */
6505
6506 static bfd_boolean
6507 elfNN_aarch64_set_private_flags (bfd *abfd, flagword flags)
6508 {
6509   if (elf_flags_init (abfd) && elf_elfheader (abfd)->e_flags != flags)
6510     {
6511     }
6512   else
6513     {
6514       elf_elfheader (abfd)->e_flags = flags;
6515       elf_flags_init (abfd) = TRUE;
6516     }
6517
6518   return TRUE;
6519 }
6520
6521 /* Merge backend specific data from an object file to the output
6522    object file when linking.  */
6523
6524 static bfd_boolean
6525 elfNN_aarch64_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
6526 {
6527   flagword out_flags;
6528   flagword in_flags;
6529   bfd_boolean flags_compatible = TRUE;
6530   asection *sec;
6531
6532   /* Check if we have the same endianess.  */
6533   if (!_bfd_generic_verify_endian_match (ibfd, obfd))
6534     return FALSE;
6535
6536   if (!is_aarch64_elf (ibfd) || !is_aarch64_elf (obfd))
6537     return TRUE;
6538
6539   /* The input BFD must have had its flags initialised.  */
6540   /* The following seems bogus to me -- The flags are initialized in
6541      the assembler but I don't think an elf_flags_init field is
6542      written into the object.  */
6543   /* BFD_ASSERT (elf_flags_init (ibfd)); */
6544
6545   in_flags = elf_elfheader (ibfd)->e_flags;
6546   out_flags = elf_elfheader (obfd)->e_flags;
6547
6548   if (!elf_flags_init (obfd))
6549     {
6550       /* If the input is the default architecture and had the default
6551          flags then do not bother setting the flags for the output
6552          architecture, instead allow future merges to do this.  If no
6553          future merges ever set these flags then they will retain their
6554          uninitialised values, which surprise surprise, correspond
6555          to the default values.  */
6556       if (bfd_get_arch_info (ibfd)->the_default
6557           && elf_elfheader (ibfd)->e_flags == 0)
6558         return TRUE;
6559
6560       elf_flags_init (obfd) = TRUE;
6561       elf_elfheader (obfd)->e_flags = in_flags;
6562
6563       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6564           && bfd_get_arch_info (obfd)->the_default)
6565         return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6566                                   bfd_get_mach (ibfd));
6567
6568       return TRUE;
6569     }
6570
6571   /* Identical flags must be compatible.  */
6572   if (in_flags == out_flags)
6573     return TRUE;
6574
6575   /* Check to see if the input BFD actually contains any sections.  If
6576      not, its flags may not have been initialised either, but it
6577      cannot actually cause any incompatiblity.  Do not short-circuit
6578      dynamic objects; their section list may be emptied by
6579      elf_link_add_object_symbols.
6580
6581      Also check to see if there are no code sections in the input.
6582      In this case there is no need to check for code specific flags.
6583      XXX - do we need to worry about floating-point format compatability
6584      in data sections ?  */
6585   if (!(ibfd->flags & DYNAMIC))
6586     {
6587       bfd_boolean null_input_bfd = TRUE;
6588       bfd_boolean only_data_sections = TRUE;
6589
6590       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6591         {
6592           if ((bfd_get_section_flags (ibfd, sec)
6593                & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
6594               == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
6595             only_data_sections = FALSE;
6596
6597           null_input_bfd = FALSE;
6598           break;
6599         }
6600
6601       if (null_input_bfd || only_data_sections)
6602         return TRUE;
6603     }
6604
6605   return flags_compatible;
6606 }
6607
6608 /* Display the flags field.  */
6609
6610 static bfd_boolean
6611 elfNN_aarch64_print_private_bfd_data (bfd *abfd, void *ptr)
6612 {
6613   FILE *file = (FILE *) ptr;
6614   unsigned long flags;
6615
6616   BFD_ASSERT (abfd != NULL && ptr != NULL);
6617
6618   /* Print normal ELF private data.  */
6619   _bfd_elf_print_private_bfd_data (abfd, ptr);
6620
6621   flags = elf_elfheader (abfd)->e_flags;
6622   /* Ignore init flag - it may not be set, despite the flags field
6623      containing valid data.  */
6624
6625   /* xgettext:c-format */
6626   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
6627
6628   if (flags)
6629     fprintf (file, _("<Unrecognised flag bits set>"));
6630
6631   fputc ('\n', file);
6632
6633   return TRUE;
6634 }
6635
6636 /* Update the got entry reference counts for the section being removed.  */
6637
6638 static bfd_boolean
6639 elfNN_aarch64_gc_sweep_hook (bfd *abfd,
6640                              struct bfd_link_info *info,
6641                              asection *sec,
6642                              const Elf_Internal_Rela * relocs)
6643 {
6644   struct elf_aarch64_link_hash_table *htab;
6645   Elf_Internal_Shdr *symtab_hdr;
6646   struct elf_link_hash_entry **sym_hashes;
6647   struct elf_aarch64_local_symbol *locals;
6648   const Elf_Internal_Rela *rel, *relend;
6649
6650   if (bfd_link_relocatable (info))
6651     return TRUE;
6652
6653   htab = elf_aarch64_hash_table (info);
6654
6655   if (htab == NULL)
6656     return FALSE;
6657
6658   elf_section_data (sec)->local_dynrel = NULL;
6659
6660   symtab_hdr = &elf_symtab_hdr (abfd);
6661   sym_hashes = elf_sym_hashes (abfd);
6662
6663   locals = elf_aarch64_locals (abfd);
6664
6665   relend = relocs + sec->reloc_count;
6666   for (rel = relocs; rel < relend; rel++)
6667     {
6668       unsigned long r_symndx;
6669       unsigned int r_type;
6670       struct elf_link_hash_entry *h = NULL;
6671
6672       r_symndx = ELFNN_R_SYM (rel->r_info);
6673
6674       if (r_symndx >= symtab_hdr->sh_info)
6675         {
6676
6677           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6678           while (h->root.type == bfd_link_hash_indirect
6679                  || h->root.type == bfd_link_hash_warning)
6680             h = (struct elf_link_hash_entry *) h->root.u.i.link;
6681         }
6682       else
6683         {
6684           Elf_Internal_Sym *isym;
6685
6686           /* A local symbol.  */
6687           isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6688                                         abfd, r_symndx);
6689
6690           /* Check relocation against local STT_GNU_IFUNC symbol.  */
6691           if (isym != NULL
6692               && ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
6693             {
6694               h = elfNN_aarch64_get_local_sym_hash (htab, abfd, rel, FALSE);
6695               if (h == NULL)
6696                 abort ();
6697             }
6698         }
6699
6700       if (h)
6701         {
6702           struct elf_aarch64_link_hash_entry *eh;
6703           struct elf_dyn_relocs **pp;
6704           struct elf_dyn_relocs *p;
6705
6706           eh = (struct elf_aarch64_link_hash_entry *) h;
6707
6708           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6709             if (p->sec == sec)
6710               {
6711                 /* Everything must go for SEC.  */
6712                 *pp = p->next;
6713                 break;
6714               }
6715         }
6716
6717       r_type = ELFNN_R_TYPE (rel->r_info);
6718       switch (aarch64_tls_transition (abfd,info, r_type, h ,r_symndx))
6719         {
6720         case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
6721         case BFD_RELOC_AARCH64_GOT_LD_PREL19:
6722         case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
6723         case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
6724         case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
6725         case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
6726         case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
6727         case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
6728         case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
6729         case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
6730         case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
6731         case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
6732         case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
6733         case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
6734         case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
6735         case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
6736         case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
6737         case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
6738         case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
6739         case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
6740         case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
6741         case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
6742         case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6743         case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
6744         case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
6745         case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
6746         case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
6747         case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
6748         case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
6749         case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
6750         case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
6751           if (h != NULL)
6752             {
6753               if (h->got.refcount > 0)
6754                 h->got.refcount -= 1;
6755
6756               if (h->type == STT_GNU_IFUNC)
6757                 {
6758                   if (h->plt.refcount > 0)
6759                     h->plt.refcount -= 1;
6760                 }
6761             }
6762           else if (locals != NULL)
6763             {
6764               if (locals[r_symndx].got_refcount > 0)
6765                 locals[r_symndx].got_refcount -= 1;
6766             }
6767           break;
6768
6769         case BFD_RELOC_AARCH64_CALL26:
6770         case BFD_RELOC_AARCH64_JUMP26:
6771           /* If this is a local symbol then we resolve it
6772              directly without creating a PLT entry.  */
6773           if (h == NULL)
6774             continue;
6775
6776           if (h->plt.refcount > 0)
6777             h->plt.refcount -= 1;
6778           break;
6779
6780         case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
6781         case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
6782         case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
6783         case BFD_RELOC_AARCH64_MOVW_G0_NC:
6784         case BFD_RELOC_AARCH64_MOVW_G1_NC:
6785         case BFD_RELOC_AARCH64_MOVW_G2_NC:
6786         case BFD_RELOC_AARCH64_MOVW_G3:
6787         case BFD_RELOC_AARCH64_NN:
6788           if (h != NULL && bfd_link_executable (info))
6789             {
6790               if (h->plt.refcount > 0)
6791                 h->plt.refcount -= 1;
6792             }
6793           break;
6794
6795         default:
6796           break;
6797         }
6798     }
6799
6800   return TRUE;
6801 }
6802
6803 /* Adjust a symbol defined by a dynamic object and referenced by a
6804    regular object.  The current definition is in some section of the
6805    dynamic object, but we're not including those sections.  We have to
6806    change the definition to something the rest of the link can
6807    understand.  */
6808
6809 static bfd_boolean
6810 elfNN_aarch64_adjust_dynamic_symbol (struct bfd_link_info *info,
6811                                      struct elf_link_hash_entry *h)
6812 {
6813   struct elf_aarch64_link_hash_table *htab;
6814   asection *s;
6815
6816   /* If this is a function, put it in the procedure linkage table.  We
6817      will fill in the contents of the procedure linkage table later,
6818      when we know the address of the .got section.  */
6819   if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
6820     {
6821       if (h->plt.refcount <= 0
6822           || (h->type != STT_GNU_IFUNC
6823               && (SYMBOL_CALLS_LOCAL (info, h)
6824                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6825                       && h->root.type == bfd_link_hash_undefweak))))
6826         {
6827           /* This case can occur if we saw a CALL26 reloc in
6828              an input file, but the symbol wasn't referred to
6829              by a dynamic object or all references were
6830              garbage collected. In which case we can end up
6831              resolving.  */
6832           h->plt.offset = (bfd_vma) - 1;
6833           h->needs_plt = 0;
6834         }
6835
6836       return TRUE;
6837     }
6838   else
6839     /* Otherwise, reset to -1.  */
6840     h->plt.offset = (bfd_vma) - 1;
6841
6842
6843   /* If this is a weak symbol, and there is a real definition, the
6844      processor independent code will have arranged for us to see the
6845      real definition first, and we can just use the same value.  */
6846   if (h->u.weakdef != NULL)
6847     {
6848       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6849                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
6850       h->root.u.def.section = h->u.weakdef->root.u.def.section;
6851       h->root.u.def.value = h->u.weakdef->root.u.def.value;
6852       if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
6853         h->non_got_ref = h->u.weakdef->non_got_ref;
6854       return TRUE;
6855     }
6856
6857   /* If we are creating a shared library, we must presume that the
6858      only references to the symbol are via the global offset table.
6859      For such cases we need not do anything here; the relocations will
6860      be handled correctly by relocate_section.  */
6861   if (bfd_link_pic (info))
6862     return TRUE;
6863
6864   /* If there are no references to this symbol that do not use the
6865      GOT, we don't need to generate a copy reloc.  */
6866   if (!h->non_got_ref)
6867     return TRUE;
6868
6869   /* If -z nocopyreloc was given, we won't generate them either.  */
6870   if (info->nocopyreloc)
6871     {
6872       h->non_got_ref = 0;
6873       return TRUE;
6874     }
6875
6876   /* We must allocate the symbol in our .dynbss section, which will
6877      become part of the .bss section of the executable.  There will be
6878      an entry for this symbol in the .dynsym section.  The dynamic
6879      object will contain position independent code, so all references
6880      from the dynamic object to this symbol will go through the global
6881      offset table.  The dynamic linker will use the .dynsym entry to
6882      determine the address it must put in the global offset table, so
6883      both the dynamic object and the regular object will refer to the
6884      same memory location for the variable.  */
6885
6886   htab = elf_aarch64_hash_table (info);
6887
6888   /* We must generate a R_AARCH64_COPY reloc to tell the dynamic linker
6889      to copy the initial value out of the dynamic object and into the
6890      runtime process image.  */
6891   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6892     {
6893       htab->srelbss->size += RELOC_SIZE (htab);
6894       h->needs_copy = 1;
6895     }
6896
6897   s = htab->sdynbss;
6898
6899   return _bfd_elf_adjust_dynamic_copy (info, h, s);
6900
6901 }
6902
6903 static bfd_boolean
6904 elfNN_aarch64_allocate_local_symbols (bfd *abfd, unsigned number)
6905 {
6906   struct elf_aarch64_local_symbol *locals;
6907   locals = elf_aarch64_locals (abfd);
6908   if (locals == NULL)
6909     {
6910       locals = (struct elf_aarch64_local_symbol *)
6911         bfd_zalloc (abfd, number * sizeof (struct elf_aarch64_local_symbol));
6912       if (locals == NULL)
6913         return FALSE;
6914       elf_aarch64_locals (abfd) = locals;
6915     }
6916   return TRUE;
6917 }
6918
6919 /* Create the .got section to hold the global offset table.  */
6920
6921 static bfd_boolean
6922 aarch64_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
6923 {
6924   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6925   flagword flags;
6926   asection *s;
6927   struct elf_link_hash_entry *h;
6928   struct elf_link_hash_table *htab = elf_hash_table (info);
6929
6930   /* This function may be called more than once.  */
6931   s = bfd_get_linker_section (abfd, ".got");
6932   if (s != NULL)
6933     return TRUE;
6934
6935   flags = bed->dynamic_sec_flags;
6936
6937   s = bfd_make_section_anyway_with_flags (abfd,
6938                                           (bed->rela_plts_and_copies_p
6939                                            ? ".rela.got" : ".rel.got"),
6940                                           (bed->dynamic_sec_flags
6941                                            | SEC_READONLY));
6942   if (s == NULL
6943       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
6944     return FALSE;
6945   htab->srelgot = s;
6946
6947   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
6948   if (s == NULL
6949       || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
6950     return FALSE;
6951   htab->sgot = s;
6952   htab->sgot->size += GOT_ENTRY_SIZE;
6953
6954   if (bed->want_got_sym)
6955     {
6956       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
6957          (or .got.plt) section.  We don't do this in the linker script
6958          because we don't want to define the symbol if we are not creating
6959          a global offset table.  */
6960       h = _bfd_elf_define_linkage_sym (abfd, info, s,
6961                                        "_GLOBAL_OFFSET_TABLE_");
6962       elf_hash_table (info)->hgot = h;
6963       if (h == NULL)
6964         return FALSE;
6965     }
6966
6967   if (bed->want_got_plt)
6968     {
6969       s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
6970       if (s == NULL
6971           || !bfd_set_section_alignment (abfd, s,
6972                                          bed->s->log_file_align))
6973         return FALSE;
6974       htab->sgotplt = s;
6975     }
6976
6977   /* The first bit of the global offset table is the header.  */
6978   s->size += bed->got_header_size;
6979
6980   return TRUE;
6981 }
6982
6983 /* Look through the relocs for a section during the first phase.  */
6984
6985 static bfd_boolean
6986 elfNN_aarch64_check_relocs (bfd *abfd, struct bfd_link_info *info,
6987                             asection *sec, const Elf_Internal_Rela *relocs)
6988 {
6989   Elf_Internal_Shdr *symtab_hdr;
6990   struct elf_link_hash_entry **sym_hashes;
6991   const Elf_Internal_Rela *rel;
6992   const Elf_Internal_Rela *rel_end;
6993   asection *sreloc;
6994
6995   struct elf_aarch64_link_hash_table *htab;
6996
6997   if (bfd_link_relocatable (info))
6998     return TRUE;
6999
7000   BFD_ASSERT (is_aarch64_elf (abfd));
7001
7002   htab = elf_aarch64_hash_table (info);
7003   sreloc = NULL;
7004
7005   symtab_hdr = &elf_symtab_hdr (abfd);
7006   sym_hashes = elf_sym_hashes (abfd);
7007
7008   rel_end = relocs + sec->reloc_count;
7009   for (rel = relocs; rel < rel_end; rel++)
7010     {
7011       struct elf_link_hash_entry *h;
7012       unsigned long r_symndx;
7013       unsigned int r_type;
7014       bfd_reloc_code_real_type bfd_r_type;
7015       Elf_Internal_Sym *isym;
7016
7017       r_symndx = ELFNN_R_SYM (rel->r_info);
7018       r_type = ELFNN_R_TYPE (rel->r_info);
7019
7020       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
7021         {
7022           _bfd_error_handler (_("%B: bad symbol index: %d"), abfd, r_symndx);
7023           return FALSE;
7024         }
7025
7026       if (r_symndx < symtab_hdr->sh_info)
7027         {
7028           /* A local symbol.  */
7029           isym = bfd_sym_from_r_symndx (&htab->sym_cache,
7030                                         abfd, r_symndx);
7031           if (isym == NULL)
7032             return FALSE;
7033
7034           /* Check relocation against local STT_GNU_IFUNC symbol.  */
7035           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
7036             {
7037               h = elfNN_aarch64_get_local_sym_hash (htab, abfd, rel,
7038                                                     TRUE);
7039               if (h == NULL)
7040                 return FALSE;
7041
7042               /* Fake a STT_GNU_IFUNC symbol.  */
7043               h->type = STT_GNU_IFUNC;
7044               h->def_regular = 1;
7045               h->ref_regular = 1;
7046               h->forced_local = 1;
7047               h->root.type = bfd_link_hash_defined;
7048             }
7049           else
7050             h = NULL;
7051         }
7052       else
7053         {
7054           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7055           while (h->root.type == bfd_link_hash_indirect
7056                  || h->root.type == bfd_link_hash_warning)
7057             h = (struct elf_link_hash_entry *) h->root.u.i.link;
7058
7059           /* PR15323, ref flags aren't set for references in the same
7060              object.  */
7061           h->root.non_ir_ref = 1;
7062         }
7063
7064       /* Could be done earlier, if h were already available.  */
7065       bfd_r_type = aarch64_tls_transition (abfd, info, r_type, h, r_symndx);
7066
7067       if (h != NULL)
7068         {
7069           /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
7070              This shows up in particular in an R_AARCH64_PREL64 in large model
7071              when calculating the pc-relative address to .got section which is
7072              used to initialize the gp register.  */
7073           if (h->root.root.string
7074               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
7075             {
7076               if (htab->root.dynobj == NULL)
7077                 htab->root.dynobj = abfd;
7078
7079               if (! aarch64_elf_create_got_section (htab->root.dynobj, info))
7080                 return FALSE;
7081
7082               BFD_ASSERT (h == htab->root.hgot);
7083             }
7084
7085           /* Create the ifunc sections for static executables.  If we
7086              never see an indirect function symbol nor we are building
7087              a static executable, those sections will be empty and
7088              won't appear in output.  */
7089           switch (bfd_r_type)
7090             {
7091             default:
7092               break;
7093
7094             case BFD_RELOC_AARCH64_ADD_LO12:
7095             case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
7096             case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
7097             case BFD_RELOC_AARCH64_CALL26:
7098             case BFD_RELOC_AARCH64_GOT_LD_PREL19:
7099             case BFD_RELOC_AARCH64_JUMP26:
7100             case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
7101             case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
7102             case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
7103             case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
7104             case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
7105             case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
7106             case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
7107             case BFD_RELOC_AARCH64_NN:
7108               if (htab->root.dynobj == NULL)
7109                 htab->root.dynobj = abfd;
7110               if (!_bfd_elf_create_ifunc_sections (htab->root.dynobj, info))
7111                 return FALSE;
7112               break;
7113             }
7114
7115           /* It is referenced by a non-shared object. */
7116           h->ref_regular = 1;
7117           h->root.non_ir_ref = 1;
7118         }
7119
7120       switch (bfd_r_type)
7121         {
7122         case BFD_RELOC_AARCH64_NN:
7123
7124           /* We don't need to handle relocs into sections not going into
7125              the "real" output.  */
7126           if ((sec->flags & SEC_ALLOC) == 0)
7127             break;
7128
7129           if (h != NULL)
7130             {
7131               if (!bfd_link_pic (info))
7132                 h->non_got_ref = 1;
7133
7134               h->plt.refcount += 1;
7135               h->pointer_equality_needed = 1;
7136             }
7137
7138           /* No need to do anything if we're not creating a shared
7139              object.  */
7140           if (! bfd_link_pic (info))
7141             break;
7142
7143           {
7144             struct elf_dyn_relocs *p;
7145             struct elf_dyn_relocs **head;
7146
7147             /* We must copy these reloc types into the output file.
7148                Create a reloc section in dynobj and make room for
7149                this reloc.  */
7150             if (sreloc == NULL)
7151               {
7152                 if (htab->root.dynobj == NULL)
7153                   htab->root.dynobj = abfd;
7154
7155                 sreloc = _bfd_elf_make_dynamic_reloc_section
7156                   (sec, htab->root.dynobj, LOG_FILE_ALIGN, abfd, /*rela? */ TRUE);
7157
7158                 if (sreloc == NULL)
7159                   return FALSE;
7160               }
7161
7162             /* If this is a global symbol, we count the number of
7163                relocations we need for this symbol.  */
7164             if (h != NULL)
7165               {
7166                 struct elf_aarch64_link_hash_entry *eh;
7167                 eh = (struct elf_aarch64_link_hash_entry *) h;
7168                 head = &eh->dyn_relocs;
7169               }
7170             else
7171               {
7172                 /* Track dynamic relocs needed for local syms too.
7173                    We really need local syms available to do this
7174                    easily.  Oh well.  */
7175
7176                 asection *s;
7177                 void **vpp;
7178
7179                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
7180                                               abfd, r_symndx);
7181                 if (isym == NULL)
7182                   return FALSE;
7183
7184                 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
7185                 if (s == NULL)
7186                   s = sec;
7187
7188                 /* Beware of type punned pointers vs strict aliasing
7189                    rules.  */
7190                 vpp = &(elf_section_data (s)->local_dynrel);
7191                 head = (struct elf_dyn_relocs **) vpp;
7192               }
7193
7194             p = *head;
7195             if (p == NULL || p->sec != sec)
7196               {
7197                 bfd_size_type amt = sizeof *p;
7198                 p = ((struct elf_dyn_relocs *)
7199                      bfd_zalloc (htab->root.dynobj, amt));
7200                 if (p == NULL)
7201                   return FALSE;
7202                 p->next = *head;
7203                 *head = p;
7204                 p->sec = sec;
7205               }
7206
7207             p->count += 1;
7208
7209           }
7210           break;
7211
7212           /* RR: We probably want to keep a consistency check that
7213              there are no dangling GOT_PAGE relocs.  */
7214         case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
7215         case BFD_RELOC_AARCH64_GOT_LD_PREL19:
7216         case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
7217         case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
7218         case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
7219         case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
7220         case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
7221         case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
7222         case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
7223         case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
7224         case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
7225         case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
7226         case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
7227         case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
7228         case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
7229         case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
7230         case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
7231         case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
7232         case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
7233         case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
7234         case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
7235         case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
7236         case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
7237         case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
7238         case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
7239         case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
7240         case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
7241         case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
7242         case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
7243         case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
7244         case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
7245         case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
7246         case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
7247         case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
7248           {
7249             unsigned got_type;
7250             unsigned old_got_type;
7251
7252             got_type = aarch64_reloc_got_type (bfd_r_type);
7253
7254             if (h)
7255               {
7256                 h->got.refcount += 1;
7257                 old_got_type = elf_aarch64_hash_entry (h)->got_type;
7258               }
7259             else
7260               {
7261                 struct elf_aarch64_local_symbol *locals;
7262
7263                 if (!elfNN_aarch64_allocate_local_symbols
7264                     (abfd, symtab_hdr->sh_info))
7265                   return FALSE;
7266
7267                 locals = elf_aarch64_locals (abfd);
7268                 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7269                 locals[r_symndx].got_refcount += 1;
7270                 old_got_type = locals[r_symndx].got_type;
7271               }
7272
7273             /* If a variable is accessed with both general dynamic TLS
7274                methods, two slots may be created.  */
7275             if (GOT_TLS_GD_ANY_P (old_got_type) && GOT_TLS_GD_ANY_P (got_type))
7276               got_type |= old_got_type;
7277
7278             /* We will already have issued an error message if there
7279                is a TLS/non-TLS mismatch, based on the symbol type.
7280                So just combine any TLS types needed.  */
7281             if (old_got_type != GOT_UNKNOWN && old_got_type != GOT_NORMAL
7282                 && got_type != GOT_NORMAL)
7283               got_type |= old_got_type;
7284
7285             /* If the symbol is accessed by both IE and GD methods, we
7286                are able to relax.  Turn off the GD flag, without
7287                messing up with any other kind of TLS types that may be
7288                involved.  */
7289             if ((got_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (got_type))
7290               got_type &= ~ (GOT_TLSDESC_GD | GOT_TLS_GD);
7291
7292             if (old_got_type != got_type)
7293               {
7294                 if (h != NULL)
7295                   elf_aarch64_hash_entry (h)->got_type = got_type;
7296                 else
7297                   {
7298                     struct elf_aarch64_local_symbol *locals;
7299                     locals = elf_aarch64_locals (abfd);
7300                     BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7301                     locals[r_symndx].got_type = got_type;
7302                   }
7303               }
7304
7305             if (htab->root.dynobj == NULL)
7306               htab->root.dynobj = abfd;
7307             if (! aarch64_elf_create_got_section (htab->root.dynobj, info))
7308               return FALSE;
7309             break;
7310           }
7311
7312         case BFD_RELOC_AARCH64_MOVW_G0_NC:
7313         case BFD_RELOC_AARCH64_MOVW_G1_NC:
7314         case BFD_RELOC_AARCH64_MOVW_G2_NC:
7315         case BFD_RELOC_AARCH64_MOVW_G3:
7316           if (bfd_link_pic (info))
7317             {
7318               int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
7319               _bfd_error_handler
7320                 (_("%B: relocation %s against `%s' can not be used when making "
7321                    "a shared object; recompile with -fPIC"),
7322                  abfd, elfNN_aarch64_howto_table[howto_index].name,
7323                  (h) ? h->root.root.string : "a local symbol");
7324               bfd_set_error (bfd_error_bad_value);
7325               return FALSE;
7326             }
7327           /* Fall through.  */
7328
7329         case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
7330         case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
7331         case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
7332           if (h != NULL && bfd_link_executable (info))
7333             {
7334               /* If this reloc is in a read-only section, we might
7335                  need a copy reloc.  We can't check reliably at this
7336                  stage whether the section is read-only, as input
7337                  sections have not yet been mapped to output sections.
7338                  Tentatively set the flag for now, and correct in
7339                  adjust_dynamic_symbol.  */
7340               h->non_got_ref = 1;
7341               h->plt.refcount += 1;
7342               h->pointer_equality_needed = 1;
7343             }
7344           /* FIXME:: RR need to handle these in shared libraries
7345              and essentially bomb out as these being non-PIC
7346              relocations in shared libraries.  */
7347           break;
7348
7349         case BFD_RELOC_AARCH64_CALL26:
7350         case BFD_RELOC_AARCH64_JUMP26:
7351           /* If this is a local symbol then we resolve it
7352              directly without creating a PLT entry.  */
7353           if (h == NULL)
7354             continue;
7355
7356           h->needs_plt = 1;
7357           if (h->plt.refcount <= 0)
7358             h->plt.refcount = 1;
7359           else
7360             h->plt.refcount += 1;
7361           break;
7362
7363         default:
7364           break;
7365         }
7366     }
7367
7368   return TRUE;
7369 }
7370
7371 /* Treat mapping symbols as special target symbols.  */
7372
7373 static bfd_boolean
7374 elfNN_aarch64_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED,
7375                                         asymbol *sym)
7376 {
7377   return bfd_is_aarch64_special_symbol_name (sym->name,
7378                                              BFD_AARCH64_SPECIAL_SYM_TYPE_ANY);
7379 }
7380
7381 /* This is a copy of elf_find_function () from elf.c except that
7382    AArch64 mapping symbols are ignored when looking for function names.  */
7383
7384 static bfd_boolean
7385 aarch64_elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
7386                            asymbol **symbols,
7387                            asection *section,
7388                            bfd_vma offset,
7389                            const char **filename_ptr,
7390                            const char **functionname_ptr)
7391 {
7392   const char *filename = NULL;
7393   asymbol *func = NULL;
7394   bfd_vma low_func = 0;
7395   asymbol **p;
7396
7397   for (p = symbols; *p != NULL; p++)
7398     {
7399       elf_symbol_type *q;
7400
7401       q = (elf_symbol_type *) * p;
7402
7403       switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
7404         {
7405         default:
7406           break;
7407         case STT_FILE:
7408           filename = bfd_asymbol_name (&q->symbol);
7409           break;
7410         case STT_FUNC:
7411         case STT_NOTYPE:
7412           /* Skip mapping symbols.  */
7413           if ((q->symbol.flags & BSF_LOCAL)
7414               && (bfd_is_aarch64_special_symbol_name
7415                   (q->symbol.name, BFD_AARCH64_SPECIAL_SYM_TYPE_ANY)))
7416             continue;
7417           /* Fall through.  */
7418           if (bfd_get_section (&q->symbol) == section
7419               && q->symbol.value >= low_func && q->symbol.value <= offset)
7420             {
7421               func = (asymbol *) q;
7422               low_func = q->symbol.value;
7423             }
7424           break;
7425         }
7426     }
7427
7428   if (func == NULL)
7429     return FALSE;
7430
7431   if (filename_ptr)
7432     *filename_ptr = filename;
7433   if (functionname_ptr)
7434     *functionname_ptr = bfd_asymbol_name (func);
7435
7436   return TRUE;
7437 }
7438
7439
7440 /* Find the nearest line to a particular section and offset, for error
7441    reporting.   This code is a duplicate of the code in elf.c, except
7442    that it uses aarch64_elf_find_function.  */
7443
7444 static bfd_boolean
7445 elfNN_aarch64_find_nearest_line (bfd *abfd,
7446                                  asymbol **symbols,
7447                                  asection *section,
7448                                  bfd_vma offset,
7449                                  const char **filename_ptr,
7450                                  const char **functionname_ptr,
7451                                  unsigned int *line_ptr,
7452                                  unsigned int *discriminator_ptr)
7453 {
7454   bfd_boolean found = FALSE;
7455
7456   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
7457                                      filename_ptr, functionname_ptr,
7458                                      line_ptr, discriminator_ptr,
7459                                      dwarf_debug_sections, 0,
7460                                      &elf_tdata (abfd)->dwarf2_find_line_info))
7461     {
7462       if (!*functionname_ptr)
7463         aarch64_elf_find_function (abfd, symbols, section, offset,
7464                                    *filename_ptr ? NULL : filename_ptr,
7465                                    functionname_ptr);
7466
7467       return TRUE;
7468     }
7469
7470   /* Skip _bfd_dwarf1_find_nearest_line since no known AArch64
7471      toolchain uses DWARF1.  */
7472
7473   if (!_bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7474                                             &found, filename_ptr,
7475                                             functionname_ptr, line_ptr,
7476                                             &elf_tdata (abfd)->line_info))
7477     return FALSE;
7478
7479   if (found && (*functionname_ptr || *line_ptr))
7480     return TRUE;
7481
7482   if (symbols == NULL)
7483     return FALSE;
7484
7485   if (!aarch64_elf_find_function (abfd, symbols, section, offset,
7486                                   filename_ptr, functionname_ptr))
7487     return FALSE;
7488
7489   *line_ptr = 0;
7490   return TRUE;
7491 }
7492
7493 static bfd_boolean
7494 elfNN_aarch64_find_inliner_info (bfd *abfd,
7495                                  const char **filename_ptr,
7496                                  const char **functionname_ptr,
7497                                  unsigned int *line_ptr)
7498 {
7499   bfd_boolean found;
7500   found = _bfd_dwarf2_find_inliner_info
7501     (abfd, filename_ptr,
7502      functionname_ptr, line_ptr, &elf_tdata (abfd)->dwarf2_find_line_info);
7503   return found;
7504 }
7505
7506
7507 static void
7508 elfNN_aarch64_post_process_headers (bfd *abfd,
7509                                     struct bfd_link_info *link_info)
7510 {
7511   Elf_Internal_Ehdr *i_ehdrp;   /* ELF file header, internal form.  */
7512
7513   i_ehdrp = elf_elfheader (abfd);
7514   i_ehdrp->e_ident[EI_ABIVERSION] = AARCH64_ELF_ABI_VERSION;
7515
7516   _bfd_elf_post_process_headers (abfd, link_info);
7517 }
7518
7519 static enum elf_reloc_type_class
7520 elfNN_aarch64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
7521                                 const asection *rel_sec ATTRIBUTE_UNUSED,
7522                                 const Elf_Internal_Rela *rela)
7523 {
7524   switch ((int) ELFNN_R_TYPE (rela->r_info))
7525     {
7526     case AARCH64_R (RELATIVE):
7527       return reloc_class_relative;
7528     case AARCH64_R (JUMP_SLOT):
7529       return reloc_class_plt;
7530     case AARCH64_R (COPY):
7531       return reloc_class_copy;
7532     default:
7533       return reloc_class_normal;
7534     }
7535 }
7536
7537 /* Handle an AArch64 specific section when reading an object file.  This is
7538    called when bfd_section_from_shdr finds a section with an unknown
7539    type.  */
7540
7541 static bfd_boolean
7542 elfNN_aarch64_section_from_shdr (bfd *abfd,
7543                                  Elf_Internal_Shdr *hdr,
7544                                  const char *name, int shindex)
7545 {
7546   /* There ought to be a place to keep ELF backend specific flags, but
7547      at the moment there isn't one.  We just keep track of the
7548      sections by their name, instead.  Fortunately, the ABI gives
7549      names for all the AArch64 specific sections, so we will probably get
7550      away with this.  */
7551   switch (hdr->sh_type)
7552     {
7553     case SHT_AARCH64_ATTRIBUTES:
7554       break;
7555
7556     default:
7557       return FALSE;
7558     }
7559
7560   if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
7561     return FALSE;
7562
7563   return TRUE;
7564 }
7565
7566 /* A structure used to record a list of sections, independently
7567    of the next and prev fields in the asection structure.  */
7568 typedef struct section_list
7569 {
7570   asection *sec;
7571   struct section_list *next;
7572   struct section_list *prev;
7573 }
7574 section_list;
7575
7576 /* Unfortunately we need to keep a list of sections for which
7577    an _aarch64_elf_section_data structure has been allocated.  This
7578    is because it is possible for functions like elfNN_aarch64_write_section
7579    to be called on a section which has had an elf_data_structure
7580    allocated for it (and so the used_by_bfd field is valid) but
7581    for which the AArch64 extended version of this structure - the
7582    _aarch64_elf_section_data structure - has not been allocated.  */
7583 static section_list *sections_with_aarch64_elf_section_data = NULL;
7584
7585 static void
7586 record_section_with_aarch64_elf_section_data (asection *sec)
7587 {
7588   struct section_list *entry;
7589
7590   entry = bfd_malloc (sizeof (*entry));
7591   if (entry == NULL)
7592     return;
7593   entry->sec = sec;
7594   entry->next = sections_with_aarch64_elf_section_data;
7595   entry->prev = NULL;
7596   if (entry->next != NULL)
7597     entry->next->prev = entry;
7598   sections_with_aarch64_elf_section_data = entry;
7599 }
7600
7601 static struct section_list *
7602 find_aarch64_elf_section_entry (asection *sec)
7603 {
7604   struct section_list *entry;
7605   static struct section_list *last_entry = NULL;
7606
7607   /* This is a short cut for the typical case where the sections are added
7608      to the sections_with_aarch64_elf_section_data list in forward order and
7609      then looked up here in backwards order.  This makes a real difference
7610      to the ld-srec/sec64k.exp linker test.  */
7611   entry = sections_with_aarch64_elf_section_data;
7612   if (last_entry != NULL)
7613     {
7614       if (last_entry->sec == sec)
7615         entry = last_entry;
7616       else if (last_entry->next != NULL && last_entry->next->sec == sec)
7617         entry = last_entry->next;
7618     }
7619
7620   for (; entry; entry = entry->next)
7621     if (entry->sec == sec)
7622       break;
7623
7624   if (entry)
7625     /* Record the entry prior to this one - it is the entry we are
7626        most likely to want to locate next time.  Also this way if we
7627        have been called from
7628        unrecord_section_with_aarch64_elf_section_data () we will not
7629        be caching a pointer that is about to be freed.  */
7630     last_entry = entry->prev;
7631
7632   return entry;
7633 }
7634
7635 static void
7636 unrecord_section_with_aarch64_elf_section_data (asection *sec)
7637 {
7638   struct section_list *entry;
7639
7640   entry = find_aarch64_elf_section_entry (sec);
7641
7642   if (entry)
7643     {
7644       if (entry->prev != NULL)
7645         entry->prev->next = entry->next;
7646       if (entry->next != NULL)
7647         entry->next->prev = entry->prev;
7648       if (entry == sections_with_aarch64_elf_section_data)
7649         sections_with_aarch64_elf_section_data = entry->next;
7650       free (entry);
7651     }
7652 }
7653
7654
7655 typedef struct
7656 {
7657   void *finfo;
7658   struct bfd_link_info *info;
7659   asection *sec;
7660   int sec_shndx;
7661   int (*func) (void *, const char *, Elf_Internal_Sym *,
7662                asection *, struct elf_link_hash_entry *);
7663 } output_arch_syminfo;
7664
7665 enum map_symbol_type
7666 {
7667   AARCH64_MAP_INSN,
7668   AARCH64_MAP_DATA
7669 };
7670
7671
7672 /* Output a single mapping symbol.  */
7673
7674 static bfd_boolean
7675 elfNN_aarch64_output_map_sym (output_arch_syminfo *osi,
7676                               enum map_symbol_type type, bfd_vma offset)
7677 {
7678   static const char *names[2] = { "$x", "$d" };
7679   Elf_Internal_Sym sym;
7680
7681   sym.st_value = (osi->sec->output_section->vma
7682                   + osi->sec->output_offset + offset);
7683   sym.st_size = 0;
7684   sym.st_other = 0;
7685   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
7686   sym.st_shndx = osi->sec_shndx;
7687   return osi->func (osi->finfo, names[type], &sym, osi->sec, NULL) == 1;
7688 }
7689
7690 /* Output a single local symbol for a generated stub.  */
7691
7692 static bfd_boolean
7693 elfNN_aarch64_output_stub_sym (output_arch_syminfo *osi, const char *name,
7694                                bfd_vma offset, bfd_vma size)
7695 {
7696   Elf_Internal_Sym sym;
7697
7698   sym.st_value = (osi->sec->output_section->vma
7699                   + osi->sec->output_offset + offset);
7700   sym.st_size = size;
7701   sym.st_other = 0;
7702   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7703   sym.st_shndx = osi->sec_shndx;
7704   return osi->func (osi->finfo, name, &sym, osi->sec, NULL) == 1;
7705 }
7706
7707 static bfd_boolean
7708 aarch64_map_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
7709 {
7710   struct elf_aarch64_stub_hash_entry *stub_entry;
7711   asection *stub_sec;
7712   bfd_vma addr;
7713   char *stub_name;
7714   output_arch_syminfo *osi;
7715
7716   /* Massage our args to the form they really have.  */
7717   stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
7718   osi = (output_arch_syminfo *) in_arg;
7719
7720   stub_sec = stub_entry->stub_sec;
7721
7722   /* Ensure this stub is attached to the current section being
7723      processed.  */
7724   if (stub_sec != osi->sec)
7725     return TRUE;
7726
7727   addr = (bfd_vma) stub_entry->stub_offset;
7728
7729   stub_name = stub_entry->output_name;
7730
7731   switch (stub_entry->stub_type)
7732     {
7733     case aarch64_stub_adrp_branch:
7734       if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
7735                                           sizeof (aarch64_adrp_branch_stub)))
7736         return FALSE;
7737       if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
7738         return FALSE;
7739       break;
7740     case aarch64_stub_long_branch:
7741       if (!elfNN_aarch64_output_stub_sym
7742           (osi, stub_name, addr, sizeof (aarch64_long_branch_stub)))
7743         return FALSE;
7744       if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
7745         return FALSE;
7746       if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_DATA, addr + 16))
7747         return FALSE;
7748       break;
7749     case aarch64_stub_erratum_835769_veneer:
7750       if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
7751                                           sizeof (aarch64_erratum_835769_stub)))
7752         return FALSE;
7753       if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
7754         return FALSE;
7755       break;
7756     case aarch64_stub_erratum_843419_veneer:
7757       if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
7758                                           sizeof (aarch64_erratum_843419_stub)))
7759         return FALSE;
7760       if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
7761         return FALSE;
7762       break;
7763
7764     default:
7765       abort ();
7766     }
7767
7768   return TRUE;
7769 }
7770
7771 /* Output mapping symbols for linker generated sections.  */
7772
7773 static bfd_boolean
7774 elfNN_aarch64_output_arch_local_syms (bfd *output_bfd,
7775                                       struct bfd_link_info *info,
7776                                       void *finfo,
7777                                       int (*func) (void *, const char *,
7778                                                    Elf_Internal_Sym *,
7779                                                    asection *,
7780                                                    struct elf_link_hash_entry
7781                                                    *))
7782 {
7783   output_arch_syminfo osi;
7784   struct elf_aarch64_link_hash_table *htab;
7785
7786   htab = elf_aarch64_hash_table (info);
7787
7788   osi.finfo = finfo;
7789   osi.info = info;
7790   osi.func = func;
7791
7792   /* Long calls stubs.  */
7793   if (htab->stub_bfd && htab->stub_bfd->sections)
7794     {
7795       asection *stub_sec;
7796
7797       for (stub_sec = htab->stub_bfd->sections;
7798            stub_sec != NULL; stub_sec = stub_sec->next)
7799         {
7800           /* Ignore non-stub sections.  */
7801           if (!strstr (stub_sec->name, STUB_SUFFIX))
7802             continue;
7803
7804           osi.sec = stub_sec;
7805
7806           osi.sec_shndx = _bfd_elf_section_from_bfd_section
7807             (output_bfd, osi.sec->output_section);
7808
7809           /* The first instruction in a stub is always a branch.  */
7810           if (!elfNN_aarch64_output_map_sym (&osi, AARCH64_MAP_INSN, 0))
7811             return FALSE;
7812
7813           bfd_hash_traverse (&htab->stub_hash_table, aarch64_map_one_stub,
7814                              &osi);
7815         }
7816     }
7817
7818   /* Finally, output mapping symbols for the PLT.  */
7819   if (!htab->root.splt || htab->root.splt->size == 0)
7820     return TRUE;
7821
7822   osi.sec_shndx = _bfd_elf_section_from_bfd_section
7823     (output_bfd, htab->root.splt->output_section);
7824   osi.sec = htab->root.splt;
7825
7826   elfNN_aarch64_output_map_sym (&osi, AARCH64_MAP_INSN, 0);
7827
7828   return TRUE;
7829
7830 }
7831
7832 /* Allocate target specific section data.  */
7833
7834 static bfd_boolean
7835 elfNN_aarch64_new_section_hook (bfd *abfd, asection *sec)
7836 {
7837   if (!sec->used_by_bfd)
7838     {
7839       _aarch64_elf_section_data *sdata;
7840       bfd_size_type amt = sizeof (*sdata);
7841
7842       sdata = bfd_zalloc (abfd, amt);
7843       if (sdata == NULL)
7844         return FALSE;
7845       sec->used_by_bfd = sdata;
7846     }
7847
7848   record_section_with_aarch64_elf_section_data (sec);
7849
7850   return _bfd_elf_new_section_hook (abfd, sec);
7851 }
7852
7853
7854 static void
7855 unrecord_section_via_map_over_sections (bfd *abfd ATTRIBUTE_UNUSED,
7856                                         asection *sec,
7857                                         void *ignore ATTRIBUTE_UNUSED)
7858 {
7859   unrecord_section_with_aarch64_elf_section_data (sec);
7860 }
7861
7862 static bfd_boolean
7863 elfNN_aarch64_close_and_cleanup (bfd *abfd)
7864 {
7865   if (abfd->sections)
7866     bfd_map_over_sections (abfd,
7867                            unrecord_section_via_map_over_sections, NULL);
7868
7869   return _bfd_elf_close_and_cleanup (abfd);
7870 }
7871
7872 static bfd_boolean
7873 elfNN_aarch64_bfd_free_cached_info (bfd *abfd)
7874 {
7875   if (abfd->sections)
7876     bfd_map_over_sections (abfd,
7877                            unrecord_section_via_map_over_sections, NULL);
7878
7879   return _bfd_free_cached_info (abfd);
7880 }
7881
7882 /* Create dynamic sections. This is different from the ARM backend in that
7883    the got, plt, gotplt and their relocation sections are all created in the
7884    standard part of the bfd elf backend.  */
7885
7886 static bfd_boolean
7887 elfNN_aarch64_create_dynamic_sections (bfd *dynobj,
7888                                        struct bfd_link_info *info)
7889 {
7890   struct elf_aarch64_link_hash_table *htab;
7891
7892   /* We need to create .got section.  */
7893   if (!aarch64_elf_create_got_section (dynobj, info))
7894     return FALSE;
7895
7896   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
7897     return FALSE;
7898
7899   htab = elf_aarch64_hash_table (info);
7900   htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
7901   if (!bfd_link_pic (info))
7902     htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss");
7903
7904   if (!htab->sdynbss || (!bfd_link_pic (info) && !htab->srelbss))
7905     abort ();
7906
7907   return TRUE;
7908 }
7909
7910
7911 /* Allocate space in .plt, .got and associated reloc sections for
7912    dynamic relocs.  */
7913
7914 static bfd_boolean
7915 elfNN_aarch64_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
7916 {
7917   struct bfd_link_info *info;
7918   struct elf_aarch64_link_hash_table *htab;
7919   struct elf_aarch64_link_hash_entry *eh;
7920   struct elf_dyn_relocs *p;
7921
7922   /* An example of a bfd_link_hash_indirect symbol is versioned
7923      symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
7924      -> __gxx_personality_v0(bfd_link_hash_defined)
7925
7926      There is no need to process bfd_link_hash_indirect symbols here
7927      because we will also be presented with the concrete instance of
7928      the symbol and elfNN_aarch64_copy_indirect_symbol () will have been
7929      called to copy all relevant data from the generic to the concrete
7930      symbol instance.
7931    */
7932   if (h->root.type == bfd_link_hash_indirect)
7933     return TRUE;
7934
7935   if (h->root.type == bfd_link_hash_warning)
7936     h = (struct elf_link_hash_entry *) h->root.u.i.link;
7937
7938   info = (struct bfd_link_info *) inf;
7939   htab = elf_aarch64_hash_table (info);
7940
7941   /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
7942      here if it is defined and referenced in a non-shared object.  */
7943   if (h->type == STT_GNU_IFUNC
7944       && h->def_regular)
7945     return TRUE;
7946   else if (htab->root.dynamic_sections_created && h->plt.refcount > 0)
7947     {
7948       /* Make sure this symbol is output as a dynamic symbol.
7949          Undefined weak syms won't yet be marked as dynamic.  */
7950       if (h->dynindx == -1 && !h->forced_local)
7951         {
7952           if (!bfd_elf_link_record_dynamic_symbol (info, h))
7953             return FALSE;
7954         }
7955
7956       if (bfd_link_pic (info) || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
7957         {
7958           asection *s = htab->root.splt;
7959
7960           /* If this is the first .plt entry, make room for the special
7961              first entry.  */
7962           if (s->size == 0)
7963             s->size += htab->plt_header_size;
7964
7965           h->plt.offset = s->size;
7966
7967           /* If this symbol is not defined in a regular file, and we are
7968              not generating a shared library, then set the symbol to this
7969              location in the .plt.  This is required to make function
7970              pointers compare as equal between the normal executable and
7971              the shared library.  */
7972           if (!bfd_link_pic (info) && !h->def_regular)
7973             {
7974               h->root.u.def.section = s;
7975               h->root.u.def.value = h->plt.offset;
7976             }
7977
7978           /* Make room for this entry. For now we only create the
7979              small model PLT entries. We later need to find a way
7980              of relaxing into these from the large model PLT entries.  */
7981           s->size += PLT_SMALL_ENTRY_SIZE;
7982
7983           /* We also need to make an entry in the .got.plt section, which
7984              will be placed in the .got section by the linker script.  */
7985           htab->root.sgotplt->size += GOT_ENTRY_SIZE;
7986
7987           /* We also need to make an entry in the .rela.plt section.  */
7988           htab->root.srelplt->size += RELOC_SIZE (htab);
7989
7990           /* We need to ensure that all GOT entries that serve the PLT
7991              are consecutive with the special GOT slots [0] [1] and
7992              [2]. Any addtional relocations, such as
7993              R_AARCH64_TLSDESC, must be placed after the PLT related
7994              entries.  We abuse the reloc_count such that during
7995              sizing we adjust reloc_count to indicate the number of
7996              PLT related reserved entries.  In subsequent phases when
7997              filling in the contents of the reloc entries, PLT related
7998              entries are placed by computing their PLT index (0
7999              .. reloc_count). While other none PLT relocs are placed
8000              at the slot indicated by reloc_count and reloc_count is
8001              updated.  */
8002
8003           htab->root.srelplt->reloc_count++;
8004         }
8005       else
8006         {
8007           h->plt.offset = (bfd_vma) - 1;
8008           h->needs_plt = 0;
8009         }
8010     }
8011   else
8012     {
8013       h->plt.offset = (bfd_vma) - 1;
8014       h->needs_plt = 0;
8015     }
8016
8017   eh = (struct elf_aarch64_link_hash_entry *) h;
8018   eh->tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
8019
8020   if (h->got.refcount > 0)
8021     {
8022       bfd_boolean dyn;
8023       unsigned got_type = elf_aarch64_hash_entry (h)->got_type;
8024
8025       h->got.offset = (bfd_vma) - 1;
8026
8027       dyn = htab->root.dynamic_sections_created;
8028
8029       /* Make sure this symbol is output as a dynamic symbol.
8030          Undefined weak syms won't yet be marked as dynamic.  */
8031       if (dyn && h->dynindx == -1 && !h->forced_local)
8032         {
8033           if (!bfd_elf_link_record_dynamic_symbol (info, h))
8034             return FALSE;
8035         }
8036
8037       if (got_type == GOT_UNKNOWN)
8038         {
8039         }
8040       else if (got_type == GOT_NORMAL)
8041         {
8042           h->got.offset = htab->root.sgot->size;
8043           htab->root.sgot->size += GOT_ENTRY_SIZE;
8044           if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8045                || h->root.type != bfd_link_hash_undefweak)
8046               && (bfd_link_pic (info)
8047                   || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
8048             {
8049               htab->root.srelgot->size += RELOC_SIZE (htab);
8050             }
8051         }
8052       else
8053         {
8054           int indx;
8055           if (got_type & GOT_TLSDESC_GD)
8056             {
8057               eh->tlsdesc_got_jump_table_offset =
8058                 (htab->root.sgotplt->size
8059                  - aarch64_compute_jump_table_size (htab));
8060               htab->root.sgotplt->size += GOT_ENTRY_SIZE * 2;
8061               h->got.offset = (bfd_vma) - 2;
8062             }
8063
8064           if (got_type & GOT_TLS_GD)
8065             {
8066               h->got.offset = htab->root.sgot->size;
8067               htab->root.sgot->size += GOT_ENTRY_SIZE * 2;
8068             }
8069
8070           if (got_type & GOT_TLS_IE)
8071             {
8072               h->got.offset = htab->root.sgot->size;
8073               htab->root.sgot->size += GOT_ENTRY_SIZE;
8074             }
8075
8076           indx = h && h->dynindx != -1 ? h->dynindx : 0;
8077           if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8078                || h->root.type != bfd_link_hash_undefweak)
8079               && (bfd_link_pic (info)
8080                   || indx != 0
8081                   || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
8082             {
8083               if (got_type & GOT_TLSDESC_GD)
8084                 {
8085                   htab->root.srelplt->size += RELOC_SIZE (htab);
8086                   /* Note reloc_count not incremented here!  We have
8087                      already adjusted reloc_count for this relocation
8088                      type.  */
8089
8090                   /* TLSDESC PLT is now needed, but not yet determined.  */
8091                   htab->tlsdesc_plt = (bfd_vma) - 1;
8092                 }
8093
8094               if (got_type & GOT_TLS_GD)
8095                 htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
8096
8097               if (got_type & GOT_TLS_IE)
8098                 htab->root.srelgot->size += RELOC_SIZE (htab);
8099             }
8100         }
8101     }
8102   else
8103     {
8104       h->got.offset = (bfd_vma) - 1;
8105     }
8106
8107   if (eh->dyn_relocs == NULL)
8108     return TRUE;
8109
8110   /* In the shared -Bsymbolic case, discard space allocated for
8111      dynamic pc-relative relocs against symbols which turn out to be
8112      defined in regular objects.  For the normal shared case, discard
8113      space for pc-relative relocs that have become local due to symbol
8114      visibility changes.  */
8115
8116   if (bfd_link_pic (info))
8117     {
8118       /* Relocs that use pc_count are those that appear on a call
8119          insn, or certain REL relocs that can generated via assembly.
8120          We want calls to protected symbols to resolve directly to the
8121          function rather than going via the plt.  If people want
8122          function pointer comparisons to work as expected then they
8123          should avoid writing weird assembly.  */
8124       if (SYMBOL_CALLS_LOCAL (info, h))
8125         {
8126           struct elf_dyn_relocs **pp;
8127
8128           for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
8129             {
8130               p->count -= p->pc_count;
8131               p->pc_count = 0;
8132               if (p->count == 0)
8133                 *pp = p->next;
8134               else
8135                 pp = &p->next;
8136             }
8137         }
8138
8139       /* Also discard relocs on undefined weak syms with non-default
8140          visibility.  */
8141       if (eh->dyn_relocs != NULL && h->root.type == bfd_link_hash_undefweak)
8142         {
8143           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8144             eh->dyn_relocs = NULL;
8145
8146           /* Make sure undefined weak symbols are output as a dynamic
8147              symbol in PIEs.  */
8148           else if (h->dynindx == -1
8149                    && !h->forced_local
8150                    && !bfd_elf_link_record_dynamic_symbol (info, h))
8151             return FALSE;
8152         }
8153
8154     }
8155   else if (ELIMINATE_COPY_RELOCS)
8156     {
8157       /* For the non-shared case, discard space for relocs against
8158          symbols which turn out to need copy relocs or are not
8159          dynamic.  */
8160
8161       if (!h->non_got_ref
8162           && ((h->def_dynamic
8163                && !h->def_regular)
8164               || (htab->root.dynamic_sections_created
8165                   && (h->root.type == bfd_link_hash_undefweak
8166                       || h->root.type == bfd_link_hash_undefined))))
8167         {
8168           /* Make sure this symbol is output as a dynamic symbol.
8169              Undefined weak syms won't yet be marked as dynamic.  */
8170           if (h->dynindx == -1
8171               && !h->forced_local
8172               && !bfd_elf_link_record_dynamic_symbol (info, h))
8173             return FALSE;
8174
8175           /* If that succeeded, we know we'll be keeping all the
8176              relocs.  */
8177           if (h->dynindx != -1)
8178             goto keep;
8179         }
8180
8181       eh->dyn_relocs = NULL;
8182
8183     keep:;
8184     }
8185
8186   /* Finally, allocate space.  */
8187   for (p = eh->dyn_relocs; p != NULL; p = p->next)
8188     {
8189       asection *sreloc;
8190
8191       sreloc = elf_section_data (p->sec)->sreloc;
8192
8193       BFD_ASSERT (sreloc != NULL);
8194
8195       sreloc->size += p->count * RELOC_SIZE (htab);
8196     }
8197
8198   return TRUE;
8199 }
8200
8201 /* Allocate space in .plt, .got and associated reloc sections for
8202    ifunc dynamic relocs.  */
8203
8204 static bfd_boolean
8205 elfNN_aarch64_allocate_ifunc_dynrelocs (struct elf_link_hash_entry *h,
8206                                         void *inf)
8207 {
8208   struct bfd_link_info *info;
8209   struct elf_aarch64_link_hash_table *htab;
8210   struct elf_aarch64_link_hash_entry *eh;
8211
8212   /* An example of a bfd_link_hash_indirect symbol is versioned
8213      symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
8214      -> __gxx_personality_v0(bfd_link_hash_defined)
8215
8216      There is no need to process bfd_link_hash_indirect symbols here
8217      because we will also be presented with the concrete instance of
8218      the symbol and elfNN_aarch64_copy_indirect_symbol () will have been
8219      called to copy all relevant data from the generic to the concrete
8220      symbol instance.
8221    */
8222   if (h->root.type == bfd_link_hash_indirect)
8223     return TRUE;
8224
8225   if (h->root.type == bfd_link_hash_warning)
8226     h = (struct elf_link_hash_entry *) h->root.u.i.link;
8227
8228   info = (struct bfd_link_info *) inf;
8229   htab = elf_aarch64_hash_table (info);
8230
8231   eh = (struct elf_aarch64_link_hash_entry *) h;
8232
8233   /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
8234      here if it is defined and referenced in a non-shared object.  */
8235   if (h->type == STT_GNU_IFUNC
8236       && h->def_regular)
8237     return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
8238                                                &eh->dyn_relocs,
8239                                                NULL,
8240                                                htab->plt_entry_size,
8241                                                htab->plt_header_size,
8242                                                GOT_ENTRY_SIZE,
8243                                                FALSE);
8244   return TRUE;
8245 }
8246
8247 /* Allocate space in .plt, .got and associated reloc sections for
8248    local dynamic relocs.  */
8249
8250 static bfd_boolean
8251 elfNN_aarch64_allocate_local_dynrelocs (void **slot, void *inf)
8252 {
8253   struct elf_link_hash_entry *h
8254     = (struct elf_link_hash_entry *) *slot;
8255
8256   if (h->type != STT_GNU_IFUNC
8257       || !h->def_regular
8258       || !h->ref_regular
8259       || !h->forced_local
8260       || h->root.type != bfd_link_hash_defined)
8261     abort ();
8262
8263   return elfNN_aarch64_allocate_dynrelocs (h, inf);
8264 }
8265
8266 /* Allocate space in .plt, .got and associated reloc sections for
8267    local ifunc dynamic relocs.  */
8268
8269 static bfd_boolean
8270 elfNN_aarch64_allocate_local_ifunc_dynrelocs (void **slot, void *inf)
8271 {
8272   struct elf_link_hash_entry *h
8273     = (struct elf_link_hash_entry *) *slot;
8274
8275   if (h->type != STT_GNU_IFUNC
8276       || !h->def_regular
8277       || !h->ref_regular
8278       || !h->forced_local
8279       || h->root.type != bfd_link_hash_defined)
8280     abort ();
8281
8282   return elfNN_aarch64_allocate_ifunc_dynrelocs (h, inf);
8283 }
8284
8285 /* Find any dynamic relocs that apply to read-only sections.  */
8286
8287 static bfd_boolean
8288 aarch64_readonly_dynrelocs (struct elf_link_hash_entry * h, void * inf)
8289 {
8290   struct elf_aarch64_link_hash_entry * eh;
8291   struct elf_dyn_relocs * p;
8292
8293   eh = (struct elf_aarch64_link_hash_entry *) h;
8294   for (p = eh->dyn_relocs; p != NULL; p = p->next)
8295     {
8296       asection *s = p->sec;
8297
8298       if (s != NULL && (s->flags & SEC_READONLY) != 0)
8299         {
8300           struct bfd_link_info *info = (struct bfd_link_info *) inf;
8301
8302           info->flags |= DF_TEXTREL;
8303
8304           /* Not an error, just cut short the traversal.  */
8305           return FALSE;
8306         }
8307     }
8308   return TRUE;
8309 }
8310
8311 /* This is the most important function of all . Innocuosly named
8312    though !  */
8313 static bfd_boolean
8314 elfNN_aarch64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
8315                                      struct bfd_link_info *info)
8316 {
8317   struct elf_aarch64_link_hash_table *htab;
8318   bfd *dynobj;
8319   asection *s;
8320   bfd_boolean relocs;
8321   bfd *ibfd;
8322
8323   htab = elf_aarch64_hash_table ((info));
8324   dynobj = htab->root.dynobj;
8325
8326   BFD_ASSERT (dynobj != NULL);
8327
8328   if (htab->root.dynamic_sections_created)
8329     {
8330       if (bfd_link_executable (info) && !info->nointerp)
8331         {
8332           s = bfd_get_linker_section (dynobj, ".interp");
8333           if (s == NULL)
8334             abort ();
8335           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
8336           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
8337         }
8338     }
8339
8340   /* Set up .got offsets for local syms, and space for local dynamic
8341      relocs.  */
8342   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8343     {
8344       struct elf_aarch64_local_symbol *locals = NULL;
8345       Elf_Internal_Shdr *symtab_hdr;
8346       asection *srel;
8347       unsigned int i;
8348
8349       if (!is_aarch64_elf (ibfd))
8350         continue;
8351
8352       for (s = ibfd->sections; s != NULL; s = s->next)
8353         {
8354           struct elf_dyn_relocs *p;
8355
8356           for (p = (struct elf_dyn_relocs *)
8357                (elf_section_data (s)->local_dynrel); p != NULL; p = p->next)
8358             {
8359               if (!bfd_is_abs_section (p->sec)
8360                   && bfd_is_abs_section (p->sec->output_section))
8361                 {
8362                   /* Input section has been discarded, either because
8363                      it is a copy of a linkonce section or due to
8364                      linker script /DISCARD/, so we'll be discarding
8365                      the relocs too.  */
8366                 }
8367               else if (p->count != 0)
8368                 {
8369                   srel = elf_section_data (p->sec)->sreloc;
8370                   srel->size += p->count * RELOC_SIZE (htab);
8371                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
8372                     info->flags |= DF_TEXTREL;
8373                 }
8374             }
8375         }
8376
8377       locals = elf_aarch64_locals (ibfd);
8378       if (!locals)
8379         continue;
8380
8381       symtab_hdr = &elf_symtab_hdr (ibfd);
8382       srel = htab->root.srelgot;
8383       for (i = 0; i < symtab_hdr->sh_info; i++)
8384         {
8385           locals[i].got_offset = (bfd_vma) - 1;
8386           locals[i].tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
8387           if (locals[i].got_refcount > 0)
8388             {
8389               unsigned got_type = locals[i].got_type;
8390               if (got_type & GOT_TLSDESC_GD)
8391                 {
8392                   locals[i].tlsdesc_got_jump_table_offset =
8393                     (htab->root.sgotplt->size
8394                      - aarch64_compute_jump_table_size (htab));
8395                   htab->root.sgotplt->size += GOT_ENTRY_SIZE * 2;
8396                   locals[i].got_offset = (bfd_vma) - 2;
8397                 }
8398
8399               if (got_type & GOT_TLS_GD)
8400                 {
8401                   locals[i].got_offset = htab->root.sgot->size;
8402                   htab->root.sgot->size += GOT_ENTRY_SIZE * 2;
8403                 }
8404
8405               if (got_type & GOT_TLS_IE
8406                   || got_type & GOT_NORMAL)
8407                 {
8408                   locals[i].got_offset = htab->root.sgot->size;
8409                   htab->root.sgot->size += GOT_ENTRY_SIZE;
8410                 }
8411
8412               if (got_type == GOT_UNKNOWN)
8413                 {
8414                 }
8415
8416               if (bfd_link_pic (info))
8417                 {
8418                   if (got_type & GOT_TLSDESC_GD)
8419                     {
8420                       htab->root.srelplt->size += RELOC_SIZE (htab);
8421                       /* Note RELOC_COUNT not incremented here! */
8422                       htab->tlsdesc_plt = (bfd_vma) - 1;
8423                     }
8424
8425                   if (got_type & GOT_TLS_GD)
8426                     htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
8427
8428                   if (got_type & GOT_TLS_IE
8429                       || got_type & GOT_NORMAL)
8430                     htab->root.srelgot->size += RELOC_SIZE (htab);
8431                 }
8432             }
8433           else
8434             {
8435               locals[i].got_refcount = (bfd_vma) - 1;
8436             }
8437         }
8438     }
8439
8440
8441   /* Allocate global sym .plt and .got entries, and space for global
8442      sym dynamic relocs.  */
8443   elf_link_hash_traverse (&htab->root, elfNN_aarch64_allocate_dynrelocs,
8444                           info);
8445
8446   /* Allocate global ifunc sym .plt and .got entries, and space for global
8447      ifunc sym dynamic relocs.  */
8448   elf_link_hash_traverse (&htab->root, elfNN_aarch64_allocate_ifunc_dynrelocs,
8449                           info);
8450
8451   /* Allocate .plt and .got entries, and space for local symbols.  */
8452   htab_traverse (htab->loc_hash_table,
8453                  elfNN_aarch64_allocate_local_dynrelocs,
8454                  info);
8455
8456   /* Allocate .plt and .got entries, and space for local ifunc symbols.  */
8457   htab_traverse (htab->loc_hash_table,
8458                  elfNN_aarch64_allocate_local_ifunc_dynrelocs,
8459                  info);
8460
8461   /* For every jump slot reserved in the sgotplt, reloc_count is
8462      incremented.  However, when we reserve space for TLS descriptors,
8463      it's not incremented, so in order to compute the space reserved
8464      for them, it suffices to multiply the reloc count by the jump
8465      slot size.  */
8466
8467   if (htab->root.srelplt)
8468     htab->sgotplt_jump_table_size = aarch64_compute_jump_table_size (htab);
8469
8470   if (htab->tlsdesc_plt)
8471     {
8472       if (htab->root.splt->size == 0)
8473         htab->root.splt->size += PLT_ENTRY_SIZE;
8474
8475       htab->tlsdesc_plt = htab->root.splt->size;
8476       htab->root.splt->size += PLT_TLSDESC_ENTRY_SIZE;
8477
8478       /* If we're not using lazy TLS relocations, don't generate the
8479          GOT entry required.  */
8480       if (!(info->flags & DF_BIND_NOW))
8481         {
8482           htab->dt_tlsdesc_got = htab->root.sgot->size;
8483           htab->root.sgot->size += GOT_ENTRY_SIZE;
8484         }
8485     }
8486
8487   /* Init mapping symbols information to use later to distingush between
8488      code and data while scanning for errata.  */
8489   if (htab->fix_erratum_835769 || htab->fix_erratum_843419)
8490     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8491       {
8492         if (!is_aarch64_elf (ibfd))
8493           continue;
8494         bfd_elfNN_aarch64_init_maps (ibfd);
8495       }
8496
8497   /* We now have determined the sizes of the various dynamic sections.
8498      Allocate memory for them.  */
8499   relocs = FALSE;
8500   for (s = dynobj->sections; s != NULL; s = s->next)
8501     {
8502       if ((s->flags & SEC_LINKER_CREATED) == 0)
8503         continue;
8504
8505       if (s == htab->root.splt
8506           || s == htab->root.sgot
8507           || s == htab->root.sgotplt
8508           || s == htab->root.iplt
8509           || s == htab->root.igotplt || s == htab->sdynbss)
8510         {
8511           /* Strip this section if we don't need it; see the
8512              comment below.  */
8513         }
8514       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
8515         {
8516           if (s->size != 0 && s != htab->root.srelplt)
8517             relocs = TRUE;
8518
8519           /* We use the reloc_count field as a counter if we need
8520              to copy relocs into the output file.  */
8521           if (s != htab->root.srelplt)
8522             s->reloc_count = 0;
8523         }
8524       else
8525         {
8526           /* It's not one of our sections, so don't allocate space.  */
8527           continue;
8528         }
8529
8530       if (s->size == 0)
8531         {
8532           /* If we don't need this section, strip it from the
8533              output file.  This is mostly to handle .rela.bss and
8534              .rela.plt.  We must create both sections in
8535              create_dynamic_sections, because they must be created
8536              before the linker maps input sections to output
8537              sections.  The linker does that before
8538              adjust_dynamic_symbol is called, and it is that
8539              function which decides whether anything needs to go
8540              into these sections.  */
8541
8542           s->flags |= SEC_EXCLUDE;
8543           continue;
8544         }
8545
8546       if ((s->flags & SEC_HAS_CONTENTS) == 0)
8547         continue;
8548
8549       /* Allocate memory for the section contents.  We use bfd_zalloc
8550          here in case unused entries are not reclaimed before the
8551          section's contents are written out.  This should not happen,
8552          but this way if it does, we get a R_AARCH64_NONE reloc instead
8553          of garbage.  */
8554       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
8555       if (s->contents == NULL)
8556         return FALSE;
8557     }
8558
8559   if (htab->root.dynamic_sections_created)
8560     {
8561       /* Add some entries to the .dynamic section.  We fill in the
8562          values later, in elfNN_aarch64_finish_dynamic_sections, but we
8563          must add the entries now so that we get the correct size for
8564          the .dynamic section.  The DT_DEBUG entry is filled in by the
8565          dynamic linker and used by the debugger.  */
8566 #define add_dynamic_entry(TAG, VAL)                     \
8567       _bfd_elf_add_dynamic_entry (info, TAG, VAL)
8568
8569       if (bfd_link_executable (info))
8570         {
8571           if (!add_dynamic_entry (DT_DEBUG, 0))
8572             return FALSE;
8573         }
8574
8575       if (htab->root.splt->size != 0)
8576         {
8577           if (!add_dynamic_entry (DT_PLTGOT, 0)
8578               || !add_dynamic_entry (DT_PLTRELSZ, 0)
8579               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
8580               || !add_dynamic_entry (DT_JMPREL, 0))
8581             return FALSE;
8582
8583           if (htab->tlsdesc_plt
8584               && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
8585                   || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
8586             return FALSE;
8587         }
8588
8589       if (relocs)
8590         {
8591           if (!add_dynamic_entry (DT_RELA, 0)
8592               || !add_dynamic_entry (DT_RELASZ, 0)
8593               || !add_dynamic_entry (DT_RELAENT, RELOC_SIZE (htab)))
8594             return FALSE;
8595
8596           /* If any dynamic relocs apply to a read-only section,
8597              then we need a DT_TEXTREL entry.  */
8598           if ((info->flags & DF_TEXTREL) == 0)
8599             elf_link_hash_traverse (& htab->root, aarch64_readonly_dynrelocs,
8600                                     info);
8601
8602           if ((info->flags & DF_TEXTREL) != 0)
8603             {
8604               if (!add_dynamic_entry (DT_TEXTREL, 0))
8605                 return FALSE;
8606             }
8607         }
8608     }
8609 #undef add_dynamic_entry
8610
8611   return TRUE;
8612 }
8613
8614 static inline void
8615 elf_aarch64_update_plt_entry (bfd *output_bfd,
8616                               bfd_reloc_code_real_type r_type,
8617                               bfd_byte *plt_entry, bfd_vma value)
8618 {
8619   reloc_howto_type *howto = elfNN_aarch64_howto_from_bfd_reloc (r_type);
8620
8621   _bfd_aarch64_elf_put_addend (output_bfd, plt_entry, r_type, howto, value);
8622 }
8623
8624 static void
8625 elfNN_aarch64_create_small_pltn_entry (struct elf_link_hash_entry *h,
8626                                        struct elf_aarch64_link_hash_table
8627                                        *htab, bfd *output_bfd,
8628                                        struct bfd_link_info *info)
8629 {
8630   bfd_byte *plt_entry;
8631   bfd_vma plt_index;
8632   bfd_vma got_offset;
8633   bfd_vma gotplt_entry_address;
8634   bfd_vma plt_entry_address;
8635   Elf_Internal_Rela rela;
8636   bfd_byte *loc;
8637   asection *plt, *gotplt, *relplt;
8638
8639   /* When building a static executable, use .iplt, .igot.plt and
8640      .rela.iplt sections for STT_GNU_IFUNC symbols.  */
8641   if (htab->root.splt != NULL)
8642     {
8643       plt = htab->root.splt;
8644       gotplt = htab->root.sgotplt;
8645       relplt = htab->root.srelplt;
8646     }
8647   else
8648     {
8649       plt = htab->root.iplt;
8650       gotplt = htab->root.igotplt;
8651       relplt = htab->root.irelplt;
8652     }
8653
8654   /* Get the index in the procedure linkage table which
8655      corresponds to this symbol.  This is the index of this symbol
8656      in all the symbols for which we are making plt entries.  The
8657      first entry in the procedure linkage table is reserved.
8658
8659      Get the offset into the .got table of the entry that
8660      corresponds to this function.      Each .got entry is GOT_ENTRY_SIZE
8661      bytes. The first three are reserved for the dynamic linker.
8662
8663      For static executables, we don't reserve anything.  */
8664
8665   if (plt == htab->root.splt)
8666     {
8667       plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
8668       got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
8669     }
8670   else
8671     {
8672       plt_index = h->plt.offset / htab->plt_entry_size;
8673       got_offset = plt_index * GOT_ENTRY_SIZE;
8674     }
8675
8676   plt_entry = plt->contents + h->plt.offset;
8677   plt_entry_address = plt->output_section->vma
8678     + plt->output_offset + h->plt.offset;
8679   gotplt_entry_address = gotplt->output_section->vma +
8680     gotplt->output_offset + got_offset;
8681
8682   /* Copy in the boiler-plate for the PLTn entry.  */
8683   memcpy (plt_entry, elfNN_aarch64_small_plt_entry, PLT_SMALL_ENTRY_SIZE);
8684
8685   /* Fill in the top 21 bits for this: ADRP x16, PLT_GOT + n * 8.
8686      ADRP:   ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
8687   elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADR_HI21_PCREL,
8688                                 plt_entry,
8689                                 PG (gotplt_entry_address) -
8690                                 PG (plt_entry_address));
8691
8692   /* Fill in the lo12 bits for the load from the pltgot.  */
8693   elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_LDSTNN_LO12,
8694                                 plt_entry + 4,
8695                                 PG_OFFSET (gotplt_entry_address));
8696
8697   /* Fill in the lo12 bits for the add from the pltgot entry.  */
8698   elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADD_LO12,
8699                                 plt_entry + 8,
8700                                 PG_OFFSET (gotplt_entry_address));
8701
8702   /* All the GOTPLT Entries are essentially initialized to PLT0.  */
8703   bfd_put_NN (output_bfd,
8704               plt->output_section->vma + plt->output_offset,
8705               gotplt->contents + got_offset);
8706
8707   rela.r_offset = gotplt_entry_address;
8708
8709   if (h->dynindx == -1
8710       || ((bfd_link_executable (info)
8711            || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8712           && h->def_regular
8713           && h->type == STT_GNU_IFUNC))
8714     {
8715       /* If an STT_GNU_IFUNC symbol is locally defined, generate
8716          R_AARCH64_IRELATIVE instead of R_AARCH64_JUMP_SLOT.  */
8717       rela.r_info = ELFNN_R_INFO (0, AARCH64_R (IRELATIVE));
8718       rela.r_addend = (h->root.u.def.value
8719                        + h->root.u.def.section->output_section->vma
8720                        + h->root.u.def.section->output_offset);
8721     }
8722   else
8723     {
8724       /* Fill in the entry in the .rela.plt section.  */
8725       rela.r_info = ELFNN_R_INFO (h->dynindx, AARCH64_R (JUMP_SLOT));
8726       rela.r_addend = 0;
8727     }
8728
8729   /* Compute the relocation entry to used based on PLT index and do
8730      not adjust reloc_count. The reloc_count has already been adjusted
8731      to account for this entry.  */
8732   loc = relplt->contents + plt_index * RELOC_SIZE (htab);
8733   bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
8734 }
8735
8736 /* Size sections even though they're not dynamic.  We use it to setup
8737    _TLS_MODULE_BASE_, if needed.  */
8738
8739 static bfd_boolean
8740 elfNN_aarch64_always_size_sections (bfd *output_bfd,
8741                                     struct bfd_link_info *info)
8742 {
8743   asection *tls_sec;
8744
8745   if (bfd_link_relocatable (info))
8746     return TRUE;
8747
8748   tls_sec = elf_hash_table (info)->tls_sec;
8749
8750   if (tls_sec)
8751     {
8752       struct elf_link_hash_entry *tlsbase;
8753
8754       tlsbase = elf_link_hash_lookup (elf_hash_table (info),
8755                                       "_TLS_MODULE_BASE_", TRUE, TRUE, FALSE);
8756
8757       if (tlsbase)
8758         {
8759           struct bfd_link_hash_entry *h = NULL;
8760           const struct elf_backend_data *bed =
8761             get_elf_backend_data (output_bfd);
8762
8763           if (!(_bfd_generic_link_add_one_symbol
8764                 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
8765                  tls_sec, 0, NULL, FALSE, bed->collect, &h)))
8766             return FALSE;
8767
8768           tlsbase->type = STT_TLS;
8769           tlsbase = (struct elf_link_hash_entry *) h;
8770           tlsbase->def_regular = 1;
8771           tlsbase->other = STV_HIDDEN;
8772           (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
8773         }
8774     }
8775
8776   return TRUE;
8777 }
8778
8779 /* Finish up dynamic symbol handling.  We set the contents of various
8780    dynamic sections here.  */
8781 static bfd_boolean
8782 elfNN_aarch64_finish_dynamic_symbol (bfd *output_bfd,
8783                                      struct bfd_link_info *info,
8784                                      struct elf_link_hash_entry *h,
8785                                      Elf_Internal_Sym *sym)
8786 {
8787   struct elf_aarch64_link_hash_table *htab;
8788   htab = elf_aarch64_hash_table (info);
8789
8790   if (h->plt.offset != (bfd_vma) - 1)
8791     {
8792       asection *plt, *gotplt, *relplt;
8793
8794       /* This symbol has an entry in the procedure linkage table.  Set
8795          it up.  */
8796
8797       /* When building a static executable, use .iplt, .igot.plt and
8798          .rela.iplt sections for STT_GNU_IFUNC symbols.  */
8799       if (htab->root.splt != NULL)
8800         {
8801           plt = htab->root.splt;
8802           gotplt = htab->root.sgotplt;
8803           relplt = htab->root.srelplt;
8804         }
8805       else
8806         {
8807           plt = htab->root.iplt;
8808           gotplt = htab->root.igotplt;
8809           relplt = htab->root.irelplt;
8810         }
8811
8812       /* This symbol has an entry in the procedure linkage table.  Set
8813          it up.  */
8814       if ((h->dynindx == -1
8815            && !((h->forced_local || bfd_link_executable (info))
8816                 && h->def_regular
8817                 && h->type == STT_GNU_IFUNC))
8818           || plt == NULL
8819           || gotplt == NULL
8820           || relplt == NULL)
8821         abort ();
8822
8823       elfNN_aarch64_create_small_pltn_entry (h, htab, output_bfd, info);
8824       if (!h->def_regular)
8825         {
8826           /* Mark the symbol as undefined, rather than as defined in
8827              the .plt section.  */
8828           sym->st_shndx = SHN_UNDEF;
8829           /* If the symbol is weak we need to clear the value.
8830              Otherwise, the PLT entry would provide a definition for
8831              the symbol even if the symbol wasn't defined anywhere,
8832              and so the symbol would never be NULL.  Leave the value if
8833              there were any relocations where pointer equality matters
8834              (this is a clue for the dynamic linker, to make function
8835              pointer comparisons work between an application and shared
8836              library).  */
8837           if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
8838             sym->st_value = 0;
8839         }
8840     }
8841
8842   if (h->got.offset != (bfd_vma) - 1
8843       && elf_aarch64_hash_entry (h)->got_type == GOT_NORMAL)
8844     {
8845       Elf_Internal_Rela rela;
8846       bfd_byte *loc;
8847
8848       /* This symbol has an entry in the global offset table.  Set it
8849          up.  */
8850       if (htab->root.sgot == NULL || htab->root.srelgot == NULL)
8851         abort ();
8852
8853       rela.r_offset = (htab->root.sgot->output_section->vma
8854                        + htab->root.sgot->output_offset
8855                        + (h->got.offset & ~(bfd_vma) 1));
8856
8857       if (h->def_regular
8858           && h->type == STT_GNU_IFUNC)
8859         {
8860           if (bfd_link_pic (info))
8861             {
8862               /* Generate R_AARCH64_GLOB_DAT.  */
8863               goto do_glob_dat;
8864             }
8865           else
8866             {
8867               asection *plt;
8868
8869               if (!h->pointer_equality_needed)
8870                 abort ();
8871
8872               /* For non-shared object, we can't use .got.plt, which
8873                  contains the real function address if we need pointer
8874                  equality.  We load the GOT entry with the PLT entry.  */
8875               plt = htab->root.splt ? htab->root.splt : htab->root.iplt;
8876               bfd_put_NN (output_bfd, (plt->output_section->vma
8877                                        + plt->output_offset
8878                                        + h->plt.offset),
8879                           htab->root.sgot->contents
8880                           + (h->got.offset & ~(bfd_vma) 1));
8881               return TRUE;
8882             }
8883         }
8884       else if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
8885         {
8886           if (!h->def_regular)
8887             return FALSE;
8888
8889           BFD_ASSERT ((h->got.offset & 1) != 0);
8890           rela.r_info = ELFNN_R_INFO (0, AARCH64_R (RELATIVE));
8891           rela.r_addend = (h->root.u.def.value
8892                            + h->root.u.def.section->output_section->vma
8893                            + h->root.u.def.section->output_offset);
8894         }
8895       else
8896         {
8897 do_glob_dat:
8898           BFD_ASSERT ((h->got.offset & 1) == 0);
8899           bfd_put_NN (output_bfd, (bfd_vma) 0,
8900                       htab->root.sgot->contents + h->got.offset);
8901           rela.r_info = ELFNN_R_INFO (h->dynindx, AARCH64_R (GLOB_DAT));
8902           rela.r_addend = 0;
8903         }
8904
8905       loc = htab->root.srelgot->contents;
8906       loc += htab->root.srelgot->reloc_count++ * RELOC_SIZE (htab);
8907       bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
8908     }
8909
8910   if (h->needs_copy)
8911     {
8912       Elf_Internal_Rela rela;
8913       bfd_byte *loc;
8914
8915       /* This symbol needs a copy reloc.  Set it up.  */
8916
8917       if (h->dynindx == -1
8918           || (h->root.type != bfd_link_hash_defined
8919               && h->root.type != bfd_link_hash_defweak)
8920           || htab->srelbss == NULL)
8921         abort ();
8922
8923       rela.r_offset = (h->root.u.def.value
8924                        + h->root.u.def.section->output_section->vma
8925                        + h->root.u.def.section->output_offset);
8926       rela.r_info = ELFNN_R_INFO (h->dynindx, AARCH64_R (COPY));
8927       rela.r_addend = 0;
8928       loc = htab->srelbss->contents;
8929       loc += htab->srelbss->reloc_count++ * RELOC_SIZE (htab);
8930       bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
8931     }
8932
8933   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  SYM may
8934      be NULL for local symbols.  */
8935   if (sym != NULL
8936       && (h == elf_hash_table (info)->hdynamic
8937           || h == elf_hash_table (info)->hgot))
8938     sym->st_shndx = SHN_ABS;
8939
8940   return TRUE;
8941 }
8942
8943 /* Finish up local dynamic symbol handling.  We set the contents of
8944    various dynamic sections here.  */
8945
8946 static bfd_boolean
8947 elfNN_aarch64_finish_local_dynamic_symbol (void **slot, void *inf)
8948 {
8949   struct elf_link_hash_entry *h
8950     = (struct elf_link_hash_entry *) *slot;
8951   struct bfd_link_info *info
8952     = (struct bfd_link_info *) inf;
8953
8954   return elfNN_aarch64_finish_dynamic_symbol (info->output_bfd,
8955                                               info, h, NULL);
8956 }
8957
8958 static void
8959 elfNN_aarch64_init_small_plt0_entry (bfd *output_bfd ATTRIBUTE_UNUSED,
8960                                      struct elf_aarch64_link_hash_table
8961                                      *htab)
8962 {
8963   /* Fill in PLT0. Fixme:RR Note this doesn't distinguish between
8964      small and large plts and at the minute just generates
8965      the small PLT.  */
8966
8967   /* PLT0 of the small PLT looks like this in ELF64 -
8968      stp x16, x30, [sp, #-16]!          // Save the reloc and lr on stack.
8969      adrp x16, PLT_GOT + 16             // Get the page base of the GOTPLT
8970      ldr  x17, [x16, #:lo12:PLT_GOT+16] // Load the address of the
8971                                         // symbol resolver
8972      add  x16, x16, #:lo12:PLT_GOT+16   // Load the lo12 bits of the
8973                                         // GOTPLT entry for this.
8974      br   x17
8975      PLT0 will be slightly different in ELF32 due to different got entry
8976      size.
8977    */
8978   bfd_vma plt_got_2nd_ent;      /* Address of GOT[2].  */
8979   bfd_vma plt_base;
8980
8981
8982   memcpy (htab->root.splt->contents, elfNN_aarch64_small_plt0_entry,
8983           PLT_ENTRY_SIZE);
8984   elf_section_data (htab->root.splt->output_section)->this_hdr.sh_entsize =
8985     PLT_ENTRY_SIZE;
8986
8987   plt_got_2nd_ent = (htab->root.sgotplt->output_section->vma
8988                   + htab->root.sgotplt->output_offset
8989                   + GOT_ENTRY_SIZE * 2);
8990
8991   plt_base = htab->root.splt->output_section->vma +
8992     htab->root.splt->output_offset;
8993
8994   /* Fill in the top 21 bits for this: ADRP x16, PLT_GOT + n * 8.
8995      ADRP:   ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
8996   elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADR_HI21_PCREL,
8997                                 htab->root.splt->contents + 4,
8998                                 PG (plt_got_2nd_ent) - PG (plt_base + 4));
8999
9000   elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_LDSTNN_LO12,
9001                                 htab->root.splt->contents + 8,
9002                                 PG_OFFSET (plt_got_2nd_ent));
9003
9004   elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADD_LO12,
9005                                 htab->root.splt->contents + 12,
9006                                 PG_OFFSET (plt_got_2nd_ent));
9007 }
9008
9009 static bfd_boolean
9010 elfNN_aarch64_finish_dynamic_sections (bfd *output_bfd,
9011                                        struct bfd_link_info *info)
9012 {
9013   struct elf_aarch64_link_hash_table *htab;
9014   bfd *dynobj;
9015   asection *sdyn;
9016
9017   htab = elf_aarch64_hash_table (info);
9018   dynobj = htab->root.dynobj;
9019   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
9020
9021   if (htab->root.dynamic_sections_created)
9022     {
9023       ElfNN_External_Dyn *dyncon, *dynconend;
9024
9025       if (sdyn == NULL || htab->root.sgot == NULL)
9026         abort ();
9027
9028       dyncon = (ElfNN_External_Dyn *) sdyn->contents;
9029       dynconend = (ElfNN_External_Dyn *) (sdyn->contents + sdyn->size);
9030       for (; dyncon < dynconend; dyncon++)
9031         {
9032           Elf_Internal_Dyn dyn;
9033           asection *s;
9034
9035           bfd_elfNN_swap_dyn_in (dynobj, dyncon, &dyn);
9036
9037           switch (dyn.d_tag)
9038             {
9039             default:
9040               continue;
9041
9042             case DT_PLTGOT:
9043               s = htab->root.sgotplt;
9044               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
9045               break;
9046
9047             case DT_JMPREL:
9048               s = htab->root.srelplt;
9049               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
9050               break;
9051
9052             case DT_PLTRELSZ:
9053               s = htab->root.srelplt;
9054               dyn.d_un.d_val = s->size;
9055               break;
9056
9057             case DT_RELASZ:
9058               /* The procedure linkage table relocs (DT_JMPREL) should
9059                  not be included in the overall relocs (DT_RELA).
9060                  Therefore, we override the DT_RELASZ entry here to
9061                  make it not include the JMPREL relocs.  Since the
9062                  linker script arranges for .rela.plt to follow all
9063                  other relocation sections, we don't have to worry
9064                  about changing the DT_RELA entry.  */
9065               if (htab->root.srelplt != NULL)
9066                 {
9067                   s = htab->root.srelplt;
9068                   dyn.d_un.d_val -= s->size;
9069                 }
9070               break;
9071
9072             case DT_TLSDESC_PLT:
9073               s = htab->root.splt;
9074               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
9075                 + htab->tlsdesc_plt;
9076               break;
9077
9078             case DT_TLSDESC_GOT:
9079               s = htab->root.sgot;
9080               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
9081                 + htab->dt_tlsdesc_got;
9082               break;
9083             }
9084
9085           bfd_elfNN_swap_dyn_out (output_bfd, &dyn, dyncon);
9086         }
9087
9088     }
9089
9090   /* Fill in the special first entry in the procedure linkage table.  */
9091   if (htab->root.splt && htab->root.splt->size > 0)
9092     {
9093       elfNN_aarch64_init_small_plt0_entry (output_bfd, htab);
9094
9095       elf_section_data (htab->root.splt->output_section)->
9096         this_hdr.sh_entsize = htab->plt_entry_size;
9097
9098
9099       if (htab->tlsdesc_plt)
9100         {
9101           bfd_put_NN (output_bfd, (bfd_vma) 0,
9102                       htab->root.sgot->contents + htab->dt_tlsdesc_got);
9103
9104           memcpy (htab->root.splt->contents + htab->tlsdesc_plt,
9105                   elfNN_aarch64_tlsdesc_small_plt_entry,
9106                   sizeof (elfNN_aarch64_tlsdesc_small_plt_entry));
9107
9108           {
9109             bfd_vma adrp1_addr =
9110               htab->root.splt->output_section->vma
9111               + htab->root.splt->output_offset + htab->tlsdesc_plt + 4;
9112
9113             bfd_vma adrp2_addr = adrp1_addr + 4;
9114
9115             bfd_vma got_addr =
9116               htab->root.sgot->output_section->vma
9117               + htab->root.sgot->output_offset;
9118
9119             bfd_vma pltgot_addr =
9120               htab->root.sgotplt->output_section->vma
9121               + htab->root.sgotplt->output_offset;
9122
9123             bfd_vma dt_tlsdesc_got = got_addr + htab->dt_tlsdesc_got;
9124
9125             bfd_byte *plt_entry =
9126               htab->root.splt->contents + htab->tlsdesc_plt;
9127
9128             /* adrp x2, DT_TLSDESC_GOT */
9129             elf_aarch64_update_plt_entry (output_bfd,
9130                                           BFD_RELOC_AARCH64_ADR_HI21_PCREL,
9131                                           plt_entry + 4,
9132                                           (PG (dt_tlsdesc_got)
9133                                            - PG (adrp1_addr)));
9134
9135             /* adrp x3, 0 */
9136             elf_aarch64_update_plt_entry (output_bfd,
9137                                           BFD_RELOC_AARCH64_ADR_HI21_PCREL,
9138                                           plt_entry + 8,
9139                                           (PG (pltgot_addr)
9140                                            - PG (adrp2_addr)));
9141
9142             /* ldr x2, [x2, #0] */
9143             elf_aarch64_update_plt_entry (output_bfd,
9144                                           BFD_RELOC_AARCH64_LDSTNN_LO12,
9145                                           plt_entry + 12,
9146                                           PG_OFFSET (dt_tlsdesc_got));
9147
9148             /* add x3, x3, 0 */
9149             elf_aarch64_update_plt_entry (output_bfd,
9150                                           BFD_RELOC_AARCH64_ADD_LO12,
9151                                           plt_entry + 16,
9152                                           PG_OFFSET (pltgot_addr));
9153           }
9154         }
9155     }
9156
9157   if (htab->root.sgotplt)
9158     {
9159       if (bfd_is_abs_section (htab->root.sgotplt->output_section))
9160         {
9161           _bfd_error_handler
9162             (_("discarded output section: `%A'"), htab->root.sgotplt);
9163           return FALSE;
9164         }
9165
9166       /* Fill in the first three entries in the global offset table.  */
9167       if (htab->root.sgotplt->size > 0)
9168         {
9169           bfd_put_NN (output_bfd, (bfd_vma) 0, htab->root.sgotplt->contents);
9170
9171           /* Write GOT[1] and GOT[2], needed for the dynamic linker.  */
9172           bfd_put_NN (output_bfd,
9173                       (bfd_vma) 0,
9174                       htab->root.sgotplt->contents + GOT_ENTRY_SIZE);
9175           bfd_put_NN (output_bfd,
9176                       (bfd_vma) 0,
9177                       htab->root.sgotplt->contents + GOT_ENTRY_SIZE * 2);
9178         }
9179
9180       if (htab->root.sgot)
9181         {
9182           if (htab->root.sgot->size > 0)
9183             {
9184               bfd_vma addr =
9185                 sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0;
9186               bfd_put_NN (output_bfd, addr, htab->root.sgot->contents);
9187             }
9188         }
9189
9190       elf_section_data (htab->root.sgotplt->output_section)->
9191         this_hdr.sh_entsize = GOT_ENTRY_SIZE;
9192     }
9193
9194   if (htab->root.sgot && htab->root.sgot->size > 0)
9195     elf_section_data (htab->root.sgot->output_section)->this_hdr.sh_entsize
9196       = GOT_ENTRY_SIZE;
9197
9198   /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols.  */
9199   htab_traverse (htab->loc_hash_table,
9200                  elfNN_aarch64_finish_local_dynamic_symbol,
9201                  info);
9202
9203   return TRUE;
9204 }
9205
9206 /* Return address for Ith PLT stub in section PLT, for relocation REL
9207    or (bfd_vma) -1 if it should not be included.  */
9208
9209 static bfd_vma
9210 elfNN_aarch64_plt_sym_val (bfd_vma i, const asection *plt,
9211                            const arelent *rel ATTRIBUTE_UNUSED)
9212 {
9213   return plt->vma + PLT_ENTRY_SIZE + i * PLT_SMALL_ENTRY_SIZE;
9214 }
9215
9216 /* Returns TRUE if NAME is an AArch64 mapping symbol.
9217    The ARM ELF standard defines $x (for A64 code) and $d (for data).
9218    It also allows a period initiated suffix to be added to the symbol, ie:
9219    "$[adtx]\.[:sym_char]+".  */
9220
9221 static bfd_boolean
9222 is_aarch64_mapping_symbol (const char * name)
9223 {
9224   return name != NULL /* Paranoia.  */
9225     && name[0] == '$' /* Note: if objcopy --prefix-symbols has been used then
9226                          the mapping symbols could have acquired a prefix.
9227                          We do not support this here, since such symbols no
9228                          longer conform to the ARM ELF ABI.  */
9229     && (name[1] == 'd' || name[1] == 'x')
9230     && (name[2] == 0 || name[2] == '.');
9231   /* FIXME: Strictly speaking the symbol is only a valid mapping symbol if
9232      any characters that follow the period are legal characters for the body
9233      of a symbol's name.  For now we just assume that this is the case.  */
9234 }
9235
9236 /* Make sure that mapping symbols in object files are not removed via the
9237    "strip --strip-unneeded" tool.  These symbols might needed in order to
9238    correctly generate linked files.  Once an object file has been linked,
9239    it should be safe to remove them.  */
9240
9241 static void
9242 elfNN_aarch64_backend_symbol_processing (bfd *abfd, asymbol *sym)
9243 {
9244   if (((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
9245       && sym->section != bfd_abs_section_ptr
9246       && is_aarch64_mapping_symbol (sym->name))
9247     sym->flags |= BSF_KEEP;
9248 }
9249
9250
9251 /* We use this so we can override certain functions
9252    (though currently we don't).  */
9253
9254 const struct elf_size_info elfNN_aarch64_size_info =
9255 {
9256   sizeof (ElfNN_External_Ehdr),
9257   sizeof (ElfNN_External_Phdr),
9258   sizeof (ElfNN_External_Shdr),
9259   sizeof (ElfNN_External_Rel),
9260   sizeof (ElfNN_External_Rela),
9261   sizeof (ElfNN_External_Sym),
9262   sizeof (ElfNN_External_Dyn),
9263   sizeof (Elf_External_Note),
9264   4,                            /* Hash table entry size.  */
9265   1,                            /* Internal relocs per external relocs.  */
9266   ARCH_SIZE,                    /* Arch size.  */
9267   LOG_FILE_ALIGN,               /* Log_file_align.  */
9268   ELFCLASSNN, EV_CURRENT,
9269   bfd_elfNN_write_out_phdrs,
9270   bfd_elfNN_write_shdrs_and_ehdr,
9271   bfd_elfNN_checksum_contents,
9272   bfd_elfNN_write_relocs,
9273   bfd_elfNN_swap_symbol_in,
9274   bfd_elfNN_swap_symbol_out,
9275   bfd_elfNN_slurp_reloc_table,
9276   bfd_elfNN_slurp_symbol_table,
9277   bfd_elfNN_swap_dyn_in,
9278   bfd_elfNN_swap_dyn_out,
9279   bfd_elfNN_swap_reloc_in,
9280   bfd_elfNN_swap_reloc_out,
9281   bfd_elfNN_swap_reloca_in,
9282   bfd_elfNN_swap_reloca_out
9283 };
9284
9285 #define ELF_ARCH                        bfd_arch_aarch64
9286 #define ELF_MACHINE_CODE                EM_AARCH64
9287 #define ELF_MAXPAGESIZE                 0x10000
9288 #define ELF_MINPAGESIZE                 0x1000
9289 #define ELF_COMMONPAGESIZE              0x1000
9290
9291 #define bfd_elfNN_close_and_cleanup             \
9292   elfNN_aarch64_close_and_cleanup
9293
9294 #define bfd_elfNN_bfd_free_cached_info          \
9295   elfNN_aarch64_bfd_free_cached_info
9296
9297 #define bfd_elfNN_bfd_is_target_special_symbol  \
9298   elfNN_aarch64_is_target_special_symbol
9299
9300 #define bfd_elfNN_bfd_link_hash_table_create    \
9301   elfNN_aarch64_link_hash_table_create
9302
9303 #define bfd_elfNN_bfd_merge_private_bfd_data    \
9304   elfNN_aarch64_merge_private_bfd_data
9305
9306 #define bfd_elfNN_bfd_print_private_bfd_data    \
9307   elfNN_aarch64_print_private_bfd_data
9308
9309 #define bfd_elfNN_bfd_reloc_type_lookup         \
9310   elfNN_aarch64_reloc_type_lookup
9311
9312 #define bfd_elfNN_bfd_reloc_name_lookup         \
9313   elfNN_aarch64_reloc_name_lookup
9314
9315 #define bfd_elfNN_bfd_set_private_flags         \
9316   elfNN_aarch64_set_private_flags
9317
9318 #define bfd_elfNN_find_inliner_info             \
9319   elfNN_aarch64_find_inliner_info
9320
9321 #define bfd_elfNN_find_nearest_line             \
9322   elfNN_aarch64_find_nearest_line
9323
9324 #define bfd_elfNN_mkobject                      \
9325   elfNN_aarch64_mkobject
9326
9327 #define bfd_elfNN_new_section_hook              \
9328   elfNN_aarch64_new_section_hook
9329
9330 #define elf_backend_adjust_dynamic_symbol       \
9331   elfNN_aarch64_adjust_dynamic_symbol
9332
9333 #define elf_backend_always_size_sections        \
9334   elfNN_aarch64_always_size_sections
9335
9336 #define elf_backend_check_relocs                \
9337   elfNN_aarch64_check_relocs
9338
9339 #define elf_backend_copy_indirect_symbol        \
9340   elfNN_aarch64_copy_indirect_symbol
9341
9342 /* Create .dynbss, and .rela.bss sections in DYNOBJ, and set up shortcuts
9343    to them in our hash.  */
9344 #define elf_backend_create_dynamic_sections     \
9345   elfNN_aarch64_create_dynamic_sections
9346
9347 #define elf_backend_init_index_section          \
9348   _bfd_elf_init_2_index_sections
9349
9350 #define elf_backend_finish_dynamic_sections     \
9351   elfNN_aarch64_finish_dynamic_sections
9352
9353 #define elf_backend_finish_dynamic_symbol       \
9354   elfNN_aarch64_finish_dynamic_symbol
9355
9356 #define elf_backend_gc_sweep_hook               \
9357   elfNN_aarch64_gc_sweep_hook
9358
9359 #define elf_backend_object_p                    \
9360   elfNN_aarch64_object_p
9361
9362 #define elf_backend_output_arch_local_syms      \
9363   elfNN_aarch64_output_arch_local_syms
9364
9365 #define elf_backend_plt_sym_val                 \
9366   elfNN_aarch64_plt_sym_val
9367
9368 #define elf_backend_post_process_headers        \
9369   elfNN_aarch64_post_process_headers
9370
9371 #define elf_backend_relocate_section            \
9372   elfNN_aarch64_relocate_section
9373
9374 #define elf_backend_reloc_type_class            \
9375   elfNN_aarch64_reloc_type_class
9376
9377 #define elf_backend_section_from_shdr           \
9378   elfNN_aarch64_section_from_shdr
9379
9380 #define elf_backend_size_dynamic_sections       \
9381   elfNN_aarch64_size_dynamic_sections
9382
9383 #define elf_backend_size_info                   \
9384   elfNN_aarch64_size_info
9385
9386 #define elf_backend_write_section               \
9387   elfNN_aarch64_write_section
9388
9389 #define elf_backend_symbol_processing           \
9390   elfNN_aarch64_backend_symbol_processing
9391
9392 #define elf_backend_can_refcount       1
9393 #define elf_backend_can_gc_sections    1
9394 #define elf_backend_plt_readonly       1
9395 #define elf_backend_want_got_plt       1
9396 #define elf_backend_want_plt_sym       0
9397 #define elf_backend_may_use_rel_p      0
9398 #define elf_backend_may_use_rela_p     1
9399 #define elf_backend_default_use_rela_p 1
9400 #define elf_backend_rela_normal        1
9401 #define elf_backend_got_header_size (GOT_ENTRY_SIZE * 3)
9402 #define elf_backend_default_execstack  0
9403 #define elf_backend_extern_protected_data 1
9404
9405 #undef  elf_backend_obj_attrs_section
9406 #define elf_backend_obj_attrs_section           ".ARM.attributes"
9407
9408 #include "elfNN-target.h"
9409
9410 /* CloudABI support.  */
9411
9412 #undef  TARGET_LITTLE_SYM
9413 #define TARGET_LITTLE_SYM       aarch64_elfNN_le_cloudabi_vec
9414 #undef  TARGET_LITTLE_NAME
9415 #define TARGET_LITTLE_NAME      "elfNN-littleaarch64-cloudabi"
9416 #undef  TARGET_BIG_SYM
9417 #define TARGET_BIG_SYM          aarch64_elfNN_be_cloudabi_vec
9418 #undef  TARGET_BIG_NAME
9419 #define TARGET_BIG_NAME         "elfNN-bigaarch64-cloudabi"
9420
9421 #undef  ELF_OSABI
9422 #define ELF_OSABI               ELFOSABI_CLOUDABI
9423
9424 #undef  elfNN_bed
9425 #define elfNN_bed               elfNN_aarch64_cloudabi_bed
9426
9427 #include "elfNN-target.h"