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