Coverage Report

Created: 2026-07-25 10:20

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