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