]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elfnn-aarch64.c
ld, aarch64: Account for stubs in bounds sizing
[thirdparty/binutils-gdb.git] / bfd / elfnn-aarch64.c
CommitLineData
cec5225b 1/* AArch64-specific support for NN-bit ELF.
b3adc24a 2 Copyright (C) 2009-2020 Free Software Foundation, Inc.
a06ea964
NC
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
07d6d2b8 34 R_AARCH64_TLSGD_ADR_PAGE21(foo)
a06ea964 35 add x0, :tlsgd_lo12:foo
07d6d2b8 36 R_AARCH64_TLSGD_ADD_LO12_NC(foo)
a06ea964
NC
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
07d6d2b8
AM
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)
a06ea964 46 .tlsdesccall foo
07d6d2b8 47 blr x1 R_AARCH64_TLSDESC_CALL(foo)
a06ea964
NC
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
a6bb11b2 53 The relocations R_AARCH64_TLSDESC_{ADR_PAGE21,LD64_LO12_NC,ADD_LO12_NC}
a06ea964
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
a6bb11b2 67 entries. The static linker places the relocation R_AARCH64_TLS_DTPMOD
a06ea964
NC
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
a6bb11b2 72 R_AARCH64_TLS_DTPREL relocation on the offset entry. The loader
a06ea964
NC
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
cec5225b 95 elfNN_aarch64_check_relocs()
a06ea964
NC
96
97 This function is invoked for each relocation.
98
99 The TLS relocations R_AARCH64_TLSGD_{ADR_PREL21,ADD_LO12_NC} and
a6bb11b2 100 R_AARCH64_TLSDESC_{ADR_PAGE21,LD64_LO12_NC,ADD_LO12_NC} are
a06ea964
NC
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
cec5225b 107 elfNN_aarch64_allocate_dynrelocs ()
a06ea964
NC
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
cec5225b 115 elfNN_aarch64_size_dynamic_sections ()
a06ea964
NC
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
cec5225b 122 elfNN_aarch64_relocate_section ()
a06ea964 123
cec5225b 124 Calls elfNN_aarch64_final_link_relocate ()
a06ea964
NC
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
cec5225b 134 elfNN_aarch64_final_link_relocate ()
a06ea964
NC
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"
a06ea964
NC
142#include "elf-bfd.h"
143#include "bfdlink.h"
1419bbe5 144#include "objalloc.h"
a06ea964 145#include "elf/aarch64.h"
caed7120 146#include "elfxx-aarch64.h"
a8bfaadb 147#include "cpu-aarch64.h"
a06ea964 148
cec5225b
YZ
149#define ARCH_SIZE NN
150
151#if ARCH_SIZE == 64
152#define AARCH64_R(NAME) R_AARCH64_ ## NAME
153#define AARCH64_R_STR(NAME) "R_AARCH64_" #NAME
a6bb11b2
YZ
154#define HOWTO64(...) HOWTO (__VA_ARGS__)
155#define HOWTO32(...) EMPTY_HOWTO (0)
cec5225b 156#define LOG_FILE_ALIGN 3
f955cccf 157#define BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC BFD_RELOC_AARCH64_TLSDESC_LD64_LO12
cec5225b
YZ
158#endif
159
f7d2c675
SP
160#define MORELLO_R(NAME) R_MORELLO_ ## NAME
161#define MORELLO_R_STR(NAME) "R_MORELLO_" #NAME
162
cec5225b
YZ
163#if ARCH_SIZE == 32
164#define AARCH64_R(NAME) R_AARCH64_P32_ ## NAME
165#define AARCH64_R_STR(NAME) "R_AARCH64_P32_" #NAME
a6bb11b2
YZ
166#define HOWTO64(...) EMPTY_HOWTO (0)
167#define HOWTO32(...) HOWTO (__VA_ARGS__)
cec5225b 168#define LOG_FILE_ALIGN 2
07d6d2b8
AM
169#define BFD_RELOC_AARCH64_TLSDESC_LD32_LO12 BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC
170#define R_AARCH64_P32_TLSDESC_ADD_LO12 R_AARCH64_P32_TLSDESC_ADD_LO12_NC
cec5225b
YZ
171#endif
172
a6bb11b2 173#define IS_AARCH64_TLS_RELOC(R_TYPE) \
4c0a9a6f
JW
174 ((R_TYPE) == BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC \
175 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21 \
3c12b054 176 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_ADR_PREL21 \
3e8286c0 177 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC \
1aa66fb1 178 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_MOVW_G1 \
1408e112
MM
179 || (R_TYPE) == BFD_RELOC_MORELLO_TLSIE_ADR_GOTTPREL_PAGE20 \
180 || (R_TYPE) == BFD_RELOC_MORELLO_TLSIE_ADD_LO12 \
a6bb11b2 181 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 \
a6bb11b2 182 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC \
4c0a9a6f 183 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC \
a6bb11b2 184 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19 \
4c0a9a6f
JW
185 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC \
186 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1 \
6ffe9a1b 187 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12 \
40fbed84 188 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12 \
753999c1 189 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC \
73f925cc 190 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC \
f69e4920 191 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21 \
77a69ff8 192 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_ADR_PREL21 \
07c9aa07
JW
193 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12 \
194 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC \
195 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12 \
196 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC \
197 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12 \
198 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC \
199 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12 \
200 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC \
6ffe9a1b
JW
201 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0 \
202 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC \
203 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1 \
204 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC \
205 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2 \
26c32295
MM
206 || (R_TYPE) == BFD_RELOC_AARCH64_TLS_DTPMOD \
207 || (R_TYPE) == BFD_RELOC_AARCH64_TLS_DTPREL \
208 || (R_TYPE) == BFD_RELOC_AARCH64_TLS_TPREL \
209 || IS_AARCH64_TLSLE_RELOC ((R_TYPE)) \
210 || IS_AARCH64_TLSDESC_RELOC ((R_TYPE)))
211
212#define IS_AARCH64_TLSLE_RELOC(R_TYPE) \
213 ((R_TYPE) == BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12 \
4c0a9a6f 214 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12 \
a6bb11b2 215 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC \
e04ef022
RL
216 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12 \
217 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12_NC \
218 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12 \
219 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12_NC \
220 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12 \
221 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12_NC \
222 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12 \
223 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12_NC \
a6bb11b2
YZ
224 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0 \
225 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC \
4c0a9a6f
JW
226 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1 \
227 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC \
26c32295 228 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2)
a06ea964 229
9331eea1 230#define IS_AARCH64_TLS_RELAX_RELOC(R_TYPE) \
f955cccf
NC
231 ((R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADD \
232 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADD_LO12 \
4ca9b406 233 || (R_TYPE) == BFD_RELOC_MORELLO_TLSDESC_ADR_PAGE20 \
4af68b9c
JW
234 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21 \
235 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21 \
4ca9b406 236 || (R_TYPE) == BFD_RELOC_MORELLO_TLSDESC_CALL \
4af68b9c
JW
237 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_CALL \
238 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LD_PREL19 \
4ca9b406 239 || (R_TYPE) == BFD_RELOC_MORELLO_TLSDESC_LD128_LO12 \
4af68b9c 240 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC \
0484b454
RL
241 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LDR \
242 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC \
243 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_OFF_G1 \
244 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LDR \
4af68b9c 245 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21 \
9331eea1
JW
246 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_ADR_PREL21 \
247 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC \
ac734732
RL
248 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC \
249 || (R_TYPE) == BFD_RELOC_AARCH64_TLSGD_MOVW_G1 \
1408e112
MM
250 || (R_TYPE) == BFD_RELOC_MORELLO_TLSIE_ADR_GOTTPREL_PAGE20 \
251 || (R_TYPE) == BFD_RELOC_MORELLO_TLSIE_ADD_LO12 \
9331eea1
JW
252 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 \
253 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19 \
254 || (R_TYPE) == BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC \
259364ad
JW
255 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC \
256 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21 \
4af68b9c 257 || (R_TYPE) == BFD_RELOC_AARCH64_TLSLD_ADR_PREL21)
9331eea1 258
a6bb11b2 259#define IS_AARCH64_TLSDESC_RELOC(R_TYPE) \
4c0a9a6f
JW
260 ((R_TYPE) == BFD_RELOC_AARCH64_TLSDESC \
261 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADD \
f955cccf 262 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADD_LO12 \
a6bb11b2 263 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21 \
4ca9b406 264 || (R_TYPE) == BFD_RELOC_MORELLO_TLSDESC_ADR_PAGE20 \
389b8029 265 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21 \
4c0a9a6f 266 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_CALL \
4ca9b406 267 || (R_TYPE) == BFD_RELOC_MORELLO_TLSDESC_CALL \
a6bb11b2 268 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC \
f955cccf 269 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LD64_LO12 \
4ca9b406 270 || (R_TYPE) == BFD_RELOC_MORELLO_TLSDESC_LD128_LO12 \
a6bb11b2 271 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LDR \
4c0a9a6f
JW
272 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_LD_PREL19 \
273 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC \
274 || (R_TYPE) == BFD_RELOC_AARCH64_TLSDESC_OFF_G1)
a06ea964 275
1408e112
MM
276#define IS_MORELLO_SIZE_RELOC(R_TYPE) \
277 ((R_TYPE) == BFD_RELOC_MORELLO_MOVW_SIZE_G0 \
278 || (R_TYPE) == BFD_RELOC_MORELLO_MOVW_SIZE_G0_NC \
279 || (R_TYPE) == BFD_RELOC_MORELLO_MOVW_SIZE_G1 \
280 || (R_TYPE) == BFD_RELOC_MORELLO_MOVW_SIZE_G1_NC \
281 || (R_TYPE) == BFD_RELOC_MORELLO_MOVW_SIZE_G2 \
282 || (R_TYPE) == BFD_RELOC_MORELLO_MOVW_SIZE_G2_NC \
283 || (R_TYPE) == BFD_RELOC_MORELLO_MOVW_SIZE_G3) \
284
6353d82b 285#define ELIMINATE_COPY_RELOCS 1
a06ea964 286
a06ea964 287/* Return size of a relocation entry. HTAB is the bfd's
cec5225b
YZ
288 elf_aarch64_link_hash_entry. */
289#define RELOC_SIZE(HTAB) (sizeof (ElfNN_External_Rela))
a06ea964 290
a1bdea65
SP
291/* GOT Entry size - 16 bytes in C64, 8 bytes in ELF64 and 4 bytes in ELF32. */
292#define GOT_ENTRY_SIZE(htab) (ARCH_SIZE >> (3 - htab->c64_rel))
293#define GOT_RESERVED_HEADER_SLOTS (3)
07d6d2b8
AM
294#define PLT_ENTRY_SIZE (32)
295#define PLT_SMALL_ENTRY_SIZE (16)
296#define PLT_TLSDESC_ENTRY_SIZE (32)
37c18eed 297/* PLT sizes with BTI insn. */
68bb0359 298#define PLT_BTI_SMALL_ENTRY_SIZE (24)
1dbade74 299/* PLT sizes with PAC insn. */
68bb0359 300#define PLT_PAC_SMALL_ENTRY_SIZE (24)
1dbade74 301/* PLT sizes with BTI and PAC insn. */
1dbade74 302#define PLT_BTI_PAC_SMALL_ENTRY_SIZE (24)
a06ea964 303
2d0ca824 304/* Encoding of the nop instruction. */
a06ea964
NC
305#define INSN_NOP 0xd503201f
306
1408e112
MM
307/* This is just a neater name for something we want to check here. The
308 difference between SYMBOL_CALLS_LOCAL and SYMBOL_REFERENCES_LOCAL is only in
309 their treating of protected symbols. For SYMBOL_REFERENCES_LOCAL protected
310 symbols are not treated as known to reference locally. This is because in
311 the case that the symbol is a function symbol it is possible that
312 `&protected_sym` could return an address in an executable (after function
313 equality has necessitated making the canonical address of that function the
314 PLT entry in the running executable).
315
316 SYMBOL_CALLS_LOCAL does not have the same treatment of protected symbols
317 since we know we will always *call* a protected symbol.
318
319 For TLS symbols we do not need to worry about this, since they can not be
320 function symbols. But we don't want to have a confusing name asking whether
321 we will be calling a TLS symbol, so we rename it to
322 TLS_SYMBOL_REFERENCES_LOCAL. */
323#define TLS_SYMBOL_REFERENCES_LOCAL(INFO, H) \
324 SYMBOL_CALLS_LOCAL ((INFO), (H))
325
a06ea964
NC
326#define aarch64_compute_jump_table_size(htab) \
327 (((htab)->root.srelplt == NULL) ? 0 \
a1bdea65 328 : (htab)->root.srelplt->reloc_count * GOT_ENTRY_SIZE (htab))
a06ea964 329
aeba84b1
MM
330/* Macro to check for a static non-PIE binary. Checking for this in incorrect
331 ways is something that has been the cause of a few bugs throughout Morello
332 development. Making a macro for the check should help make this easier to
333 check.
334 N.b. this macro can only be called after symbols have been loaded by the
335 generic linker. In practice this is not much of a restriction, since the
336 check_relocs, size_dynamic_sections, and relocate_section hooks are all done
337 after that point. */
338#define static_pde(info) (!elf_hash_table (info)->dynamic_sections_created \
339 && bfd_link_executable (info))
340
c0a10a86
MM
341/* The only time that we want the value of a symbol but do not want a
342 relocation for it in Morello is when that symbol is undefined weak. In this
343 case we just need the zero capability and there's no point emitting a
344 relocation for it when we can get an untagged zero capability by just
345 loading some zeros. */
346#define c64_needs_relocation(info, h) \
ece841e8
MM
347 (!((h) \
348 && (h)->root.type == bfd_link_hash_undefweak \
c0a10a86
MM
349 && (UNDEFWEAK_NO_DYNAMIC_RELOC ((info), (h)) \
350 || !elf_hash_table ((info))->dynamic_sections_created)))
351
a06ea964
NC
352/* The first entry in a procedure linkage table looks like this
353 if the distance between the PLTGOT and the PLT is < 4GB use
354 these PLT entries. Note that the dynamic linker gets &PLTGOT[2]
355 in x16 and needs to work out PLTGOT[1] by using an address of
cec5225b
YZ
356 [x16,#-GOT_ENTRY_SIZE]. */
357static const bfd_byte elfNN_aarch64_small_plt0_entry[PLT_ENTRY_SIZE] =
a06ea964
NC
358{
359 0xf0, 0x7b, 0xbf, 0xa9, /* stp x16, x30, [sp, #-16]! */
360 0x10, 0x00, 0x00, 0x90, /* adrp x16, (GOT+16) */
caed7120 361#if ARCH_SIZE == 64
a06ea964
NC
362 0x11, 0x0A, 0x40, 0xf9, /* ldr x17, [x16, #PLT_GOT+0x10] */
363 0x10, 0x42, 0x00, 0x91, /* add x16, x16,#PLT_GOT+0x10 */
caed7120
YZ
364#else
365 0x11, 0x0A, 0x40, 0xb9, /* ldr w17, [x16, #PLT_GOT+0x8] */
366 0x10, 0x22, 0x00, 0x11, /* add w16, w16,#PLT_GOT+0x8 */
367#endif
a06ea964
NC
368 0x20, 0x02, 0x1f, 0xd6, /* br x17 */
369 0x1f, 0x20, 0x03, 0xd5, /* nop */
370 0x1f, 0x20, 0x03, 0xd5, /* nop */
371 0x1f, 0x20, 0x03, 0xd5, /* nop */
372};
373
68bb0359 374static const bfd_byte elfNN_aarch64_small_plt0_bti_entry[PLT_ENTRY_SIZE] =
37c18eed
SD
375{
376 0x5f, 0x24, 0x03, 0xd5, /* bti c. */
377 0xf0, 0x7b, 0xbf, 0xa9, /* stp x16, x30, [sp, #-16]! */
378 0x10, 0x00, 0x00, 0x90, /* adrp x16, (GOT+16) */
379#if ARCH_SIZE == 64
380 0x11, 0x0A, 0x40, 0xf9, /* ldr x17, [x16, #PLT_GOT+0x10] */
381 0x10, 0x42, 0x00, 0x91, /* add x16, x16,#PLT_GOT+0x10 */
382#else
383 0x11, 0x0A, 0x40, 0xb9, /* ldr w17, [x16, #PLT_GOT+0x8] */
384 0x10, 0x22, 0x00, 0x11, /* add w16, w16,#PLT_GOT+0x8 */
385#endif
386 0x20, 0x02, 0x1f, 0xd6, /* br x17 */
387 0x1f, 0x20, 0x03, 0xd5, /* nop */
388 0x1f, 0x20, 0x03, 0xd5, /* nop */
1dbade74
SD
389};
390
e19e9199
SP
391/* The C64 PLT0. */
392static const bfd_byte elfNN_c64_small_plt0_entry[PLT_ENTRY_SIZE] =
393{
394 0xf0, 0x7b, 0xbf, 0x62, /* stp c16, c30, [csp, #-32]! */
395 0x10, 0x00, 0x80, 0x90, /* adrp c16, (GOT+16) */
396 0x11, 0x0a, 0x40, 0xc2, /* ldr c17, [c16, #PLT_GOT+0x10] */
397 0x10, 0x02, 0x00, 0x02, /* add c16, c16,#PLT_GOT+0x10 */
398 0x20, 0x12, 0xc2, 0xc2, /* br c17 */
399 0x1f, 0x20, 0x03, 0xd5, /* nop */
400 0x1f, 0x20, 0x03, 0xd5, /* nop */
401 0x1f, 0x20, 0x03, 0xd5, /* nop */
402};
403
a06ea964
NC
404/* Per function entry in a procedure linkage table looks like this
405 if the distance between the PLTGOT and the PLT is < 4GB use
37c18eed 406 these PLT entries. Use BTI versions of the PLTs when enabled. */
cec5225b 407static const bfd_byte elfNN_aarch64_small_plt_entry[PLT_SMALL_ENTRY_SIZE] =
a06ea964
NC
408{
409 0x10, 0x00, 0x00, 0x90, /* adrp x16, PLTGOT + n * 8 */
caed7120 410#if ARCH_SIZE == 64
a06ea964
NC
411 0x11, 0x02, 0x40, 0xf9, /* ldr x17, [x16, PLTGOT + n * 8] */
412 0x10, 0x02, 0x00, 0x91, /* add x16, x16, :lo12:PLTGOT + n * 8 */
caed7120
YZ
413#else
414 0x11, 0x02, 0x40, 0xb9, /* ldr w17, [x16, PLTGOT + n * 4] */
415 0x10, 0x02, 0x00, 0x11, /* add w16, w16, :lo12:PLTGOT + n * 4 */
416#endif
a06ea964
NC
417 0x20, 0x02, 0x1f, 0xd6, /* br x17. */
418};
419
e19e9199
SP
420/* The C64 PLT. */
421static const bfd_byte elfNN_c64_small_plt_entry[PLT_SMALL_ENTRY_SIZE] =
422{
423 0x10, 0x00, 0x80, 0x90, /* adrp c16, PLTGOT + offset */
424 0x11, 0x02, 0x40, 0xc2, /* ldr c17, [c16, PLTGOT + offset] */
425 0x10, 0x02, 0x00, 0x02, /* add c16, c16, :lo12:PLTGOT + offset */
426 0x20, 0x12, 0xc2, 0xc2, /* br c17. */
427};
428
37c18eed
SD
429static const bfd_byte
430elfNN_aarch64_small_plt_bti_entry[PLT_BTI_SMALL_ENTRY_SIZE] =
431{
432 0x5f, 0x24, 0x03, 0xd5, /* bti c. */
433 0x10, 0x00, 0x00, 0x90, /* adrp x16, PLTGOT + n * 8 */
434#if ARCH_SIZE == 64
435 0x11, 0x02, 0x40, 0xf9, /* ldr x17, [x16, PLTGOT + n * 8] */
436 0x10, 0x02, 0x00, 0x91, /* add x16, x16, :lo12:PLTGOT + n * 8 */
437#else
438 0x11, 0x02, 0x40, 0xb9, /* ldr w17, [x16, PLTGOT + n * 4] */
439 0x10, 0x02, 0x00, 0x11, /* add w16, w16, :lo12:PLTGOT + n * 4 */
440#endif
441 0x20, 0x02, 0x1f, 0xd6, /* br x17. */
68bb0359 442 0x1f, 0x20, 0x03, 0xd5, /* nop */
37c18eed
SD
443};
444
1dbade74
SD
445static const bfd_byte
446elfNN_aarch64_small_plt_pac_entry[PLT_PAC_SMALL_ENTRY_SIZE] =
447{
448 0x10, 0x00, 0x00, 0x90, /* adrp x16, PLTGOT + n * 8 */
449#if ARCH_SIZE == 64
450 0x11, 0x02, 0x40, 0xf9, /* ldr x17, [x16, PLTGOT + n * 8] */
451 0x10, 0x02, 0x00, 0x91, /* add x16, x16, :lo12:PLTGOT + n * 8 */
452#else
453 0x11, 0x02, 0x40, 0xb9, /* ldr w17, [x16, PLTGOT + n * 4] */
454 0x10, 0x02, 0x00, 0x11, /* add w16, w16, :lo12:PLTGOT + n * 4 */
455#endif
456 0x9f, 0x21, 0x03, 0xd5, /* autia1716 */
457 0x20, 0x02, 0x1f, 0xd6, /* br x17. */
68bb0359 458 0x1f, 0x20, 0x03, 0xd5, /* nop */
1dbade74
SD
459};
460
461static const bfd_byte
462elfNN_aarch64_small_plt_bti_pac_entry[PLT_BTI_PAC_SMALL_ENTRY_SIZE] =
463{
464 0x5f, 0x24, 0x03, 0xd5, /* bti c. */
465 0x10, 0x00, 0x00, 0x90, /* adrp x16, PLTGOT + n * 8 */
466#if ARCH_SIZE == 64
467 0x11, 0x02, 0x40, 0xf9, /* ldr x17, [x16, PLTGOT + n * 8] */
468 0x10, 0x02, 0x00, 0x91, /* add x16, x16, :lo12:PLTGOT + n * 8 */
469#else
470 0x11, 0x02, 0x40, 0xb9, /* ldr w17, [x16, PLTGOT + n * 4] */
471 0x10, 0x02, 0x00, 0x11, /* add w16, w16, :lo12:PLTGOT + n * 4 */
472#endif
473 0x9f, 0x21, 0x03, 0xd5, /* autia1716 */
474 0x20, 0x02, 0x1f, 0xd6, /* br x17. */
475};
476
a06ea964 477static const bfd_byte
cec5225b 478elfNN_aarch64_tlsdesc_small_plt_entry[PLT_TLSDESC_ENTRY_SIZE] =
a06ea964
NC
479{
480 0xe2, 0x0f, 0xbf, 0xa9, /* stp x2, x3, [sp, #-16]! */
481 0x02, 0x00, 0x00, 0x90, /* adrp x2, 0 */
482 0x03, 0x00, 0x00, 0x90, /* adrp x3, 0 */
caed7120
YZ
483#if ARCH_SIZE == 64
484 0x42, 0x00, 0x40, 0xf9, /* ldr x2, [x2, #0] */
a06ea964 485 0x63, 0x00, 0x00, 0x91, /* add x3, x3, 0 */
caed7120
YZ
486#else
487 0x42, 0x00, 0x40, 0xb9, /* ldr w2, [x2, #0] */
488 0x63, 0x00, 0x00, 0x11, /* add w3, w3, 0 */
489#endif
490 0x40, 0x00, 0x1f, 0xd6, /* br x2 */
a06ea964
NC
491 0x1f, 0x20, 0x03, 0xd5, /* nop */
492 0x1f, 0x20, 0x03, 0xd5, /* nop */
493};
494
37c18eed 495static const bfd_byte
68bb0359 496elfNN_aarch64_tlsdesc_small_plt_bti_entry[PLT_TLSDESC_ENTRY_SIZE] =
37c18eed
SD
497{
498 0x5f, 0x24, 0x03, 0xd5, /* bti c. */
499 0xe2, 0x0f, 0xbf, 0xa9, /* stp x2, x3, [sp, #-16]! */
500 0x02, 0x00, 0x00, 0x90, /* adrp x2, 0 */
501 0x03, 0x00, 0x00, 0x90, /* adrp x3, 0 */
502#if ARCH_SIZE == 64
503 0x42, 0x00, 0x40, 0xf9, /* ldr x2, [x2, #0] */
504 0x63, 0x00, 0x00, 0x91, /* add x3, x3, 0 */
505#else
506 0x42, 0x00, 0x40, 0xb9, /* ldr w2, [x2, #0] */
507 0x63, 0x00, 0x00, 0x11, /* add w3, w3, 0 */
508#endif
509 0x40, 0x00, 0x1f, 0xd6, /* br x2 */
510 0x1f, 0x20, 0x03, 0xd5, /* nop */
37c18eed
SD
511};
512
4ca9b406
SP
513static const bfd_byte
514elfNN_aarch64_tlsdesc_small_plt_c64_entry[PLT_TLSDESC_ENTRY_SIZE] =
515{
516 0xe2, 0x8f, 0xbf, 0x62, /* stp c2, c3, [sp, #-16]! */
517 0x02, 0x00, 0x80, 0x90, /* adrp c2, 0 */
518 0x03, 0x00, 0x80, 0x90, /* adrp c3, 0 */
519 0x42, 0x00, 0x40, 0xc2, /* ldr c2, [c2, #0] */
520 0x63, 0x00, 0x00, 0x02, /* add c3, c3, 0 */
521 0x40, 0x10, 0xc2, 0xc2, /* br c2 */
522 0x1f, 0x20, 0x03, 0xd5, /* nop */
523 0x1f, 0x20, 0x03, 0xd5, /* nop */
524};
525
07d6d2b8
AM
526#define elf_info_to_howto elfNN_aarch64_info_to_howto
527#define elf_info_to_howto_rel elfNN_aarch64_info_to_howto
a06ea964
NC
528
529#define AARCH64_ELF_ABI_VERSION 0
a06ea964
NC
530
531/* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
532#define ALL_ONES (~ (bfd_vma) 0)
533
a6bb11b2
YZ
534/* Indexed by the bfd interal reloc enumerators.
535 Therefore, the table needs to be synced with BFD_RELOC_AARCH64_*
536 in reloc.c. */
a06ea964 537
a6bb11b2 538static reloc_howto_type elfNN_aarch64_howto_table[] =
a06ea964 539{
a6bb11b2 540 EMPTY_HOWTO (0),
a06ea964 541
a6bb11b2 542 /* Basic data relocations. */
a06ea964 543
b7f28d87
JW
544 /* Deprecated, but retained for backwards compatibility. */
545 HOWTO64 (R_AARCH64_NULL, /* type */
a06ea964 546 0, /* rightshift */
6346d5ca 547 3, /* size (0 = byte, 1 = short, 2 = long) */
a6bb11b2 548 0, /* bitsize */
a06ea964
NC
549 FALSE, /* pc_relative */
550 0, /* bitpos */
551 complain_overflow_dont, /* complain_on_overflow */
552 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 553 "R_AARCH64_NULL", /* name */
a06ea964
NC
554 FALSE, /* partial_inplace */
555 0, /* src_mask */
a6bb11b2 556 0, /* dst_mask */
a06ea964 557 FALSE), /* pcrel_offset */
a6bb11b2 558 HOWTO (R_AARCH64_NONE, /* type */
a06ea964 559 0, /* rightshift */
6346d5ca 560 3, /* size (0 = byte, 1 = short, 2 = long) */
a06ea964
NC
561 0, /* bitsize */
562 FALSE, /* pc_relative */
563 0, /* bitpos */
564 complain_overflow_dont, /* complain_on_overflow */
565 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 566 "R_AARCH64_NONE", /* name */
a06ea964
NC
567 FALSE, /* partial_inplace */
568 0, /* src_mask */
569 0, /* dst_mask */
570 FALSE), /* pcrel_offset */
571
572 /* .xword: (S+A) */
a6bb11b2 573 HOWTO64 (AARCH64_R (ABS64), /* type */
a06ea964
NC
574 0, /* rightshift */
575 4, /* size (4 = long long) */
576 64, /* bitsize */
577 FALSE, /* pc_relative */
578 0, /* bitpos */
579 complain_overflow_unsigned, /* complain_on_overflow */
580 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 581 AARCH64_R_STR (ABS64), /* name */
a06ea964
NC
582 FALSE, /* partial_inplace */
583 ALL_ONES, /* src_mask */
584 ALL_ONES, /* dst_mask */
585 FALSE), /* pcrel_offset */
586
587 /* .word: (S+A) */
a6bb11b2 588 HOWTO (AARCH64_R (ABS32), /* type */
a06ea964
NC
589 0, /* rightshift */
590 2, /* size (0 = byte, 1 = short, 2 = long) */
591 32, /* bitsize */
592 FALSE, /* pc_relative */
593 0, /* bitpos */
594 complain_overflow_unsigned, /* complain_on_overflow */
595 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 596 AARCH64_R_STR (ABS32), /* name */
a06ea964
NC
597 FALSE, /* partial_inplace */
598 0xffffffff, /* src_mask */
599 0xffffffff, /* dst_mask */
600 FALSE), /* pcrel_offset */
601
602 /* .half: (S+A) */
a6bb11b2 603 HOWTO (AARCH64_R (ABS16), /* type */
a06ea964
NC
604 0, /* rightshift */
605 1, /* size (0 = byte, 1 = short, 2 = long) */
606 16, /* bitsize */
607 FALSE, /* pc_relative */
608 0, /* bitpos */
609 complain_overflow_unsigned, /* complain_on_overflow */
610 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 611 AARCH64_R_STR (ABS16), /* name */
a06ea964
NC
612 FALSE, /* partial_inplace */
613 0xffff, /* src_mask */
614 0xffff, /* dst_mask */
615 FALSE), /* pcrel_offset */
616
617 /* .xword: (S+A-P) */
a6bb11b2 618 HOWTO64 (AARCH64_R (PREL64), /* type */
a06ea964
NC
619 0, /* rightshift */
620 4, /* size (4 = long long) */
621 64, /* bitsize */
622 TRUE, /* pc_relative */
623 0, /* bitpos */
624 complain_overflow_signed, /* complain_on_overflow */
625 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 626 AARCH64_R_STR (PREL64), /* name */
a06ea964
NC
627 FALSE, /* partial_inplace */
628 ALL_ONES, /* src_mask */
629 ALL_ONES, /* dst_mask */
630 TRUE), /* pcrel_offset */
631
632 /* .word: (S+A-P) */
a6bb11b2 633 HOWTO (AARCH64_R (PREL32), /* type */
a06ea964
NC
634 0, /* rightshift */
635 2, /* size (0 = byte, 1 = short, 2 = long) */
636 32, /* bitsize */
637 TRUE, /* pc_relative */
638 0, /* bitpos */
639 complain_overflow_signed, /* complain_on_overflow */
640 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 641 AARCH64_R_STR (PREL32), /* name */
a06ea964
NC
642 FALSE, /* partial_inplace */
643 0xffffffff, /* src_mask */
644 0xffffffff, /* dst_mask */
645 TRUE), /* pcrel_offset */
646
647 /* .half: (S+A-P) */
a6bb11b2 648 HOWTO (AARCH64_R (PREL16), /* type */
a06ea964
NC
649 0, /* rightshift */
650 1, /* size (0 = byte, 1 = short, 2 = long) */
651 16, /* bitsize */
652 TRUE, /* pc_relative */
653 0, /* bitpos */
654 complain_overflow_signed, /* complain_on_overflow */
655 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 656 AARCH64_R_STR (PREL16), /* name */
a06ea964
NC
657 FALSE, /* partial_inplace */
658 0xffff, /* src_mask */
659 0xffff, /* dst_mask */
660 TRUE), /* pcrel_offset */
661
662 /* Group relocations to create a 16, 32, 48 or 64 bit
663 unsigned data or abs address inline. */
664
665 /* MOVZ: ((S+A) >> 0) & 0xffff */
a6bb11b2 666 HOWTO (AARCH64_R (MOVW_UABS_G0), /* type */
a06ea964
NC
667 0, /* rightshift */
668 2, /* size (0 = byte, 1 = short, 2 = long) */
669 16, /* bitsize */
670 FALSE, /* pc_relative */
671 0, /* bitpos */
672 complain_overflow_unsigned, /* complain_on_overflow */
673 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 674 AARCH64_R_STR (MOVW_UABS_G0), /* name */
a06ea964
NC
675 FALSE, /* partial_inplace */
676 0xffff, /* src_mask */
677 0xffff, /* dst_mask */
678 FALSE), /* pcrel_offset */
679
680 /* MOVK: ((S+A) >> 0) & 0xffff [no overflow check] */
a6bb11b2 681 HOWTO (AARCH64_R (MOVW_UABS_G0_NC), /* type */
a06ea964
NC
682 0, /* rightshift */
683 2, /* size (0 = byte, 1 = short, 2 = long) */
684 16, /* bitsize */
685 FALSE, /* pc_relative */
686 0, /* bitpos */
687 complain_overflow_dont, /* complain_on_overflow */
688 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 689 AARCH64_R_STR (MOVW_UABS_G0_NC), /* name */
a06ea964
NC
690 FALSE, /* partial_inplace */
691 0xffff, /* src_mask */
692 0xffff, /* dst_mask */
693 FALSE), /* pcrel_offset */
694
695 /* MOVZ: ((S+A) >> 16) & 0xffff */
a6bb11b2 696 HOWTO (AARCH64_R (MOVW_UABS_G1), /* type */
a06ea964
NC
697 16, /* rightshift */
698 2, /* size (0 = byte, 1 = short, 2 = long) */
699 16, /* bitsize */
700 FALSE, /* pc_relative */
701 0, /* bitpos */
702 complain_overflow_unsigned, /* complain_on_overflow */
703 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 704 AARCH64_R_STR (MOVW_UABS_G1), /* name */
a06ea964
NC
705 FALSE, /* partial_inplace */
706 0xffff, /* src_mask */
707 0xffff, /* dst_mask */
708 FALSE), /* pcrel_offset */
709
710 /* MOVK: ((S+A) >> 16) & 0xffff [no overflow check] */
a6bb11b2 711 HOWTO64 (AARCH64_R (MOVW_UABS_G1_NC), /* type */
a06ea964
NC
712 16, /* rightshift */
713 2, /* size (0 = byte, 1 = short, 2 = long) */
714 16, /* bitsize */
715 FALSE, /* pc_relative */
716 0, /* bitpos */
717 complain_overflow_dont, /* complain_on_overflow */
718 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 719 AARCH64_R_STR (MOVW_UABS_G1_NC), /* name */
a06ea964
NC
720 FALSE, /* partial_inplace */
721 0xffff, /* src_mask */
722 0xffff, /* dst_mask */
723 FALSE), /* pcrel_offset */
724
725 /* MOVZ: ((S+A) >> 32) & 0xffff */
a6bb11b2 726 HOWTO64 (AARCH64_R (MOVW_UABS_G2), /* type */
a06ea964
NC
727 32, /* rightshift */
728 2, /* size (0 = byte, 1 = short, 2 = long) */
729 16, /* bitsize */
730 FALSE, /* pc_relative */
731 0, /* bitpos */
732 complain_overflow_unsigned, /* complain_on_overflow */
733 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 734 AARCH64_R_STR (MOVW_UABS_G2), /* name */
a06ea964
NC
735 FALSE, /* partial_inplace */
736 0xffff, /* src_mask */
737 0xffff, /* dst_mask */
738 FALSE), /* pcrel_offset */
739
740 /* MOVK: ((S+A) >> 32) & 0xffff [no overflow check] */
a6bb11b2 741 HOWTO64 (AARCH64_R (MOVW_UABS_G2_NC), /* type */
a06ea964
NC
742 32, /* rightshift */
743 2, /* size (0 = byte, 1 = short, 2 = long) */
744 16, /* bitsize */
745 FALSE, /* pc_relative */
746 0, /* bitpos */
747 complain_overflow_dont, /* complain_on_overflow */
748 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 749 AARCH64_R_STR (MOVW_UABS_G2_NC), /* name */
a06ea964
NC
750 FALSE, /* partial_inplace */
751 0xffff, /* src_mask */
752 0xffff, /* dst_mask */
753 FALSE), /* pcrel_offset */
754
755 /* MOVZ: ((S+A) >> 48) & 0xffff */
a6bb11b2 756 HOWTO64 (AARCH64_R (MOVW_UABS_G3), /* type */
a06ea964
NC
757 48, /* rightshift */
758 2, /* size (0 = byte, 1 = short, 2 = long) */
759 16, /* bitsize */
760 FALSE, /* pc_relative */
761 0, /* bitpos */
762 complain_overflow_unsigned, /* complain_on_overflow */
763 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 764 AARCH64_R_STR (MOVW_UABS_G3), /* name */
a06ea964
NC
765 FALSE, /* partial_inplace */
766 0xffff, /* src_mask */
767 0xffff, /* dst_mask */
768 FALSE), /* pcrel_offset */
769
770 /* Group relocations to create high part of a 16, 32, 48 or 64 bit
771 signed data or abs address inline. Will change instruction
772 to MOVN or MOVZ depending on sign of calculated value. */
773
774 /* MOV[ZN]: ((S+A) >> 0) & 0xffff */
a6bb11b2 775 HOWTO (AARCH64_R (MOVW_SABS_G0), /* type */
a06ea964
NC
776 0, /* rightshift */
777 2, /* size (0 = byte, 1 = short, 2 = long) */
c5e3a364 778 17, /* bitsize */
a06ea964
NC
779 FALSE, /* pc_relative */
780 0, /* bitpos */
781 complain_overflow_signed, /* complain_on_overflow */
782 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 783 AARCH64_R_STR (MOVW_SABS_G0), /* name */
a06ea964
NC
784 FALSE, /* partial_inplace */
785 0xffff, /* src_mask */
786 0xffff, /* dst_mask */
787 FALSE), /* pcrel_offset */
788
789 /* MOV[ZN]: ((S+A) >> 16) & 0xffff */
a6bb11b2 790 HOWTO64 (AARCH64_R (MOVW_SABS_G1), /* type */
a06ea964
NC
791 16, /* rightshift */
792 2, /* size (0 = byte, 1 = short, 2 = long) */
c5e3a364 793 17, /* bitsize */
a06ea964
NC
794 FALSE, /* pc_relative */
795 0, /* bitpos */
796 complain_overflow_signed, /* complain_on_overflow */
797 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 798 AARCH64_R_STR (MOVW_SABS_G1), /* name */
a06ea964
NC
799 FALSE, /* partial_inplace */
800 0xffff, /* src_mask */
801 0xffff, /* dst_mask */
802 FALSE), /* pcrel_offset */
803
804 /* MOV[ZN]: ((S+A) >> 32) & 0xffff */
a6bb11b2 805 HOWTO64 (AARCH64_R (MOVW_SABS_G2), /* type */
a06ea964
NC
806 32, /* rightshift */
807 2, /* size (0 = byte, 1 = short, 2 = long) */
c5e3a364 808 17, /* bitsize */
a06ea964
NC
809 FALSE, /* pc_relative */
810 0, /* bitpos */
811 complain_overflow_signed, /* complain_on_overflow */
812 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 813 AARCH64_R_STR (MOVW_SABS_G2), /* name */
a06ea964
NC
814 FALSE, /* partial_inplace */
815 0xffff, /* src_mask */
816 0xffff, /* dst_mask */
817 FALSE), /* pcrel_offset */
818
32247401
RL
819 /* Group relocations to create a 16, 32, 48 or 64 bit
820 PC relative address inline. */
821
822 /* MOV[NZ]: ((S+A-P) >> 0) & 0xffff */
e30d1fa1 823 HOWTO (AARCH64_R (MOVW_PREL_G0), /* type */
32247401
RL
824 0, /* rightshift */
825 2, /* size (0 = byte, 1 = short, 2 = long) */
826 17, /* bitsize */
827 TRUE, /* pc_relative */
828 0, /* bitpos */
829 complain_overflow_signed, /* complain_on_overflow */
830 bfd_elf_generic_reloc, /* special_function */
831 AARCH64_R_STR (MOVW_PREL_G0), /* name */
832 FALSE, /* partial_inplace */
833 0xffff, /* src_mask */
834 0xffff, /* dst_mask */
835 TRUE), /* pcrel_offset */
836
837 /* MOVK: ((S+A-P) >> 0) & 0xffff [no overflow check] */
e30d1fa1 838 HOWTO (AARCH64_R (MOVW_PREL_G0_NC), /* type */
32247401
RL
839 0, /* rightshift */
840 2, /* size (0 = byte, 1 = short, 2 = long) */
841 16, /* bitsize */
842 TRUE, /* pc_relative */
843 0, /* bitpos */
844 complain_overflow_dont, /* complain_on_overflow */
845 bfd_elf_generic_reloc, /* special_function */
846 AARCH64_R_STR (MOVW_PREL_G0_NC), /* name */
847 FALSE, /* partial_inplace */
848 0xffff, /* src_mask */
849 0xffff, /* dst_mask */
850 TRUE), /* pcrel_offset */
851
852 /* MOV[NZ]: ((S+A-P) >> 16) & 0xffff */
e30d1fa1 853 HOWTO (AARCH64_R (MOVW_PREL_G1), /* type */
32247401
RL
854 16, /* rightshift */
855 2, /* size (0 = byte, 1 = short, 2 = long) */
856 17, /* bitsize */
857 TRUE, /* pc_relative */
858 0, /* bitpos */
859 complain_overflow_signed, /* complain_on_overflow */
860 bfd_elf_generic_reloc, /* special_function */
861 AARCH64_R_STR (MOVW_PREL_G1), /* name */
862 FALSE, /* partial_inplace */
863 0xffff, /* src_mask */
864 0xffff, /* dst_mask */
865 TRUE), /* pcrel_offset */
866
867 /* MOVK: ((S+A-P) >> 16) & 0xffff [no overflow check] */
868 HOWTO64 (AARCH64_R (MOVW_PREL_G1_NC), /* type */
869 16, /* rightshift */
870 2, /* size (0 = byte, 1 = short, 2 = long) */
871 16, /* bitsize */
872 TRUE, /* pc_relative */
873 0, /* bitpos */
874 complain_overflow_dont, /* complain_on_overflow */
875 bfd_elf_generic_reloc, /* special_function */
876 AARCH64_R_STR (MOVW_PREL_G1_NC), /* name */
877 FALSE, /* partial_inplace */
878 0xffff, /* src_mask */
879 0xffff, /* dst_mask */
880 TRUE), /* pcrel_offset */
881
882 /* MOV[NZ]: ((S+A-P) >> 32) & 0xffff */
883 HOWTO64 (AARCH64_R (MOVW_PREL_G2), /* type */
884 32, /* rightshift */
885 2, /* size (0 = byte, 1 = short, 2 = long) */
886 17, /* bitsize */
887 TRUE, /* pc_relative */
888 0, /* bitpos */
889 complain_overflow_signed, /* complain_on_overflow */
890 bfd_elf_generic_reloc, /* special_function */
891 AARCH64_R_STR (MOVW_PREL_G2), /* name */
892 FALSE, /* partial_inplace */
893 0xffff, /* src_mask */
894 0xffff, /* dst_mask */
895 TRUE), /* pcrel_offset */
896
897 /* MOVK: ((S+A-P) >> 32) & 0xffff [no overflow check] */
898 HOWTO64 (AARCH64_R (MOVW_PREL_G2_NC), /* type */
899 32, /* rightshift */
900 2, /* size (0 = byte, 1 = short, 2 = long) */
901 16, /* bitsize */
902 TRUE, /* pc_relative */
903 0, /* bitpos */
904 complain_overflow_dont, /* complain_on_overflow */
905 bfd_elf_generic_reloc, /* special_function */
906 AARCH64_R_STR (MOVW_PREL_G2_NC), /* name */
907 FALSE, /* partial_inplace */
908 0xffff, /* src_mask */
909 0xffff, /* dst_mask */
910 TRUE), /* pcrel_offset */
911
912 /* MOV[NZ]: ((S+A-P) >> 48) & 0xffff */
913 HOWTO64 (AARCH64_R (MOVW_PREL_G3), /* type */
914 48, /* rightshift */
915 2, /* size (0 = byte, 1 = short, 2 = long) */
916 16, /* bitsize */
917 TRUE, /* pc_relative */
918 0, /* bitpos */
919 complain_overflow_dont, /* complain_on_overflow */
920 bfd_elf_generic_reloc, /* special_function */
921 AARCH64_R_STR (MOVW_PREL_G3), /* name */
922 FALSE, /* partial_inplace */
923 0xffff, /* src_mask */
924 0xffff, /* dst_mask */
925 TRUE), /* pcrel_offset */
926
1408e112
MM
927 /* Relocations to get the size of a symbol. Used for Morello. */
928 /* MOVZ: ((S+A) >> 0) & 0xffff */
929 HOWTO64 (MORELLO_R (MOVW_SIZE_G0), /* type */
930 0, /* rightshift */
931 2, /* size (0 = byte, 1 = short, 2 = long) */
932 16, /* bitsize */
933 FALSE, /* pc_relative */
934 0, /* bitpos */
935 complain_overflow_unsigned, /* complain_on_overflow */
936 bfd_elf_generic_reloc, /* special_function */
937 MORELLO_R_STR (MOVW_SIZE_G0), /* name */
938 FALSE, /* partial_inplace */
939 0xffff, /* src_mask */
940 0xffff, /* dst_mask */
941 FALSE), /* pcrel_offset */
942
943 /* MOVK: ((S+A) >> 0) & 0xffff [no overflow check] */
944 HOWTO64 (MORELLO_R (MOVW_SIZE_G0_NC), /* type */
945 0, /* rightshift */
946 2, /* size (0 = byte, 1 = short, 2 = long) */
947 16, /* bitsize */
948 FALSE, /* pc_relative */
949 0, /* bitpos */
950 complain_overflow_dont, /* complain_on_overflow */
951 bfd_elf_generic_reloc, /* special_function */
952 MORELLO_R_STR (MOVW_SIZE_G0_NC), /* name */
953 FALSE, /* partial_inplace */
954 0xffff, /* src_mask */
955 0xffff, /* dst_mask */
956 FALSE), /* pcrel_offset */
957
958 /* MOVZ: ((S+A) >> 16) & 0xffff */
959 HOWTO64 (MORELLO_R (MOVW_SIZE_G1), /* type */
960 16, /* rightshift */
961 2, /* size (0 = byte, 1 = short, 2 = long) */
962 16, /* bitsize */
963 FALSE, /* pc_relative */
964 0, /* bitpos */
965 complain_overflow_unsigned, /* complain_on_overflow */
966 bfd_elf_generic_reloc, /* special_function */
967 MORELLO_R_STR (MOVW_SIZE_G1), /* name */
968 FALSE, /* partial_inplace */
969 0xffff, /* src_mask */
970 0xffff, /* dst_mask */
971 FALSE), /* pcrel_offset */
972
973 /* MOVK: ((S+A) >> 16) & 0xffff [no overflow check] */
974 HOWTO64 (MORELLO_R (MOVW_SIZE_G1_NC), /* type */
975 16, /* rightshift */
976 2, /* size (0 = byte, 1 = short, 2 = long) */
977 16, /* bitsize */
978 FALSE, /* pc_relative */
979 0, /* bitpos */
980 complain_overflow_dont, /* complain_on_overflow */
981 bfd_elf_generic_reloc, /* special_function */
982 MORELLO_R_STR (MOVW_SIZE_G1_NC), /* name */
983 FALSE, /* partial_inplace */
984 0xffff, /* src_mask */
985 0xffff, /* dst_mask */
986 FALSE), /* pcrel_offset */
987
988 /* MOVZ: ((S+A) >> 32) & 0xffff */
989 HOWTO64 (MORELLO_R (MOVW_SIZE_G2), /* type */
990 32, /* rightshift */
991 2, /* size (0 = byte, 1 = short, 2 = long) */
992 16, /* bitsize */
993 FALSE, /* pc_relative */
994 0, /* bitpos */
995 complain_overflow_unsigned, /* complain_on_overflow */
996 bfd_elf_generic_reloc, /* special_function */
997 MORELLO_R_STR (MOVW_SIZE_G2), /* name */
998 FALSE, /* partial_inplace */
999 0xffff, /* src_mask */
1000 0xffff, /* dst_mask */
1001 FALSE), /* pcrel_offset */
1002
1003 /* MOVK: ((S+A) >> 32) & 0xffff [no overflow check] */
1004 HOWTO64 (MORELLO_R (MOVW_SIZE_G2_NC), /* type */
1005 32, /* rightshift */
1006 2, /* size (0 = byte, 1 = short, 2 = long) */
1007 16, /* bitsize */
1008 FALSE, /* pc_relative */
1009 0, /* bitpos */
1010 complain_overflow_dont, /* complain_on_overflow */
1011 bfd_elf_generic_reloc, /* special_function */
1012 MORELLO_R_STR (MOVW_SIZE_G2_NC), /* name */
1013 FALSE, /* partial_inplace */
1014 0xffff, /* src_mask */
1015 0xffff, /* dst_mask */
1016 FALSE), /* pcrel_offset */
1017
1018 /* MOVZ: ((S+A) >> 48) & 0xffff */
1019 HOWTO64 (MORELLO_R (MOVW_SIZE_G3), /* type */
1020 48, /* rightshift */
1021 2, /* size (0 = byte, 1 = short, 2 = long) */
1022 16, /* bitsize */
1023 FALSE, /* pc_relative */
1024 0, /* bitpos */
1025 complain_overflow_unsigned, /* complain_on_overflow */
1026 bfd_elf_generic_reloc, /* special_function */
1027 MORELLO_R_STR (MOVW_SIZE_G3), /* name */
1028 FALSE, /* partial_inplace */
1029 0xffff, /* src_mask */
1030 0xffff, /* dst_mask */
1031 FALSE), /* pcrel_offset */
1032
a06ea964
NC
1033/* Relocations to generate 19, 21 and 33 bit PC-relative load/store
1034 addresses: PG(x) is (x & ~0xfff). */
1035
f7d2c675
SP
1036 /* LD-lit: ((S+A-P) >> 4) & 0x1ffff */
1037 HOWTO64 (MORELLO_R (LD_PREL_LO17), /* type */
1038 4, /* rightshift */
1039 2, /* size (0 = byte, 1 = short, 2 = long) */
1040 17, /* bitsize */
1041 TRUE, /* pc_relative */
1042 0, /* bitpos */
1043 complain_overflow_signed, /* complain_on_overflow */
1044 bfd_elf_generic_reloc, /* special_function */
1045 MORELLO_R_STR (LD_PREL_LO17), /* name */
1046 FALSE, /* partial_inplace */
1047 0x1ffff, /* src_mask */
1048 0x1ffff, /* dst_mask */
1049 TRUE), /* pcrel_offset */
1050
a06ea964 1051 /* LD-lit: ((S+A-P) >> 2) & 0x7ffff */
a6bb11b2 1052 HOWTO (AARCH64_R (LD_PREL_LO19), /* type */
a06ea964
NC
1053 2, /* rightshift */
1054 2, /* size (0 = byte, 1 = short, 2 = long) */
1055 19, /* bitsize */
1056 TRUE, /* pc_relative */
1057 0, /* bitpos */
1058 complain_overflow_signed, /* complain_on_overflow */
1059 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1060 AARCH64_R_STR (LD_PREL_LO19), /* name */
a06ea964
NC
1061 FALSE, /* partial_inplace */
1062 0x7ffff, /* src_mask */
1063 0x7ffff, /* dst_mask */
1064 TRUE), /* pcrel_offset */
1065
92504105
SP
1066 /* C64 ADRP: ((PG(S+A)-PG(P)) >> 12) & 0xfffff */
1067 HOWTO64 (MORELLO_R (ADR_PREL_PG_HI20), /* type */
1068 12, /* rightshift */
1069 2, /* size (0 = byte, 1 = short, 2 = long) */
1070 20, /* bitsize */
1071 TRUE, /* pc_relative */
1072 0, /* bitpos */
1073 complain_overflow_signed, /* complain_on_overflow */
1074 bfd_elf_generic_reloc, /* special_function */
1075 MORELLO_R_STR (ADR_PREL_PG_HI20), /* name */
1076 FALSE, /* partial_inplace */
1077 0xfffff, /* src_mask */
1078 0xfffff, /* dst_mask */
1079 TRUE), /* pcrel_offset */
1080
1081 /* C64 ADRP: ((PG(S+A)-PG(P)) >> 12) & 0xfffff [no overflow check] */
1082 HOWTO64 (MORELLO_R (ADR_PREL_PG_HI20_NC), /* type */
1083 12, /* rightshift */
1084 2, /* size (0 = byte, 1 = short, 2 = long) */
1085 20, /* bitsize */
1086 TRUE, /* pc_relative */
1087 0, /* bitpos */
1088 complain_overflow_dont, /* complain_on_overflow */
1089 bfd_elf_generic_reloc, /* special_function */
1090 MORELLO_R_STR (ADR_PREL_PG_HI20_NC), /* name */
1091 FALSE, /* partial_inplace */
1092 0xfffff, /* src_mask */
1093 0xfffff, /* dst_mask */
1094 TRUE), /* pcrel_offset */
1095
a06ea964 1096 /* ADR: (S+A-P) & 0x1fffff */
a6bb11b2 1097 HOWTO (AARCH64_R (ADR_PREL_LO21), /* type */
a06ea964
NC
1098 0, /* rightshift */
1099 2, /* size (0 = byte, 1 = short, 2 = long) */
1100 21, /* bitsize */
1101 TRUE, /* pc_relative */
1102 0, /* bitpos */
1103 complain_overflow_signed, /* complain_on_overflow */
1104 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1105 AARCH64_R_STR (ADR_PREL_LO21), /* name */
a06ea964
NC
1106 FALSE, /* partial_inplace */
1107 0x1fffff, /* src_mask */
1108 0x1fffff, /* dst_mask */
1109 TRUE), /* pcrel_offset */
1110
1111 /* ADRP: ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
a6bb11b2 1112 HOWTO (AARCH64_R (ADR_PREL_PG_HI21), /* type */
a06ea964
NC
1113 12, /* rightshift */
1114 2, /* size (0 = byte, 1 = short, 2 = long) */
1115 21, /* bitsize */
1116 TRUE, /* pc_relative */
1117 0, /* bitpos */
1118 complain_overflow_signed, /* complain_on_overflow */
1119 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1120 AARCH64_R_STR (ADR_PREL_PG_HI21), /* name */
a06ea964
NC
1121 FALSE, /* partial_inplace */
1122 0x1fffff, /* src_mask */
1123 0x1fffff, /* dst_mask */
1124 TRUE), /* pcrel_offset */
1125
1126 /* ADRP: ((PG(S+A)-PG(P)) >> 12) & 0x1fffff [no overflow check] */
a6bb11b2 1127 HOWTO64 (AARCH64_R (ADR_PREL_PG_HI21_NC), /* type */
a06ea964
NC
1128 12, /* rightshift */
1129 2, /* size (0 = byte, 1 = short, 2 = long) */
1130 21, /* bitsize */
1131 TRUE, /* pc_relative */
1132 0, /* bitpos */
1133 complain_overflow_dont, /* complain_on_overflow */
1134 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1135 AARCH64_R_STR (ADR_PREL_PG_HI21_NC), /* name */
a06ea964
NC
1136 FALSE, /* partial_inplace */
1137 0x1fffff, /* src_mask */
1138 0x1fffff, /* dst_mask */
1139 TRUE), /* pcrel_offset */
1140
1141 /* ADD: (S+A) & 0xfff [no overflow check] */
a6bb11b2 1142 HOWTO (AARCH64_R (ADD_ABS_LO12_NC), /* type */
a06ea964
NC
1143 0, /* rightshift */
1144 2, /* size (0 = byte, 1 = short, 2 = long) */
1145 12, /* bitsize */
1146 FALSE, /* pc_relative */
1147 10, /* bitpos */
1148 complain_overflow_dont, /* complain_on_overflow */
1149 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1150 AARCH64_R_STR (ADD_ABS_LO12_NC), /* name */
a06ea964
NC
1151 FALSE, /* partial_inplace */
1152 0x3ffc00, /* src_mask */
1153 0x3ffc00, /* dst_mask */
1154 FALSE), /* pcrel_offset */
1155
1156 /* LD/ST8: (S+A) & 0xfff */
a6bb11b2 1157 HOWTO (AARCH64_R (LDST8_ABS_LO12_NC), /* type */
a06ea964
NC
1158 0, /* rightshift */
1159 2, /* size (0 = byte, 1 = short, 2 = long) */
1160 12, /* bitsize */
1161 FALSE, /* pc_relative */
1162 0, /* bitpos */
1163 complain_overflow_dont, /* complain_on_overflow */
1164 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1165 AARCH64_R_STR (LDST8_ABS_LO12_NC), /* name */
a06ea964
NC
1166 FALSE, /* partial_inplace */
1167 0xfff, /* src_mask */
1168 0xfff, /* dst_mask */
1169 FALSE), /* pcrel_offset */
1170
1171 /* Relocations for control-flow instructions. */
1172
1173 /* TBZ/NZ: ((S+A-P) >> 2) & 0x3fff */
a6bb11b2 1174 HOWTO (AARCH64_R (TSTBR14), /* type */
a06ea964
NC
1175 2, /* rightshift */
1176 2, /* size (0 = byte, 1 = short, 2 = long) */
1177 14, /* bitsize */
1178 TRUE, /* pc_relative */
1179 0, /* bitpos */
1180 complain_overflow_signed, /* complain_on_overflow */
1181 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1182 AARCH64_R_STR (TSTBR14), /* name */
a06ea964
NC
1183 FALSE, /* partial_inplace */
1184 0x3fff, /* src_mask */
1185 0x3fff, /* dst_mask */
1186 TRUE), /* pcrel_offset */
1187
1188 /* B.cond: ((S+A-P) >> 2) & 0x7ffff */
a6bb11b2 1189 HOWTO (AARCH64_R (CONDBR19), /* type */
a06ea964
NC
1190 2, /* rightshift */
1191 2, /* size (0 = byte, 1 = short, 2 = long) */
1192 19, /* bitsize */
1193 TRUE, /* pc_relative */
1194 0, /* bitpos */
1195 complain_overflow_signed, /* complain_on_overflow */
1196 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1197 AARCH64_R_STR (CONDBR19), /* name */
a06ea964
NC
1198 FALSE, /* partial_inplace */
1199 0x7ffff, /* src_mask */
1200 0x7ffff, /* dst_mask */
1201 TRUE), /* pcrel_offset */
1202
a06ea964 1203 /* B: ((S+A-P) >> 2) & 0x3ffffff */
a6bb11b2 1204 HOWTO (AARCH64_R (JUMP26), /* type */
a06ea964
NC
1205 2, /* rightshift */
1206 2, /* size (0 = byte, 1 = short, 2 = long) */
1207 26, /* bitsize */
1208 TRUE, /* pc_relative */
1209 0, /* bitpos */
1210 complain_overflow_signed, /* complain_on_overflow */
1211 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1212 AARCH64_R_STR (JUMP26), /* name */
a06ea964
NC
1213 FALSE, /* partial_inplace */
1214 0x3ffffff, /* src_mask */
1215 0x3ffffff, /* dst_mask */
1216 TRUE), /* pcrel_offset */
1217
1218 /* BL: ((S+A-P) >> 2) & 0x3ffffff */
a6bb11b2 1219 HOWTO (AARCH64_R (CALL26), /* type */
a06ea964
NC
1220 2, /* rightshift */
1221 2, /* size (0 = byte, 1 = short, 2 = long) */
1222 26, /* bitsize */
1223 TRUE, /* pc_relative */
1224 0, /* bitpos */
1225 complain_overflow_signed, /* complain_on_overflow */
1226 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1227 AARCH64_R_STR (CALL26), /* name */
a06ea964
NC
1228 FALSE, /* partial_inplace */
1229 0x3ffffff, /* src_mask */
1230 0x3ffffff, /* dst_mask */
1231 TRUE), /* pcrel_offset */
1232
e19e9199
SP
1233 /* TBZ/NZ: ((S+A-P) >> 2) & 0x3fff */
1234 HOWTO64 (MORELLO_R (TSTBR14), /* type */
1235 2, /* rightshift */
1236 2, /* size (0 = byte, 1 = short, 2 = long) */
1237 14, /* bitsize */
1238 TRUE, /* pc_relative */
1239 0, /* bitpos */
1240 complain_overflow_signed, /* complain_on_overflow */
1241 bfd_elf_generic_reloc, /* special_function */
1242 MORELLO_R_STR (TSTBR14), /* name */
1243 FALSE, /* partial_inplace */
1244 0x3fff, /* src_mask */
1245 0x3fff, /* dst_mask */
1246 TRUE), /* pcrel_offset */
1247
1248 /* B.cond: ((S+A-P) >> 2) & 0x7ffff */
1249 HOWTO64 (MORELLO_R (CONDBR19), /* type */
1250 2, /* rightshift */
1251 2, /* size (0 = byte, 1 = short, 2 = long) */
1252 19, /* bitsize */
1253 TRUE, /* pc_relative */
1254 0, /* bitpos */
1255 complain_overflow_signed, /* complain_on_overflow */
1256 bfd_elf_generic_reloc, /* special_function */
1257 MORELLO_R_STR (CONDBR19), /* name */
1258 FALSE, /* partial_inplace */
1259 0x7ffff, /* src_mask */
1260 0x7ffff, /* dst_mask */
1261 TRUE), /* pcrel_offset */
1262
1263 /* B: ((S+A-P) >> 2) & 0x3ffffff */
1264 HOWTO64 (MORELLO_R (JUMP26), /* type */
1265 2, /* rightshift */
1266 2, /* size (0 = byte, 1 = short, 2 = long) */
1267 26, /* bitsize */
1268 TRUE, /* pc_relative */
1269 0, /* bitpos */
1270 complain_overflow_signed, /* complain_on_overflow */
1271 bfd_elf_generic_reloc, /* special_function */
1272 MORELLO_R_STR (JUMP26), /* name */
1273 FALSE, /* partial_inplace */
1274 0x3ffffff, /* src_mask */
1275 0x3ffffff, /* dst_mask */
1276 TRUE), /* pcrel_offset */
1277
1278 /* BL: ((S+A-P) >> 2) & 0x3ffffff */
1279 HOWTO64 (MORELLO_R (CALL26), /* type */
1280 2, /* rightshift */
1281 2, /* size (0 = byte, 1 = short, 2 = long) */
1282 26, /* bitsize */
1283 TRUE, /* pc_relative */
1284 0, /* bitpos */
1285 complain_overflow_signed, /* complain_on_overflow */
1286 bfd_elf_generic_reloc, /* special_function */
1287 MORELLO_R_STR (CALL26), /* name */
1288 FALSE, /* partial_inplace */
1289 0x3ffffff, /* src_mask */
1290 0x3ffffff, /* dst_mask */
1291 TRUE), /* pcrel_offset */
1292
a06ea964 1293 /* LD/ST16: (S+A) & 0xffe */
a6bb11b2 1294 HOWTO (AARCH64_R (LDST16_ABS_LO12_NC), /* type */
a06ea964
NC
1295 1, /* rightshift */
1296 2, /* size (0 = byte, 1 = short, 2 = long) */
1297 12, /* bitsize */
1298 FALSE, /* pc_relative */
1299 0, /* bitpos */
1300 complain_overflow_dont, /* complain_on_overflow */
1301 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1302 AARCH64_R_STR (LDST16_ABS_LO12_NC), /* name */
a06ea964
NC
1303 FALSE, /* partial_inplace */
1304 0xffe, /* src_mask */
1305 0xffe, /* dst_mask */
1306 FALSE), /* pcrel_offset */
1307
1308 /* LD/ST32: (S+A) & 0xffc */
a6bb11b2 1309 HOWTO (AARCH64_R (LDST32_ABS_LO12_NC), /* type */
a06ea964
NC
1310 2, /* rightshift */
1311 2, /* size (0 = byte, 1 = short, 2 = long) */
1312 12, /* bitsize */
1313 FALSE, /* pc_relative */
1314 0, /* bitpos */
1315 complain_overflow_dont, /* complain_on_overflow */
1316 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1317 AARCH64_R_STR (LDST32_ABS_LO12_NC), /* name */
a06ea964
NC
1318 FALSE, /* partial_inplace */
1319 0xffc, /* src_mask */
1320 0xffc, /* dst_mask */
1321 FALSE), /* pcrel_offset */
1322
1323 /* LD/ST64: (S+A) & 0xff8 */
a6bb11b2 1324 HOWTO (AARCH64_R (LDST64_ABS_LO12_NC), /* type */
a06ea964
NC
1325 3, /* rightshift */
1326 2, /* size (0 = byte, 1 = short, 2 = long) */
1327 12, /* bitsize */
1328 FALSE, /* pc_relative */
1329 0, /* bitpos */
1330 complain_overflow_dont, /* complain_on_overflow */
1331 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1332 AARCH64_R_STR (LDST64_ABS_LO12_NC), /* name */
a06ea964
NC
1333 FALSE, /* partial_inplace */
1334 0xff8, /* src_mask */
1335 0xff8, /* dst_mask */
1336 FALSE), /* pcrel_offset */
1337
a06ea964 1338 /* LD/ST128: (S+A) & 0xff0 */
a6bb11b2 1339 HOWTO (AARCH64_R (LDST128_ABS_LO12_NC), /* type */
a06ea964
NC
1340 4, /* rightshift */
1341 2, /* size (0 = byte, 1 = short, 2 = long) */
1342 12, /* bitsize */
1343 FALSE, /* pc_relative */
1344 0, /* bitpos */
1345 complain_overflow_dont, /* complain_on_overflow */
1346 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1347 AARCH64_R_STR (LDST128_ABS_LO12_NC), /* name */
a06ea964
NC
1348 FALSE, /* partial_inplace */
1349 0xff0, /* src_mask */
1350 0xff0, /* dst_mask */
1351 FALSE), /* pcrel_offset */
1352
f41aef5f
RE
1353 /* Set a load-literal immediate field to bits
1354 0x1FFFFC of G(S)-P */
a6bb11b2 1355 HOWTO (AARCH64_R (GOT_LD_PREL19), /* type */
f41aef5f
RE
1356 2, /* rightshift */
1357 2, /* size (0 = byte,1 = short,2 = long) */
1358 19, /* bitsize */
1359 TRUE, /* pc_relative */
1360 0, /* bitpos */
1361 complain_overflow_signed, /* complain_on_overflow */
1362 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1363 AARCH64_R_STR (GOT_LD_PREL19), /* name */
f41aef5f
RE
1364 FALSE, /* partial_inplace */
1365 0xffffe0, /* src_mask */
1366 0xffffe0, /* dst_mask */
1367 TRUE), /* pcrel_offset */
1368
a06ea964
NC
1369 /* Get to the page for the GOT entry for the symbol
1370 (G(S) - P) using an ADRP instruction. */
a6bb11b2 1371 HOWTO (AARCH64_R (ADR_GOT_PAGE), /* type */
a06ea964
NC
1372 12, /* rightshift */
1373 2, /* size (0 = byte, 1 = short, 2 = long) */
1374 21, /* bitsize */
1375 TRUE, /* pc_relative */
1376 0, /* bitpos */
1377 complain_overflow_dont, /* complain_on_overflow */
1378 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1379 AARCH64_R_STR (ADR_GOT_PAGE), /* name */
a06ea964
NC
1380 FALSE, /* partial_inplace */
1381 0x1fffff, /* src_mask */
1382 0x1fffff, /* dst_mask */
1383 TRUE), /* pcrel_offset */
1384
92504105
SP
1385 /* Get to the page for the GOT entry for the symbol
1386 (G(S) - P) using a C64 ADRP instruction. */
1387 HOWTO64 (MORELLO_R (ADR_GOT_PAGE), /* type */
1388 12, /* rightshift */
1389 2, /* size (0 = byte, 1 = short, 2 = long) */
1390 20, /* bitsize */
1391 TRUE, /* pc_relative */
1392 0, /* bitpos */
1393 complain_overflow_dont, /* complain_on_overflow */
1394 bfd_elf_generic_reloc, /* special_function */
1395 MORELLO_R_STR (ADR_GOT_PAGE), /* name */
1396 FALSE, /* partial_inplace */
1397 0xfffff, /* src_mask */
1398 0xfffff, /* dst_mask */
1399 TRUE), /* pcrel_offset */
1400
a6bb11b2
YZ
1401 /* LD64: GOT offset G(S) & 0xff8 */
1402 HOWTO64 (AARCH64_R (LD64_GOT_LO12_NC), /* type */
a06ea964
NC
1403 3, /* rightshift */
1404 2, /* size (0 = byte, 1 = short, 2 = long) */
1405 12, /* bitsize */
1406 FALSE, /* pc_relative */
1407 0, /* bitpos */
1408 complain_overflow_dont, /* complain_on_overflow */
1409 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1410 AARCH64_R_STR (LD64_GOT_LO12_NC), /* name */
a06ea964
NC
1411 FALSE, /* partial_inplace */
1412 0xff8, /* src_mask */
1413 0xff8, /* dst_mask */
a6bb11b2 1414 FALSE), /* pcrel_offset */
a06ea964 1415
a1bdea65
SP
1416 /* LD128: GOT offset G(S) & 0xff0 */
1417 HOWTO64 (MORELLO_R (LD128_GOT_LO12_NC), /* type */
1418 4, /* rightshift */
1419 2, /* size (0 = byte, 1 = short, 2 = long) */
1420 12, /* bitsize */
1421 FALSE, /* pc_relative */
1422 0, /* bitpos */
1423 complain_overflow_dont, /* complain_on_overflow */
1424 bfd_elf_generic_reloc, /* special_function */
1425 MORELLO_R_STR (LD128_GOT_LO12_NC), /* name */
1426 FALSE, /* partial_inplace */
1427 0xff0, /* src_mask */
1428 0xff0, /* dst_mask */
1429 FALSE), /* pcrel_offset */
1430
a6bb11b2
YZ
1431 /* LD32: GOT offset G(S) & 0xffc */
1432 HOWTO32 (AARCH64_R (LD32_GOT_LO12_NC), /* type */
1433 2, /* rightshift */
1434 2, /* size (0 = byte, 1 = short, 2 = long) */
1435 12, /* bitsize */
1436 FALSE, /* pc_relative */
1437 0, /* bitpos */
1438 complain_overflow_dont, /* complain_on_overflow */
1439 bfd_elf_generic_reloc, /* special_function */
1440 AARCH64_R_STR (LD32_GOT_LO12_NC), /* name */
1441 FALSE, /* partial_inplace */
1442 0xffc, /* src_mask */
1443 0xffc, /* dst_mask */
1444 FALSE), /* pcrel_offset */
a06ea964 1445
ca632371
RL
1446 /* Lower 16 bits of GOT offset for the symbol. */
1447 HOWTO64 (AARCH64_R (MOVW_GOTOFF_G0_NC), /* type */
1448 0, /* rightshift */
1449 2, /* size (0 = byte, 1 = short, 2 = long) */
1450 16, /* bitsize */
1451 FALSE, /* pc_relative */
1452 0, /* bitpos */
1453 complain_overflow_dont, /* complain_on_overflow */
1454 bfd_elf_generic_reloc, /* special_function */
1455 AARCH64_R_STR (MOVW_GOTOFF_G0_NC), /* name */
1456 FALSE, /* partial_inplace */
1457 0xffff, /* src_mask */
1458 0xffff, /* dst_mask */
1459 FALSE), /* pcrel_offset */
1460
654248e7
RL
1461 /* Higher 16 bits of GOT offset for the symbol. */
1462 HOWTO64 (AARCH64_R (MOVW_GOTOFF_G1), /* type */
1463 16, /* rightshift */
1464 2, /* size (0 = byte, 1 = short, 2 = long) */
1465 16, /* bitsize */
1466 FALSE, /* pc_relative */
1467 0, /* bitpos */
1468 complain_overflow_unsigned, /* complain_on_overflow */
1469 bfd_elf_generic_reloc, /* special_function */
1470 AARCH64_R_STR (MOVW_GOTOFF_G1), /* name */
1471 FALSE, /* partial_inplace */
1472 0xffff, /* src_mask */
1473 0xffff, /* dst_mask */
1474 FALSE), /* pcrel_offset */
1475
87f5fbcc
RL
1476 /* LD64: GOT offset for the symbol. */
1477 HOWTO64 (AARCH64_R (LD64_GOTOFF_LO15), /* type */
1478 3, /* rightshift */
1479 2, /* size (0 = byte, 1 = short, 2 = long) */
1480 12, /* bitsize */
1481 FALSE, /* pc_relative */
1482 0, /* bitpos */
1483 complain_overflow_unsigned, /* complain_on_overflow */
1484 bfd_elf_generic_reloc, /* special_function */
1485 AARCH64_R_STR (LD64_GOTOFF_LO15), /* name */
1486 FALSE, /* partial_inplace */
1487 0x7ff8, /* src_mask */
1488 0x7ff8, /* dst_mask */
1489 FALSE), /* pcrel_offset */
1490
3d715ce4
JW
1491 /* LD32: GOT offset to the page address of GOT table.
1492 (G(S) - PAGE (_GLOBAL_OFFSET_TABLE_)) & 0x5ffc. */
1493 HOWTO32 (AARCH64_R (LD32_GOTPAGE_LO14), /* type */
1494 2, /* rightshift */
1495 2, /* size (0 = byte, 1 = short, 2 = long) */
1496 12, /* bitsize */
1497 FALSE, /* pc_relative */
1498 0, /* bitpos */
1499 complain_overflow_unsigned, /* complain_on_overflow */
1500 bfd_elf_generic_reloc, /* special_function */
1501 AARCH64_R_STR (LD32_GOTPAGE_LO14), /* name */
1502 FALSE, /* partial_inplace */
1503 0x5ffc, /* src_mask */
1504 0x5ffc, /* dst_mask */
1505 FALSE), /* pcrel_offset */
1506
a921b5bd
JW
1507 /* LD64: GOT offset to the page address of GOT table.
1508 (G(S) - PAGE (_GLOBAL_OFFSET_TABLE_)) & 0x7ff8. */
1509 HOWTO64 (AARCH64_R (LD64_GOTPAGE_LO15), /* type */
1510 3, /* rightshift */
1511 2, /* size (0 = byte, 1 = short, 2 = long) */
1512 12, /* bitsize */
1513 FALSE, /* pc_relative */
1514 0, /* bitpos */
1515 complain_overflow_unsigned, /* complain_on_overflow */
1516 bfd_elf_generic_reloc, /* special_function */
1517 AARCH64_R_STR (LD64_GOTPAGE_LO15), /* name */
1518 FALSE, /* partial_inplace */
1519 0x7ff8, /* src_mask */
1520 0x7ff8, /* dst_mask */
1521 FALSE), /* pcrel_offset */
1522
a06ea964
NC
1523 /* Get to the page for the GOT entry for the symbol
1524 (G(S) - P) using an ADRP instruction. */
a6bb11b2 1525 HOWTO (AARCH64_R (TLSGD_ADR_PAGE21), /* type */
a06ea964
NC
1526 12, /* rightshift */
1527 2, /* size (0 = byte, 1 = short, 2 = long) */
1528 21, /* bitsize */
1529 TRUE, /* pc_relative */
1530 0, /* bitpos */
1531 complain_overflow_dont, /* complain_on_overflow */
1532 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1533 AARCH64_R_STR (TLSGD_ADR_PAGE21), /* name */
a06ea964
NC
1534 FALSE, /* partial_inplace */
1535 0x1fffff, /* src_mask */
1536 0x1fffff, /* dst_mask */
1537 TRUE), /* pcrel_offset */
1538
3c12b054
MS
1539 HOWTO (AARCH64_R (TLSGD_ADR_PREL21), /* type */
1540 0, /* rightshift */
1541 2, /* size (0 = byte, 1 = short, 2 = long) */
1542 21, /* bitsize */
1543 TRUE, /* pc_relative */
1544 0, /* bitpos */
1545 complain_overflow_dont, /* complain_on_overflow */
1546 bfd_elf_generic_reloc, /* special_function */
1547 AARCH64_R_STR (TLSGD_ADR_PREL21), /* name */
1548 FALSE, /* partial_inplace */
1549 0x1fffff, /* src_mask */
1550 0x1fffff, /* dst_mask */
1551 TRUE), /* pcrel_offset */
1552
a06ea964 1553 /* ADD: GOT offset G(S) & 0xff8 [no overflow check] */
a6bb11b2 1554 HOWTO (AARCH64_R (TLSGD_ADD_LO12_NC), /* type */
a06ea964
NC
1555 0, /* rightshift */
1556 2, /* size (0 = byte, 1 = short, 2 = long) */
1557 12, /* bitsize */
1558 FALSE, /* pc_relative */
1559 0, /* bitpos */
1560 complain_overflow_dont, /* complain_on_overflow */
1561 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1562 AARCH64_R_STR (TLSGD_ADD_LO12_NC), /* name */
a06ea964
NC
1563 FALSE, /* partial_inplace */
1564 0xfff, /* src_mask */
1565 0xfff, /* dst_mask */
1566 FALSE), /* pcrel_offset */
1567
3e8286c0
RL
1568 /* Lower 16 bits of GOT offset to tls_index. */
1569 HOWTO64 (AARCH64_R (TLSGD_MOVW_G0_NC), /* type */
1570 0, /* rightshift */
1571 2, /* size (0 = byte, 1 = short, 2 = long) */
1572 16, /* bitsize */
1573 FALSE, /* pc_relative */
1574 0, /* bitpos */
1575 complain_overflow_dont, /* complain_on_overflow */
1576 bfd_elf_generic_reloc, /* special_function */
1577 AARCH64_R_STR (TLSGD_MOVW_G0_NC), /* name */
1578 FALSE, /* partial_inplace */
1579 0xffff, /* src_mask */
1580 0xffff, /* dst_mask */
1581 FALSE), /* pcrel_offset */
1582
1aa66fb1
RL
1583 /* Higher 16 bits of GOT offset to tls_index. */
1584 HOWTO64 (AARCH64_R (TLSGD_MOVW_G1), /* type */
1585 16, /* rightshift */
1586 2, /* size (0 = byte, 1 = short, 2 = long) */
1587 16, /* bitsize */
1588 FALSE, /* pc_relative */
1589 0, /* bitpos */
1590 complain_overflow_unsigned, /* complain_on_overflow */
1591 bfd_elf_generic_reloc, /* special_function */
1592 AARCH64_R_STR (TLSGD_MOVW_G1), /* name */
1593 FALSE, /* partial_inplace */
1594 0xffff, /* src_mask */
1595 0xffff, /* dst_mask */
1596 FALSE), /* pcrel_offset */
1597
a6bb11b2 1598 HOWTO (AARCH64_R (TLSIE_ADR_GOTTPREL_PAGE21), /* type */
a06ea964
NC
1599 12, /* rightshift */
1600 2, /* size (0 = byte, 1 = short, 2 = long) */
1601 21, /* bitsize */
1602 FALSE, /* pc_relative */
1603 0, /* bitpos */
1604 complain_overflow_dont, /* complain_on_overflow */
1605 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1606 AARCH64_R_STR (TLSIE_ADR_GOTTPREL_PAGE21), /* name */
a06ea964
NC
1607 FALSE, /* partial_inplace */
1608 0x1fffff, /* src_mask */
1609 0x1fffff, /* dst_mask */
1610 FALSE), /* pcrel_offset */
1611
a6bb11b2 1612 HOWTO64 (AARCH64_R (TLSIE_LD64_GOTTPREL_LO12_NC), /* type */
a06ea964
NC
1613 3, /* rightshift */
1614 2, /* size (0 = byte, 1 = short, 2 = long) */
1615 12, /* bitsize */
1616 FALSE, /* pc_relative */
1617 0, /* bitpos */
1618 complain_overflow_dont, /* complain_on_overflow */
1619 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1620 AARCH64_R_STR (TLSIE_LD64_GOTTPREL_LO12_NC), /* name */
a06ea964
NC
1621 FALSE, /* partial_inplace */
1622 0xff8, /* src_mask */
1623 0xff8, /* dst_mask */
1624 FALSE), /* pcrel_offset */
1625
a6bb11b2
YZ
1626 HOWTO32 (AARCH64_R (TLSIE_LD32_GOTTPREL_LO12_NC), /* type */
1627 2, /* rightshift */
1628 2, /* size (0 = byte, 1 = short, 2 = long) */
1629 12, /* bitsize */
1630 FALSE, /* pc_relative */
1631 0, /* bitpos */
1632 complain_overflow_dont, /* complain_on_overflow */
1633 bfd_elf_generic_reloc, /* special_function */
1634 AARCH64_R_STR (TLSIE_LD32_GOTTPREL_LO12_NC), /* name */
1635 FALSE, /* partial_inplace */
1636 0xffc, /* src_mask */
1637 0xffc, /* dst_mask */
1638 FALSE), /* pcrel_offset */
1639
1640 HOWTO (AARCH64_R (TLSIE_LD_GOTTPREL_PREL19), /* type */
bb3f9ed8 1641 2, /* rightshift */
a06ea964 1642 2, /* size (0 = byte, 1 = short, 2 = long) */
043bf05a 1643 19, /* bitsize */
a06ea964
NC
1644 FALSE, /* pc_relative */
1645 0, /* bitpos */
1646 complain_overflow_dont, /* complain_on_overflow */
1647 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1648 AARCH64_R_STR (TLSIE_LD_GOTTPREL_PREL19), /* name */
a06ea964
NC
1649 FALSE, /* partial_inplace */
1650 0x1ffffc, /* src_mask */
1651 0x1ffffc, /* dst_mask */
1652 FALSE), /* pcrel_offset */
1653
3b957e5b
RL
1654 HOWTO64 (AARCH64_R (TLSIE_MOVW_GOTTPREL_G0_NC), /* type */
1655 0, /* rightshift */
1656 2, /* size (0 = byte, 1 = short, 2 = long) */
1657 16, /* bitsize */
1658 FALSE, /* pc_relative */
1659 0, /* bitpos */
1660 complain_overflow_dont, /* complain_on_overflow */
1661 bfd_elf_generic_reloc, /* special_function */
1662 AARCH64_R_STR (TLSIE_MOVW_GOTTPREL_G0_NC), /* name */
1663 FALSE, /* partial_inplace */
1664 0xffff, /* src_mask */
1665 0xffff, /* dst_mask */
1666 FALSE), /* pcrel_offset */
1667
1668 HOWTO64 (AARCH64_R (TLSIE_MOVW_GOTTPREL_G1), /* type */
1669 16, /* rightshift */
1670 2, /* size (0 = byte, 1 = short, 2 = long) */
1671 16, /* bitsize */
1672 FALSE, /* pc_relative */
1673 0, /* bitpos */
1674 complain_overflow_unsigned, /* complain_on_overflow */
1675 bfd_elf_generic_reloc, /* special_function */
1676 AARCH64_R_STR (TLSIE_MOVW_GOTTPREL_G1), /* name */
1677 FALSE, /* partial_inplace */
1678 0xffff, /* src_mask */
1679 0xffff, /* dst_mask */
1680 FALSE), /* pcrel_offset */
1681
49df5539
JW
1682 /* ADD: bit[23:12] of byte offset to module TLS base address. */
1683 HOWTO (AARCH64_R (TLSLD_ADD_DTPREL_HI12), /* type */
1684 12, /* rightshift */
1685 2, /* size (0 = byte, 1 = short, 2 = long) */
1686 12, /* bitsize */
1687 FALSE, /* pc_relative */
1688 0, /* bitpos */
1689 complain_overflow_unsigned, /* complain_on_overflow */
1690 bfd_elf_generic_reloc, /* special_function */
1691 AARCH64_R_STR (TLSLD_ADD_DTPREL_HI12), /* name */
1692 FALSE, /* partial_inplace */
1693 0xfff, /* src_mask */
1694 0xfff, /* dst_mask */
1695 FALSE), /* pcrel_offset */
1696
70151fb5
JW
1697 /* Unsigned 12 bit byte offset to module TLS base address. */
1698 HOWTO (AARCH64_R (TLSLD_ADD_DTPREL_LO12), /* type */
1699 0, /* rightshift */
1700 2, /* size (0 = byte, 1 = short, 2 = long) */
1701 12, /* bitsize */
1702 FALSE, /* pc_relative */
1703 0, /* bitpos */
1704 complain_overflow_unsigned, /* complain_on_overflow */
1705 bfd_elf_generic_reloc, /* special_function */
1706 AARCH64_R_STR (TLSLD_ADD_DTPREL_LO12), /* name */
1707 FALSE, /* partial_inplace */
1708 0xfff, /* src_mask */
1709 0xfff, /* dst_mask */
1710 FALSE), /* pcrel_offset */
13289c10
JW
1711
1712 /* No overflow check version of BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12. */
1713 HOWTO (AARCH64_R (TLSLD_ADD_DTPREL_LO12_NC), /* type */
1714 0, /* rightshift */
1715 2, /* size (0 = byte, 1 = short, 2 = long) */
1716 12, /* bitsize */
1717 FALSE, /* pc_relative */
1718 0, /* bitpos */
1719 complain_overflow_dont, /* complain_on_overflow */
1720 bfd_elf_generic_reloc, /* special_function */
1721 AARCH64_R_STR (TLSLD_ADD_DTPREL_LO12_NC), /* name */
1722 FALSE, /* partial_inplace */
1723 0xfff, /* src_mask */
1724 0xfff, /* dst_mask */
1725 FALSE), /* pcrel_offset */
70151fb5 1726
a12fad50
JW
1727 /* ADD: GOT offset G(S) & 0xff8 [no overflow check] */
1728 HOWTO (AARCH64_R (TLSLD_ADD_LO12_NC), /* type */
1729 0, /* rightshift */
1730 2, /* size (0 = byte, 1 = short, 2 = long) */
1731 12, /* bitsize */
1732 FALSE, /* pc_relative */
1733 0, /* bitpos */
1734 complain_overflow_dont, /* complain_on_overflow */
1735 bfd_elf_generic_reloc, /* special_function */
1736 AARCH64_R_STR (TLSLD_ADD_LO12_NC), /* name */
1737 FALSE, /* partial_inplace */
1738 0xfff, /* src_mask */
1739 0xfff, /* dst_mask */
1740 FALSE), /* pcrel_offset */
1741
1107e076
JW
1742 /* Get to the page for the GOT entry for the symbol
1743 (G(S) - P) using an ADRP instruction. */
1744 HOWTO (AARCH64_R (TLSLD_ADR_PAGE21), /* type */
1745 12, /* rightshift */
1746 2, /* size (0 = byte, 1 = short, 2 = long) */
1747 21, /* bitsize */
1748 TRUE, /* pc_relative */
1749 0, /* bitpos */
1750 complain_overflow_signed, /* complain_on_overflow */
1751 bfd_elf_generic_reloc, /* special_function */
1752 AARCH64_R_STR (TLSLD_ADR_PAGE21), /* name */
1753 FALSE, /* partial_inplace */
1754 0x1fffff, /* src_mask */
1755 0x1fffff, /* dst_mask */
1756 TRUE), /* pcrel_offset */
1757
6c37fedc
JW
1758 HOWTO (AARCH64_R (TLSLD_ADR_PREL21), /* type */
1759 0, /* rightshift */
1760 2, /* size (0 = byte, 1 = short, 2 = long) */
1761 21, /* bitsize */
1762 TRUE, /* pc_relative */
1763 0, /* bitpos */
1764 complain_overflow_signed, /* complain_on_overflow */
1765 bfd_elf_generic_reloc, /* special_function */
1766 AARCH64_R_STR (TLSLD_ADR_PREL21), /* name */
1767 FALSE, /* partial_inplace */
1768 0x1fffff, /* src_mask */
1769 0x1fffff, /* dst_mask */
1770 TRUE), /* pcrel_offset */
1771
4c562523
JW
1772 /* LD/ST16: bit[11:1] of byte offset to module TLS base address. */
1773 HOWTO64 (AARCH64_R (TLSLD_LDST16_DTPREL_LO12), /* type */
1774 1, /* rightshift */
1775 2, /* size (0 = byte, 1 = short, 2 = long) */
1776 11, /* bitsize */
1777 FALSE, /* pc_relative */
1778 10, /* bitpos */
1779 complain_overflow_unsigned, /* complain_on_overflow */
1780 bfd_elf_generic_reloc, /* special_function */
1781 AARCH64_R_STR (TLSLD_LDST16_DTPREL_LO12), /* name */
1782 FALSE, /* partial_inplace */
1783 0x1ffc00, /* src_mask */
1784 0x1ffc00, /* dst_mask */
1785 FALSE), /* pcrel_offset */
1786
1787 /* Same as BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12, but no overflow check. */
1788 HOWTO64 (AARCH64_R (TLSLD_LDST16_DTPREL_LO12_NC), /* type */
1789 1, /* rightshift */
1790 2, /* size (0 = byte, 1 = short, 2 = long) */
1791 11, /* bitsize */
1792 FALSE, /* pc_relative */
1793 10, /* bitpos */
1794 complain_overflow_dont, /* complain_on_overflow */
1795 bfd_elf_generic_reloc, /* special_function */
1796 AARCH64_R_STR (TLSLD_LDST16_DTPREL_LO12_NC), /* name */
1797 FALSE, /* partial_inplace */
1798 0x1ffc00, /* src_mask */
1799 0x1ffc00, /* dst_mask */
1800 FALSE), /* pcrel_offset */
1801
1802 /* LD/ST32: bit[11:2] of byte offset to module TLS base address. */
1803 HOWTO64 (AARCH64_R (TLSLD_LDST32_DTPREL_LO12), /* type */
1804 2, /* rightshift */
1805 2, /* size (0 = byte, 1 = short, 2 = long) */
1806 10, /* bitsize */
1807 FALSE, /* pc_relative */
1808 10, /* bitpos */
1809 complain_overflow_unsigned, /* complain_on_overflow */
1810 bfd_elf_generic_reloc, /* special_function */
1811 AARCH64_R_STR (TLSLD_LDST32_DTPREL_LO12), /* name */
1812 FALSE, /* partial_inplace */
1813 0x3ffc00, /* src_mask */
1814 0x3ffc00, /* dst_mask */
1815 FALSE), /* pcrel_offset */
1816
1817 /* Same as BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12, but no overflow check. */
1818 HOWTO64 (AARCH64_R (TLSLD_LDST32_DTPREL_LO12_NC), /* type */
1819 2, /* rightshift */
1820 2, /* size (0 = byte, 1 = short, 2 = long) */
1821 10, /* bitsize */
1822 FALSE, /* pc_relative */
1823 10, /* bitpos */
1824 complain_overflow_dont, /* complain_on_overflow */
1825 bfd_elf_generic_reloc, /* special_function */
1826 AARCH64_R_STR (TLSLD_LDST32_DTPREL_LO12_NC), /* name */
1827 FALSE, /* partial_inplace */
1828 0xffc00, /* src_mask */
1829 0xffc00, /* dst_mask */
1830 FALSE), /* pcrel_offset */
1831
1832 /* LD/ST64: bit[11:3] of byte offset to module TLS base address. */
1833 HOWTO64 (AARCH64_R (TLSLD_LDST64_DTPREL_LO12), /* type */
1834 3, /* rightshift */
1835 2, /* size (0 = byte, 1 = short, 2 = long) */
1836 9, /* bitsize */
1837 FALSE, /* pc_relative */
1838 10, /* bitpos */
1839 complain_overflow_unsigned, /* complain_on_overflow */
1840 bfd_elf_generic_reloc, /* special_function */
1841 AARCH64_R_STR (TLSLD_LDST64_DTPREL_LO12), /* name */
1842 FALSE, /* partial_inplace */
1843 0x3ffc00, /* src_mask */
1844 0x3ffc00, /* dst_mask */
1845 FALSE), /* pcrel_offset */
1846
1847 /* Same as BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12, but no overflow check. */
1848 HOWTO64 (AARCH64_R (TLSLD_LDST64_DTPREL_LO12_NC), /* type */
1849 3, /* rightshift */
1850 2, /* size (0 = byte, 1 = short, 2 = long) */
1851 9, /* bitsize */
1852 FALSE, /* pc_relative */
1853 10, /* bitpos */
1854 complain_overflow_dont, /* complain_on_overflow */
1855 bfd_elf_generic_reloc, /* special_function */
1856 AARCH64_R_STR (TLSLD_LDST64_DTPREL_LO12_NC), /* name */
1857 FALSE, /* partial_inplace */
1858 0x7fc00, /* src_mask */
1859 0x7fc00, /* dst_mask */
1860 FALSE), /* pcrel_offset */
1861
1862 /* LD/ST8: bit[11:0] of byte offset to module TLS base address. */
1863 HOWTO64 (AARCH64_R (TLSLD_LDST8_DTPREL_LO12), /* type */
1864 0, /* rightshift */
1865 2, /* size (0 = byte, 1 = short, 2 = long) */
1866 12, /* bitsize */
1867 FALSE, /* pc_relative */
1868 10, /* bitpos */
1869 complain_overflow_unsigned, /* complain_on_overflow */
1870 bfd_elf_generic_reloc, /* special_function */
1871 AARCH64_R_STR (TLSLD_LDST8_DTPREL_LO12), /* name */
1872 FALSE, /* partial_inplace */
1873 0x3ffc00, /* src_mask */
1874 0x3ffc00, /* dst_mask */
1875 FALSE), /* pcrel_offset */
1876
1877 /* Same as BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12, but no overflow check. */
1878 HOWTO64 (AARCH64_R (TLSLD_LDST8_DTPREL_LO12_NC), /* type */
1879 0, /* rightshift */
1880 2, /* size (0 = byte, 1 = short, 2 = long) */
1881 12, /* bitsize */
1882 FALSE, /* pc_relative */
1883 10, /* bitpos */
1884 complain_overflow_dont, /* complain_on_overflow */
1885 bfd_elf_generic_reloc, /* special_function */
1886 AARCH64_R_STR (TLSLD_LDST8_DTPREL_LO12_NC), /* name */
1887 FALSE, /* partial_inplace */
1888 0x3ffc00, /* src_mask */
1889 0x3ffc00, /* dst_mask */
1890 FALSE), /* pcrel_offset */
1891
49df5539
JW
1892 /* MOVZ: bit[15:0] of byte offset to module TLS base address. */
1893 HOWTO (AARCH64_R (TLSLD_MOVW_DTPREL_G0), /* type */
1894 0, /* rightshift */
1895 2, /* size (0 = byte, 1 = short, 2 = long) */
1896 16, /* bitsize */
1897 FALSE, /* pc_relative */
1898 0, /* bitpos */
1899 complain_overflow_unsigned, /* complain_on_overflow */
1900 bfd_elf_generic_reloc, /* special_function */
1901 AARCH64_R_STR (TLSLD_MOVW_DTPREL_G0), /* name */
1902 FALSE, /* partial_inplace */
1903 0xffff, /* src_mask */
1904 0xffff, /* dst_mask */
1905 FALSE), /* pcrel_offset */
1906
1907 /* No overflow check version of BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0. */
1908 HOWTO (AARCH64_R (TLSLD_MOVW_DTPREL_G0_NC), /* type */
1909 0, /* rightshift */
1910 2, /* size (0 = byte, 1 = short, 2 = long) */
1911 16, /* bitsize */
1912 FALSE, /* pc_relative */
1913 0, /* bitpos */
1914 complain_overflow_dont, /* complain_on_overflow */
1915 bfd_elf_generic_reloc, /* special_function */
1916 AARCH64_R_STR (TLSLD_MOVW_DTPREL_G0_NC), /* name */
1917 FALSE, /* partial_inplace */
1918 0xffff, /* src_mask */
1919 0xffff, /* dst_mask */
1920 FALSE), /* pcrel_offset */
1921
1922 /* MOVZ: bit[31:16] of byte offset to module TLS base address. */
1923 HOWTO (AARCH64_R (TLSLD_MOVW_DTPREL_G1), /* type */
1924 16, /* rightshift */
1925 2, /* size (0 = byte, 1 = short, 2 = long) */
1926 16, /* bitsize */
1927 FALSE, /* pc_relative */
1928 0, /* bitpos */
1929 complain_overflow_unsigned, /* complain_on_overflow */
1930 bfd_elf_generic_reloc, /* special_function */
1931 AARCH64_R_STR (TLSLD_MOVW_DTPREL_G1), /* name */
1932 FALSE, /* partial_inplace */
1933 0xffff, /* src_mask */
1934 0xffff, /* dst_mask */
1935 FALSE), /* pcrel_offset */
1936
1937 /* No overflow check version of BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1. */
1938 HOWTO64 (AARCH64_R (TLSLD_MOVW_DTPREL_G1_NC), /* type */
1939 16, /* rightshift */
1940 2, /* size (0 = byte, 1 = short, 2 = long) */
1941 16, /* bitsize */
1942 FALSE, /* pc_relative */
1943 0, /* bitpos */
1944 complain_overflow_dont, /* complain_on_overflow */
1945 bfd_elf_generic_reloc, /* special_function */
1946 AARCH64_R_STR (TLSLD_MOVW_DTPREL_G1_NC), /* name */
1947 FALSE, /* partial_inplace */
1948 0xffff, /* src_mask */
1949 0xffff, /* dst_mask */
1950 FALSE), /* pcrel_offset */
1951
1952 /* MOVZ: bit[47:32] of byte offset to module TLS base address. */
1953 HOWTO64 (AARCH64_R (TLSLD_MOVW_DTPREL_G2), /* type */
1954 32, /* rightshift */
1955 2, /* size (0 = byte, 1 = short, 2 = long) */
1956 16, /* bitsize */
1957 FALSE, /* pc_relative */
1958 0, /* bitpos */
1959 complain_overflow_unsigned, /* complain_on_overflow */
1960 bfd_elf_generic_reloc, /* special_function */
1961 AARCH64_R_STR (TLSLD_MOVW_DTPREL_G2), /* name */
1962 FALSE, /* partial_inplace */
1963 0xffff, /* src_mask */
1964 0xffff, /* dst_mask */
1965 FALSE), /* pcrel_offset */
1966
a6bb11b2 1967 HOWTO64 (AARCH64_R (TLSLE_MOVW_TPREL_G2), /* type */
bb3f9ed8 1968 32, /* rightshift */
a06ea964 1969 2, /* size (0 = byte, 1 = short, 2 = long) */
07875fbc 1970 16, /* bitsize */
a06ea964
NC
1971 FALSE, /* pc_relative */
1972 0, /* bitpos */
0172429c 1973 complain_overflow_unsigned, /* complain_on_overflow */
a06ea964 1974 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1975 AARCH64_R_STR (TLSLE_MOVW_TPREL_G2), /* name */
a06ea964
NC
1976 FALSE, /* partial_inplace */
1977 0xffff, /* src_mask */
1978 0xffff, /* dst_mask */
1979 FALSE), /* pcrel_offset */
1980
a6bb11b2 1981 HOWTO (AARCH64_R (TLSLE_MOVW_TPREL_G1), /* type */
bb3f9ed8 1982 16, /* rightshift */
a06ea964 1983 2, /* size (0 = byte, 1 = short, 2 = long) */
07875fbc 1984 16, /* bitsize */
a06ea964
NC
1985 FALSE, /* pc_relative */
1986 0, /* bitpos */
1987 complain_overflow_dont, /* complain_on_overflow */
1988 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 1989 AARCH64_R_STR (TLSLE_MOVW_TPREL_G1), /* name */
a06ea964
NC
1990 FALSE, /* partial_inplace */
1991 0xffff, /* src_mask */
1992 0xffff, /* dst_mask */
1993 FALSE), /* pcrel_offset */
1994
a6bb11b2 1995 HOWTO64 (AARCH64_R (TLSLE_MOVW_TPREL_G1_NC), /* type */
bb3f9ed8 1996 16, /* rightshift */
a06ea964 1997 2, /* size (0 = byte, 1 = short, 2 = long) */
07875fbc 1998 16, /* bitsize */
a06ea964
NC
1999 FALSE, /* pc_relative */
2000 0, /* bitpos */
2001 complain_overflow_dont, /* complain_on_overflow */
2002 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 2003 AARCH64_R_STR (TLSLE_MOVW_TPREL_G1_NC), /* name */
a06ea964
NC
2004 FALSE, /* partial_inplace */
2005 0xffff, /* src_mask */
2006 0xffff, /* dst_mask */
2007 FALSE), /* pcrel_offset */
2008
a6bb11b2 2009 HOWTO (AARCH64_R (TLSLE_MOVW_TPREL_G0), /* type */
a06ea964
NC
2010 0, /* rightshift */
2011 2, /* size (0 = byte, 1 = short, 2 = long) */
07875fbc 2012 16, /* bitsize */
a06ea964
NC
2013 FALSE, /* pc_relative */
2014 0, /* bitpos */
2015 complain_overflow_dont, /* complain_on_overflow */
2016 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 2017 AARCH64_R_STR (TLSLE_MOVW_TPREL_G0), /* name */
a06ea964
NC
2018 FALSE, /* partial_inplace */
2019 0xffff, /* src_mask */
2020 0xffff, /* dst_mask */
2021 FALSE), /* pcrel_offset */
2022
a6bb11b2 2023 HOWTO (AARCH64_R (TLSLE_MOVW_TPREL_G0_NC), /* type */
a06ea964
NC
2024 0, /* rightshift */
2025 2, /* size (0 = byte, 1 = short, 2 = long) */
07875fbc 2026 16, /* bitsize */
a06ea964
NC
2027 FALSE, /* pc_relative */
2028 0, /* bitpos */
2029 complain_overflow_dont, /* complain_on_overflow */
2030 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 2031 AARCH64_R_STR (TLSLE_MOVW_TPREL_G0_NC), /* name */
a06ea964
NC
2032 FALSE, /* partial_inplace */
2033 0xffff, /* src_mask */
2034 0xffff, /* dst_mask */
2035 FALSE), /* pcrel_offset */
2036
a6bb11b2 2037 HOWTO (AARCH64_R (TLSLE_ADD_TPREL_HI12), /* type */
bb3f9ed8 2038 12, /* rightshift */
a06ea964
NC
2039 2, /* size (0 = byte, 1 = short, 2 = long) */
2040 12, /* bitsize */
2041 FALSE, /* pc_relative */
2042 0, /* bitpos */
bab91cce 2043 complain_overflow_unsigned, /* complain_on_overflow */
a06ea964 2044 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 2045 AARCH64_R_STR (TLSLE_ADD_TPREL_HI12), /* name */
a06ea964
NC
2046 FALSE, /* partial_inplace */
2047 0xfff, /* src_mask */
2048 0xfff, /* dst_mask */
2049 FALSE), /* pcrel_offset */
2050
a6bb11b2 2051 HOWTO (AARCH64_R (TLSLE_ADD_TPREL_LO12), /* type */
a06ea964
NC
2052 0, /* rightshift */
2053 2, /* size (0 = byte, 1 = short, 2 = long) */
2054 12, /* bitsize */
2055 FALSE, /* pc_relative */
2056 0, /* bitpos */
36e6c140 2057 complain_overflow_unsigned, /* complain_on_overflow */
a06ea964 2058 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 2059 AARCH64_R_STR (TLSLE_ADD_TPREL_LO12), /* name */
a06ea964
NC
2060 FALSE, /* partial_inplace */
2061 0xfff, /* src_mask */
2062 0xfff, /* dst_mask */
2063 FALSE), /* pcrel_offset */
2064
a6bb11b2 2065 HOWTO (AARCH64_R (TLSLE_ADD_TPREL_LO12_NC), /* type */
a06ea964
NC
2066 0, /* rightshift */
2067 2, /* size (0 = byte, 1 = short, 2 = long) */
2068 12, /* bitsize */
2069 FALSE, /* pc_relative */
2070 0, /* bitpos */
2071 complain_overflow_dont, /* complain_on_overflow */
2072 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 2073 AARCH64_R_STR (TLSLE_ADD_TPREL_LO12_NC), /* name */
a06ea964
NC
2074 FALSE, /* partial_inplace */
2075 0xfff, /* src_mask */
2076 0xfff, /* dst_mask */
2077 FALSE), /* pcrel_offset */
a06ea964 2078
84f1b9fb
RL
2079 /* LD/ST16: bit[11:1] of byte offset to module TLS base address. */
2080 HOWTO (AARCH64_R (TLSLE_LDST16_TPREL_LO12), /* type */
2081 1, /* rightshift */
2082 2, /* size (0 = byte, 1 = short, 2 = long) */
2083 11, /* bitsize */
2084 FALSE, /* pc_relative */
2085 10, /* bitpos */
2086 complain_overflow_unsigned, /* complain_on_overflow */
2087 bfd_elf_generic_reloc, /* special_function */
2088 AARCH64_R_STR (TLSLE_LDST16_TPREL_LO12), /* name */
2089 FALSE, /* partial_inplace */
2090 0x1ffc00, /* src_mask */
2091 0x1ffc00, /* dst_mask */
2092 FALSE), /* pcrel_offset */
2093
2094 /* Same as BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12, but no overflow check. */
2095 HOWTO (AARCH64_R (TLSLE_LDST16_TPREL_LO12_NC), /* type */
2096 1, /* rightshift */
2097 2, /* size (0 = byte, 1 = short, 2 = long) */
2098 11, /* bitsize */
2099 FALSE, /* pc_relative */
2100 10, /* bitpos */
2101 complain_overflow_dont, /* complain_on_overflow */
2102 bfd_elf_generic_reloc, /* special_function */
2103 AARCH64_R_STR (TLSLE_LDST16_TPREL_LO12_NC), /* name */
2104 FALSE, /* partial_inplace */
2105 0x1ffc00, /* src_mask */
2106 0x1ffc00, /* dst_mask */
2107 FALSE), /* pcrel_offset */
2108
2109 /* LD/ST32: bit[11:2] of byte offset to module TLS base address. */
2110 HOWTO (AARCH64_R (TLSLE_LDST32_TPREL_LO12), /* type */
2111 2, /* rightshift */
2112 2, /* size (0 = byte, 1 = short, 2 = long) */
2113 10, /* bitsize */
2114 FALSE, /* pc_relative */
2115 10, /* bitpos */
2116 complain_overflow_unsigned, /* complain_on_overflow */
2117 bfd_elf_generic_reloc, /* special_function */
2118 AARCH64_R_STR (TLSLE_LDST32_TPREL_LO12), /* name */
2119 FALSE, /* partial_inplace */
2120 0xffc00, /* src_mask */
2121 0xffc00, /* dst_mask */
2122 FALSE), /* pcrel_offset */
2123
2124 /* Same as BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12, but no overflow check. */
2125 HOWTO (AARCH64_R (TLSLE_LDST32_TPREL_LO12_NC), /* type */
2126 2, /* rightshift */
2127 2, /* size (0 = byte, 1 = short, 2 = long) */
2128 10, /* bitsize */
2129 FALSE, /* pc_relative */
2130 10, /* bitpos */
2131 complain_overflow_dont, /* complain_on_overflow */
2132 bfd_elf_generic_reloc, /* special_function */
2133 AARCH64_R_STR (TLSLE_LDST32_TPREL_LO12_NC), /* name */
2134 FALSE, /* partial_inplace */
2135 0xffc00, /* src_mask */
2136 0xffc00, /* dst_mask */
2137 FALSE), /* pcrel_offset */
2138
2139 /* LD/ST64: bit[11:3] of byte offset to module TLS base address. */
2140 HOWTO (AARCH64_R (TLSLE_LDST64_TPREL_LO12), /* type */
2141 3, /* rightshift */
2142 2, /* size (0 = byte, 1 = short, 2 = long) */
2143 9, /* bitsize */
2144 FALSE, /* pc_relative */
2145 10, /* bitpos */
2146 complain_overflow_unsigned, /* complain_on_overflow */
2147 bfd_elf_generic_reloc, /* special_function */
2148 AARCH64_R_STR (TLSLE_LDST64_TPREL_LO12), /* name */
2149 FALSE, /* partial_inplace */
2150 0x7fc00, /* src_mask */
2151 0x7fc00, /* dst_mask */
2152 FALSE), /* pcrel_offset */
2153
2154 /* Same as BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12, but no overflow check. */
2155 HOWTO (AARCH64_R (TLSLE_LDST64_TPREL_LO12_NC), /* type */
2156 3, /* rightshift */
2157 2, /* size (0 = byte, 1 = short, 2 = long) */
2158 9, /* bitsize */
2159 FALSE, /* pc_relative */
2160 10, /* bitpos */
2161 complain_overflow_dont, /* complain_on_overflow */
2162 bfd_elf_generic_reloc, /* special_function */
2163 AARCH64_R_STR (TLSLE_LDST64_TPREL_LO12_NC), /* name */
2164 FALSE, /* partial_inplace */
2165 0x7fc00, /* src_mask */
2166 0x7fc00, /* dst_mask */
2167 FALSE), /* pcrel_offset */
2168
2169 /* LD/ST8: bit[11:0] of byte offset to module TLS base address. */
2170 HOWTO (AARCH64_R (TLSLE_LDST8_TPREL_LO12), /* type */
2171 0, /* rightshift */
2172 2, /* size (0 = byte, 1 = short, 2 = long) */
2173 12, /* bitsize */
2174 FALSE, /* pc_relative */
2175 10, /* bitpos */
2176 complain_overflow_unsigned, /* complain_on_overflow */
2177 bfd_elf_generic_reloc, /* special_function */
2178 AARCH64_R_STR (TLSLE_LDST8_TPREL_LO12), /* name */
2179 FALSE, /* partial_inplace */
2180 0x3ffc00, /* src_mask */
2181 0x3ffc00, /* dst_mask */
2182 FALSE), /* pcrel_offset */
2183
2184 /* Same as BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12, but no overflow check. */
2185 HOWTO (AARCH64_R (TLSLE_LDST8_TPREL_LO12_NC), /* type */
2186 0, /* rightshift */
2187 2, /* size (0 = byte, 1 = short, 2 = long) */
2188 12, /* bitsize */
2189 FALSE, /* pc_relative */
2190 10, /* bitpos */
2191 complain_overflow_dont, /* complain_on_overflow */
2192 bfd_elf_generic_reloc, /* special_function */
2193 AARCH64_R_STR (TLSLE_LDST8_TPREL_LO12_NC), /* name */
2194 FALSE, /* partial_inplace */
2195 0x3ffc00, /* src_mask */
2196 0x3ffc00, /* dst_mask */
2197 FALSE), /* pcrel_offset */
2198
a6bb11b2 2199 HOWTO (AARCH64_R (TLSDESC_LD_PREL19), /* type */
bb3f9ed8 2200 2, /* rightshift */
a06ea964 2201 2, /* size (0 = byte, 1 = short, 2 = long) */
1ada945d 2202 19, /* bitsize */
a06ea964
NC
2203 TRUE, /* pc_relative */
2204 0, /* bitpos */
2205 complain_overflow_dont, /* complain_on_overflow */
2206 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 2207 AARCH64_R_STR (TLSDESC_LD_PREL19), /* name */
a06ea964 2208 FALSE, /* partial_inplace */
1ada945d
MS
2209 0x0ffffe0, /* src_mask */
2210 0x0ffffe0, /* dst_mask */
a06ea964
NC
2211 TRUE), /* pcrel_offset */
2212
a6bb11b2 2213 HOWTO (AARCH64_R (TLSDESC_ADR_PREL21), /* type */
a06ea964
NC
2214 0, /* rightshift */
2215 2, /* size (0 = byte, 1 = short, 2 = long) */
2216 21, /* bitsize */
2217 TRUE, /* pc_relative */
2218 0, /* bitpos */
2219 complain_overflow_dont, /* complain_on_overflow */
2220 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 2221 AARCH64_R_STR (TLSDESC_ADR_PREL21), /* name */
a06ea964
NC
2222 FALSE, /* partial_inplace */
2223 0x1fffff, /* src_mask */
2224 0x1fffff, /* dst_mask */
2225 TRUE), /* pcrel_offset */
2226
2227 /* Get to the page for the GOT entry for the symbol
2228 (G(S) - P) using an ADRP instruction. */
a6bb11b2 2229 HOWTO (AARCH64_R (TLSDESC_ADR_PAGE21), /* type */
a06ea964
NC
2230 12, /* rightshift */
2231 2, /* size (0 = byte, 1 = short, 2 = long) */
2232 21, /* bitsize */
2233 TRUE, /* pc_relative */
2234 0, /* bitpos */
2235 complain_overflow_dont, /* complain_on_overflow */
2236 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 2237 AARCH64_R_STR (TLSDESC_ADR_PAGE21), /* name */
a06ea964
NC
2238 FALSE, /* partial_inplace */
2239 0x1fffff, /* src_mask */
2240 0x1fffff, /* dst_mask */
2241 TRUE), /* pcrel_offset */
2242
a6bb11b2 2243 /* LD64: GOT offset G(S) & 0xff8. */
f955cccf 2244 HOWTO64 (AARCH64_R (TLSDESC_LD64_LO12), /* type */
a06ea964
NC
2245 3, /* rightshift */
2246 2, /* size (0 = byte, 1 = short, 2 = long) */
2247 12, /* bitsize */
2248 FALSE, /* pc_relative */
2249 0, /* bitpos */
2250 complain_overflow_dont, /* complain_on_overflow */
2251 bfd_elf_generic_reloc, /* special_function */
f955cccf 2252 AARCH64_R_STR (TLSDESC_LD64_LO12), /* name */
a06ea964 2253 FALSE, /* partial_inplace */
a6bb11b2
YZ
2254 0xff8, /* src_mask */
2255 0xff8, /* dst_mask */
2256 FALSE), /* pcrel_offset */
2257
2258 /* LD32: GOT offset G(S) & 0xffc. */
2259 HOWTO32 (AARCH64_R (TLSDESC_LD32_LO12_NC), /* type */
2260 2, /* rightshift */
2261 2, /* size (0 = byte, 1 = short, 2 = long) */
2262 12, /* bitsize */
2263 FALSE, /* pc_relative */
2264 0, /* bitpos */
2265 complain_overflow_dont, /* complain_on_overflow */
2266 bfd_elf_generic_reloc, /* special_function */
2267 AARCH64_R_STR (TLSDESC_LD32_LO12_NC), /* name */
2268 FALSE, /* partial_inplace */
2269 0xffc, /* src_mask */
2270 0xffc, /* dst_mask */
a06ea964
NC
2271 FALSE), /* pcrel_offset */
2272
2273 /* ADD: GOT offset G(S) & 0xfff. */
f955cccf 2274 HOWTO (AARCH64_R (TLSDESC_ADD_LO12), /* type */
a06ea964
NC
2275 0, /* rightshift */
2276 2, /* size (0 = byte, 1 = short, 2 = long) */
2277 12, /* bitsize */
2278 FALSE, /* pc_relative */
2279 0, /* bitpos */
f955cccf 2280 complain_overflow_dont,/* complain_on_overflow */
a06ea964 2281 bfd_elf_generic_reloc, /* special_function */
f955cccf 2282 AARCH64_R_STR (TLSDESC_ADD_LO12), /* name */
a06ea964
NC
2283 FALSE, /* partial_inplace */
2284 0xfff, /* src_mask */
2285 0xfff, /* dst_mask */
2286 FALSE), /* pcrel_offset */
2287
a6bb11b2 2288 HOWTO64 (AARCH64_R (TLSDESC_OFF_G1), /* type */
bb3f9ed8 2289 16, /* rightshift */
a06ea964
NC
2290 2, /* size (0 = byte, 1 = short, 2 = long) */
2291 12, /* bitsize */
2292 FALSE, /* pc_relative */
2293 0, /* bitpos */
43a357f9 2294 complain_overflow_unsigned, /* complain_on_overflow */
a06ea964 2295 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 2296 AARCH64_R_STR (TLSDESC_OFF_G1), /* name */
a06ea964
NC
2297 FALSE, /* partial_inplace */
2298 0xffff, /* src_mask */
2299 0xffff, /* dst_mask */
2300 FALSE), /* pcrel_offset */
2301
a6bb11b2 2302 HOWTO64 (AARCH64_R (TLSDESC_OFF_G0_NC), /* type */
a06ea964
NC
2303 0, /* rightshift */
2304 2, /* size (0 = byte, 1 = short, 2 = long) */
2305 12, /* bitsize */
2306 FALSE, /* pc_relative */
2307 0, /* bitpos */
2308 complain_overflow_dont, /* complain_on_overflow */
2309 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 2310 AARCH64_R_STR (TLSDESC_OFF_G0_NC), /* name */
a06ea964
NC
2311 FALSE, /* partial_inplace */
2312 0xffff, /* src_mask */
2313 0xffff, /* dst_mask */
2314 FALSE), /* pcrel_offset */
2315
a6bb11b2 2316 HOWTO64 (AARCH64_R (TLSDESC_LDR), /* type */
a06ea964
NC
2317 0, /* rightshift */
2318 2, /* size (0 = byte, 1 = short, 2 = long) */
2319 12, /* bitsize */
2320 FALSE, /* pc_relative */
2321 0, /* bitpos */
2322 complain_overflow_dont, /* complain_on_overflow */
2323 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 2324 AARCH64_R_STR (TLSDESC_LDR), /* name */
a06ea964
NC
2325 FALSE, /* partial_inplace */
2326 0x0, /* src_mask */
2327 0x0, /* dst_mask */
2328 FALSE), /* pcrel_offset */
2329
a6bb11b2 2330 HOWTO64 (AARCH64_R (TLSDESC_ADD), /* type */
a06ea964
NC
2331 0, /* rightshift */
2332 2, /* size (0 = byte, 1 = short, 2 = long) */
2333 12, /* bitsize */
2334 FALSE, /* pc_relative */
2335 0, /* bitpos */
2336 complain_overflow_dont, /* complain_on_overflow */
2337 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 2338 AARCH64_R_STR (TLSDESC_ADD), /* name */
a06ea964
NC
2339 FALSE, /* partial_inplace */
2340 0x0, /* src_mask */
2341 0x0, /* dst_mask */
2342 FALSE), /* pcrel_offset */
2343
a6bb11b2 2344 HOWTO (AARCH64_R (TLSDESC_CALL), /* type */
a06ea964
NC
2345 0, /* rightshift */
2346 2, /* size (0 = byte, 1 = short, 2 = long) */
7366006f 2347 0, /* bitsize */
a06ea964
NC
2348 FALSE, /* pc_relative */
2349 0, /* bitpos */
2350 complain_overflow_dont, /* complain_on_overflow */
2351 bfd_elf_generic_reloc, /* special_function */
a6bb11b2 2352 AARCH64_R_STR (TLSDESC_CALL), /* name */
a06ea964
NC
2353 FALSE, /* partial_inplace */
2354 0x0, /* src_mask */
2355 0x0, /* dst_mask */
2356 FALSE), /* pcrel_offset */
a6bb11b2 2357
4ca9b406
SP
2358 /* Get to the page for the GOT entry for the symbol
2359 (G(S) - P) using an ADRP instruction. */
2360 HOWTO64 (MORELLO_R (TLSDESC_ADR_PAGE20), /* type */
2361 12, /* rightshift */
2362 2, /* size (0 = byte, 1 = short, 2 = long) */
2363 20, /* bitsize */
2364 TRUE, /* pc_relative */
2365 0, /* bitpos */
2366 complain_overflow_dont, /* complain_on_overflow */
2367 bfd_elf_generic_reloc, /* special_function */
2368 MORELLO_R_STR (TLSDESC_ADR_PAGE20), /* name */
2369 FALSE, /* partial_inplace */
2370 0xfffff, /* src_mask */
2371 0xfffff, /* dst_mask */
2372 TRUE), /* pcrel_offset */
2373
2374 /* LD128: GOT offset G(S) & 0xff0. */
2375 HOWTO64 (MORELLO_R (TLSDESC_LD128_LO12), /* type */
2376 4, /* rightshift */
2377 2, /* size (0 = byte, 1 = short, 2 = long) */
2378 12, /* bitsize */
2379 FALSE, /* pc_relative */
2380 0, /* bitpos */
2381 complain_overflow_dont, /* complain_on_overflow */
2382 bfd_elf_generic_reloc, /* special_function */
2383 MORELLO_R_STR (TLSDESC_LD128_LO12), /* name */
2384 FALSE, /* partial_inplace */
2385 0xff0, /* src_mask */
2386 0xff0, /* dst_mask */
2387 FALSE), /* pcrel_offset */
2388
2389 HOWTO64 (MORELLO_R (TLSDESC_CALL), /* type */
2390 0, /* rightshift */
2391 2, /* size (0 = byte, 1 = short, 2 = long) */
2392 0, /* bitsize */
2393 FALSE, /* pc_relative */
2394 0, /* bitpos */
2395 complain_overflow_dont, /* complain_on_overflow */
2396 bfd_elf_generic_reloc, /* special_function */
2397 MORELLO_R_STR (TLSDESC_CALL), /* name */
2398 FALSE, /* partial_inplace */
2399 0x0, /* src_mask */
2400 0x0, /* dst_mask */
2401 FALSE), /* pcrel_offset */
2402
1408e112
MM
2403 HOWTO64 (MORELLO_R (TLSIE_ADR_GOTTPREL_PAGE20), /* type */
2404 12, /* rightshift */
2405 2, /* size (0 = byte, 1 = short, 2 = long) */
2406 20, /* bitsize */
2407 TRUE, /* pc_relative */
2408 0, /* bitpos */
2409 complain_overflow_signed, /* complain_on_overflow */
2410 bfd_elf_generic_reloc, /* special_function */
2411 MORELLO_R_STR (TLSIE_ADR_GOTTPREL_PAGE20), /* name */
2412 FALSE, /* partial_inplace */
2413 0xfffff, /* src_mask */
2414 0xfffff, /* dst_mask */
2415 TRUE), /* pcrel_offset */
2416
2417 HOWTO64 (MORELLO_R (TLSIE_ADD_LO12), /* type */
2418 0, /* rightshift */
2419 2, /* size (0 = byte, 1 = short, 2 = long) */
2420 12, /* bitsize */
2421 FALSE, /* pc_relative */
2422 0, /* bitpos */
2423 complain_overflow_dont, /* complain_on_overflow */
2424 bfd_elf_generic_reloc, /* special_function */
2425 MORELLO_R_STR (TLSIE_ADD_LO12), /* name */
2426 FALSE, /* partial_inplace */
2427 0xfff, /* src_mask */
2428 0xfff, /* dst_mask */
2429 FALSE), /* pcrel_offset */
2430
a6bb11b2
YZ
2431 HOWTO (AARCH64_R (COPY), /* type */
2432 0, /* rightshift */
2433 2, /* size (0 = byte, 1 = short, 2 = long) */
2434 64, /* bitsize */
2435 FALSE, /* pc_relative */
2436 0, /* bitpos */
2437 complain_overflow_bitfield, /* complain_on_overflow */
2438 bfd_elf_generic_reloc, /* special_function */
2439 AARCH64_R_STR (COPY), /* name */
2440 TRUE, /* partial_inplace */
2441 0xffffffff, /* src_mask */
2442 0xffffffff, /* dst_mask */
2443 FALSE), /* pcrel_offset */
2444
2445 HOWTO (AARCH64_R (GLOB_DAT), /* type */
2446 0, /* rightshift */
2447 2, /* size (0 = byte, 1 = short, 2 = long) */
2448 64, /* bitsize */
2449 FALSE, /* pc_relative */
2450 0, /* bitpos */
2451 complain_overflow_bitfield, /* complain_on_overflow */
2452 bfd_elf_generic_reloc, /* special_function */
2453 AARCH64_R_STR (GLOB_DAT), /* name */
2454 TRUE, /* partial_inplace */
2455 0xffffffff, /* src_mask */
2456 0xffffffff, /* dst_mask */
2457 FALSE), /* pcrel_offset */
2458
2459 HOWTO (AARCH64_R (JUMP_SLOT), /* type */
2460 0, /* rightshift */
2461 2, /* size (0 = byte, 1 = short, 2 = long) */
2462 64, /* bitsize */
2463 FALSE, /* pc_relative */
2464 0, /* bitpos */
2465 complain_overflow_bitfield, /* complain_on_overflow */
2466 bfd_elf_generic_reloc, /* special_function */
2467 AARCH64_R_STR (JUMP_SLOT), /* name */
2468 TRUE, /* partial_inplace */
2469 0xffffffff, /* src_mask */
2470 0xffffffff, /* dst_mask */
2471 FALSE), /* pcrel_offset */
2472
2473 HOWTO (AARCH64_R (RELATIVE), /* type */
2474 0, /* rightshift */
2475 2, /* size (0 = byte, 1 = short, 2 = long) */
2476 64, /* bitsize */
2477 FALSE, /* pc_relative */
2478 0, /* bitpos */
2479 complain_overflow_bitfield, /* complain_on_overflow */
2480 bfd_elf_generic_reloc, /* special_function */
2481 AARCH64_R_STR (RELATIVE), /* name */
2482 TRUE, /* partial_inplace */
2483 ALL_ONES, /* src_mask */
2484 ALL_ONES, /* dst_mask */
2485 FALSE), /* pcrel_offset */
2486
2487 HOWTO (AARCH64_R (TLS_DTPMOD), /* type */
2488 0, /* rightshift */
2489 2, /* size (0 = byte, 1 = short, 2 = long) */
2490 64, /* bitsize */
2491 FALSE, /* pc_relative */
2492 0, /* bitpos */
2493 complain_overflow_dont, /* complain_on_overflow */
2494 bfd_elf_generic_reloc, /* special_function */
da0781dc
YZ
2495#if ARCH_SIZE == 64
2496 AARCH64_R_STR (TLS_DTPMOD64), /* name */
2497#else
a6bb11b2 2498 AARCH64_R_STR (TLS_DTPMOD), /* name */
da0781dc 2499#endif
a6bb11b2
YZ
2500 FALSE, /* partial_inplace */
2501 0, /* src_mask */
2502 ALL_ONES, /* dst_mask */
2503 FALSE), /* pc_reloffset */
2504
2505 HOWTO (AARCH64_R (TLS_DTPREL), /* type */
2506 0, /* rightshift */
2507 2, /* size (0 = byte, 1 = short, 2 = long) */
2508 64, /* bitsize */
2509 FALSE, /* pc_relative */
2510 0, /* bitpos */
2511 complain_overflow_dont, /* complain_on_overflow */
2512 bfd_elf_generic_reloc, /* special_function */
da0781dc
YZ
2513#if ARCH_SIZE == 64
2514 AARCH64_R_STR (TLS_DTPREL64), /* name */
2515#else
a6bb11b2 2516 AARCH64_R_STR (TLS_DTPREL), /* name */
da0781dc 2517#endif
a6bb11b2
YZ
2518 FALSE, /* partial_inplace */
2519 0, /* src_mask */
2520 ALL_ONES, /* dst_mask */
2521 FALSE), /* pcrel_offset */
2522
2523 HOWTO (AARCH64_R (TLS_TPREL), /* type */
2524 0, /* rightshift */
2525 2, /* size (0 = byte, 1 = short, 2 = long) */
2526 64, /* bitsize */
2527 FALSE, /* pc_relative */
2528 0, /* bitpos */
2529 complain_overflow_dont, /* complain_on_overflow */
2530 bfd_elf_generic_reloc, /* special_function */
da0781dc
YZ
2531#if ARCH_SIZE == 64
2532 AARCH64_R_STR (TLS_TPREL64), /* name */
2533#else
a6bb11b2 2534 AARCH64_R_STR (TLS_TPREL), /* name */
da0781dc 2535#endif
a6bb11b2
YZ
2536 FALSE, /* partial_inplace */
2537 0, /* src_mask */
2538 ALL_ONES, /* dst_mask */
2539 FALSE), /* pcrel_offset */
2540
2541 HOWTO (AARCH64_R (TLSDESC), /* type */
2542 0, /* rightshift */
2543 2, /* size (0 = byte, 1 = short, 2 = long) */
2544 64, /* bitsize */
2545 FALSE, /* pc_relative */
2546 0, /* bitpos */
2547 complain_overflow_dont, /* complain_on_overflow */
2548 bfd_elf_generic_reloc, /* special_function */
2549 AARCH64_R_STR (TLSDESC), /* name */
2550 FALSE, /* partial_inplace */
2551 0, /* src_mask */
2552 ALL_ONES, /* dst_mask */
2553 FALSE), /* pcrel_offset */
2554
2555 HOWTO (AARCH64_R (IRELATIVE), /* type */
2556 0, /* rightshift */
2557 2, /* size (0 = byte, 1 = short, 2 = long) */
2558 64, /* bitsize */
2559 FALSE, /* pc_relative */
2560 0, /* bitpos */
2561 complain_overflow_bitfield, /* complain_on_overflow */
2562 bfd_elf_generic_reloc, /* special_function */
2563 AARCH64_R_STR (IRELATIVE), /* name */
2564 FALSE, /* partial_inplace */
2565 0, /* src_mask */
2566 ALL_ONES, /* dst_mask */
2567 FALSE), /* pcrel_offset */
2568
40bbb79e
SP
2569 HOWTO64 (MORELLO_R (CAPINIT), /* type */
2570 0, /* rightshift */
2571 4, /* size (0 = byte, 1 = short, 2 = long) */
2572 0, /* bitsize */
2573 FALSE, /* pc_relative */
2574 0, /* bitpos */
2575 complain_overflow_dont, /* complain_on_overflow */
2576 bfd_elf_generic_reloc, /* special_function */
2577 MORELLO_R_STR (CAPINIT), /* name */
2578 FALSE, /* partial_inplace */
2579 ALL_ONES, /* src_mask */
2580 ALL_ONES, /* dst_mask */
2581 FALSE), /* pcrel_offset */
2582
a1bdea65
SP
2583 HOWTO64 (MORELLO_R (GLOB_DAT),/* type */
2584 0, /* rightshift */
2585 2, /* size (0 = byte, 1 = short, 2 = long) */
2586 64, /* bitsize */
2587 FALSE, /* pc_relative */
2588 0, /* bitpos */
2589 complain_overflow_bitfield, /* complain_on_overflow */
2590 bfd_elf_generic_reloc, /* special_function */
2591 MORELLO_R_STR (GLOB_DAT), /* name */
2592 TRUE, /* partial_inplace */
2593 0xffffffff, /* src_mask */
2594 0xffffffff, /* dst_mask */
2595 FALSE), /* pcrel_offset */
2596
e19e9199
SP
2597 HOWTO64 (MORELLO_R (JUMP_SLOT), /* type */
2598 0, /* rightshift */
2599 2, /* size (0 = byte, 1 = short, 2 = long) */
2600 64, /* bitsize */
2601 FALSE, /* pc_relative */
2602 0, /* bitpos */
2603 complain_overflow_bitfield, /* complain_on_overflow */
2604 bfd_elf_generic_reloc, /* special_function */
2605 MORELLO_R_STR (JUMP_SLOT), /* name */
2606 TRUE, /* partial_inplace */
2607 0xffffffff, /* src_mask */
2608 0xffffffff, /* dst_mask */
2609 FALSE), /* pcrel_offset */
2610
2611 HOWTO64 (MORELLO_R (RELATIVE), /* type */
40bbb79e
SP
2612 0, /* rightshift */
2613 2, /* size (0 = byte, 1 = short, 2 = long) */
2614 64, /* bitsize */
2615 FALSE, /* pc_relative */
2616 0, /* bitpos */
2617 complain_overflow_bitfield, /* complain_on_overflow */
2618 bfd_elf_generic_reloc, /* special_function */
2619 MORELLO_R_STR (RELATIVE), /* name */
2620 TRUE, /* partial_inplace */
2621 ALL_ONES, /* src_mask */
2622 ALL_ONES, /* dst_mask */
2623 FALSE), /* pcrel_offset */
2624
e19e9199
SP
2625 HOWTO64 (MORELLO_R (IRELATIVE), /* type */
2626 0, /* rightshift */
2627 2, /* size (0 = byte, 1 = short, 2 = long) */
2628 64, /* bitsize */
2629 FALSE, /* pc_relative */
2630 0, /* bitpos */
2631 complain_overflow_bitfield, /* complain_on_overflow */
2632 bfd_elf_generic_reloc, /* special_function */
2633 MORELLO_R_STR (IRELATIVE), /* name */
2634 FALSE, /* partial_inplace */
2635 0, /* src_mask */
2636 ALL_ONES, /* dst_mask */
2637 FALSE), /* pcrel_offset */
2638
4ca9b406
SP
2639 HOWTO64 (MORELLO_R (TLSDESC), /* type */
2640 0, /* rightshift */
2641 2, /* size (0 = byte, 1 = short, 2 = long) */
2642 64, /* bitsize */
2643 FALSE, /* pc_relative */
2644 0, /* bitpos */
2645 complain_overflow_dont, /* complain_on_overflow */
2646 bfd_elf_generic_reloc, /* special_function */
2647 MORELLO_R_STR (TLSDESC), /* name */
2648 FALSE, /* partial_inplace */
2649 0, /* src_mask */
2650 ALL_ONES, /* dst_mask */
2651 FALSE), /* pcrel_offset */
2652
1408e112
MM
2653 HOWTO64 (MORELLO_R (TPREL128), /* type */
2654 0, /* rightshift */
2655 2, /* size (0 = byte, 1 = short, 2 = long) */
2656 64, /* bitsize */
2657 FALSE, /* pc_relative */
2658 0, /* bitpos */
2659 complain_overflow_dont, /* complain_on_overflow */
2660 bfd_elf_generic_reloc, /* special_function */
2661 MORELLO_R_STR (TPREL128), /* name */
2662 FALSE, /* partial_inplace */
2663 0, /* src_mask */
2664 ALL_ONES, /* dst_mask */
2665 FALSE), /* pcrel_offset */
2666
a6bb11b2 2667 EMPTY_HOWTO (0),
a06ea964
NC
2668};
2669
a6bb11b2
YZ
2670static reloc_howto_type elfNN_aarch64_howto_none =
2671 HOWTO (R_AARCH64_NONE, /* type */
2672 0, /* rightshift */
6346d5ca 2673 3, /* size (0 = byte, 1 = short, 2 = long) */
a6bb11b2
YZ
2674 0, /* bitsize */
2675 FALSE, /* pc_relative */
2676 0, /* bitpos */
2677 complain_overflow_dont,/* complain_on_overflow */
2678 bfd_elf_generic_reloc, /* special_function */
2679 "R_AARCH64_NONE", /* name */
2680 FALSE, /* partial_inplace */
2681 0, /* src_mask */
2682 0, /* dst_mask */
2683 FALSE); /* pcrel_offset */
2684
2685/* Given HOWTO, return the bfd internal relocation enumerator. */
2686
2687static bfd_reloc_code_real_type
2688elfNN_aarch64_bfd_reloc_from_howto (reloc_howto_type *howto)
2689{
2690 const int size
2691 = (int) ARRAY_SIZE (elfNN_aarch64_howto_table);
2692 const ptrdiff_t offset
2693 = howto - elfNN_aarch64_howto_table;
2694
2695 if (offset > 0 && offset < size - 1)
2696 return BFD_RELOC_AARCH64_RELOC_START + offset;
2697
2698 if (howto == &elfNN_aarch64_howto_none)
2699 return BFD_RELOC_AARCH64_NONE;
2700
2701 return BFD_RELOC_AARCH64_RELOC_START;
2702}
2703
2704/* Given R_TYPE, return the bfd internal relocation enumerator. */
2705
2706static bfd_reloc_code_real_type
0aa13fee 2707elfNN_aarch64_bfd_reloc_from_type (bfd *abfd, unsigned int r_type)
a6bb11b2
YZ
2708{
2709 static bfd_boolean initialized_p = FALSE;
2710 /* Indexed by R_TYPE, values are offsets in the howto_table. */
2711 static unsigned int offsets[R_AARCH64_end];
2712
535b785f 2713 if (!initialized_p)
a6bb11b2
YZ
2714 {
2715 unsigned int i;
2716
2717 for (i = 1; i < ARRAY_SIZE (elfNN_aarch64_howto_table) - 1; ++i)
2718 if (elfNN_aarch64_howto_table[i].type != 0)
2719 offsets[elfNN_aarch64_howto_table[i].type] = i;
2720
2721 initialized_p = TRUE;
2722 }
2723
2724 if (r_type == R_AARCH64_NONE || r_type == R_AARCH64_NULL)
2725 return BFD_RELOC_AARCH64_NONE;
2726
5860e3f8
NC
2727 /* PR 17512: file: b371e70a. */
2728 if (r_type >= R_AARCH64_end)
2729 {
0aa13fee
AM
2730 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2731 abfd, r_type);
5860e3f8
NC
2732 bfd_set_error (bfd_error_bad_value);
2733 return BFD_RELOC_AARCH64_NONE;
2734 }
2735
a6bb11b2
YZ
2736 return BFD_RELOC_AARCH64_RELOC_START + offsets[r_type];
2737}
2738
2739struct elf_aarch64_reloc_map
2740{
2741 bfd_reloc_code_real_type from;
2742 bfd_reloc_code_real_type to;
2743};
2744
2745/* Map bfd generic reloc to AArch64-specific reloc. */
2746static const struct elf_aarch64_reloc_map elf_aarch64_reloc_map[] =
2747{
2748 {BFD_RELOC_NONE, BFD_RELOC_AARCH64_NONE},
2749
2750 /* Basic data relocations. */
2751 {BFD_RELOC_CTOR, BFD_RELOC_AARCH64_NN},
2752 {BFD_RELOC_64, BFD_RELOC_AARCH64_64},
2753 {BFD_RELOC_32, BFD_RELOC_AARCH64_32},
2754 {BFD_RELOC_16, BFD_RELOC_AARCH64_16},
2755 {BFD_RELOC_64_PCREL, BFD_RELOC_AARCH64_64_PCREL},
2756 {BFD_RELOC_32_PCREL, BFD_RELOC_AARCH64_32_PCREL},
2757 {BFD_RELOC_16_PCREL, BFD_RELOC_AARCH64_16_PCREL},
2758};
2759
2760/* Given the bfd internal relocation enumerator in CODE, return the
2761 corresponding howto entry. */
2762
2763static reloc_howto_type *
2764elfNN_aarch64_howto_from_bfd_reloc (bfd_reloc_code_real_type code)
2765{
2766 unsigned int i;
2767
2768 /* Convert bfd generic reloc to AArch64-specific reloc. */
2769 if (code < BFD_RELOC_AARCH64_RELOC_START
2770 || code > BFD_RELOC_AARCH64_RELOC_END)
2771 for (i = 0; i < ARRAY_SIZE (elf_aarch64_reloc_map); i++)
2772 if (elf_aarch64_reloc_map[i].from == code)
2773 {
2774 code = elf_aarch64_reloc_map[i].to;
2775 break;
2776 }
2777
2778 if (code > BFD_RELOC_AARCH64_RELOC_START
2779 && code < BFD_RELOC_AARCH64_RELOC_END)
2780 if (elfNN_aarch64_howto_table[code - BFD_RELOC_AARCH64_RELOC_START].type)
2781 return &elfNN_aarch64_howto_table[code - BFD_RELOC_AARCH64_RELOC_START];
2782
54757ed1
AP
2783 if (code == BFD_RELOC_AARCH64_NONE)
2784 return &elfNN_aarch64_howto_none;
2785
a6bb11b2
YZ
2786 return NULL;
2787}
2788
a06ea964 2789static reloc_howto_type *
0aa13fee 2790elfNN_aarch64_howto_from_type (bfd *abfd, unsigned int r_type)
a06ea964 2791{
a6bb11b2
YZ
2792 bfd_reloc_code_real_type val;
2793 reloc_howto_type *howto;
2794
cec5225b
YZ
2795#if ARCH_SIZE == 32
2796 if (r_type > 256)
2797 {
2798 bfd_set_error (bfd_error_bad_value);
2799 return NULL;
2800 }
2801#endif
2802
a6bb11b2
YZ
2803 if (r_type == R_AARCH64_NONE)
2804 return &elfNN_aarch64_howto_none;
a06ea964 2805
0aa13fee 2806 val = elfNN_aarch64_bfd_reloc_from_type (abfd, r_type);
a6bb11b2 2807 howto = elfNN_aarch64_howto_from_bfd_reloc (val);
a06ea964 2808
a6bb11b2
YZ
2809 if (howto != NULL)
2810 return howto;
a06ea964 2811
a06ea964
NC
2812 bfd_set_error (bfd_error_bad_value);
2813 return NULL;
2814}
2815
f3185997 2816static bfd_boolean
0aa13fee 2817elfNN_aarch64_info_to_howto (bfd *abfd, arelent *bfd_reloc,
a06ea964
NC
2818 Elf_Internal_Rela *elf_reloc)
2819{
2820 unsigned int r_type;
2821
cec5225b 2822 r_type = ELFNN_R_TYPE (elf_reloc->r_info);
0aa13fee 2823 bfd_reloc->howto = elfNN_aarch64_howto_from_type (abfd, r_type);
f3185997
NC
2824
2825 if (bfd_reloc->howto == NULL)
2826 {
2827 /* xgettext:c-format */
2828 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, r_type);
2829 return FALSE;
2830 }
2831 return TRUE;
a06ea964
NC
2832}
2833
a06ea964 2834static reloc_howto_type *
cec5225b 2835elfNN_aarch64_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
a06ea964
NC
2836 bfd_reloc_code_real_type code)
2837{
a6bb11b2 2838 reloc_howto_type *howto = elfNN_aarch64_howto_from_bfd_reloc (code);
a06ea964 2839
a6bb11b2
YZ
2840 if (howto != NULL)
2841 return howto;
a06ea964
NC
2842
2843 bfd_set_error (bfd_error_bad_value);
2844 return NULL;
2845}
2846
2847static reloc_howto_type *
cec5225b 2848elfNN_aarch64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
a06ea964
NC
2849 const char *r_name)
2850{
2851 unsigned int i;
2852
a6bb11b2
YZ
2853 for (i = 1; i < ARRAY_SIZE (elfNN_aarch64_howto_table) - 1; ++i)
2854 if (elfNN_aarch64_howto_table[i].name != NULL
2855 && strcasecmp (elfNN_aarch64_howto_table[i].name, r_name) == 0)
2856 return &elfNN_aarch64_howto_table[i];
a06ea964
NC
2857
2858 return NULL;
2859}
2860
07d6d2b8
AM
2861#define TARGET_LITTLE_SYM aarch64_elfNN_le_vec
2862#define TARGET_LITTLE_NAME "elfNN-littleaarch64"
2863#define TARGET_BIG_SYM aarch64_elfNN_be_vec
2864#define TARGET_BIG_NAME "elfNN-bigaarch64"
a06ea964 2865
a06ea964
NC
2866/* The linker script knows the section names for placement.
2867 The entry_names are used to do simple name mangling on the stubs.
2868 Given a function name, and its type, the stub can be found. The
2869 name can be changed. The only requirement is the %s be present. */
50e192f0 2870#define STUB_ENTRY_NAME "__%s%s_veneer"
a06ea964
NC
2871
2872/* The name of the dynamic interpreter. This is put in the .interp
2873 section. */
2874#define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
2875
2876#define AARCH64_MAX_FWD_BRANCH_OFFSET \
2877 (((1 << 25) - 1) << 2)
2878#define AARCH64_MAX_BWD_BRANCH_OFFSET \
2879 (-((1 << 25) << 2))
2880
2881#define AARCH64_MAX_ADRP_IMM ((1 << 20) - 1)
2882#define AARCH64_MIN_ADRP_IMM (-(1 << 20))
2883
50e192f0
SP
2884#define C64_MAX_ADRP_IMM ((1 << 19) - 1)
2885#define C64_MIN_ADRP_IMM (-(1 << 19))
2886
2887static bfd_boolean
2888aarch64_branch_reloc_p (unsigned int r_type)
2889{
2890 switch (r_type)
2891 {
2892 case MORELLO_R (JUMP26):
2893 case MORELLO_R (CALL26):
2894 case AARCH64_R (JUMP26):
2895 case AARCH64_R (CALL26):
2896 return TRUE;
2897
2898 default: break;
2899 }
2900
2901 return FALSE;
2902}
2903
a06ea964
NC
2904static int
2905aarch64_valid_for_adrp_p (bfd_vma value, bfd_vma place)
2906{
2907 bfd_signed_vma offset = (bfd_signed_vma) (PG (value) - PG (place)) >> 12;
2908 return offset <= AARCH64_MAX_ADRP_IMM && offset >= AARCH64_MIN_ADRP_IMM;
2909}
2910
50e192f0
SP
2911static bfd_boolean
2912c64_valid_for_adrp_p (bfd_vma value, bfd_vma place)
2913{
2914 bfd_signed_vma offset = (bfd_signed_vma) (PG (value) - PG (place)) >> 12;
2915 return offset <= C64_MAX_ADRP_IMM && offset >= C64_MIN_ADRP_IMM;
2916}
2917
a06ea964
NC
2918static int
2919aarch64_valid_branch_p (bfd_vma value, bfd_vma place)
2920{
2921 bfd_signed_vma offset = (bfd_signed_vma) (value - place);
2922 return (offset <= AARCH64_MAX_FWD_BRANCH_OFFSET
2923 && offset >= AARCH64_MAX_BWD_BRANCH_OFFSET);
2924}
2925
2926static const uint32_t aarch64_adrp_branch_stub [] =
2927{
2928 0x90000010, /* adrp ip0, X */
2929 /* R_AARCH64_ADR_HI21_PCREL(X) */
2930 0x91000210, /* add ip0, ip0, :lo12:X */
2931 /* R_AARCH64_ADD_ABS_LO12_NC(X) */
2932 0xd61f0200, /* br ip0 */
2933};
2934
2935static const uint32_t aarch64_long_branch_stub[] =
2936{
cec5225b 2937#if ARCH_SIZE == 64
a06ea964 2938 0x58000090, /* ldr ip0, 1f */
cec5225b
YZ
2939#else
2940 0x18000090, /* ldr wip0, 1f */
2941#endif
a06ea964
NC
2942 0x10000011, /* adr ip1, #0 */
2943 0x8b110210, /* add ip0, ip0, ip1 */
2944 0xd61f0200, /* br ip0 */
cec5225b
YZ
2945 0x00000000, /* 1: .xword or .word
2946 R_AARCH64_PRELNN(X) + 12
a06ea964
NC
2947 */
2948 0x00000000,
2949};
2950
68fcca92
JW
2951static const uint32_t aarch64_erratum_835769_stub[] =
2952{
2953 0x00000000, /* Placeholder for multiply accumulate. */
2954 0x14000000, /* b <label> */
2955};
2956
4106101c
MS
2957static const uint32_t aarch64_erratum_843419_stub[] =
2958{
2959 0x00000000, /* Placeholder for LDR instruction. */
2960 0x14000000, /* b <label> */
2961};
2962
50e192f0
SP
2963static const uint32_t aarch64_c64_branch_stub [] =
2964{
2965 0xc2c273e0, /* bx #4 */
2966 0x90800010, /* adrp c16, X */
2967 /* R_MORELLO_ADR_HI20_PCREL(X) */
2968 0x02000210, /* add c16, c16, :lo12:X */
2969 /* R_AARCH64_ADD_ABS_LO12_NC(X) */
2970 0xc2c21200, /* br c16 */
2971};
2972
2973static const uint32_t c64_aarch64_branch_stub [] =
2974{
2975 0x90800010, /* adrp c16, X */
2976 /* R_MORELLO_ADR_HI20_PCREL(X) */
2977 0x02000210, /* add c16, c16, :lo12:X */
2978 /* R_AARCH64_ADD_ABS_LO12_NC(X) */
2979 0xc2c21200, /* br c16 */
2980};
2981
a06ea964
NC
2982/* Section name for stubs is the associated section name plus this
2983 string. */
2984#define STUB_SUFFIX ".stub"
2985
cec5225b 2986enum elf_aarch64_stub_type
a06ea964
NC
2987{
2988 aarch64_stub_none,
2989 aarch64_stub_adrp_branch,
2990 aarch64_stub_long_branch,
68fcca92 2991 aarch64_stub_erratum_835769_veneer,
4106101c 2992 aarch64_stub_erratum_843419_veneer,
50e192f0
SP
2993 aarch64_stub_branch_c64,
2994 c64_stub_branch_aarch64,
2995 c64_stub_branch_c64,
a06ea964
NC
2996};
2997
cec5225b 2998struct elf_aarch64_stub_hash_entry
a06ea964
NC
2999{
3000 /* Base hash table entry structure. */
3001 struct bfd_hash_entry root;
3002
3003 /* The stub section. */
3004 asection *stub_sec;
3005
3006 /* Offset within stub_sec of the beginning of this stub. */
3007 bfd_vma stub_offset;
3008
3009 /* Given the symbol's value and its section we can determine its final
3010 value when building the stubs (so the stub knows where to jump). */
3011 bfd_vma target_value;
3012 asection *target_section;
3013
cec5225b 3014 enum elf_aarch64_stub_type stub_type;
a06ea964
NC
3015
3016 /* The symbol table entry, if any, that this was derived from. */
cec5225b 3017 struct elf_aarch64_link_hash_entry *h;
a06ea964
NC
3018
3019 /* Destination symbol type */
3020 unsigned char st_type;
3021
3022 /* Where this stub is being called from, or, in the case of combined
3023 stub sections, the first input section in the group. */
3024 asection *id_sec;
3025
3026 /* The name for the local symbol at the start of this stub. The
3027 stub name in the hash table has to be unique; this does not, so
3028 it can be friendlier. */
3029 char *output_name;
68fcca92
JW
3030
3031 /* The instruction which caused this stub to be generated (only valid for
3032 erratum 835769 workaround stubs at present). */
3033 uint32_t veneered_insn;
4106101c
MS
3034
3035 /* In an erratum 843419 workaround stub, the ADRP instruction offset. */
3036 bfd_vma adrp_offset;
a06ea964
NC
3037};
3038
ece841e8
MM
3039struct elf_c64_tls_data_stub_hash_entry
3040{
3041 /* Symbol table entry, if any, for which this stub is made. */
3042 struct elf_aarch64_link_hash_entry *h;
3043 /* Local symbol table index and BFD associated with it, if required, for
3044 which this stub is made. These are only used when `h` is NULL. */
3045 unsigned long r_symndx;
3046 bfd *input_bfd;
3047 /* Offset within htab->sc64_tls_stubs of the beginning of this stub. */
3048 bfd_vma tls_stub_offset;
3049 bfd_boolean populated;
3050};
3051
a06ea964
NC
3052/* Used to build a map of a section. This is required for mixed-endian
3053 code/data. */
3054
cec5225b 3055typedef struct elf_elf_section_map
a06ea964
NC
3056{
3057 bfd_vma vma;
3058 char type;
3059}
cec5225b 3060elf_aarch64_section_map;
a06ea964
NC
3061
3062
3063typedef struct _aarch64_elf_section_data
3064{
3065 struct bfd_elf_section_data elf;
3066 unsigned int mapcount;
3067 unsigned int mapsize;
cec5225b 3068 elf_aarch64_section_map *map;
f0070c1e 3069 bfd_boolean sorted;
a06ea964
NC
3070}
3071_aarch64_elf_section_data;
3072
cec5225b 3073#define elf_aarch64_section_data(sec) \
a06ea964
NC
3074 ((_aarch64_elf_section_data *) elf_section_data (sec))
3075
f0070c1e
SP
3076/* Used to order a list of mapping symbols by address. */
3077
3078static int
3079elf_aarch64_compare_mapping (const void *a, const void *b)
3080{
3081 const elf_aarch64_section_map *amap = (const elf_aarch64_section_map *) a;
3082 const elf_aarch64_section_map *bmap = (const elf_aarch64_section_map *) b;
3083
3084 if (amap->vma > bmap->vma)
3085 return 1;
3086 else if (amap->vma < bmap->vma)
3087 return -1;
3088 else if (amap->type > bmap->type)
3089 /* Ensure results do not depend on the host qsort for objects with
3090 multiple mapping symbols at the same address by sorting on type
3091 after vma. */
3092 return 1;
3093 else if (amap->type < bmap->type)
3094 return -1;
3095 else
3096 return 0;
3097}
3098
3099static _aarch64_elf_section_data *
3100elf_aarch64_section_data_get (asection *sec)
3101{
3102 _aarch64_elf_section_data *sec_data = elf_aarch64_section_data(sec);
3103
3104 /* A section that does not have aarch64 section data, so it does not have any
3105 map information. Assume A64. */
3106 if (sec_data == NULL || !sec_data->elf.is_target_section_data)
3107 return NULL;
3108
3109 if (sec_data->sorted)
3110 goto done;
3111
3112 qsort (sec_data->map, sec_data->mapcount, sizeof (elf_aarch64_section_map),
3113 elf_aarch64_compare_mapping);
3114
3115 sec_data->sorted = TRUE;
3116
3117done:
3118 return sec_data;
3119}
3120
3121/* Returns TRUE if the label with st_value as VALUE is within a C64 code
3122 section or not. */
3123
3124static bfd_boolean
3125c64_value_p (asection *section, unsigned int value)
3126{
3127 struct _aarch64_elf_section_data *sec_data =
3128 elf_aarch64_section_data_get (section);
3129
3130 if (sec_data == NULL)
3131 return FALSE;
3132
3133 unsigned int span;
3134
3135 for (span = 0; span < sec_data->mapcount; span++)
3136 {
3137 unsigned int span_start = sec_data->map[span].vma;
3138 unsigned int span_end = ((span == sec_data->mapcount - 1)
3139 ? sec_data->map[0].vma + section->size
3140 : sec_data->map[span + 1].vma);
3141 char span_type = sec_data->map[span].type;
3142
3143 if (span_start <= value && value < span_end && span_type == 'c')
3144 return TRUE;
3145 }
3146 return FALSE;
3147}
3148
4e8516b2 3149/* The size of the thread control block which is defined to be two pointers. */
bf3eaebd
MM
3150#define TCB_SIZE(cur_bfd) \
3151 elf_elfheader(cur_bfd)->e_flags & EF_AARCH64_CHERI_PURECAP ? 32 : (ARCH_SIZE/8)*2
a06ea964
NC
3152
3153struct elf_aarch64_local_symbol
3154{
3155 unsigned int got_type;
3156 bfd_signed_vma got_refcount;
3157 bfd_vma got_offset;
3158
3159 /* Offset of the GOTPLT entry reserved for the TLS descriptor. The
3160 offset is from the end of the jump table and reserved entries
3161 within the PLTGOT.
3162
3163 The magic value (bfd_vma) -1 indicates that an offset has not be
3164 allocated. */
3165 bfd_vma tlsdesc_got_jump_table_offset;
3166};
3167
3168struct elf_aarch64_obj_tdata
3169{
3170 struct elf_obj_tdata root;
3171
3172 /* local symbol descriptors */
3173 struct elf_aarch64_local_symbol *locals;
3174
3175 /* Zero to warn when linking objects with incompatible enum sizes. */
3176 int no_enum_size_warning;
3177
3178 /* Zero to warn when linking objects with incompatible wchar_t sizes. */
3179 int no_wchar_size_warning;
cd702818
SD
3180
3181 /* All GNU_PROPERTY_AARCH64_FEATURE_1_AND properties. */
3182 uint32_t gnu_and_prop;
37c18eed
SD
3183
3184 /* Zero to warn when linking objects with incompatible
3185 GNU_PROPERTY_AARCH64_FEATURE_1_BTI. */
3186 int no_bti_warn;
3187
3188 /* PLT type based on security. */
3189 aarch64_plt_type plt_type;
f0070c1e
SP
3190
3191 /* Flag to check if section maps have been initialised for all sections in
3192 this object. */
3193 bfd_boolean secmaps_initialised;
a06ea964
NC
3194};
3195
3196#define elf_aarch64_tdata(bfd) \
3197 ((struct elf_aarch64_obj_tdata *) (bfd)->tdata.any)
3198
cec5225b 3199#define elf_aarch64_locals(bfd) (elf_aarch64_tdata (bfd)->locals)
a06ea964
NC
3200
3201#define is_aarch64_elf(bfd) \
3202 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
3203 && elf_tdata (bfd) != NULL \
3204 && elf_object_id (bfd) == AARCH64_ELF_DATA)
3205
3206static bfd_boolean
cec5225b 3207elfNN_aarch64_mkobject (bfd *abfd)
a06ea964
NC
3208{
3209 return bfd_elf_allocate_object (abfd, sizeof (struct elf_aarch64_obj_tdata),
3210 AARCH64_ELF_DATA);
3211}
3212
cec5225b
YZ
3213#define elf_aarch64_hash_entry(ent) \
3214 ((struct elf_aarch64_link_hash_entry *)(ent))
a06ea964
NC
3215
3216#define GOT_UNKNOWN 0
3217#define GOT_NORMAL 1
3218#define GOT_TLS_GD 2
3219#define GOT_TLS_IE 4
3220#define GOT_TLSDESC_GD 8
3221
3222#define GOT_TLS_GD_ANY_P(type) ((type & GOT_TLS_GD) || (type & GOT_TLSDESC_GD))
3223
3224/* AArch64 ELF linker hash entry. */
cec5225b 3225struct elf_aarch64_link_hash_entry
a06ea964
NC
3226{
3227 struct elf_link_hash_entry root;
3228
a06ea964
NC
3229 /* Since PLT entries have variable size, we need to record the
3230 index into .got.plt instead of recomputing it from the PLT
3231 offset. */
3232 bfd_signed_vma plt_got_offset;
3233
3234 /* Bit mask representing the type of GOT entry(s) if any required by
3235 this symbol. */
3236 unsigned int got_type;
3237
3238 /* A pointer to the most recently used stub hash entry against this
3239 symbol. */
cec5225b 3240 struct elf_aarch64_stub_hash_entry *stub_cache;
a06ea964
NC
3241
3242 /* Offset of the GOTPLT entry reserved for the TLS descriptor. The offset
3243 is from the end of the jump table and reserved entries within the PLTGOT.
3244
3245 The magic value (bfd_vma) -1 indicates that an offset has not
3246 be allocated. */
3247 bfd_vma tlsdesc_got_jump_table_offset;
3248};
3249
3250static unsigned int
cec5225b 3251elfNN_aarch64_symbol_got_type (struct elf_link_hash_entry *h,
a06ea964
NC
3252 bfd *abfd,
3253 unsigned long r_symndx)
3254{
3255 if (h)
cec5225b 3256 return elf_aarch64_hash_entry (h)->got_type;
a06ea964 3257
cec5225b 3258 if (! elf_aarch64_locals (abfd))
a06ea964
NC
3259 return GOT_UNKNOWN;
3260
cec5225b 3261 return elf_aarch64_locals (abfd)[r_symndx].got_type;
a06ea964
NC
3262}
3263
a06ea964 3264/* Get the AArch64 elf linker hash table from a link_info structure. */
cec5225b
YZ
3265#define elf_aarch64_hash_table(info) \
3266 ((struct elf_aarch64_link_hash_table *) ((info)->hash))
a06ea964
NC
3267
3268#define aarch64_stub_hash_lookup(table, string, create, copy) \
cec5225b 3269 ((struct elf_aarch64_stub_hash_entry *) \
a06ea964
NC
3270 bfd_hash_lookup ((table), (string), (create), (copy)))
3271
3272/* AArch64 ELF linker hash table. */
cec5225b 3273struct elf_aarch64_link_hash_table
a06ea964
NC
3274{
3275 /* The main hash table. */
3276 struct elf_link_hash_table root;
3277
3278 /* Nonzero to force PIC branch veneers. */
3279 int pic_veneer;
3280
68fcca92
JW
3281 /* Fix erratum 835769. */
3282 int fix_erratum_835769;
3283
4106101c 3284 /* Fix erratum 843419. */
739b5c9c 3285 erratum_84319_opts fix_erratum_843419;
4106101c 3286
1f56df9d
JW
3287 /* Don't apply link-time values for dynamic relocations. */
3288 int no_apply_dynamic_relocs;
3289
a06ea964
NC
3290 /* The number of bytes in the initial entry in the PLT. */
3291 bfd_size_type plt_header_size;
3292
37c18eed
SD
3293 /* The bytes of the initial PLT entry. */
3294 const bfd_byte *plt0_entry;
3295
3296 /* The number of bytes in the subsequent PLT entries. */
a06ea964
NC
3297 bfd_size_type plt_entry_size;
3298
37c18eed
SD
3299 /* The bytes of the subsequent PLT entry. */
3300 const bfd_byte *plt_entry;
3301
a06ea964
NC
3302 /* For convenience in allocate_dynrelocs. */
3303 bfd *obfd;
3304
3305 /* The amount of space used by the reserved portion of the sgotplt
3306 section, plus whatever space is used by the jump slots. */
3307 bfd_vma sgotplt_jump_table_size;
3308
3309 /* The stub hash table. */
3310 struct bfd_hash_table stub_hash_table;
3311
3312 /* Linker stub bfd. */
3313 bfd *stub_bfd;
3314
3315 /* Linker call-backs. */
3316 asection *(*add_stub_section) (const char *, asection *);
3317 void (*layout_sections_again) (void);
8504495a
AC
3318 c64_section_padding_setter_t c64_set_section_padding;
3319 c64_section_padding_getter_t c64_get_section_padding;
3320 c64_pad_after_section_t c64_pad_after_section;
a06ea964
NC
3321
3322 /* Array to keep track of which stub sections have been created, and
3323 information on stub grouping. */
3324 struct map_stub
3325 {
3326 /* This is the section to which stubs in the group will be
3327 attached. */
3328 asection *link_sec;
3329 /* The stub section. */
3330 asection *stub_sec;
3331 } *stub_group;
3332
cec5225b 3333 /* Assorted information used by elfNN_aarch64_size_stubs. */
a06ea964 3334 unsigned int bfd_count;
7292b3ac 3335 unsigned int top_index;
a06ea964
NC
3336 asection **input_list;
3337
823710d5
SN
3338 /* JUMP_SLOT relocs for variant PCS symbols may be present. */
3339 int variant_pcs;
3340
37c18eed
SD
3341 /* The number of bytes in the PLT enty for the TLS descriptor. */
3342 bfd_size_type tlsdesc_plt_entry_size;
3343
1419bbe5
WN
3344 /* Used by local STT_GNU_IFUNC symbols. */
3345 htab_t loc_hash_table;
3346 void * loc_hash_memory;
40bbb79e
SP
3347
3348 /* Used for capability relocations. */
3349 asection *srelcaps;
ece841e8 3350 asection *sc64_tls_stubs;
a1bdea65 3351 int c64_rel;
7ff36d1a 3352 bfd_boolean c64_output;
ece841e8
MM
3353 htab_t c64_tls_data_stub_hash_table;
3354 void * tls_data_stub_memory;
8504495a
AC
3355
3356 /* Bookkeeping for padding we add to sections in C64. */
3357 void **c64_sec_info;
3358 unsigned min_output_section_id;
3359 unsigned max_output_section_id;
a06ea964
NC
3360};
3361
a06ea964
NC
3362/* Create an entry in an AArch64 ELF linker hash table. */
3363
3364static struct bfd_hash_entry *
cec5225b 3365elfNN_aarch64_link_hash_newfunc (struct bfd_hash_entry *entry,
a06ea964
NC
3366 struct bfd_hash_table *table,
3367 const char *string)
3368{
cec5225b
YZ
3369 struct elf_aarch64_link_hash_entry *ret =
3370 (struct elf_aarch64_link_hash_entry *) entry;
a06ea964
NC
3371
3372 /* Allocate the structure if it has not already been allocated by a
3373 subclass. */
3374 if (ret == NULL)
3375 ret = bfd_hash_allocate (table,
cec5225b 3376 sizeof (struct elf_aarch64_link_hash_entry));
a06ea964
NC
3377 if (ret == NULL)
3378 return (struct bfd_hash_entry *) ret;
3379
3380 /* Call the allocation method of the superclass. */
cec5225b 3381 ret = ((struct elf_aarch64_link_hash_entry *)
a06ea964
NC
3382 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3383 table, string));
3384 if (ret != NULL)
3385 {
a06ea964
NC
3386 ret->got_type = GOT_UNKNOWN;
3387 ret->plt_got_offset = (bfd_vma) - 1;
3388 ret->stub_cache = NULL;
3389 ret->tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
3390 }
3391
3392 return (struct bfd_hash_entry *) ret;
3393}
3394
3395/* Initialize an entry in the stub hash table. */
3396
3397static struct bfd_hash_entry *
3398stub_hash_newfunc (struct bfd_hash_entry *entry,
3399 struct bfd_hash_table *table, const char *string)
3400{
3401 /* Allocate the structure if it has not already been allocated by a
3402 subclass. */
3403 if (entry == NULL)
3404 {
3405 entry = bfd_hash_allocate (table,
3406 sizeof (struct
cec5225b 3407 elf_aarch64_stub_hash_entry));
a06ea964
NC
3408 if (entry == NULL)
3409 return entry;
3410 }
3411
3412 /* Call the allocation method of the superclass. */
3413 entry = bfd_hash_newfunc (entry, table, string);
3414 if (entry != NULL)
3415 {
cec5225b 3416 struct elf_aarch64_stub_hash_entry *eh;
a06ea964
NC
3417
3418 /* Initialize the local fields. */
cec5225b 3419 eh = (struct elf_aarch64_stub_hash_entry *) entry;
4106101c 3420 eh->adrp_offset = 0;
a06ea964
NC
3421 eh->stub_sec = NULL;
3422 eh->stub_offset = 0;
3423 eh->target_value = 0;
3424 eh->target_section = NULL;
3425 eh->stub_type = aarch64_stub_none;
3426 eh->h = NULL;
3427 eh->id_sec = NULL;
3428 }
3429
3430 return entry;
3431}
3432
ece841e8
MM
3433static hashval_t
3434c64_tls_data_stub_hash (const void *ptr)
3435{
3436 struct elf_c64_tls_data_stub_hash_entry *entry
3437 = (struct elf_c64_tls_data_stub_hash_entry *) ptr;
3438 if (entry->h)
3439 return htab_hash_pointer (entry->h);
3440 struct elf_aarch64_local_symbol *l;
3441 l = elf_aarch64_locals (entry->input_bfd);
3442 return htab_hash_pointer (l + entry->r_symndx);
3443}
3444
3445static int
3446c64_tls_data_stub_eq (const void *ptr1, const void *ptr2)
3447{
3448 struct elf_c64_tls_data_stub_hash_entry *entry1
3449 = (struct elf_c64_tls_data_stub_hash_entry *) ptr1;
3450 struct elf_c64_tls_data_stub_hash_entry *entry2
3451 = (struct elf_c64_tls_data_stub_hash_entry *) ptr2;
3452
3453 if (entry1->h && !entry2->h)
3454 return 0;
3455 if (!entry1->h && entry2->h)
3456 return 0;
3457 if (entry1->h && entry2->h)
3458 return entry1->h == entry2->h;
3459
3460 if (entry1->input_bfd != entry2->input_bfd)
3461 return 0;
3462 return entry1->r_symndx == entry2->r_symndx;
3463}
3464
3465static bfd_boolean
3466c64_record_tls_stub (struct elf_aarch64_link_hash_table *htab,
3467 bfd *input_bfd,
3468 struct elf_link_hash_entry *h,
3469 unsigned long r_symndx)
3470{
3471 if (htab->root.dynobj == NULL)
3472 htab->root.dynobj = input_bfd;
3473
3474 if (!htab->sc64_tls_stubs)
3475 {
3476 asection *stub_sec;
3477 flagword flags;
3478
3479 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3480 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_KEEP
3481 | SEC_LINKER_CREATED);
3482 /* Using the .rodata section is ABI -- N.b. I do have an outstanding
3483 clarification on this on the ABI PR.
3484 https://github.com/ARM-software/abi-aa/pull/80 */
3485 stub_sec = bfd_make_section_anyway_with_flags
3486 (htab->root.dynobj, ".rodata", flags);
3487 if (stub_sec == NULL)
3488 return FALSE;
3489
3490 /* Section contains stubs of 64 bit values, hence requires 8 byte
3491 alignment. */
3492 bfd_set_section_alignment (stub_sec, 3);
3493 htab->sc64_tls_stubs = stub_sec;
3494 BFD_ASSERT (htab->sc64_tls_stubs->size == 0);
3495 }
3496
3497 if (h)
3498 BFD_ASSERT (h->type == STT_TLS);
3499
3500 void **slot;
3501 struct elf_c64_tls_data_stub_hash_entry e, *new_entry;
3502 e.h = (struct elf_aarch64_link_hash_entry *)h;
3503 e.r_symndx = r_symndx;
3504 e.input_bfd = input_bfd;
3505
3506 slot = htab_find_slot (htab->c64_tls_data_stub_hash_table, &e, INSERT);
3507 if (!slot)
3508 return FALSE;
3509
3510 if (*slot)
3511 return TRUE;
3512 new_entry = (struct elf_c64_tls_data_stub_hash_entry *)
3513 objalloc_alloc ((struct objalloc *) htab->tls_data_stub_memory,
3514 sizeof (struct elf_c64_tls_data_stub_hash_entry));
3515 if (new_entry)
3516 {
3517 new_entry->h = (struct elf_aarch64_link_hash_entry *)h;
3518 new_entry->r_symndx = r_symndx;
3519 new_entry->input_bfd = input_bfd;
3520 new_entry->tls_stub_offset = htab->sc64_tls_stubs->size;
3521 new_entry->populated = FALSE;
3522 /* Size of a Morello data stub is 2 * 8 byte integers. */
3523 htab->sc64_tls_stubs->size += 16;
3524 *slot = new_entry;
3525 return TRUE;
3526 }
3527 return FALSE;
3528}
3529
3530static struct elf_c64_tls_data_stub_hash_entry *
3531c64_tls_stub_find (struct elf_link_hash_entry *h,
3532 bfd *input_bfd,
3533 unsigned long r_symndx,
3534 struct elf_aarch64_link_hash_table *htab)
3535{
3536 void *ret;
3537 struct elf_c64_tls_data_stub_hash_entry e;
3538 e.h = (struct elf_aarch64_link_hash_entry *)h;
3539 e.r_symndx = r_symndx;
3540 e.input_bfd = input_bfd;
3541
3542 ret = htab_find (htab->c64_tls_data_stub_hash_table, &e);
3543 return (struct elf_c64_tls_data_stub_hash_entry *)ret;
3544}
3545
1419bbe5
WN
3546/* Compute a hash of a local hash entry. We use elf_link_hash_entry
3547 for local symbol so that we can handle local STT_GNU_IFUNC symbols
3548 as global symbol. We reuse indx and dynstr_index for local symbol
3549 hash since they aren't used by global symbols in this backend. */
3550
3551static hashval_t
3552elfNN_aarch64_local_htab_hash (const void *ptr)
3553{
3554 struct elf_link_hash_entry *h
3555 = (struct elf_link_hash_entry *) ptr;
3556 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
3557}
3558
3559/* Compare local hash entries. */
3560
3561static int
3562elfNN_aarch64_local_htab_eq (const void *ptr1, const void *ptr2)
3563{
3564 struct elf_link_hash_entry *h1
3565 = (struct elf_link_hash_entry *) ptr1;
3566 struct elf_link_hash_entry *h2
3567 = (struct elf_link_hash_entry *) ptr2;
3568
3569 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
3570}
3571
3572/* Find and/or create a hash entry for local symbol. */
3573
3574static struct elf_link_hash_entry *
3575elfNN_aarch64_get_local_sym_hash (struct elf_aarch64_link_hash_table *htab,
3576 bfd *abfd, const Elf_Internal_Rela *rel,
3577 bfd_boolean create)
3578{
3579 struct elf_aarch64_link_hash_entry e, *ret;
3580 asection *sec = abfd->sections;
3581 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
3582 ELFNN_R_SYM (rel->r_info));
3583 void **slot;
3584
3585 e.root.indx = sec->id;
3586 e.root.dynstr_index = ELFNN_R_SYM (rel->r_info);
3587 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
3588 create ? INSERT : NO_INSERT);
3589
3590 if (!slot)
3591 return NULL;
3592
3593 if (*slot)
3594 {
3595 ret = (struct elf_aarch64_link_hash_entry *) *slot;
3596 return &ret->root;
3597 }
3598
3599 ret = (struct elf_aarch64_link_hash_entry *)
3600 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
3601 sizeof (struct elf_aarch64_link_hash_entry));
3602 if (ret)
3603 {
3604 memset (ret, 0, sizeof (*ret));
3605 ret->root.indx = sec->id;
3606 ret->root.dynstr_index = ELFNN_R_SYM (rel->r_info);
3607 ret->root.dynindx = -1;
3608 *slot = ret;
3609 }
3610 return &ret->root;
3611}
a06ea964
NC
3612
3613/* Copy the extra info we tack onto an elf_link_hash_entry. */
3614
3615static void
cec5225b 3616elfNN_aarch64_copy_indirect_symbol (struct bfd_link_info *info,
a06ea964
NC
3617 struct elf_link_hash_entry *dir,
3618 struct elf_link_hash_entry *ind)
3619{
cec5225b 3620 struct elf_aarch64_link_hash_entry *edir, *eind;
a06ea964 3621
cec5225b
YZ
3622 edir = (struct elf_aarch64_link_hash_entry *) dir;
3623 eind = (struct elf_aarch64_link_hash_entry *) ind;
a06ea964 3624
a06ea964
NC
3625 if (ind->root.type == bfd_link_hash_indirect)
3626 {
3627 /* Copy over PLT info. */
3628 if (dir->got.refcount <= 0)
3629 {
3630 edir->got_type = eind->got_type;
3631 eind->got_type = GOT_UNKNOWN;
3632 }
3633 }
3634
ece841e8
MM
3635 /* This function is called to take the indirect symbol information from eind
3636 and put it onto edir. We never create a TLS data stub for a symbol which
3637 needs such a transformation, hence there is no need to worry about
3638 removing the hash entry for the indirected symbol and ensuring there is
3639 now one for the final one. */
3640 BFD_ASSERT (!c64_tls_stub_find (ind, NULL, 0,
3641 elf_aarch64_hash_table (info)));
a06ea964
NC
3642 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3643}
3644
823710d5
SN
3645/* Merge non-visibility st_other attributes. */
3646
3647static void
3648elfNN_aarch64_merge_symbol_attribute (struct elf_link_hash_entry *h,
3649 const Elf_Internal_Sym *isym,
3650 bfd_boolean definition ATTRIBUTE_UNUSED,
3651 bfd_boolean dynamic ATTRIBUTE_UNUSED)
3652{
3653 unsigned int isym_sto = isym->st_other & ~ELF_ST_VISIBILITY (-1);
3654 unsigned int h_sto = h->other & ~ELF_ST_VISIBILITY (-1);
3655
3656 if (isym_sto == h_sto)
3657 return;
3658
3659 if (isym_sto & ~STO_AARCH64_VARIANT_PCS)
3660 /* Not fatal, this callback cannot fail. */
3661 _bfd_error_handler (_("unknown attribute for symbol `%s': 0x%02x"),
3662 h->root.root.string, isym_sto);
3663
3664 /* Note: Ideally we would warn about any attribute mismatch, but
3665 this api does not allow that without substantial changes. */
3666 if (isym_sto & STO_AARCH64_VARIANT_PCS)
3667 h->other |= STO_AARCH64_VARIANT_PCS;
3668}
3669
68faa637
AM
3670/* Destroy an AArch64 elf linker hash table. */
3671
3672static void
d495ab0d 3673elfNN_aarch64_link_hash_table_free (bfd *obfd)
68faa637
AM
3674{
3675 struct elf_aarch64_link_hash_table *ret
d495ab0d 3676 = (struct elf_aarch64_link_hash_table *) obfd->link.hash;
68faa637
AM
3677
3678 if (ret->loc_hash_table)
3679 htab_delete (ret->loc_hash_table);
3680 if (ret->loc_hash_memory)
3681 objalloc_free ((struct objalloc *) ret->loc_hash_memory);
3682
ece841e8
MM
3683 if (ret->c64_tls_data_stub_hash_table)
3684 htab_delete (ret->c64_tls_data_stub_hash_table);
3685 if (ret->tls_data_stub_memory)
3686 objalloc_free ((struct objalloc *) ret->tls_data_stub_memory);
3687
68faa637 3688 bfd_hash_table_free (&ret->stub_hash_table);
d495ab0d 3689 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
3690}
3691
a06ea964
NC
3692/* Create an AArch64 elf linker hash table. */
3693
3694static struct bfd_link_hash_table *
cec5225b 3695elfNN_aarch64_link_hash_table_create (bfd *abfd)
a06ea964 3696{
cec5225b 3697 struct elf_aarch64_link_hash_table *ret;
986f0783 3698 size_t amt = sizeof (struct elf_aarch64_link_hash_table);
a06ea964 3699
7bf52ea2 3700 ret = bfd_zmalloc (amt);
a06ea964
NC
3701 if (ret == NULL)
3702 return NULL;
3703
3704 if (!_bfd_elf_link_hash_table_init
cec5225b
YZ
3705 (&ret->root, abfd, elfNN_aarch64_link_hash_newfunc,
3706 sizeof (struct elf_aarch64_link_hash_entry), AARCH64_ELF_DATA))
a06ea964
NC
3707 {
3708 free (ret);
3709 return NULL;
3710 }
3711
a06ea964 3712 ret->plt_header_size = PLT_ENTRY_SIZE;
37c18eed 3713 ret->plt0_entry = elfNN_aarch64_small_plt0_entry;
a06ea964 3714 ret->plt_entry_size = PLT_SMALL_ENTRY_SIZE;
37c18eed
SD
3715 ret->plt_entry = elfNN_aarch64_small_plt_entry;
3716 ret->tlsdesc_plt_entry_size = PLT_TLSDESC_ENTRY_SIZE;
a06ea964 3717 ret->obfd = abfd;
9bcc30e4 3718 ret->root.tlsdesc_got = (bfd_vma) - 1;
a06ea964
NC
3719
3720 if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
cec5225b 3721 sizeof (struct elf_aarch64_stub_hash_entry)))
a06ea964 3722 {
d495ab0d 3723 _bfd_elf_link_hash_table_free (abfd);
a06ea964
NC
3724 return NULL;
3725 }
3726
ece841e8
MM
3727 ret->c64_tls_data_stub_hash_table
3728 = htab_try_create (256, c64_tls_data_stub_hash, c64_tls_data_stub_eq,
3729 NULL);
3730 ret->tls_data_stub_memory = objalloc_create ();
3731 if (!ret->c64_tls_data_stub_hash_table || !ret->tls_data_stub_memory)
3732 {
3733 elfNN_aarch64_link_hash_table_free (abfd);
3734 return NULL;
3735 }
3736
1419bbe5
WN
3737 ret->loc_hash_table = htab_try_create (1024,
3738 elfNN_aarch64_local_htab_hash,
3739 elfNN_aarch64_local_htab_eq,
3740 NULL);
3741 ret->loc_hash_memory = objalloc_create ();
3742 if (!ret->loc_hash_table || !ret->loc_hash_memory)
3743 {
d495ab0d 3744 elfNN_aarch64_link_hash_table_free (abfd);
1419bbe5
WN
3745 return NULL;
3746 }
d495ab0d 3747 ret->root.root.hash_table_free = elfNN_aarch64_link_hash_table_free;
1419bbe5 3748
a06ea964
NC
3749 return &ret->root.root;
3750}
3751
1d75a8e2
NC
3752/* Perform relocation R_TYPE. Returns TRUE upon success, FALSE otherwise. */
3753
a06ea964
NC
3754static bfd_boolean
3755aarch64_relocate (unsigned int r_type, bfd *input_bfd, asection *input_section,
3756 bfd_vma offset, bfd_vma value)
3757{
3758 reloc_howto_type *howto;
3759 bfd_vma place;
3760
0aa13fee 3761 howto = elfNN_aarch64_howto_from_type (input_bfd, r_type);
a06ea964
NC
3762 place = (input_section->output_section->vma + input_section->output_offset
3763 + offset);
caed7120 3764
0aa13fee 3765 r_type = elfNN_aarch64_bfd_reloc_from_type (input_bfd, r_type);
652afeef
TC
3766 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, r_type, place,
3767 value, 0, FALSE);
caed7120
YZ
3768 return _bfd_aarch64_elf_put_addend (input_bfd,
3769 input_section->contents + offset, r_type,
1d75a8e2 3770 howto, value) == bfd_reloc_ok;
a06ea964
NC
3771}
3772
50e192f0
SP
3773/* Return interworking stub for a relocation. */
3774
3775static enum elf_aarch64_stub_type
3776aarch64_interwork_stub (unsigned int r_type,
3777 bfd_boolean branch_to_c64)
3778{
3779 switch (r_type)
3780 {
3781 case MORELLO_R (JUMP26):
3782 case MORELLO_R (CALL26):
3783 if (!branch_to_c64)
3784 return c64_stub_branch_aarch64;
3785 break;
3786 case AARCH64_R (JUMP26):
3787 case AARCH64_R (CALL26):
3788 if (branch_to_c64)
3789 return aarch64_stub_branch_c64;
3790 break;
3791 default:
3792 break;
3793 }
3794
3795 return aarch64_stub_none;
3796}
3797
cec5225b 3798static enum elf_aarch64_stub_type
a06ea964
NC
3799aarch64_select_branch_stub (bfd_vma value, bfd_vma place)
3800{
3801 if (aarch64_valid_for_adrp_p (value, place))
3802 return aarch64_stub_adrp_branch;
3803 return aarch64_stub_long_branch;
3804}
3805
3806/* Determine the type of stub needed, if any, for a call. */
3807
cec5225b 3808static enum elf_aarch64_stub_type
9a228467 3809aarch64_type_of_stub (asection *input_sec,
a06ea964 3810 const Elf_Internal_Rela *rel,
f678ded7 3811 asection *sym_sec,
a06ea964 3812 unsigned char st_type,
a06ea964
NC
3813 bfd_vma destination)
3814{
3815 bfd_vma location;
3816 bfd_signed_vma branch_offset;
50e192f0 3817 unsigned int r_type = ELFNN_R_TYPE (rel->r_info);
cec5225b 3818 enum elf_aarch64_stub_type stub_type = aarch64_stub_none;
a06ea964 3819
f678ded7 3820 if (st_type != STT_FUNC
2f340668 3821 && (sym_sec == input_sec))
a06ea964
NC
3822 return stub_type;
3823
a06ea964
NC
3824 /* Determine where the call point is. */
3825 location = (input_sec->output_offset
3826 + input_sec->output_section->vma + rel->r_offset);
3827
3828 branch_offset = (bfd_signed_vma) (destination - location);
3829
50e192f0
SP
3830 /* For A64 <-> C64 branches we only come here for jumps to PLT. Treat them
3831 as regular branches and leave the interworking to PLT. */
3832 if (branch_offset > AARCH64_MAX_FWD_BRANCH_OFFSET
3833 || branch_offset < AARCH64_MAX_BWD_BRANCH_OFFSET)
a06ea964 3834 {
50e192f0
SP
3835 switch (r_type)
3836 {
3837 /* We don't want to redirect any old unconditional jump in this way,
3838 only one which is being used for a sibcall, where it is
3839 acceptable for the IP0 and IP1 registers to be clobbered. */
3840 case AARCH64_R (CALL26):
3841 case AARCH64_R (JUMP26):
3842 return aarch64_stub_long_branch;
3843 case MORELLO_R (CALL26):
3844 case MORELLO_R (JUMP26):
3845 return c64_stub_branch_c64;
3846 default:
3847 break;
3848 }
a06ea964
NC
3849 }
3850
50e192f0
SP
3851 return aarch64_stub_none;
3852}
3853
3854/* Return a string to add as suffix to a veneer name. */
3855
3856static const char *
3857aarch64_lookup_stub_type_suffix (enum elf_aarch64_stub_type stub_type)
3858{
3859 switch (stub_type)
3860 {
3861 case aarch64_stub_branch_c64:
3862 return "_a64c64";
3863 case c64_stub_branch_aarch64:
3864 return "_c64a64";
3865 break;
3866 default:
3867 return "";
3868 }
a06ea964
NC
3869}
3870
3871/* Build a name for an entry in the stub hash table. */
3872
3873static char *
cec5225b 3874elfNN_aarch64_stub_name (const asection *input_section,
a06ea964 3875 const asection *sym_sec,
cec5225b 3876 const struct elf_aarch64_link_hash_entry *hash,
50e192f0
SP
3877 const Elf_Internal_Rela *rel,
3878 enum elf_aarch64_stub_type stub_type)
a06ea964
NC
3879{
3880 char *stub_name;
3881 bfd_size_type len;
50e192f0 3882 const char *suffix = aarch64_lookup_stub_type_suffix (stub_type);;
a06ea964
NC
3883
3884 if (hash)
3885 {
3886 len = 8 + 1 + strlen (hash->root.root.root.string) + 1 + 16 + 1;
3887 stub_name = bfd_malloc (len);
3888 if (stub_name != NULL)
50e192f0 3889 snprintf (stub_name, len, "%08x_%s%s+%" BFD_VMA_FMT "x",
a06ea964
NC
3890 (unsigned int) input_section->id,
3891 hash->root.root.root.string,
50e192f0 3892 suffix, rel->r_addend);
a06ea964
NC
3893 }
3894 else
3895 {
3896 len = 8 + 1 + 8 + 1 + 8 + 1 + 16 + 1;
3897 stub_name = bfd_malloc (len);
3898 if (stub_name != NULL)
50e192f0 3899 snprintf (stub_name, len, "%08x_%x:%x%s+%" BFD_VMA_FMT "x",
a06ea964
NC
3900 (unsigned int) input_section->id,
3901 (unsigned int) sym_sec->id,
cec5225b 3902 (unsigned int) ELFNN_R_SYM (rel->r_info),
50e192f0 3903 suffix, rel->r_addend);
a06ea964
NC
3904 }
3905
3906 return stub_name;
3907}
3908
7f784814
JW
3909/* Return TRUE if symbol H should be hashed in the `.gnu.hash' section. For
3910 executable PLT slots where the executable never takes the address of those
3911 functions, the function symbols are not added to the hash table. */
3912
3913static bfd_boolean
3914elf_aarch64_hash_symbol (struct elf_link_hash_entry *h)
3915{
3916 if (h->plt.offset != (bfd_vma) -1
3917 && !h->def_regular
3918 && !h->pointer_equality_needed)
3919 return FALSE;
3920
3921 return _bfd_elf_hash_symbol (h);
3922}
3923
a06ea964
NC
3924/* Look up an entry in the stub hash. Stub entries are cached because
3925 creating the stub name takes a bit of time. */
3926
cec5225b
YZ
3927static struct elf_aarch64_stub_hash_entry *
3928elfNN_aarch64_get_stub_entry (const asection *input_section,
a06ea964
NC
3929 const asection *sym_sec,
3930 struct elf_link_hash_entry *hash,
3931 const Elf_Internal_Rela *rel,
50e192f0
SP
3932 struct elf_aarch64_link_hash_table *htab,
3933 enum elf_aarch64_stub_type stub_type)
a06ea964 3934{
cec5225b
YZ
3935 struct elf_aarch64_stub_hash_entry *stub_entry;
3936 struct elf_aarch64_link_hash_entry *h =
3937 (struct elf_aarch64_link_hash_entry *) hash;
a06ea964
NC
3938 const asection *id_sec;
3939
3940 if ((input_section->flags & SEC_CODE) == 0)
3941 return NULL;
3942
3943 /* If this input section is part of a group of sections sharing one
3944 stub section, then use the id of the first section in the group.
3945 Stub names need to include a section id, as there may well be
3946 more than one stub used to reach say, printf, and we need to
3947 distinguish between them. */
3948 id_sec = htab->stub_group[input_section->id].link_sec;
3949
3950 if (h != NULL && h->stub_cache != NULL
3951 && h->stub_cache->h == h && h->stub_cache->id_sec == id_sec)
3952 {
3953 stub_entry = h->stub_cache;
3954 }
3955 else
3956 {
3957 char *stub_name;
3958
50e192f0 3959 stub_name = elfNN_aarch64_stub_name (id_sec, sym_sec, h, rel, stub_type);
a06ea964
NC
3960 if (stub_name == NULL)
3961 return NULL;
3962
3963 stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table,
3964 stub_name, FALSE, FALSE);
3965 if (h != NULL)
3966 h->stub_cache = stub_entry;
3967
3968 free (stub_name);
3969 }
3970
3971 return stub_entry;
3972}
3973
a06ea964 3974
66585675
MS
3975/* Create a stub section. */
3976
3977static asection *
3978_bfd_aarch64_create_stub_section (asection *section,
3979 struct elf_aarch64_link_hash_table *htab)
3980{
3981 size_t namelen;
3982 bfd_size_type len;
3983 char *s_name;
3984
3985 namelen = strlen (section->name);
3986 len = namelen + sizeof (STUB_SUFFIX);
3987 s_name = bfd_alloc (htab->stub_bfd, len);
3988 if (s_name == NULL)
3989 return NULL;
3990
3991 memcpy (s_name, section->name, namelen);
3992 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3993 return (*htab->add_stub_section) (s_name, section);
3994}
3995
3996
fc6d53be
MS
3997/* Find or create a stub section for a link section.
3998
3999 Fix or create the stub section used to collect stubs attached to
4000 the specified link section. */
4001
4002static asection *
4003_bfd_aarch64_get_stub_for_link_section (asection *link_section,
4004 struct elf_aarch64_link_hash_table *htab)
4005{
4006 if (htab->stub_group[link_section->id].stub_sec == NULL)
4007 htab->stub_group[link_section->id].stub_sec
4008 = _bfd_aarch64_create_stub_section (link_section, htab);
4009 return htab->stub_group[link_section->id].stub_sec;
4010}
4011
4012
ef857521
MS
4013/* Find or create a stub section in the stub group for an input
4014 section. */
4015
4016static asection *
4017_bfd_aarch64_create_or_find_stub_sec (asection *section,
4018 struct elf_aarch64_link_hash_table *htab)
a06ea964 4019{
fc6d53be
MS
4020 asection *link_sec = htab->stub_group[section->id].link_sec;
4021 return _bfd_aarch64_get_stub_for_link_section (link_sec, htab);
ef857521
MS
4022}
4023
4024
4025/* Add a new stub entry in the stub group associated with an input
4026 section to the stub hash. Not all fields of the new stub entry are
4027 initialised. */
4028
4029static struct elf_aarch64_stub_hash_entry *
4030_bfd_aarch64_add_stub_entry_in_group (const char *stub_name,
4031 asection *section,
4032 struct elf_aarch64_link_hash_table *htab)
4033{
4034 asection *link_sec;
4035 asection *stub_sec;
4036 struct elf_aarch64_stub_hash_entry *stub_entry;
4037
4038 link_sec = htab->stub_group[section->id].link_sec;
4039 stub_sec = _bfd_aarch64_create_or_find_stub_sec (section, htab);
4040
a06ea964
NC
4041 /* Enter this entry into the linker stub hash table. */
4042 stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4043 TRUE, FALSE);
4044 if (stub_entry == NULL)
4045 {
695344c0 4046 /* xgettext:c-format */
871b3ab2 4047 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4eca0228 4048 section->owner, stub_name);
a06ea964
NC
4049 return NULL;
4050 }
4051
4052 stub_entry->stub_sec = stub_sec;
4053 stub_entry->stub_offset = 0;
4054 stub_entry->id_sec = link_sec;
4055
4056 return stub_entry;
4057}
4058
4106101c
MS
4059/* Add a new stub entry in the final stub section to the stub hash.
4060 Not all fields of the new stub entry are initialised. */
4061
4062static struct elf_aarch64_stub_hash_entry *
4063_bfd_aarch64_add_stub_entry_after (const char *stub_name,
4064 asection *link_section,
4065 struct elf_aarch64_link_hash_table *htab)
4066{
4067 asection *stub_sec;
4068 struct elf_aarch64_stub_hash_entry *stub_entry;
4069
739b5c9c
TC
4070 stub_sec = NULL;
4071 /* Only create the actual stub if we will end up needing it. */
4072 if (htab->fix_erratum_843419 & ERRAT_ADRP)
4073 stub_sec = _bfd_aarch64_get_stub_for_link_section (link_section, htab);
4106101c
MS
4074 stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4075 TRUE, FALSE);
4076 if (stub_entry == NULL)
4077 {
4eca0228 4078 _bfd_error_handler (_("cannot create stub entry %s"), stub_name);
4106101c
MS
4079 return NULL;
4080 }
4081
4082 stub_entry->stub_sec = stub_sec;
4083 stub_entry->stub_offset = 0;
4084 stub_entry->id_sec = link_section;
4085
4086 return stub_entry;
4087}
4088
4089
a06ea964
NC
4090static bfd_boolean
4091aarch64_build_one_stub (struct bfd_hash_entry *gen_entry,
abf874aa 4092 void *in_arg)
a06ea964 4093{
cec5225b 4094 struct elf_aarch64_stub_hash_entry *stub_entry;
a06ea964
NC
4095 asection *stub_sec;
4096 bfd *stub_bfd;
4097 bfd_byte *loc;
4098 bfd_vma sym_value;
68fcca92
JW
4099 bfd_vma veneered_insn_loc;
4100 bfd_vma veneer_entry_loc;
4101 bfd_signed_vma branch_offset = 0;
a06ea964
NC
4102 unsigned int template_size;
4103 const uint32_t *template;
4104 unsigned int i;
abf874aa 4105 struct bfd_link_info *info;
a06ea964
NC
4106
4107 /* Massage our args to the form they really have. */
cec5225b 4108 stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
a06ea964 4109
abf874aa
CL
4110 info = (struct bfd_link_info *) in_arg;
4111
4112 /* Fail if the target section could not be assigned to an output
4113 section. The user should fix his linker script. */
4114 if (stub_entry->target_section->output_section == NULL
4115 && info->non_contiguous_regions)
53215f21
CL
4116 info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
4117 "Retry without "
4118 "--enable-non-contiguous-regions.\n"),
4119 stub_entry->target_section);
abf874aa 4120
a06ea964
NC
4121 stub_sec = stub_entry->stub_sec;
4122
4123 /* Make a note of the offset within the stubs for this entry. */
4124 stub_entry->stub_offset = stub_sec->size;
4125 loc = stub_sec->contents + stub_entry->stub_offset;
4126
4127 stub_bfd = stub_sec->owner;
4128
4129 /* This is the address of the stub destination. */
4130 sym_value = (stub_entry->target_value
4131 + stub_entry->target_section->output_offset
4132 + stub_entry->target_section->output_section->vma);
4133
50e192f0
SP
4134 bfd_vma place = (stub_entry->stub_offset + stub_sec->output_section->vma
4135 + stub_sec->output_offset);
4136
a06ea964
NC
4137 if (stub_entry->stub_type == aarch64_stub_long_branch)
4138 {
a06ea964
NC
4139 /* See if we can relax the stub. */
4140 if (aarch64_valid_for_adrp_p (sym_value, place))
4141 stub_entry->stub_type = aarch64_select_branch_stub (sym_value, place);
4142 }
4143
50e192f0
SP
4144 if ((stub_entry->stub_type == aarch64_stub_branch_c64
4145 || stub_entry->stub_type == c64_stub_branch_aarch64
4146 || stub_entry->stub_type == c64_stub_branch_c64)
4147 && !c64_valid_for_adrp_p (sym_value, place))
4148 {
4149 _bfd_error_handler
4150 (_("%s: stub target out of range for %s branch"),
4151 stub_entry->output_name,
4152 (stub_entry->stub_type == aarch64_stub_branch_c64
4153 ? "A64 to C64" : "C64 to A64"));
4154 bfd_set_error (bfd_error_bad_value);
4155 return FALSE;
4156 }
4157
a06ea964
NC
4158 switch (stub_entry->stub_type)
4159 {
4160 case aarch64_stub_adrp_branch:
4161 template = aarch64_adrp_branch_stub;
4162 template_size = sizeof (aarch64_adrp_branch_stub);
4163 break;
4164 case aarch64_stub_long_branch:
4165 template = aarch64_long_branch_stub;
4166 template_size = sizeof (aarch64_long_branch_stub);
4167 break;
68fcca92
JW
4168 case aarch64_stub_erratum_835769_veneer:
4169 template = aarch64_erratum_835769_stub;
4170 template_size = sizeof (aarch64_erratum_835769_stub);
4171 break;
4106101c
MS
4172 case aarch64_stub_erratum_843419_veneer:
4173 template = aarch64_erratum_843419_stub;
4174 template_size = sizeof (aarch64_erratum_843419_stub);
4175 break;
50e192f0
SP
4176 case aarch64_stub_branch_c64:
4177 template = aarch64_c64_branch_stub;
4178 template_size = sizeof (aarch64_c64_branch_stub);
4179 break;
4180 case c64_stub_branch_aarch64:
4181 case c64_stub_branch_c64:
4182 template = c64_aarch64_branch_stub;
4183 template_size = sizeof (c64_aarch64_branch_stub);
4184 break;
a06ea964 4185 default:
8e2fe09f 4186 abort ();
a06ea964
NC
4187 }
4188
4189 for (i = 0; i < (template_size / sizeof template[0]); i++)
4190 {
4191 bfd_putl32 (template[i], loc);
4192 loc += 4;
4193 }
4194
4195 template_size = (template_size + 7) & ~7;
4196 stub_sec->size += template_size;
4197
50e192f0
SP
4198 bfd_vma stub_offset = stub_entry->stub_offset;
4199
a06ea964
NC
4200 switch (stub_entry->stub_type)
4201 {
4202 case aarch64_stub_adrp_branch:
1d75a8e2
NC
4203 if (!aarch64_relocate (AARCH64_R (ADR_PREL_PG_HI21), stub_bfd, stub_sec,
4204 stub_entry->stub_offset, sym_value))
a06ea964
NC
4205 /* The stub would not have been relaxed if the offset was out
4206 of range. */
4207 BFD_FAIL ();
4208
1d75a8e2
NC
4209 if (!aarch64_relocate (AARCH64_R (ADD_ABS_LO12_NC), stub_bfd, stub_sec,
4210 stub_entry->stub_offset + 4, sym_value))
93ca8569 4211 BFD_FAIL ();
a06ea964
NC
4212 break;
4213
4214 case aarch64_stub_long_branch:
4215 /* We want the value relative to the address 12 bytes back from the
07d6d2b8 4216 value itself. */
1d75a8e2
NC
4217 if (!aarch64_relocate (AARCH64_R (PRELNN), stub_bfd, stub_sec,
4218 stub_entry->stub_offset + 16, sym_value + 12))
93ca8569 4219 BFD_FAIL ();
a06ea964 4220 break;
68fcca92
JW
4221
4222 case aarch64_stub_erratum_835769_veneer:
4223 veneered_insn_loc = stub_entry->target_section->output_section->vma
4224 + stub_entry->target_section->output_offset
4225 + stub_entry->target_value;
4226 veneer_entry_loc = stub_entry->stub_sec->output_section->vma
4227 + stub_entry->stub_sec->output_offset
4228 + stub_entry->stub_offset;
4229 branch_offset = veneered_insn_loc - veneer_entry_loc;
4230 branch_offset >>= 2;
4231 branch_offset &= 0x3ffffff;
4232 bfd_putl32 (stub_entry->veneered_insn,
4233 stub_sec->contents + stub_entry->stub_offset);
4234 bfd_putl32 (template[1] | branch_offset,
4235 stub_sec->contents + stub_entry->stub_offset + 4);
4236 break;
4237
4106101c 4238 case aarch64_stub_erratum_843419_veneer:
1d75a8e2
NC
4239 if (!aarch64_relocate (AARCH64_R (JUMP26), stub_bfd, stub_sec,
4240 stub_entry->stub_offset + 4, sym_value + 4))
4106101c
MS
4241 BFD_FAIL ();
4242 break;
4243
50e192f0
SP
4244 case aarch64_stub_branch_c64:
4245 stub_offset += 4;
4246 /* Fall through. */
4247 case c64_stub_branch_aarch64:
4248 case c64_stub_branch_c64:
4249 if (!aarch64_relocate (R_MORELLO_ADR_PREL_PG_HI20, stub_bfd, stub_sec,
4250 stub_offset, sym_value))
4251 /* We fail early if offset is out of range. */
4252 BFD_FAIL ();
4253
4254 if (!aarch64_relocate (AARCH64_R (ADD_ABS_LO12_NC), stub_bfd, stub_sec,
4255 stub_offset + 4, sym_value))
4256 BFD_FAIL ();
4257 break;
4258
a06ea964 4259 default:
8e2fe09f 4260 abort ();
a06ea964
NC
4261 }
4262
4263 return TRUE;
4264}
4265
4266/* As above, but don't actually build the stub. Just bump offset so
4267 we know stub section sizes. */
4268
4269static bfd_boolean
739b5c9c 4270aarch64_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
a06ea964 4271{
cec5225b 4272 struct elf_aarch64_stub_hash_entry *stub_entry;
739b5c9c 4273 struct elf_aarch64_link_hash_table *htab;
a06ea964
NC
4274 int size;
4275
4276 /* Massage our args to the form they really have. */
cec5225b 4277 stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
739b5c9c 4278 htab = (struct elf_aarch64_link_hash_table *) in_arg;
a06ea964
NC
4279
4280 switch (stub_entry->stub_type)
4281 {
4282 case aarch64_stub_adrp_branch:
4283 size = sizeof (aarch64_adrp_branch_stub);
4284 break;
4285 case aarch64_stub_long_branch:
4286 size = sizeof (aarch64_long_branch_stub);
4287 break;
68fcca92
JW
4288 case aarch64_stub_erratum_835769_veneer:
4289 size = sizeof (aarch64_erratum_835769_stub);
4290 break;
4106101c 4291 case aarch64_stub_erratum_843419_veneer:
739b5c9c
TC
4292 {
4293 if (htab->fix_erratum_843419 == ERRAT_ADR)
4294 return TRUE;
4295 size = sizeof (aarch64_erratum_843419_stub);
4296 }
4106101c 4297 break;
50e192f0
SP
4298 case aarch64_stub_branch_c64:
4299 size = sizeof (aarch64_c64_branch_stub);
4300 break;
4301 case c64_stub_branch_aarch64:
4302 case c64_stub_branch_c64:
4303 size = sizeof (c64_aarch64_branch_stub);
4304 break;
a06ea964 4305 default:
8e2fe09f 4306 abort ();
a06ea964
NC
4307 }
4308
4309 size = (size + 7) & ~7;
4310 stub_entry->stub_sec->size += size;
4311 return TRUE;
4312}
4313
4314/* External entry points for sizing and building linker stubs. */
4315
4316/* Set up various things so that we can make a list of input sections
4317 for each output section included in the link. Returns -1 on error,
4318 0 when no stubs will be needed, and 1 on success. */
4319
4320int
cec5225b 4321elfNN_aarch64_setup_section_lists (bfd *output_bfd,
a06ea964
NC
4322 struct bfd_link_info *info)
4323{
4324 bfd *input_bfd;
4325 unsigned int bfd_count;
7292b3ac 4326 unsigned int top_id, top_index;
a06ea964
NC
4327 asection *section;
4328 asection **input_list, **list;
986f0783 4329 size_t amt;
cec5225b
YZ
4330 struct elf_aarch64_link_hash_table *htab =
4331 elf_aarch64_hash_table (info);
a06ea964 4332
8504495a
AC
4333 /* XXX: in practice, we believe this branch should never be taken.
4334 This function is only ever called from gld*_after_allocation
4335 (defined in ld/emultempl/aarch64elf.em), which can only ever be
4336 called if the LD emulation is an AArch64 ELF emulation. The branch
4337 can only be taken if the BFD backend is not an ELF backend. In
4338 practice, this isn't supported, see PR29649 for details. */
a06ea964
NC
4339 if (!is_elf_hash_table (htab))
4340 return 0;
4341
4342 /* Count the number of input BFDs and find the top input section id. */
4343 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
c72f2fb2 4344 input_bfd != NULL; input_bfd = input_bfd->link.next)
a06ea964
NC
4345 {
4346 bfd_count += 1;
4347 for (section = input_bfd->sections;
4348 section != NULL; section = section->next)
4349 {
4350 if (top_id < section->id)
4351 top_id = section->id;
4352 }
4353 }
4354 htab->bfd_count = bfd_count;
4355
4356 amt = sizeof (struct map_stub) * (top_id + 1);
4357 htab->stub_group = bfd_zmalloc (amt);
4358 if (htab->stub_group == NULL)
4359 return -1;
4360
4361 /* We can't use output_bfd->section_count here to find the top output
4362 section index as some sections may have been removed, and
4363 _bfd_strip_section_from_output doesn't renumber the indices. */
4364 for (section = output_bfd->sections, top_index = 0;
4365 section != NULL; section = section->next)
4366 {
4367 if (top_index < section->index)
4368 top_index = section->index;
4369 }
4370
4371 htab->top_index = top_index;
4372 amt = sizeof (asection *) * (top_index + 1);
4373 input_list = bfd_malloc (amt);
4374 htab->input_list = input_list;
4375 if (input_list == NULL)
4376 return -1;
4377
4378 /* For sections we aren't interested in, mark their entries with a
4379 value we can check later. */
4380 list = input_list + top_index;
4381 do
4382 *list = bfd_abs_section_ptr;
4383 while (list-- != input_list);
4384
4385 for (section = output_bfd->sections;
4386 section != NULL; section = section->next)
4387 {
4388 if ((section->flags & SEC_CODE) != 0)
4389 input_list[section->index] = NULL;
4390 }
4391
4392 return 1;
4393}
4394
cec5225b 4395/* Used by elfNN_aarch64_next_input_section and group_sections. */
a06ea964
NC
4396#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
4397
4398/* The linker repeatedly calls this function for each input section,
4399 in the order that input sections are linked into output sections.
4400 Build lists of input sections to determine groupings between which
4401 we may insert linker stubs. */
4402
4403void
cec5225b 4404elfNN_aarch64_next_input_section (struct bfd_link_info *info, asection *isec)
a06ea964 4405{
cec5225b
YZ
4406 struct elf_aarch64_link_hash_table *htab =
4407 elf_aarch64_hash_table (info);
a06ea964
NC
4408
4409 if (isec->output_section->index <= htab->top_index)
4410 {
4411 asection **list = htab->input_list + isec->output_section->index;
4412
cff69cf4 4413 if (*list != bfd_abs_section_ptr && (isec->flags & SEC_CODE) != 0)
a06ea964
NC
4414 {
4415 /* Steal the link_sec pointer for our list. */
4416 /* This happens to make the list in reverse order,
4417 which is what we want. */
4418 PREV_SEC (isec) = *list;
4419 *list = isec;
4420 }
4421 }
4422}
4423
4424/* See whether we can group stub sections together. Grouping stub
4425 sections may result in fewer stubs. More importantly, we need to
4426 put all .init* and .fini* stubs at the beginning of the .init or
4427 .fini output sections respectively, because glibc splits the
4428 _init and _fini functions into multiple parts. Putting a stub in
4429 the middle of a function is not a good idea. */
4430
4431static void
cec5225b 4432group_sections (struct elf_aarch64_link_hash_table *htab,
a06ea964 4433 bfd_size_type stub_group_size,
cff69cf4 4434 bfd_boolean stubs_always_after_branch)
a06ea964 4435{
cff69cf4 4436 asection **list = htab->input_list;
a06ea964
NC
4437
4438 do
4439 {
4440 asection *tail = *list;
cff69cf4 4441 asection *head;
a06ea964
NC
4442
4443 if (tail == bfd_abs_section_ptr)
4444 continue;
4445
cff69cf4
WD
4446 /* Reverse the list: we must avoid placing stubs at the
4447 beginning of the section because the beginning of the text
4448 section may be required for an interrupt vector in bare metal
4449 code. */
4450#define NEXT_SEC PREV_SEC
4451 head = NULL;
a06ea964 4452 while (tail != NULL)
cff69cf4
WD
4453 {
4454 /* Pop from tail. */
4455 asection *item = tail;
4456 tail = PREV_SEC (item);
4457
4458 /* Push on head. */
4459 NEXT_SEC (item) = head;
4460 head = item;
4461 }
4462
4463 while (head != NULL)
a06ea964
NC
4464 {
4465 asection *curr;
cff69cf4
WD
4466 asection *next;
4467 bfd_vma stub_group_start = head->output_offset;
4468 bfd_vma end_of_next;
a06ea964 4469
cff69cf4
WD
4470 curr = head;
4471 while (NEXT_SEC (curr) != NULL)
4472 {
4473 next = NEXT_SEC (curr);
4474 end_of_next = next->output_offset + next->size;
4475 if (end_of_next - stub_group_start >= stub_group_size)
4476 /* End of NEXT is too far from start, so stop. */
4477 break;
4478 /* Add NEXT to the group. */
4479 curr = next;
4480 }
a06ea964 4481
cff69cf4 4482 /* OK, the size from the start to the start of CURR is less
a06ea964 4483 than stub_group_size and thus can be handled by one stub
cff69cf4 4484 section. (Or the head section is itself larger than
a06ea964
NC
4485 stub_group_size, in which case we may be toast.)
4486 We should really be keeping track of the total size of
4487 stubs added here, as stubs contribute to the final output
4488 section size. */
4489 do
4490 {
cff69cf4 4491 next = NEXT_SEC (head);
a06ea964 4492 /* Set up this stub group. */
cff69cf4 4493 htab->stub_group[head->id].link_sec = curr;
a06ea964 4494 }
cff69cf4 4495 while (head != curr && (head = next) != NULL);
a06ea964
NC
4496
4497 /* But wait, there's more! Input sections up to stub_group_size
cff69cf4
WD
4498 bytes after the stub section can be handled by it too. */
4499 if (!stubs_always_after_branch)
a06ea964 4500 {
cff69cf4
WD
4501 stub_group_start = curr->output_offset + curr->size;
4502
4503 while (next != NULL)
a06ea964 4504 {
cff69cf4
WD
4505 end_of_next = next->output_offset + next->size;
4506 if (end_of_next - stub_group_start >= stub_group_size)
4507 /* End of NEXT is too far from stubs, so stop. */
4508 break;
4509 /* Add NEXT to the stub group. */
4510 head = next;
4511 next = NEXT_SEC (head);
4512 htab->stub_group[head->id].link_sec = curr;
a06ea964
NC
4513 }
4514 }
cff69cf4 4515 head = next;
a06ea964
NC
4516 }
4517 }
cff69cf4 4518 while (list++ != htab->input_list + htab->top_index);
a06ea964
NC
4519
4520 free (htab->input_list);
4521}
4522
cff69cf4 4523#undef PREV_SEC
a06ea964
NC
4524#undef PREV_SEC
4525
68fcca92
JW
4526#define AARCH64_BITS(x, pos, n) (((x) >> (pos)) & ((1 << (n)) - 1))
4527
4528#define AARCH64_RT(insn) AARCH64_BITS (insn, 0, 5)
4529#define AARCH64_RT2(insn) AARCH64_BITS (insn, 10, 5)
4530#define AARCH64_RA(insn) AARCH64_BITS (insn, 10, 5)
4531#define AARCH64_RD(insn) AARCH64_BITS (insn, 0, 5)
4532#define AARCH64_RN(insn) AARCH64_BITS (insn, 5, 5)
4533#define AARCH64_RM(insn) AARCH64_BITS (insn, 16, 5)
4534
4535#define AARCH64_MAC(insn) (((insn) & 0xff000000) == 0x9b000000)
4536#define AARCH64_BIT(insn, n) AARCH64_BITS (insn, n, 1)
4537#define AARCH64_OP31(insn) AARCH64_BITS (insn, 21, 3)
4538#define AARCH64_ZR 0x1f
4539
4540/* All ld/st ops. See C4-182 of the ARM ARM. The encoding space for
4541 LD_PCREL, LDST_RO, LDST_UI and LDST_UIMM cover prefetch ops. */
4542
4543#define AARCH64_LD(insn) (AARCH64_BIT (insn, 22) == 1)
4544#define AARCH64_LDST(insn) (((insn) & 0x0a000000) == 0x08000000)
4545#define AARCH64_LDST_EX(insn) (((insn) & 0x3f000000) == 0x08000000)
4546#define AARCH64_LDST_PCREL(insn) (((insn) & 0x3b000000) == 0x18000000)
4547#define AARCH64_LDST_NAP(insn) (((insn) & 0x3b800000) == 0x28000000)
4548#define AARCH64_LDSTP_PI(insn) (((insn) & 0x3b800000) == 0x28800000)
4549#define AARCH64_LDSTP_O(insn) (((insn) & 0x3b800000) == 0x29000000)
4550#define AARCH64_LDSTP_PRE(insn) (((insn) & 0x3b800000) == 0x29800000)
4551#define AARCH64_LDST_UI(insn) (((insn) & 0x3b200c00) == 0x38000000)
4552#define AARCH64_LDST_PIIMM(insn) (((insn) & 0x3b200c00) == 0x38000400)
4553#define AARCH64_LDST_U(insn) (((insn) & 0x3b200c00) == 0x38000800)
4554#define AARCH64_LDST_PREIMM(insn) (((insn) & 0x3b200c00) == 0x38000c00)
4555#define AARCH64_LDST_RO(insn) (((insn) & 0x3b200c00) == 0x38200800)
4556#define AARCH64_LDST_UIMM(insn) (((insn) & 0x3b000000) == 0x39000000)
4557#define AARCH64_LDST_SIMD_M(insn) (((insn) & 0xbfbf0000) == 0x0c000000)
4558#define AARCH64_LDST_SIMD_M_PI(insn) (((insn) & 0xbfa00000) == 0x0c800000)
4559#define AARCH64_LDST_SIMD_S(insn) (((insn) & 0xbf9f0000) == 0x0d000000)
4560#define AARCH64_LDST_SIMD_S_PI(insn) (((insn) & 0xbf800000) == 0x0d800000)
4561
3d14faea
MS
4562/* Classify an INSN if it is indeed a load/store.
4563
4564 Return TRUE if INSN is a LD/ST instruction otherwise return FALSE.
4565
4566 For scalar LD/ST instructions PAIR is FALSE, RT is returned and RT2
4567 is set equal to RT.
4568
2d0ca824 4569 For LD/ST pair instructions PAIR is TRUE, RT and RT2 are returned. */
68fcca92
JW
4570
4571static bfd_boolean
3d14faea 4572aarch64_mem_op_p (uint32_t insn, unsigned int *rt, unsigned int *rt2,
68fcca92
JW
4573 bfd_boolean *pair, bfd_boolean *load)
4574{
4575 uint32_t opcode;
4576 unsigned int r;
4577 uint32_t opc = 0;
4578 uint32_t v = 0;
4579 uint32_t opc_v = 0;
4580
de194d85 4581 /* Bail out quickly if INSN doesn't fall into the load-store
68fcca92
JW
4582 encoding space. */
4583 if (!AARCH64_LDST (insn))
4584 return FALSE;
4585
4586 *pair = FALSE;
4587 *load = FALSE;
4588 if (AARCH64_LDST_EX (insn))
4589 {
4590 *rt = AARCH64_RT (insn);
3d14faea 4591 *rt2 = *rt;
68fcca92 4592 if (AARCH64_BIT (insn, 21) == 1)
07d6d2b8 4593 {
68fcca92 4594 *pair = TRUE;
3d14faea 4595 *rt2 = AARCH64_RT2 (insn);
68fcca92
JW
4596 }
4597 *load = AARCH64_LD (insn);
4598 return TRUE;
4599 }
4600 else if (AARCH64_LDST_NAP (insn)
4601 || AARCH64_LDSTP_PI (insn)
4602 || AARCH64_LDSTP_O (insn)
4603 || AARCH64_LDSTP_PRE (insn))
4604 {
4605 *pair = TRUE;
4606 *rt = AARCH64_RT (insn);
3d14faea 4607 *rt2 = AARCH64_RT2 (insn);
68fcca92
JW
4608 *load = AARCH64_LD (insn);
4609 return TRUE;
4610 }
4611 else if (AARCH64_LDST_PCREL (insn)
4612 || AARCH64_LDST_UI (insn)
4613 || AARCH64_LDST_PIIMM (insn)
4614 || AARCH64_LDST_U (insn)
4615 || AARCH64_LDST_PREIMM (insn)
4616 || AARCH64_LDST_RO (insn)
4617 || AARCH64_LDST_UIMM (insn))
4618 {
4619 *rt = AARCH64_RT (insn);
3d14faea 4620 *rt2 = *rt;
68fcca92
JW
4621 if (AARCH64_LDST_PCREL (insn))
4622 *load = TRUE;
4623 opc = AARCH64_BITS (insn, 22, 2);
4624 v = AARCH64_BIT (insn, 26);
4625 opc_v = opc | (v << 2);
4626 *load = (opc_v == 1 || opc_v == 2 || opc_v == 3
4627 || opc_v == 5 || opc_v == 7);
4628 return TRUE;
4629 }
4630 else if (AARCH64_LDST_SIMD_M (insn)
4631 || AARCH64_LDST_SIMD_M_PI (insn))
4632 {
4633 *rt = AARCH64_RT (insn);
4634 *load = AARCH64_BIT (insn, 22);
4635 opcode = (insn >> 12) & 0xf;
4636 switch (opcode)
4637 {
4638 case 0:
4639 case 2:
3d14faea 4640 *rt2 = *rt + 3;
68fcca92
JW
4641 break;
4642
4643 case 4:
4644 case 6:
3d14faea 4645 *rt2 = *rt + 2;
68fcca92
JW
4646 break;
4647
4648 case 7:
3d14faea 4649 *rt2 = *rt;
68fcca92
JW
4650 break;
4651
4652 case 8:
4653 case 10:
3d14faea 4654 *rt2 = *rt + 1;
68fcca92
JW
4655 break;
4656
4657 default:
4658 return FALSE;
4659 }
4660 return TRUE;
4661 }
4662 else if (AARCH64_LDST_SIMD_S (insn)
4663 || AARCH64_LDST_SIMD_S_PI (insn))
4664 {
4665 *rt = AARCH64_RT (insn);
4666 r = (insn >> 21) & 1;
4667 *load = AARCH64_BIT (insn, 22);
4668 opcode = (insn >> 13) & 0x7;
4669 switch (opcode)
4670 {
4671 case 0:
4672 case 2:
4673 case 4:
3d14faea 4674 *rt2 = *rt + r;
68fcca92
JW
4675 break;
4676
4677 case 1:
4678 case 3:
4679 case 5:
3d14faea 4680 *rt2 = *rt + (r == 0 ? 2 : 3);
68fcca92
JW
4681 break;
4682
4683 case 6:
3d14faea 4684 *rt2 = *rt + r;
68fcca92
JW
4685 break;
4686
4687 case 7:
3d14faea 4688 *rt2 = *rt + (r == 0 ? 2 : 3);
68fcca92
JW
4689 break;
4690
4691 default:
4692 return FALSE;
4693 }
4694 return TRUE;
4695 }
4696
4697 return FALSE;
4698}
4699
4700/* Return TRUE if INSN is multiply-accumulate. */
4701
4702static bfd_boolean
4703aarch64_mlxl_p (uint32_t insn)
4704{
4705 uint32_t op31 = AARCH64_OP31 (insn);
4706
4707 if (AARCH64_MAC (insn)
4708 && (op31 == 0 || op31 == 1 || op31 == 5)
4709 /* Exclude MUL instructions which are encoded as a multiple accumulate
4710 with RA = XZR. */
4711 && AARCH64_RA (insn) != AARCH64_ZR)
4712 return TRUE;
4713
4714 return FALSE;
4715}
4716
4717/* Some early revisions of the Cortex-A53 have an erratum (835769) whereby
4718 it is possible for a 64-bit multiply-accumulate instruction to generate an
4719 incorrect result. The details are quite complex and hard to
4720 determine statically, since branches in the code may exist in some
4721 circumstances, but all cases end with a memory (load, store, or
4722 prefetch) instruction followed immediately by the multiply-accumulate
4723 operation. We employ a linker patching technique, by moving the potentially
4724 affected multiply-accumulate instruction into a patch region and replacing
4725 the original instruction with a branch to the patch. This function checks
4726 if INSN_1 is the memory operation followed by a multiply-accumulate
4727 operation (INSN_2). Return TRUE if an erratum sequence is found, FALSE
4728 if INSN_1 and INSN_2 are safe. */
4729
4730static bfd_boolean
4731aarch64_erratum_sequence (uint32_t insn_1, uint32_t insn_2)
4732{
4733 uint32_t rt;
3d14faea 4734 uint32_t rt2;
68fcca92
JW
4735 uint32_t rn;
4736 uint32_t rm;
4737 uint32_t ra;
4738 bfd_boolean pair;
4739 bfd_boolean load;
4740
4741 if (aarch64_mlxl_p (insn_2)
3d14faea 4742 && aarch64_mem_op_p (insn_1, &rt, &rt2, &pair, &load))
68fcca92
JW
4743 {
4744 /* Any SIMD memory op is independent of the subsequent MLA
4745 by definition of the erratum. */
4746 if (AARCH64_BIT (insn_1, 26))
4747 return TRUE;
4748
4749 /* If not SIMD, check for integer memory ops and MLA relationship. */
4750 rn = AARCH64_RN (insn_2);
4751 ra = AARCH64_RA (insn_2);
4752 rm = AARCH64_RM (insn_2);
4753
4754 /* If this is a load and there's a true(RAW) dependency, we are safe
4755 and this is not an erratum sequence. */
4756 if (load &&
4757 (rt == rn || rt == rm || rt == ra
3d14faea 4758 || (pair && (rt2 == rn || rt2 == rm || rt2 == ra))))
68fcca92
JW
4759 return FALSE;
4760
4761 /* We conservatively put out stubs for all other cases (including
4762 writebacks). */
4763 return TRUE;
4764 }
4765
4766 return FALSE;
4767}
4768
2144188d 4769
35fee8b7
MS
4770static char *
4771_bfd_aarch64_erratum_835769_stub_name (unsigned num_fixes)
4772{
4773 char *stub_name = (char *) bfd_malloc
4774 (strlen ("__erratum_835769_veneer_") + 16);
bb69498c
NC
4775 if (stub_name != NULL)
4776 sprintf (stub_name,"__erratum_835769_veneer_%d", num_fixes);
35fee8b7
MS
4777 return stub_name;
4778}
4779
4106101c 4780/* Scan for Cortex-A53 erratum 835769 sequence.
2144188d
MS
4781
4782 Return TRUE else FALSE on abnormal termination. */
4783
68fcca92 4784static bfd_boolean
5421cc6e
MS
4785_bfd_aarch64_erratum_835769_scan (bfd *input_bfd,
4786 struct bfd_link_info *info,
4787 unsigned int *num_fixes_p)
68fcca92
JW
4788{
4789 asection *section;
4790 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
68fcca92 4791 unsigned int num_fixes = *num_fixes_p;
68fcca92
JW
4792
4793 if (htab == NULL)
2144188d 4794 return TRUE;
68fcca92
JW
4795
4796 for (section = input_bfd->sections;
4797 section != NULL;
4798 section = section->next)
4799 {
4800 bfd_byte *contents = NULL;
4801 struct _aarch64_elf_section_data *sec_data;
4802 unsigned int span;
4803
4804 if (elf_section_type (section) != SHT_PROGBITS
4805 || (elf_section_flags (section) & SHF_EXECINSTR) == 0
4806 || (section->flags & SEC_EXCLUDE) != 0
4807 || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4808 || (section->output_section == bfd_abs_section_ptr))
4809 continue;
4810
4811 if (elf_section_data (section)->this_hdr.contents != NULL)
4812 contents = elf_section_data (section)->this_hdr.contents;
4813 else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
2144188d 4814 return FALSE;
68fcca92
JW
4815
4816 sec_data = elf_aarch64_section_data (section);
520c7b56 4817
ccf61261
NC
4818 if (sec_data->mapcount)
4819 qsort (sec_data->map, sec_data->mapcount,
4820 sizeof (elf_aarch64_section_map), elf_aarch64_compare_mapping);
520c7b56 4821
68fcca92
JW
4822 for (span = 0; span < sec_data->mapcount; span++)
4823 {
4824 unsigned int span_start = sec_data->map[span].vma;
4825 unsigned int span_end = ((span == sec_data->mapcount - 1)
4826 ? sec_data->map[0].vma + section->size
4827 : sec_data->map[span + 1].vma);
4828 unsigned int i;
4829 char span_type = sec_data->map[span].type;
4830
4831 if (span_type == 'd')
4832 continue;
4833
4834 for (i = span_start; i + 4 < span_end; i += 4)
4835 {
4836 uint32_t insn_1 = bfd_getl32 (contents + i);
4837 uint32_t insn_2 = bfd_getl32 (contents + i + 4);
4838
4839 if (aarch64_erratum_sequence (insn_1, insn_2))
4840 {
5421cc6e 4841 struct elf_aarch64_stub_hash_entry *stub_entry;
35fee8b7
MS
4842 char *stub_name = _bfd_aarch64_erratum_835769_stub_name (num_fixes);
4843 if (! stub_name)
2144188d 4844 return FALSE;
68fcca92 4845
5421cc6e
MS
4846 stub_entry = _bfd_aarch64_add_stub_entry_in_group (stub_name,
4847 section,
4848 htab);
4849 if (! stub_entry)
4850 return FALSE;
68fcca92 4851
5421cc6e
MS
4852 stub_entry->stub_type = aarch64_stub_erratum_835769_veneer;
4853 stub_entry->target_section = section;
4854 stub_entry->target_value = i + 4;
4855 stub_entry->veneered_insn = insn_2;
4856 stub_entry->output_name = stub_name;
68fcca92
JW
4857 num_fixes++;
4858 }
4859 }
4860 }
4861 if (elf_section_data (section)->this_hdr.contents == NULL)
4862 free (contents);
4863 }
4864
357d1523
MS
4865 *num_fixes_p = num_fixes;
4866
2144188d 4867 return TRUE;
68fcca92
JW
4868}
4869
13f622ec 4870
4106101c
MS
4871/* Test if instruction INSN is ADRP. */
4872
4873static bfd_boolean
4874_bfd_aarch64_adrp_p (uint32_t insn)
4875{
9fca35fc 4876 return ((insn & AARCH64_ADRP_OP_MASK) == AARCH64_ADRP_OP);
4106101c
MS
4877}
4878
4879
4880/* Helper predicate to look for cortex-a53 erratum 843419 sequence 1. */
4881
4882static bfd_boolean
4883_bfd_aarch64_erratum_843419_sequence_p (uint32_t insn_1, uint32_t insn_2,
4884 uint32_t insn_3)
4885{
4886 uint32_t rt;
4887 uint32_t rt2;
4888 bfd_boolean pair;
4889 bfd_boolean load;
4890
4891 return (aarch64_mem_op_p (insn_2, &rt, &rt2, &pair, &load)
4892 && (!pair
4893 || (pair && !load))
4894 && AARCH64_LDST_UIMM (insn_3)
4895 && AARCH64_RN (insn_3) == AARCH64_RD (insn_1));
4896}
4897
4898
4899/* Test for the presence of Cortex-A53 erratum 843419 instruction sequence.
4900
4901 Return TRUE if section CONTENTS at offset I contains one of the
4902 erratum 843419 sequences, otherwise return FALSE. If a sequence is
4903 seen set P_VENEER_I to the offset of the final LOAD/STORE
4904 instruction in the sequence.
4905 */
4906
4907static bfd_boolean
4908_bfd_aarch64_erratum_843419_p (bfd_byte *contents, bfd_vma vma,
4909 bfd_vma i, bfd_vma span_end,
4910 bfd_vma *p_veneer_i)
4911{
4912 uint32_t insn_1 = bfd_getl32 (contents + i);
4913
4914 if (!_bfd_aarch64_adrp_p (insn_1))
4915 return FALSE;
4916
4917 if (span_end < i + 12)
4918 return FALSE;
4919
4920 uint32_t insn_2 = bfd_getl32 (contents + i + 4);
4921 uint32_t insn_3 = bfd_getl32 (contents + i + 8);
4922
4923 if ((vma & 0xfff) != 0xff8 && (vma & 0xfff) != 0xffc)
4924 return FALSE;
4925
4926 if (_bfd_aarch64_erratum_843419_sequence_p (insn_1, insn_2, insn_3))
4927 {
4928 *p_veneer_i = i + 8;
4929 return TRUE;
4930 }
4931
4932 if (span_end < i + 16)
4933 return FALSE;
4934
4935 uint32_t insn_4 = bfd_getl32 (contents + i + 12);
4936
4937 if (_bfd_aarch64_erratum_843419_sequence_p (insn_1, insn_2, insn_4))
4938 {
4939 *p_veneer_i = i + 12;
4940 return TRUE;
4941 }
4942
4943 return FALSE;
4944}
4945
4946
13f622ec
MS
4947/* Resize all stub sections. */
4948
4949static void
4950_bfd_aarch64_resize_stubs (struct elf_aarch64_link_hash_table *htab)
4951{
4952 asection *section;
4953
4954 /* OK, we've added some stubs. Find out the new size of the
4955 stub sections. */
4956 for (section = htab->stub_bfd->sections;
4957 section != NULL; section = section->next)
4958 {
4959 /* Ignore non-stub sections. */
4960 if (!strstr (section->name, STUB_SUFFIX))
4961 continue;
4962 section->size = 0;
4963 }
4964
4965 bfd_hash_traverse (&htab->stub_hash_table, aarch64_size_one_stub, htab);
13f622ec 4966
61865519
MS
4967 for (section = htab->stub_bfd->sections;
4968 section != NULL; section = section->next)
4969 {
4970 if (!strstr (section->name, STUB_SUFFIX))
4971 continue;
4972
9a2ebffd
JW
4973 /* Add space for a branch. Add 8 bytes to keep section 8 byte aligned,
4974 as long branch stubs contain a 64-bit address. */
61865519 4975 if (section->size)
9a2ebffd 4976 section->size += 8;
4106101c
MS
4977
4978 /* Ensure all stub sections have a size which is a multiple of
4979 4096. This is important in order to ensure that the insertion
4980 of stub sections does not in itself move existing code around
739b5c9c
TC
4981 in such a way that new errata sequences are created. We only do this
4982 when the ADRP workaround is enabled. If only the ADR workaround is
4983 enabled then the stubs workaround won't ever be used. */
4984 if (htab->fix_erratum_843419 & ERRAT_ADRP)
4106101c
MS
4985 if (section->size)
4986 section->size = BFD_ALIGN (section->size, 0x1000);
4987 }
4988}
4989
9a2ebffd 4990/* Construct an erratum 843419 workaround stub name. */
4106101c
MS
4991
4992static char *
4993_bfd_aarch64_erratum_843419_stub_name (asection *input_section,
4994 bfd_vma offset)
4995{
4996 const bfd_size_type len = 8 + 4 + 1 + 8 + 1 + 16 + 1;
4997 char *stub_name = bfd_malloc (len);
4998
4999 if (stub_name != NULL)
5000 snprintf (stub_name, len, "e843419@%04x_%08x_%" BFD_VMA_FMT "x",
5001 input_section->owner->id,
5002 input_section->id,
5003 offset);
5004 return stub_name;
5005}
5006
5007/* Build a stub_entry structure describing an 843419 fixup.
5008
5009 The stub_entry constructed is populated with the bit pattern INSN
5010 of the instruction located at OFFSET within input SECTION.
5011
5012 Returns TRUE on success. */
5013
5014static bfd_boolean
5015_bfd_aarch64_erratum_843419_fixup (uint32_t insn,
5016 bfd_vma adrp_offset,
5017 bfd_vma ldst_offset,
5018 asection *section,
5019 struct bfd_link_info *info)
5020{
5021 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
5022 char *stub_name;
5023 struct elf_aarch64_stub_hash_entry *stub_entry;
5024
5025 stub_name = _bfd_aarch64_erratum_843419_stub_name (section, ldst_offset);
bb69498c
NC
5026 if (stub_name == NULL)
5027 return FALSE;
4106101c
MS
5028 stub_entry = aarch64_stub_hash_lookup (&htab->stub_hash_table, stub_name,
5029 FALSE, FALSE);
5030 if (stub_entry)
5031 {
5032 free (stub_name);
5033 return TRUE;
5034 }
5035
5036 /* We always place an 843419 workaround veneer in the stub section
5037 attached to the input section in which an erratum sequence has
5038 been found. This ensures that later in the link process (in
5039 elfNN_aarch64_write_section) when we copy the veneered
5040 instruction from the input section into the stub section the
5041 copied instruction will have had any relocations applied to it.
5042 If we placed workaround veneers in any other stub section then we
5043 could not assume that all relocations have been processed on the
5044 corresponding input section at the point we output the stub
bb69498c 5045 section. */
4106101c
MS
5046
5047 stub_entry = _bfd_aarch64_add_stub_entry_after (stub_name, section, htab);
5048 if (stub_entry == NULL)
5049 {
5050 free (stub_name);
5051 return FALSE;
5052 }
5053
5054 stub_entry->adrp_offset = adrp_offset;
5055 stub_entry->target_value = ldst_offset;
5056 stub_entry->target_section = section;
5057 stub_entry->stub_type = aarch64_stub_erratum_843419_veneer;
5058 stub_entry->veneered_insn = insn;
5059 stub_entry->output_name = stub_name;
5060
5061 return TRUE;
5062}
5063
5064
5065/* Scan an input section looking for the signature of erratum 843419.
5066
5067 Scans input SECTION in INPUT_BFD looking for erratum 843419
5068 signatures, for each signature found a stub_entry is created
5069 describing the location of the erratum for subsequent fixup.
5070
5071 Return TRUE on successful scan, FALSE on failure to scan.
5072 */
5073
5074static bfd_boolean
5075_bfd_aarch64_erratum_843419_scan (bfd *input_bfd, asection *section,
5076 struct bfd_link_info *info)
5077{
5078 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
5079
5080 if (htab == NULL)
5081 return TRUE;
5082
5083 if (elf_section_type (section) != SHT_PROGBITS
5084 || (elf_section_flags (section) & SHF_EXECINSTR) == 0
5085 || (section->flags & SEC_EXCLUDE) != 0
5086 || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5087 || (section->output_section == bfd_abs_section_ptr))
5088 return TRUE;
5089
5090 do
5091 {
5092 bfd_byte *contents = NULL;
5093 struct _aarch64_elf_section_data *sec_data;
5094 unsigned int span;
5095
5096 if (elf_section_data (section)->this_hdr.contents != NULL)
5097 contents = elf_section_data (section)->this_hdr.contents;
5098 else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
5099 return FALSE;
5100
5101 sec_data = elf_aarch64_section_data (section);
5102
ccf61261
NC
5103 if (sec_data->mapcount)
5104 qsort (sec_data->map, sec_data->mapcount,
5105 sizeof (elf_aarch64_section_map), elf_aarch64_compare_mapping);
4106101c
MS
5106
5107 for (span = 0; span < sec_data->mapcount; span++)
5108 {
5109 unsigned int span_start = sec_data->map[span].vma;
5110 unsigned int span_end = ((span == sec_data->mapcount - 1)
5111 ? sec_data->map[0].vma + section->size
5112 : sec_data->map[span + 1].vma);
5113 unsigned int i;
5114 char span_type = sec_data->map[span].type;
5115
5116 if (span_type == 'd')
5117 continue;
5118
5119 for (i = span_start; i + 8 < span_end; i += 4)
5120 {
5121 bfd_vma vma = (section->output_section->vma
5122 + section->output_offset
5123 + i);
5124 bfd_vma veneer_i;
5125
5126 if (_bfd_aarch64_erratum_843419_p
5127 (contents, vma, i, span_end, &veneer_i))
5128 {
5129 uint32_t insn = bfd_getl32 (contents + veneer_i);
5130
5131 if (!_bfd_aarch64_erratum_843419_fixup (insn, i, veneer_i,
5132 section, info))
5133 return FALSE;
5134 }
5135 }
5136 }
5137
5138 if (elf_section_data (section)->this_hdr.contents == NULL)
5139 free (contents);
61865519 5140 }
4106101c
MS
5141 while (0);
5142
5143 return TRUE;
61865519 5144}
13f622ec 5145
7ff36d1a
SP
5146static bfd_boolean
5147section_start_symbol (bfd *abfd ATTRIBUTE_UNUSED, asection *section,
5148 void *valp)
5149{
5150 return section->vma == *(bfd_vma *)valp;
5151}
5152
5153/* Capability format functions. */
5154
5155static unsigned
5156exponent (uint64_t len)
5157{
5158#define CAP_MAX_EXPONENT 50
5159 /* Size is a 65 bit value, so there's an implicit 0 MSB. */
5160 unsigned zeroes = __builtin_clzl (len) + 1;
5161
5162 /* All bits up to and including CAP_MW - 2 are zero. */
5163 if (CAP_MAX_EXPONENT < zeroes)
5164 return (unsigned) -1;
5165 else
5166 return CAP_MAX_EXPONENT - zeroes;
5167#undef CAP_MAX_EXPONENT
5168}
5169
dbd880c7 5170#define ONES(x) ((1ULL << (x)) - 1)
7ff36d1a
SP
5171#define ALIGN_UP(x, a) (((x) + ONES (a)) & (~ONES (a)))
5172
5173static bfd_boolean
5a9e7a18 5174c64_valid_cap_range (bfd_vma *basep, bfd_vma *limitp, unsigned *alignmentp)
7ff36d1a
SP
5175{
5176 bfd_vma base = *basep, size = *limitp - *basep;
5177
5178 unsigned e, old_e;
5a9e7a18 5179 *alignmentp = 1;
7ff36d1a
SP
5180
5181 if ((e = exponent (size)) == (unsigned) -1)
5182 return TRUE;
5183
5184 size = ALIGN_UP (size, e + 3);
5185 old_e = e;
5186 e = exponent (size);
5187 if (old_e != e)
5188 size = ALIGN_UP (size, e + 3);
5189
5190 base = ALIGN_UP (base, e + 3);
5191
5a9e7a18 5192 *alignmentp = e+3;
7ff36d1a
SP
5193 if (base == *basep && *limitp == base + size)
5194 return TRUE;
5195
5196 *basep = base;
5197 *limitp = base + size;
5198 return FALSE;
5199}
5200
8504495a
AC
5201static bfd_boolean
5202c64_get_section_padding_info (struct elf_aarch64_link_hash_table *htab,
5203 asection *sec, void ***info)
5204{
5205 if (!htab->c64_sec_info)
5206 {
5207 unsigned min_os_id, max_os_id;
5208 min_os_id = max_os_id = sec->id;
5209 asection *iter;
5210 bfd *output_bfd = sec->owner;
5211 for (iter = output_bfd->sections; iter; iter = iter->next)
5212 {
5213 BFD_ASSERT (iter->output_section == iter);
5214 if (iter->id < min_os_id)
5215 min_os_id = iter->id;
5216 if (iter->id > max_os_id)
5217 max_os_id = iter->id;
5218 }
5219
5220 /* Create a sparse array mapping sections IDs onto cookies. */
5221 const int num_slots = max_os_id - min_os_id + 1;
5222 htab->min_output_section_id = min_os_id;
5223 htab->max_output_section_id = max_os_id;
5224 const size_t mem_sz = num_slots * sizeof (void *);
5225 htab->c64_sec_info = bfd_zalloc (output_bfd, mem_sz);
5226 if (!htab->c64_sec_info)
5227 {
5228 _bfd_error_handler (_("out of memory allocating padding info"));
5229 bfd_set_error (bfd_error_no_memory);
5230 return FALSE;
5231 }
5232 }
5233
5234 BFD_ASSERT (sec->id >= htab->min_output_section_id);
5235 BFD_ASSERT (sec->id <= htab->max_output_section_id);
5236 *info = htab->c64_sec_info + (sec->id - htab->min_output_section_id);
5237 return TRUE;
5238}
5239
396742bf
MM
5240/* Check if the bounds of section SEC will get rounded off in the Morello
5241 capability format and if it would, adjust the section to ensure any
5242 capability spanning this section would have its bounds precise. */
8504495a 5243static bfd_boolean
396742bf
MM
5244ensure_precisely_bounded_section (asection *sec,
5245 struct elf_aarch64_link_hash_table *htab,
8504495a 5246 bfd_boolean *changed_layout)
7ff36d1a 5247{
396742bf
MM
5248 bfd_vma low = sec->vma;
5249 bfd_vma high = sec->vma + sec->size;
5250 unsigned alignment;
7ff36d1a 5251
8504495a
AC
5252 void **info;
5253 if (!c64_get_section_padding_info (htab, sec, &info))
5254 return FALSE;
5255
5256 bfd_vma old_padding = htab->c64_get_section_padding (*info);
5257
5258 /* Ignore any existing padding when calculating bounds validity. */
5259 high -= old_padding;
5260
396742bf
MM
5261 bfd_boolean did_change = FALSE;
5262 if (!c64_valid_cap_range (&low, &high, &alignment))
7ff36d1a 5263 {
8504495a
AC
5264 bfd_vma padding = high - low - (sec->size - old_padding);
5265 if (padding != old_padding)
5266 {
5267 htab->c64_set_section_padding (sec, padding, info);
5268 did_change = TRUE;
5269 }
5270 }
5271 else if (old_padding)
5272 {
5273 /* If the range without the padding is valid, then
5274 we can drop the padding. This may happen e.g. if a stub gets
5275 added that happens to take up the space occupied by the
5276 padding. */
5277 htab->c64_set_section_padding (sec, 0, info);
396742bf 5278 did_change = TRUE;
7ff36d1a 5279 }
8504495a 5280
396742bf 5281 if (sec->alignment_power < alignment)
7ff36d1a 5282 {
396742bf
MM
5283 sec->alignment_power = alignment;
5284 did_change = TRUE;
7ff36d1a
SP
5285 }
5286
396742bf 5287 if (did_change)
8504495a
AC
5288 {
5289 (*htab->layout_sections_again) ();
5290 if (changed_layout)
5291 *changed_layout = TRUE;
5292 }
5293
5294 return TRUE;
7ff36d1a
SP
5295}
5296
5297/* Make sure that all capabilities that refer to sections have bounds that
5298 won't overlap with neighbouring sections. This is needed in two specific
5299 cases. The first case is that of PCC, which needs to span across all
5a9e7a18
MM
5300 readonly sections as well as the GOT and PLT sections in the output binary.
5301 The second case is that of linker and ldscript defined symbols that indicate
5302 start and/or end of sections and/or zero-sized symbols.
7ff36d1a
SP
5303
5304 In both cases, overlap of capability bounds are avoided by aligning the base
5305 of the section and if necessary, adding a pad at the end of the section so
5306 that the section following it starts only after the pad. */
5307
5fa80905
AC
5308static bfd_vma pcc_low;
5309static bfd_vma pcc_high;
8504495a
AC
5310static asection *pcc_low_sec;
5311static asection *pcc_high_sec;
5312
5313static bfd_boolean
5314c64_resize_sections (bfd *output_bfd, struct bfd_link_info *info,
5315 bfd_boolean *changed_layout)
7ff36d1a 5316{
4a74b265 5317 asection *sec;
7ff36d1a 5318 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
2d99dff6 5319 bfd *input_bfd;
e2314302 5320 unsigned align = 0;
7ff36d1a 5321
4a74b265
MM
5322 /* If this is not a PURECAP binary, and has no C64 code in it, then this is
5323 just a stock AArch64 binary and the section padding is not necessary.
5324 We can have PURECAP shared libraries that are data-only, so just checking
5325 if there is C64 code in this executable is not enough. We can have HYBRID
5326 binaries, so just checking for PURECAP is not enough. */
5327 if (!(htab->c64_output
5328 || (elf_elfheader (output_bfd)->e_flags & EF_AARCH64_CHERI_PURECAP)))
8504495a 5329 return TRUE;
7ff36d1a 5330
7ff36d1a
SP
5331 /* First, walk through all the relocations to find those referring to linker
5332 defined and ldscript defined symbols since we set their range to their
5333 output sections. */
2d99dff6 5334 for (input_bfd = info->input_bfds;
4a74b265
MM
5335 /* No point iterating over all relocations to ensure each section that
5336 needs to give the bounds for a capability is padded accordingly if
5337 there are no capability relocations in the GOT and there are no
5338 CAPINIT relocations.
5339 N.b. It is possible that when creating a dynamic object a
5340 section-spanning symbol could be used by some other executable
5341 linking to it when we don't have a relocation to it in the given
5342 dynamic library. Rather than pad every section which has a linker
5343 defined symbol pointing into it we choose to allow such use-cases to
5344 have sizes which bleed into another section. */
5345 (htab->c64_rel || htab->srelcaps) && input_bfd != NULL;
5346 input_bfd = input_bfd->link.next)
7ff36d1a
SP
5347 {
5348 Elf_Internal_Shdr *symtab_hdr;
5349
5350 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5351 if (symtab_hdr->sh_info == 0)
5352 continue;
5353
5354 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
5355 {
5356 Elf_Internal_Rela *irelaend, *irela;
5357
5358 /* If there aren't any relocs, then there's nothing more to do. */
5359 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
5360 continue;
5361
5362 irela = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
5363 info->keep_memory);
5364 if (irela == NULL)
5365 continue;
5366
5367 /* Now examine each relocation. */
5368 irelaend = irela + sec->reloc_count;
5369 for (; irela < irelaend; irela++)
5370 {
5371 unsigned int r_indx;
5372 struct elf_link_hash_entry *h;
5373 int e_indx;
5374 asection *os;
5375
5376 r_indx = ELFNN_R_SYM (irela->r_info);
5377
5378 /* Linker defined or linker script defined symbols are always in
5379 the symbol hash. */
5380 if (r_indx < symtab_hdr->sh_info)
5381 continue;
5382
5383 e_indx = r_indx - symtab_hdr->sh_info;
5384 h = elf_sym_hashes (input_bfd)[e_indx];
5385
5386 /* XXX Does this ever happen? */
5387 if (h == NULL)
5388 continue;
5389
5390 os = h->root.u.def.section->output_section;
5391
8504495a
AC
5392 /* XXX: we may see some ldscript-defined
5393 symbols here whose output section is set to *ABS*.
5394 These symbols should get their output section resolved
5395 to a real output section in ldexp_finalize_syms, but
5396 because we are running earlier in the lang_process
5397 flow (specifically in ldemul_after_allocation here) we
5398 see these symbols as pointing into the *ABS* section.
5399
5400 For now, we just skip such symbols, but this should be
5401 fixed properly later on. */
5402 if (os == bfd_abs_section_ptr)
5403 continue;
5404
5405 if (h->root.linker_def || h->start_stop)
5406 {
5407 if (!ensure_precisely_bounded_section (os, htab,
5408 changed_layout))
5409 return FALSE;
5410 }
7ff36d1a
SP
5411 else if (h->root.ldscript_def)
5412 {
5413 const char *name = h->root.root.string;
5414 size_t len = strlen (name);
ea41358b
MM
5415 asection *altos = NULL;
5416 bfd_vma value = os->vma + os->size;
7ff36d1a
SP
5417
5418 if (len > 8 && name[0] == '_' && name[1] == '_'
5419 && (!strncmp (name + 2, "start_", 6)
ea41358b
MM
5420 || !strcmp (name + len - 6, "_start"))
5421 && ((altos = bfd_sections_find_if
5422 (info->output_bfd, section_start_symbol, &value))
5423 != NULL))
8504495a
AC
5424 if (!ensure_precisely_bounded_section (altos, htab,
5425 changed_layout))
5426 return FALSE;
5427
7ff36d1a
SP
5428 /* XXX We're overfitting here because the offset of H within
5429 the output section is not yet resolved and ldscript
5430 defined symbols do not have input section information. */
8504495a
AC
5431 if (!ensure_precisely_bounded_section (os, htab,
5432 changed_layout))
5433 return FALSE;
7ff36d1a
SP
5434 }
5435 }
5436 }
5437 }
5438
5439 /* Next, walk through output sections to find the PCC span and add a padding
5440 at the end to ensure that PCC bounds don't bleed into neighbouring
5441 sections. For now PCC needs to encompass all code sections, .got, .plt
4a74b265
MM
5442 and .got.plt.
5443 If there is no C64 code in this binary, then we do not need to care about
5444 PCC bounds, hence skip this bit.
5445 It's tempting to also avoid padding the PCC range when we have no static
5446 relocations in this binary, since it would seem that we can never end up
5447 trying to access something "outside" the PCC bounds (any PCC bounded
5448 capability provided to an outside dynamic object would be sealed by the
5449 runtime, and hence can't be offset). Unfortunately it is still possible,
5450 since an `adr c0, 0` gives an unsealed capability to the current code
5451 which could then be offset by some other means.
5452 While that seems unlikely to happen, having no relocations in a file also
5453 seems quite unlikely, so we may as well play it safe. */
5454 if (!htab->c64_output)
8504495a 5455 return TRUE;
4a74b265
MM
5456
5457 bfd_vma low = (bfd_vma) -1, high = 0;
8504495a 5458 pcc_low_sec = pcc_high_sec = NULL;
7ff36d1a
SP
5459 for (sec = output_bfd->sections; sec != NULL; sec = sec->next)
5460 {
5461 /* XXX This is a good place to figure out if there are any readable or
5462 writable sections in the PCC range that are not in the list of
5463 sections we want the PCC to span and then warn the user of it. */
5464
5465#define NOT_OP_SECTION(s) ((s) == NULL || (s)->output_section != sec)
5466
0e02111b 5467 if ((sec->flags & SEC_READONLY) == 0
7ff36d1a
SP
5468 && NOT_OP_SECTION (htab->root.sgotplt)
5469 && NOT_OP_SECTION (htab->root.igotplt)
5470 && NOT_OP_SECTION (htab->root.sgot)
5471 && NOT_OP_SECTION (htab->root.splt)
0e02111b
MM
5472 && NOT_OP_SECTION (htab->root.iplt)
5473 && (sec->vma < info->relro_start
5474 || sec->vma >= info->relro_end))
5475 continue;
5476 if ((sec->flags & SEC_ALLOC) == 0)
7ff36d1a
SP
5477 continue;
5478
5479 if (sec->vma < low)
5480 {
5481 low = sec->vma;
5482 pcc_low_sec = sec;
5483 }
5484 if (sec->vma + sec->size > high)
5485 {
5486 high = sec->vma + sec->size;
5487 pcc_high_sec = sec;
5488 }
5489
5490#undef NOT_OP_SECTION
5491 }
5492
4a74b265
MM
5493 /* Set the PCC range to have precise bounds to ensure that PCC relative loads
5494 can not access outside of their given range. */
e2314302 5495 if (pcc_low_sec != NULL)
5fa80905 5496 {
e2314302
MM
5497 BFD_ASSERT (pcc_high_sec);
5498
5499 bfd_vma pcc_low_tmp;
5500 bfd_vma pcc_high_tmp;
5501
5502 /* We have to be a little careful about the padding we introduce. The
5503 padding we could calculate here may not be the padding that we would
5504 want after the very first section in the PCC bounds has been aligned
5505 properly. That change in the start address propagated through a few
5506 different sections with their own alignment requirements can easily
5507 change the length of the region we want the PCC to span.
4a74b265 5508 Also, that change in length could change the alignment we want. We
8504495a 5509 don't prove that the alignment requirement converges, but believe
4a74b265
MM
5510 that it should (there is only so much space that existing alignment
5511 requirements could trigger to be added -- a section with an alignment
5512 requirement of 16 can only really add 15 bytes to the length). */
e2314302
MM
5513 while (TRUE) {
5514 pcc_low_tmp = pcc_low_sec->vma;
5515 pcc_high_tmp = pcc_high_sec->vma + pcc_high_sec->size;
8504495a 5516 c64_valid_cap_range (&pcc_low_tmp, &pcc_high_tmp, &align);
e2314302
MM
5517 if (pcc_low_sec->alignment_power >= align)
5518 break;
5519 pcc_low_sec->alignment_power = align;
5520 (*htab->layout_sections_again) ();
8504495a
AC
5521 if (changed_layout)
5522 *changed_layout = TRUE;
e2314302
MM
5523 }
5524
8504495a
AC
5525 /* We have aligned the base section appropriately, but we may
5526 still (later) need to add padding after pcc_high_sec to
5527 get representable PCC bounds. */
5528 BFD_ASSERT (pcc_low_tmp == pcc_low_sec->vma);
5529 pcc_low = pcc_low_tmp;
5530 pcc_high = pcc_high_tmp;
5fa80905 5531 }
8504495a
AC
5532
5533 return TRUE;
5534}
5535
5536bfd_boolean
5537elfNN_c64_resize_sections (bfd *output_bfd, struct bfd_link_info *info,
5538 c64_section_padding_setter_t set_padding,
5539 c64_section_padding_getter_t get_padding,
5540 c64_pad_after_section_t pad_after_section,
5541 void (*layout_sections_again) (void))
5542{
5543 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
5544 htab->c64_set_section_padding = set_padding;
5545 htab->c64_get_section_padding = get_padding;
5546 htab->c64_pad_after_section = pad_after_section;
5547 htab->layout_sections_again = layout_sections_again;
5548
5549 /* We assert this here since we require elfNN_aarch64_size_stubs to be
5550 called to complete setting the PCC bounds, see the comment at the
5551 top of elfNN_aarch64_setup_section_lists. */
5552 BFD_ASSERT (is_elf_hash_table (htab));
5553
5554 return c64_resize_sections (output_bfd, info, NULL);
7ff36d1a 5555}
4106101c 5556
a06ea964
NC
5557/* Determine and set the size of the stub section for a final link.
5558
5559 The basic idea here is to examine all the relocations looking for
50e192f0
SP
5560 PC-relative calls to a target that either needs a PE state change (A64 to
5561 C64 or vice versa) or in case of unconditional branches (B/BL), is
5562 unreachable. */
a06ea964 5563
8504495a
AC
5564static bfd_boolean
5565aarch64_size_stubs (bfd *output_bfd,
5566 struct bfd_link_info *info)
a06ea964 5567{
5421cc6e 5568 bfd_boolean stub_changed = FALSE;
cec5225b 5569 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
68fcca92 5570 unsigned int num_erratum_835769_fixes = 0;
a06ea964 5571
5421cc6e
MS
5572 if (htab->fix_erratum_835769)
5573 {
5574 bfd *input_bfd;
5575
5576 for (input_bfd = info->input_bfds;
5577 input_bfd != NULL; input_bfd = input_bfd->link.next)
8c803a2d
AM
5578 {
5579 if (!is_aarch64_elf (input_bfd)
5580 || (input_bfd->flags & BFD_LINKER_CREATED) != 0)
5581 continue;
5582
5583 if (!_bfd_aarch64_erratum_835769_scan (input_bfd, info,
5584 &num_erratum_835769_fixes))
5585 return FALSE;
5586 }
5421cc6e 5587
4106101c
MS
5588 _bfd_aarch64_resize_stubs (htab);
5589 (*htab->layout_sections_again) ();
5590 }
5591
739b5c9c 5592 if (htab->fix_erratum_843419 != ERRAT_NONE)
4106101c
MS
5593 {
5594 bfd *input_bfd;
5595
5596 for (input_bfd = info->input_bfds;
5597 input_bfd != NULL;
5598 input_bfd = input_bfd->link.next)
5599 {
5600 asection *section;
5601
8c803a2d
AM
5602 if (!is_aarch64_elf (input_bfd)
5603 || (input_bfd->flags & BFD_LINKER_CREATED) != 0)
5604 continue;
5605
4106101c
MS
5606 for (section = input_bfd->sections;
5607 section != NULL;
5608 section = section->next)
5609 if (!_bfd_aarch64_erratum_843419_scan (input_bfd, section, info))
5610 return FALSE;
5611 }
5612
5613 _bfd_aarch64_resize_stubs (htab);
5614 (*htab->layout_sections_again) ();
5421cc6e
MS
5615 }
5616
a06ea964
NC
5617 while (1)
5618 {
5619 bfd *input_bfd;
a06ea964 5620
9b9971aa
MS
5621 for (input_bfd = info->input_bfds;
5622 input_bfd != NULL; input_bfd = input_bfd->link.next)
a06ea964
NC
5623 {
5624 Elf_Internal_Shdr *symtab_hdr;
5625 asection *section;
a06ea964 5626
8c803a2d
AM
5627 if (!is_aarch64_elf (input_bfd)
5628 || (input_bfd->flags & BFD_LINKER_CREATED) != 0)
5629 continue;
5630
a06ea964
NC
5631 /* We'll need the symbol table in a second. */
5632 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5633 if (symtab_hdr->sh_info == 0)
5634 continue;
5635
5636 /* Walk over each section attached to the input bfd. */
5637 for (section = input_bfd->sections;
5638 section != NULL; section = section->next)
5639 {
5640 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
5641
5642 /* If there aren't any relocs, then there's nothing more
5643 to do. */
5644 if ((section->flags & SEC_RELOC) == 0
5645 || section->reloc_count == 0
5646 || (section->flags & SEC_CODE) == 0)
5647 continue;
5648
5649 /* If this section is a link-once section that will be
5650 discarded, then don't create any stubs. */
5651 if (section->output_section == NULL
5652 || section->output_section->owner != output_bfd)
5653 continue;
5654
5655 /* Get the relocs. */
5656 internal_relocs
5657 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
5658 NULL, info->keep_memory);
5659 if (internal_relocs == NULL)
5660 goto error_ret_free_local;
5661
5662 /* Now examine each relocation. */
5663 irela = internal_relocs;
5664 irelaend = irela + section->reloc_count;
5665 for (; irela < irelaend; irela++)
5666 {
5667 unsigned int r_type, r_indx;
50e192f0 5668 enum elf_aarch64_stub_type stub_type = aarch64_stub_none;
cec5225b 5669 struct elf_aarch64_stub_hash_entry *stub_entry;
a06ea964
NC
5670 asection *sym_sec;
5671 bfd_vma sym_value;
5672 bfd_vma destination;
cec5225b 5673 struct elf_aarch64_link_hash_entry *hash;
a06ea964
NC
5674 const char *sym_name;
5675 char *stub_name;
5676 const asection *id_sec;
5677 unsigned char st_type;
5678 bfd_size_type len;
50e192f0
SP
5679 unsigned branch_to_c64 = FALSE;
5680 const char *suffix;
a06ea964 5681
cec5225b
YZ
5682 r_type = ELFNN_R_TYPE (irela->r_info);
5683 r_indx = ELFNN_R_SYM (irela->r_info);
a06ea964
NC
5684
5685 if (r_type >= (unsigned int) R_AARCH64_end)
5686 {
5687 bfd_set_error (bfd_error_bad_value);
5688 error_ret_free_internal:
5689 if (elf_section_data (section)->relocs == NULL)
5690 free (internal_relocs);
5691 goto error_ret_free_local;
5692 }
5693
5694 /* Only look for stubs on unconditional branch and
5695 branch and link instructions. */
50e192f0 5696 if (!aarch64_branch_reloc_p (r_type))
a06ea964
NC
5697 continue;
5698
5699 /* Now determine the call target, its name, value,
5700 section. */
5701 sym_sec = NULL;
5702 sym_value = 0;
5703 destination = 0;
5704 hash = NULL;
5705 sym_name = NULL;
5706 if (r_indx < symtab_hdr->sh_info)
5707 {
5708 /* It's a local symbol. */
f0070c1e
SP
5709 Elf_Internal_Sym *sym =
5710 bfd_sym_from_r_symndx (&htab->root.sym_cache,
5711 input_bfd, r_indx);
5712 if (sym == NULL)
5713 goto error_ret_free_internal;
a06ea964 5714
50e192f0
SP
5715 branch_to_c64 |= (sym->st_target_internal
5716 & ST_BRANCH_TO_C64);
5717
f0070c1e
SP
5718 Elf_Internal_Shdr *hdr =
5719 elf_elfsections (input_bfd)[sym->st_shndx];
a06ea964 5720
a06ea964
NC
5721 sym_sec = hdr->bfd_section;
5722 if (!sym_sec)
5723 /* This is an undefined symbol. It can never
5724 be resolved. */
5725 continue;
5726
5727 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5728 sym_value = sym->st_value;
5729 destination = (sym_value + irela->r_addend
5730 + sym_sec->output_offset
5731 + sym_sec->output_section->vma);
5732 st_type = ELF_ST_TYPE (sym->st_info);
5733 sym_name
5734 = bfd_elf_string_from_elf_section (input_bfd,
5735 symtab_hdr->sh_link,
5736 sym->st_name);
50e192f0
SP
5737
5738 /* Get the interworking stub if needed. */
5739 stub_type = aarch64_interwork_stub (r_type,
5740 branch_to_c64);
a06ea964
NC
5741 }
5742 else
5743 {
5744 int e_indx;
50e192f0
SP
5745 struct elf_aarch64_link_hash_table *globals =
5746 elf_aarch64_hash_table (info);
a06ea964
NC
5747
5748 e_indx = r_indx - symtab_hdr->sh_info;
cec5225b 5749 hash = ((struct elf_aarch64_link_hash_entry *)
a06ea964
NC
5750 elf_sym_hashes (input_bfd)[e_indx]);
5751
5752 while (hash->root.root.type == bfd_link_hash_indirect
5753 || hash->root.root.type == bfd_link_hash_warning)
cec5225b 5754 hash = ((struct elf_aarch64_link_hash_entry *)
a06ea964
NC
5755 hash->root.root.u.i.link);
5756
50e192f0
SP
5757 /* Static executable. */
5758 if (globals->root.splt == NULL || hash == NULL
5759 || hash->root.plt.offset == (bfd_vma) - 1)
5760 {
5761 branch_to_c64 |= (hash->root.target_internal
5762 & ST_BRANCH_TO_C64);
5763 stub_type = aarch64_interwork_stub (r_type,
5764 branch_to_c64);
5765 }
5766
a06ea964
NC
5767 if (hash->root.root.type == bfd_link_hash_defined
5768 || hash->root.root.type == bfd_link_hash_defweak)
5769 {
a06ea964
NC
5770 sym_sec = hash->root.root.u.def.section;
5771 sym_value = hash->root.root.u.def.value;
5772 /* For a destination in a shared library,
5773 use the PLT stub as target address to
5774 decide whether a branch stub is
5775 needed. */
5776 if (globals->root.splt != NULL && hash != NULL
5777 && hash->root.plt.offset != (bfd_vma) - 1)
5778 {
5779 sym_sec = globals->root.splt;
5780 sym_value = hash->root.plt.offset;
5781 if (sym_sec->output_section != NULL)
5782 destination = (sym_value
5783 + sym_sec->output_offset
5784 +
5785 sym_sec->output_section->vma);
5786 }
5787 else if (sym_sec->output_section != NULL)
5788 destination = (sym_value + irela->r_addend
5789 + sym_sec->output_offset
5790 + sym_sec->output_section->vma);
5791 }
5792 else if (hash->root.root.type == bfd_link_hash_undefined
5793 || (hash->root.root.type
5794 == bfd_link_hash_undefweak))
5795 {
5796 /* For a shared library, use the PLT stub as
5797 target address to decide whether a long
5798 branch stub is needed.
5799 For absolute code, they cannot be handled. */
a06ea964
NC
5800
5801 if (globals->root.splt != NULL && hash != NULL
5802 && hash->root.plt.offset != (bfd_vma) - 1)
5803 {
5804 sym_sec = globals->root.splt;
5805 sym_value = hash->root.plt.offset;
5806 if (sym_sec->output_section != NULL)
5807 destination = (sym_value
5808 + sym_sec->output_offset
5809 +
5810 sym_sec->output_section->vma);
5811 }
5812 else
5813 continue;
5814 }
5815 else
5816 {
5817 bfd_set_error (bfd_error_bad_value);
5818 goto error_ret_free_internal;
5819 }
5820 st_type = ELF_ST_TYPE (hash->root.type);
5821 sym_name = hash->root.root.root.string;
5822 }
5823
5824 /* Determine what (if any) linker stub is needed. */
50e192f0
SP
5825 if (stub_type == aarch64_stub_none)
5826 stub_type = aarch64_type_of_stub (section, irela, sym_sec,
5827 st_type, destination);
5828
a06ea964
NC
5829 if (stub_type == aarch64_stub_none)
5830 continue;
5831
5832 /* Support for grouping stub sections. */
5833 id_sec = htab->stub_group[section->id].link_sec;
5834
5835 /* Get the name of this stub. */
cec5225b 5836 stub_name = elfNN_aarch64_stub_name (id_sec, sym_sec, hash,
50e192f0 5837 irela, stub_type);
a06ea964
NC
5838 if (!stub_name)
5839 goto error_ret_free_internal;
5840
5841 stub_entry =
5842 aarch64_stub_hash_lookup (&htab->stub_hash_table,
5843 stub_name, FALSE, FALSE);
5844 if (stub_entry != NULL)
5845 {
5846 /* The proper stub has already been created. */
5847 free (stub_name);
3da64fe4
RA
5848 /* Always update this stub's target since it may have
5849 changed after layout. */
5850 stub_entry->target_value = sym_value + irela->r_addend;
50e192f0
SP
5851
5852 /* Set LSB for A64 to C64 branch. */
5853 if (branch_to_c64)
5854 stub_entry->target_value |= 1;
5855
a06ea964
NC
5856 continue;
5857 }
5858
ef857521
MS
5859 stub_entry = _bfd_aarch64_add_stub_entry_in_group
5860 (stub_name, section, htab);
a06ea964
NC
5861 if (stub_entry == NULL)
5862 {
5863 free (stub_name);
5864 goto error_ret_free_internal;
5865 }
5866
2f340668 5867 stub_entry->target_value = sym_value + irela->r_addend;
50e192f0
SP
5868 /* Set LSB for A64 to C64 branch. */
5869 if (branch_to_c64)
5870 stub_entry->target_value |= 1;
5871
a06ea964
NC
5872 stub_entry->target_section = sym_sec;
5873 stub_entry->stub_type = stub_type;
5874 stub_entry->h = hash;
5875 stub_entry->st_type = st_type;
5876
50e192f0
SP
5877 suffix = aarch64_lookup_stub_type_suffix (stub_type);
5878
a06ea964
NC
5879 if (sym_name == NULL)
5880 sym_name = "unnamed";
50e192f0
SP
5881 len = (sizeof (STUB_ENTRY_NAME) + strlen (sym_name)
5882 + strlen (suffix));
a06ea964
NC
5883 stub_entry->output_name = bfd_alloc (htab->stub_bfd, len);
5884 if (stub_entry->output_name == NULL)
5885 {
5886 free (stub_name);
5887 goto error_ret_free_internal;
5888 }
5889
5890 snprintf (stub_entry->output_name, len, STUB_ENTRY_NAME,
50e192f0 5891 sym_name, suffix);
a06ea964
NC
5892
5893 stub_changed = TRUE;
5894 }
5895
5896 /* We're done with the internal relocs, free them. */
5897 if (elf_section_data (section)->relocs == NULL)
5898 free (internal_relocs);
5899 }
5900 }
5901
5902 if (!stub_changed)
5903 break;
5904
13f622ec 5905 _bfd_aarch64_resize_stubs (htab);
a06ea964
NC
5906
5907 /* Ask the linker to do its stuff. */
5908 (*htab->layout_sections_again) ();
5909 stub_changed = FALSE;
5910 }
5911
5912 return TRUE;
5913
dc1e8a47 5914 error_ret_free_local:
a06ea964
NC
5915 return FALSE;
5916}
5917
8504495a
AC
5918bfd_boolean
5919elfNN_aarch64_size_stubs (bfd *output_bfd,
5920 bfd *stub_bfd,
5921 struct bfd_link_info *info,
5922 bfd_signed_vma group_size,
5923 asection * (*add_stub_section) (const char *,
5924 asection *))
5925{
5926 bfd_size_type stub_group_size;
5927 bfd_boolean stubs_always_before_branch;
5928 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
5929
5930 /* Propagate mach to stub bfd, because it may not have been
5931 finalized when we created stub_bfd. */
5932 bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
5933 bfd_get_mach (output_bfd));
5934
5935 /* Stash our params away. */
5936 htab->stub_bfd = stub_bfd;
5937 htab->add_stub_section = add_stub_section;
5938
5939 stubs_always_before_branch = group_size < 0;
5940 if (group_size < 0)
5941 stub_group_size = -group_size;
5942 else
5943 stub_group_size = group_size;
5944
5945 if (stub_group_size == 1)
5946 {
5947 /* Default values. */
5948 /* AArch64 branch range is +-128MB. The value used is 1MB less. */
5949 stub_group_size = 127 * 1024 * 1024;
5950 }
5951
5952 group_sections (htab, stub_group_size, stubs_always_before_branch);
5953
5954 (*htab->layout_sections_again) ();
5955
5956 /* The layout changes we do in aarch64_size_stubs may mean we need to
5957 adjust the PCC base alignment (or individual section bounds) again,
5958 and vice versa. Here we run both in a loop until we no longer make
5959 further adjustments. */
5960 enum { max_tries = 10 };
5961 int tries;
5962 for (tries = 0; tries < max_tries; tries++)
5963 {
5964 if (!aarch64_size_stubs (output_bfd, info))
5965 return FALSE;
5966
5967 bfd_boolean changed_layout = FALSE;
5968 if (!c64_resize_sections (output_bfd, info, &changed_layout))
5969 return FALSE;
5970
5971 if (!changed_layout)
5972 break;
5973 }
5974
5975 if (tries >= max_tries)
5976 {
5977 _bfd_error_handler (_("looping in elfNN_aarch64_size_stubs"));
5978 abort ();
5979 }
5980
5981 if (pcc_high_sec)
5982 {
5983 /* Now that we've sized everything up, add any additional
5984 padding *after* the last section needed to get exact
5985 PCC bounds. */
5986 bfd_vma current_high = pcc_high_sec->vma + pcc_high_sec->size;
5987 bfd_vma desired_high = pcc_high;
5988 BFD_ASSERT (desired_high >= current_high);
5989 bfd_vma padding = desired_high - current_high;
5990 if (padding)
5991 {
5992 htab->c64_pad_after_section (pcc_high_sec, padding);
5993 htab->layout_sections_again ();
5994 }
5995 }
5996
5997 return TRUE;
5998}
5999
6000
a06ea964
NC
6001/* Build all the stubs associated with the current output file. The
6002 stubs are kept in a hash table attached to the main linker hash
6003 table. We also set up the .plt entries for statically linked PIC
6004 functions here. This function is called via aarch64_elf_finish in the
6005 linker. */
6006
6007bfd_boolean
cec5225b 6008elfNN_aarch64_build_stubs (struct bfd_link_info *info)
a06ea964
NC
6009{
6010 asection *stub_sec;
6011 struct bfd_hash_table *table;
cec5225b 6012 struct elf_aarch64_link_hash_table *htab;
a06ea964 6013
cec5225b 6014 htab = elf_aarch64_hash_table (info);
a06ea964
NC
6015
6016 for (stub_sec = htab->stub_bfd->sections;
6017 stub_sec != NULL; stub_sec = stub_sec->next)
6018 {
6019 bfd_size_type size;
6020
6021 /* Ignore non-stub sections. */
6022 if (!strstr (stub_sec->name, STUB_SUFFIX))
6023 continue;
6024
6025 /* Allocate memory to hold the linker stubs. */
6026 size = stub_sec->size;
6027 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
6028 if (stub_sec->contents == NULL && size != 0)
6029 return FALSE;
6030 stub_sec->size = 0;
61865519 6031
9a2ebffd
JW
6032 /* Add a branch around the stub section, and a nop, to keep it 8 byte
6033 aligned, as long branch stubs contain a 64-bit address. */
61865519 6034 bfd_putl32 (0x14000000 | (size >> 2), stub_sec->contents);
9a2ebffd
JW
6035 bfd_putl32 (INSN_NOP, stub_sec->contents + 4);
6036 stub_sec->size += 8;
a06ea964
NC
6037 }
6038
6039 /* Build the stubs as directed by the stub hash table. */
6040 table = &htab->stub_hash_table;
50e192f0
SP
6041
6042 bfd_error_type save_error = bfd_get_error ();
6043 bfd_set_error (bfd_error_no_error);
a06ea964
NC
6044 bfd_hash_traverse (table, aarch64_build_one_stub, info);
6045
50e192f0
SP
6046 if (bfd_get_error () != bfd_error_no_error)
6047 return FALSE;
6048
6049 bfd_set_error (save_error);
6050
a06ea964
NC
6051 return TRUE;
6052}
6053
6054
6055/* Add an entry to the code/data map for section SEC. */
6056
6057static void
f0070c1e
SP
6058elfNN_aarch64_section_map_add (bfd *abfd, asection *sec, char type,
6059 bfd_vma vma)
a06ea964
NC
6060{
6061 struct _aarch64_elf_section_data *sec_data =
cec5225b 6062 elf_aarch64_section_data (sec);
a06ea964
NC
6063 unsigned int newidx;
6064
f0070c1e
SP
6065 /* The aarch64 section hook was not called for this section. */
6066 if (!sec_data->elf.is_target_section_data)
6067 {
6068 struct _aarch64_elf_section_data *newdata =
6069 bfd_zalloc (abfd, sizeof (*newdata));
6070
6071 if (newdata == NULL)
6072 return;
6073
6074 newdata->elf = sec_data->elf;
6075 newdata->elf.is_target_section_data = TRUE;
6076 free (sec_data);
6077 sec->used_by_bfd = sec_data = newdata;
6078 }
6079
a06ea964
NC
6080 if (sec_data->map == NULL)
6081 {
cec5225b 6082 sec_data->map = bfd_malloc (sizeof (elf_aarch64_section_map));
a06ea964
NC
6083 sec_data->mapcount = 0;
6084 sec_data->mapsize = 1;
6085 }
6086
6087 newidx = sec_data->mapcount++;
6088
6089 if (sec_data->mapcount > sec_data->mapsize)
6090 {
6091 sec_data->mapsize *= 2;
6092 sec_data->map = bfd_realloc_or_free
cec5225b 6093 (sec_data->map, sec_data->mapsize * sizeof (elf_aarch64_section_map));
a06ea964
NC
6094 }
6095
6096 if (sec_data->map)
6097 {
6098 sec_data->map[newidx].vma = vma;
6099 sec_data->map[newidx].type = type;
6100 }
6101}
6102
6103
6104/* Initialise maps of insn/data for input BFDs. */
6105void
7ff36d1a 6106bfd_elfNN_aarch64_init_maps (bfd *abfd, struct bfd_link_info *info)
a06ea964
NC
6107{
6108 Elf_Internal_Sym *isymbuf;
6109 Elf_Internal_Shdr *hdr;
6110 unsigned int i, localsyms;
6111
6112 /* Make sure that we are dealing with an AArch64 elf binary. */
6113 if (!is_aarch64_elf (abfd))
6114 return;
6115
f0070c1e
SP
6116 if (elf_aarch64_tdata (abfd)->secmaps_initialised)
6117 return;
6118
a06ea964 6119 if ((abfd->flags & DYNAMIC) != 0)
68fcca92 6120 return;
a06ea964
NC
6121
6122 hdr = &elf_symtab_hdr (abfd);
6123 localsyms = hdr->sh_info;
6124
6125 /* Obtain a buffer full of symbols for this BFD. The hdr->sh_info field
6126 should contain the number of local symbols, which should come before any
6127 global symbols. Mapping symbols are always local. */
6128 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, localsyms, 0, NULL, NULL, NULL);
6129
6130 /* No internal symbols read? Skip this BFD. */
6131 if (isymbuf == NULL)
6132 return;
6133
7ff36d1a
SP
6134 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table ((info));
6135
a06ea964
NC
6136 for (i = 0; i < localsyms; i++)
6137 {
6138 Elf_Internal_Sym *isym = &isymbuf[i];
6139 asection *sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6140 const char *name;
6141
6142 if (sec != NULL && ELF_ST_BIND (isym->st_info) == STB_LOCAL)
6143 {
6144 name = bfd_elf_string_from_elf_section (abfd,
6145 hdr->sh_link,
6146 isym->st_name);
6147
6148 if (bfd_is_aarch64_special_symbol_name
6149 (name, BFD_AARCH64_SPECIAL_SYM_TYPE_MAP))
7ff36d1a
SP
6150 {
6151 elfNN_aarch64_section_map_add (abfd, sec, name[1],
6152 isym->st_value);
6153 if (!htab->c64_output && name[1] == 'c')
6154 htab->c64_output = TRUE;
6155 }
a06ea964
NC
6156 }
6157 }
f0070c1e 6158 elf_aarch64_tdata (abfd)->secmaps_initialised = TRUE;
a06ea964
NC
6159}
6160
37c18eed
SD
6161static void
6162setup_plt_values (struct bfd_link_info *link_info,
6163 aarch64_plt_type plt_type)
6164{
6165 struct elf_aarch64_link_hash_table *globals;
6166 globals = elf_aarch64_hash_table (link_info);
6167
e19e9199
SP
6168 /* Set up plt stubs in case we need C64 PLT. Override BTI/PAC since they're
6169 not compatible. PLT stub sizes are the same as the default ones. */
6170 if (globals->c64_rel)
6171 {
6172 if (plt_type != PLT_NORMAL)
6173 _bfd_error_handler
6174 (_("ignoring C64-incompatible extensions: %s"),
6175 (plt_type == PLT_BTI_PAC ? "BTI, PAC"
6176 : plt_type == PLT_BTI ? "BTI" : "PAC"));
6177
6178 globals->plt0_entry = elfNN_c64_small_plt0_entry;
6179 globals->plt_entry = elfNN_c64_small_plt_entry;
6180 return;
6181 }
6182
1dbade74
SD
6183 if (plt_type == PLT_BTI_PAC)
6184 {
68bb0359 6185 globals->plt0_entry = elfNN_aarch64_small_plt0_bti_entry;
1dbade74
SD
6186
6187 /* Only in ET_EXEC we need PLTn with BTI. */
6188 if (bfd_link_pde (link_info))
6189 {
6190 globals->plt_entry_size = PLT_BTI_PAC_SMALL_ENTRY_SIZE;
6191 globals->plt_entry = elfNN_aarch64_small_plt_bti_pac_entry;
6192 }
6193 else
6194 {
6195 globals->plt_entry_size = PLT_PAC_SMALL_ENTRY_SIZE;
6196 globals->plt_entry = elfNN_aarch64_small_plt_pac_entry;
6197 }
6198 }
6199 else if (plt_type == PLT_BTI)
37c18eed 6200 {
37c18eed 6201 globals->plt0_entry = elfNN_aarch64_small_plt0_bti_entry;
37c18eed
SD
6202
6203 /* Only in ET_EXEC we need PLTn with BTI. */
6204 if (bfd_link_pde (link_info))
6205 {
6206 globals->plt_entry_size = PLT_BTI_SMALL_ENTRY_SIZE;
6207 globals->plt_entry = elfNN_aarch64_small_plt_bti_entry;
6208 }
6209 }
1dbade74
SD
6210 else if (plt_type == PLT_PAC)
6211 {
1dbade74
SD
6212 globals->plt_entry_size = PLT_PAC_SMALL_ENTRY_SIZE;
6213 globals->plt_entry = elfNN_aarch64_small_plt_pac_entry;
6214 }
37c18eed
SD
6215}
6216
a06ea964
NC
6217/* Set option values needed during linking. */
6218void
cec5225b 6219bfd_elfNN_aarch64_set_options (struct bfd *output_bfd,
a06ea964
NC
6220 struct bfd_link_info *link_info,
6221 int no_enum_warn,
68fcca92 6222 int no_wchar_warn, int pic_veneer,
4106101c 6223 int fix_erratum_835769,
739b5c9c 6224 erratum_84319_opts fix_erratum_843419,
37c18eed
SD
6225 int no_apply_dynamic_relocs,
6226 aarch64_bti_pac_info bp_info)
a06ea964 6227{
cec5225b 6228 struct elf_aarch64_link_hash_table *globals;
a06ea964 6229
cec5225b 6230 globals = elf_aarch64_hash_table (link_info);
a06ea964 6231 globals->pic_veneer = pic_veneer;
68fcca92 6232 globals->fix_erratum_835769 = fix_erratum_835769;
739b5c9c
TC
6233 /* If the default options are used, then ERRAT_ADR will be set by default
6234 which will enable the ADRP->ADR workaround for the erratum 843419
6235 workaround. */
4106101c 6236 globals->fix_erratum_843419 = fix_erratum_843419;
1f56df9d 6237 globals->no_apply_dynamic_relocs = no_apply_dynamic_relocs;
a1bdea65 6238 globals->c64_rel = 0;
a06ea964
NC
6239
6240 BFD_ASSERT (is_aarch64_elf (output_bfd));
6241 elf_aarch64_tdata (output_bfd)->no_enum_size_warning = no_enum_warn;
6242 elf_aarch64_tdata (output_bfd)->no_wchar_size_warning = no_wchar_warn;
37c18eed
SD
6243
6244 switch (bp_info.bti_type)
6245 {
6246 case BTI_WARN:
6247 elf_aarch64_tdata (output_bfd)->no_bti_warn = 0;
6248 elf_aarch64_tdata (output_bfd)->gnu_and_prop
6249 |= GNU_PROPERTY_AARCH64_FEATURE_1_BTI;
6250 break;
6251
6252 default:
6253 break;
6254 }
6255 elf_aarch64_tdata (output_bfd)->plt_type = bp_info.plt_type;
f0070c1e 6256 elf_aarch64_tdata (output_bfd)->secmaps_initialised = FALSE;
a06ea964
NC
6257}
6258
a06ea964
NC
6259static bfd_vma
6260aarch64_calculate_got_entry_vma (struct elf_link_hash_entry *h,
cec5225b 6261 struct elf_aarch64_link_hash_table
a06ea964
NC
6262 *globals, struct bfd_link_info *info,
6263 bfd_vma value, bfd *output_bfd,
6264 bfd_boolean *unresolved_reloc_p)
6265{
6266 bfd_vma off = (bfd_vma) - 1;
6267 asection *basegot = globals->root.sgot;
6268 bfd_boolean dyn = globals->root.dynamic_sections_created;
6269
6270 if (h != NULL)
6271 {
a6bb11b2 6272 BFD_ASSERT (basegot != NULL);
a06ea964
NC
6273 off = h->got.offset;
6274 BFD_ASSERT (off != (bfd_vma) - 1);
0e1862bb
L
6275 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
6276 || (bfd_link_pic (info)
a06ea964
NC
6277 && SYMBOL_REFERENCES_LOCAL (info, h))
6278 || (ELF_ST_VISIBILITY (h->other)
6279 && h->root.type == bfd_link_hash_undefweak))
6280 {
6281 /* This is actually a static link, or it is a -Bsymbolic link
6282 and the symbol is defined locally. We must initialize this
6283 entry in the global offset table. Since the offset must
a6bb11b2
YZ
6284 always be a multiple of 8 (4 in the case of ILP32), we use
6285 the least significant bit to record whether we have
6286 initialized it already.
a06ea964
NC
6287 When doing a dynamic link, we create a .rel(a).got relocation
6288 entry to initialize the value. This is done in the
6289 finish_dynamic_symbol routine. */
6290 if ((off & 1) != 0)
6291 off &= ~1;
6292 else
6293 {
cec5225b 6294 bfd_put_NN (output_bfd, value, basegot->contents + off);
a06ea964
NC
6295 h->got.offset |= 1;
6296 }
6297 }
6298 else
6299 *unresolved_reloc_p = FALSE;
6300
6301 off = off + basegot->output_section->vma + basegot->output_offset;
6302 }
6303
6304 return off;
6305}
6306
6307/* Change R_TYPE to a more efficient access model where possible,
6308 return the new reloc type. */
6309
a6bb11b2
YZ
6310static bfd_reloc_code_real_type
6311aarch64_tls_transition_without_check (bfd_reloc_code_real_type r_type,
4ca9b406
SP
6312 struct bfd_link_info *info,
6313 struct elf_link_hash_entry *h,
ece841e8 6314 bfd_boolean *requires_c64_tls_stub)
a06ea964 6315{
8a4f8507 6316 bfd_boolean local_exec = (bfd_link_executable (info)
1408e112 6317 && TLS_SYMBOL_REFERENCES_LOCAL (info, h));
a6bb11b2 6318
a06ea964
NC
6319 switch (r_type)
6320 {
a6bb11b2 6321 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
ce336788 6322 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
8a4f8507 6323 return (local_exec
a6bb11b2
YZ
6324 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1
6325 : BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21);
6326
389b8029 6327 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
8a4f8507 6328 return (local_exec
389b8029
MS
6329 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
6330 : r_type);
6331
1ada945d 6332 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
8a4f8507 6333 return (local_exec
1ada945d
MS
6334 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1
6335 : BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19);
6336
0484b454 6337 case BFD_RELOC_AARCH64_TLSDESC_LDR:
8a4f8507 6338 return (local_exec
0484b454
RL
6339 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
6340 : BFD_RELOC_AARCH64_NONE);
6341
6342 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
8a4f8507 6343 return (local_exec
0484b454
RL
6344 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC
6345 : BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC);
6346
6347 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
8a4f8507 6348 return (local_exec
0484b454
RL
6349 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2
6350 : BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1);
6351
a6bb11b2 6352 case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
ce336788 6353 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
8a4f8507 6354 return (local_exec
a6bb11b2
YZ
6355 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC
6356 : BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC);
6357
ece841e8
MM
6358 case BFD_RELOC_MORELLO_TLSIE_ADR_GOTTPREL_PAGE20:
6359 if (!local_exec)
6360 return r_type;
6361 *requires_c64_tls_stub = TRUE;
6362 return BFD_RELOC_MORELLO_ADR_HI20_PCREL;
6363
6364 case BFD_RELOC_MORELLO_TLSIE_ADD_LO12:
6365 if (!local_exec)
6366 return r_type;
6367 *requires_c64_tls_stub = TRUE;
6368 return BFD_RELOC_AARCH64_ADD_LO12;
6369
a6bb11b2 6370 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
8a4f8507 6371 return local_exec ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1 : r_type;
a6bb11b2
YZ
6372
6373 case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
8a4f8507 6374 return local_exec ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC : r_type;
a6bb11b2 6375
043bf05a
MS
6376 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
6377 return r_type;
6378
3c12b054 6379 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
8a4f8507 6380 return (local_exec
3c12b054
MS
6381 ? BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12
6382 : BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19);
6383
ece841e8
MM
6384 case BFD_RELOC_MORELLO_TLSDESC_ADR_PAGE20:
6385 if (local_exec)
6386 {
6387 *requires_c64_tls_stub = TRUE;
6388 return BFD_RELOC_MORELLO_ADR_HI20_PCREL;
6389 }
6390 return BFD_RELOC_MORELLO_TLSIE_ADR_GOTTPREL_PAGE20;
6391
6392 case BFD_RELOC_MORELLO_TLSDESC_LD128_LO12:
6393 if (local_exec)
6394 {
6395 *requires_c64_tls_stub = TRUE;
6396 return BFD_RELOC_AARCH64_ADD_LO12;
6397 }
6398 return BFD_RELOC_MORELLO_TLSIE_ADD_LO12;
6399
4ca9b406 6400 case BFD_RELOC_MORELLO_TLSDESC_CALL:
ece841e8
MM
6401 /* Instructions with this relocation will be fully resolved during the
6402 transition into an add and scbnds pair. */
6403 return BFD_RELOC_AARCH64_NONE;
4ca9b406 6404
f955cccf 6405 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
4ca9b406 6406 case BFD_RELOC_AARCH64_TLSDESC_ADD:
a6bb11b2 6407 case BFD_RELOC_AARCH64_TLSDESC_CALL:
4ca9b406 6408 /* Instructions with these relocations will be fully resolved during the
ece841e8 6409 transition into either a NOP in the A64 case or an ldp in C64. */
a6bb11b2
YZ
6410 return BFD_RELOC_AARCH64_NONE;
6411
259364ad
JW
6412 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
6413 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
6414 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
8a4f8507 6415 return local_exec ? BFD_RELOC_AARCH64_NONE : r_type;
259364ad 6416
ac734732
RL
6417#if ARCH_SIZE == 64
6418 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
8a4f8507 6419 return local_exec
ac734732
RL
6420 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC
6421 : BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC;
6422
6423 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
8a4f8507 6424 return local_exec
ac734732
RL
6425 ? BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2
6426 : BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1;
6427#endif
6428
a6bb11b2
YZ
6429 default:
6430 break;
a06ea964
NC
6431 }
6432
6433 return r_type;
6434}
6435
6436static unsigned int
a6bb11b2 6437aarch64_reloc_got_type (bfd_reloc_code_real_type r_type)
a06ea964
NC
6438{
6439 switch (r_type)
6440 {
a6bb11b2
YZ
6441 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
6442 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
7018c030 6443 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
ce336788 6444 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
a2e1db00 6445 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
99ad26cb 6446 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
ce336788 6447 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
dc8008f5 6448 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
74a1bfe1 6449 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
a06ea964
NC
6450 return GOT_NORMAL;
6451
a1bdea65
SP
6452 case BFD_RELOC_MORELLO_ADR_GOT_PAGE:
6453 case BFD_RELOC_MORELLO_LD128_GOT_LO12_NC:
60d18fc3 6454 return GOT_NORMAL;
a1bdea65 6455
ce336788 6456 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
a6bb11b2 6457 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
3c12b054 6458 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
7ba7cfe4 6459 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
94facae3 6460 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
73f925cc 6461 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
f69e4920 6462 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
77a69ff8 6463 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
a06ea964
NC
6464 return GOT_TLS_GD;
6465
4ca9b406
SP
6466 case BFD_RELOC_MORELLO_TLSDESC_ADR_PAGE20:
6467 case BFD_RELOC_MORELLO_TLSDESC_CALL:
6468 case BFD_RELOC_MORELLO_TLSDESC_LD128_LO12:
0484b454 6469 case BFD_RELOC_AARCH64_TLSDESC_ADD:
f955cccf 6470 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
a6bb11b2 6471 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
389b8029 6472 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
a6bb11b2 6473 case BFD_RELOC_AARCH64_TLSDESC_CALL:
a6bb11b2 6474 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
f955cccf 6475 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12:
1ada945d 6476 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
0484b454
RL
6477 case BFD_RELOC_AARCH64_TLSDESC_LDR:
6478 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
6479 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
a06ea964
NC
6480 return GOT_TLSDESC_GD;
6481
1408e112
MM
6482 case BFD_RELOC_MORELLO_TLSIE_ADR_GOTTPREL_PAGE20:
6483 case BFD_RELOC_MORELLO_TLSIE_ADD_LO12:
a6bb11b2 6484 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
a6bb11b2 6485 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
ce336788 6486 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
043bf05a 6487 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
3b957e5b
RL
6488 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
6489 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
a06ea964
NC
6490 return GOT_TLS_IE;
6491
a6bb11b2
YZ
6492 default:
6493 break;
a06ea964
NC
6494 }
6495 return GOT_UNKNOWN;
6496}
6497
6498static bfd_boolean
6499aarch64_can_relax_tls (bfd *input_bfd,
6500 struct bfd_link_info *info,
4ca9b406 6501 const Elf_Internal_Rela *rel,
a06ea964
NC
6502 struct elf_link_hash_entry *h,
6503 unsigned long r_symndx)
6504{
6505 unsigned int symbol_got_type;
6506 unsigned int reloc_got_type;
6507
4ca9b406
SP
6508 bfd_reloc_code_real_type bfd_r_type
6509 = elfNN_aarch64_bfd_reloc_from_type (input_bfd,
6510 ELFNN_R_TYPE (rel->r_info));
6511
6512 if (! IS_AARCH64_TLS_RELAX_RELOC (bfd_r_type))
a06ea964
NC
6513 return FALSE;
6514
cec5225b 6515 symbol_got_type = elfNN_aarch64_symbol_got_type (h, input_bfd, r_symndx);
4ca9b406 6516 reloc_got_type = aarch64_reloc_got_type (bfd_r_type);
a06ea964
NC
6517
6518 if (symbol_got_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (reloc_got_type))
6519 return TRUE;
6520
6dda7875 6521 if (!bfd_link_executable (info))
a06ea964
NC
6522 return FALSE;
6523
6524 if (h && h->root.type == bfd_link_hash_undefweak)
6525 return FALSE;
6526
6527 return TRUE;
6528}
6529
a6bb11b2
YZ
6530/* Given the relocation code R_TYPE, return the relaxed bfd reloc
6531 enumerator. */
6532
6533static bfd_reloc_code_real_type
a06ea964
NC
6534aarch64_tls_transition (bfd *input_bfd,
6535 struct bfd_link_info *info,
4ca9b406 6536 const Elf_Internal_Rela *rel,
a06ea964 6537 struct elf_link_hash_entry *h,
ece841e8
MM
6538 unsigned long r_symndx,
6539 bfd_boolean *requires_c64_tls_stub)
a06ea964 6540{
ece841e8
MM
6541 /* Initialisation done here. The set to TRUE is done in
6542 aarch64_tls_transition_without_check if necessary. */
6543 *requires_c64_tls_stub = FALSE;
a6bb11b2 6544 bfd_reloc_code_real_type bfd_r_type
4ca9b406
SP
6545 = elfNN_aarch64_bfd_reloc_from_type (input_bfd,
6546 ELFNN_R_TYPE (rel->r_info));
6547
6548 if (! aarch64_can_relax_tls (input_bfd, info, rel, h, r_symndx))
6549 return bfd_r_type;
a06ea964 6550
4ca9b406 6551 return aarch64_tls_transition_without_check (bfd_r_type, info, h,
ece841e8 6552 requires_c64_tls_stub);
a06ea964
NC
6553}
6554
6555/* Return the base VMA address which should be subtracted from real addresses
a6bb11b2 6556 when resolving R_AARCH64_TLS_DTPREL relocation. */
a06ea964
NC
6557
6558static bfd_vma
6559dtpoff_base (struct bfd_link_info *info)
6560{
6561 /* If tls_sec is NULL, we should have signalled an error already. */
6562 BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
6563 return elf_hash_table (info)->tls_sec->vma;
6564}
6565
a06ea964
NC
6566/* Return the base VMA address which should be subtracted from real addresses
6567 when resolving R_AARCH64_TLS_GOTTPREL64 relocations. */
6568
6569static bfd_vma
6570tpoff_base (struct bfd_link_info *info)
6571{
6572 struct elf_link_hash_table *htab = elf_hash_table (info);
6573
6574 /* If tls_sec is NULL, we should have signalled an error already. */
ac21917f 6575 BFD_ASSERT (htab->tls_sec != NULL);
a06ea964 6576
bf3eaebd 6577 bfd_vma base = align_power ((bfd_vma) TCB_SIZE (info->output_bfd),
a06ea964
NC
6578 htab->tls_sec->alignment_power);
6579 return htab->tls_sec->vma - base;
6580}
6581
6582static bfd_vma *
6583symbol_got_offset_ref (bfd *input_bfd, struct elf_link_hash_entry *h,
6584 unsigned long r_symndx)
6585{
6586 /* Calculate the address of the GOT entry for symbol
6587 referred to in h. */
6588 if (h != NULL)
6589 return &h->got.offset;
6590 else
6591 {
6592 /* local symbol */
6593 struct elf_aarch64_local_symbol *l;
6594
cec5225b 6595 l = elf_aarch64_locals (input_bfd);
a06ea964
NC
6596 return &l[r_symndx].got_offset;
6597 }
6598}
6599
6600static void
6601symbol_got_offset_mark (bfd *input_bfd, struct elf_link_hash_entry *h,
6602 unsigned long r_symndx)
6603{
6604 bfd_vma *p;
6605 p = symbol_got_offset_ref (input_bfd, h, r_symndx);
6606 *p |= 1;
6607}
6608
6609static int
6610symbol_got_offset_mark_p (bfd *input_bfd, struct elf_link_hash_entry *h,
6611 unsigned long r_symndx)
6612{
6613 bfd_vma value;
6614 value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
6615 return value & 1;
6616}
6617
6618static bfd_vma
6619symbol_got_offset (bfd *input_bfd, struct elf_link_hash_entry *h,
6620 unsigned long r_symndx)
6621{
6622 bfd_vma value;
6623 value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
6624 value &= ~1;
6625 return value;
6626}
6627
6628static bfd_vma *
6629symbol_tlsdesc_got_offset_ref (bfd *input_bfd, struct elf_link_hash_entry *h,
6630 unsigned long r_symndx)
6631{
6632 /* Calculate the address of the GOT entry for symbol
6633 referred to in h. */
6634 if (h != NULL)
6635 {
cec5225b
YZ
6636 struct elf_aarch64_link_hash_entry *eh;
6637 eh = (struct elf_aarch64_link_hash_entry *) h;
a06ea964
NC
6638 return &eh->tlsdesc_got_jump_table_offset;
6639 }
6640 else
6641 {
6642 /* local symbol */
6643 struct elf_aarch64_local_symbol *l;
6644
cec5225b 6645 l = elf_aarch64_locals (input_bfd);
a06ea964
NC
6646 return &l[r_symndx].tlsdesc_got_jump_table_offset;
6647 }
6648}
6649
6650static void
6651symbol_tlsdesc_got_offset_mark (bfd *input_bfd, struct elf_link_hash_entry *h,
6652 unsigned long r_symndx)
6653{
6654 bfd_vma *p;
6655 p = symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
6656 *p |= 1;
6657}
6658
6659static int
6660symbol_tlsdesc_got_offset_mark_p (bfd *input_bfd,
6661 struct elf_link_hash_entry *h,
6662 unsigned long r_symndx)
6663{
6664 bfd_vma value;
6665 value = * symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
6666 return value & 1;
6667}
6668
6669static bfd_vma
6670symbol_tlsdesc_got_offset (bfd *input_bfd, struct elf_link_hash_entry *h,
6671 unsigned long r_symndx)
6672{
6673 bfd_vma value;
6674 value = * symbol_tlsdesc_got_offset_ref (input_bfd, h, r_symndx);
6675 value &= ~1;
6676 return value;
6677}
6678
68fcca92
JW
6679/* Data for make_branch_to_erratum_835769_stub(). */
6680
6681struct erratum_835769_branch_to_stub_data
6682{
4106101c 6683 struct bfd_link_info *info;
68fcca92
JW
6684 asection *output_section;
6685 bfd_byte *contents;
6686};
6687
6688/* Helper to insert branches to erratum 835769 stubs in the right
6689 places for a particular section. */
6690
6691static bfd_boolean
6692make_branch_to_erratum_835769_stub (struct bfd_hash_entry *gen_entry,
6693 void *in_arg)
6694{
6695 struct elf_aarch64_stub_hash_entry *stub_entry;
6696 struct erratum_835769_branch_to_stub_data *data;
6697 bfd_byte *contents;
6698 unsigned long branch_insn = 0;
6699 bfd_vma veneered_insn_loc, veneer_entry_loc;
6700 bfd_signed_vma branch_offset;
6701 unsigned int target;
6702 bfd *abfd;
6703
6704 stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
6705 data = (struct erratum_835769_branch_to_stub_data *) in_arg;
6706
6707 if (stub_entry->target_section != data->output_section
6708 || stub_entry->stub_type != aarch64_stub_erratum_835769_veneer)
6709 return TRUE;
6710
6711 contents = data->contents;
6712 veneered_insn_loc = stub_entry->target_section->output_section->vma
6713 + stub_entry->target_section->output_offset
6714 + stub_entry->target_value;
6715 veneer_entry_loc = stub_entry->stub_sec->output_section->vma
6716 + stub_entry->stub_sec->output_offset
6717 + stub_entry->stub_offset;
6718 branch_offset = veneer_entry_loc - veneered_insn_loc;
6719
6720 abfd = stub_entry->target_section->owner;
6721 if (!aarch64_valid_branch_p (veneer_entry_loc, veneered_insn_loc))
4eca0228 6722 _bfd_error_handler
90b6238f 6723 (_("%pB: error: erratum 835769 stub out "
4eca0228 6724 "of range (input file too large)"), abfd);
68fcca92
JW
6725
6726 target = stub_entry->target_value;
6727 branch_insn = 0x14000000;
6728 branch_offset >>= 2;
6729 branch_offset &= 0x3ffffff;
6730 branch_insn |= branch_offset;
6731 bfd_putl32 (branch_insn, &contents[target]);
6732
6733 return TRUE;
6734}
6735
4106101c
MS
6736
6737static bfd_boolean
6738_bfd_aarch64_erratum_843419_branch_to_stub (struct bfd_hash_entry *gen_entry,
6739 void *in_arg)
6740{
6741 struct elf_aarch64_stub_hash_entry *stub_entry
6742 = (struct elf_aarch64_stub_hash_entry *) gen_entry;
6743 struct erratum_835769_branch_to_stub_data *data
6744 = (struct erratum_835769_branch_to_stub_data *) in_arg;
6745 struct bfd_link_info *info;
6746 struct elf_aarch64_link_hash_table *htab;
6747 bfd_byte *contents;
6748 asection *section;
6749 bfd *abfd;
6750 bfd_vma place;
6751 uint32_t insn;
6752
6753 info = data->info;
6754 contents = data->contents;
6755 section = data->output_section;
6756
6757 htab = elf_aarch64_hash_table (info);
6758
6759 if (stub_entry->target_section != section
6760 || stub_entry->stub_type != aarch64_stub_erratum_843419_veneer)
6761 return TRUE;
6762
739b5c9c
TC
6763 BFD_ASSERT (((htab->fix_erratum_843419 & ERRAT_ADRP) && stub_entry->stub_sec)
6764 || (htab->fix_erratum_843419 & ERRAT_ADR));
6765
6766 /* Only update the stub section if we have one. We should always have one if
6767 we're allowed to use the ADRP errata workaround, otherwise it is not
6768 required. */
6769 if (stub_entry->stub_sec)
6770 {
6771 insn = bfd_getl32 (contents + stub_entry->target_value);
6772 bfd_putl32 (insn,
6773 stub_entry->stub_sec->contents + stub_entry->stub_offset);
6774 }
4106101c
MS
6775
6776 place = (section->output_section->vma + section->output_offset
6777 + stub_entry->adrp_offset);
6778 insn = bfd_getl32 (contents + stub_entry->adrp_offset);
6779
9fca35fc 6780 if (!_bfd_aarch64_adrp_p (insn))
4106101c
MS
6781 abort ();
6782
6783 bfd_signed_vma imm =
6784 (_bfd_aarch64_sign_extend
6785 ((bfd_vma) _bfd_aarch64_decode_adrp_imm (insn) << 12, 33)
6786 - (place & 0xfff));
6787
739b5c9c 6788 if ((htab->fix_erratum_843419 & ERRAT_ADR)
4106101c
MS
6789 && (imm >= AARCH64_MIN_ADRP_IMM && imm <= AARCH64_MAX_ADRP_IMM))
6790 {
92504105 6791 insn = (_bfd_aarch64_reencode_adr_imm (AARCH64_ADR_OP, imm, 0)
4106101c
MS
6792 | AARCH64_RT (insn));
6793 bfd_putl32 (insn, contents + stub_entry->adrp_offset);
739b5c9c
TC
6794 /* Stub is not needed, don't map it out. */
6795 stub_entry->stub_type = aarch64_stub_none;
4106101c 6796 }
739b5c9c 6797 else if (htab->fix_erratum_843419 & ERRAT_ADRP)
4106101c
MS
6798 {
6799 bfd_vma veneered_insn_loc;
6800 bfd_vma veneer_entry_loc;
6801 bfd_signed_vma branch_offset;
6802 uint32_t branch_insn;
6803
6804 veneered_insn_loc = stub_entry->target_section->output_section->vma
6805 + stub_entry->target_section->output_offset
6806 + stub_entry->target_value;
6807 veneer_entry_loc = stub_entry->stub_sec->output_section->vma
6808 + stub_entry->stub_sec->output_offset
6809 + stub_entry->stub_offset;
6810 branch_offset = veneer_entry_loc - veneered_insn_loc;
6811
6812 abfd = stub_entry->target_section->owner;
6813 if (!aarch64_valid_branch_p (veneer_entry_loc, veneered_insn_loc))
4eca0228 6814 _bfd_error_handler
90b6238f 6815 (_("%pB: error: erratum 843419 stub out "
4106101c
MS
6816 "of range (input file too large)"), abfd);
6817
6818 branch_insn = 0x14000000;
6819 branch_offset >>= 2;
6820 branch_offset &= 0x3ffffff;
6821 branch_insn |= branch_offset;
6822 bfd_putl32 (branch_insn, contents + stub_entry->target_value);
6823 }
739b5c9c
TC
6824 else
6825 {
6826 abfd = stub_entry->target_section->owner;
6827 _bfd_error_handler
64672071 6828 (_("%pB: error: erratum 843419 immediate 0x%" BFD_VMA_FMT "x "
739b5c9c
TC
6829 "out of range for ADR (input file too large) and "
6830 "--fix-cortex-a53-843419=adr used. Run the linker with "
6831 "--fix-cortex-a53-843419=full instead"), abfd, imm);
6832 bfd_set_error (bfd_error_bad_value);
6833 /* This function is called inside a hashtable traversal and the error
6834 handlers called above turn into non-fatal errors. Which means this
6835 case ld returns an exit code 0 and also produces a broken object file.
6836 To prevent this, issue a hard abort. */
6837 BFD_FAIL ();
6838 }
4106101c
MS
6839 return TRUE;
6840}
6841
6842
68fcca92
JW
6843static bfd_boolean
6844elfNN_aarch64_write_section (bfd *output_bfd ATTRIBUTE_UNUSED,
6845 struct bfd_link_info *link_info,
6846 asection *sec,
6847 bfd_byte *contents)
6848
6849{
6850 struct elf_aarch64_link_hash_table *globals =
f872121a 6851 elf_aarch64_hash_table (link_info);
68fcca92
JW
6852
6853 if (globals == NULL)
6854 return FALSE;
6855
6856 /* Fix code to point to erratum 835769 stubs. */
6857 if (globals->fix_erratum_835769)
6858 {
6859 struct erratum_835769_branch_to_stub_data data;
6860
4106101c 6861 data.info = link_info;
68fcca92
JW
6862 data.output_section = sec;
6863 data.contents = contents;
6864 bfd_hash_traverse (&globals->stub_hash_table,
6865 make_branch_to_erratum_835769_stub, &data);
6866 }
6867
4106101c
MS
6868 if (globals->fix_erratum_843419)
6869 {
6870 struct erratum_835769_branch_to_stub_data data;
6871
6872 data.info = link_info;
6873 data.output_section = sec;
6874 data.contents = contents;
6875 bfd_hash_traverse (&globals->stub_hash_table,
6876 _bfd_aarch64_erratum_843419_branch_to_stub, &data);
6877 }
6878
68fcca92
JW
6879 return FALSE;
6880}
6881
2aff25ba
JW
6882/* Return TRUE if RELOC is a relocation against the base of GOT table. */
6883
6884static bfd_boolean
6885aarch64_relocation_aginst_gp_p (bfd_reloc_code_real_type reloc)
6886{
6887 return (reloc == BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14
6888 || reloc == BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15
6889 || reloc == BFD_RELOC_AARCH64_LD64_GOTOFF_LO15
6890 || reloc == BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC
6891 || reloc == BFD_RELOC_AARCH64_MOVW_GOTOFF_G1);
6892}
6893
40bbb79e
SP
6894/* Build capability meta data, i.e. size and permissions for a capability. */
6895
6896static bfd_vma
e10c835d 6897cap_meta (size_t size, const asection *sec, bfd_boolean *guessed)
40bbb79e
SP
6898{
6899
6900 if (size >= (1ULL << 56))
6901 return (bfd_vma) -1;
6902
b235f0e0
MM
6903 /* N.b. We are only ever using this function for Morello.
6904 Morello is little-endian.
6905 We are returning a 64bit sized integer.
6906 The format this metadata is supposed to fit is
6907 | 56 bit length | 8 bit permissions |
6908 This means that (in little endian layout) we need to put the 56 bit size
6909 in the *lower* bits of the uint64_t. */
6910 uint64_t flags = 0;
40bbb79e 6911 if (sec->flags & SEC_CODE)
b235f0e0
MM
6912 flags = 4;
6913 else if (sec->flags & SEC_READONLY
6914 || sec->flags & SEC_ROM)
6915 flags = 1;
6916 else if (sec->flags & SEC_ALLOC)
6917 flags = 2;
40bbb79e 6918
e10c835d
MM
6919 /* We should usually be able to derive a valid set of permissions
6920 from the section flags. We know that when a relocation is against an
6921 SHN_ABS symbol the section has no associated flags and we must guess.
6922
6923 As it stands we don't know of any other instances where we do not have
6924 permission flags on a section. We choose to allow instances that we do
6925 not know of rather than abort on them so that if the guess is correct we
6926 don't hamper anyone progressing. */
b235f0e0 6927 if (flags == 0)
e10c835d
MM
6928 {
6929 flags = 2;
6930 *guessed = TRUE;
6931 }
6932
b235f0e0 6933 return size | (flags << 56);
40bbb79e
SP
6934}
6935
5fa80905
AC
6936enum c64_section_perm_type {
6937 C64_SYM_UNKNOWN = 0,
6938 C64_SYM_STANDARD,
6939 C64_SYM_LINKER_DEF,
6940 C64_SYM_LDSCRIPT_DEF,
6941 C64_SYM_LDSCRIPT_START,
6942};
6943
6944static enum c64_section_perm_type
6945c64_symbol_section_adjustment (struct elf_link_hash_entry *h, bfd_vma value,
6946 asection *sym_sec, asection **ret_sec,
6947 struct bfd_link_info *info)
6948{
6949 if (!sym_sec)
6950 return C64_SYM_UNKNOWN;
6951
6952 *ret_sec = sym_sec;
6953 if (!h)
6954 return C64_SYM_STANDARD;
6955
6956 /* Linker defined symbols are always at the start of the section they
6957 track. */
6958 if (h->root.linker_def)
6959 return C64_SYM_LINKER_DEF;
6960 else if (h->root.ldscript_def)
6961 {
6962 const char *name = h->root.root.string;
6963 size_t len = strlen (name);
6964
6965 bfd_vma size = sym_sec->size - (value - sym_sec->vma);
6966 /* The special case: the symbol is at the end of the section.
6967 This could either mean that it is an end symbol or it is the
6968 start of the output section following the symbol. We try to
6969 guess if it is a start of the next section by reading its
6970 name. This is a compatibility hack, ideally linker scripts
6971 should be written such that start symbols are defined within
6972 the output section it intends to track. */
6973 if (size == 0
6974 && (len > 8 && name[0] == '_' && name[1] == '_'
6975 && (!strncmp (name + 2, "start_", 6)
6976 || !strcmp (name + len - 6, "_start"))))
6977 {
6978 asection *s = bfd_sections_find_if (info->output_bfd,
6979 section_start_symbol,
6980 &value);
6981 if (s != NULL)
6982 {
6983 *ret_sec = s;
6984 return C64_SYM_LDSCRIPT_START;
6985 }
6986 }
6987 return C64_SYM_LDSCRIPT_DEF;
6988 }
47b91198
MM
6989
6990 if (h->start_stop)
6991 {
6992 asection *s = h->u2.start_stop_section->output_section;
6993 BFD_ASSERT (s != NULL);
6994 *ret_sec = s;
6995 return C64_SYM_LDSCRIPT_DEF;
6996 }
6997
5fa80905
AC
6998 return C64_SYM_STANDARD;
6999}
7000
40bbb79e
SP
7001static bfd_reloc_status_type
7002c64_fixup_frag (bfd *input_bfd, struct bfd_link_info *info,
e10c835d
MM
7003 bfd_reloc_code_real_type bfd_r_type, Elf_Internal_Sym *sym,
7004 struct elf_link_hash_entry *h, asection *sym_sec,
7005 asection *reloc_sec, bfd_byte *frag_loc, bfd_vma value,
7006 bfd_signed_vma addend, bfd_vma r_offset)
40bbb79e 7007{
5fa80905
AC
7008 BFD_ASSERT (h || sym);
7009 bfd_vma size = sym ? sym->st_size : h->size;
40bbb79e
SP
7010 asection *perm_sec = sym_sec;
7011 bfd_boolean bounds_ok = FALSE;
7012
e10c835d
MM
7013 const int aarch64_reloc_idx = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
7014 const char *reloc_name = elfNN_aarch64_howto_table[aarch64_reloc_idx].name;
7401203c
MM
7015 const char *sym_name;
7016
7017 if (sym)
7018 {
7019 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_bfd);
7020 sym_name = (bfd_elf_string_from_elf_section (input_bfd,
7021 symtab_hdr->sh_link,
7022 sym->st_name));
7023 }
7024 else
7025 sym_name = h->root.root.string;
7026
5fa80905 7027 if (size == 0 && sym_sec)
40bbb79e 7028 {
5fa80905
AC
7029 bounds_ok = TRUE;
7030 enum c64_section_perm_type type
7031 = c64_symbol_section_adjustment (h, value, sym_sec, &perm_sec, info);
40bbb79e 7032
5fa80905 7033 switch (type)
40bbb79e 7034 {
5fa80905
AC
7035 case C64_SYM_STANDARD:
7036 break;
7037 case C64_SYM_LINKER_DEF:
7038 size = perm_sec->output_section->size;
7039 break;
7040 case C64_SYM_LDSCRIPT_DEF:
7041 size = perm_sec->size - (value - perm_sec->vma);
7042 break;
7043 case C64_SYM_LDSCRIPT_START:
7044 size = perm_sec->size;
7045 break;
7046 default:
7047 abort ();
40bbb79e
SP
7048 }
7049 }
7050
7051 /* Negative addends are not allowed for capability symbols. */
7052 if (addend < 0 || (bfd_vma) addend > size)
7053 return bfd_reloc_outofrange;
7054
5fa80905 7055 bfd_vma base = value, limit = value + size;
5a9e7a18 7056 unsigned align = 0;
40bbb79e 7057
5a9e7a18 7058 if (!bounds_ok && !c64_valid_cap_range (&base, &limit, &align))
40bbb79e 7059 {
7401203c
MM
7060 /* Just warn about this. It's not a requirement that bounds on
7061 objects should be precise, so there's no reason to error out on
7062 such an object. */
40bbb79e 7063 /* xgettext:c-format */
7401203c
MM
7064 _bfd_error_handler
7065 (_("%pB: capability range for '%s' may exceed object bounds"),
7066 input_bfd, sym_name);
40bbb79e
SP
7067 }
7068
5fa80905
AC
7069 if (perm_sec && perm_sec->flags & SEC_CODE)
7070 {
7071 /* Any symbol pointing into an executable section gets bounds according
7072 to PCC. In this case the relocation is set up so that the value is
7073 the base of the PCC, the addend is the offset from the PCC base to the
7074 VA that we want, and the size is the length of the PCC range.
7075 In this function we only use `value` to check the bounds make sense,
7076 which is somewhat superfluous when we're using pcc_high and pcc_low
7077 since we already enforced that in elfNN_c64_resize_sections. No harm
7078 in instead checking that the bounds on the object that were requested
7079 made sense even if they were overridden because this symbol points
7080 into an executable section.
7081
7082 `size` on the other hand is part of the fragment that we output to and
7083 we need to change it in order to have functions that can access global
7084 data or jump to other functions. */
7085 size = pcc_high - pcc_low;
7086 }
7087
40bbb79e
SP
7088 if (perm_sec != NULL)
7089 {
e10c835d
MM
7090 bfd_boolean permissions_guessed = FALSE;
7091 bfd_vma frag = cap_meta (size, perm_sec, &permissions_guessed);
40bbb79e
SP
7092
7093 if (frag == (bfd_vma) -1)
7094 return bfd_reloc_outofrange;
7095
e10c835d
MM
7096 if (permissions_guessed)
7097 {
7098 _bfd_error_handler (_("%pB(%pA+%#" PRIx64 "): "
7099 "warning: relocation %s against symbol '%s' in "
7100 "section without permission flags '%s'. "
7101 "Assuming Read-Write."),
7102 input_bfd, reloc_sec, r_offset, reloc_name,
7103 sym_name, perm_sec->name);
7104 }
7105
40bbb79e
SP
7106 bfd_put_64 (input_bfd, frag, frag_loc);
7107 }
7108
7109 return bfd_reloc_continue;
5fa80905 7110}
40bbb79e 7111
5fa80905
AC
7112/* Given either a local symbol SYM or global symbol H, do we need to adjust
7113 capability relocations against the symbol due to the fact that it points to
7114 a code section? */
7115static bfd_boolean
7116c64_symbol_adjust (struct elf_link_hash_entry *h,
7117 bfd_vma value, asection *sym_sec, struct bfd_link_info *info,
7118 bfd_vma *adjust_addr)
7119{
7120 asection *tmp_sec;
7121 enum c64_section_perm_type type
7122 = c64_symbol_section_adjustment (h, value, sym_sec, &tmp_sec, info);
7123
7124 if (type == C64_SYM_UNKNOWN)
7125 return FALSE;
7126
7127 if (tmp_sec->flags & SEC_CODE)
40bbb79e 7128 {
5fa80905
AC
7129 *adjust_addr = pcc_low;
7130 return TRUE;
40bbb79e 7131 }
5fa80905
AC
7132
7133 return FALSE;
40bbb79e
SP
7134}
7135
4e7fbb34
JW
7136/* Perform a relocation as part of a final link. The input relocation type
7137 should be TLS relaxed. */
7138
a06ea964 7139static bfd_reloc_status_type
cec5225b 7140elfNN_aarch64_final_link_relocate (reloc_howto_type *howto,
a06ea964
NC
7141 bfd *input_bfd,
7142 bfd *output_bfd,
7143 asection *input_section,
7144 bfd_byte *contents,
7145 Elf_Internal_Rela *rel,
7146 bfd_vma value,
7147 struct bfd_link_info *info,
7148 asection *sym_sec,
7149 struct elf_link_hash_entry *h,
7150 bfd_boolean *unresolved_reloc_p,
7151 bfd_boolean save_addend,
1419bbe5
WN
7152 bfd_vma *saved_addend,
7153 Elf_Internal_Sym *sym)
a06ea964 7154{
1419bbe5 7155 Elf_Internal_Shdr *symtab_hdr;
a06ea964 7156 unsigned int r_type = howto->type;
a6bb11b2
YZ
7157 bfd_reloc_code_real_type bfd_r_type
7158 = elfNN_aarch64_bfd_reloc_from_howto (howto);
a06ea964
NC
7159 unsigned long r_symndx;
7160 bfd_byte *hit_data = contents + rel->r_offset;
96d01d93 7161 bfd_vma place, off, got_entry_addr = 0;
a06ea964 7162 bfd_signed_vma signed_addend;
cec5225b 7163 struct elf_aarch64_link_hash_table *globals;
a06ea964 7164 bfd_boolean weak_undef_p;
ff07562f 7165 bfd_boolean relative_reloc;
808c324b 7166 bfd_boolean c64_needs_frag_fixup;
b53b1bed 7167 asection *base_got;
ff07562f 7168 bfd_vma orig_value = value;
ddb7fd0f 7169 bfd_boolean resolved_to_zero;
0c1ded8d 7170 bfd_boolean abs_symbol_p;
f0070c1e 7171 Elf_Internal_Sym *isym = NULL;
50e192f0
SP
7172 bfd_boolean c64_rtype = FALSE;
7173 bfd_boolean to_c64 = FALSE;
a06ea964 7174
cec5225b 7175 globals = elf_aarch64_hash_table (info);
a06ea964 7176
1419bbe5
WN
7177 symtab_hdr = &elf_symtab_hdr (input_bfd);
7178
a06ea964
NC
7179 BFD_ASSERT (is_aarch64_elf (input_bfd));
7180
cec5225b 7181 r_symndx = ELFNN_R_SYM (rel->r_info);
a06ea964 7182
a06ea964
NC
7183 place = input_section->output_section->vma
7184 + input_section->output_offset + rel->r_offset;
7185
7186 /* Get addend, accumulating the addend for consecutive relocs
7187 which refer to the same offset. */
7188 signed_addend = saved_addend ? *saved_addend : 0;
7189 signed_addend += rel->r_addend;
7190
7191 weak_undef_p = (h ? h->root.type == bfd_link_hash_undefweak
7192 : bfd_is_und_section (sym_sec));
c691de6a 7193 abs_symbol_p = h != NULL && bfd_is_abs_symbol (&h->root);
0c1ded8d 7194
f0070c1e 7195 if (sym)
50e192f0
SP
7196 {
7197 isym = bfd_sym_from_r_symndx (&globals->root.sym_cache, input_bfd,
7198 r_symndx);
7199 BFD_ASSERT (isym != NULL);
7200 to_c64 = (isym->st_target_internal & ST_BRANCH_TO_C64) != 0;
7201 }
7202 else
7203 to_c64 = (h->target_internal & ST_BRANCH_TO_C64) != 0;
f0070c1e 7204
a6bb11b2 7205
1419bbe5
WN
7206 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
7207 it here if it is defined in a non-shared object. */
7208 if (h != NULL
7209 && h->type == STT_GNU_IFUNC
7210 && h->def_regular)
7211 {
7212 asection *plt;
7213 const char *name;
99ad26cb 7214 bfd_vma addend = 0;
1419bbe5 7215
545bc2b3
SN
7216 if ((input_section->flags & SEC_ALLOC) == 0)
7217 {
f657f8c4
NC
7218 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
7219 STT_GNU_IFUNC symbol as STT_FUNC. */
7220 if (elf_section_type (input_section) == SHT_NOTE)
7221 goto skip_ifunc;
7222
545bc2b3
SN
7223 /* Dynamic relocs are not propagated for SEC_DEBUGGING
7224 sections because such sections are not SEC_ALLOC and
7225 thus ld.so will not process them. */
7226 if ((input_section->flags & SEC_DEBUGGING) != 0)
7227 return bfd_reloc_ok;
7228
7229 if (h->root.root.string)
7230 name = h->root.root.string;
7231 else
7232 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, NULL);
7233 _bfd_error_handler
7234 /* xgettext:c-format */
2dcf00ce
AM
7235 (_("%pB(%pA+%#" PRIx64 "): "
7236 "unresolvable %s relocation against symbol `%s'"),
7237 input_bfd, input_section, (uint64_t) rel->r_offset,
7238 howto->name, name);
545bc2b3 7239 bfd_set_error (bfd_error_bad_value);
1d75a8e2 7240 return bfd_reloc_notsupported;
545bc2b3
SN
7241 }
7242 else if (h->plt.offset == (bfd_vma) -1)
7243 goto bad_ifunc_reloc;
1419bbe5
WN
7244
7245 /* STT_GNU_IFUNC symbol must go through PLT. */
7246 plt = globals->root.splt ? globals->root.splt : globals->root.iplt;
7247 value = (plt->output_section->vma + plt->output_offset + h->plt.offset);
7248
7249 switch (bfd_r_type)
7250 {
7251 default:
dc1e8a47 7252 bad_ifunc_reloc:
1419bbe5
WN
7253 if (h->root.root.string)
7254 name = h->root.root.string;
7255 else
7256 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
7257 NULL);
4eca0228 7258 _bfd_error_handler
695344c0 7259 /* xgettext:c-format */
871b3ab2 7260 (_("%pB: relocation %s against STT_GNU_IFUNC "
1419bbe5
WN
7261 "symbol `%s' isn't handled by %s"), input_bfd,
7262 howto->name, name, __FUNCTION__);
7263 bfd_set_error (bfd_error_bad_value);
1d75a8e2 7264 return bfd_reloc_notsupported;
1419bbe5
WN
7265
7266 case BFD_RELOC_AARCH64_NN:
7267 if (rel->r_addend != 0)
7268 {
7269 if (h->root.root.string)
7270 name = h->root.root.string;
7271 else
7272 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
7273 sym, NULL);
4eca0228 7274 _bfd_error_handler
695344c0 7275 /* xgettext:c-format */
871b3ab2 7276 (_("%pB: relocation %s against STT_GNU_IFUNC "
2dcf00ce
AM
7277 "symbol `%s' has non-zero addend: %" PRId64),
7278 input_bfd, howto->name, name, (int64_t) rel->r_addend);
1419bbe5 7279 bfd_set_error (bfd_error_bad_value);
1d75a8e2 7280 return bfd_reloc_notsupported;
1419bbe5
WN
7281 }
7282
7283 /* Generate dynamic relocation only when there is a
7284 non-GOT reference in a shared object. */
0e1862bb 7285 if (bfd_link_pic (info) && h->non_got_ref)
1419bbe5
WN
7286 {
7287 Elf_Internal_Rela outrel;
7288 asection *sreloc;
7289
7290 /* Need a dynamic relocation to get the real function
7291 address. */
7292 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
7293 info,
7294 input_section,
7295 rel->r_offset);
7296 if (outrel.r_offset == (bfd_vma) -1
7297 || outrel.r_offset == (bfd_vma) -2)
7298 abort ();
7299
7300 outrel.r_offset += (input_section->output_section->vma
7301 + input_section->output_offset);
7302
7303 if (h->dynindx == -1
7304 || h->forced_local
0e1862bb 7305 || bfd_link_executable (info))
1419bbe5
WN
7306 {
7307 /* This symbol is resolved locally. */
60d18fc3 7308 outrel.r_info = (globals->c64_rel
e19e9199
SP
7309 ? ELFNN_R_INFO (0, MORELLO_R (IRELATIVE))
7310 : ELFNN_R_INFO (0, AARCH64_R (IRELATIVE)));
1419bbe5
WN
7311 outrel.r_addend = (h->root.u.def.value
7312 + h->root.u.def.section->output_section->vma
7313 + h->root.u.def.section->output_offset);
7314 }
7315 else
7316 {
7317 outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
7318 outrel.r_addend = 0;
7319 }
7320
7321 sreloc = globals->root.irelifunc;
7322 elf_append_rela (output_bfd, sreloc, &outrel);
7323
7324 /* If this reloc is against an external symbol, we
7325 do not want to fiddle with the addend. Otherwise,
7326 we need to include the symbol value so that it
7327 becomes an addend for the dynamic reloc. For an
7328 internal symbol, we have updated addend. */
7329 return bfd_reloc_ok;
7330 }
7331 /* FALLTHROUGH */
e19e9199
SP
7332 case BFD_RELOC_MORELLO_CALL26:
7333 case BFD_RELOC_MORELLO_JUMP26:
1419bbe5 7334 case BFD_RELOC_AARCH64_CALL26:
ce336788 7335 case BFD_RELOC_AARCH64_JUMP26:
652afeef
TC
7336 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
7337 place, value,
1419bbe5
WN
7338 signed_addend,
7339 weak_undef_p);
7340 return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type,
7341 howto, value);
1419bbe5 7342 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
92504105 7343 case BFD_RELOC_MORELLO_ADR_GOT_PAGE:
1419bbe5 7344 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
7018c030 7345 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
ce336788 7346 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
99ad26cb 7347 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
dc8008f5 7348 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
74a1bfe1 7349 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
a2e1db00 7350 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
ce336788 7351 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
a1bdea65 7352 case BFD_RELOC_MORELLO_LD128_GOT_LO12_NC:
1419bbe5
WN
7353 base_got = globals->root.sgot;
7354 off = h->got.offset;
7355
7356 if (base_got == NULL)
7357 abort ();
7358
7359 if (off == (bfd_vma) -1)
7360 {
7361 bfd_vma plt_index;
7362
7363 /* We can't use h->got.offset here to save state, or
7364 even just remember the offset, as finish_dynamic_symbol
7365 would use that as offset into .got. */
7366
7367 if (globals->root.splt != NULL)
7368 {
b1ee0cc4
WN
7369 plt_index = ((h->plt.offset - globals->plt_header_size) /
7370 globals->plt_entry_size);
a1bdea65 7371 off = (plt_index + 3) * GOT_ENTRY_SIZE (globals);
1419bbe5
WN
7372 base_got = globals->root.sgotplt;
7373 }
7374 else
7375 {
7376 plt_index = h->plt.offset / globals->plt_entry_size;
a1bdea65 7377 off = plt_index * GOT_ENTRY_SIZE (globals);
1419bbe5
WN
7378 base_got = globals->root.igotplt;
7379 }
7380
7381 if (h->dynindx == -1
7382 || h->forced_local
7383 || info->symbolic)
7384 {
7385 /* This references the local definition. We must
7386 initialize this entry in the global offset table.
7387 Since the offset must always be a multiple of 8,
7388 we use the least significant bit to record
7389 whether we have initialized it already.
7390
7391 When doing a dynamic link, we create a .rela.got
7392 relocation entry to initialize the value. This
7393 is done in the finish_dynamic_symbol routine. */
7394 if ((off & 1) != 0)
7395 off &= ~1;
7396 else
7397 {
7398 bfd_put_NN (output_bfd, value,
7399 base_got->contents + off);
7400 /* Note that this is harmless as -1 | 1 still is -1. */
7401 h->got.offset |= 1;
7402 }
7403 }
7404 value = (base_got->output_section->vma
7405 + base_got->output_offset + off);
7406 }
7407 else
7408 value = aarch64_calculate_got_entry_vma (h, globals, info,
7409 value, output_bfd,
7410 unresolved_reloc_p);
a0becb89 7411
2aff25ba
JW
7412 if (aarch64_relocation_aginst_gp_p (bfd_r_type))
7413 addend = (globals->root.sgot->output_section->vma
7414 + globals->root.sgot->output_offset);
a0becb89 7415
652afeef
TC
7416 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
7417 place, value,
99ad26cb 7418 addend, weak_undef_p);
1419bbe5 7419 return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type, howto, value);
1419bbe5 7420 case BFD_RELOC_AARCH64_ADD_LO12:
ce336788 7421 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
92504105 7422 case BFD_RELOC_MORELLO_ADR_HI20_PCREL:
1419bbe5
WN
7423 break;
7424 }
7425 }
7426
f657f8c4 7427 skip_ifunc:
ddb7fd0f
L
7428 resolved_to_zero = (h != NULL
7429 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7430
a6bb11b2 7431 switch (bfd_r_type)
a06ea964 7432 {
a6bb11b2 7433 case BFD_RELOC_AARCH64_NONE:
0484b454 7434 case BFD_RELOC_AARCH64_TLSDESC_ADD:
a6bb11b2 7435 case BFD_RELOC_AARCH64_TLSDESC_CALL:
0484b454 7436 case BFD_RELOC_AARCH64_TLSDESC_LDR:
4ca9b406 7437 case BFD_RELOC_MORELLO_TLSDESC_CALL:
a06ea964
NC
7438 *unresolved_reloc_p = FALSE;
7439 return bfd_reloc_ok;
7440
a6bb11b2 7441 case BFD_RELOC_AARCH64_NN:
7470bce3
MM
7442 /* If we are relocating against a C64 symbol, then the value can't
7443 already have the LSB set (since STT_FUNC symbols are code labels and
7444 they will be aligned). Hence it's safe just to or-equal in order
7445 to ensure the LSB is set in that case. */
7446 value |= to_c64 ? 1 : 0;
a06ea964
NC
7447
7448 /* When generating a shared object or relocatable executable, these
07d6d2b8
AM
7449 relocations are copied into the output file to be resolved at
7450 run time. */
6353d82b
JW
7451 if (((bfd_link_pic (info)
7452 || globals->root.is_relocatable_executable)
7453 && (input_section->flags & SEC_ALLOC)
7454 && (h == NULL
ddb7fd0f
L
7455 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7456 && !resolved_to_zero)
6353d82b
JW
7457 || h->root.type != bfd_link_hash_undefweak))
7458 /* Or we are creating an executable, we may need to keep relocations
7459 for symbols satisfied by a dynamic library if we manage to avoid
7460 copy relocs for the symbol. */
7461 || (ELIMINATE_COPY_RELOCS
7462 && !bfd_link_pic (info)
7463 && h != NULL
7464 && (input_section->flags & SEC_ALLOC)
7465 && h->dynindx != -1
7466 && !h->non_got_ref
7467 && ((h->def_dynamic
7468 && !h->def_regular)
7469 || h->root.type == bfd_link_hash_undefweak
7470 || h->root.type == bfd_link_hash_undefined)))
a06ea964
NC
7471 {
7472 Elf_Internal_Rela outrel;
7473 bfd_byte *loc;
7474 bfd_boolean skip, relocate;
7475 asection *sreloc;
7476
7477 *unresolved_reloc_p = FALSE;
7478
a06ea964
NC
7479 skip = FALSE;
7480 relocate = FALSE;
7481
7482 outrel.r_addend = signed_addend;
7483 outrel.r_offset =
7484 _bfd_elf_section_offset (output_bfd, info, input_section,
7485 rel->r_offset);
7486 if (outrel.r_offset == (bfd_vma) - 1)
7487 skip = TRUE;
7488 else if (outrel.r_offset == (bfd_vma) - 2)
7489 {
7490 skip = TRUE;
7491 relocate = TRUE;
7492 }
0c1ded8d
RL
7493 else if (abs_symbol_p)
7494 {
7495 /* Local absolute symbol. */
7496 skip = (h->forced_local || (h->dynindx == -1));
7497 relocate = skip;
7498 }
a06ea964
NC
7499
7500 outrel.r_offset += (input_section->output_section->vma
7501 + input_section->output_offset);
7502
7503 if (skip)
7504 memset (&outrel, 0, sizeof outrel);
7505 else if (h != NULL
7506 && h->dynindx != -1
0e1862bb 7507 && (!bfd_link_pic (info)
0c1ded8d 7508 || !(bfd_link_pie (info) || SYMBOLIC_BIND (info, h))
0e1862bb 7509 || !h->def_regular))
cec5225b 7510 outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
a06ea964
NC
7511 else
7512 {
7513 int symbol;
7514
7515 /* On SVR4-ish systems, the dynamic loader cannot
7516 relocate the text and data segments independently,
7517 so the symbol does not matter. */
7518 symbol = 0;
1f56df9d 7519 relocate = globals->no_apply_dynamic_relocs ? FALSE : TRUE;
a6bb11b2 7520 outrel.r_info = ELFNN_R_INFO (symbol, AARCH64_R (RELATIVE));
a06ea964
NC
7521 outrel.r_addend += value;
7522 }
7523
1419bbe5
WN
7524 sreloc = elf_section_data (input_section)->sreloc;
7525 if (sreloc == NULL || sreloc->contents == NULL)
7526 return bfd_reloc_notsupported;
7527
7528 loc = sreloc->contents + sreloc->reloc_count++ * RELOC_SIZE (globals);
cec5225b 7529 bfd_elfNN_swap_reloca_out (output_bfd, &outrel, loc);
a06ea964 7530
1419bbe5 7531 if (sreloc->reloc_count * RELOC_SIZE (globals) > sreloc->size)
a06ea964
NC
7532 {
7533 /* Sanity to check that we have previously allocated
7534 sufficient space in the relocation section for the
7535 number of relocations we actually want to emit. */
7536 abort ();
7537 }
7538
7539 /* If this reloc is against an external symbol, we do not want to
7540 fiddle with the addend. Otherwise, we need to include the symbol
7541 value so that it becomes an addend for the dynamic reloc. */
7542 if (!relocate)
7543 return bfd_reloc_ok;
7544
7545 return _bfd_final_link_relocate (howto, input_bfd, input_section,
7546 contents, rel->r_offset, value,
7547 signed_addend);
7548 }
7549 else
7550 value += signed_addend;
7551 break;
7552
e19e9199
SP
7553 case BFD_RELOC_MORELLO_CALL26:
7554 case BFD_RELOC_MORELLO_JUMP26:
a6bb11b2 7555 case BFD_RELOC_AARCH64_CALL26:
ce336788 7556 case BFD_RELOC_AARCH64_JUMP26:
a06ea964
NC
7557 {
7558 asection *splt = globals->root.splt;
c7cd2917
SP
7559 bfd_boolean via_plt_p =
7560 splt != NULL && h != NULL && h->plt.offset != (bfd_vma) - 1;
a06ea964
NC
7561
7562 /* A call to an undefined weak symbol is converted to a jump to
7563 the next instruction unless a PLT entry will be created.
7564 The jump to the next instruction is optimized as a NOP.
7565 Do the same for local undefined symbols. */
7566 if (weak_undef_p && ! via_plt_p)
7567 {
7568 bfd_putl32 (INSN_NOP, hit_data);
7569 return bfd_reloc_ok;
7570 }
7571
7572 /* If the call goes through a PLT entry, make sure to
7573 check distance to the right destination address. */
7574 if (via_plt_p)
07f9ddfe
JW
7575 value = (splt->output_section->vma
7576 + splt->output_offset + h->plt.offset);
7577
7578 /* Check if a stub has to be inserted because the destination
7579 is too far away. */
7580 struct elf_aarch64_stub_hash_entry *stub_entry = NULL;
2f340668 7581
50e192f0
SP
7582 enum elf_aarch64_stub_type c64_stub = aarch64_stub_none;
7583
7584 /* Figure out if we need an interworking stub and if yes, what
7585 kind. */
7586 if (!via_plt_p)
7587 c64_stub = aarch64_interwork_stub (r_type, to_c64);
7588
2f340668
JW
7589 /* If the branch destination is directed to plt stub, "value" will be
7590 the final destination, otherwise we should plus signed_addend, it may
7591 contain non-zero value, for example call to local function symbol
7592 which are turned into "sec_sym + sec_off", and sec_off is kept in
7593 signed_addend. */
50e192f0
SP
7594 if (c64_stub != aarch64_stub_none
7595 || (aarch64_branch_reloc_p (r_type)
7596 && !aarch64_valid_branch_p ((via_plt_p ? value
7597 : value + signed_addend), place)))
7598 {
7599 /* The target is out of reach, so redirect the branch to
7600 the local stub for this function. */
7601 stub_entry = elfNN_aarch64_get_stub_entry (input_section, sym_sec,
7602 h, rel, globals,
7603 c64_stub);
7604 }
7605
07f9ddfe 7606 if (stub_entry != NULL)
2f340668
JW
7607 {
7608 value = (stub_entry->stub_offset
7609 + stub_entry->stub_sec->output_offset
7610 + stub_entry->stub_sec->output_section->vma);
7611
7612 /* We have redirected the destination to stub entry address,
7613 so ignore any addend record in the original rela entry. */
7614 signed_addend = 0;
7615 }
a06ea964 7616 }
652afeef
TC
7617 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
7618 place, value,
caed7120 7619 signed_addend, weak_undef_p);
07f9ddfe 7620 *unresolved_reloc_p = FALSE;
a06ea964
NC
7621 break;
7622
dcbd20eb
JW
7623 case BFD_RELOC_AARCH64_16_PCREL:
7624 case BFD_RELOC_AARCH64_32_PCREL:
7625 case BFD_RELOC_AARCH64_64_PCREL:
ce336788
JW
7626 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
7627 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
92504105
SP
7628 case BFD_RELOC_MORELLO_ADR_HI20_NC_PCREL:
7629 case BFD_RELOC_MORELLO_ADR_HI20_PCREL:
ce336788
JW
7630 case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
7631 case BFD_RELOC_AARCH64_LD_LO19_PCREL:
f7d2c675 7632 case BFD_RELOC_MORELLO_LD_LO17_PCREL:
1daf502a
RL
7633 case BFD_RELOC_AARCH64_MOVW_PREL_G0:
7634 case BFD_RELOC_AARCH64_MOVW_PREL_G0_NC:
7635 case BFD_RELOC_AARCH64_MOVW_PREL_G1:
7636 case BFD_RELOC_AARCH64_MOVW_PREL_G1_NC:
7637 case BFD_RELOC_AARCH64_MOVW_PREL_G2:
7638 case BFD_RELOC_AARCH64_MOVW_PREL_G2_NC:
7639 case BFD_RELOC_AARCH64_MOVW_PREL_G3:
0e1862bb 7640 if (bfd_link_pic (info)
dcbd20eb
JW
7641 && (input_section->flags & SEC_ALLOC) != 0
7642 && (input_section->flags & SEC_READONLY) != 0
d68f1976 7643 && !SYMBOL_REFERENCES_LOCAL (info, h))
dcbd20eb
JW
7644 {
7645 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
7646
4eca0228 7647 _bfd_error_handler
695344c0 7648 /* xgettext:c-format */
871b3ab2 7649 (_("%pB: relocation %s against symbol `%s' which may bind "
d68f1976
JW
7650 "externally can not be used when making a shared object; "
7651 "recompile with -fPIC"),
dcbd20eb
JW
7652 input_bfd, elfNN_aarch64_howto_table[howto_index].name,
7653 h->root.root.string);
7654 bfd_set_error (bfd_error_bad_value);
1d75a8e2 7655 return bfd_reloc_notsupported;
dcbd20eb 7656 }
c7cd2917
SP
7657 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
7658 place, value,
7659 signed_addend,
7660 weak_undef_p);
8b21361b 7661
7470bce3 7662 if (bfd_r_type == BFD_RELOC_AARCH64_ADR_LO21_PCREL && to_c64)
8b21361b 7663 value |= 1;
c7cd2917
SP
7664 break;
7665
e19e9199
SP
7666 case BFD_RELOC_MORELLO_BRANCH19:
7667 case BFD_RELOC_MORELLO_TSTBR14:
50e192f0
SP
7668 c64_rtype = TRUE;
7669 /* Fall through. */
c7cd2917
SP
7670 case BFD_RELOC_AARCH64_BRANCH19:
7671 case BFD_RELOC_AARCH64_TSTBR14:
7672 if (h && h->root.type == bfd_link_hash_undefined)
7673 {
7674 _bfd_error_handler
7675 /* xgettext:c-format */
7676 (_("%pB: conditional branch to undefined symbol `%s' "
7677 "not allowed"), input_bfd, h->root.root.string);
7678 bfd_set_error (bfd_error_bad_value);
7679 return bfd_reloc_notsupported;
7680 }
50e192f0
SP
7681 {
7682 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
7683
7684 if ((c64_rtype && !to_c64) || (!c64_rtype && to_c64))
7685 {
7686 _bfd_error_handler
7687 /* xgettext:c-format */
7688 (_("%pB: interworking not supported on relocation %s"),
7689 input_bfd, elfNN_aarch64_howto_table[howto_index].name);
7690 return bfd_reloc_notsupported;
7691 }
7692 }
1a0670f3 7693 /* Fall through. */
dcbd20eb 7694
a6bb11b2 7695 case BFD_RELOC_AARCH64_16:
92d77487
RL
7696#if ARCH_SIZE == 64
7697 case BFD_RELOC_AARCH64_32:
7698#endif
a6bb11b2 7699 case BFD_RELOC_AARCH64_ADD_LO12:
ce336788 7700 case BFD_RELOC_AARCH64_LDST128_LO12:
a6bb11b2
YZ
7701 case BFD_RELOC_AARCH64_LDST16_LO12:
7702 case BFD_RELOC_AARCH64_LDST32_LO12:
7703 case BFD_RELOC_AARCH64_LDST64_LO12:
ce336788 7704 case BFD_RELOC_AARCH64_LDST8_LO12:
a6bb11b2
YZ
7705 case BFD_RELOC_AARCH64_MOVW_G0:
7706 case BFD_RELOC_AARCH64_MOVW_G0_NC:
ce336788 7707 case BFD_RELOC_AARCH64_MOVW_G0_S:
a6bb11b2
YZ
7708 case BFD_RELOC_AARCH64_MOVW_G1:
7709 case BFD_RELOC_AARCH64_MOVW_G1_NC:
ce336788 7710 case BFD_RELOC_AARCH64_MOVW_G1_S:
a6bb11b2
YZ
7711 case BFD_RELOC_AARCH64_MOVW_G2:
7712 case BFD_RELOC_AARCH64_MOVW_G2_NC:
ce336788 7713 case BFD_RELOC_AARCH64_MOVW_G2_S:
a6bb11b2 7714 case BFD_RELOC_AARCH64_MOVW_G3:
652afeef
TC
7715 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
7716 place, value,
caed7120 7717 signed_addend, weak_undef_p);
7470bce3
MM
7718 if ((bfd_r_type == BFD_RELOC_AARCH64_ADD_LO12
7719 || bfd_r_type == BFD_RELOC_AARCH64_MOVW_G0
7720 || bfd_r_type == BFD_RELOC_AARCH64_MOVW_G0_S
7721 || bfd_r_type == BFD_RELOC_AARCH64_MOVW_G0_NC
7722 || bfd_r_type == BFD_RELOC_AARCH64_32
7723 || bfd_r_type == BFD_RELOC_AARCH64_16)
7724 && to_c64)
8b21361b
SP
7725 value |= 1;
7726
a06ea964
NC
7727 break;
7728
1408e112
MM
7729 case BFD_RELOC_MORELLO_MOVW_SIZE_G0:
7730 case BFD_RELOC_MORELLO_MOVW_SIZE_G0_NC:
7731 case BFD_RELOC_MORELLO_MOVW_SIZE_G1:
7732 case BFD_RELOC_MORELLO_MOVW_SIZE_G1_NC:
7733 case BFD_RELOC_MORELLO_MOVW_SIZE_G2:
7734 case BFD_RELOC_MORELLO_MOVW_SIZE_G2_NC:
7735 case BFD_RELOC_MORELLO_MOVW_SIZE_G3:
26c32295
MM
7736 if (weak_undef_p || !SYMBOL_REFERENCES_LOCAL (info, h))
7737 {
7738 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
7739 const char *name;
7740 if (h && h->root.root.string)
7741 name = h->root.root.string;
7742 else
7743 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, NULL);
7744 _bfd_error_handler
7745 /* xgettext:c-format */
7746 (_("%pB: relocation %s against `%s' must be used against a "
7747 "non-interposable defined symbol"),
7748 input_bfd, elfNN_aarch64_howto_table[howto_index].name, name);
7749 bfd_set_error (bfd_error_bad_value);
7750 return bfd_reloc_continue;
7751 }
7752 /* signed addend should have been handled by relocate_section. */
7753 BFD_ASSERT (!signed_addend);
1408e112
MM
7754 value = sym ? sym->st_size : h->size;
7755 /* N.b. the call to resolve relocation is not really necessary since
7756 the relocation does not allow any addend, the relocation is not
7757 PC-relative, and the relocation is against the base value. I.e. there
7758 is no modification to `value` that we need to perform. We keep it for
7759 consistency with other relocations. */
7760 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
7761 place, value,
7762 signed_addend, weak_undef_p);
7763 break;
7764
a6bb11b2 7765 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
92504105 7766 case BFD_RELOC_MORELLO_ADR_GOT_PAGE:
a6bb11b2 7767 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
7018c030 7768 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
ce336788 7769 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
99ad26cb 7770 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
ce336788 7771 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
a1bdea65 7772 case BFD_RELOC_MORELLO_LD128_GOT_LO12_NC:
2aff25ba
JW
7773 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
7774 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
7775 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
8d4edc5f
SP
7776 off = symbol_got_offset (input_bfd, h, r_symndx);
7777 base_got = globals->root.sgot;
5fa80905 7778
d9f4f6fc 7779 bfd_boolean is_dynamic = elf_hash_table (info)->dynamic_sections_created;
8d4edc5f 7780
5fa80905
AC
7781 if (signed_addend != 0)
7782 {
7783 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
7784 _bfd_error_handler
7785 /* xgettext:c-format */
7786 (_("%pB: symbol plus addend can not be placed into the GOT "
7787 "for relocation %s"),
7788 input_bfd, elfNN_aarch64_howto_table[howto_index].name);
7789 abort ();
7790 }
7791
8d4edc5f 7792 if (base_got == NULL)
a06ea964
NC
7793 BFD_ASSERT (h != NULL);
7794
ff07562f 7795 relative_reloc = FALSE;
808c324b 7796 c64_needs_frag_fixup = FALSE;
a06ea964
NC
7797 if (h != NULL)
7798 {
99ad26cb 7799 bfd_vma addend = 0;
5fa80905 7800 bfd_vma frag_value;
ff07562f
JW
7801
7802 /* If a symbol is not dynamic and is not undefined weak, bind it
7803 locally and generate a RELATIVE relocation under PIC mode.
7804
7805 NOTE: one symbol may be referenced by several relocations, we
7806 should only generate one RELATIVE relocation for that symbol.
c0a10a86
MM
7807 Therefore, check GOT offset mark first. */
7808 if (h->dynindx == -1
7809 && !h->forced_local
7810 && h->root.type != bfd_link_hash_undefweak
7811 && bfd_link_pic (info)
ff07562f 7812 && !symbol_got_offset_mark_p (input_bfd, h, r_symndx))
808c324b 7813 {
c0a10a86
MM
7814 /* Here we look for symbols which are not going to have their
7815 relocations added by finish_dynamic_symbol, but which still
7816 need a dynamic relocation because we're compiling for PIC.
7817
7818 Action on this clause and the one below is the same.
7819 Written that way to make the three different cases and their
7820 interpretation clear. */
8f5baae3
MM
7821 BFD_ASSERT (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
7822 (is_dynamic, bfd_link_pic (info), h));
808c324b 7823 relative_reloc = TRUE;
d465be7b 7824 c64_needs_frag_fixup = globals->c64_rel ? TRUE : FALSE;
808c324b 7825 }
d465be7b 7826 else if (!globals->c64_rel || !c64_needs_relocation (info, h))
c0a10a86
MM
7827 {
7828 /* Symbol references via GOT in C64 should always have
7829 relocations of some kind unless they are undefined weak
7830 symbols which cannot be provided at runtime. In those cases
7831 we need a plain zero.
7832
7833 This clause catches the case when we're not relocating for
7834 GOT, or when we're relocating an undefined weak symbol. */
7835 }
f0a8e7c6 7836 else if (!bfd_link_relocatable (info)
c0a10a86
MM
7837 && !WILL_CALL_FINISH_DYNAMIC_SYMBOL (is_dynamic,
7838 bfd_link_pic (info), h)
c0a10a86
MM
7839 && !symbol_got_offset_mark_p (input_bfd, h, r_symndx))
7840 {
7841 /* This clause is here to catch any c64 entries in the GOT which
7842 need a relocation, but whose relocation will not be provided
7843 by finish_dynamic_symbol. */
7844 relative_reloc = TRUE;
7845 c64_needs_frag_fixup = TRUE;
7846 }
7847 else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (is_dynamic,
7848 bfd_link_pic (info), h)
808c324b
MM
7849 && bfd_link_pic (info)
7850 && SYMBOL_REFERENCES_LOCAL (info, h))
7851 {
c0a10a86
MM
7852 /* If this is a dynamic symbol that binds locally then the
7853 generic code and elfNN_aarch64_finish_dynamic_symbol will
7854 already handle creating the RELATIVE reloc pointing into the
7855 GOT for this symbol. That means that this function does not
7856 need to handle *creating* such a relocation. We already
7857 handle setting the base value in the fragment for that
7858 relocation below, but we also need to make sure we set the
7859 rest of the fragment correctly for C64 code (i.e. including
7860 the required permissions and bounds). */
808c324b
MM
7861 c64_needs_frag_fixup = TRUE;
7862 }
ff07562f 7863
d465be7b 7864 if (globals->c64_rel
5fa80905
AC
7865 && c64_symbol_adjust (h, value, sym_sec, info, &frag_value))
7866 signed_addend = (value | h->target_internal) - frag_value;
7867 else
7868 frag_value = value | h->target_internal;
7869
c50aec72 7870 value = aarch64_calculate_got_entry_vma (h, globals, info,
5fa80905 7871 frag_value,
a06ea964
NC
7872 output_bfd,
7873 unresolved_reloc_p);
ff07562f
JW
7874 /* Record the GOT entry address which will be used when generating
7875 RELATIVE relocation. */
7876 if (relative_reloc)
8d4edc5f 7877 got_entry_addr = value;
ff07562f 7878
2aff25ba 7879 if (aarch64_relocation_aginst_gp_p (bfd_r_type))
99ad26cb
JW
7880 addend = (globals->root.sgot->output_section->vma
7881 + globals->root.sgot->output_offset);
652afeef
TC
7882 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
7883 place, value,
99ad26cb 7884 addend, weak_undef_p);
a06ea964 7885 }
b53b1bed
JW
7886 else
7887 {
99ad26cb 7888 bfd_vma addend = 0;
b53b1bed
JW
7889 struct elf_aarch64_local_symbol *locals
7890 = elf_aarch64_locals (input_bfd);
7891
7892 if (locals == NULL)
7893 {
7894 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
4eca0228 7895 _bfd_error_handler
695344c0 7896 /* xgettext:c-format */
90b6238f 7897 (_("%pB: local symbol descriptor table be NULL when applying "
b53b1bed
JW
7898 "relocation %s against local symbol"),
7899 input_bfd, elfNN_aarch64_howto_table[howto_index].name);
7900 abort ();
7901 }
7902
ff07562f
JW
7903 got_entry_addr = (base_got->output_section->vma
7904 + base_got->output_offset + off);
b53b1bed
JW
7905
7906 if (!symbol_got_offset_mark_p (input_bfd, h, r_symndx))
7907 {
5fa80905
AC
7908 bfd_vma frag_value;
7909
d465be7b 7910 if (globals->c64_rel
5fa80905
AC
7911 && c64_symbol_adjust (h, value, sym_sec, info, &frag_value))
7912 signed_addend = (value | sym->st_target_internal) - frag_value;
7913 else
7914 frag_value = value | sym->st_target_internal;
7915
7916 bfd_put_64 (output_bfd, frag_value, base_got->contents + off);
b53b1bed 7917
ff07562f
JW
7918 /* For local symbol, we have done absolute relocation in static
7919 linking stage. While for shared library, we need to update the
7920 content of GOT entry according to the shared object's runtime
7921 base address. So, we need to generate a R_AARCH64_RELATIVE reloc
808c324b
MM
7922 for dynamic linker.
7923
7924 For any C64 binary we need to ensure there are RELATIVE
7925 relocations to initialise the capabilities. The only case when
7926 we would not want to emit such relocations is when the producing
7927 a relocatable object file (since such files should not have
7928 dynamic relocations). */
c50aec72
MM
7929 if (bfd_link_pic (info)
7930 || (!bfd_link_pic (info) && bfd_link_executable (info)
d465be7b 7931 && globals->c64_rel))
808c324b 7932 {
c0a10a86
MM
7933 /* We have not handled the case for weak undefined symbols in
7934 this clause. That is because we believe there can not be
7935 weak undefined symbols as we reach this clause. We believe
7936 that any symbol with WEAK binding in an object file would be
7937 put into the hash table (and hence go into the `h != NULL`
7938 clause above). The only time that `weak_undef_p` should be
7939 set for something not in the hash table is when we have
7940 removed a relocation by marking it as against the undefined
7941 symbol (e.g. during TLS relaxation). We only ever do that
7942 while also setting the relocation to R_AARCH64_NONE, so we
7943 would not see it in this clause. */
7944 BFD_ASSERT (!weak_undef_p);
808c324b 7945 relative_reloc = TRUE;
d465be7b 7946 c64_needs_frag_fixup = globals->c64_rel ? TRUE : FALSE;
808c324b 7947 }
b53b1bed
JW
7948
7949 symbol_got_offset_mark (input_bfd, h, r_symndx);
7950 }
7951
7952 /* Update the relocation value to GOT entry addr as we have transformed
7953 the direct data access into indirect data access through GOT. */
7954 value = got_entry_addr;
99ad26cb 7955
2aff25ba 7956 if (aarch64_relocation_aginst_gp_p (bfd_r_type))
99ad26cb
JW
7957 addend = base_got->output_section->vma + base_got->output_offset;
7958
652afeef
TC
7959 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
7960 place, value,
99ad26cb 7961 addend, weak_undef_p);
b53b1bed 7962 }
ff07562f 7963
808c324b
MM
7964 if (c64_needs_frag_fixup)
7965 {
d465be7b 7966 BFD_ASSERT (globals->c64_rel);
808c324b
MM
7967 /* For a C64 relative relocation, also add size and permissions into
7968 the frag. */
7969 bfd_reloc_status_type ret;
7970
7971 ret = c64_fixup_frag (input_bfd, info, bfd_r_type, sym, h,
7972 sym_sec, globals->root.srelgot,
7973 base_got->contents + off + 8,
7974 orig_value, 0, off);
7975
7976 if (ret != bfd_reloc_continue)
7977 return ret;
7978 }
7979
ff07562f
JW
7980 if (relative_reloc)
7981 {
7982 asection *s;
7983 Elf_Internal_Rela outrel;
7984
a1bdea65
SP
7985 enum elf_aarch64_reloc_type rtype = AARCH64_R (RELATIVE);
7986
8d4edc5f
SP
7987 s = globals->root.srelgot;
7988
d465be7b 7989 if (globals->c64_rel)
a1bdea65 7990 {
a1bdea65 7991 rtype = MORELLO_R (RELATIVE);
8d4edc5f 7992
d9f4f6fc
MM
7993 /* Ensure that Morello RELATIVE relocations for static non-PIE
7994 binaries are all stored in the same input section. This is
7995 done so that we can mark that section with
7996 __rela_dyn_{start,end} symbols for the runtime to find and
7997 initialise relocations with. */
aeba84b1 7998 if (static_pde (info))
8d4edc5f
SP
7999 s = globals->srelcaps;
8000
5fa80905 8001 outrel.r_addend = signed_addend;
a1bdea65
SP
8002 }
8003 else
8004 outrel.r_addend = orig_value;
8005
ff07562f
JW
8006 if (s == NULL)
8007 abort ();
8008
8009 outrel.r_offset = got_entry_addr;
a1bdea65 8010 outrel.r_info = ELFNN_R_INFO (0, rtype);
ff07562f
JW
8011 elf_append_rela (output_bfd, s, &outrel);
8012 }
a2e1db00
RL
8013 break;
8014
1408e112
MM
8015 case BFD_RELOC_MORELLO_TLSIE_ADR_GOTTPREL_PAGE20:
8016 case BFD_RELOC_MORELLO_TLSIE_ADD_LO12:
ce336788 8017 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
a6bb11b2 8018 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
3c12b054 8019 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
a6bb11b2 8020 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
a6bb11b2 8021 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
ce336788 8022 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
043bf05a 8023 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
73f925cc 8024 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
f69e4920 8025 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
77a69ff8 8026 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
a06ea964
NC
8027 if (globals->root.sgot == NULL)
8028 return bfd_reloc_notsupported;
8029
8030 value = (symbol_got_offset (input_bfd, h, r_symndx)
8031 + globals->root.sgot->output_section->vma
f44a1f8e 8032 + globals->root.sgot->output_offset);
a06ea964 8033
652afeef
TC
8034 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
8035 place, value,
caed7120 8036 0, weak_undef_p);
a06ea964
NC
8037 *unresolved_reloc_p = FALSE;
8038 break;
8039
7ba7cfe4 8040 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
94facae3 8041 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
3b957e5b
RL
8042 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
8043 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
94facae3
RL
8044 if (globals->root.sgot == NULL)
8045 return bfd_reloc_notsupported;
8046
8047 value = symbol_got_offset (input_bfd, h, r_symndx);
652afeef
TC
8048 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
8049 place, value,
94facae3
RL
8050 0, weak_undef_p);
8051 *unresolved_reloc_p = FALSE;
8052 break;
8053
6ffe9a1b 8054 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12:
40fbed84 8055 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12:
753999c1 8056 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
07c9aa07
JW
8057 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12:
8058 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC:
8059 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12:
8060 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC:
8061 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12:
8062 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC:
8063 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12:
8064 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC:
6ffe9a1b
JW
8065 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0:
8066 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
8067 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1:
8068 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC:
8069 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2:
652afeef
TC
8070 {
8071 if (!(weak_undef_p || elf_hash_table (info)->tls_sec))
8072 {
8073 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
8074 _bfd_error_handler
8075 /* xgettext:c-format */
8076 (_("%pB: TLS relocation %s against undefined symbol `%s'"),
8077 input_bfd, elfNN_aarch64_howto_table[howto_index].name,
8078 h->root.root.string);
8079 bfd_set_error (bfd_error_bad_value);
8080 return bfd_reloc_notsupported;
8081 }
8082
8083 bfd_vma def_value
8084 = weak_undef_p ? 0 : signed_addend - dtpoff_base (info);
8085 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
8086 place, value,
8087 def_value, weak_undef_p);
8088 break;
8089 }
40fbed84 8090
a6bb11b2
YZ
8091 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
8092 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
8093 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
e04ef022
RL
8094 case BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12:
8095 case BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
8096 case BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12:
8097 case BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
8098 case BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12:
8099 case BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
8100 case BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12:
8101 case BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
a6bb11b2
YZ
8102 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
8103 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
8104 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
8105 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
8106 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
652afeef
TC
8107 {
8108 if (!(weak_undef_p || elf_hash_table (info)->tls_sec))
8109 {
8110 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
8111 _bfd_error_handler
8112 /* xgettext:c-format */
8113 (_("%pB: TLS relocation %s against undefined symbol `%s'"),
8114 input_bfd, elfNN_aarch64_howto_table[howto_index].name,
8115 h->root.root.string);
8116 bfd_set_error (bfd_error_bad_value);
8117 return bfd_reloc_notsupported;
8118 }
26c32295
MM
8119 /* Cannot have a Local-Exec relocation against a symbol that is not in
8120 the current binary. */
8121 if (!TLS_SYMBOL_REFERENCES_LOCAL (info, h))
8122 {
8123 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
8124 _bfd_error_handler
8125 /* xgettext:c-format */
8126 (_("%pB: Local-Exec TLS relocation %s against non-local "
8127 "symbol `%s'"),
8128 input_bfd, elfNN_aarch64_howto_table[howto_index].name,
8129 h->root.root.string);
8130 bfd_set_error (bfd_error_bad_value);
8131 return bfd_reloc_notsupported;
8132 }
652afeef
TC
8133
8134 bfd_vma def_value
8135 = weak_undef_p ? 0 : signed_addend - tpoff_base (info);
8136 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
8137 place, value,
8138 def_value, weak_undef_p);
8139 *unresolved_reloc_p = FALSE;
8140 break;
8141 }
a06ea964 8142
f955cccf 8143 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
a6bb11b2 8144 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
4ca9b406 8145 case BFD_RELOC_MORELLO_TLSDESC_ADR_PAGE20:
389b8029 8146 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
a6bb11b2 8147 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
f955cccf 8148 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12:
1ada945d 8149 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
4ca9b406 8150 case BFD_RELOC_MORELLO_TLSDESC_LD128_LO12:
a06ea964
NC
8151 if (globals->root.sgot == NULL)
8152 return bfd_reloc_notsupported;
a06ea964
NC
8153 value = (symbol_tlsdesc_got_offset (input_bfd, h, r_symndx)
8154 + globals->root.sgotplt->output_section->vma
f44a1f8e 8155 + globals->root.sgotplt->output_offset
a06ea964
NC
8156 + globals->sgotplt_jump_table_size);
8157
652afeef
TC
8158 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
8159 place, value,
caed7120 8160 0, weak_undef_p);
a06ea964
NC
8161 *unresolved_reloc_p = FALSE;
8162 break;
8163
0484b454
RL
8164 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
8165 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
8166 if (globals->root.sgot == NULL)
8167 return bfd_reloc_notsupported;
8168
8169 value = (symbol_tlsdesc_got_offset (input_bfd, h, r_symndx)
8170 + globals->root.sgotplt->output_section->vma
8171 + globals->root.sgotplt->output_offset
8172 + globals->sgotplt_jump_table_size);
8173
8174 value -= (globals->root.sgot->output_section->vma
8175 + globals->root.sgot->output_offset);
8176
652afeef
TC
8177 value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
8178 place, value,
0484b454
RL
8179 0, weak_undef_p);
8180 *unresolved_reloc_p = FALSE;
8181 break;
8182
40bbb79e
SP
8183 case BFD_RELOC_MORELLO_CAPINIT:
8184 {
8185 Elf_Internal_Rela outrel;
8186
8187 if (input_section->flags & SEC_READONLY)
8188 {
8189 _bfd_error_handler
8190 /* xgettext:c-format */
8191 (_("%pB: capability relocation section must be writable"),
8192 input_bfd);
8193 bfd_set_error (bfd_error_bad_value);
8194 return bfd_reloc_notsupported;
8195 }
8196
8197 outrel.r_offset = _bfd_elf_section_offset (output_bfd, info,
8198 input_section,
8199 rel->r_offset);
8200
8201 outrel.r_offset += (input_section->output_section->vma
8202 + input_section->output_offset);
8203
8204 /* Capability-aligned. */
8205 if (outrel.r_offset & 0xf)
8206 return bfd_reloc_overflow;
8207
40bbb79e
SP
8208 outrel.r_addend = signed_addend;
8209
ece841e8 8210 if (!c64_needs_relocation (info, h))
c0a10a86
MM
8211 {
8212 /* If we know this symbol does not need a C64 dynamic relocation
8213 then it must be because this is an undefined weak symbol which
8214 can not find a definition at runtime.
8215
8216 To handle that we just ensure that we've put a zero into the
8217 binary file at this point and mark the relocation as resolved.
8218 */
8219 value = 0;
8220 *unresolved_reloc_p = FALSE;
8221 break;
8222 }
9a68ca09
MM
8223 /* Emit a dynamic relocation if we are handling a symbol which the
8224 dynamic linker will be told about. */
c0a10a86
MM
8225 else if (h != NULL
8226 && h->dynindx != -1
8227 && globals->root.dynamic_sections_created
8228 && !SYMBOL_REFERENCES_LOCAL (info, h))
9a68ca09
MM
8229 {
8230 outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
8231 /* Dynamic symbols will be handled by the dynamic loader. Hence
8232 there is no need to fill a fragment with a value even if there
8233 is a value that we would use assuming no interception. */
8234 value = 0;
8235 }
40bbb79e 8236 else
5fa80905 8237 {
9a68ca09
MM
8238 /* This relocation will point into the object we are building
8239 (either because this is a statically linked executable and
8240 hence there is only one object it could point at, or because
8241 we know it will resolve locally even though this is
8242 dynamically linked).
8243
8244 Hence we want to emit a RELATIVE relocation rather than a
8245 CAPINIT one. */
8246 bfd_reloc_status_type ret;
8247
8248 value |= (h != NULL ? h->target_internal : sym->st_target_internal);
8249 ret = c64_fixup_frag (input_bfd, info, bfd_r_type, sym, h, sym_sec,
8250 input_section, hit_data + 8, value,
8251 signed_addend, rel->r_offset);
8252
8253 if (ret != bfd_reloc_continue)
8254 return ret;
8255
8256 outrel.r_info = ELFNN_R_INFO (0, MORELLO_R (RELATIVE));
8257
8258 /* Symbols without size information get bounds to the
8259 whole section: adjust the base of the capability to the
8260 start of the section and set the addend to obtain the
8261 correct address for the symbol. */
8262 bfd_vma new_value;
8263 if (c64_symbol_adjust (h, value, sym_sec, info, &new_value))
8264 {
8265 outrel.r_addend += (value - new_value);
8266 value = new_value;
8267 }
5fa80905 8268 }
40bbb79e
SP
8269
8270 asection *s = globals->srelcaps;
8271
8272 elf_append_rela (output_bfd, s, &outrel);
8273 *unresolved_reloc_p = FALSE;
8274 }
8275 break;
8276
a06ea964
NC
8277 default:
8278 return bfd_reloc_notsupported;
8279 }
8280
8281 if (saved_addend)
8282 *saved_addend = value;
8283
8284 /* Only apply the final relocation in a sequence. */
8285 if (save_addend)
8286 return bfd_reloc_continue;
8287
caed7120
YZ
8288 return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type,
8289 howto, value);
a06ea964
NC
8290}
8291
2d0ca824
YN
8292/* LP64 and ILP32 operates on x- and w-registers respectively.
8293 Next definitions take into account the difference between
8294 corresponding machine codes. R means x-register if the target
8295 arch is LP64, and w-register if the target is ILP32. */
8296
8297#if ARCH_SIZE == 64
8298# define add_R0_R0 (0x91000000)
8299# define add_R0_R0_R1 (0x8b000020)
8300# define add_R0_R1 (0x91400020)
8301# define ldr_R0 (0x58000000)
8302# define ldr_R0_mask(i) (i & 0xffffffe0)
8303# define ldr_R0_x0 (0xf9400000)
8304# define ldr_hw_R0 (0xf2a00000)
8305# define movk_R0 (0xf2800000)
8306# define movz_R0 (0xd2a00000)
8307# define movz_hw_R0 (0xd2c00000)
8308#else /*ARCH_SIZE == 32 */
8309# define add_R0_R0 (0x11000000)
8310# define add_R0_R0_R1 (0x0b000020)
8311# define add_R0_R1 (0x11400020)
8312# define ldr_R0 (0x18000000)
8313# define ldr_R0_mask(i) (i & 0xbfffffe0)
8314# define ldr_R0_x0 (0xb9400000)
8315# define ldr_hw_R0 (0x72a00000)
8316# define movk_R0 (0x72800000)
8317# define movz_R0 (0x52a00000)
8318# define movz_hw_R0 (0x52c00000)
8319#endif
8320
ece841e8
MM
8321/* C64 only instructions. */
8322#define add_C0_C0 (0x02000000)
8323#define ldp_X0_X1_C0 (0xa9400400)
8324#define add_C0_C2_X0 (0xc2a06040)
8325#define scbnds_C0_C0_X1 (0xc2c10000)
8326
9fca35fc
TC
8327/* Structure to hold payload for _bfd_aarch64_erratum_843419_clear_stub,
8328 it is used to identify the stub information to reset. */
8329
8330struct erratum_843419_branch_to_stub_clear_data
8331{
8332 bfd_vma adrp_offset;
8333 asection *output_section;
8334};
8335
8336/* Clear the erratum information for GEN_ENTRY if the ADRP_OFFSET and
8337 section inside IN_ARG matches. The clearing is done by setting the
8338 stub_type to none. */
8339
8340static bfd_boolean
8341_bfd_aarch64_erratum_843419_clear_stub (struct bfd_hash_entry *gen_entry,
8342 void *in_arg)
8343{
8344 struct elf_aarch64_stub_hash_entry *stub_entry
8345 = (struct elf_aarch64_stub_hash_entry *) gen_entry;
8346 struct erratum_843419_branch_to_stub_clear_data *data
8347 = (struct erratum_843419_branch_to_stub_clear_data *) in_arg;
8348
8349 if (stub_entry->target_section != data->output_section
8350 || stub_entry->stub_type != aarch64_stub_erratum_843419_veneer
8351 || stub_entry->adrp_offset != data->adrp_offset)
8352 return TRUE;
8353
8354 /* Change the stub type instead of removing the entry, removing from the hash
8355 table would be slower and we have already reserved the memory for the entry
8356 so there wouldn't be much gain. Changing the stub also keeps around a
8357 record of what was there before. */
8358 stub_entry->stub_type = aarch64_stub_none;
8359
8360 /* We're done and there could have been only one matching stub at that
8361 particular offset, so abort further traversal. */
8362 return FALSE;
8363}
8364
8365/* TLS Relaxations may relax an adrp sequence that matches the erratum 843419
8366 sequence. In this case the erratum no longer applies and we need to remove
8367 the entry from the pending stub generation. This clears matching adrp insn
8368 at ADRP_OFFSET in INPUT_SECTION in the stub table defined in GLOBALS. */
8369
8370static void
8371clear_erratum_843419_entry (struct elf_aarch64_link_hash_table *globals,
8372 bfd_vma adrp_offset, asection *input_section)
8373{
739b5c9c 8374 if (globals->fix_erratum_843419 & ERRAT_ADRP)
9fca35fc
TC
8375 {
8376 struct erratum_843419_branch_to_stub_clear_data data;
8377 data.adrp_offset = adrp_offset;
8378 data.output_section = input_section;
8379
8380 bfd_hash_traverse (&globals->stub_hash_table,
8381 _bfd_aarch64_erratum_843419_clear_stub, &data);
8382 }
8383}
8384
4ca9b406
SP
8385#define BUILD_MOVZ(_reg, _imm) (movz_R0 \
8386 | ((((_imm) >> 16) & 0xffff) << 5) \
8387 | (_reg))
8388#define BUILD_MOVK(_reg, _imm) (movk_R0 | (((_imm) & 0xffff) << 5) | (_reg))
8389
a06ea964
NC
8390/* Handle TLS relaxations. Relaxing is possible for symbols that use
8391 R_AARCH64_TLSDESC_ADR_{PAGE, LD64_LO12_NC, ADD_LO12_NC} during a static
8392 link.
8393
8394 Return bfd_reloc_ok if we're done, bfd_reloc_continue if the caller
8395 is to then call final_link_relocate. Return other values in the
8396 case of error. */
8397
8398static bfd_reloc_status_type
4ca9b406
SP
8399elfNN_aarch64_tls_relax (bfd *input_bfd, struct bfd_link_info *info,
8400 asection *input_section,
9fca35fc 8401 bfd_byte *contents, Elf_Internal_Rela *rel,
ece841e8 8402 struct elf_link_hash_entry *h)
a06ea964 8403{
8a4f8507 8404 bfd_boolean local_exec = (bfd_link_executable (info)
1408e112 8405 && TLS_SYMBOL_REFERENCES_LOCAL (info, h));
cec5225b 8406 unsigned int r_type = ELFNN_R_TYPE (rel->r_info);
a06ea964 8407 unsigned long insn;
4ca9b406 8408 struct elf_aarch64_link_hash_table *globals = elf_aarch64_hash_table (info);
a06ea964
NC
8409
8410 BFD_ASSERT (globals && input_bfd && contents && rel);
8411
0aa13fee 8412 switch (elfNN_aarch64_bfd_reloc_from_type (input_bfd, r_type))
a06ea964 8413 {
a6bb11b2 8414 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
ce336788 8415 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
8a4f8507 8416 if (local_exec)
a06ea964
NC
8417 {
8418 /* GD->LE relaxation:
2d0ca824 8419 adrp x0, :tlsgd:var => movz R0, :tprel_g1:var
a06ea964 8420 or
2d0ca824
YN
8421 adrp x0, :tlsdesc:var => movz R0, :tprel_g1:var
8422
8423 Where R is x for LP64, and w for ILP32. */
8424 bfd_putl32 (movz_R0, contents + rel->r_offset);
9fca35fc
TC
8425 /* We have relaxed the adrp into a mov, we may have to clear any
8426 pending erratum fixes. */
8427 clear_erratum_843419_entry (globals, rel->r_offset, input_section);
a06ea964
NC
8428 return bfd_reloc_continue;
8429 }
8430 else
8431 {
8432 /* GD->IE relaxation:
8433 adrp x0, :tlsgd:var => adrp x0, :gottprel:var
8434 or
8435 adrp x0, :tlsdesc:var => adrp x0, :gottprel:var
8436 */
a06ea964
NC
8437 return bfd_reloc_continue;
8438 }
8439
389b8029
MS
8440 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
8441 BFD_ASSERT (0);
8442 break;
8443
1ada945d 8444 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
8a4f8507 8445 if (local_exec)
1ada945d
MS
8446 {
8447 /* Tiny TLSDESC->LE relaxation:
07d6d2b8
AM
8448 ldr x1, :tlsdesc:var => movz R0, #:tprel_g1:var
8449 adr x0, :tlsdesc:var => movk R0, #:tprel_g0_nc:var
1ada945d 8450 .tlsdesccall var
07d6d2b8 8451 blr x1 => nop
2d0ca824
YN
8452
8453 Where R is x for LP64, and w for ILP32. */
1ada945d
MS
8454 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (TLSDESC_ADR_PREL21));
8455 BFD_ASSERT (ELFNN_R_TYPE (rel[2].r_info) == AARCH64_R (TLSDESC_CALL));
8456
8457 rel[1].r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
8458 AARCH64_R (TLSLE_MOVW_TPREL_G0_NC));
8459 rel[2].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
8460
2d0ca824
YN
8461 bfd_putl32 (movz_R0, contents + rel->r_offset);
8462 bfd_putl32 (movk_R0, contents + rel->r_offset + 4);
1ada945d
MS
8463 bfd_putl32 (INSN_NOP, contents + rel->r_offset + 8);
8464 return bfd_reloc_continue;
8465 }
8466 else
8467 {
8468 /* Tiny TLSDESC->IE relaxation:
07d6d2b8
AM
8469 ldr x1, :tlsdesc:var => ldr x0, :gottprel:var
8470 adr x0, :tlsdesc:var => nop
1ada945d 8471 .tlsdesccall var
07d6d2b8 8472 blr x1 => nop
1ada945d
MS
8473 */
8474 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (TLSDESC_ADR_PREL21));
8475 BFD_ASSERT (ELFNN_R_TYPE (rel[2].r_info) == AARCH64_R (TLSDESC_CALL));
8476
8477 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
8478 rel[2].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
8479
2d0ca824 8480 bfd_putl32 (ldr_R0, contents + rel->r_offset);
1ada945d
MS
8481 bfd_putl32 (INSN_NOP, contents + rel->r_offset + 4);
8482 bfd_putl32 (INSN_NOP, contents + rel->r_offset + 8);
8483 return bfd_reloc_continue;
8484 }
8485
3c12b054 8486 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
8a4f8507 8487 if (local_exec)
3c12b054
MS
8488 {
8489 /* Tiny GD->LE relaxation:
07d6d2b8
AM
8490 adr x0, :tlsgd:var => mrs x1, tpidr_el0
8491 bl __tls_get_addr => add R0, R1, #:tprel_hi12:x, lsl #12
8492 nop => add R0, R0, #:tprel_lo12_nc:x
2d0ca824
YN
8493
8494 Where R is x for LP64, and x for Ilp32. */
3c12b054
MS
8495
8496 /* First kill the tls_get_addr reloc on the bl instruction. */
8497 BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
8498
8499 bfd_putl32 (0xd53bd041, contents + rel->r_offset + 0);
2d0ca824
YN
8500 bfd_putl32 (add_R0_R1, contents + rel->r_offset + 4);
8501 bfd_putl32 (add_R0_R0, contents + rel->r_offset + 8);
3c12b054
MS
8502
8503 rel[1].r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
8504 AARCH64_R (TLSLE_ADD_TPREL_LO12_NC));
8505 rel[1].r_offset = rel->r_offset + 8;
8506
8507 /* Move the current relocation to the second instruction in
8508 the sequence. */
8509 rel->r_offset += 4;
8510 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
8511 AARCH64_R (TLSLE_ADD_TPREL_HI12));
8512 return bfd_reloc_continue;
8513 }
8514 else
8515 {
8516 /* Tiny GD->IE relaxation:
07d6d2b8
AM
8517 adr x0, :tlsgd:var => ldr R0, :gottprel:var
8518 bl __tls_get_addr => mrs x1, tpidr_el0
8519 nop => add R0, R0, R1
2d0ca824
YN
8520
8521 Where R is x for LP64, and w for Ilp32. */
3c12b054
MS
8522
8523 /* First kill the tls_get_addr reloc on the bl instruction. */
8524 BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
8525 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
8526
2d0ca824 8527 bfd_putl32 (ldr_R0, contents + rel->r_offset);
3c12b054 8528 bfd_putl32 (0xd53bd041, contents + rel->r_offset + 4);
2d0ca824 8529 bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 8);
3c12b054
MS
8530 return bfd_reloc_continue;
8531 }
8532
ac734732
RL
8533#if ARCH_SIZE == 64
8534 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
8535 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (TLSGD_MOVW_G0_NC));
8536 BFD_ASSERT (rel->r_offset + 12 == rel[2].r_offset);
8537 BFD_ASSERT (ELFNN_R_TYPE (rel[2].r_info) == AARCH64_R (CALL26));
8538
8a4f8507 8539 if (local_exec)
ac734732
RL
8540 {
8541 /* Large GD->LE relaxation:
07d6d2b8 8542 movz x0, #:tlsgd_g1:var => movz x0, #:tprel_g2:var, lsl #32
ac734732 8543 movk x0, #:tlsgd_g0_nc:var => movk x0, #:tprel_g1_nc:var, lsl #16
07d6d2b8
AM
8544 add x0, gp, x0 => movk x0, #:tprel_g0_nc:var
8545 bl __tls_get_addr => mrs x1, tpidr_el0
8546 nop => add x0, x0, x1
ac734732
RL
8547 */
8548 rel[2].r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
8549 AARCH64_R (TLSLE_MOVW_TPREL_G0_NC));
8550 rel[2].r_offset = rel->r_offset + 8;
8551
2d0ca824
YN
8552 bfd_putl32 (movz_hw_R0, contents + rel->r_offset + 0);
8553 bfd_putl32 (ldr_hw_R0, contents + rel->r_offset + 4);
8554 bfd_putl32 (movk_R0, contents + rel->r_offset + 8);
ac734732 8555 bfd_putl32 (0xd53bd041, contents + rel->r_offset + 12);
2d0ca824 8556 bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 16);
ac734732
RL
8557 }
8558 else
8559 {
8560 /* Large GD->IE relaxation:
07d6d2b8 8561 movz x0, #:tlsgd_g1:var => movz x0, #:gottprel_g1:var, lsl #16
ac734732 8562 movk x0, #:tlsgd_g0_nc:var => movk x0, #:gottprel_g0_nc:var
07d6d2b8
AM
8563 add x0, gp, x0 => ldr x0, [gp, x0]
8564 bl __tls_get_addr => mrs x1, tpidr_el0
8565 nop => add x0, x0, x1
ac734732
RL
8566 */
8567 rel[2].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
8568 bfd_putl32 (0xd2a80000, contents + rel->r_offset + 0);
2d0ca824 8569 bfd_putl32 (ldr_R0, contents + rel->r_offset + 8);
ac734732 8570 bfd_putl32 (0xd53bd041, contents + rel->r_offset + 12);
2d0ca824 8571 bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 16);
ac734732
RL
8572 }
8573 return bfd_reloc_continue;
8574
8575 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
8576 return bfd_reloc_continue;
8577#endif
8578
ece841e8
MM
8579 case BFD_RELOC_MORELLO_TLSIE_ADR_GOTTPREL_PAGE20:
8580 /* IE->LE relaxation:
8581 adrp c0, :gottprel:var => adrp c0, __var_data
8582 Instruction does not change (just the relocation on it). */
8583 return bfd_reloc_continue;
8584
8585 case BFD_RELOC_MORELLO_TLSIE_ADD_LO12:
8586 /* IE->LE relaxation:
8587 add c0, c0, :gottprel_lo12:var => add c0, c0, :lo12:__var_data
8588 Instruction does not change (just the relocation on it). */
8589 return bfd_reloc_continue;
8590
043bf05a
MS
8591 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
8592 return bfd_reloc_continue;
8593
a6bb11b2 8594 case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
8a4f8507 8595 if (local_exec)
a06ea964
NC
8596 {
8597 /* GD->LE relaxation:
8598 ldr xd, [x0, #:tlsdesc_lo12:var] => movk x0, :tprel_g0_nc:var
2d0ca824
YN
8599
8600 Where R is x for lp64 mode, and w for ILP32 mode. */
8601 bfd_putl32 (movk_R0, contents + rel->r_offset);
a06ea964
NC
8602 return bfd_reloc_continue;
8603 }
8604 else
8605 {
8606 /* GD->IE relaxation:
2d0ca824
YN
8607 ldr xd, [x0, #:tlsdesc_lo12:var] => ldr R0, [x0, #:gottprel_lo12:var]
8608
8609 Where R is x for lp64 mode, and w for ILP32 mode. */
a06ea964 8610 insn = bfd_getl32 (contents + rel->r_offset);
2d0ca824 8611 bfd_putl32 (ldr_R0_mask (insn), contents + rel->r_offset);
a06ea964
NC
8612 return bfd_reloc_continue;
8613 }
8614
a6bb11b2 8615 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
8a4f8507 8616 if (local_exec)
a06ea964
NC
8617 {
8618 /* GD->LE relaxation
07d6d2b8
AM
8619 add x0, #:tlsgd_lo12:var => movk R0, :tprel_g0_nc:var
8620 bl __tls_get_addr => mrs x1, tpidr_el0
8621 nop => add R0, R1, R0
2d0ca824
YN
8622
8623 Where R is x for lp64 mode, and w for ILP32 mode. */
a06ea964
NC
8624
8625 /* First kill the tls_get_addr reloc on the bl instruction. */
8626 BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
cec5225b 8627 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
a06ea964 8628
2d0ca824 8629 bfd_putl32 (movk_R0, contents + rel->r_offset);
a06ea964 8630 bfd_putl32 (0xd53bd041, contents + rel->r_offset + 4);
2d0ca824 8631 bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 8);
a06ea964
NC
8632 return bfd_reloc_continue;
8633 }
8634 else
8635 {
8636 /* GD->IE relaxation
07d6d2b8
AM
8637 ADD x0, #:tlsgd_lo12:var => ldr R0, [x0, #:gottprel_lo12:var]
8638 BL __tls_get_addr => mrs x1, tpidr_el0
a06ea964 8639 R_AARCH64_CALL26
07d6d2b8 8640 NOP => add R0, R1, R0
5cd1d8bc
YN
8641
8642 Where R is x for lp64 mode, and w for ilp32 mode. */
a06ea964 8643
a6bb11b2 8644 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
a06ea964
NC
8645
8646 /* Remove the relocation on the BL instruction. */
cec5225b 8647 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
a06ea964 8648
a06ea964
NC
8649 /* We choose to fixup the BL and NOP instructions using the
8650 offset from the second relocation to allow flexibility in
8651 scheduling instructions between the ADD and BL. */
2d0ca824 8652 bfd_putl32 (ldr_R0_x0, contents + rel->r_offset);
5cd1d8bc 8653 bfd_putl32 (0xd53bd041, contents + rel[1].r_offset);
2d0ca824 8654 bfd_putl32 (add_R0_R0_R1, contents + rel[1].r_offset + 4);
a06ea964
NC
8655 return bfd_reloc_continue;
8656 }
8657
ece841e8
MM
8658 case BFD_RELOC_MORELLO_TLSDESC_ADR_PAGE20:
8659 /* GD->IE relaxation:
8660 adrp c0, :tlsdesc:var => adrp c0, :gottprel:var
8661 GD->LE relaxation:
8662 adrp c0, :tlsdesc:var => adrp c0, __var_data
8663 (No change in instructions, just need a different relocation). */
8664 return bfd_reloc_continue;
8665
8666 case BFD_RELOC_MORELLO_TLSDESC_LD128_LO12:
4ca9b406 8667 /* GD->LE relaxation:
ece841e8
MM
8668 ldr c1, [c0, #:tlsdesc_lo12:var] => add c0, c0, :lo12:__var_data
8669 GD->IE relaxation:
8670 ldr c1, [c0, #:tlsdesc_lo12:var] => add c0, c0, :gottprel_lo12:__var_data
8671 (same relaxation in terms of instruction change, only different in
8672 terms of relocation that is used). */
8673 bfd_putl32 (add_C0_C0, contents + rel->r_offset);
8674 return bfd_reloc_continue;
8675
8676 case BFD_RELOC_MORELLO_TLSDESC_CALL:
8677 /* GD->LE relaxation AND GD->IE relaxation:
8678 nop => add c0, c2, x0
8679 blr cd => scbnds c0, c0, x1 */
8680 bfd_putl32 (add_C0_C2_X0, contents + rel->r_offset - 4);
8681 bfd_putl32 (scbnds_C0_C0_X1, contents + rel->r_offset);
8682 return bfd_reloc_ok;
4ca9b406 8683
f955cccf 8684 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
ece841e8
MM
8685 /* GD->LE relaxation AND GD->IE relaxation:
8686 add c0, c0, :tlsdesc_lo12:var => ldp x0, x1, [c0] */
8687 if (ELFNN_R_TYPE (rel[1].r_info) == MORELLO_R (TLSDESC_CALL))
4ca9b406 8688 {
ece841e8
MM
8689 bfd_putl32 (ldp_X0_X1_C0, contents + rel->r_offset);
8690 return bfd_reloc_ok;
4ca9b406
SP
8691 }
8692
8693 /* Fall through. */
8694 case BFD_RELOC_AARCH64_TLSDESC_ADD:
a6bb11b2 8695 case BFD_RELOC_AARCH64_TLSDESC_CALL:
a06ea964 8696 /* GD->IE/LE relaxation:
07d6d2b8
AM
8697 add x0, x0, #:tlsdesc_lo12:var => nop
8698 blr xd => nop
a06ea964
NC
8699 */
8700 bfd_putl32 (INSN_NOP, contents + rel->r_offset);
8701 return bfd_reloc_ok;
8702
0484b454 8703 case BFD_RELOC_AARCH64_TLSDESC_LDR:
8a4f8507 8704 if (local_exec)
0484b454
RL
8705 {
8706 /* GD->LE relaxation:
2d0ca824
YN
8707 ldr xd, [gp, xn] => movk R0, #:tprel_g0_nc:var
8708
8709 Where R is x for lp64 mode, and w for ILP32 mode. */
8710 bfd_putl32 (movk_R0, contents + rel->r_offset);
0484b454
RL
8711 return bfd_reloc_continue;
8712 }
8713 else
8714 {
8715 /* GD->IE relaxation:
2d0ca824
YN
8716 ldr xd, [gp, xn] => ldr R0, [gp, xn]
8717
8718 Where R is x for lp64 mode, and w for ILP32 mode. */
0484b454 8719 insn = bfd_getl32 (contents + rel->r_offset);
2d0ca824 8720 bfd_putl32 (ldr_R0_mask (insn), contents + rel->r_offset);
0484b454
RL
8721 return bfd_reloc_ok;
8722 }
8723
8724 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
8725 /* GD->LE relaxation:
2d0ca824 8726 movk xd, #:tlsdesc_off_g0_nc:var => movk R0, #:tprel_g1_nc:var, lsl #16
0484b454 8727 GD->IE relaxation:
2d0ca824
YN
8728 movk xd, #:tlsdesc_off_g0_nc:var => movk Rd, #:gottprel_g0_nc:var
8729
8730 Where R is x for lp64 mode, and w for ILP32 mode. */
8a4f8507 8731 if (local_exec)
2d0ca824 8732 bfd_putl32 (ldr_hw_R0, contents + rel->r_offset);
0484b454
RL
8733 return bfd_reloc_continue;
8734
8735 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
8a4f8507 8736 if (local_exec)
0484b454
RL
8737 {
8738 /* GD->LE relaxation:
2d0ca824
YN
8739 movz xd, #:tlsdesc_off_g1:var => movz R0, #:tprel_g2:var, lsl #32
8740
8741 Where R is x for lp64 mode, and w for ILP32 mode. */
8742 bfd_putl32 (movz_hw_R0, contents + rel->r_offset);
0484b454
RL
8743 return bfd_reloc_continue;
8744 }
8745 else
8746 {
8747 /* GD->IE relaxation:
2d0ca824
YN
8748 movz xd, #:tlsdesc_off_g1:var => movz Rd, #:gottprel_g1:var, lsl #16
8749
8750 Where R is x for lp64 mode, and w for ILP32 mode. */
0484b454 8751 insn = bfd_getl32 (contents + rel->r_offset);
2d0ca824 8752 bfd_putl32 (movz_R0 | (insn & 0x1f), contents + rel->r_offset);
0484b454
RL
8753 return bfd_reloc_continue;
8754 }
8755
a6bb11b2 8756 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
a06ea964 8757 /* IE->LE relaxation:
07d6d2b8 8758 adrp xd, :gottprel:var => movz Rd, :tprel_g1:var
2d0ca824
YN
8759
8760 Where R is x for lp64 mode, and w for ILP32 mode. */
8a4f8507 8761 if (local_exec)
a06ea964
NC
8762 {
8763 insn = bfd_getl32 (contents + rel->r_offset);
2d0ca824 8764 bfd_putl32 (movz_R0 | (insn & 0x1f), contents + rel->r_offset);
9fca35fc
TC
8765 /* We have relaxed the adrp into a mov, we may have to clear any
8766 pending erratum fixes. */
8767 clear_erratum_843419_entry (globals, rel->r_offset, input_section);
a06ea964
NC
8768 }
8769 return bfd_reloc_continue;
8770
a6bb11b2 8771 case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
a06ea964 8772 /* IE->LE relaxation:
07d6d2b8 8773 ldr xd, [xm, #:gottprel_lo12:var] => movk Rd, :tprel_g0_nc:var
2d0ca824
YN
8774
8775 Where R is x for lp64 mode, and w for ILP32 mode. */
8a4f8507 8776 if (local_exec)
a06ea964
NC
8777 {
8778 insn = bfd_getl32 (contents + rel->r_offset);
2d0ca824 8779 bfd_putl32 (movk_R0 | (insn & 0x1f), contents + rel->r_offset);
a06ea964
NC
8780 }
8781 return bfd_reloc_continue;
8782
259364ad
JW
8783 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
8784 /* LD->LE relaxation (tiny):
8785 adr x0, :tlsldm:x => mrs x0, tpidr_el0
c1fc2d7e
YN
8786 bl __tls_get_addr => add R0, R0, TCB_SIZE
8787
8788 Where R is x for lp64 mode, and w for ilp32 mode. */
8a4f8507 8789 if (local_exec)
259364ad
JW
8790 {
8791 BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
8792 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
8793 /* No need of CALL26 relocation for tls_get_addr. */
8794 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
8795 bfd_putl32 (0xd53bd040, contents + rel->r_offset + 0);
bf3eaebd 8796 bfd_putl32 (add_R0_R0 | (TCB_SIZE (input_bfd) << 10),
2d0ca824 8797 contents + rel->r_offset + 4);
259364ad
JW
8798 return bfd_reloc_ok;
8799 }
8800 return bfd_reloc_continue;
8801
8802 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
8803 /* LD->LE relaxation (small):
8804 adrp x0, :tlsldm:x => mrs x0, tpidr_el0
8805 */
8a4f8507 8806 if (local_exec)
259364ad
JW
8807 {
8808 bfd_putl32 (0xd53bd040, contents + rel->r_offset);
8809 return bfd_reloc_ok;
8810 }
8811 return bfd_reloc_continue;
8812
8813 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
8814 /* LD->LE relaxation (small):
c1fc2d7e 8815 add x0, #:tlsldm_lo12:x => add R0, R0, TCB_SIZE
259364ad 8816 bl __tls_get_addr => nop
c1fc2d7e
YN
8817
8818 Where R is x for lp64 mode, and w for ilp32 mode. */
8a4f8507 8819 if (local_exec)
259364ad
JW
8820 {
8821 BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
8822 BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
8823 /* No need of CALL26 relocation for tls_get_addr. */
8824 rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
bf3eaebd 8825 bfd_putl32 (add_R0_R0 | (TCB_SIZE (input_bfd) << 10),
2d0ca824 8826 contents + rel->r_offset + 0);
c1fc2d7e 8827 bfd_putl32 (INSN_NOP, contents + rel->r_offset + 4);
259364ad
JW
8828 return bfd_reloc_ok;
8829 }
8830 return bfd_reloc_continue;
8831
a06ea964
NC
8832 default:
8833 return bfd_reloc_continue;
8834 }
8835
8836 return bfd_reloc_ok;
8837}
8838
8839/* Relocate an AArch64 ELF section. */
8840
ece841e8
MM
8841static bfd_vma
8842c64_populate_tls_data_stub (struct elf_aarch64_link_hash_table *globals,
8843 bfd *input_bfd,
8844 struct elf_link_hash_entry *h,
8845 unsigned long r_symndx,
8846 bfd_vma offset, bfd_vma size,
8847 bfd *output_bfd)
8848{
8849 struct elf_c64_tls_data_stub_hash_entry *found
8850 = c64_tls_stub_find (h, input_bfd, r_symndx, globals);
8851 BFD_ASSERT (found);
8852
8853 if (!found->populated)
8854 {
8855 bfd_put_NN (output_bfd, offset,
8856 globals->sc64_tls_stubs->contents +
8857 found->tls_stub_offset);
8858 bfd_put_NN (output_bfd, size,
8859 globals->sc64_tls_stubs->contents +
8860 found->tls_stub_offset + 8);
8861 found->populated = TRUE;
8862 }
8863
8864 return globals->sc64_tls_stubs->output_section->vma
8865 + globals->sc64_tls_stubs->output_offset
8866 + found->tls_stub_offset;
8867}
8868
8869
a06ea964 8870static bfd_boolean
cec5225b 8871elfNN_aarch64_relocate_section (bfd *output_bfd,
a06ea964
NC
8872 struct bfd_link_info *info,
8873 bfd *input_bfd,
8874 asection *input_section,
8875 bfd_byte *contents,
8876 Elf_Internal_Rela *relocs,
8877 Elf_Internal_Sym *local_syms,
8878 asection **local_sections)
8879{
26c32295 8880 bfd_boolean ret = TRUE;
a06ea964
NC
8881 Elf_Internal_Shdr *symtab_hdr;
8882 struct elf_link_hash_entry **sym_hashes;
8883 Elf_Internal_Rela *rel;
8884 Elf_Internal_Rela *relend;
8885 const char *name;
cec5225b 8886 struct elf_aarch64_link_hash_table *globals;
a06ea964
NC
8887 bfd_boolean save_addend = FALSE;
8888 bfd_vma addend = 0;
8889
cec5225b 8890 globals = elf_aarch64_hash_table (info);
a06ea964
NC
8891
8892 symtab_hdr = &elf_symtab_hdr (input_bfd);
8893 sym_hashes = elf_sym_hashes (input_bfd);
8894
8895 rel = relocs;
8896 relend = relocs + input_section->reloc_count;
8897 for (; rel < relend; rel++)
8898 {
8899 unsigned int r_type;
a6bb11b2
YZ
8900 bfd_reloc_code_real_type bfd_r_type;
8901 bfd_reloc_code_real_type relaxed_bfd_r_type;
a06ea964
NC
8902 reloc_howto_type *howto;
8903 unsigned long r_symndx;
8904 Elf_Internal_Sym *sym;
8905 asection *sec;
8906 struct elf_link_hash_entry *h;
8907 bfd_vma relocation;
8908 bfd_reloc_status_type r;
8909 arelent bfd_reloc;
8910 char sym_type;
8911 bfd_boolean unresolved_reloc = FALSE;
8912 char *error_message = NULL;
8913
cec5225b
YZ
8914 r_symndx = ELFNN_R_SYM (rel->r_info);
8915 r_type = ELFNN_R_TYPE (rel->r_info);
a06ea964 8916
0aa13fee
AM
8917 bfd_reloc.howto = elfNN_aarch64_howto_from_type (input_bfd, r_type);
8918 howto = bfd_reloc.howto;
a06ea964 8919
7fcfd62d 8920 if (howto == NULL)
47aeb64c
NC
8921 return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
8922
a6bb11b2 8923 bfd_r_type = elfNN_aarch64_bfd_reloc_from_howto (howto);
7fcfd62d 8924
a06ea964
NC
8925 h = NULL;
8926 sym = NULL;
8927 sec = NULL;
8928
8929 if (r_symndx < symtab_hdr->sh_info)
8930 {
8931 sym = local_syms + r_symndx;
cec5225b 8932 sym_type = ELFNN_ST_TYPE (sym->st_info);
a06ea964
NC
8933 sec = local_sections[r_symndx];
8934
8935 /* An object file might have a reference to a local
8936 undefined symbol. This is a daft object file, but we
8937 should at least do something about it. */
8938 if (r_type != R_AARCH64_NONE && r_type != R_AARCH64_NULL
8939 && bfd_is_und_section (sec)
8940 && ELF_ST_BIND (sym->st_info) != STB_WEAK)
1a72702b
AM
8941 (*info->callbacks->undefined_symbol)
8942 (info, bfd_elf_string_from_elf_section
8943 (input_bfd, symtab_hdr->sh_link, sym->st_name),
8944 input_bfd, input_section, rel->r_offset, TRUE);
a06ea964 8945
a06ea964 8946 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1419bbe5
WN
8947
8948 /* Relocate against local STT_GNU_IFUNC symbol. */
0e1862bb 8949 if (!bfd_link_relocatable (info)
1419bbe5
WN
8950 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8951 {
8952 h = elfNN_aarch64_get_local_sym_hash (globals, input_bfd,
8953 rel, FALSE);
8954 if (h == NULL)
8955 abort ();
8956
8957 /* Set STT_GNU_IFUNC symbol value. */
8958 h->root.u.def.value = sym->st_value;
8959 h->root.u.def.section = sec;
8960 }
a06ea964
NC
8961 }
8962 else
8963 {
62d887d4 8964 bfd_boolean warned, ignored;
a06ea964
NC
8965
8966 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
8967 r_symndx, symtab_hdr, sym_hashes,
8968 h, sec, relocation,
62d887d4 8969 unresolved_reloc, warned, ignored);
a06ea964
NC
8970
8971 sym_type = h->type;
8972 }
8973
8974 if (sec != NULL && discarded_section (sec))
8975 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
8976 rel, 1, relend, howto, 0, contents);
8977
0e1862bb 8978 if (bfd_link_relocatable (info))
2e0488d3 8979 continue;
a06ea964
NC
8980
8981 if (h != NULL)
8982 name = h->root.root.string;
8983 else
8984 {
8985 name = (bfd_elf_string_from_elf_section
8986 (input_bfd, symtab_hdr->sh_link, sym->st_name));
8987 if (name == NULL || *name == '\0')
fd361982 8988 name = bfd_section_name (sec);
a06ea964
NC
8989 }
8990
8991 if (r_symndx != 0
8992 && r_type != R_AARCH64_NONE
8993 && r_type != R_AARCH64_NULL
8994 && (h == NULL
8995 || h->root.type == bfd_link_hash_defined
8996 || h->root.type == bfd_link_hash_defweak)
1408e112
MM
8997 && IS_AARCH64_TLS_RELOC (bfd_r_type) != (sym_type == STT_TLS)
8998 /* Morello SIZE relocation is allowed on TLS symbols and non-TLS
8999 symbols. */
9000 && !IS_MORELLO_SIZE_RELOC (bfd_r_type))
a06ea964 9001 {
4eca0228 9002 _bfd_error_handler
a06ea964 9003 ((sym_type == STT_TLS
695344c0 9004 /* xgettext:c-format */
2dcf00ce 9005 ? _("%pB(%pA+%#" PRIx64 "): %s used with TLS symbol %s")
695344c0 9006 /* xgettext:c-format */
2dcf00ce 9007 : _("%pB(%pA+%#" PRIx64 "): %s used with non-TLS symbol %s")),
a06ea964 9008 input_bfd,
2dcf00ce 9009 input_section, (uint64_t) rel->r_offset, howto->name, name);
a06ea964
NC
9010 }
9011
f3ecc5c8
AC
9012 if (r_symndx
9013 && h
9014 && IS_AARCH64_TLS_RELOC (bfd_r_type)
9015 && h->root.type == bfd_link_hash_undefweak)
9016 /* We have already warned about these in aarch64_check_relocs,
9017 so just skip over them. */
9018 continue;
9019
26c32295
MM
9020 /* We check TLS relocations with an addend here rather than in the
9021 final_link_relocate function since we want to base our decision on if
9022 the original relocation was a TLS relocation and the relaxation below
9023 could turn this relocation into a plain MORELLO_ADR_PREL_PG_HI20 or
9024 AARCH64_ADD_ABS_LO12_NC. */
9025 if (((IS_AARCH64_TLS_RELOC (bfd_r_type)
9026 && !IS_AARCH64_TLSLE_RELOC (bfd_r_type))
9027 || IS_MORELLO_SIZE_RELOC (bfd_r_type))
9028 && (rel->r_addend != 0 || addend != 0))
9029 {
9030 _bfd_error_handler (_("%pB(%pA+%#" PRIx64 "): "
9031 "relocation %s against `%s' is disallowed "
9032 "with addend"),
9033 input_bfd, input_section,
9034 (uint64_t) rel->r_offset, howto->name, name);
9035 /* It could be confusing if there's not a TLS relocation with an
9036 addend but there was a TLS relocation with the previous relocation
9037 at the same spot. */
9038 if (rel->r_addend == 0 && addend != 0)
9039 info->callbacks->warning
9040 (info,
9041 _("note: addend comes from previous relocation"),
9042 name, input_bfd, input_section, rel->r_offset);
9043 ret = FALSE;
9044 continue;
9045 }
9046
a06ea964 9047 /* We relax only if we can see that there can be a valid transition
07d6d2b8
AM
9048 from a reloc type to another.
9049 We call elfNN_aarch64_final_link_relocate unless we're completely
9050 done, i.e., the relaxation produced the final output we want. */
a06ea964 9051
ece841e8 9052 bfd_boolean requires_c64_tls_stub;
4ca9b406 9053 relaxed_bfd_r_type = aarch64_tls_transition (input_bfd, info, rel,
ece841e8
MM
9054 h, r_symndx,
9055 &requires_c64_tls_stub);
a6bb11b2 9056 if (relaxed_bfd_r_type != bfd_r_type)
a06ea964 9057 {
a6bb11b2
YZ
9058 bfd_r_type = relaxed_bfd_r_type;
9059 howto = elfNN_aarch64_howto_from_bfd_reloc (bfd_r_type);
9060 BFD_ASSERT (howto != NULL);
9061 r_type = howto->type;
ece841e8
MM
9062 if (requires_c64_tls_stub)
9063 {
9064 relocation
9065 = c64_populate_tls_data_stub (globals, input_bfd, h, r_symndx,
9066 relocation - tpoff_base (info),
9067 h ? h->size : sym->st_size,
9068 output_bfd);
9069 }
4ca9b406 9070 r = elfNN_aarch64_tls_relax (input_bfd, info, input_section,
ece841e8 9071 contents, rel, h);
26c32295 9072 unresolved_reloc = FALSE;
a06ea964
NC
9073 }
9074 else
9075 r = bfd_reloc_continue;
9076
9077 /* There may be multiple consecutive relocations for the
07d6d2b8
AM
9078 same offset. In that case we are supposed to treat the
9079 output of each relocation as the addend for the next. */
a06ea964
NC
9080 if (rel + 1 < relend
9081 && rel->r_offset == rel[1].r_offset
cec5225b
YZ
9082 && ELFNN_R_TYPE (rel[1].r_info) != R_AARCH64_NONE
9083 && ELFNN_R_TYPE (rel[1].r_info) != R_AARCH64_NULL)
a06ea964
NC
9084 save_addend = TRUE;
9085 else
9086 save_addend = FALSE;
9087
9088 if (r == bfd_reloc_continue)
cec5225b 9089 r = elfNN_aarch64_final_link_relocate (howto, input_bfd, output_bfd,
a06ea964
NC
9090 input_section, contents, rel,
9091 relocation, info, sec,
9092 h, &unresolved_reloc,
1419bbe5 9093 save_addend, &addend, sym);
a06ea964 9094
4ca9b406
SP
9095 bfd_boolean c64_rtype = FALSE;
9096
0aa13fee 9097 switch (elfNN_aarch64_bfd_reloc_from_type (input_bfd, r_type))
a06ea964 9098 {
ce336788 9099 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
a6bb11b2 9100 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
3c12b054 9101 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
7ba7cfe4 9102 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
94facae3 9103 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
73f925cc 9104 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
f69e4920 9105 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
77a69ff8 9106 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
a06ea964
NC
9107 if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
9108 {
9109 bfd_boolean need_relocs = FALSE;
9110 bfd_byte *loc;
9111 int indx;
9112 bfd_vma off;
9113
9114 off = symbol_got_offset (input_bfd, h, r_symndx);
9115 indx = h && h->dynindx != -1 ? h->dynindx : 0;
9116
9117 need_relocs =
6dda7875 9118 (!bfd_link_executable (info) || indx != 0) &&
a06ea964
NC
9119 (h == NULL
9120 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9121 || h->root.type != bfd_link_hash_undefweak);
9122
9123 BFD_ASSERT (globals->root.srelgot != NULL);
9124
9125 if (need_relocs)
9126 {
9127 Elf_Internal_Rela rela;
a6bb11b2 9128 rela.r_info = ELFNN_R_INFO (indx, AARCH64_R (TLS_DTPMOD));
a06ea964
NC
9129 rela.r_addend = 0;
9130 rela.r_offset = globals->root.sgot->output_section->vma +
9131 globals->root.sgot->output_offset + off;
9132
9133
9134 loc = globals->root.srelgot->contents;
9135 loc += globals->root.srelgot->reloc_count++
ece841e8 9136 * RELOC_SIZE (globals);
cec5225b 9137 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
a06ea964 9138
f69e4920 9139 bfd_reloc_code_real_type real_type =
0aa13fee 9140 elfNN_aarch64_bfd_reloc_from_type (input_bfd, r_type);
f69e4920
JW
9141
9142 if (real_type == BFD_RELOC_AARCH64_TLSLD_ADR_PREL21
73f925cc
JW
9143 || real_type == BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21
9144 || real_type == BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC)
77a69ff8
JW
9145 {
9146 /* For local dynamic, don't generate DTPREL in any case.
9147 Initialize the DTPREL slot into zero, so we get module
9148 base address when invoke runtime TLS resolver. */
9149 bfd_put_NN (output_bfd, 0,
9150 globals->root.sgot->contents + off
a1bdea65 9151 + GOT_ENTRY_SIZE (globals));
77a69ff8
JW
9152 }
9153 else if (indx == 0)
a06ea964 9154 {
cec5225b 9155 bfd_put_NN (output_bfd,
a06ea964
NC
9156 relocation - dtpoff_base (info),
9157 globals->root.sgot->contents + off
a1bdea65 9158 + GOT_ENTRY_SIZE (globals));
a06ea964
NC
9159 }
9160 else
9161 {
9162 /* This TLS symbol is global. We emit a
9163 relocation to fixup the tls offset at load
9164 time. */
9165 rela.r_info =
a6bb11b2 9166 ELFNN_R_INFO (indx, AARCH64_R (TLS_DTPREL));
a06ea964
NC
9167 rela.r_addend = 0;
9168 rela.r_offset =
9169 (globals->root.sgot->output_section->vma
9170 + globals->root.sgot->output_offset + off
a1bdea65 9171 + GOT_ENTRY_SIZE (globals));
a06ea964
NC
9172
9173 loc = globals->root.srelgot->contents;
9174 loc += globals->root.srelgot->reloc_count++
9175 * RELOC_SIZE (globals);
cec5225b
YZ
9176 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
9177 bfd_put_NN (output_bfd, (bfd_vma) 0,
a06ea964 9178 globals->root.sgot->contents + off
a1bdea65 9179 + GOT_ENTRY_SIZE (globals));
a06ea964
NC
9180 }
9181 }
9182 else
9183 {
cec5225b 9184 bfd_put_NN (output_bfd, (bfd_vma) 1,
a06ea964 9185 globals->root.sgot->contents + off);
cec5225b 9186 bfd_put_NN (output_bfd,
a06ea964
NC
9187 relocation - dtpoff_base (info),
9188 globals->root.sgot->contents + off
a1bdea65 9189 + GOT_ENTRY_SIZE (globals));
a06ea964
NC
9190 }
9191
9192 symbol_got_offset_mark (input_bfd, h, r_symndx);
9193 }
9194 break;
9195
1408e112
MM
9196 case BFD_RELOC_MORELLO_TLSIE_ADR_GOTTPREL_PAGE20:
9197 case BFD_RELOC_MORELLO_TLSIE_ADD_LO12:
ece841e8
MM
9198 c64_rtype = TRUE;
9199 /* Fall through. */
9200
a6bb11b2
YZ
9201 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
9202 case BFD_RELOC_AARCH64_TLSIE_LDNN_GOTTPREL_LO12_NC:
043bf05a 9203 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
3b957e5b
RL
9204 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
9205 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
a06ea964
NC
9206 if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
9207 {
9208 bfd_boolean need_relocs = FALSE;
9209 bfd_byte *loc;
9210 int indx;
9211 bfd_vma off;
9212
9213 off = symbol_got_offset (input_bfd, h, r_symndx);
9214
9215 indx = h && h->dynindx != -1 ? h->dynindx : 0;
9216
9217 need_relocs =
6dda7875 9218 (!bfd_link_executable (info) || indx != 0) &&
a06ea964
NC
9219 (h == NULL
9220 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9221 || h->root.type != bfd_link_hash_undefweak);
ece841e8 9222 BFD_ASSERT (!c64_rtype || c64_needs_relocation (info, h));
1408e112 9223 need_relocs = need_relocs || c64_rtype;
a06ea964
NC
9224
9225 BFD_ASSERT (globals->root.srelgot != NULL);
9226
9227 if (need_relocs)
9228 {
9229 Elf_Internal_Rela rela;
9230
9231 if (indx == 0)
9232 rela.r_addend = relocation - dtpoff_base (info);
9233 else
9234 rela.r_addend = 0;
9235
1408e112
MM
9236 rela.r_info = ELFNN_R_INFO (indx,
9237 globals->c64_rel
9238 ? MORELLO_R (TPREL128)
9239 : AARCH64_R (TLS_TPREL));
a06ea964
NC
9240 rela.r_offset = globals->root.sgot->output_section->vma +
9241 globals->root.sgot->output_offset + off;
9242
9243 loc = globals->root.srelgot->contents;
9244 loc += globals->root.srelgot->reloc_count++
ece841e8 9245 * RELOC_SIZE (globals);
a06ea964 9246
cec5225b 9247 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
a06ea964 9248
cec5225b 9249 bfd_put_NN (output_bfd, rela.r_addend,
a06ea964 9250 globals->root.sgot->contents + off);
1408e112
MM
9251 if (c64_rtype && TLS_SYMBOL_REFERENCES_LOCAL (info, h))
9252 {
9253 bfd_vma sym_size = h ? h->size : sym->st_size;
9254 bfd_put_NN (output_bfd, sym_size,
9255 globals->root.sgot->contents + off + 8);
9256 }
a06ea964
NC
9257 }
9258 else
cec5225b 9259 bfd_put_NN (output_bfd, relocation - tpoff_base (info),
a06ea964
NC
9260 globals->root.sgot->contents + off);
9261
9262 symbol_got_offset_mark (input_bfd, h, r_symndx);
9263 }
9264 break;
9265
4ca9b406
SP
9266 case BFD_RELOC_MORELLO_TLSDESC_ADR_PAGE20:
9267 case BFD_RELOC_MORELLO_TLSDESC_LD128_LO12:
9268 c64_rtype = TRUE;
9269 /* Fall through. */
9270
f955cccf 9271 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
a6bb11b2 9272 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
389b8029 9273 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
a6bb11b2 9274 case BFD_RELOC_AARCH64_TLSDESC_LDNN_LO12_NC:
1ada945d 9275 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
0484b454
RL
9276 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
9277 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
a06ea964
NC
9278 if (! symbol_tlsdesc_got_offset_mark_p (input_bfd, h, r_symndx))
9279 {
9280 bfd_boolean need_relocs = FALSE;
9281 int indx = h && h->dynindx != -1 ? h->dynindx : 0;
9282 bfd_vma off = symbol_tlsdesc_got_offset (input_bfd, h, r_symndx);
9283
9284 need_relocs = (h == NULL
9285 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9286 || h->root.type != bfd_link_hash_undefweak);
ece841e8 9287 BFD_ASSERT (!c64_rtype || c64_needs_relocation (info, h));
1408e112 9288 need_relocs = need_relocs || c64_rtype;
a06ea964
NC
9289
9290 BFD_ASSERT (globals->root.srelgot != NULL);
9291 BFD_ASSERT (globals->root.sgot != NULL);
9292
9293 if (need_relocs)
9294 {
9295 bfd_byte *loc;
9296 Elf_Internal_Rela rela;
4ca9b406
SP
9297
9298 rela.r_info = ELFNN_R_INFO (indx,
9299 (c64_rtype ? MORELLO_R (TLSDESC)
9300 : AARCH64_R (TLSDESC)));
a6bb11b2 9301
a06ea964
NC
9302 rela.r_addend = 0;
9303 rela.r_offset = (globals->root.sgotplt->output_section->vma
9304 + globals->root.sgotplt->output_offset
9305 + off + globals->sgotplt_jump_table_size);
9306
9307 if (indx == 0)
9308 rela.r_addend = relocation - dtpoff_base (info);
9309
9310 /* Allocate the next available slot in the PLT reloc
9311 section to hold our R_AARCH64_TLSDESC, the next
9312 available slot is determined from reloc_count,
9313 which we step. But note, reloc_count was
9314 artifically moved down while allocating slots for
9315 real PLT relocs such that all of the PLT relocs
9316 will fit above the initial reloc_count and the
9317 extra stuff will fit below. */
9318 loc = globals->root.srelplt->contents;
9319 loc += globals->root.srelplt->reloc_count++
9320 * RELOC_SIZE (globals);
9321
cec5225b 9322 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
a06ea964 9323
1408e112
MM
9324 if (!c64_rtype)
9325 {
9326 bfd_put_NN (output_bfd, (bfd_vma) 0,
9327 globals->root.sgotplt->contents + off +
9328 globals->sgotplt_jump_table_size);
9329 bfd_put_NN (output_bfd, (bfd_vma) 0,
9330 globals->root.sgotplt->contents + off +
9331 globals->sgotplt_jump_table_size +
9332 GOT_ENTRY_SIZE (globals));
9333 }
9334 else
9335 {
9336 void * fragment_start
9337 = globals->root.sgotplt->contents + off +
9338 globals->sgotplt_jump_table_size;
9339
9340 bfd_vma sym_size = !TLS_SYMBOL_REFERENCES_LOCAL (info, h)
9341 ? 0
9342 : h ? h->size : sym->st_size;
9343
9344 bfd_put_NN (output_bfd, (bfd_vma) 0, fragment_start);
9345 bfd_put_NN (output_bfd, (bfd_vma) 0, fragment_start + 8);
9346 bfd_put_NN (output_bfd, (bfd_vma) 0, fragment_start + 16);
9347 bfd_put_NN (output_bfd, sym_size, fragment_start + 24);
9348 }
a06ea964
NC
9349 }
9350
9351 symbol_tlsdesc_got_offset_mark (input_bfd, h, r_symndx);
9352 }
9353 break;
a6bb11b2
YZ
9354 default:
9355 break;
a06ea964
NC
9356 }
9357
a06ea964 9358 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
07d6d2b8
AM
9359 because such sections are not SEC_ALLOC and thus ld.so will
9360 not process them. */
a06ea964
NC
9361 if (unresolved_reloc
9362 && !((input_section->flags & SEC_DEBUGGING) != 0
9363 && h->def_dynamic)
9364 && _bfd_elf_section_offset (output_bfd, info, input_section,
9365 +rel->r_offset) != (bfd_vma) - 1)
9366 {
4eca0228 9367 _bfd_error_handler
695344c0 9368 /* xgettext:c-format */
2dcf00ce
AM
9369 (_("%pB(%pA+%#" PRIx64 "): "
9370 "unresolvable %s relocation against symbol `%s'"),
9371 input_bfd, input_section, (uint64_t) rel->r_offset, howto->name,
a06ea964
NC
9372 h->root.root.string);
9373 return FALSE;
9374 }
9375
9376 if (r != bfd_reloc_ok && r != bfd_reloc_continue)
9377 {
c674f5cd 9378 bfd_reloc_code_real_type real_r_type
0aa13fee 9379 = elfNN_aarch64_bfd_reloc_from_type (input_bfd, r_type);
c674f5cd 9380
a06ea964
NC
9381 switch (r)
9382 {
9383 case bfd_reloc_overflow:
1a72702b
AM
9384 (*info->callbacks->reloc_overflow)
9385 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
9386 input_bfd, input_section, rel->r_offset);
c674f5cd
JW
9387 if (real_r_type == BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15
9388 || real_r_type == BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14)
9389 {
9390 (*info->callbacks->warning)
9391 (info,
90b6238f 9392 _("too many GOT entries for -fpic, "
c674f5cd
JW
9393 "please recompile with -fPIC"),
9394 name, input_bfd, input_section, rel->r_offset);
9395 return FALSE;
9396 }
027e9c75
NC
9397 /* Overflow can occur when a variable is referenced with a type
9398 that has a larger alignment than the type with which it was
9399 declared. eg:
9400 file1.c: extern int foo; int a (void) { return foo; }
9401 file2.c: char bar, foo, baz;
9402 If the variable is placed into a data section at an offset
9403 that is incompatible with the larger alignment requirement
9404 overflow will occur. (Strictly speaking this is not overflow
9405 but rather an alignment problem, but the bfd_reloc_ error
9406 enum does not have a value to cover that situation).
9407
9408 Try to catch this situation here and provide a more helpful
9409 error message to the user. */
26009aa7 9410 if (addend & (((bfd_vma) 1 << howto->rightshift) - 1)
027e9c75
NC
9411 /* FIXME: Are we testing all of the appropriate reloc
9412 types here ? */
9413 && (real_r_type == BFD_RELOC_AARCH64_LD_LO19_PCREL
9414 || real_r_type == BFD_RELOC_AARCH64_LDST16_LO12
9415 || real_r_type == BFD_RELOC_AARCH64_LDST32_LO12
9416 || real_r_type == BFD_RELOC_AARCH64_LDST64_LO12
9417 || real_r_type == BFD_RELOC_AARCH64_LDST128_LO12))
9418 {
9419 info->callbacks->warning
90b6238f 9420 (info, _("one possible cause of this error is that the \
027e9c75 9421symbol is being referenced in the indicated code as if it had a larger \
90b6238f 9422alignment than was declared where it was defined"),
027e9c75
NC
9423 name, input_bfd, input_section, rel->r_offset);
9424 }
40bbb79e
SP
9425
9426 if (real_r_type == BFD_RELOC_MORELLO_CAPINIT)
9427 info->callbacks->warning
9428 (info, _("relocation offset must be capability aligned"),
9429 name, input_bfd, input_section, rel->r_offset);
a06ea964
NC
9430 break;
9431
9432 case bfd_reloc_undefined:
1a72702b
AM
9433 (*info->callbacks->undefined_symbol)
9434 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
a06ea964
NC
9435 break;
9436
9437 case bfd_reloc_outofrange:
9438 error_message = _("out of range");
9439 goto common_error;
9440
9441 case bfd_reloc_notsupported:
9442 error_message = _("unsupported relocation");
9443 goto common_error;
9444
9445 case bfd_reloc_dangerous:
9446 /* error_message should already be set. */
9447 goto common_error;
9448
9449 default:
9450 error_message = _("unknown error");
9451 /* Fall through. */
9452
9453 common_error:
9454 BFD_ASSERT (error_message != NULL);
1a72702b
AM
9455 (*info->callbacks->reloc_dangerous)
9456 (info, error_message, input_bfd, input_section, rel->r_offset);
a06ea964
NC
9457 break;
9458 }
9459 }
027e9c75
NC
9460
9461 if (!save_addend)
9462 addend = 0;
a06ea964
NC
9463 }
9464
26c32295 9465 return ret;
a06ea964
NC
9466}
9467
9468/* Set the right machine number. */
9469
9470static bfd_boolean
cec5225b 9471elfNN_aarch64_object_p (bfd *abfd)
a06ea964 9472{
cec5225b
YZ
9473#if ARCH_SIZE == 32
9474 bfd_default_set_arch_mach (abfd, bfd_arch_aarch64, bfd_mach_aarch64_ilp32);
9475#else
a06ea964 9476 bfd_default_set_arch_mach (abfd, bfd_arch_aarch64, bfd_mach_aarch64);
cec5225b 9477#endif
a06ea964
NC
9478 return TRUE;
9479}
9480
9481/* Function to keep AArch64 specific flags in the ELF header. */
9482
9483static bfd_boolean
cec5225b 9484elfNN_aarch64_set_private_flags (bfd *abfd, flagword flags)
a06ea964
NC
9485{
9486 if (elf_flags_init (abfd) && elf_elfheader (abfd)->e_flags != flags)
9487 {
9488 }
9489 else
9490 {
9491 elf_elfheader (abfd)->e_flags = flags;
9492 elf_flags_init (abfd) = TRUE;
9493 }
9494
9495 return TRUE;
9496}
9497
a06ea964
NC
9498/* Merge backend specific data from an object file to the output
9499 object file when linking. */
9500
9501static bfd_boolean
50e03d47 9502elfNN_aarch64_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
a06ea964 9503{
50e03d47 9504 bfd *obfd = info->output_bfd;
a06ea964
NC
9505 flagword out_flags;
9506 flagword in_flags;
f8180c20 9507 bfd_boolean flags_compatible = FALSE;
a06ea964
NC
9508 asection *sec;
9509
9510 /* Check if we have the same endianess. */
50e03d47 9511 if (!_bfd_generic_verify_endian_match (ibfd, info))
a06ea964
NC
9512 return FALSE;
9513
9514 if (!is_aarch64_elf (ibfd) || !is_aarch64_elf (obfd))
9515 return TRUE;
9516
9517 /* The input BFD must have had its flags initialised. */
9518 /* The following seems bogus to me -- The flags are initialized in
9519 the assembler but I don't think an elf_flags_init field is
9520 written into the object. */
9521 /* BFD_ASSERT (elf_flags_init (ibfd)); */
9522
9523 in_flags = elf_elfheader (ibfd)->e_flags;
9524 out_flags = elf_elfheader (obfd)->e_flags;
9525
9526 if (!elf_flags_init (obfd))
9527 {
f8180c20
MM
9528 elf_flags_init (obfd) = TRUE;
9529
a06ea964 9530 /* If the input is the default architecture and had the default
07d6d2b8
AM
9531 flags then do not bother setting the flags for the output
9532 architecture, instead allow future merges to do this. If no
9533 future merges ever set these flags then they will retain their
9534 uninitialised values, which surprise surprise, correspond
9535 to the default values. */
a06ea964
NC
9536 if (bfd_get_arch_info (ibfd)->the_default
9537 && elf_elfheader (ibfd)->e_flags == 0)
9538 return TRUE;
9539
a06ea964 9540 elf_elfheader (obfd)->e_flags = in_flags;
60d18fc3
MM
9541 /* Determine if we are linking purecap or not based on the flags of the
9542 input binaries. Among other things this decides the size of GOT
9543 entries. */
9544 if (in_flags & EF_AARCH64_CHERI_PURECAP)
9545 {
9546 struct elf_aarch64_link_hash_table *globals;
9547 globals = elf_aarch64_hash_table (info);
9548 globals->c64_rel = 1;
9549 }
a06ea964
NC
9550
9551 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
9552 && bfd_get_arch_info (obfd)->the_default)
9553 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
9554 bfd_get_mach (ibfd));
9555
9556 return TRUE;
9557 }
9558
9559 /* Identical flags must be compatible. */
9560 if (in_flags == out_flags)
9561 return TRUE;
9562
9563 /* Check to see if the input BFD actually contains any sections. If
9564 not, its flags may not have been initialised either, but it
9565 cannot actually cause any incompatiblity. Do not short-circuit
9566 dynamic objects; their section list may be emptied by
9567 elf_link_add_object_symbols.
9568
9569 Also check to see if there are no code sections in the input.
9570 In this case there is no need to check for code specific flags.
9571 XXX - do we need to worry about floating-point format compatability
f8180c20
MM
9572 in data sections ?
9573
9574 We definitely need to check for data sections if one set of flags is
9575 targetting the PURECAP abi and another is not. Pointers being
9576 capabilities in data sections can not be glossed over. */
a06ea964
NC
9577 if (!(ibfd->flags & DYNAMIC))
9578 {
9579 bfd_boolean null_input_bfd = TRUE;
f8180c20
MM
9580 bfd_boolean only_data_sections
9581 = !(in_flags & EF_AARCH64_CHERI_PURECAP
9582 || out_flags & EF_AARCH64_CHERI_PURECAP);
a06ea964
NC
9583
9584 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9585 {
fd361982 9586 if ((bfd_section_flags (sec)
a06ea964
NC
9587 & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
9588 == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
9589 only_data_sections = FALSE;
9590
9591 null_input_bfd = FALSE;
9592 break;
9593 }
9594
9595 if (null_input_bfd || only_data_sections)
9596 return TRUE;
9597 }
9598
9599 return flags_compatible;
9600}
9601
9602/* Display the flags field. */
9603
9604static bfd_boolean
cec5225b 9605elfNN_aarch64_print_private_bfd_data (bfd *abfd, void *ptr)
a06ea964
NC
9606{
9607 FILE *file = (FILE *) ptr;
9608 unsigned long flags;
9609
9610 BFD_ASSERT (abfd != NULL && ptr != NULL);
9611
9612 /* Print normal ELF private data. */
9613 _bfd_elf_print_private_bfd_data (abfd, ptr);
9614
9615 flags = elf_elfheader (abfd)->e_flags;
9616 /* Ignore init flag - it may not be set, despite the flags field
9617 containing valid data. */
9618
9619 /* xgettext:c-format */
9620 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
9621
9622 if (flags)
9623 fprintf (file, _("<Unrecognised flag bits set>"));
9624
9625 fputc ('\n', file);
9626
9627 return TRUE;
9628}
9629
6353d82b
JW
9630/* Return true if we need copy relocation against EH. */
9631
9632static bfd_boolean
9633need_copy_relocation_p (struct elf_aarch64_link_hash_entry *eh)
9634{
9635 struct elf_dyn_relocs *p;
9636 asection *s;
9637
190eb1dd 9638 for (p = eh->root.dyn_relocs; p != NULL; p = p->next)
6353d82b
JW
9639 {
9640 /* If there is any pc-relative reference, we need to keep copy relocation
9641 to avoid propagating the relocation into runtime that current glibc
9642 does not support. */
9643 if (p->pc_count)
9644 return TRUE;
9645
9646 s = p->sec->output_section;
9647 /* Need copy relocation if it's against read-only section. */
9648 if (s != NULL && (s->flags & SEC_READONLY) != 0)
9649 return TRUE;
9650 }
9651
9652 return FALSE;
9653}
9654
a06ea964
NC
9655/* Adjust a symbol defined by a dynamic object and referenced by a
9656 regular object. The current definition is in some section of the
9657 dynamic object, but we're not including those sections. We have to
9658 change the definition to something the rest of the link can
9659 understand. */
9660
9661static bfd_boolean
cec5225b 9662elfNN_aarch64_adjust_dynamic_symbol (struct bfd_link_info *info,
a06ea964
NC
9663 struct elf_link_hash_entry *h)
9664{
cec5225b 9665 struct elf_aarch64_link_hash_table *htab;
5474d94f 9666 asection *s, *srel;
a06ea964
NC
9667
9668 /* If this is a function, put it in the procedure linkage table. We
9669 will fill in the contents of the procedure linkage table later,
9670 when we know the address of the .got section. */
1419bbe5 9671 if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
a06ea964
NC
9672 {
9673 if (h->plt.refcount <= 0
1419bbe5
WN
9674 || (h->type != STT_GNU_IFUNC
9675 && (SYMBOL_CALLS_LOCAL (info, h)
9676 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
9677 && h->root.type == bfd_link_hash_undefweak))))
a06ea964
NC
9678 {
9679 /* This case can occur if we saw a CALL26 reloc in
9680 an input file, but the symbol wasn't referred to
9681 by a dynamic object or all references were
9682 garbage collected. In which case we can end up
9683 resolving. */
9684 h->plt.offset = (bfd_vma) - 1;
9685 h->needs_plt = 0;
9686 }
9687
9688 return TRUE;
9689 }
9690 else
80de0c6d 9691 /* Otherwise, reset to -1. */
a06ea964
NC
9692 h->plt.offset = (bfd_vma) - 1;
9693
9694
9695 /* If this is a weak symbol, and there is a real definition, the
9696 processor independent code will have arranged for us to see the
9697 real definition first, and we can just use the same value. */
60d67dc8 9698 if (h->is_weakalias)
a06ea964 9699 {
60d67dc8
AM
9700 struct elf_link_hash_entry *def = weakdef (h);
9701 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
9702 h->root.u.def.section = def->root.u.def.section;
9703 h->root.u.def.value = def->root.u.def.value;
a06ea964 9704 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
60d67dc8 9705 h->non_got_ref = def->non_got_ref;
a06ea964
NC
9706 return TRUE;
9707 }
9708
9709 /* If we are creating a shared library, we must presume that the
9710 only references to the symbol are via the global offset table.
9711 For such cases we need not do anything here; the relocations will
9712 be handled correctly by relocate_section. */
0e1862bb 9713 if (bfd_link_pic (info))
a06ea964
NC
9714 return TRUE;
9715
9716 /* If there are no references to this symbol that do not use the
9717 GOT, we don't need to generate a copy reloc. */
9718 if (!h->non_got_ref)
9719 return TRUE;
9720
9721 /* If -z nocopyreloc was given, we won't generate them either. */
9722 if (info->nocopyreloc)
9723 {
9724 h->non_got_ref = 0;
9725 return TRUE;
9726 }
9727
6353d82b
JW
9728 if (ELIMINATE_COPY_RELOCS)
9729 {
9730 struct elf_aarch64_link_hash_entry *eh;
dce2246a 9731 /* If we don't find any dynamic relocs in read-only sections, then
6353d82b
JW
9732 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
9733 eh = (struct elf_aarch64_link_hash_entry *) h;
9734 if (!need_copy_relocation_p (eh))
9735 {
9736 h->non_got_ref = 0;
9737 return TRUE;
9738 }
9739 }
9740
a06ea964
NC
9741 /* We must allocate the symbol in our .dynbss section, which will
9742 become part of the .bss section of the executable. There will be
9743 an entry for this symbol in the .dynsym section. The dynamic
9744 object will contain position independent code, so all references
9745 from the dynamic object to this symbol will go through the global
9746 offset table. The dynamic linker will use the .dynsym entry to
9747 determine the address it must put in the global offset table, so
9748 both the dynamic object and the regular object will refer to the
9749 same memory location for the variable. */
9750
cec5225b 9751 htab = elf_aarch64_hash_table (info);
a06ea964
NC
9752
9753 /* We must generate a R_AARCH64_COPY reloc to tell the dynamic linker
9754 to copy the initial value out of the dynamic object and into the
9755 runtime process image. */
5474d94f
AM
9756 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
9757 {
9758 s = htab->root.sdynrelro;
9759 srel = htab->root.sreldynrelro;
9760 }
9761 else
9762 {
9763 s = htab->root.sdynbss;
9764 srel = htab->root.srelbss;
9765 }
a06ea964
NC
9766 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
9767 {
5474d94f 9768 srel->size += RELOC_SIZE (htab);
a06ea964
NC
9769 h->needs_copy = 1;
9770 }
9771
6cabe1ea 9772 return _bfd_elf_adjust_dynamic_copy (info, h, s);
a06ea964
NC
9773
9774}
9775
9776static bfd_boolean
cec5225b 9777elfNN_aarch64_allocate_local_symbols (bfd *abfd, unsigned number)
a06ea964
NC
9778{
9779 struct elf_aarch64_local_symbol *locals;
cec5225b 9780 locals = elf_aarch64_locals (abfd);
a06ea964
NC
9781 if (locals == NULL)
9782 {
9783 locals = (struct elf_aarch64_local_symbol *)
9784 bfd_zalloc (abfd, number * sizeof (struct elf_aarch64_local_symbol));
9785 if (locals == NULL)
9786 return FALSE;
cec5225b 9787 elf_aarch64_locals (abfd) = locals;
a06ea964
NC
9788 }
9789 return TRUE;
9790}
9791
a1bdea65
SP
9792/* Initialise the .got section to hold the global offset table. */
9793
9794static void
9795aarch64_elf_init_got_section (bfd *abfd, struct bfd_link_info *info)
9796{
9797 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9798 asection *s;
9799 struct elf_aarch64_link_hash_table *globals = elf_aarch64_hash_table (info);
9800 unsigned int align = bed->s->log_file_align + globals->c64_rel;
9801
9802 if (globals->root.sgot != NULL)
9803 {
9804 bfd_set_section_alignment (globals->root.srelgot,
9805 bed->s->log_file_align);
9806 bfd_set_section_alignment (globals->root.sgot, align);
9807 globals->root.sgot->size += GOT_ENTRY_SIZE (globals);
9808 }
9809
8d4edc5f 9810 /* Track capability initialisation for static non-PIE binaries. */
aeba84b1 9811 if (static_pde (info) && globals->srelcaps == NULL)
8d4edc5f
SP
9812 globals->srelcaps = globals->root.srelgot;
9813
a1bdea65
SP
9814 if (globals->root.igotplt != NULL)
9815 bfd_set_section_alignment (globals->root.igotplt, align);
9816
9817 s = globals->root.sgot;
9818
9819 if (globals->root.sgotplt != NULL)
9820 {
9821 bfd_set_section_alignment (globals->root.sgotplt, align);
9822 s = globals->root.sgotplt;
9823 }
9824
9825 /* The first bit of the global offset table is the header. */
9826 if (s != NULL)
9827 s->size += bed->got_header_size (info);
9828}
9829
cc0efaa8
MS
9830/* Create the .got section to hold the global offset table. */
9831
9832static bfd_boolean
9833aarch64_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
9834{
9835 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9836 flagword flags;
9837 asection *s;
9838 struct elf_link_hash_entry *h;
9839 struct elf_link_hash_table *htab = elf_hash_table (info);
9840
9841 /* This function may be called more than once. */
ce558b89 9842 if (htab->sgot != NULL)
cc0efaa8
MS
9843 return TRUE;
9844
9845 flags = bed->dynamic_sec_flags;
9846
9847 s = bfd_make_section_anyway_with_flags (abfd,
9848 (bed->rela_plts_and_copies_p
9849 ? ".rela.got" : ".rel.got"),
9850 (bed->dynamic_sec_flags
9851 | SEC_READONLY));
a1bdea65 9852 if (s == NULL)
cc0efaa8
MS
9853 return FALSE;
9854 htab->srelgot = s;
9855
9856 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
a1bdea65 9857 if (s == NULL)
cc0efaa8
MS
9858 return FALSE;
9859 htab->sgot = s;
cc0efaa8
MS
9860
9861 if (bed->want_got_sym)
9862 {
9863 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
9864 (or .got.plt) section. We don't do this in the linker script
9865 because we don't want to define the symbol if we are not creating
9866 a global offset table. */
9867 h = _bfd_elf_define_linkage_sym (abfd, info, s,
9868 "_GLOBAL_OFFSET_TABLE_");
9869 elf_hash_table (info)->hgot = h;
9870 if (h == NULL)
9871 return FALSE;
9872 }
9873
9874 if (bed->want_got_plt)
9875 {
9876 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
a1bdea65 9877 if (s == NULL)
cc0efaa8
MS
9878 return FALSE;
9879 htab->sgotplt = s;
9880 }
9881
cc0efaa8
MS
9882 return TRUE;
9883}
9884
a06ea964
NC
9885/* Look through the relocs for a section during the first phase. */
9886
9887static bfd_boolean
cec5225b 9888elfNN_aarch64_check_relocs (bfd *abfd, struct bfd_link_info *info,
a06ea964
NC
9889 asection *sec, const Elf_Internal_Rela *relocs)
9890{
9891 Elf_Internal_Shdr *symtab_hdr;
9892 struct elf_link_hash_entry **sym_hashes;
9893 const Elf_Internal_Rela *rel;
9894 const Elf_Internal_Rela *rel_end;
9895 asection *sreloc;
9896
cec5225b 9897 struct elf_aarch64_link_hash_table *htab;
a06ea964 9898
0e1862bb 9899 if (bfd_link_relocatable (info))
a06ea964
NC
9900 return TRUE;
9901
9902 BFD_ASSERT (is_aarch64_elf (abfd));
9903
cec5225b 9904 htab = elf_aarch64_hash_table (info);
a06ea964
NC
9905 sreloc = NULL;
9906
9907 symtab_hdr = &elf_symtab_hdr (abfd);
9908 sym_hashes = elf_sym_hashes (abfd);
a06ea964 9909
7ff36d1a 9910 bfd_elfNN_aarch64_init_maps (abfd, info);
f0070c1e 9911
a06ea964
NC
9912 rel_end = relocs + sec->reloc_count;
9913 for (rel = relocs; rel < rel_end; rel++)
9914 {
9915 struct elf_link_hash_entry *h;
f3ecc5c8 9916 unsigned int r_symndx, r_type;
a6bb11b2 9917 bfd_reloc_code_real_type bfd_r_type;
1419bbe5 9918 Elf_Internal_Sym *isym;
a06ea964 9919
cec5225b 9920 r_symndx = ELFNN_R_SYM (rel->r_info);
f3ecc5c8
AC
9921 r_type = ELFNN_R_TYPE (rel->r_info);
9922 bfd_r_type = elfNN_aarch64_bfd_reloc_from_type (abfd, r_type);
a06ea964
NC
9923
9924 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
9925 {
695344c0 9926 /* xgettext:c-format */
871b3ab2 9927 _bfd_error_handler (_("%pB: bad symbol index: %d"), abfd, r_symndx);
a06ea964
NC
9928 return FALSE;
9929 }
9930
ed5acf27 9931 if (r_symndx < symtab_hdr->sh_info)
1419bbe5
WN
9932 {
9933 /* A local symbol. */
f1dfbfdb 9934 isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
1419bbe5
WN
9935 abfd, r_symndx);
9936 if (isym == NULL)
9937 return FALSE;
9938
9939 /* Check relocation against local STT_GNU_IFUNC symbol. */
9940 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
9941 {
9942 h = elfNN_aarch64_get_local_sym_hash (htab, abfd, rel,
9943 TRUE);
9944 if (h == NULL)
9945 return FALSE;
9946
9947 /* Fake a STT_GNU_IFUNC symbol. */
9948 h->type = STT_GNU_IFUNC;
9949 h->def_regular = 1;
9950 h->ref_regular = 1;
9951 h->forced_local = 1;
9952 h->root.type = bfd_link_hash_defined;
9953 }
9954 else
9955 h = NULL;
9956 }
a06ea964
NC
9957 else
9958 {
9959 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
9960 while (h->root.type == bfd_link_hash_indirect
9961 || h->root.type == bfd_link_hash_warning)
9962 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9963 }
9964
f3ecc5c8
AC
9965 /* Ignore TLS relocations against weak undef symbols and warn about them.
9966 The behaviour of weak TLS variables is not well defined. Since making
9967 these well behaved is not a priority for Morello, we simply ignore
9968 TLS relocations against such symbols here to avoid the linker crashing
9969 on these and to enable making progress in other areas. */
9970 if (r_symndx
9971 && h
9972 && IS_AARCH64_TLS_RELOC (bfd_r_type)
9973 && h->root.type == bfd_link_hash_undefweak)
9974 {
9975 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
9976 _bfd_error_handler (_("%pB(%pA+%#" PRIx64 "): ignoring TLS relocation "
9977 "%s against undef weak symbol %s"),
9978 abfd, sec,
9979 (uint64_t) rel->r_offset,
9980 elfNN_aarch64_howto_table[howto_index].name,
9981 h->root.root.string);
9982 continue;
9983 }
9984
a06ea964 9985 /* Could be done earlier, if h were already available. */
ece841e8
MM
9986 bfd_boolean requires_c64_tls_stub;
9987 bfd_r_type = aarch64_tls_transition (abfd, info, rel, h, r_symndx,
9988 &requires_c64_tls_stub);
9989 if (requires_c64_tls_stub
9990 && !c64_record_tls_stub (htab, abfd, h, r_symndx))
9991 {
9992 _bfd_error_handler (_("%pB: failed to record TLS stub"), abfd);
9993 bfd_set_error (bfd_error_no_memory);
9994 return FALSE;
9995 }
a06ea964 9996
1419bbe5
WN
9997 if (h != NULL)
9998 {
18f822a0
JW
9999 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
10000 This shows up in particular in an R_AARCH64_PREL64 in large model
10001 when calculating the pc-relative address to .got section which is
10002 used to initialize the gp register. */
10003 if (h->root.root.string
10004 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
10005 {
10006 if (htab->root.dynobj == NULL)
10007 htab->root.dynobj = abfd;
10008
10009 if (! aarch64_elf_create_got_section (htab->root.dynobj, info))
10010 return FALSE;
10011
10012 BFD_ASSERT (h == htab->root.hgot);
10013 }
10014
1419bbe5
WN
10015 /* Create the ifunc sections for static executables. If we
10016 never see an indirect function symbol nor we are building
10017 a static executable, those sections will be empty and
10018 won't appear in output. */
10019 switch (bfd_r_type)
10020 {
10021 default:
10022 break;
10023
e19e9199
SP
10024 case BFD_RELOC_MORELLO_CALL26:
10025 case BFD_RELOC_MORELLO_JUMP26:
ce336788
JW
10026 case BFD_RELOC_AARCH64_ADD_LO12:
10027 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
92504105 10028 case BFD_RELOC_MORELLO_ADR_GOT_PAGE:
ce336788 10029 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
92504105 10030 case BFD_RELOC_MORELLO_ADR_HI20_PCREL:
1419bbe5 10031 case BFD_RELOC_AARCH64_CALL26:
ce336788 10032 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
1419bbe5 10033 case BFD_RELOC_AARCH64_JUMP26:
7018c030 10034 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
1419bbe5 10035 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
a2e1db00 10036 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
99ad26cb 10037 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
1419bbe5 10038 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
a1bdea65 10039 case BFD_RELOC_MORELLO_LD128_GOT_LO12_NC:
dc8008f5 10040 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
74a1bfe1 10041 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
ce336788 10042 case BFD_RELOC_AARCH64_NN:
1419bbe5
WN
10043 if (htab->root.dynobj == NULL)
10044 htab->root.dynobj = abfd;
10045 if (!_bfd_elf_create_ifunc_sections (htab->root.dynobj, info))
10046 return FALSE;
10047 break;
10048 }
10049
2d0ca824 10050 /* It is referenced by a non-shared object. */
1419bbe5 10051 h->ref_regular = 1;
1419bbe5
WN
10052 }
10053
a6bb11b2 10054 switch (bfd_r_type)
a06ea964 10055 {
79e74192
RL
10056 case BFD_RELOC_AARCH64_16:
10057#if ARCH_SIZE == 64
10058 case BFD_RELOC_AARCH64_32:
10059#endif
279b2f94 10060 if (bfd_link_pic (info) && (sec->flags & SEC_ALLOC) != 0)
79e74192 10061 {
279b2f94
RL
10062 if (h != NULL
10063 /* This is an absolute symbol. It represents a value instead
10064 of an address. */
c691de6a 10065 && (bfd_is_abs_symbol (&h->root)
279b2f94
RL
10066 /* This is an undefined symbol. */
10067 || h->root.type == bfd_link_hash_undefined))
10068 break;
10069
10070 /* For local symbols, defined global symbols in a non-ABS section,
10071 it is assumed that the value is an address. */
79e74192
RL
10072 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
10073 _bfd_error_handler
10074 /* xgettext:c-format */
871b3ab2 10075 (_("%pB: relocation %s against `%s' can not be used when making "
79e74192
RL
10076 "a shared object"),
10077 abfd, elfNN_aarch64_howto_table[howto_index].name,
10078 (h) ? h->root.root.string : "a local symbol");
10079 bfd_set_error (bfd_error_bad_value);
10080 return FALSE;
10081 }
10082 else
10083 break;
10084
6353d82b
JW
10085 case BFD_RELOC_AARCH64_MOVW_G0_NC:
10086 case BFD_RELOC_AARCH64_MOVW_G1_NC:
10087 case BFD_RELOC_AARCH64_MOVW_G2_NC:
10088 case BFD_RELOC_AARCH64_MOVW_G3:
10089 if (bfd_link_pic (info))
10090 {
10091 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
10092 _bfd_error_handler
10093 /* xgettext:c-format */
871b3ab2 10094 (_("%pB: relocation %s against `%s' can not be used when making "
6353d82b
JW
10095 "a shared object; recompile with -fPIC"),
10096 abfd, elfNN_aarch64_howto_table[howto_index].name,
10097 (h) ? h->root.root.string : "a local symbol");
10098 bfd_set_error (bfd_error_bad_value);
10099 return FALSE;
10100 }
10101 /* Fall through. */
10102
10103 case BFD_RELOC_AARCH64_16_PCREL:
10104 case BFD_RELOC_AARCH64_32_PCREL:
10105 case BFD_RELOC_AARCH64_64_PCREL:
10106 case BFD_RELOC_AARCH64_ADD_LO12:
10107 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
10108 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
92504105
SP
10109 case BFD_RELOC_MORELLO_ADR_HI20_NC_PCREL:
10110 case BFD_RELOC_MORELLO_ADR_HI20_PCREL:
6353d82b
JW
10111 case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
10112 case BFD_RELOC_AARCH64_LDST128_LO12:
10113 case BFD_RELOC_AARCH64_LDST16_LO12:
10114 case BFD_RELOC_AARCH64_LDST32_LO12:
10115 case BFD_RELOC_AARCH64_LDST64_LO12:
10116 case BFD_RELOC_AARCH64_LDST8_LO12:
10117 case BFD_RELOC_AARCH64_LD_LO19_PCREL:
f7d2c675 10118 case BFD_RELOC_MORELLO_LD_LO17_PCREL:
6353d82b
JW
10119 if (h == NULL || bfd_link_pic (info))
10120 break;
10121 /* Fall through. */
10122
a6bb11b2 10123 case BFD_RELOC_AARCH64_NN:
a06ea964
NC
10124
10125 /* We don't need to handle relocs into sections not going into
10126 the "real" output. */
10127 if ((sec->flags & SEC_ALLOC) == 0)
10128 break;
10129
10130 if (h != NULL)
10131 {
0e1862bb 10132 if (!bfd_link_pic (info))
a06ea964
NC
10133 h->non_got_ref = 1;
10134
10135 h->plt.refcount += 1;
10136 h->pointer_equality_needed = 1;
10137 }
10138
10139 /* No need to do anything if we're not creating a shared
10140 object. */
6353d82b
JW
10141 if (!(bfd_link_pic (info)
10142 /* If on the other hand, we are creating an executable, we
10143 may need to keep relocations for symbols satisfied by a
10144 dynamic library if we manage to avoid copy relocs for the
10145 symbol.
10146
10147 NOTE: Currently, there is no support of copy relocs
10148 elimination on pc-relative relocation types, because there is
10149 no dynamic relocation support for them in glibc. We still
10150 record the dynamic symbol reference for them. This is
10151 because one symbol may be referenced by both absolute
10152 relocation (for example, BFD_RELOC_AARCH64_NN) and
10153 pc-relative relocation. We need full symbol reference
10154 information to make correct decision later in
10155 elfNN_aarch64_adjust_dynamic_symbol. */
10156 || (ELIMINATE_COPY_RELOCS
10157 && !bfd_link_pic (info)
10158 && h != NULL
10159 && (h->root.type == bfd_link_hash_defweak
10160 || !h->def_regular))))
a06ea964
NC
10161 break;
10162
10163 {
10164 struct elf_dyn_relocs *p;
10165 struct elf_dyn_relocs **head;
6353d82b 10166 int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
a06ea964
NC
10167
10168 /* We must copy these reloc types into the output file.
10169 Create a reloc section in dynobj and make room for
10170 this reloc. */
10171 if (sreloc == NULL)
10172 {
10173 if (htab->root.dynobj == NULL)
10174 htab->root.dynobj = abfd;
10175
10176 sreloc = _bfd_elf_make_dynamic_reloc_section
0608afa7 10177 (sec, htab->root.dynobj, LOG_FILE_ALIGN, abfd, /*rela? */ TRUE);
a06ea964
NC
10178
10179 if (sreloc == NULL)
10180 return FALSE;
10181 }
10182
10183 /* If this is a global symbol, we count the number of
10184 relocations we need for this symbol. */
10185 if (h != NULL)
10186 {
190eb1dd 10187 head = &h->dyn_relocs;
a06ea964
NC
10188 }
10189 else
10190 {
10191 /* Track dynamic relocs needed for local syms too.
10192 We really need local syms available to do this
10193 easily. Oh well. */
10194
10195 asection *s;
10196 void **vpp;
a06ea964 10197
f1dfbfdb 10198 isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
a06ea964
NC
10199 abfd, r_symndx);
10200 if (isym == NULL)
10201 return FALSE;
10202
10203 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
10204 if (s == NULL)
10205 s = sec;
10206
10207 /* Beware of type punned pointers vs strict aliasing
10208 rules. */
10209 vpp = &(elf_section_data (s)->local_dynrel);
10210 head = (struct elf_dyn_relocs **) vpp;
10211 }
10212
10213 p = *head;
10214 if (p == NULL || p->sec != sec)
10215 {
986f0783 10216 size_t amt = sizeof *p;
a06ea964
NC
10217 p = ((struct elf_dyn_relocs *)
10218 bfd_zalloc (htab->root.dynobj, amt));
10219 if (p == NULL)
10220 return FALSE;
10221 p->next = *head;
10222 *head = p;
10223 p->sec = sec;
10224 }
10225
10226 p->count += 1;
10227
6353d82b
JW
10228 if (elfNN_aarch64_howto_table[howto_index].pc_relative)
10229 p->pc_count += 1;
a06ea964
NC
10230 }
10231 break;
10232
10233 /* RR: We probably want to keep a consistency check that
10234 there are no dangling GOT_PAGE relocs. */
92504105 10235 case BFD_RELOC_MORELLO_ADR_GOT_PAGE:
a1bdea65 10236 case BFD_RELOC_MORELLO_LD128_GOT_LO12_NC:
4ca9b406
SP
10237 case BFD_RELOC_MORELLO_TLSDESC_ADR_PAGE20:
10238 case BFD_RELOC_MORELLO_TLSDESC_LD128_LO12:
1408e112
MM
10239 case BFD_RELOC_MORELLO_TLSIE_ADR_GOTTPREL_PAGE20:
10240 case BFD_RELOC_MORELLO_TLSIE_ADD_LO12:
a1bdea65
SP
10241 htab->c64_rel = 1;
10242 /* Fall through. */
10243
10244 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
7bcccb57 10245 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
7018c030 10246 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
7bcccb57 10247 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
a2e1db00 10248 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
99ad26cb 10249 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
7bcccb57 10250 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
dc8008f5 10251 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
74a1bfe1 10252 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
f955cccf 10253 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
7bcccb57 10254 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
389b8029 10255 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
7bcccb57 10256 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
f955cccf 10257 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12:
1ada945d 10258 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
0484b454
RL
10259 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
10260 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
a6bb11b2 10261 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
7bcccb57 10262 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
3c12b054 10263 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
7ba7cfe4 10264 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
94facae3 10265 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
a6bb11b2 10266 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
a6bb11b2 10267 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
7bcccb57 10268 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
043bf05a 10269 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
3b957e5b
RL
10270 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
10271 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
73f925cc 10272 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
f69e4920 10273 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
77a69ff8 10274 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
a06ea964
NC
10275 {
10276 unsigned got_type;
10277 unsigned old_got_type;
10278
a6bb11b2 10279 got_type = aarch64_reloc_got_type (bfd_r_type);
a06ea964
NC
10280
10281 if (h)
10282 {
10283 h->got.refcount += 1;
cec5225b 10284 old_got_type = elf_aarch64_hash_entry (h)->got_type;
a06ea964
NC
10285 }
10286 else
10287 {
10288 struct elf_aarch64_local_symbol *locals;
10289
cec5225b 10290 if (!elfNN_aarch64_allocate_local_symbols
a06ea964
NC
10291 (abfd, symtab_hdr->sh_info))
10292 return FALSE;
10293
cec5225b 10294 locals = elf_aarch64_locals (abfd);
a06ea964
NC
10295 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
10296 locals[r_symndx].got_refcount += 1;
10297 old_got_type = locals[r_symndx].got_type;
10298 }
10299
10300 /* If a variable is accessed with both general dynamic TLS
10301 methods, two slots may be created. */
10302 if (GOT_TLS_GD_ANY_P (old_got_type) && GOT_TLS_GD_ANY_P (got_type))
10303 got_type |= old_got_type;
10304
10305 /* We will already have issued an error message if there
10306 is a TLS/non-TLS mismatch, based on the symbol type.
10307 So just combine any TLS types needed. */
60d18fc3
MM
10308 if (old_got_type != GOT_UNKNOWN && old_got_type != GOT_NORMAL &&
10309 got_type != GOT_NORMAL)
a06ea964
NC
10310 got_type |= old_got_type;
10311
10312 /* If the symbol is accessed by both IE and GD methods, we
10313 are able to relax. Turn off the GD flag, without
10314 messing up with any other kind of TLS types that may be
10315 involved. */
10316 if ((got_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (got_type))
10317 got_type &= ~ (GOT_TLSDESC_GD | GOT_TLS_GD);
10318
4ca9b406 10319 if (old_got_type != got_type)
a06ea964
NC
10320 {
10321 if (h != NULL)
cec5225b 10322 elf_aarch64_hash_entry (h)->got_type = got_type;
a06ea964
NC
10323 else
10324 {
10325 struct elf_aarch64_local_symbol *locals;
cec5225b 10326 locals = elf_aarch64_locals (abfd);
a06ea964
NC
10327 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
10328 locals[r_symndx].got_type = got_type;
10329 }
10330 }
10331
cc0efaa8
MS
10332 if (htab->root.dynobj == NULL)
10333 htab->root.dynobj = abfd;
10334 if (! aarch64_elf_create_got_section (htab->root.dynobj, info))
10335 return FALSE;
a06ea964
NC
10336 break;
10337 }
10338
e19e9199
SP
10339 case BFD_RELOC_MORELLO_CALL26:
10340 case BFD_RELOC_MORELLO_JUMP26:
10341 htab->c64_rel = 1;
e19e9199 10342 /* Fall through. */
a6bb11b2
YZ
10343 case BFD_RELOC_AARCH64_CALL26:
10344 case BFD_RELOC_AARCH64_JUMP26:
a06ea964 10345 if (h == NULL)
f0070c1e
SP
10346 {
10347 isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, abfd,
10348 r_symndx);
10349 if (isym == NULL)
10350 return FALSE;
10351
10352 asection *s = bfd_section_from_elf_index (abfd, isym->st_shndx);
10353
10354 if (s == NULL)
10355 s = sec;
10356
10357 if (c64_value_p (s, isym->st_value))
10358 isym->st_target_internal |= ST_BRANCH_TO_C64;
10359
10360 /* If this is a local symbol then we resolve it
10361 directly without creating a PLT entry. */
10362 continue;
10363 }
10364
10365 if (h->root.type == bfd_link_hash_defined
10366 || h->root.type == bfd_link_hash_defweak)
10367 {
10368 asection *sym_sec = h->root.u.def.section;
10369 bfd_vma sym_value = h->root.u.def.value;
10370
10371 if (sym_sec != NULL && c64_value_p (sym_sec, sym_value))
10372 h->target_internal |= ST_BRANCH_TO_C64;
10373 }
a06ea964
NC
10374
10375 h->needs_plt = 1;
1419bbe5
WN
10376 if (h->plt.refcount <= 0)
10377 h->plt.refcount = 1;
10378 else
10379 h->plt.refcount += 1;
a06ea964 10380 break;
a6bb11b2 10381
40bbb79e 10382 case BFD_RELOC_MORELLO_CAPINIT:
ece841e8 10383 if (!c64_needs_relocation (info, h))
c0a10a86
MM
10384 /* If this symbol does not need a relocation, then there's no
10385 reason to increase the srelcaps size for a relocation. */
10386 break;
40bbb79e
SP
10387 if (htab->srelcaps == NULL)
10388 {
10389 if (htab->root.dynobj == NULL)
10390 htab->root.dynobj = abfd;
10391
10392 sreloc = _bfd_elf_make_dynamic_reloc_section
10393 (sec, htab->root.dynobj, LOG_FILE_ALIGN,
10394 abfd, /*rela? */ TRUE);
10395
10396 if (sreloc == NULL)
10397 return FALSE;
10398
10399 htab->srelcaps = sreloc;
10400 }
10401 htab->srelcaps->size += RELOC_SIZE (htab);
10402
10403 break;
10404
a6bb11b2
YZ
10405 default:
10406 break;
a06ea964
NC
10407 }
10408 }
a6bb11b2 10409
a06ea964
NC
10410 return TRUE;
10411}
10412
10413/* Treat mapping symbols as special target symbols. */
10414
10415static bfd_boolean
cec5225b 10416elfNN_aarch64_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED,
a06ea964
NC
10417 asymbol *sym)
10418{
10419 return bfd_is_aarch64_special_symbol_name (sym->name,
10420 BFD_AARCH64_SPECIAL_SYM_TYPE_ANY);
10421}
10422
e7679060
AM
10423/* If the ELF symbol SYM might be a function in SEC, return the
10424 function size and set *CODE_OFF to the function's entry point,
10425 otherwise return zero. */
a06ea964 10426
e7679060
AM
10427static bfd_size_type
10428elfNN_aarch64_maybe_function_sym (const asymbol *sym, asection *sec,
10429 bfd_vma *code_off)
10430{
10431 bfd_size_type size;
a06ea964 10432
e7679060
AM
10433 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
10434 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
10435 || sym->section != sec)
10436 return 0;
a06ea964 10437
e7679060
AM
10438 if (!(sym->flags & BSF_SYNTHETIC))
10439 switch (ELF_ST_TYPE (((elf_symbol_type *) sym)->internal_elf_sym.st_info))
10440 {
a06ea964
NC
10441 case STT_FUNC:
10442 case STT_NOTYPE:
a06ea964 10443 break;
e7679060
AM
10444 default:
10445 return 0;
10446 }
a06ea964 10447
e7679060
AM
10448 if ((sym->flags & BSF_LOCAL)
10449 && bfd_is_aarch64_special_symbol_name (sym->name,
10450 BFD_AARCH64_SPECIAL_SYM_TYPE_ANY))
10451 return 0;
a06ea964 10452
e7679060
AM
10453 *code_off = sym->value;
10454 size = 0;
10455 if (!(sym->flags & BSF_SYNTHETIC))
10456 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
10457 if (size == 0)
10458 size = 1;
10459 return size;
a06ea964
NC
10460}
10461
10462static bfd_boolean
cec5225b 10463elfNN_aarch64_find_inliner_info (bfd *abfd,
a06ea964
NC
10464 const char **filename_ptr,
10465 const char **functionname_ptr,
10466 unsigned int *line_ptr)
10467{
10468 bfd_boolean found;
10469 found = _bfd_dwarf2_find_inliner_info
10470 (abfd, filename_ptr,
10471 functionname_ptr, line_ptr, &elf_tdata (abfd)->dwarf2_find_line_info);
10472 return found;
10473}
10474
10475
ed7e9d0b
AM
10476static bfd_boolean
10477elfNN_aarch64_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
a06ea964
NC
10478{
10479 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
10480
ed7e9d0b
AM
10481 if (!_bfd_elf_init_file_header (abfd, link_info))
10482 return FALSE;
10483
a06ea964 10484 i_ehdrp = elf_elfheader (abfd);
a06ea964 10485 i_ehdrp->e_ident[EI_ABIVERSION] = AARCH64_ELF_ABI_VERSION;
ed7e9d0b 10486 return TRUE;
a06ea964
NC
10487}
10488
10489static enum elf_reloc_type_class
cec5225b 10490elfNN_aarch64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
7e612e98
AM
10491 const asection *rel_sec ATTRIBUTE_UNUSED,
10492 const Elf_Internal_Rela *rela)
a06ea964 10493{
f2e6a843
SN
10494 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
10495
10496 if (htab->root.dynsym != NULL
10497 && htab->root.dynsym->contents != NULL)
10498 {
10499 /* Check relocation against STT_GNU_IFUNC symbol if there are
10500 dynamic symbols. */
10501 bfd *abfd = info->output_bfd;
10502 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10503 unsigned long r_symndx = ELFNN_R_SYM (rela->r_info);
10504 if (r_symndx != STN_UNDEF)
10505 {
10506 Elf_Internal_Sym sym;
10507 if (!bed->s->swap_symbol_in (abfd,
10508 (htab->root.dynsym->contents
10509 + r_symndx * bed->s->sizeof_sym),
10510 0, &sym))
10511 {
10512 /* xgettext:c-format */
871b3ab2 10513 _bfd_error_handler (_("%pB symbol number %lu references"
f2e6a843
SN
10514 " nonexistent SHT_SYMTAB_SHNDX section"),
10515 abfd, r_symndx);
10516 /* Ideally an error class should be returned here. */
10517 }
10518 else if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
10519 return reloc_class_ifunc;
10520 }
10521 }
10522
cec5225b 10523 switch ((int) ELFNN_R_TYPE (rela->r_info))
a06ea964 10524 {
f2e6a843 10525 case AARCH64_R (IRELATIVE):
e19e9199 10526 case MORELLO_R (IRELATIVE):
f2e6a843 10527 return reloc_class_ifunc;
a6bb11b2 10528 case AARCH64_R (RELATIVE):
e19e9199 10529 case MORELLO_R (RELATIVE):
a06ea964 10530 return reloc_class_relative;
a6bb11b2 10531 case AARCH64_R (JUMP_SLOT):
e19e9199 10532 case MORELLO_R (JUMP_SLOT):
a06ea964 10533 return reloc_class_plt;
a6bb11b2 10534 case AARCH64_R (COPY):
a06ea964
NC
10535 return reloc_class_copy;
10536 default:
10537 return reloc_class_normal;
10538 }
10539}
10540
a06ea964
NC
10541/* Handle an AArch64 specific section when reading an object file. This is
10542 called when bfd_section_from_shdr finds a section with an unknown
10543 type. */
10544
10545static bfd_boolean
cec5225b 10546elfNN_aarch64_section_from_shdr (bfd *abfd,
a06ea964
NC
10547 Elf_Internal_Shdr *hdr,
10548 const char *name, int shindex)
10549{
10550 /* There ought to be a place to keep ELF backend specific flags, but
10551 at the moment there isn't one. We just keep track of the
10552 sections by their name, instead. Fortunately, the ABI gives
10553 names for all the AArch64 specific sections, so we will probably get
10554 away with this. */
10555 switch (hdr->sh_type)
10556 {
10557 case SHT_AARCH64_ATTRIBUTES:
10558 break;
10559
10560 default:
10561 return FALSE;
10562 }
10563
10564 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
10565 return FALSE;
10566
10567 return TRUE;
10568}
10569
a06ea964
NC
10570typedef struct
10571{
10572 void *finfo;
10573 struct bfd_link_info *info;
10574 asection *sec;
10575 int sec_shndx;
10576 int (*func) (void *, const char *, Elf_Internal_Sym *,
10577 asection *, struct elf_link_hash_entry *);
10578} output_arch_syminfo;
10579
10580enum map_symbol_type
10581{
10582 AARCH64_MAP_INSN,
e19e9199
SP
10583 AARCH64_MAP_DATA,
10584 AARCH64_MAP_C64,
a06ea964
NC
10585};
10586
10587
10588/* Output a single mapping symbol. */
10589
10590static bfd_boolean
cec5225b 10591elfNN_aarch64_output_map_sym (output_arch_syminfo *osi,
a06ea964
NC
10592 enum map_symbol_type type, bfd_vma offset)
10593{
3979cf50 10594 static const char *names[3] = { "$x", "$d", "$c" };
a06ea964
NC
10595 Elf_Internal_Sym sym;
10596
10597 sym.st_value = (osi->sec->output_section->vma
10598 + osi->sec->output_offset + offset);
10599 sym.st_size = 0;
10600 sym.st_other = 0;
10601 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
10602 sym.st_shndx = osi->sec_shndx;
8b21361b 10603 sym.st_target_internal = 0;
a06ea964
NC
10604 return osi->func (osi->finfo, names[type], &sym, osi->sec, NULL) == 1;
10605}
10606
a06ea964
NC
10607/* Output a single local symbol for a generated stub. */
10608
10609static bfd_boolean
cec5225b 10610elfNN_aarch64_output_stub_sym (output_arch_syminfo *osi, const char *name,
a06ea964
NC
10611 bfd_vma offset, bfd_vma size)
10612{
10613 Elf_Internal_Sym sym;
10614
10615 sym.st_value = (osi->sec->output_section->vma
10616 + osi->sec->output_offset + offset);
10617 sym.st_size = size;
10618 sym.st_other = 0;
10619 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
10620 sym.st_shndx = osi->sec_shndx;
8b21361b 10621 sym.st_target_internal = 0;
a06ea964
NC
10622 return osi->func (osi->finfo, name, &sym, osi->sec, NULL) == 1;
10623}
10624
10625static bfd_boolean
10626aarch64_map_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10627{
cec5225b 10628 struct elf_aarch64_stub_hash_entry *stub_entry;
a06ea964
NC
10629 asection *stub_sec;
10630 bfd_vma addr;
10631 char *stub_name;
10632 output_arch_syminfo *osi;
10633
10634 /* Massage our args to the form they really have. */
cec5225b 10635 stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
a06ea964
NC
10636 osi = (output_arch_syminfo *) in_arg;
10637
10638 stub_sec = stub_entry->stub_sec;
10639
10640 /* Ensure this stub is attached to the current section being
10641 processed. */
10642 if (stub_sec != osi->sec)
10643 return TRUE;
10644
10645 addr = (bfd_vma) stub_entry->stub_offset;
10646
10647 stub_name = stub_entry->output_name;
10648
10649 switch (stub_entry->stub_type)
10650 {
10651 case aarch64_stub_adrp_branch:
cec5225b 10652 if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
a06ea964
NC
10653 sizeof (aarch64_adrp_branch_stub)))
10654 return FALSE;
cec5225b 10655 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
a06ea964
NC
10656 return FALSE;
10657 break;
10658 case aarch64_stub_long_branch:
cec5225b 10659 if (!elfNN_aarch64_output_stub_sym
a06ea964
NC
10660 (osi, stub_name, addr, sizeof (aarch64_long_branch_stub)))
10661 return FALSE;
cec5225b 10662 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
a06ea964 10663 return FALSE;
cec5225b 10664 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_DATA, addr + 16))
a06ea964
NC
10665 return FALSE;
10666 break;
68fcca92
JW
10667 case aarch64_stub_erratum_835769_veneer:
10668 if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
10669 sizeof (aarch64_erratum_835769_stub)))
10670 return FALSE;
10671 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
10672 return FALSE;
10673 break;
4106101c
MS
10674 case aarch64_stub_erratum_843419_veneer:
10675 if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
10676 sizeof (aarch64_erratum_843419_stub)))
10677 return FALSE;
10678 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
10679 return FALSE;
10680 break;
50e192f0
SP
10681 case aarch64_stub_branch_c64:
10682 if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
10683 sizeof (aarch64_c64_branch_stub)))
10684 return FALSE;
10685 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_C64, addr))
10686 return FALSE;
10687 break;
10688 case c64_stub_branch_aarch64:
10689 case c64_stub_branch_c64:
10690 if (!elfNN_aarch64_output_stub_sym (osi, stub_name, addr,
10691 sizeof (c64_aarch64_branch_stub)))
10692 return FALSE;
10693 if (!elfNN_aarch64_output_map_sym (osi, AARCH64_MAP_C64, addr))
10694 return FALSE;
10695 break;
9fca35fc
TC
10696 case aarch64_stub_none:
10697 break;
4106101c 10698
a06ea964 10699 default:
8e2fe09f 10700 abort ();
a06ea964
NC
10701 }
10702
10703 return TRUE;
10704}
10705
10706/* Output mapping symbols for linker generated sections. */
10707
10708static bfd_boolean
cec5225b 10709elfNN_aarch64_output_arch_local_syms (bfd *output_bfd,
a06ea964
NC
10710 struct bfd_link_info *info,
10711 void *finfo,
10712 int (*func) (void *, const char *,
10713 Elf_Internal_Sym *,
10714 asection *,
10715 struct elf_link_hash_entry
10716 *))
10717{
10718 output_arch_syminfo osi;
cec5225b 10719 struct elf_aarch64_link_hash_table *htab;
a06ea964 10720
cec5225b 10721 htab = elf_aarch64_hash_table (info);
a06ea964
NC
10722
10723 osi.finfo = finfo;
10724 osi.info = info;
10725 osi.func = func;
10726
10727 /* Long calls stubs. */
10728 if (htab->stub_bfd && htab->stub_bfd->sections)
10729 {
10730 asection *stub_sec;
10731
10732 for (stub_sec = htab->stub_bfd->sections;
10733 stub_sec != NULL; stub_sec = stub_sec->next)
10734 {
10735 /* Ignore non-stub sections. */
10736 if (!strstr (stub_sec->name, STUB_SUFFIX))
10737 continue;
10738
10739 osi.sec = stub_sec;
10740
10741 osi.sec_shndx = _bfd_elf_section_from_bfd_section
10742 (output_bfd, osi.sec->output_section);
10743
61865519
MS
10744 /* The first instruction in a stub is always a branch. */
10745 if (!elfNN_aarch64_output_map_sym (&osi, AARCH64_MAP_INSN, 0))
10746 return FALSE;
10747
a06ea964
NC
10748 bfd_hash_traverse (&htab->stub_hash_table, aarch64_map_one_stub,
10749 &osi);
10750 }
10751 }
10752
10753 /* Finally, output mapping symbols for the PLT. */
10754 if (!htab->root.splt || htab->root.splt->size == 0)
10755 return TRUE;
10756
a06ea964
NC
10757 osi.sec_shndx = _bfd_elf_section_from_bfd_section
10758 (output_bfd, htab->root.splt->output_section);
10759 osi.sec = htab->root.splt;
10760
e19e9199
SP
10761 elfNN_aarch64_output_map_sym (&osi, (htab->c64_rel ? AARCH64_MAP_C64
10762 : AARCH64_MAP_INSN), 0);
a06ea964
NC
10763
10764 return TRUE;
10765
10766}
10767
10768/* Allocate target specific section data. */
10769
10770static bfd_boolean
cec5225b 10771elfNN_aarch64_new_section_hook (bfd *abfd, asection *sec)
a06ea964
NC
10772{
10773 if (!sec->used_by_bfd)
10774 {
10775 _aarch64_elf_section_data *sdata;
986f0783 10776 size_t amt = sizeof (*sdata);
a06ea964
NC
10777
10778 sdata = bfd_zalloc (abfd, amt);
10779 if (sdata == NULL)
10780 return FALSE;
f0070c1e 10781 sdata->elf.is_target_section_data = TRUE;
a06ea964
NC
10782 sec->used_by_bfd = sdata;
10783 }
10784
a06ea964
NC
10785 return _bfd_elf_new_section_hook (abfd, sec);
10786}
10787
10788
a06ea964
NC
10789/* Create dynamic sections. This is different from the ARM backend in that
10790 the got, plt, gotplt and their relocation sections are all created in the
10791 standard part of the bfd elf backend. */
10792
10793static bfd_boolean
cec5225b 10794elfNN_aarch64_create_dynamic_sections (bfd *dynobj,
a06ea964
NC
10795 struct bfd_link_info *info)
10796{
cc0efaa8
MS
10797 /* We need to create .got section. */
10798 if (!aarch64_elf_create_got_section (dynobj, info))
10799 return FALSE;
a06ea964 10800
9d19e4fd 10801 return _bfd_elf_create_dynamic_sections (dynobj, info);
a06ea964
NC
10802}
10803
10804
10805/* Allocate space in .plt, .got and associated reloc sections for
10806 dynamic relocs. */
10807
10808static bfd_boolean
cec5225b 10809elfNN_aarch64_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
a06ea964
NC
10810{
10811 struct bfd_link_info *info;
cec5225b
YZ
10812 struct elf_aarch64_link_hash_table *htab;
10813 struct elf_aarch64_link_hash_entry *eh;
a06ea964
NC
10814 struct elf_dyn_relocs *p;
10815
10816 /* An example of a bfd_link_hash_indirect symbol is versioned
10817 symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
10818 -> __gxx_personality_v0(bfd_link_hash_defined)
10819
10820 There is no need to process bfd_link_hash_indirect symbols here
10821 because we will also be presented with the concrete instance of
cec5225b 10822 the symbol and elfNN_aarch64_copy_indirect_symbol () will have been
a06ea964 10823 called to copy all relevant data from the generic to the concrete
2d0ca824 10824 symbol instance. */
a06ea964
NC
10825 if (h->root.type == bfd_link_hash_indirect)
10826 return TRUE;
10827
10828 if (h->root.type == bfd_link_hash_warning)
10829 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10830
10831 info = (struct bfd_link_info *) inf;
cec5225b 10832 htab = elf_aarch64_hash_table (info);
a06ea964 10833
1419bbe5
WN
10834 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
10835 here if it is defined and referenced in a non-shared object. */
10836 if (h->type == STT_GNU_IFUNC
10837 && h->def_regular)
10838 return TRUE;
10839 else if (htab->root.dynamic_sections_created && h->plt.refcount > 0)
a06ea964
NC
10840 {
10841 /* Make sure this symbol is output as a dynamic symbol.
07d6d2b8 10842 Undefined weak syms won't yet be marked as dynamic. */
ff07562f
JW
10843 if (h->dynindx == -1 && !h->forced_local
10844 && h->root.type == bfd_link_hash_undefweak)
a06ea964
NC
10845 {
10846 if (!bfd_elf_link_record_dynamic_symbol (info, h))
10847 return FALSE;
10848 }
10849
0e1862bb 10850 if (bfd_link_pic (info) || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
a06ea964
NC
10851 {
10852 asection *s = htab->root.splt;
10853
10854 /* If this is the first .plt entry, make room for the special
10855 first entry. */
10856 if (s->size == 0)
10857 s->size += htab->plt_header_size;
10858
10859 h->plt.offset = s->size;
10860
10861 /* If this symbol is not defined in a regular file, and we are
10862 not generating a shared library, then set the symbol to this
10863 location in the .plt. This is required to make function
10864 pointers compare as equal between the normal executable and
10865 the shared library. */
0e1862bb 10866 if (!bfd_link_pic (info) && !h->def_regular)
a06ea964
NC
10867 {
10868 h->root.u.def.section = s;
10869 h->root.u.def.value = h->plt.offset;
10870 }
10871
10872 /* Make room for this entry. For now we only create the
10873 small model PLT entries. We later need to find a way
10874 of relaxing into these from the large model PLT entries. */
37c18eed 10875 s->size += htab->plt_entry_size;
a06ea964
NC
10876
10877 /* We also need to make an entry in the .got.plt section, which
10878 will be placed in the .got section by the linker script. */
a1bdea65 10879 htab->root.sgotplt->size += GOT_ENTRY_SIZE (htab);
a06ea964
NC
10880
10881 /* We also need to make an entry in the .rela.plt section. */
10882 htab->root.srelplt->size += RELOC_SIZE (htab);
10883
10884 /* We need to ensure that all GOT entries that serve the PLT
10885 are consecutive with the special GOT slots [0] [1] and
10886 [2]. Any addtional relocations, such as
10887 R_AARCH64_TLSDESC, must be placed after the PLT related
10888 entries. We abuse the reloc_count such that during
10889 sizing we adjust reloc_count to indicate the number of
10890 PLT related reserved entries. In subsequent phases when
10891 filling in the contents of the reloc entries, PLT related
10892 entries are placed by computing their PLT index (0
10893 .. reloc_count). While other none PLT relocs are placed
10894 at the slot indicated by reloc_count and reloc_count is
10895 updated. */
10896
10897 htab->root.srelplt->reloc_count++;
823710d5
SN
10898
10899 /* Mark the DSO in case R_<CLS>_JUMP_SLOT relocs against
10900 variant PCS symbols are present. */
10901 if (h->other & STO_AARCH64_VARIANT_PCS)
10902 htab->variant_pcs = 1;
10903
a06ea964
NC
10904 }
10905 else
10906 {
10907 h->plt.offset = (bfd_vma) - 1;
10908 h->needs_plt = 0;
10909 }
10910 }
10911 else
10912 {
10913 h->plt.offset = (bfd_vma) - 1;
10914 h->needs_plt = 0;
10915 }
10916
cec5225b 10917 eh = (struct elf_aarch64_link_hash_entry *) h;
a06ea964
NC
10918 eh->tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
10919
10920 if (h->got.refcount > 0)
10921 {
10922 bfd_boolean dyn;
cec5225b 10923 unsigned got_type = elf_aarch64_hash_entry (h)->got_type;
a06ea964
NC
10924
10925 h->got.offset = (bfd_vma) - 1;
10926
10927 dyn = htab->root.dynamic_sections_created;
10928
10929 /* Make sure this symbol is output as a dynamic symbol.
07d6d2b8 10930 Undefined weak syms won't yet be marked as dynamic. */
ff07562f
JW
10931 if (dyn && h->dynindx == -1 && !h->forced_local
10932 && h->root.type == bfd_link_hash_undefweak)
a06ea964
NC
10933 {
10934 if (!bfd_elf_link_record_dynamic_symbol (info, h))
10935 return FALSE;
10936 }
10937
10938 if (got_type == GOT_UNKNOWN)
10939 {
10940 }
60d18fc3 10941 else if (got_type == GOT_NORMAL)
a06ea964
NC
10942 {
10943 h->got.offset = htab->root.sgot->size;
a1bdea65 10944 htab->root.sgot->size += GOT_ENTRY_SIZE (htab);
c0a10a86
MM
10945
10946 if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10947 || h->root.type != bfd_link_hash_undefweak)
10948 && (bfd_link_pic (info)
10949 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
10950 /* Undefined weak symbol in static PIE resolves to 0 without
10951 any dynamic relocations. */
10952 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
10953 {
10954 htab->root.srelgot->size += RELOC_SIZE (htab);
10955 }
10956 else if (!htab->c64_rel || !c64_needs_relocation (info, h))
a06ea964 10957 {
c0a10a86
MM
10958 /* Either not relocating for C64, and hence all problematic
10959 relocations are handled above, or this is an undefined weak
10960 symbol that we know will not be resolved to anything by the
10961 runtime do not need a relocation. */
10962 }
aeba84b1
MM
10963 else if (!static_pde (info))
10964 /* Any capability relocations required in something other than a
10965 static PDE should go in the srelgot. N.b. many capability
10966 relocations would be caught by the first clause in this if
10967 chain. */
10968 htab->root.srelgot->size += RELOC_SIZE (htab);
10969 else
10970 /* We want capability relocations in a statically linked
10971 PDE to go in the srelcaps section. */
10972 htab->srelcaps->size += RELOC_SIZE (htab);
a06ea964
NC
10973 }
10974 else
10975 {
10976 int indx;
10977 if (got_type & GOT_TLSDESC_GD)
10978 {
10979 eh->tlsdesc_got_jump_table_offset =
10980 (htab->root.sgotplt->size
10981 - aarch64_compute_jump_table_size (htab));
a1bdea65 10982 htab->root.sgotplt->size += GOT_ENTRY_SIZE (htab) * 2;
a06ea964
NC
10983 h->got.offset = (bfd_vma) - 2;
10984 }
10985
10986 if (got_type & GOT_TLS_GD)
10987 {
10988 h->got.offset = htab->root.sgot->size;
a1bdea65 10989 htab->root.sgot->size += GOT_ENTRY_SIZE (htab) * 2;
a06ea964
NC
10990 }
10991
10992 if (got_type & GOT_TLS_IE)
10993 {
10994 h->got.offset = htab->root.sgot->size;
a1bdea65 10995 htab->root.sgot->size += GOT_ENTRY_SIZE (htab);
a06ea964
NC
10996 }
10997
ece841e8
MM
10998 /* We avoid TLS relocations on undefweak symbols since it is not
10999 well defined. Hence we should not be seeing got entries
11000 on any symbol which would not need a relocation in a C64 binary.
11001 */
11002 BFD_ASSERT (c64_needs_relocation (info, h));
a06ea964
NC
11003 indx = h && h->dynindx != -1 ? h->dynindx : 0;
11004 if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
11005 || h->root.type != bfd_link_hash_undefweak)
6dda7875 11006 && (!bfd_link_executable (info)
a06ea964 11007 || indx != 0
4ca9b406 11008 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)
60d18fc3 11009 || htab->c64_rel))
a06ea964
NC
11010 {
11011 if (got_type & GOT_TLSDESC_GD)
11012 {
11013 htab->root.srelplt->size += RELOC_SIZE (htab);
11014 /* Note reloc_count not incremented here! We have
11015 already adjusted reloc_count for this relocation
11016 type. */
11017
11018 /* TLSDESC PLT is now needed, but not yet determined. */
9bcc30e4 11019 htab->root.tlsdesc_plt = (bfd_vma) - 1;
a06ea964
NC
11020 }
11021
11022 if (got_type & GOT_TLS_GD)
11023 htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
11024
11025 if (got_type & GOT_TLS_IE)
11026 htab->root.srelgot->size += RELOC_SIZE (htab);
11027 }
11028 }
11029 }
11030 else
11031 {
11032 h->got.offset = (bfd_vma) - 1;
11033 }
11034
190eb1dd 11035 if (h->dyn_relocs == NULL)
a06ea964
NC
11036 return TRUE;
11037
11038 /* In the shared -Bsymbolic case, discard space allocated for
11039 dynamic pc-relative relocs against symbols which turn out to be
11040 defined in regular objects. For the normal shared case, discard
11041 space for pc-relative relocs that have become local due to symbol
11042 visibility changes. */
11043
0e1862bb 11044 if (bfd_link_pic (info))
a06ea964
NC
11045 {
11046 /* Relocs that use pc_count are those that appear on a call
07d6d2b8
AM
11047 insn, or certain REL relocs that can generated via assembly.
11048 We want calls to protected symbols to resolve directly to the
11049 function rather than going via the plt. If people want
11050 function pointer comparisons to work as expected then they
11051 should avoid writing weird assembly. */
a06ea964
NC
11052 if (SYMBOL_CALLS_LOCAL (info, h))
11053 {
11054 struct elf_dyn_relocs **pp;
11055
190eb1dd 11056 for (pp = &h->dyn_relocs; (p = *pp) != NULL;)
a06ea964
NC
11057 {
11058 p->count -= p->pc_count;
11059 p->pc_count = 0;
11060 if (p->count == 0)
11061 *pp = p->next;
11062 else
11063 pp = &p->next;
11064 }
11065 }
11066
11067 /* Also discard relocs on undefined weak syms with non-default
07d6d2b8 11068 visibility. */
190eb1dd 11069 if (h->dyn_relocs != NULL && h->root.type == bfd_link_hash_undefweak)
a06ea964 11070 {
ddb7fd0f
L
11071 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
11072 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
190eb1dd 11073 h->dyn_relocs = NULL;
a06ea964
NC
11074
11075 /* Make sure undefined weak symbols are output as a dynamic
11076 symbol in PIEs. */
11077 else if (h->dynindx == -1
11078 && !h->forced_local
ff07562f 11079 && h->root.type == bfd_link_hash_undefweak
a06ea964
NC
11080 && !bfd_elf_link_record_dynamic_symbol (info, h))
11081 return FALSE;
11082 }
11083
11084 }
11085 else if (ELIMINATE_COPY_RELOCS)
11086 {
11087 /* For the non-shared case, discard space for relocs against
07d6d2b8
AM
11088 symbols which turn out to need copy relocs or are not
11089 dynamic. */
a06ea964
NC
11090
11091 if (!h->non_got_ref
11092 && ((h->def_dynamic
11093 && !h->def_regular)
11094 || (htab->root.dynamic_sections_created
11095 && (h->root.type == bfd_link_hash_undefweak
11096 || h->root.type == bfd_link_hash_undefined))))
11097 {
11098 /* Make sure this symbol is output as a dynamic symbol.
11099 Undefined weak syms won't yet be marked as dynamic. */
11100 if (h->dynindx == -1
11101 && !h->forced_local
ff07562f 11102 && h->root.type == bfd_link_hash_undefweak
a06ea964
NC
11103 && !bfd_elf_link_record_dynamic_symbol (info, h))
11104 return FALSE;
11105
11106 /* If that succeeded, we know we'll be keeping all the
11107 relocs. */
11108 if (h->dynindx != -1)
11109 goto keep;
11110 }
11111
190eb1dd 11112 h->dyn_relocs = NULL;
a06ea964
NC
11113
11114 keep:;
11115 }
11116
11117 /* Finally, allocate space. */
190eb1dd 11118 for (p = h->dyn_relocs; p != NULL; p = p->next)
a06ea964
NC
11119 {
11120 asection *sreloc;
11121
11122 sreloc = elf_section_data (p->sec)->sreloc;
11123
11124 BFD_ASSERT (sreloc != NULL);
11125
11126 sreloc->size += p->count * RELOC_SIZE (htab);
11127 }
11128
11129 return TRUE;
11130}
11131
1419bbe5
WN
11132/* Allocate space in .plt, .got and associated reloc sections for
11133 ifunc dynamic relocs. */
11134
11135static bfd_boolean
11136elfNN_aarch64_allocate_ifunc_dynrelocs (struct elf_link_hash_entry *h,
11137 void *inf)
11138{
11139 struct bfd_link_info *info;
11140 struct elf_aarch64_link_hash_table *htab;
1419bbe5
WN
11141
11142 /* An example of a bfd_link_hash_indirect symbol is versioned
11143 symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
11144 -> __gxx_personality_v0(bfd_link_hash_defined)
11145
11146 There is no need to process bfd_link_hash_indirect symbols here
11147 because we will also be presented with the concrete instance of
11148 the symbol and elfNN_aarch64_copy_indirect_symbol () will have been
11149 called to copy all relevant data from the generic to the concrete
2d0ca824 11150 symbol instance. */
1419bbe5
WN
11151 if (h->root.type == bfd_link_hash_indirect)
11152 return TRUE;
11153
11154 if (h->root.type == bfd_link_hash_warning)
11155 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11156
11157 info = (struct bfd_link_info *) inf;
11158 htab = elf_aarch64_hash_table (info);
11159
1419bbe5
WN
11160 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
11161 here if it is defined and referenced in a non-shared object. */
11162 if (h->type == STT_GNU_IFUNC
11163 && h->def_regular)
11164 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
190eb1dd 11165 &h->dyn_relocs,
1419bbe5
WN
11166 htab->plt_entry_size,
11167 htab->plt_header_size,
a1bdea65 11168 GOT_ENTRY_SIZE (htab),
233cc9c1 11169 FALSE);
1419bbe5
WN
11170 return TRUE;
11171}
11172
1419bbe5
WN
11173/* Allocate space in .plt, .got and associated reloc sections for
11174 local ifunc dynamic relocs. */
11175
11176static bfd_boolean
11177elfNN_aarch64_allocate_local_ifunc_dynrelocs (void **slot, void *inf)
11178{
11179 struct elf_link_hash_entry *h
11180 = (struct elf_link_hash_entry *) *slot;
11181
11182 if (h->type != STT_GNU_IFUNC
11183 || !h->def_regular
11184 || !h->ref_regular
11185 || !h->forced_local
11186 || h->root.type != bfd_link_hash_defined)
11187 abort ();
11188
11189 return elfNN_aarch64_allocate_ifunc_dynrelocs (h, inf);
11190}
a06ea964 11191
a06ea964
NC
11192/* This is the most important function of all . Innocuosly named
11193 though ! */
2d0ca824 11194
a06ea964 11195static bfd_boolean
a1bdea65 11196elfNN_aarch64_size_dynamic_sections (bfd *output_bfd,
a06ea964
NC
11197 struct bfd_link_info *info)
11198{
cec5225b 11199 struct elf_aarch64_link_hash_table *htab;
a06ea964
NC
11200 bfd *dynobj;
11201 asection *s;
11202 bfd_boolean relocs;
11203 bfd *ibfd;
11204
cec5225b 11205 htab = elf_aarch64_hash_table ((info));
a06ea964
NC
11206 dynobj = htab->root.dynobj;
11207
11208 BFD_ASSERT (dynobj != NULL);
11209
11210 if (htab->root.dynamic_sections_created)
11211 {
9b8b325a 11212 if (bfd_link_executable (info) && !info->nointerp)
a06ea964
NC
11213 {
11214 s = bfd_get_linker_section (dynobj, ".interp");
11215 if (s == NULL)
11216 abort ();
11217 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
11218 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
11219 }
11220 }
11221
a1bdea65
SP
11222 aarch64_elf_init_got_section (output_bfd, info);
11223
e19e9199
SP
11224 setup_plt_values (info, elf_aarch64_tdata (output_bfd)->plt_type);
11225
a06ea964
NC
11226 /* Set up .got offsets for local syms, and space for local dynamic
11227 relocs. */
c72f2fb2 11228 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
a06ea964
NC
11229 {
11230 struct elf_aarch64_local_symbol *locals = NULL;
11231 Elf_Internal_Shdr *symtab_hdr;
11232 asection *srel;
11233 unsigned int i;
11234
11235 if (!is_aarch64_elf (ibfd))
11236 continue;
11237
11238 for (s = ibfd->sections; s != NULL; s = s->next)
11239 {
11240 struct elf_dyn_relocs *p;
11241
11242 for (p = (struct elf_dyn_relocs *)
11243 (elf_section_data (s)->local_dynrel); p != NULL; p = p->next)
11244 {
11245 if (!bfd_is_abs_section (p->sec)
11246 && bfd_is_abs_section (p->sec->output_section))
11247 {
11248 /* Input section has been discarded, either because
11249 it is a copy of a linkonce section or due to
11250 linker script /DISCARD/, so we'll be discarding
11251 the relocs too. */
11252 }
11253 else if (p->count != 0)
11254 {
11255 srel = elf_section_data (p->sec)->sreloc;
11256 srel->size += p->count * RELOC_SIZE (htab);
11257 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
11258 info->flags |= DF_TEXTREL;
11259 }
11260 }
11261 }
11262
cec5225b 11263 locals = elf_aarch64_locals (ibfd);
a06ea964
NC
11264 if (!locals)
11265 continue;
11266
11267 symtab_hdr = &elf_symtab_hdr (ibfd);
11268 srel = htab->root.srelgot;
11269 for (i = 0; i < symtab_hdr->sh_info; i++)
11270 {
11271 locals[i].got_offset = (bfd_vma) - 1;
11272 locals[i].tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
11273 if (locals[i].got_refcount > 0)
11274 {
11275 unsigned got_type = locals[i].got_type;
11276 if (got_type & GOT_TLSDESC_GD)
11277 {
11278 locals[i].tlsdesc_got_jump_table_offset =
11279 (htab->root.sgotplt->size
11280 - aarch64_compute_jump_table_size (htab));
a1bdea65 11281 htab->root.sgotplt->size += GOT_ENTRY_SIZE (htab) * 2;
a06ea964
NC
11282 locals[i].got_offset = (bfd_vma) - 2;
11283 }
11284
11285 if (got_type & GOT_TLS_GD)
11286 {
11287 locals[i].got_offset = htab->root.sgot->size;
a1bdea65 11288 htab->root.sgot->size += GOT_ENTRY_SIZE (htab) * 2;
a06ea964
NC
11289 }
11290
b53b1bed 11291 if (got_type & GOT_TLS_IE
60d18fc3 11292 || got_type & GOT_NORMAL)
a06ea964
NC
11293 {
11294 locals[i].got_offset = htab->root.sgot->size;
a1bdea65 11295 htab->root.sgot->size += GOT_ENTRY_SIZE (htab);
a06ea964
NC
11296 }
11297
11298 if (got_type == GOT_UNKNOWN)
11299 {
11300 }
11301
0e1862bb 11302 if (bfd_link_pic (info))
a06ea964
NC
11303 {
11304 if (got_type & GOT_TLSDESC_GD)
11305 {
11306 htab->root.srelplt->size += RELOC_SIZE (htab);
11307 /* Note RELOC_COUNT not incremented here! */
9bcc30e4 11308 htab->root.tlsdesc_plt = (bfd_vma) - 1;
a06ea964
NC
11309 }
11310
11311 if (got_type & GOT_TLS_GD)
11312 htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
11313
b53b1bed 11314 if (got_type & GOT_TLS_IE
60d18fc3 11315 || got_type & GOT_NORMAL)
a06ea964
NC
11316 htab->root.srelgot->size += RELOC_SIZE (htab);
11317 }
aeba84b1
MM
11318 /* Static non-PIE; put relocs into srelcaps. */
11319 else if (static_pde (info) && htab->c64_rel)
8d4edc5f 11320 htab->srelcaps->size += RELOC_SIZE (htab);
d9f4f6fc 11321 /* Else capability relocation needs to go into srelgot. */
60d18fc3 11322 else if (htab->c64_rel)
d9f4f6fc 11323 htab->root.srelgot->size += RELOC_SIZE (htab);
a06ea964
NC
11324 }
11325 else
11326 {
11327 locals[i].got_refcount = (bfd_vma) - 1;
11328 }
11329 }
11330 }
11331
11332
11333 /* Allocate global sym .plt and .got entries, and space for global
11334 sym dynamic relocs. */
cec5225b 11335 elf_link_hash_traverse (&htab->root, elfNN_aarch64_allocate_dynrelocs,
a06ea964
NC
11336 info);
11337
1419bbe5
WN
11338 /* Allocate global ifunc sym .plt and .got entries, and space for global
11339 ifunc sym dynamic relocs. */
11340 elf_link_hash_traverse (&htab->root, elfNN_aarch64_allocate_ifunc_dynrelocs,
11341 info);
11342
1419bbe5
WN
11343 /* Allocate .plt and .got entries, and space for local ifunc symbols. */
11344 htab_traverse (htab->loc_hash_table,
11345 elfNN_aarch64_allocate_local_ifunc_dynrelocs,
11346 info);
a06ea964 11347
aeba84b1 11348 if (static_pde (info)
d6cf562a
MM
11349 && htab->srelcaps
11350 && htab->srelcaps->size > 0)
11351 {
11352 struct elf_link_hash_entry *h;
11353
11354 h = _bfd_elf_define_linkage_sym (output_bfd, info,
11355 htab->srelcaps,
11356 "__rela_dyn_start");
11357 h = _bfd_elf_define_linkage_sym (output_bfd, info,
11358 htab->srelcaps,
11359 "__rela_dyn_end");
11360
11361 h->root.u.def.value = htab->srelcaps->vma + htab->srelcaps->size;
11362 }
11363
a06ea964
NC
11364 /* For every jump slot reserved in the sgotplt, reloc_count is
11365 incremented. However, when we reserve space for TLS descriptors,
11366 it's not incremented, so in order to compute the space reserved
11367 for them, it suffices to multiply the reloc count by the jump
11368 slot size. */
11369
11370 if (htab->root.srelplt)
8847944f 11371 htab->sgotplt_jump_table_size = aarch64_compute_jump_table_size (htab);
a06ea964 11372
9bcc30e4 11373 if (htab->root.tlsdesc_plt)
a06ea964
NC
11374 {
11375 if (htab->root.splt->size == 0)
37c18eed 11376 htab->root.splt->size += htab->plt_header_size;
a06ea964 11377
a06ea964 11378 /* If we're not using lazy TLS relocations, don't generate the
ce12121b 11379 GOT and PLT entry required. */
9bcc30e4
L
11380 if ((info->flags & DF_BIND_NOW))
11381 htab->root.tlsdesc_plt = 0;
11382 else
a06ea964 11383 {
9bcc30e4 11384 htab->root.tlsdesc_plt = htab->root.splt->size;
ce12121b
TC
11385 htab->root.splt->size += htab->tlsdesc_plt_entry_size;
11386
9bcc30e4 11387 htab->root.tlsdesc_got = htab->root.sgot->size;
a1bdea65 11388 htab->root.sgot->size += GOT_ENTRY_SIZE (htab);
a06ea964
NC
11389 }
11390 }
11391
68fcca92 11392 /* Init mapping symbols information to use later to distingush between
4106101c
MS
11393 code and data while scanning for errata. */
11394 if (htab->fix_erratum_835769 || htab->fix_erratum_843419)
68fcca92
JW
11395 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11396 {
11397 if (!is_aarch64_elf (ibfd))
11398 continue;
7ff36d1a 11399 bfd_elfNN_aarch64_init_maps (ibfd, info);
68fcca92
JW
11400 }
11401
a06ea964
NC
11402 /* We now have determined the sizes of the various dynamic sections.
11403 Allocate memory for them. */
11404 relocs = FALSE;
11405 for (s = dynobj->sections; s != NULL; s = s->next)
11406 {
11407 if ((s->flags & SEC_LINKER_CREATED) == 0)
11408 continue;
11409
11410 if (s == htab->root.splt
11411 || s == htab->root.sgot
11412 || s == htab->root.sgotplt
11413 || s == htab->root.iplt
9d19e4fd 11414 || s == htab->root.igotplt
5474d94f 11415 || s == htab->root.sdynbss
ece841e8
MM
11416 || s == htab->root.sdynrelro
11417 || s == htab->sc64_tls_stubs)
a06ea964
NC
11418 {
11419 /* Strip this section if we don't need it; see the
11420 comment below. */
11421 }
fd361982 11422 else if (CONST_STRNEQ (bfd_section_name (s), ".rela"))
a06ea964
NC
11423 {
11424 if (s->size != 0 && s != htab->root.srelplt)
11425 relocs = TRUE;
11426
11427 /* We use the reloc_count field as a counter if we need
11428 to copy relocs into the output file. */
11429 if (s != htab->root.srelplt)
11430 s->reloc_count = 0;
11431 }
11432 else
11433 {
11434 /* It's not one of our sections, so don't allocate space. */
11435 continue;
11436 }
11437
11438 if (s->size == 0)
11439 {
11440 /* If we don't need this section, strip it from the
11441 output file. This is mostly to handle .rela.bss and
11442 .rela.plt. We must create both sections in
11443 create_dynamic_sections, because they must be created
11444 before the linker maps input sections to output
11445 sections. The linker does that before
11446 adjust_dynamic_symbol is called, and it is that
11447 function which decides whether anything needs to go
11448 into these sections. */
a06ea964
NC
11449 s->flags |= SEC_EXCLUDE;
11450 continue;
11451 }
11452
11453 if ((s->flags & SEC_HAS_CONTENTS) == 0)
11454 continue;
11455
11456 /* Allocate memory for the section contents. We use bfd_zalloc
07d6d2b8
AM
11457 here in case unused entries are not reclaimed before the
11458 section's contents are written out. This should not happen,
11459 but this way if it does, we get a R_AARCH64_NONE reloc instead
11460 of garbage. */
a06ea964
NC
11461 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
11462 if (s->contents == NULL)
11463 return FALSE;
11464 }
11465
11466 if (htab->root.dynamic_sections_created)
11467 {
11468 /* Add some entries to the .dynamic section. We fill in the
07d6d2b8
AM
11469 values later, in elfNN_aarch64_finish_dynamic_sections, but we
11470 must add the entries now so that we get the correct size for
11471 the .dynamic section. The DT_DEBUG entry is filled in by the
11472 dynamic linker and used by the debugger. */
a06ea964
NC
11473#define add_dynamic_entry(TAG, VAL) \
11474 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
11475
3084d7a2
L
11476 if (!_bfd_elf_add_dynamic_tags (output_bfd, info, relocs))
11477 return FALSE;
a06ea964
NC
11478
11479 if (htab->root.splt->size != 0)
11480 {
823710d5
SN
11481 if (htab->variant_pcs
11482 && !add_dynamic_entry (DT_AARCH64_VARIANT_PCS, 0))
11483 return FALSE;
11484
1dbade74
SD
11485 if ((elf_aarch64_tdata (output_bfd)->plt_type == PLT_BTI_PAC)
11486 && (!add_dynamic_entry (DT_AARCH64_BTI_PLT, 0)
11487 || !add_dynamic_entry (DT_AARCH64_PAC_PLT, 0)))
11488 return FALSE;
11489
11490 else if ((elf_aarch64_tdata (output_bfd)->plt_type == PLT_BTI)
11491 && !add_dynamic_entry (DT_AARCH64_BTI_PLT, 0))
11492 return FALSE;
11493
11494 else if ((elf_aarch64_tdata (output_bfd)->plt_type == PLT_PAC)
11495 && !add_dynamic_entry (DT_AARCH64_PAC_PLT, 0))
37c18eed 11496 return FALSE;
a06ea964 11497 }
a06ea964
NC
11498 }
11499#undef add_dynamic_entry
11500
11501 return TRUE;
a06ea964
NC
11502}
11503
11504static inline void
caed7120
YZ
11505elf_aarch64_update_plt_entry (bfd *output_bfd,
11506 bfd_reloc_code_real_type r_type,
11507 bfd_byte *plt_entry, bfd_vma value)
a06ea964 11508{
caed7120
YZ
11509 reloc_howto_type *howto = elfNN_aarch64_howto_from_bfd_reloc (r_type);
11510
1d75a8e2
NC
11511 /* FIXME: We should check the return value from this function call. */
11512 (void) _bfd_aarch64_elf_put_addend (output_bfd, plt_entry, r_type, howto, value);
a06ea964
NC
11513}
11514
e19e9199
SP
11515static void
11516aarch64_update_c64_plt_entry (bfd *output_bfd, bfd_byte *plt_entry,
11517 bfd_vma plt_base, bfd_vma plt_got_ent)
11518{
11519 /* Fill in the top 20 bits for this: ADRP c16, PLT_GOT + n * 16.
11520 ADRP: ((PG(S+A)-PG(P)) >> 12) & 0xfffff */
11521 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_MORELLO_ADR_HI20_PCREL,
11522 plt_entry,
11523 PG (plt_got_ent) - PG (plt_base));
11524
11525 elf_aarch64_update_plt_entry (output_bfd,
11526 BFD_RELOC_AARCH64_LDST128_LO12,
11527 plt_entry + 4,
11528 PG_OFFSET (plt_got_ent));
11529
11530 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADD_LO12,
11531 plt_entry + 8,
11532 PG_OFFSET (plt_got_ent));
11533}
11534
a06ea964 11535static void
cec5225b
YZ
11536elfNN_aarch64_create_small_pltn_entry (struct elf_link_hash_entry *h,
11537 struct elf_aarch64_link_hash_table
1419bbe5
WN
11538 *htab, bfd *output_bfd,
11539 struct bfd_link_info *info)
a06ea964
NC
11540{
11541 bfd_byte *plt_entry;
11542 bfd_vma plt_index;
11543 bfd_vma got_offset;
11544 bfd_vma gotplt_entry_address;
11545 bfd_vma plt_entry_address;
11546 Elf_Internal_Rela rela;
11547 bfd_byte *loc;
1419bbe5
WN
11548 asection *plt, *gotplt, *relplt;
11549
11550 /* When building a static executable, use .iplt, .igot.plt and
11551 .rela.iplt sections for STT_GNU_IFUNC symbols. */
11552 if (htab->root.splt != NULL)
11553 {
11554 plt = htab->root.splt;
11555 gotplt = htab->root.sgotplt;
11556 relplt = htab->root.srelplt;
11557 }
11558 else
11559 {
11560 plt = htab->root.iplt;
11561 gotplt = htab->root.igotplt;
11562 relplt = htab->root.irelplt;
11563 }
11564
11565 /* Get the index in the procedure linkage table which
11566 corresponds to this symbol. This is the index of this symbol
11567 in all the symbols for which we are making plt entries. The
11568 first entry in the procedure linkage table is reserved.
a06ea964 11569
1419bbe5
WN
11570 Get the offset into the .got table of the entry that
11571 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
11572 bytes. The first three are reserved for the dynamic linker.
692e2b8b 11573
1419bbe5
WN
11574 For static executables, we don't reserve anything. */
11575
11576 if (plt == htab->root.splt)
11577 {
11578 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
a1bdea65 11579 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE (htab);
1419bbe5
WN
11580 }
11581 else
11582 {
11583 plt_index = h->plt.offset / htab->plt_entry_size;
a1bdea65 11584 got_offset = plt_index * GOT_ENTRY_SIZE (htab);
1419bbe5
WN
11585 }
11586
11587 plt_entry = plt->contents + h->plt.offset;
11588 plt_entry_address = plt->output_section->vma
f44a1f8e 11589 + plt->output_offset + h->plt.offset;
1419bbe5
WN
11590 gotplt_entry_address = gotplt->output_section->vma +
11591 gotplt->output_offset + got_offset;
a06ea964
NC
11592
11593 /* Copy in the boiler-plate for the PLTn entry. */
37c18eed
SD
11594 memcpy (plt_entry, htab->plt_entry, htab->plt_entry_size);
11595
e19e9199
SP
11596 if (htab->c64_rel)
11597 aarch64_update_c64_plt_entry (output_bfd, plt_entry, plt_entry_address,
11598 gotplt_entry_address);
11599 else
11600 {
a06ea964 11601
e19e9199
SP
11602 /* First instruction in BTI enabled PLT stub is a BTI
11603 instruction so skip it. */
11604 if (elf_aarch64_tdata (output_bfd)->plt_type & PLT_BTI
11605 && elf_elfheader (output_bfd)->e_type == ET_EXEC)
11606 plt_entry = plt_entry + 4;
a06ea964 11607
e19e9199
SP
11608 /* Fill in the top 21 bits for this: ADRP x16, PLT_GOT + n * 8.
11609 ADRP: ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
11610 elf_aarch64_update_plt_entry (output_bfd,
11611 BFD_RELOC_AARCH64_ADR_HI21_PCREL,
11612 plt_entry,
11613 PG (gotplt_entry_address) -
11614 PG (plt_entry_address));
a06ea964 11615
e19e9199
SP
11616 /* Fill in the lo12 bits for the load from the pltgot. */
11617 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_LDSTNN_LO12,
11618 plt_entry + 4,
11619 PG_OFFSET (gotplt_entry_address));
a06ea964 11620
e19e9199
SP
11621 /* Fill in the lo12 bits for the add from the pltgot entry. */
11622 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADD_LO12,
11623 plt_entry + 8,
11624 PG_OFFSET (gotplt_entry_address));
11625 }
11626
11627 /* All the GOTPLT Entries are essentially initialized to PLT0. Set LSB if
11628 the PLT is C64. */
11629 bfd_vma plt0 = ((plt->output_section->vma + plt->output_offset)
11630 | htab->c64_rel);
11631 bfd_put_NN (output_bfd, plt0, gotplt->contents + got_offset);
a06ea964 11632
a06ea964 11633 rela.r_offset = gotplt_entry_address;
1419bbe5
WN
11634
11635 if (h->dynindx == -1
0e1862bb 11636 || ((bfd_link_executable (info)
1419bbe5
WN
11637 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
11638 && h->def_regular
11639 && h->type == STT_GNU_IFUNC))
11640 {
11641 /* If an STT_GNU_IFUNC symbol is locally defined, generate
11642 R_AARCH64_IRELATIVE instead of R_AARCH64_JUMP_SLOT. */
60d18fc3 11643 rela.r_info = (htab->c64_rel
e19e9199
SP
11644 ? ELFNN_R_INFO (0, MORELLO_R (IRELATIVE))
11645 : ELFNN_R_INFO (0, AARCH64_R (IRELATIVE)));
1419bbe5
WN
11646 rela.r_addend = (h->root.u.def.value
11647 + h->root.u.def.section->output_section->vma
11648 + h->root.u.def.section->output_offset);
11649 }
11650 else
11651 {
11652 /* Fill in the entry in the .rela.plt section. */
60d18fc3 11653 rela.r_info = (htab->c64_rel
e19e9199
SP
11654 ? ELFNN_R_INFO (h->dynindx, MORELLO_R (JUMP_SLOT))
11655 : ELFNN_R_INFO (h->dynindx, AARCH64_R (JUMP_SLOT)));
1419bbe5
WN
11656 rela.r_addend = 0;
11657 }
a06ea964
NC
11658
11659 /* Compute the relocation entry to used based on PLT index and do
11660 not adjust reloc_count. The reloc_count has already been adjusted
11661 to account for this entry. */
1419bbe5 11662 loc = relplt->contents + plt_index * RELOC_SIZE (htab);
cec5225b 11663 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
a06ea964
NC
11664}
11665
11666/* Size sections even though they're not dynamic. We use it to setup
11667 _TLS_MODULE_BASE_, if needed. */
11668
11669static bfd_boolean
cec5225b 11670elfNN_aarch64_always_size_sections (bfd *output_bfd,
a06ea964
NC
11671 struct bfd_link_info *info)
11672{
11673 asection *tls_sec;
11674
0e1862bb 11675 if (bfd_link_relocatable (info))
a06ea964
NC
11676 return TRUE;
11677
11678 tls_sec = elf_hash_table (info)->tls_sec;
11679
11680 if (tls_sec)
11681 {
11682 struct elf_link_hash_entry *tlsbase;
11683
11684 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
11685 "_TLS_MODULE_BASE_", TRUE, TRUE, FALSE);
11686
11687 if (tlsbase)
11688 {
11689 struct bfd_link_hash_entry *h = NULL;
11690 const struct elf_backend_data *bed =
11691 get_elf_backend_data (output_bfd);
11692
11693 if (!(_bfd_generic_link_add_one_symbol
11694 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
11695 tls_sec, 0, NULL, FALSE, bed->collect, &h)))
11696 return FALSE;
11697
11698 tlsbase->type = STT_TLS;
11699 tlsbase = (struct elf_link_hash_entry *) h;
11700 tlsbase->def_regular = 1;
11701 tlsbase->other = STV_HIDDEN;
11702 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
11703 }
11704 }
11705
11706 return TRUE;
11707}
11708
11709/* Finish up dynamic symbol handling. We set the contents of various
11710 dynamic sections here. */
2d0ca824 11711
a06ea964 11712static bfd_boolean
cec5225b 11713elfNN_aarch64_finish_dynamic_symbol (bfd *output_bfd,
a06ea964
NC
11714 struct bfd_link_info *info,
11715 struct elf_link_hash_entry *h,
11716 Elf_Internal_Sym *sym)
11717{
cec5225b
YZ
11718 struct elf_aarch64_link_hash_table *htab;
11719 htab = elf_aarch64_hash_table (info);
a06ea964
NC
11720
11721 if (h->plt.offset != (bfd_vma) - 1)
11722 {
1419bbe5
WN
11723 asection *plt, *gotplt, *relplt;
11724
a06ea964 11725 /* This symbol has an entry in the procedure linkage table. Set
07d6d2b8 11726 it up. */
a06ea964 11727
1419bbe5
WN
11728 /* When building a static executable, use .iplt, .igot.plt and
11729 .rela.iplt sections for STT_GNU_IFUNC symbols. */
11730 if (htab->root.splt != NULL)
11731 {
11732 plt = htab->root.splt;
11733 gotplt = htab->root.sgotplt;
11734 relplt = htab->root.srelplt;
11735 }
11736 else
11737 {
11738 plt = htab->root.iplt;
11739 gotplt = htab->root.igotplt;
11740 relplt = htab->root.irelplt;
11741 }
11742
11743 /* This symbol has an entry in the procedure linkage table. Set
11744 it up. */
11745 if ((h->dynindx == -1
0e1862bb 11746 && !((h->forced_local || bfd_link_executable (info))
1419bbe5
WN
11747 && h->def_regular
11748 && h->type == STT_GNU_IFUNC))
11749 || plt == NULL
11750 || gotplt == NULL
11751 || relplt == NULL)
f955cccf 11752 return FALSE;
a06ea964 11753
1419bbe5 11754 elfNN_aarch64_create_small_pltn_entry (h, htab, output_bfd, info);
a06ea964
NC
11755 if (!h->def_regular)
11756 {
11757 /* Mark the symbol as undefined, rather than as defined in
46b87d49 11758 the .plt section. */
a06ea964 11759 sym->st_shndx = SHN_UNDEF;
46b87d49
WN
11760 /* If the symbol is weak we need to clear the value.
11761 Otherwise, the PLT entry would provide a definition for
11762 the symbol even if the symbol wasn't defined anywhere,
11763 and so the symbol would never be NULL. Leave the value if
11764 there were any relocations where pointer equality matters
11765 (this is a clue for the dynamic linker, to make function
11766 pointer comparisons work between an application and shared
11767 library). */
11768 if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
11769 sym->st_value = 0;
a06ea964
NC
11770 }
11771 }
11772
11773 if (h->got.offset != (bfd_vma) - 1
60d18fc3 11774 && elf_aarch64_hash_entry (h)->got_type == GOT_NORMAL
a377ae2a
SN
11775 /* Undefined weak symbol in static PIE resolves to 0 without
11776 any dynamic relocations. */
11777 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
a06ea964
NC
11778 {
11779 Elf_Internal_Rela rela;
11780 bfd_byte *loc;
11781
11782 /* This symbol has an entry in the global offset table. Set it
07d6d2b8 11783 up. */
a06ea964
NC
11784 if (htab->root.sgot == NULL || htab->root.srelgot == NULL)
11785 abort ();
11786
11787 rela.r_offset = (htab->root.sgot->output_section->vma
11788 + htab->root.sgot->output_offset
11789 + (h->got.offset & ~(bfd_vma) 1));
11790
49206388
WN
11791 if (h->def_regular
11792 && h->type == STT_GNU_IFUNC)
11793 {
0e1862bb 11794 if (bfd_link_pic (info))
49206388
WN
11795 {
11796 /* Generate R_AARCH64_GLOB_DAT. */
11797 goto do_glob_dat;
11798 }
11799 else
11800 {
11801 asection *plt;
11802
11803 if (!h->pointer_equality_needed)
11804 abort ();
11805
11806 /* For non-shared object, we can't use .got.plt, which
11807 contains the real function address if we need pointer
11808 equality. We load the GOT entry with the PLT entry. */
11809 plt = htab->root.splt ? htab->root.splt : htab->root.iplt;
11810 bfd_put_NN (output_bfd, (plt->output_section->vma
11811 + plt->output_offset
11812 + h->plt.offset),
11813 htab->root.sgot->contents
11814 + (h->got.offset & ~(bfd_vma) 1));
11815 return TRUE;
11816 }
11817 }
0e1862bb 11818 else if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
a06ea964 11819 {
0ee3a6db 11820 if (!(h->def_regular || ELF_COMMON_DEF_P (h)))
a06ea964
NC
11821 return FALSE;
11822
11823 BFD_ASSERT ((h->got.offset & 1) != 0);
808c324b
MM
11824 bfd_vma value = h->root.u.def.value
11825 + h->root.u.def.section->output_section->vma
11826 + h->root.u.def.section->output_offset;
60d18fc3 11827 if (htab->c64_rel)
a1bdea65
SP
11828 {
11829 rela.r_info = ELFNN_R_INFO (0, MORELLO_R (RELATIVE));
808c324b
MM
11830 bfd_vma base_value = 0;
11831 if (c64_symbol_adjust (h, value, h->root.u.def.section, info,
11832 &base_value))
11833 rela.r_addend = (value | h->target_internal) - base_value;
11834 else
11835 rela.r_addend = 0;
a1bdea65
SP
11836 }
11837 else
11838 {
11839 rela.r_info = ELFNN_R_INFO (0, AARCH64_R (RELATIVE));
808c324b 11840 rela.r_addend = value;
a1bdea65 11841 }
a06ea964
NC
11842 }
11843 else
11844 {
dc1e8a47 11845 do_glob_dat:
a06ea964 11846 BFD_ASSERT ((h->got.offset & 1) == 0);
cec5225b 11847 bfd_put_NN (output_bfd, (bfd_vma) 0,
a06ea964 11848 htab->root.sgot->contents + h->got.offset);
a1bdea65 11849 rela.r_info = ELFNN_R_INFO (h->dynindx,
60d18fc3
MM
11850 (htab->c64_rel
11851 ? MORELLO_R (GLOB_DAT)
a1bdea65 11852 : AARCH64_R (GLOB_DAT)));
a06ea964
NC
11853 rela.r_addend = 0;
11854 }
11855
11856 loc = htab->root.srelgot->contents;
11857 loc += htab->root.srelgot->reloc_count++ * RELOC_SIZE (htab);
cec5225b 11858 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
a06ea964
NC
11859 }
11860
11861 if (h->needs_copy)
11862 {
11863 Elf_Internal_Rela rela;
5474d94f 11864 asection *s;
a06ea964
NC
11865 bfd_byte *loc;
11866
11867 /* This symbol needs a copy reloc. Set it up. */
a06ea964
NC
11868 if (h->dynindx == -1
11869 || (h->root.type != bfd_link_hash_defined
11870 && h->root.type != bfd_link_hash_defweak)
9d19e4fd 11871 || htab->root.srelbss == NULL)
a06ea964
NC
11872 abort ();
11873
11874 rela.r_offset = (h->root.u.def.value
11875 + h->root.u.def.section->output_section->vma
11876 + h->root.u.def.section->output_offset);
a6bb11b2 11877 rela.r_info = ELFNN_R_INFO (h->dynindx, AARCH64_R (COPY));
a06ea964 11878 rela.r_addend = 0;
afbf7e8e 11879 if (h->root.u.def.section == htab->root.sdynrelro)
5474d94f
AM
11880 s = htab->root.sreldynrelro;
11881 else
11882 s = htab->root.srelbss;
11883 loc = s->contents + s->reloc_count++ * RELOC_SIZE (htab);
cec5225b 11884 bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
a06ea964
NC
11885 }
11886
11887 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. SYM may
11888 be NULL for local symbols. */
11889 if (sym != NULL
9637f6ef 11890 && (h == elf_hash_table (info)->hdynamic
a06ea964
NC
11891 || h == elf_hash_table (info)->hgot))
11892 sym->st_shndx = SHN_ABS;
11893
11894 return TRUE;
11895}
11896
1419bbe5
WN
11897/* Finish up local dynamic symbol handling. We set the contents of
11898 various dynamic sections here. */
11899
11900static bfd_boolean
11901elfNN_aarch64_finish_local_dynamic_symbol (void **slot, void *inf)
11902{
11903 struct elf_link_hash_entry *h
11904 = (struct elf_link_hash_entry *) *slot;
11905 struct bfd_link_info *info
11906 = (struct bfd_link_info *) inf;
11907
11908 return elfNN_aarch64_finish_dynamic_symbol (info->output_bfd,
11909 info, h, NULL);
11910}
11911
a06ea964 11912static void
cec5225b
YZ
11913elfNN_aarch64_init_small_plt0_entry (bfd *output_bfd ATTRIBUTE_UNUSED,
11914 struct elf_aarch64_link_hash_table
a06ea964
NC
11915 *htab)
11916{
11917 /* Fill in PLT0. Fixme:RR Note this doesn't distinguish between
11918 small and large plts and at the minute just generates
11919 the small PLT. */
11920
cec5225b 11921 /* PLT0 of the small PLT looks like this in ELF64 -
a06ea964
NC
11922 stp x16, x30, [sp, #-16]! // Save the reloc and lr on stack.
11923 adrp x16, PLT_GOT + 16 // Get the page base of the GOTPLT
11924 ldr x17, [x16, #:lo12:PLT_GOT+16] // Load the address of the
11925 // symbol resolver
11926 add x16, x16, #:lo12:PLT_GOT+16 // Load the lo12 bits of the
11927 // GOTPLT entry for this.
11928 br x17
cec5225b 11929 PLT0 will be slightly different in ELF32 due to different got entry
2d0ca824 11930 size. */
caed7120 11931 bfd_vma plt_got_2nd_ent; /* Address of GOT[2]. */
a06ea964
NC
11932 bfd_vma plt_base;
11933
11934
37c18eed
SD
11935 memcpy (htab->root.splt->contents, htab->plt0_entry,
11936 htab->plt_header_size);
4d3bb356
SN
11937
11938 /* PR 26312: Explicitly set the sh_entsize to 0 so that
11939 consumers do not think that the section contains fixed
11940 sized objects. */
11941 elf_section_data (htab->root.splt->output_section)->this_hdr.sh_entsize = 0;
a06ea964 11942
caed7120
YZ
11943 plt_got_2nd_ent = (htab->root.sgotplt->output_section->vma
11944 + htab->root.sgotplt->output_offset
a1bdea65 11945 + GOT_ENTRY_SIZE (htab) * 2);
a06ea964
NC
11946
11947 plt_base = htab->root.splt->output_section->vma +
f44a1f8e 11948 htab->root.splt->output_offset;
a06ea964 11949
e19e9199
SP
11950 bfd_byte *plt0_entry = htab->root.splt->contents;
11951
11952 if (htab->c64_rel)
11953 {
11954 aarch64_update_c64_plt_entry (output_bfd, plt0_entry + 4,
11955 plt_base + 4, plt_got_2nd_ent);
11956 return;
11957 }
11958
37c18eed
SD
11959 /* First instruction in BTI enabled PLT stub is a BTI
11960 instruction so skip it. */
37c18eed
SD
11961 if (elf_aarch64_tdata (output_bfd)->plt_type & PLT_BTI)
11962 plt0_entry = plt0_entry + 4;
11963
a06ea964
NC
11964 /* Fill in the top 21 bits for this: ADRP x16, PLT_GOT + n * 8.
11965 ADRP: ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
caed7120 11966 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADR_HI21_PCREL,
37c18eed 11967 plt0_entry + 4,
caed7120 11968 PG (plt_got_2nd_ent) - PG (plt_base + 4));
a06ea964 11969
caed7120 11970 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_LDSTNN_LO12,
37c18eed 11971 plt0_entry + 8,
caed7120 11972 PG_OFFSET (plt_got_2nd_ent));
a06ea964 11973
caed7120 11974 elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADD_LO12,
37c18eed 11975 plt0_entry + 12,
caed7120 11976 PG_OFFSET (plt_got_2nd_ent));
a06ea964
NC
11977}
11978
11979static bfd_boolean
cec5225b 11980elfNN_aarch64_finish_dynamic_sections (bfd *output_bfd,
a06ea964
NC
11981 struct bfd_link_info *info)
11982{
cec5225b 11983 struct elf_aarch64_link_hash_table *htab;
a06ea964
NC
11984 bfd *dynobj;
11985 asection *sdyn;
11986
cec5225b 11987 htab = elf_aarch64_hash_table (info);
a06ea964
NC
11988 dynobj = htab->root.dynobj;
11989 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
11990
11991 if (htab->root.dynamic_sections_created)
11992 {
b2cb8a19 11993 const char *name;
cec5225b 11994 ElfNN_External_Dyn *dyncon, *dynconend;
a06ea964
NC
11995
11996 if (sdyn == NULL || htab->root.sgot == NULL)
11997 abort ();
11998
cec5225b
YZ
11999 dyncon = (ElfNN_External_Dyn *) sdyn->contents;
12000 dynconend = (ElfNN_External_Dyn *) (sdyn->contents + sdyn->size);
a06ea964
NC
12001 for (; dyncon < dynconend; dyncon++)
12002 {
12003 Elf_Internal_Dyn dyn;
12004 asection *s;
12005
cec5225b 12006 bfd_elfNN_swap_dyn_in (dynobj, dyncon, &dyn);
a06ea964
NC
12007
12008 switch (dyn.d_tag)
12009 {
12010 default:
12011 continue;
12012
12013 case DT_PLTGOT:
12014 s = htab->root.sgotplt;
12015 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
12016 break;
12017
12018 case DT_JMPREL:
4ade44b7
AM
12019 s = htab->root.srelplt;
12020 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
a06ea964
NC
12021 break;
12022
12023 case DT_PLTRELSZ:
c955de36 12024 s = htab->root.srelplt;
a06ea964
NC
12025 dyn.d_un.d_val = s->size;
12026 break;
12027
a06ea964
NC
12028 case DT_TLSDESC_PLT:
12029 s = htab->root.splt;
12030 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
9bcc30e4 12031 + htab->root.tlsdesc_plt;
a06ea964
NC
12032 break;
12033
12034 case DT_TLSDESC_GOT:
12035 s = htab->root.sgot;
9bcc30e4 12036 BFD_ASSERT (htab->root.tlsdesc_got != (bfd_vma)-1);
a06ea964 12037 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
9bcc30e4 12038 + htab->root.tlsdesc_got;
a06ea964 12039 break;
b2cb8a19
MM
12040
12041 /* Set the bottom bit of DT_INIT/FINI if the
12042 corresponding function is C64. */
12043 case DT_INIT:
12044 name = info->init_function;
12045 goto get_sym;
12046 case DT_FINI:
12047 name = info->fini_function;
12048get_sym:
12049 /* If it wasn't set by elf_bfd_final_link
12050 then there is nothing to adjust. */
12051 if (dyn.d_un.d_val != 0)
12052 {
12053 struct elf_link_hash_entry * eh;
12054
12055 eh = elf_link_hash_lookup (elf_hash_table (info), name,
12056 FALSE, FALSE, TRUE);
12057 if (eh != NULL)
12058 dyn.d_un.d_val |= eh->target_internal;
12059 }
12060 break;
a06ea964
NC
12061 }
12062
cec5225b 12063 bfd_elfNN_swap_dyn_out (output_bfd, &dyn, dyncon);
a06ea964
NC
12064 }
12065
12066 }
12067
12068 /* Fill in the special first entry in the procedure linkage table. */
12069 if (htab->root.splt && htab->root.splt->size > 0)
12070 {
cec5225b 12071 elfNN_aarch64_init_small_plt0_entry (output_bfd, htab);
a06ea964 12072
9bcc30e4 12073 if (htab->root.tlsdesc_plt && !(info->flags & DF_BIND_NOW))
a06ea964 12074 {
9bcc30e4 12075 BFD_ASSERT (htab->root.tlsdesc_got != (bfd_vma)-1);
cec5225b 12076 bfd_put_NN (output_bfd, (bfd_vma) 0,
9bcc30e4 12077 htab->root.sgot->contents + htab->root.tlsdesc_got);
a06ea964 12078
37c18eed
SD
12079 const bfd_byte *entry = elfNN_aarch64_tlsdesc_small_plt_entry;
12080 htab->tlsdesc_plt_entry_size = PLT_TLSDESC_ENTRY_SIZE;
12081
4ca9b406
SP
12082 unsigned adrp_rtype = BFD_RELOC_AARCH64_ADR_HI21_PCREL;
12083 unsigned ldr_rtype = BFD_RELOC_AARCH64_LDSTNN_LO12;
12084
37c18eed 12085 aarch64_plt_type type = elf_aarch64_tdata (output_bfd)->plt_type;
4ca9b406
SP
12086 if (htab->c64_rel)
12087 {
12088 entry = elfNN_aarch64_tlsdesc_small_plt_c64_entry;
12089 adrp_rtype = BFD_RELOC_MORELLO_ADR_HI20_PCREL;
12090 ldr_rtype = BFD_RELOC_AARCH64_LDST128_LO12;
12091 }
12092 else if (type == PLT_BTI || type == PLT_BTI_PAC)
37c18eed
SD
12093 {
12094 entry = elfNN_aarch64_tlsdesc_small_plt_bti_entry;
37c18eed
SD
12095 }
12096
9bcc30e4 12097 memcpy (htab->root.splt->contents + htab->root.tlsdesc_plt,
37c18eed 12098 entry, htab->tlsdesc_plt_entry_size);
a06ea964
NC
12099
12100 {
12101 bfd_vma adrp1_addr =
12102 htab->root.splt->output_section->vma
9bcc30e4
L
12103 + htab->root.splt->output_offset
12104 + htab->root.tlsdesc_plt + 4;
a06ea964 12105
caed7120 12106 bfd_vma adrp2_addr = adrp1_addr + 4;
a06ea964
NC
12107
12108 bfd_vma got_addr =
12109 htab->root.sgot->output_section->vma
12110 + htab->root.sgot->output_offset;
12111
12112 bfd_vma pltgot_addr =
12113 htab->root.sgotplt->output_section->vma
12114 + htab->root.sgotplt->output_offset;
12115
9bcc30e4 12116 bfd_vma dt_tlsdesc_got = got_addr + htab->root.tlsdesc_got;
caed7120
YZ
12117
12118 bfd_byte *plt_entry =
9bcc30e4 12119 htab->root.splt->contents + htab->root.tlsdesc_plt;
a06ea964 12120
37c18eed
SD
12121 /* First instruction in BTI enabled PLT stub is a BTI
12122 instruction so skip it. */
12123 if (type & PLT_BTI)
12124 {
12125 plt_entry = plt_entry + 4;
12126 adrp1_addr = adrp1_addr + 4;
12127 adrp2_addr = adrp2_addr + 4;
12128 }
12129
a06ea964 12130 /* adrp x2, DT_TLSDESC_GOT */
caed7120 12131 elf_aarch64_update_plt_entry (output_bfd,
4ca9b406 12132 adrp_rtype,
caed7120
YZ
12133 plt_entry + 4,
12134 (PG (dt_tlsdesc_got)
12135 - PG (adrp1_addr)));
a06ea964
NC
12136
12137 /* adrp x3, 0 */
caed7120 12138 elf_aarch64_update_plt_entry (output_bfd,
4ca9b406 12139 adrp_rtype,
caed7120
YZ
12140 plt_entry + 8,
12141 (PG (pltgot_addr)
12142 - PG (adrp2_addr)));
a06ea964
NC
12143
12144 /* ldr x2, [x2, #0] */
caed7120 12145 elf_aarch64_update_plt_entry (output_bfd,
4ca9b406 12146 ldr_rtype,
caed7120
YZ
12147 plt_entry + 12,
12148 PG_OFFSET (dt_tlsdesc_got));
a06ea964
NC
12149
12150 /* add x3, x3, 0 */
caed7120
YZ
12151 elf_aarch64_update_plt_entry (output_bfd,
12152 BFD_RELOC_AARCH64_ADD_LO12,
12153 plt_entry + 16,
12154 PG_OFFSET (pltgot_addr));
a06ea964
NC
12155 }
12156 }
12157 }
12158
12159 if (htab->root.sgotplt)
12160 {
12161 if (bfd_is_abs_section (htab->root.sgotplt->output_section))
12162 {
4eca0228 12163 _bfd_error_handler
871b3ab2 12164 (_("discarded output section: `%pA'"), htab->root.sgotplt);
a06ea964
NC
12165 return FALSE;
12166 }
12167
12168 /* Fill in the first three entries in the global offset table. */
12169 if (htab->root.sgotplt->size > 0)
12170 {
8db339a6
MS
12171 bfd_put_NN (output_bfd, (bfd_vma) 0, htab->root.sgotplt->contents);
12172
a06ea964 12173 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
cec5225b 12174 bfd_put_NN (output_bfd,
a06ea964 12175 (bfd_vma) 0,
a1bdea65 12176 htab->root.sgotplt->contents + GOT_ENTRY_SIZE (htab));
cec5225b 12177 bfd_put_NN (output_bfd,
a06ea964 12178 (bfd_vma) 0,
a1bdea65
SP
12179 (htab->root.sgotplt->contents
12180 + GOT_ENTRY_SIZE (htab) * 2));
a06ea964
NC
12181 }
12182
8db339a6
MS
12183 if (htab->root.sgot)
12184 {
12185 if (htab->root.sgot->size > 0)
12186 {
12187 bfd_vma addr =
12188 sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0;
12189 bfd_put_NN (output_bfd, addr, htab->root.sgot->contents);
12190 }
12191 }
12192
a06ea964 12193 elf_section_data (htab->root.sgotplt->output_section)->
a1bdea65 12194 this_hdr.sh_entsize = GOT_ENTRY_SIZE (htab);
a06ea964
NC
12195 }
12196
12197 if (htab->root.sgot && htab->root.sgot->size > 0)
12198 elf_section_data (htab->root.sgot->output_section)->this_hdr.sh_entsize
a1bdea65 12199 = GOT_ENTRY_SIZE (htab);
a06ea964 12200
1419bbe5
WN
12201 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
12202 htab_traverse (htab->loc_hash_table,
12203 elfNN_aarch64_finish_local_dynamic_symbol,
12204 info);
12205
a06ea964
NC
12206 return TRUE;
12207}
12208
37c18eed
SD
12209/* Check if BTI enabled PLTs are needed. Returns the type needed. */
12210static aarch64_plt_type
12211get_plt_type (bfd *abfd)
12212{
12213 aarch64_plt_type ret = PLT_NORMAL;
12214 bfd_byte *contents, *extdyn, *extdynend;
12215 asection *sec = bfd_get_section_by_name (abfd, ".dynamic");
12216 if (!sec || !bfd_malloc_and_get_section (abfd, sec, &contents))
12217 return ret;
12218 extdyn = contents;
12219 extdynend = contents + sec->size;
12220 for (; extdyn < extdynend; extdyn += sizeof (ElfNN_External_Dyn))
12221 {
12222 Elf_Internal_Dyn dyn;
12223 bfd_elfNN_swap_dyn_in (abfd, extdyn, &dyn);
12224
12225 /* Let's check the processor specific dynamic array tags. */
12226 bfd_vma tag = dyn.d_tag;
12227 if (tag < DT_LOPROC || tag > DT_HIPROC)
12228 continue;
12229
12230 switch (tag)
12231 {
12232 case DT_AARCH64_BTI_PLT:
1dbade74
SD
12233 ret |= PLT_BTI;
12234 break;
12235
12236 case DT_AARCH64_PAC_PLT:
12237 ret |= PLT_PAC;
37c18eed
SD
12238 break;
12239
12240 default: break;
12241 }
12242 }
12243 free (contents);
12244 return ret;
12245}
12246
12247static long
12248elfNN_aarch64_get_synthetic_symtab (bfd *abfd,
12249 long symcount,
12250 asymbol **syms,
12251 long dynsymcount,
12252 asymbol **dynsyms,
12253 asymbol **ret)
12254{
12255 elf_aarch64_tdata (abfd)->plt_type = get_plt_type (abfd);
12256 return _bfd_elf_get_synthetic_symtab (abfd, symcount, syms,
12257 dynsymcount, dynsyms, ret);
12258}
12259
a06ea964
NC
12260/* Return address for Ith PLT stub in section PLT, for relocation REL
12261 or (bfd_vma) -1 if it should not be included. */
12262
12263static bfd_vma
cec5225b 12264elfNN_aarch64_plt_sym_val (bfd_vma i, const asection *plt,
a06ea964
NC
12265 const arelent *rel ATTRIBUTE_UNUSED)
12266{
37c18eed
SD
12267 size_t plt0_size = PLT_ENTRY_SIZE;
12268 size_t pltn_size = PLT_SMALL_ENTRY_SIZE;
12269
1dbade74
SD
12270 if (elf_aarch64_tdata (plt->owner)->plt_type == PLT_BTI_PAC)
12271 {
1dbade74
SD
12272 if (elf_elfheader (plt->owner)->e_type == ET_EXEC)
12273 pltn_size = PLT_BTI_PAC_SMALL_ENTRY_SIZE;
12274 else
12275 pltn_size = PLT_PAC_SMALL_ENTRY_SIZE;
12276 }
12277 else if (elf_aarch64_tdata (plt->owner)->plt_type == PLT_BTI)
37c18eed 12278 {
37c18eed
SD
12279 if (elf_elfheader (plt->owner)->e_type == ET_EXEC)
12280 pltn_size = PLT_BTI_SMALL_ENTRY_SIZE;
12281 }
1dbade74
SD
12282 else if (elf_aarch64_tdata (plt->owner)->plt_type == PLT_PAC)
12283 {
1dbade74
SD
12284 pltn_size = PLT_PAC_SMALL_ENTRY_SIZE;
12285 }
12286
37c18eed 12287 return plt->vma + plt0_size + i * pltn_size;
a06ea964
NC
12288}
12289
d691934d
NC
12290/* Returns TRUE if NAME is an AArch64 mapping symbol.
12291 The ARM ELF standard defines $x (for A64 code) and $d (for data).
12292 It also allows a period initiated suffix to be added to the symbol, ie:
12293 "$[adtx]\.[:sym_char]+". */
12294
12295static bfd_boolean
12296is_aarch64_mapping_symbol (const char * name)
12297{
12298 return name != NULL /* Paranoia. */
12299 && name[0] == '$' /* Note: if objcopy --prefix-symbols has been used then
12300 the mapping symbols could have acquired a prefix.
12301 We do not support this here, since such symbols no
12302 longer conform to the ARM ELF ABI. */
3979cf50 12303 && (name[1] == 'd' || name[1] == 'x' || name[1] == 'c')
d691934d
NC
12304 && (name[2] == 0 || name[2] == '.');
12305 /* FIXME: Strictly speaking the symbol is only a valid mapping symbol if
12306 any characters that follow the period are legal characters for the body
12307 of a symbol's name. For now we just assume that this is the case. */
12308}
12309
12310/* Make sure that mapping symbols in object files are not removed via the
12311 "strip --strip-unneeded" tool. These symbols might needed in order to
12312 correctly generate linked files. Once an object file has been linked,
12313 it should be safe to remove them. */
12314
12315static void
12316elfNN_aarch64_backend_symbol_processing (bfd *abfd, asymbol *sym)
12317{
12318 if (((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
12319 && sym->section != bfd_abs_section_ptr
12320 && is_aarch64_mapping_symbol (sym->name))
12321 sym->flags |= BSF_KEEP;
12322}
12323
cd702818
SD
12324/* Implement elf_backend_setup_gnu_properties for AArch64. It serves as a
12325 wrapper function for _bfd_aarch64_elf_link_setup_gnu_properties to account
12326 for the effect of GNU properties of the output_bfd. */
12327static bfd *
12328elfNN_aarch64_link_setup_gnu_properties (struct bfd_link_info *info)
12329{
12330 uint32_t prop = elf_aarch64_tdata (info->output_bfd)->gnu_and_prop;
12331 bfd *pbfd = _bfd_aarch64_elf_link_setup_gnu_properties (info, &prop);
12332 elf_aarch64_tdata (info->output_bfd)->gnu_and_prop = prop;
37c18eed
SD
12333 elf_aarch64_tdata (info->output_bfd)->plt_type
12334 |= (prop & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) ? PLT_BTI : 0;
cd702818
SD
12335 return pbfd;
12336}
12337
12338/* Implement elf_backend_merge_gnu_properties for AArch64. It serves as a
12339 wrapper function for _bfd_aarch64_elf_merge_gnu_properties to account
12340 for the effect of GNU properties of the output_bfd. */
12341static bfd_boolean
12342elfNN_aarch64_merge_gnu_properties (struct bfd_link_info *info,
4e539114 12343 bfd *abfd, bfd *bbfd,
cd702818
SD
12344 elf_property *aprop,
12345 elf_property *bprop)
12346{
12347 uint32_t prop
12348 = elf_aarch64_tdata (info->output_bfd)->gnu_and_prop;
37c18eed
SD
12349
12350 /* If output has been marked with BTI using command line argument, give out
12351 warning if necessary. */
4e539114
SD
12352 /* Properties are merged per type, hence only check for warnings when merging
12353 GNU_PROPERTY_AARCH64_FEATURE_1_AND. */
12354 if (((aprop && aprop->pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
12355 || (bprop && bprop->pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND))
12356 && (prop & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
37c18eed
SD
12357 && (!elf_aarch64_tdata (info->output_bfd)->no_bti_warn))
12358 {
12359 if ((aprop && !(aprop->u.number & GNU_PROPERTY_AARCH64_FEATURE_1_BTI))
4e539114 12360 || !aprop)
37c18eed 12361 {
8bf6d176 12362 _bfd_error_handler (_("%pB: warning: BTI turned on by -z force-bti when "
4e539114
SD
12363 "all inputs do not have BTI in NOTE section."),
12364 abfd);
12365 }
12366 if ((bprop && !(bprop->u.number & GNU_PROPERTY_AARCH64_FEATURE_1_BTI))
12367 || !bprop)
12368 {
8bf6d176 12369 _bfd_error_handler (_("%pB: warning: BTI turned on by -z force-bti when "
4e539114
SD
12370 "all inputs do not have BTI in NOTE section."),
12371 bbfd);
37c18eed
SD
12372 }
12373 }
12374
cd702818
SD
12375 return _bfd_aarch64_elf_merge_gnu_properties (info, abfd, aprop,
12376 bprop, prop);
12377}
a06ea964 12378
8b21361b
SP
12379/* Demangle c64 function symbols as we read them in. */
12380
12381static bfd_boolean
12382aarch64_elfNN_swap_symbol_in (bfd * abfd,
12383 const void *psrc,
12384 const void *pshn,
12385 Elf_Internal_Sym *dst)
12386{
12387 if (!bfd_elfNN_swap_symbol_in (abfd, psrc, pshn, dst))
12388 return FALSE;
12389
12390 dst->st_target_internal = 0;
12391
12392 if (ELF_ST_TYPE (dst->st_info) == STT_FUNC
12393 || ELF_ST_TYPE (dst->st_info) == STT_GNU_IFUNC)
12394 {
12395 dst->st_target_internal = dst->st_value & ST_BRANCH_TO_C64;
12396 dst->st_value &= ~(bfd_vma) ST_BRANCH_TO_C64;
12397 }
12398
12399 return TRUE;
12400}
12401
12402
12403/* Mangle c64 function symbols as we write them out. */
12404
12405static void
12406aarch64_elfNN_swap_symbol_out (bfd *abfd,
12407 const Elf_Internal_Sym *src,
12408 void *cdst,
12409 void *shndx)
12410{
12411 Elf_Internal_Sym newsym = *src;
12412
12413 if ((ELF_ST_TYPE (newsym.st_info) == STT_FUNC
12414 || ELF_ST_TYPE (newsym.st_info) == STT_GNU_IFUNC)
12415 && newsym.st_shndx != SHN_UNDEF)
12416 newsym.st_value |= newsym.st_target_internal;
12417
12418 bfd_elfNN_swap_symbol_out (abfd, &newsym, cdst, shndx);
12419}
12420
a1bdea65
SP
12421/* Define the size of a GOT element for the generic mid-end. */
12422
12423static bfd_vma
12424elfNN_aarch64_got_elt_size (bfd *abfd ATTRIBUTE_UNUSED,
12425 struct bfd_link_info *info,
12426 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
12427 bfd *ibfd ATTRIBUTE_UNUSED,
12428 unsigned long symndx ATTRIBUTE_UNUSED)
12429{
12430 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
12431
12432 return GOT_ENTRY_SIZE (htab);
12433}
12434
f938669f
SP
12435/* Define the size of a GOT header, which is the minimum size of the GOT section
12436 when one is needed. */
12437
12438static bfd_vma
a1bdea65 12439elfNN_aarch64_got_header_size (struct bfd_link_info *info)
f938669f 12440{
a1bdea65
SP
12441 struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
12442
12443 return GOT_ENTRY_SIZE (htab) * GOT_RESERVED_HEADER_SLOTS;
f938669f
SP
12444}
12445
fa6ca5e2
SP
12446/* Identify the 'C' in the CIE augmentation string. */
12447
12448static bfd_boolean
12449elf64_aarch64_eh_frame_augmentation_char (const char aug)
12450{
12451 return aug == 'C';
12452}
12453
a06ea964
NC
12454/* We use this so we can override certain functions
12455 (though currently we don't). */
12456
cec5225b 12457const struct elf_size_info elfNN_aarch64_size_info =
a06ea964 12458{
cec5225b
YZ
12459 sizeof (ElfNN_External_Ehdr),
12460 sizeof (ElfNN_External_Phdr),
12461 sizeof (ElfNN_External_Shdr),
12462 sizeof (ElfNN_External_Rel),
12463 sizeof (ElfNN_External_Rela),
12464 sizeof (ElfNN_External_Sym),
12465 sizeof (ElfNN_External_Dyn),
a06ea964
NC
12466 sizeof (Elf_External_Note),
12467 4, /* Hash table entry size. */
12468 1, /* Internal relocs per external relocs. */
cec5225b
YZ
12469 ARCH_SIZE, /* Arch size. */
12470 LOG_FILE_ALIGN, /* Log_file_align. */
12471 ELFCLASSNN, EV_CURRENT,
12472 bfd_elfNN_write_out_phdrs,
12473 bfd_elfNN_write_shdrs_and_ehdr,
12474 bfd_elfNN_checksum_contents,
12475 bfd_elfNN_write_relocs,
8b21361b
SP
12476 aarch64_elfNN_swap_symbol_in,
12477 aarch64_elfNN_swap_symbol_out,
cec5225b
YZ
12478 bfd_elfNN_slurp_reloc_table,
12479 bfd_elfNN_slurp_symbol_table,
12480 bfd_elfNN_swap_dyn_in,
12481 bfd_elfNN_swap_dyn_out,
12482 bfd_elfNN_swap_reloc_in,
12483 bfd_elfNN_swap_reloc_out,
12484 bfd_elfNN_swap_reloca_in,
12485 bfd_elfNN_swap_reloca_out
a06ea964
NC
12486};
12487
12488#define ELF_ARCH bfd_arch_aarch64
12489#define ELF_MACHINE_CODE EM_AARCH64
12490#define ELF_MAXPAGESIZE 0x10000
12491#define ELF_MINPAGESIZE 0x1000
12492#define ELF_COMMONPAGESIZE 0x1000
12493
cec5225b
YZ
12494#define bfd_elfNN_bfd_is_target_special_symbol \
12495 elfNN_aarch64_is_target_special_symbol
a06ea964 12496
07d6d2b8 12497#define bfd_elfNN_bfd_link_hash_table_create \
cec5225b 12498 elfNN_aarch64_link_hash_table_create
a06ea964 12499
cec5225b
YZ
12500#define bfd_elfNN_bfd_merge_private_bfd_data \
12501 elfNN_aarch64_merge_private_bfd_data
a06ea964 12502
cec5225b
YZ
12503#define bfd_elfNN_bfd_print_private_bfd_data \
12504 elfNN_aarch64_print_private_bfd_data
a06ea964 12505
cec5225b
YZ
12506#define bfd_elfNN_bfd_reloc_type_lookup \
12507 elfNN_aarch64_reloc_type_lookup
a06ea964 12508
cec5225b
YZ
12509#define bfd_elfNN_bfd_reloc_name_lookup \
12510 elfNN_aarch64_reloc_name_lookup
a06ea964 12511
cec5225b
YZ
12512#define bfd_elfNN_bfd_set_private_flags \
12513 elfNN_aarch64_set_private_flags
a06ea964 12514
cec5225b
YZ
12515#define bfd_elfNN_find_inliner_info \
12516 elfNN_aarch64_find_inliner_info
a06ea964 12517
37c18eed
SD
12518#define bfd_elfNN_get_synthetic_symtab \
12519 elfNN_aarch64_get_synthetic_symtab
12520
cec5225b
YZ
12521#define bfd_elfNN_mkobject \
12522 elfNN_aarch64_mkobject
a06ea964 12523
cec5225b
YZ
12524#define bfd_elfNN_new_section_hook \
12525 elfNN_aarch64_new_section_hook
a06ea964
NC
12526
12527#define elf_backend_adjust_dynamic_symbol \
cec5225b 12528 elfNN_aarch64_adjust_dynamic_symbol
a06ea964
NC
12529
12530#define elf_backend_always_size_sections \
cec5225b 12531 elfNN_aarch64_always_size_sections
a06ea964
NC
12532
12533#define elf_backend_check_relocs \
cec5225b 12534 elfNN_aarch64_check_relocs
a06ea964
NC
12535
12536#define elf_backend_copy_indirect_symbol \
cec5225b 12537 elfNN_aarch64_copy_indirect_symbol
a06ea964 12538
823710d5
SN
12539#define elf_backend_merge_symbol_attribute \
12540 elfNN_aarch64_merge_symbol_attribute
12541
a06ea964
NC
12542/* Create .dynbss, and .rela.bss sections in DYNOBJ, and set up shortcuts
12543 to them in our hash. */
12544#define elf_backend_create_dynamic_sections \
cec5225b 12545 elfNN_aarch64_create_dynamic_sections
a06ea964
NC
12546
12547#define elf_backend_init_index_section \
12548 _bfd_elf_init_2_index_sections
12549
a06ea964 12550#define elf_backend_finish_dynamic_sections \
cec5225b 12551 elfNN_aarch64_finish_dynamic_sections
a06ea964
NC
12552
12553#define elf_backend_finish_dynamic_symbol \
cec5225b 12554 elfNN_aarch64_finish_dynamic_symbol
a06ea964 12555
a06ea964 12556#define elf_backend_object_p \
cec5225b 12557 elfNN_aarch64_object_p
a06ea964 12558
07d6d2b8 12559#define elf_backend_output_arch_local_syms \
cec5225b 12560 elfNN_aarch64_output_arch_local_syms
a06ea964 12561
e7679060
AM
12562#define elf_backend_maybe_function_sym \
12563 elfNN_aarch64_maybe_function_sym
12564
a06ea964 12565#define elf_backend_plt_sym_val \
cec5225b 12566 elfNN_aarch64_plt_sym_val
a06ea964 12567
ed7e9d0b
AM
12568#define elf_backend_init_file_header \
12569 elfNN_aarch64_init_file_header
a06ea964
NC
12570
12571#define elf_backend_relocate_section \
cec5225b 12572 elfNN_aarch64_relocate_section
a06ea964
NC
12573
12574#define elf_backend_reloc_type_class \
cec5225b 12575 elfNN_aarch64_reloc_type_class
a06ea964 12576
a06ea964 12577#define elf_backend_section_from_shdr \
cec5225b 12578 elfNN_aarch64_section_from_shdr
a06ea964
NC
12579
12580#define elf_backend_size_dynamic_sections \
cec5225b 12581 elfNN_aarch64_size_dynamic_sections
a06ea964
NC
12582
12583#define elf_backend_size_info \
cec5225b 12584 elfNN_aarch64_size_info
a06ea964 12585
68fcca92
JW
12586#define elf_backend_write_section \
12587 elfNN_aarch64_write_section
12588
d691934d
NC
12589#define elf_backend_symbol_processing \
12590 elfNN_aarch64_backend_symbol_processing
12591
cd702818
SD
12592#define elf_backend_setup_gnu_properties \
12593 elfNN_aarch64_link_setup_gnu_properties
12594
12595#define elf_backend_merge_gnu_properties \
12596 elfNN_aarch64_merge_gnu_properties
12597
f938669f
SP
12598#define elf_backend_got_header_size \
12599 elfNN_aarch64_got_header_size
12600
a1bdea65
SP
12601#define elf_backend_got_elt_size \
12602 elfNN_aarch64_got_elt_size
12603
fa6ca5e2
SP
12604#define elf_backend_eh_frame_augmentation_char \
12605 elf64_aarch64_eh_frame_augmentation_char
12606
a06ea964 12607#define elf_backend_can_refcount 1
59c108f7 12608#define elf_backend_can_gc_sections 1
a06ea964
NC
12609#define elf_backend_plt_readonly 1
12610#define elf_backend_want_got_plt 1
12611#define elf_backend_want_plt_sym 0
5474d94f 12612#define elf_backend_want_dynrelro 1
a06ea964
NC
12613#define elf_backend_may_use_rel_p 0
12614#define elf_backend_may_use_rela_p 1
12615#define elf_backend_default_use_rela_p 1
07d6d2b8 12616#define elf_backend_rela_normal 1
64f52338 12617#define elf_backend_dtrel_excludes_plt 1
c495064d 12618#define elf_backend_default_execstack 0
32f573bc 12619#define elf_backend_extern_protected_data 1
7f784814 12620#define elf_backend_hash_symbol elf_aarch64_hash_symbol
a06ea964 12621
07d6d2b8 12622#undef elf_backend_obj_attrs_section
a06ea964
NC
12623#define elf_backend_obj_attrs_section ".ARM.attributes"
12624
cec5225b 12625#include "elfNN-target.h"
a75cf613
ES
12626
12627/* CloudABI support. */
12628
12629#undef TARGET_LITTLE_SYM
12630#define TARGET_LITTLE_SYM aarch64_elfNN_le_cloudabi_vec
12631#undef TARGET_LITTLE_NAME
12632#define TARGET_LITTLE_NAME "elfNN-littleaarch64-cloudabi"
12633#undef TARGET_BIG_SYM
12634#define TARGET_BIG_SYM aarch64_elfNN_be_cloudabi_vec
12635#undef TARGET_BIG_NAME
12636#define TARGET_BIG_NAME "elfNN-bigaarch64-cloudabi"
12637
12638#undef ELF_OSABI
12639#define ELF_OSABI ELFOSABI_CLOUDABI
12640
12641#undef elfNN_bed
12642#define elfNN_bed elfNN_aarch64_cloudabi_bed
12643
12644#include "elfNN-target.h"