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