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