Remove _bfd_dwarf2_find_nearest_line addr_size parameter
[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   HOWTO (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   HOWTO (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   HOWTO (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   erratum_84319_opts fix_erratum_843419;
2632
2633   /* Don't apply link-time values for dynamic relocations.  */
2634   int no_apply_dynamic_relocs;
2635
2636   /* The number of bytes in the initial entry in the PLT.  */
2637   bfd_size_type plt_header_size;
2638
2639   /* The bytes of the initial PLT entry.  */
2640   const bfd_byte *plt0_entry;
2641
2642   /* The number of bytes in the subsequent PLT entries.  */
2643   bfd_size_type plt_entry_size;
2644
2645   /* The bytes of the subsequent PLT entry.  */
2646   const bfd_byte *plt_entry;
2647
2648   /* Small local sym cache.  */
2649   struct sym_cache sym_cache;
2650
2651   /* For convenience in allocate_dynrelocs.  */
2652   bfd *obfd;
2653
2654   /* The amount of space used by the reserved portion of the sgotplt
2655      section, plus whatever space is used by the jump slots.  */
2656   bfd_vma sgotplt_jump_table_size;
2657
2658   /* The stub hash table.  */
2659   struct bfd_hash_table stub_hash_table;
2660
2661   /* Linker stub bfd.  */
2662   bfd *stub_bfd;
2663
2664   /* Linker call-backs.  */
2665   asection *(*add_stub_section) (const char *, asection *);
2666   void (*layout_sections_again) (void);
2667
2668   /* Array to keep track of which stub sections have been created, and
2669      information on stub grouping.  */
2670   struct map_stub
2671   {
2672     /* This is the section to which stubs in the group will be
2673        attached.  */
2674     asection *link_sec;
2675     /* The stub section.  */
2676     asection *stub_sec;
2677   } *stub_group;
2678
2679   /* Assorted information used by elfNN_aarch64_size_stubs.  */
2680   unsigned int bfd_count;
2681   unsigned int top_index;
2682   asection **input_list;
2683
2684   /* JUMP_SLOT relocs for variant PCS symbols may be present.  */
2685   int variant_pcs;
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 /* Merge non-visibility st_other attributes.  */
2902
2903 static void
2904 elfNN_aarch64_merge_symbol_attribute (struct elf_link_hash_entry *h,
2905                                       const Elf_Internal_Sym *isym,
2906                                       bfd_boolean definition ATTRIBUTE_UNUSED,
2907                                       bfd_boolean dynamic ATTRIBUTE_UNUSED)
2908 {
2909   unsigned int isym_sto = isym->st_other & ~ELF_ST_VISIBILITY (-1);
2910   unsigned int h_sto = h->other & ~ELF_ST_VISIBILITY (-1);
2911
2912   if (isym_sto == h_sto)
2913     return;
2914
2915   if (isym_sto & ~STO_AARCH64_VARIANT_PCS)
2916     /* Not fatal, this callback cannot fail.  */
2917     _bfd_error_handler (_("unknown attribute for symbol `%s': 0x%02x"),
2918                         h->root.root.string, isym_sto);
2919
2920   /* Note: Ideally we would warn about any attribute mismatch, but
2921      this api does not allow that without substantial changes.  */
2922   if (isym_sto & STO_AARCH64_VARIANT_PCS)
2923     h->other |= STO_AARCH64_VARIANT_PCS;
2924 }
2925
2926 /* Destroy an AArch64 elf linker hash table.  */
2927
2928 static void
2929 elfNN_aarch64_link_hash_table_free (bfd *obfd)
2930 {
2931   struct elf_aarch64_link_hash_table *ret
2932     = (struct elf_aarch64_link_hash_table *) obfd->link.hash;
2933
2934   if (ret->loc_hash_table)
2935     htab_delete (ret->loc_hash_table);
2936   if (ret->loc_hash_memory)
2937     objalloc_free ((struct objalloc *) ret->loc_hash_memory);
2938
2939   bfd_hash_table_free (&ret->stub_hash_table);
2940   _bfd_elf_link_hash_table_free (obfd);
2941 }
2942
2943 /* Create an AArch64 elf linker hash table.  */
2944
2945 static struct bfd_link_hash_table *
2946 elfNN_aarch64_link_hash_table_create (bfd *abfd)
2947 {
2948   struct elf_aarch64_link_hash_table *ret;
2949   bfd_size_type amt = sizeof (struct elf_aarch64_link_hash_table);
2950
2951   ret = bfd_zmalloc (amt);
2952   if (ret == NULL)
2953     return NULL;
2954
2955   if (!_bfd_elf_link_hash_table_init
2956       (&ret->root, abfd, elfNN_aarch64_link_hash_newfunc,
2957        sizeof (struct elf_aarch64_link_hash_entry), AARCH64_ELF_DATA))
2958     {
2959       free (ret);
2960       return NULL;
2961     }
2962
2963   ret->plt_header_size = PLT_ENTRY_SIZE;
2964   ret->plt0_entry = elfNN_aarch64_small_plt0_entry;
2965   ret->plt_entry_size = PLT_SMALL_ENTRY_SIZE;
2966   ret->plt_entry = elfNN_aarch64_small_plt_entry;
2967   ret->tlsdesc_plt_entry_size = PLT_TLSDESC_ENTRY_SIZE;
2968   ret->obfd = abfd;
2969   ret->dt_tlsdesc_got = (bfd_vma) - 1;
2970
2971   if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
2972                             sizeof (struct elf_aarch64_stub_hash_entry)))
2973     {
2974       _bfd_elf_link_hash_table_free (abfd);
2975       return NULL;
2976     }
2977
2978   ret->loc_hash_table = htab_try_create (1024,
2979                                          elfNN_aarch64_local_htab_hash,
2980                                          elfNN_aarch64_local_htab_eq,
2981                                          NULL);
2982   ret->loc_hash_memory = objalloc_create ();
2983   if (!ret->loc_hash_table || !ret->loc_hash_memory)
2984     {
2985       elfNN_aarch64_link_hash_table_free (abfd);
2986       return NULL;
2987     }
2988   ret->root.root.hash_table_free = elfNN_aarch64_link_hash_table_free;
2989
2990   return &ret->root.root;
2991 }
2992
2993 /* Perform relocation R_TYPE.  Returns TRUE upon success, FALSE otherwise.  */
2994
2995 static bfd_boolean
2996 aarch64_relocate (unsigned int r_type, bfd *input_bfd, asection *input_section,
2997                   bfd_vma offset, bfd_vma value)
2998 {
2999   reloc_howto_type *howto;
3000   bfd_vma place;
3001
3002   howto = elfNN_aarch64_howto_from_type (input_bfd, r_type);
3003   place = (input_section->output_section->vma + input_section->output_offset
3004            + offset);
3005
3006   r_type = elfNN_aarch64_bfd_reloc_from_type (input_bfd, r_type);
3007   value = _bfd_aarch64_elf_resolve_relocation (r_type, place, value, 0, FALSE);
3008   return _bfd_aarch64_elf_put_addend (input_bfd,
3009                                       input_section->contents + offset, r_type,
3010                                       howto, value) == bfd_reloc_ok;
3011 }
3012
3013 static enum elf_aarch64_stub_type
3014 aarch64_select_branch_stub (bfd_vma value, bfd_vma place)
3015 {
3016   if (aarch64_valid_for_adrp_p (value, place))
3017     return aarch64_stub_adrp_branch;
3018   return aarch64_stub_long_branch;
3019 }
3020
3021 /* Determine the type of stub needed, if any, for a call.  */
3022
3023 static enum elf_aarch64_stub_type
3024 aarch64_type_of_stub (asection *input_sec,
3025                       const Elf_Internal_Rela *rel,
3026                       asection *sym_sec,
3027                       unsigned char st_type,
3028                       bfd_vma destination)
3029 {
3030   bfd_vma location;
3031   bfd_signed_vma branch_offset;
3032   unsigned int r_type;
3033   enum elf_aarch64_stub_type stub_type = aarch64_stub_none;
3034
3035   if (st_type != STT_FUNC
3036       && (sym_sec == input_sec))
3037     return stub_type;
3038
3039   /* Determine where the call point is.  */
3040   location = (input_sec->output_offset
3041               + input_sec->output_section->vma + rel->r_offset);
3042
3043   branch_offset = (bfd_signed_vma) (destination - location);
3044
3045   r_type = ELFNN_R_TYPE (rel->r_info);
3046
3047   /* We don't want to redirect any old unconditional jump in this way,
3048      only one which is being used for a sibcall, where it is
3049      acceptable for the IP0 and IP1 registers to be clobbered.  */
3050   if ((r_type == AARCH64_R (CALL26) || r_type == AARCH64_R (JUMP26))
3051       && (branch_offset > AARCH64_MAX_FWD_BRANCH_OFFSET
3052           || branch_offset < AARCH64_MAX_BWD_BRANCH_OFFSET))
3053     {
3054       stub_type = aarch64_stub_long_branch;
3055     }
3056
3057   return stub_type;
3058 }
3059
3060 /* Build a name for an entry in the stub hash table.  */
3061
3062 static char *
3063 elfNN_aarch64_stub_name (const asection *input_section,
3064                          const asection *sym_sec,
3065                          const struct elf_aarch64_link_hash_entry *hash,
3066                          const Elf_Internal_Rela *rel)
3067 {
3068   char *stub_name;
3069   bfd_size_type len;
3070
3071   if (hash)
3072     {
3073       len = 8 + 1 + strlen (hash->root.root.root.string) + 1 + 16 + 1;
3074       stub_name = bfd_malloc (len);
3075       if (stub_name != NULL)
3076         snprintf (stub_name, len, "%08x_%s+%" BFD_VMA_FMT "x",
3077                   (unsigned int) input_section->id,
3078                   hash->root.root.root.string,
3079                   rel->r_addend);
3080     }
3081   else
3082     {
3083       len = 8 + 1 + 8 + 1 + 8 + 1 + 16 + 1;
3084       stub_name = bfd_malloc (len);
3085       if (stub_name != NULL)
3086         snprintf (stub_name, len, "%08x_%x:%x+%" BFD_VMA_FMT "x",
3087                   (unsigned int) input_section->id,
3088                   (unsigned int) sym_sec->id,
3089                   (unsigned int) ELFNN_R_SYM (rel->r_info),
3090                   rel->r_addend);
3091     }
3092
3093   return stub_name;
3094 }
3095
3096 /* Return TRUE if symbol H should be hashed in the `.gnu.hash' section.  For
3097    executable PLT slots where the executable never takes the address of those
3098    functions, the function symbols are not added to the hash table.  */
3099
3100 static bfd_boolean
3101 elf_aarch64_hash_symbol (struct elf_link_hash_entry *h)
3102 {
3103   if (h->plt.offset != (bfd_vma) -1
3104       && !h->def_regular
3105       && !h->pointer_equality_needed)
3106     return FALSE;
3107
3108   return _bfd_elf_hash_symbol (h);
3109 }
3110
3111
3112 /* Look up an entry in the stub hash.  Stub entries are cached because
3113    creating the stub name takes a bit of time.  */
3114
3115 static struct elf_aarch64_stub_hash_entry *
3116 elfNN_aarch64_get_stub_entry (const asection *input_section,
3117                               const asection *sym_sec,
3118                               struct elf_link_hash_entry *hash,
3119                               const Elf_Internal_Rela *rel,
3120                               struct elf_aarch64_link_hash_table *htab)
3121 {
3122   struct elf_aarch64_stub_hash_entry *stub_entry;
3123   struct elf_aarch64_link_hash_entry *h =
3124     (struct elf_aarch64_link_hash_entry *) hash;
3125   const asection *id_sec;
3126
3127   if ((input_section->flags & SEC_CODE) == 0)
3128     return NULL;
3129
3130   /* If this input section is part of a group of sections sharing one
3131      stub section, then use the id of the first section in the group.
3132      Stub names need to include a section id, as there may well be
3133      more than one stub used to reach say, printf, and we need to
3134      distinguish between them.  */
3135   id_sec = htab->stub_group[input_section->id].link_sec;
3136
3137   if (h != NULL && h->stub_cache != NULL
3138       && h->stub_cache->h == h && h->stub_cache->id_sec == id_sec)
3139     {
3140       stub_entry = h->stub_cache;
3141     }
3142   else
3143     {
3144       char *stub_name;
3145
3146       stub_name = elfNN_aarch64_stub_name (id_sec, sym_sec, h, rel);
3147       if (stub_name == NULL)
3148         return NULL;
3149
3150       stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table,
3151                                              stub_name, FALSE, FALSE);
3152       if (h != NULL)
3153         h->stub_cache = stub_entry;
3154
3155       free (stub_name);
3156     }
3157
3158   return stub_entry;
3159 }
3160
3161
3162 /* Create a stub section.  */
3163
3164 static asection *
3165 _bfd_aarch64_create_stub_section (asection *section,
3166                                   struct elf_aarch64_link_hash_table *htab)
3167 {
3168   size_t namelen;
3169   bfd_size_type len;
3170   char *s_name;
3171
3172   namelen = strlen (section->name);
3173   len = namelen + sizeof (STUB_SUFFIX);
3174   s_name = bfd_alloc (htab->stub_bfd, len);
3175   if (s_name == NULL)
3176     return NULL;
3177
3178   memcpy (s_name, section->name, namelen);
3179   memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3180   return (*htab->add_stub_section) (s_name, section);
3181 }
3182
3183
3184 /* Find or create a stub section for a link section.
3185
3186    Fix or create the stub section used to collect stubs attached to
3187    the specified link section.  */
3188
3189 static asection *
3190 _bfd_aarch64_get_stub_for_link_section (asection *link_section,
3191                                         struct elf_aarch64_link_hash_table *htab)
3192 {
3193   if (htab->stub_group[link_section->id].stub_sec == NULL)
3194     htab->stub_group[link_section->id].stub_sec
3195       = _bfd_aarch64_create_stub_section (link_section, htab);
3196   return htab->stub_group[link_section->id].stub_sec;
3197 }
3198
3199
3200 /* Find or create a stub section in the stub group for an input
3201    section.  */
3202
3203 static asection *
3204 _bfd_aarch64_create_or_find_stub_sec (asection *section,
3205                                       struct elf_aarch64_link_hash_table *htab)
3206 {
3207   asection *link_sec = htab->stub_group[section->id].link_sec;
3208   return _bfd_aarch64_get_stub_for_link_section (link_sec, htab);
3209 }
3210
3211
3212 /* Add a new stub entry in the stub group associated with an input
3213    section to the stub hash.  Not all fields of the new stub entry are
3214    initialised.  */
3215
3216 static struct elf_aarch64_stub_hash_entry *
3217 _bfd_aarch64_add_stub_entry_in_group (const char *stub_name,
3218                                       asection *section,
3219                                       struct elf_aarch64_link_hash_table *htab)
3220 {
3221   asection *link_sec;
3222   asection *stub_sec;
3223   struct elf_aarch64_stub_hash_entry *stub_entry;
3224
3225   link_sec = htab->stub_group[section->id].link_sec;
3226   stub_sec = _bfd_aarch64_create_or_find_stub_sec (section, htab);
3227
3228   /* Enter this entry into the linker stub hash table.  */
3229   stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3230                                          TRUE, FALSE);
3231   if (stub_entry == NULL)
3232     {
3233       /* xgettext:c-format */
3234       _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3235                           section->owner, stub_name);
3236       return NULL;
3237     }
3238
3239   stub_entry->stub_sec = stub_sec;
3240   stub_entry->stub_offset = 0;
3241   stub_entry->id_sec = link_sec;
3242
3243   return stub_entry;
3244 }
3245
3246 /* Add a new stub entry in the final stub section to the stub hash.
3247    Not all fields of the new stub entry are initialised.  */
3248
3249 static struct elf_aarch64_stub_hash_entry *
3250 _bfd_aarch64_add_stub_entry_after (const char *stub_name,
3251                                    asection *link_section,
3252                                    struct elf_aarch64_link_hash_table *htab)
3253 {
3254   asection *stub_sec;
3255   struct elf_aarch64_stub_hash_entry *stub_entry;
3256
3257   stub_sec = NULL;
3258   /* Only create the actual stub if we will end up needing it.  */
3259   if (htab->fix_erratum_843419 & ERRAT_ADRP)
3260     stub_sec = _bfd_aarch64_get_stub_for_link_section (link_section, htab);
3261   stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3262                                          TRUE, FALSE);
3263   if (stub_entry == NULL)
3264     {
3265       _bfd_error_handler (_("cannot create stub entry %s"), stub_name);
3266       return NULL;
3267     }
3268
3269   stub_entry->stub_sec = stub_sec;
3270   stub_entry->stub_offset = 0;
3271   stub_entry->id_sec = link_section;
3272
3273   return stub_entry;
3274 }
3275
3276
3277 static bfd_boolean
3278 aarch64_build_one_stub (struct bfd_hash_entry *gen_entry,
3279                         void *in_arg ATTRIBUTE_UNUSED)
3280 {
3281   struct elf_aarch64_stub_hash_entry *stub_entry;
3282   asection *stub_sec;
3283   bfd *stub_bfd;
3284   bfd_byte *loc;
3285   bfd_vma sym_value;
3286   bfd_vma veneered_insn_loc;
3287   bfd_vma veneer_entry_loc;
3288   bfd_signed_vma branch_offset = 0;
3289   unsigned int template_size;
3290   const uint32_t *template;
3291   unsigned int i;
3292
3293   /* Massage our args to the form they really have.  */
3294   stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
3295
3296   stub_sec = stub_entry->stub_sec;
3297
3298   /* Make a note of the offset within the stubs for this entry.  */
3299   stub_entry->stub_offset = stub_sec->size;
3300   loc = stub_sec->contents + stub_entry->stub_offset;
3301
3302   stub_bfd = stub_sec->owner;
3303
3304   /* This is the address of the stub destination.  */
3305   sym_value = (stub_entry->target_value
3306                + stub_entry->target_section->output_offset
3307                + stub_entry->target_section->output_section->vma);
3308
3309   if (stub_entry->stub_type == aarch64_stub_long_branch)
3310     {
3311       bfd_vma place = (stub_entry->stub_offset + stub_sec->output_section->vma
3312                        + stub_sec->output_offset);
3313
3314       /* See if we can relax the stub.  */
3315       if (aarch64_valid_for_adrp_p (sym_value, place))
3316         stub_entry->stub_type = aarch64_select_branch_stub (sym_value, place);
3317     }
3318
3319   switch (stub_entry->stub_type)
3320     {
3321     case aarch64_stub_adrp_branch:
3322       template = aarch64_adrp_branch_stub;
3323       template_size = sizeof (aarch64_adrp_branch_stub);
3324       break;
3325     case aarch64_stub_long_branch:
3326       template = aarch64_long_branch_stub;
3327       template_size = sizeof (aarch64_long_branch_stub);
3328       break;
3329     case aarch64_stub_erratum_835769_veneer:
3330       template = aarch64_erratum_835769_stub;
3331       template_size = sizeof (aarch64_erratum_835769_stub);
3332       break;
3333     case aarch64_stub_erratum_843419_veneer:
3334       template = aarch64_erratum_843419_stub;
3335       template_size = sizeof (aarch64_erratum_843419_stub);
3336       break;
3337     default:
3338       abort ();
3339     }
3340
3341   for (i = 0; i < (template_size / sizeof template[0]); i++)
3342     {
3343       bfd_putl32 (template[i], loc);
3344       loc += 4;
3345     }
3346
3347   template_size = (template_size + 7) & ~7;
3348   stub_sec->size += template_size;
3349
3350   switch (stub_entry->stub_type)
3351     {
3352     case aarch64_stub_adrp_branch:
3353       if (!aarch64_relocate (AARCH64_R (ADR_PREL_PG_HI21), stub_bfd, stub_sec,
3354                              stub_entry->stub_offset, sym_value))
3355         /* The stub would not have been relaxed if the offset was out
3356            of range.  */
3357         BFD_FAIL ();
3358
3359       if (!aarch64_relocate (AARCH64_R (ADD_ABS_LO12_NC), stub_bfd, stub_sec,
3360                              stub_entry->stub_offset + 4, sym_value))
3361         BFD_FAIL ();
3362       break;
3363
3364     case aarch64_stub_long_branch:
3365       /* We want the value relative to the address 12 bytes back from the
3366          value itself.  */
3367       if (!aarch64_relocate (AARCH64_R (PRELNN), stub_bfd, stub_sec,
3368                              stub_entry->stub_offset + 16, sym_value + 12))
3369         BFD_FAIL ();
3370       break;
3371
3372     case aarch64_stub_erratum_835769_veneer:
3373       veneered_insn_loc = stub_entry->target_section->output_section->vma
3374                           + stub_entry->target_section->output_offset
3375                           + stub_entry->target_value;
3376       veneer_entry_loc = stub_entry->stub_sec->output_section->vma
3377                           + stub_entry->stub_sec->output_offset
3378                           + stub_entry->stub_offset;
3379       branch_offset = veneered_insn_loc - veneer_entry_loc;
3380       branch_offset >>= 2;
3381       branch_offset &= 0x3ffffff;
3382       bfd_putl32 (stub_entry->veneered_insn,
3383                   stub_sec->contents + stub_entry->stub_offset);
3384       bfd_putl32 (template[1] | branch_offset,
3385                   stub_sec->contents + stub_entry->stub_offset + 4);
3386       break;
3387
3388     case aarch64_stub_erratum_843419_veneer:
3389       if (!aarch64_relocate (AARCH64_R (JUMP26), stub_bfd, stub_sec,
3390                              stub_entry->stub_offset + 4, sym_value + 4))
3391         BFD_FAIL ();
3392       break;
3393
3394     default:
3395       abort ();
3396     }
3397
3398   return TRUE;
3399 }
3400
3401 /* As above, but don't actually build the stub.  Just bump offset so
3402    we know stub section sizes.  */
3403
3404 static bfd_boolean
3405 aarch64_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
3406 {
3407   struct elf_aarch64_stub_hash_entry *stub_entry;
3408   struct elf_aarch64_link_hash_table *htab;
3409   int size;
3410
3411   /* Massage our args to the form they really have.  */
3412   stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
3413   htab = (struct elf_aarch64_link_hash_table *) in_arg;
3414
3415   switch (stub_entry->stub_type)
3416     {
3417     case aarch64_stub_adrp_branch:
3418       size = sizeof (aarch64_adrp_branch_stub);
3419       break;
3420     case aarch64_stub_long_branch:
3421       size = sizeof (aarch64_long_branch_stub);
3422       break;
3423     case aarch64_stub_erratum_835769_veneer:
3424       size = sizeof (aarch64_erratum_835769_stub);
3425       break;
3426     case aarch64_stub_erratum_843419_veneer:
3427       {
3428         if (htab->fix_erratum_843419 == ERRAT_ADR)
3429           return TRUE;
3430         size = sizeof (aarch64_erratum_843419_stub);
3431       }
3432       break;
3433     default:
3434       abort ();
3435     }
3436
3437   size = (size + 7) & ~7;
3438   stub_entry->stub_sec->size += size;
3439   return TRUE;
3440 }
3441
3442 /* External entry points for sizing and building linker stubs.  */
3443
3444 /* Set up various things so that we can make a list of input sections
3445    for each output section included in the link.  Returns -1 on error,
3446    0 when no stubs will be needed, and 1 on success.  */
3447
3448 int
3449 elfNN_aarch64_setup_section_lists (bfd *output_bfd,
3450                                    struct bfd_link_info *info)
3451 {
3452   bfd *input_bfd;
3453   unsigned int bfd_count;
3454   unsigned int top_id, top_index;
3455   asection *section;
3456   asection **input_list, **list;
3457   bfd_size_type amt;
3458   struct elf_aarch64_link_hash_table *htab =
3459     elf_aarch64_hash_table (info);
3460
3461   if (!is_elf_hash_table (htab))
3462     return 0;
3463
3464   /* Count the number of input BFDs and find the top input section id.  */
3465   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
3466        input_bfd != NULL; input_bfd = input_bfd->link.next)
3467     {
3468       bfd_count += 1;
3469       for (section = input_bfd->sections;
3470            section != NULL; section = section->next)
3471         {
3472           if (top_id < section->id)
3473             top_id = section->id;
3474         }
3475     }
3476   htab->bfd_count = bfd_count;
3477
3478   amt = sizeof (struct map_stub) * (top_id + 1);
3479   htab->stub_group = bfd_zmalloc (amt);
3480   if (htab->stub_group == NULL)
3481     return -1;
3482
3483   /* We can't use output_bfd->section_count here to find the top output
3484      section index as some sections may have been removed, and
3485      _bfd_strip_section_from_output doesn't renumber the indices.  */
3486   for (section = output_bfd->sections, top_index = 0;
3487        section != NULL; section = section->next)
3488     {
3489       if (top_index < section->index)
3490         top_index = section->index;
3491     }
3492
3493   htab->top_index = top_index;
3494   amt = sizeof (asection *) * (top_index + 1);
3495   input_list = bfd_malloc (amt);
3496   htab->input_list = input_list;
3497   if (input_list == NULL)
3498     return -1;
3499
3500   /* For sections we aren't interested in, mark their entries with a
3501      value we can check later.  */
3502   list = input_list + top_index;
3503   do
3504     *list = bfd_abs_section_ptr;
3505   while (list-- != input_list);
3506
3507   for (section = output_bfd->sections;
3508        section != NULL; section = section->next)
3509     {
3510       if ((section->flags & SEC_CODE) != 0)
3511         input_list[section->index] = NULL;
3512     }
3513
3514   return 1;
3515 }
3516
3517 /* Used by elfNN_aarch64_next_input_section and group_sections.  */
3518 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3519
3520 /* The linker repeatedly calls this function for each input section,
3521    in the order that input sections are linked into output sections.
3522    Build lists of input sections to determine groupings between which
3523    we may insert linker stubs.  */
3524
3525 void
3526 elfNN_aarch64_next_input_section (struct bfd_link_info *info, asection *isec)
3527 {
3528   struct elf_aarch64_link_hash_table *htab =
3529     elf_aarch64_hash_table (info);
3530
3531   if (isec->output_section->index <= htab->top_index)
3532     {
3533       asection **list = htab->input_list + isec->output_section->index;
3534
3535       if (*list != bfd_abs_section_ptr)
3536         {
3537           /* Steal the link_sec pointer for our list.  */
3538           /* This happens to make the list in reverse order,
3539              which is what we want.  */
3540           PREV_SEC (isec) = *list;
3541           *list = isec;
3542         }
3543     }
3544 }
3545
3546 /* See whether we can group stub sections together.  Grouping stub
3547    sections may result in fewer stubs.  More importantly, we need to
3548    put all .init* and .fini* stubs at the beginning of the .init or
3549    .fini output sections respectively, because glibc splits the
3550    _init and _fini functions into multiple parts.  Putting a stub in
3551    the middle of a function is not a good idea.  */
3552
3553 static void
3554 group_sections (struct elf_aarch64_link_hash_table *htab,
3555                 bfd_size_type stub_group_size,
3556                 bfd_boolean stubs_always_before_branch)
3557 {
3558   asection **list = htab->input_list + htab->top_index;
3559
3560   do
3561     {
3562       asection *tail = *list;
3563
3564       if (tail == bfd_abs_section_ptr)
3565         continue;
3566
3567       while (tail != NULL)
3568         {
3569           asection *curr;
3570           asection *prev;
3571           bfd_size_type total;
3572
3573           curr = tail;
3574           total = tail->size;
3575           while ((prev = PREV_SEC (curr)) != NULL
3576                  && ((total += curr->output_offset - prev->output_offset)
3577                      < stub_group_size))
3578             curr = prev;
3579
3580           /* OK, the size from the start of CURR to the end is less
3581              than stub_group_size and thus can be handled by one stub
3582              section.  (Or the tail section is itself larger than
3583              stub_group_size, in which case we may be toast.)
3584              We should really be keeping track of the total size of
3585              stubs added here, as stubs contribute to the final output
3586              section size.  */
3587           do
3588             {
3589               prev = PREV_SEC (tail);
3590               /* Set up this stub group.  */
3591               htab->stub_group[tail->id].link_sec = curr;
3592             }
3593           while (tail != curr && (tail = prev) != NULL);
3594
3595           /* But wait, there's more!  Input sections up to stub_group_size
3596              bytes before the stub section can be handled by it too.  */
3597           if (!stubs_always_before_branch)
3598             {
3599               total = 0;
3600               while (prev != NULL
3601                      && ((total += tail->output_offset - prev->output_offset)
3602                          < stub_group_size))
3603                 {
3604                   tail = prev;
3605                   prev = PREV_SEC (tail);
3606                   htab->stub_group[tail->id].link_sec = curr;
3607                 }
3608             }
3609           tail = prev;
3610         }
3611     }
3612   while (list-- != htab->input_list);
3613
3614   free (htab->input_list);
3615 }
3616
3617 #undef PREV_SEC
3618
3619 #define AARCH64_BITS(x, pos, n) (((x) >> (pos)) & ((1 << (n)) - 1))
3620
3621 #define AARCH64_RT(insn) AARCH64_BITS (insn, 0, 5)
3622 #define AARCH64_RT2(insn) AARCH64_BITS (insn, 10, 5)
3623 #define AARCH64_RA(insn) AARCH64_BITS (insn, 10, 5)
3624 #define AARCH64_RD(insn) AARCH64_BITS (insn, 0, 5)
3625 #define AARCH64_RN(insn) AARCH64_BITS (insn, 5, 5)
3626 #define AARCH64_RM(insn) AARCH64_BITS (insn, 16, 5)
3627
3628 #define AARCH64_MAC(insn) (((insn) & 0xff000000) == 0x9b000000)
3629 #define AARCH64_BIT(insn, n) AARCH64_BITS (insn, n, 1)
3630 #define AARCH64_OP31(insn) AARCH64_BITS (insn, 21, 3)
3631 #define AARCH64_ZR 0x1f
3632
3633 /* All ld/st ops.  See C4-182 of the ARM ARM.  The encoding space for
3634    LD_PCREL, LDST_RO, LDST_UI and LDST_UIMM cover prefetch ops.  */
3635
3636 #define AARCH64_LD(insn) (AARCH64_BIT (insn, 22) == 1)
3637 #define AARCH64_LDST(insn) (((insn) & 0x0a000000) == 0x08000000)
3638 #define AARCH64_LDST_EX(insn) (((insn) & 0x3f000000) == 0x08000000)
3639 #define AARCH64_LDST_PCREL(insn) (((insn) & 0x3b000000) == 0x18000000)
3640 #define AARCH64_LDST_NAP(insn) (((insn) & 0x3b800000) == 0x28000000)
3641 #define AARCH64_LDSTP_PI(insn) (((insn) & 0x3b800000) == 0x28800000)
3642 #define AARCH64_LDSTP_O(insn) (((insn) & 0x3b800000) == 0x29000000)
3643 #define AARCH64_LDSTP_PRE(insn) (((insn) & 0x3b800000) == 0x29800000)
3644 #define AARCH64_LDST_UI(insn) (((insn) & 0x3b200c00) == 0x38000000)
3645 #define AARCH64_LDST_PIIMM(insn) (((insn) & 0x3b200c00) == 0x38000400)
3646 #define AARCH64_LDST_U(insn) (((insn) & 0x3b200c00) == 0x38000800)
3647 #define AARCH64_LDST_PREIMM(insn) (((insn) & 0x3b200c00) == 0x38000c00)
3648 #define AARCH64_LDST_RO(insn) (((insn) & 0x3b200c00) == 0x38200800)
3649 #define AARCH64_LDST_UIMM(insn) (((insn) & 0x3b000000) == 0x39000000)
3650 #define AARCH64_LDST_SIMD_M(insn) (((insn) & 0xbfbf0000) == 0x0c000000)
3651 #define AARCH64_LDST_SIMD_M_PI(insn) (((insn) & 0xbfa00000) == 0x0c800000)
3652 #define AARCH64_LDST_SIMD_S(insn) (((insn) & 0xbf9f0000) == 0x0d000000)
3653 #define AARCH64_LDST_SIMD_S_PI(insn) (((insn) & 0xbf800000) == 0x0d800000)
3654
3655 /* Classify an INSN if it is indeed a load/store.
3656
3657    Return TRUE if INSN is a LD/ST instruction otherwise return FALSE.
3658
3659    For scalar LD/ST instructions PAIR is FALSE, RT is returned and RT2
3660    is set equal to RT.
3661
3662    For LD/ST pair instructions PAIR is TRUE, RT and RT2 are returned.  */
3663
3664 static bfd_boolean
3665 aarch64_mem_op_p (uint32_t insn, unsigned int *rt, unsigned int *rt2,
3666                   bfd_boolean *pair, bfd_boolean *load)
3667 {
3668   uint32_t opcode;
3669   unsigned int r;
3670   uint32_t opc = 0;
3671   uint32_t v = 0;
3672   uint32_t opc_v = 0;
3673
3674   /* Bail out quickly if INSN doesn't fall into the load-store
3675      encoding space.  */
3676   if (!AARCH64_LDST (insn))
3677     return FALSE;
3678
3679   *pair = FALSE;
3680   *load = FALSE;
3681   if (AARCH64_LDST_EX (insn))
3682     {
3683       *rt = AARCH64_RT (insn);
3684       *rt2 = *rt;
3685       if (AARCH64_BIT (insn, 21) == 1)
3686         {
3687           *pair = TRUE;
3688           *rt2 = AARCH64_RT2 (insn);
3689         }
3690       *load = AARCH64_LD (insn);
3691       return TRUE;
3692     }
3693   else if (AARCH64_LDST_NAP (insn)
3694            || AARCH64_LDSTP_PI (insn)
3695            || AARCH64_LDSTP_O (insn)
3696            || AARCH64_LDSTP_PRE (insn))
3697     {
3698       *pair = TRUE;
3699       *rt = AARCH64_RT (insn);
3700       *rt2 = AARCH64_RT2 (insn);
3701       *load = AARCH64_LD (insn);
3702       return TRUE;
3703     }
3704   else if (AARCH64_LDST_PCREL (insn)
3705            || AARCH64_LDST_UI (insn)
3706            || AARCH64_LDST_PIIMM (insn)
3707            || AARCH64_LDST_U (insn)
3708            || AARCH64_LDST_PREIMM (insn)
3709            || AARCH64_LDST_RO (insn)
3710            || AARCH64_LDST_UIMM (insn))
3711    {
3712       *rt = AARCH64_RT (insn);
3713       *rt2 = *rt;
3714       if (AARCH64_LDST_PCREL (insn))
3715         *load = TRUE;
3716       opc = AARCH64_BITS (insn, 22, 2);
3717       v = AARCH64_BIT (insn, 26);
3718       opc_v = opc | (v << 2);
3719       *load =  (opc_v == 1 || opc_v == 2 || opc_v == 3
3720                 || opc_v == 5 || opc_v == 7);
3721       return TRUE;
3722    }
3723   else if (AARCH64_LDST_SIMD_M (insn)
3724            || AARCH64_LDST_SIMD_M_PI (insn))
3725     {
3726       *rt = AARCH64_RT (insn);
3727       *load = AARCH64_BIT (insn, 22);
3728       opcode = (insn >> 12) & 0xf;
3729       switch (opcode)
3730         {
3731         case 0:
3732         case 2:
3733           *rt2 = *rt + 3;
3734           break;
3735
3736         case 4:
3737         case 6:
3738           *rt2 = *rt + 2;
3739           break;
3740
3741         case 7:
3742           *rt2 = *rt;
3743           break;
3744
3745         case 8:
3746         case 10:
3747           *rt2 = *rt + 1;
3748           break;
3749
3750         default:
3751           return FALSE;
3752         }
3753       return TRUE;
3754     }
3755   else if (AARCH64_LDST_SIMD_S (insn)
3756            || AARCH64_LDST_SIMD_S_PI (insn))
3757     {
3758       *rt = AARCH64_RT (insn);
3759       r = (insn >> 21) & 1;
3760       *load = AARCH64_BIT (insn, 22);
3761       opcode = (insn >> 13) & 0x7;
3762       switch (opcode)
3763         {
3764         case 0:
3765         case 2:
3766         case 4:
3767           *rt2 = *rt + r;
3768           break;
3769
3770         case 1:
3771         case 3:
3772         case 5:
3773           *rt2 = *rt + (r == 0 ? 2 : 3);
3774           break;
3775
3776         case 6:
3777           *rt2 = *rt + r;
3778           break;
3779
3780         case 7:
3781           *rt2 = *rt + (r == 0 ? 2 : 3);
3782           break;
3783
3784         default:
3785           return FALSE;
3786         }
3787       return TRUE;
3788     }
3789
3790   return FALSE;
3791 }
3792
3793 /* Return TRUE if INSN is multiply-accumulate.  */
3794
3795 static bfd_boolean
3796 aarch64_mlxl_p (uint32_t insn)
3797 {
3798   uint32_t op31 = AARCH64_OP31 (insn);
3799
3800   if (AARCH64_MAC (insn)
3801       && (op31 == 0 || op31 == 1 || op31 == 5)
3802       /* Exclude MUL instructions which are encoded as a multiple accumulate
3803          with RA = XZR.  */
3804       && AARCH64_RA (insn) != AARCH64_ZR)
3805     return TRUE;
3806
3807   return FALSE;
3808 }
3809
3810 /* Some early revisions of the Cortex-A53 have an erratum (835769) whereby
3811    it is possible for a 64-bit multiply-accumulate instruction to generate an
3812    incorrect result.  The details are quite complex and hard to
3813    determine statically, since branches in the code may exist in some
3814    circumstances, but all cases end with a memory (load, store, or
3815    prefetch) instruction followed immediately by the multiply-accumulate
3816    operation.  We employ a linker patching technique, by moving the potentially
3817    affected multiply-accumulate instruction into a patch region and replacing
3818    the original instruction with a branch to the patch.  This function checks
3819    if INSN_1 is the memory operation followed by a multiply-accumulate
3820    operation (INSN_2).  Return TRUE if an erratum sequence is found, FALSE
3821    if INSN_1 and INSN_2 are safe.  */
3822
3823 static bfd_boolean
3824 aarch64_erratum_sequence (uint32_t insn_1, uint32_t insn_2)
3825 {
3826   uint32_t rt;
3827   uint32_t rt2;
3828   uint32_t rn;
3829   uint32_t rm;
3830   uint32_t ra;
3831   bfd_boolean pair;
3832   bfd_boolean load;
3833
3834   if (aarch64_mlxl_p (insn_2)
3835       && aarch64_mem_op_p (insn_1, &rt, &rt2, &pair, &load))
3836     {
3837       /* Any SIMD memory op is independent of the subsequent MLA
3838          by definition of the erratum.  */
3839       if (AARCH64_BIT (insn_1, 26))
3840         return TRUE;
3841
3842       /* If not SIMD, check for integer memory ops and MLA relationship.  */
3843       rn = AARCH64_RN (insn_2);
3844       ra = AARCH64_RA (insn_2);
3845       rm = AARCH64_RM (insn_2);
3846
3847       /* If this is a load and there's a true(RAW) dependency, we are safe
3848          and this is not an erratum sequence.  */
3849       if (load &&
3850           (rt == rn || rt == rm || rt == ra
3851            || (pair && (rt2 == rn || rt2 == rm || rt2 == ra))))
3852         return FALSE;
3853
3854       /* We conservatively put out stubs for all other cases (including
3855          writebacks).  */
3856       return TRUE;
3857     }
3858
3859   return FALSE;
3860 }
3861
3862 /* Used to order a list of mapping symbols by address.  */
3863
3864 static int
3865 elf_aarch64_compare_mapping (const void *a, const void *b)
3866 {
3867   const elf_aarch64_section_map *amap = (const elf_aarch64_section_map *) a;
3868   const elf_aarch64_section_map *bmap = (const elf_aarch64_section_map *) b;
3869
3870   if (amap->vma > bmap->vma)
3871     return 1;
3872   else if (amap->vma < bmap->vma)
3873     return -1;
3874   else if (amap->type > bmap->type)
3875     /* Ensure results do not depend on the host qsort for objects with
3876        multiple mapping symbols at the same address by sorting on type
3877        after vma.  */
3878     return 1;
3879   else if (amap->type < bmap->type)
3880     return -1;
3881   else
3882     return 0;
3883 }
3884
3885
3886 static char *
3887 _bfd_aarch64_erratum_835769_stub_name (unsigned num_fixes)
3888 {
3889   char *stub_name = (char *) bfd_malloc
3890     (strlen ("__erratum_835769_veneer_") + 16);
3891   if (stub_name != NULL)
3892     sprintf (stub_name,"__erratum_835769_veneer_%d", num_fixes);
3893   return stub_name;
3894 }
3895
3896 /* Scan for Cortex-A53 erratum 835769 sequence.
3897
3898    Return TRUE else FALSE on abnormal termination.  */
3899
3900 static bfd_boolean
3901 _bfd_aarch64_erratum_835769_scan (bfd *input_bfd,
3902                                   struct bfd_link_info *info,
3903                                   unsigned int *num_fixes_p)
3904 {
3905   asection *section;
3906   struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
3907   unsigned int num_fixes = *num_fixes_p;
3908
3909   if (htab == NULL)
3910     return TRUE;
3911
3912   for (section = input_bfd->sections;
3913        section != NULL;
3914        section = section->next)
3915     {
3916       bfd_byte *contents = NULL;
3917       struct _aarch64_elf_section_data *sec_data;
3918       unsigned int span;
3919
3920       if (elf_section_type (section) != SHT_PROGBITS
3921           || (elf_section_flags (section) & SHF_EXECINSTR) == 0
3922           || (section->flags & SEC_EXCLUDE) != 0
3923           || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
3924           || (section->output_section == bfd_abs_section_ptr))
3925         continue;
3926
3927       if (elf_section_data (section)->this_hdr.contents != NULL)
3928         contents = elf_section_data (section)->this_hdr.contents;
3929       else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
3930         return FALSE;
3931
3932       sec_data = elf_aarch64_section_data (section);
3933
3934       qsort (sec_data->map, sec_data->mapcount,
3935              sizeof (elf_aarch64_section_map), elf_aarch64_compare_mapping);
3936
3937       for (span = 0; span < sec_data->mapcount; span++)
3938         {
3939           unsigned int span_start = sec_data->map[span].vma;
3940           unsigned int span_end = ((span == sec_data->mapcount - 1)
3941                                    ? sec_data->map[0].vma + section->size
3942                                    : sec_data->map[span + 1].vma);
3943           unsigned int i;
3944           char span_type = sec_data->map[span].type;
3945
3946           if (span_type == 'd')
3947             continue;
3948
3949           for (i = span_start; i + 4 < span_end; i += 4)
3950             {
3951               uint32_t insn_1 = bfd_getl32 (contents + i);
3952               uint32_t insn_2 = bfd_getl32 (contents + i + 4);
3953
3954               if (aarch64_erratum_sequence (insn_1, insn_2))
3955                 {
3956                   struct elf_aarch64_stub_hash_entry *stub_entry;
3957                   char *stub_name = _bfd_aarch64_erratum_835769_stub_name (num_fixes);
3958                   if (! stub_name)
3959                     return FALSE;
3960
3961                   stub_entry = _bfd_aarch64_add_stub_entry_in_group (stub_name,
3962                                                                      section,
3963                                                                      htab);
3964                   if (! stub_entry)
3965                     return FALSE;
3966
3967                   stub_entry->stub_type = aarch64_stub_erratum_835769_veneer;
3968                   stub_entry->target_section = section;
3969                   stub_entry->target_value = i + 4;
3970                   stub_entry->veneered_insn = insn_2;
3971                   stub_entry->output_name = stub_name;
3972                   num_fixes++;
3973                 }
3974             }
3975         }
3976       if (elf_section_data (section)->this_hdr.contents == NULL)
3977         free (contents);
3978     }
3979
3980   *num_fixes_p = num_fixes;
3981
3982   return TRUE;
3983 }
3984
3985
3986 /* Test if instruction INSN is ADRP.  */
3987
3988 static bfd_boolean
3989 _bfd_aarch64_adrp_p (uint32_t insn)
3990 {
3991   return ((insn & AARCH64_ADRP_OP_MASK) == AARCH64_ADRP_OP);
3992 }
3993
3994
3995 /* Helper predicate to look for cortex-a53 erratum 843419 sequence 1.  */
3996
3997 static bfd_boolean
3998 _bfd_aarch64_erratum_843419_sequence_p (uint32_t insn_1, uint32_t insn_2,
3999                                         uint32_t insn_3)
4000 {
4001   uint32_t rt;
4002   uint32_t rt2;
4003   bfd_boolean pair;
4004   bfd_boolean load;
4005
4006   return (aarch64_mem_op_p (insn_2, &rt, &rt2, &pair, &load)
4007           && (!pair
4008               || (pair && !load))
4009           && AARCH64_LDST_UIMM (insn_3)
4010           && AARCH64_RN (insn_3) == AARCH64_RD (insn_1));
4011 }
4012
4013
4014 /* Test for the presence of Cortex-A53 erratum 843419 instruction sequence.
4015
4016    Return TRUE if section CONTENTS at offset I contains one of the
4017    erratum 843419 sequences, otherwise return FALSE.  If a sequence is
4018    seen set P_VENEER_I to the offset of the final LOAD/STORE
4019    instruction in the sequence.
4020  */
4021
4022 static bfd_boolean
4023 _bfd_aarch64_erratum_843419_p (bfd_byte *contents, bfd_vma vma,
4024                                bfd_vma i, bfd_vma span_end,
4025                                bfd_vma *p_veneer_i)
4026 {
4027   uint32_t insn_1 = bfd_getl32 (contents + i);
4028
4029   if (!_bfd_aarch64_adrp_p (insn_1))
4030     return FALSE;
4031
4032   if (span_end < i + 12)
4033     return FALSE;
4034
4035   uint32_t insn_2 = bfd_getl32 (contents + i + 4);
4036   uint32_t insn_3 = bfd_getl32 (contents + i + 8);
4037
4038   if ((vma & 0xfff) != 0xff8 && (vma & 0xfff) != 0xffc)
4039     return FALSE;
4040
4041   if (_bfd_aarch64_erratum_843419_sequence_p (insn_1, insn_2, insn_3))
4042     {
4043       *p_veneer_i = i + 8;
4044       return TRUE;
4045     }
4046
4047   if (span_end < i + 16)
4048     return FALSE;
4049
4050   uint32_t insn_4 = bfd_getl32 (contents + i + 12);
4051
4052   if (_bfd_aarch64_erratum_843419_sequence_p (insn_1, insn_2, insn_4))
4053     {
4054       *p_veneer_i = i + 12;
4055       return TRUE;
4056     }
4057
4058   return FALSE;
4059 }
4060
4061
4062 /* Resize all stub sections.  */
4063
4064 static void
4065 _bfd_aarch64_resize_stubs (struct elf_aarch64_link_hash_table *htab)
4066 {
4067   asection *section;
4068
4069   /* OK, we've added some stubs.  Find out the new size of the
4070      stub sections.  */
4071   for (section = htab->stub_bfd->sections;
4072        section != NULL; section = section->next)
4073     {
4074       /* Ignore non-stub sections.  */
4075       if (!strstr (section->name, STUB_SUFFIX))
4076         continue;
4077       section->size = 0;
4078     }
4079
4080   bfd_hash_traverse (&htab->stub_hash_table, aarch64_size_one_stub, htab);
4081
4082   for (section = htab->stub_bfd->sections;
4083        section != NULL; section = section->next)
4084     {
4085       if (!strstr (section->name, STUB_SUFFIX))
4086         continue;
4087
4088       /* Add space for a branch.  Add 8 bytes to keep section 8 byte aligned,
4089          as long branch stubs contain a 64-bit address.  */
4090       if (section->size)
4091         section->size += 8;
4092
4093       /* Ensure all stub sections have a size which is a multiple of
4094          4096.  This is important in order to ensure that the insertion
4095          of stub sections does not in itself move existing code around
4096          in such a way that new errata sequences are created.  We only do this
4097          when the ADRP workaround is enabled.  If only the ADR workaround is
4098          enabled then the stubs workaround won't ever be used.  */
4099       if (htab->fix_erratum_843419 & ERRAT_ADRP)
4100         if (section->size)
4101           section->size = BFD_ALIGN (section->size, 0x1000);
4102     }
4103 }
4104
4105 /* Construct an erratum 843419 workaround stub name.  */
4106
4107 static char *
4108 _bfd_aarch64_erratum_843419_stub_name (asection *input_section,
4109                                        bfd_vma offset)
4110 {
4111   const bfd_size_type len = 8 + 4 + 1 + 8 + 1 + 16 + 1;
4112   char *stub_name = bfd_malloc (len);
4113
4114   if (stub_name != NULL)
4115     snprintf (stub_name, len, "e843419@%04x_%08x_%" BFD_VMA_FMT "x",
4116               input_section->owner->id,
4117               input_section->id,
4118               offset);
4119   return stub_name;
4120 }
4121
4122 /*  Build a stub_entry structure describing an 843419 fixup.
4123
4124     The stub_entry constructed is populated with the bit pattern INSN
4125     of the instruction located at OFFSET within input SECTION.
4126
4127     Returns TRUE on success.  */
4128
4129 static bfd_boolean
4130 _bfd_aarch64_erratum_843419_fixup (uint32_t insn,
4131                                    bfd_vma adrp_offset,
4132                                    bfd_vma ldst_offset,
4133                                    asection *section,
4134                                    struct bfd_link_info *info)
4135 {
4136   struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
4137   char *stub_name;
4138   struct elf_aarch64_stub_hash_entry *stub_entry;
4139
4140   stub_name = _bfd_aarch64_erratum_843419_stub_name (section, ldst_offset);
4141   if (stub_name == NULL)
4142     return FALSE;
4143   stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4144                                          FALSE, FALSE);
4145   if (stub_entry)
4146     {
4147       free (stub_name);
4148       return TRUE;
4149     }
4150
4151   /* We always place an 843419 workaround veneer in the stub section
4152      attached to the input section in which an erratum sequence has
4153      been found.  This ensures that later in the link process (in
4154      elfNN_aarch64_write_section) when we copy the veneered
4155      instruction from the input section into the stub section the
4156      copied instruction will have had any relocations applied to it.
4157      If we placed workaround veneers in any other stub section then we
4158      could not assume that all relocations have been processed on the
4159      corresponding input section at the point we output the stub
4160      section.  */
4161
4162   stub_entry = _bfd_aarch64_add_stub_entry_after (stub_name, section, htab);
4163   if (stub_entry == NULL)
4164     {
4165       free (stub_name);
4166       return FALSE;
4167     }
4168
4169   stub_entry->adrp_offset = adrp_offset;
4170   stub_entry->target_value = ldst_offset;
4171   stub_entry->target_section = section;
4172   stub_entry->stub_type = aarch64_stub_erratum_843419_veneer;
4173   stub_entry->veneered_insn = insn;
4174   stub_entry->output_name = stub_name;
4175
4176   return TRUE;
4177 }
4178
4179
4180 /* Scan an input section looking for the signature of erratum 843419.
4181
4182    Scans input SECTION in INPUT_BFD looking for erratum 843419
4183    signatures, for each signature found a stub_entry is created
4184    describing the location of the erratum for subsequent fixup.
4185
4186    Return TRUE on successful scan, FALSE on failure to scan.
4187  */
4188
4189 static bfd_boolean
4190 _bfd_aarch64_erratum_843419_scan (bfd *input_bfd, asection *section,
4191                                   struct bfd_link_info *info)
4192 {
4193   struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
4194
4195   if (htab == NULL)
4196     return TRUE;
4197
4198   if (elf_section_type (section) != SHT_PROGBITS
4199       || (elf_section_flags (section) & SHF_EXECINSTR) == 0
4200       || (section->flags & SEC_EXCLUDE) != 0
4201       || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4202       || (section->output_section == bfd_abs_section_ptr))
4203     return TRUE;
4204
4205   do
4206     {
4207       bfd_byte *contents = NULL;
4208       struct _aarch64_elf_section_data *sec_data;
4209       unsigned int span;
4210
4211       if (elf_section_data (section)->this_hdr.contents != NULL)
4212         contents = elf_section_data (section)->this_hdr.contents;
4213       else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
4214         return FALSE;
4215
4216       sec_data = elf_aarch64_section_data (section);
4217
4218       qsort (sec_data->map, sec_data->mapcount,
4219              sizeof (elf_aarch64_section_map), elf_aarch64_compare_mapping);
4220
4221       for (span = 0; span < sec_data->mapcount; span++)
4222         {
4223           unsigned int span_start = sec_data->map[span].vma;
4224           unsigned int span_end = ((span == sec_data->mapcount - 1)
4225                                    ? sec_data->map[0].vma + section->size
4226                                    : sec_data->map[span + 1].vma);
4227           unsigned int i;
4228           char span_type = sec_data->map[span].type;
4229
4230           if (span_type == 'd')
4231             continue;
4232
4233           for (i = span_start; i + 8 < span_end; i += 4)
4234             {
4235               bfd_vma vma = (section->output_section->vma
4236                              + section->output_offset
4237                              + i);
4238               bfd_vma veneer_i;
4239
4240               if (_bfd_aarch64_erratum_843419_p
4241                   (contents, vma, i, span_end, &veneer_i))
4242                 {
4243                   uint32_t insn = bfd_getl32 (contents + veneer_i);
4244
4245                   if (!_bfd_aarch64_erratum_843419_fixup (insn, i, veneer_i,
4246                                                           section, info))
4247                     return FALSE;
4248                 }
4249             }
4250         }
4251
4252       if (elf_section_data (section)->this_hdr.contents == NULL)
4253         free (contents);
4254     }
4255   while (0);
4256
4257   return TRUE;
4258 }
4259
4260
4261 /* Determine and set the size of the stub section for a final link.
4262
4263    The basic idea here is to examine all the relocations looking for
4264    PC-relative calls to a target that is unreachable with a "bl"
4265    instruction.  */
4266
4267 bfd_boolean
4268 elfNN_aarch64_size_stubs (bfd *output_bfd,
4269                           bfd *stub_bfd,
4270                           struct bfd_link_info *info,
4271                           bfd_signed_vma group_size,
4272                           asection * (*add_stub_section) (const char *,
4273                                                           asection *),
4274                           void (*layout_sections_again) (void))
4275 {
4276   bfd_size_type stub_group_size;
4277   bfd_boolean stubs_always_before_branch;
4278   bfd_boolean stub_changed = FALSE;
4279   struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
4280   unsigned int num_erratum_835769_fixes = 0;
4281
4282   /* Propagate mach to stub bfd, because it may not have been
4283      finalized when we created stub_bfd.  */
4284   bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
4285                      bfd_get_mach (output_bfd));
4286
4287   /* Stash our params away.  */
4288   htab->stub_bfd = stub_bfd;
4289   htab->add_stub_section = add_stub_section;
4290   htab->layout_sections_again = layout_sections_again;
4291   stubs_always_before_branch = group_size < 0;
4292   if (group_size < 0)
4293     stub_group_size = -group_size;
4294   else
4295     stub_group_size = group_size;
4296
4297   if (stub_group_size == 1)
4298     {
4299       /* Default values.  */
4300       /* AArch64 branch range is +-128MB. The value used is 1MB less.  */
4301       stub_group_size = 127 * 1024 * 1024;
4302     }
4303
4304   group_sections (htab, stub_group_size, stubs_always_before_branch);
4305
4306   (*htab->layout_sections_again) ();
4307
4308   if (htab->fix_erratum_835769)
4309     {
4310       bfd *input_bfd;
4311
4312       for (input_bfd = info->input_bfds;
4313            input_bfd != NULL; input_bfd = input_bfd->link.next)
4314         if (!_bfd_aarch64_erratum_835769_scan (input_bfd, info,
4315                                                &num_erratum_835769_fixes))
4316           return FALSE;
4317
4318       _bfd_aarch64_resize_stubs (htab);
4319       (*htab->layout_sections_again) ();
4320     }
4321
4322   if (htab->fix_erratum_843419 != ERRAT_NONE)
4323     {
4324       bfd *input_bfd;
4325
4326       for (input_bfd = info->input_bfds;
4327            input_bfd != NULL;
4328            input_bfd = input_bfd->link.next)
4329         {
4330           asection *section;
4331
4332           for (section = input_bfd->sections;
4333                section != NULL;
4334                section = section->next)
4335             if (!_bfd_aarch64_erratum_843419_scan (input_bfd, section, info))
4336               return FALSE;
4337         }
4338
4339       _bfd_aarch64_resize_stubs (htab);
4340       (*htab->layout_sections_again) ();
4341     }
4342
4343   while (1)
4344     {
4345       bfd *input_bfd;
4346
4347       for (input_bfd = info->input_bfds;
4348            input_bfd != NULL; input_bfd = input_bfd->link.next)
4349         {
4350           Elf_Internal_Shdr *symtab_hdr;
4351           asection *section;
4352           Elf_Internal_Sym *local_syms = NULL;
4353
4354           /* We'll need the symbol table in a second.  */
4355           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4356           if (symtab_hdr->sh_info == 0)
4357             continue;
4358
4359           /* Walk over each section attached to the input bfd.  */
4360           for (section = input_bfd->sections;
4361                section != NULL; section = section->next)
4362             {
4363               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
4364
4365               /* If there aren't any relocs, then there's nothing more
4366                  to do.  */
4367               if ((section->flags & SEC_RELOC) == 0
4368                   || section->reloc_count == 0
4369                   || (section->flags & SEC_CODE) == 0)
4370                 continue;
4371
4372               /* If this section is a link-once section that will be
4373                  discarded, then don't create any stubs.  */
4374               if (section->output_section == NULL
4375                   || section->output_section->owner != output_bfd)
4376                 continue;
4377
4378               /* Get the relocs.  */
4379               internal_relocs
4380                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
4381                                              NULL, info->keep_memory);
4382               if (internal_relocs == NULL)
4383                 goto error_ret_free_local;
4384
4385               /* Now examine each relocation.  */
4386               irela = internal_relocs;
4387               irelaend = irela + section->reloc_count;
4388               for (; irela < irelaend; irela++)
4389                 {
4390                   unsigned int r_type, r_indx;
4391                   enum elf_aarch64_stub_type stub_type;
4392                   struct elf_aarch64_stub_hash_entry *stub_entry;
4393                   asection *sym_sec;
4394                   bfd_vma sym_value;
4395                   bfd_vma destination;
4396                   struct elf_aarch64_link_hash_entry *hash;
4397                   const char *sym_name;
4398                   char *stub_name;
4399                   const asection *id_sec;
4400                   unsigned char st_type;
4401                   bfd_size_type len;
4402
4403                   r_type = ELFNN_R_TYPE (irela->r_info);
4404                   r_indx = ELFNN_R_SYM (irela->r_info);
4405
4406                   if (r_type >= (unsigned int) R_AARCH64_end)
4407                     {
4408                       bfd_set_error (bfd_error_bad_value);
4409                     error_ret_free_internal:
4410                       if (elf_section_data (section)->relocs == NULL)
4411                         free (internal_relocs);
4412                       goto error_ret_free_local;
4413                     }
4414
4415                   /* Only look for stubs on unconditional branch and
4416                      branch and link instructions.  */
4417                   if (r_type != (unsigned int) AARCH64_R (CALL26)
4418                       && r_type != (unsigned int) AARCH64_R (JUMP26))
4419                     continue;
4420
4421                   /* Now determine the call target, its name, value,
4422                      section.  */
4423                   sym_sec = NULL;
4424                   sym_value = 0;
4425                   destination = 0;
4426                   hash = NULL;
4427                   sym_name = NULL;
4428                   if (r_indx < symtab_hdr->sh_info)
4429                     {
4430                       /* It's a local symbol.  */
4431                       Elf_Internal_Sym *sym;
4432                       Elf_Internal_Shdr *hdr;
4433
4434                       if (local_syms == NULL)
4435                         {
4436                           local_syms
4437                             = (Elf_Internal_Sym *) symtab_hdr->contents;
4438                           if (local_syms == NULL)
4439                             local_syms
4440                               = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
4441                                                       symtab_hdr->sh_info, 0,
4442                                                       NULL, NULL, NULL);
4443                           if (local_syms == NULL)
4444                             goto error_ret_free_internal;
4445                         }
4446
4447                       sym = local_syms + r_indx;
4448                       hdr = elf_elfsections (input_bfd)[sym->st_shndx];
4449                       sym_sec = hdr->bfd_section;
4450                       if (!sym_sec)
4451                         /* This is an undefined symbol.  It can never
4452                            be resolved.  */
4453                         continue;
4454
4455                       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4456                         sym_value = sym->st_value;
4457                       destination = (sym_value + irela->r_addend
4458                                      + sym_sec->output_offset
4459                                      + sym_sec->output_section->vma);
4460                       st_type = ELF_ST_TYPE (sym->st_info);
4461                       sym_name
4462                         = bfd_elf_string_from_elf_section (input_bfd,
4463                                                            symtab_hdr->sh_link,
4464                                                            sym->st_name);
4465                     }
4466                   else
4467                     {
4468                       int e_indx;
4469
4470                       e_indx = r_indx - symtab_hdr->sh_info;
4471                       hash = ((struct elf_aarch64_link_hash_entry *)
4472                               elf_sym_hashes (input_bfd)[e_indx]);
4473
4474                       while (hash->root.root.type == bfd_link_hash_indirect
4475                              || hash->root.root.type == bfd_link_hash_warning)
4476                         hash = ((struct elf_aarch64_link_hash_entry *)
4477                                 hash->root.root.u.i.link);
4478
4479                       if (hash->root.root.type == bfd_link_hash_defined
4480                           || hash->root.root.type == bfd_link_hash_defweak)
4481                         {
4482                           struct elf_aarch64_link_hash_table *globals =
4483                             elf_aarch64_hash_table (info);
4484                           sym_sec = hash->root.root.u.def.section;
4485                           sym_value = hash->root.root.u.def.value;
4486                           /* For a destination in a shared library,
4487                              use the PLT stub as target address to
4488                              decide whether a branch stub is
4489                              needed.  */
4490                           if (globals->root.splt != NULL && hash != NULL
4491                               && hash->root.plt.offset != (bfd_vma) - 1)
4492                             {
4493                               sym_sec = globals->root.splt;
4494                               sym_value = hash->root.plt.offset;
4495                               if (sym_sec->output_section != NULL)
4496                                 destination = (sym_value
4497                                                + sym_sec->output_offset
4498                                                +
4499                                                sym_sec->output_section->vma);
4500                             }
4501                           else if (sym_sec->output_section != NULL)
4502                             destination = (sym_value + irela->r_addend
4503                                            + sym_sec->output_offset
4504                                            + sym_sec->output_section->vma);
4505                         }
4506                       else if (hash->root.root.type == bfd_link_hash_undefined
4507                                || (hash->root.root.type
4508                                    == bfd_link_hash_undefweak))
4509                         {
4510                           /* For a shared library, use the PLT stub as
4511                              target address to decide whether a long
4512                              branch stub is needed.
4513                              For absolute code, they cannot be handled.  */
4514                           struct elf_aarch64_link_hash_table *globals =
4515                             elf_aarch64_hash_table (info);
4516
4517                           if (globals->root.splt != NULL && hash != NULL
4518                               && hash->root.plt.offset != (bfd_vma) - 1)
4519                             {
4520                               sym_sec = globals->root.splt;
4521                               sym_value = hash->root.plt.offset;
4522                               if (sym_sec->output_section != NULL)
4523                                 destination = (sym_value
4524                                                + sym_sec->output_offset
4525                                                +
4526                                                sym_sec->output_section->vma);
4527                             }
4528                           else
4529                             continue;
4530                         }
4531                       else
4532                         {
4533                           bfd_set_error (bfd_error_bad_value);
4534                           goto error_ret_free_internal;
4535                         }
4536                       st_type = ELF_ST_TYPE (hash->root.type);
4537                       sym_name = hash->root.root.root.string;
4538                     }
4539
4540                   /* Determine what (if any) linker stub is needed.  */
4541                   stub_type = aarch64_type_of_stub (section, irela, sym_sec,
4542                                                     st_type, destination);
4543                   if (stub_type == aarch64_stub_none)
4544                     continue;
4545
4546                   /* Support for grouping stub sections.  */
4547                   id_sec = htab->stub_group[section->id].link_sec;
4548
4549                   /* Get the name of this stub.  */
4550                   stub_name = elfNN_aarch64_stub_name (id_sec, sym_sec, hash,
4551                                                        irela);
4552                   if (!stub_name)
4553                     goto error_ret_free_internal;
4554
4555                   stub_entry =
4556                     aarch64_stub_hash_lookup (&htab->stub_hash_table,
4557                                               stub_name, FALSE, FALSE);
4558                   if (stub_entry != NULL)
4559                     {
4560                       /* The proper stub has already been created.  */
4561                       free (stub_name);
4562                       /* Always update this stub's target since it may have
4563                          changed after layout.  */
4564                       stub_entry->target_value = sym_value + irela->r_addend;
4565                       continue;
4566                     }
4567
4568                   stub_entry = _bfd_aarch64_add_stub_entry_in_group
4569                     (stub_name, section, htab);
4570                   if (stub_entry == NULL)
4571                     {
4572                       free (stub_name);
4573                       goto error_ret_free_internal;
4574                     }
4575
4576                   stub_entry->target_value = sym_value + irela->r_addend;
4577                   stub_entry->target_section = sym_sec;
4578                   stub_entry->stub_type = stub_type;
4579                   stub_entry->h = hash;
4580                   stub_entry->st_type = st_type;
4581
4582                   if (sym_name == NULL)
4583                     sym_name = "unnamed";
4584                   len = sizeof (STUB_ENTRY_NAME) + strlen (sym_name);
4585                   stub_entry->output_name = bfd_alloc (htab->stub_bfd, len);
4586                   if (stub_entry->output_name == NULL)
4587                     {
4588                       free (stub_name);
4589                       goto error_ret_free_internal;
4590                     }
4591
4592                   snprintf (stub_entry->output_name, len, STUB_ENTRY_NAME,
4593                             sym_name);
4594
4595                   stub_changed = TRUE;
4596                 }
4597
4598               /* We're done with the internal relocs, free them.  */
4599               if (elf_section_data (section)->relocs == NULL)
4600                 free (internal_relocs);
4601             }
4602         }
4603
4604       if (!stub_changed)
4605         break;
4606
4607       _bfd_aarch64_resize_stubs (htab);
4608
4609       /* Ask the linker to do its stuff.  */
4610       (*htab->layout_sections_again) ();
4611       stub_changed = FALSE;
4612     }
4613
4614   return TRUE;
4615
4616 error_ret_free_local:
4617   return FALSE;
4618 }
4619
4620 /* Build all the stubs associated with the current output file.  The
4621    stubs are kept in a hash table attached to the main linker hash
4622    table.  We also set up the .plt entries for statically linked PIC
4623    functions here.  This function is called via aarch64_elf_finish in the
4624    linker.  */
4625
4626 bfd_boolean
4627 elfNN_aarch64_build_stubs (struct bfd_link_info *info)
4628 {
4629   asection *stub_sec;
4630   struct bfd_hash_table *table;
4631   struct elf_aarch64_link_hash_table *htab;
4632
4633   htab = elf_aarch64_hash_table (info);
4634
4635   for (stub_sec = htab->stub_bfd->sections;
4636        stub_sec != NULL; stub_sec = stub_sec->next)
4637     {
4638       bfd_size_type size;
4639
4640       /* Ignore non-stub sections.  */
4641       if (!strstr (stub_sec->name, STUB_SUFFIX))
4642         continue;
4643
4644       /* Allocate memory to hold the linker stubs.  */
4645       size = stub_sec->size;
4646       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
4647       if (stub_sec->contents == NULL && size != 0)
4648         return FALSE;
4649       stub_sec->size = 0;
4650
4651       /* Add a branch around the stub section, and a nop, to keep it 8 byte
4652          aligned, as long branch stubs contain a 64-bit address.  */
4653       bfd_putl32 (0x14000000 | (size >> 2), stub_sec->contents);
4654       bfd_putl32 (INSN_NOP, stub_sec->contents + 4);
4655       stub_sec->size += 8;
4656     }
4657
4658   /* Build the stubs as directed by the stub hash table.  */
4659   table = &htab->stub_hash_table;
4660   bfd_hash_traverse (table, aarch64_build_one_stub, info);
4661
4662   return TRUE;
4663 }
4664
4665
4666 /* Add an entry to the code/data map for section SEC.  */
4667
4668 static void
4669 elfNN_aarch64_section_map_add (asection *sec, char type, bfd_vma vma)
4670 {
4671   struct _aarch64_elf_section_data *sec_data =
4672     elf_aarch64_section_data (sec);
4673   unsigned int newidx;
4674
4675   if (sec_data->map == NULL)
4676     {
4677       sec_data->map = bfd_malloc (sizeof (elf_aarch64_section_map));
4678       sec_data->mapcount = 0;
4679       sec_data->mapsize = 1;
4680     }
4681
4682   newidx = sec_data->mapcount++;
4683
4684   if (sec_data->mapcount > sec_data->mapsize)
4685     {
4686       sec_data->mapsize *= 2;
4687       sec_data->map = bfd_realloc_or_free
4688         (sec_data->map, sec_data->mapsize * sizeof (elf_aarch64_section_map));
4689     }
4690
4691   if (sec_data->map)
4692     {
4693       sec_data->map[newidx].vma = vma;
4694       sec_data->map[newidx].type = type;
4695     }
4696 }
4697
4698
4699 /* Initialise maps of insn/data for input BFDs.  */
4700 void
4701 bfd_elfNN_aarch64_init_maps (bfd *abfd)
4702 {
4703   Elf_Internal_Sym *isymbuf;
4704   Elf_Internal_Shdr *hdr;
4705   unsigned int i, localsyms;
4706
4707   /* Make sure that we are dealing with an AArch64 elf binary.  */
4708   if (!is_aarch64_elf (abfd))
4709     return;
4710
4711   if ((abfd->flags & DYNAMIC) != 0)
4712    return;
4713
4714   hdr = &elf_symtab_hdr (abfd);
4715   localsyms = hdr->sh_info;
4716
4717   /* Obtain a buffer full of symbols for this BFD. The hdr->sh_info field
4718      should contain the number of local symbols, which should come before any
4719      global symbols.  Mapping symbols are always local.  */
4720   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, localsyms, 0, NULL, NULL, NULL);
4721
4722   /* No internal symbols read?  Skip this BFD.  */
4723   if (isymbuf == NULL)
4724     return;
4725
4726   for (i = 0; i < localsyms; i++)
4727     {
4728       Elf_Internal_Sym *isym = &isymbuf[i];
4729       asection *sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4730       const char *name;
4731
4732       if (sec != NULL && ELF_ST_BIND (isym->st_info) == STB_LOCAL)
4733         {
4734           name = bfd_elf_string_from_elf_section (abfd,
4735                                                   hdr->sh_link,
4736                                                   isym->st_name);
4737
4738           if (bfd_is_aarch64_special_symbol_name
4739               (name, BFD_AARCH64_SPECIAL_SYM_TYPE_MAP))
4740             elfNN_aarch64_section_map_add (sec, name[1], isym->st_value);
4741         }
4742     }
4743 }
4744
4745 static void
4746 setup_plt_values (struct bfd_link_info *link_info,
4747                   aarch64_plt_type plt_type)
4748 {
4749   struct elf_aarch64_link_hash_table *globals;
4750   globals = elf_aarch64_hash_table (link_info);
4751
4752   if (plt_type == PLT_BTI_PAC)
4753     {
4754       globals->plt0_entry = elfNN_aarch64_small_plt0_bti_entry;
4755
4756       /* Only in ET_EXEC we need PLTn with BTI.  */
4757       if (bfd_link_pde (link_info))
4758         {
4759           globals->plt_entry_size = PLT_BTI_PAC_SMALL_ENTRY_SIZE;
4760           globals->plt_entry = elfNN_aarch64_small_plt_bti_pac_entry;
4761         }
4762       else
4763         {
4764           globals->plt_entry_size = PLT_PAC_SMALL_ENTRY_SIZE;
4765           globals->plt_entry = elfNN_aarch64_small_plt_pac_entry;
4766         }
4767     }
4768   else if (plt_type == PLT_BTI)
4769     {
4770       globals->plt0_entry = elfNN_aarch64_small_plt0_bti_entry;
4771
4772       /* Only in ET_EXEC we need PLTn with BTI.  */
4773       if (bfd_link_pde (link_info))
4774         {
4775           globals->plt_entry_size = PLT_BTI_SMALL_ENTRY_SIZE;
4776           globals->plt_entry = elfNN_aarch64_small_plt_bti_entry;
4777         }
4778     }
4779   else if (plt_type == PLT_PAC)
4780     {
4781       globals->plt_entry_size = PLT_PAC_SMALL_ENTRY_SIZE;
4782       globals->plt_entry = elfNN_aarch64_small_plt_pac_entry;
4783     }
4784 }
4785
4786 /* Set option values needed during linking.  */
4787 void
4788 bfd_elfNN_aarch64_set_options (struct bfd *output_bfd,
4789                                struct bfd_link_info *link_info,
4790                                int no_enum_warn,
4791                                int no_wchar_warn, int pic_veneer,
4792                                int fix_erratum_835769,
4793                                erratum_84319_opts fix_erratum_843419,
4794                                int no_apply_dynamic_relocs,
4795                                aarch64_bti_pac_info bp_info)
4796 {
4797   struct elf_aarch64_link_hash_table *globals;
4798
4799   globals = elf_aarch64_hash_table (link_info);
4800   globals->pic_veneer = pic_veneer;
4801   globals->fix_erratum_835769 = fix_erratum_835769;
4802   /* If the default options are used, then ERRAT_ADR will be set by default
4803      which will enable the ADRP->ADR workaround for the erratum 843419
4804      workaround.  */
4805   globals->fix_erratum_843419 = fix_erratum_843419;
4806   globals->no_apply_dynamic_relocs = no_apply_dynamic_relocs;
4807
4808   BFD_ASSERT (is_aarch64_elf (output_bfd));
4809   elf_aarch64_tdata (output_bfd)->no_enum_size_warning = no_enum_warn;
4810   elf_aarch64_tdata (output_bfd)->no_wchar_size_warning = no_wchar_warn;
4811
4812   switch (bp_info.bti_type)
4813     {
4814     case BTI_WARN:
4815       elf_aarch64_tdata (output_bfd)->no_bti_warn = 0;
4816       elf_aarch64_tdata (output_bfd)->gnu_and_prop
4817         |= GNU_PROPERTY_AARCH64_FEATURE_1_BTI;
4818       break;
4819
4820     default:
4821       break;
4822     }
4823   elf_aarch64_tdata (output_bfd)->plt_type = bp_info.plt_type;
4824   setup_plt_values (link_info, bp_info.plt_type);
4825 }
4826
4827 static bfd_vma
4828 aarch64_calculate_got_entry_vma (struct elf_link_hash_entry *h,
4829                                  struct elf_aarch64_link_hash_table
4830                                  *globals, struct bfd_link_info *info,
4831                                  bfd_vma value, bfd *output_bfd,
4832                                  bfd_boolean *unresolved_reloc_p)
4833 {
4834   bfd_vma off = (bfd_vma) - 1;
4835   asection *basegot = globals->root.sgot;
4836   bfd_boolean dyn = globals->root.dynamic_sections_created;
4837
4838   if (h != NULL)
4839     {
4840       BFD_ASSERT (basegot != NULL);
4841       off = h->got.offset;
4842       BFD_ASSERT (off != (bfd_vma) - 1);
4843       if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
4844           || (bfd_link_pic (info)
4845               && SYMBOL_REFERENCES_LOCAL (info, h))
4846           || (ELF_ST_VISIBILITY (h->other)
4847               && h->root.type == bfd_link_hash_undefweak))
4848         {
4849           /* This is actually a static link, or it is a -Bsymbolic link
4850              and the symbol is defined locally.  We must initialize this
4851              entry in the global offset table.  Since the offset must
4852              always be a multiple of 8 (4 in the case of ILP32), we use
4853              the least significant bit to record whether we have
4854              initialized it already.
4855              When doing a dynamic link, we create a .rel(a).got relocation
4856              entry to initialize the value.  This is done in the
4857              finish_dynamic_symbol routine.  */
4858           if ((off & 1) != 0)
4859             off &= ~1;
4860           else
4861             {
4862               bfd_put_NN (output_bfd, value, basegot->contents + off);
4863               h->got.offset |= 1;
4864             }
4865         }
4866       else
4867         *unresolved_reloc_p = FALSE;
4868
4869       off = off + basegot->output_section->vma + basegot->output_offset;
4870     }
4871
4872   return off;
4873 }
4874
4875 /* Change R_TYPE to a more efficient access model where possible,
4876    return the new reloc type.  */
4877
4878 static bfd_reloc_code_real_type
4879 aarch64_tls_transition_without_check (bfd_reloc_code_real_type r_type,
4880                                       struct elf_link_hash_entry *h)
4881 {
4882   bfd_boolean is_local = h == NULL;
4883
4884   switch (r_type)
4885     {
4886     case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
4887     case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
4888       return (is_local
4889               ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1
4890               : BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21);
4891
4892     case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
4893       return (is_local
4894               ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
4895               : r_type);
4896
4897     case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
4898       return (is_local
4899               ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1
4900               : BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19);
4901
4902     case BFD_RELOC_AARCH64_TLSDESC_LDR:
4903       return (is_local
4904               ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
4905               : BFD_RELOC_AARCH64_NONE);
4906
4907     case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
4908       return (is_local
4909               ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC
4910               : BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC);
4911
4912     case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
4913       return (is_local
4914               ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2
4915               : BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1);
4916
4917     case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
4918     case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
4919       return (is_local
4920               ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
4921               : BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC);
4922
4923     case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
4924       return is_local ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1 : r_type;
4925
4926     case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
4927       return is_local ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC : r_type;
4928
4929     case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
4930       return r_type;
4931
4932     case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
4933       return (is_local
4934               ? BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12
4935               : BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19);
4936
4937     case BFD_RELOC_AARCH64_TLSDESC_ADD:
4938     case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
4939     case BFD_RELOC_AARCH64_TLSDESC_CALL:
4940       /* Instructions with these relocations will become NOPs.  */
4941       return BFD_RELOC_AARCH64_NONE;
4942
4943     case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
4944     case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
4945     case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
4946       return is_local ? BFD_RELOC_AARCH64_NONE : r_type;
4947
4948 #if ARCH_SIZE == 64
4949     case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
4950       return is_local
4951         ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC
4952         : BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC;
4953
4954     case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
4955       return is_local
4956         ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2
4957         : BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1;
4958 #endif
4959
4960     default:
4961       break;
4962     }
4963
4964   return r_type;
4965 }
4966
4967 static unsigned int
4968 aarch64_reloc_got_type (bfd_reloc_code_real_type r_type)
4969 {
4970   switch (r_type)
4971     {
4972     case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
4973     case BFD_RELOC_AARCH64_GOT_LD_PREL19:
4974     case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
4975     case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
4976     case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
4977     case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
4978     case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
4979     case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
4980     case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
4981       return GOT_NORMAL;
4982
4983     case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
4984     case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
4985     case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
4986     case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
4987     case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
4988     case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
4989     case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
4990     case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
4991       return GOT_TLS_GD;
4992
4993     case BFD_RELOC_AARCH64_TLSDESC_ADD:
4994     case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
4995     case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
4996     case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
4997     case BFD_RELOC_AARCH64_TLSDESC_CALL:
4998     case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
4999     case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12:
5000     case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
5001     case BFD_RELOC_AARCH64_TLSDESC_LDR:
5002     case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
5003     case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
5004       return GOT_TLSDESC_GD;
5005
5006     case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5007     case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
5008     case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5009     case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
5010     case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
5011     case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
5012       return GOT_TLS_IE;
5013
5014     default:
5015       break;
5016     }
5017   return GOT_UNKNOWN;
5018 }
5019
5020 static bfd_boolean
5021 aarch64_can_relax_tls (bfd *input_bfd,
5022                        struct bfd_link_info *info,
5023                        bfd_reloc_code_real_type r_type,
5024                        struct elf_link_hash_entry *h,
5025                        unsigned long r_symndx)
5026 {
5027   unsigned int symbol_got_type;
5028   unsigned int reloc_got_type;
5029
5030   if (! IS_AARCH64_TLS_RELAX_RELOC (r_type))
5031     return FALSE;
5032
5033   symbol_got_type = elfNN_aarch64_symbol_got_type (h, input_bfd, r_symndx);
5034   reloc_got_type = aarch64_reloc_got_type (r_type);
5035
5036   if (symbol_got_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (reloc_got_type))
5037     return TRUE;
5038
5039   if (!bfd_link_executable (info))
5040     return FALSE;
5041
5042   if  (h && h->root.type == bfd_link_hash_undefweak)
5043     return FALSE;
5044
5045   return TRUE;
5046 }
5047
5048 /* Given the relocation code R_TYPE, return the relaxed bfd reloc
5049    enumerator.  */
5050
5051 static bfd_reloc_code_real_type
5052 aarch64_tls_transition (bfd *input_bfd,
5053                         struct bfd_link_info *info,
5054                         unsigned int r_type,
5055                         struct elf_link_hash_entry *h,
5056                         unsigned long r_symndx)
5057 {
5058   bfd_reloc_code_real_type bfd_r_type
5059     = elfNN_aarch64_bfd_reloc_from_type (input_bfd, r_type);
5060
5061   if (! aarch64_can_relax_tls (input_bfd, info, bfd_r_type, h, r_symndx))
5062     return bfd_r_type;
5063
5064   return aarch64_tls_transition_without_check (bfd_r_type, h);
5065 }
5066
5067 /* Return the base VMA address which should be subtracted from real addresses
5068    when resolving R_AARCH64_TLS_DTPREL relocation.  */
5069
5070 static bfd_vma
5071 dtpoff_base (struct bfd_link_info *info)
5072 {
5073   /* If tls_sec is NULL, we should have signalled an error already.  */
5074   BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
5075   return elf_hash_table (info)->tls_sec->vma;
5076 }
5077
5078 /* Return the base VMA address which should be subtracted from real addresses
5079    when resolving R_AARCH64_TLS_GOTTPREL64 relocations.  */
5080
5081 static bfd_vma
5082 tpoff_base (struct bfd_link_info *info)
5083 {
5084   struct elf_link_hash_table *htab = elf_hash_table (info);
5085
5086   /* If tls_sec is NULL, we should have signalled an error already.  */
5087   BFD_ASSERT (htab->tls_sec != NULL);
5088
5089   bfd_vma base = align_power ((bfd_vma) TCB_SIZE,
5090                               htab->tls_sec->alignment_power);
5091   return htab->tls_sec->vma - base;
5092 }
5093
5094 static bfd_vma *
5095 symbol_got_offset_ref (bfd *input_bfd, struct elf_link_hash_entry *h,
5096                        unsigned long r_symndx)
5097 {
5098   /* Calculate the address of the GOT entry for symbol
5099      referred to in h.  */
5100   if (h != NULL)
5101     return &h->got.offset;
5102   else
5103     {
5104       /* local symbol */
5105       struct elf_aarch64_local_symbol *l;
5106
5107       l = elf_aarch64_locals (input_bfd);
5108       return &l[r_symndx].got_offset;
5109     }
5110 }
5111
5112 static void
5113 symbol_got_offset_mark (bfd *input_bfd, struct elf_link_hash_entry *h,
5114                         unsigned long r_symndx)
5115 {
5116   bfd_vma *p;
5117   p = symbol_got_offset_ref (input_bfd, h, r_symndx);
5118   *p |= 1;
5119 }
5120
5121 static int
5122 symbol_got_offset_mark_p (bfd *input_bfd, struct elf_link_hash_entry *h,
5123                           unsigned long r_symndx)
5124 {
5125   bfd_vma value;
5126   value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
5127   return value & 1;
5128 }
5129
5130 static bfd_vma
5131 symbol_got_offset (bfd *input_bfd, struct elf_link_hash_entry *h,
5132                    unsigned long r_symndx)
5133 {
5134   bfd_vma value;
5135   value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
5136   value &= ~1;
5137   return value;
5138 }
5139
5140 static bfd_vma *
5141 symbol_tlsdesc_got_offset_ref (bfd *input_bfd, struct elf_link_hash_entry *h,
5142                                unsigned long r_symndx)
5143 {
5144   /* Calculate the address of the GOT entry for symbol
5145      referred to in h.  */
5146   if (h != NULL)
5147     {
5148       struct elf_aarch64_link_hash_entry *eh;
5149       eh = (struct elf_aarch64_link_hash_entry *) h;
5150       return &eh->tlsdesc_got_jump_table_offset;
5151     }
5152   else
5153     {
5154       /* local symbol */
5155       struct elf_aarch64_local_symbol *l;
5156
5157       l = elf_aarch64_locals (input_bfd);
5158       return &l[r_symndx].tlsdesc_got_jump_table_offset;
5159     }
5160 }
5161
5162 static void
5163 symbol_tlsdesc_got_offset_mark (bfd *input_bfd, struct elf_link_hash_entry *h,
5164                                 unsigned long r_symndx)
5165 {
5166   bfd_vma *p;
5167   p = symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
5168   *p |= 1;
5169 }
5170
5171 static int
5172 symbol_tlsdesc_got_offset_mark_p (bfd *input_bfd,
5173                                   struct elf_link_hash_entry *h,
5174                                   unsigned long r_symndx)
5175 {
5176   bfd_vma value;
5177   value = * symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
5178   return value & 1;
5179 }
5180
5181 static bfd_vma
5182 symbol_tlsdesc_got_offset (bfd *input_bfd, struct elf_link_hash_entry *h,
5183                           unsigned long r_symndx)
5184 {
5185   bfd_vma value;
5186   value = * symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
5187   value &= ~1;
5188   return value;
5189 }
5190
5191 /* Data for make_branch_to_erratum_835769_stub().  */
5192
5193 struct erratum_835769_branch_to_stub_data
5194 {
5195   struct bfd_link_info *info;
5196   asection *output_section;
5197   bfd_byte *contents;
5198 };
5199
5200 /* Helper to insert branches to erratum 835769 stubs in the right
5201    places for a particular section.  */
5202
5203 static bfd_boolean
5204 make_branch_to_erratum_835769_stub (struct bfd_hash_entry *gen_entry,
5205                                     void *in_arg)
5206 {
5207   struct elf_aarch64_stub_hash_entry *stub_entry;
5208   struct erratum_835769_branch_to_stub_data *data;
5209   bfd_byte *contents;
5210   unsigned long branch_insn = 0;
5211   bfd_vma veneered_insn_loc, veneer_entry_loc;
5212   bfd_signed_vma branch_offset;
5213   unsigned int target;
5214   bfd *abfd;
5215
5216   stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
5217   data = (struct erratum_835769_branch_to_stub_data *) in_arg;
5218
5219   if (stub_entry->target_section != data->output_section
5220       || stub_entry->stub_type != aarch64_stub_erratum_835769_veneer)
5221     return TRUE;
5222
5223   contents = data->contents;
5224   veneered_insn_loc = stub_entry->target_section->output_section->vma
5225                       + stub_entry->target_section->output_offset
5226                       + stub_entry->target_value;
5227   veneer_entry_loc = stub_entry->stub_sec->output_section->vma
5228                      + stub_entry->stub_sec->output_offset
5229                      + stub_entry->stub_offset;
5230   branch_offset = veneer_entry_loc - veneered_insn_loc;
5231
5232   abfd = stub_entry->target_section->owner;
5233   if (!aarch64_valid_branch_p (veneer_entry_loc, veneered_insn_loc))
5234     _bfd_error_handler
5235       (_("%pB: error: erratum 835769 stub out "
5236          "of range (input file too large)"), abfd);
5237
5238   target = stub_entry->target_value;
5239   branch_insn = 0x14000000;
5240   branch_offset >>= 2;
5241   branch_offset &= 0x3ffffff;
5242   branch_insn |= branch_offset;
5243   bfd_putl32 (branch_insn, &contents[target]);
5244
5245   return TRUE;
5246 }
5247
5248
5249 static bfd_boolean
5250 _bfd_aarch64_erratum_843419_branch_to_stub (struct bfd_hash_entry *gen_entry,
5251                                             void *in_arg)
5252 {
5253   struct elf_aarch64_stub_hash_entry *stub_entry
5254     = (struct elf_aarch64_stub_hash_entry *) gen_entry;
5255   struct erratum_835769_branch_to_stub_data *data
5256     = (struct erratum_835769_branch_to_stub_data *) in_arg;
5257   struct bfd_link_info *info;
5258   struct elf_aarch64_link_hash_table *htab;
5259   bfd_byte *contents;
5260   asection *section;
5261   bfd *abfd;
5262   bfd_vma place;
5263   uint32_t insn;
5264
5265   info = data->info;
5266   contents = data->contents;
5267   section = data->output_section;
5268
5269   htab = elf_aarch64_hash_table (info);
5270
5271   if (stub_entry->target_section != section
5272       || stub_entry->stub_type != aarch64_stub_erratum_843419_veneer)
5273     return TRUE;
5274
5275   BFD_ASSERT (((htab->fix_erratum_843419 & ERRAT_ADRP) && stub_entry->stub_sec)
5276               || (htab->fix_erratum_843419 & ERRAT_ADR));
5277
5278   /* Only update the stub section if we have one.  We should always have one if
5279      we're allowed to use the ADRP errata workaround, otherwise it is not
5280      required.  */
5281   if (stub_entry->stub_sec)
5282     {
5283       insn = bfd_getl32 (contents + stub_entry->target_value);
5284       bfd_putl32 (insn,
5285                   stub_entry->stub_sec->contents + stub_entry->stub_offset);
5286     }
5287
5288   place = (section->output_section->vma + section->output_offset
5289            + stub_entry->adrp_offset);
5290   insn = bfd_getl32 (contents + stub_entry->adrp_offset);
5291
5292   if (!_bfd_aarch64_adrp_p (insn))
5293     abort ();
5294
5295   bfd_signed_vma imm =
5296     (_bfd_aarch64_sign_extend
5297      ((bfd_vma) _bfd_aarch64_decode_adrp_imm (insn) << 12, 33)
5298      - (place & 0xfff));
5299
5300   if ((htab->fix_erratum_843419 & ERRAT_ADR)
5301       && (imm >= AARCH64_MIN_ADRP_IMM  && imm <= AARCH64_MAX_ADRP_IMM))
5302     {
5303       insn = (_bfd_aarch64_reencode_adr_imm (AARCH64_ADR_OP, imm)
5304               | AARCH64_RT (insn));
5305       bfd_putl32 (insn, contents + stub_entry->adrp_offset);
5306       /* Stub is not needed, don't map it out.  */
5307       stub_entry->stub_type = aarch64_stub_none;
5308     }
5309   else if (htab->fix_erratum_843419 & ERRAT_ADRP)
5310     {
5311       bfd_vma veneered_insn_loc;
5312       bfd_vma veneer_entry_loc;
5313       bfd_signed_vma branch_offset;
5314       uint32_t branch_insn;
5315
5316       veneered_insn_loc = stub_entry->target_section->output_section->vma
5317         + stub_entry->target_section->output_offset
5318         + stub_entry->target_value;
5319       veneer_entry_loc = stub_entry->stub_sec->output_section->vma
5320         + stub_entry->stub_sec->output_offset
5321         + stub_entry->stub_offset;
5322       branch_offset = veneer_entry_loc - veneered_insn_loc;
5323
5324       abfd = stub_entry->target_section->owner;
5325       if (!aarch64_valid_branch_p (veneer_entry_loc, veneered_insn_loc))
5326         _bfd_error_handler
5327           (_("%pB: error: erratum 843419 stub out "
5328              "of range (input file too large)"), abfd);
5329
5330       branch_insn = 0x14000000;
5331       branch_offset >>= 2;
5332       branch_offset &= 0x3ffffff;
5333       branch_insn |= branch_offset;
5334       bfd_putl32 (branch_insn, contents + stub_entry->target_value);
5335     }
5336   else
5337     {
5338       abfd = stub_entry->target_section->owner;
5339       _bfd_error_handler
5340         (_("%pB: error: erratum 843419 immediate 0x%" BFD_VMA_FMT "x "
5341            "out of range for ADR (input file too large) and "
5342            "--fix-cortex-a53-843419=adr used.  Run the linker with "
5343            "--fix-cortex-a53-843419=full instead"), abfd, imm);
5344       bfd_set_error (bfd_error_bad_value);
5345       /* This function is called inside a hashtable traversal and the error
5346          handlers called above turn into non-fatal errors.  Which means this
5347          case ld returns an exit code 0 and also produces a broken object file.
5348          To prevent this, issue a hard abort.  */
5349       BFD_FAIL ();
5350     }
5351   return TRUE;
5352 }
5353
5354
5355 static bfd_boolean
5356 elfNN_aarch64_write_section (bfd *output_bfd  ATTRIBUTE_UNUSED,
5357                              struct bfd_link_info *link_info,
5358                              asection *sec,
5359                              bfd_byte *contents)
5360
5361 {
5362   struct elf_aarch64_link_hash_table *globals =
5363     elf_aarch64_hash_table (link_info);
5364
5365   if (globals == NULL)
5366     return FALSE;
5367
5368   /* Fix code to point to erratum 835769 stubs.  */
5369   if (globals->fix_erratum_835769)
5370     {
5371       struct erratum_835769_branch_to_stub_data data;
5372
5373       data.info = link_info;
5374       data.output_section = sec;
5375       data.contents = contents;
5376       bfd_hash_traverse (&globals->stub_hash_table,
5377                          make_branch_to_erratum_835769_stub, &data);
5378     }
5379
5380   if (globals->fix_erratum_843419)
5381     {
5382       struct erratum_835769_branch_to_stub_data data;
5383
5384       data.info = link_info;
5385       data.output_section = sec;
5386       data.contents = contents;
5387       bfd_hash_traverse (&globals->stub_hash_table,
5388                          _bfd_aarch64_erratum_843419_branch_to_stub, &data);
5389     }
5390
5391   return FALSE;
5392 }
5393
5394 /* Return TRUE if RELOC is a relocation against the base of GOT table.  */
5395
5396 static bfd_boolean
5397 aarch64_relocation_aginst_gp_p (bfd_reloc_code_real_type reloc)
5398 {
5399   return (reloc == BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14
5400           || reloc == BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15
5401           || reloc == BFD_RELOC_AARCH64_LD64_GOTOFF_LO15
5402           || reloc == BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC
5403           || reloc == BFD_RELOC_AARCH64_MOVW_GOTOFF_G1);
5404 }
5405
5406 /* Perform a relocation as part of a final link.  The input relocation type
5407    should be TLS relaxed.  */
5408
5409 static bfd_reloc_status_type
5410 elfNN_aarch64_final_link_relocate (reloc_howto_type *howto,
5411                                    bfd *input_bfd,
5412                                    bfd *output_bfd,
5413                                    asection *input_section,
5414                                    bfd_byte *contents,
5415                                    Elf_Internal_Rela *rel,
5416                                    bfd_vma value,
5417                                    struct bfd_link_info *info,
5418                                    asection *sym_sec,
5419                                    struct elf_link_hash_entry *h,
5420                                    bfd_boolean *unresolved_reloc_p,
5421                                    bfd_boolean save_addend,
5422                                    bfd_vma *saved_addend,
5423                                    Elf_Internal_Sym *sym)
5424 {
5425   Elf_Internal_Shdr *symtab_hdr;
5426   unsigned int r_type = howto->type;
5427   bfd_reloc_code_real_type bfd_r_type
5428     = elfNN_aarch64_bfd_reloc_from_howto (howto);
5429   unsigned long r_symndx;
5430   bfd_byte *hit_data = contents + rel->r_offset;
5431   bfd_vma place, off, got_entry_addr = 0;
5432   bfd_signed_vma signed_addend;
5433   struct elf_aarch64_link_hash_table *globals;
5434   bfd_boolean weak_undef_p;
5435   bfd_boolean relative_reloc;
5436   asection *base_got;
5437   bfd_vma orig_value = value;
5438   bfd_boolean resolved_to_zero;
5439   bfd_boolean abs_symbol_p;
5440
5441   globals = elf_aarch64_hash_table (info);
5442
5443   symtab_hdr = &elf_symtab_hdr (input_bfd);
5444
5445   BFD_ASSERT (is_aarch64_elf (input_bfd));
5446
5447   r_symndx = ELFNN_R_SYM (rel->r_info);
5448
5449   place = input_section->output_section->vma
5450     + input_section->output_offset + rel->r_offset;
5451
5452   /* Get addend, accumulating the addend for consecutive relocs
5453      which refer to the same offset.  */
5454   signed_addend = saved_addend ? *saved_addend : 0;
5455   signed_addend += rel->r_addend;
5456
5457   weak_undef_p = (h ? h->root.type == bfd_link_hash_undefweak
5458                   : bfd_is_und_section (sym_sec));
5459   abs_symbol_p = h != NULL && bfd_is_abs_symbol (&h->root);
5460
5461
5462   /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
5463      it here if it is defined in a non-shared object.  */
5464   if (h != NULL
5465       && h->type == STT_GNU_IFUNC
5466       && h->def_regular)
5467     {
5468       asection *plt;
5469       const char *name;
5470       bfd_vma addend = 0;
5471
5472       if ((input_section->flags & SEC_ALLOC) == 0)
5473         {
5474           /* If this is a SHT_NOTE section without SHF_ALLOC, treat
5475              STT_GNU_IFUNC symbol as STT_FUNC.  */
5476           if (elf_section_type (input_section) == SHT_NOTE)
5477             goto skip_ifunc;
5478
5479           /* Dynamic relocs are not propagated for SEC_DEBUGGING
5480              sections because such sections are not SEC_ALLOC and
5481              thus ld.so will not process them.  */
5482           if ((input_section->flags & SEC_DEBUGGING) != 0)
5483             return bfd_reloc_ok;
5484
5485           if (h->root.root.string)
5486             name = h->root.root.string;
5487           else
5488             name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, NULL);
5489           _bfd_error_handler
5490             /* xgettext:c-format */
5491             (_("%pB(%pA+%#" PRIx64 "): "
5492                "unresolvable %s relocation against symbol `%s'"),
5493              input_bfd, input_section, (uint64_t) rel->r_offset,
5494              howto->name, name);
5495           bfd_set_error (bfd_error_bad_value);
5496           return bfd_reloc_notsupported;
5497         }
5498       else if (h->plt.offset == (bfd_vma) -1)
5499         goto bad_ifunc_reloc;
5500
5501       /* STT_GNU_IFUNC symbol must go through PLT.  */
5502       plt = globals->root.splt ? globals->root.splt : globals->root.iplt;
5503       value = (plt->output_section->vma + plt->output_offset + h->plt.offset);
5504
5505       switch (bfd_r_type)
5506         {
5507         default:
5508 bad_ifunc_reloc:
5509           if (h->root.root.string)
5510             name = h->root.root.string;
5511           else
5512             name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
5513                                      NULL);
5514           _bfd_error_handler
5515             /* xgettext:c-format */
5516             (_("%pB: relocation %s against STT_GNU_IFUNC "
5517                "symbol `%s' isn't handled by %s"), input_bfd,
5518              howto->name, name, __FUNCTION__);
5519           bfd_set_error (bfd_error_bad_value);
5520           return bfd_reloc_notsupported;
5521
5522         case BFD_RELOC_AARCH64_NN:
5523           if (rel->r_addend != 0)
5524             {
5525               if (h->root.root.string)
5526                 name = h->root.root.string;
5527               else
5528                 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
5529                                          sym, NULL);
5530               _bfd_error_handler
5531                 /* xgettext:c-format */
5532                 (_("%pB: relocation %s against STT_GNU_IFUNC "
5533                    "symbol `%s' has non-zero addend: %" PRId64),
5534                  input_bfd, howto->name, name, (int64_t) rel->r_addend);
5535               bfd_set_error (bfd_error_bad_value);
5536               return bfd_reloc_notsupported;
5537             }
5538
5539           /* Generate dynamic relocation only when there is a
5540              non-GOT reference in a shared object.  */
5541           if (bfd_link_pic (info) && h->non_got_ref)
5542             {
5543               Elf_Internal_Rela outrel;
5544               asection *sreloc;
5545
5546               /* Need a dynamic relocation to get the real function
5547                  address.  */
5548               outrel.r_offset = _bfd_elf_section_offset (output_bfd,
5549                                                          info,
5550                                                          input_section,
5551                                                          rel->r_offset);
5552               if (outrel.r_offset == (bfd_vma) -1
5553                   || outrel.r_offset == (bfd_vma) -2)
5554                 abort ();
5555
5556               outrel.r_offset += (input_section->output_section->vma
5557                                   + input_section->output_offset);
5558
5559               if (h->dynindx == -1
5560                   || h->forced_local
5561                   || bfd_link_executable (info))
5562                 {
5563                   /* This symbol is resolved locally.  */
5564                   outrel.r_info = ELFNN_R_INFO (0, AARCH64_R (IRELATIVE));
5565                   outrel.r_addend = (h->root.u.def.value
5566                                      + h->root.u.def.section->output_section->vma
5567                                      + h->root.u.def.section->output_offset);
5568                 }
5569               else
5570                 {
5571                   outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
5572                   outrel.r_addend = 0;
5573                 }
5574
5575               sreloc = globals->root.irelifunc;
5576               elf_append_rela (output_bfd, sreloc, &outrel);
5577
5578               /* If this reloc is against an external symbol, we
5579                  do not want to fiddle with the addend.  Otherwise,
5580                  we need to include the symbol value so that it
5581                  becomes an addend for the dynamic reloc.  For an
5582                  internal symbol, we have updated addend.  */
5583               return bfd_reloc_ok;
5584             }
5585           /* FALLTHROUGH */
5586         case BFD_RELOC_AARCH64_CALL26:
5587         case BFD_RELOC_AARCH64_JUMP26:
5588           value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5589                                                        signed_addend,
5590                                                        weak_undef_p);
5591           return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type,
5592                                               howto, value);
5593         case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
5594         case BFD_RELOC_AARCH64_GOT_LD_PREL19:
5595         case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
5596         case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
5597         case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
5598         case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
5599         case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
5600         case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
5601         case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
5602           base_got = globals->root.sgot;
5603           off = h->got.offset;
5604
5605           if (base_got == NULL)
5606             abort ();
5607
5608           if (off == (bfd_vma) -1)
5609             {
5610               bfd_vma plt_index;
5611
5612               /* We can't use h->got.offset here to save state, or
5613                  even just remember the offset, as finish_dynamic_symbol
5614                  would use that as offset into .got.  */
5615
5616               if (globals->root.splt != NULL)
5617                 {
5618                   plt_index = ((h->plt.offset - globals->plt_header_size) /
5619                                globals->plt_entry_size);
5620                   off = (plt_index + 3) * GOT_ENTRY_SIZE;
5621                   base_got = globals->root.sgotplt;
5622                 }
5623               else
5624                 {
5625                   plt_index = h->plt.offset / globals->plt_entry_size;
5626                   off = plt_index * GOT_ENTRY_SIZE;
5627                   base_got = globals->root.igotplt;
5628                 }
5629
5630               if (h->dynindx == -1
5631                   || h->forced_local
5632                   || info->symbolic)
5633                 {
5634                   /* This references the local definition.  We must
5635                      initialize this entry in the global offset table.
5636                      Since the offset must always be a multiple of 8,
5637                      we use the least significant bit to record
5638                      whether we have initialized it already.
5639
5640                      When doing a dynamic link, we create a .rela.got
5641                      relocation entry to initialize the value.  This
5642                      is done in the finish_dynamic_symbol routine.       */
5643                   if ((off & 1) != 0)
5644                     off &= ~1;
5645                   else
5646                     {
5647                       bfd_put_NN (output_bfd, value,
5648                                   base_got->contents + off);
5649                       /* Note that this is harmless as -1 | 1 still is -1.  */
5650                       h->got.offset |= 1;
5651                     }
5652                 }
5653               value = (base_got->output_section->vma
5654                        + base_got->output_offset + off);
5655             }
5656           else
5657             value = aarch64_calculate_got_entry_vma (h, globals, info,
5658                                                      value, output_bfd,
5659                                                      unresolved_reloc_p);
5660
5661           if (aarch64_relocation_aginst_gp_p (bfd_r_type))
5662             addend = (globals->root.sgot->output_section->vma
5663                       + globals->root.sgot->output_offset);
5664
5665           value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5666                                                        addend, weak_undef_p);
5667           return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type, howto, value);
5668         case BFD_RELOC_AARCH64_ADD_LO12:
5669         case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
5670           break;
5671         }
5672     }
5673
5674  skip_ifunc:
5675   resolved_to_zero = (h != NULL
5676                       && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
5677
5678   switch (bfd_r_type)
5679     {
5680     case BFD_RELOC_AARCH64_NONE:
5681     case BFD_RELOC_AARCH64_TLSDESC_ADD:
5682     case BFD_RELOC_AARCH64_TLSDESC_CALL:
5683     case BFD_RELOC_AARCH64_TLSDESC_LDR:
5684       *unresolved_reloc_p = FALSE;
5685       return bfd_reloc_ok;
5686
5687     case BFD_RELOC_AARCH64_NN:
5688
5689       /* When generating a shared object or relocatable executable, these
5690          relocations are copied into the output file to be resolved at
5691          run time.  */
5692       if (((bfd_link_pic (info)
5693             || globals->root.is_relocatable_executable)
5694            && (input_section->flags & SEC_ALLOC)
5695            && (h == NULL
5696                || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5697                    && !resolved_to_zero)
5698                || h->root.type != bfd_link_hash_undefweak))
5699           /* Or we are creating an executable, we may need to keep relocations
5700              for symbols satisfied by a dynamic library if we manage to avoid
5701              copy relocs for the symbol.  */
5702           || (ELIMINATE_COPY_RELOCS
5703               && !bfd_link_pic (info)
5704               && h != NULL
5705               && (input_section->flags & SEC_ALLOC)
5706               && h->dynindx != -1
5707               && !h->non_got_ref
5708               && ((h->def_dynamic
5709                    && !h->def_regular)
5710                   || h->root.type == bfd_link_hash_undefweak
5711                   || h->root.type == bfd_link_hash_undefined)))
5712         {
5713           Elf_Internal_Rela outrel;
5714           bfd_byte *loc;
5715           bfd_boolean skip, relocate;
5716           asection *sreloc;
5717
5718           *unresolved_reloc_p = FALSE;
5719
5720           skip = FALSE;
5721           relocate = FALSE;
5722
5723           outrel.r_addend = signed_addend;
5724           outrel.r_offset =
5725             _bfd_elf_section_offset (output_bfd, info, input_section,
5726                                      rel->r_offset);
5727           if (outrel.r_offset == (bfd_vma) - 1)
5728             skip = TRUE;
5729           else if (outrel.r_offset == (bfd_vma) - 2)
5730             {
5731               skip = TRUE;
5732               relocate = TRUE;
5733             }
5734           else if (abs_symbol_p)
5735             {
5736               /* Local absolute symbol.  */
5737               skip = (h->forced_local || (h->dynindx == -1));
5738               relocate = skip;
5739             }
5740
5741           outrel.r_offset += (input_section->output_section->vma
5742                               + input_section->output_offset);
5743
5744           if (skip)
5745             memset (&outrel, 0, sizeof outrel);
5746           else if (h != NULL
5747                    && h->dynindx != -1
5748                    && (!bfd_link_pic (info)
5749                        || !(bfd_link_pie (info) || SYMBOLIC_BIND (info, h))
5750                        || !h->def_regular))
5751             outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
5752           else
5753             {
5754               int symbol;
5755
5756               /* On SVR4-ish systems, the dynamic loader cannot
5757                  relocate the text and data segments independently,
5758                  so the symbol does not matter.  */
5759               symbol = 0;
5760               relocate = globals->no_apply_dynamic_relocs ? FALSE : TRUE;
5761               outrel.r_info = ELFNN_R_INFO (symbol, AARCH64_R (RELATIVE));
5762               outrel.r_addend += value;
5763             }
5764
5765           sreloc = elf_section_data (input_section)->sreloc;
5766           if (sreloc == NULL || sreloc->contents == NULL)
5767             return bfd_reloc_notsupported;
5768
5769           loc = sreloc->contents + sreloc->reloc_count++ * RELOC_SIZE (globals);
5770           bfd_elfNN_swap_reloca_out (output_bfd, &outrel, loc);
5771
5772           if (sreloc->reloc_count * RELOC_SIZE (globals) > sreloc->size)
5773             {
5774               /* Sanity to check that we have previously allocated
5775                  sufficient space in the relocation section for the
5776                  number of relocations we actually want to emit.  */
5777               abort ();
5778             }
5779
5780           /* If this reloc is against an external symbol, we do not want to
5781              fiddle with the addend.  Otherwise, we need to include the symbol
5782              value so that it becomes an addend for the dynamic reloc.  */
5783           if (!relocate)
5784             return bfd_reloc_ok;
5785
5786           return _bfd_final_link_relocate (howto, input_bfd, input_section,
5787                                            contents, rel->r_offset, value,
5788                                            signed_addend);
5789         }
5790       else
5791         value += signed_addend;
5792       break;
5793
5794     case BFD_RELOC_AARCH64_CALL26:
5795     case BFD_RELOC_AARCH64_JUMP26:
5796       {
5797         asection *splt = globals->root.splt;
5798         bfd_boolean via_plt_p =
5799           splt != NULL && h != NULL && h->plt.offset != (bfd_vma) - 1;
5800
5801         /* A call to an undefined weak symbol is converted to a jump to
5802            the next instruction unless a PLT entry will be created.
5803            The jump to the next instruction is optimized as a NOP.
5804            Do the same for local undefined symbols.  */
5805         if (weak_undef_p && ! via_plt_p)
5806           {
5807             bfd_putl32 (INSN_NOP, hit_data);
5808             return bfd_reloc_ok;
5809           }
5810
5811         /* If the call goes through a PLT entry, make sure to
5812            check distance to the right destination address.  */
5813         if (via_plt_p)
5814           value = (splt->output_section->vma
5815                    + splt->output_offset + h->plt.offset);
5816
5817         /* Check if a stub has to be inserted because the destination
5818            is too far away.  */
5819         struct elf_aarch64_stub_hash_entry *stub_entry = NULL;
5820
5821         /* If the branch destination is directed to plt stub, "value" will be
5822            the final destination, otherwise we should plus signed_addend, it may
5823            contain non-zero value, for example call to local function symbol
5824            which are turned into "sec_sym + sec_off", and sec_off is kept in
5825            signed_addend.  */
5826         if (! aarch64_valid_branch_p (via_plt_p ? value : value + signed_addend,
5827                                       place))
5828           /* The target is out of reach, so redirect the branch to
5829              the local stub for this function.  */
5830         stub_entry = elfNN_aarch64_get_stub_entry (input_section, sym_sec, h,
5831                                                    rel, globals);
5832         if (stub_entry != NULL)
5833           {
5834             value = (stub_entry->stub_offset
5835                      + stub_entry->stub_sec->output_offset
5836                      + stub_entry->stub_sec->output_section->vma);
5837
5838             /* We have redirected the destination to stub entry address,
5839                so ignore any addend record in the original rela entry.  */
5840             signed_addend = 0;
5841           }
5842       }
5843       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5844                                                    signed_addend, weak_undef_p);
5845       *unresolved_reloc_p = FALSE;
5846       break;
5847
5848     case BFD_RELOC_AARCH64_16_PCREL:
5849     case BFD_RELOC_AARCH64_32_PCREL:
5850     case BFD_RELOC_AARCH64_64_PCREL:
5851     case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
5852     case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
5853     case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
5854     case BFD_RELOC_AARCH64_LD_LO19_PCREL:
5855     case BFD_RELOC_AARCH64_MOVW_PREL_G0:
5856     case BFD_RELOC_AARCH64_MOVW_PREL_G0_NC:
5857     case BFD_RELOC_AARCH64_MOVW_PREL_G1:
5858     case BFD_RELOC_AARCH64_MOVW_PREL_G1_NC:
5859     case BFD_RELOC_AARCH64_MOVW_PREL_G2:
5860     case BFD_RELOC_AARCH64_MOVW_PREL_G2_NC:
5861     case BFD_RELOC_AARCH64_MOVW_PREL_G3:
5862       if (bfd_link_pic (info)
5863           && (input_section->flags & SEC_ALLOC) != 0
5864           && (input_section->flags & SEC_READONLY) != 0
5865           && !SYMBOL_REFERENCES_LOCAL (info, h))
5866         {
5867           int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
5868
5869           _bfd_error_handler
5870             /* xgettext:c-format */
5871             (_("%pB: relocation %s against symbol `%s' which may bind "
5872                "externally can not be used when making a shared object; "
5873                "recompile with -fPIC"),
5874              input_bfd, elfNN_aarch64_howto_table[howto_index].name,
5875              h->root.root.string);
5876           bfd_set_error (bfd_error_bad_value);
5877           return bfd_reloc_notsupported;
5878         }
5879       /* Fall through.  */
5880
5881     case BFD_RELOC_AARCH64_16:
5882 #if ARCH_SIZE == 64
5883     case BFD_RELOC_AARCH64_32:
5884 #endif
5885     case BFD_RELOC_AARCH64_ADD_LO12:
5886     case BFD_RELOC_AARCH64_BRANCH19:
5887     case BFD_RELOC_AARCH64_LDST128_LO12:
5888     case BFD_RELOC_AARCH64_LDST16_LO12:
5889     case BFD_RELOC_AARCH64_LDST32_LO12:
5890     case BFD_RELOC_AARCH64_LDST64_LO12:
5891     case BFD_RELOC_AARCH64_LDST8_LO12:
5892     case BFD_RELOC_AARCH64_MOVW_G0:
5893     case BFD_RELOC_AARCH64_MOVW_G0_NC:
5894     case BFD_RELOC_AARCH64_MOVW_G0_S:
5895     case BFD_RELOC_AARCH64_MOVW_G1:
5896     case BFD_RELOC_AARCH64_MOVW_G1_NC:
5897     case BFD_RELOC_AARCH64_MOVW_G1_S:
5898     case BFD_RELOC_AARCH64_MOVW_G2:
5899     case BFD_RELOC_AARCH64_MOVW_G2_NC:
5900     case BFD_RELOC_AARCH64_MOVW_G2_S:
5901     case BFD_RELOC_AARCH64_MOVW_G3:
5902     case BFD_RELOC_AARCH64_TSTBR14:
5903       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5904                                                    signed_addend, weak_undef_p);
5905       break;
5906
5907     case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
5908     case BFD_RELOC_AARCH64_GOT_LD_PREL19:
5909     case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
5910     case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
5911     case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
5912     case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
5913     case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
5914     case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
5915     case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
5916       if (globals->root.sgot == NULL)
5917         BFD_ASSERT (h != NULL);
5918
5919       relative_reloc = FALSE;
5920       if (h != NULL)
5921         {
5922           bfd_vma addend = 0;
5923
5924           /* If a symbol is not dynamic and is not undefined weak, bind it
5925              locally and generate a RELATIVE relocation under PIC mode.
5926
5927              NOTE: one symbol may be referenced by several relocations, we
5928              should only generate one RELATIVE relocation for that symbol.
5929              Therefore, check GOT offset mark first.  */
5930           if (h->dynindx == -1
5931               && !h->forced_local
5932               && h->root.type != bfd_link_hash_undefweak
5933               && bfd_link_pic (info)
5934               && !symbol_got_offset_mark_p (input_bfd, h, r_symndx))
5935             relative_reloc = TRUE;
5936
5937           value = aarch64_calculate_got_entry_vma (h, globals, info, value,
5938                                                    output_bfd,
5939                                                    unresolved_reloc_p);
5940           /* Record the GOT entry address which will be used when generating
5941              RELATIVE relocation.  */
5942           if (relative_reloc)
5943             got_entry_addr = value;
5944
5945           if (aarch64_relocation_aginst_gp_p (bfd_r_type))
5946             addend = (globals->root.sgot->output_section->vma
5947                       + globals->root.sgot->output_offset);
5948           value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5949                                                        addend, weak_undef_p);
5950         }
5951       else
5952       {
5953         bfd_vma addend = 0;
5954         struct elf_aarch64_local_symbol *locals
5955           = elf_aarch64_locals (input_bfd);
5956
5957         if (locals == NULL)
5958           {
5959             int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
5960             _bfd_error_handler
5961               /* xgettext:c-format */
5962               (_("%pB: local symbol descriptor table be NULL when applying "
5963                  "relocation %s against local symbol"),
5964                input_bfd, elfNN_aarch64_howto_table[howto_index].name);
5965             abort ();
5966           }
5967
5968         off = symbol_got_offset (input_bfd, h, r_symndx);
5969         base_got = globals->root.sgot;
5970         got_entry_addr = (base_got->output_section->vma
5971                           + base_got->output_offset + off);
5972
5973         if (!symbol_got_offset_mark_p (input_bfd, h, r_symndx))
5974           {
5975             bfd_put_64 (output_bfd, value, base_got->contents + off);
5976
5977             /* For local symbol, we have done absolute relocation in static
5978                linking stage.  While for shared library, we need to update the
5979                content of GOT entry according to the shared object's runtime
5980                base address.  So, we need to generate a R_AARCH64_RELATIVE reloc
5981                for dynamic linker.  */
5982             if (bfd_link_pic (info))
5983               relative_reloc = TRUE;
5984
5985             symbol_got_offset_mark (input_bfd, h, r_symndx);
5986           }
5987
5988         /* Update the relocation value to GOT entry addr as we have transformed
5989            the direct data access into indirect data access through GOT.  */
5990         value = got_entry_addr;
5991
5992         if (aarch64_relocation_aginst_gp_p (bfd_r_type))
5993           addend = base_got->output_section->vma + base_got->output_offset;
5994
5995         value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
5996                                                      addend, weak_undef_p);
5997       }
5998
5999       if (relative_reloc)
6000         {
6001           asection *s;
6002           Elf_Internal_Rela outrel;
6003
6004           s = globals->root.srelgot;
6005           if (s == NULL)
6006             abort ();
6007
6008           outrel.r_offset = got_entry_addr;
6009           outrel.r_info = ELFNN_R_INFO (0, AARCH64_R (RELATIVE));
6010           outrel.r_addend = orig_value;
6011           elf_append_rela (output_bfd, s, &outrel);
6012         }
6013       break;
6014
6015     case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
6016     case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
6017     case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
6018     case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6019     case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
6020     case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
6021     case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
6022     case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
6023     case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
6024     case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
6025       if (globals->root.sgot == NULL)
6026         return bfd_reloc_notsupported;
6027
6028       value = (symbol_got_offset (input_bfd, h, r_symndx)
6029                + globals->root.sgot->output_section->vma
6030                + globals->root.sgot->output_offset);
6031
6032       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
6033                                                    0, weak_undef_p);
6034       *unresolved_reloc_p = FALSE;
6035       break;
6036
6037     case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
6038     case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
6039     case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
6040     case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
6041       if (globals->root.sgot == NULL)
6042         return bfd_reloc_notsupported;
6043
6044       value = symbol_got_offset (input_bfd, h, r_symndx);
6045       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
6046                                                    0, weak_undef_p);
6047       *unresolved_reloc_p = FALSE;
6048       break;
6049
6050     case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12:
6051     case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12:
6052     case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
6053     case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12:
6054     case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC:
6055     case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12:
6056     case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC:
6057     case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12:
6058     case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC:
6059     case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12:
6060     case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC:
6061     case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0:
6062     case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6063     case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1:
6064     case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC:
6065     case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2:
6066       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
6067                                                    signed_addend - dtpoff_base (info),
6068                                                    weak_undef_p);
6069       break;
6070
6071     case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
6072     case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
6073     case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
6074     case BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12:
6075     case BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
6076     case BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12:
6077     case BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
6078     case BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12:
6079     case BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
6080     case BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12:
6081     case BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
6082     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
6083     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
6084     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
6085     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6086     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
6087       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
6088                                                    signed_addend - tpoff_base (info),
6089                                                    weak_undef_p);
6090       *unresolved_reloc_p = FALSE;
6091       break;
6092
6093     case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
6094     case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
6095     case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
6096     case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
6097     case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12:
6098     case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
6099       if (globals->root.sgot == NULL)
6100         return bfd_reloc_notsupported;
6101       value = (symbol_tlsdesc_got_offset (input_bfd, h, r_symndx)
6102                + globals->root.sgotplt->output_section->vma
6103                + globals->root.sgotplt->output_offset
6104                + globals->sgotplt_jump_table_size);
6105
6106       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
6107                                                    0, weak_undef_p);
6108       *unresolved_reloc_p = FALSE;
6109       break;
6110
6111     case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
6112     case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
6113       if (globals->root.sgot == NULL)
6114         return bfd_reloc_notsupported;
6115
6116       value = (symbol_tlsdesc_got_offset (input_bfd, h, r_symndx)
6117                + globals->root.sgotplt->output_section->vma
6118                + globals->root.sgotplt->output_offset
6119                + globals->sgotplt_jump_table_size);
6120
6121       value -= (globals->root.sgot->output_section->vma
6122                 + globals->root.sgot->output_offset);
6123
6124       value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
6125                                                    0, weak_undef_p);
6126       *unresolved_reloc_p = FALSE;
6127       break;
6128
6129     default:
6130       return bfd_reloc_notsupported;
6131     }
6132
6133   if (saved_addend)
6134     *saved_addend = value;
6135
6136   /* Only apply the final relocation in a sequence.  */
6137   if (save_addend)
6138     return bfd_reloc_continue;
6139
6140   return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type,
6141                                       howto, value);
6142 }
6143
6144 /* LP64 and ILP32 operates on x- and w-registers respectively.
6145    Next definitions take into account the difference between
6146    corresponding machine codes. R means x-register if the target
6147    arch is LP64, and w-register if the target is ILP32.  */
6148
6149 #if ARCH_SIZE == 64
6150 # define add_R0_R0      (0x91000000)
6151 # define add_R0_R0_R1   (0x8b000020)
6152 # define add_R0_R1      (0x91400020)
6153 # define ldr_R0         (0x58000000)
6154 # define ldr_R0_mask(i) (i & 0xffffffe0)
6155 # define ldr_R0_x0      (0xf9400000)
6156 # define ldr_hw_R0      (0xf2a00000)
6157 # define movk_R0        (0xf2800000)
6158 # define movz_R0        (0xd2a00000)
6159 # define movz_hw_R0     (0xd2c00000)
6160 #else /*ARCH_SIZE == 32 */
6161 # define add_R0_R0      (0x11000000)
6162 # define add_R0_R0_R1   (0x0b000020)
6163 # define add_R0_R1      (0x11400020)
6164 # define ldr_R0         (0x18000000)
6165 # define ldr_R0_mask(i) (i & 0xbfffffe0)
6166 # define ldr_R0_x0      (0xb9400000)
6167 # define ldr_hw_R0      (0x72a00000)
6168 # define movk_R0        (0x72800000)
6169 # define movz_R0        (0x52a00000)
6170 # define movz_hw_R0     (0x52c00000)
6171 #endif
6172
6173 /* Structure to hold payload for _bfd_aarch64_erratum_843419_clear_stub,
6174    it is used to identify the stub information to reset.  */
6175
6176 struct erratum_843419_branch_to_stub_clear_data
6177 {
6178   bfd_vma adrp_offset;
6179   asection *output_section;
6180 };
6181
6182 /* Clear the erratum information for GEN_ENTRY if the ADRP_OFFSET and
6183    section inside IN_ARG matches.  The clearing is done by setting the
6184    stub_type to none.  */
6185
6186 static bfd_boolean
6187 _bfd_aarch64_erratum_843419_clear_stub (struct bfd_hash_entry *gen_entry,
6188                                         void *in_arg)
6189 {
6190   struct elf_aarch64_stub_hash_entry *stub_entry
6191     = (struct elf_aarch64_stub_hash_entry *) gen_entry;
6192   struct erratum_843419_branch_to_stub_clear_data *data
6193     = (struct erratum_843419_branch_to_stub_clear_data *) in_arg;
6194
6195   if (stub_entry->target_section != data->output_section
6196       || stub_entry->stub_type != aarch64_stub_erratum_843419_veneer
6197       || stub_entry->adrp_offset != data->adrp_offset)
6198     return TRUE;
6199
6200   /* Change the stub type instead of removing the entry, removing from the hash
6201      table would be slower and we have already reserved the memory for the entry
6202      so there wouldn't be much gain.  Changing the stub also keeps around a
6203      record of what was there before.  */
6204   stub_entry->stub_type = aarch64_stub_none;
6205
6206   /* We're done and there could have been only one matching stub at that
6207      particular offset, so abort further traversal.  */
6208   return FALSE;
6209 }
6210
6211 /* TLS Relaxations may relax an adrp sequence that matches the erratum 843419
6212    sequence.  In this case the erratum no longer applies and we need to remove
6213    the entry from the pending stub generation.  This clears matching adrp insn
6214    at ADRP_OFFSET in INPUT_SECTION in the stub table defined in GLOBALS.  */
6215
6216 static void
6217 clear_erratum_843419_entry (struct elf_aarch64_link_hash_table *globals,
6218                             bfd_vma adrp_offset, asection *input_section)
6219 {
6220   if (globals->fix_erratum_843419 & ERRAT_ADRP)
6221     {
6222       struct erratum_843419_branch_to_stub_clear_data data;
6223       data.adrp_offset = adrp_offset;
6224       data.output_section = input_section;
6225
6226       bfd_hash_traverse (&globals->stub_hash_table,
6227                          _bfd_aarch64_erratum_843419_clear_stub, &data);
6228     }
6229 }
6230
6231 /* Handle TLS relaxations.  Relaxing is possible for symbols that use
6232    R_AARCH64_TLSDESC_ADR_{PAGE, LD64_LO12_NC, ADD_LO12_NC} during a static
6233    link.
6234
6235    Return bfd_reloc_ok if we're done, bfd_reloc_continue if the caller
6236    is to then call final_link_relocate.  Return other values in the
6237    case of error.  */
6238
6239 static bfd_reloc_status_type
6240 elfNN_aarch64_tls_relax (struct elf_aarch64_link_hash_table *globals,
6241                          bfd *input_bfd, asection *input_section,
6242                          bfd_byte *contents, Elf_Internal_Rela *rel,
6243                          struct elf_link_hash_entry *h)
6244 {
6245   bfd_boolean is_local = h == NULL;
6246   unsigned int r_type = ELFNN_R_TYPE (rel->r_info);
6247   unsigned long insn;
6248
6249   BFD_ASSERT (globals && input_bfd && contents && rel);
6250
6251   switch (elfNN_aarch64_bfd_reloc_from_type (input_bfd, r_type))
6252     {
6253     case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
6254     case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
6255       if (is_local)
6256         {
6257           /* GD->LE relaxation:
6258              adrp x0, :tlsgd:var     =>   movz R0, :tprel_g1:var
6259              or
6260              adrp x0, :tlsdesc:var   =>   movz R0, :tprel_g1:var
6261
6262              Where R is x for LP64, and w for ILP32.  */
6263           bfd_putl32 (movz_R0, contents + rel->r_offset);
6264           /* We have relaxed the adrp into a mov, we may have to clear any
6265              pending erratum fixes.  */
6266           clear_erratum_843419_entry (globals, rel->r_offset, input_section);
6267           return bfd_reloc_continue;
6268         }
6269       else
6270         {
6271           /* GD->IE relaxation:
6272              adrp x0, :tlsgd:var     =>   adrp x0, :gottprel:var
6273              or
6274              adrp x0, :tlsdesc:var   =>   adrp x0, :gottprel:var
6275            */
6276           return bfd_reloc_continue;
6277         }
6278
6279     case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
6280       BFD_ASSERT (0);
6281       break;
6282
6283     case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
6284       if (is_local)
6285         {
6286           /* Tiny TLSDESC->LE relaxation:
6287              ldr   x1, :tlsdesc:var      =>  movz  R0, #:tprel_g1:var
6288              adr   x0, :tlsdesc:var      =>  movk  R0, #:tprel_g0_nc:var
6289              .tlsdesccall var
6290              blr   x1                    =>  nop
6291
6292              Where R is x for LP64, and w for ILP32.  */
6293           BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (TLSDESC_ADR_PREL21));
6294           BFD_ASSERT (ELFNN_R_TYPE (rel[2].r_info) == AARCH64_R (TLSDESC_CALL));
6295
6296           rel[1].r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
6297                                         AARCH64_R (TLSLE_MOVW_TPREL_G0_NC));
6298           rel[2].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
6299
6300           bfd_putl32 (movz_R0, contents + rel->r_offset);
6301           bfd_putl32 (movk_R0, contents + rel->r_offset + 4);
6302           bfd_putl32 (INSN_NOP, contents + rel->r_offset + 8);
6303           return bfd_reloc_continue;
6304         }
6305       else
6306         {
6307           /* Tiny TLSDESC->IE relaxation:
6308              ldr   x1, :tlsdesc:var      =>  ldr   x0, :gottprel:var
6309              adr   x0, :tlsdesc:var      =>  nop
6310              .tlsdesccall var
6311              blr   x1                    =>  nop
6312            */
6313           BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (TLSDESC_ADR_PREL21));
6314           BFD_ASSERT (ELFNN_R_TYPE (rel[2].r_info) == AARCH64_R (TLSDESC_CALL));
6315
6316           rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
6317           rel[2].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
6318
6319           bfd_putl32 (ldr_R0, contents + rel->r_offset);
6320           bfd_putl32 (INSN_NOP, contents + rel->r_offset + 4);
6321           bfd_putl32 (INSN_NOP, contents + rel->r_offset + 8);
6322           return bfd_reloc_continue;
6323         }
6324
6325     case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
6326       if (is_local)
6327         {
6328           /* Tiny GD->LE relaxation:
6329              adr x0, :tlsgd:var      =>   mrs  x1, tpidr_el0
6330              bl   __tls_get_addr     =>   add  R0, R1, #:tprel_hi12:x, lsl #12
6331              nop                     =>   add  R0, R0, #:tprel_lo12_nc:x
6332
6333              Where R is x for LP64, and x for Ilp32.  */
6334
6335           /* First kill the tls_get_addr reloc on the bl instruction.  */
6336           BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
6337
6338           bfd_putl32 (0xd53bd041, contents + rel->r_offset + 0);
6339           bfd_putl32 (add_R0_R1, contents + rel->r_offset + 4);
6340           bfd_putl32 (add_R0_R0, contents + rel->r_offset + 8);
6341
6342           rel[1].r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
6343                                         AARCH64_R (TLSLE_ADD_TPREL_LO12_NC));
6344           rel[1].r_offset = rel->r_offset + 8;
6345
6346           /* Move the current relocation to the second instruction in
6347              the sequence.  */
6348           rel->r_offset += 4;
6349           rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
6350                                       AARCH64_R (TLSLE_ADD_TPREL_HI12));
6351           return bfd_reloc_continue;
6352         }
6353       else
6354         {
6355           /* Tiny GD->IE relaxation:
6356              adr x0, :tlsgd:var      =>   ldr  R0, :gottprel:var
6357              bl   __tls_get_addr     =>   mrs  x1, tpidr_el0
6358              nop                     =>   add  R0, R0, R1
6359
6360              Where R is x for LP64, and w for Ilp32.  */
6361
6362           /* First kill the tls_get_addr reloc on the bl instruction.  */
6363           BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
6364           rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
6365
6366           bfd_putl32 (ldr_R0, contents + rel->r_offset);
6367           bfd_putl32 (0xd53bd041, contents + rel->r_offset + 4);
6368           bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 8);
6369           return bfd_reloc_continue;
6370         }
6371
6372 #if ARCH_SIZE == 64
6373     case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
6374       BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (TLSGD_MOVW_G0_NC));
6375       BFD_ASSERT (rel->r_offset + 12 == rel[2].r_offset);
6376       BFD_ASSERT (ELFNN_R_TYPE (rel[2].r_info) == AARCH64_R (CALL26));
6377
6378       if (is_local)
6379         {
6380           /* Large GD->LE relaxation:
6381              movz x0, #:tlsgd_g1:var    => movz x0, #:tprel_g2:var, lsl #32
6382              movk x0, #:tlsgd_g0_nc:var => movk x0, #:tprel_g1_nc:var, lsl #16
6383              add x0, gp, x0             => movk x0, #:tprel_g0_nc:var
6384              bl __tls_get_addr          => mrs x1, tpidr_el0
6385              nop                        => add x0, x0, x1
6386            */
6387           rel[2].r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
6388                                         AARCH64_R (TLSLE_MOVW_TPREL_G0_NC));
6389           rel[2].r_offset = rel->r_offset + 8;
6390
6391           bfd_putl32 (movz_hw_R0, contents + rel->r_offset + 0);
6392           bfd_putl32 (ldr_hw_R0, contents + rel->r_offset + 4);
6393           bfd_putl32 (movk_R0, contents + rel->r_offset + 8);
6394           bfd_putl32 (0xd53bd041, contents + rel->r_offset + 12);
6395           bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 16);
6396         }
6397       else
6398         {
6399           /* Large GD->IE relaxation:
6400              movz x0, #:tlsgd_g1:var    => movz x0, #:gottprel_g1:var, lsl #16
6401              movk x0, #:tlsgd_g0_nc:var => movk x0, #:gottprel_g0_nc:var
6402              add x0, gp, x0             => ldr x0, [gp, x0]
6403              bl __tls_get_addr          => mrs x1, tpidr_el0
6404              nop                        => add x0, x0, x1
6405            */
6406           rel[2].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
6407           bfd_putl32 (0xd2a80000, contents + rel->r_offset + 0);
6408           bfd_putl32 (ldr_R0, contents + rel->r_offset + 8);
6409           bfd_putl32 (0xd53bd041, contents + rel->r_offset + 12);
6410           bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 16);
6411         }
6412       return bfd_reloc_continue;
6413
6414     case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
6415       return bfd_reloc_continue;
6416 #endif
6417
6418     case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
6419       return bfd_reloc_continue;
6420
6421     case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
6422       if (is_local)
6423         {
6424           /* GD->LE relaxation:
6425              ldr xd, [x0, #:tlsdesc_lo12:var]   =>   movk x0, :tprel_g0_nc:var
6426
6427              Where R is x for lp64 mode, and w for ILP32 mode.  */
6428           bfd_putl32 (movk_R0, contents + rel->r_offset);
6429           return bfd_reloc_continue;
6430         }
6431       else
6432         {
6433           /* GD->IE relaxation:
6434              ldr xd, [x0, #:tlsdesc_lo12:var] => ldr R0, [x0, #:gottprel_lo12:var]
6435
6436              Where R is x for lp64 mode, and w for ILP32 mode.  */
6437           insn = bfd_getl32 (contents + rel->r_offset);
6438           bfd_putl32 (ldr_R0_mask (insn), contents + rel->r_offset);
6439           return bfd_reloc_continue;
6440         }
6441
6442     case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
6443       if (is_local)
6444         {
6445           /* GD->LE relaxation
6446              add  x0, #:tlsgd_lo12:var  => movk R0, :tprel_g0_nc:var
6447              bl   __tls_get_addr        => mrs  x1, tpidr_el0
6448              nop                        => add  R0, R1, R0
6449
6450              Where R is x for lp64 mode, and w for ILP32 mode.  */
6451
6452           /* First kill the tls_get_addr reloc on the bl instruction.  */
6453           BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
6454           rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
6455
6456           bfd_putl32 (movk_R0, contents + rel->r_offset);
6457           bfd_putl32 (0xd53bd041, contents + rel->r_offset + 4);
6458           bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 8);
6459           return bfd_reloc_continue;
6460         }
6461       else
6462         {
6463           /* GD->IE relaxation
6464              ADD  x0, #:tlsgd_lo12:var  => ldr  R0, [x0, #:gottprel_lo12:var]
6465              BL   __tls_get_addr        => mrs  x1, tpidr_el0
6466                R_AARCH64_CALL26
6467              NOP                        => add  R0, R1, R0
6468
6469              Where R is x for lp64 mode, and w for ilp32 mode.  */
6470
6471           BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
6472
6473           /* Remove the relocation on the BL instruction.  */
6474           rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
6475
6476           /* We choose to fixup the BL and NOP instructions using the
6477              offset from the second relocation to allow flexibility in
6478              scheduling instructions between the ADD and BL.  */
6479           bfd_putl32 (ldr_R0_x0, contents + rel->r_offset);
6480           bfd_putl32 (0xd53bd041, contents + rel[1].r_offset);
6481           bfd_putl32 (add_R0_R0_R1, contents + rel[1].r_offset + 4);
6482           return bfd_reloc_continue;
6483         }
6484
6485     case BFD_RELOC_AARCH64_TLSDESC_ADD:
6486     case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
6487     case BFD_RELOC_AARCH64_TLSDESC_CALL:
6488       /* GD->IE/LE relaxation:
6489          add x0, x0, #:tlsdesc_lo12:var   =>   nop
6490          blr xd                           =>   nop
6491        */
6492       bfd_putl32 (INSN_NOP, contents + rel->r_offset);
6493       return bfd_reloc_ok;
6494
6495     case BFD_RELOC_AARCH64_TLSDESC_LDR:
6496       if (is_local)
6497         {
6498           /* GD->LE relaxation:
6499              ldr xd, [gp, xn]   =>   movk R0, #:tprel_g0_nc:var
6500
6501              Where R is x for lp64 mode, and w for ILP32 mode.  */
6502           bfd_putl32 (movk_R0, contents + rel->r_offset);
6503           return bfd_reloc_continue;
6504         }
6505       else
6506         {
6507           /* GD->IE relaxation:
6508              ldr xd, [gp, xn]   =>   ldr R0, [gp, xn]
6509
6510              Where R is x for lp64 mode, and w for ILP32 mode.  */
6511           insn = bfd_getl32 (contents + rel->r_offset);
6512           bfd_putl32 (ldr_R0_mask (insn), contents + rel->r_offset);
6513           return bfd_reloc_ok;
6514         }
6515
6516     case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
6517       /* GD->LE relaxation:
6518          movk xd, #:tlsdesc_off_g0_nc:var => movk R0, #:tprel_g1_nc:var, lsl #16
6519          GD->IE relaxation:
6520          movk xd, #:tlsdesc_off_g0_nc:var => movk Rd, #:gottprel_g0_nc:var
6521
6522          Where R is x for lp64 mode, and w for ILP32 mode.  */
6523       if (is_local)
6524         bfd_putl32 (ldr_hw_R0, contents + rel->r_offset);
6525       return bfd_reloc_continue;
6526
6527     case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
6528       if (is_local)
6529         {
6530           /* GD->LE relaxation:
6531              movz xd, #:tlsdesc_off_g1:var => movz R0, #:tprel_g2:var, lsl #32
6532
6533              Where R is x for lp64 mode, and w for ILP32 mode.  */
6534           bfd_putl32 (movz_hw_R0, contents + rel->r_offset);
6535           return bfd_reloc_continue;
6536         }
6537       else
6538         {
6539           /*  GD->IE relaxation:
6540               movz xd, #:tlsdesc_off_g1:var => movz Rd, #:gottprel_g1:var, lsl #16
6541
6542              Where R is x for lp64 mode, and w for ILP32 mode.  */
6543           insn = bfd_getl32 (contents + rel->r_offset);
6544           bfd_putl32 (movz_R0 | (insn & 0x1f), contents + rel->r_offset);
6545           return bfd_reloc_continue;
6546         }
6547
6548     case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6549       /* IE->LE relaxation:
6550          adrp xd, :gottprel:var   =>   movz Rd, :tprel_g1:var
6551
6552          Where R is x for lp64 mode, and w for ILP32 mode.  */
6553       if (is_local)
6554         {
6555           insn = bfd_getl32 (contents + rel->r_offset);
6556           bfd_putl32 (movz_R0 | (insn & 0x1f), contents + rel->r_offset);
6557           /* We have relaxed the adrp into a mov, we may have to clear any
6558              pending erratum fixes.  */
6559           clear_erratum_843419_entry (globals, rel->r_offset, input_section);
6560         }
6561       return bfd_reloc_continue;
6562
6563     case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
6564       /* IE->LE relaxation:
6565          ldr  xd, [xm, #:gottprel_lo12:var]   =>   movk Rd, :tprel_g0_nc:var
6566
6567          Where R is x for lp64 mode, and w for ILP32 mode.  */
6568       if (is_local)
6569         {
6570           insn = bfd_getl32 (contents + rel->r_offset);
6571           bfd_putl32 (movk_R0 | (insn & 0x1f), contents + rel->r_offset);
6572         }
6573       return bfd_reloc_continue;
6574
6575     case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
6576       /* LD->LE relaxation (tiny):
6577          adr  x0, :tlsldm:x  => mrs x0, tpidr_el0
6578          bl   __tls_get_addr => add R0, R0, TCB_SIZE
6579
6580          Where R is x for lp64 mode, and w for ilp32 mode.  */
6581       if (is_local)
6582         {
6583           BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
6584           BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
6585           /* No need of CALL26 relocation for tls_get_addr.  */
6586           rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
6587           bfd_putl32 (0xd53bd040, contents + rel->r_offset + 0);
6588           bfd_putl32 (add_R0_R0 | (TCB_SIZE << 10),
6589                       contents + rel->r_offset + 4);
6590           return bfd_reloc_ok;
6591         }
6592       return bfd_reloc_continue;
6593
6594     case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
6595       /* LD->LE relaxation (small):
6596          adrp  x0, :tlsldm:x       => mrs x0, tpidr_el0
6597        */
6598       if (is_local)
6599         {
6600           bfd_putl32 (0xd53bd040, contents + rel->r_offset);
6601           return bfd_reloc_ok;
6602         }
6603       return bfd_reloc_continue;
6604
6605     case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
6606       /* LD->LE relaxation (small):
6607          add   x0, #:tlsldm_lo12:x => add R0, R0, TCB_SIZE
6608          bl   __tls_get_addr       => nop
6609
6610          Where R is x for lp64 mode, and w for ilp32 mode.  */
6611       if (is_local)
6612         {
6613           BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
6614           BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
6615           /* No need of CALL26 relocation for tls_get_addr.  */
6616           rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
6617           bfd_putl32 (add_R0_R0 | (TCB_SIZE << 10),
6618                       contents + rel->r_offset + 0);
6619           bfd_putl32 (INSN_NOP, contents + rel->r_offset + 4);
6620           return bfd_reloc_ok;
6621         }
6622       return bfd_reloc_continue;
6623
6624     default:
6625       return bfd_reloc_continue;
6626     }
6627
6628   return bfd_reloc_ok;
6629 }
6630
6631 /* Relocate an AArch64 ELF section.  */
6632
6633 static bfd_boolean
6634 elfNN_aarch64_relocate_section (bfd *output_bfd,
6635                                 struct bfd_link_info *info,
6636                                 bfd *input_bfd,
6637                                 asection *input_section,
6638                                 bfd_byte *contents,
6639                                 Elf_Internal_Rela *relocs,
6640                                 Elf_Internal_Sym *local_syms,
6641                                 asection **local_sections)
6642 {
6643   Elf_Internal_Shdr *symtab_hdr;
6644   struct elf_link_hash_entry **sym_hashes;
6645   Elf_Internal_Rela *rel;
6646   Elf_Internal_Rela *relend;
6647   const char *name;
6648   struct elf_aarch64_link_hash_table *globals;
6649   bfd_boolean save_addend = FALSE;
6650   bfd_vma addend = 0;
6651
6652   globals = elf_aarch64_hash_table (info);
6653
6654   symtab_hdr = &elf_symtab_hdr (input_bfd);
6655   sym_hashes = elf_sym_hashes (input_bfd);
6656
6657   rel = relocs;
6658   relend = relocs + input_section->reloc_count;
6659   for (; rel < relend; rel++)
6660     {
6661       unsigned int r_type;
6662       bfd_reloc_code_real_type bfd_r_type;
6663       bfd_reloc_code_real_type relaxed_bfd_r_type;
6664       reloc_howto_type *howto;
6665       unsigned long r_symndx;
6666       Elf_Internal_Sym *sym;
6667       asection *sec;
6668       struct elf_link_hash_entry *h;
6669       bfd_vma relocation;
6670       bfd_reloc_status_type r;
6671       arelent bfd_reloc;
6672       char sym_type;
6673       bfd_boolean unresolved_reloc = FALSE;
6674       char *error_message = NULL;
6675
6676       r_symndx = ELFNN_R_SYM (rel->r_info);
6677       r_type = ELFNN_R_TYPE (rel->r_info);
6678
6679       bfd_reloc.howto = elfNN_aarch64_howto_from_type (input_bfd, r_type);
6680       howto = bfd_reloc.howto;
6681
6682       if (howto == NULL)
6683         return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
6684
6685       bfd_r_type = elfNN_aarch64_bfd_reloc_from_howto (howto);
6686
6687       h = NULL;
6688       sym = NULL;
6689       sec = NULL;
6690
6691       if (r_symndx < symtab_hdr->sh_info)
6692         {
6693           sym = local_syms + r_symndx;
6694           sym_type = ELFNN_ST_TYPE (sym->st_info);
6695           sec = local_sections[r_symndx];
6696
6697           /* An object file might have a reference to a local
6698              undefined symbol.  This is a daft object file, but we
6699              should at least do something about it.  */
6700           if (r_type != R_AARCH64_NONE && r_type != R_AARCH64_NULL
6701               && bfd_is_und_section (sec)
6702               && ELF_ST_BIND (sym->st_info) != STB_WEAK)
6703             (*info->callbacks->undefined_symbol)
6704               (info, bfd_elf_string_from_elf_section
6705                (input_bfd, symtab_hdr->sh_link, sym->st_name),
6706                input_bfd, input_section, rel->r_offset, TRUE);
6707
6708           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
6709
6710           /* Relocate against local STT_GNU_IFUNC symbol.  */
6711           if (!bfd_link_relocatable (info)
6712               && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
6713             {
6714               h = elfNN_aarch64_get_local_sym_hash (globals, input_bfd,
6715                                                     rel, FALSE);
6716               if (h == NULL)
6717                 abort ();
6718
6719               /* Set STT_GNU_IFUNC symbol value.  */
6720               h->root.u.def.value = sym->st_value;
6721               h->root.u.def.section = sec;
6722             }
6723         }
6724       else
6725         {
6726           bfd_boolean warned, ignored;
6727
6728           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
6729                                    r_symndx, symtab_hdr, sym_hashes,
6730                                    h, sec, relocation,
6731                                    unresolved_reloc, warned, ignored);
6732
6733           sym_type = h->type;
6734         }
6735
6736       if (sec != NULL && discarded_section (sec))
6737         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
6738                                          rel, 1, relend, howto, 0, contents);
6739
6740       if (bfd_link_relocatable (info))
6741         continue;
6742
6743       if (h != NULL)
6744         name = h->root.root.string;
6745       else
6746         {
6747           name = (bfd_elf_string_from_elf_section
6748                   (input_bfd, symtab_hdr->sh_link, sym->st_name));
6749           if (name == NULL || *name == '\0')
6750             name = bfd_section_name (input_bfd, sec);
6751         }
6752
6753       if (r_symndx != 0
6754           && r_type != R_AARCH64_NONE
6755           && r_type != R_AARCH64_NULL
6756           && (h == NULL
6757               || h->root.type == bfd_link_hash_defined
6758               || h->root.type == bfd_link_hash_defweak)
6759           && IS_AARCH64_TLS_RELOC (bfd_r_type) != (sym_type == STT_TLS))
6760         {
6761           _bfd_error_handler
6762             ((sym_type == STT_TLS
6763               /* xgettext:c-format */
6764               ? _("%pB(%pA+%#" PRIx64 "): %s used with TLS symbol %s")
6765               /* xgettext:c-format */
6766               : _("%pB(%pA+%#" PRIx64 "): %s used with non-TLS symbol %s")),
6767              input_bfd,
6768              input_section, (uint64_t) rel->r_offset, howto->name, name);
6769         }
6770
6771       /* We relax only if we can see that there can be a valid transition
6772          from a reloc type to another.
6773          We call elfNN_aarch64_final_link_relocate unless we're completely
6774          done, i.e., the relaxation produced the final output we want.  */
6775
6776       relaxed_bfd_r_type = aarch64_tls_transition (input_bfd, info, r_type,
6777                                                    h, r_symndx);
6778       if (relaxed_bfd_r_type != bfd_r_type)
6779         {
6780           bfd_r_type = relaxed_bfd_r_type;
6781           howto = elfNN_aarch64_howto_from_bfd_reloc (bfd_r_type);
6782           BFD_ASSERT (howto != NULL);
6783           r_type = howto->type;
6784           r = elfNN_aarch64_tls_relax (globals, input_bfd, input_section,
6785                                        contents, rel, h);
6786           unresolved_reloc = 0;
6787         }
6788       else
6789         r = bfd_reloc_continue;
6790
6791       /* There may be multiple consecutive relocations for the
6792          same offset.  In that case we are supposed to treat the
6793          output of each relocation as the addend for the next.  */
6794       if (rel + 1 < relend
6795           && rel->r_offset == rel[1].r_offset
6796           && ELFNN_R_TYPE (rel[1].r_info) != R_AARCH64_NONE
6797           && ELFNN_R_TYPE (rel[1].r_info) != R_AARCH64_NULL)
6798         save_addend = TRUE;
6799       else
6800         save_addend = FALSE;
6801
6802       if (r == bfd_reloc_continue)
6803         r = elfNN_aarch64_final_link_relocate (howto, input_bfd, output_bfd,
6804                                                input_section, contents, rel,
6805                                                relocation, info, sec,
6806                                                h, &unresolved_reloc,
6807                                                save_addend, &addend, sym);
6808
6809       switch (elfNN_aarch64_bfd_reloc_from_type (input_bfd, r_type))
6810         {
6811         case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
6812         case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
6813         case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
6814         case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
6815         case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
6816         case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
6817         case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
6818         case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
6819           if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
6820             {
6821               bfd_boolean need_relocs = FALSE;
6822               bfd_byte *loc;
6823               int indx;
6824               bfd_vma off;
6825
6826               off = symbol_got_offset (input_bfd, h, r_symndx);
6827               indx = h && h->dynindx != -1 ? h->dynindx : 0;
6828
6829               need_relocs =
6830                 (!bfd_link_executable (info) || indx != 0) &&
6831                 (h == NULL
6832                  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6833                  || h->root.type != bfd_link_hash_undefweak);
6834
6835               BFD_ASSERT (globals->root.srelgot != NULL);
6836
6837               if (need_relocs)
6838                 {
6839                   Elf_Internal_Rela rela;
6840                   rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLS_DTPMOD));
6841                   rela.r_addend = 0;
6842                   rela.r_offset = globals->root.sgot->output_section->vma +
6843                     globals->root.sgot->output_offset + off;
6844
6845
6846                   loc = globals->root.srelgot->contents;
6847                   loc += globals->root.srelgot->reloc_count++
6848                     * RELOC_SIZE (htab);
6849                   bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
6850
6851                   bfd_reloc_code_real_type real_type =
6852                     elfNN_aarch64_bfd_reloc_from_type (input_bfd, r_type);
6853
6854                   if (real_type == BFD_RELOC_AARCH64_TLSLD_ADR_PREL21
6855                       || real_type == BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21
6856                       || real_type == BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC)
6857                     {
6858                       /* For local dynamic, don't generate DTPREL in any case.
6859                          Initialize the DTPREL slot into zero, so we get module
6860                          base address when invoke runtime TLS resolver.  */
6861                       bfd_put_NN (output_bfd, 0,
6862                                   globals->root.sgot->contents + off
6863                                   + GOT_ENTRY_SIZE);
6864                     }
6865                   else if (indx == 0)
6866                     {
6867                       bfd_put_NN (output_bfd,
6868                                   relocation - dtpoff_base (info),
6869                                   globals->root.sgot->contents + off
6870                                   + GOT_ENTRY_SIZE);
6871                     }
6872                   else
6873                     {
6874                       /* This TLS symbol is global. We emit a
6875                          relocation to fixup the tls offset at load
6876                          time.  */
6877                       rela.r_info =
6878                         ELFNN_R_INFO (indx, AARCH64_R (TLS_DTPREL));
6879                       rela.r_addend = 0;
6880                       rela.r_offset =
6881                         (globals->root.sgot->output_section->vma
6882                          + globals->root.sgot->output_offset + off
6883                          + GOT_ENTRY_SIZE);
6884
6885                       loc = globals->root.srelgot->contents;
6886                       loc += globals->root.srelgot->reloc_count++
6887                         * RELOC_SIZE (globals);
6888                       bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
6889                       bfd_put_NN (output_bfd, (bfd_vma) 0,
6890                                   globals->root.sgot->contents + off
6891                                   + GOT_ENTRY_SIZE);
6892                     }
6893                 }
6894               else
6895                 {
6896                   bfd_put_NN (output_bfd, (bfd_vma) 1,
6897                               globals->root.sgot->contents + off);
6898                   bfd_put_NN (output_bfd,
6899                               relocation - dtpoff_base (info),
6900                               globals->root.sgot->contents + off
6901                               + GOT_ENTRY_SIZE);
6902                 }
6903
6904               symbol_got_offset_mark (input_bfd, h, r_symndx);
6905             }
6906           break;
6907
6908         case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6909         case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
6910         case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
6911         case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
6912         case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
6913           if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
6914             {
6915               bfd_boolean need_relocs = FALSE;
6916               bfd_byte *loc;
6917               int indx;
6918               bfd_vma off;
6919
6920               off = symbol_got_offset (input_bfd, h, r_symndx);
6921
6922               indx = h && h->dynindx != -1 ? h->dynindx : 0;
6923
6924               need_relocs =
6925                 (!bfd_link_executable (info) || indx != 0) &&
6926                 (h == NULL
6927                  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6928                  || h->root.type != bfd_link_hash_undefweak);
6929
6930               BFD_ASSERT (globals->root.srelgot != NULL);
6931
6932               if (need_relocs)
6933                 {
6934                   Elf_Internal_Rela rela;
6935
6936                   if (indx == 0)
6937                     rela.r_addend = relocation - dtpoff_base (info);
6938                   else
6939                     rela.r_addend = 0;
6940
6941                   rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLS_TPREL));
6942                   rela.r_offset = globals->root.sgot->output_section->vma +
6943                     globals->root.sgot->output_offset + off;
6944
6945                   loc = globals->root.srelgot->contents;
6946                   loc += globals->root.srelgot->reloc_count++
6947                     * RELOC_SIZE (htab);
6948
6949                   bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
6950
6951                   bfd_put_NN (output_bfd, rela.r_addend,
6952                               globals->root.sgot->contents + off);
6953                 }
6954               else
6955                 bfd_put_NN (output_bfd, relocation - tpoff_base (info),
6956                             globals->root.sgot->contents + off);
6957
6958               symbol_got_offset_mark (input_bfd, h, r_symndx);
6959             }
6960           break;
6961
6962         case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
6963         case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
6964         case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
6965         case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
6966         case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
6967         case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
6968         case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
6969           if (! symbol_tlsdesc_got_offset_mark_p (input_bfd, h, r_symndx))
6970             {
6971               bfd_boolean need_relocs = FALSE;
6972               int indx = h && h->dynindx != -1 ? h->dynindx : 0;
6973               bfd_vma off = symbol_tlsdesc_got_offset (input_bfd, h, r_symndx);
6974
6975               need_relocs = (h == NULL
6976                              || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6977                              || h->root.type != bfd_link_hash_undefweak);
6978
6979               BFD_ASSERT (globals->root.srelgot != NULL);
6980               BFD_ASSERT (globals->root.sgot != NULL);
6981
6982               if (need_relocs)
6983                 {
6984                   bfd_byte *loc;
6985                   Elf_Internal_Rela rela;
6986                   rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLSDESC));
6987
6988                   rela.r_addend = 0;
6989                   rela.r_offset = (globals->root.sgotplt->output_section->vma
6990                                    + globals->root.sgotplt->output_offset
6991                                    + off + globals->sgotplt_jump_table_size);
6992
6993                   if (indx == 0)
6994                     rela.r_addend = relocation - dtpoff_base (info);
6995
6996                   /* Allocate the next available slot in the PLT reloc
6997                      section to hold our R_AARCH64_TLSDESC, the next
6998                      available slot is determined from reloc_count,
6999                      which we step. But note, reloc_count was
7000                      artifically moved down while allocating slots for
7001                      real PLT relocs such that all of the PLT relocs
7002                      will fit above the initial reloc_count and the
7003                      extra stuff will fit below.  */
7004                   loc = globals->root.srelplt->contents;
7005                   loc += globals->root.srelplt->reloc_count++
7006                     * RELOC_SIZE (globals);
7007
7008                   bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
7009
7010                   bfd_put_NN (output_bfd, (bfd_vma) 0,
7011                               globals->root.sgotplt->contents + off +
7012                               globals->sgotplt_jump_table_size);
7013                   bfd_put_NN (output_bfd, (bfd_vma) 0,
7014                               globals->root.sgotplt->contents + off +
7015                               globals->sgotplt_jump_table_size +
7016                               GOT_ENTRY_SIZE);
7017                 }
7018
7019               symbol_tlsdesc_got_offset_mark (input_bfd, h, r_symndx);
7020             }
7021           break;
7022         default:
7023           break;
7024         }
7025
7026       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
7027          because such sections are not SEC_ALLOC and thus ld.so will
7028          not process them.  */
7029       if (unresolved_reloc
7030           && !((input_section->flags & SEC_DEBUGGING) != 0
7031                && h->def_dynamic)
7032           && _bfd_elf_section_offset (output_bfd, info, input_section,
7033                                       +rel->r_offset) != (bfd_vma) - 1)
7034         {
7035           _bfd_error_handler
7036             /* xgettext:c-format */
7037             (_("%pB(%pA+%#" PRIx64 "): "
7038                "unresolvable %s relocation against symbol `%s'"),
7039              input_bfd, input_section, (uint64_t) rel->r_offset, howto->name,
7040              h->root.root.string);
7041           return FALSE;
7042         }
7043
7044       if (r != bfd_reloc_ok && r != bfd_reloc_continue)
7045         {
7046           bfd_reloc_code_real_type real_r_type
7047             = elfNN_aarch64_bfd_reloc_from_type (input_bfd, r_type);
7048
7049           switch (r)
7050             {
7051             case bfd_reloc_overflow:
7052               (*info->callbacks->reloc_overflow)
7053                 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
7054                  input_bfd, input_section, rel->r_offset);
7055               if (real_r_type == BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15
7056                   || real_r_type == BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14)
7057                 {
7058                   (*info->callbacks->warning)
7059                     (info,
7060                      _("too many GOT entries for -fpic, "
7061                        "please recompile with -fPIC"),
7062                      name, input_bfd, input_section, rel->r_offset);
7063                   return FALSE;
7064                 }
7065               /* Overflow can occur when a variable is referenced with a type
7066                  that has a larger alignment than the type with which it was
7067                  declared. eg:
7068                    file1.c: extern int foo; int a (void) { return foo; }
7069                    file2.c: char bar, foo, baz;
7070                  If the variable is placed into a data section at an offset
7071                  that is incompatible with the larger alignment requirement
7072                  overflow will occur.  (Strictly speaking this is not overflow
7073                  but rather an alignment problem, but the bfd_reloc_ error
7074                  enum does not have a value to cover that situation).
7075
7076                  Try to catch this situation here and provide a more helpful
7077                  error message to the user.  */
7078               if (addend & ((1 << howto->rightshift) - 1)
7079                   /* FIXME: Are we testing all of the appropriate reloc
7080                      types here ?  */
7081                   && (real_r_type == BFD_RELOC_AARCH64_LD_LO19_PCREL
7082                       || real_r_type == BFD_RELOC_AARCH64_LDST16_LO12
7083                       || real_r_type == BFD_RELOC_AARCH64_LDST32_LO12
7084                       || real_r_type == BFD_RELOC_AARCH64_LDST64_LO12
7085                       || real_r_type == BFD_RELOC_AARCH64_LDST128_LO12))
7086                 {
7087                   info->callbacks->warning
7088                     (info, _("one possible cause of this error is that the \
7089 symbol is being referenced in the indicated code as if it had a larger \
7090 alignment than was declared where it was defined"),
7091                      name, input_bfd, input_section, rel->r_offset);
7092                 }
7093               break;
7094
7095             case bfd_reloc_undefined:
7096               (*info->callbacks->undefined_symbol)
7097                 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
7098               break;
7099
7100             case bfd_reloc_outofrange:
7101               error_message = _("out of range");
7102               goto common_error;
7103
7104             case bfd_reloc_notsupported:
7105               error_message = _("unsupported relocation");
7106               goto common_error;
7107
7108             case bfd_reloc_dangerous:
7109               /* error_message should already be set.  */
7110               goto common_error;
7111
7112             default:
7113               error_message = _("unknown error");
7114               /* Fall through.  */
7115
7116             common_error:
7117               BFD_ASSERT (error_message != NULL);
7118               (*info->callbacks->reloc_dangerous)
7119                 (info, error_message, input_bfd, input_section, rel->r_offset);
7120               break;
7121             }
7122         }
7123
7124       if (!save_addend)
7125         addend = 0;
7126     }
7127
7128   return TRUE;
7129 }
7130
7131 /* Set the right machine number.  */
7132
7133 static bfd_boolean
7134 elfNN_aarch64_object_p (bfd *abfd)
7135 {
7136 #if ARCH_SIZE == 32
7137   bfd_default_set_arch_mach (abfd, bfd_arch_aarch64, bfd_mach_aarch64_ilp32);
7138 #else
7139   bfd_default_set_arch_mach (abfd, bfd_arch_aarch64, bfd_mach_aarch64);
7140 #endif
7141   return TRUE;
7142 }
7143
7144 /* Function to keep AArch64 specific flags in the ELF header.  */
7145
7146 static bfd_boolean
7147 elfNN_aarch64_set_private_flags (bfd *abfd, flagword flags)
7148 {
7149   if (elf_flags_init (abfd) && elf_elfheader (abfd)->e_flags != flags)
7150     {
7151     }
7152   else
7153     {
7154       elf_elfheader (abfd)->e_flags = flags;
7155       elf_flags_init (abfd) = TRUE;
7156     }
7157
7158   return TRUE;
7159 }
7160
7161 /* Merge backend specific data from an object file to the output
7162    object file when linking.  */
7163
7164 static bfd_boolean
7165 elfNN_aarch64_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
7166 {
7167   bfd *obfd = info->output_bfd;
7168   flagword out_flags;
7169   flagword in_flags;
7170   bfd_boolean flags_compatible = TRUE;
7171   asection *sec;
7172
7173   /* Check if we have the same endianess.  */
7174   if (!_bfd_generic_verify_endian_match (ibfd, info))
7175     return FALSE;
7176
7177   if (!is_aarch64_elf (ibfd) || !is_aarch64_elf (obfd))
7178     return TRUE;
7179
7180   /* The input BFD must have had its flags initialised.  */
7181   /* The following seems bogus to me -- The flags are initialized in
7182      the assembler but I don't think an elf_flags_init field is
7183      written into the object.  */
7184   /* BFD_ASSERT (elf_flags_init (ibfd)); */
7185
7186   in_flags = elf_elfheader (ibfd)->e_flags;
7187   out_flags = elf_elfheader (obfd)->e_flags;
7188
7189   if (!elf_flags_init (obfd))
7190     {
7191       /* If the input is the default architecture and had the default
7192          flags then do not bother setting the flags for the output
7193          architecture, instead allow future merges to do this.  If no
7194          future merges ever set these flags then they will retain their
7195          uninitialised values, which surprise surprise, correspond
7196          to the default values.  */
7197       if (bfd_get_arch_info (ibfd)->the_default
7198           && elf_elfheader (ibfd)->e_flags == 0)
7199         return TRUE;
7200
7201       elf_flags_init (obfd) = TRUE;
7202       elf_elfheader (obfd)->e_flags = in_flags;
7203
7204       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
7205           && bfd_get_arch_info (obfd)->the_default)
7206         return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
7207                                   bfd_get_mach (ibfd));
7208
7209       return TRUE;
7210     }
7211
7212   /* Identical flags must be compatible.  */
7213   if (in_flags == out_flags)
7214     return TRUE;
7215
7216   /* Check to see if the input BFD actually contains any sections.  If
7217      not, its flags may not have been initialised either, but it
7218      cannot actually cause any incompatiblity.  Do not short-circuit
7219      dynamic objects; their section list may be emptied by
7220      elf_link_add_object_symbols.
7221
7222      Also check to see if there are no code sections in the input.
7223      In this case there is no need to check for code specific flags.
7224      XXX - do we need to worry about floating-point format compatability
7225      in data sections ?  */
7226   if (!(ibfd->flags & DYNAMIC))
7227     {
7228       bfd_boolean null_input_bfd = TRUE;
7229       bfd_boolean only_data_sections = TRUE;
7230
7231       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7232         {
7233           if ((bfd_get_section_flags (ibfd, sec)
7234                & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
7235               == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
7236             only_data_sections = FALSE;
7237
7238           null_input_bfd = FALSE;
7239           break;
7240         }
7241
7242       if (null_input_bfd || only_data_sections)
7243         return TRUE;
7244     }
7245
7246   return flags_compatible;
7247 }
7248
7249 /* Display the flags field.  */
7250
7251 static bfd_boolean
7252 elfNN_aarch64_print_private_bfd_data (bfd *abfd, void *ptr)
7253 {
7254   FILE *file = (FILE *) ptr;
7255   unsigned long flags;
7256
7257   BFD_ASSERT (abfd != NULL && ptr != NULL);
7258
7259   /* Print normal ELF private data.  */
7260   _bfd_elf_print_private_bfd_data (abfd, ptr);
7261
7262   flags = elf_elfheader (abfd)->e_flags;
7263   /* Ignore init flag - it may not be set, despite the flags field
7264      containing valid data.  */
7265
7266   /* xgettext:c-format */
7267   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
7268
7269   if (flags)
7270     fprintf (file, _("<Unrecognised flag bits set>"));
7271
7272   fputc ('\n', file);
7273
7274   return TRUE;
7275 }
7276
7277 /* Find dynamic relocs for H that apply to read-only sections.  */
7278
7279 static asection *
7280 readonly_dynrelocs (struct elf_link_hash_entry *h)
7281 {
7282   struct elf_dyn_relocs *p;
7283
7284   for (p = elf_aarch64_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
7285     {
7286       asection *s = p->sec->output_section;
7287
7288       if (s != NULL && (s->flags & SEC_READONLY) != 0)
7289         return p->sec;
7290     }
7291   return NULL;
7292 }
7293
7294 /* Return true if we need copy relocation against EH.  */
7295
7296 static bfd_boolean
7297 need_copy_relocation_p (struct elf_aarch64_link_hash_entry *eh)
7298 {
7299   struct elf_dyn_relocs *p;
7300   asection *s;
7301
7302   for (p = eh->dyn_relocs; p != NULL; p = p->next)
7303     {
7304       /* If there is any pc-relative reference, we need to keep copy relocation
7305          to avoid propagating the relocation into runtime that current glibc
7306          does not support.  */
7307       if (p->pc_count)
7308         return TRUE;
7309
7310       s = p->sec->output_section;
7311       /* Need copy relocation if it's against read-only section.  */
7312       if (s != NULL && (s->flags & SEC_READONLY) != 0)
7313         return TRUE;
7314     }
7315
7316   return FALSE;
7317 }
7318
7319 /* Adjust a symbol defined by a dynamic object and referenced by a
7320    regular object.  The current definition is in some section of the
7321    dynamic object, but we're not including those sections.  We have to
7322    change the definition to something the rest of the link can
7323    understand.  */
7324
7325 static bfd_boolean
7326 elfNN_aarch64_adjust_dynamic_symbol (struct bfd_link_info *info,
7327                                      struct elf_link_hash_entry *h)
7328 {
7329   struct elf_aarch64_link_hash_table *htab;
7330   asection *s, *srel;
7331
7332   /* If this is a function, put it in the procedure linkage table.  We
7333      will fill in the contents of the procedure linkage table later,
7334      when we know the address of the .got section.  */
7335   if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
7336     {
7337       if (h->plt.refcount <= 0
7338           || (h->type != STT_GNU_IFUNC
7339               && (SYMBOL_CALLS_LOCAL (info, h)
7340                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
7341                       && h->root.type == bfd_link_hash_undefweak))))
7342         {
7343           /* This case can occur if we saw a CALL26 reloc in
7344              an input file, but the symbol wasn't referred to
7345              by a dynamic object or all references were
7346              garbage collected. In which case we can end up
7347              resolving.  */
7348           h->plt.offset = (bfd_vma) - 1;
7349           h->needs_plt = 0;
7350         }
7351
7352       return TRUE;
7353     }
7354   else
7355     /* Otherwise, reset to -1.  */
7356     h->plt.offset = (bfd_vma) - 1;
7357
7358
7359   /* If this is a weak symbol, and there is a real definition, the
7360      processor independent code will have arranged for us to see the
7361      real definition first, and we can just use the same value.  */
7362   if (h->is_weakalias)
7363     {
7364       struct elf_link_hash_entry *def = weakdef (h);
7365       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7366       h->root.u.def.section = def->root.u.def.section;
7367       h->root.u.def.value = def->root.u.def.value;
7368       if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
7369         h->non_got_ref = def->non_got_ref;
7370       return TRUE;
7371     }
7372
7373   /* If we are creating a shared library, we must presume that the
7374      only references to the symbol are via the global offset table.
7375      For such cases we need not do anything here; the relocations will
7376      be handled correctly by relocate_section.  */
7377   if (bfd_link_pic (info))
7378     return TRUE;
7379
7380   /* If there are no references to this symbol that do not use the
7381      GOT, we don't need to generate a copy reloc.  */
7382   if (!h->non_got_ref)
7383     return TRUE;
7384
7385   /* If -z nocopyreloc was given, we won't generate them either.  */
7386   if (info->nocopyreloc)
7387     {
7388       h->non_got_ref = 0;
7389       return TRUE;
7390     }
7391
7392   if (ELIMINATE_COPY_RELOCS)
7393     {
7394       struct elf_aarch64_link_hash_entry *eh;
7395       /* If we don't find any dynamic relocs in read-only sections, then
7396          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
7397       eh = (struct elf_aarch64_link_hash_entry *) h;
7398       if (!need_copy_relocation_p (eh))
7399         {
7400           h->non_got_ref = 0;
7401           return TRUE;
7402         }
7403     }
7404
7405   /* We must allocate the symbol in our .dynbss section, which will
7406      become part of the .bss section of the executable.  There will be
7407      an entry for this symbol in the .dynsym section.  The dynamic
7408      object will contain position independent code, so all references
7409      from the dynamic object to this symbol will go through the global
7410      offset table.  The dynamic linker will use the .dynsym entry to
7411      determine the address it must put in the global offset table, so
7412      both the dynamic object and the regular object will refer to the
7413      same memory location for the variable.  */
7414
7415   htab = elf_aarch64_hash_table (info);
7416
7417   /* We must generate a R_AARCH64_COPY reloc to tell the dynamic linker
7418      to copy the initial value out of the dynamic object and into the
7419      runtime process image.  */
7420   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7421     {
7422       s = htab->root.sdynrelro;
7423       srel = htab->root.sreldynrelro;
7424     }
7425   else
7426     {
7427       s = htab->root.sdynbss;
7428       srel = htab->root.srelbss;
7429     }
7430   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7431     {
7432       srel->size += RELOC_SIZE (htab);
7433       h->needs_copy = 1;
7434     }
7435
7436   return _bfd_elf_adjust_dynamic_copy (info, h, s);
7437
7438 }
7439
7440 static bfd_boolean
7441 elfNN_aarch64_allocate_local_symbols (bfd *abfd, unsigned number)
7442 {
7443   struct elf_aarch64_local_symbol *locals;
7444   locals = elf_aarch64_locals (abfd);
7445   if (locals == NULL)
7446     {
7447       locals = (struct elf_aarch64_local_symbol *)
7448         bfd_zalloc (abfd, number * sizeof (struct elf_aarch64_local_symbol));
7449       if (locals == NULL)
7450         return FALSE;
7451       elf_aarch64_locals (abfd) = locals;
7452     }
7453   return TRUE;
7454 }
7455
7456 /* Create the .got section to hold the global offset table.  */
7457
7458 static bfd_boolean
7459 aarch64_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
7460 {
7461   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7462   flagword flags;
7463   asection *s;
7464   struct elf_link_hash_entry *h;
7465   struct elf_link_hash_table *htab = elf_hash_table (info);
7466
7467   /* This function may be called more than once.  */
7468   if (htab->sgot != NULL)
7469     return TRUE;
7470
7471   flags = bed->dynamic_sec_flags;
7472
7473   s = bfd_make_section_anyway_with_flags (abfd,
7474                                           (bed->rela_plts_and_copies_p
7475                                            ? ".rela.got" : ".rel.got"),
7476                                           (bed->dynamic_sec_flags
7477                                            | SEC_READONLY));
7478   if (s == NULL
7479       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
7480     return FALSE;
7481   htab->srelgot = s;
7482
7483   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
7484   if (s == NULL
7485       || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
7486     return FALSE;
7487   htab->sgot = s;
7488   htab->sgot->size += GOT_ENTRY_SIZE;
7489
7490   if (bed->want_got_sym)
7491     {
7492       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
7493          (or .got.plt) section.  We don't do this in the linker script
7494          because we don't want to define the symbol if we are not creating
7495          a global offset table.  */
7496       h = _bfd_elf_define_linkage_sym (abfd, info, s,
7497                                        "_GLOBAL_OFFSET_TABLE_");
7498       elf_hash_table (info)->hgot = h;
7499       if (h == NULL)
7500         return FALSE;
7501     }
7502
7503   if (bed->want_got_plt)
7504     {
7505       s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
7506       if (s == NULL
7507           || !bfd_set_section_alignment (abfd, s,
7508                                          bed->s->log_file_align))
7509         return FALSE;
7510       htab->sgotplt = s;
7511     }
7512
7513   /* The first bit of the global offset table is the header.  */
7514   s->size += bed->got_header_size;
7515
7516   return TRUE;
7517 }
7518
7519 /* Look through the relocs for a section during the first phase.  */
7520
7521 static bfd_boolean
7522 elfNN_aarch64_check_relocs (bfd *abfd, struct bfd_link_info *info,
7523                             asection *sec, const Elf_Internal_Rela *relocs)
7524 {
7525   Elf_Internal_Shdr *symtab_hdr;
7526   struct elf_link_hash_entry **sym_hashes;
7527   const Elf_Internal_Rela *rel;
7528   const Elf_Internal_Rela *rel_end;
7529   asection *sreloc;
7530
7531   struct elf_aarch64_link_hash_table *htab;
7532
7533   if (bfd_link_relocatable (info))
7534     return TRUE;
7535
7536   BFD_ASSERT (is_aarch64_elf (abfd));
7537
7538   htab = elf_aarch64_hash_table (info);
7539   sreloc = NULL;
7540
7541   symtab_hdr = &elf_symtab_hdr (abfd);
7542   sym_hashes = elf_sym_hashes (abfd);
7543
7544   rel_end = relocs + sec->reloc_count;
7545   for (rel = relocs; rel < rel_end; rel++)
7546     {
7547       struct elf_link_hash_entry *h;
7548       unsigned int r_symndx;
7549       unsigned int r_type;
7550       bfd_reloc_code_real_type bfd_r_type;
7551       Elf_Internal_Sym *isym;
7552
7553       r_symndx = ELFNN_R_SYM (rel->r_info);
7554       r_type = ELFNN_R_TYPE (rel->r_info);
7555
7556       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
7557         {
7558           /* xgettext:c-format */
7559           _bfd_error_handler (_("%pB: bad symbol index: %d"), abfd, r_symndx);
7560           return FALSE;
7561         }
7562
7563       if (r_symndx < symtab_hdr->sh_info)
7564         {
7565           /* A local symbol.  */
7566           isym = bfd_sym_from_r_symndx (&htab->sym_cache,
7567                                         abfd, r_symndx);
7568           if (isym == NULL)
7569             return FALSE;
7570
7571           /* Check relocation against local STT_GNU_IFUNC symbol.  */
7572           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
7573             {
7574               h = elfNN_aarch64_get_local_sym_hash (htab, abfd, rel,
7575                                                     TRUE);
7576               if (h == NULL)
7577                 return FALSE;
7578
7579               /* Fake a STT_GNU_IFUNC symbol.  */
7580               h->type = STT_GNU_IFUNC;
7581               h->def_regular = 1;
7582               h->ref_regular = 1;
7583               h->forced_local = 1;
7584               h->root.type = bfd_link_hash_defined;
7585             }
7586           else
7587             h = NULL;
7588         }
7589       else
7590         {
7591           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7592           while (h->root.type == bfd_link_hash_indirect
7593                  || h->root.type == bfd_link_hash_warning)
7594             h = (struct elf_link_hash_entry *) h->root.u.i.link;
7595         }
7596
7597       /* Could be done earlier, if h were already available.  */
7598       bfd_r_type = aarch64_tls_transition (abfd, info, r_type, h, r_symndx);
7599
7600       if (h != NULL)
7601         {
7602           /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
7603              This shows up in particular in an R_AARCH64_PREL64 in large model
7604              when calculating the pc-relative address to .got section which is
7605              used to initialize the gp register.  */
7606           if (h->root.root.string
7607               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
7608             {
7609               if (htab->root.dynobj == NULL)
7610                 htab->root.dynobj = abfd;
7611
7612               if (! aarch64_elf_create_got_section (htab->root.dynobj, info))
7613                 return FALSE;
7614
7615               BFD_ASSERT (h == htab->root.hgot);
7616             }
7617
7618           /* Create the ifunc sections for static executables.  If we
7619              never see an indirect function symbol nor we are building
7620              a static executable, those sections will be empty and
7621              won't appear in output.  */
7622           switch (bfd_r_type)
7623             {
7624             default:
7625               break;
7626
7627             case BFD_RELOC_AARCH64_ADD_LO12:
7628             case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
7629             case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
7630             case BFD_RELOC_AARCH64_CALL26:
7631             case BFD_RELOC_AARCH64_GOT_LD_PREL19:
7632             case BFD_RELOC_AARCH64_JUMP26:
7633             case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
7634             case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
7635             case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
7636             case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
7637             case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
7638             case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
7639             case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
7640             case BFD_RELOC_AARCH64_NN:
7641               if (htab->root.dynobj == NULL)
7642                 htab->root.dynobj = abfd;
7643               if (!_bfd_elf_create_ifunc_sections (htab->root.dynobj, info))
7644                 return FALSE;
7645               break;
7646             }
7647
7648           /* It is referenced by a non-shared object.  */
7649           h->ref_regular = 1;
7650         }
7651
7652       switch (bfd_r_type)
7653         {
7654         case BFD_RELOC_AARCH64_16:
7655 #if ARCH_SIZE == 64
7656         case BFD_RELOC_AARCH64_32:
7657 #endif
7658           if (bfd_link_pic (info) && (sec->flags & SEC_ALLOC) != 0)
7659             {
7660               if (h != NULL
7661                   /* This is an absolute symbol.  It represents a value instead
7662                      of an address.  */
7663                   && (bfd_is_abs_symbol (&h->root)
7664                       /* This is an undefined symbol.  */
7665                       || h->root.type == bfd_link_hash_undefined))
7666                 break;
7667
7668               /* For local symbols, defined global symbols in a non-ABS section,
7669                  it is assumed that the value is an address.  */
7670               int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
7671               _bfd_error_handler
7672                 /* xgettext:c-format */
7673                 (_("%pB: relocation %s against `%s' can not be used when making "
7674                    "a shared object"),
7675                  abfd, elfNN_aarch64_howto_table[howto_index].name,
7676                  (h) ? h->root.root.string : "a local symbol");
7677               bfd_set_error (bfd_error_bad_value);
7678               return FALSE;
7679             }
7680           else
7681             break;
7682
7683         case BFD_RELOC_AARCH64_MOVW_G0_NC:
7684         case BFD_RELOC_AARCH64_MOVW_G1_NC:
7685         case BFD_RELOC_AARCH64_MOVW_G2_NC:
7686         case BFD_RELOC_AARCH64_MOVW_G3:
7687           if (bfd_link_pic (info))
7688             {
7689               int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
7690               _bfd_error_handler
7691                 /* xgettext:c-format */
7692                 (_("%pB: relocation %s against `%s' can not be used when making "
7693                    "a shared object; recompile with -fPIC"),
7694                  abfd, elfNN_aarch64_howto_table[howto_index].name,
7695                  (h) ? h->root.root.string : "a local symbol");
7696               bfd_set_error (bfd_error_bad_value);
7697               return FALSE;
7698             }
7699           /* Fall through.  */
7700
7701         case BFD_RELOC_AARCH64_16_PCREL:
7702         case BFD_RELOC_AARCH64_32_PCREL:
7703         case BFD_RELOC_AARCH64_64_PCREL:
7704         case BFD_RELOC_AARCH64_ADD_LO12:
7705         case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
7706         case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
7707         case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
7708         case BFD_RELOC_AARCH64_LDST128_LO12:
7709         case BFD_RELOC_AARCH64_LDST16_LO12:
7710         case BFD_RELOC_AARCH64_LDST32_LO12:
7711         case BFD_RELOC_AARCH64_LDST64_LO12:
7712         case BFD_RELOC_AARCH64_LDST8_LO12:
7713         case BFD_RELOC_AARCH64_LD_LO19_PCREL:
7714           if (h == NULL || bfd_link_pic (info))
7715             break;
7716           /* Fall through.  */
7717
7718         case BFD_RELOC_AARCH64_NN:
7719
7720           /* We don't need to handle relocs into sections not going into
7721              the "real" output.  */
7722           if ((sec->flags & SEC_ALLOC) == 0)
7723             break;
7724
7725           if (h != NULL)
7726             {
7727               if (!bfd_link_pic (info))
7728                 h->non_got_ref = 1;
7729
7730               h->plt.refcount += 1;
7731               h->pointer_equality_needed = 1;
7732             }
7733
7734           /* No need to do anything if we're not creating a shared
7735              object.  */
7736           if (!(bfd_link_pic (info)
7737                 /* If on the other hand, we are creating an executable, we
7738                    may need to keep relocations for symbols satisfied by a
7739                    dynamic library if we manage to avoid copy relocs for the
7740                    symbol.
7741
7742                    NOTE: Currently, there is no support of copy relocs
7743                    elimination on pc-relative relocation types, because there is
7744                    no dynamic relocation support for them in glibc.  We still
7745                    record the dynamic symbol reference for them.  This is
7746                    because one symbol may be referenced by both absolute
7747                    relocation (for example, BFD_RELOC_AARCH64_NN) and
7748                    pc-relative relocation.  We need full symbol reference
7749                    information to make correct decision later in
7750                    elfNN_aarch64_adjust_dynamic_symbol.  */
7751                 || (ELIMINATE_COPY_RELOCS
7752                     && !bfd_link_pic (info)
7753                     && h != NULL
7754                     && (h->root.type == bfd_link_hash_defweak
7755                         || !h->def_regular))))
7756             break;
7757
7758           {
7759             struct elf_dyn_relocs *p;
7760             struct elf_dyn_relocs **head;
7761             int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
7762
7763             /* We must copy these reloc types into the output file.
7764                Create a reloc section in dynobj and make room for
7765                this reloc.  */
7766             if (sreloc == NULL)
7767               {
7768                 if (htab->root.dynobj == NULL)
7769                   htab->root.dynobj = abfd;
7770
7771                 sreloc = _bfd_elf_make_dynamic_reloc_section
7772                   (sec, htab->root.dynobj, LOG_FILE_ALIGN, abfd, /*rela? */ TRUE);
7773
7774                 if (sreloc == NULL)
7775                   return FALSE;
7776               }
7777
7778             /* If this is a global symbol, we count the number of
7779                relocations we need for this symbol.  */
7780             if (h != NULL)
7781               {
7782                 struct elf_aarch64_link_hash_entry *eh;
7783                 eh = (struct elf_aarch64_link_hash_entry *) h;
7784                 head = &eh->dyn_relocs;
7785               }
7786             else
7787               {
7788                 /* Track dynamic relocs needed for local syms too.
7789                    We really need local syms available to do this
7790                    easily.  Oh well.  */
7791
7792                 asection *s;
7793                 void **vpp;
7794
7795                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
7796                                               abfd, r_symndx);
7797                 if (isym == NULL)
7798                   return FALSE;
7799
7800                 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
7801                 if (s == NULL)
7802                   s = sec;
7803
7804                 /* Beware of type punned pointers vs strict aliasing
7805                    rules.  */
7806                 vpp = &(elf_section_data (s)->local_dynrel);
7807                 head = (struct elf_dyn_relocs **) vpp;
7808               }
7809
7810             p = *head;
7811             if (p == NULL || p->sec != sec)
7812               {
7813                 bfd_size_type amt = sizeof *p;
7814                 p = ((struct elf_dyn_relocs *)
7815                      bfd_zalloc (htab->root.dynobj, amt));
7816                 if (p == NULL)
7817                   return FALSE;
7818                 p->next = *head;
7819                 *head = p;
7820                 p->sec = sec;
7821               }
7822
7823             p->count += 1;
7824
7825             if (elfNN_aarch64_howto_table[howto_index].pc_relative)
7826               p->pc_count += 1;
7827           }
7828           break;
7829
7830           /* RR: We probably want to keep a consistency check that
7831              there are no dangling GOT_PAGE relocs.  */
7832         case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
7833         case BFD_RELOC_AARCH64_GOT_LD_PREL19:
7834         case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
7835         case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
7836         case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
7837         case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
7838         case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
7839         case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
7840         case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
7841         case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
7842         case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
7843         case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
7844         case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
7845         case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12:
7846         case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
7847         case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
7848         case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
7849         case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
7850         case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
7851         case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
7852         case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
7853         case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
7854         case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
7855         case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
7856         case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
7857         case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
7858         case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
7859         case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
7860         case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
7861         case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
7862         case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
7863           {
7864             unsigned got_type;
7865             unsigned old_got_type;
7866
7867             got_type = aarch64_reloc_got_type (bfd_r_type);
7868
7869             if (h)
7870               {
7871                 h->got.refcount += 1;
7872                 old_got_type = elf_aarch64_hash_entry (h)->got_type;
7873               }
7874             else
7875               {
7876                 struct elf_aarch64_local_symbol *locals;
7877
7878                 if (!elfNN_aarch64_allocate_local_symbols
7879                     (abfd, symtab_hdr->sh_info))
7880                   return FALSE;
7881
7882                 locals = elf_aarch64_locals (abfd);
7883                 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7884                 locals[r_symndx].got_refcount += 1;
7885                 old_got_type = locals[r_symndx].got_type;
7886               }
7887
7888             /* If a variable is accessed with both general dynamic TLS
7889                methods, two slots may be created.  */
7890             if (GOT_TLS_GD_ANY_P (old_got_type) && GOT_TLS_GD_ANY_P (got_type))
7891               got_type |= old_got_type;
7892
7893             /* We will already have issued an error message if there
7894                is a TLS/non-TLS mismatch, based on the symbol type.
7895                So just combine any TLS types needed.  */
7896             if (old_got_type != GOT_UNKNOWN && old_got_type != GOT_NORMAL
7897                 && got_type != GOT_NORMAL)
7898               got_type |= old_got_type;
7899
7900             /* If the symbol is accessed by both IE and GD methods, we
7901                are able to relax.  Turn off the GD flag, without
7902                messing up with any other kind of TLS types that may be
7903                involved.  */
7904             if ((got_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (got_type))
7905               got_type &= ~ (GOT_TLSDESC_GD | GOT_TLS_GD);
7906
7907             if (old_got_type != got_type)
7908               {
7909                 if (h != NULL)
7910                   elf_aarch64_hash_entry (h)->got_type = got_type;
7911                 else
7912                   {
7913                     struct elf_aarch64_local_symbol *locals;
7914                     locals = elf_aarch64_locals (abfd);
7915                     BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7916                     locals[r_symndx].got_type = got_type;
7917                   }
7918               }
7919
7920             if (htab->root.dynobj == NULL)
7921               htab->root.dynobj = abfd;
7922             if (! aarch64_elf_create_got_section (htab->root.dynobj, info))
7923               return FALSE;
7924             break;
7925           }
7926
7927         case BFD_RELOC_AARCH64_CALL26:
7928         case BFD_RELOC_AARCH64_JUMP26:
7929           /* If this is a local symbol then we resolve it
7930              directly without creating a PLT entry.  */
7931           if (h == NULL)
7932             continue;
7933
7934           h->needs_plt = 1;
7935           if (h->plt.refcount <= 0)
7936             h->plt.refcount = 1;
7937           else
7938             h->plt.refcount += 1;
7939           break;
7940
7941         default:
7942           break;
7943         }
7944     }
7945
7946   return TRUE;
7947 }
7948
7949 /* Treat mapping symbols as special target symbols.  */
7950
7951 static bfd_boolean
7952 elfNN_aarch64_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED,
7953                                         asymbol *sym)
7954 {
7955   return bfd_is_aarch64_special_symbol_name (sym->name,
7956                                              BFD_AARCH64_SPECIAL_SYM_TYPE_ANY);
7957 }
7958
7959 /* This is a version of _bfd_elf_find_function() from dwarf2.c except that
7960    AArch64 mapping symbols are ignored when looking for function names.  */
7961
7962 static bfd_boolean
7963 aarch64_elf_find_function (bfd *          abfd,
7964                            asymbol **     symbols,
7965                            asection *     section,
7966                            bfd_vma        offset,
7967                            const char **  filename_ptr,
7968                            const char **  functionname_ptr)
7969 {
7970   const char *filename = NULL;
7971   asymbol *func = NULL;
7972   bfd_vma low_func = 0;
7973   asymbol **p;
7974
7975   if (symbols == NULL)
7976     return FALSE;
7977
7978   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
7979     return FALSE;
7980
7981   for (p = symbols; *p != NULL; p++)
7982     {
7983       elf_symbol_type *q;
7984
7985       q = (elf_symbol_type *) * p;
7986
7987       switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
7988         {
7989         default:
7990           break;
7991         case STT_FILE:
7992           filename = bfd_asymbol_name (&q->symbol);
7993           break;
7994         case STT_FUNC:
7995         case STT_NOTYPE:
7996           /* Skip mapping symbols.  */
7997           if ((q->symbol.flags & BSF_LOCAL)
7998               && (bfd_is_aarch64_special_symbol_name
7999                   (q->symbol.name, BFD_AARCH64_SPECIAL_SYM_TYPE_ANY)))
8000             continue;
8001           /* Fall through.  */
8002           if (bfd_get_section (&q->symbol) == section
8003               && q->symbol.value >= low_func && q->symbol.value <= offset)
8004             {
8005               func = (asymbol *) q;
8006               low_func = q->symbol.value;
8007             }
8008           break;
8009         }
8010     }
8011
8012   if (func == NULL)
8013     return FALSE;
8014
8015   if (filename_ptr)
8016     *filename_ptr = filename;
8017   if (functionname_ptr)
8018     *functionname_ptr = bfd_asymbol_name (func);
8019
8020   return TRUE;
8021 }
8022
8023
8024 /* Find the nearest line to a particular section and offset, for error
8025    reporting.   This code is a duplicate of the code in elf.c, except
8026    that it uses aarch64_elf_find_function.  */
8027
8028 static bfd_boolean
8029 elfNN_aarch64_find_nearest_line (bfd *abfd,
8030                                  asymbol **symbols,
8031                                  asection *section,
8032                                  bfd_vma offset,
8033                                  const char **filename_ptr,
8034                                  const char **functionname_ptr,
8035                                  unsigned int *line_ptr,
8036                                  unsigned int *discriminator_ptr)
8037 {
8038   bfd_boolean found = FALSE;
8039
8040   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
8041                                      filename_ptr, functionname_ptr,
8042                                      line_ptr, discriminator_ptr,
8043                                      dwarf_debug_sections,
8044                                      &elf_tdata (abfd)->dwarf2_find_line_info))
8045     {
8046       if (!*functionname_ptr)
8047         aarch64_elf_find_function (abfd, symbols, section, offset,
8048                                    *filename_ptr ? NULL : filename_ptr,
8049                                    functionname_ptr);
8050
8051       return TRUE;
8052     }
8053
8054   /* Skip _bfd_dwarf1_find_nearest_line since no known AArch64
8055      toolchain uses DWARF1.  */
8056
8057   if (!_bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
8058                                             &found, filename_ptr,
8059                                             functionname_ptr, line_ptr,
8060                                             &elf_tdata (abfd)->line_info))
8061     return FALSE;
8062
8063   if (found && (*functionname_ptr || *line_ptr))
8064     return TRUE;
8065
8066   if (symbols == NULL)
8067     return FALSE;
8068
8069   if (!aarch64_elf_find_function (abfd, symbols, section, offset,
8070                                   filename_ptr, functionname_ptr))
8071     return FALSE;
8072
8073   *line_ptr = 0;
8074   return TRUE;
8075 }
8076
8077 static bfd_boolean
8078 elfNN_aarch64_find_inliner_info (bfd *abfd,
8079                                  const char **filename_ptr,
8080                                  const char **functionname_ptr,
8081                                  unsigned int *line_ptr)
8082 {
8083   bfd_boolean found;
8084   found = _bfd_dwarf2_find_inliner_info
8085     (abfd, filename_ptr,
8086      functionname_ptr, line_ptr, &elf_tdata (abfd)->dwarf2_find_line_info);
8087   return found;
8088 }
8089
8090
8091 static void
8092 elfNN_aarch64_post_process_headers (bfd *abfd,
8093                                     struct bfd_link_info *link_info)
8094 {
8095   Elf_Internal_Ehdr *i_ehdrp;   /* ELF file header, internal form.  */
8096
8097   i_ehdrp = elf_elfheader (abfd);
8098   i_ehdrp->e_ident[EI_ABIVERSION] = AARCH64_ELF_ABI_VERSION;
8099
8100   _bfd_elf_post_process_headers (abfd, link_info);
8101 }
8102
8103 static enum elf_reloc_type_class
8104 elfNN_aarch64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
8105                                 const asection *rel_sec ATTRIBUTE_UNUSED,
8106                                 const Elf_Internal_Rela *rela)
8107 {
8108   struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
8109
8110   if (htab->root.dynsym != NULL
8111       && htab->root.dynsym->contents != NULL)
8112     {
8113       /* Check relocation against STT_GNU_IFUNC symbol if there are
8114          dynamic symbols.  */
8115       bfd *abfd = info->output_bfd;
8116       const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8117       unsigned long r_symndx = ELFNN_R_SYM (rela->r_info);
8118       if (r_symndx != STN_UNDEF)
8119         {
8120           Elf_Internal_Sym sym;
8121           if (!bed->s->swap_symbol_in (abfd,
8122                                        (htab->root.dynsym->contents
8123                                         + r_symndx * bed->s->sizeof_sym),
8124                                        0, &sym))
8125             {
8126               /* xgettext:c-format */
8127               _bfd_error_handler (_("%pB symbol number %lu references"
8128                                     " nonexistent SHT_SYMTAB_SHNDX section"),
8129                                     abfd, r_symndx);
8130               /* Ideally an error class should be returned here.  */
8131             }
8132           else if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
8133             return reloc_class_ifunc;
8134         }
8135     }
8136
8137   switch ((int) ELFNN_R_TYPE (rela->r_info))
8138     {
8139     case AARCH64_R (IRELATIVE):
8140       return reloc_class_ifunc;
8141     case AARCH64_R (RELATIVE):
8142       return reloc_class_relative;
8143     case AARCH64_R (JUMP_SLOT):
8144       return reloc_class_plt;
8145     case AARCH64_R (COPY):
8146       return reloc_class_copy;
8147     default:
8148       return reloc_class_normal;
8149     }
8150 }
8151
8152 /* Handle an AArch64 specific section when reading an object file.  This is
8153    called when bfd_section_from_shdr finds a section with an unknown
8154    type.  */
8155
8156 static bfd_boolean
8157 elfNN_aarch64_section_from_shdr (bfd *abfd,
8158                                  Elf_Internal_Shdr *hdr,
8159                                  const char *name, int shindex)
8160 {
8161   /* There ought to be a place to keep ELF backend specific flags, but
8162      at the moment there isn't one.  We just keep track of the
8163      sections by their name, instead.  Fortunately, the ABI gives
8164      names for all the AArch64 specific sections, so we will probably get
8165      away with this.  */
8166   switch (hdr->sh_type)
8167     {
8168     case SHT_AARCH64_ATTRIBUTES:
8169       break;
8170
8171     default:
8172       return FALSE;
8173     }
8174
8175   if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
8176     return FALSE;
8177
8178   return TRUE;
8179 }
8180
8181 /* A structure used to record a list of sections, independently
8182    of the next and prev fields in the asection structure.  */
8183 typedef struct section_list
8184 {
8185   asection *sec;
8186   struct section_list *next;
8187   struct section_list *prev;
8188 }
8189 section_list;
8190
8191 /* Unfortunately we need to keep a list of sections for which
8192    an _aarch64_elf_section_data structure has been allocated.  This
8193    is because it is possible for functions like elfNN_aarch64_write_section
8194    to be called on a section which has had an elf_data_structure
8195    allocated for it (and so the used_by_bfd field is valid) but
8196    for which the AArch64 extended version of this structure - the
8197    _aarch64_elf_section_data structure - has not been allocated.  */
8198 static section_list *sections_with_aarch64_elf_section_data = NULL;
8199
8200 static void
8201 record_section_with_aarch64_elf_section_data (asection *sec)
8202 {
8203   struct section_list *entry;
8204
8205   entry = bfd_malloc (sizeof (*entry));
8206   if (entry == NULL)
8207     return;
8208   entry->sec = sec;
8209   entry->next = sections_with_aarch64_elf_section_data;
8210   entry->prev = NULL;
8211   if (entry->next != NULL)
8212     entry->next->prev = entry;
8213   sections_with_aarch64_elf_section_data = entry;
8214 }
8215
8216 static struct section_list *
8217 find_aarch64_elf_section_entry (asection *sec)
8218 {
8219   struct section_list *entry;
8220   static struct section_list *last_entry = NULL;
8221
8222   /* This is a short cut for the typical case where the sections are added
8223      to the sections_with_aarch64_elf_section_data list in forward order and
8224      then looked up here in backwards order.  This makes a real difference
8225      to the ld-srec/sec64k.exp linker test.  */
8226   entry = sections_with_aarch64_elf_section_data;
8227   if (last_entry != NULL)
8228     {
8229       if (last_entry->sec == sec)
8230         entry = last_entry;
8231       else if (last_entry->next != NULL && last_entry->next->sec == sec)
8232         entry = last_entry->next;
8233     }
8234
8235   for (; entry; entry = entry->next)
8236     if (entry->sec == sec)
8237       break;
8238
8239   if (entry)
8240     /* Record the entry prior to this one - it is the entry we are
8241        most likely to want to locate next time.  Also this way if we
8242        have been called from
8243        unrecord_section_with_aarch64_elf_section_data () we will not
8244        be caching a pointer that is about to be freed.  */
8245     last_entry = entry->prev;
8246
8247   return entry;
8248 }
8249
8250 static void
8251 unrecord_section_with_aarch64_elf_section_data (asection *sec)
8252 {
8253   struct section_list *entry;
8254
8255   entry = find_aarch64_elf_section_entry (sec);
8256
8257   if (entry)
8258     {
8259       if (entry->prev != NULL)
8260         entry->prev->next = entry->next;
8261       if (entry->next != NULL)
8262         entry->next->prev = entry->prev;
8263       if (entry == sections_with_aarch64_elf_section_data)
8264         sections_with_aarch64_elf_section_data = entry->next;
8265       free (entry);
8266     }
8267 }
8268
8269
8270 typedef struct
8271 {
8272   void *finfo;
8273   struct bfd_link_info *info;
8274   asection *sec;
8275   int sec_shndx;
8276   int (*func) (void *, const char *, Elf_Internal_Sym *,
8277                asection *, struct elf_link_hash_entry *);
8278 } output_arch_syminfo;
8279
8280 enum map_symbol_type
8281 {
8282   AARCH64_MAP_INSN,
8283   AARCH64_MAP_DATA
8284 };
8285
8286
8287 /* Output a single mapping symbol.  */
8288
8289 static bfd_boolean
8290 elfNN_aarch64_output_map_sym (output_arch_syminfo *osi,
8291                               enum map_symbol_type type, bfd_vma offset)
8292 {
8293   static const char *names[2] = { "$x", "$d" };
8294   Elf_Internal_Sym sym;
8295
8296   sym.st_value = (osi->sec->output_section->vma
8297                   + osi->sec->output_offset + offset);
8298   sym.st_size = 0;
8299   sym.st_other = 0;
8300   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
8301   sym.st_shndx = osi->sec_shndx;
8302   return osi->func (osi->finfo, names[type], &sym, osi->sec, NULL) == 1;
8303 }
8304
8305 /* Output a single local symbol for a generated stub.  */
8306
8307 static bfd_boolean
8308 elfNN_aarch64_output_stub_sym (output_arch_syminfo *osi, const char *name,
8309                                bfd_vma offset, bfd_vma size)
8310 {
8311   Elf_Internal_Sym sym;
8312
8313   sym.st_value = (osi->sec->output_section->vma
8314                   + osi->sec->output_offset + offset);
8315   sym.st_size = size;
8316   sym.st_other = 0;
8317   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
8318   sym.st_shndx = osi->sec_shndx;
8319   return osi->func (osi->finfo, name, &sym, osi->sec, NULL) == 1;
8320 }
8321
8322 static bfd_boolean
8323 aarch64_map_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
8324 {
8325   struct elf_aarch64_stub_hash_entry *stub_entry;
8326   asection *stub_sec;
8327   bfd_vma addr;
8328   char *stub_name;
8329   output_arch_syminfo *osi;
8330
8331   /* Massage our args to the form they really have.  */
8332   stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
8333   osi = (output_arch_syminfo *) in_arg;
8334
8335   stub_sec = stub_entry->stub_sec;
8336
8337   /* Ensure this stub is attached to the current section being
8338      processed.  */
8339   if (stub_sec != osi->sec)
8340     return TRUE;
8341
8342   addr = (bfd_vma) stub_entry->stub_offset;
8343
8344   stub_name = stub_entry->output_name;
8345
8346   switch (stub_entry->stub_type)
8347     {
8348     case aarch64_stub_adrp_branch:
8349       if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
8350                                           sizeof (aarch64_adrp_branch_stub)))
8351         return FALSE;
8352       if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
8353         return FALSE;
8354       break;
8355     case aarch64_stub_long_branch:
8356       if (!elfNN_aarch64_output_stub_sym
8357           (osi, stub_name, addr, sizeof (aarch64_long_branch_stub)))
8358         return FALSE;
8359       if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
8360         return FALSE;
8361       if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_DATA, addr + 16))
8362         return FALSE;
8363       break;
8364     case aarch64_stub_erratum_835769_veneer:
8365       if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
8366                                           sizeof (aarch64_erratum_835769_stub)))
8367         return FALSE;
8368       if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
8369         return FALSE;
8370       break;
8371     case aarch64_stub_erratum_843419_veneer:
8372       if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
8373                                           sizeof (aarch64_erratum_843419_stub)))
8374         return FALSE;
8375       if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
8376         return FALSE;
8377       break;
8378     case aarch64_stub_none:
8379       break;
8380
8381     default:
8382       abort ();
8383     }
8384
8385   return TRUE;
8386 }
8387
8388 /* Output mapping symbols for linker generated sections.  */
8389
8390 static bfd_boolean
8391 elfNN_aarch64_output_arch_local_syms (bfd *output_bfd,
8392                                       struct bfd_link_info *info,
8393                                       void *finfo,
8394                                       int (*func) (void *, const char *,
8395                                                    Elf_Internal_Sym *,
8396                                                    asection *,
8397                                                    struct elf_link_hash_entry
8398                                                    *))
8399 {
8400   output_arch_syminfo osi;
8401   struct elf_aarch64_link_hash_table *htab;
8402
8403   htab = elf_aarch64_hash_table (info);
8404
8405   osi.finfo = finfo;
8406   osi.info = info;
8407   osi.func = func;
8408
8409   /* Long calls stubs.  */
8410   if (htab->stub_bfd && htab->stub_bfd->sections)
8411     {
8412       asection *stub_sec;
8413
8414       for (stub_sec = htab->stub_bfd->sections;
8415            stub_sec != NULL; stub_sec = stub_sec->next)
8416         {
8417           /* Ignore non-stub sections.  */
8418           if (!strstr (stub_sec->name, STUB_SUFFIX))
8419             continue;
8420
8421           osi.sec = stub_sec;
8422
8423           osi.sec_shndx = _bfd_elf_section_from_bfd_section
8424             (output_bfd, osi.sec->output_section);
8425
8426           /* The first instruction in a stub is always a branch.  */
8427           if (!elfNN_aarch64_output_map_sym (&osi, AARCH64_MAP_INSN, 0))
8428             return FALSE;
8429
8430           bfd_hash_traverse (&htab->stub_hash_table, aarch64_map_one_stub,
8431                              &osi);
8432         }
8433     }
8434
8435   /* Finally, output mapping symbols for the PLT.  */
8436   if (!htab->root.splt || htab->root.splt->size == 0)
8437     return TRUE;
8438
8439   osi.sec_shndx = _bfd_elf_section_from_bfd_section
8440     (output_bfd, htab->root.splt->output_section);
8441   osi.sec = htab->root.splt;
8442
8443   elfNN_aarch64_output_map_sym (&osi, AARCH64_MAP_INSN, 0);
8444
8445   return TRUE;
8446
8447 }
8448
8449 /* Allocate target specific section data.  */
8450
8451 static bfd_boolean
8452 elfNN_aarch64_new_section_hook (bfd *abfd, asection *sec)
8453 {
8454   if (!sec->used_by_bfd)
8455     {
8456       _aarch64_elf_section_data *sdata;
8457       bfd_size_type amt = sizeof (*sdata);
8458
8459       sdata = bfd_zalloc (abfd, amt);
8460       if (sdata == NULL)
8461         return FALSE;
8462       sec->used_by_bfd = sdata;
8463     }
8464
8465   record_section_with_aarch64_elf_section_data (sec);
8466
8467   return _bfd_elf_new_section_hook (abfd, sec);
8468 }
8469
8470
8471 static void
8472 unrecord_section_via_map_over_sections (bfd *abfd ATTRIBUTE_UNUSED,
8473                                         asection *sec,
8474                                         void *ignore ATTRIBUTE_UNUSED)
8475 {
8476   unrecord_section_with_aarch64_elf_section_data (sec);
8477 }
8478
8479 static bfd_boolean
8480 elfNN_aarch64_close_and_cleanup (bfd *abfd)
8481 {
8482   if (abfd->sections)
8483     bfd_map_over_sections (abfd,
8484                            unrecord_section_via_map_over_sections, NULL);
8485
8486   return _bfd_elf_close_and_cleanup (abfd);
8487 }
8488
8489 static bfd_boolean
8490 elfNN_aarch64_bfd_free_cached_info (bfd *abfd)
8491 {
8492   if (abfd->sections)
8493     bfd_map_over_sections (abfd,
8494                            unrecord_section_via_map_over_sections, NULL);
8495
8496   return _bfd_free_cached_info (abfd);
8497 }
8498
8499 /* Create dynamic sections. This is different from the ARM backend in that
8500    the got, plt, gotplt and their relocation sections are all created in the
8501    standard part of the bfd elf backend.  */
8502
8503 static bfd_boolean
8504 elfNN_aarch64_create_dynamic_sections (bfd *dynobj,
8505                                        struct bfd_link_info *info)
8506 {
8507   /* We need to create .got section.  */
8508   if (!aarch64_elf_create_got_section (dynobj, info))
8509     return FALSE;
8510
8511   return _bfd_elf_create_dynamic_sections (dynobj, info);
8512 }
8513
8514
8515 /* Allocate space in .plt, .got and associated reloc sections for
8516    dynamic relocs.  */
8517
8518 static bfd_boolean
8519 elfNN_aarch64_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8520 {
8521   struct bfd_link_info *info;
8522   struct elf_aarch64_link_hash_table *htab;
8523   struct elf_aarch64_link_hash_entry *eh;
8524   struct elf_dyn_relocs *p;
8525
8526   /* An example of a bfd_link_hash_indirect symbol is versioned
8527      symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
8528      -> __gxx_personality_v0(bfd_link_hash_defined)
8529
8530      There is no need to process bfd_link_hash_indirect symbols here
8531      because we will also be presented with the concrete instance of
8532      the symbol and elfNN_aarch64_copy_indirect_symbol () will have been
8533      called to copy all relevant data from the generic to the concrete
8534      symbol instance.  */
8535   if (h->root.type == bfd_link_hash_indirect)
8536     return TRUE;
8537
8538   if (h->root.type == bfd_link_hash_warning)
8539     h = (struct elf_link_hash_entry *) h->root.u.i.link;
8540
8541   info = (struct bfd_link_info *) inf;
8542   htab = elf_aarch64_hash_table (info);
8543
8544   /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
8545      here if it is defined and referenced in a non-shared object.  */
8546   if (h->type == STT_GNU_IFUNC
8547       && h->def_regular)
8548     return TRUE;
8549   else if (htab->root.dynamic_sections_created && h->plt.refcount > 0)
8550     {
8551       /* Make sure this symbol is output as a dynamic symbol.
8552          Undefined weak syms won't yet be marked as dynamic.  */
8553       if (h->dynindx == -1 && !h->forced_local
8554           && h->root.type == bfd_link_hash_undefweak)
8555         {
8556           if (!bfd_elf_link_record_dynamic_symbol (info, h))
8557             return FALSE;
8558         }
8559
8560       if (bfd_link_pic (info) || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
8561         {
8562           asection *s = htab->root.splt;
8563
8564           /* If this is the first .plt entry, make room for the special
8565              first entry.  */
8566           if (s->size == 0)
8567             s->size += htab->plt_header_size;
8568
8569           h->plt.offset = s->size;
8570
8571           /* If this symbol is not defined in a regular file, and we are
8572              not generating a shared library, then set the symbol to this
8573              location in the .plt.  This is required to make function
8574              pointers compare as equal between the normal executable and
8575              the shared library.  */
8576           if (!bfd_link_pic (info) && !h->def_regular)
8577             {
8578               h->root.u.def.section = s;
8579               h->root.u.def.value = h->plt.offset;
8580             }
8581
8582           /* Make room for this entry. For now we only create the
8583              small model PLT entries. We later need to find a way
8584              of relaxing into these from the large model PLT entries.  */
8585           s->size += htab->plt_entry_size;
8586
8587           /* We also need to make an entry in the .got.plt section, which
8588              will be placed in the .got section by the linker script.  */
8589           htab->root.sgotplt->size += GOT_ENTRY_SIZE;
8590
8591           /* We also need to make an entry in the .rela.plt section.  */
8592           htab->root.srelplt->size += RELOC_SIZE (htab);
8593
8594           /* We need to ensure that all GOT entries that serve the PLT
8595              are consecutive with the special GOT slots [0] [1] and
8596              [2]. Any addtional relocations, such as
8597              R_AARCH64_TLSDESC, must be placed after the PLT related
8598              entries.  We abuse the reloc_count such that during
8599              sizing we adjust reloc_count to indicate the number of
8600              PLT related reserved entries.  In subsequent phases when
8601              filling in the contents of the reloc entries, PLT related
8602              entries are placed by computing their PLT index (0
8603              .. reloc_count). While other none PLT relocs are placed
8604              at the slot indicated by reloc_count and reloc_count is
8605              updated.  */
8606
8607           htab->root.srelplt->reloc_count++;
8608
8609           /* Mark the DSO in case R_<CLS>_JUMP_SLOT relocs against
8610              variant PCS symbols are present.  */
8611           if (h->other & STO_AARCH64_VARIANT_PCS)
8612             htab->variant_pcs = 1;
8613
8614         }
8615       else
8616         {
8617           h->plt.offset = (bfd_vma) - 1;
8618           h->needs_plt = 0;
8619         }
8620     }
8621   else
8622     {
8623       h->plt.offset = (bfd_vma) - 1;
8624       h->needs_plt = 0;
8625     }
8626
8627   eh = (struct elf_aarch64_link_hash_entry *) h;
8628   eh->tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
8629
8630   if (h->got.refcount > 0)
8631     {
8632       bfd_boolean dyn;
8633       unsigned got_type = elf_aarch64_hash_entry (h)->got_type;
8634
8635       h->got.offset = (bfd_vma) - 1;
8636
8637       dyn = htab->root.dynamic_sections_created;
8638
8639       /* Make sure this symbol is output as a dynamic symbol.
8640          Undefined weak syms won't yet be marked as dynamic.  */
8641       if (dyn && h->dynindx == -1 && !h->forced_local
8642           && h->root.type == bfd_link_hash_undefweak)
8643         {
8644           if (!bfd_elf_link_record_dynamic_symbol (info, h))
8645             return FALSE;
8646         }
8647
8648       if (got_type == GOT_UNKNOWN)
8649         {
8650         }
8651       else if (got_type == GOT_NORMAL)
8652         {
8653           h->got.offset = htab->root.sgot->size;
8654           htab->root.sgot->size += GOT_ENTRY_SIZE;
8655           if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8656                || h->root.type != bfd_link_hash_undefweak)
8657               && (bfd_link_pic (info)
8658                   || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
8659               /* Undefined weak symbol in static PIE resolves to 0 without
8660                  any dynamic relocations.  */
8661               && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8662             {
8663               htab->root.srelgot->size += RELOC_SIZE (htab);
8664             }
8665         }
8666       else
8667         {
8668           int indx;
8669           if (got_type & GOT_TLSDESC_GD)
8670             {
8671               eh->tlsdesc_got_jump_table_offset =
8672                 (htab->root.sgotplt->size
8673                  - aarch64_compute_jump_table_size (htab));
8674               htab->root.sgotplt->size += GOT_ENTRY_SIZE * 2;
8675               h->got.offset = (bfd_vma) - 2;
8676             }
8677
8678           if (got_type & GOT_TLS_GD)
8679             {
8680               h->got.offset = htab->root.sgot->size;
8681               htab->root.sgot->size += GOT_ENTRY_SIZE * 2;
8682             }
8683
8684           if (got_type & GOT_TLS_IE)
8685             {
8686               h->got.offset = htab->root.sgot->size;
8687               htab->root.sgot->size += GOT_ENTRY_SIZE;
8688             }
8689
8690           indx = h && h->dynindx != -1 ? h->dynindx : 0;
8691           if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8692                || h->root.type != bfd_link_hash_undefweak)
8693               && (!bfd_link_executable (info)
8694                   || indx != 0
8695                   || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
8696             {
8697               if (got_type & GOT_TLSDESC_GD)
8698                 {
8699                   htab->root.srelplt->size += RELOC_SIZE (htab);
8700                   /* Note reloc_count not incremented here!  We have
8701                      already adjusted reloc_count for this relocation
8702                      type.  */
8703
8704                   /* TLSDESC PLT is now needed, but not yet determined.  */
8705                   htab->tlsdesc_plt = (bfd_vma) - 1;
8706                 }
8707
8708               if (got_type & GOT_TLS_GD)
8709                 htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
8710
8711               if (got_type & GOT_TLS_IE)
8712                 htab->root.srelgot->size += RELOC_SIZE (htab);
8713             }
8714         }
8715     }
8716   else
8717     {
8718       h->got.offset = (bfd_vma) - 1;
8719     }
8720
8721   if (eh->dyn_relocs == NULL)
8722     return TRUE;
8723
8724   /* In the shared -Bsymbolic case, discard space allocated for
8725      dynamic pc-relative relocs against symbols which turn out to be
8726      defined in regular objects.  For the normal shared case, discard
8727      space for pc-relative relocs that have become local due to symbol
8728      visibility changes.  */
8729
8730   if (bfd_link_pic (info))
8731     {
8732       /* Relocs that use pc_count are those that appear on a call
8733          insn, or certain REL relocs that can generated via assembly.
8734          We want calls to protected symbols to resolve directly to the
8735          function rather than going via the plt.  If people want
8736          function pointer comparisons to work as expected then they
8737          should avoid writing weird assembly.  */
8738       if (SYMBOL_CALLS_LOCAL (info, h))
8739         {
8740           struct elf_dyn_relocs **pp;
8741
8742           for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
8743             {
8744               p->count -= p->pc_count;
8745               p->pc_count = 0;
8746               if (p->count == 0)
8747                 *pp = p->next;
8748               else
8749                 pp = &p->next;
8750             }
8751         }
8752
8753       /* Also discard relocs on undefined weak syms with non-default
8754          visibility.  */
8755       if (eh->dyn_relocs != NULL && h->root.type == bfd_link_hash_undefweak)
8756         {
8757           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8758               || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8759             eh->dyn_relocs = NULL;
8760
8761           /* Make sure undefined weak symbols are output as a dynamic
8762              symbol in PIEs.  */
8763           else if (h->dynindx == -1
8764                    && !h->forced_local
8765                    && h->root.type == bfd_link_hash_undefweak
8766                    && !bfd_elf_link_record_dynamic_symbol (info, h))
8767             return FALSE;
8768         }
8769
8770     }
8771   else if (ELIMINATE_COPY_RELOCS)
8772     {
8773       /* For the non-shared case, discard space for relocs against
8774          symbols which turn out to need copy relocs or are not
8775          dynamic.  */
8776
8777       if (!h->non_got_ref
8778           && ((h->def_dynamic
8779                && !h->def_regular)
8780               || (htab->root.dynamic_sections_created
8781                   && (h->root.type == bfd_link_hash_undefweak
8782                       || h->root.type == bfd_link_hash_undefined))))
8783         {
8784           /* Make sure this symbol is output as a dynamic symbol.
8785              Undefined weak syms won't yet be marked as dynamic.  */
8786           if (h->dynindx == -1
8787               && !h->forced_local
8788               && h->root.type == bfd_link_hash_undefweak
8789               && !bfd_elf_link_record_dynamic_symbol (info, h))
8790             return FALSE;
8791
8792           /* If that succeeded, we know we'll be keeping all the
8793              relocs.  */
8794           if (h->dynindx != -1)
8795             goto keep;
8796         }
8797
8798       eh->dyn_relocs = NULL;
8799
8800     keep:;
8801     }
8802
8803   /* Finally, allocate space.  */
8804   for (p = eh->dyn_relocs; p != NULL; p = p->next)
8805     {
8806       asection *sreloc;
8807
8808       sreloc = elf_section_data (p->sec)->sreloc;
8809
8810       BFD_ASSERT (sreloc != NULL);
8811
8812       sreloc->size += p->count * RELOC_SIZE (htab);
8813     }
8814
8815   return TRUE;
8816 }
8817
8818 /* Allocate space in .plt, .got and associated reloc sections for
8819    ifunc dynamic relocs.  */
8820
8821 static bfd_boolean
8822 elfNN_aarch64_allocate_ifunc_dynrelocs (struct elf_link_hash_entry *h,
8823                                         void *inf)
8824 {
8825   struct bfd_link_info *info;
8826   struct elf_aarch64_link_hash_table *htab;
8827   struct elf_aarch64_link_hash_entry *eh;
8828
8829   /* An example of a bfd_link_hash_indirect symbol is versioned
8830      symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
8831      -> __gxx_personality_v0(bfd_link_hash_defined)
8832
8833      There is no need to process bfd_link_hash_indirect symbols here
8834      because we will also be presented with the concrete instance of
8835      the symbol and elfNN_aarch64_copy_indirect_symbol () will have been
8836      called to copy all relevant data from the generic to the concrete
8837      symbol instance.  */
8838   if (h->root.type == bfd_link_hash_indirect)
8839     return TRUE;
8840
8841   if (h->root.type == bfd_link_hash_warning)
8842     h = (struct elf_link_hash_entry *) h->root.u.i.link;
8843
8844   info = (struct bfd_link_info *) inf;
8845   htab = elf_aarch64_hash_table (info);
8846
8847   eh = (struct elf_aarch64_link_hash_entry *) h;
8848
8849   /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
8850      here if it is defined and referenced in a non-shared object.  */
8851   if (h->type == STT_GNU_IFUNC
8852       && h->def_regular)
8853     return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
8854                                                &eh->dyn_relocs,
8855                                                NULL,
8856                                                htab->plt_entry_size,
8857                                                htab->plt_header_size,
8858                                                GOT_ENTRY_SIZE,
8859                                                FALSE);
8860   return TRUE;
8861 }
8862
8863 /* Allocate space in .plt, .got and associated reloc sections for
8864    local ifunc dynamic relocs.  */
8865
8866 static bfd_boolean
8867 elfNN_aarch64_allocate_local_ifunc_dynrelocs (void **slot, void *inf)
8868 {
8869   struct elf_link_hash_entry *h
8870     = (struct elf_link_hash_entry *) *slot;
8871
8872   if (h->type != STT_GNU_IFUNC
8873       || !h->def_regular
8874       || !h->ref_regular
8875       || !h->forced_local
8876       || h->root.type != bfd_link_hash_defined)
8877     abort ();
8878
8879   return elfNN_aarch64_allocate_ifunc_dynrelocs (h, inf);
8880 }
8881
8882 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
8883    read-only sections.  */
8884
8885 static bfd_boolean
8886 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
8887 {
8888   asection *sec;
8889
8890   if (h->root.type == bfd_link_hash_indirect)
8891     return TRUE;
8892
8893   sec = readonly_dynrelocs (h);
8894   if (sec != NULL)
8895     {
8896       struct bfd_link_info *info = (struct bfd_link_info *) info_p;
8897
8898       info->flags |= DF_TEXTREL;
8899       info->callbacks->minfo
8900         (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
8901          sec->owner, h->root.root.string, sec);
8902
8903       /* Not an error, just cut short the traversal.  */
8904       return FALSE;
8905     }
8906   return TRUE;
8907 }
8908
8909 /* This is the most important function of all . Innocuosly named
8910    though !  */
8911
8912 static bfd_boolean
8913 elfNN_aarch64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
8914                                      struct bfd_link_info *info)
8915 {
8916   struct elf_aarch64_link_hash_table *htab;
8917   bfd *dynobj;
8918   asection *s;
8919   bfd_boolean relocs;
8920   bfd *ibfd;
8921
8922   htab = elf_aarch64_hash_table ((info));
8923   dynobj = htab->root.dynobj;
8924
8925   BFD_ASSERT (dynobj != NULL);
8926
8927   if (htab->root.dynamic_sections_created)
8928     {
8929       if (bfd_link_executable (info) && !info->nointerp)
8930         {
8931           s = bfd_get_linker_section (dynobj, ".interp");
8932           if (s == NULL)
8933             abort ();
8934           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
8935           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
8936         }
8937     }
8938
8939   /* Set up .got offsets for local syms, and space for local dynamic
8940      relocs.  */
8941   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8942     {
8943       struct elf_aarch64_local_symbol *locals = NULL;
8944       Elf_Internal_Shdr *symtab_hdr;
8945       asection *srel;
8946       unsigned int i;
8947
8948       if (!is_aarch64_elf (ibfd))
8949         continue;
8950
8951       for (s = ibfd->sections; s != NULL; s = s->next)
8952         {
8953           struct elf_dyn_relocs *p;
8954
8955           for (p = (struct elf_dyn_relocs *)
8956                (elf_section_data (s)->local_dynrel); p != NULL; p = p->next)
8957             {
8958               if (!bfd_is_abs_section (p->sec)
8959                   && bfd_is_abs_section (p->sec->output_section))
8960                 {
8961                   /* Input section has been discarded, either because
8962                      it is a copy of a linkonce section or due to
8963                      linker script /DISCARD/, so we'll be discarding
8964                      the relocs too.  */
8965                 }
8966               else if (p->count != 0)
8967                 {
8968                   srel = elf_section_data (p->sec)->sreloc;
8969                   srel->size += p->count * RELOC_SIZE (htab);
8970                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
8971                     info->flags |= DF_TEXTREL;
8972                 }
8973             }
8974         }
8975
8976       locals = elf_aarch64_locals (ibfd);
8977       if (!locals)
8978         continue;
8979
8980       symtab_hdr = &elf_symtab_hdr (ibfd);
8981       srel = htab->root.srelgot;
8982       for (i = 0; i < symtab_hdr->sh_info; i++)
8983         {
8984           locals[i].got_offset = (bfd_vma) - 1;
8985           locals[i].tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
8986           if (locals[i].got_refcount > 0)
8987             {
8988               unsigned got_type = locals[i].got_type;
8989               if (got_type & GOT_TLSDESC_GD)
8990                 {
8991                   locals[i].tlsdesc_got_jump_table_offset =
8992                     (htab->root.sgotplt->size
8993                      - aarch64_compute_jump_table_size (htab));
8994                   htab->root.sgotplt->size += GOT_ENTRY_SIZE * 2;
8995                   locals[i].got_offset = (bfd_vma) - 2;
8996                 }
8997
8998               if (got_type & GOT_TLS_GD)
8999                 {
9000                   locals[i].got_offset = htab->root.sgot->size;
9001                   htab->root.sgot->size += GOT_ENTRY_SIZE * 2;
9002                 }
9003
9004               if (got_type & GOT_TLS_IE
9005                   || got_type & GOT_NORMAL)
9006                 {
9007                   locals[i].got_offset = htab->root.sgot->size;
9008                   htab->root.sgot->size += GOT_ENTRY_SIZE;
9009                 }
9010
9011               if (got_type == GOT_UNKNOWN)
9012                 {
9013                 }
9014
9015               if (bfd_link_pic (info))
9016                 {
9017                   if (got_type & GOT_TLSDESC_GD)
9018                     {
9019                       htab->root.srelplt->size += RELOC_SIZE (htab);
9020                       /* Note RELOC_COUNT not incremented here! */
9021                       htab->tlsdesc_plt = (bfd_vma) - 1;
9022                     }
9023
9024                   if (got_type & GOT_TLS_GD)
9025                     htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
9026
9027                   if (got_type & GOT_TLS_IE
9028                       || got_type & GOT_NORMAL)
9029                     htab->root.srelgot->size += RELOC_SIZE (htab);
9030                 }
9031             }
9032           else
9033             {
9034               locals[i].got_refcount = (bfd_vma) - 1;
9035             }
9036         }
9037     }
9038
9039
9040   /* Allocate global sym .plt and .got entries, and space for global
9041      sym dynamic relocs.  */
9042   elf_link_hash_traverse (&htab->root, elfNN_aarch64_allocate_dynrelocs,
9043                           info);
9044
9045   /* Allocate global ifunc sym .plt and .got entries, and space for global
9046      ifunc sym dynamic relocs.  */
9047   elf_link_hash_traverse (&htab->root, elfNN_aarch64_allocate_ifunc_dynrelocs,
9048                           info);
9049
9050   /* Allocate .plt and .got entries, and space for local ifunc symbols.  */
9051   htab_traverse (htab->loc_hash_table,
9052                  elfNN_aarch64_allocate_local_ifunc_dynrelocs,
9053                  info);
9054
9055   /* For every jump slot reserved in the sgotplt, reloc_count is
9056      incremented.  However, when we reserve space for TLS descriptors,
9057      it's not incremented, so in order to compute the space reserved
9058      for them, it suffices to multiply the reloc count by the jump
9059      slot size.  */
9060
9061   if (htab->root.srelplt)
9062     htab->sgotplt_jump_table_size = aarch64_compute_jump_table_size (htab);
9063
9064   if (htab->tlsdesc_plt)
9065     {
9066       if (htab->root.splt->size == 0)
9067         htab->root.splt->size += htab->plt_header_size;
9068
9069       /* If we're not using lazy TLS relocations, don't generate the
9070          GOT and PLT entry required.  */
9071       if (!(info->flags & DF_BIND_NOW))
9072         {
9073           htab->tlsdesc_plt = htab->root.splt->size;
9074           htab->root.splt->size += htab->tlsdesc_plt_entry_size;
9075
9076           htab->dt_tlsdesc_got = htab->root.sgot->size;
9077           htab->root.sgot->size += GOT_ENTRY_SIZE;
9078         }
9079     }
9080
9081   /* Init mapping symbols information to use later to distingush between
9082      code and data while scanning for errata.  */
9083   if (htab->fix_erratum_835769 || htab->fix_erratum_843419)
9084     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9085       {
9086         if (!is_aarch64_elf (ibfd))
9087           continue;
9088         bfd_elfNN_aarch64_init_maps (ibfd);
9089       }
9090
9091   /* We now have determined the sizes of the various dynamic sections.
9092      Allocate memory for them.  */
9093   relocs = FALSE;
9094   for (s = dynobj->sections; s != NULL; s = s->next)
9095     {
9096       if ((s->flags & SEC_LINKER_CREATED) == 0)
9097         continue;
9098
9099       if (s == htab->root.splt
9100           || s == htab->root.sgot
9101           || s == htab->root.sgotplt
9102           || s == htab->root.iplt
9103           || s == htab->root.igotplt
9104           || s == htab->root.sdynbss
9105           || s == htab->root.sdynrelro)
9106         {
9107           /* Strip this section if we don't need it; see the
9108              comment below.  */
9109         }
9110       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
9111         {
9112           if (s->size != 0 && s != htab->root.srelplt)
9113             relocs = TRUE;
9114
9115           /* We use the reloc_count field as a counter if we need
9116              to copy relocs into the output file.  */
9117           if (s != htab->root.srelplt)
9118             s->reloc_count = 0;
9119         }
9120       else
9121         {
9122           /* It's not one of our sections, so don't allocate space.  */
9123           continue;
9124         }
9125
9126       if (s->size == 0)
9127         {
9128           /* If we don't need this section, strip it from the
9129              output file.  This is mostly to handle .rela.bss and
9130              .rela.plt.  We must create both sections in
9131              create_dynamic_sections, because they must be created
9132              before the linker maps input sections to output
9133              sections.  The linker does that before
9134              adjust_dynamic_symbol is called, and it is that
9135              function which decides whether anything needs to go
9136              into these sections.  */
9137           s->flags |= SEC_EXCLUDE;
9138           continue;
9139         }
9140
9141       if ((s->flags & SEC_HAS_CONTENTS) == 0)
9142         continue;
9143
9144       /* Allocate memory for the section contents.  We use bfd_zalloc
9145          here in case unused entries are not reclaimed before the
9146          section's contents are written out.  This should not happen,
9147          but this way if it does, we get a R_AARCH64_NONE reloc instead
9148          of garbage.  */
9149       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
9150       if (s->contents == NULL)
9151         return FALSE;
9152     }
9153
9154   if (htab->root.dynamic_sections_created)
9155     {
9156       /* Add some entries to the .dynamic section.  We fill in the
9157          values later, in elfNN_aarch64_finish_dynamic_sections, but we
9158          must add the entries now so that we get the correct size for
9159          the .dynamic section.  The DT_DEBUG entry is filled in by the
9160          dynamic linker and used by the debugger.  */
9161 #define add_dynamic_entry(TAG, VAL)                     \
9162       _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9163
9164       if (bfd_link_executable (info))
9165         {
9166           if (!add_dynamic_entry (DT_DEBUG, 0))
9167             return FALSE;
9168         }
9169
9170       if (htab->root.splt->size != 0)
9171         {
9172           if (!add_dynamic_entry (DT_PLTGOT, 0)
9173               || !add_dynamic_entry (DT_PLTRELSZ, 0)
9174               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9175               || !add_dynamic_entry (DT_JMPREL, 0))
9176             return FALSE;
9177
9178           if (htab->variant_pcs
9179               && !add_dynamic_entry (DT_AARCH64_VARIANT_PCS, 0))
9180             return FALSE;
9181
9182           if (htab->tlsdesc_plt
9183               && !(info->flags & DF_BIND_NOW)
9184               && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
9185                   || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
9186             return FALSE;
9187
9188           if ((elf_aarch64_tdata (output_bfd)->plt_type == PLT_BTI_PAC)
9189               && (!add_dynamic_entry (DT_AARCH64_BTI_PLT, 0)
9190                   || !add_dynamic_entry (DT_AARCH64_PAC_PLT, 0)))
9191             return FALSE;
9192
9193           else if ((elf_aarch64_tdata (output_bfd)->plt_type == PLT_BTI)
9194                    && !add_dynamic_entry (DT_AARCH64_BTI_PLT, 0))
9195             return FALSE;
9196
9197           else if ((elf_aarch64_tdata (output_bfd)->plt_type == PLT_PAC)
9198                    && !add_dynamic_entry (DT_AARCH64_PAC_PLT, 0))
9199             return FALSE;
9200         }
9201
9202       if (relocs)
9203         {
9204           if (!add_dynamic_entry (DT_RELA, 0)
9205               || !add_dynamic_entry (DT_RELASZ, 0)
9206               || !add_dynamic_entry (DT_RELAENT, RELOC_SIZE (htab)))
9207             return FALSE;
9208
9209           /* If any dynamic relocs apply to a read-only section,
9210              then we need a DT_TEXTREL entry.  */
9211           if ((info->flags & DF_TEXTREL) == 0)
9212             elf_link_hash_traverse (&htab->root, maybe_set_textrel, info);
9213
9214           if ((info->flags & DF_TEXTREL) != 0)
9215             {
9216               if (!add_dynamic_entry (DT_TEXTREL, 0))
9217                 return FALSE;
9218             }
9219         }
9220     }
9221 #undef add_dynamic_entry
9222
9223   return TRUE;
9224 }
9225
9226 static inline void
9227 elf_aarch64_update_plt_entry (bfd *output_bfd,
9228                               bfd_reloc_code_real_type r_type,
9229                               bfd_byte *plt_entry, bfd_vma value)
9230 {
9231   reloc_howto_type *howto = elfNN_aarch64_howto_from_bfd_reloc (r_type);
9232
9233   /* FIXME: We should check the return value from this function call.  */
9234   (void) _bfd_aarch64_elf_put_addend (output_bfd, plt_entry, r_type, howto, value);
9235 }
9236
9237 static void
9238 elfNN_aarch64_create_small_pltn_entry (struct elf_link_hash_entry *h,
9239                                        struct elf_aarch64_link_hash_table
9240                                        *htab, bfd *output_bfd,
9241                                        struct bfd_link_info *info)
9242 {
9243   bfd_byte *plt_entry;
9244   bfd_vma plt_index;
9245   bfd_vma got_offset;
9246   bfd_vma gotplt_entry_address;
9247   bfd_vma plt_entry_address;
9248   Elf_Internal_Rela rela;
9249   bfd_byte *loc;
9250   asection *plt, *gotplt, *relplt;
9251
9252   /* When building a static executable, use .iplt, .igot.plt and
9253      .rela.iplt sections for STT_GNU_IFUNC symbols.  */
9254   if (htab->root.splt != NULL)
9255     {
9256       plt = htab->root.splt;
9257       gotplt = htab->root.sgotplt;
9258       relplt = htab->root.srelplt;
9259     }
9260   else
9261     {
9262       plt = htab->root.iplt;
9263       gotplt = htab->root.igotplt;
9264       relplt = htab->root.irelplt;
9265     }
9266
9267   /* Get the index in the procedure linkage table which
9268      corresponds to this symbol.  This is the index of this symbol
9269      in all the symbols for which we are making plt entries.  The
9270      first entry in the procedure linkage table is reserved.
9271
9272      Get the offset into the .got table of the entry that
9273      corresponds to this function.      Each .got entry is GOT_ENTRY_SIZE
9274      bytes. The first three are reserved for the dynamic linker.
9275
9276      For static executables, we don't reserve anything.  */
9277
9278   if (plt == htab->root.splt)
9279     {
9280       plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
9281       got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
9282     }
9283   else
9284     {
9285       plt_index = h->plt.offset / htab->plt_entry_size;
9286       got_offset = plt_index * GOT_ENTRY_SIZE;
9287     }
9288
9289   plt_entry = plt->contents + h->plt.offset;
9290   plt_entry_address = plt->output_section->vma
9291     + plt->output_offset + h->plt.offset;
9292   gotplt_entry_address = gotplt->output_section->vma +
9293     gotplt->output_offset + got_offset;
9294
9295   /* Copy in the boiler-plate for the PLTn entry.  */
9296   memcpy (plt_entry, htab->plt_entry, htab->plt_entry_size);
9297
9298   /* First instruction in BTI enabled PLT stub is a BTI
9299      instruction so skip it.  */
9300   if (elf_aarch64_tdata (output_bfd)->plt_type & PLT_BTI
9301       && elf_elfheader (output_bfd)->e_type == ET_EXEC)
9302     plt_entry = plt_entry + 4;
9303
9304   /* Fill in the top 21 bits for this: ADRP x16, PLT_GOT + n * 8.
9305      ADRP:   ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
9306   elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADR_HI21_PCREL,
9307                                 plt_entry,
9308                                 PG (gotplt_entry_address) -
9309                                 PG (plt_entry_address));
9310
9311   /* Fill in the lo12 bits for the load from the pltgot.  */
9312   elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_LDSTNN_LO12,
9313                                 plt_entry + 4,
9314                                 PG_OFFSET (gotplt_entry_address));
9315
9316   /* Fill in the lo12 bits for the add from the pltgot entry.  */
9317   elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADD_LO12,
9318                                 plt_entry + 8,
9319                                 PG_OFFSET (gotplt_entry_address));
9320
9321   /* All the GOTPLT Entries are essentially initialized to PLT0.  */
9322   bfd_put_NN (output_bfd,
9323               plt->output_section->vma + plt->output_offset,
9324               gotplt->contents + got_offset);
9325
9326   rela.r_offset = gotplt_entry_address;
9327
9328   if (h->dynindx == -1
9329       || ((bfd_link_executable (info)
9330            || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9331           && h->def_regular
9332           && h->type == STT_GNU_IFUNC))
9333     {
9334       /* If an STT_GNU_IFUNC symbol is locally defined, generate
9335          R_AARCH64_IRELATIVE instead of R_AARCH64_JUMP_SLOT.  */
9336       rela.r_info = ELFNN_R_INFO (0, AARCH64_R (IRELATIVE));
9337       rela.r_addend = (h->root.u.def.value
9338                        + h->root.u.def.section->output_section->vma
9339                        + h->root.u.def.section->output_offset);
9340     }
9341   else
9342     {
9343       /* Fill in the entry in the .rela.plt section.  */
9344       rela.r_info = ELFNN_R_INFO (h->dynindx, AARCH64_R (JUMP_SLOT));
9345       rela.r_addend = 0;
9346     }
9347
9348   /* Compute the relocation entry to used based on PLT index and do
9349      not adjust reloc_count. The reloc_count has already been adjusted
9350      to account for this entry.  */
9351   loc = relplt->contents + plt_index * RELOC_SIZE (htab);
9352   bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
9353 }
9354
9355 /* Size sections even though they're not dynamic.  We use it to setup
9356    _TLS_MODULE_BASE_, if needed.  */
9357
9358 static bfd_boolean
9359 elfNN_aarch64_always_size_sections (bfd *output_bfd,
9360                                     struct bfd_link_info *info)
9361 {
9362   asection *tls_sec;
9363
9364   if (bfd_link_relocatable (info))
9365     return TRUE;
9366
9367   tls_sec = elf_hash_table (info)->tls_sec;
9368
9369   if (tls_sec)
9370     {
9371       struct elf_link_hash_entry *tlsbase;
9372
9373       tlsbase = elf_link_hash_lookup (elf_hash_table (info),
9374                                       "_TLS_MODULE_BASE_", TRUE, TRUE, FALSE);
9375
9376       if (tlsbase)
9377         {
9378           struct bfd_link_hash_entry *h = NULL;
9379           const struct elf_backend_data *bed =
9380             get_elf_backend_data (output_bfd);
9381
9382           if (!(_bfd_generic_link_add_one_symbol
9383                 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
9384                  tls_sec, 0, NULL, FALSE, bed->collect, &h)))
9385             return FALSE;
9386
9387           tlsbase->type = STT_TLS;
9388           tlsbase = (struct elf_link_hash_entry *) h;
9389           tlsbase->def_regular = 1;
9390           tlsbase->other = STV_HIDDEN;
9391           (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
9392         }
9393     }
9394
9395   return TRUE;
9396 }
9397
9398 /* Finish up dynamic symbol handling.  We set the contents of various
9399    dynamic sections here.  */
9400
9401 static bfd_boolean
9402 elfNN_aarch64_finish_dynamic_symbol (bfd *output_bfd,
9403                                      struct bfd_link_info *info,
9404                                      struct elf_link_hash_entry *h,
9405                                      Elf_Internal_Sym *sym)
9406 {
9407   struct elf_aarch64_link_hash_table *htab;
9408   htab = elf_aarch64_hash_table (info);
9409
9410   if (h->plt.offset != (bfd_vma) - 1)
9411     {
9412       asection *plt, *gotplt, *relplt;
9413
9414       /* This symbol has an entry in the procedure linkage table.  Set
9415          it up.  */
9416
9417       /* When building a static executable, use .iplt, .igot.plt and
9418          .rela.iplt sections for STT_GNU_IFUNC symbols.  */
9419       if (htab->root.splt != NULL)
9420         {
9421           plt = htab->root.splt;
9422           gotplt = htab->root.sgotplt;
9423           relplt = htab->root.srelplt;
9424         }
9425       else
9426         {
9427           plt = htab->root.iplt;
9428           gotplt = htab->root.igotplt;
9429           relplt = htab->root.irelplt;
9430         }
9431
9432       /* This symbol has an entry in the procedure linkage table.  Set
9433          it up.  */
9434       if ((h->dynindx == -1
9435            && !((h->forced_local || bfd_link_executable (info))
9436                 && h->def_regular
9437                 && h->type == STT_GNU_IFUNC))
9438           || plt == NULL
9439           || gotplt == NULL
9440           || relplt == NULL)
9441         return FALSE;
9442
9443       elfNN_aarch64_create_small_pltn_entry (h, htab, output_bfd, info);
9444       if (!h->def_regular)
9445         {
9446           /* Mark the symbol as undefined, rather than as defined in
9447              the .plt section.  */
9448           sym->st_shndx = SHN_UNDEF;
9449           /* If the symbol is weak we need to clear the value.
9450              Otherwise, the PLT entry would provide a definition for
9451              the symbol even if the symbol wasn't defined anywhere,
9452              and so the symbol would never be NULL.  Leave the value if
9453              there were any relocations where pointer equality matters
9454              (this is a clue for the dynamic linker, to make function
9455              pointer comparisons work between an application and shared
9456              library).  */
9457           if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
9458             sym->st_value = 0;
9459         }
9460     }
9461
9462   if (h->got.offset != (bfd_vma) - 1
9463       && elf_aarch64_hash_entry (h)->got_type == GOT_NORMAL
9464       /* Undefined weak symbol in static PIE resolves to 0 without
9465          any dynamic relocations.  */
9466       && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9467     {
9468       Elf_Internal_Rela rela;
9469       bfd_byte *loc;
9470
9471       /* This symbol has an entry in the global offset table.  Set it
9472          up.  */
9473       if (htab->root.sgot == NULL || htab->root.srelgot == NULL)
9474         abort ();
9475
9476       rela.r_offset = (htab->root.sgot->output_section->vma
9477                        + htab->root.sgot->output_offset
9478                        + (h->got.offset & ~(bfd_vma) 1));
9479
9480       if (h->def_regular
9481           && h->type == STT_GNU_IFUNC)
9482         {
9483           if (bfd_link_pic (info))
9484             {
9485               /* Generate R_AARCH64_GLOB_DAT.  */
9486               goto do_glob_dat;
9487             }
9488           else
9489             {
9490               asection *plt;
9491
9492               if (!h->pointer_equality_needed)
9493                 abort ();
9494
9495               /* For non-shared object, we can't use .got.plt, which
9496                  contains the real function address if we need pointer
9497                  equality.  We load the GOT entry with the PLT entry.  */
9498               plt = htab->root.splt ? htab->root.splt : htab->root.iplt;
9499               bfd_put_NN (output_bfd, (plt->output_section->vma
9500                                        + plt->output_offset
9501                                        + h->plt.offset),
9502                           htab->root.sgot->contents
9503                           + (h->got.offset & ~(bfd_vma) 1));
9504               return TRUE;
9505             }
9506         }
9507       else if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
9508         {
9509           if (!(h->def_regular || ELF_COMMON_DEF_P (h)))
9510             return FALSE;
9511
9512           BFD_ASSERT ((h->got.offset & 1) != 0);
9513           rela.r_info = ELFNN_R_INFO (0, AARCH64_R (RELATIVE));
9514           rela.r_addend = (h->root.u.def.value
9515                            + h->root.u.def.section->output_section->vma
9516                            + h->root.u.def.section->output_offset);
9517         }
9518       else
9519         {
9520 do_glob_dat:
9521           BFD_ASSERT ((h->got.offset & 1) == 0);
9522           bfd_put_NN (output_bfd, (bfd_vma) 0,
9523                       htab->root.sgot->contents + h->got.offset);
9524           rela.r_info = ELFNN_R_INFO (h->dynindx, AARCH64_R (GLOB_DAT));
9525           rela.r_addend = 0;
9526         }
9527
9528       loc = htab->root.srelgot->contents;
9529       loc += htab->root.srelgot->reloc_count++ * RELOC_SIZE (htab);
9530       bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
9531     }
9532
9533   if (h->needs_copy)
9534     {
9535       Elf_Internal_Rela rela;
9536       asection *s;
9537       bfd_byte *loc;
9538
9539       /* This symbol needs a copy reloc.  Set it up.  */
9540       if (h->dynindx == -1
9541           || (h->root.type != bfd_link_hash_defined
9542               && h->root.type != bfd_link_hash_defweak)
9543           || htab->root.srelbss == NULL)
9544         abort ();
9545
9546       rela.r_offset = (h->root.u.def.value
9547                        + h->root.u.def.section->output_section->vma
9548                        + h->root.u.def.section->output_offset);
9549       rela.r_info = ELFNN_R_INFO (h->dynindx, AARCH64_R (COPY));
9550       rela.r_addend = 0;
9551       if (h->root.u.def.section == htab->root.sdynrelro)
9552         s = htab->root.sreldynrelro;
9553       else
9554         s = htab->root.srelbss;
9555       loc = s->contents + s->reloc_count++ * RELOC_SIZE (htab);
9556       bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
9557     }
9558
9559   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  SYM may
9560      be NULL for local symbols.  */
9561   if (sym != NULL
9562       && (h == elf_hash_table (info)->hdynamic
9563           || h == elf_hash_table (info)->hgot))
9564     sym->st_shndx = SHN_ABS;
9565
9566   return TRUE;
9567 }
9568
9569 /* Finish up local dynamic symbol handling.  We set the contents of
9570    various dynamic sections here.  */
9571
9572 static bfd_boolean
9573 elfNN_aarch64_finish_local_dynamic_symbol (void **slot, void *inf)
9574 {
9575   struct elf_link_hash_entry *h
9576     = (struct elf_link_hash_entry *) *slot;
9577   struct bfd_link_info *info
9578     = (struct bfd_link_info *) inf;
9579
9580   return elfNN_aarch64_finish_dynamic_symbol (info->output_bfd,
9581                                               info, h, NULL);
9582 }
9583
9584 static void
9585 elfNN_aarch64_init_small_plt0_entry (bfd *output_bfd ATTRIBUTE_UNUSED,
9586                                      struct elf_aarch64_link_hash_table
9587                                      *htab)
9588 {
9589   /* Fill in PLT0. Fixme:RR Note this doesn't distinguish between
9590      small and large plts and at the minute just generates
9591      the small PLT.  */
9592
9593   /* PLT0 of the small PLT looks like this in ELF64 -
9594      stp x16, x30, [sp, #-16]!          // Save the reloc and lr on stack.
9595      adrp x16, PLT_GOT + 16             // Get the page base of the GOTPLT
9596      ldr  x17, [x16, #:lo12:PLT_GOT+16] // Load the address of the
9597                                         // symbol resolver
9598      add  x16, x16, #:lo12:PLT_GOT+16   // Load the lo12 bits of the
9599                                         // GOTPLT entry for this.
9600      br   x17
9601      PLT0 will be slightly different in ELF32 due to different got entry
9602      size.  */
9603   bfd_vma plt_got_2nd_ent;      /* Address of GOT[2].  */
9604   bfd_vma plt_base;
9605
9606
9607   memcpy (htab->root.splt->contents, htab->plt0_entry,
9608           htab->plt_header_size);
9609   elf_section_data (htab->root.splt->output_section)->this_hdr.sh_entsize =
9610     htab->plt_header_size;
9611
9612   plt_got_2nd_ent = (htab->root.sgotplt->output_section->vma
9613                   + htab->root.sgotplt->output_offset
9614                   + GOT_ENTRY_SIZE * 2);
9615
9616   plt_base = htab->root.splt->output_section->vma +
9617     htab->root.splt->output_offset;
9618
9619   /* First instruction in BTI enabled PLT stub is a BTI
9620      instruction so skip it.  */
9621   bfd_byte *plt0_entry = htab->root.splt->contents;
9622   if (elf_aarch64_tdata (output_bfd)->plt_type & PLT_BTI)
9623     plt0_entry = plt0_entry + 4;
9624
9625   /* Fill in the top 21 bits for this: ADRP x16, PLT_GOT + n * 8.
9626      ADRP:   ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
9627   elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADR_HI21_PCREL,
9628                                 plt0_entry + 4,
9629                                 PG (plt_got_2nd_ent) - PG (plt_base + 4));
9630
9631   elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_LDSTNN_LO12,
9632                                 plt0_entry + 8,
9633                                 PG_OFFSET (plt_got_2nd_ent));
9634
9635   elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADD_LO12,
9636                                 plt0_entry + 12,
9637                                 PG_OFFSET (plt_got_2nd_ent));
9638 }
9639
9640 static bfd_boolean
9641 elfNN_aarch64_finish_dynamic_sections (bfd *output_bfd,
9642                                        struct bfd_link_info *info)
9643 {
9644   struct elf_aarch64_link_hash_table *htab;
9645   bfd *dynobj;
9646   asection *sdyn;
9647
9648   htab = elf_aarch64_hash_table (info);
9649   dynobj = htab->root.dynobj;
9650   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
9651
9652   if (htab->root.dynamic_sections_created)
9653     {
9654       ElfNN_External_Dyn *dyncon, *dynconend;
9655
9656       if (sdyn == NULL || htab->root.sgot == NULL)
9657         abort ();
9658
9659       dyncon = (ElfNN_External_Dyn *) sdyn->contents;
9660       dynconend = (ElfNN_External_Dyn *) (sdyn->contents + sdyn->size);
9661       for (; dyncon < dynconend; dyncon++)
9662         {
9663           Elf_Internal_Dyn dyn;
9664           asection *s;
9665
9666           bfd_elfNN_swap_dyn_in (dynobj, dyncon, &dyn);
9667
9668           switch (dyn.d_tag)
9669             {
9670             default:
9671               continue;
9672
9673             case DT_PLTGOT:
9674               s = htab->root.sgotplt;
9675               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
9676               break;
9677
9678             case DT_JMPREL:
9679               s = htab->root.srelplt;
9680               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
9681               break;
9682
9683             case DT_PLTRELSZ:
9684               s = htab->root.srelplt;
9685               dyn.d_un.d_val = s->size;
9686               break;
9687
9688             case DT_TLSDESC_PLT:
9689               s = htab->root.splt;
9690               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
9691                 + htab->tlsdesc_plt;
9692               break;
9693
9694             case DT_TLSDESC_GOT:
9695               s = htab->root.sgot;
9696               BFD_ASSERT (htab->dt_tlsdesc_got != (bfd_vma)-1);
9697               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
9698                 + htab->dt_tlsdesc_got;
9699               break;
9700             }
9701
9702           bfd_elfNN_swap_dyn_out (output_bfd, &dyn, dyncon);
9703         }
9704
9705     }
9706
9707   /* Fill in the special first entry in the procedure linkage table.  */
9708   if (htab->root.splt && htab->root.splt->size > 0)
9709     {
9710       elfNN_aarch64_init_small_plt0_entry (output_bfd, htab);
9711
9712       elf_section_data (htab->root.splt->output_section)->
9713         this_hdr.sh_entsize = htab->plt_entry_size;
9714
9715
9716       if (htab->tlsdesc_plt && !(info->flags & DF_BIND_NOW))
9717         {
9718           BFD_ASSERT (htab->dt_tlsdesc_got != (bfd_vma)-1);
9719           bfd_put_NN (output_bfd, (bfd_vma) 0,
9720                       htab->root.sgot->contents + htab->dt_tlsdesc_got);
9721
9722           const bfd_byte *entry = elfNN_aarch64_tlsdesc_small_plt_entry;
9723           htab->tlsdesc_plt_entry_size = PLT_TLSDESC_ENTRY_SIZE;
9724
9725           aarch64_plt_type type = elf_aarch64_tdata (output_bfd)->plt_type;
9726           if (type == PLT_BTI || type == PLT_BTI_PAC)
9727             {
9728               entry = elfNN_aarch64_tlsdesc_small_plt_bti_entry;
9729             }
9730
9731           memcpy (htab->root.splt->contents + htab->tlsdesc_plt,
9732                   entry, htab->tlsdesc_plt_entry_size);
9733
9734           {
9735             bfd_vma adrp1_addr =
9736               htab->root.splt->output_section->vma
9737               + htab->root.splt->output_offset + htab->tlsdesc_plt + 4;
9738
9739             bfd_vma adrp2_addr = adrp1_addr + 4;
9740
9741             bfd_vma got_addr =
9742               htab->root.sgot->output_section->vma
9743               + htab->root.sgot->output_offset;
9744
9745             bfd_vma pltgot_addr =
9746               htab->root.sgotplt->output_section->vma
9747               + htab->root.sgotplt->output_offset;
9748
9749             bfd_vma dt_tlsdesc_got = got_addr + htab->dt_tlsdesc_got;
9750
9751             bfd_byte *plt_entry =
9752               htab->root.splt->contents + htab->tlsdesc_plt;
9753
9754            /* First instruction in BTI enabled PLT stub is a BTI
9755               instruction so skip it.  */
9756             if (type & PLT_BTI)
9757               {
9758                 plt_entry = plt_entry + 4;
9759                 adrp1_addr = adrp1_addr + 4;
9760                 adrp2_addr = adrp2_addr + 4;
9761               }
9762
9763             /* adrp x2, DT_TLSDESC_GOT */
9764             elf_aarch64_update_plt_entry (output_bfd,
9765                                           BFD_RELOC_AARCH64_ADR_HI21_PCREL,
9766                                           plt_entry + 4,
9767                                           (PG (dt_tlsdesc_got)
9768                                            - PG (adrp1_addr)));
9769
9770             /* adrp x3, 0 */
9771             elf_aarch64_update_plt_entry (output_bfd,
9772                                           BFD_RELOC_AARCH64_ADR_HI21_PCREL,
9773                                           plt_entry + 8,
9774                                           (PG (pltgot_addr)
9775                                            - PG (adrp2_addr)));
9776
9777             /* ldr x2, [x2, #0] */
9778             elf_aarch64_update_plt_entry (output_bfd,
9779                                           BFD_RELOC_AARCH64_LDSTNN_LO12,
9780                                           plt_entry + 12,
9781                                           PG_OFFSET (dt_tlsdesc_got));
9782
9783             /* add x3, x3, 0 */
9784             elf_aarch64_update_plt_entry (output_bfd,
9785                                           BFD_RELOC_AARCH64_ADD_LO12,
9786                                           plt_entry + 16,
9787                                           PG_OFFSET (pltgot_addr));
9788           }
9789         }
9790     }
9791
9792   if (htab->root.sgotplt)
9793     {
9794       if (bfd_is_abs_section (htab->root.sgotplt->output_section))
9795         {
9796           _bfd_error_handler
9797             (_("discarded output section: `%pA'"), htab->root.sgotplt);
9798           return FALSE;
9799         }
9800
9801       /* Fill in the first three entries in the global offset table.  */
9802       if (htab->root.sgotplt->size > 0)
9803         {
9804           bfd_put_NN (output_bfd, (bfd_vma) 0, htab->root.sgotplt->contents);
9805
9806           /* Write GOT[1] and GOT[2], needed for the dynamic linker.  */
9807           bfd_put_NN (output_bfd,
9808                       (bfd_vma) 0,
9809                       htab->root.sgotplt->contents + GOT_ENTRY_SIZE);
9810           bfd_put_NN (output_bfd,
9811                       (bfd_vma) 0,
9812                       htab->root.sgotplt->contents + GOT_ENTRY_SIZE * 2);
9813         }
9814
9815       if (htab->root.sgot)
9816         {
9817           if (htab->root.sgot->size > 0)
9818             {
9819               bfd_vma addr =
9820                 sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0;
9821               bfd_put_NN (output_bfd, addr, htab->root.sgot->contents);
9822             }
9823         }
9824
9825       elf_section_data (htab->root.sgotplt->output_section)->
9826         this_hdr.sh_entsize = GOT_ENTRY_SIZE;
9827     }
9828
9829   if (htab->root.sgot && htab->root.sgot->size > 0)
9830     elf_section_data (htab->root.sgot->output_section)->this_hdr.sh_entsize
9831       = GOT_ENTRY_SIZE;
9832
9833   /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols.  */
9834   htab_traverse (htab->loc_hash_table,
9835                  elfNN_aarch64_finish_local_dynamic_symbol,
9836                  info);
9837
9838   return TRUE;
9839 }
9840
9841 /* Check if BTI enabled PLTs are needed.  Returns the type needed.  */
9842 static aarch64_plt_type
9843 get_plt_type (bfd *abfd)
9844 {
9845   aarch64_plt_type ret = PLT_NORMAL;
9846   bfd_byte *contents, *extdyn, *extdynend;
9847   asection *sec = bfd_get_section_by_name (abfd, ".dynamic");
9848   if (!sec || !bfd_malloc_and_get_section (abfd, sec, &contents))
9849     return ret;
9850   extdyn = contents;
9851   extdynend = contents + sec->size;
9852   for (; extdyn < extdynend; extdyn += sizeof (ElfNN_External_Dyn))
9853     {
9854       Elf_Internal_Dyn dyn;
9855       bfd_elfNN_swap_dyn_in (abfd, extdyn, &dyn);
9856
9857       /* Let's check the processor specific dynamic array tags.  */
9858       bfd_vma tag = dyn.d_tag;
9859       if (tag < DT_LOPROC || tag > DT_HIPROC)
9860         continue;
9861
9862       switch (tag)
9863         {
9864         case DT_AARCH64_BTI_PLT:
9865           ret |= PLT_BTI;
9866           break;
9867
9868         case DT_AARCH64_PAC_PLT:
9869           ret |= PLT_PAC;
9870           break;
9871
9872         default: break;
9873         }
9874     }
9875   free (contents);
9876   return ret;
9877 }
9878
9879 static long
9880 elfNN_aarch64_get_synthetic_symtab (bfd *abfd,
9881                                     long symcount,
9882                                     asymbol **syms,
9883                                     long dynsymcount,
9884                                     asymbol **dynsyms,
9885                                     asymbol **ret)
9886 {
9887   elf_aarch64_tdata (abfd)->plt_type = get_plt_type (abfd);
9888   return _bfd_elf_get_synthetic_symtab (abfd, symcount, syms,
9889                                         dynsymcount, dynsyms, ret);
9890 }
9891
9892 /* Return address for Ith PLT stub in section PLT, for relocation REL
9893    or (bfd_vma) -1 if it should not be included.  */
9894
9895 static bfd_vma
9896 elfNN_aarch64_plt_sym_val (bfd_vma i, const asection *plt,
9897                            const arelent *rel ATTRIBUTE_UNUSED)
9898 {
9899   size_t plt0_size = PLT_ENTRY_SIZE;
9900   size_t pltn_size = PLT_SMALL_ENTRY_SIZE;
9901
9902   if (elf_aarch64_tdata (plt->owner)->plt_type == PLT_BTI_PAC)
9903     {
9904       if (elf_elfheader (plt->owner)->e_type == ET_EXEC)
9905         pltn_size = PLT_BTI_PAC_SMALL_ENTRY_SIZE;
9906       else
9907         pltn_size = PLT_PAC_SMALL_ENTRY_SIZE;
9908     }
9909   else if (elf_aarch64_tdata (plt->owner)->plt_type == PLT_BTI)
9910     {
9911       if (elf_elfheader (plt->owner)->e_type == ET_EXEC)
9912         pltn_size = PLT_BTI_SMALL_ENTRY_SIZE;
9913     }
9914   else if (elf_aarch64_tdata (plt->owner)->plt_type == PLT_PAC)
9915     {
9916       pltn_size = PLT_PAC_SMALL_ENTRY_SIZE;
9917     }
9918
9919   return plt->vma + plt0_size + i * pltn_size;
9920 }
9921
9922 /* Returns TRUE if NAME is an AArch64 mapping symbol.
9923    The ARM ELF standard defines $x (for A64 code) and $d (for data).
9924    It also allows a period initiated suffix to be added to the symbol, ie:
9925    "$[adtx]\.[:sym_char]+".  */
9926
9927 static bfd_boolean
9928 is_aarch64_mapping_symbol (const char * name)
9929 {
9930   return name != NULL /* Paranoia.  */
9931     && name[0] == '$' /* Note: if objcopy --prefix-symbols has been used then
9932                          the mapping symbols could have acquired a prefix.
9933                          We do not support this here, since such symbols no
9934                          longer conform to the ARM ELF ABI.  */
9935     && (name[1] == 'd' || name[1] == 'x')
9936     && (name[2] == 0 || name[2] == '.');
9937   /* FIXME: Strictly speaking the symbol is only a valid mapping symbol if
9938      any characters that follow the period are legal characters for the body
9939      of a symbol's name.  For now we just assume that this is the case.  */
9940 }
9941
9942 /* Make sure that mapping symbols in object files are not removed via the
9943    "strip --strip-unneeded" tool.  These symbols might needed in order to
9944    correctly generate linked files.  Once an object file has been linked,
9945    it should be safe to remove them.  */
9946
9947 static void
9948 elfNN_aarch64_backend_symbol_processing (bfd *abfd, asymbol *sym)
9949 {
9950   if (((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
9951       && sym->section != bfd_abs_section_ptr
9952       && is_aarch64_mapping_symbol (sym->name))
9953     sym->flags |= BSF_KEEP;
9954 }
9955
9956 /* Implement elf_backend_setup_gnu_properties for AArch64.  It serves as a
9957    wrapper function for _bfd_aarch64_elf_link_setup_gnu_properties to account
9958    for the effect of GNU properties of the output_bfd.  */
9959 static bfd *
9960 elfNN_aarch64_link_setup_gnu_properties (struct bfd_link_info *info)
9961 {
9962   uint32_t prop = elf_aarch64_tdata (info->output_bfd)->gnu_and_prop;
9963   bfd *pbfd = _bfd_aarch64_elf_link_setup_gnu_properties (info, &prop);
9964   elf_aarch64_tdata (info->output_bfd)->gnu_and_prop = prop;
9965   elf_aarch64_tdata (info->output_bfd)->plt_type
9966     |= (prop & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) ? PLT_BTI : 0;
9967   setup_plt_values (info, elf_aarch64_tdata (info->output_bfd)->plt_type);
9968   return pbfd;
9969 }
9970
9971 /* Implement elf_backend_merge_gnu_properties for AArch64.  It serves as a
9972    wrapper function for _bfd_aarch64_elf_merge_gnu_properties to account
9973    for the effect of GNU properties of the output_bfd.  */
9974 static bfd_boolean
9975 elfNN_aarch64_merge_gnu_properties (struct bfd_link_info *info,
9976                                        bfd *abfd, bfd *bbfd,
9977                                        elf_property *aprop,
9978                                        elf_property *bprop)
9979 {
9980   uint32_t prop
9981     = elf_aarch64_tdata (info->output_bfd)->gnu_and_prop;
9982
9983   /* If output has been marked with BTI using command line argument, give out
9984      warning if necessary.  */
9985   /* Properties are merged per type, hence only check for warnings when merging
9986      GNU_PROPERTY_AARCH64_FEATURE_1_AND.  */
9987   if (((aprop && aprop->pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
9988         || (bprop && bprop->pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND))
9989       && (prop & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
9990       && (!elf_aarch64_tdata (info->output_bfd)->no_bti_warn))
9991     {
9992       if ((aprop && !(aprop->u.number & GNU_PROPERTY_AARCH64_FEATURE_1_BTI))
9993            || !aprop)
9994         {
9995           _bfd_error_handler (_("%pB: warning: BTI turned on by -z force-bti when "
9996                                 "all inputs do not have BTI in NOTE section."),
9997                               abfd);
9998         }
9999       if ((bprop && !(bprop->u.number & GNU_PROPERTY_AARCH64_FEATURE_1_BTI))
10000            || !bprop)
10001         {
10002           _bfd_error_handler (_("%pB: warning: BTI turned on by -z force-bti when "
10003                                 "all inputs do not have BTI in NOTE section."),
10004                               bbfd);
10005         }
10006     }
10007
10008   return  _bfd_aarch64_elf_merge_gnu_properties (info, abfd, aprop,
10009                                                  bprop, prop);
10010 }
10011
10012 /* We use this so we can override certain functions
10013    (though currently we don't).  */
10014
10015 const struct elf_size_info elfNN_aarch64_size_info =
10016 {
10017   sizeof (ElfNN_External_Ehdr),
10018   sizeof (ElfNN_External_Phdr),
10019   sizeof (ElfNN_External_Shdr),
10020   sizeof (ElfNN_External_Rel),
10021   sizeof (ElfNN_External_Rela),
10022   sizeof (ElfNN_External_Sym),
10023   sizeof (ElfNN_External_Dyn),
10024   sizeof (Elf_External_Note),
10025   4,                            /* Hash table entry size.  */
10026   1,                            /* Internal relocs per external relocs.  */
10027   ARCH_SIZE,                    /* Arch size.  */
10028   LOG_FILE_ALIGN,               /* Log_file_align.  */
10029   ELFCLASSNN, EV_CURRENT,
10030   bfd_elfNN_write_out_phdrs,
10031   bfd_elfNN_write_shdrs_and_ehdr,
10032   bfd_elfNN_checksum_contents,
10033   bfd_elfNN_write_relocs,
10034   bfd_elfNN_swap_symbol_in,
10035   bfd_elfNN_swap_symbol_out,
10036   bfd_elfNN_slurp_reloc_table,
10037   bfd_elfNN_slurp_symbol_table,
10038   bfd_elfNN_swap_dyn_in,
10039   bfd_elfNN_swap_dyn_out,
10040   bfd_elfNN_swap_reloc_in,
10041   bfd_elfNN_swap_reloc_out,
10042   bfd_elfNN_swap_reloca_in,
10043   bfd_elfNN_swap_reloca_out
10044 };
10045
10046 #define ELF_ARCH                        bfd_arch_aarch64
10047 #define ELF_MACHINE_CODE                EM_AARCH64
10048 #define ELF_MAXPAGESIZE                 0x10000
10049 #define ELF_MINPAGESIZE                 0x1000
10050 #define ELF_COMMONPAGESIZE              0x1000
10051
10052 #define bfd_elfNN_close_and_cleanup             \
10053   elfNN_aarch64_close_and_cleanup
10054
10055 #define bfd_elfNN_bfd_free_cached_info          \
10056   elfNN_aarch64_bfd_free_cached_info
10057
10058 #define bfd_elfNN_bfd_is_target_special_symbol  \
10059   elfNN_aarch64_is_target_special_symbol
10060
10061 #define bfd_elfNN_bfd_link_hash_table_create    \
10062   elfNN_aarch64_link_hash_table_create
10063
10064 #define bfd_elfNN_bfd_merge_private_bfd_data    \
10065   elfNN_aarch64_merge_private_bfd_data
10066
10067 #define bfd_elfNN_bfd_print_private_bfd_data    \
10068   elfNN_aarch64_print_private_bfd_data
10069
10070 #define bfd_elfNN_bfd_reloc_type_lookup         \
10071   elfNN_aarch64_reloc_type_lookup
10072
10073 #define bfd_elfNN_bfd_reloc_name_lookup         \
10074   elfNN_aarch64_reloc_name_lookup
10075
10076 #define bfd_elfNN_bfd_set_private_flags         \
10077   elfNN_aarch64_set_private_flags
10078
10079 #define bfd_elfNN_find_inliner_info             \
10080   elfNN_aarch64_find_inliner_info
10081
10082 #define bfd_elfNN_find_nearest_line             \
10083   elfNN_aarch64_find_nearest_line
10084
10085 #define bfd_elfNN_get_synthetic_symtab          \
10086   elfNN_aarch64_get_synthetic_symtab
10087
10088 #define bfd_elfNN_mkobject                      \
10089   elfNN_aarch64_mkobject
10090
10091 #define bfd_elfNN_new_section_hook              \
10092   elfNN_aarch64_new_section_hook
10093
10094 #define elf_backend_adjust_dynamic_symbol       \
10095   elfNN_aarch64_adjust_dynamic_symbol
10096
10097 #define elf_backend_always_size_sections        \
10098   elfNN_aarch64_always_size_sections
10099
10100 #define elf_backend_check_relocs                \
10101   elfNN_aarch64_check_relocs
10102
10103 #define elf_backend_copy_indirect_symbol        \
10104   elfNN_aarch64_copy_indirect_symbol
10105
10106 #define elf_backend_merge_symbol_attribute      \
10107   elfNN_aarch64_merge_symbol_attribute
10108
10109 /* Create .dynbss, and .rela.bss sections in DYNOBJ, and set up shortcuts
10110    to them in our hash.  */
10111 #define elf_backend_create_dynamic_sections     \
10112   elfNN_aarch64_create_dynamic_sections
10113
10114 #define elf_backend_init_index_section          \
10115   _bfd_elf_init_2_index_sections
10116
10117 #define elf_backend_finish_dynamic_sections     \
10118   elfNN_aarch64_finish_dynamic_sections
10119
10120 #define elf_backend_finish_dynamic_symbol       \
10121   elfNN_aarch64_finish_dynamic_symbol
10122
10123 #define elf_backend_object_p                    \
10124   elfNN_aarch64_object_p
10125
10126 #define elf_backend_output_arch_local_syms      \
10127   elfNN_aarch64_output_arch_local_syms
10128
10129 #define elf_backend_plt_sym_val                 \
10130   elfNN_aarch64_plt_sym_val
10131
10132 #define elf_backend_post_process_headers        \
10133   elfNN_aarch64_post_process_headers
10134
10135 #define elf_backend_relocate_section            \
10136   elfNN_aarch64_relocate_section
10137
10138 #define elf_backend_reloc_type_class            \
10139   elfNN_aarch64_reloc_type_class
10140
10141 #define elf_backend_section_from_shdr           \
10142   elfNN_aarch64_section_from_shdr
10143
10144 #define elf_backend_size_dynamic_sections       \
10145   elfNN_aarch64_size_dynamic_sections
10146
10147 #define elf_backend_size_info                   \
10148   elfNN_aarch64_size_info
10149
10150 #define elf_backend_write_section               \
10151   elfNN_aarch64_write_section
10152
10153 #define elf_backend_symbol_processing           \
10154   elfNN_aarch64_backend_symbol_processing
10155
10156 #define elf_backend_setup_gnu_properties        \
10157   elfNN_aarch64_link_setup_gnu_properties
10158
10159 #define elf_backend_merge_gnu_properties        \
10160   elfNN_aarch64_merge_gnu_properties
10161
10162 #define elf_backend_can_refcount       1
10163 #define elf_backend_can_gc_sections    1
10164 #define elf_backend_plt_readonly       1
10165 #define elf_backend_want_got_plt       1
10166 #define elf_backend_want_plt_sym       0
10167 #define elf_backend_want_dynrelro      1
10168 #define elf_backend_may_use_rel_p      0
10169 #define elf_backend_may_use_rela_p     1
10170 #define elf_backend_default_use_rela_p 1
10171 #define elf_backend_rela_normal        1
10172 #define elf_backend_dtrel_excludes_plt 1
10173 #define elf_backend_got_header_size (GOT_ENTRY_SIZE * 3)
10174 #define elf_backend_default_execstack  0
10175 #define elf_backend_extern_protected_data 1
10176 #define elf_backend_hash_symbol elf_aarch64_hash_symbol
10177
10178 #undef  elf_backend_obj_attrs_section
10179 #define elf_backend_obj_attrs_section           ".ARM.attributes"
10180
10181 #include "elfNN-target.h"
10182
10183 /* CloudABI support.  */
10184
10185 #undef  TARGET_LITTLE_SYM
10186 #define TARGET_LITTLE_SYM       aarch64_elfNN_le_cloudabi_vec
10187 #undef  TARGET_LITTLE_NAME
10188 #define TARGET_LITTLE_NAME      "elfNN-littleaarch64-cloudabi"
10189 #undef  TARGET_BIG_SYM
10190 #define TARGET_BIG_SYM          aarch64_elfNN_be_cloudabi_vec
10191 #undef  TARGET_BIG_NAME
10192 #define TARGET_BIG_NAME         "elfNN-bigaarch64-cloudabi"
10193
10194 #undef  ELF_OSABI
10195 #define ELF_OSABI               ELFOSABI_CLOUDABI
10196
10197 #undef  elfNN_bed
10198 #define elfNN_bed               elfNN_aarch64_cloudabi_bed
10199
10200 #include "elfNN-target.h"