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