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