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