Coverage Report

Created: 2026-05-11 07:54

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
0
#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
34
{
2195
34
  static bool initialized_p = false;
2196
  /* Indexed by R_TYPE, values are offsets in the howto_table.  */
2197
34
  static unsigned int offsets[R_AARCH64_end];
2198
2199
34
  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
34
  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
33
  if (r_type >= R_AARCH64_end)
2215
28
    {
2216
28
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2217
28
        abfd, r_type);
2218
28
      bfd_set_error (bfd_error_bad_value);
2219
28
      return BFD_RELOC_AARCH64_NONE;
2220
28
    }
2221
2222
5
  return BFD_RELOC_AARCH64_RELOC_START + offsets[r_type];
2223
33
}
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
34
{
2252
34
  unsigned int i;
2253
2254
  /* Convert bfd generic reloc to AArch64-specific reloc.  */
2255
34
  if (code < BFD_RELOC_AARCH64_RELOC_START
2256
34
      || 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
34
  if (code > BFD_RELOC_AARCH64_RELOC_START
2265
29
      && code < BFD_RELOC_AARCH64_RELOC_END)
2266
29
    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
34
  if (code == BFD_RELOC_AARCH64_NONE)
2270
29
    return &elf64_aarch64_howto_none;
2271
2272
5
  if (code == BFD_RELOC_AARCH64_BRANCH9)
2273
0
    return &elf64_aarch64_howto_none;
2274
2275
5
  return NULL;
2276
5
}
2277
2278
static reloc_howto_type *
2279
elf64_aarch64_howto_from_type (bfd *abfd, unsigned int r_type)
2280
54
{
2281
54
  bfd_reloc_code_real_type val;
2282
54
  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
54
  if (r_type == R_AARCH64_NONE)
2293
20
    return &elf64_aarch64_howto_none;
2294
2295
34
  val = elf64_aarch64_bfd_reloc_from_type (abfd, r_type);
2296
34
  howto = elf64_aarch64_howto_from_bfd_reloc (val);
2297
2298
34
  if (howto != NULL)
2299
29
    return howto;
2300
2301
5
  bfd_set_error (bfd_error_bad_value);
2302
5
  return NULL;
2303
34
}
2304
2305
static bool
2306
elf64_aarch64_info_to_howto (bfd *abfd, arelent *bfd_reloc,
2307
           Elf_Internal_Rela *elf_reloc)
2308
54
{
2309
54
  unsigned int r_type;
2310
2311
54
  r_type = ELF64_R_TYPE (elf_reloc->r_info);
2312
54
  bfd_reloc->howto = elf64_aarch64_howto_from_type (abfd, r_type);
2313
2314
54
  if (bfd_reloc->howto == NULL)
2315
5
    {
2316
      /* xgettext:c-format */
2317
5
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, r_type);
2318
5
      return false;
2319
5
    }
2320
49
  return true;
2321
54
}
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
14.8k
{
2543
14.8k
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_aarch64_obj_tdata));
2544
14.8k
}
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_symtab_hdr (input_bfd);
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_TLS_DTPREL:
6501
0
      if (input_section->flags & SEC_ALLOC)
6502
0
  return bfd_reloc_notsupported;
6503
0
      value -= dtpoff_base (info);
6504
0
      value += rel->r_addend;
6505
6506
0
      bfd_put_64 (output_bfd, value, contents + rel->r_offset);
6507
6508
0
      *unresolved_reloc_p = false;
6509
0
      break;
6510
6511
0
    case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
6512
0
    case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
6513
0
      if (globals->root.sgot == NULL)
6514
0
  return bfd_reloc_notsupported;
6515
6516
0
      value = (symbol_tlsdesc_got_offset (input_bfd, h, r_symndx)
6517
0
         + globals->root.sgotplt->output_section->vma
6518
0
         + globals->root.sgotplt->output_offset
6519
0
         + globals->sgotplt_jump_table_size);
6520
6521
0
      value -= (globals->root.sgot->output_section->vma
6522
0
    + globals->root.sgot->output_offset);
6523
6524
0
      value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
6525
0
               place, value,
6526
0
               0, weak_undef_p);
6527
0
      *unresolved_reloc_p = false;
6528
0
      break;
6529
6530
0
    default:
6531
0
      return bfd_reloc_notsupported;
6532
0
    }
6533
6534
0
  if (saved_addend)
6535
0
    *saved_addend = value;
6536
6537
  /* Only apply the final relocation in a sequence.  */
6538
0
  if (save_addend)
6539
0
    return bfd_reloc_continue;
6540
6541
0
  return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type,
6542
0
              howto, value);
6543
0
}
6544
6545
/* LP64 and ILP32 operates on x- and w-registers respectively.
6546
   Next definitions take into account the difference between
6547
   corresponding machine codes. R means x-register if the target
6548
   arch is LP64, and w-register if the target is ILP32.  */
6549
6550
#if ARCH_SIZE == 64
6551
0
# define add_R0_R0  (0x91000000)
6552
0
# define add_R0_R0_R1 (0x8b000020)
6553
0
# define add_R0_R1  (0x91400020)
6554
0
# define ldr_R0   (0x58000000)
6555
0
# define ldr_R0_mask(i) (i & 0xffffffe0)
6556
0
# define ldr_R0_x0  (0xf9400000)
6557
0
# define ldr_hw_R0  (0xf2a00000)
6558
0
# define movk_R0  (0xf2800000)
6559
0
# define movz_R0  (0xd2a00000)
6560
0
# define movz_hw_R0 (0xd2c00000)
6561
#else /*ARCH_SIZE == 32 */
6562
# define add_R0_R0  (0x11000000)
6563
# define add_R0_R0_R1 (0x0b000020)
6564
# define add_R0_R1  (0x11400020)
6565
# define ldr_R0   (0x18000000)
6566
# define ldr_R0_mask(i) (i & 0xbfffffe0)
6567
# define ldr_R0_x0  (0xb9400000)
6568
# define ldr_hw_R0  (0x72a00000)
6569
# define movk_R0  (0x72800000)
6570
# define movz_R0  (0x52a00000)
6571
# define movz_hw_R0 (0x52c00000)
6572
#endif
6573
6574
/* Structure to hold payload for _bfd_aarch64_erratum_843419_clear_stub,
6575
   it is used to identify the stub information to reset.  */
6576
6577
struct erratum_843419_branch_to_stub_clear_data
6578
{
6579
  bfd_vma adrp_offset;
6580
  asection *output_section;
6581
};
6582
6583
/* Clear the erratum information for GEN_ENTRY if the ADRP_OFFSET and
6584
   section inside IN_ARG matches.  The clearing is done by setting the
6585
   stub_type to none.  */
6586
6587
static bool
6588
_bfd_aarch64_erratum_843419_clear_stub (struct bfd_hash_entry *gen_entry,
6589
          void *in_arg)
6590
0
{
6591
0
  struct elf_aarch64_stub_hash_entry *stub_entry
6592
0
    = (struct elf_aarch64_stub_hash_entry *) gen_entry;
6593
0
  struct erratum_843419_branch_to_stub_clear_data *data
6594
0
    = (struct erratum_843419_branch_to_stub_clear_data *) in_arg;
6595
6596
0
  if (stub_entry->target_section != data->output_section
6597
0
      || stub_entry->stub_type != aarch64_stub_erratum_843419_veneer
6598
0
      || stub_entry->adrp_offset != data->adrp_offset)
6599
0
    return true;
6600
6601
  /* Change the stub type instead of removing the entry, removing from the hash
6602
     table would be slower and we have already reserved the memory for the entry
6603
     so there wouldn't be much gain.  Changing the stub also keeps around a
6604
     record of what was there before.  */
6605
0
  stub_entry->stub_type = aarch64_stub_none;
6606
6607
  /* We're done and there could have been only one matching stub at that
6608
     particular offset, so abort further traversal.  */
6609
0
  return false;
6610
0
}
6611
6612
/* TLS Relaxations may relax an adrp sequence that matches the erratum 843419
6613
   sequence.  In this case the erratum no longer applies and we need to remove
6614
   the entry from the pending stub generation.  This clears matching adrp insn
6615
   at ADRP_OFFSET in INPUT_SECTION in the stub table defined in GLOBALS.  */
6616
6617
static void
6618
clear_erratum_843419_entry (struct elf_aarch64_link_hash_table *globals,
6619
          bfd_vma adrp_offset, asection *input_section)
6620
0
{
6621
0
  if (globals->fix_erratum_843419 & ERRAT_ADRP)
6622
0
    {
6623
0
      struct erratum_843419_branch_to_stub_clear_data data;
6624
0
      data.adrp_offset = adrp_offset;
6625
0
      data.output_section = input_section;
6626
6627
0
      bfd_hash_traverse (&globals->stub_hash_table,
6628
0
       _bfd_aarch64_erratum_843419_clear_stub, &data);
6629
0
    }
6630
0
}
6631
6632
/* Handle TLS relaxations.  Relaxing is possible for symbols that use
6633
   R_AARCH64_TLSDESC_ADR_{PAGE, LD64_LO12_NC, ADD_LO12_NC} during a static
6634
   link.
6635
6636
   Return bfd_reloc_ok if we're done, bfd_reloc_continue if the caller
6637
   is to then call final_link_relocate.  Return other values in the
6638
   case of error.  */
6639
6640
static bfd_reloc_status_type
6641
elf64_aarch64_tls_relax (struct elf_aarch64_link_hash_table *globals,
6642
       bfd *input_bfd, asection *input_section,
6643
       bfd_byte *contents, Elf_Internal_Rela *rel,
6644
       struct elf_link_hash_entry *h,
6645
       struct bfd_link_info *info)
6646
0
{
6647
0
  bool local_exec = bfd_link_executable (info)
6648
0
    && SYMBOL_REFERENCES_LOCAL (info, h);
6649
0
  unsigned int r_type = ELF64_R_TYPE (rel->r_info);
6650
0
  unsigned long insn;
6651
6652
0
  BFD_ASSERT (globals && input_bfd && contents && rel);
6653
6654
0
  switch (elf64_aarch64_bfd_reloc_from_type (input_bfd, r_type))
6655
0
    {
6656
0
    case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
6657
0
    case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
6658
0
      if (local_exec)
6659
0
  {
6660
    /* GD->LE relaxation:
6661
       adrp x0, :tlsgd:var     =>   movz R0, :tprel_g1:var
6662
       or
6663
       adrp x0, :tlsdesc:var   =>   movz R0, :tprel_g1:var
6664
6665
       Where R is x for LP64, and w for ILP32.  */
6666
0
    bfd_putl32 (movz_R0, contents + rel->r_offset);
6667
    /* We have relaxed the adrp into a mov, we may have to clear any
6668
       pending erratum fixes.  */
6669
0
    clear_erratum_843419_entry (globals, rel->r_offset, input_section);
6670
0
    return bfd_reloc_continue;
6671
0
  }
6672
0
      else
6673
0
  {
6674
    /* GD->IE relaxation:
6675
       adrp x0, :tlsgd:var     =>   adrp x0, :gottprel:var
6676
       or
6677
       adrp x0, :tlsdesc:var   =>   adrp x0, :gottprel:var
6678
     */
6679
0
    return bfd_reloc_continue;
6680
0
  }
6681
6682
0
    case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
6683
0
      BFD_ASSERT (0);
6684
0
      break;
6685
6686
0
    case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
6687
0
      if (local_exec)
6688
0
  {
6689
    /* Tiny TLSDESC->LE relaxation:
6690
       ldr   x1, :tlsdesc:var  =>  movz  R0, #:tprel_g1:var
6691
       adr   x0, :tlsdesc:var  =>  movk  R0, #:tprel_g0_nc:var
6692
       .tlsdesccall var
6693
       blr   x1      =>  nop
6694
6695
       Where R is x for LP64, and w for ILP32.  */
6696
0
    BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == AARCH64_R (TLSDESC_ADR_PREL21));
6697
0
    BFD_ASSERT (ELF64_R_TYPE (rel[2].r_info) == AARCH64_R (TLSDESC_CALL));
6698
6699
0
    rel[1].r_info = ELF64_R_INFO (ELF64_R_SYM (rel->r_info),
6700
0
          AARCH64_R (TLSLE_MOVW_TPREL_G0_NC));
6701
0
    rel[2].r_info = ELF64_R_INFO (STN_UNDEF, R_AARCH64_NONE);
6702
6703
0
    bfd_putl32 (movz_R0, contents + rel->r_offset);
6704
0
    bfd_putl32 (movk_R0, contents + rel->r_offset + 4);
6705
0
    bfd_putl32 (INSN_NOP, contents + rel->r_offset + 8);
6706
0
    return bfd_reloc_continue;
6707
0
  }
6708
0
      else
6709
0
  {
6710
    /* Tiny TLSDESC->IE relaxation:
6711
       ldr   x1, :tlsdesc:var  =>  ldr   x0, :gottprel:var
6712
       adr   x0, :tlsdesc:var  =>  nop
6713
       .tlsdesccall var
6714
       blr   x1      =>  nop
6715
     */
6716
0
    BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == AARCH64_R (TLSDESC_ADR_PREL21));
6717
0
    BFD_ASSERT (ELF64_R_TYPE (rel[2].r_info) == AARCH64_R (TLSDESC_CALL));
6718
6719
0
    rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_AARCH64_NONE);
6720
0
    rel[2].r_info = ELF64_R_INFO (STN_UNDEF, R_AARCH64_NONE);
6721
6722
0
    bfd_putl32 (ldr_R0, contents + rel->r_offset);
6723
0
    bfd_putl32 (INSN_NOP, contents + rel->r_offset + 4);
6724
0
    bfd_putl32 (INSN_NOP, contents + rel->r_offset + 8);
6725
0
    return bfd_reloc_continue;
6726
0
  }
6727
6728
0
    case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
6729
0
      if (local_exec)
6730
0
  {
6731
    /* Tiny GD->LE relaxation:
6732
       adr x0, :tlsgd:var      =>   mrs  x1, tpidr_el0
6733
       bl   __tls_get_addr     =>   add  R0, R1, #:tprel_hi12:x, lsl #12
6734
       nop         =>   add  R0, R0, #:tprel_lo12_nc:x
6735
6736
       Where R is x for LP64, and x for Ilp32.  */
6737
6738
    /* First kill the tls_get_addr reloc on the bl instruction.  */
6739
0
    BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
6740
6741
0
    bfd_putl32 (0xd53bd041, contents + rel->r_offset + 0);
6742
0
    bfd_putl32 (add_R0_R1, contents + rel->r_offset + 4);
6743
0
    bfd_putl32 (add_R0_R0, contents + rel->r_offset + 8);
6744
6745
0
    rel[1].r_info = ELF64_R_INFO (ELF64_R_SYM (rel->r_info),
6746
0
          AARCH64_R (TLSLE_ADD_TPREL_LO12_NC));
6747
0
    rel[1].r_offset = rel->r_offset + 8;
6748
6749
    /* Move the current relocation to the second instruction in
6750
       the sequence.  */
6751
0
    rel->r_offset += 4;
6752
0
    rel->r_info = ELF64_R_INFO (ELF64_R_SYM (rel->r_info),
6753
0
              AARCH64_R (TLSLE_ADD_TPREL_HI12));
6754
0
    return bfd_reloc_continue;
6755
0
  }
6756
0
      else
6757
0
  {
6758
    /* Tiny GD->IE relaxation:
6759
       adr x0, :tlsgd:var      =>   ldr  R0, :gottprel:var
6760
       bl   __tls_get_addr     =>   mrs  x1, tpidr_el0
6761
       nop         =>   add  R0, R0, R1
6762
6763
       Where R is x for LP64, and w for Ilp32.  */
6764
6765
    /* First kill the tls_get_addr reloc on the bl instruction.  */
6766
0
    BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
6767
0
    rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_AARCH64_NONE);
6768
6769
0
    bfd_putl32 (ldr_R0, contents + rel->r_offset);
6770
0
    bfd_putl32 (0xd53bd041, contents + rel->r_offset + 4);
6771
0
    bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 8);
6772
0
    return bfd_reloc_continue;
6773
0
  }
6774
6775
0
#if ARCH_SIZE == 64
6776
0
    case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
6777
0
      BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == AARCH64_R (TLSGD_MOVW_G0_NC));
6778
0
      BFD_ASSERT (rel->r_offset + 12 == rel[2].r_offset);
6779
0
      BFD_ASSERT (ELF64_R_TYPE (rel[2].r_info) == AARCH64_R (CALL26));
6780
6781
0
      if (local_exec)
6782
0
  {
6783
    /* Large GD->LE relaxation:
6784
       movz x0, #:tlsgd_g1:var  => movz x0, #:tprel_g2:var, lsl #32
6785
       movk x0, #:tlsgd_g0_nc:var => movk x0, #:tprel_g1_nc:var, lsl #16
6786
       add x0, gp, x0   => movk x0, #:tprel_g0_nc:var
6787
       bl __tls_get_addr    => mrs x1, tpidr_el0
6788
       nop      => add x0, x0, x1
6789
     */
6790
0
    rel[2].r_info = ELF64_R_INFO (ELF64_R_SYM (rel->r_info),
6791
0
          AARCH64_R (TLSLE_MOVW_TPREL_G0_NC));
6792
0
    rel[2].r_offset = rel->r_offset + 8;
6793
6794
0
    bfd_putl32 (movz_hw_R0, contents + rel->r_offset + 0);
6795
0
    bfd_putl32 (ldr_hw_R0, contents + rel->r_offset + 4);
6796
0
    bfd_putl32 (movk_R0, contents + rel->r_offset + 8);
6797
0
    bfd_putl32 (0xd53bd041, contents + rel->r_offset + 12);
6798
0
    bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 16);
6799
0
  }
6800
0
      else
6801
0
  {
6802
    /* Large GD->IE relaxation:
6803
       movz x0, #:tlsgd_g1:var  => movz x0, #:gottprel_g1:var, lsl #16
6804
       movk x0, #:tlsgd_g0_nc:var => movk x0, #:gottprel_g0_nc:var
6805
       add x0, gp, x0   => ldr x0, [gp, x0]
6806
       bl __tls_get_addr    => mrs x1, tpidr_el0
6807
       nop      => add x0, x0, x1
6808
     */
6809
0
    rel[2].r_info = ELF64_R_INFO (STN_UNDEF, R_AARCH64_NONE);
6810
0
    bfd_putl32 (0xd2a80000, contents + rel->r_offset + 0);
6811
0
    bfd_putl32 (ldr_R0, contents + rel->r_offset + 8);
6812
0
    bfd_putl32 (0xd53bd041, contents + rel->r_offset + 12);
6813
0
    bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 16);
6814
0
  }
6815
0
      return bfd_reloc_continue;
6816
6817
0
    case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
6818
0
      return bfd_reloc_continue;
6819
0
#endif
6820
6821
0
    case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
6822
0
      return bfd_reloc_continue;
6823
6824
0
    case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
6825
0
      if (local_exec)
6826
0
  {
6827
    /* GD->LE relaxation:
6828
       ldr xd, [x0, #:tlsdesc_lo12:var]   =>   movk x0, :tprel_g0_nc:var
6829
6830
       Where R is x for lp64 mode, and w for ILP32 mode.  */
6831
0
    bfd_putl32 (movk_R0, contents + rel->r_offset);
6832
0
    return bfd_reloc_continue;
6833
0
  }
6834
0
      else
6835
0
  {
6836
    /* GD->IE relaxation:
6837
       ldr xd, [x0, #:tlsdesc_lo12:var] => ldr R0, [x0, #:gottprel_lo12:var]
6838
6839
       Where R is x for lp64 mode, and w for ILP32 mode.  */
6840
0
    insn = bfd_getl32 (contents + rel->r_offset);
6841
0
    bfd_putl32 (ldr_R0_mask (insn), contents + rel->r_offset);
6842
0
    return bfd_reloc_continue;
6843
0
  }
6844
6845
0
    case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
6846
0
      if (local_exec)
6847
0
  {
6848
    /* GD->LE relaxation
6849
       add  x0, #:tlsgd_lo12:var  => movk R0, :tprel_g0_nc:var
6850
       bl   __tls_get_addr  => mrs  x1, tpidr_el0
6851
       nop      => add  R0, R1, R0
6852
6853
       Where R is x for lp64 mode, and w for ILP32 mode.  */
6854
6855
    /* First kill the tls_get_addr reloc on the bl instruction.  */
6856
0
    BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
6857
0
    rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_AARCH64_NONE);
6858
6859
0
    bfd_putl32 (movk_R0, contents + rel->r_offset);
6860
0
    bfd_putl32 (0xd53bd041, contents + rel->r_offset + 4);
6861
0
    bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 8);
6862
0
    return bfd_reloc_continue;
6863
0
  }
6864
0
      else
6865
0
  {
6866
    /* GD->IE relaxation
6867
       ADD  x0, #:tlsgd_lo12:var  => ldr  R0, [x0, #:gottprel_lo12:var]
6868
       BL   __tls_get_addr  => mrs  x1, tpidr_el0
6869
         R_AARCH64_CALL26
6870
       NOP      => add  R0, R1, R0
6871
6872
       Where R is x for lp64 mode, and w for ilp32 mode.  */
6873
6874
0
    BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
6875
6876
    /* Remove the relocation on the BL instruction.  */
6877
0
    rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_AARCH64_NONE);
6878
6879
    /* We choose to fixup the BL and NOP instructions using the
6880
       offset from the second relocation to allow flexibility in
6881
       scheduling instructions between the ADD and BL.  */
6882
0
    bfd_putl32 (ldr_R0_x0, contents + rel->r_offset);
6883
0
    bfd_putl32 (0xd53bd041, contents + rel[1].r_offset);
6884
0
    bfd_putl32 (add_R0_R0_R1, contents + rel[1].r_offset + 4);
6885
0
    return bfd_reloc_continue;
6886
0
  }
6887
6888
0
    case BFD_RELOC_AARCH64_TLSDESC_ADD:
6889
0
    case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
6890
0
    case BFD_RELOC_AARCH64_TLSDESC_CALL:
6891
      /* GD->IE/LE relaxation:
6892
   add x0, x0, #:tlsdesc_lo12:var   =>   nop
6893
   blr xd         =>   nop
6894
       */
6895
0
      bfd_putl32 (INSN_NOP, contents + rel->r_offset);
6896
0
      return bfd_reloc_ok;
6897
6898
0
    case BFD_RELOC_AARCH64_TLSDESC_LDR:
6899
0
      if (local_exec)
6900
0
  {
6901
    /* GD->LE relaxation:
6902
       ldr xd, [gp, xn]   =>   movk R0, #:tprel_g0_nc:var
6903
6904
       Where R is x for lp64 mode, and w for ILP32 mode.  */
6905
0
    bfd_putl32 (movk_R0, contents + rel->r_offset);
6906
0
    return bfd_reloc_continue;
6907
0
  }
6908
0
      else
6909
0
  {
6910
    /* GD->IE relaxation:
6911
       ldr xd, [gp, xn]   =>   ldr R0, [gp, xn]
6912
6913
       Where R is x for lp64 mode, and w for ILP32 mode.  */
6914
0
    insn = bfd_getl32 (contents + rel->r_offset);
6915
0
    bfd_putl32 (ldr_R0_mask (insn), contents + rel->r_offset);
6916
0
    return bfd_reloc_ok;
6917
0
  }
6918
6919
0
    case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
6920
      /* GD->LE relaxation:
6921
   movk xd, #:tlsdesc_off_g0_nc:var => movk R0, #:tprel_g1_nc:var, lsl #16
6922
   GD->IE relaxation:
6923
   movk xd, #:tlsdesc_off_g0_nc:var => movk Rd, #:gottprel_g0_nc:var
6924
6925
   Where R is x for lp64 mode, and w for ILP32 mode.  */
6926
0
      if (local_exec)
6927
0
  bfd_putl32 (ldr_hw_R0, contents + rel->r_offset);
6928
0
      return bfd_reloc_continue;
6929
6930
0
    case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
6931
0
      if (local_exec)
6932
0
  {
6933
    /* GD->LE relaxation:
6934
       movz xd, #:tlsdesc_off_g1:var => movz R0, #:tprel_g2:var, lsl #32
6935
6936
       Where R is x for lp64 mode, and w for ILP32 mode.  */
6937
0
    bfd_putl32 (movz_hw_R0, contents + rel->r_offset);
6938
0
    return bfd_reloc_continue;
6939
0
  }
6940
0
      else
6941
0
  {
6942
    /*  GD->IE relaxation:
6943
        movz xd, #:tlsdesc_off_g1:var => movz Rd, #:gottprel_g1:var, lsl #16
6944
6945
       Where R is x for lp64 mode, and w for ILP32 mode.  */
6946
0
    insn = bfd_getl32 (contents + rel->r_offset);
6947
0
    bfd_putl32 (movz_R0 | (insn & 0x1f), contents + rel->r_offset);
6948
0
    return bfd_reloc_continue;
6949
0
  }
6950
6951
0
    case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6952
      /* IE->LE relaxation:
6953
   adrp xd, :gottprel:var   =>   movz Rd, :tprel_g1:var
6954
6955
   Where R is x for lp64 mode, and w for ILP32 mode.  */
6956
0
      if (local_exec)
6957
0
  {
6958
0
    insn = bfd_getl32 (contents + rel->r_offset);
6959
0
    bfd_putl32 (movz_R0 | (insn & 0x1f), contents + rel->r_offset);
6960
    /* We have relaxed the adrp into a mov, we may have to clear any
6961
       pending erratum fixes.  */
6962
0
    clear_erratum_843419_entry (globals, rel->r_offset, input_section);
6963
0
  }
6964
0
      return bfd_reloc_continue;
6965
6966
0
    case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
6967
      /* IE->LE relaxation:
6968
   ldr  xd, [xm, #:gottprel_lo12:var]   =>   movk Rd, :tprel_g0_nc:var
6969
6970
   Where R is x for lp64 mode, and w for ILP32 mode.  */
6971
0
      if (local_exec)
6972
0
  {
6973
0
    insn = bfd_getl32 (contents + rel->r_offset);
6974
0
    bfd_putl32 (movk_R0 | (insn & 0x1f), contents + rel->r_offset);
6975
0
  }
6976
0
      return bfd_reloc_continue;
6977
6978
0
    case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
6979
      /* LD->LE relaxation (tiny):
6980
   adr  x0, :tlsldm:x  => mrs x0, tpidr_el0
6981
   bl   __tls_get_addr => add R0, R0, TCB_SIZE
6982
6983
   Where R is x for lp64 mode, and w for ilp32 mode.  */
6984
0
      if (local_exec)
6985
0
  {
6986
0
    BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
6987
0
    BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
6988
    /* No need of CALL26 relocation for tls_get_addr.  */
6989
0
    rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_AARCH64_NONE);
6990
0
    bfd_putl32 (0xd53bd040, contents + rel->r_offset + 0);
6991
0
    bfd_putl32 (add_R0_R0 | (TCB_SIZE << 10),
6992
0
          contents + rel->r_offset + 4);
6993
0
    return bfd_reloc_ok;
6994
0
  }
6995
0
      return bfd_reloc_continue;
6996
6997
0
    case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
6998
      /* LD->LE relaxation (small):
6999
   adrp  x0, :tlsldm:x       => mrs x0, tpidr_el0
7000
       */
7001
0
      if (local_exec)
7002
0
  {
7003
0
    bfd_putl32 (0xd53bd040, contents + rel->r_offset);
7004
0
    return bfd_reloc_ok;
7005
0
  }
7006
0
      return bfd_reloc_continue;
7007
7008
0
    case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
7009
      /* LD->LE relaxation (small):
7010
   add   x0, #:tlsldm_lo12:x => add R0, R0, TCB_SIZE
7011
   bl   __tls_get_addr       => nop
7012
7013
   Where R is x for lp64 mode, and w for ilp32 mode.  */
7014
0
      if (local_exec)
7015
0
  {
7016
0
    BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
7017
0
    BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
7018
    /* No need of CALL26 relocation for tls_get_addr.  */
7019
0
    rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_AARCH64_NONE);
7020
0
    bfd_putl32 (add_R0_R0 | (TCB_SIZE << 10),
7021
0
          contents + rel->r_offset + 0);
7022
0
    bfd_putl32 (INSN_NOP, contents + rel->r_offset + 4);
7023
0
    return bfd_reloc_ok;
7024
0
  }
7025
0
      return bfd_reloc_continue;
7026
7027
0
    default:
7028
0
      return bfd_reloc_continue;
7029
0
    }
7030
7031
0
  return bfd_reloc_ok;
7032
0
}
7033
7034
/* Relocate an AArch64 ELF section.  */
7035
7036
static int
7037
elf64_aarch64_relocate_section (bfd *output_bfd,
7038
        struct bfd_link_info *info,
7039
        bfd *input_bfd,
7040
        asection *input_section,
7041
        bfd_byte *contents,
7042
        Elf_Internal_Rela *relocs,
7043
        Elf_Internal_Sym *local_syms,
7044
        asection **local_sections)
7045
0
{
7046
0
  Elf_Internal_Shdr *symtab_hdr;
7047
0
  struct elf_link_hash_entry **sym_hashes;
7048
0
  Elf_Internal_Rela *rel;
7049
0
  Elf_Internal_Rela *relend;
7050
0
  const char *name;
7051
0
  struct elf_aarch64_link_hash_table *globals;
7052
0
  bool save_addend = false;
7053
0
  bfd_vma addend = 0;
7054
7055
0
  globals = elf_aarch64_hash_table (info);
7056
7057
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
7058
0
  sym_hashes = elf_sym_hashes (input_bfd);
7059
7060
0
  rel = relocs;
7061
0
  relend = relocs + input_section->reloc_count;
7062
0
  for (; rel < relend; rel++)
7063
0
    {
7064
0
      unsigned int r_type;
7065
0
      bfd_reloc_code_real_type bfd_r_type;
7066
0
      bfd_reloc_code_real_type relaxed_bfd_r_type;
7067
0
      reloc_howto_type *howto;
7068
0
      unsigned long r_symndx;
7069
0
      Elf_Internal_Sym *sym;
7070
0
      asection *sec;
7071
0
      struct elf_link_hash_entry *h;
7072
0
      bfd_vma relocation;
7073
0
      bfd_reloc_status_type r;
7074
0
      arelent bfd_reloc;
7075
0
      char sym_type;
7076
0
      bool unresolved_reloc = false;
7077
0
      char *error_message = NULL;
7078
7079
0
      r_symndx = ELF64_R_SYM (rel->r_info);
7080
0
      r_type = ELF64_R_TYPE (rel->r_info);
7081
7082
0
      bfd_reloc.howto = elf64_aarch64_howto_from_type (input_bfd, r_type);
7083
0
      howto = bfd_reloc.howto;
7084
7085
0
      if (howto == NULL)
7086
0
  return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
7087
7088
0
      bfd_r_type = elf64_aarch64_bfd_reloc_from_howto (howto);
7089
7090
0
      h = NULL;
7091
0
      sym = NULL;
7092
0
      sec = NULL;
7093
7094
0
      if (r_symndx < symtab_hdr->sh_info)
7095
0
  {
7096
0
    sym = local_syms + r_symndx;
7097
0
    sym_type = ELF64_ST_TYPE (sym->st_info);
7098
0
    sec = local_sections[r_symndx];
7099
0
    BFD_ASSERT (sec->output_section != NULL);
7100
7101
    /* An object file might have a reference to a local
7102
       undefined symbol.  This is a daft object file, but we
7103
       should at least do something about it.  NONE and NULL
7104
       relocations do not use the symbol and are explicitly
7105
       allowed to use an undefined one, so allow those.
7106
       Likewise for relocations against STN_UNDEF.  */
7107
0
    if (r_type != R_AARCH64_NONE && r_type != R_AARCH64_NULL
7108
0
        && r_symndx != STN_UNDEF
7109
0
        && bfd_is_und_section (sec)
7110
0
        && ELF_ST_BIND (sym->st_info) != STB_WEAK)
7111
0
      (*info->callbacks->undefined_symbol)
7112
0
        (info, bfd_elf_string_from_elf_section
7113
0
         (input_bfd, symtab_hdr->sh_link, sym->st_name),
7114
0
         input_bfd, input_section, rel->r_offset, true);
7115
7116
0
    relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
7117
7118
    /* Relocate against local STT_GNU_IFUNC symbol.  */
7119
0
    if (!bfd_link_relocatable (info)
7120
0
        && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
7121
0
      {
7122
0
        h = elf64_aarch64_get_local_sym_hash (globals, input_bfd,
7123
0
                rel, false);
7124
0
        if (h == NULL)
7125
0
    abort ();
7126
7127
        /* Set STT_GNU_IFUNC symbol value.  */
7128
0
        h->root.u.def.value = sym->st_value;
7129
0
        h->root.u.def.section = sec;
7130
0
      }
7131
0
  }
7132
0
      else
7133
0
  {
7134
0
    bool warned, ignored;
7135
7136
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
7137
0
           r_symndx, symtab_hdr, sym_hashes,
7138
0
           h, sec, relocation,
7139
0
           unresolved_reloc, warned, ignored);
7140
7141
0
    sym_type = h->type;
7142
0
  }
7143
7144
0
      if (sec != NULL && discarded_section (sec))
7145
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
7146
0
           rel, 1, relend, R_AARCH64_NONE,
7147
0
           howto, 0, contents);
7148
7149
0
      if (bfd_link_relocatable (info))
7150
0
  continue;
7151
7152
0
      if (h != NULL)
7153
0
  name = h->root.root.string;
7154
0
      else
7155
0
  {
7156
0
    name = (bfd_elf_string_from_elf_section
7157
0
      (input_bfd, symtab_hdr->sh_link, sym->st_name));
7158
0
    if (name == NULL || *name == '\0')
7159
0
      name = bfd_section_name (sec);
7160
0
  }
7161
7162
0
      if (r_symndx != 0
7163
0
    && r_type != R_AARCH64_NONE
7164
0
    && r_type != R_AARCH64_NULL
7165
0
    && (h == NULL
7166
0
        || h->root.type == bfd_link_hash_defined
7167
0
        || h->root.type == bfd_link_hash_defweak)
7168
0
    && IS_AARCH64_TLS_RELOC (bfd_r_type) != (sym_type == STT_TLS))
7169
0
  {
7170
0
    _bfd_error_handler
7171
0
      ((sym_type == STT_TLS
7172
        /* xgettext:c-format */
7173
0
        ? _("%pB(%pA+%#" PRIx64 "): %s used with TLS symbol %s")
7174
        /* xgettext:c-format */
7175
0
        : _("%pB(%pA+%#" PRIx64 "): %s used with non-TLS symbol %s")),
7176
0
       input_bfd,
7177
0
       input_section, (uint64_t) rel->r_offset, howto->name, name);
7178
0
  }
7179
7180
      /* We relax only if we can see that there can be a valid transition
7181
   from a reloc type to another.
7182
   We call elf64_aarch64_final_link_relocate unless we're completely
7183
   done, i.e., the relaxation produced the final output we want.  */
7184
7185
0
      relaxed_bfd_r_type = aarch64_tls_transition (input_bfd, info, r_type,
7186
0
               h, r_symndx);
7187
0
      if (relaxed_bfd_r_type != bfd_r_type)
7188
0
  {
7189
0
    bfd_r_type = relaxed_bfd_r_type;
7190
0
    howto = elf64_aarch64_howto_from_bfd_reloc (bfd_r_type);
7191
0
    BFD_ASSERT (howto != NULL);
7192
0
    r_type = howto->type;
7193
0
    r = elf64_aarch64_tls_relax (globals, input_bfd, input_section,
7194
0
               contents, rel, h, info);
7195
0
    unresolved_reloc = 0;
7196
0
  }
7197
0
      else
7198
0
  r = bfd_reloc_continue;
7199
7200
      /* There may be multiple consecutive relocations for the
7201
   same offset.  In that case we are supposed to treat the
7202
   output of each relocation as the addend for the next.  */
7203
0
      if (rel + 1 < relend
7204
0
    && rel->r_offset == rel[1].r_offset
7205
0
    && ELF64_R_TYPE (rel[1].r_info) != R_AARCH64_NONE
7206
0
    && ELF64_R_TYPE (rel[1].r_info) != R_AARCH64_NULL)
7207
0
  save_addend = true;
7208
0
      else
7209
0
  save_addend = false;
7210
7211
0
      if (r == bfd_reloc_continue)
7212
0
  r = elf64_aarch64_final_link_relocate (howto, input_bfd, output_bfd,
7213
0
                 input_section, contents, rel,
7214
0
                 relocation, info, sec,
7215
0
                 h, &unresolved_reloc,
7216
0
                 save_addend, &addend, sym);
7217
7218
0
      switch (elf64_aarch64_bfd_reloc_from_type (input_bfd, r_type))
7219
0
  {
7220
0
  case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
7221
0
  case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
7222
0
  case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
7223
0
  case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
7224
0
  case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
7225
0
  case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
7226
0
  case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
7227
0
  case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
7228
0
    if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
7229
0
      {
7230
0
        bool need_relocs = false;
7231
0
        bfd_byte *loc;
7232
0
        int indx;
7233
0
        bfd_vma off;
7234
7235
0
        off = symbol_got_offset (input_bfd, h, r_symndx);
7236
0
        indx = h && h->dynindx != -1 ? h->dynindx : 0;
7237
7238
0
        need_relocs =
7239
0
    (!bfd_link_executable (info) || indx != 0) &&
7240
0
    (h == NULL
7241
0
     || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7242
0
     || h->root.type != bfd_link_hash_undefweak);
7243
7244
0
        BFD_ASSERT (globals->root.srelgot != NULL);
7245
7246
0
        if (need_relocs)
7247
0
    {
7248
0
      Elf_Internal_Rela rela;
7249
0
      rela.r_info = ELF64_R_INFO (indx, AARCH64_R (TLS_DTPMOD));
7250
0
      rela.r_addend = 0;
7251
0
      rela.r_offset = globals->root.sgot->output_section->vma +
7252
0
        globals->root.sgot->output_offset + off;
7253
7254
7255
0
      loc = globals->root.srelgot->contents;
7256
0
      loc += globals->root.srelgot->reloc_count++
7257
0
        * RELOC_SIZE (htab);
7258
0
      bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
7259
7260
0
      bfd_reloc_code_real_type real_type =
7261
0
        elf64_aarch64_bfd_reloc_from_type (input_bfd, r_type);
7262
7263
0
      if (real_type == BFD_RELOC_AARCH64_TLSLD_ADR_PREL21
7264
0
          || real_type == BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21
7265
0
          || real_type == BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC)
7266
0
        {
7267
          /* For local dynamic, don't generate DTPREL in any case.
7268
       Initialize the DTPREL slot into zero, so we get module
7269
       base address when invoke runtime TLS resolver.  */
7270
0
          bfd_put_64 (output_bfd, 0,
7271
0
          globals->root.sgot->contents + off
7272
0
          + GOT_ENTRY_SIZE);
7273
0
        }
7274
0
      else if (indx == 0)
7275
0
        {
7276
0
          bfd_put_64 (output_bfd,
7277
0
          relocation - dtpoff_base (info),
7278
0
          globals->root.sgot->contents + off
7279
0
          + GOT_ENTRY_SIZE);
7280
0
        }
7281
0
      else
7282
0
        {
7283
          /* This TLS symbol is global. We emit a
7284
       relocation to fixup the tls offset at load
7285
       time.  */
7286
0
          rela.r_info =
7287
0
      ELF64_R_INFO (indx, AARCH64_R (TLS_DTPREL));
7288
0
          rela.r_addend = 0;
7289
0
          rela.r_offset =
7290
0
      (globals->root.sgot->output_section->vma
7291
0
       + globals->root.sgot->output_offset + off
7292
0
       + GOT_ENTRY_SIZE);
7293
7294
0
          loc = globals->root.srelgot->contents;
7295
0
          loc += globals->root.srelgot->reloc_count++
7296
0
      * RELOC_SIZE (globals);
7297
0
          bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
7298
0
          bfd_put_64 (output_bfd, (bfd_vma) 0,
7299
0
          globals->root.sgot->contents + off
7300
0
          + GOT_ENTRY_SIZE);
7301
0
        }
7302
0
    }
7303
0
        else
7304
0
    {
7305
0
      bfd_put_64 (output_bfd, (bfd_vma) 1,
7306
0
            globals->root.sgot->contents + off);
7307
0
      bfd_put_64 (output_bfd,
7308
0
            relocation - dtpoff_base (info),
7309
0
            globals->root.sgot->contents + off
7310
0
            + GOT_ENTRY_SIZE);
7311
0
    }
7312
7313
0
        symbol_got_offset_mark (input_bfd, h, r_symndx);
7314
0
      }
7315
0
    break;
7316
7317
0
  case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
7318
0
  case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
7319
0
  case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
7320
0
  case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
7321
0
  case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
7322
0
    if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
7323
0
      {
7324
0
        bool need_relocs = false;
7325
0
        bfd_byte *loc;
7326
0
        int indx;
7327
0
        bfd_vma off;
7328
7329
0
        off = symbol_got_offset (input_bfd, h, r_symndx);
7330
7331
0
        indx = h && h->dynindx != -1 ? h->dynindx : 0;
7332
7333
0
        need_relocs =
7334
0
    (!bfd_link_executable (info) || indx != 0) &&
7335
0
    (h == NULL
7336
0
     || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7337
0
     || h->root.type != bfd_link_hash_undefweak);
7338
7339
0
        BFD_ASSERT (globals->root.srelgot != NULL);
7340
7341
0
        if (need_relocs)
7342
0
    {
7343
0
      Elf_Internal_Rela rela;
7344
7345
0
      if (indx == 0)
7346
0
        rela.r_addend = relocation - dtpoff_base (info);
7347
0
      else
7348
0
        rela.r_addend = 0;
7349
7350
0
      rela.r_info = ELF64_R_INFO (indx, AARCH64_R (TLS_TPREL));
7351
0
      rela.r_offset = globals->root.sgot->output_section->vma +
7352
0
        globals->root.sgot->output_offset + off;
7353
7354
0
      loc = globals->root.srelgot->contents;
7355
0
      loc += globals->root.srelgot->reloc_count++
7356
0
        * RELOC_SIZE (htab);
7357
7358
0
      bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
7359
7360
0
      bfd_put_64 (output_bfd, rela.r_addend,
7361
0
            globals->root.sgot->contents + off);
7362
0
    }
7363
0
        else
7364
0
    bfd_put_64 (output_bfd, relocation - tpoff_base (info),
7365
0
          globals->root.sgot->contents + off);
7366
7367
0
        symbol_got_offset_mark (input_bfd, h, r_symndx);
7368
0
      }
7369
0
    break;
7370
7371
0
  case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
7372
0
  case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
7373
0
  case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
7374
0
  case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
7375
0
  case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
7376
0
  case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
7377
0
  case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
7378
0
    if (! symbol_tlsdesc_got_offset_mark_p (input_bfd, h, r_symndx))
7379
0
      {
7380
0
        bool need_relocs = false;
7381
0
        int indx = h && h->dynindx != -1 ? h->dynindx : 0;
7382
0
        bfd_vma off = symbol_tlsdesc_got_offset (input_bfd, h, r_symndx);
7383
7384
0
        need_relocs = (h == NULL
7385
0
           || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7386
0
           || h->root.type != bfd_link_hash_undefweak);
7387
7388
0
        BFD_ASSERT (globals->root.srelgot != NULL);
7389
0
        BFD_ASSERT (globals->root.sgot != NULL);
7390
7391
0
        if (need_relocs)
7392
0
    {
7393
0
      bfd_byte *loc;
7394
0
      Elf_Internal_Rela rela;
7395
0
      rela.r_info = ELF64_R_INFO (indx, AARCH64_R (TLSDESC));
7396
7397
0
      rela.r_addend = 0;
7398
0
      rela.r_offset = (globals->root.sgotplt->output_section->vma
7399
0
           + globals->root.sgotplt->output_offset
7400
0
           + off + globals->sgotplt_jump_table_size);
7401
7402
0
      if (indx == 0)
7403
0
        rela.r_addend = relocation - dtpoff_base (info);
7404
7405
      /* Allocate the next available slot in the PLT reloc
7406
         section to hold our R_AARCH64_TLSDESC, the next
7407
         available slot is determined from reloc_count,
7408
         which we step. But note, reloc_count was
7409
         artifically moved down while allocating slots for
7410
         real PLT relocs such that all of the PLT relocs
7411
         will fit above the initial reloc_count and the
7412
         extra stuff will fit below.  */
7413
0
      loc = globals->root.srelplt->contents;
7414
0
      loc += globals->root.srelplt->reloc_count++
7415
0
        * RELOC_SIZE (globals);
7416
7417
0
      bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
7418
7419
0
      bfd_put_64 (output_bfd, (bfd_vma) 0,
7420
0
            globals->root.sgotplt->contents + off +
7421
0
            globals->sgotplt_jump_table_size);
7422
0
      bfd_put_64 (output_bfd, (bfd_vma) 0,
7423
0
            globals->root.sgotplt->contents + off +
7424
0
            globals->sgotplt_jump_table_size +
7425
0
            GOT_ENTRY_SIZE);
7426
0
    }
7427
7428
0
        symbol_tlsdesc_got_offset_mark (input_bfd, h, r_symndx);
7429
0
      }
7430
0
    break;
7431
0
  default:
7432
0
    break;
7433
0
  }
7434
7435
      /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
7436
   because such sections are not SEC_ALLOC and thus ld.so will
7437
   not process them.  */
7438
0
      if (unresolved_reloc
7439
0
    && !((input_section->flags & SEC_DEBUGGING) != 0
7440
0
         && h->def_dynamic)
7441
0
    && _bfd_elf_section_offset (output_bfd, info, input_section,
7442
0
              +rel->r_offset) != (bfd_vma) - 1)
7443
0
  {
7444
0
    _bfd_error_handler
7445
      /* xgettext:c-format */
7446
0
      (_("%pB(%pA+%#" PRIx64 "): "
7447
0
         "unresolvable %s relocation against symbol `%s'"),
7448
0
       input_bfd, input_section, (uint64_t) rel->r_offset, howto->name,
7449
0
       h->root.root.string);
7450
0
    return false;
7451
0
  }
7452
7453
0
      if (r != bfd_reloc_ok && r != bfd_reloc_continue)
7454
0
  {
7455
0
    bfd_reloc_code_real_type real_r_type
7456
0
      = elf64_aarch64_bfd_reloc_from_type (input_bfd, r_type);
7457
7458
0
    switch (r)
7459
0
      {
7460
0
      case bfd_reloc_overflow:
7461
0
        (*info->callbacks->reloc_overflow)
7462
0
    (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
7463
0
     input_bfd, input_section, rel->r_offset);
7464
0
        if (real_r_type == BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15
7465
0
      || real_r_type == BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14)
7466
0
    {
7467
0
      (*info->callbacks->warning)
7468
0
        (info,
7469
0
         _("too many GOT entries for -fpic, "
7470
0
           "please recompile with -fPIC"),
7471
0
         name, input_bfd, input_section, rel->r_offset);
7472
0
      return false;
7473
0
    }
7474
        /* Overflow can occur when a variable is referenced with a type
7475
     that has a larger alignment than the type with which it was
7476
     declared. eg:
7477
       file1.c: extern int foo; int a (void) { return foo; }
7478
       file2.c: char bar, foo, baz;
7479
     If the variable is placed into a data section at an offset
7480
     that is incompatible with the larger alignment requirement
7481
     overflow will occur.  (Strictly speaking this is not overflow
7482
     but rather an alignment problem, but the bfd_reloc_ error
7483
     enum does not have a value to cover that situation).
7484
7485
     Try to catch this situation here and provide a more helpful
7486
     error message to the user.  */
7487
0
        if (addend & (((bfd_vma) 1 << howto->rightshift) - 1)
7488
      /* FIXME: Are we testing all of the appropriate reloc
7489
         types here ?  */
7490
0
      && (real_r_type == BFD_RELOC_AARCH64_LD_LO19_PCREL
7491
0
          || real_r_type == BFD_RELOC_AARCH64_LDST16_LO12
7492
0
          || real_r_type == BFD_RELOC_AARCH64_LDST32_LO12
7493
0
          || real_r_type == BFD_RELOC_AARCH64_LDST64_LO12
7494
0
          || real_r_type == BFD_RELOC_AARCH64_LDST128_LO12))
7495
0
    {
7496
0
      info->callbacks->warning
7497
0
        (info, _("one possible cause of this error is that the \
7498
0
symbol is being referenced in the indicated code as if it had a larger \
7499
0
alignment than was declared where it was defined"),
7500
0
         name, input_bfd, input_section, rel->r_offset);
7501
0
    }
7502
0
        break;
7503
7504
0
      case bfd_reloc_undefined:
7505
0
        (*info->callbacks->undefined_symbol)
7506
0
    (info, name, input_bfd, input_section, rel->r_offset, true);
7507
0
        break;
7508
7509
0
      case bfd_reloc_outofrange:
7510
0
        error_message = _("out of range");
7511
0
        goto common_error;
7512
7513
0
      case bfd_reloc_notsupported:
7514
0
        error_message = _("unsupported relocation");
7515
0
        goto common_error;
7516
7517
0
      case bfd_reloc_dangerous:
7518
        /* error_message should already be set.  */
7519
0
        goto common_error;
7520
7521
0
      default:
7522
0
        error_message = _("unknown error");
7523
        /* Fall through.  */
7524
7525
0
      common_error:
7526
0
        BFD_ASSERT (error_message != NULL);
7527
0
        (*info->callbacks->reloc_dangerous)
7528
0
    (info, error_message, input_bfd, input_section, rel->r_offset);
7529
0
        break;
7530
0
      }
7531
0
  }
7532
7533
0
      if (!save_addend)
7534
0
  addend = 0;
7535
0
    }
7536
7537
0
  return true;
7538
0
}
7539
7540
/* Set the right machine number.  */
7541
7542
static bool
7543
elf64_aarch64_object_p (bfd *abfd)
7544
459
{
7545
#if ARCH_SIZE == 32
7546
  bfd_default_set_arch_mach (abfd, bfd_arch_aarch64, bfd_mach_aarch64_ilp32);
7547
#else
7548
459
  bfd_default_set_arch_mach (abfd, bfd_arch_aarch64, bfd_mach_aarch64);
7549
459
#endif
7550
459
  return true;
7551
459
}
7552
7553
/* Function to keep AArch64 specific flags in the ELF header.  */
7554
7555
static bool
7556
elf64_aarch64_set_private_flags (bfd *abfd, flagword flags)
7557
0
{
7558
0
  if (elf_flags_init (abfd) && elf_elfheader (abfd)->e_flags != flags)
7559
0
    {
7560
0
    }
7561
0
  else
7562
0
    {
7563
0
      elf_elfheader (abfd)->e_flags = flags;
7564
0
      elf_flags_init (abfd) = true;
7565
0
    }
7566
7567
0
  return true;
7568
0
}
7569
7570
/* Merge backend specific data from an object file to the output
7571
   object file when linking.  */
7572
7573
static bool
7574
elf64_aarch64_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
7575
0
{
7576
0
  bfd *obfd = info->output_bfd;
7577
0
  flagword out_flags;
7578
0
  flagword in_flags;
7579
0
  bool flags_compatible = true;
7580
0
  asection *sec;
7581
7582
  /* Check if we have the same endianess.  */
7583
0
  if (!_bfd_generic_verify_endian_match (ibfd, info))
7584
0
    return false;
7585
7586
0
  if (!is_aarch64_elf (ibfd))
7587
0
    return true;
7588
7589
  /* The input BFD must have had its flags initialised.  */
7590
  /* The following seems bogus to me -- The flags are initialized in
7591
     the assembler but I don't think an elf_flags_init field is
7592
     written into the object.  */
7593
  /* BFD_ASSERT (elf_flags_init (ibfd)); */
7594
7595
0
  in_flags = elf_elfheader (ibfd)->e_flags;
7596
0
  out_flags = elf_elfheader (obfd)->e_flags;
7597
7598
0
  if (!elf_flags_init (obfd))
7599
0
    {
7600
      /* If the input is the default architecture and had the default
7601
   flags then do not bother setting the flags for the output
7602
   architecture, instead allow future merges to do this.  If no
7603
   future merges ever set these flags then they will retain their
7604
   uninitialised values, which surprise surprise, correspond
7605
   to the default values.  */
7606
0
      if (bfd_get_arch_info (ibfd)->the_default
7607
0
    && elf_elfheader (ibfd)->e_flags == 0)
7608
0
  return true;
7609
7610
0
      elf_flags_init (obfd) = true;
7611
0
      elf_elfheader (obfd)->e_flags = in_flags;
7612
7613
0
      if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
7614
0
    && bfd_get_arch_info (obfd)->the_default)
7615
0
  return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
7616
0
          bfd_get_mach (ibfd));
7617
7618
0
      return true;
7619
0
    }
7620
7621
  /* Identical flags must be compatible.  */
7622
0
  if (in_flags == out_flags)
7623
0
    return true;
7624
7625
  /* Check to see if the input BFD actually contains any sections.  If
7626
     not, its flags may not have been initialised either, but it
7627
     cannot actually cause any incompatiblity.  Do not short-circuit
7628
     dynamic objects; their section list may be emptied by
7629
     elf_link_add_object_symbols.
7630
7631
     Also check to see if there are no code sections in the input.
7632
     In this case there is no need to check for code specific flags.
7633
     XXX - do we need to worry about floating-point format compatability
7634
     in data sections ?  */
7635
0
  if (!(ibfd->flags & DYNAMIC))
7636
0
    {
7637
0
      bool null_input_bfd = true;
7638
0
      bool only_data_sections = true;
7639
7640
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7641
0
  {
7642
0
    if ((bfd_section_flags (sec)
7643
0
         & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
7644
0
        == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
7645
0
      only_data_sections = false;
7646
7647
0
    null_input_bfd = false;
7648
0
    break;
7649
0
  }
7650
7651
0
      if (null_input_bfd || only_data_sections)
7652
0
  return true;
7653
0
    }
7654
7655
0
  return flags_compatible;
7656
0
}
7657
7658
/* Display the flags field.  */
7659
7660
static bool
7661
elf64_aarch64_print_private_bfd_data (bfd *abfd, void *ptr)
7662
6
{
7663
6
  FILE *file = (FILE *) ptr;
7664
6
  unsigned long flags;
7665
7666
6
  BFD_ASSERT (abfd != NULL && ptr != NULL);
7667
7668
  /* Print normal ELF private data.  */
7669
6
  _bfd_elf_print_private_bfd_data (abfd, ptr);
7670
7671
6
  flags = elf_elfheader (abfd)->e_flags;
7672
  /* Ignore init flag - it may not be set, despite the flags field
7673
     containing valid data.  */
7674
7675
  /* xgettext:c-format */
7676
6
  fprintf (file, _("private flags = 0x%lx:"), elf_elfheader (abfd)->e_flags);
7677
7678
6
  if (flags)
7679
4
    fprintf (file, _(" <Unrecognised flag bits set>"));
7680
7681
6
  fputc ('\n', file);
7682
7683
6
  return true;
7684
6
}
7685
7686
/* Return true if we need copy relocation against EH.  */
7687
7688
static bool
7689
need_copy_relocation_p (struct elf_aarch64_link_hash_entry *eh)
7690
0
{
7691
0
  struct elf_dyn_relocs *p;
7692
0
  asection *s;
7693
7694
0
  for (p = eh->root.dyn_relocs; p != NULL; p = p->next)
7695
0
    {
7696
      /* If there is any pc-relative reference, we need to keep copy relocation
7697
   to avoid propagating the relocation into runtime that current glibc
7698
   does not support.  */
7699
0
      if (p->pc_count)
7700
0
  return true;
7701
7702
0
      s = p->sec->output_section;
7703
      /* Need copy relocation if it's against read-only section.  */
7704
0
      if (s != NULL && (s->flags & SEC_READONLY) != 0)
7705
0
  return true;
7706
0
    }
7707
7708
0
  return false;
7709
0
}
7710
7711
/* Adjust a symbol defined by a dynamic object and referenced by a
7712
   regular object.  The current definition is in some section of the
7713
   dynamic object, but we're not including those sections.  We have to
7714
   change the definition to something the rest of the link can
7715
   understand.  */
7716
7717
static bool
7718
elf64_aarch64_adjust_dynamic_symbol (struct bfd_link_info *info,
7719
             struct elf_link_hash_entry *h)
7720
0
{
7721
0
  struct elf_aarch64_link_hash_table *htab;
7722
0
  asection *s, *srel;
7723
7724
  /* If this is a function, put it in the procedure linkage table.  We
7725
     will fill in the contents of the procedure linkage table later,
7726
     when we know the address of the .got section.  */
7727
0
  if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
7728
0
    {
7729
0
      if (h->plt.refcount <= 0
7730
0
    || (h->type != STT_GNU_IFUNC
7731
0
        && (SYMBOL_CALLS_LOCAL (info, h)
7732
0
      || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
7733
0
          && h->root.type == bfd_link_hash_undefweak))))
7734
0
  {
7735
    /* This case can occur if we saw a CALL26 reloc in
7736
       an input file, but the symbol wasn't referred to
7737
       by a dynamic object or all references were
7738
       garbage collected. In which case we can end up
7739
       resolving.  */
7740
0
    h->plt.offset = (bfd_vma) - 1;
7741
0
    h->needs_plt = 0;
7742
0
  }
7743
7744
0
      return true;
7745
0
    }
7746
0
  else
7747
    /* Otherwise, reset to -1.  */
7748
0
    h->plt.offset = (bfd_vma) - 1;
7749
7750
7751
  /* If this is a weak symbol, and there is a real definition, the
7752
     processor independent code will have arranged for us to see the
7753
     real definition first, and we can just use the same value.  */
7754
0
  if (h->is_weakalias)
7755
0
    {
7756
0
      struct elf_link_hash_entry *def = weakdef (h);
7757
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7758
0
      h->root.u.def.section = def->root.u.def.section;
7759
0
      h->root.u.def.value = def->root.u.def.value;
7760
0
      if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
7761
0
  h->non_got_ref = def->non_got_ref;
7762
0
      return true;
7763
0
    }
7764
7765
  /* If we are creating a shared library, we must presume that the
7766
     only references to the symbol are via the global offset table.
7767
     For such cases we need not do anything here; the relocations will
7768
     be handled correctly by relocate_section.  */
7769
0
  if (bfd_link_pic (info))
7770
0
    return true;
7771
7772
  /* If there are no references to this symbol that do not use the
7773
     GOT, we don't need to generate a copy reloc.  */
7774
0
  if (!h->non_got_ref)
7775
0
    return true;
7776
7777
  /* If -z nocopyreloc was given, we won't generate them either.  */
7778
0
  if (info->nocopyreloc)
7779
0
    {
7780
0
      h->non_got_ref = 0;
7781
0
      return true;
7782
0
    }
7783
7784
0
  if (ELIMINATE_COPY_RELOCS)
7785
0
    {
7786
0
      struct elf_aarch64_link_hash_entry *eh;
7787
      /* If we don't find any dynamic relocs in read-only sections, then
7788
   we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
7789
0
      eh = (struct elf_aarch64_link_hash_entry *) h;
7790
0
      if (!need_copy_relocation_p (eh))
7791
0
  {
7792
0
    h->non_got_ref = 0;
7793
0
    return true;
7794
0
  }
7795
0
    }
7796
7797
  /* We must allocate the symbol in our .dynbss section, which will
7798
     become part of the .bss section of the executable.  There will be
7799
     an entry for this symbol in the .dynsym section.  The dynamic
7800
     object will contain position independent code, so all references
7801
     from the dynamic object to this symbol will go through the global
7802
     offset table.  The dynamic linker will use the .dynsym entry to
7803
     determine the address it must put in the global offset table, so
7804
     both the dynamic object and the regular object will refer to the
7805
     same memory location for the variable.  */
7806
7807
0
  htab = elf_aarch64_hash_table (info);
7808
7809
  /* We must generate a R_AARCH64_COPY reloc to tell the dynamic linker
7810
     to copy the initial value out of the dynamic object and into the
7811
     runtime process image.  */
7812
0
  if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7813
0
    {
7814
0
      s = htab->root.sdynrelro;
7815
0
      srel = htab->root.sreldynrelro;
7816
0
    }
7817
0
  else
7818
0
    {
7819
0
      s = htab->root.sdynbss;
7820
0
      srel = htab->root.srelbss;
7821
0
    }
7822
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7823
0
    {
7824
0
      srel->size += RELOC_SIZE (htab);
7825
0
      h->needs_copy = 1;
7826
0
    }
7827
7828
0
  return _bfd_elf_adjust_dynamic_copy (info, h, s);
7829
7830
0
}
7831
7832
static bool
7833
elf64_aarch64_allocate_local_symbols (bfd *abfd, unsigned number)
7834
0
{
7835
0
  struct elf_aarch64_local_symbol *locals;
7836
0
  locals = elf_aarch64_locals (abfd);
7837
0
  if (locals == NULL)
7838
0
    {
7839
0
      locals = (struct elf_aarch64_local_symbol *)
7840
0
  bfd_zalloc (abfd, number * sizeof (struct elf_aarch64_local_symbol));
7841
0
      if (locals == NULL)
7842
0
  return false;
7843
0
      elf_aarch64_locals (abfd) = locals;
7844
0
    }
7845
0
  return true;
7846
0
}
7847
7848
/* Create the .got section to hold the global offset table.  */
7849
7850
static bool
7851
aarch64_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
7852
0
{
7853
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
7854
0
  flagword flags;
7855
0
  asection *s;
7856
0
  struct elf_link_hash_entry *h;
7857
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
7858
7859
  /* This function may be called more than once.  */
7860
0
  if (htab->sgot != NULL)
7861
0
    return true;
7862
7863
0
  flags = bed->dynamic_sec_flags;
7864
7865
0
  s = bfd_make_section_anyway_with_flags (abfd,
7866
0
            (bed->rela_plts_and_copies_p
7867
0
             ? ".rela.got" : ".rel.got"),
7868
0
            (bed->dynamic_sec_flags
7869
0
             | SEC_READONLY));
7870
0
  if (s == NULL
7871
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
7872
0
    return false;
7873
0
  htab->srelgot = s;
7874
7875
0
  s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
7876
0
  if (s == NULL
7877
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
7878
0
    return false;
7879
0
  htab->sgot = s;
7880
0
  htab->sgot->size += GOT_ENTRY_SIZE;
7881
7882
0
  if (bed->want_got_sym)
7883
0
    {
7884
      /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
7885
   (or .got.plt) section.  We don't do this in the linker script
7886
   because we don't want to define the symbol if we are not creating
7887
   a global offset table.  */
7888
0
      h = _bfd_elf_define_linkage_sym (abfd, info, s,
7889
0
               "_GLOBAL_OFFSET_TABLE_");
7890
0
      elf_hash_table (info)->hgot = h;
7891
0
      if (h == NULL)
7892
0
  return false;
7893
0
    }
7894
7895
0
  if (bed->want_got_plt)
7896
0
    {
7897
0
      s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
7898
0
      if (s == NULL
7899
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
7900
0
  return false;
7901
0
      htab->sgotplt = s;
7902
0
    }
7903
7904
  /* The first bit of the global offset table is the header.  */
7905
0
  s->size += bed->got_header_size;
7906
7907
0
  return true;
7908
0
}
7909
7910
/* Look through the relocs for a section during the first phase.  */
7911
7912
static bool
7913
elf64_aarch64_check_relocs (bfd *abfd, struct bfd_link_info *info,
7914
          asection *sec, const Elf_Internal_Rela *relocs)
7915
0
{
7916
0
  Elf_Internal_Shdr *symtab_hdr;
7917
0
  struct elf_link_hash_entry **sym_hashes;
7918
0
  const Elf_Internal_Rela *rel;
7919
0
  const Elf_Internal_Rela *rel_end;
7920
0
  asection *sreloc;
7921
7922
0
  struct elf_aarch64_link_hash_table *htab;
7923
7924
0
  if (bfd_link_relocatable (info))
7925
0
    return true;
7926
7927
0
  BFD_ASSERT (is_aarch64_elf (abfd));
7928
7929
0
  htab = elf_aarch64_hash_table (info);
7930
0
  sreloc = NULL;
7931
7932
0
  symtab_hdr = &elf_symtab_hdr (abfd);
7933
0
  sym_hashes = elf_sym_hashes (abfd);
7934
7935
0
  rel_end = relocs + sec->reloc_count;
7936
0
  for (rel = relocs; rel < rel_end; rel++)
7937
0
    {
7938
0
      struct elf_link_hash_entry *h;
7939
0
      unsigned int r_symndx;
7940
0
      unsigned int r_type;
7941
0
      bfd_reloc_code_real_type bfd_r_type;
7942
0
      Elf_Internal_Sym *isym;
7943
7944
0
      r_symndx = ELF64_R_SYM (rel->r_info);
7945
0
      r_type = ELF64_R_TYPE (rel->r_info);
7946
7947
0
      if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
7948
0
  {
7949
    /* xgettext:c-format */
7950
0
    _bfd_error_handler (_("%pB: bad symbol index: %d"), abfd, r_symndx);
7951
0
    return false;
7952
0
  }
7953
7954
0
      if (r_symndx < symtab_hdr->sh_info)
7955
0
  {
7956
    /* A local symbol.  */
7957
0
    isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
7958
0
          abfd, r_symndx);
7959
0
    if (isym == NULL)
7960
0
      return false;
7961
7962
    /* Check relocation against local STT_GNU_IFUNC symbol.  */
7963
0
    if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
7964
0
      {
7965
0
        h = elf64_aarch64_get_local_sym_hash (htab, abfd, rel,
7966
0
                true);
7967
0
        if (h == NULL)
7968
0
    return false;
7969
7970
        /* Fake a STT_GNU_IFUNC symbol.  */
7971
0
        h->type = STT_GNU_IFUNC;
7972
0
        h->def_regular = 1;
7973
0
        h->ref_regular = 1;
7974
0
        h->forced_local = 1;
7975
0
        h->root.type = bfd_link_hash_defined;
7976
0
      }
7977
0
    else
7978
0
      h = NULL;
7979
0
  }
7980
0
      else
7981
0
  {
7982
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7983
0
    while (h->root.type == bfd_link_hash_indirect
7984
0
     || h->root.type == bfd_link_hash_warning)
7985
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
7986
0
  }
7987
7988
      /* Could be done earlier, if h were already available.  */
7989
0
      bfd_r_type = aarch64_tls_transition (abfd, info, r_type, h, r_symndx);
7990
7991
0
      if (h != NULL)
7992
0
  {
7993
    /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
7994
       This shows up in particular in an R_AARCH64_PREL64 in large model
7995
       when calculating the pc-relative address to .got section which is
7996
       used to initialize the gp register.  */
7997
0
    if (h->root.root.string
7998
0
        && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
7999
0
      {
8000
0
        if (htab->root.dynobj == NULL)
8001
0
    htab->root.dynobj = abfd;
8002
8003
0
        if (! aarch64_elf_create_got_section (htab->root.dynobj, info))
8004
0
    return false;
8005
8006
0
        BFD_ASSERT (h == htab->root.hgot);
8007
0
      }
8008
8009
    /* Create the ifunc sections for static executables.  If we
8010
       never see an indirect function symbol nor we are building
8011
       a static executable, those sections will be empty and
8012
       won't appear in output.  */
8013
0
    switch (bfd_r_type)
8014
0
      {
8015
0
      default:
8016
0
        break;
8017
8018
0
      case BFD_RELOC_AARCH64_ADD_LO12:
8019
0
      case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
8020
0
      case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
8021
0
      case BFD_RELOC_AARCH64_CALL26:
8022
0
      case BFD_RELOC_AARCH64_GOT_LD_PREL19:
8023
0
      case BFD_RELOC_AARCH64_JUMP26:
8024
0
      case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
8025
0
      case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
8026
0
      case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
8027
0
      case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
8028
0
      case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
8029
0
      case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
8030
0
      case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
8031
0
      case BFD_RELOC_AARCH64_64:
8032
0
        if (htab->root.dynobj == NULL)
8033
0
    htab->root.dynobj = abfd;
8034
0
        if (!_bfd_elf_create_ifunc_sections (htab->root.dynobj, info))
8035
0
    return false;
8036
0
        break;
8037
0
      }
8038
8039
    /* It is referenced by a non-shared object.  */
8040
0
    h->ref_regular = 1;
8041
0
  }
8042
8043
0
      switch (bfd_r_type)
8044
0
  {
8045
0
  case BFD_RELOC_AARCH64_16:
8046
0
#if ARCH_SIZE == 64
8047
0
  case BFD_RELOC_AARCH64_32:
8048
0
#endif
8049
0
    if (bfd_link_pic (info) && (sec->flags & SEC_ALLOC) != 0)
8050
0
      {
8051
0
        if (h != NULL
8052
      /* This is an absolute symbol.  It represents a value instead
8053
         of an address.  */
8054
0
      && (bfd_is_abs_symbol (&h->root)
8055
          /* This is an undefined symbol.  */
8056
0
          || h->root.type == bfd_link_hash_undefined))
8057
0
    break;
8058
8059
        /* For local symbols, defined global symbols in a non-ABS section,
8060
     it is assumed that the value is an address.  */
8061
0
        int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
8062
0
        _bfd_error_handler
8063
    /* xgettext:c-format */
8064
0
    (_("%pB: relocation %s against `%s' can not be used when making "
8065
0
       "a shared object"),
8066
0
     abfd, elf64_aarch64_howto_table[howto_index].name,
8067
0
     (h) ? h->root.root.string : "a local symbol");
8068
0
        bfd_set_error (bfd_error_bad_value);
8069
0
        return false;
8070
0
      }
8071
0
    else
8072
0
      break;
8073
8074
0
  case BFD_RELOC_AARCH64_MOVW_G0_NC:
8075
0
  case BFD_RELOC_AARCH64_MOVW_G1_NC:
8076
0
  case BFD_RELOC_AARCH64_MOVW_G2_NC:
8077
0
  case BFD_RELOC_AARCH64_MOVW_G3:
8078
0
    if (bfd_link_pic (info))
8079
0
      {
8080
0
        int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
8081
0
        _bfd_error_handler
8082
    /* xgettext:c-format */
8083
0
    (_("%pB: relocation %s against `%s' can not be used when making "
8084
0
       "a shared object; recompile with -fPIC"),
8085
0
     abfd, elf64_aarch64_howto_table[howto_index].name,
8086
0
     (h) ? h->root.root.string : "a local symbol");
8087
0
        bfd_set_error (bfd_error_bad_value);
8088
0
        return false;
8089
0
      }
8090
    /* Fall through.  */
8091
8092
0
  case BFD_RELOC_AARCH64_16_PCREL:
8093
0
  case BFD_RELOC_AARCH64_32_PCREL:
8094
0
  case BFD_RELOC_AARCH64_64_PCREL:
8095
0
  case BFD_RELOC_AARCH64_ADD_LO12:
8096
0
  case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
8097
0
  case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
8098
0
  case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
8099
0
  case BFD_RELOC_AARCH64_LDST128_LO12:
8100
0
  case BFD_RELOC_AARCH64_LDST16_LO12:
8101
0
  case BFD_RELOC_AARCH64_LDST32_LO12:
8102
0
  case BFD_RELOC_AARCH64_LDST64_LO12:
8103
0
  case BFD_RELOC_AARCH64_LDST8_LO12:
8104
0
  case BFD_RELOC_AARCH64_LD_LO19_PCREL:
8105
0
    if (h == NULL || bfd_link_pic (info))
8106
0
      break;
8107
    /* Fall through.  */
8108
8109
0
  case BFD_RELOC_AARCH64_64:
8110
8111
    /* We don't need to handle relocs into sections not going into
8112
       the "real" output.  */
8113
0
    if ((sec->flags & SEC_ALLOC) == 0)
8114
0
      break;
8115
8116
0
    if (h != NULL)
8117
0
      {
8118
0
        if (!bfd_link_pic (info))
8119
0
    h->non_got_ref = 1;
8120
8121
0
        h->plt.refcount += 1;
8122
0
        h->pointer_equality_needed = 1;
8123
0
      }
8124
8125
    /* No need to do anything if we're not creating a shared
8126
       object.  */
8127
0
    if (!(bfd_link_pic (info)
8128
    /* If on the other hand, we are creating an executable, we
8129
       may need to keep relocations for symbols satisfied by a
8130
       dynamic library if we manage to avoid copy relocs for the
8131
       symbol.
8132
8133
       NOTE: Currently, there is no support of copy relocs
8134
       elimination on pc-relative relocation types, because there is
8135
       no dynamic relocation support for them in glibc.  We still
8136
       record the dynamic symbol reference for them.  This is
8137
       because one symbol may be referenced by both absolute
8138
       relocation (for example, BFD_RELOC_AARCH64_64) and
8139
       pc-relative relocation.  We need full symbol reference
8140
       information to make correct decision later in
8141
       elf64_aarch64_adjust_dynamic_symbol.  */
8142
0
    || (ELIMINATE_COPY_RELOCS
8143
0
        && !bfd_link_pic (info)
8144
0
        && h != NULL
8145
0
        && (h->root.type == bfd_link_hash_defweak
8146
0
      || !h->def_regular))))
8147
0
      break;
8148
8149
0
    {
8150
0
      struct elf_dyn_relocs *p;
8151
0
      struct elf_dyn_relocs **head;
8152
0
      int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
8153
8154
      /* We must copy these reloc types into the output file.
8155
         Create a reloc section in dynobj and make room for
8156
         this reloc.  */
8157
0
      if (sreloc == NULL)
8158
0
        {
8159
0
    if (htab->root.dynobj == NULL)
8160
0
      htab->root.dynobj = abfd;
8161
8162
0
    sreloc = _bfd_elf_make_dynamic_reloc_section
8163
0
      (sec, htab->root.dynobj, LOG_FILE_ALIGN, abfd, /*rela? */ true);
8164
8165
0
    if (sreloc == NULL)
8166
0
      return false;
8167
0
        }
8168
8169
      /* If this is a global symbol, we count the number of
8170
         relocations we need for this symbol.  */
8171
0
      if (h != NULL)
8172
0
        {
8173
0
    head = &h->dyn_relocs;
8174
0
        }
8175
0
      else
8176
0
        {
8177
    /* Track dynamic relocs needed for local syms too.
8178
       We really need local syms available to do this
8179
       easily.  Oh well.  */
8180
8181
0
    asection *s;
8182
0
    void **vpp;
8183
8184
0
    isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
8185
0
                abfd, r_symndx);
8186
0
    if (isym == NULL)
8187
0
      return false;
8188
8189
0
    s = bfd_section_from_elf_index (abfd, isym->st_shndx);
8190
0
    if (s == NULL)
8191
0
      s = sec;
8192
8193
    /* Beware of type punned pointers vs strict aliasing
8194
       rules.  */
8195
0
    vpp = &(elf_section_data (s)->local_dynrel);
8196
0
    head = (struct elf_dyn_relocs **) vpp;
8197
0
        }
8198
8199
0
      p = *head;
8200
0
      if (p == NULL || p->sec != sec)
8201
0
        {
8202
0
    size_t amt = sizeof *p;
8203
0
    p = ((struct elf_dyn_relocs *)
8204
0
         bfd_zalloc (htab->root.dynobj, amt));
8205
0
    if (p == NULL)
8206
0
      return false;
8207
0
    p->next = *head;
8208
0
    *head = p;
8209
0
    p->sec = sec;
8210
0
        }
8211
8212
0
      p->count += 1;
8213
8214
0
      if (elf64_aarch64_howto_table[howto_index].pc_relative)
8215
0
        p->pc_count += 1;
8216
0
    }
8217
0
    break;
8218
8219
    /* RR: We probably want to keep a consistency check that
8220
       there are no dangling GOT_PAGE relocs.  */
8221
0
  case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
8222
0
  case BFD_RELOC_AARCH64_GOT_LD_PREL19:
8223
0
  case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
8224
0
  case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
8225
0
  case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
8226
0
  case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
8227
0
  case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
8228
0
  case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
8229
0
  case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
8230
0
  case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
8231
0
  case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
8232
0
  case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
8233
0
  case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
8234
0
  case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12:
8235
0
  case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
8236
0
  case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
8237
0
  case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
8238
0
  case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
8239
0
  case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
8240
0
  case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
8241
0
  case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
8242
0
  case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
8243
0
  case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
8244
0
  case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
8245
0
  case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
8246
0
  case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
8247
0
  case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
8248
0
  case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
8249
0
  case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
8250
0
  case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
8251
0
  case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
8252
0
    {
8253
0
      unsigned got_type;
8254
0
      unsigned old_got_type;
8255
8256
0
      got_type = aarch64_reloc_got_type (bfd_r_type);
8257
8258
0
      if (h)
8259
0
        {
8260
0
    h->got.refcount += 1;
8261
0
    old_got_type = elf_aarch64_hash_entry (h)->got_type;
8262
0
        }
8263
0
      else
8264
0
        {
8265
0
    struct elf_aarch64_local_symbol *locals;
8266
8267
0
    if (!elf64_aarch64_allocate_local_symbols
8268
0
        (abfd, symtab_hdr->sh_info))
8269
0
      return false;
8270
8271
0
    locals = elf_aarch64_locals (abfd);
8272
0
    BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
8273
0
    locals[r_symndx].got_refcount += 1;
8274
0
    old_got_type = locals[r_symndx].got_type;
8275
0
        }
8276
8277
      /* If a variable is accessed with both general dynamic TLS
8278
         methods, two slots may be created.  */
8279
0
      if (GOT_TLS_GD_ANY_P (old_got_type) && GOT_TLS_GD_ANY_P (got_type))
8280
0
        got_type |= old_got_type;
8281
8282
      /* We will already have issued an error message if there
8283
         is a TLS/non-TLS mismatch, based on the symbol type.
8284
         So just combine any TLS types needed.  */
8285
0
      if (old_got_type != GOT_UNKNOWN && old_got_type != GOT_NORMAL
8286
0
    && got_type != GOT_NORMAL)
8287
0
        got_type |= old_got_type;
8288
8289
      /* If the symbol is accessed by both IE and GD methods, we
8290
         are able to relax.  Turn off the GD flag, without
8291
         messing up with any other kind of TLS types that may be
8292
         involved.  */
8293
0
      if ((got_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (got_type))
8294
0
        got_type &= ~ (GOT_TLSDESC_GD | GOT_TLS_GD);
8295
8296
0
      if (old_got_type != got_type)
8297
0
        {
8298
0
    if (h != NULL)
8299
0
      elf_aarch64_hash_entry (h)->got_type = got_type;
8300
0
    else
8301
0
      {
8302
0
        struct elf_aarch64_local_symbol *locals;
8303
0
        locals = elf_aarch64_locals (abfd);
8304
0
        BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
8305
0
        locals[r_symndx].got_type = got_type;
8306
0
      }
8307
0
        }
8308
8309
0
      if (htab->root.dynobj == NULL)
8310
0
        htab->root.dynobj = abfd;
8311
0
      if (! aarch64_elf_create_got_section (htab->root.dynobj, info))
8312
0
        return false;
8313
0
      break;
8314
0
    }
8315
8316
0
  case BFD_RELOC_AARCH64_CALL26:
8317
0
  case BFD_RELOC_AARCH64_JUMP26:
8318
    /* If this is a local symbol then we resolve it
8319
       directly without creating a PLT entry.  */
8320
0
    if (h == NULL)
8321
0
      continue;
8322
8323
0
    h->needs_plt = 1;
8324
0
    if (h->plt.refcount <= 0)
8325
0
      h->plt.refcount = 1;
8326
0
    else
8327
0
      h->plt.refcount += 1;
8328
0
    break;
8329
8330
0
  default:
8331
0
    break;
8332
0
  }
8333
0
    }
8334
8335
0
  return true;
8336
0
}
8337
8338
/* Treat mapping symbols as special target symbols.  */
8339
8340
static bool
8341
elf64_aarch64_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED,
8342
          asymbol *sym)
8343
0
{
8344
0
  return bfd_is_aarch64_special_symbol_name (sym->name,
8345
0
               BFD_AARCH64_SPECIAL_SYM_TYPE_ANY);
8346
0
}
8347
8348
/* If the ELF symbol SYM might be a function in SEC, return the
8349
   function size and set *CODE_OFF to the function's entry point,
8350
   otherwise return zero.  */
8351
8352
static bfd_size_type
8353
elf64_aarch64_maybe_function_sym (const asymbol *sym, asection *sec,
8354
          bfd_vma *code_off)
8355
1.97k
{
8356
1.97k
  bfd_size_type size;
8357
1.97k
  elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
8358
8359
1.97k
  if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
8360
1.97k
         | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
8361
1.49k
      || sym->section != sec)
8362
1.89k
    return 0;
8363
8364
87
  size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
8365
8366
87
  if (!(sym->flags & BSF_SYNTHETIC))
8367
87
    switch (ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info))
8368
87
      {
8369
39
  case STT_NOTYPE:
8370
    /* Ignore symbols created by the annobin plugin for gcc and clang.
8371
       These symbols are hidden, local, notype and have a size of 0.  */
8372
39
    if (size == 0
8373
19
        && sym->flags & BSF_LOCAL
8374
19
        && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
8375
0
      return 0;
8376
    /* Fall through.  */
8377
65
  case STT_FUNC:
8378
    /* FIXME: Allow STT_GNU_IFUNC as well ?  */
8379
65
    break;
8380
22
  default:
8381
22
    return 0;
8382
87
      }
8383
8384
65
  if ((sym->flags & BSF_LOCAL)
8385
53
      && bfd_is_aarch64_special_symbol_name (sym->name,
8386
53
               BFD_AARCH64_SPECIAL_SYM_TYPE_ANY))
8387
0
    return 0;
8388
8389
65
  *code_off = sym->value;
8390
8391
  /* Do not return 0 for the function's size.  */
8392
65
  return size ? size : 1;
8393
65
}
8394
8395
static bool
8396
elf64_aarch64_find_inliner_info (bfd *abfd,
8397
         const char **filename_ptr,
8398
         const char **functionname_ptr,
8399
         unsigned int *line_ptr)
8400
0
{
8401
0
  bool found;
8402
0
  found = _bfd_dwarf2_find_inliner_info
8403
0
    (abfd, filename_ptr,
8404
0
     functionname_ptr, line_ptr, &elf_tdata (abfd)->dwarf2_find_line_info);
8405
0
  return found;
8406
0
}
8407
8408
8409
static bool
8410
elf64_aarch64_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
8411
0
{
8412
0
  Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form.  */
8413
8414
0
  if (!_bfd_elf_init_file_header (abfd, link_info))
8415
0
    return false;
8416
8417
0
  i_ehdrp = elf_elfheader (abfd);
8418
0
  i_ehdrp->e_ident[EI_ABIVERSION] = AARCH64_ELF_ABI_VERSION;
8419
0
  return true;
8420
0
}
8421
8422
static enum elf_reloc_type_class
8423
elf64_aarch64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
8424
        const asection *rel_sec ATTRIBUTE_UNUSED,
8425
        const Elf_Internal_Rela *rela)
8426
0
{
8427
0
  struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
8428
8429
0
  if (htab->root.dynsym != NULL
8430
0
      && htab->root.dynsym->contents != NULL)
8431
0
    {
8432
      /* Check relocation against STT_GNU_IFUNC symbol if there are
8433
   dynamic symbols.  */
8434
0
      bfd *abfd = info->output_bfd;
8435
0
      elf_backend_data *bed = get_elf_backend_data (abfd);
8436
0
      unsigned long r_symndx = ELF64_R_SYM (rela->r_info);
8437
0
      if (r_symndx != STN_UNDEF)
8438
0
  {
8439
0
    Elf_Internal_Sym sym;
8440
0
    if (!bed->s->swap_symbol_in (abfd,
8441
0
               (htab->root.dynsym->contents
8442
0
          + r_symndx * bed->s->sizeof_sym),
8443
0
               0, &sym))
8444
0
      {
8445
        /* xgettext:c-format */
8446
0
        _bfd_error_handler (_("%pB symbol number %lu references"
8447
0
            " nonexistent SHT_SYMTAB_SHNDX section"),
8448
0
            abfd, r_symndx);
8449
        /* Ideally an error class should be returned here.  */
8450
0
      }
8451
0
    else if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
8452
0
      return reloc_class_ifunc;
8453
0
  }
8454
0
    }
8455
8456
0
  switch ((int) ELF64_R_TYPE (rela->r_info))
8457
0
    {
8458
0
    case AARCH64_R (IRELATIVE):
8459
0
      return reloc_class_ifunc;
8460
0
    case AARCH64_R (RELATIVE):
8461
0
      return reloc_class_relative;
8462
0
    case AARCH64_R (JUMP_SLOT):
8463
0
      return reloc_class_plt;
8464
0
    case AARCH64_R (COPY):
8465
0
      return reloc_class_copy;
8466
0
    default:
8467
0
      return reloc_class_normal;
8468
0
    }
8469
0
}
8470
8471
/* Handle an AArch64 specific section when reading an object file.  This is
8472
   called when bfd_section_from_shdr finds a section with an unknown
8473
   type.  */
8474
8475
static bool
8476
elf64_aarch64_section_from_shdr (bfd *abfd,
8477
         Elf_Internal_Shdr *hdr,
8478
         const char *name, int shindex)
8479
1.01k
{
8480
  /* There ought to be a place to keep ELF backend specific flags, but
8481
     at the moment there isn't one.  We just keep track of the
8482
     sections by their name, instead.  Fortunately, the ABI gives
8483
     names for all the AArch64 specific sections, so we will probably get
8484
     away with this.  */
8485
1.01k
  switch (hdr->sh_type)
8486
1.01k
    {
8487
0
    case SHT_AARCH64_ATTRIBUTES:
8488
0
      break;
8489
8490
1.01k
    default:
8491
1.01k
      return false;
8492
1.01k
    }
8493
8494
0
  if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
8495
0
    return false;
8496
8497
0
  return true;
8498
0
}
8499
8500
/* Process any AArch64-specific program segment types.  */
8501
8502
static bool
8503
elf64_aarch64_section_from_phdr (bfd *abfd ATTRIBUTE_UNUSED,
8504
         Elf_Internal_Phdr *hdr,
8505
         int hdr_index ATTRIBUTE_UNUSED,
8506
         const char *name ATTRIBUTE_UNUSED)
8507
0
{
8508
  /* Right now we only handle the PT_AARCH64_MEMTAG_MTE segment type.  */
8509
0
  if (hdr == NULL || hdr->p_type != PT_AARCH64_MEMTAG_MTE)
8510
0
    return false;
8511
8512
0
  if (hdr->p_filesz > 0)
8513
0
    {
8514
      /* Sections created from memory tag p_type's are always named
8515
   "memtag".  This makes it easier for tools (for example, GDB)
8516
   to find them.  */
8517
0
      asection *newsect = bfd_make_section_anyway (abfd, "memtag");
8518
8519
0
      if (newsect == NULL)
8520
0
  return false;
8521
8522
0
      unsigned int opb = bfd_octets_per_byte (abfd, NULL);
8523
8524
      /* p_vaddr holds the original start address of the tagged memory
8525
   range.  */
8526
0
      newsect->vma = hdr->p_vaddr / opb;
8527
8528
      /* p_filesz holds the storage size of the packed tags.  */
8529
0
      newsect->size = hdr->p_filesz;
8530
0
      newsect->filepos = hdr->p_offset;
8531
8532
      /* p_memsz holds the size of the memory range that contains tags.  The
8533
   section's rawsize field is reused for this purpose.  */
8534
0
      newsect->rawsize = hdr->p_memsz;
8535
8536
      /* Make sure the section's flags has SEC_HAS_CONTENTS set, otherwise
8537
   BFD will return all zeroes when attempting to get contents from this
8538
   section.  */
8539
0
      newsect->flags |= SEC_HAS_CONTENTS;
8540
0
    }
8541
8542
0
  return true;
8543
0
}
8544
8545
/* Implements the bfd_elf_modify_headers hook for aarch64.  */
8546
8547
static bool
8548
elf64_aarch64_modify_headers (bfd *abfd,
8549
            struct bfd_link_info *info)
8550
0
{
8551
0
  struct elf_segment_map *m;
8552
0
  Elf_Internal_Phdr *p;
8553
8554
0
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
8555
0
    {
8556
      /* We are only interested in the memory tag segment that will be dumped
8557
   to a core file.  If we have no memory tags or this isn't a core file we
8558
   are dealing with, just skip this segment.  */
8559
0
      if (m->p_type != PT_AARCH64_MEMTAG_MTE
8560
0
    || bfd_get_format (abfd) != bfd_core)
8561
0
  continue;
8562
8563
      /* For memory tag segments in core files, the size of the file contents
8564
   is smaller than the size of the memory range.  Adjust the memory size
8565
   accordingly.  The real memory size is held in the section's rawsize
8566
   field.  */
8567
0
      if (m->count > 0)
8568
0
  {
8569
0
    p = elf_tdata (abfd)->phdr;
8570
0
    p += m->idx;
8571
0
    p->p_memsz = m->sections[0]->rawsize;
8572
0
    p->p_flags = 0;
8573
0
    p->p_paddr = 0;
8574
0
    p->p_align = 0;
8575
0
  }
8576
0
    }
8577
8578
  /* Give the generic code a chance to handle the headers.  */
8579
0
  return _bfd_elf_modify_headers (abfd, info);
8580
0
}
8581
8582
8583
typedef struct
8584
{
8585
  void *finfo;
8586
  struct bfd_link_info *info;
8587
  asection *sec;
8588
  int sec_shndx;
8589
  int (*func) (void *, const char *, Elf_Internal_Sym *,
8590
         asection *, struct elf_link_hash_entry *);
8591
} output_arch_syminfo;
8592
8593
enum map_symbol_type
8594
{
8595
  AARCH64_MAP_INSN,
8596
  AARCH64_MAP_DATA
8597
};
8598
8599
8600
/* Output a single mapping symbol.  */
8601
8602
static bool
8603
elf64_aarch64_output_map_sym (output_arch_syminfo *osi,
8604
            enum map_symbol_type type, bfd_vma offset)
8605
0
{
8606
0
  static const char *names[2] = { "$x", "$d" };
8607
0
  Elf_Internal_Sym sym;
8608
8609
0
  sym.st_value = (osi->sec->output_section->vma
8610
0
      + osi->sec->output_offset + offset);
8611
0
  sym.st_size = 0;
8612
0
  sym.st_other = 0;
8613
0
  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
8614
0
  sym.st_shndx = osi->sec_shndx;
8615
0
  return osi->func (osi->finfo, names[type], &sym, osi->sec, NULL) == 1;
8616
0
}
8617
8618
/* Output a single local symbol for a generated stub.  */
8619
8620
static bool
8621
elf64_aarch64_output_stub_sym (output_arch_syminfo *osi, const char *name,
8622
             bfd_vma offset, bfd_vma size)
8623
0
{
8624
0
  Elf_Internal_Sym sym;
8625
8626
0
  sym.st_value = (osi->sec->output_section->vma
8627
0
      + osi->sec->output_offset + offset);
8628
0
  sym.st_size = size;
8629
0
  sym.st_other = 0;
8630
0
  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
8631
0
  sym.st_shndx = osi->sec_shndx;
8632
0
  return osi->func (osi->finfo, name, &sym, osi->sec, NULL) == 1;
8633
0
}
8634
8635
static bool
8636
aarch64_map_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
8637
0
{
8638
0
  struct elf_aarch64_stub_hash_entry *stub_entry;
8639
0
  asection *stub_sec;
8640
0
  bfd_vma addr;
8641
0
  char *stub_name;
8642
0
  output_arch_syminfo *osi;
8643
8644
  /* Massage our args to the form they really have.  */
8645
0
  stub_entry = (struct elf_aarch64_stub_hash_entry *) gen_entry;
8646
0
  osi = (output_arch_syminfo *) in_arg;
8647
8648
0
  stub_sec = stub_entry->stub_sec;
8649
8650
  /* Ensure this stub is attached to the current section being
8651
     processed.  */
8652
0
  if (stub_sec != osi->sec)
8653
0
    return true;
8654
8655
0
  addr = (bfd_vma) stub_entry->stub_offset;
8656
8657
0
  stub_name = stub_entry->output_name;
8658
8659
0
  switch (stub_entry->stub_type)
8660
0
    {
8661
0
    case aarch64_stub_adrp_branch:
8662
0
      if (!elf64_aarch64_output_stub_sym (osi, stub_name, addr,
8663
0
            sizeof (aarch64_adrp_branch_stub)))
8664
0
  return false;
8665
0
      if (!elf64_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
8666
0
  return false;
8667
0
      break;
8668
0
    case aarch64_stub_long_branch:
8669
0
      if (!elf64_aarch64_output_stub_sym
8670
0
    (osi, stub_name, addr, sizeof (aarch64_long_branch_stub)))
8671
0
  return false;
8672
0
      if (!elf64_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
8673
0
  return false;
8674
0
      if (!elf64_aarch64_output_map_sym (osi, AARCH64_MAP_DATA, addr + 16))
8675
0
  return false;
8676
0
      break;
8677
0
    case aarch64_stub_bti_direct_branch:
8678
0
      if (!elf64_aarch64_output_stub_sym (osi, stub_name, addr,
8679
0
    sizeof (aarch64_bti_direct_branch_stub)))
8680
0
  return false;
8681
0
      if (!elf64_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
8682
0
  return false;
8683
0
      break;
8684
0
    case aarch64_stub_erratum_835769_veneer:
8685
0
      if (!elf64_aarch64_output_stub_sym (osi, stub_name, addr,
8686
0
            sizeof (aarch64_erratum_835769_stub)))
8687
0
  return false;
8688
0
      if (!elf64_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
8689
0
  return false;
8690
0
      break;
8691
0
    case aarch64_stub_erratum_843419_veneer:
8692
0
      if (!elf64_aarch64_output_stub_sym (osi, stub_name, addr,
8693
0
            sizeof (aarch64_erratum_843419_stub)))
8694
0
  return false;
8695
0
      if (!elf64_aarch64_output_map_sym (osi, AARCH64_MAP_INSN, addr))
8696
0
  return false;
8697
0
      break;
8698
0
    case aarch64_stub_none:
8699
0
      break;
8700
8701
0
    default:
8702
0
      abort ();
8703
0
    }
8704
8705
0
  return true;
8706
0
}
8707
8708
/* Output mapping symbols for linker generated sections.  */
8709
8710
static bool
8711
elf64_aarch64_output_arch_local_syms (bfd *output_bfd,
8712
              struct bfd_link_info *info,
8713
              void *finfo,
8714
              int (*func) (void *, const char *,
8715
               Elf_Internal_Sym *,
8716
               asection *,
8717
               struct elf_link_hash_entry
8718
               *))
8719
0
{
8720
0
  output_arch_syminfo osi;
8721
0
  struct elf_aarch64_link_hash_table *htab;
8722
8723
0
  if (info->strip == strip_all
8724
0
      && !info->emitrelocations
8725
0
      && !bfd_link_relocatable (info))
8726
0
    return true;
8727
8728
0
  htab = elf_aarch64_hash_table (info);
8729
8730
0
  osi.finfo = finfo;
8731
0
  osi.info = info;
8732
0
  osi.func = func;
8733
8734
  /* Long calls stubs.  */
8735
0
  if (htab->stub_bfd && htab->stub_bfd->sections)
8736
0
    {
8737
0
      asection *stub_sec;
8738
8739
0
      for (stub_sec = htab->stub_bfd->sections;
8740
0
     stub_sec != NULL; stub_sec = stub_sec->next)
8741
0
  {
8742
    /* Ignore non-stub sections.  */
8743
0
    if (!strstr (stub_sec->name, STUB_SUFFIX))
8744
0
      continue;
8745
8746
0
    osi.sec = stub_sec;
8747
8748
0
    osi.sec_shndx = _bfd_elf_section_from_bfd_section
8749
0
      (output_bfd, osi.sec->output_section);
8750
8751
    /* The first instruction in a stub is always a branch.  */
8752
0
    if (!elf64_aarch64_output_map_sym (&osi, AARCH64_MAP_INSN, 0))
8753
0
      return false;
8754
8755
0
    bfd_hash_traverse (&htab->stub_hash_table, aarch64_map_one_stub,
8756
0
           &osi);
8757
0
  }
8758
0
    }
8759
8760
  /* Finally, output mapping symbols for the PLT.  */
8761
0
  if (!htab->root.splt || htab->root.splt->size == 0)
8762
0
    return true;
8763
8764
0
  osi.sec_shndx = _bfd_elf_section_from_bfd_section
8765
0
    (output_bfd, htab->root.splt->output_section);
8766
0
  osi.sec = htab->root.splt;
8767
8768
0
  elf64_aarch64_output_map_sym (&osi, AARCH64_MAP_INSN, 0);
8769
8770
0
  return true;
8771
8772
0
}
8773
8774
/* Allocate target specific section data.  */
8775
8776
static bool
8777
elf64_aarch64_new_section_hook (bfd *abfd, asection *sec)
8778
2.18k
{
8779
2.18k
  _aarch64_elf_section_data *sdata;
8780
8781
2.18k
  sdata = bfd_zalloc (abfd, sizeof (*sdata));
8782
2.18k
  if (sdata == NULL)
8783
0
    return false;
8784
2.18k
  sec->used_by_bfd = sdata;
8785
8786
2.18k
  return _bfd_elf_new_section_hook (abfd, sec);
8787
2.18k
}
8788
8789
8790
/* Create dynamic sections. This is different from the ARM backend in that
8791
   the got, plt, gotplt and their relocation sections are all created in the
8792
   standard part of the bfd elf backend.  */
8793
8794
static bool
8795
elf64_aarch64_create_dynamic_sections (bfd *dynobj,
8796
               struct bfd_link_info *info)
8797
0
{
8798
  /* We need to create .got section.  */
8799
0
  if (!aarch64_elf_create_got_section (dynobj, info))
8800
0
    return false;
8801
8802
0
  return _bfd_elf_create_dynamic_sections (dynobj, info);
8803
0
}
8804
8805
8806
/* Allocate space in .plt, .got and associated reloc sections for
8807
   dynamic relocs.  */
8808
8809
static bool
8810
elf64_aarch64_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8811
0
{
8812
0
  struct bfd_link_info *info;
8813
0
  struct elf_aarch64_link_hash_table *htab;
8814
0
  struct elf_aarch64_link_hash_entry *eh;
8815
0
  struct elf_dyn_relocs *p;
8816
8817
  /* An example of a bfd_link_hash_indirect symbol is versioned
8818
     symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
8819
     -> __gxx_personality_v0(bfd_link_hash_defined)
8820
8821
     There is no need to process bfd_link_hash_indirect symbols here
8822
     because we will also be presented with the concrete instance of
8823
     the symbol and elf64_aarch64_copy_indirect_symbol () will have been
8824
     called to copy all relevant data from the generic to the concrete
8825
     symbol instance.  */
8826
0
  if (h->root.type == bfd_link_hash_indirect)
8827
0
    return true;
8828
8829
0
  if (h->root.type == bfd_link_hash_warning)
8830
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
8831
8832
0
  info = (struct bfd_link_info *) inf;
8833
0
  htab = elf_aarch64_hash_table (info);
8834
8835
  /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
8836
     here if it is defined and referenced in a non-shared object.  */
8837
0
  if (h->type == STT_GNU_IFUNC
8838
0
      && h->def_regular)
8839
0
    return true;
8840
0
  else if (htab->root.dynamic_sections_created && h->plt.refcount > 0)
8841
0
    {
8842
      /* Make sure this symbol is output as a dynamic symbol.
8843
   Undefined weak syms won't yet be marked as dynamic.  */
8844
0
      if (h->dynindx == -1 && !h->forced_local
8845
0
    && h->root.type == bfd_link_hash_undefweak)
8846
0
  {
8847
0
    if (!bfd_elf_link_record_dynamic_symbol (info, h))
8848
0
      return false;
8849
0
  }
8850
8851
0
      if (bfd_link_pic (info) || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
8852
0
  {
8853
0
    asection *s = htab->root.splt;
8854
8855
    /* If this is the first .plt entry, make room for the special
8856
       first entry.  */
8857
0
    if (s->size == 0)
8858
0
      s->size += htab->plt_header_size;
8859
8860
0
    h->plt.offset = s->size;
8861
8862
    /* If this symbol is not defined in a regular file, and we are
8863
       not generating a shared library, then set the symbol to this
8864
       location in the .plt.  This is required to make function
8865
       pointers compare as equal between the normal executable and
8866
       the shared library.  */
8867
0
    if (!bfd_link_pic (info) && !h->def_regular)
8868
0
      {
8869
0
        h->root.u.def.section = s;
8870
0
        h->root.u.def.value = h->plt.offset;
8871
0
      }
8872
8873
    /* Make room for this entry. For now we only create the
8874
       small model PLT entries. We later need to find a way
8875
       of relaxing into these from the large model PLT entries.  */
8876
0
    s->size += htab->plt_entry_size;
8877
8878
    /* We also need to make an entry in the .got.plt section, which
8879
       will be placed in the .got section by the linker script.  */
8880
0
    htab->root.sgotplt->size += GOT_ENTRY_SIZE;
8881
8882
    /* We also need to make an entry in the .rela.plt section.  */
8883
0
    htab->root.srelplt->size += RELOC_SIZE (htab);
8884
8885
    /* We need to ensure that all GOT entries that serve the PLT
8886
       are consecutive with the special GOT slots [0] [1] and
8887
       [2]. Any addtional relocations, such as
8888
       R_AARCH64_TLSDESC, must be placed after the PLT related
8889
       entries.  We abuse the reloc_count such that during
8890
       sizing we adjust reloc_count to indicate the number of
8891
       PLT related reserved entries.  In subsequent phases when
8892
       filling in the contents of the reloc entries, PLT related
8893
       entries are placed by computing their PLT index (0
8894
       .. reloc_count). While other none PLT relocs are placed
8895
       at the slot indicated by reloc_count and reloc_count is
8896
       updated.  */
8897
8898
0
    htab->root.srelplt->reloc_count++;
8899
8900
    /* Mark the DSO in case R_<CLS>_JUMP_SLOT relocs against
8901
       variant PCS symbols are present.  */
8902
0
    if (h->other & STO_AARCH64_VARIANT_PCS)
8903
0
      htab->variant_pcs = 1;
8904
8905
0
  }
8906
0
      else
8907
0
  {
8908
0
    h->plt.offset = (bfd_vma) - 1;
8909
0
    h->needs_plt = 0;
8910
0
  }
8911
0
    }
8912
0
  else
8913
0
    {
8914
0
      h->plt.offset = (bfd_vma) - 1;
8915
0
      h->needs_plt = 0;
8916
0
    }
8917
8918
0
  eh = (struct elf_aarch64_link_hash_entry *) h;
8919
0
  eh->tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
8920
8921
0
  if (h->got.refcount > 0)
8922
0
    {
8923
0
      bool dyn;
8924
0
      unsigned got_type = elf_aarch64_hash_entry (h)->got_type;
8925
8926
0
      h->got.offset = (bfd_vma) - 1;
8927
8928
0
      dyn = htab->root.dynamic_sections_created;
8929
8930
      /* Make sure this symbol is output as a dynamic symbol.
8931
   Undefined weak syms won't yet be marked as dynamic.  */
8932
0
      if (dyn && h->dynindx == -1 && !h->forced_local
8933
0
    && h->root.type == bfd_link_hash_undefweak)
8934
0
  {
8935
0
    if (!bfd_elf_link_record_dynamic_symbol (info, h))
8936
0
      return false;
8937
0
  }
8938
8939
0
      if (got_type == GOT_UNKNOWN)
8940
0
  {
8941
0
  }
8942
0
      else if (got_type == GOT_NORMAL)
8943
0
  {
8944
0
    h->got.offset = htab->root.sgot->size;
8945
0
    htab->root.sgot->size += GOT_ENTRY_SIZE;
8946
0
    if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8947
0
         || h->root.type != bfd_link_hash_undefweak)
8948
0
        && (bfd_link_pic (info)
8949
0
      || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
8950
        /* Undefined weak symbol in static PIE resolves to 0 without
8951
     any dynamic relocations.  */
8952
0
        && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8953
0
      {
8954
0
        htab->root.srelgot->size += RELOC_SIZE (htab);
8955
0
      }
8956
0
  }
8957
0
      else
8958
0
  {
8959
0
    int indx;
8960
0
    if (got_type & GOT_TLSDESC_GD)
8961
0
      {
8962
0
        eh->tlsdesc_got_jump_table_offset =
8963
0
    (htab->root.sgotplt->size
8964
0
     - aarch64_compute_jump_table_size (htab));
8965
0
        htab->root.sgotplt->size += GOT_ENTRY_SIZE * 2;
8966
0
        h->got.offset = (bfd_vma) - 2;
8967
0
      }
8968
8969
0
    if (got_type & GOT_TLS_GD)
8970
0
      {
8971
0
        h->got.offset = htab->root.sgot->size;
8972
0
        htab->root.sgot->size += GOT_ENTRY_SIZE * 2;
8973
0
      }
8974
8975
0
    if (got_type & GOT_TLS_IE)
8976
0
      {
8977
0
        h->got.offset = htab->root.sgot->size;
8978
0
        htab->root.sgot->size += GOT_ENTRY_SIZE;
8979
0
      }
8980
8981
0
    indx = h && h->dynindx != -1 ? h->dynindx : 0;
8982
0
    if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8983
0
         || h->root.type != bfd_link_hash_undefweak)
8984
0
        && (!bfd_link_executable (info)
8985
0
      || indx != 0
8986
0
      || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
8987
0
      {
8988
0
        if (got_type & GOT_TLSDESC_GD)
8989
0
    {
8990
0
      htab->root.srelplt->size += RELOC_SIZE (htab);
8991
      /* Note reloc_count not incremented here!  We have
8992
         already adjusted reloc_count for this relocation
8993
         type.  */
8994
8995
      /* TLSDESC PLT is now needed, but not yet determined.  */
8996
0
      htab->root.tlsdesc_plt = (bfd_vma) - 1;
8997
0
    }
8998
8999
0
        if (got_type & GOT_TLS_GD)
9000
0
    htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
9001
9002
0
        if (got_type & GOT_TLS_IE)
9003
0
    htab->root.srelgot->size += RELOC_SIZE (htab);
9004
0
      }
9005
0
  }
9006
0
    }
9007
0
  else
9008
0
    {
9009
0
      h->got.offset = (bfd_vma) - 1;
9010
0
    }
9011
9012
0
  if (h->dyn_relocs == NULL)
9013
0
    return true;
9014
9015
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
9016
0
    if (eh->def_protected)
9017
0
      {
9018
  /* Disallow copy relocations against protected symbol.  */
9019
0
  asection *s = p->sec->output_section;
9020
0
  if (s != NULL && (s->flags & SEC_READONLY) != 0)
9021
0
    {
9022
0
      info->callbacks->fatal
9023
    /* xgettext:c-format */
9024
0
    (_ ("%P: %pB: copy relocation against non-copyable "
9025
0
        "protected symbol `%s'\n"),
9026
0
     p->sec->owner, h->root.root.string);
9027
0
      return false;
9028
0
    }
9029
0
      }
9030
9031
  /* In the shared -Bsymbolic case, discard space allocated for
9032
     dynamic pc-relative relocs against symbols which turn out to be
9033
     defined in regular objects.  For the normal shared case, discard
9034
     space for pc-relative relocs that have become local due to symbol
9035
     visibility changes.  */
9036
9037
0
  if (bfd_link_pic (info))
9038
0
    {
9039
      /* Relocs that use pc_count are those that appear on a call
9040
   insn, or certain REL relocs that can generated via assembly.
9041
   We want calls to protected symbols to resolve directly to the
9042
   function rather than going via the plt.  If people want
9043
   function pointer comparisons to work as expected then they
9044
   should avoid writing weird assembly.  */
9045
0
      if (SYMBOL_CALLS_LOCAL (info, h))
9046
0
  {
9047
0
    struct elf_dyn_relocs **pp;
9048
9049
0
    for (pp = &h->dyn_relocs; (p = *pp) != NULL;)
9050
0
      {
9051
0
        p->count -= p->pc_count;
9052
0
        p->pc_count = 0;
9053
0
        if (p->count == 0)
9054
0
    *pp = p->next;
9055
0
        else
9056
0
    pp = &p->next;
9057
0
      }
9058
0
  }
9059
9060
      /* Also discard relocs on undefined weak syms with non-default
9061
   visibility.  */
9062
0
      if (h->dyn_relocs != NULL && h->root.type == bfd_link_hash_undefweak)
9063
0
  {
9064
0
    if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
9065
0
        || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9066
0
      h->dyn_relocs = NULL;
9067
9068
    /* Make sure undefined weak symbols are output as a dynamic
9069
       symbol in PIEs.  */
9070
0
    else if (h->dynindx == -1
9071
0
       && !h->forced_local
9072
0
       && h->root.type == bfd_link_hash_undefweak
9073
0
       && !bfd_elf_link_record_dynamic_symbol (info, h))
9074
0
      return false;
9075
0
  }
9076
9077
0
    }
9078
0
  else if (ELIMINATE_COPY_RELOCS)
9079
0
    {
9080
      /* For the non-shared case, discard space for relocs against
9081
   symbols which turn out to need copy relocs or are not
9082
   dynamic.  */
9083
9084
0
      if (!h->non_got_ref
9085
0
    && ((h->def_dynamic
9086
0
         && !h->def_regular)
9087
0
        || (htab->root.dynamic_sections_created
9088
0
      && (h->root.type == bfd_link_hash_undefweak
9089
0
          || h->root.type == bfd_link_hash_undefined))))
9090
0
  {
9091
    /* Make sure this symbol is output as a dynamic symbol.
9092
       Undefined weak syms won't yet be marked as dynamic.  */
9093
0
    if (h->dynindx == -1
9094
0
        && !h->forced_local
9095
0
        && h->root.type == bfd_link_hash_undefweak
9096
0
        && !bfd_elf_link_record_dynamic_symbol (info, h))
9097
0
      return false;
9098
9099
    /* If that succeeded, we know we'll be keeping all the
9100
       relocs.  */
9101
0
    if (h->dynindx != -1)
9102
0
      goto keep;
9103
0
  }
9104
9105
0
      h->dyn_relocs = NULL;
9106
9107
0
    keep:;
9108
0
    }
9109
9110
  /* Finally, allocate space.  */
9111
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
9112
0
    {
9113
0
      asection *sreloc;
9114
9115
0
      sreloc = elf_section_data (p->sec)->sreloc;
9116
9117
0
      BFD_ASSERT (sreloc != NULL);
9118
9119
0
      sreloc->size += p->count * RELOC_SIZE (htab);
9120
0
    }
9121
9122
0
  return true;
9123
0
}
9124
9125
/* Allocate space in .plt, .got and associated reloc sections for
9126
   ifunc dynamic relocs.  */
9127
9128
static bool
9129
elf64_aarch64_allocate_ifunc_dynrelocs (struct elf_link_hash_entry *h,
9130
          void *inf)
9131
0
{
9132
0
  struct bfd_link_info *info;
9133
0
  struct elf_aarch64_link_hash_table *htab;
9134
9135
  /* An example of a bfd_link_hash_indirect symbol is versioned
9136
     symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
9137
     -> __gxx_personality_v0(bfd_link_hash_defined)
9138
9139
     There is no need to process bfd_link_hash_indirect symbols here
9140
     because we will also be presented with the concrete instance of
9141
     the symbol and elf64_aarch64_copy_indirect_symbol () will have been
9142
     called to copy all relevant data from the generic to the concrete
9143
     symbol instance.  */
9144
0
  if (h->root.type == bfd_link_hash_indirect)
9145
0
    return true;
9146
9147
0
  if (h->root.type == bfd_link_hash_warning)
9148
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
9149
9150
0
  info = (struct bfd_link_info *) inf;
9151
0
  htab = elf_aarch64_hash_table (info);
9152
9153
  /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
9154
     here if it is defined and referenced in a non-shared object.  */
9155
0
  if (h->type == STT_GNU_IFUNC
9156
0
      && h->def_regular)
9157
0
    return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
9158
0
                 &h->dyn_relocs,
9159
0
                 htab->plt_entry_size,
9160
0
                 htab->plt_header_size,
9161
0
                 GOT_ENTRY_SIZE,
9162
0
                 false);
9163
0
  return true;
9164
0
}
9165
9166
/* Allocate space in .plt, .got and associated reloc sections for
9167
   local ifunc dynamic relocs.  */
9168
9169
static int
9170
elf64_aarch64_allocate_local_ifunc_dynrelocs (void **slot, void *inf)
9171
0
{
9172
0
  struct elf_link_hash_entry *h
9173
0
    = (struct elf_link_hash_entry *) *slot;
9174
9175
0
  if (h->type != STT_GNU_IFUNC
9176
0
      || !h->def_regular
9177
0
      || !h->ref_regular
9178
0
      || !h->forced_local
9179
0
      || h->root.type != bfd_link_hash_defined)
9180
0
    abort ();
9181
9182
0
  return elf64_aarch64_allocate_ifunc_dynrelocs (h, inf);
9183
0
}
9184
9185
/* Record a relative relocation that will be emitted packed (DT_RELR).
9186
   Called after relocation sections are sized, so undo the size accounting
9187
   for this relocation.  */
9188
9189
static bool
9190
record_relr (struct elf_aarch64_link_hash_table *htab, asection *sec,
9191
       bfd_vma off, asection *sreloc)
9192
0
{
9193
  /* Undo the relocation section size accounting.  */
9194
0
  BFD_ASSERT (sreloc->size >= RELOC_SIZE (htab));
9195
0
  sreloc->size -= RELOC_SIZE (htab);
9196
  /* The packing format uses the last bit of the address so that
9197
     must be aligned.  We don't pack relocations that may not be
9198
     aligned even though the final output address could end up
9199
     aligned, to avoid complex sizing logic for a rare case.  */
9200
0
  BFD_ASSERT (off % 2 == 0 && sec->alignment_power > 0);
9201
0
  if (htab->relr_count >= htab->relr_alloc)
9202
0
    {
9203
0
      if (htab->relr_alloc == 0)
9204
0
  htab->relr_alloc = 4096;
9205
0
      else
9206
0
  htab->relr_alloc *= 2;
9207
0
      htab->relr = bfd_realloc (htab->relr,
9208
0
        htab->relr_alloc * sizeof (*htab->relr));
9209
0
      if (htab->relr == NULL)
9210
0
  return false;
9211
0
    }
9212
0
  htab->relr[htab->relr_count].sec = sec;
9213
0
  htab->relr[htab->relr_count].off = off;
9214
0
  htab->relr_count++;
9215
0
  return true;
9216
0
}
9217
9218
/* Follow elf64_aarch64_allocate_dynrelocs, but only record relative
9219
   relocations against the GOT and undo their previous size accounting.  */
9220
9221
static bool
9222
record_relr_dyn_got_relocs (struct elf_link_hash_entry *h, void *inf)
9223
0
{
9224
9225
0
  if (h->root.type == bfd_link_hash_indirect)
9226
0
    return true;
9227
0
  if (h->root.type == bfd_link_hash_warning)
9228
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
9229
0
  if (h->type == STT_GNU_IFUNC && h->def_regular)
9230
0
    return true;
9231
0
  if (h->got.refcount <= 0)
9232
0
    return true;
9233
0
  if (elf_aarch64_hash_entry (h)->got_type != GOT_NORMAL)
9234
0
    return true;
9235
9236
0
  struct bfd_link_info *info = (struct bfd_link_info *) inf;
9237
0
  struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
9238
9239
0
  if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9240
0
       || h->root.type != bfd_link_hash_undefweak)
9241
0
      && bfd_link_pic (info)
9242
      /* Undefined weak symbol in static PIE resolves to 0 without
9243
   any dynamic relocations.  */
9244
0
      && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9245
0
    {
9246
0
      bool relative_reloc = SYMBOL_REFERENCES_LOCAL (info, h)
9247
0
          && !bfd_is_abs_symbol (&h->root);
9248
0
      if (relative_reloc)
9249
0
  if (!record_relr (htab, htab->root.sgot, h->got.offset,
9250
0
        htab->root.srelgot))
9251
0
    return false;
9252
0
    }
9253
0
  return true;
9254
0
}
9255
9256
/* Record packed relative relocs against the GOT for local symbols.
9257
   Undo the size accounting of elf64_aarch64_late_size_sections.  */
9258
9259
static bool
9260
record_relr_local_got_relocs (bfd *input_bfd, struct bfd_link_info *info)
9261
0
{
9262
0
  struct elf_aarch64_local_symbol *locals;
9263
0
  Elf_Internal_Shdr *symtab_hdr;
9264
0
  struct elf_aarch64_link_hash_table *htab;
9265
9266
0
  if (!bfd_link_pic (info))
9267
0
    return true;
9268
9269
0
  locals = elf_aarch64_locals (input_bfd);
9270
0
  if (locals == NULL)
9271
0
    return true;
9272
9273
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
9274
0
  htab = elf_aarch64_hash_table (info);
9275
0
  for (unsigned int i = 0; i < symtab_hdr->sh_info; i++)
9276
0
    {
9277
0
      bfd_vma off = locals[i].got_offset;
9278
0
      if (locals[i].got_refcount <= 0)
9279
0
  continue;
9280
0
      if ((locals[i].got_type & GOT_NORMAL) == 0)
9281
0
  continue;
9282
9283
      /* FIXME: If the local symbol is in SHN_ABS then emitting
9284
   a relative relocation is not correct, but it seems to
9285
   be wrong in elf64_aarch64_final_link_relocate too.  */
9286
0
      if (!record_relr (htab, htab->root.sgot, off, htab->root.srelgot))
9287
0
  return false;
9288
0
    }
9289
0
  return true;
9290
0
}
9291
9292
/* Follows the logic of elf64_aarch64_relocate_section to decide which
9293
   relocations will become relative and possible to pack.  Ignore
9294
   relocations against the GOT, those are handled separately per-symbol.
9295
   Undo the size accounting of the packed relocations and record them
9296
   so the relr section can be sized later.  */
9297
9298
static bool
9299
record_relr_non_got_relocs (bfd *input_bfd, struct bfd_link_info *info,
9300
          asection *sec)
9301
0
{
9302
0
  const Elf_Internal_Rela *relocs;
9303
0
  const Elf_Internal_Rela *rel;
9304
0
  const Elf_Internal_Rela *rel_end;
9305
0
  asection *sreloc;
9306
0
  struct elf_aarch64_link_hash_table *htab;
9307
0
  Elf_Internal_Shdr *symtab_hdr;
9308
0
  struct elf_link_hash_entry **sym_hashes;
9309
9310
0
  if (sec->reloc_count == 0)
9311
0
    return true;
9312
0
  if ((sec->flags & (SEC_RELOC | SEC_ALLOC | SEC_DEBUGGING))
9313
0
      != (SEC_RELOC | SEC_ALLOC))
9314
0
    return true;
9315
0
  if (sec->alignment_power == 0)
9316
0
    return true;
9317
0
  if (discarded_section (sec))
9318
0
    return true;
9319
0
  sreloc = elf_section_data (sec)->sreloc;
9320
0
  if (sreloc == NULL)
9321
0
    return true;
9322
0
  htab = elf_aarch64_hash_table (info);
9323
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
9324
0
  sym_hashes = elf_sym_hashes (input_bfd);
9325
0
  relocs = _bfd_elf_link_info_read_relocs (input_bfd, info, sec, NULL, NULL,
9326
0
             info->keep_memory);
9327
0
  BFD_ASSERT (relocs != NULL);
9328
0
  rel_end = relocs + sec->reloc_count;
9329
0
  for (rel = relocs; rel < rel_end; rel++)
9330
0
    {
9331
0
      unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
9332
0
      unsigned int r_type = ELF64_R_TYPE (rel->r_info);
9333
9334
0
      bfd_reloc_code_real_type bfd_r_type
9335
0
  = elf64_aarch64_bfd_reloc_from_type (input_bfd, r_type);
9336
      /* Handle relocs that can become R_AARCH64_RELATIVE,
9337
   but not ones against the GOT as those are handled
9338
   separately per-symbol.  */
9339
0
      if (bfd_r_type != BFD_RELOC_AARCH64_64)
9340
0
  continue;
9341
      /* Can only pack relocation against an aligned address.  */
9342
0
      if (rel->r_offset % 2 != 0)
9343
0
  continue;
9344
9345
0
      struct elf_link_hash_entry *h = NULL;
9346
0
      asection *def_sec = NULL;
9347
0
      bool resolved_to_zero = false;
9348
0
      if (r_symndx < symtab_hdr->sh_info)
9349
0
  {
9350
    /* A local symbol.  */
9351
0
    Elf_Internal_Sym *isym;
9352
0
    isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
9353
0
          input_bfd, r_symndx);
9354
0
    BFD_ASSERT (isym != NULL);
9355
0
    if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
9356
0
      continue;
9357
0
    def_sec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9358
0
  }
9359
0
      else
9360
0
  {
9361
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
9362
0
    while (h->root.type == bfd_link_hash_indirect
9363
0
     || h->root.type == bfd_link_hash_warning)
9364
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
9365
9366
    /* Filter out symbols that cannot have a relative reloc.  */
9367
0
    if (h->dyn_relocs == NULL)
9368
0
      continue;
9369
0
    if (bfd_is_abs_symbol (&h->root))
9370
0
      continue;
9371
0
    if (h->type == STT_GNU_IFUNC)
9372
0
      continue;
9373
9374
0
    if (h->root.type == bfd_link_hash_defined
9375
0
        || h->root.type == bfd_link_hash_defweak)
9376
0
      def_sec = h->root.u.def.section;
9377
0
    resolved_to_zero = UNDEFWEAK_NO_DYNAMIC_RELOC (info, h);
9378
0
  }
9379
0
      if (def_sec != NULL && discarded_section (def_sec))
9380
0
  continue;
9381
      /* Same logic as in elf64_aarch64_final_link_relocate.
9382
   Except conditionals trimmed that cannot result a reltive reloc.  */
9383
0
      if (bfd_link_pic (info)
9384
0
    && (h == NULL
9385
0
        || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9386
0
      && !resolved_to_zero)
9387
0
        || h->root.type != bfd_link_hash_undefweak))
9388
0
  {
9389
0
    if (h != NULL
9390
0
        && h->dynindx != -1
9391
0
        && (!(bfd_link_pie (info) || SYMBOLIC_BIND (info, h))
9392
0
      || !h->def_regular))
9393
0
      continue;
9394
0
    if (!record_relr (htab, sec, rel->r_offset, sreloc))
9395
0
      return false;
9396
0
  }
9397
0
    }
9398
0
  return true;
9399
0
}
9400
9401
static int
9402
cmp_relr_addr (const void *p, const void *q)
9403
0
{
9404
0
  const bfd_vma *a = p;
9405
0
  const bfd_vma *b = q;
9406
0
  return *a < *b ? -1 : *a > *b ? 1 : 0;
9407
0
}
9408
9409
/* Produce a malloc'd sorted array of reloc addresses in htab->relr_sorted.
9410
   Returns false on allocation failure.  */
9411
9412
static bool
9413
sort_relr (struct bfd_link_info *info,
9414
     struct elf_aarch64_link_hash_table *htab)
9415
0
{
9416
0
  if (htab->relr_count == 0)
9417
0
    return true;
9418
9419
0
  bfd_vma *addr = htab->relr_sorted;
9420
0
  if (addr == NULL)
9421
0
    {
9422
0
      addr = bfd_malloc (htab->relr_count * sizeof (*addr));
9423
0
      if (addr == NULL)
9424
0
  return false;
9425
0
      htab->relr_sorted = addr;
9426
0
    }
9427
9428
0
  for (bfd_size_type i = 0; i < htab->relr_count; i++)
9429
0
    {
9430
0
      bfd_vma off = _bfd_elf_section_offset (info->output_bfd, info,
9431
0
               htab->relr[i].sec,
9432
0
               htab->relr[i].off);
9433
0
      addr[i] = htab->relr[i].sec->output_section->vma
9434
0
    + htab->relr[i].sec->output_offset
9435
0
    + off;
9436
0
    }
9437
0
  qsort (addr, htab->relr_count, sizeof (*addr), cmp_relr_addr);
9438
0
  return true;
9439
0
}
9440
9441
/* Size of a relr entry and a relocated location.  */
9442
0
#define RELR_SZ (ARCH_SIZE / 8)
9443
/* Number of consecutive locations a relr bitmap entry references.  */
9444
0
#define RELR_N (ARCH_SIZE - 1)
9445
9446
/* Size .relr.dyn whenever the layout changes, the number of packed
9447
   relocs are unchanged but the packed representation can.  */
9448
9449
bool
9450
elf64_aarch64_size_relative_relocs (struct bfd_link_info *info,
9451
           bool *need_layout)
9452
0
{
9453
0
  struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
9454
0
  asection *srelrdyn = htab->root.srelrdyn;
9455
0
  *need_layout = false;
9456
9457
0
  if (!sort_relr (info, htab))
9458
0
    return false;
9459
0
  bfd_vma *addr = htab->relr_sorted;
9460
9461
0
  BFD_ASSERT (srelrdyn != NULL);
9462
0
  bfd_size_type oldsize = srelrdyn->size;
9463
0
  srelrdyn->size = 0;
9464
0
  for (bfd_size_type i = 0; i < htab->relr_count; )
9465
0
    {
9466
0
      bfd_vma base = addr[i];
9467
0
      i++;
9468
0
      srelrdyn->size += RELR_SZ;
9469
0
      base += RELR_SZ;
9470
0
      for (;;)
9471
0
  {
9472
0
    bfd_size_type start_i = i;
9473
0
    while (i < htab->relr_count
9474
0
     && addr[i] - base < RELR_N * RELR_SZ
9475
0
     && (addr[i] - base) % RELR_SZ == 0)
9476
0
      i++;
9477
0
    if (i == start_i)
9478
0
      break;
9479
0
    srelrdyn->size += RELR_SZ;
9480
0
    base += RELR_N * RELR_SZ;
9481
0
  }
9482
0
    }
9483
0
  if (srelrdyn->size != oldsize)
9484
0
    {
9485
0
      *need_layout = true;
9486
      /* Stop after a few iterations in case the layout does not converge,
9487
   we can do this when the size would shrink.  */
9488
0
      if (htab->relr_layout_iter++ > 5 && srelrdyn->size < oldsize)
9489
0
  {
9490
0
    srelrdyn->size = oldsize;
9491
0
    *need_layout = false;
9492
0
  }
9493
0
    }
9494
0
  return true;
9495
0
}
9496
9497
/* Emit the .relr.dyn section after it is sized and the layout is fixed.  */
9498
9499
bool
9500
elf64_aarch64_finish_relative_relocs (struct bfd_link_info *info)
9501
0
{
9502
0
  struct elf_aarch64_link_hash_table *htab = elf_aarch64_hash_table (info);
9503
0
  asection *srelrdyn = htab->root.srelrdyn;
9504
0
  bfd *dynobj = htab->root.dynobj;
9505
9506
0
  if (srelrdyn == NULL || srelrdyn->size == 0)
9507
0
    return true;
9508
0
  srelrdyn->contents = bfd_alloc (dynobj, srelrdyn->size);
9509
0
  if (srelrdyn->contents == NULL)
9510
0
    return false;
9511
0
  srelrdyn->alloced = 1;
9512
0
  bfd_vma *addr = htab->relr_sorted;
9513
0
  bfd_byte *loc = srelrdyn->contents;
9514
0
  for (bfd_size_type i = 0; i < htab->relr_count; )
9515
0
    {
9516
0
      bfd_vma base = addr[i];
9517
0
      i++;
9518
0
      bfd_put_64 (dynobj, base, loc);
9519
0
      loc += RELR_SZ;
9520
0
      base += RELR_SZ;
9521
0
      for (;;)
9522
0
  {
9523
0
    bfd_vma bits = 0;
9524
0
    while (i < htab->relr_count)
9525
0
      {
9526
0
        bfd_vma delta = addr[i] - base;
9527
0
        if (delta >= RELR_N * RELR_SZ || delta % RELR_SZ != 0)
9528
0
    break;
9529
0
        bits |= (bfd_vma) 1 << (delta / RELR_SZ);
9530
0
        i++;
9531
0
      }
9532
0
    if (bits == 0)
9533
0
      break;
9534
0
    bfd_put_64 (dynobj, (bits << 1) | 1, loc);
9535
0
    loc += RELR_SZ;
9536
0
    base += RELR_N * RELR_SZ;
9537
0
  }
9538
0
    }
9539
0
  free (addr);
9540
0
  htab->relr_sorted = NULL;
9541
  /* Pad any excess with 1's, a do-nothing encoding.  */
9542
0
  while (loc < srelrdyn->contents + srelrdyn->size)
9543
0
    {
9544
0
      bfd_put_64 (dynobj, 1, loc);
9545
0
      loc += RELR_SZ;
9546
0
    }
9547
0
  return true;
9548
0
}
9549
9550
/* This is the most important function of all . Innocuosly named
9551
   though !  */
9552
9553
static bool
9554
elf64_aarch64_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
9555
          struct bfd_link_info *info)
9556
0
{
9557
0
  struct elf_aarch64_link_hash_table *htab;
9558
0
  bfd *dynobj;
9559
0
  asection *s;
9560
0
  bool relocs;
9561
0
  bfd *ibfd;
9562
9563
0
  htab = elf_aarch64_hash_table ((info));
9564
0
  dynobj = htab->root.dynobj;
9565
9566
0
  if (dynobj == NULL)
9567
0
    return true;
9568
9569
0
  if (htab->root.dynamic_sections_created)
9570
0
    {
9571
0
      if (bfd_link_executable (info) && !info->nointerp)
9572
0
  {
9573
0
    s = htab->root.interp;
9574
0
    if (s == NULL)
9575
0
      abort ();
9576
0
    s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9577
0
    s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9578
0
    s->alloced = 1;
9579
0
  }
9580
0
    }
9581
9582
  /* Set up .got offsets for local syms, and space for local dynamic
9583
     relocs.  */
9584
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9585
0
    {
9586
0
      struct elf_aarch64_local_symbol *locals = NULL;
9587
0
      Elf_Internal_Shdr *symtab_hdr;
9588
0
      asection *srel;
9589
0
      unsigned int i;
9590
9591
0
      if (!is_aarch64_elf (ibfd))
9592
0
  continue;
9593
9594
0
      for (s = ibfd->sections; s != NULL; s = s->next)
9595
0
  {
9596
0
    struct elf_dyn_relocs *p;
9597
9598
0
    for (p = (struct elf_dyn_relocs *)
9599
0
         (elf_section_data (s)->local_dynrel); p != NULL; p = p->next)
9600
0
      {
9601
0
        if (discarded_section (p->sec))
9602
0
    {
9603
      /* Input section has been discarded, either because
9604
         it is a copy of a linkonce section or due to
9605
         linker script /DISCARD/, so we'll be discarding
9606
         the relocs too.  */
9607
0
    }
9608
0
        else if (p->count != 0)
9609
0
    {
9610
0
      srel = elf_section_data (p->sec)->sreloc;
9611
0
      srel->size += p->count * RELOC_SIZE (htab);
9612
0
      if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9613
0
        info->flags |= DF_TEXTREL;
9614
0
    }
9615
0
      }
9616
0
  }
9617
9618
0
      locals = elf_aarch64_locals (ibfd);
9619
0
      if (!locals)
9620
0
  continue;
9621
9622
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
9623
0
      srel = htab->root.srelgot;
9624
0
      for (i = 0; i < symtab_hdr->sh_info; i++)
9625
0
  {
9626
0
    locals[i].got_offset = (bfd_vma) - 1;
9627
0
    locals[i].tlsdesc_got_jump_table_offset = (bfd_vma) - 1;
9628
0
    if (locals[i].got_refcount > 0)
9629
0
      {
9630
0
        unsigned got_type = locals[i].got_type;
9631
0
        if (got_type & GOT_TLSDESC_GD)
9632
0
    {
9633
0
      locals[i].tlsdesc_got_jump_table_offset =
9634
0
        (htab->root.sgotplt->size
9635
0
         - aarch64_compute_jump_table_size (htab));
9636
0
      htab->root.sgotplt->size += GOT_ENTRY_SIZE * 2;
9637
0
      locals[i].got_offset = (bfd_vma) - 2;
9638
0
    }
9639
9640
0
        if (got_type & GOT_TLS_GD)
9641
0
    {
9642
0
      locals[i].got_offset = htab->root.sgot->size;
9643
0
      htab->root.sgot->size += GOT_ENTRY_SIZE * 2;
9644
0
    }
9645
9646
0
        if (got_type & GOT_TLS_IE
9647
0
      || got_type & GOT_NORMAL)
9648
0
    {
9649
0
      locals[i].got_offset = htab->root.sgot->size;
9650
0
      htab->root.sgot->size += GOT_ENTRY_SIZE;
9651
0
    }
9652
9653
0
        if (got_type == GOT_UNKNOWN)
9654
0
    {
9655
0
    }
9656
9657
0
        if (bfd_link_pic (info))
9658
0
    {
9659
0
      if (got_type & GOT_TLSDESC_GD)
9660
0
        {
9661
0
          htab->root.srelplt->size += RELOC_SIZE (htab);
9662
          /* Note RELOC_COUNT not incremented here! */
9663
0
          htab->root.tlsdesc_plt = (bfd_vma) - 1;
9664
0
        }
9665
9666
0
      if (got_type & GOT_TLS_GD)
9667
0
        htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
9668
9669
0
      if (got_type & GOT_TLS_IE
9670
0
          || got_type & GOT_NORMAL)
9671
0
        htab->root.srelgot->size += RELOC_SIZE (htab);
9672
0
    }
9673
0
      }
9674
0
    else
9675
0
      {
9676
0
        locals[i].got_refcount = (bfd_vma) - 1;
9677
0
      }
9678
0
  }
9679
0
    }
9680
9681
9682
  /* Allocate global sym .plt and .got entries, and space for global
9683
     sym dynamic relocs.  */
9684
0
  elf_link_hash_traverse (&htab->root, elf64_aarch64_allocate_dynrelocs,
9685
0
        info);
9686
9687
  /* Allocate global ifunc sym .plt and .got entries, and space for global
9688
     ifunc sym dynamic relocs.  */
9689
0
  elf_link_hash_traverse (&htab->root, elf64_aarch64_allocate_ifunc_dynrelocs,
9690
0
        info);
9691
9692
  /* Allocate .plt and .got entries, and space for local ifunc symbols.  */
9693
0
  htab_traverse (htab->loc_hash_table,
9694
0
     elf64_aarch64_allocate_local_ifunc_dynrelocs,
9695
0
     info);
9696
9697
  /* For every jump slot reserved in the sgotplt, reloc_count is
9698
     incremented.  However, when we reserve space for TLS descriptors,
9699
     it's not incremented, so in order to compute the space reserved
9700
     for them, it suffices to multiply the reloc count by the jump
9701
     slot size.  */
9702
9703
0
  if (htab->root.srelplt)
9704
0
    htab->sgotplt_jump_table_size = aarch64_compute_jump_table_size (htab);
9705
9706
0
  if (htab->root.tlsdesc_plt)
9707
0
    {
9708
0
      if (htab->root.splt->size == 0)
9709
0
  htab->root.splt->size += htab->plt_header_size;
9710
9711
      /* If we're not using lazy TLS relocations, don't generate the
9712
   GOT and PLT entry required.  */
9713
0
      if ((info->flags & DF_BIND_NOW))
9714
0
  htab->root.tlsdesc_plt = 0;
9715
0
      else
9716
0
  {
9717
0
    htab->root.tlsdesc_plt = htab->root.splt->size;
9718
0
    htab->root.splt->size += htab->tlsdesc_plt_entry_size;
9719
9720
0
    htab->root.tlsdesc_got = htab->root.sgot->size;
9721
0
    htab->root.sgot->size += GOT_ENTRY_SIZE;
9722
0
  }
9723
0
    }
9724
9725
  /* Record the relative relocations that will be packed and undo the
9726
     size allocation for them in .rela.*. The size of .relr.dyn will be
9727
     computed later iteratively since it depends on the final layout.  */
9728
0
  if (info->enable_dt_relr && !bfd_link_relocatable (info))
9729
0
    {
9730
0
      elf_link_hash_traverse (&htab->root, record_relr_dyn_got_relocs, info);
9731
9732
0
      for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9733
0
  {
9734
0
    if (!is_aarch64_elf (ibfd))
9735
0
      continue;
9736
9737
0
    for (s = ibfd->sections; s != NULL; s = s->next)
9738
0
      if (!record_relr_non_got_relocs (ibfd, info, s))
9739
0
        return false;
9740
9741
0
    if (!record_relr_local_got_relocs (ibfd, info))
9742
0
      return false;
9743
0
  }
9744
0
    }
9745
9746
  /* Init mapping symbols information to use later to distingush between
9747
     code and data while scanning for errata.  */
9748
0
  if (htab->fix_erratum_835769 || htab->fix_erratum_843419)
9749
0
    for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9750
0
      {
9751
0
  if (!is_aarch64_elf (ibfd))
9752
0
    continue;
9753
0
  bfd_elf64_aarch64_init_maps (ibfd);
9754
0
      }
9755
9756
  /* We now have determined the sizes of the various dynamic sections.
9757
     Allocate memory for them.  */
9758
0
  relocs = false;
9759
0
  for (s = dynobj->sections; s != NULL; s = s->next)
9760
0
    {
9761
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
9762
0
  continue;
9763
9764
0
      if (s == htab->root.splt
9765
0
    || s == htab->root.sgot
9766
0
    || s == htab->root.sgotplt
9767
0
    || s == htab->root.iplt
9768
0
    || s == htab->root.igotplt
9769
0
    || s == htab->root.sdynbss
9770
0
    || s == htab->root.sdynrelro)
9771
0
  {
9772
    /* Strip this section if we don't need it; see the
9773
       comment below.  */
9774
0
  }
9775
0
      else if (startswith (bfd_section_name (s), ".rela"))
9776
0
  {
9777
0
    if (s->size != 0 && s != htab->root.srelplt)
9778
0
      relocs = true;
9779
9780
    /* We use the reloc_count field as a counter if we need
9781
       to copy relocs into the output file.  */
9782
0
    if (s != htab->root.srelplt)
9783
0
      s->reloc_count = 0;
9784
0
  }
9785
0
      else if (s == htab->root.srelrdyn)
9786
0
  {
9787
    /* Remove .relr.dyn based on relr_count, not size, since
9788
       it is not sized yet.  */
9789
0
    if (htab->relr_count == 0)
9790
0
      s->flags |= SEC_EXCLUDE;
9791
0
    else
9792
      /* Force dynamic tags for relocs even if there are no
9793
         .rela* relocs, required for setting DT_TEXTREL.  */
9794
0
      relocs = true;
9795
    /* Allocate contents later.  */
9796
0
    continue;
9797
0
  }
9798
0
      else
9799
0
  {
9800
    /* It's not one of our sections, so don't allocate space.  */
9801
0
    continue;
9802
0
  }
9803
9804
0
      if (s->size == 0)
9805
0
  {
9806
    /* If we don't need this section, strip it from the
9807
       output file.  This is mostly to handle .rela.bss and
9808
       .rela.plt.  We must create both sections in
9809
       create_dynamic_sections, because they must be created
9810
       before the linker maps input sections to output
9811
       sections.  The linker does that before
9812
       adjust_dynamic_symbol is called, and it is that
9813
       function which decides whether anything needs to go
9814
       into these sections.  */
9815
0
    s->flags |= SEC_EXCLUDE;
9816
0
    continue;
9817
0
  }
9818
9819
0
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
9820
0
  continue;
9821
9822
      /* Allocate memory for the section contents.  We use bfd_zalloc
9823
   here in case unused entries are not reclaimed before the
9824
   section's contents are written out.  This should not happen,
9825
   but this way if it does, we get a R_AARCH64_NONE reloc instead
9826
   of garbage.  */
9827
0
      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
9828
0
      if (s->contents == NULL)
9829
0
  return false;
9830
0
      s->alloced = 1;
9831
0
    }
9832
9833
0
  if (htab->root.dynamic_sections_created)
9834
0
    {
9835
      /* Add some entries to the .dynamic section.  We fill in the
9836
   values later, in elf64_aarch64_finish_dynamic_sections, but we
9837
   must add the entries now so that we get the correct size for
9838
   the .dynamic section.  The DT_DEBUG entry is filled in by the
9839
   dynamic linker and used by the debugger.  */
9840
0
#define add_dynamic_entry(TAG, VAL)     \
9841
0
      _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9842
9843
0
      if (!_bfd_elf_add_dynamic_tags (output_bfd, info, relocs))
9844
0
  return false;
9845
9846
0
      if (htab->root.splt->size != 0)
9847
0
  {
9848
0
    if (htab->variant_pcs
9849
0
        && !add_dynamic_entry (DT_AARCH64_VARIANT_PCS, 0))
9850
0
      return false;
9851
9852
0
    aarch64_plt_type plt_type
9853
0
      = elf_aarch64_tdata (output_bfd)->sw_protections.plt_type;
9854
0
    if (plt_type == PLT_BTI_PAC
9855
0
        && (!add_dynamic_entry (DT_AARCH64_BTI_PLT, 0)
9856
0
      || !add_dynamic_entry (DT_AARCH64_PAC_PLT, 0)))
9857
0
      return false;
9858
9859
0
    else if (plt_type == PLT_BTI
9860
0
       && !add_dynamic_entry (DT_AARCH64_BTI_PLT, 0))
9861
0
      return false;
9862
9863
0
    else if (plt_type == PLT_PAC
9864
0
       && !add_dynamic_entry (DT_AARCH64_PAC_PLT, 0))
9865
0
      return false;
9866
0
  }
9867
9868
0
      if (is_aarch64_elf (output_bfd)
9869
0
    && htab->memtag_opts.memtag_mode != AARCH64_MEMTAG_MODE_NONE
9870
0
    && !add_dynamic_entry (DT_AARCH64_MEMTAG_MODE,
9871
0
         htab->memtag_opts.memtag_mode == AARCH64_MEMTAG_MODE_ASYNC))
9872
0
  return false;
9873
9874
0
      if (is_aarch64_elf (output_bfd)
9875
0
    && htab->memtag_opts.memtag_stack == 1
9876
0
    && !add_dynamic_entry (DT_AARCH64_MEMTAG_STACK,
9877
0
         htab->memtag_opts.memtag_stack == 1))
9878
0
  return false;
9879
0
    }
9880
9881
0
#undef add_dynamic_entry
9882
9883
0
  return true;
9884
0
}
9885
9886
static inline void
9887
elf_aarch64_update_plt_entry (bfd *output_bfd,
9888
            bfd_reloc_code_real_type r_type,
9889
            bfd_byte *plt_entry, bfd_vma value)
9890
0
{
9891
0
  reloc_howto_type *howto = elf64_aarch64_howto_from_bfd_reloc (r_type);
9892
9893
  /* FIXME: We should check the return value from this function call.  */
9894
0
  (void) _bfd_aarch64_elf_put_addend (output_bfd, plt_entry, r_type, howto, value);
9895
0
}
9896
9897
static void
9898
elf64_aarch64_create_small_pltn_entry (struct elf_link_hash_entry *h,
9899
               struct elf_aarch64_link_hash_table
9900
               *htab, bfd *output_bfd,
9901
               struct bfd_link_info *info)
9902
0
{
9903
0
  bfd_byte *plt_entry;
9904
0
  bfd_vma plt_index;
9905
0
  bfd_vma got_offset;
9906
0
  bfd_vma gotplt_entry_address;
9907
0
  bfd_vma plt_entry_address;
9908
0
  Elf_Internal_Rela rela;
9909
0
  bfd_byte *loc;
9910
0
  asection *plt, *gotplt, *relplt;
9911
9912
  /* When building a static executable, use .iplt, .igot.plt and
9913
     .rela.iplt sections for STT_GNU_IFUNC symbols.  */
9914
0
  if (htab->root.splt != NULL)
9915
0
    {
9916
0
      plt = htab->root.splt;
9917
0
      gotplt = htab->root.sgotplt;
9918
0
      relplt = htab->root.srelplt;
9919
0
    }
9920
0
  else
9921
0
    {
9922
0
      plt = htab->root.iplt;
9923
0
      gotplt = htab->root.igotplt;
9924
0
      relplt = htab->root.irelplt;
9925
0
    }
9926
9927
  /* Get the index in the procedure linkage table which
9928
     corresponds to this symbol.  This is the index of this symbol
9929
     in all the symbols for which we are making plt entries.  The
9930
     first entry in the procedure linkage table is reserved.
9931
9932
     Get the offset into the .got table of the entry that
9933
     corresponds to this function.  Each .got entry is GOT_ENTRY_SIZE
9934
     bytes. The first three are reserved for the dynamic linker.
9935
9936
     For static executables, we don't reserve anything.  */
9937
9938
0
  if (plt == htab->root.splt)
9939
0
    {
9940
0
      plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
9941
0
      got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
9942
0
    }
9943
0
  else
9944
0
    {
9945
0
      plt_index = h->plt.offset / htab->plt_entry_size;
9946
0
      got_offset = plt_index * GOT_ENTRY_SIZE;
9947
0
    }
9948
9949
0
  plt_entry = plt->contents + h->plt.offset;
9950
0
  plt_entry_address = plt->output_section->vma
9951
0
    + plt->output_offset + h->plt.offset;
9952
0
  gotplt_entry_address = gotplt->output_section->vma +
9953
0
    gotplt->output_offset + got_offset;
9954
9955
  /* Copy in the boiler-plate for the PLTn entry.  */
9956
0
  memcpy (plt_entry, htab->plt_entry, htab->plt_entry_size);
9957
9958
  /* Allow for any delta (such as a BTI instruction) before the common
9959
     sequence.  */
9960
0
  plt_entry += htab->plt_entry_delta;
9961
9962
  /* Fill in the top 21 bits for this: ADRP x16, PLT_GOT + n * 8.
9963
     ADRP:   ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
9964
0
  elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADR_HI21_PCREL,
9965
0
        plt_entry,
9966
0
        PG (gotplt_entry_address) -
9967
0
        PG (plt_entry_address));
9968
9969
  /* Fill in the lo12 bits for the load from the pltgot.  */
9970
0
  elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_LDST64_LO12,
9971
0
        plt_entry + 4,
9972
0
        PG_OFFSET (gotplt_entry_address));
9973
9974
  /* Fill in the lo12 bits for the add from the pltgot entry.  */
9975
0
  elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADD_LO12,
9976
0
        plt_entry + 8,
9977
0
        PG_OFFSET (gotplt_entry_address));
9978
9979
  /* All the GOTPLT Entries are essentially initialized to PLT0.  */
9980
0
  bfd_put_64 (output_bfd,
9981
0
        plt->output_section->vma + plt->output_offset,
9982
0
        gotplt->contents + got_offset);
9983
9984
0
  rela.r_offset = gotplt_entry_address;
9985
9986
0
  if (h->dynindx == -1
9987
0
      || ((bfd_link_executable (info)
9988
0
     || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9989
0
    && h->def_regular
9990
0
    && h->type == STT_GNU_IFUNC))
9991
0
    {
9992
      /* If an STT_GNU_IFUNC symbol is locally defined, generate
9993
   R_AARCH64_IRELATIVE instead of R_AARCH64_JUMP_SLOT.  */
9994
0
      rela.r_info = ELF64_R_INFO (0, AARCH64_R (IRELATIVE));
9995
0
      rela.r_addend = (h->root.u.def.value
9996
0
           + h->root.u.def.section->output_section->vma
9997
0
           + h->root.u.def.section->output_offset);
9998
0
    }
9999
0
  else
10000
0
    {
10001
      /* Fill in the entry in the .rela.plt section.  */
10002
0
      rela.r_info = ELF64_R_INFO (h->dynindx, AARCH64_R (JUMP_SLOT));
10003
0
      rela.r_addend = 0;
10004
0
    }
10005
10006
  /* Compute the relocation entry to used based on PLT index and do
10007
     not adjust reloc_count. The reloc_count has already been adjusted
10008
     to account for this entry.  */
10009
0
  loc = relplt->contents + plt_index * RELOC_SIZE (htab);
10010
0
  bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
10011
0
}
10012
10013
/* Size sections even though they're not dynamic.  We use it to setup
10014
   _TLS_MODULE_BASE_, if needed.  */
10015
10016
static bool
10017
elf64_aarch64_early_size_sections (bfd *output_bfd,
10018
           struct bfd_link_info *info)
10019
0
{
10020
0
  asection *tls_sec;
10021
10022
0
  if (bfd_link_relocatable (info))
10023
0
    return true;
10024
10025
0
  tls_sec = elf_hash_table (info)->tls_sec;
10026
10027
0
  if (tls_sec)
10028
0
    {
10029
0
      struct elf_link_hash_entry *tlsbase;
10030
10031
0
      tlsbase = elf_link_hash_lookup (elf_hash_table (info),
10032
0
              "_TLS_MODULE_BASE_", true, true, false);
10033
10034
0
      if (tlsbase)
10035
0
  {
10036
0
    struct bfd_link_hash_entry *h = NULL;
10037
0
    elf_backend_data *bed = get_elf_backend_data (output_bfd);
10038
10039
0
    if (!(_bfd_generic_link_add_one_symbol
10040
0
    (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
10041
0
     tls_sec, 0, NULL, false, bed->collect, &h)))
10042
0
      return false;
10043
10044
0
    tlsbase->type = STT_TLS;
10045
0
    tlsbase = (struct elf_link_hash_entry *) h;
10046
0
    tlsbase->def_regular = 1;
10047
0
    tlsbase->other = STV_HIDDEN;
10048
0
    (*bed->elf_backend_hide_symbol) (info, tlsbase, true);
10049
0
  }
10050
0
    }
10051
10052
0
  return true;
10053
0
}
10054
10055
/* Finish up dynamic symbol handling.  We set the contents of various
10056
   dynamic sections here.  */
10057
10058
static bool
10059
elf64_aarch64_finish_dynamic_symbol (bfd *output_bfd,
10060
             struct bfd_link_info *info,
10061
             struct elf_link_hash_entry *h,
10062
             Elf_Internal_Sym *sym)
10063
0
{
10064
0
  struct elf_aarch64_link_hash_table *htab;
10065
0
  htab = elf_aarch64_hash_table (info);
10066
10067
0
  if (h->plt.offset != (bfd_vma) - 1)
10068
0
    {
10069
0
      asection *plt, *gotplt, *relplt;
10070
10071
      /* This symbol has an entry in the procedure linkage table.  Set
10072
   it up.  */
10073
10074
      /* When building a static executable, use .iplt, .igot.plt and
10075
   .rela.iplt sections for STT_GNU_IFUNC symbols.  */
10076
0
      if (htab->root.splt != NULL)
10077
0
  {
10078
0
    plt = htab->root.splt;
10079
0
    gotplt = htab->root.sgotplt;
10080
0
    relplt = htab->root.srelplt;
10081
0
  }
10082
0
      else
10083
0
  {
10084
0
    plt = htab->root.iplt;
10085
0
    gotplt = htab->root.igotplt;
10086
0
    relplt = htab->root.irelplt;
10087
0
  }
10088
10089
      /* This symbol has an entry in the procedure linkage table.  Set
10090
   it up.  */
10091
0
      if ((h->dynindx == -1
10092
0
     && !((h->forced_local || bfd_link_executable (info))
10093
0
    && h->def_regular
10094
0
    && h->type == STT_GNU_IFUNC))
10095
0
    || plt == NULL
10096
0
    || gotplt == NULL
10097
0
    || relplt == NULL)
10098
0
  abort ();
10099
10100
0
      elf64_aarch64_create_small_pltn_entry (h, htab, output_bfd, info);
10101
0
      if (!h->def_regular)
10102
0
  {
10103
    /* Mark the symbol as undefined, rather than as defined in
10104
       the .plt section.  */
10105
0
    sym->st_shndx = SHN_UNDEF;
10106
    /* If the symbol is weak we need to clear the value.
10107
       Otherwise, the PLT entry would provide a definition for
10108
       the symbol even if the symbol wasn't defined anywhere,
10109
       and so the symbol would never be NULL.  Leave the value if
10110
       there were any relocations where pointer equality matters
10111
       (this is a clue for the dynamic linker, to make function
10112
       pointer comparisons work between an application and shared
10113
       library).  */
10114
0
    if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
10115
0
      sym->st_value = 0;
10116
0
  }
10117
0
    }
10118
10119
0
  if (h->got.offset != (bfd_vma) - 1
10120
0
      && elf_aarch64_hash_entry (h)->got_type == GOT_NORMAL
10121
      /* Undefined weak symbol in static PIE resolves to 0 without
10122
   any dynamic relocations.  */
10123
0
      && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
10124
0
    {
10125
0
      Elf_Internal_Rela rela;
10126
0
      bfd_byte *loc;
10127
10128
      /* This symbol has an entry in the global offset table.  Set it
10129
   up.  */
10130
0
      if (htab->root.sgot == NULL || htab->root.srelgot == NULL)
10131
0
  abort ();
10132
10133
0
      rela.r_offset = (htab->root.sgot->output_section->vma
10134
0
           + htab->root.sgot->output_offset
10135
0
           + (h->got.offset & ~(bfd_vma) 1));
10136
10137
0
      if (h->def_regular
10138
0
    && h->type == STT_GNU_IFUNC)
10139
0
  {
10140
0
    if (bfd_link_pic (info))
10141
0
      {
10142
        /* Generate R_AARCH64_GLOB_DAT.  */
10143
0
        goto do_glob_dat;
10144
0
      }
10145
0
    else
10146
0
      {
10147
0
        asection *plt;
10148
10149
0
        if (!h->pointer_equality_needed)
10150
0
    abort ();
10151
10152
        /* For non-shared object, we can't use .got.plt, which
10153
     contains the real function address if we need pointer
10154
     equality.  We load the GOT entry with the PLT entry.  */
10155
0
        plt = htab->root.splt ? htab->root.splt : htab->root.iplt;
10156
0
        bfd_put_64 (output_bfd, (plt->output_section->vma
10157
0
               + plt->output_offset
10158
0
               + h->plt.offset),
10159
0
        htab->root.sgot->contents
10160
0
        + (h->got.offset & ~(bfd_vma) 1));
10161
0
        return true;
10162
0
      }
10163
0
  }
10164
0
      else if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
10165
0
  {
10166
0
    if (!(h->def_regular || ELF_COMMON_DEF_P (h)))
10167
0
      return false;
10168
0
    BFD_ASSERT ((h->got.offset & 1) != 0);
10169
    /* Don't emit relative relocs if they are packed.  */
10170
0
    if (info->enable_dt_relr)
10171
0
      goto skip_got_reloc;
10172
0
    rela.r_info = ELF64_R_INFO (0, AARCH64_R (RELATIVE));
10173
0
    rela.r_addend = (h->root.u.def.value
10174
0
         + h->root.u.def.section->output_section->vma
10175
0
         + h->root.u.def.section->output_offset);
10176
0
  }
10177
0
      else
10178
0
  {
10179
0
  do_glob_dat:
10180
0
    BFD_ASSERT ((h->got.offset & 1) == 0);
10181
0
    bfd_put_64 (output_bfd, (bfd_vma) 0,
10182
0
          htab->root.sgot->contents + h->got.offset);
10183
0
    rela.r_info = ELF64_R_INFO (h->dynindx, AARCH64_R (GLOB_DAT));
10184
0
    rela.r_addend = 0;
10185
0
  }
10186
10187
0
      loc = htab->root.srelgot->contents;
10188
0
      loc += htab->root.srelgot->reloc_count++ * RELOC_SIZE (htab);
10189
0
      bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
10190
0
    }
10191
0
skip_got_reloc:
10192
10193
0
  if (h->needs_copy)
10194
0
    {
10195
0
      Elf_Internal_Rela rela;
10196
0
      asection *s;
10197
0
      bfd_byte *loc;
10198
10199
      /* This symbol needs a copy reloc.  Set it up.  */
10200
0
      if (h->dynindx == -1
10201
0
    || (h->root.type != bfd_link_hash_defined
10202
0
        && h->root.type != bfd_link_hash_defweak)
10203
0
    || htab->root.srelbss == NULL)
10204
0
  abort ();
10205
10206
0
      rela.r_offset = (h->root.u.def.value
10207
0
           + h->root.u.def.section->output_section->vma
10208
0
           + h->root.u.def.section->output_offset);
10209
0
      rela.r_info = ELF64_R_INFO (h->dynindx, AARCH64_R (COPY));
10210
0
      rela.r_addend = 0;
10211
0
      if (h->root.u.def.section == htab->root.sdynrelro)
10212
0
  s = htab->root.sreldynrelro;
10213
0
      else
10214
0
  s = htab->root.srelbss;
10215
0
      loc = s->contents + s->reloc_count++ * RELOC_SIZE (htab);
10216
0
      bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
10217
0
    }
10218
10219
  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  SYM may
10220
     be NULL for local symbols.  */
10221
0
  if (sym != NULL
10222
0
      && (h == elf_hash_table (info)->hdynamic
10223
0
    || h == elf_hash_table (info)->hgot))
10224
0
    sym->st_shndx = SHN_ABS;
10225
10226
0
  return true;
10227
0
}
10228
10229
/* Finish up local dynamic symbol handling.  We set the contents of
10230
   various dynamic sections here.  */
10231
10232
static int
10233
elf64_aarch64_finish_local_dynamic_symbol (void **slot, void *inf)
10234
0
{
10235
0
  struct elf_link_hash_entry *h
10236
0
    = (struct elf_link_hash_entry *) *slot;
10237
0
  struct bfd_link_info *info
10238
0
    = (struct bfd_link_info *) inf;
10239
10240
0
  return elf64_aarch64_finish_dynamic_symbol (info->output_bfd,
10241
0
                info, h, NULL);
10242
0
}
10243
10244
static void
10245
elf64_aarch64_init_small_plt0_entry (bfd *output_bfd ATTRIBUTE_UNUSED,
10246
             struct elf_aarch64_link_hash_table
10247
             *htab)
10248
0
{
10249
  /* Fill in PLT0. Fixme:RR Note this doesn't distinguish between
10250
     small and large plts and at the minute just generates
10251
     the small PLT.  */
10252
10253
  /* PLT0 of the small PLT looks like this in ELF64 -
10254
     stp x16, x30, [sp, #-16]!    // Save the reloc and lr on stack.
10255
     adrp x16, PLT_GOT + 16   // Get the page base of the GOTPLT
10256
     ldr  x17, [x16, #:lo12:PLT_GOT+16] // Load the address of the
10257
          // symbol resolver
10258
     add  x16, x16, #:lo12:PLT_GOT+16   // Load the lo12 bits of the
10259
          // GOTPLT entry for this.
10260
     br   x17
10261
     PLT0 will be slightly different in ELF32 due to different got entry
10262
     size.  */
10263
0
  bfd_vma plt_got_2nd_ent;  /* Address of GOT[2].  */
10264
0
  bfd_vma plt_base;
10265
10266
10267
0
  memcpy (htab->root.splt->contents, htab->plt0_entry,
10268
0
    htab->plt_header_size);
10269
10270
  /* PR 26312: Explicitly set the sh_entsize to 0 so that
10271
     consumers do not think that the section contains fixed
10272
     sized objects.  */
10273
0
  elf_section_data (htab->root.splt->output_section)->this_hdr.sh_entsize = 0;
10274
10275
0
  plt_got_2nd_ent = (htab->root.sgotplt->output_section->vma
10276
0
      + htab->root.sgotplt->output_offset
10277
0
      + GOT_ENTRY_SIZE * 2);
10278
10279
0
  plt_base = htab->root.splt->output_section->vma +
10280
0
    htab->root.splt->output_offset;
10281
10282
  /* First instruction in BTI enabled PLT stub is a BTI
10283
     instruction so skip it.  */
10284
0
  bfd_byte *plt0_entry = htab->root.splt->contents;
10285
0
  if (elf_aarch64_tdata (output_bfd)->sw_protections.plt_type & PLT_BTI)
10286
0
    plt0_entry = plt0_entry + 4;
10287
10288
  /* Fill in the top 21 bits for this: ADRP x16, PLT_GOT + n * 8.
10289
     ADRP:   ((PG(S+A)-PG(P)) >> 12) & 0x1fffff */
10290
0
  elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADR_HI21_PCREL,
10291
0
        plt0_entry + 4,
10292
0
        PG (plt_got_2nd_ent) - PG (plt_base + 4));
10293
10294
0
  elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_LDST64_LO12,
10295
0
        plt0_entry + 8,
10296
0
        PG_OFFSET (plt_got_2nd_ent));
10297
10298
0
  elf_aarch64_update_plt_entry (output_bfd, BFD_RELOC_AARCH64_ADD_LO12,
10299
0
        plt0_entry + 12,
10300
0
        PG_OFFSET (plt_got_2nd_ent));
10301
0
}
10302
10303
static bool
10304
elf64_aarch64_finish_dynamic_sections (bfd *output_bfd,
10305
               struct bfd_link_info *info,
10306
               bfd_byte *buf ATTRIBUTE_UNUSED)
10307
0
{
10308
0
  struct elf_aarch64_link_hash_table *htab;
10309
0
  bfd *dynobj;
10310
0
  asection *sdyn;
10311
10312
0
  htab = elf_aarch64_hash_table (info);
10313
0
  dynobj = htab->root.dynobj;
10314
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
10315
10316
0
  if (htab->root.dynamic_sections_created)
10317
0
    {
10318
0
      Elf64_External_Dyn *dyncon, *dynconend;
10319
10320
0
      if (sdyn == NULL || htab->root.sgot == NULL)
10321
0
  abort ();
10322
10323
0
      dyncon = (Elf64_External_Dyn *) sdyn->contents;
10324
0
      dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
10325
0
      for (; dyncon < dynconend; dyncon++)
10326
0
  {
10327
0
    Elf_Internal_Dyn dyn;
10328
0
    asection *s;
10329
10330
0
    bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
10331
10332
0
    switch (dyn.d_tag)
10333
0
      {
10334
0
      default:
10335
0
        continue;
10336
10337
0
      case DT_PLTGOT:
10338
0
        s = htab->root.sgotplt;
10339
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10340
0
        break;
10341
10342
0
      case DT_JMPREL:
10343
0
        s = htab->root.srelplt;
10344
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10345
0
        break;
10346
10347
0
      case DT_PLTRELSZ:
10348
0
        s = htab->root.srelplt;
10349
0
        dyn.d_un.d_val = s->size;
10350
0
        break;
10351
10352
0
      case DT_TLSDESC_PLT:
10353
0
        s = htab->root.splt;
10354
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
10355
0
    + htab->root.tlsdesc_plt;
10356
0
        break;
10357
10358
0
      case DT_TLSDESC_GOT:
10359
0
        s = htab->root.sgot;
10360
0
        BFD_ASSERT (htab->root.tlsdesc_got != (bfd_vma)-1);
10361
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
10362
0
    + htab->root.tlsdesc_got;
10363
0
        break;
10364
0
      }
10365
10366
0
    bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
10367
0
  }
10368
10369
0
    }
10370
10371
  /* Fill in the special first entry in the procedure linkage table.  */
10372
0
  if (htab->root.splt && htab->root.splt->size > 0)
10373
0
    {
10374
0
      elf64_aarch64_init_small_plt0_entry (output_bfd, htab);
10375
10376
0
      if (htab->root.tlsdesc_plt && !(info->flags & DF_BIND_NOW))
10377
0
  {
10378
0
    BFD_ASSERT (htab->root.tlsdesc_got != (bfd_vma)-1);
10379
0
    bfd_put_64 (output_bfd, (bfd_vma) 0,
10380
0
          htab->root.sgot->contents + htab->root.tlsdesc_got);
10381
10382
0
    const bfd_byte *entry = elf64_aarch64_tlsdesc_small_plt_entry;
10383
0
    htab->tlsdesc_plt_entry_size = PLT_TLSDESC_ENTRY_SIZE;
10384
10385
0
    aarch64_plt_type plt_type
10386
0
      = elf_aarch64_tdata (output_bfd)->sw_protections.plt_type;
10387
0
    if (plt_type & PLT_BTI)
10388
0
      entry = elf64_aarch64_tlsdesc_small_plt_bti_entry;
10389
10390
0
    memcpy (htab->root.splt->contents + htab->root.tlsdesc_plt,
10391
0
      entry, htab->tlsdesc_plt_entry_size);
10392
10393
0
    {
10394
0
      bfd_vma adrp1_addr =
10395
0
        htab->root.splt->output_section->vma
10396
0
        + htab->root.splt->output_offset
10397
0
        + htab->root.tlsdesc_plt + 4;
10398
10399
0
      bfd_vma adrp2_addr = adrp1_addr + 4;
10400
10401
0
      bfd_vma got_addr =
10402
0
        htab->root.sgot->output_section->vma
10403
0
        + htab->root.sgot->output_offset;
10404
10405
0
      bfd_vma pltgot_addr =
10406
0
        htab->root.sgotplt->output_section->vma
10407
0
        + htab->root.sgotplt->output_offset;
10408
10409
0
      bfd_vma dt_tlsdesc_got = got_addr + htab->root.tlsdesc_got;
10410
10411
0
      bfd_byte *plt_entry =
10412
0
        htab->root.splt->contents + htab->root.tlsdesc_plt;
10413
10414
     /* First instruction in BTI enabled PLT stub is a BTI
10415
        instruction so skip it.  */
10416
0
      if (plt_type & PLT_BTI)
10417
0
        {
10418
0
    plt_entry = plt_entry + 4;
10419
0
    adrp1_addr = adrp1_addr + 4;
10420
0
    adrp2_addr = adrp2_addr + 4;
10421
0
        }
10422
10423
      /* adrp x2, DT_TLSDESC_GOT */
10424
0
      elf_aarch64_update_plt_entry (output_bfd,
10425
0
            BFD_RELOC_AARCH64_ADR_HI21_PCREL,
10426
0
            plt_entry + 4,
10427
0
            (PG (dt_tlsdesc_got)
10428
0
             - PG (adrp1_addr)));
10429
10430
      /* adrp x3, 0 */
10431
0
      elf_aarch64_update_plt_entry (output_bfd,
10432
0
            BFD_RELOC_AARCH64_ADR_HI21_PCREL,
10433
0
            plt_entry + 8,
10434
0
            (PG (pltgot_addr)
10435
0
             - PG (adrp2_addr)));
10436
10437
      /* ldr x2, [x2, #0] */
10438
0
      elf_aarch64_update_plt_entry (output_bfd,
10439
0
            BFD_RELOC_AARCH64_LDST64_LO12,
10440
0
            plt_entry + 12,
10441
0
            PG_OFFSET (dt_tlsdesc_got));
10442
10443
      /* add x3, x3, 0 */
10444
0
      elf_aarch64_update_plt_entry (output_bfd,
10445
0
            BFD_RELOC_AARCH64_ADD_LO12,
10446
0
            plt_entry + 16,
10447
0
            PG_OFFSET (pltgot_addr));
10448
0
    }
10449
0
  }
10450
0
    }
10451
10452
0
  if (htab->root.sgotplt)
10453
0
    {
10454
0
      if (bfd_is_abs_section (htab->root.sgotplt->output_section))
10455
0
  {
10456
0
    _bfd_error_handler
10457
0
      (_("discarded output section: `%pA'"), htab->root.sgotplt);
10458
0
    return false;
10459
0
  }
10460
10461
      /* Fill in the first three entries in the global offset table.  */
10462
0
      if (htab->root.sgotplt->size > 0)
10463
0
  {
10464
0
    bfd_put_64 (output_bfd, (bfd_vma) 0, htab->root.sgotplt->contents);
10465
10466
    /* Write GOT[1] and GOT[2], needed for the dynamic linker.  */
10467
0
    bfd_put_64 (output_bfd,
10468
0
          (bfd_vma) 0,
10469
0
          htab->root.sgotplt->contents + GOT_ENTRY_SIZE);
10470
0
    bfd_put_64 (output_bfd,
10471
0
          (bfd_vma) 0,
10472
0
          htab->root.sgotplt->contents + GOT_ENTRY_SIZE * 2);
10473
0
  }
10474
10475
0
      if (htab->root.sgot)
10476
0
  {
10477
0
    if (htab->root.sgot->size > 0)
10478
0
      {
10479
0
        bfd_vma addr =
10480
0
    sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0;
10481
0
        bfd_put_64 (output_bfd, addr, htab->root.sgot->contents);
10482
0
      }
10483
0
  }
10484
10485
0
      elf_section_data (htab->root.sgotplt->output_section)->
10486
0
  this_hdr.sh_entsize = GOT_ENTRY_SIZE;
10487
0
    }
10488
10489
0
  if (htab->root.sgot && htab->root.sgot->size > 0)
10490
0
    elf_section_data (htab->root.sgot->output_section)->this_hdr.sh_entsize
10491
0
      = GOT_ENTRY_SIZE;
10492
10493
  /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols.  */
10494
0
  htab_traverse (htab->loc_hash_table,
10495
0
     elf64_aarch64_finish_local_dynamic_symbol,
10496
0
     info);
10497
10498
0
  return true;
10499
0
}
10500
10501
/* Check if BTI-enabled (and/or PAC-enabled) PLTs are needed.
10502
   Returns the type needed, and whether DF_1_PIE is set via tdata is_pie.  */
10503
static aarch64_plt_type
10504
get_plt_type (bfd *abfd)
10505
2
{
10506
2
  aarch64_plt_type ret = PLT_NORMAL;
10507
2
  bfd_byte *contents, *extdyn, *extdynend;
10508
2
  asection *sec = bfd_get_section_by_name (abfd, ".dynamic");
10509
2
  if (!sec
10510
0
      || (sec->flags & SEC_HAS_CONTENTS) == 0
10511
0
      || sec->size < sizeof (Elf64_External_Dyn)
10512
0
      || !bfd_malloc_and_get_section (abfd, sec, &contents))
10513
2
    return ret;
10514
0
  extdyn = contents;
10515
0
  extdynend = contents + sec->size - sizeof (Elf64_External_Dyn);
10516
0
  for (; extdyn <= extdynend; extdyn += sizeof (Elf64_External_Dyn))
10517
0
    {
10518
0
      Elf_Internal_Dyn dyn;
10519
0
      bfd_elf64_swap_dyn_in (abfd, extdyn, &dyn);
10520
10521
0
      switch (dyn.d_tag)
10522
0
  {
10523
0
  case DT_FLAGS_1:
10524
0
    elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
10525
0
    break;
10526
10527
0
  case DT_AARCH64_BTI_PLT:
10528
0
    ret |= PLT_BTI;
10529
0
    break;
10530
10531
0
  case DT_AARCH64_PAC_PLT:
10532
0
    ret |= PLT_PAC;
10533
0
    break;
10534
10535
0
  default: break;
10536
0
  }
10537
0
    }
10538
0
  free (contents);
10539
0
  return ret;
10540
0
}
10541
10542
static long
10543
elf64_aarch64_get_synthetic_symtab (bfd *abfd,
10544
            long symcount,
10545
            asymbol **syms,
10546
            long dynsymcount,
10547
            asymbol **dynsyms,
10548
            asymbol **ret)
10549
2
{
10550
2
  elf_aarch64_tdata (abfd)->sw_protections.plt_type = get_plt_type (abfd);
10551
2
  return _bfd_elf_get_synthetic_symtab (abfd, symcount, syms,
10552
2
          dynsymcount, dynsyms, ret);
10553
2
}
10554
10555
/* Return address for Ith PLT stub in section PLT, for relocation REL
10556
   or (bfd_vma) -1 if it should not be included.  */
10557
10558
static bfd_vma
10559
elf64_aarch64_plt_sym_val (bfd_vma i, const asection *plt,
10560
         const arelent *rel ATTRIBUTE_UNUSED)
10561
0
{
10562
0
  size_t plt0_size = PLT_ENTRY_SIZE;
10563
0
  size_t pltn_size = PLT_SMALL_ENTRY_SIZE;
10564
10565
0
  aarch64_plt_type plt_type
10566
0
    = elf_aarch64_tdata (plt->owner)->sw_protections.plt_type;
10567
0
  if (plt_type == PLT_BTI_PAC)
10568
0
    {
10569
0
      if (elf_elfheader (plt->owner)->e_type == ET_EXEC
10570
0
    || (elf_elfheader (plt->owner)->e_type == ET_DYN
10571
0
        && elf_tdata (plt->owner)->is_pie))
10572
0
  pltn_size = PLT_BTI_PAC_SMALL_ENTRY_SIZE;
10573
0
      else
10574
0
  pltn_size = PLT_PAC_SMALL_ENTRY_SIZE;
10575
0
    }
10576
0
  else if (plt_type == PLT_BTI)
10577
0
    {
10578
0
      if (elf_elfheader (plt->owner)->e_type == ET_EXEC
10579
0
    || (elf_elfheader (plt->owner)->e_type == ET_DYN
10580
0
        && elf_tdata (plt->owner)->is_pie))
10581
0
  pltn_size = PLT_BTI_SMALL_ENTRY_SIZE;
10582
0
    }
10583
0
  else if (plt_type == PLT_PAC)
10584
0
    {
10585
0
      pltn_size = PLT_PAC_SMALL_ENTRY_SIZE;
10586
0
    }
10587
10588
0
  return plt->vma + plt0_size + i * pltn_size;
10589
0
}
10590
10591
/* Returns TRUE if NAME is an AArch64 mapping symbol.
10592
   The ARM ELF standard defines $x (for A64 code) and $d (for data).
10593
   It also allows a period initiated suffix to be added to the symbol, ie:
10594
   "$[adtx]\.[:sym_char]+".  */
10595
10596
static bool
10597
is_aarch64_mapping_symbol (const char * name)
10598
129
{
10599
129
  return name != NULL /* Paranoia.  */
10600
129
    && name[0] == '$' /* Note: if objcopy --prefix-symbols has been used then
10601
       the mapping symbols could have acquired a prefix.
10602
       We do not support this here, since such symbols no
10603
       longer conform to the ARM ELF ABI.  */
10604
0
    && (name[1] == 'd' || name[1] == 'x')
10605
0
    && (name[2] == 0 || name[2] == '.');
10606
  /* FIXME: Strictly speaking the symbol is only a valid mapping symbol if
10607
     any characters that follow the period are legal characters for the body
10608
     of a symbol's name.  For now we just assume that this is the case.  */
10609
129
}
10610
10611
/* Make sure that mapping symbols in object files are not removed via the
10612
   "strip --strip-unneeded" tool.  These symbols might needed in order to
10613
   correctly generate linked files.  Once an object file has been linked,
10614
   it should be safe to remove them.  */
10615
10616
static void
10617
elf64_aarch64_backend_symbol_processing (bfd *abfd, asymbol *sym)
10618
306
{
10619
306
  if (((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
10620
306
      && sym->section != bfd_abs_section_ptr
10621
129
      && is_aarch64_mapping_symbol (sym->name))
10622
0
    sym->flags |= BSF_KEEP;
10623
306
}
10624
10625
/* Implement elf_backend_setup_object_attributes for AArch64.  */
10626
static bfd *
10627
elf64_aarch64_link_setup_object_attributes (struct bfd_link_info *info)
10628
0
{
10629
0
  bfd *pbfd = _bfd_aarch64_elf_link_setup_object_attributes (info);
10630
10631
0
  struct elf_aarch64_obj_tdata *tdata = elf_aarch64_tdata (info->output_bfd);
10632
10633
  /* When BTI is forced on the command line, information flows from plt_type to
10634
     the frozen object attributes (a.k.a FROZEN), so plt_type has already been
10635
     set and FROZEN doesn't have any effect on plt_type.
10636
     Whereas if BTI is inferred from the input bfds, information flows from
10637
     output object attributes to plt_type.  If the property GNU_PROPERTY_AARCH64
10638
     _FEATURE_1_BTI has been set on all the input bfds, then BTI is set on the
10639
     output bfd and plt_type is updated accordingly.
10640
10641
     Important note: this is not true for GNU_PROPERTY_AARCH64_FEATURE_1_PAC.
10642
     See more explanation in bfd_elf64_aarch64_set_options.  */
10643
0
  const obj_attr_subsection_v2_t *aeabi_feature_and_bits_subsec
10644
0
    = bfd_obj_attr_subsection_v2_find_by_name
10645
0
      (elf_obj_attr_subsections (info->output_bfd).first,
10646
0
       "aeabi_feature_and_bits", true);
10647
0
  if (aeabi_feature_and_bits_subsec != NULL)
10648
0
    {
10649
0
      const obj_attr_v2_t *attr_bti
10650
0
  = bfd_obj_attr_v2_find_by_tag (aeabi_feature_and_bits_subsec,
10651
0
          Tag_Feature_BTI, true);
10652
0
      if (attr_bti && attr_bti->val.uint == 1)
10653
0
  tdata->sw_protections.plt_type |= PLT_BTI;
10654
0
    }
10655
10656
0
  setup_plt_values (info, tdata->sw_protections.plt_type);
10657
10658
0
  return pbfd;
10659
0
}
10660
10661
/* Implement elf_backend_setup_gnu_properties for AArch64.  It serves as a
10662
   wrapper function for _bfd_aarch64_elf_link_setup_gnu_properties to account
10663
   for the effect of GNU properties of the output_bfd.  */
10664
static bfd *
10665
elf64_aarch64_link_setup_gnu_properties (struct bfd_link_info *info)
10666
0
{
10667
0
  bfd *pbfd = _bfd_aarch64_elf_link_setup_gnu_properties (info);
10668
10669
  /* When BTI is forced on the command line, information flows from plt_type to
10670
     outprop, so plt_type has already been set and outprop don't have any effect
10671
     on plt_type.
10672
     Whereas if BTI is inferred from the input bfds, information flows from
10673
     outprop to plt_type.  If the property GNU_PROPERTY_AARCH64_FEATURE_1_BTI
10674
     has been set on all the input bfds, then BTI is set on the output bfd and
10675
     plt_type is updated accordingly.  */
10676
0
  struct elf_aarch64_obj_tdata *tdata = elf_aarch64_tdata (info->output_bfd);
10677
0
  uint32_t outprop = tdata->gnu_property_aarch64_feature_1_and;
10678
0
  if (outprop & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
10679
0
    tdata->sw_protections.plt_type |= PLT_BTI;
10680
0
  setup_plt_values (info, tdata->sw_protections.plt_type);
10681
0
  return pbfd;
10682
0
}
10683
10684
/* Implement elf_backend_merge_gnu_properties for AArch64.  It serves as a
10685
   wrapper function for _bfd_aarch64_elf_merge_gnu_properties to account
10686
   for the effect of GNU properties of the output_bfd.  */
10687
static bool
10688
elf64_aarch64_merge_gnu_properties (struct bfd_link_info *info,
10689
               bfd *abfd, bfd *bbfd,
10690
               elf_property *aprop,
10691
               elf_property *bprop)
10692
0
{
10693
0
  uint32_t outprop
10694
0
    = elf_aarch64_tdata (info->output_bfd)->gnu_property_aarch64_feature_1_and;
10695
10696
  /* Properties are merged per type, hence only check for warnings when merging
10697
     GNU_PROPERTY_AARCH64_FEATURE_1_AND.  */
10698
0
  if ((aprop && aprop->pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
10699
0
   || (bprop && bprop->pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND))
10700
0
    {
10701
0
      const aarch64_protection_opts *sw_protections
10702
0
  = &elf_aarch64_tdata (info->output_bfd)->sw_protections;
10703
0
      aarch64_feature_marking_report bti_report = sw_protections->bti_report;
10704
0
      aarch64_feature_marking_report gcs_report = sw_protections->gcs_report;
10705
10706
      /* If output has been marked with BTI using command line argument, give
10707
   out warning if necessary.  */
10708
0
      if ((outprop & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
10709
0
    && (bti_report != MARKING_NONE))
10710
0
  {
10711
0
    if (!aprop || !(aprop->u.number & GNU_PROPERTY_AARCH64_FEATURE_1_BTI))
10712
0
      _bfd_aarch64_elf_check_bti_report (info, abfd);
10713
0
    if (!bprop || !(bprop->u.number & GNU_PROPERTY_AARCH64_FEATURE_1_BTI))
10714
0
      _bfd_aarch64_elf_check_bti_report (info, bbfd);
10715
0
  }
10716
10717
      /* If the output has been marked with GCS using '-z gcs' and the input is
10718
   missing GCS feature tag, throw a warning/error in accordance with
10719
   -z gcs-report=warning/error.  */
10720
0
      if ((outprop & GNU_PROPERTY_AARCH64_FEATURE_1_GCS)
10721
0
    && gcs_report != MARKING_NONE)
10722
0
  {
10723
0
    if (!aprop || !(aprop->u.number & GNU_PROPERTY_AARCH64_FEATURE_1_GCS))
10724
0
      _bfd_aarch64_elf_check_gcs_report (info, abfd);
10725
0
    if (!bprop || !(bprop->u.number & GNU_PROPERTY_AARCH64_FEATURE_1_GCS))
10726
0
      _bfd_aarch64_elf_check_gcs_report (info, bbfd);
10727
0
  }
10728
0
    }
10729
10730
0
  return  _bfd_aarch64_elf_merge_gnu_properties (info, abfd, aprop,
10731
0
             bprop, outprop);
10732
0
}
10733
10734
/* We use this so we can override certain functions
10735
   (though currently we don't).  */
10736
10737
static const struct elf_size_info elf64_aarch64_size_info =
10738
{
10739
  sizeof (Elf64_External_Ehdr),
10740
  sizeof (Elf64_External_Phdr),
10741
  sizeof (Elf64_External_Shdr),
10742
  sizeof (Elf64_External_Rel),
10743
  sizeof (Elf64_External_Rela),
10744
  sizeof (Elf64_External_Sym),
10745
  sizeof (Elf64_External_Dyn),
10746
  sizeof (Elf_External_Note),
10747
  4,        /* Hash table entry size.  */
10748
  1,        /* Internal relocs per external relocs.  */
10749
  ARCH_SIZE,      /* Arch size.  */
10750
  LOG_FILE_ALIGN,   /* Log_file_align.  */
10751
  ELFCLASS64, EV_CURRENT,
10752
  bfd_elf64_write_out_phdrs,
10753
  bfd_elf64_write_shdrs_and_ehdr,
10754
  bfd_elf64_checksum_contents,
10755
  bfd_elf64_write_relocs,
10756
  bfd_elf64_swap_symbol_in,
10757
  bfd_elf64_swap_symbol_out,
10758
  bfd_elf64_slurp_reloc_table,
10759
  bfd_elf64_slurp_symbol_table,
10760
  bfd_elf64_swap_dyn_in,
10761
  bfd_elf64_swap_dyn_out,
10762
  bfd_elf64_swap_reloc_in,
10763
  bfd_elf64_swap_reloc_out,
10764
  bfd_elf64_swap_reloca_in,
10765
  bfd_elf64_swap_reloca_out
10766
};
10767
10768
#define ELF_ARCH      bfd_arch_aarch64
10769
#define ELF_TARGET_ID     AARCH64_ELF_DATA
10770
#define ELF_MACHINE_CODE    EM_AARCH64
10771
#define ELF_MAXPAGESIZE     0x10000
10772
#define ELF_COMMONPAGESIZE    0x1000
10773
10774
#define bfd_elf64_bfd_is_target_special_symbol  \
10775
  elf64_aarch64_is_target_special_symbol
10776
10777
#define bfd_elf64_bfd_link_hash_table_create  \
10778
  elf64_aarch64_link_hash_table_create
10779
10780
#define bfd_elf64_bfd_merge_private_bfd_data  \
10781
  elf64_aarch64_merge_private_bfd_data
10782
10783
#define bfd_elf64_bfd_print_private_bfd_data  \
10784
  elf64_aarch64_print_private_bfd_data
10785
10786
#define bfd_elf64_bfd_reloc_type_lookup   \
10787
  elf64_aarch64_reloc_type_lookup
10788
10789
#define bfd_elf64_bfd_reloc_name_lookup   \
10790
  elf64_aarch64_reloc_name_lookup
10791
10792
#define bfd_elf64_bfd_set_private_flags   \
10793
  elf64_aarch64_set_private_flags
10794
10795
#define bfd_elf64_find_inliner_info   \
10796
  elf64_aarch64_find_inliner_info
10797
10798
#define bfd_elf64_get_synthetic_symtab    \
10799
  elf64_aarch64_get_synthetic_symtab
10800
10801
#define bfd_elf64_mkobject      \
10802
  elf64_aarch64_mkobject
10803
10804
#define bfd_elf64_new_section_hook    \
10805
  elf64_aarch64_new_section_hook
10806
10807
#define elf_backend_adjust_dynamic_symbol \
10808
  elf64_aarch64_adjust_dynamic_symbol
10809
10810
#define elf_backend_early_size_sections   \
10811
  elf64_aarch64_early_size_sections
10812
10813
#define elf_backend_check_relocs    \
10814
  elf64_aarch64_check_relocs
10815
10816
#define elf_backend_copy_indirect_symbol  \
10817
  elf64_aarch64_copy_indirect_symbol
10818
10819
#define elf_backend_merge_symbol_attribute  \
10820
  elf64_aarch64_merge_symbol_attribute
10821
10822
/* Create .dynbss, and .rela.bss sections in DYNOBJ, and set up shortcuts
10823
   to them in our hash.  */
10824
#define elf_backend_create_dynamic_sections \
10825
  elf64_aarch64_create_dynamic_sections
10826
10827
#define elf_backend_init_index_section    \
10828
  _bfd_elf_init_2_index_sections
10829
10830
#define elf_backend_finish_dynamic_sections \
10831
  elf64_aarch64_finish_dynamic_sections
10832
10833
#define elf_backend_finish_dynamic_symbol \
10834
  elf64_aarch64_finish_dynamic_symbol
10835
10836
#define elf_backend_object_p      \
10837
  elf64_aarch64_object_p
10838
10839
#define elf_backend_output_arch_local_syms  \
10840
  elf64_aarch64_output_arch_local_syms
10841
10842
#define elf_backend_maybe_function_sym    \
10843
  elf64_aarch64_maybe_function_sym
10844
10845
#define elf_backend_plt_sym_val     \
10846
  elf64_aarch64_plt_sym_val
10847
10848
#define elf_backend_init_file_header    \
10849
  elf64_aarch64_init_file_header
10850
10851
#define elf_backend_relocate_section    \
10852
  elf64_aarch64_relocate_section
10853
10854
#define elf_backend_reloc_type_class    \
10855
  elf64_aarch64_reloc_type_class
10856
10857
#define elf_backend_section_from_shdr   \
10858
  elf64_aarch64_section_from_shdr
10859
10860
#define elf_backend_section_from_phdr   \
10861
  elf64_aarch64_section_from_phdr
10862
10863
#define elf_backend_modify_headers    \
10864
  elf64_aarch64_modify_headers
10865
10866
#define elf_backend_late_size_sections    \
10867
  elf64_aarch64_late_size_sections
10868
10869
#define elf_backend_size_info     \
10870
  elf64_aarch64_size_info
10871
10872
#define elf_backend_write_section   \
10873
  elf64_aarch64_write_section
10874
10875
#define elf_backend_symbol_processing   \
10876
  elf64_aarch64_backend_symbol_processing
10877
10878
#define elf_backend_setup_object_attributes \
10879
  elf64_aarch64_link_setup_object_attributes
10880
10881
#define elf_backend_setup_gnu_properties  \
10882
  elf64_aarch64_link_setup_gnu_properties
10883
10884
#define elf_backend_translate_gnu_props_to_obj_attrs \
10885
  _bfd_aarch64_translate_gnu_props_to_obj_attrs
10886
10887
#define elf_backend_translate_obj_attrs_to_gnu_props \
10888
  _bfd_aarch64_translate_obj_attrs_to_gnu_props
10889
10890
#define elf_backend_obj_attr_v2_default_value \
10891
  _bfd_aarch64_oav2_default_value
10892
10893
#define elf_backend_obj_attr_v2_merge \
10894
  _bfd_aarch64_oav2_attr_merge
10895
10896
#define elf_backend_merge_gnu_properties  \
10897
  elf64_aarch64_merge_gnu_properties
10898
10899
#define elf_backend_size_relative_relocs  \
10900
  elf64_aarch64_size_relative_relocs
10901
10902
#define elf_backend_finish_relative_relocs  \
10903
  elf64_aarch64_finish_relative_relocs
10904
10905
#define elf_backend_can_refcount       1
10906
#define elf_backend_can_gc_sections    1
10907
#define elf_backend_plt_readonly       1
10908
#define elf_backend_want_got_plt       1
10909
#define elf_backend_want_plt_sym       0
10910
#define elf_backend_want_dynrelro      1
10911
#define elf_backend_may_use_rel_p      0
10912
#define elf_backend_may_use_rela_p     1
10913
#define elf_backend_default_use_rela_p 1
10914
#define elf_backend_rela_normal        1
10915
#define elf_backend_dtrel_excludes_plt 1
10916
#define elf_backend_got_header_size (GOT_ENTRY_SIZE * 3)
10917
#define elf_backend_default_execstack  0
10918
#define elf_backend_extern_protected_data 0
10919
#define elf_backend_hash_symbol elf_aarch64_hash_symbol
10920
10921
/* In OAv2, the presence of a vendor prefix means that the contents (syntax) can
10922
   be fully parsed, even if the interpretation of each tag is unknown.*/
10923
#undef  elf_backend_obj_attrs_vendor
10924
#define elf_backend_obj_attrs_vendor    "aeabi"
10925
#undef  elf_backend_obj_attrs_section
10926
#define elf_backend_obj_attrs_section   SEC_AARCH64_ATTRIBUTES
10927
/* In OAv2, the type of an attribute is specified by the subsection that
10928
   contains it.  */
10929
#define elf_backend_obj_attrs_arg_type    NULL
10930
#undef  elf_backend_obj_attrs_section_type
10931
#define elf_backend_obj_attrs_section_type  SHT_AARCH64_ATTRIBUTES
10932
#undef  elf_backend_default_obj_attr_version
10933
#define elf_backend_default_obj_attr_version  OBJ_ATTR_V2
10934
#undef  elf_backend_obj_attrs_version_dec
10935
#define elf_backend_obj_attrs_version_dec \
10936
  _bfd_aarch64_obj_attrs_version_dec
10937
#undef  elf_backend_obj_attrs_version_enc
10938
#define elf_backend_obj_attrs_version_enc \
10939
  _bfd_aarch64_obj_attrs_version_enc
10940
/* Object attributes v2 specific values.  */
10941
#undef  elf_backend_obj_attr_v2_known_subsections
10942
#define elf_backend_obj_attr_v2_known_subsections \
10943
  aarch64_obj_attr_v2_known_subsections
10944
#undef  elf_backend_obj_attr_v2_known_subsections_size
10945
#define elf_backend_obj_attr_v2_known_subsections_size \
10946
  ARRAY_SIZE(aarch64_obj_attr_v2_known_subsections)
10947
10948
#include "elf64-target.h"