Coverage Report

Created: 2026-03-10 08:46

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