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