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