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