Coverage Report

Created: 2026-03-10 08:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf32-arm.c
Line
Count
Source
1
/* 32-bit ELF support for ARM
2
   Copyright (C) 1998-2026 Free Software Foundation, Inc.
3
4
   This file is part of BFD, the Binary File Descriptor library.
5
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3 of the License, or
9
   (at your option) any later version.
10
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19
   MA 02110-1301, USA.  */
20
21
#include "sysdep.h"
22
#include <limits.h>
23
24
#include "bfd.h"
25
#include "libiberty.h"
26
#include "libbfd.h"
27
#include "elf-bfd.h"
28
#include "elf-vxworks.h"
29
#include "elf/arm.h"
30
#include "elf32-arm.h"
31
#include "cpu-arm.h"
32
33
/* Return the relocation section associated with NAME.  HTAB is the
34
   bfd's elf32_arm_link_hash_entry.  */
35
#define RELOC_SECTION(HTAB, NAME) \
36
0
  ((HTAB)->use_rel ? ".rel" NAME : ".rela" NAME)
37
38
/* Return size of a relocation entry.  HTAB is the bfd's
39
   elf32_arm_link_hash_entry.  */
40
#define RELOC_SIZE(HTAB) \
41
0
  ((HTAB)->use_rel \
42
0
   ? sizeof (Elf32_External_Rel) \
43
0
   : sizeof (Elf32_External_Rela))
44
45
/* Return function to swap relocations in.  HTAB is the bfd's
46
   elf32_arm_link_hash_entry.  */
47
#define SWAP_RELOC_IN(HTAB) \
48
0
  ((HTAB)->use_rel \
49
0
   ? bfd_elf32_swap_reloc_in \
50
0
   : bfd_elf32_swap_reloca_in)
51
52
/* Return function to swap relocations out.  HTAB is the bfd's
53
   elf32_arm_link_hash_entry.  */
54
#define SWAP_RELOC_OUT(HTAB) \
55
0
  ((HTAB)->use_rel \
56
0
   ? bfd_elf32_swap_reloc_out \
57
0
   : bfd_elf32_swap_reloca_out)
58
59
#define elf_info_to_howto   NULL
60
#define elf_info_to_howto_rel   elf32_arm_info_to_howto
61
62
3
#define ARM_ELF_ABI_VERSION   0
63
#define ARM_ELF_OS_ABI_VERSION    ELFOSABI_ARM
64
65
/* The Adjusted Place, as defined by AAELF.  */
66
0
#define Pa(X) ((X) & 0xfffffffc)
67
68
static bool elf32_arm_write_section (bfd *output_bfd,
69
             struct bfd_link_info *link_info,
70
             asection *sec,
71
             bfd_byte *contents);
72
73
/* Note: code such as elf32_arm_reloc_type_lookup expect to use e.g.
74
   R_ARM_PC24 as an index into this, and find the R_ARM_PC24 HOWTO
75
   in that slot.  */
76
77
static reloc_howto_type elf32_arm_howto_table_1[] =
78
{
79
  /* No relocation.  */
80
  HOWTO (R_ARM_NONE,    /* type */
81
   0,     /* rightshift */
82
   0,     /* size */
83
   0,     /* bitsize */
84
   false,     /* pc_relative */
85
   0,     /* bitpos */
86
   complain_overflow_dont,/* complain_on_overflow */
87
   bfd_elf_generic_reloc, /* special_function */
88
   "R_ARM_NONE",    /* name */
89
   false,     /* partial_inplace */
90
   0,     /* src_mask */
91
   0,     /* dst_mask */
92
   false),    /* pcrel_offset */
93
94
  HOWTO (R_ARM_PC24,    /* type */
95
   2,     /* rightshift */
96
   4,     /* size */
97
   24,      /* bitsize */
98
   true,      /* pc_relative */
99
   0,     /* bitpos */
100
   complain_overflow_signed,/* complain_on_overflow */
101
   bfd_elf_generic_reloc, /* special_function */
102
   "R_ARM_PC24",    /* name */
103
   false,     /* partial_inplace */
104
   0x00ffffff,    /* src_mask */
105
   0x00ffffff,    /* dst_mask */
106
   true),     /* pcrel_offset */
107
108
  /* 32 bit absolute */
109
  HOWTO (R_ARM_ABS32,   /* type */
110
   0,     /* rightshift */
111
   4,     /* size */
112
   32,      /* bitsize */
113
   false,     /* pc_relative */
114
   0,     /* bitpos */
115
   complain_overflow_bitfield,/* complain_on_overflow */
116
   bfd_elf_generic_reloc, /* special_function */
117
   "R_ARM_ABS32",   /* name */
118
   false,     /* partial_inplace */
119
   0xffffffff,    /* src_mask */
120
   0xffffffff,    /* dst_mask */
121
   false),    /* pcrel_offset */
122
123
  /* standard 32bit pc-relative reloc */
124
  HOWTO (R_ARM_REL32,   /* type */
125
   0,     /* rightshift */
126
   4,     /* size */
127
   32,      /* bitsize */
128
   true,      /* pc_relative */
129
   0,     /* bitpos */
130
   complain_overflow_bitfield,/* complain_on_overflow */
131
   bfd_elf_generic_reloc, /* special_function */
132
   "R_ARM_REL32",   /* name */
133
   false,     /* partial_inplace */
134
   0xffffffff,    /* src_mask */
135
   0xffffffff,    /* dst_mask */
136
   true),     /* pcrel_offset */
137
138
  /* 8 bit absolute - R_ARM_LDR_PC_G0 in AAELF */
139
  HOWTO (R_ARM_LDR_PC_G0, /* type */
140
   0,     /* rightshift */
141
   1,     /* size */
142
   32,      /* bitsize */
143
   true,      /* pc_relative */
144
   0,     /* bitpos */
145
   complain_overflow_dont,/* complain_on_overflow */
146
   bfd_elf_generic_reloc, /* special_function */
147
   "R_ARM_LDR_PC_G0",     /* name */
148
   false,     /* partial_inplace */
149
   0xffffffff,    /* src_mask */
150
   0xffffffff,    /* dst_mask */
151
   true),     /* pcrel_offset */
152
153
   /* 16 bit absolute */
154
  HOWTO (R_ARM_ABS16,   /* type */
155
   0,     /* rightshift */
156
   2,     /* size */
157
   16,      /* bitsize */
158
   false,     /* pc_relative */
159
   0,     /* bitpos */
160
   complain_overflow_bitfield,/* complain_on_overflow */
161
   bfd_elf_generic_reloc, /* special_function */
162
   "R_ARM_ABS16",   /* name */
163
   false,     /* partial_inplace */
164
   0x0000ffff,    /* src_mask */
165
   0x0000ffff,    /* dst_mask */
166
   false),    /* pcrel_offset */
167
168
  /* 12 bit absolute */
169
  HOWTO (R_ARM_ABS12,   /* type */
170
   0,     /* rightshift */
171
   4,     /* size */
172
   12,      /* bitsize */
173
   false,     /* pc_relative */
174
   0,     /* bitpos */
175
   complain_overflow_bitfield,/* complain_on_overflow */
176
   bfd_elf_generic_reloc, /* special_function */
177
   "R_ARM_ABS12",   /* name */
178
   false,     /* partial_inplace */
179
   0x00000fff,    /* src_mask */
180
   0x00000fff,    /* dst_mask */
181
   false),    /* pcrel_offset */
182
183
  HOWTO (R_ARM_THM_ABS5,  /* type */
184
   6,     /* rightshift */
185
   2,     /* size */
186
   5,     /* bitsize */
187
   false,     /* pc_relative */
188
   0,     /* bitpos */
189
   complain_overflow_bitfield,/* complain_on_overflow */
190
   bfd_elf_generic_reloc, /* special_function */
191
   "R_ARM_THM_ABS5",  /* name */
192
   false,     /* partial_inplace */
193
   0x000007e0,    /* src_mask */
194
   0x000007e0,    /* dst_mask */
195
   false),    /* pcrel_offset */
196
197
  /* 8 bit absolute */
198
  HOWTO (R_ARM_ABS8,    /* type */
199
   0,     /* rightshift */
200
   1,     /* size */
201
   8,     /* bitsize */
202
   false,     /* pc_relative */
203
   0,     /* bitpos */
204
   complain_overflow_bitfield,/* complain_on_overflow */
205
   bfd_elf_generic_reloc, /* special_function */
206
   "R_ARM_ABS8",    /* name */
207
   false,     /* partial_inplace */
208
   0x000000ff,    /* src_mask */
209
   0x000000ff,    /* dst_mask */
210
   false),    /* pcrel_offset */
211
212
  HOWTO (R_ARM_SBREL32,   /* type */
213
   0,     /* rightshift */
214
   4,     /* size */
215
   32,      /* bitsize */
216
   false,     /* pc_relative */
217
   0,     /* bitpos */
218
   complain_overflow_dont,/* complain_on_overflow */
219
   bfd_elf_generic_reloc, /* special_function */
220
   "R_ARM_SBREL32", /* name */
221
   false,     /* partial_inplace */
222
   0xffffffff,    /* src_mask */
223
   0xffffffff,    /* dst_mask */
224
   false),    /* pcrel_offset */
225
226
  HOWTO (R_ARM_THM_CALL,  /* type */
227
   1,     /* rightshift */
228
   4,     /* size */
229
   24,      /* bitsize */
230
   true,      /* pc_relative */
231
   0,     /* bitpos */
232
   complain_overflow_signed,/* complain_on_overflow */
233
   bfd_elf_generic_reloc, /* special_function */
234
   "R_ARM_THM_CALL",  /* name */
235
   false,     /* partial_inplace */
236
   0x07ff2fff,    /* src_mask */
237
   0x07ff2fff,    /* dst_mask */
238
   true),     /* pcrel_offset */
239
240
  HOWTO (R_ARM_THM_PC8,   /* type */
241
   1,     /* rightshift */
242
   2,     /* size */
243
   8,     /* bitsize */
244
   true,      /* pc_relative */
245
   0,     /* bitpos */
246
   complain_overflow_signed,/* complain_on_overflow */
247
   bfd_elf_generic_reloc, /* special_function */
248
   "R_ARM_THM_PC8", /* name */
249
   false,     /* partial_inplace */
250
   0x000000ff,    /* src_mask */
251
   0x000000ff,    /* dst_mask */
252
   true),     /* pcrel_offset */
253
254
  HOWTO (R_ARM_BREL_ADJ,  /* type */
255
   1,     /* rightshift */
256
   2,     /* size */
257
   32,      /* bitsize */
258
   false,     /* pc_relative */
259
   0,     /* bitpos */
260
   complain_overflow_signed,/* complain_on_overflow */
261
   bfd_elf_generic_reloc, /* special_function */
262
   "R_ARM_BREL_ADJ",  /* name */
263
   false,     /* partial_inplace */
264
   0xffffffff,    /* src_mask */
265
   0xffffffff,    /* dst_mask */
266
   false),    /* pcrel_offset */
267
268
  HOWTO (R_ARM_TLS_DESC,  /* type */
269
   0,     /* rightshift */
270
   4,     /* size */
271
   32,      /* bitsize */
272
   false,     /* pc_relative */
273
   0,     /* bitpos */
274
   complain_overflow_bitfield,/* complain_on_overflow */
275
   bfd_elf_generic_reloc, /* special_function */
276
   "R_ARM_TLS_DESC",  /* name */
277
   false,     /* partial_inplace */
278
   0xffffffff,    /* src_mask */
279
   0xffffffff,    /* dst_mask */
280
   false),    /* pcrel_offset */
281
282
  HOWTO (R_ARM_THM_SWI8,  /* type */
283
   0,     /* rightshift */
284
   0,     /* size */
285
   0,     /* bitsize */
286
   false,     /* pc_relative */
287
   0,     /* bitpos */
288
   complain_overflow_signed,/* complain_on_overflow */
289
   bfd_elf_generic_reloc, /* special_function */
290
   "R_ARM_SWI8",    /* name */
291
   false,     /* partial_inplace */
292
   0x00000000,    /* src_mask */
293
   0x00000000,    /* dst_mask */
294
   false),    /* pcrel_offset */
295
296
  /* BLX instruction for the ARM.  */
297
  HOWTO (R_ARM_XPC25,   /* type */
298
   2,     /* rightshift */
299
   4,     /* size */
300
   24,      /* bitsize */
301
   true,      /* pc_relative */
302
   0,     /* bitpos */
303
   complain_overflow_signed,/* complain_on_overflow */
304
   bfd_elf_generic_reloc, /* special_function */
305
   "R_ARM_XPC25",   /* name */
306
   false,     /* partial_inplace */
307
   0x00ffffff,    /* src_mask */
308
   0x00ffffff,    /* dst_mask */
309
   true),     /* pcrel_offset */
310
311
  /* BLX instruction for the Thumb.  */
312
  HOWTO (R_ARM_THM_XPC22, /* type */
313
   2,     /* rightshift */
314
   4,     /* size */
315
   24,      /* bitsize */
316
   true,      /* pc_relative */
317
   0,     /* bitpos */
318
   complain_overflow_signed,/* complain_on_overflow */
319
   bfd_elf_generic_reloc, /* special_function */
320
   "R_ARM_THM_XPC22", /* name */
321
   false,     /* partial_inplace */
322
   0x07ff2fff,    /* src_mask */
323
   0x07ff2fff,    /* dst_mask */
324
   true),     /* pcrel_offset */
325
326
  /* Dynamic TLS relocations.  */
327
328
  HOWTO (R_ARM_TLS_DTPMOD32,  /* type */
329
   0,     /* rightshift */
330
   4,     /* size */
331
   32,      /* bitsize */
332
   false,     /* pc_relative */
333
   0,     /* bitpos */
334
   complain_overflow_bitfield,/* complain_on_overflow */
335
   bfd_elf_generic_reloc, /* special_function */
336
   "R_ARM_TLS_DTPMOD32",  /* name */
337
   true,      /* partial_inplace */
338
   0xffffffff,    /* src_mask */
339
   0xffffffff,    /* dst_mask */
340
   false),    /* pcrel_offset */
341
342
  HOWTO (R_ARM_TLS_DTPOFF32,  /* type */
343
   0,     /* rightshift */
344
   4,     /* size */
345
   32,      /* bitsize */
346
   false,     /* pc_relative */
347
   0,     /* bitpos */
348
   complain_overflow_bitfield,/* complain_on_overflow */
349
   bfd_elf_generic_reloc, /* special_function */
350
   "R_ARM_TLS_DTPOFF32",  /* name */
351
   true,      /* partial_inplace */
352
   0xffffffff,    /* src_mask */
353
   0xffffffff,    /* dst_mask */
354
   false),    /* pcrel_offset */
355
356
  HOWTO (R_ARM_TLS_TPOFF32, /* type */
357
   0,     /* rightshift */
358
   4,     /* size */
359
   32,      /* bitsize */
360
   false,     /* pc_relative */
361
   0,     /* bitpos */
362
   complain_overflow_bitfield,/* complain_on_overflow */
363
   bfd_elf_generic_reloc, /* special_function */
364
   "R_ARM_TLS_TPOFF32", /* name */
365
   true,      /* partial_inplace */
366
   0xffffffff,    /* src_mask */
367
   0xffffffff,    /* dst_mask */
368
   false),    /* pcrel_offset */
369
370
  /* Relocs used in ARM Linux */
371
372
  HOWTO (R_ARM_COPY,    /* type */
373
   0,     /* rightshift */
374
   4,     /* size */
375
   32,      /* bitsize */
376
   false,     /* pc_relative */
377
   0,     /* bitpos */
378
   complain_overflow_bitfield,/* complain_on_overflow */
379
   bfd_elf_generic_reloc, /* special_function */
380
   "R_ARM_COPY",    /* name */
381
   true,      /* partial_inplace */
382
   0xffffffff,    /* src_mask */
383
   0xffffffff,    /* dst_mask */
384
   false),    /* pcrel_offset */
385
386
  HOWTO (R_ARM_GLOB_DAT,  /* type */
387
   0,     /* rightshift */
388
   4,     /* size */
389
   32,      /* bitsize */
390
   false,     /* pc_relative */
391
   0,     /* bitpos */
392
   complain_overflow_bitfield,/* complain_on_overflow */
393
   bfd_elf_generic_reloc, /* special_function */
394
   "R_ARM_GLOB_DAT",  /* name */
395
   true,      /* partial_inplace */
396
   0xffffffff,    /* src_mask */
397
   0xffffffff,    /* dst_mask */
398
   false),    /* pcrel_offset */
399
400
  HOWTO (R_ARM_JUMP_SLOT, /* type */
401
   0,     /* rightshift */
402
   4,     /* size */
403
   32,      /* bitsize */
404
   false,     /* pc_relative */
405
   0,     /* bitpos */
406
   complain_overflow_bitfield,/* complain_on_overflow */
407
   bfd_elf_generic_reloc, /* special_function */
408
   "R_ARM_JUMP_SLOT", /* name */
409
   true,      /* partial_inplace */
410
   0xffffffff,    /* src_mask */
411
   0xffffffff,    /* dst_mask */
412
   false),    /* pcrel_offset */
413
414
  HOWTO (R_ARM_RELATIVE,  /* type */
415
   0,     /* rightshift */
416
   4,     /* size */
417
   32,      /* bitsize */
418
   false,     /* pc_relative */
419
   0,     /* bitpos */
420
   complain_overflow_bitfield,/* complain_on_overflow */
421
   bfd_elf_generic_reloc, /* special_function */
422
   "R_ARM_RELATIVE",  /* name */
423
   true,      /* partial_inplace */
424
   0xffffffff,    /* src_mask */
425
   0xffffffff,    /* dst_mask */
426
   false),    /* pcrel_offset */
427
428
  HOWTO (R_ARM_GOTOFF32,  /* type */
429
   0,     /* rightshift */
430
   4,     /* size */
431
   32,      /* bitsize */
432
   false,     /* pc_relative */
433
   0,     /* bitpos */
434
   complain_overflow_bitfield,/* complain_on_overflow */
435
   bfd_elf_generic_reloc, /* special_function */
436
   "R_ARM_GOTOFF32",  /* name */
437
   true,      /* partial_inplace */
438
   0xffffffff,    /* src_mask */
439
   0xffffffff,    /* dst_mask */
440
   false),    /* pcrel_offset */
441
442
  HOWTO (R_ARM_GOTPC,   /* type */
443
   0,     /* rightshift */
444
   4,     /* size */
445
   32,      /* bitsize */
446
   true,      /* pc_relative */
447
   0,     /* bitpos */
448
   complain_overflow_bitfield,/* complain_on_overflow */
449
   bfd_elf_generic_reloc, /* special_function */
450
   "R_ARM_GOTPC",   /* name */
451
   true,      /* partial_inplace */
452
   0xffffffff,    /* src_mask */
453
   0xffffffff,    /* dst_mask */
454
   true),     /* pcrel_offset */
455
456
  HOWTO (R_ARM_GOT32,   /* type */
457
   0,     /* rightshift */
458
   4,     /* size */
459
   32,      /* bitsize */
460
   false,     /* pc_relative */
461
   0,     /* bitpos */
462
   complain_overflow_bitfield,/* complain_on_overflow */
463
   bfd_elf_generic_reloc, /* special_function */
464
   "R_ARM_GOT32",   /* name */
465
   true,      /* partial_inplace */
466
   0xffffffff,    /* src_mask */
467
   0xffffffff,    /* dst_mask */
468
   false),    /* pcrel_offset */
469
470
  HOWTO (R_ARM_PLT32,   /* type */
471
   2,     /* rightshift */
472
   4,     /* size */
473
   24,      /* bitsize */
474
   true,      /* pc_relative */
475
   0,     /* bitpos */
476
   complain_overflow_bitfield,/* complain_on_overflow */
477
   bfd_elf_generic_reloc, /* special_function */
478
   "R_ARM_PLT32",   /* name */
479
   false,     /* partial_inplace */
480
   0x00ffffff,    /* src_mask */
481
   0x00ffffff,    /* dst_mask */
482
   true),     /* pcrel_offset */
483
484
  HOWTO (R_ARM_CALL,    /* type */
485
   2,     /* rightshift */
486
   4,     /* size */
487
   24,      /* bitsize */
488
   true,      /* pc_relative */
489
   0,     /* bitpos */
490
   complain_overflow_signed,/* complain_on_overflow */
491
   bfd_elf_generic_reloc, /* special_function */
492
   "R_ARM_CALL",    /* name */
493
   false,     /* partial_inplace */
494
   0x00ffffff,    /* src_mask */
495
   0x00ffffff,    /* dst_mask */
496
   true),     /* pcrel_offset */
497
498
  HOWTO (R_ARM_JUMP24,    /* type */
499
   2,     /* rightshift */
500
   4,     /* size */
501
   24,      /* bitsize */
502
   true,      /* pc_relative */
503
   0,     /* bitpos */
504
   complain_overflow_signed,/* complain_on_overflow */
505
   bfd_elf_generic_reloc, /* special_function */
506
   "R_ARM_JUMP24",  /* name */
507
   false,     /* partial_inplace */
508
   0x00ffffff,    /* src_mask */
509
   0x00ffffff,    /* dst_mask */
510
   true),     /* pcrel_offset */
511
512
  HOWTO (R_ARM_THM_JUMP24,  /* type */
513
   1,     /* rightshift */
514
   4,     /* size */
515
   24,      /* bitsize */
516
   true,      /* pc_relative */
517
   0,     /* bitpos */
518
   complain_overflow_signed,/* complain_on_overflow */
519
   bfd_elf_generic_reloc, /* special_function */
520
   "R_ARM_THM_JUMP24",  /* name */
521
   false,     /* partial_inplace */
522
   0x07ff2fff,    /* src_mask */
523
   0x07ff2fff,    /* dst_mask */
524
   true),     /* pcrel_offset */
525
526
  HOWTO (R_ARM_BASE_ABS,  /* type */
527
   0,     /* rightshift */
528
   4,     /* size */
529
   32,      /* bitsize */
530
   false,     /* pc_relative */
531
   0,     /* bitpos */
532
   complain_overflow_dont,/* complain_on_overflow */
533
   bfd_elf_generic_reloc, /* special_function */
534
   "R_ARM_BASE_ABS",  /* name */
535
   false,     /* partial_inplace */
536
   0xffffffff,    /* src_mask */
537
   0xffffffff,    /* dst_mask */
538
   false),    /* pcrel_offset */
539
540
  HOWTO (R_ARM_ALU_PCREL7_0,  /* type */
541
   0,     /* rightshift */
542
   4,     /* size */
543
   12,      /* bitsize */
544
   true,      /* pc_relative */
545
   0,     /* bitpos */
546
   complain_overflow_dont,/* complain_on_overflow */
547
   bfd_elf_generic_reloc, /* special_function */
548
   "R_ARM_ALU_PCREL_7_0", /* name */
549
   false,     /* partial_inplace */
550
   0x00000fff,    /* src_mask */
551
   0x00000fff,    /* dst_mask */
552
   true),     /* pcrel_offset */
553
554
  HOWTO (R_ARM_ALU_PCREL15_8, /* type */
555
   0,     /* rightshift */
556
   4,     /* size */
557
   12,      /* bitsize */
558
   true,      /* pc_relative */
559
   8,     /* bitpos */
560
   complain_overflow_dont,/* complain_on_overflow */
561
   bfd_elf_generic_reloc, /* special_function */
562
   "R_ARM_ALU_PCREL_15_8",/* name */
563
   false,     /* partial_inplace */
564
   0x00000fff,    /* src_mask */
565
   0x00000fff,    /* dst_mask */
566
   true),     /* pcrel_offset */
567
568
  HOWTO (R_ARM_ALU_PCREL23_15,  /* type */
569
   0,     /* rightshift */
570
   4,     /* size */
571
   12,      /* bitsize */
572
   true,      /* pc_relative */
573
   16,      /* bitpos */
574
   complain_overflow_dont,/* complain_on_overflow */
575
   bfd_elf_generic_reloc, /* special_function */
576
   "R_ARM_ALU_PCREL_23_15",/* name */
577
   false,     /* partial_inplace */
578
   0x00000fff,    /* src_mask */
579
   0x00000fff,    /* dst_mask */
580
   true),     /* pcrel_offset */
581
582
  HOWTO (R_ARM_LDR_SBREL_11_0,  /* type */
583
   0,     /* rightshift */
584
   4,     /* size */
585
   12,      /* bitsize */
586
   false,     /* pc_relative */
587
   0,     /* bitpos */
588
   complain_overflow_dont,/* complain_on_overflow */
589
   bfd_elf_generic_reloc, /* special_function */
590
   "R_ARM_LDR_SBREL_11_0",/* name */
591
   false,     /* partial_inplace */
592
   0x00000fff,    /* src_mask */
593
   0x00000fff,    /* dst_mask */
594
   false),    /* pcrel_offset */
595
596
  HOWTO (R_ARM_ALU_SBREL_19_12, /* type */
597
   0,     /* rightshift */
598
   4,     /* size */
599
   8,     /* bitsize */
600
   false,     /* pc_relative */
601
   12,      /* bitpos */
602
   complain_overflow_dont,/* complain_on_overflow */
603
   bfd_elf_generic_reloc, /* special_function */
604
   "R_ARM_ALU_SBREL_19_12",/* name */
605
   false,     /* partial_inplace */
606
   0x000ff000,    /* src_mask */
607
   0x000ff000,    /* dst_mask */
608
   false),    /* pcrel_offset */
609
610
  HOWTO (R_ARM_ALU_SBREL_27_20, /* type */
611
   0,     /* rightshift */
612
   4,     /* size */
613
   8,     /* bitsize */
614
   false,     /* pc_relative */
615
   20,      /* bitpos */
616
   complain_overflow_dont,/* complain_on_overflow */
617
   bfd_elf_generic_reloc, /* special_function */
618
   "R_ARM_ALU_SBREL_27_20",/* name */
619
   false,     /* partial_inplace */
620
   0x0ff00000,    /* src_mask */
621
   0x0ff00000,    /* dst_mask */
622
   false),    /* pcrel_offset */
623
624
  HOWTO (R_ARM_TARGET1,   /* type */
625
   0,     /* rightshift */
626
   4,     /* size */
627
   32,      /* bitsize */
628
   false,     /* pc_relative */
629
   0,     /* bitpos */
630
   complain_overflow_dont,/* complain_on_overflow */
631
   bfd_elf_generic_reloc, /* special_function */
632
   "R_ARM_TARGET1", /* name */
633
   false,     /* partial_inplace */
634
   0xffffffff,    /* src_mask */
635
   0xffffffff,    /* dst_mask */
636
   false),    /* pcrel_offset */
637
638
  HOWTO (R_ARM_ROSEGREL32,  /* type */
639
   0,     /* rightshift */
640
   4,     /* size */
641
   32,      /* bitsize */
642
   false,     /* pc_relative */
643
   0,     /* bitpos */
644
   complain_overflow_dont,/* complain_on_overflow */
645
   bfd_elf_generic_reloc, /* special_function */
646
   "R_ARM_ROSEGREL32",  /* name */
647
   false,     /* partial_inplace */
648
   0xffffffff,    /* src_mask */
649
   0xffffffff,    /* dst_mask */
650
   false),    /* pcrel_offset */
651
652
  HOWTO (R_ARM_V4BX,    /* type */
653
   0,     /* rightshift */
654
   4,     /* size */
655
   32,      /* bitsize */
656
   false,     /* pc_relative */
657
   0,     /* bitpos */
658
   complain_overflow_dont,/* complain_on_overflow */
659
   bfd_elf_generic_reloc, /* special_function */
660
   "R_ARM_V4BX",    /* name */
661
   false,     /* partial_inplace */
662
   0xffffffff,    /* src_mask */
663
   0xffffffff,    /* dst_mask */
664
   false),    /* pcrel_offset */
665
666
  HOWTO (R_ARM_TARGET2,   /* type */
667
   0,     /* rightshift */
668
   4,     /* size */
669
   32,      /* bitsize */
670
   false,     /* pc_relative */
671
   0,     /* bitpos */
672
   complain_overflow_signed,/* complain_on_overflow */
673
   bfd_elf_generic_reloc, /* special_function */
674
   "R_ARM_TARGET2", /* name */
675
   false,     /* partial_inplace */
676
   0xffffffff,    /* src_mask */
677
   0xffffffff,    /* dst_mask */
678
   true),     /* pcrel_offset */
679
680
  HOWTO (R_ARM_PREL31,    /* type */
681
   0,     /* rightshift */
682
   4,     /* size */
683
   31,      /* bitsize */
684
   true,      /* pc_relative */
685
   0,     /* bitpos */
686
   complain_overflow_signed,/* complain_on_overflow */
687
   bfd_elf_generic_reloc, /* special_function */
688
   "R_ARM_PREL31",  /* name */
689
   false,     /* partial_inplace */
690
   0x7fffffff,    /* src_mask */
691
   0x7fffffff,    /* dst_mask */
692
   true),     /* pcrel_offset */
693
694
  HOWTO (R_ARM_MOVW_ABS_NC, /* type */
695
   0,     /* rightshift */
696
   4,     /* size */
697
   16,      /* bitsize */
698
   false,     /* pc_relative */
699
   0,     /* bitpos */
700
   complain_overflow_dont,/* complain_on_overflow */
701
   bfd_elf_generic_reloc, /* special_function */
702
   "R_ARM_MOVW_ABS_NC", /* name */
703
   false,     /* partial_inplace */
704
   0x000f0fff,    /* src_mask */
705
   0x000f0fff,    /* dst_mask */
706
   false),    /* pcrel_offset */
707
708
  HOWTO (R_ARM_MOVT_ABS,  /* type */
709
   0,     /* rightshift */
710
   4,     /* size */
711
   16,      /* bitsize */
712
   false,     /* pc_relative */
713
   0,     /* bitpos */
714
   complain_overflow_bitfield,/* complain_on_overflow */
715
   bfd_elf_generic_reloc, /* special_function */
716
   "R_ARM_MOVT_ABS",  /* name */
717
   false,     /* partial_inplace */
718
   0x000f0fff,    /* src_mask */
719
   0x000f0fff,    /* dst_mask */
720
   false),    /* pcrel_offset */
721
722
  HOWTO (R_ARM_MOVW_PREL_NC,  /* type */
723
   0,     /* rightshift */
724
   4,     /* size */
725
   16,      /* bitsize */
726
   true,      /* pc_relative */
727
   0,     /* bitpos */
728
   complain_overflow_dont,/* complain_on_overflow */
729
   bfd_elf_generic_reloc, /* special_function */
730
   "R_ARM_MOVW_PREL_NC",  /* name */
731
   false,     /* partial_inplace */
732
   0x000f0fff,    /* src_mask */
733
   0x000f0fff,    /* dst_mask */
734
   true),     /* pcrel_offset */
735
736
  HOWTO (R_ARM_MOVT_PREL, /* type */
737
   0,     /* rightshift */
738
   4,     /* size */
739
   16,      /* bitsize */
740
   true,      /* pc_relative */
741
   0,     /* bitpos */
742
   complain_overflow_bitfield,/* complain_on_overflow */
743
   bfd_elf_generic_reloc, /* special_function */
744
   "R_ARM_MOVT_PREL", /* name */
745
   false,     /* partial_inplace */
746
   0x000f0fff,    /* src_mask */
747
   0x000f0fff,    /* dst_mask */
748
   true),     /* pcrel_offset */
749
750
  HOWTO (R_ARM_THM_MOVW_ABS_NC, /* type */
751
   0,     /* rightshift */
752
   4,     /* size */
753
   16,      /* bitsize */
754
   false,     /* pc_relative */
755
   0,     /* bitpos */
756
   complain_overflow_dont,/* complain_on_overflow */
757
   bfd_elf_generic_reloc, /* special_function */
758
   "R_ARM_THM_MOVW_ABS_NC",/* name */
759
   false,     /* partial_inplace */
760
   0x040f70ff,    /* src_mask */
761
   0x040f70ff,    /* dst_mask */
762
   false),    /* pcrel_offset */
763
764
  HOWTO (R_ARM_THM_MOVT_ABS,  /* type */
765
   0,     /* rightshift */
766
   4,     /* size */
767
   16,      /* bitsize */
768
   false,     /* pc_relative */
769
   0,     /* bitpos */
770
   complain_overflow_bitfield,/* complain_on_overflow */
771
   bfd_elf_generic_reloc, /* special_function */
772
   "R_ARM_THM_MOVT_ABS",  /* name */
773
   false,     /* partial_inplace */
774
   0x040f70ff,    /* src_mask */
775
   0x040f70ff,    /* dst_mask */
776
   false),    /* pcrel_offset */
777
778
  HOWTO (R_ARM_THM_MOVW_PREL_NC,/* type */
779
   0,     /* rightshift */
780
   4,     /* size */
781
   16,      /* bitsize */
782
   true,      /* pc_relative */
783
   0,     /* bitpos */
784
   complain_overflow_dont,/* complain_on_overflow */
785
   bfd_elf_generic_reloc, /* special_function */
786
   "R_ARM_THM_MOVW_PREL_NC",/* name */
787
   false,     /* partial_inplace */
788
   0x040f70ff,    /* src_mask */
789
   0x040f70ff,    /* dst_mask */
790
   true),     /* pcrel_offset */
791
792
  HOWTO (R_ARM_THM_MOVT_PREL, /* type */
793
   0,     /* rightshift */
794
   4,     /* size */
795
   16,      /* bitsize */
796
   true,      /* pc_relative */
797
   0,     /* bitpos */
798
   complain_overflow_bitfield,/* complain_on_overflow */
799
   bfd_elf_generic_reloc, /* special_function */
800
   "R_ARM_THM_MOVT_PREL", /* name */
801
   false,     /* partial_inplace */
802
   0x040f70ff,    /* src_mask */
803
   0x040f70ff,    /* dst_mask */
804
   true),     /* pcrel_offset */
805
806
  HOWTO (R_ARM_THM_JUMP19,  /* type */
807
   1,     /* rightshift */
808
   4,     /* size */
809
   19,      /* bitsize */
810
   true,      /* pc_relative */
811
   0,     /* bitpos */
812
   complain_overflow_signed,/* complain_on_overflow */
813
   bfd_elf_generic_reloc, /* special_function */
814
   "R_ARM_THM_JUMP19",  /* name */
815
   false,     /* partial_inplace */
816
   0x043f2fff,    /* src_mask */
817
   0x043f2fff,    /* dst_mask */
818
   true),     /* pcrel_offset */
819
820
  HOWTO (R_ARM_THM_JUMP6, /* type */
821
   1,     /* rightshift */
822
   2,     /* size */
823
   6,     /* bitsize */
824
   true,      /* pc_relative */
825
   0,     /* bitpos */
826
   complain_overflow_unsigned,/* complain_on_overflow */
827
   bfd_elf_generic_reloc, /* special_function */
828
   "R_ARM_THM_JUMP6", /* name */
829
   false,     /* partial_inplace */
830
   0x02f8,    /* src_mask */
831
   0x02f8,    /* dst_mask */
832
   true),     /* pcrel_offset */
833
834
  /* These are declared as 13-bit signed relocations because we can
835
     address -4095 .. 4095(base) by altering ADDW to SUBW or vice
836
     versa.  */
837
  HOWTO (R_ARM_THM_ALU_PREL_11_0,/* type */
838
   0,     /* rightshift */
839
   4,     /* size */
840
   13,      /* bitsize */
841
   true,      /* pc_relative */
842
   0,     /* bitpos */
843
   complain_overflow_dont,/* complain_on_overflow */
844
   bfd_elf_generic_reloc, /* special_function */
845
   "R_ARM_THM_ALU_PREL_11_0",/* name */
846
   false,     /* partial_inplace */
847
   0xffffffff,    /* src_mask */
848
   0xffffffff,    /* dst_mask */
849
   true),     /* pcrel_offset */
850
851
  HOWTO (R_ARM_THM_PC12,  /* type */
852
   0,     /* rightshift */
853
   4,     /* size */
854
   13,      /* bitsize */
855
   true,      /* pc_relative */
856
   0,     /* bitpos */
857
   complain_overflow_dont,/* complain_on_overflow */
858
   bfd_elf_generic_reloc, /* special_function */
859
   "R_ARM_THM_PC12",  /* name */
860
   false,     /* partial_inplace */
861
   0xffffffff,    /* src_mask */
862
   0xffffffff,    /* dst_mask */
863
   true),     /* pcrel_offset */
864
865
  HOWTO (R_ARM_ABS32_NOI, /* type */
866
   0,     /* rightshift */
867
   4,     /* size */
868
   32,      /* bitsize */
869
   false,     /* pc_relative */
870
   0,     /* bitpos */
871
   complain_overflow_dont,/* complain_on_overflow */
872
   bfd_elf_generic_reloc, /* special_function */
873
   "R_ARM_ABS32_NOI", /* name */
874
   false,     /* partial_inplace */
875
   0xffffffff,    /* src_mask */
876
   0xffffffff,    /* dst_mask */
877
   false),    /* pcrel_offset */
878
879
  HOWTO (R_ARM_REL32_NOI, /* type */
880
   0,     /* rightshift */
881
   4,     /* size */
882
   32,      /* bitsize */
883
   true,      /* pc_relative */
884
   0,     /* bitpos */
885
   complain_overflow_dont,/* complain_on_overflow */
886
   bfd_elf_generic_reloc, /* special_function */
887
   "R_ARM_REL32_NOI", /* name */
888
   false,     /* partial_inplace */
889
   0xffffffff,    /* src_mask */
890
   0xffffffff,    /* dst_mask */
891
   false),    /* pcrel_offset */
892
893
  /* Group relocations.  */
894
895
  HOWTO (R_ARM_ALU_PC_G0_NC,  /* type */
896
   0,     /* rightshift */
897
   4,     /* size */
898
   32,      /* bitsize */
899
   true,      /* pc_relative */
900
   0,     /* bitpos */
901
   complain_overflow_dont,/* complain_on_overflow */
902
   bfd_elf_generic_reloc, /* special_function */
903
   "R_ARM_ALU_PC_G0_NC",  /* name */
904
   false,     /* partial_inplace */
905
   0xffffffff,    /* src_mask */
906
   0xffffffff,    /* dst_mask */
907
   true),     /* pcrel_offset */
908
909
  HOWTO (R_ARM_ALU_PC_G0, /* type */
910
   0,     /* rightshift */
911
   4,     /* size */
912
   32,      /* bitsize */
913
   true,      /* pc_relative */
914
   0,     /* bitpos */
915
   complain_overflow_dont,/* complain_on_overflow */
916
   bfd_elf_generic_reloc, /* special_function */
917
   "R_ARM_ALU_PC_G0", /* name */
918
   false,     /* partial_inplace */
919
   0xffffffff,    /* src_mask */
920
   0xffffffff,    /* dst_mask */
921
   true),     /* pcrel_offset */
922
923
  HOWTO (R_ARM_ALU_PC_G1_NC,  /* type */
924
   0,     /* rightshift */
925
   4,     /* size */
926
   32,      /* bitsize */
927
   true,      /* pc_relative */
928
   0,     /* bitpos */
929
   complain_overflow_dont,/* complain_on_overflow */
930
   bfd_elf_generic_reloc, /* special_function */
931
   "R_ARM_ALU_PC_G1_NC",  /* name */
932
   false,     /* partial_inplace */
933
   0xffffffff,    /* src_mask */
934
   0xffffffff,    /* dst_mask */
935
   true),     /* pcrel_offset */
936
937
  HOWTO (R_ARM_ALU_PC_G1, /* type */
938
   0,     /* rightshift */
939
   4,     /* size */
940
   32,      /* bitsize */
941
   true,      /* pc_relative */
942
   0,     /* bitpos */
943
   complain_overflow_dont,/* complain_on_overflow */
944
   bfd_elf_generic_reloc, /* special_function */
945
   "R_ARM_ALU_PC_G1", /* name */
946
   false,     /* partial_inplace */
947
   0xffffffff,    /* src_mask */
948
   0xffffffff,    /* dst_mask */
949
   true),     /* pcrel_offset */
950
951
  HOWTO (R_ARM_ALU_PC_G2, /* type */
952
   0,     /* rightshift */
953
   4,     /* size */
954
   32,      /* bitsize */
955
   true,      /* pc_relative */
956
   0,     /* bitpos */
957
   complain_overflow_dont,/* complain_on_overflow */
958
   bfd_elf_generic_reloc, /* special_function */
959
   "R_ARM_ALU_PC_G2", /* name */
960
   false,     /* partial_inplace */
961
   0xffffffff,    /* src_mask */
962
   0xffffffff,    /* dst_mask */
963
   true),     /* pcrel_offset */
964
965
  HOWTO (R_ARM_LDR_PC_G1, /* type */
966
   0,     /* rightshift */
967
   4,     /* size */
968
   32,      /* bitsize */
969
   true,      /* pc_relative */
970
   0,     /* bitpos */
971
   complain_overflow_dont,/* complain_on_overflow */
972
   bfd_elf_generic_reloc, /* special_function */
973
   "R_ARM_LDR_PC_G1", /* name */
974
   false,     /* partial_inplace */
975
   0xffffffff,    /* src_mask */
976
   0xffffffff,    /* dst_mask */
977
   true),     /* pcrel_offset */
978
979
  HOWTO (R_ARM_LDR_PC_G2, /* type */
980
   0,     /* rightshift */
981
   4,     /* size */
982
   32,      /* bitsize */
983
   true,      /* pc_relative */
984
   0,     /* bitpos */
985
   complain_overflow_dont,/* complain_on_overflow */
986
   bfd_elf_generic_reloc, /* special_function */
987
   "R_ARM_LDR_PC_G2", /* name */
988
   false,     /* partial_inplace */
989
   0xffffffff,    /* src_mask */
990
   0xffffffff,    /* dst_mask */
991
   true),     /* pcrel_offset */
992
993
  HOWTO (R_ARM_LDRS_PC_G0,  /* type */
994
   0,     /* rightshift */
995
   4,     /* size */
996
   32,      /* bitsize */
997
   true,      /* pc_relative */
998
   0,     /* bitpos */
999
   complain_overflow_dont,/* complain_on_overflow */
1000
   bfd_elf_generic_reloc, /* special_function */
1001
   "R_ARM_LDRS_PC_G0",  /* name */
1002
   false,     /* partial_inplace */
1003
   0xffffffff,    /* src_mask */
1004
   0xffffffff,    /* dst_mask */
1005
   true),     /* pcrel_offset */
1006
1007
  HOWTO (R_ARM_LDRS_PC_G1,  /* type */
1008
   0,     /* rightshift */
1009
   4,     /* size */
1010
   32,      /* bitsize */
1011
   true,      /* pc_relative */
1012
   0,     /* bitpos */
1013
   complain_overflow_dont,/* complain_on_overflow */
1014
   bfd_elf_generic_reloc, /* special_function */
1015
   "R_ARM_LDRS_PC_G1",  /* name */
1016
   false,     /* partial_inplace */
1017
   0xffffffff,    /* src_mask */
1018
   0xffffffff,    /* dst_mask */
1019
   true),     /* pcrel_offset */
1020
1021
  HOWTO (R_ARM_LDRS_PC_G2,  /* type */
1022
   0,     /* rightshift */
1023
   4,     /* size */
1024
   32,      /* bitsize */
1025
   true,      /* pc_relative */
1026
   0,     /* bitpos */
1027
   complain_overflow_dont,/* complain_on_overflow */
1028
   bfd_elf_generic_reloc, /* special_function */
1029
   "R_ARM_LDRS_PC_G2",  /* name */
1030
   false,     /* partial_inplace */
1031
   0xffffffff,    /* src_mask */
1032
   0xffffffff,    /* dst_mask */
1033
   true),     /* pcrel_offset */
1034
1035
  HOWTO (R_ARM_LDC_PC_G0, /* type */
1036
   0,     /* rightshift */
1037
   4,     /* size */
1038
   32,      /* bitsize */
1039
   true,      /* pc_relative */
1040
   0,     /* bitpos */
1041
   complain_overflow_dont,/* complain_on_overflow */
1042
   bfd_elf_generic_reloc, /* special_function */
1043
   "R_ARM_LDC_PC_G0", /* name */
1044
   false,     /* partial_inplace */
1045
   0xffffffff,    /* src_mask */
1046
   0xffffffff,    /* dst_mask */
1047
   true),     /* pcrel_offset */
1048
1049
  HOWTO (R_ARM_LDC_PC_G1, /* type */
1050
   0,     /* rightshift */
1051
   4,     /* size */
1052
   32,      /* bitsize */
1053
   true,      /* pc_relative */
1054
   0,     /* bitpos */
1055
   complain_overflow_dont,/* complain_on_overflow */
1056
   bfd_elf_generic_reloc, /* special_function */
1057
   "R_ARM_LDC_PC_G1", /* name */
1058
   false,     /* partial_inplace */
1059
   0xffffffff,    /* src_mask */
1060
   0xffffffff,    /* dst_mask */
1061
   true),     /* pcrel_offset */
1062
1063
  HOWTO (R_ARM_LDC_PC_G2, /* type */
1064
   0,     /* rightshift */
1065
   4,     /* size */
1066
   32,      /* bitsize */
1067
   true,      /* pc_relative */
1068
   0,     /* bitpos */
1069
   complain_overflow_dont,/* complain_on_overflow */
1070
   bfd_elf_generic_reloc, /* special_function */
1071
   "R_ARM_LDC_PC_G2", /* name */
1072
   false,     /* partial_inplace */
1073
   0xffffffff,    /* src_mask */
1074
   0xffffffff,    /* dst_mask */
1075
   true),     /* pcrel_offset */
1076
1077
  HOWTO (R_ARM_ALU_SB_G0_NC,  /* type */
1078
   0,     /* rightshift */
1079
   4,     /* size */
1080
   32,      /* bitsize */
1081
   true,      /* pc_relative */
1082
   0,     /* bitpos */
1083
   complain_overflow_dont,/* complain_on_overflow */
1084
   bfd_elf_generic_reloc, /* special_function */
1085
   "R_ARM_ALU_SB_G0_NC",  /* name */
1086
   false,     /* partial_inplace */
1087
   0xffffffff,    /* src_mask */
1088
   0xffffffff,    /* dst_mask */
1089
   true),     /* pcrel_offset */
1090
1091
  HOWTO (R_ARM_ALU_SB_G0, /* type */
1092
   0,     /* rightshift */
1093
   4,     /* size */
1094
   32,      /* bitsize */
1095
   true,      /* pc_relative */
1096
   0,     /* bitpos */
1097
   complain_overflow_dont,/* complain_on_overflow */
1098
   bfd_elf_generic_reloc, /* special_function */
1099
   "R_ARM_ALU_SB_G0", /* name */
1100
   false,     /* partial_inplace */
1101
   0xffffffff,    /* src_mask */
1102
   0xffffffff,    /* dst_mask */
1103
   true),     /* pcrel_offset */
1104
1105
  HOWTO (R_ARM_ALU_SB_G1_NC,  /* type */
1106
   0,     /* rightshift */
1107
   4,     /* size */
1108
   32,      /* bitsize */
1109
   true,      /* pc_relative */
1110
   0,     /* bitpos */
1111
   complain_overflow_dont,/* complain_on_overflow */
1112
   bfd_elf_generic_reloc, /* special_function */
1113
   "R_ARM_ALU_SB_G1_NC",  /* name */
1114
   false,     /* partial_inplace */
1115
   0xffffffff,    /* src_mask */
1116
   0xffffffff,    /* dst_mask */
1117
   true),     /* pcrel_offset */
1118
1119
  HOWTO (R_ARM_ALU_SB_G1, /* type */
1120
   0,     /* rightshift */
1121
   4,     /* size */
1122
   32,      /* bitsize */
1123
   true,      /* pc_relative */
1124
   0,     /* bitpos */
1125
   complain_overflow_dont,/* complain_on_overflow */
1126
   bfd_elf_generic_reloc, /* special_function */
1127
   "R_ARM_ALU_SB_G1", /* name */
1128
   false,     /* partial_inplace */
1129
   0xffffffff,    /* src_mask */
1130
   0xffffffff,    /* dst_mask */
1131
   true),     /* pcrel_offset */
1132
1133
  HOWTO (R_ARM_ALU_SB_G2, /* type */
1134
   0,     /* rightshift */
1135
   4,     /* size */
1136
   32,      /* bitsize */
1137
   true,      /* pc_relative */
1138
   0,     /* bitpos */
1139
   complain_overflow_dont,/* complain_on_overflow */
1140
   bfd_elf_generic_reloc, /* special_function */
1141
   "R_ARM_ALU_SB_G2", /* name */
1142
   false,     /* partial_inplace */
1143
   0xffffffff,    /* src_mask */
1144
   0xffffffff,    /* dst_mask */
1145
   true),     /* pcrel_offset */
1146
1147
  HOWTO (R_ARM_LDR_SB_G0, /* type */
1148
   0,     /* rightshift */
1149
   4,     /* size */
1150
   32,      /* bitsize */
1151
   true,      /* pc_relative */
1152
   0,     /* bitpos */
1153
   complain_overflow_dont,/* complain_on_overflow */
1154
   bfd_elf_generic_reloc, /* special_function */
1155
   "R_ARM_LDR_SB_G0", /* name */
1156
   false,     /* partial_inplace */
1157
   0xffffffff,    /* src_mask */
1158
   0xffffffff,    /* dst_mask */
1159
   true),     /* pcrel_offset */
1160
1161
  HOWTO (R_ARM_LDR_SB_G1, /* type */
1162
   0,     /* rightshift */
1163
   4,     /* size */
1164
   32,      /* bitsize */
1165
   true,      /* pc_relative */
1166
   0,     /* bitpos */
1167
   complain_overflow_dont,/* complain_on_overflow */
1168
   bfd_elf_generic_reloc, /* special_function */
1169
   "R_ARM_LDR_SB_G1", /* name */
1170
   false,     /* partial_inplace */
1171
   0xffffffff,    /* src_mask */
1172
   0xffffffff,    /* dst_mask */
1173
   true),     /* pcrel_offset */
1174
1175
  HOWTO (R_ARM_LDR_SB_G2, /* type */
1176
   0,     /* rightshift */
1177
   4,     /* size */
1178
   32,      /* bitsize */
1179
   true,      /* pc_relative */
1180
   0,     /* bitpos */
1181
   complain_overflow_dont,/* complain_on_overflow */
1182
   bfd_elf_generic_reloc, /* special_function */
1183
   "R_ARM_LDR_SB_G2", /* name */
1184
   false,     /* partial_inplace */
1185
   0xffffffff,    /* src_mask */
1186
   0xffffffff,    /* dst_mask */
1187
   true),     /* pcrel_offset */
1188
1189
  HOWTO (R_ARM_LDRS_SB_G0,  /* type */
1190
   0,     /* rightshift */
1191
   4,     /* size */
1192
   32,      /* bitsize */
1193
   true,      /* pc_relative */
1194
   0,     /* bitpos */
1195
   complain_overflow_dont,/* complain_on_overflow */
1196
   bfd_elf_generic_reloc, /* special_function */
1197
   "R_ARM_LDRS_SB_G0",  /* name */
1198
   false,     /* partial_inplace */
1199
   0xffffffff,    /* src_mask */
1200
   0xffffffff,    /* dst_mask */
1201
   true),     /* pcrel_offset */
1202
1203
  HOWTO (R_ARM_LDRS_SB_G1,  /* type */
1204
   0,     /* rightshift */
1205
   4,     /* size */
1206
   32,      /* bitsize */
1207
   true,      /* pc_relative */
1208
   0,     /* bitpos */
1209
   complain_overflow_dont,/* complain_on_overflow */
1210
   bfd_elf_generic_reloc, /* special_function */
1211
   "R_ARM_LDRS_SB_G1",  /* name */
1212
   false,     /* partial_inplace */
1213
   0xffffffff,    /* src_mask */
1214
   0xffffffff,    /* dst_mask */
1215
   true),     /* pcrel_offset */
1216
1217
  HOWTO (R_ARM_LDRS_SB_G2,  /* type */
1218
   0,     /* rightshift */
1219
   4,     /* size */
1220
   32,      /* bitsize */
1221
   true,      /* pc_relative */
1222
   0,     /* bitpos */
1223
   complain_overflow_dont,/* complain_on_overflow */
1224
   bfd_elf_generic_reloc, /* special_function */
1225
   "R_ARM_LDRS_SB_G2",  /* name */
1226
   false,     /* partial_inplace */
1227
   0xffffffff,    /* src_mask */
1228
   0xffffffff,    /* dst_mask */
1229
   true),     /* pcrel_offset */
1230
1231
  HOWTO (R_ARM_LDC_SB_G0, /* type */
1232
   0,     /* rightshift */
1233
   4,     /* size */
1234
   32,      /* bitsize */
1235
   true,      /* pc_relative */
1236
   0,     /* bitpos */
1237
   complain_overflow_dont,/* complain_on_overflow */
1238
   bfd_elf_generic_reloc, /* special_function */
1239
   "R_ARM_LDC_SB_G0", /* name */
1240
   false,     /* partial_inplace */
1241
   0xffffffff,    /* src_mask */
1242
   0xffffffff,    /* dst_mask */
1243
   true),     /* pcrel_offset */
1244
1245
  HOWTO (R_ARM_LDC_SB_G1, /* type */
1246
   0,     /* rightshift */
1247
   4,     /* size */
1248
   32,      /* bitsize */
1249
   true,      /* pc_relative */
1250
   0,     /* bitpos */
1251
   complain_overflow_dont,/* complain_on_overflow */
1252
   bfd_elf_generic_reloc, /* special_function */
1253
   "R_ARM_LDC_SB_G1", /* name */
1254
   false,     /* partial_inplace */
1255
   0xffffffff,    /* src_mask */
1256
   0xffffffff,    /* dst_mask */
1257
   true),     /* pcrel_offset */
1258
1259
  HOWTO (R_ARM_LDC_SB_G2, /* type */
1260
   0,     /* rightshift */
1261
   4,     /* size */
1262
   32,      /* bitsize */
1263
   true,      /* pc_relative */
1264
   0,     /* bitpos */
1265
   complain_overflow_dont,/* complain_on_overflow */
1266
   bfd_elf_generic_reloc, /* special_function */
1267
   "R_ARM_LDC_SB_G2", /* name */
1268
   false,     /* partial_inplace */
1269
   0xffffffff,    /* src_mask */
1270
   0xffffffff,    /* dst_mask */
1271
   true),     /* pcrel_offset */
1272
1273
  /* End of group relocations.  */
1274
1275
  HOWTO (R_ARM_MOVW_BREL_NC,  /* type */
1276
   0,     /* rightshift */
1277
   4,     /* size */
1278
   16,      /* bitsize */
1279
   false,     /* pc_relative */
1280
   0,     /* bitpos */
1281
   complain_overflow_dont,/* complain_on_overflow */
1282
   bfd_elf_generic_reloc, /* special_function */
1283
   "R_ARM_MOVW_BREL_NC",  /* name */
1284
   false,     /* partial_inplace */
1285
   0x0000ffff,    /* src_mask */
1286
   0x0000ffff,    /* dst_mask */
1287
   false),    /* pcrel_offset */
1288
1289
  HOWTO (R_ARM_MOVT_BREL, /* type */
1290
   0,     /* rightshift */
1291
   4,     /* size */
1292
   16,      /* bitsize */
1293
   false,     /* pc_relative */
1294
   0,     /* bitpos */
1295
   complain_overflow_bitfield,/* complain_on_overflow */
1296
   bfd_elf_generic_reloc, /* special_function */
1297
   "R_ARM_MOVT_BREL", /* name */
1298
   false,     /* partial_inplace */
1299
   0x0000ffff,    /* src_mask */
1300
   0x0000ffff,    /* dst_mask */
1301
   false),    /* pcrel_offset */
1302
1303
  HOWTO (R_ARM_MOVW_BREL, /* type */
1304
   0,     /* rightshift */
1305
   4,     /* size */
1306
   16,      /* bitsize */
1307
   false,     /* pc_relative */
1308
   0,     /* bitpos */
1309
   complain_overflow_dont,/* complain_on_overflow */
1310
   bfd_elf_generic_reloc, /* special_function */
1311
   "R_ARM_MOVW_BREL", /* name */
1312
   false,     /* partial_inplace */
1313
   0x0000ffff,    /* src_mask */
1314
   0x0000ffff,    /* dst_mask */
1315
   false),    /* pcrel_offset */
1316
1317
  HOWTO (R_ARM_THM_MOVW_BREL_NC,/* type */
1318
   0,     /* rightshift */
1319
   4,     /* size */
1320
   16,      /* bitsize */
1321
   false,     /* pc_relative */
1322
   0,     /* bitpos */
1323
   complain_overflow_dont,/* complain_on_overflow */
1324
   bfd_elf_generic_reloc, /* special_function */
1325
   "R_ARM_THM_MOVW_BREL_NC",/* name */
1326
   false,     /* partial_inplace */
1327
   0x040f70ff,    /* src_mask */
1328
   0x040f70ff,    /* dst_mask */
1329
   false),    /* pcrel_offset */
1330
1331
  HOWTO (R_ARM_THM_MOVT_BREL, /* type */
1332
   0,     /* rightshift */
1333
   4,     /* size */
1334
   16,      /* bitsize */
1335
   false,     /* pc_relative */
1336
   0,     /* bitpos */
1337
   complain_overflow_bitfield,/* complain_on_overflow */
1338
   bfd_elf_generic_reloc, /* special_function */
1339
   "R_ARM_THM_MOVT_BREL", /* name */
1340
   false,     /* partial_inplace */
1341
   0x040f70ff,    /* src_mask */
1342
   0x040f70ff,    /* dst_mask */
1343
   false),    /* pcrel_offset */
1344
1345
  HOWTO (R_ARM_THM_MOVW_BREL, /* type */
1346
   0,     /* rightshift */
1347
   4,     /* size */
1348
   16,      /* bitsize */
1349
   false,     /* pc_relative */
1350
   0,     /* bitpos */
1351
   complain_overflow_dont,/* complain_on_overflow */
1352
   bfd_elf_generic_reloc, /* special_function */
1353
   "R_ARM_THM_MOVW_BREL", /* name */
1354
   false,     /* partial_inplace */
1355
   0x040f70ff,    /* src_mask */
1356
   0x040f70ff,    /* dst_mask */
1357
   false),    /* pcrel_offset */
1358
1359
  HOWTO (R_ARM_TLS_GOTDESC, /* type */
1360
   0,     /* rightshift */
1361
   4,     /* size */
1362
   32,      /* bitsize */
1363
   false,     /* pc_relative */
1364
   0,     /* bitpos */
1365
   complain_overflow_bitfield,/* complain_on_overflow */
1366
   NULL,      /* special_function */
1367
   "R_ARM_TLS_GOTDESC", /* name */
1368
   true,      /* partial_inplace */
1369
   0xffffffff,    /* src_mask */
1370
   0xffffffff,    /* dst_mask */
1371
   false),    /* pcrel_offset */
1372
1373
  HOWTO (R_ARM_TLS_CALL,  /* type */
1374
   0,     /* rightshift */
1375
   4,     /* size */
1376
   24,      /* bitsize */
1377
   false,     /* pc_relative */
1378
   0,     /* bitpos */
1379
   complain_overflow_dont,/* complain_on_overflow */
1380
   bfd_elf_generic_reloc, /* special_function */
1381
   "R_ARM_TLS_CALL",  /* name */
1382
   false,     /* partial_inplace */
1383
   0x00ffffff,    /* src_mask */
1384
   0x00ffffff,    /* dst_mask */
1385
   false),    /* pcrel_offset */
1386
1387
  HOWTO (R_ARM_TLS_DESCSEQ, /* type */
1388
   0,     /* rightshift */
1389
   4,     /* size */
1390
   0,     /* bitsize */
1391
   false,     /* pc_relative */
1392
   0,     /* bitpos */
1393
   complain_overflow_dont,/* complain_on_overflow */
1394
   bfd_elf_generic_reloc, /* special_function */
1395
   "R_ARM_TLS_DESCSEQ", /* name */
1396
   false,     /* partial_inplace */
1397
   0x00000000,    /* src_mask */
1398
   0x00000000,    /* dst_mask */
1399
   false),    /* pcrel_offset */
1400
1401
  HOWTO (R_ARM_THM_TLS_CALL,  /* type */
1402
   0,     /* rightshift */
1403
   4,     /* size */
1404
   24,      /* bitsize */
1405
   false,     /* pc_relative */
1406
   0,     /* bitpos */
1407
   complain_overflow_dont,/* complain_on_overflow */
1408
   bfd_elf_generic_reloc, /* special_function */
1409
   "R_ARM_THM_TLS_CALL",  /* name */
1410
   false,     /* partial_inplace */
1411
   0x07ff07ff,    /* src_mask */
1412
   0x07ff07ff,    /* dst_mask */
1413
   false),    /* pcrel_offset */
1414
1415
  HOWTO (R_ARM_PLT32_ABS, /* type */
1416
   0,     /* rightshift */
1417
   4,     /* size */
1418
   32,      /* bitsize */
1419
   false,     /* pc_relative */
1420
   0,     /* bitpos */
1421
   complain_overflow_dont,/* complain_on_overflow */
1422
   bfd_elf_generic_reloc, /* special_function */
1423
   "R_ARM_PLT32_ABS", /* name */
1424
   false,     /* partial_inplace */
1425
   0xffffffff,    /* src_mask */
1426
   0xffffffff,    /* dst_mask */
1427
   false),    /* pcrel_offset */
1428
1429
  HOWTO (R_ARM_GOT_ABS,   /* type */
1430
   0,     /* rightshift */
1431
   4,     /* size */
1432
   32,      /* bitsize */
1433
   false,     /* pc_relative */
1434
   0,     /* bitpos */
1435
   complain_overflow_dont,/* complain_on_overflow */
1436
   bfd_elf_generic_reloc, /* special_function */
1437
   "R_ARM_GOT_ABS", /* name */
1438
   false,     /* partial_inplace */
1439
   0xffffffff,    /* src_mask */
1440
   0xffffffff,    /* dst_mask */
1441
   false),      /* pcrel_offset */
1442
1443
  HOWTO (R_ARM_GOT_PREL,  /* type */
1444
   0,     /* rightshift */
1445
   4,     /* size */
1446
   32,      /* bitsize */
1447
   true,      /* pc_relative */
1448
   0,     /* bitpos */
1449
   complain_overflow_dont,  /* complain_on_overflow */
1450
   bfd_elf_generic_reloc, /* special_function */
1451
   "R_ARM_GOT_PREL",  /* name */
1452
   false,     /* partial_inplace */
1453
   0xffffffff,    /* src_mask */
1454
   0xffffffff,    /* dst_mask */
1455
   true),     /* pcrel_offset */
1456
1457
  HOWTO (R_ARM_GOT_BREL12,  /* type */
1458
   0,     /* rightshift */
1459
   4,     /* size */
1460
   12,      /* bitsize */
1461
   false,     /* pc_relative */
1462
   0,     /* bitpos */
1463
   complain_overflow_bitfield,/* complain_on_overflow */
1464
   bfd_elf_generic_reloc, /* special_function */
1465
   "R_ARM_GOT_BREL12",  /* name */
1466
   false,     /* partial_inplace */
1467
   0x00000fff,    /* src_mask */
1468
   0x00000fff,    /* dst_mask */
1469
   false),    /* pcrel_offset */
1470
1471
  HOWTO (R_ARM_GOTOFF12,  /* type */
1472
   0,     /* rightshift */
1473
   4,     /* size */
1474
   12,      /* bitsize */
1475
   false,     /* pc_relative */
1476
   0,     /* bitpos */
1477
   complain_overflow_bitfield,/* complain_on_overflow */
1478
   bfd_elf_generic_reloc, /* special_function */
1479
   "R_ARM_GOTOFF12",  /* name */
1480
   false,     /* partial_inplace */
1481
   0x00000fff,    /* src_mask */
1482
   0x00000fff,    /* dst_mask */
1483
   false),    /* pcrel_offset */
1484
1485
  EMPTY_HOWTO (R_ARM_GOTRELAX),  /* reserved for future GOT-load optimizations */
1486
1487
  /* GNU extension to record C++ vtable member usage */
1488
  HOWTO (R_ARM_GNU_VTENTRY, /* type */
1489
   0,     /* rightshift */
1490
   4,     /* size */
1491
   0,     /* bitsize */
1492
   false,     /* pc_relative */
1493
   0,     /* bitpos */
1494
   complain_overflow_dont, /* complain_on_overflow */
1495
   _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
1496
   "R_ARM_GNU_VTENTRY", /* name */
1497
   false,     /* partial_inplace */
1498
   0,     /* src_mask */
1499
   0,     /* dst_mask */
1500
   false),    /* pcrel_offset */
1501
1502
  /* GNU extension to record C++ vtable hierarchy */
1503
  HOWTO (R_ARM_GNU_VTINHERIT, /* type */
1504
   0,     /* rightshift */
1505
   4,     /* size */
1506
   0,     /* bitsize */
1507
   false,     /* pc_relative */
1508
   0,     /* bitpos */
1509
   complain_overflow_dont, /* complain_on_overflow */
1510
   NULL,      /* special_function */
1511
   "R_ARM_GNU_VTINHERIT", /* name */
1512
   false,     /* partial_inplace */
1513
   0,     /* src_mask */
1514
   0,     /* dst_mask */
1515
   false),    /* pcrel_offset */
1516
1517
  HOWTO (R_ARM_THM_JUMP11,  /* type */
1518
   1,     /* rightshift */
1519
   2,     /* size */
1520
   11,      /* bitsize */
1521
   true,      /* pc_relative */
1522
   0,     /* bitpos */
1523
   complain_overflow_signed,  /* complain_on_overflow */
1524
   bfd_elf_generic_reloc, /* special_function */
1525
   "R_ARM_THM_JUMP11",  /* name */
1526
   false,     /* partial_inplace */
1527
   0x000007ff,    /* src_mask */
1528
   0x000007ff,    /* dst_mask */
1529
   true),     /* pcrel_offset */
1530
1531
  HOWTO (R_ARM_THM_JUMP8, /* type */
1532
   1,     /* rightshift */
1533
   2,     /* size */
1534
   8,     /* bitsize */
1535
   true,      /* pc_relative */
1536
   0,     /* bitpos */
1537
   complain_overflow_signed,  /* complain_on_overflow */
1538
   bfd_elf_generic_reloc, /* special_function */
1539
   "R_ARM_THM_JUMP8", /* name */
1540
   false,     /* partial_inplace */
1541
   0x000000ff,    /* src_mask */
1542
   0x000000ff,    /* dst_mask */
1543
   true),     /* pcrel_offset */
1544
1545
  /* TLS relocations */
1546
  HOWTO (R_ARM_TLS_GD32,  /* type */
1547
   0,     /* rightshift */
1548
   4,     /* size */
1549
   32,      /* bitsize */
1550
   false,     /* pc_relative */
1551
   0,     /* bitpos */
1552
   complain_overflow_bitfield,/* complain_on_overflow */
1553
   NULL,      /* special_function */
1554
   "R_ARM_TLS_GD32",  /* name */
1555
   true,      /* partial_inplace */
1556
   0xffffffff,    /* src_mask */
1557
   0xffffffff,    /* dst_mask */
1558
   false),    /* pcrel_offset */
1559
1560
  HOWTO (R_ARM_TLS_LDM32, /* type */
1561
   0,     /* rightshift */
1562
   4,     /* size */
1563
   32,      /* bitsize */
1564
   false,     /* pc_relative */
1565
   0,     /* bitpos */
1566
   complain_overflow_bitfield,/* complain_on_overflow */
1567
   bfd_elf_generic_reloc, /* special_function */
1568
   "R_ARM_TLS_LDM32", /* name */
1569
   true,      /* partial_inplace */
1570
   0xffffffff,    /* src_mask */
1571
   0xffffffff,    /* dst_mask */
1572
   false),    /* pcrel_offset */
1573
1574
  HOWTO (R_ARM_TLS_LDO32, /* type */
1575
   0,     /* rightshift */
1576
   4,     /* size */
1577
   32,      /* bitsize */
1578
   false,     /* pc_relative */
1579
   0,     /* bitpos */
1580
   complain_overflow_bitfield,/* complain_on_overflow */
1581
   bfd_elf_generic_reloc, /* special_function */
1582
   "R_ARM_TLS_LDO32", /* name */
1583
   true,      /* partial_inplace */
1584
   0xffffffff,    /* src_mask */
1585
   0xffffffff,    /* dst_mask */
1586
   false),    /* pcrel_offset */
1587
1588
  HOWTO (R_ARM_TLS_IE32,  /* type */
1589
   0,     /* rightshift */
1590
   4,     /* size */
1591
   32,      /* bitsize */
1592
   false,      /* pc_relative */
1593
   0,     /* bitpos */
1594
   complain_overflow_bitfield,/* complain_on_overflow */
1595
   NULL,      /* special_function */
1596
   "R_ARM_TLS_IE32",  /* name */
1597
   true,      /* partial_inplace */
1598
   0xffffffff,    /* src_mask */
1599
   0xffffffff,    /* dst_mask */
1600
   false),    /* pcrel_offset */
1601
1602
  HOWTO (R_ARM_TLS_LE32,  /* type */
1603
   0,     /* rightshift */
1604
   4,     /* size */
1605
   32,      /* bitsize */
1606
   false,     /* pc_relative */
1607
   0,     /* bitpos */
1608
   complain_overflow_bitfield,/* complain_on_overflow */
1609
   NULL,      /* special_function */
1610
   "R_ARM_TLS_LE32",  /* name */
1611
   true,      /* partial_inplace */
1612
   0xffffffff,    /* src_mask */
1613
   0xffffffff,    /* dst_mask */
1614
   false),    /* pcrel_offset */
1615
1616
  HOWTO (R_ARM_TLS_LDO12, /* type */
1617
   0,     /* rightshift */
1618
   4,     /* size */
1619
   12,      /* bitsize */
1620
   false,     /* pc_relative */
1621
   0,     /* bitpos */
1622
   complain_overflow_bitfield,/* complain_on_overflow */
1623
   bfd_elf_generic_reloc, /* special_function */
1624
   "R_ARM_TLS_LDO12", /* name */
1625
   false,     /* partial_inplace */
1626
   0x00000fff,    /* src_mask */
1627
   0x00000fff,    /* dst_mask */
1628
   false),    /* pcrel_offset */
1629
1630
  HOWTO (R_ARM_TLS_LE12,  /* type */
1631
   0,     /* rightshift */
1632
   4,     /* size */
1633
   12,      /* bitsize */
1634
   false,     /* pc_relative */
1635
   0,     /* bitpos */
1636
   complain_overflow_bitfield,/* complain_on_overflow */
1637
   bfd_elf_generic_reloc, /* special_function */
1638
   "R_ARM_TLS_LE12",  /* name */
1639
   false,     /* partial_inplace */
1640
   0x00000fff,    /* src_mask */
1641
   0x00000fff,    /* dst_mask */
1642
   false),    /* pcrel_offset */
1643
1644
  HOWTO (R_ARM_TLS_IE12GP,  /* type */
1645
   0,     /* rightshift */
1646
   4,     /* size */
1647
   12,      /* bitsize */
1648
   false,     /* pc_relative */
1649
   0,     /* bitpos */
1650
   complain_overflow_bitfield,/* complain_on_overflow */
1651
   bfd_elf_generic_reloc, /* special_function */
1652
   "R_ARM_TLS_IE12GP",  /* name */
1653
   false,     /* partial_inplace */
1654
   0x00000fff,    /* src_mask */
1655
   0x00000fff,    /* dst_mask */
1656
   false),    /* pcrel_offset */
1657
1658
  /* 112-127 private relocations.  */
1659
  EMPTY_HOWTO (112),
1660
  EMPTY_HOWTO (113),
1661
  EMPTY_HOWTO (114),
1662
  EMPTY_HOWTO (115),
1663
  EMPTY_HOWTO (116),
1664
  EMPTY_HOWTO (117),
1665
  EMPTY_HOWTO (118),
1666
  EMPTY_HOWTO (119),
1667
  EMPTY_HOWTO (120),
1668
  EMPTY_HOWTO (121),
1669
  EMPTY_HOWTO (122),
1670
  EMPTY_HOWTO (123),
1671
  EMPTY_HOWTO (124),
1672
  EMPTY_HOWTO (125),
1673
  EMPTY_HOWTO (126),
1674
  EMPTY_HOWTO (127),
1675
1676
  /* R_ARM_ME_TOO, obsolete.  */
1677
  EMPTY_HOWTO (128),
1678
1679
  HOWTO (R_ARM_THM_TLS_DESCSEQ, /* type */
1680
   0,     /* rightshift */
1681
   2,     /* size */
1682
   0,     /* bitsize */
1683
   false,     /* pc_relative */
1684
   0,     /* bitpos */
1685
   complain_overflow_dont,/* complain_on_overflow */
1686
   bfd_elf_generic_reloc, /* special_function */
1687
   "R_ARM_THM_TLS_DESCSEQ",/* name */
1688
   false,     /* partial_inplace */
1689
   0x00000000,    /* src_mask */
1690
   0x00000000,    /* dst_mask */
1691
   false),    /* pcrel_offset */
1692
  EMPTY_HOWTO (130),
1693
  EMPTY_HOWTO (131),
1694
  HOWTO (R_ARM_THM_ALU_ABS_G0_NC,/* type.  */
1695
   0,     /* rightshift.  */
1696
   2,     /* size.  */
1697
   16,      /* bitsize.  */
1698
   false,     /* pc_relative.  */
1699
   0,     /* bitpos.  */
1700
   complain_overflow_bitfield,/* complain_on_overflow.  */
1701
   bfd_elf_generic_reloc, /* special_function.  */
1702
   "R_ARM_THM_ALU_ABS_G0_NC",/* name.  */
1703
   false,     /* partial_inplace.  */
1704
   0x00000000,    /* src_mask.  */
1705
   0x00000000,    /* dst_mask.  */
1706
   false),    /* pcrel_offset.  */
1707
  HOWTO (R_ARM_THM_ALU_ABS_G1_NC,/* type.  */
1708
   0,     /* rightshift.  */
1709
   2,     /* size.  */
1710
   16,      /* bitsize.  */
1711
   false,     /* pc_relative.  */
1712
   0,     /* bitpos.  */
1713
   complain_overflow_bitfield,/* complain_on_overflow.  */
1714
   bfd_elf_generic_reloc, /* special_function.  */
1715
   "R_ARM_THM_ALU_ABS_G1_NC",/* name.  */
1716
   false,     /* partial_inplace.  */
1717
   0x00000000,    /* src_mask.  */
1718
   0x00000000,    /* dst_mask.  */
1719
   false),    /* pcrel_offset.  */
1720
  HOWTO (R_ARM_THM_ALU_ABS_G2_NC,/* type.  */
1721
   0,     /* rightshift.  */
1722
   2,     /* size.  */
1723
   16,      /* bitsize.  */
1724
   false,     /* pc_relative.  */
1725
   0,     /* bitpos.  */
1726
   complain_overflow_bitfield,/* complain_on_overflow.  */
1727
   bfd_elf_generic_reloc, /* special_function.  */
1728
   "R_ARM_THM_ALU_ABS_G2_NC",/* name.  */
1729
   false,     /* partial_inplace.  */
1730
   0x00000000,    /* src_mask.  */
1731
   0x00000000,    /* dst_mask.  */
1732
   false),    /* pcrel_offset.  */
1733
  HOWTO (R_ARM_THM_ALU_ABS_G3_NC,/* type.  */
1734
   0,     /* rightshift.  */
1735
   2,     /* size.  */
1736
   16,      /* bitsize.  */
1737
   false,     /* pc_relative.  */
1738
   0,     /* bitpos.  */
1739
   complain_overflow_bitfield,/* complain_on_overflow.  */
1740
   bfd_elf_generic_reloc, /* special_function.  */
1741
   "R_ARM_THM_ALU_ABS_G3_NC",/* name.  */
1742
   false,     /* partial_inplace.  */
1743
   0x00000000,    /* src_mask.  */
1744
   0x00000000,    /* dst_mask.  */
1745
   false),    /* pcrel_offset.  */
1746
  /* Relocations for Armv8.1-M Mainline.  */
1747
  HOWTO (R_ARM_THM_BF16,  /* type.  */
1748
   0,     /* rightshift.  */
1749
   2,     /* size.  */
1750
   16,      /* bitsize.  */
1751
   true,      /* pc_relative.  */
1752
   0,     /* bitpos.  */
1753
   complain_overflow_dont,/* do not complain_on_overflow.  */
1754
   bfd_elf_generic_reloc, /* special_function.  */
1755
   "R_ARM_THM_BF16",  /* name.  */
1756
   false,     /* partial_inplace.  */
1757
   0x001f0ffe,    /* src_mask.  */
1758
   0x001f0ffe,    /* dst_mask.  */
1759
   true),     /* pcrel_offset.  */
1760
  HOWTO (R_ARM_THM_BF12,  /* type.  */
1761
   0,     /* rightshift.  */
1762
   2,     /* size.  */
1763
   12,      /* bitsize.  */
1764
   true,      /* pc_relative.  */
1765
   0,     /* bitpos.  */
1766
   complain_overflow_dont,/* do not complain_on_overflow.  */
1767
   bfd_elf_generic_reloc, /* special_function.  */
1768
   "R_ARM_THM_BF12",  /* name.  */
1769
   false,     /* partial_inplace.  */
1770
   0x00010ffe,    /* src_mask.  */
1771
   0x00010ffe,    /* dst_mask.  */
1772
   true),     /* pcrel_offset.  */
1773
  HOWTO (R_ARM_THM_BF18,  /* type.  */
1774
   0,     /* rightshift.  */
1775
   2,     /* size.  */
1776
   18,      /* bitsize.  */
1777
   true,      /* pc_relative.  */
1778
   0,     /* bitpos.  */
1779
   complain_overflow_dont,/* do not complain_on_overflow.  */
1780
   bfd_elf_generic_reloc, /* special_function.  */
1781
   "R_ARM_THM_BF18",  /* name.  */
1782
   false,     /* partial_inplace.  */
1783
   0x007f0ffe,    /* src_mask.  */
1784
   0x007f0ffe,    /* dst_mask.  */
1785
   true),     /* pcrel_offset.  */
1786
};
1787
1788
/* 160 onwards: */
1789
static reloc_howto_type elf32_arm_howto_table_2[8] =
1790
{
1791
  HOWTO (R_ARM_IRELATIVE, /* type */
1792
   0,     /* rightshift */
1793
   4,     /* size */
1794
   32,      /* bitsize */
1795
   false,     /* pc_relative */
1796
   0,     /* bitpos */
1797
   complain_overflow_bitfield,/* complain_on_overflow */
1798
   bfd_elf_generic_reloc, /* special_function */
1799
   "R_ARM_IRELATIVE", /* name */
1800
   true,      /* partial_inplace */
1801
   0xffffffff,    /* src_mask */
1802
   0xffffffff,    /* dst_mask */
1803
   false),    /* pcrel_offset */
1804
  HOWTO (R_ARM_GOTFUNCDESC, /* type */
1805
   0,     /* rightshift */
1806
   4,     /* size */
1807
   32,      /* bitsize */
1808
   false,     /* pc_relative */
1809
   0,     /* bitpos */
1810
   complain_overflow_bitfield,/* complain_on_overflow */
1811
   bfd_elf_generic_reloc, /* special_function */
1812
   "R_ARM_GOTFUNCDESC", /* name */
1813
   false,     /* partial_inplace */
1814
   0,     /* src_mask */
1815
   0xffffffff,    /* dst_mask */
1816
   false),    /* pcrel_offset */
1817
  HOWTO (R_ARM_GOTOFFFUNCDESC, /* type */
1818
   0,     /* rightshift */
1819
   4,     /* size */
1820
   32,      /* bitsize */
1821
   false,     /* pc_relative */
1822
   0,     /* bitpos */
1823
   complain_overflow_bitfield,/* complain_on_overflow */
1824
   bfd_elf_generic_reloc, /* special_function */
1825
   "R_ARM_GOTOFFFUNCDESC",/* name */
1826
   false,     /* partial_inplace */
1827
   0,     /* src_mask */
1828
   0xffffffff,    /* dst_mask */
1829
   false),    /* pcrel_offset */
1830
  HOWTO (R_ARM_FUNCDESC,  /* type */
1831
   0,     /* rightshift */
1832
   4,     /* size */
1833
   32,      /* bitsize */
1834
   false,     /* pc_relative */
1835
   0,     /* bitpos */
1836
   complain_overflow_bitfield,/* complain_on_overflow */
1837
   bfd_elf_generic_reloc, /* special_function */
1838
   "R_ARM_FUNCDESC",  /* name */
1839
   false,     /* partial_inplace */
1840
   0,     /* src_mask */
1841
   0xffffffff,    /* dst_mask */
1842
   false),    /* pcrel_offset */
1843
  HOWTO (R_ARM_FUNCDESC_VALUE,  /* type */
1844
   0,     /* rightshift */
1845
   4,     /* size */
1846
   64,      /* bitsize */
1847
   false,     /* pc_relative */
1848
   0,     /* bitpos */
1849
   complain_overflow_bitfield,/* complain_on_overflow */
1850
   bfd_elf_generic_reloc, /* special_function */
1851
   "R_ARM_FUNCDESC_VALUE",/* name */
1852
   false,     /* partial_inplace */
1853
   0,     /* src_mask */
1854
   0xffffffff,    /* dst_mask */
1855
   false),    /* pcrel_offset */
1856
  HOWTO (R_ARM_TLS_GD32_FDPIC,  /* type */
1857
   0,     /* rightshift */
1858
   4,     /* size */
1859
   32,      /* bitsize */
1860
   false,     /* pc_relative */
1861
   0,     /* bitpos */
1862
   complain_overflow_bitfield,/* complain_on_overflow */
1863
   bfd_elf_generic_reloc, /* special_function */
1864
   "R_ARM_TLS_GD32_FDPIC",/* name */
1865
   false,     /* partial_inplace */
1866
   0,     /* src_mask */
1867
   0xffffffff,    /* dst_mask */
1868
   false),    /* pcrel_offset */
1869
  HOWTO (R_ARM_TLS_LDM32_FDPIC, /* type */
1870
   0,     /* rightshift */
1871
   4,     /* size */
1872
   32,      /* bitsize */
1873
   false,     /* pc_relative */
1874
   0,     /* bitpos */
1875
   complain_overflow_bitfield,/* complain_on_overflow */
1876
   bfd_elf_generic_reloc, /* special_function */
1877
   "R_ARM_TLS_LDM32_FDPIC",/* name */
1878
   false,     /* partial_inplace */
1879
   0,     /* src_mask */
1880
   0xffffffff,    /* dst_mask */
1881
   false),    /* pcrel_offset */
1882
  HOWTO (R_ARM_TLS_IE32_FDPIC,  /* type */
1883
   0,     /* rightshift */
1884
   4,     /* size */
1885
   32,      /* bitsize */
1886
   false,     /* pc_relative */
1887
   0,     /* bitpos */
1888
   complain_overflow_bitfield,/* complain_on_overflow */
1889
   bfd_elf_generic_reloc, /* special_function */
1890
   "R_ARM_TLS_IE32_FDPIC",/* name */
1891
   false,     /* partial_inplace */
1892
   0,     /* src_mask */
1893
   0xffffffff,    /* dst_mask */
1894
   false),    /* pcrel_offset */
1895
};
1896
1897
/* 249-255 extended, currently unused, relocations:  */
1898
static reloc_howto_type elf32_arm_howto_table_3[4] =
1899
{
1900
  HOWTO (R_ARM_RREL32,    /* type */
1901
   0,     /* rightshift */
1902
   0,     /* size */
1903
   0,     /* bitsize */
1904
   false,     /* pc_relative */
1905
   0,     /* bitpos */
1906
   complain_overflow_dont,/* complain_on_overflow */
1907
   bfd_elf_generic_reloc, /* special_function */
1908
   "R_ARM_RREL32",  /* name */
1909
   false,     /* partial_inplace */
1910
   0,     /* src_mask */
1911
   0,     /* dst_mask */
1912
   false),    /* pcrel_offset */
1913
1914
  HOWTO (R_ARM_RABS32,    /* type */
1915
   0,     /* rightshift */
1916
   0,     /* size */
1917
   0,     /* bitsize */
1918
   false,     /* pc_relative */
1919
   0,     /* bitpos */
1920
   complain_overflow_dont,/* complain_on_overflow */
1921
   bfd_elf_generic_reloc, /* special_function */
1922
   "R_ARM_RABS32",  /* name */
1923
   false,     /* partial_inplace */
1924
   0,     /* src_mask */
1925
   0,     /* dst_mask */
1926
   false),    /* pcrel_offset */
1927
1928
  HOWTO (R_ARM_RPC24,   /* type */
1929
   0,     /* rightshift */
1930
   0,     /* size */
1931
   0,     /* bitsize */
1932
   false,     /* pc_relative */
1933
   0,     /* bitpos */
1934
   complain_overflow_dont,/* complain_on_overflow */
1935
   bfd_elf_generic_reloc, /* special_function */
1936
   "R_ARM_RPC24",   /* name */
1937
   false,     /* partial_inplace */
1938
   0,     /* src_mask */
1939
   0,     /* dst_mask */
1940
   false),    /* pcrel_offset */
1941
1942
  HOWTO (R_ARM_RBASE,   /* type */
1943
   0,     /* rightshift */
1944
   0,     /* size */
1945
   0,     /* bitsize */
1946
   false,     /* pc_relative */
1947
   0,     /* bitpos */
1948
   complain_overflow_dont,/* complain_on_overflow */
1949
   bfd_elf_generic_reloc, /* special_function */
1950
   "R_ARM_RBASE",   /* name */
1951
   false,     /* partial_inplace */
1952
   0,     /* src_mask */
1953
   0,     /* dst_mask */
1954
   false)     /* pcrel_offset */
1955
};
1956
1957
static reloc_howto_type *
1958
elf32_arm_howto_from_type (unsigned int r_type)
1959
0
{
1960
0
  if (r_type < ARRAY_SIZE (elf32_arm_howto_table_1))
1961
0
    return &elf32_arm_howto_table_1[r_type];
1962
1963
0
  if (r_type >= R_ARM_IRELATIVE
1964
0
      && r_type < R_ARM_IRELATIVE + ARRAY_SIZE (elf32_arm_howto_table_2))
1965
0
    return &elf32_arm_howto_table_2[r_type - R_ARM_IRELATIVE];
1966
1967
0
  if (r_type >= R_ARM_RREL32
1968
0
      && r_type < R_ARM_RREL32 + ARRAY_SIZE (elf32_arm_howto_table_3))
1969
0
    return &elf32_arm_howto_table_3[r_type - R_ARM_RREL32];
1970
1971
0
  return NULL;
1972
0
}
1973
1974
static bool
1975
elf32_arm_info_to_howto (bfd * abfd, arelent * bfd_reloc,
1976
       Elf_Internal_Rela * elf_reloc)
1977
0
{
1978
0
  unsigned int r_type;
1979
1980
0
  r_type = ELF32_R_TYPE (elf_reloc->r_info);
1981
0
  if ((bfd_reloc->howto = elf32_arm_howto_from_type (r_type)) == NULL)
1982
0
    {
1983
      /* xgettext:c-format */
1984
0
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1985
0
        abfd, r_type);
1986
0
      bfd_set_error (bfd_error_bad_value);
1987
0
      return false;
1988
0
    }
1989
0
  return true;
1990
0
}
1991
1992
struct elf32_arm_reloc_map
1993
  {
1994
    bfd_reloc_code_real_type  bfd_reloc_val;
1995
    unsigned char       elf_reloc_val;
1996
  };
1997
1998
/* All entries in this list must also be present in elf32_arm_howto_table.  */
1999
static const struct elf32_arm_reloc_map elf32_arm_reloc_map[] =
2000
  {
2001
    {BFD_RELOC_NONE,         R_ARM_NONE},
2002
    {BFD_RELOC_ARM_PCREL_BRANCH,     R_ARM_PC24},
2003
    {BFD_RELOC_ARM_PCREL_CALL,       R_ARM_CALL},
2004
    {BFD_RELOC_ARM_PCREL_JUMP,       R_ARM_JUMP24},
2005
    {BFD_RELOC_ARM_PCREL_BLX,      R_ARM_XPC25},
2006
    {BFD_RELOC_THUMB_PCREL_BLX,      R_ARM_THM_XPC22},
2007
    {BFD_RELOC_32,         R_ARM_ABS32},
2008
    {BFD_RELOC_32_PCREL,       R_ARM_REL32},
2009
    {BFD_RELOC_8,        R_ARM_ABS8},
2010
    {BFD_RELOC_16,         R_ARM_ABS16},
2011
    {BFD_RELOC_ARM_OFFSET_IMM,       R_ARM_ABS12},
2012
    {BFD_RELOC_ARM_THUMB_OFFSET,     R_ARM_THM_ABS5},
2013
    {BFD_RELOC_THUMB_PCREL_BRANCH25, R_ARM_THM_JUMP24},
2014
    {BFD_RELOC_THUMB_PCREL_BRANCH23, R_ARM_THM_CALL},
2015
    {BFD_RELOC_THUMB_PCREL_BRANCH12, R_ARM_THM_JUMP11},
2016
    {BFD_RELOC_THUMB_PCREL_BRANCH20, R_ARM_THM_JUMP19},
2017
    {BFD_RELOC_THUMB_PCREL_BRANCH9,  R_ARM_THM_JUMP8},
2018
    {BFD_RELOC_THUMB_PCREL_BRANCH7,  R_ARM_THM_JUMP6},
2019
    {BFD_RELOC_GLOB_DAT,       R_ARM_GLOB_DAT},
2020
    {BFD_RELOC_JMP_SLOT,       R_ARM_JUMP_SLOT},
2021
    {BFD_RELOC_RELATIVE,       R_ARM_RELATIVE},
2022
    {BFD_RELOC_ARM_GOTOFF,       R_ARM_GOTOFF32},
2023
    {BFD_RELOC_ARM_GOTPC,      R_ARM_GOTPC},
2024
    {BFD_RELOC_ARM_GOT_PREL,       R_ARM_GOT_PREL},
2025
    {BFD_RELOC_ARM_GOT32,      R_ARM_GOT32},
2026
    {BFD_RELOC_32_PLT_PCREL,       R_ARM_PLT32},
2027
    {BFD_RELOC_ARM_TARGET1,      R_ARM_TARGET1},
2028
    {BFD_RELOC_ARM_ROSEGREL32,       R_ARM_ROSEGREL32},
2029
    {BFD_RELOC_ARM_SBREL32,      R_ARM_SBREL32},
2030
    {BFD_RELOC_ARM_PREL31,       R_ARM_PREL31},
2031
    {BFD_RELOC_ARM_TARGET2,      R_ARM_TARGET2},
2032
    {BFD_RELOC_ARM_TLS_GOTDESC,      R_ARM_TLS_GOTDESC},
2033
    {BFD_RELOC_ARM_TLS_CALL,       R_ARM_TLS_CALL},
2034
    {BFD_RELOC_ARM_THM_TLS_CALL,     R_ARM_THM_TLS_CALL},
2035
    {BFD_RELOC_ARM_TLS_DESCSEQ,      R_ARM_TLS_DESCSEQ},
2036
    {BFD_RELOC_ARM_THM_TLS_DESCSEQ,  R_ARM_THM_TLS_DESCSEQ},
2037
    {BFD_RELOC_ARM_TLS_DESC,       R_ARM_TLS_DESC},
2038
    {BFD_RELOC_ARM_TLS_GD32,       R_ARM_TLS_GD32},
2039
    {BFD_RELOC_ARM_TLS_LDO32,      R_ARM_TLS_LDO32},
2040
    {BFD_RELOC_ARM_TLS_LDM32,      R_ARM_TLS_LDM32},
2041
    {BFD_RELOC_ARM_TLS_DTPMOD32,     R_ARM_TLS_DTPMOD32},
2042
    {BFD_RELOC_ARM_TLS_DTPOFF32,     R_ARM_TLS_DTPOFF32},
2043
    {BFD_RELOC_ARM_TLS_TPOFF32,      R_ARM_TLS_TPOFF32},
2044
    {BFD_RELOC_ARM_TLS_IE32,       R_ARM_TLS_IE32},
2045
    {BFD_RELOC_ARM_TLS_LE32,       R_ARM_TLS_LE32},
2046
    {BFD_RELOC_IRELATIVE,      R_ARM_IRELATIVE},
2047
    {BFD_RELOC_ARM_GOTFUNCDESC,      R_ARM_GOTFUNCDESC},
2048
    {BFD_RELOC_ARM_GOTOFFFUNCDESC,   R_ARM_GOTOFFFUNCDESC},
2049
    {BFD_RELOC_ARM_FUNCDESC,         R_ARM_FUNCDESC},
2050
    {BFD_RELOC_ARM_FUNCDESC_VALUE,   R_ARM_FUNCDESC_VALUE},
2051
    {BFD_RELOC_ARM_TLS_GD32_FDPIC,   R_ARM_TLS_GD32_FDPIC},
2052
    {BFD_RELOC_ARM_TLS_LDM32_FDPIC,  R_ARM_TLS_LDM32_FDPIC},
2053
    {BFD_RELOC_ARM_TLS_IE32_FDPIC,   R_ARM_TLS_IE32_FDPIC},
2054
    {BFD_RELOC_VTABLE_INHERIT,       R_ARM_GNU_VTINHERIT},
2055
    {BFD_RELOC_VTABLE_ENTRY,       R_ARM_GNU_VTENTRY},
2056
    {BFD_RELOC_ARM_MOVW,       R_ARM_MOVW_ABS_NC},
2057
    {BFD_RELOC_ARM_MOVT,       R_ARM_MOVT_ABS},
2058
    {BFD_RELOC_ARM_MOVW_PCREL,       R_ARM_MOVW_PREL_NC},
2059
    {BFD_RELOC_ARM_MOVT_PCREL,       R_ARM_MOVT_PREL},
2060
    {BFD_RELOC_ARM_THUMB_MOVW,       R_ARM_THM_MOVW_ABS_NC},
2061
    {BFD_RELOC_ARM_THUMB_MOVT,       R_ARM_THM_MOVT_ABS},
2062
    {BFD_RELOC_ARM_THUMB_MOVW_PCREL, R_ARM_THM_MOVW_PREL_NC},
2063
    {BFD_RELOC_ARM_THUMB_MOVT_PCREL, R_ARM_THM_MOVT_PREL},
2064
    {BFD_RELOC_ARM_ALU_PC_G0_NC, R_ARM_ALU_PC_G0_NC},
2065
    {BFD_RELOC_ARM_ALU_PC_G0, R_ARM_ALU_PC_G0},
2066
    {BFD_RELOC_ARM_ALU_PC_G1_NC, R_ARM_ALU_PC_G1_NC},
2067
    {BFD_RELOC_ARM_ALU_PC_G1, R_ARM_ALU_PC_G1},
2068
    {BFD_RELOC_ARM_ALU_PC_G2, R_ARM_ALU_PC_G2},
2069
    {BFD_RELOC_ARM_LDR_PC_G0, R_ARM_LDR_PC_G0},
2070
    {BFD_RELOC_ARM_LDR_PC_G1, R_ARM_LDR_PC_G1},
2071
    {BFD_RELOC_ARM_LDR_PC_G2, R_ARM_LDR_PC_G2},
2072
    {BFD_RELOC_ARM_LDRS_PC_G0, R_ARM_LDRS_PC_G0},
2073
    {BFD_RELOC_ARM_LDRS_PC_G1, R_ARM_LDRS_PC_G1},
2074
    {BFD_RELOC_ARM_LDRS_PC_G2, R_ARM_LDRS_PC_G2},
2075
    {BFD_RELOC_ARM_LDC_PC_G0, R_ARM_LDC_PC_G0},
2076
    {BFD_RELOC_ARM_LDC_PC_G1, R_ARM_LDC_PC_G1},
2077
    {BFD_RELOC_ARM_LDC_PC_G2, R_ARM_LDC_PC_G2},
2078
    {BFD_RELOC_ARM_ALU_SB_G0_NC, R_ARM_ALU_SB_G0_NC},
2079
    {BFD_RELOC_ARM_ALU_SB_G0, R_ARM_ALU_SB_G0},
2080
    {BFD_RELOC_ARM_ALU_SB_G1_NC, R_ARM_ALU_SB_G1_NC},
2081
    {BFD_RELOC_ARM_ALU_SB_G1, R_ARM_ALU_SB_G1},
2082
    {BFD_RELOC_ARM_ALU_SB_G2, R_ARM_ALU_SB_G2},
2083
    {BFD_RELOC_ARM_LDR_SB_G0, R_ARM_LDR_SB_G0},
2084
    {BFD_RELOC_ARM_LDR_SB_G1, R_ARM_LDR_SB_G1},
2085
    {BFD_RELOC_ARM_LDR_SB_G2, R_ARM_LDR_SB_G2},
2086
    {BFD_RELOC_ARM_LDRS_SB_G0, R_ARM_LDRS_SB_G0},
2087
    {BFD_RELOC_ARM_LDRS_SB_G1, R_ARM_LDRS_SB_G1},
2088
    {BFD_RELOC_ARM_LDRS_SB_G2, R_ARM_LDRS_SB_G2},
2089
    {BFD_RELOC_ARM_LDC_SB_G0, R_ARM_LDC_SB_G0},
2090
    {BFD_RELOC_ARM_LDC_SB_G1, R_ARM_LDC_SB_G1},
2091
    {BFD_RELOC_ARM_LDC_SB_G2, R_ARM_LDC_SB_G2},
2092
    {BFD_RELOC_ARM_V4BX,       R_ARM_V4BX},
2093
    {BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC, R_ARM_THM_ALU_ABS_G3_NC},
2094
    {BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC, R_ARM_THM_ALU_ABS_G2_NC},
2095
    {BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC, R_ARM_THM_ALU_ABS_G1_NC},
2096
    {BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC, R_ARM_THM_ALU_ABS_G0_NC},
2097
    {BFD_RELOC_ARM_THUMB_BF17, R_ARM_THM_BF16},
2098
    {BFD_RELOC_ARM_THUMB_BF13, R_ARM_THM_BF12},
2099
    {BFD_RELOC_ARM_THUMB_BF19, R_ARM_THM_BF18}
2100
  };
2101
2102
static reloc_howto_type *
2103
elf32_arm_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2104
           bfd_reloc_code_real_type code)
2105
0
{
2106
0
  unsigned int i;
2107
2108
0
  for (i = 0; i < ARRAY_SIZE (elf32_arm_reloc_map); i ++)
2109
0
    if (elf32_arm_reloc_map[i].bfd_reloc_val == code)
2110
0
      return elf32_arm_howto_from_type (elf32_arm_reloc_map[i].elf_reloc_val);
2111
2112
0
  return NULL;
2113
0
}
2114
2115
static reloc_howto_type *
2116
elf32_arm_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2117
           const char *r_name)
2118
0
{
2119
0
  unsigned int i;
2120
2121
0
  for (i = 0; i < ARRAY_SIZE (elf32_arm_howto_table_1); i++)
2122
0
    if (elf32_arm_howto_table_1[i].name != NULL
2123
0
  && strcasecmp (elf32_arm_howto_table_1[i].name, r_name) == 0)
2124
0
      return &elf32_arm_howto_table_1[i];
2125
2126
0
  for (i = 0; i < ARRAY_SIZE (elf32_arm_howto_table_2); i++)
2127
0
    if (elf32_arm_howto_table_2[i].name != NULL
2128
0
  && strcasecmp (elf32_arm_howto_table_2[i].name, r_name) == 0)
2129
0
      return &elf32_arm_howto_table_2[i];
2130
2131
0
  for (i = 0; i < ARRAY_SIZE (elf32_arm_howto_table_3); i++)
2132
0
    if (elf32_arm_howto_table_3[i].name != NULL
2133
0
  && strcasecmp (elf32_arm_howto_table_3[i].name, r_name) == 0)
2134
0
      return &elf32_arm_howto_table_3[i];
2135
2136
0
  return NULL;
2137
0
}
2138
2139
/* Support for core dump NOTE sections.  */
2140
2141
static bool
2142
elf32_arm_nabi_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2143
0
{
2144
0
  int offset;
2145
0
  size_t size;
2146
2147
0
  switch (note->descsz)
2148
0
    {
2149
0
      default:
2150
0
  return false;
2151
2152
0
      case 156:         /* Linux/ARM 32-bit, some pre-v5.9 linux kernels.  */
2153
  /* There's a linux kernel bug for CONFIG_BINFMT_ELF_FDPIC=y
2154
     configurations, fixed by v5.9 linux kernel commit 16aead81018c
2155
     ("take fdpic-related parts of elf_prstatus out").
2156
     The bug causes the FDPIC-specific unsigned long fields
2157
     pr_exec_fdpic_loadmap and pr_interp_fdpic_loadmap to be added to
2158
     struct elf_prstatus in case the FDPIC ABI is not used.
2159
     The two fields are added after pr_reg, so just ignore them.  */
2160
2161
  /* Fall through.  */
2162
0
      case 148:   /* Linux/ARM 32-bit.  */
2163
  /* pr_cursig */
2164
0
  elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2165
2166
  /* pr_pid */
2167
0
  elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
2168
2169
  /* pr_reg */
2170
0
  offset = 72;
2171
0
  size = 72;
2172
2173
0
  break;
2174
0
    }
2175
2176
  /* Make a ".reg/999" section.  */
2177
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2178
0
            size, note->descpos + offset);
2179
0
}
2180
2181
static bool
2182
elf32_arm_nabi_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2183
2
{
2184
2
  switch (note->descsz)
2185
2
    {
2186
2
      default:
2187
2
  return false;
2188
2189
0
      case 124:   /* Linux/ARM elf_prpsinfo.  */
2190
0
  elf_tdata (abfd)->core->pid
2191
0
   = bfd_get_32 (abfd, note->descdata + 12);
2192
0
  elf_tdata (abfd)->core->program
2193
0
   = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
2194
0
  elf_tdata (abfd)->core->command
2195
0
   = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
2196
2
    }
2197
2198
  /* Note that for some reason, a spurious space is tacked
2199
     onto the end of the args in some (at least one anyway)
2200
     implementations, so strip it off if it exists.  */
2201
0
  {
2202
0
    char *command = elf_tdata (abfd)->core->command;
2203
0
    int n = strlen (command);
2204
2205
0
    if (0 < n && command[n - 1] == ' ')
2206
0
      command[n - 1] = '\0';
2207
0
  }
2208
2209
0
  return true;
2210
2
}
2211
2212
static char *
2213
elf32_arm_nabi_write_core_note (bfd *abfd, char *buf, int *bufsiz,
2214
        int note_type, ...)
2215
0
{
2216
0
  switch (note_type)
2217
0
    {
2218
0
    default:
2219
0
      return NULL;
2220
2221
0
    case NT_PRPSINFO:
2222
0
      {
2223
0
  char data[124] ATTRIBUTE_NONSTRING;
2224
0
  va_list ap;
2225
2226
0
  va_start (ap, note_type);
2227
0
  memset (data, 0, sizeof (data));
2228
0
  strncpy (data + 28, va_arg (ap, const char *), 16);
2229
#if GCC_VERSION == 8000 || GCC_VERSION == 8001
2230
  DIAGNOSTIC_PUSH;
2231
  /* GCC 8.0 and 8.1 warn about 80 equals destination size with
2232
     -Wstringop-truncation:
2233
     https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
2234
   */
2235
  DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
2236
#endif
2237
0
  strncpy (data + 44, va_arg (ap, const char *), 80);
2238
#if GCC_VERSION == 8000 || GCC_VERSION == 8001
2239
  DIAGNOSTIC_POP;
2240
#endif
2241
0
  va_end (ap);
2242
2243
0
  return elfcore_write_note (abfd, buf, bufsiz,
2244
0
           "CORE", note_type, data, sizeof (data));
2245
0
      }
2246
2247
0
    case NT_PRSTATUS:
2248
0
      {
2249
0
  char data[148];
2250
0
  va_list ap;
2251
0
  long pid;
2252
0
  int cursig;
2253
0
  const void *greg;
2254
2255
0
  va_start (ap, note_type);
2256
0
  memset (data, 0, sizeof (data));
2257
0
  pid = va_arg (ap, long);
2258
0
  bfd_put_32 (abfd, pid, data + 24);
2259
0
  cursig = va_arg (ap, int);
2260
0
  bfd_put_16 (abfd, cursig, data + 12);
2261
0
  greg = va_arg (ap, const void *);
2262
0
  memcpy (data + 72, greg, 72);
2263
0
  va_end (ap);
2264
2265
0
  return elfcore_write_note (abfd, buf, bufsiz,
2266
0
           "CORE", note_type, data, sizeof (data));
2267
0
      }
2268
0
    }
2269
0
}
2270
2271
#define TARGET_LITTLE_SYM   arm_elf32_le_vec
2272
#define TARGET_LITTLE_NAME    "elf32-littlearm"
2273
#define TARGET_BIG_SYM      arm_elf32_be_vec
2274
#define TARGET_BIG_NAME     "elf32-bigarm"
2275
2276
#define elf_backend_grok_prstatus elf32_arm_nabi_grok_prstatus
2277
#define elf_backend_grok_psinfo   elf32_arm_nabi_grok_psinfo
2278
#define elf_backend_write_core_note elf32_arm_nabi_write_core_note
2279
2280
typedef unsigned long int insn32;
2281
typedef unsigned short int insn16;
2282
2283
/* In lieu of proper flags, assume all EABIv4 or later objects are
2284
   interworkable.  */
2285
#define INTERWORK_FLAG(abfd)  \
2286
0
  (EF_ARM_EABI_VERSION (elf_elfheader (abfd)->e_flags) >= EF_ARM_EABI_VER4 \
2287
0
  || (elf_elfheader (abfd)->e_flags & EF_ARM_INTERWORK) \
2288
0
  || ((abfd)->flags & BFD_LINKER_CREATED))
2289
2290
/* The linker script knows the section names for placement.
2291
   The entry_names are used to do simple name mangling on the stubs.
2292
   Given a function name, and its type, the stub can be found. The
2293
   name can be changed. The only requirement is the %s be present.  */
2294
0
#define THUMB2ARM_GLUE_SECTION_NAME ".glue_7t"
2295
0
#define THUMB2ARM_GLUE_ENTRY_NAME   "__%s_from_thumb"
2296
2297
0
#define ARM2THUMB_GLUE_SECTION_NAME ".glue_7"
2298
0
#define ARM2THUMB_GLUE_ENTRY_NAME   "__%s_from_arm"
2299
2300
0
#define VFP11_ERRATUM_VENEER_SECTION_NAME ".vfp11_veneer"
2301
0
#define VFP11_ERRATUM_VENEER_ENTRY_NAME   "__vfp11_veneer_%x"
2302
2303
0
#define STM32L4XX_ERRATUM_VENEER_SECTION_NAME ".text.stm32l4xx_veneer"
2304
0
#define STM32L4XX_ERRATUM_VENEER_ENTRY_NAME   "__stm32l4xx_veneer_%x"
2305
2306
0
#define ARM_BX_GLUE_SECTION_NAME ".v4_bx"
2307
0
#define ARM_BX_GLUE_ENTRY_NAME   "__bx_r%d"
2308
2309
0
#define STUB_ENTRY_NAME   "__%s_veneer"
2310
2311
0
#define CMSE_PREFIX "__acle_se_"
2312
2313
0
#define CMSE_STUB_NAME ".gnu.sgstubs"
2314
2315
/* The name of the dynamic interpreter.  This is put in the .interp
2316
   section.  */
2317
0
#define ELF_DYNAMIC_INTERPRETER     "/usr/lib/ld.so.1"
2318
2319
/* FDPIC default stack size.  */
2320
0
#define DEFAULT_STACK_SIZE 0x8000
2321
2322
static const unsigned long tls_trampoline [] =
2323
{
2324
  0xe08e0000,   /* add r0, lr, r0 */
2325
  0xe5901004,   /* ldr r1, [r0,#4] */
2326
  0xe12fff11,   /* bx  r1 */
2327
};
2328
2329
static const unsigned long dl_tlsdesc_lazy_trampoline [] =
2330
{
2331
  0xe52d2004, /*  push    {r2}      */
2332
  0xe59f200c, /*      ldr     r2, [pc, #3f - . - 8] */
2333
  0xe59f100c, /*      ldr     r1, [pc, #4f - . - 8] */
2334
  0xe79f2002, /* 1:   ldr     r2, [pc, r2]    */
2335
  0xe081100f, /* 2:   add     r1, pc      */
2336
  0xe12fff12, /*      bx      r2      */
2337
  0x00000014, /* 3:   .word  _GLOBAL_OFFSET_TABLE_ - 1b - 8
2338
        + dl_tlsdesc_lazy_resolver(GOT)   */
2339
  0x00000018, /* 4:   .word  _GLOBAL_OFFSET_TABLE_ - 2b - 8 */
2340
};
2341
2342
/* NOTE: [Thumb nop sequence]
2343
   When adding code that transitions from Thumb to Arm the instruction that
2344
   should be used for the alignment padding should be 0xe7fd (b .-2) instead of
2345
   a nop for performance reasons.  */
2346
2347
/* ARM FDPIC PLT entry.  */
2348
/* The last 5 words contain PLT lazy fragment code and data.  */
2349
static const bfd_vma elf32_arm_fdpic_plt_entry [] =
2350
  {
2351
    0xe59fc008,    /* ldr     r12, .L1 */
2352
    0xe08cc009,    /* add     r12, r12, r9 */
2353
    0xe59c9004,    /* ldr     r9, [r12, #4] */
2354
    0xe59cf000,    /* ldr     pc, [r12] */
2355
    0x00000000,    /* L1.     .word   foo(GOTOFFFUNCDESC) */
2356
    0x00000000,    /* L1.     .word   foo(funcdesc_value_reloc_offset) */
2357
    0xe51fc00c,    /* ldr     r12, [pc, #-12] */
2358
    0xe92d1000,    /* push    {r12} */
2359
    0xe599c004,    /* ldr     r12, [r9, #4] */
2360
    0xe599f000,    /* ldr     pc, [r9] */
2361
  };
2362
2363
/* Thumb FDPIC PLT entry.  */
2364
/* The last 5 words contain PLT lazy fragment code and data.  */
2365
static const bfd_vma elf32_arm_fdpic_thumb_plt_entry [] =
2366
  {
2367
    0xc00cf8df,    /* ldr.w   r12, .L1 */
2368
    0x0c09eb0c,    /* add.w   r12, r12, r9 */
2369
    0x9004f8dc,    /* ldr.w   r9, [r12, #4] */
2370
    0xf000f8dc,    /* ldr.w   pc, [r12] */
2371
    0x00000000,    /* .L1     .word   foo(GOTOFFFUNCDESC) */
2372
    0x00000000,    /* .L2     .word   foo(funcdesc_value_reloc_offset) */
2373
    0xc008f85f,    /* ldr.w   r12, .L2 */
2374
    0xcd04f84d,    /* push    {r12} */
2375
    0xc004f8d9,    /* ldr.w   r12, [r9, #4] */
2376
    0xf000f8d9,    /* ldr.w   pc, [r9] */
2377
  };
2378
2379
#ifdef FOUR_WORD_PLT
2380
2381
/* The first entry in a procedure linkage table looks like
2382
   this.  It is set up so that any shared library function that is
2383
   called before the relocation has been set up calls the dynamic
2384
   linker first.  */
2385
static const bfd_vma elf32_arm_plt0_entry [] =
2386
{
2387
  0xe52de004,   /* str   lr, [sp, #-4]! */
2388
  0xe59fe010,   /* ldr   lr, [pc, #16]  */
2389
  0xe08fe00e,   /* add   lr, pc, lr     */
2390
  0xe5bef008,   /* ldr   pc, [lr, #8]!  */
2391
};
2392
2393
/* Subsequent entries in a procedure linkage table look like
2394
   this.  */
2395
static const bfd_vma elf32_arm_plt_entry [] =
2396
{
2397
  0xe28fc600,   /* add   ip, pc, #NN  */
2398
  0xe28cca00,   /* add   ip, ip, #NN  */
2399
  0xe5bcf000,   /* ldr   pc, [ip, #NN]! */
2400
  0x00000000,   /* unused   */
2401
};
2402
2403
#else /* not FOUR_WORD_PLT */
2404
2405
/* The first entry in a procedure linkage table looks like
2406
   this.  It is set up so that any shared library function that is
2407
   called before the relocation has been set up calls the dynamic
2408
   linker first.  */
2409
static const bfd_vma elf32_arm_plt0_entry [] =
2410
{
2411
  0xe52de004,   /* str   lr, [sp, #-4]! */
2412
  0xe59fe004,   /* ldr   lr, [pc, #4] */
2413
  0xe08fe00e,   /* add   lr, pc, lr */
2414
  0xe5bef008,   /* ldr   pc, [lr, #8]!  */
2415
  0x00000000,   /* &GOT[0] - .    */
2416
};
2417
2418
/* By default subsequent entries in a procedure linkage table look like
2419
   this. Offsets that don't fit into 28 bits will cause link error.  */
2420
static const bfd_vma elf32_arm_plt_entry_short [] =
2421
{
2422
  0xe28fc600,   /* add   ip, pc, #0xNN00000 */
2423
  0xe28cca00,   /* add   ip, ip, #0xNN000   */
2424
  0xe5bcf000,   /* ldr   pc, [ip, #0xNNN]!  */
2425
};
2426
2427
/* When explicitly asked, we'll use this "long" entry format
2428
   which can cope with arbitrary displacements.  */
2429
static const bfd_vma elf32_arm_plt_entry_long [] =
2430
{
2431
  0xe28fc200,   /* add   ip, pc, #0xN0000000 */
2432
  0xe28cc600,   /* add   ip, ip, #0xNN00000  */
2433
  0xe28cca00,   /* add   ip, ip, #0xNN000    */
2434
  0xe5bcf000,   /* ldr   pc, [ip, #0xNNN]!   */
2435
};
2436
2437
static bool elf32_arm_use_long_plt_entry = false;
2438
2439
#endif /* not FOUR_WORD_PLT */
2440
2441
/* The first entry in a procedure linkage table looks like this.
2442
   It is set up so that any shared library function that is called before the
2443
   relocation has been set up calls the dynamic linker first.  */
2444
static const bfd_vma elf32_thumb2_plt0_entry [] =
2445
{
2446
  /* NOTE: As this is a mixture of 16-bit and 32-bit instructions,
2447
     an instruction maybe encoded to one or two array elements.  */
2448
  0xf8dfb500,   /* push    {lr}    */
2449
  0x44fee008,   /* ldr.w   lr, [pc, #8]  */
2450
      /* add     lr, pc  */
2451
  0xff08f85e,   /* ldr.w   pc, [lr, #8]! */
2452
  0x00000000,   /* &GOT[0] - .     */
2453
};
2454
2455
/* Subsequent entries in a procedure linkage table for thumb only target
2456
   look like this.  */
2457
static const bfd_vma elf32_thumb2_plt_entry [] =
2458
{
2459
  /* NOTE: As this is a mixture of 16-bit and 32-bit instructions,
2460
     an instruction maybe encoded to one or two array elements.  */
2461
  0x0c00f240,   /* movw    ip, #0xNNNN    */
2462
  0x0c00f2c0,   /* movt    ip, #0xNNNN    */
2463
  0xf8dc44fc,   /* add     ip, pc   */
2464
  0xe7fcf000    /* ldr.w   pc, [ip]   */
2465
      /* b      .-4     */
2466
};
2467
2468
/* The format of the first entry in the procedure linkage table
2469
   for a VxWorks executable.  */
2470
static const bfd_vma elf32_arm_vxworks_exec_plt0_entry[] =
2471
{
2472
  0xe52dc008,   /* str    ip,[sp,#-8]!      */
2473
  0xe59fc000,   /* ldr    ip,[pc]     */
2474
  0xe59cf008,   /* ldr    pc,[ip,#8]      */
2475
  0x00000000,   /* .long  _GLOBAL_OFFSET_TABLE_   */
2476
};
2477
2478
/* The format of subsequent entries in a VxWorks executable.  */
2479
static const bfd_vma elf32_arm_vxworks_exec_plt_entry[] =
2480
{
2481
  0xe59fc000,       /* ldr  ip,[pc]     */
2482
  0xe59cf000,       /* ldr  pc,[ip]     */
2483
  0x00000000,       /* .long  @got        */
2484
  0xe59fc000,       /* ldr  ip,[pc]     */
2485
  0xea000000,       /* b  _PLT        */
2486
  0x00000000,       /* .long  @pltindex*sizeof(Elf32_Rela)  */
2487
};
2488
2489
/* The format of entries in a VxWorks shared library.  */
2490
static const bfd_vma elf32_arm_vxworks_shared_plt_entry[] =
2491
{
2492
  0xe59fc000,       /* ldr  ip,[pc]     */
2493
  0xe79cf009,       /* ldr  pc,[ip,r9]      */
2494
  0x00000000,       /* .long  @got        */
2495
  0xe59fc000,       /* ldr  ip,[pc]     */
2496
  0xe599f008,       /* ldr  pc,[r9,#8]      */
2497
  0x00000000,       /* .long  @pltindex*sizeof(Elf32_Rela)  */
2498
};
2499
2500
/* An initial stub used if the PLT entry is referenced from Thumb code.  */
2501
0
#define PLT_THUMB_STUB_SIZE 4
2502
static const bfd_vma elf32_arm_plt_thumb_stub [] =
2503
{
2504
  0x4778,   /* bx pc */
2505
  0xe7fd    /* b .-2 */
2506
};
2507
2508
/* PR 28924:
2509
   There was a bug due to too high values of THM_MAX_FWD_BRANCH_OFFSET and
2510
   THM2_MAX_FWD_BRANCH_OFFSET.  The first macro concerns the case when Thumb-2
2511
   is not available, and second macro when Thumb-2 is available.  Among other
2512
   things, they affect the range of branches represented as BLX instructions
2513
   in Encoding T2 defined in Section A8.8.25 of the ARM Architecture
2514
   Reference Manual ARMv7-A and ARMv7-R edition issue C.d.  Such branches are
2515
   specified there to have a maximum forward offset that is a multiple of 4.
2516
   Previously, the respective values defined here were multiples of 2 but not
2517
   4 and they are included in comments for reference.  */
2518
0
#define ARM_MAX_FWD_BRANCH_OFFSET  ((((1 << 23) - 1) << 2) + 8)
2519
0
#define ARM_MAX_BWD_BRANCH_OFFSET ((-((1 << 23) << 2)) + 8)
2520
0
#define THM_MAX_FWD_BRANCH_OFFSET   ((1 << 22) - 4 + 4)
2521
/* #def THM_MAX_FWD_BRANCH_OFFSET   ((1 << 22) - 2 + 4) */
2522
0
#define THM_MAX_BWD_BRANCH_OFFSET  (-(1 << 22) + 4)
2523
0
#define THM2_MAX_FWD_BRANCH_OFFSET (((1 << 24) - 4) + 4)
2524
/* #def THM2_MAX_FWD_BRANCH_OFFSET (((1 << 24) - 2) + 4) */
2525
0
#define THM2_MAX_BWD_BRANCH_OFFSET (-(1 << 24) + 4)
2526
0
#define THM2_MAX_FWD_COND_BRANCH_OFFSET (((1 << 20) -2) + 4)
2527
0
#define THM2_MAX_BWD_COND_BRANCH_OFFSET (-(1 << 20) + 4)
2528
2529
enum stub_insn_type
2530
{
2531
  THUMB16_TYPE = 1,
2532
  THUMB32_TYPE,
2533
  ARM_TYPE,
2534
  DATA_TYPE
2535
};
2536
2537
#define THUMB16_INSN(X)   {(X), THUMB16_TYPE, R_ARM_NONE, 0}
2538
/* A bit of a hack.  A Thumb conditional branch, in which the proper condition
2539
   is inserted in arm_build_one_stub().  */
2540
#define THUMB16_BCOND_INSN(X) {(X), THUMB16_TYPE, R_ARM_NONE, 1}
2541
#define THUMB32_INSN(X)   {(X), THUMB32_TYPE, R_ARM_NONE, 0}
2542
#define THUMB32_MOVT(X)   {(X), THUMB32_TYPE, R_ARM_THM_MOVT_ABS, 0}
2543
#define THUMB32_MOVW(X)   {(X), THUMB32_TYPE, R_ARM_THM_MOVW_ABS_NC, 0}
2544
#define THUMB32_B_INSN(X, Z)  {(X), THUMB32_TYPE, R_ARM_THM_JUMP24, (Z)}
2545
#define ARM_INSN(X)   {(X), ARM_TYPE, R_ARM_NONE, 0}
2546
#define ARM_REL_INSN(X, Z)  {(X), ARM_TYPE, R_ARM_JUMP24, (Z)}
2547
#define DATA_WORD(X,Y,Z)  {(X), DATA_TYPE, (Y), (Z)}
2548
2549
typedef struct
2550
{
2551
  bfd_vma        data;
2552
  enum stub_insn_type  type;
2553
  unsigned int         r_type;
2554
  int          reloc_addend;
2555
}  insn_sequence;
2556
2557
/* See note [Thumb nop sequence] when adding a veneer.  */
2558
2559
/* Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2560
   to reach the stub if necessary.  */
2561
static const insn_sequence elf32_arm_stub_long_branch_any_any[] =
2562
{
2563
  ARM_INSN (0xe51ff004),      /* ldr   pc, [pc, #-4] */
2564
  DATA_WORD (0, R_ARM_ABS32, 0),    /* dcd   R_ARM_ABS32(X) */
2565
};
2566
2567
/* V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2568
   available.  */
2569
static const insn_sequence elf32_arm_stub_long_branch_v4t_arm_thumb[] =
2570
{
2571
  ARM_INSN (0xe59fc000),      /* ldr   ip, [pc, #0] */
2572
  ARM_INSN (0xe12fff1c),      /* bx    ip */
2573
  DATA_WORD (0, R_ARM_ABS32, 0),    /* dcd   R_ARM_ABS32(X) */
2574
};
2575
2576
/* Thumb -> Thumb long branch stub. Used on M-profile architectures.  */
2577
static const insn_sequence elf32_arm_stub_long_branch_thumb_only[] =
2578
{
2579
  THUMB16_INSN (0xb401),       /* push {r0} */
2580
  THUMB16_INSN (0x4802),       /* ldr  r0, [pc, #8] */
2581
  THUMB16_INSN (0x4684),       /* mov  ip, r0 */
2582
  THUMB16_INSN (0xbc01),       /* pop  {r0} */
2583
  THUMB16_INSN (0x4760),       /* bx   ip */
2584
  THUMB16_INSN (0xbf00),       /* nop */
2585
  DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd  R_ARM_ABS32(X) */
2586
};
2587
2588
/* Thumb -> Thumb long branch stub in thumb2 encoding.  Used on armv7.  */
2589
static const insn_sequence elf32_arm_stub_long_branch_thumb2_only[] =
2590
{
2591
  THUMB32_INSN (0xf85ff000),       /* ldr.w  pc, [pc, #-0] */
2592
  DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd  R_ARM_ABS32(x) */
2593
};
2594
2595
/* Thumb -> Thumb long branch stub. Used for PureCode sections on Thumb2
2596
   M-profile architectures.  */
2597
static const insn_sequence elf32_arm_stub_long_branch_thumb2_only_pure[] =
2598
{
2599
  THUMB32_MOVW (0xf2400c00),       /* mov.w ip, R_ARM_MOVW_ABS_NC */
2600
  THUMB32_MOVT (0xf2c00c00),       /* movt  ip, R_ARM_MOVT_ABS << 16 */
2601
  THUMB16_INSN (0x4760),       /* bx   ip */
2602
  THUMB16_INSN (0xbf00),       /* nop */
2603
  /* The nop is added to ensure alignment of following stubs in the section.  */
2604
};
2605
2606
/* V4T Thumb -> Thumb long branch stub. Using the stack is not
2607
   allowed.  */
2608
static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
2609
{
2610
  THUMB16_INSN (0x4778),       /* bx   pc */
2611
  THUMB16_INSN (0xe7fd),       /* b   .-2 */
2612
  ARM_INSN (0xe59fc000),       /* ldr  ip, [pc, #0] */
2613
  ARM_INSN (0xe12fff1c),       /* bx   ip */
2614
  DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd  R_ARM_ABS32(X) */
2615
};
2616
2617
/* V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2618
   available.  */
2619
static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_arm[] =
2620
{
2621
  THUMB16_INSN (0x4778),       /* bx   pc */
2622
  THUMB16_INSN (0xe7fd),       /* b   .-2 */
2623
  ARM_INSN (0xe51ff004),       /* ldr   pc, [pc, #-4] */
2624
  DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd   R_ARM_ABS32(X) */
2625
};
2626
2627
/* V4T Thumb -> ARM short branch stub. Shorter variant of the above
2628
   one, when the destination is close enough.  */
2629
static const insn_sequence elf32_arm_stub_short_branch_v4t_thumb_arm[] =
2630
{
2631
  THUMB16_INSN (0x4778),       /* bx   pc */
2632
  THUMB16_INSN (0xe7fd),       /* b   .-2 */
2633
  ARM_REL_INSN (0xea000000, -8),     /* b    (X-8) */
2634
};
2635
2636
/* ARM/Thumb -> ARM long branch stub, PIC.  On V5T and above, use
2637
   blx to reach the stub if necessary.  */
2638
static const insn_sequence elf32_arm_stub_long_branch_any_arm_pic[] =
2639
{
2640
  ARM_INSN (0xe59fc000),       /* ldr   ip, [pc] */
2641
  ARM_INSN (0xe08ff00c),       /* add   pc, pc, ip */
2642
  DATA_WORD (0, R_ARM_REL32, -4),    /* dcd   R_ARM_REL32(X-4) */
2643
};
2644
2645
/* ARM/Thumb -> Thumb long branch stub, PIC.  On V5T and above, use
2646
   blx to reach the stub if necessary.  We can not add into pc;
2647
   it is not guaranteed to mode switch (different in ARMv6 and
2648
   ARMv7).  */
2649
static const insn_sequence elf32_arm_stub_long_branch_any_thumb_pic[] =
2650
{
2651
  ARM_INSN (0xe59fc004),       /* ldr   ip, [pc, #4] */
2652
  ARM_INSN (0xe08fc00c),       /* add   ip, pc, ip */
2653
  ARM_INSN (0xe12fff1c),       /* bx    ip */
2654
  DATA_WORD (0, R_ARM_REL32, 0),     /* dcd   R_ARM_REL32(X) */
2655
};
2656
2657
/* V4T ARM -> ARM long branch stub, PIC.  */
2658
static const insn_sequence elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
2659
{
2660
  ARM_INSN (0xe59fc004),       /* ldr   ip, [pc, #4] */
2661
  ARM_INSN (0xe08fc00c),       /* add   ip, pc, ip */
2662
  ARM_INSN (0xe12fff1c),       /* bx    ip */
2663
  DATA_WORD (0, R_ARM_REL32, 0),     /* dcd   R_ARM_REL32(X) */
2664
};
2665
2666
/* V4T Thumb -> ARM long branch stub, PIC.  */
2667
static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
2668
{
2669
  THUMB16_INSN (0x4778),       /* bx   pc */
2670
  THUMB16_INSN (0xe7fd),       /* b   .-2 */
2671
  ARM_INSN (0xe59fc000),       /* ldr  ip, [pc, #0] */
2672
  ARM_INSN (0xe08cf00f),       /* add  pc, ip, pc */
2673
  DATA_WORD (0, R_ARM_REL32, -4),     /* dcd  R_ARM_REL32(X) */
2674
};
2675
2676
/* Thumb -> Thumb long branch stub, PIC. Used on M-profile
2677
   architectures.  */
2678
static const insn_sequence elf32_arm_stub_long_branch_thumb_only_pic[] =
2679
{
2680
  THUMB16_INSN (0xb401),       /* push {r0} */
2681
  THUMB16_INSN (0x4802),       /* ldr  r0, [pc, #8] */
2682
  THUMB16_INSN (0x46fc),       /* mov  ip, pc */
2683
  THUMB16_INSN (0x4484),       /* add  ip, r0 */
2684
  THUMB16_INSN (0xbc01),       /* pop  {r0} */
2685
  THUMB16_INSN (0x4760),       /* bx   ip */
2686
  DATA_WORD (0, R_ARM_REL32, 4),     /* dcd  R_ARM_REL32(X) */
2687
};
2688
2689
/* V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
2690
   allowed.  */
2691
static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
2692
{
2693
  THUMB16_INSN (0x4778),       /* bx   pc */
2694
  THUMB16_INSN (0xe7fd),       /* b   .-2 */
2695
  ARM_INSN (0xe59fc004),       /* ldr  ip, [pc, #4] */
2696
  ARM_INSN (0xe08fc00c),       /* add   ip, pc, ip */
2697
  ARM_INSN (0xe12fff1c),       /* bx   ip */
2698
  DATA_WORD (0, R_ARM_REL32, 0),     /* dcd  R_ARM_REL32(X) */
2699
};
2700
2701
/* Thumb2/ARM -> TLS trampoline.  Lowest common denominator, which is a
2702
   long PIC stub.  We can use r1 as a scratch -- and cannot use ip.  */
2703
static const insn_sequence elf32_arm_stub_long_branch_any_tls_pic[] =
2704
{
2705
  ARM_INSN (0xe59f1000),       /* ldr   r1, [pc] */
2706
  ARM_INSN (0xe08ff001),       /* add   pc, pc, r1 */
2707
  DATA_WORD (0, R_ARM_REL32, -4),    /* dcd   R_ARM_REL32(X-4) */
2708
};
2709
2710
/* V4T Thumb -> TLS trampoline.  lowest common denominator, which is a
2711
   long PIC stub.  We can use r1 as a scratch -- and cannot use ip.  */
2712
static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_tls_pic[] =
2713
{
2714
  THUMB16_INSN (0x4778),       /* bx   pc */
2715
  THUMB16_INSN (0xe7fd),       /* b   .-2 */
2716
  ARM_INSN (0xe59f1000),       /* ldr  r1, [pc, #0] */
2717
  ARM_INSN (0xe081f00f),       /* add  pc, r1, pc */
2718
  DATA_WORD (0, R_ARM_REL32, -4),    /* dcd  R_ARM_REL32(X) */
2719
};
2720
2721
/* Stub used for transition to secure state (aka SG veneer).  */
2722
static const insn_sequence elf32_arm_stub_cmse_branch_thumb_only[] =
2723
{
2724
  THUMB32_INSN (0xe97fe97f),    /* sg.  */
2725
  THUMB32_B_INSN (0xf000b800, -4),  /* b.w original_branch_dest.  */
2726
};
2727
2728
2729
/* Cortex-A8 erratum-workaround stubs.  */
2730
2731
/* Stub used for conditional branches (which may be beyond +/-1MB away, so we
2732
   can't use a conditional branch to reach this stub).  */
2733
2734
static const insn_sequence elf32_arm_stub_a8_veneer_b_cond[] =
2735
{
2736
  THUMB16_BCOND_INSN (0xd001),         /* b<cond>.n true.  */
2737
  THUMB32_B_INSN (0xf000b800, -4),     /* b.w insn_after_original_branch.  */
2738
  THUMB32_B_INSN (0xf000b800, -4)      /* true: b.w original_branch_dest.  */
2739
};
2740
2741
/* Stub used for b.w and bl.w instructions.  */
2742
2743
static const insn_sequence elf32_arm_stub_a8_veneer_b[] =
2744
{
2745
  THUMB32_B_INSN (0xf000b800, -4) /* b.w original_branch_dest.  */
2746
};
2747
2748
static const insn_sequence elf32_arm_stub_a8_veneer_bl[] =
2749
{
2750
  THUMB32_B_INSN (0xf000b800, -4) /* b.w original_branch_dest.  */
2751
};
2752
2753
/* Stub used for Thumb-2 blx.w instructions.  We modified the original blx.w
2754
   instruction (which switches to ARM mode) to point to this stub.  Jump to the
2755
   real destination using an ARM-mode branch.  */
2756
2757
static const insn_sequence elf32_arm_stub_a8_veneer_blx[] =
2758
{
2759
  ARM_REL_INSN (0xea000000, -8) /* b original_branch_dest.  */
2760
};
2761
2762
/* For each section group there can be a specially created linker section
2763
   to hold the stubs for that group.  The name of the stub section is based
2764
   upon the name of another section within that group with the suffix below
2765
   applied.
2766
2767
   PR 13049: STUB_SUFFIX used to be ".stub", but this allowed the user to
2768
   create what appeared to be a linker stub section when it actually
2769
   contained user code/data.  For example, consider this fragment:
2770
2771
     const char * stubborn_problems[] = { "np" };
2772
2773
   If this is compiled with "-fPIC -fdata-sections" then gcc produces a
2774
   section called:
2775
2776
     .data.rel.local.stubborn_problems
2777
2778
   This then causes problems in arm32_arm_build_stubs() as it triggers:
2779
2780
      // Ignore non-stub sections.
2781
      if (!strstr (stub_sec->name, STUB_SUFFIX))
2782
  continue;
2783
2784
   And so the section would be ignored instead of being processed.  Hence
2785
   the change in definition of STUB_SUFFIX to a name that cannot be a valid
2786
   C identifier.  */
2787
0
#define STUB_SUFFIX ".__stub"
2788
2789
/* One entry per long/short branch stub defined above.  */
2790
#define DEF_STUBS \
2791
  DEF_STUB (long_branch_any_any)  \
2792
  DEF_STUB (long_branch_v4t_arm_thumb) \
2793
  DEF_STUB (long_branch_thumb_only) \
2794
  DEF_STUB (long_branch_v4t_thumb_thumb)  \
2795
  DEF_STUB (long_branch_v4t_thumb_arm) \
2796
  DEF_STUB (short_branch_v4t_thumb_arm) \
2797
  DEF_STUB (long_branch_any_arm_pic) \
2798
  DEF_STUB (long_branch_any_thumb_pic) \
2799
  DEF_STUB (long_branch_v4t_thumb_thumb_pic) \
2800
  DEF_STUB (long_branch_v4t_arm_thumb_pic) \
2801
  DEF_STUB (long_branch_v4t_thumb_arm_pic) \
2802
  DEF_STUB (long_branch_thumb_only_pic) \
2803
  DEF_STUB (long_branch_any_tls_pic) \
2804
  DEF_STUB (long_branch_v4t_thumb_tls_pic) \
2805
  DEF_STUB (cmse_branch_thumb_only) \
2806
  DEF_STUB (a8_veneer_b_cond) \
2807
  DEF_STUB (a8_veneer_b) \
2808
  DEF_STUB (a8_veneer_bl) \
2809
  DEF_STUB (a8_veneer_blx) \
2810
  DEF_STUB (long_branch_thumb2_only) \
2811
  DEF_STUB (long_branch_thumb2_only_pure)
2812
2813
#define DEF_STUB(x) arm_stub_##x,
2814
enum elf32_arm_stub_type
2815
{
2816
  arm_stub_none,
2817
  DEF_STUBS
2818
  max_stub_type
2819
};
2820
#undef DEF_STUB
2821
2822
/* Note the first a8_veneer type.  */
2823
const unsigned arm_stub_a8_veneer_lwm = arm_stub_a8_veneer_b_cond;
2824
2825
typedef struct
2826
{
2827
  const insn_sequence* template_sequence;
2828
  int template_size;
2829
} stub_def;
2830
2831
#define DEF_STUB(x) {elf32_arm_stub_##x, ARRAY_SIZE(elf32_arm_stub_##x)},
2832
static const stub_def stub_definitions[] =
2833
{
2834
  {NULL, 0},
2835
  DEF_STUBS
2836
};
2837
2838
struct elf32_arm_stub_hash_entry
2839
{
2840
  /* Base hash table entry structure.  */
2841
  struct bfd_hash_entry root;
2842
2843
  /* The stub section.  */
2844
  asection *stub_sec;
2845
2846
  /* Offset within stub_sec of the beginning of this stub.  */
2847
  bfd_vma stub_offset;
2848
2849
  /* Given the symbol's value and its section we can determine its final
2850
     value when building the stubs (so the stub knows where to jump).  */
2851
  bfd_vma target_value;
2852
  asection *target_section;
2853
2854
  /* Same as above but for the source of the branch to the stub.  Used for
2855
     Cortex-A8 erratum workaround to patch it to branch to the stub.  As
2856
     such, source section does not need to be recorded since Cortex-A8 erratum
2857
     workaround stubs are only generated when both source and target are in the
2858
     same section.  */
2859
  bfd_vma source_value;
2860
2861
  /* The instruction which caused this stub to be generated (only valid for
2862
     Cortex-A8 erratum workaround stubs at present).  */
2863
  unsigned long orig_insn;
2864
2865
  /* The stub type.  */
2866
  enum elf32_arm_stub_type stub_type;
2867
  /* Its encoding size in bytes.  */
2868
  int stub_size;
2869
  /* Its template.  */
2870
  const insn_sequence *stub_template;
2871
  /* The size of the template (number of entries).  */
2872
  int stub_template_size;
2873
2874
  /* The symbol table entry, if any, that this was derived from.  */
2875
  struct elf32_arm_link_hash_entry *h;
2876
2877
  /* Type of branch.  */
2878
  enum arm_st_branch_type branch_type;
2879
2880
  /* Where this stub is being called from, or, in the case of combined
2881
     stub sections, the first input section in the group.  */
2882
  asection *id_sec;
2883
2884
  /* The name for the local symbol at the start of this stub.  The
2885
     stub name in the hash table has to be unique; this does not, so
2886
     it can be friendlier.  */
2887
  char *output_name;
2888
};
2889
2890
/* Used to build a map of a section.  This is required for mixed-endian
2891
   code/data.  */
2892
2893
typedef struct elf32_elf_section_map
2894
{
2895
  bfd_vma vma;
2896
  char type;
2897
}
2898
elf32_arm_section_map;
2899
2900
/* Information about a VFP11 erratum veneer, or a branch to such a veneer.  */
2901
2902
typedef enum
2903
{
2904
  VFP11_ERRATUM_BRANCH_TO_ARM_VENEER,
2905
  VFP11_ERRATUM_BRANCH_TO_THUMB_VENEER,
2906
  VFP11_ERRATUM_ARM_VENEER,
2907
  VFP11_ERRATUM_THUMB_VENEER
2908
}
2909
elf32_vfp11_erratum_type;
2910
2911
typedef struct elf32_vfp11_erratum_list
2912
{
2913
  struct elf32_vfp11_erratum_list *next;
2914
  bfd_vma vma;
2915
  union
2916
  {
2917
    struct
2918
    {
2919
      struct elf32_vfp11_erratum_list *veneer;
2920
      unsigned int vfp_insn;
2921
    } b;
2922
    struct
2923
    {
2924
      struct elf32_vfp11_erratum_list *branch;
2925
      unsigned int id;
2926
    } v;
2927
  } u;
2928
  elf32_vfp11_erratum_type type;
2929
}
2930
elf32_vfp11_erratum_list;
2931
2932
/* Information about a STM32L4XX erratum veneer, or a branch to such a
2933
   veneer.  */
2934
typedef enum
2935
{
2936
  STM32L4XX_ERRATUM_BRANCH_TO_VENEER,
2937
  STM32L4XX_ERRATUM_VENEER
2938
}
2939
elf32_stm32l4xx_erratum_type;
2940
2941
typedef struct elf32_stm32l4xx_erratum_list
2942
{
2943
  struct elf32_stm32l4xx_erratum_list *next;
2944
  bfd_vma vma;
2945
  union
2946
  {
2947
    struct
2948
    {
2949
      struct elf32_stm32l4xx_erratum_list *veneer;
2950
      unsigned int insn;
2951
    } b;
2952
    struct
2953
    {
2954
      struct elf32_stm32l4xx_erratum_list *branch;
2955
      unsigned int id;
2956
    } v;
2957
  } u;
2958
  elf32_stm32l4xx_erratum_type type;
2959
}
2960
elf32_stm32l4xx_erratum_list;
2961
2962
typedef enum
2963
{
2964
  DELETE_EXIDX_ENTRY,
2965
  INSERT_EXIDX_CANTUNWIND_AT_END
2966
}
2967
arm_unwind_edit_type;
2968
2969
/* A (sorted) list of edits to apply to an unwind table.  */
2970
typedef struct arm_unwind_table_edit
2971
{
2972
  arm_unwind_edit_type type;
2973
  /* Note: we sometimes want to insert an unwind entry corresponding to a
2974
     section different from the one we're currently writing out, so record the
2975
     (text) section this edit relates to here.  */
2976
  asection *linked_section;
2977
  unsigned int index;
2978
  struct arm_unwind_table_edit *next;
2979
}
2980
arm_unwind_table_edit;
2981
2982
typedef struct _arm_elf_section_data
2983
{
2984
  /* Information about mapping symbols.  */
2985
  struct bfd_elf_section_data elf;
2986
  unsigned int mapcount;
2987
  unsigned int mapsize;
2988
  elf32_arm_section_map *map;
2989
  /* Information about CPU errata.  */
2990
  unsigned int erratumcount;
2991
  elf32_vfp11_erratum_list *erratumlist;
2992
  unsigned int stm32l4xx_erratumcount;
2993
  elf32_stm32l4xx_erratum_list *stm32l4xx_erratumlist;
2994
  unsigned int additional_reloc_count;
2995
  /* Information about unwind tables.  */
2996
  union
2997
  {
2998
    /* Unwind info attached to a text section.  */
2999
    struct
3000
    {
3001
      asection *arm_exidx_sec;
3002
    } text;
3003
3004
    /* Unwind info attached to an .ARM.exidx section.  */
3005
    struct
3006
    {
3007
      arm_unwind_table_edit *unwind_edit_list;
3008
      arm_unwind_table_edit *unwind_edit_tail;
3009
    } exidx;
3010
  } u;
3011
}
3012
_arm_elf_section_data;
3013
3014
#define elf32_arm_section_data(sec) \
3015
0
  ((_arm_elf_section_data *) elf_section_data (sec))
3016
3017
/* A fix which might be required for Cortex-A8 Thumb-2 branch/TLB erratum.
3018
   These fixes are subject to a relaxation procedure (in elf32_arm_size_stubs),
3019
   so may be created multiple times: we use an array of these entries whilst
3020
   relaxing which we can refresh easily, then create stubs for each potentially
3021
   erratum-triggering instruction once we've settled on a solution.  */
3022
3023
struct a8_erratum_fix
3024
{
3025
  bfd *input_bfd;
3026
  asection *section;
3027
  bfd_vma offset;
3028
  bfd_vma target_offset;
3029
  unsigned long orig_insn;
3030
  char *stub_name;
3031
  enum elf32_arm_stub_type stub_type;
3032
  enum arm_st_branch_type branch_type;
3033
};
3034
3035
/* A table of relocs applied to branches which might trigger Cortex-A8
3036
   erratum.  */
3037
3038
struct a8_erratum_reloc
3039
{
3040
  bfd_vma from;
3041
  bfd_vma destination;
3042
  struct elf32_arm_link_hash_entry *hash;
3043
  const char *sym_name;
3044
  unsigned int r_type;
3045
  enum arm_st_branch_type branch_type;
3046
  bool non_a8_stub;
3047
};
3048
3049
/* The size of the thread control block.  */
3050
#define TCB_SIZE  8
3051
3052
/* ARM-specific information about a PLT entry, over and above the usual
3053
   gotplt_union.  */
3054
struct arm_plt_info
3055
{
3056
  /* We reference count Thumb references to a PLT entry separately,
3057
     so that we can emit the Thumb trampoline only if needed.  */
3058
  bfd_signed_vma thumb_refcount;
3059
3060
  /* Some references from Thumb code may be eliminated by BL->BLX
3061
     conversion, so record them separately.  */
3062
  bfd_signed_vma maybe_thumb_refcount;
3063
3064
  /* How many of the recorded PLT accesses were from non-call relocations.
3065
     This information is useful when deciding whether anything takes the
3066
     address of an STT_GNU_IFUNC PLT.  A value of 0 means that all
3067
     non-call references to the function should resolve directly to the
3068
     real runtime target.  */
3069
  unsigned int noncall_refcount;
3070
3071
  /* Since PLT entries have variable size if the Thumb prologue is
3072
     used, we need to record the index into .got.plt instead of
3073
     recomputing it from the PLT offset.  */
3074
  bfd_signed_vma got_offset;
3075
};
3076
3077
/* Information about an .iplt entry for a local STT_GNU_IFUNC symbol.  */
3078
struct arm_local_iplt_info
3079
{
3080
  /* The information that is usually found in the generic ELF part of
3081
     the hash table entry.  */
3082
  union gotplt_union root;
3083
3084
  /* The information that is usually found in the ARM-specific part of
3085
     the hash table entry.  */
3086
  struct arm_plt_info arm;
3087
3088
  /* A list of all potential dynamic relocations against this symbol.  */
3089
  struct elf_dyn_relocs *dyn_relocs;
3090
};
3091
3092
/* Structure to handle FDPIC support for local functions.  */
3093
struct fdpic_local
3094
{
3095
  unsigned int funcdesc_cnt;
3096
  unsigned int gotofffuncdesc_cnt;
3097
  int funcdesc_offset;
3098
};
3099
3100
struct elf_arm_obj_tdata
3101
{
3102
  struct elf_obj_tdata root;
3103
3104
  /* Zero to warn when linking objects with incompatible enum sizes.  */
3105
  int no_enum_size_warning;
3106
3107
  /* Zero to warn when linking objects with incompatible wchar_t sizes.  */
3108
  int no_wchar_size_warning;
3109
3110
  /* The number of entries in each of the arrays in this strcuture.
3111
     Used to avoid buffer overruns.  */
3112
  bfd_size_type num_entries;
3113
3114
  /* tls_type for each local got entry.  */
3115
  char *local_got_tls_type;
3116
3117
  /* GOTPLT entries for TLS descriptors.  */
3118
  bfd_vma *local_tlsdesc_gotent;
3119
3120
  /* Information for local symbols that need entries in .iplt.  */
3121
  struct arm_local_iplt_info **local_iplt;
3122
3123
  /* Maintains FDPIC counters and funcdesc info.  */
3124
  struct fdpic_local *local_fdpic_cnts;
3125
};
3126
3127
#define elf_arm_tdata(bfd) \
3128
0
  ((struct elf_arm_obj_tdata *) (bfd)->tdata.any)
3129
3130
#define elf32_arm_num_entries(bfd) \
3131
0
  (elf_arm_tdata (bfd)->num_entries)
3132
3133
#define elf32_arm_local_got_tls_type(bfd) \
3134
0
  (elf_arm_tdata (bfd)->local_got_tls_type)
3135
3136
#define elf32_arm_local_tlsdesc_gotent(bfd) \
3137
0
  (elf_arm_tdata (bfd)->local_tlsdesc_gotent)
3138
3139
#define elf32_arm_local_iplt(bfd) \
3140
0
  (elf_arm_tdata (bfd)->local_iplt)
3141
3142
#define elf32_arm_local_fdpic_cnts(bfd) \
3143
0
  (elf_arm_tdata (bfd)->local_fdpic_cnts)
3144
3145
#define is_arm_elf(bfd) \
3146
3
  (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
3147
3
   && elf_tdata (bfd) != NULL \
3148
3
   && elf_object_id (bfd) == ARM_ELF_DATA)
3149
3150
static bool
3151
elf32_arm_mkobject (bfd *abfd)
3152
86.1k
{
3153
86.1k
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_arm_obj_tdata));
3154
86.1k
}
3155
3156
0
#define elf32_arm_hash_entry(ent) ((struct elf32_arm_link_hash_entry *)(ent))
3157
3158
/* Structure to handle FDPIC support for extern functions.  */
3159
struct fdpic_global {
3160
  unsigned int gotofffuncdesc_cnt;
3161
  unsigned int gotfuncdesc_cnt;
3162
  unsigned int funcdesc_cnt;
3163
  int funcdesc_offset;
3164
  int gotfuncdesc_offset;
3165
};
3166
3167
/* Arm ELF linker hash entry.  */
3168
struct elf32_arm_link_hash_entry
3169
{
3170
  struct elf_link_hash_entry root;
3171
3172
  /* ARM-specific PLT information.  */
3173
  struct arm_plt_info plt;
3174
3175
0
#define GOT_UNKNOWN 0
3176
0
#define GOT_NORMAL  1
3177
0
#define GOT_TLS_GD  2
3178
0
#define GOT_TLS_IE  4
3179
0
#define GOT_TLS_GDESC 8
3180
0
#define GOT_TLS_GD_ANY_P(type)  ((type & GOT_TLS_GD) || (type & GOT_TLS_GDESC))
3181
  unsigned int tls_type : 8;
3182
3183
  /* True if the symbol's PLT entry is in .iplt rather than .plt.  */
3184
  unsigned int is_iplt : 1;
3185
3186
  unsigned int unused : 23;
3187
3188
  /* Offset of the GOTPLT entry reserved for the TLS descriptor,
3189
     starting at the end of the jump table.  */
3190
  bfd_vma tlsdesc_got;
3191
3192
  /* The symbol marking the real symbol location for exported thumb
3193
     symbols with Arm stubs.  */
3194
  struct elf_link_hash_entry *export_glue;
3195
3196
  /* A pointer to the most recently used stub hash entry against this
3197
     symbol.  */
3198
  struct elf32_arm_stub_hash_entry *stub_cache;
3199
3200
  /* Counter for FDPIC relocations against this symbol.  */
3201
  struct fdpic_global fdpic_cnts;
3202
};
3203
3204
/* Traverse an arm ELF linker hash table.  */
3205
#define elf32_arm_link_hash_traverse(table, func, info)     \
3206
  (elf_link_hash_traverse           \
3207
   (&(table)->root,             \
3208
    (bool (*) (struct elf_link_hash_entry *, void *)) (func),   \
3209
    (info)))
3210
3211
/* Get the ARM elf linker hash table from a link_info structure.  */
3212
#define elf32_arm_hash_table(p) \
3213
0
  ((is_elf_hash_table ((p)->hash)          \
3214
0
    && elf_hash_table_id (elf_hash_table (p)) == ARM_ELF_DATA)   \
3215
0
   ? (struct elf32_arm_link_hash_table *) (p)->hash : NULL)
3216
3217
#define arm_stub_hash_lookup(table, string, create, copy) \
3218
0
  ((struct elf32_arm_stub_hash_entry *) \
3219
0
   bfd_hash_lookup ((table), (string), (create), (copy)))
3220
3221
/* Array to keep track of which stub sections have been created, and
3222
   information on stub grouping.  */
3223
struct map_stub
3224
{
3225
  /* This is the section to which stubs in the group will be
3226
     attached.  */
3227
  asection *link_sec;
3228
  /* The stub section.  */
3229
  asection *stub_sec;
3230
};
3231
3232
#define elf32_arm_compute_jump_table_size(htab) \
3233
0
  ((htab)->next_tls_desc_index * 4)
3234
3235
/* ARM ELF linker hash table.  */
3236
struct elf32_arm_link_hash_table
3237
{
3238
  /* The main hash table.  */
3239
  struct elf_link_hash_table root;
3240
3241
  /* The size in bytes of the section containing the Thumb-to-ARM glue.  */
3242
  bfd_size_type thumb_glue_size;
3243
3244
  /* The size in bytes of the section containing the ARM-to-Thumb glue.  */
3245
  bfd_size_type arm_glue_size;
3246
3247
  /* The size in bytes of section containing the ARMv4 BX veneers.  */
3248
  bfd_size_type bx_glue_size;
3249
3250
  /* Offsets of ARMv4 BX veneers.  Bit1 set if present, and Bit0 set when
3251
     veneer has been populated.  */
3252
  bfd_vma bx_glue_offset[15];
3253
3254
  /* The size in bytes of the section containing glue for VFP11 erratum
3255
     veneers.  */
3256
  bfd_size_type vfp11_erratum_glue_size;
3257
3258
 /* The size in bytes of the section containing glue for STM32L4XX erratum
3259
     veneers.  */
3260
  bfd_size_type stm32l4xx_erratum_glue_size;
3261
3262
  /* A table of fix locations for Cortex-A8 Thumb-2 branch/TLB erratum.  This
3263
     holds Cortex-A8 erratum fix locations between elf32_arm_size_stubs() and
3264
     elf32_arm_write_section().  */
3265
  struct a8_erratum_fix *a8_erratum_fixes;
3266
  unsigned int num_a8_erratum_fixes;
3267
3268
  /* An arbitrary input BFD chosen to hold the glue sections.  */
3269
  bfd * bfd_of_glue_owner;
3270
3271
  /* Nonzero to output a BE8 image.  */
3272
  int byteswap_code;
3273
3274
  /* Zero if R_ARM_TARGET1 means R_ARM_ABS32.
3275
     Nonzero if R_ARM_TARGET1 means R_ARM_REL32.  */
3276
  int target1_is_rel;
3277
3278
  /* The relocation to use for R_ARM_TARGET2 relocations.  */
3279
  int target2_reloc;
3280
3281
  /* 0 = Ignore R_ARM_V4BX.
3282
     1 = Convert BX to MOV PC.
3283
     2 = Generate v4 interworing stubs.  */
3284
  int fix_v4bx;
3285
3286
  /* Whether we should fix the Cortex-A8 Thumb-2 branch/TLB erratum.  */
3287
  int fix_cortex_a8;
3288
3289
  /* Whether we should fix the ARM1176 BLX immediate issue.  */
3290
  int fix_arm1176;
3291
3292
  /* Nonzero if the ARM/Thumb BLX instructions are available for use.  */
3293
  int use_blx;
3294
3295
  /* What sort of code sequences we should look for which may trigger the
3296
     VFP11 denorm erratum.  */
3297
  bfd_arm_vfp11_fix vfp11_fix;
3298
3299
  /* Global counter for the number of fixes we have emitted.  */
3300
  int num_vfp11_fixes;
3301
3302
  /* What sort of code sequences we should look for which may trigger the
3303
     STM32L4XX erratum.  */
3304
  bfd_arm_stm32l4xx_fix stm32l4xx_fix;
3305
3306
  /* Global counter for the number of fixes we have emitted.  */
3307
  int num_stm32l4xx_fixes;
3308
3309
  /* Nonzero to force PIC branch veneers.  */
3310
  int pic_veneer;
3311
3312
  /* The number of bytes in the initial entry in the PLT.  */
3313
  bfd_size_type plt_header_size;
3314
3315
  /* The number of bytes in the subsequent PLT etries.  */
3316
  bfd_size_type plt_entry_size;
3317
3318
  /* True if the target uses REL relocations.  */
3319
  bool use_rel;
3320
3321
  /* Nonzero if import library must be a secure gateway import library
3322
     as per ARMv8-M Security Extensions.  */
3323
  int cmse_implib;
3324
3325
  /* The import library whose symbols' address must remain stable in
3326
     the import library generated.  */
3327
  bfd *in_implib_bfd;
3328
3329
  /* The index of the next unused R_ARM_TLS_DESC slot in .rel.plt.  */
3330
  bfd_vma next_tls_desc_index;
3331
3332
  /* How many R_ARM_TLS_DESC relocations were generated so far.  */
3333
  bfd_vma num_tls_desc;
3334
3335
  /* The (unloaded but important) VxWorks .rela.plt.unloaded section.  */
3336
  asection *srelplt2;
3337
3338
  /* Offset in .plt section of tls_arm_trampoline.  */
3339
  bfd_vma tls_trampoline;
3340
3341
  /* Data for R_ARM_TLS_LDM32/R_ARM_TLS_LDM32_FDPIC relocations.  */
3342
  union
3343
  {
3344
    bfd_signed_vma refcount;
3345
    bfd_vma offset;
3346
  } tls_ldm_got;
3347
3348
  /* For convenience in allocate_dynrelocs.  */
3349
  bfd * obfd;
3350
3351
  /* The amount of space used by the reserved portion of the sgotplt
3352
     section, plus whatever space is used by the jump slots.  */
3353
  bfd_vma sgotplt_jump_table_size;
3354
3355
  /* The stub hash table.  */
3356
  struct bfd_hash_table stub_hash_table;
3357
3358
  /* Linker stub bfd.  */
3359
  bfd *stub_bfd;
3360
3361
  /* Linker call-backs.  */
3362
  asection * (*add_stub_section) (const char *, asection *, asection *,
3363
          unsigned int);
3364
  void (*layout_sections_again) (void);
3365
3366
  /* Array to keep track of which stub sections have been created, and
3367
     information on stub grouping.  */
3368
  struct map_stub *stub_group;
3369
3370
  /* Input stub section holding secure gateway veneers.  */
3371
  asection *cmse_stub_sec;
3372
3373
  /* Offset in cmse_stub_sec where new SG veneers (not in input import library)
3374
     start to be allocated.  */
3375
  bfd_vma new_cmse_stub_offset;
3376
3377
  /* Number of elements in stub_group.  */
3378
  unsigned int top_id;
3379
3380
  /* Assorted information used by elf32_arm_size_stubs.  */
3381
  unsigned int bfd_count;
3382
  unsigned int top_index;
3383
  asection **input_list;
3384
3385
  /* True if the target system uses FDPIC. */
3386
  int fdpic_p;
3387
3388
  /* Fixup section. Used for FDPIC.  */
3389
  asection *srofixup;
3390
};
3391
3392
/* Add an FDPIC read-only fixup.  */
3393
static void
3394
arm_elf_add_rofixup (bfd *output_bfd, asection *srofixup, bfd_vma offset)
3395
0
{
3396
0
  bfd_vma fixup_offset;
3397
3398
0
  fixup_offset = srofixup->reloc_count++ * 4;
3399
0
  BFD_ASSERT (fixup_offset < srofixup->size);
3400
0
  bfd_put_32 (output_bfd, offset, srofixup->contents + fixup_offset);
3401
0
}
3402
3403
static inline int
3404
ctz (unsigned int mask)
3405
0
{
3406
0
#if GCC_VERSION >= 3004
3407
0
  return __builtin_ctz (mask);
3408
#else
3409
  unsigned int i;
3410
3411
  for (i = 0; i < 8 * sizeof (mask); i++)
3412
    {
3413
      if (mask & 0x1)
3414
  break;
3415
      mask = (mask >> 1);
3416
    }
3417
  return i;
3418
#endif
3419
0
}
3420
3421
static inline int
3422
elf32_arm_popcount (unsigned int mask)
3423
0
{
3424
0
#if GCC_VERSION >= 3004
3425
0
  return __builtin_popcount (mask);
3426
#else
3427
  unsigned int i;
3428
  int sum = 0;
3429
3430
  for (i = 0; i < 8 * sizeof (mask); i++)
3431
    {
3432
      if (mask & 0x1)
3433
  sum++;
3434
      mask = (mask >> 1);
3435
    }
3436
  return sum;
3437
#endif
3438
0
}
3439
3440
static void elf32_arm_add_dynreloc (bfd *output_bfd, struct bfd_link_info *info,
3441
            asection *sreloc, Elf_Internal_Rela *rel);
3442
3443
static void
3444
arm_elf_fill_funcdesc (bfd *output_bfd,
3445
           struct bfd_link_info *info,
3446
           int *funcdesc_offset,
3447
           int dynindx,
3448
           int offset,
3449
           bfd_vma addr,
3450
           bfd_vma dynreloc_value,
3451
           bfd_vma seg)
3452
0
{
3453
0
  if ((*funcdesc_offset & 1) == 0)
3454
0
    {
3455
0
      struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
3456
0
      asection *sgot = globals->root.sgot;
3457
3458
0
      if (bfd_link_pic (info))
3459
0
  {
3460
0
    asection *srelgot = globals->root.srelgot;
3461
0
    Elf_Internal_Rela outrel;
3462
3463
0
    outrel.r_info = ELF32_R_INFO (dynindx, R_ARM_FUNCDESC_VALUE);
3464
0
    outrel.r_offset = sgot->output_section->vma + sgot->output_offset + offset;
3465
0
    outrel.r_addend = 0;
3466
3467
0
    elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
3468
0
    bfd_put_32 (output_bfd, addr, sgot->contents + offset);
3469
0
    bfd_put_32 (output_bfd, seg, sgot->contents + offset + 4);
3470
0
  }
3471
0
      else
3472
0
  {
3473
0
    struct elf_link_hash_entry *hgot = globals->root.hgot;
3474
0
    bfd_vma got_value = hgot->root.u.def.value
3475
0
      + hgot->root.u.def.section->output_section->vma
3476
0
      + hgot->root.u.def.section->output_offset;
3477
3478
0
    arm_elf_add_rofixup (output_bfd, globals->srofixup,
3479
0
             sgot->output_section->vma + sgot->output_offset
3480
0
             + offset);
3481
0
    arm_elf_add_rofixup (output_bfd, globals->srofixup,
3482
0
             sgot->output_section->vma + sgot->output_offset
3483
0
             + offset + 4);
3484
0
    bfd_put_32 (output_bfd, dynreloc_value, sgot->contents + offset);
3485
0
    bfd_put_32 (output_bfd, got_value, sgot->contents + offset + 4);
3486
0
  }
3487
0
      *funcdesc_offset |= 1;
3488
0
    }
3489
0
}
3490
3491
/* Create an entry in an ARM ELF linker hash table.  */
3492
3493
static struct bfd_hash_entry *
3494
elf32_arm_link_hash_newfunc (struct bfd_hash_entry * entry,
3495
           struct bfd_hash_table * table,
3496
           const char * string)
3497
0
{
3498
0
  struct elf32_arm_link_hash_entry * ret =
3499
0
    (struct elf32_arm_link_hash_entry *) entry;
3500
3501
  /* Allocate the structure if it has not already been allocated by a
3502
     subclass.  */
3503
0
  if (ret == NULL)
3504
0
    ret = (struct elf32_arm_link_hash_entry *)
3505
0
  bfd_hash_allocate (table, sizeof (struct elf32_arm_link_hash_entry));
3506
0
  if (ret == NULL)
3507
0
    return (struct bfd_hash_entry *) ret;
3508
3509
  /* Call the allocation method of the superclass.  */
3510
0
  ret = ((struct elf32_arm_link_hash_entry *)
3511
0
   _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3512
0
             table, string));
3513
0
  if (ret != NULL)
3514
0
    {
3515
0
      ret->tls_type = GOT_UNKNOWN;
3516
0
      ret->tlsdesc_got = (bfd_vma) -1;
3517
0
      ret->plt.thumb_refcount = 0;
3518
0
      ret->plt.maybe_thumb_refcount = 0;
3519
0
      ret->plt.noncall_refcount = 0;
3520
0
      ret->plt.got_offset = -1;
3521
0
      ret->is_iplt = false;
3522
0
      ret->export_glue = NULL;
3523
3524
0
      ret->stub_cache = NULL;
3525
3526
0
      ret->fdpic_cnts.gotofffuncdesc_cnt = 0;
3527
0
      ret->fdpic_cnts.gotfuncdesc_cnt = 0;
3528
0
      ret->fdpic_cnts.funcdesc_cnt = 0;
3529
0
      ret->fdpic_cnts.funcdesc_offset = -1;
3530
0
      ret->fdpic_cnts.gotfuncdesc_offset = -1;
3531
0
    }
3532
3533
0
  return (struct bfd_hash_entry *) ret;
3534
0
}
3535
3536
/* Ensure that we have allocated bookkeeping structures for ABFD's local
3537
   symbols.  */
3538
3539
static bool
3540
elf32_arm_allocate_local_sym_info (bfd *abfd)
3541
0
{
3542
0
  if (elf_local_got_refcounts (abfd) == NULL)
3543
0
    {
3544
0
      bfd_size_type num_syms;
3545
3546
0
      elf32_arm_num_entries (abfd) = 0;
3547
3548
      /* Whilst it might be tempting to allocate a single block of memory and
3549
   then divide it up amoungst the arrays in the elf_arm_obj_tdata
3550
   structure, this interferes with the work of memory checkers looking
3551
   for buffer overruns.  So allocate each array individually.  */
3552
3553
0
      num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
3554
3555
0
      elf_local_got_refcounts (abfd) = bfd_zalloc
3556
0
  (abfd, num_syms * sizeof (* elf_local_got_refcounts (abfd)));
3557
3558
0
      if (elf_local_got_refcounts (abfd) == NULL)
3559
0
  return false;
3560
3561
0
      elf32_arm_local_tlsdesc_gotent (abfd) = bfd_zalloc
3562
0
  (abfd, num_syms * sizeof (* elf32_arm_local_tlsdesc_gotent (abfd)));
3563
3564
0
      if (elf32_arm_local_tlsdesc_gotent (abfd) == NULL)
3565
0
  return false;
3566
3567
0
      elf32_arm_local_iplt (abfd) = bfd_zalloc
3568
0
  (abfd, num_syms * sizeof (* elf32_arm_local_iplt (abfd)));
3569
3570
0
      if (elf32_arm_local_iplt (abfd) == NULL)
3571
0
  return false;
3572
3573
0
      elf32_arm_local_fdpic_cnts (abfd) = bfd_zalloc
3574
0
  (abfd, num_syms * sizeof (* elf32_arm_local_fdpic_cnts (abfd)));
3575
3576
0
      if (elf32_arm_local_fdpic_cnts (abfd) == NULL)
3577
0
  return false;
3578
3579
0
      elf32_arm_local_got_tls_type (abfd) = bfd_zalloc
3580
0
  (abfd, num_syms * sizeof (* elf32_arm_local_got_tls_type (abfd)));
3581
3582
0
      if (elf32_arm_local_got_tls_type (abfd) == NULL)
3583
0
  return false;
3584
3585
0
      elf32_arm_num_entries (abfd) = num_syms;
3586
3587
0
#if GCC_VERSION >= 3000
3588
0
      BFD_ASSERT (__alignof__ (*elf32_arm_local_tlsdesc_gotent (abfd))
3589
0
      <= __alignof__ (*elf_local_got_refcounts (abfd)));
3590
0
      BFD_ASSERT (__alignof__ (*elf32_arm_local_iplt (abfd))
3591
0
      <= __alignof__ (*elf32_arm_local_tlsdesc_gotent (abfd)));
3592
0
      BFD_ASSERT (__alignof__ (*elf32_arm_local_fdpic_cnts (abfd))
3593
0
      <= __alignof__ (*elf32_arm_local_iplt (abfd)));
3594
0
      BFD_ASSERT (__alignof__ (*elf32_arm_local_got_tls_type (abfd))
3595
0
      <= __alignof__ (*elf32_arm_local_fdpic_cnts (abfd)));
3596
0
#endif
3597
0
    }
3598
0
  return true;
3599
0
}
3600
3601
/* Return the .iplt information for local symbol R_SYMNDX, which belongs
3602
   to input bfd ABFD.  Create the information if it doesn't already exist.
3603
   Return null if an allocation fails.  */
3604
3605
static struct arm_local_iplt_info *
3606
elf32_arm_create_local_iplt (bfd *abfd, unsigned long r_symndx)
3607
0
{
3608
0
  struct arm_local_iplt_info **ptr;
3609
3610
0
  if (!elf32_arm_allocate_local_sym_info (abfd))
3611
0
    return NULL;
3612
3613
0
  BFD_ASSERT (r_symndx < elf_tdata (abfd)->symtab_hdr.sh_info);
3614
0
  BFD_ASSERT (r_symndx < elf32_arm_num_entries (abfd));
3615
0
  ptr = &elf32_arm_local_iplt (abfd)[r_symndx];
3616
0
  if (*ptr == NULL)
3617
0
    *ptr = bfd_zalloc (abfd, sizeof (**ptr));
3618
0
  return *ptr;
3619
0
}
3620
3621
/* Try to obtain PLT information for the symbol with index R_SYMNDX
3622
   in ABFD's symbol table.  If the symbol is global, H points to its
3623
   hash table entry, otherwise H is null.
3624
3625
   Return true if the symbol does have PLT information.  When returning
3626
   true, point *ROOT_PLT at the target-independent reference count/offset
3627
   union and *ARM_PLT at the ARM-specific information.  */
3628
3629
static bool
3630
elf32_arm_get_plt_info (bfd *abfd, struct elf32_arm_link_hash_table *globals,
3631
      struct elf32_arm_link_hash_entry *h,
3632
      unsigned long r_symndx, union gotplt_union **root_plt,
3633
      struct arm_plt_info **arm_plt)
3634
0
{
3635
0
  struct arm_local_iplt_info *local_iplt;
3636
3637
0
  if (globals->root.splt == NULL && globals->root.iplt == NULL)
3638
0
    return false;
3639
3640
0
  if (h != NULL)
3641
0
    {
3642
0
      *root_plt = &h->root.plt;
3643
0
      *arm_plt = &h->plt;
3644
0
      return true;
3645
0
    }
3646
3647
0
  if (elf32_arm_local_iplt (abfd) == NULL)
3648
0
    return false;
3649
3650
0
  if (r_symndx >= elf32_arm_num_entries (abfd))
3651
0
    return false;
3652
3653
0
  local_iplt = elf32_arm_local_iplt (abfd)[r_symndx];
3654
0
  if (local_iplt == NULL)
3655
0
    return false;
3656
3657
0
  *root_plt = &local_iplt->root;
3658
0
  *arm_plt = &local_iplt->arm;
3659
0
  return true;
3660
0
}
3661
3662
static bool using_thumb_only (struct elf32_arm_link_hash_table *globals);
3663
3664
/* Return true if the PLT described by ARM_PLT requires a Thumb stub
3665
   before it.  */
3666
3667
static bool
3668
elf32_arm_plt_needs_thumb_stub_p (struct bfd_link_info *info,
3669
          struct arm_plt_info *arm_plt)
3670
0
{
3671
0
  struct elf32_arm_link_hash_table *htab;
3672
3673
0
  htab = elf32_arm_hash_table (info);
3674
3675
0
  return (!using_thumb_only (htab) && (arm_plt->thumb_refcount != 0
3676
0
    || (!htab->use_blx && arm_plt->maybe_thumb_refcount != 0)));
3677
0
}
3678
3679
/* Return a pointer to the head of the dynamic reloc list that should
3680
   be used for local symbol ISYM, which is symbol number R_SYMNDX in
3681
   ABFD's symbol table.  Return null if an error occurs.  */
3682
3683
static struct elf_dyn_relocs **
3684
elf32_arm_get_local_dynreloc_list (bfd *abfd, unsigned long r_symndx,
3685
           Elf_Internal_Sym *isym)
3686
0
{
3687
0
  if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
3688
0
    {
3689
0
      struct arm_local_iplt_info *local_iplt;
3690
3691
0
      local_iplt = elf32_arm_create_local_iplt (abfd, r_symndx);
3692
0
      if (local_iplt == NULL)
3693
0
  return NULL;
3694
0
      return &local_iplt->dyn_relocs;
3695
0
    }
3696
0
  else
3697
0
    {
3698
      /* Track dynamic relocs needed for local syms too.
3699
   We really need local syms available to do this
3700
   easily.  Oh well.  */
3701
0
      asection *s;
3702
0
      void *vpp;
3703
3704
0
      s = bfd_section_from_elf_index (abfd, isym->st_shndx);
3705
0
      if (s == NULL)
3706
0
  return NULL;
3707
3708
0
      vpp = &elf_section_data (s)->local_dynrel;
3709
0
      return (struct elf_dyn_relocs **) vpp;
3710
0
    }
3711
0
}
3712
3713
/* Initialize an entry in the stub hash table.  */
3714
3715
static struct bfd_hash_entry *
3716
stub_hash_newfunc (struct bfd_hash_entry *entry,
3717
       struct bfd_hash_table *table,
3718
       const char *string)
3719
0
{
3720
  /* Allocate the structure if it has not already been allocated by a
3721
     subclass.  */
3722
0
  if (entry == NULL)
3723
0
    {
3724
0
      entry = (struct bfd_hash_entry *)
3725
0
    bfd_hash_allocate (table, sizeof (struct elf32_arm_stub_hash_entry));
3726
0
      if (entry == NULL)
3727
0
  return entry;
3728
0
    }
3729
3730
  /* Call the allocation method of the superclass.  */
3731
0
  entry = bfd_hash_newfunc (entry, table, string);
3732
0
  if (entry != NULL)
3733
0
    {
3734
0
      struct elf32_arm_stub_hash_entry *eh;
3735
3736
      /* Initialize the local fields.  */
3737
0
      eh = (struct elf32_arm_stub_hash_entry *) entry;
3738
0
      eh->stub_sec = NULL;
3739
0
      eh->stub_offset = (bfd_vma) -1;
3740
0
      eh->source_value = 0;
3741
0
      eh->target_value = 0;
3742
0
      eh->target_section = NULL;
3743
0
      eh->orig_insn = 0;
3744
0
      eh->stub_type = arm_stub_none;
3745
0
      eh->stub_size = 0;
3746
0
      eh->stub_template = NULL;
3747
0
      eh->stub_template_size = -1;
3748
0
      eh->h = NULL;
3749
0
      eh->id_sec = NULL;
3750
0
      eh->output_name = NULL;
3751
0
    }
3752
3753
0
  return entry;
3754
0
}
3755
3756
/* Create .got, .gotplt, and .rel(a).got sections in DYNOBJ, and set up
3757
   shortcuts to them in our hash table.  */
3758
3759
static bool
3760
create_got_section (bfd *dynobj, struct bfd_link_info *info)
3761
0
{
3762
0
  struct elf32_arm_link_hash_table *htab;
3763
3764
0
  htab = elf32_arm_hash_table (info);
3765
0
  if (htab == NULL)
3766
0
    return false;
3767
3768
0
  if (! _bfd_elf_create_got_section (dynobj, info))
3769
0
    return false;
3770
3771
  /* Also create .rofixup.  */
3772
0
  if (htab->fdpic_p)
3773
0
    {
3774
0
      htab->srofixup = bfd_make_section_with_flags (dynobj, ".rofixup",
3775
0
                (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3776
0
                 | SEC_IN_MEMORY | SEC_LINKER_CREATED | SEC_READONLY));
3777
0
      if (htab->srofixup == NULL
3778
0
    || !bfd_set_section_alignment (htab->srofixup, 2))
3779
0
  return false;
3780
0
    }
3781
3782
0
  return true;
3783
0
}
3784
3785
/* Create the .iplt, .rel(a).iplt and .igot.plt sections.  */
3786
3787
static bool
3788
create_ifunc_sections (struct bfd_link_info *info)
3789
0
{
3790
0
  struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
3791
0
  bfd *dynobj = htab->root.dynobj;
3792
0
  elf_backend_data *bed = get_elf_backend_data (dynobj);
3793
0
  asection *s;
3794
0
  flagword flags = bed->dynamic_sec_flags;
3795
3796
0
  if (htab->root.iplt == NULL)
3797
0
    {
3798
0
      s = bfd_make_section_anyway_with_flags (dynobj, ".iplt",
3799
0
                flags | SEC_READONLY | SEC_CODE);
3800
0
      if (s == NULL
3801
0
    || !bfd_set_section_alignment (s, bed->plt_alignment))
3802
0
  return false;
3803
0
      htab->root.iplt = s;
3804
0
    }
3805
3806
0
  if (htab->root.irelplt == NULL)
3807
0
    {
3808
0
      s = bfd_make_section_anyway_with_flags (dynobj,
3809
0
                RELOC_SECTION (htab, ".iplt"),
3810
0
                flags | SEC_READONLY);
3811
0
      if (s == NULL
3812
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
3813
0
  return false;
3814
0
      htab->root.irelplt = s;
3815
0
    }
3816
3817
0
  if (htab->root.igotplt == NULL)
3818
0
    {
3819
0
      s = bfd_make_section_anyway_with_flags (dynobj, ".igot.plt", flags);
3820
0
      if (s == NULL
3821
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
3822
0
  return false;
3823
0
      htab->root.igotplt = s;
3824
0
    }
3825
0
  return true;
3826
0
}
3827
3828
/* Determine if we're dealing with a Thumb only architecture.  */
3829
3830
static bool
3831
using_thumb_only (struct elf32_arm_link_hash_table *globals)
3832
0
{
3833
0
  int arch;
3834
0
  int profile = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3835
0
            Tag_CPU_arch_profile);
3836
3837
0
  if (profile)
3838
0
    return profile == 'M';
3839
3840
0
  arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
3841
3842
  /* Force return logic to be reviewed for each new architecture.  */
3843
0
  BFD_ASSERT (arch <= TAG_CPU_ARCH_V8_1M_MAIN);
3844
3845
0
  if (arch == TAG_CPU_ARCH_V6_M
3846
0
      || arch == TAG_CPU_ARCH_V6S_M
3847
0
      || arch == TAG_CPU_ARCH_V7E_M
3848
0
      || arch == TAG_CPU_ARCH_V8M_BASE
3849
0
      || arch == TAG_CPU_ARCH_V8M_MAIN
3850
0
      || arch == TAG_CPU_ARCH_V8_1M_MAIN)
3851
0
    return true;
3852
3853
0
  return false;
3854
0
}
3855
3856
/* Determine if we're dealing with a Thumb-2 object.  */
3857
3858
static bool
3859
using_thumb2 (struct elf32_arm_link_hash_table *globals)
3860
0
{
3861
0
  int arch;
3862
0
  int thumb_isa = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3863
0
              Tag_THUMB_ISA_use);
3864
3865
  /* No use of thumb permitted, or a legacy thumb-1/2 definition.  */
3866
0
  if (thumb_isa < 3)
3867
0
    return thumb_isa == 2;
3868
3869
  /* Variant of thumb is described by the architecture tag.  */
3870
0
  arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
3871
3872
  /* Force return logic to be reviewed for each new architecture.  */
3873
0
  BFD_ASSERT (arch <= TAG_CPU_ARCH_V8_1M_MAIN);
3874
3875
0
  return (arch == TAG_CPU_ARCH_V6T2
3876
0
    || arch == TAG_CPU_ARCH_V7
3877
0
    || arch == TAG_CPU_ARCH_V7E_M
3878
0
    || arch == TAG_CPU_ARCH_V8
3879
0
    || arch == TAG_CPU_ARCH_V8R
3880
0
    || arch == TAG_CPU_ARCH_V8M_MAIN
3881
0
    || arch == TAG_CPU_ARCH_V8_1M_MAIN);
3882
0
}
3883
3884
/* Determine whether Thumb-2 BL instruction is available.  */
3885
3886
static bool
3887
using_thumb2_bl (struct elf32_arm_link_hash_table *globals)
3888
0
{
3889
0
  int arch =
3890
0
    bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
3891
3892
  /* Force return logic to be reviewed for each new architecture.  */
3893
0
  BFD_ASSERT (arch <= TAG_CPU_ARCH_V9);
3894
3895
  /* Architecture was introduced after ARMv6T2 (eg. ARMv6-M).  */
3896
0
  return (arch == TAG_CPU_ARCH_V6T2
3897
0
    || arch >= TAG_CPU_ARCH_V7);
3898
0
}
3899
3900
/* Create .plt, .rel(a).plt, .got, .got.plt, .rel(a).got, .dynbss, and
3901
   .rel(a).bss sections in DYNOBJ, and set up shortcuts to them in our
3902
   hash table.  */
3903
3904
static bool
3905
elf32_arm_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
3906
0
{
3907
0
  struct elf32_arm_link_hash_table *htab;
3908
3909
0
  htab = elf32_arm_hash_table (info);
3910
0
  if (htab == NULL)
3911
0
    return false;
3912
3913
0
  if (!htab->root.sgot && !create_got_section (dynobj, info))
3914
0
    return false;
3915
3916
0
  if (!_bfd_elf_create_dynamic_sections (dynobj, info))
3917
0
    return false;
3918
3919
0
  if (htab->root.target_os == is_vxworks)
3920
0
    {
3921
0
      if (!elf_vxworks_create_dynamic_sections (dynobj, info, &htab->srelplt2))
3922
0
  return false;
3923
3924
0
      if (bfd_link_pic (info))
3925
0
  {
3926
0
    htab->plt_header_size = 0;
3927
0
    htab->plt_entry_size
3928
0
      = 4 * ARRAY_SIZE (elf32_arm_vxworks_shared_plt_entry);
3929
0
  }
3930
0
      else
3931
0
  {
3932
0
    htab->plt_header_size
3933
0
      = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt0_entry);
3934
0
    htab->plt_entry_size
3935
0
      = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt_entry);
3936
0
  }
3937
3938
0
      if (elf_elfheader (dynobj))
3939
0
  elf_elfheader (dynobj)->e_ident[EI_CLASS] = ELFCLASS32;
3940
0
    }
3941
0
  else
3942
0
    {
3943
      /* PR ld/16017
3944
   Test for thumb only architectures.  Note - we cannot just call
3945
   using_thumb_only() as the attributes in the output bfd have not been
3946
   initialised at this point, so instead we use the input bfd.  */
3947
0
      bfd * saved_obfd = htab->obfd;
3948
3949
0
      htab->obfd = dynobj;
3950
0
      if (using_thumb_only (htab))
3951
0
  {
3952
0
    htab->plt_header_size = 4 * ARRAY_SIZE (elf32_thumb2_plt0_entry);
3953
0
    htab->plt_entry_size  = 4 * ARRAY_SIZE (elf32_thumb2_plt_entry);
3954
0
  }
3955
0
      htab->obfd = saved_obfd;
3956
0
    }
3957
3958
0
  if (htab->fdpic_p) {
3959
0
    htab->plt_header_size = 0;
3960
0
    if (info->flags & DF_BIND_NOW)
3961
0
      htab->plt_entry_size = 4 * (ARRAY_SIZE (elf32_arm_fdpic_plt_entry) - 5);
3962
0
    else
3963
0
      htab->plt_entry_size = 4 * ARRAY_SIZE (elf32_arm_fdpic_plt_entry);
3964
0
  }
3965
3966
0
  if (!htab->root.splt
3967
0
      || !htab->root.srelplt
3968
0
      || !htab->root.sdynbss
3969
0
      || (!bfd_link_pic (info) && !htab->root.srelbss))
3970
0
    abort ();
3971
3972
0
  return true;
3973
0
}
3974
3975
/* Copy the extra info we tack onto an elf_link_hash_entry.  */
3976
3977
static void
3978
elf32_arm_copy_indirect_symbol (struct bfd_link_info *info,
3979
        struct elf_link_hash_entry *dir,
3980
        struct elf_link_hash_entry *ind)
3981
0
{
3982
0
  struct elf32_arm_link_hash_entry *edir, *eind;
3983
3984
0
  edir = (struct elf32_arm_link_hash_entry *) dir;
3985
0
  eind = (struct elf32_arm_link_hash_entry *) ind;
3986
3987
0
  if (ind->root.type == bfd_link_hash_indirect)
3988
0
    {
3989
      /* Copy over PLT info.  */
3990
0
      edir->plt.thumb_refcount += eind->plt.thumb_refcount;
3991
0
      eind->plt.thumb_refcount = 0;
3992
0
      edir->plt.maybe_thumb_refcount += eind->plt.maybe_thumb_refcount;
3993
0
      eind->plt.maybe_thumb_refcount = 0;
3994
0
      edir->plt.noncall_refcount += eind->plt.noncall_refcount;
3995
0
      eind->plt.noncall_refcount = 0;
3996
3997
      /* Copy FDPIC counters.  */
3998
0
      edir->fdpic_cnts.gotofffuncdesc_cnt += eind->fdpic_cnts.gotofffuncdesc_cnt;
3999
0
      edir->fdpic_cnts.gotfuncdesc_cnt += eind->fdpic_cnts.gotfuncdesc_cnt;
4000
0
      edir->fdpic_cnts.funcdesc_cnt += eind->fdpic_cnts.funcdesc_cnt;
4001
4002
      /* We should only allocate a function to .iplt once the final
4003
   symbol information is known.  */
4004
0
      BFD_ASSERT (!eind->is_iplt);
4005
4006
0
      if (dir->got.refcount <= 0)
4007
0
  {
4008
0
    edir->tls_type = eind->tls_type;
4009
0
    eind->tls_type = GOT_UNKNOWN;
4010
0
  }
4011
0
    }
4012
4013
0
  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
4014
0
}
4015
4016
/* Destroy an ARM elf linker hash table.  */
4017
4018
static void
4019
elf32_arm_link_hash_table_free (bfd *obfd)
4020
0
{
4021
0
  struct elf32_arm_link_hash_table *ret
4022
0
    = (struct elf32_arm_link_hash_table *) obfd->link.hash;
4023
4024
0
  bfd_hash_table_free (&ret->stub_hash_table);
4025
0
  _bfd_elf_link_hash_table_free (obfd);
4026
0
}
4027
4028
/* Create an ARM elf linker hash table.  */
4029
4030
static struct bfd_link_hash_table *
4031
elf32_arm_link_hash_table_create (bfd *abfd)
4032
0
{
4033
0
  struct elf32_arm_link_hash_table *ret;
4034
0
  size_t amt = sizeof (struct elf32_arm_link_hash_table);
4035
4036
0
  ret = (struct elf32_arm_link_hash_table *) bfd_zmalloc (amt);
4037
0
  if (ret == NULL)
4038
0
    return NULL;
4039
4040
0
  if (!_bfd_elf_link_hash_table_init (& ret->root, abfd,
4041
0
              elf32_arm_link_hash_newfunc,
4042
0
              sizeof (struct elf32_arm_link_hash_entry)))
4043
0
    {
4044
0
      free (ret);
4045
0
      return NULL;
4046
0
    }
4047
4048
0
  ret->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
4049
0
  ret->stm32l4xx_fix = BFD_ARM_STM32L4XX_FIX_NONE;
4050
#ifdef FOUR_WORD_PLT
4051
  ret->plt_header_size = 16;
4052
  ret->plt_entry_size = 16;
4053
#else
4054
0
  ret->plt_header_size = 20;
4055
0
  ret->plt_entry_size = elf32_arm_use_long_plt_entry ? 16 : 12;
4056
0
#endif
4057
0
  ret->use_rel = true;
4058
0
  ret->obfd = abfd;
4059
0
  ret->fdpic_p = 0;
4060
4061
0
  if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
4062
0
          sizeof (struct elf32_arm_stub_hash_entry)))
4063
0
    {
4064
0
      _bfd_elf_link_hash_table_free (abfd);
4065
0
      return NULL;
4066
0
    }
4067
0
  ret->root.root.hash_table_free = elf32_arm_link_hash_table_free;
4068
4069
0
  return &ret->root.root;
4070
0
}
4071
4072
/* Determine what kind of NOPs are available.  */
4073
4074
static bool
4075
arch_has_arm_nop (struct elf32_arm_link_hash_table *globals)
4076
0
{
4077
0
  const int arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
4078
0
               Tag_CPU_arch);
4079
4080
  /* Force return logic to be reviewed for each new architecture.  */
4081
0
  BFD_ASSERT (arch <= TAG_CPU_ARCH_V9);
4082
4083
0
  return (arch == TAG_CPU_ARCH_V6T2
4084
0
    || arch == TAG_CPU_ARCH_V6K
4085
0
    || arch == TAG_CPU_ARCH_V7
4086
0
    || arch == TAG_CPU_ARCH_V8
4087
0
    || arch == TAG_CPU_ARCH_V8R
4088
0
    || arch == TAG_CPU_ARCH_V9);
4089
0
}
4090
4091
static bool
4092
arm_stub_is_thumb (enum elf32_arm_stub_type stub_type)
4093
0
{
4094
0
  switch (stub_type)
4095
0
    {
4096
0
    case arm_stub_long_branch_thumb_only:
4097
0
    case arm_stub_long_branch_thumb2_only:
4098
0
    case arm_stub_long_branch_thumb2_only_pure:
4099
0
    case arm_stub_long_branch_v4t_thumb_arm:
4100
0
    case arm_stub_short_branch_v4t_thumb_arm:
4101
0
    case arm_stub_long_branch_v4t_thumb_arm_pic:
4102
0
    case arm_stub_long_branch_v4t_thumb_tls_pic:
4103
0
    case arm_stub_long_branch_thumb_only_pic:
4104
0
    case arm_stub_cmse_branch_thumb_only:
4105
0
      return true;
4106
0
    case arm_stub_none:
4107
0
      BFD_FAIL ();
4108
0
      return false;
4109
0
      break;
4110
0
    default:
4111
0
      return false;
4112
0
    }
4113
0
}
4114
4115
/* Determine the type of stub needed, if any, for a call.  */
4116
4117
static enum elf32_arm_stub_type
4118
arm_type_of_stub (struct bfd_link_info *info,
4119
      asection *input_sec,
4120
      const Elf_Internal_Rela *rel,
4121
      unsigned char st_type,
4122
      enum arm_st_branch_type *actual_branch_type,
4123
      struct elf32_arm_link_hash_entry *hash,
4124
      bfd_vma destination,
4125
      asection *sym_sec,
4126
      bfd *input_bfd,
4127
      const char *name)
4128
0
{
4129
0
  bfd_vma location;
4130
0
  bfd_signed_vma branch_offset;
4131
0
  unsigned int r_type;
4132
0
  struct elf32_arm_link_hash_table * globals;
4133
0
  bool thumb2, thumb2_bl, thumb_only;
4134
0
  enum elf32_arm_stub_type stub_type = arm_stub_none;
4135
0
  int use_plt = 0;
4136
0
  enum arm_st_branch_type branch_type = *actual_branch_type;
4137
0
  union gotplt_union *root_plt;
4138
0
  struct arm_plt_info *arm_plt;
4139
0
  int arch;
4140
0
  int thumb2_movw;
4141
4142
0
  if (branch_type == ST_BRANCH_LONG)
4143
0
    return stub_type;
4144
4145
0
  globals = elf32_arm_hash_table (info);
4146
0
  if (globals == NULL)
4147
0
    return stub_type;
4148
4149
0
  thumb_only = using_thumb_only (globals);
4150
0
  thumb2 = using_thumb2 (globals);
4151
0
  thumb2_bl = using_thumb2_bl (globals);
4152
4153
0
  arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
4154
4155
  /* True for architectures that implement the thumb2 movw instruction.  */
4156
0
  thumb2_movw = thumb2 || (arch  == TAG_CPU_ARCH_V8M_BASE);
4157
4158
  /* Determine where the call point is.  */
4159
0
  location = (input_sec->output_offset
4160
0
        + input_sec->output_section->vma
4161
0
        + rel->r_offset);
4162
4163
0
  r_type = ELF32_R_TYPE (rel->r_info);
4164
4165
  /* Don't pretend we know what stub to use (if any) when we target a
4166
     Thumb-only target and we don't know the actual destination
4167
     type.  */
4168
0
  if (branch_type == ST_BRANCH_UNKNOWN && thumb_only)
4169
0
    return stub_type;
4170
4171
  /* ST_BRANCH_TO_ARM is nonsense to thumb-only targets when we
4172
     are considering a function call relocation.  */
4173
0
  if (thumb_only && (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24
4174
0
         || r_type == R_ARM_THM_JUMP19)
4175
0
      && branch_type == ST_BRANCH_TO_ARM)
4176
0
    {
4177
0
      if (sym_sec == bfd_abs_section_ptr)
4178
  /* As an exception, assume that absolute symbols are of the
4179
     right kind (Thumb).  They are presumably defined in the
4180
     linker script, where it is not possible to declare them as
4181
     Thumb (and thus are seen as Arm mode). We'll inform the
4182
     user with a warning, though, in
4183
     elf32_arm_final_link_relocate. */
4184
0
  branch_type = ST_BRANCH_TO_THUMB;
4185
0
      else
4186
  /* Otherwise do not silently build a stub, and let the users
4187
     know they have to fix their code.  Indeed, we could decide
4188
     to insert a stub involving Arm code and/or BLX, leading to
4189
     a run-time crash.  */
4190
0
  return stub_type;
4191
0
    }
4192
4193
  /* For TLS call relocs, it is the caller's responsibility to provide
4194
     the address of the appropriate trampoline.  */
4195
0
  if (r_type != R_ARM_TLS_CALL
4196
0
      && r_type != R_ARM_THM_TLS_CALL
4197
0
      && elf32_arm_get_plt_info (input_bfd, globals, hash,
4198
0
         ELF32_R_SYM (rel->r_info), &root_plt,
4199
0
         &arm_plt)
4200
0
      && root_plt->offset != (bfd_vma) -1)
4201
0
    {
4202
0
      asection *splt;
4203
4204
0
      if (hash == NULL || hash->is_iplt)
4205
0
  splt = globals->root.iplt;
4206
0
      else
4207
0
  splt = globals->root.splt;
4208
0
      if (splt != NULL)
4209
0
  {
4210
0
    use_plt = 1;
4211
4212
    /* Note when dealing with PLT entries: the main PLT stub is in
4213
       ARM mode, so if the branch is in Thumb mode, another
4214
       Thumb->ARM stub will be inserted later just before the ARM
4215
       PLT stub. If a long branch stub is needed, we'll add a
4216
       Thumb->Arm one and branch directly to the ARM PLT entry.
4217
       Here, we have to check if a pre-PLT Thumb->ARM stub
4218
       is needed and if it will be close enough.  */
4219
4220
0
    destination = (splt->output_section->vma
4221
0
       + splt->output_offset
4222
0
       + root_plt->offset);
4223
0
    st_type = STT_FUNC;
4224
4225
    /* Thumb branch/call to PLT: it can become a branch to ARM
4226
       or to Thumb. We must perform the same checks and
4227
       corrections as in elf32_arm_final_link_relocate.  */
4228
0
    if ((r_type == R_ARM_THM_CALL)
4229
0
        || (r_type == R_ARM_THM_JUMP24))
4230
0
      {
4231
0
        if (globals->use_blx
4232
0
      && r_type == R_ARM_THM_CALL
4233
0
      && !thumb_only)
4234
0
    {
4235
      /* If the Thumb BLX instruction is available, convert
4236
         the BL to a BLX instruction to call the ARM-mode
4237
         PLT entry.  */
4238
0
      branch_type = ST_BRANCH_TO_ARM;
4239
0
    }
4240
0
        else
4241
0
    {
4242
0
      if (!thumb_only)
4243
        /* Target the Thumb stub before the ARM PLT entry.  */
4244
0
        destination -= PLT_THUMB_STUB_SIZE;
4245
0
      branch_type = ST_BRANCH_TO_THUMB;
4246
0
    }
4247
0
      }
4248
0
    else
4249
0
      {
4250
0
        branch_type = ST_BRANCH_TO_ARM;
4251
0
      }
4252
0
  }
4253
0
    }
4254
  /* Calls to STT_GNU_IFUNC symbols should go through a PLT.  */
4255
0
  BFD_ASSERT (st_type != STT_GNU_IFUNC);
4256
4257
0
  branch_offset = (bfd_signed_vma)(destination - location);
4258
4259
0
  if (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24
4260
0
      || r_type == R_ARM_THM_TLS_CALL || r_type == R_ARM_THM_JUMP19)
4261
0
    {
4262
      /* Handle cases where:
4263
   - this call goes too far (different Thumb/Thumb2 max
4264
     distance)
4265
   - it's a Thumb->Arm call and blx is not available, or it's a
4266
     Thumb->Arm branch (not bl). A stub is needed in this case,
4267
     but only if this call is not through a PLT entry. Indeed,
4268
     PLT stubs handle mode switching already.  */
4269
0
      if ((!thumb2_bl
4270
0
      && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
4271
0
    || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
4272
0
    || (thumb2_bl
4273
0
        && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
4274
0
      || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
4275
0
    || (thumb2
4276
0
        && (branch_offset > THM2_MAX_FWD_COND_BRANCH_OFFSET
4277
0
      || (branch_offset < THM2_MAX_BWD_COND_BRANCH_OFFSET))
4278
0
        && (r_type == R_ARM_THM_JUMP19))
4279
0
    || (branch_type == ST_BRANCH_TO_ARM
4280
0
        && (((r_type == R_ARM_THM_CALL
4281
0
        || r_type == R_ARM_THM_TLS_CALL) && !globals->use_blx)
4282
0
      || (r_type == R_ARM_THM_JUMP24)
4283
0
      || (r_type == R_ARM_THM_JUMP19))
4284
0
        && !use_plt))
4285
0
  {
4286
    /* If we need to insert a Thumb-Thumb long branch stub to a
4287
       PLT, use one that branches directly to the ARM PLT
4288
       stub. If we pretended we'd use the pre-PLT Thumb->ARM
4289
       stub, undo this now.  */
4290
0
    if ((branch_type == ST_BRANCH_TO_THUMB) && use_plt && !thumb_only)
4291
0
      {
4292
0
        branch_type = ST_BRANCH_TO_ARM;
4293
0
        branch_offset += PLT_THUMB_STUB_SIZE;
4294
0
      }
4295
4296
0
    if (branch_type == ST_BRANCH_TO_THUMB)
4297
0
      {
4298
        /* Thumb to thumb.  */
4299
0
        if (!thumb_only)
4300
0
    {
4301
0
      if (input_sec->flags & SEC_ELF_PURECODE)
4302
0
        _bfd_error_handler
4303
0
          (_("%pB(%pA): warning: long branch veneers used in"
4304
0
       " section with SHF_ARM_PURECODE section"
4305
0
       " attribute is only supported for M-profile"
4306
0
       " targets that implement the movw instruction"),
4307
0
           input_bfd, input_sec);
4308
4309
0
      stub_type = (bfd_link_pic (info) | globals->pic_veneer)
4310
        /* PIC stubs.  */
4311
0
        ? ((globals->use_blx
4312
0
      && (r_type == R_ARM_THM_CALL))
4313
           /* V5T and above. Stub starts with ARM code, so
4314
        we must be able to switch mode before
4315
        reaching it, which is only possible for 'bl'
4316
        (ie R_ARM_THM_CALL relocation).  */
4317
0
           ? arm_stub_long_branch_any_thumb_pic
4318
           /* On V4T, use Thumb code only.  */
4319
0
           : arm_stub_long_branch_v4t_thumb_thumb_pic)
4320
4321
        /* non-PIC stubs.  */
4322
0
        : ((globals->use_blx
4323
0
      && (r_type == R_ARM_THM_CALL))
4324
           /* V5T and above.  */
4325
0
           ? arm_stub_long_branch_any_any
4326
           /* V4T.  */
4327
0
           : arm_stub_long_branch_v4t_thumb_thumb);
4328
0
    }
4329
0
        else
4330
0
    {
4331
0
      if (thumb2_movw && (input_sec->flags & SEC_ELF_PURECODE))
4332
0
          stub_type = arm_stub_long_branch_thumb2_only_pure;
4333
0
      else
4334
0
        {
4335
0
          if (input_sec->flags & SEC_ELF_PURECODE)
4336
0
      _bfd_error_handler
4337
0
        (_("%pB(%pA): warning: long branch veneers used in"
4338
0
           " section with SHF_ARM_PURECODE section"
4339
0
           " attribute is only supported for M-profile"
4340
0
           " targets that implement the movw instruction"),
4341
0
         input_bfd, input_sec);
4342
4343
0
          stub_type = (bfd_link_pic (info) | globals->pic_veneer)
4344
      /* PIC stub.  */
4345
0
      ? arm_stub_long_branch_thumb_only_pic
4346
      /* non-PIC stub.  */
4347
0
      : (thumb2 ? arm_stub_long_branch_thumb2_only
4348
0
          : arm_stub_long_branch_thumb_only);
4349
0
        }
4350
0
    }
4351
0
      }
4352
0
    else
4353
0
      {
4354
0
        if (input_sec->flags & SEC_ELF_PURECODE)
4355
0
    _bfd_error_handler
4356
0
      (_("%pB(%pA): warning: long branch veneers used in"
4357
0
         " section with SHF_ARM_PURECODE section"
4358
0
         " attribute is only supported" " for M-profile"
4359
0
         " targets that implement the movw instruction"),
4360
0
       input_bfd, input_sec);
4361
4362
        /* Thumb to arm.  */
4363
0
        if (sym_sec != NULL
4364
0
      && sym_sec->owner != NULL
4365
0
      && !INTERWORK_FLAG (sym_sec->owner))
4366
0
    {
4367
0
      _bfd_error_handler
4368
0
        (_("%pB(%s): warning: interworking not enabled;"
4369
0
           " first occurrence: %pB: %s call to %s"),
4370
0
         sym_sec->owner, name, input_bfd, "Thumb", "ARM");
4371
0
    }
4372
4373
0
        stub_type =
4374
0
    (bfd_link_pic (info) | globals->pic_veneer)
4375
    /* PIC stubs.  */
4376
0
    ? (r_type == R_ARM_THM_TLS_CALL
4377
       /* TLS PIC stubs.  */
4378
0
       ? (globals->use_blx ? arm_stub_long_branch_any_tls_pic
4379
0
          : arm_stub_long_branch_v4t_thumb_tls_pic)
4380
0
       : ((globals->use_blx && r_type == R_ARM_THM_CALL)
4381
          /* V5T PIC and above.  */
4382
0
          ? arm_stub_long_branch_any_arm_pic
4383
          /* V4T PIC stub.  */
4384
0
          : arm_stub_long_branch_v4t_thumb_arm_pic))
4385
4386
    /* non-PIC stubs.  */
4387
0
    : ((globals->use_blx && r_type == R_ARM_THM_CALL)
4388
       /* V5T and above.  */
4389
0
       ? arm_stub_long_branch_any_any
4390
       /* V4T.  */
4391
0
       : arm_stub_long_branch_v4t_thumb_arm);
4392
4393
        /* Handle v4t short branches.  */
4394
0
        if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
4395
0
      && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
4396
0
      && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
4397
0
    stub_type = arm_stub_short_branch_v4t_thumb_arm;
4398
0
      }
4399
0
  }
4400
0
    }
4401
0
  else if (r_type == R_ARM_CALL
4402
0
     || r_type == R_ARM_JUMP24
4403
0
     || r_type == R_ARM_PLT32
4404
0
     || r_type == R_ARM_TLS_CALL)
4405
0
    {
4406
0
      if (input_sec->flags & SEC_ELF_PURECODE)
4407
0
  _bfd_error_handler
4408
0
    (_("%pB(%pA): warning: long branch veneers used in"
4409
0
       " section with SHF_ARM_PURECODE section"
4410
0
       " attribute is only supported for M-profile"
4411
0
       " targets that implement the movw instruction"),
4412
0
     input_bfd, input_sec);
4413
0
      if (branch_type == ST_BRANCH_TO_THUMB)
4414
0
  {
4415
    /* Arm to thumb.  */
4416
4417
0
    if (sym_sec != NULL
4418
0
        && sym_sec->owner != NULL
4419
0
        && !INTERWORK_FLAG (sym_sec->owner))
4420
0
      {
4421
0
        _bfd_error_handler
4422
0
    (_("%pB(%s): warning: interworking not enabled;"
4423
0
       " first occurrence: %pB: %s call to %s"),
4424
0
     sym_sec->owner, name, input_bfd, "ARM", "Thumb");
4425
0
      }
4426
4427
    /* We have an extra 2-bytes reach because of
4428
       the mode change (bit 24 (H) of BLX encoding).  */
4429
0
    if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
4430
0
        || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
4431
0
        || (r_type == R_ARM_CALL && !globals->use_blx)
4432
0
        || (r_type == R_ARM_JUMP24)
4433
0
        || (r_type == R_ARM_PLT32))
4434
0
      {
4435
0
        stub_type = (bfd_link_pic (info) | globals->pic_veneer)
4436
    /* PIC stubs.  */
4437
0
    ? ((globals->use_blx)
4438
       /* V5T and above.  */
4439
0
       ? arm_stub_long_branch_any_thumb_pic
4440
       /* V4T stub.  */
4441
0
       : arm_stub_long_branch_v4t_arm_thumb_pic)
4442
4443
    /* non-PIC stubs.  */
4444
0
    : ((globals->use_blx)
4445
       /* V5T and above.  */
4446
0
       ? arm_stub_long_branch_any_any
4447
       /* V4T.  */
4448
0
       : arm_stub_long_branch_v4t_arm_thumb);
4449
0
      }
4450
0
  }
4451
0
      else
4452
0
  {
4453
    /* Arm to arm.  */
4454
0
    if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
4455
0
        || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
4456
0
      {
4457
0
        stub_type =
4458
0
    (bfd_link_pic (info) | globals->pic_veneer)
4459
    /* PIC stubs.  */
4460
0
    ? (r_type == R_ARM_TLS_CALL
4461
       /* TLS PIC Stub.  */
4462
0
       ? arm_stub_long_branch_any_tls_pic
4463
0
       : arm_stub_long_branch_any_arm_pic)
4464
    /* non-PIC stubs.  */
4465
0
    : arm_stub_long_branch_any_any;
4466
0
      }
4467
0
  }
4468
0
    }
4469
4470
  /* If a stub is needed, record the actual destination type.  */
4471
0
  if (stub_type != arm_stub_none)
4472
0
    *actual_branch_type = branch_type;
4473
4474
0
  return stub_type;
4475
0
}
4476
4477
/* Build a name for an entry in the stub hash table.  */
4478
4479
static char *
4480
elf32_arm_stub_name (const asection *input_section,
4481
         const asection *sym_sec,
4482
         const struct elf32_arm_link_hash_entry *hash,
4483
         const Elf_Internal_Rela *rel,
4484
         enum elf32_arm_stub_type stub_type)
4485
0
{
4486
0
  char *stub_name;
4487
0
  bfd_size_type len;
4488
4489
0
  if (hash)
4490
0
    {
4491
0
      len = 8 + 1 + strlen (hash->root.root.root.string) + 1 + 8 + 1 + 2 + 1;
4492
0
      stub_name = (char *) bfd_malloc (len);
4493
0
      if (stub_name != NULL)
4494
0
  sprintf (stub_name, "%08x_%s+%x_%d",
4495
0
     input_section->id & 0xffffffff,
4496
0
     hash->root.root.root.string,
4497
0
     (int) rel->r_addend & 0xffffffff,
4498
0
     (int) stub_type);
4499
0
    }
4500
0
  else
4501
0
    {
4502
0
      len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1 + 2 + 1;
4503
0
      stub_name = (char *) bfd_malloc (len);
4504
0
      if (stub_name != NULL)
4505
0
  sprintf (stub_name, "%08x_%x:%x+%x_%d",
4506
0
     input_section->id & 0xffffffff,
4507
0
     sym_sec->id & 0xffffffff,
4508
0
     ELF32_R_TYPE (rel->r_info) == R_ARM_TLS_CALL
4509
0
     || ELF32_R_TYPE (rel->r_info) == R_ARM_THM_TLS_CALL
4510
0
     ? 0 : (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
4511
0
     (int) rel->r_addend & 0xffffffff,
4512
0
     (int) stub_type);
4513
0
    }
4514
4515
0
  return stub_name;
4516
0
}
4517
4518
/* Look up an entry in the stub hash.  Stub entries are cached because
4519
   creating the stub name takes a bit of time.  */
4520
4521
static struct elf32_arm_stub_hash_entry *
4522
elf32_arm_get_stub_entry (const asection *input_section,
4523
        const asection *sym_sec,
4524
        struct elf_link_hash_entry *hash,
4525
        const Elf_Internal_Rela *rel,
4526
        struct elf32_arm_link_hash_table *htab,
4527
        enum elf32_arm_stub_type stub_type)
4528
0
{
4529
0
  struct elf32_arm_stub_hash_entry *stub_entry;
4530
0
  struct elf32_arm_link_hash_entry *h = (struct elf32_arm_link_hash_entry *) hash;
4531
0
  const asection *id_sec;
4532
4533
0
  if ((input_section->flags & SEC_CODE) == 0)
4534
0
    return NULL;
4535
4536
  /* If the input section is the CMSE stubs one and it needs a long
4537
     branch stub to reach it's final destination, give up with an
4538
     error message: this is not supported.  See PR ld/24709.  */
4539
0
  if (!strncmp (input_section->name, CMSE_STUB_NAME, strlen (CMSE_STUB_NAME)))
4540
0
    {
4541
0
      bfd *output_bfd = htab->obfd;
4542
0
      asection *out_sec = bfd_get_section_by_name (output_bfd, CMSE_STUB_NAME);
4543
4544
0
      _bfd_error_handler (_("ERROR: CMSE stub (%s section) too far "
4545
0
          "(%#" PRIx64 ") from destination (%#" PRIx64 ")"),
4546
0
        CMSE_STUB_NAME,
4547
0
        (uint64_t)out_sec->output_section->vma
4548
0
          + out_sec->output_offset,
4549
0
        (uint64_t)sym_sec->output_section->vma
4550
0
          + sym_sec->output_offset
4551
0
          + h->root.root.u.def.value);
4552
      /* Exit, rather than leave incompletely processed
4553
   relocations.  */
4554
0
      xexit (1);
4555
0
    }
4556
4557
  /* If this input section is part of a group of sections sharing one
4558
     stub section, then use the id of the first section in the group.
4559
     Stub names need to include a section id, as there may well be
4560
     more than one stub used to reach say, printf, and we need to
4561
     distinguish between them.  */
4562
0
  BFD_ASSERT (input_section->id <= htab->top_id);
4563
0
  id_sec = htab->stub_group[input_section->id].link_sec;
4564
4565
0
  if (h != NULL && h->stub_cache != NULL
4566
0
      && h->stub_cache->h == h
4567
0
      && h->stub_cache->id_sec == id_sec
4568
0
      && h->stub_cache->stub_type == stub_type)
4569
0
    {
4570
0
      stub_entry = h->stub_cache;
4571
0
    }
4572
0
  else
4573
0
    {
4574
0
      char *stub_name;
4575
4576
0
      stub_name = elf32_arm_stub_name (id_sec, sym_sec, h, rel, stub_type);
4577
0
      if (stub_name == NULL)
4578
0
  return NULL;
4579
4580
0
      stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table,
4581
0
          stub_name, false, false);
4582
0
      if (h != NULL)
4583
0
  h->stub_cache = stub_entry;
4584
4585
0
      free (stub_name);
4586
0
    }
4587
4588
0
  return stub_entry;
4589
0
}
4590
4591
/* Whether veneers of type STUB_TYPE require to be in a dedicated output
4592
   section.  */
4593
4594
static bool
4595
arm_dedicated_stub_output_section_required (enum elf32_arm_stub_type stub_type)
4596
0
{
4597
0
  if (stub_type >= max_stub_type)
4598
0
    abort ();  /* Should be unreachable.  */
4599
4600
0
  switch (stub_type)
4601
0
    {
4602
0
    case arm_stub_cmse_branch_thumb_only:
4603
0
      return true;
4604
4605
0
    default:
4606
0
      return false;
4607
0
    }
4608
4609
0
  abort ();  /* Should be unreachable.  */
4610
0
}
4611
4612
/* Required alignment (as a power of 2) for the dedicated section holding
4613
   veneers of type STUB_TYPE, or 0 if veneers of this type are interspersed
4614
   with input sections.  */
4615
4616
static int
4617
arm_dedicated_stub_output_section_required_alignment
4618
  (enum elf32_arm_stub_type stub_type)
4619
0
{
4620
0
  if (stub_type >= max_stub_type)
4621
0
    abort ();  /* Should be unreachable.  */
4622
4623
0
  switch (stub_type)
4624
0
    {
4625
    /* Vectors of Secure Gateway veneers must be aligned on 32byte
4626
       boundary.  */
4627
0
    case arm_stub_cmse_branch_thumb_only:
4628
0
      return 5;
4629
4630
0
    default:
4631
0
      BFD_ASSERT (!arm_dedicated_stub_output_section_required (stub_type));
4632
0
      return 0;
4633
0
    }
4634
4635
0
  abort ();  /* Should be unreachable.  */
4636
0
}
4637
4638
/* Name of the dedicated output section to put veneers of type STUB_TYPE, or
4639
   NULL if veneers of this type are interspersed with input sections.  */
4640
4641
static const char *
4642
arm_dedicated_stub_output_section_name (enum elf32_arm_stub_type stub_type)
4643
0
{
4644
0
  if (stub_type >= max_stub_type)
4645
0
    abort ();  /* Should be unreachable.  */
4646
4647
0
  switch (stub_type)
4648
0
    {
4649
0
    case arm_stub_cmse_branch_thumb_only:
4650
0
      return CMSE_STUB_NAME;
4651
4652
0
    default:
4653
0
      BFD_ASSERT (!arm_dedicated_stub_output_section_required (stub_type));
4654
0
      return NULL;
4655
0
    }
4656
4657
0
  abort ();  /* Should be unreachable.  */
4658
0
}
4659
4660
/* If veneers of type STUB_TYPE should go in a dedicated output section,
4661
   returns the address of the hash table field in HTAB holding a pointer to the
4662
   corresponding input section.  Otherwise, returns NULL.  */
4663
4664
static asection **
4665
arm_dedicated_stub_input_section_ptr (struct elf32_arm_link_hash_table *htab,
4666
              enum elf32_arm_stub_type stub_type)
4667
0
{
4668
0
  if (stub_type >= max_stub_type)
4669
0
    abort ();  /* Should be unreachable.  */
4670
4671
0
  switch (stub_type)
4672
0
    {
4673
0
    case arm_stub_cmse_branch_thumb_only:
4674
0
      return &htab->cmse_stub_sec;
4675
4676
0
    default:
4677
0
      BFD_ASSERT (!arm_dedicated_stub_output_section_required (stub_type));
4678
0
      return NULL;
4679
0
    }
4680
4681
0
  abort ();  /* Should be unreachable.  */
4682
0
}
4683
4684
/* Find or create a stub section to contain a stub of type STUB_TYPE.  SECTION
4685
   is the section that branch into veneer and can be NULL if stub should go in
4686
   a dedicated output section.  Returns a pointer to the stub section, and the
4687
   section to which the stub section will be attached (in *LINK_SEC_P).
4688
   LINK_SEC_P may be NULL.  */
4689
4690
static asection *
4691
elf32_arm_create_or_find_stub_sec (asection **link_sec_p, asection *section,
4692
           struct elf32_arm_link_hash_table *htab,
4693
           enum elf32_arm_stub_type stub_type)
4694
0
{
4695
0
  asection *link_sec, *out_sec, **stub_sec_p;
4696
0
  const char *stub_sec_prefix;
4697
0
  bool dedicated_output_section =
4698
0
    arm_dedicated_stub_output_section_required (stub_type);
4699
0
  int align;
4700
4701
0
  if (dedicated_output_section)
4702
0
    {
4703
0
      bfd *output_bfd = htab->obfd;
4704
0
      const char *out_sec_name =
4705
0
  arm_dedicated_stub_output_section_name (stub_type);
4706
0
      link_sec = NULL;
4707
0
      stub_sec_p = arm_dedicated_stub_input_section_ptr (htab, stub_type);
4708
0
      stub_sec_prefix = out_sec_name;
4709
0
      align = arm_dedicated_stub_output_section_required_alignment (stub_type);
4710
0
      out_sec = bfd_get_section_by_name (output_bfd, out_sec_name);
4711
0
      if (out_sec == NULL)
4712
0
  {
4713
0
    _bfd_error_handler (_("no address assigned to the veneers output "
4714
0
        "section %s"), out_sec_name);
4715
0
    return NULL;
4716
0
  }
4717
0
    }
4718
0
  else
4719
0
    {
4720
0
      BFD_ASSERT (section->id <= htab->top_id);
4721
0
      link_sec = htab->stub_group[section->id].link_sec;
4722
0
      BFD_ASSERT (link_sec != NULL);
4723
0
      stub_sec_p = &htab->stub_group[section->id].stub_sec;
4724
0
      if (*stub_sec_p == NULL)
4725
0
  stub_sec_p = &htab->stub_group[link_sec->id].stub_sec;
4726
0
      stub_sec_prefix = link_sec->name;
4727
0
      out_sec = link_sec->output_section;
4728
0
      align = 3;
4729
0
    }
4730
4731
0
  if (*stub_sec_p == NULL)
4732
0
    {
4733
0
      size_t namelen;
4734
0
      bfd_size_type len;
4735
0
      char *s_name;
4736
4737
0
      namelen = strlen (stub_sec_prefix);
4738
0
      len = namelen + sizeof (STUB_SUFFIX);
4739
0
      s_name = (char *) bfd_alloc (htab->stub_bfd, len);
4740
0
      if (s_name == NULL)
4741
0
  return NULL;
4742
4743
0
      memcpy (s_name, stub_sec_prefix, namelen);
4744
0
      memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4745
0
      *stub_sec_p = (*htab->add_stub_section) (s_name, out_sec, link_sec,
4746
0
                 align);
4747
0
      if (*stub_sec_p == NULL)
4748
0
  return NULL;
4749
4750
0
      out_sec->flags |= SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
4751
0
      | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY
4752
0
      | SEC_KEEP;
4753
0
    }
4754
4755
0
  if (!dedicated_output_section)
4756
0
    htab->stub_group[section->id].stub_sec = *stub_sec_p;
4757
4758
0
  if (link_sec_p)
4759
0
    *link_sec_p = link_sec;
4760
4761
0
  return *stub_sec_p;
4762
0
}
4763
4764
/* Add a new stub entry to the stub hash.  Not all fields of the new
4765
   stub entry are initialised.  */
4766
4767
static struct elf32_arm_stub_hash_entry *
4768
elf32_arm_add_stub (const char *stub_name, asection *section,
4769
        struct elf32_arm_link_hash_table *htab,
4770
        enum elf32_arm_stub_type stub_type)
4771
0
{
4772
0
  asection *link_sec;
4773
0
  asection *stub_sec;
4774
0
  struct elf32_arm_stub_hash_entry *stub_entry;
4775
4776
0
  stub_sec = elf32_arm_create_or_find_stub_sec (&link_sec, section, htab,
4777
0
            stub_type);
4778
0
  if (stub_sec == NULL)
4779
0
    return NULL;
4780
4781
  /* Enter this entry into the linker stub hash table.  */
4782
0
  stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4783
0
             true, false);
4784
0
  if (stub_entry == NULL)
4785
0
    {
4786
0
      if (section == NULL)
4787
0
  section = stub_sec;
4788
0
      _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4789
0
        section->owner, stub_name);
4790
0
      return NULL;
4791
0
    }
4792
4793
0
  stub_entry->stub_sec = stub_sec;
4794
0
  stub_entry->stub_offset = (bfd_vma) -1;
4795
0
  stub_entry->id_sec = link_sec;
4796
4797
0
  return stub_entry;
4798
0
}
4799
4800
/* Store an Arm insn into an output section not processed by
4801
   elf32_arm_write_section.  */
4802
4803
static void
4804
put_arm_insn (struct elf32_arm_link_hash_table * htab,
4805
        bfd * output_bfd, bfd_vma val, void * ptr)
4806
0
{
4807
0
  if (htab->byteswap_code != bfd_little_endian (output_bfd))
4808
0
    bfd_putl32 (val, ptr);
4809
0
  else
4810
0
    bfd_putb32 (val, ptr);
4811
0
}
4812
4813
/* Store a 16-bit Thumb insn into an output section not processed by
4814
   elf32_arm_write_section.  */
4815
4816
static void
4817
put_thumb_insn (struct elf32_arm_link_hash_table * htab,
4818
    bfd * output_bfd, bfd_vma val, void * ptr)
4819
0
{
4820
0
  if (htab->byteswap_code != bfd_little_endian (output_bfd))
4821
0
    bfd_putl16 (val, ptr);
4822
0
  else
4823
0
    bfd_putb16 (val, ptr);
4824
0
}
4825
4826
/* Store a Thumb2 insn into an output section not processed by
4827
   elf32_arm_write_section.  */
4828
4829
static void
4830
put_thumb2_insn (struct elf32_arm_link_hash_table * htab,
4831
     bfd * output_bfd, bfd_vma val, bfd_byte * ptr)
4832
0
{
4833
  /* T2 instructions are 16-bit streamed.  */
4834
0
  if (htab->byteswap_code != bfd_little_endian (output_bfd))
4835
0
    {
4836
0
      bfd_putl16 ((val >> 16) & 0xffff, ptr);
4837
0
      bfd_putl16 ((val & 0xffff), ptr + 2);
4838
0
    }
4839
0
  else
4840
0
    {
4841
0
      bfd_putb16 ((val >> 16) & 0xffff, ptr);
4842
0
      bfd_putb16 ((val & 0xffff), ptr + 2);
4843
0
    }
4844
0
}
4845
4846
/* If it's possible to change R_TYPE to a more efficient access
4847
   model, return the new reloc type.  */
4848
4849
static unsigned
4850
elf32_arm_tls_transition (struct bfd_link_info *info, int r_type,
4851
        struct elf_link_hash_entry *h)
4852
0
{
4853
0
  int is_local = (h == NULL);
4854
4855
0
  if (bfd_link_dll (info)
4856
0
      || (h && h->root.type == bfd_link_hash_undefweak))
4857
0
    return r_type;
4858
4859
  /* We do not support relaxations for Old TLS models.  */
4860
0
  switch (r_type)
4861
0
    {
4862
0
    case R_ARM_TLS_GOTDESC:
4863
0
    case R_ARM_TLS_CALL:
4864
0
    case R_ARM_THM_TLS_CALL:
4865
0
    case R_ARM_TLS_DESCSEQ:
4866
0
    case R_ARM_THM_TLS_DESCSEQ:
4867
0
      return is_local ? R_ARM_TLS_LE32 : R_ARM_TLS_IE32;
4868
0
    }
4869
4870
0
  return r_type;
4871
0
}
4872
4873
static bfd_reloc_status_type elf32_arm_final_link_relocate
4874
  (reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *,
4875
   Elf_Internal_Rela *, bfd_vma, struct bfd_link_info *, asection *,
4876
   const char *, unsigned char, enum arm_st_branch_type,
4877
   struct elf_link_hash_entry *, bool *, char **);
4878
4879
static unsigned int
4880
arm_stub_required_alignment (enum elf32_arm_stub_type stub_type)
4881
0
{
4882
0
  switch (stub_type)
4883
0
    {
4884
0
    case arm_stub_a8_veneer_b_cond:
4885
0
    case arm_stub_a8_veneer_b:
4886
0
    case arm_stub_a8_veneer_bl:
4887
0
      return 2;
4888
4889
0
    case arm_stub_long_branch_any_any:
4890
0
    case arm_stub_long_branch_v4t_arm_thumb:
4891
0
    case arm_stub_long_branch_thumb_only:
4892
0
    case arm_stub_long_branch_thumb2_only:
4893
0
    case arm_stub_long_branch_thumb2_only_pure:
4894
0
    case arm_stub_long_branch_v4t_thumb_thumb:
4895
0
    case arm_stub_long_branch_v4t_thumb_arm:
4896
0
    case arm_stub_short_branch_v4t_thumb_arm:
4897
0
    case arm_stub_long_branch_any_arm_pic:
4898
0
    case arm_stub_long_branch_any_thumb_pic:
4899
0
    case arm_stub_long_branch_v4t_thumb_thumb_pic:
4900
0
    case arm_stub_long_branch_v4t_arm_thumb_pic:
4901
0
    case arm_stub_long_branch_v4t_thumb_arm_pic:
4902
0
    case arm_stub_long_branch_thumb_only_pic:
4903
0
    case arm_stub_long_branch_any_tls_pic:
4904
0
    case arm_stub_long_branch_v4t_thumb_tls_pic:
4905
0
    case arm_stub_cmse_branch_thumb_only:
4906
0
    case arm_stub_a8_veneer_blx:
4907
0
      return 4;
4908
4909
0
    default:
4910
0
      abort ();  /* Should be unreachable.  */
4911
0
    }
4912
0
}
4913
4914
/* Returns whether stubs of type STUB_TYPE take over the symbol they are
4915
   veneering (TRUE) or have their own symbol (FALSE).  */
4916
4917
static bool
4918
arm_stub_sym_claimed (enum elf32_arm_stub_type stub_type)
4919
0
{
4920
0
  if (stub_type >= max_stub_type)
4921
0
    abort ();  /* Should be unreachable.  */
4922
4923
0
  switch (stub_type)
4924
0
    {
4925
0
    case arm_stub_cmse_branch_thumb_only:
4926
0
      return true;
4927
4928
0
    default:
4929
0
      return false;
4930
0
    }
4931
4932
0
  abort ();  /* Should be unreachable.  */
4933
0
}
4934
4935
/* Returns the padding needed for the dedicated section used stubs of type
4936
   STUB_TYPE.  */
4937
4938
static int
4939
arm_dedicated_stub_section_padding (enum elf32_arm_stub_type stub_type)
4940
0
{
4941
0
  if (stub_type >= max_stub_type)
4942
0
    abort ();  /* Should be unreachable.  */
4943
4944
0
  switch (stub_type)
4945
0
    {
4946
0
    case arm_stub_cmse_branch_thumb_only:
4947
0
      return 32;
4948
4949
0
    default:
4950
0
      return 0;
4951
0
    }
4952
4953
0
  abort ();  /* Should be unreachable.  */
4954
0
}
4955
4956
/* If veneers of type STUB_TYPE should go in a dedicated output section,
4957
   returns the address of the hash table field in HTAB holding the offset at
4958
   which new veneers should be layed out in the stub section.  */
4959
4960
static bfd_vma*
4961
arm_new_stubs_start_offset_ptr (struct elf32_arm_link_hash_table *htab,
4962
        enum elf32_arm_stub_type stub_type)
4963
0
{
4964
0
  switch (stub_type)
4965
0
    {
4966
0
    case arm_stub_cmse_branch_thumb_only:
4967
0
      return &htab->new_cmse_stub_offset;
4968
4969
0
    default:
4970
0
      BFD_ASSERT (!arm_dedicated_stub_output_section_required (stub_type));
4971
0
      return NULL;
4972
0
    }
4973
0
}
4974
4975
static bool
4976
arm_build_one_stub (struct bfd_hash_entry *gen_entry,
4977
        void * in_arg)
4978
0
{
4979
0
#define MAXRELOCS 3
4980
0
  bool removed_sg_veneer;
4981
0
  struct elf32_arm_stub_hash_entry *stub_entry;
4982
0
  struct elf32_arm_link_hash_table *globals;
4983
0
  struct bfd_link_info *info;
4984
0
  asection *stub_sec;
4985
0
  bfd *stub_bfd;
4986
0
  bfd_byte *loc;
4987
0
  bfd_vma sym_value;
4988
0
  int template_size;
4989
0
  int size;
4990
0
  const insn_sequence *template_sequence;
4991
0
  int i;
4992
0
  int stub_reloc_idx[MAXRELOCS] = {-1, -1};
4993
0
  int stub_reloc_offset[MAXRELOCS] = {0, 0};
4994
0
  int nrelocs = 0;
4995
0
  int just_allocated = 0;
4996
4997
  /* Massage our args to the form they really have.  */
4998
0
  stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
4999
0
  info = (struct bfd_link_info *) in_arg;
5000
5001
  /* Fail if the target section could not be assigned to an output
5002
     section.  The user should fix his linker script.  */
5003
0
  if (stub_entry->target_section->output_section == NULL
5004
0
      && info->non_contiguous_regions)
5005
0
    info->callbacks->fatal (_("%P: Could not assign `%pA' to an output section. "
5006
0
            "Retry without --enable-non-contiguous-regions.\n"),
5007
0
          stub_entry->target_section);
5008
5009
0
  globals = elf32_arm_hash_table (info);
5010
0
  if (globals == NULL)
5011
0
    return false;
5012
5013
0
  stub_sec = stub_entry->stub_sec;
5014
5015
0
  if ((globals->fix_cortex_a8 < 0)
5016
0
      != (arm_stub_required_alignment (stub_entry->stub_type) == 2))
5017
    /* We have to do less-strictly-aligned fixes last.  */
5018
0
    return true;
5019
5020
  /* Assign a slot at the end of section if none assigned yet.  */
5021
0
  if (stub_entry->stub_offset == (bfd_vma) -1)
5022
0
    {
5023
0
      stub_entry->stub_offset = stub_sec->size;
5024
0
      just_allocated = 1;
5025
0
    }
5026
0
  loc = stub_sec->contents + stub_entry->stub_offset;
5027
5028
0
  stub_bfd = stub_sec->owner;
5029
5030
  /* This is the address of the stub destination.  */
5031
0
  sym_value = (stub_entry->target_value
5032
0
         + stub_entry->target_section->output_offset
5033
0
         + stub_entry->target_section->output_section->vma);
5034
5035
0
  template_sequence = stub_entry->stub_template;
5036
0
  template_size = stub_entry->stub_template_size;
5037
5038
0
  size = 0;
5039
0
  for (i = 0; i < template_size; i++)
5040
0
    {
5041
0
      switch (template_sequence[i].type)
5042
0
  {
5043
0
  case THUMB16_TYPE:
5044
0
    {
5045
0
      bfd_vma data = (bfd_vma) template_sequence[i].data;
5046
0
      if (template_sequence[i].reloc_addend != 0)
5047
0
        {
5048
    /* We've borrowed the reloc_addend field to mean we should
5049
       insert a condition code into this (Thumb-1 branch)
5050
       instruction.  See THUMB16_BCOND_INSN.  */
5051
0
    BFD_ASSERT ((data & 0xff00) == 0xd000);
5052
0
    data |= ((stub_entry->orig_insn >> 22) & 0xf) << 8;
5053
0
        }
5054
0
      bfd_put_16 (stub_bfd, data, loc + size);
5055
0
      size += 2;
5056
0
    }
5057
0
    break;
5058
5059
0
  case THUMB32_TYPE:
5060
0
    bfd_put_16 (stub_bfd,
5061
0
          (template_sequence[i].data >> 16) & 0xffff,
5062
0
          loc + size);
5063
0
    bfd_put_16 (stub_bfd, template_sequence[i].data & 0xffff,
5064
0
          loc + size + 2);
5065
0
    if (template_sequence[i].r_type != R_ARM_NONE)
5066
0
      {
5067
0
        stub_reloc_idx[nrelocs] = i;
5068
0
        stub_reloc_offset[nrelocs++] = size;
5069
0
      }
5070
0
    size += 4;
5071
0
    break;
5072
5073
0
  case ARM_TYPE:
5074
0
    bfd_put_32 (stub_bfd, template_sequence[i].data,
5075
0
          loc + size);
5076
    /* Handle cases where the target is encoded within the
5077
       instruction.  */
5078
0
    if (template_sequence[i].r_type == R_ARM_JUMP24)
5079
0
      {
5080
0
        stub_reloc_idx[nrelocs] = i;
5081
0
        stub_reloc_offset[nrelocs++] = size;
5082
0
      }
5083
0
    size += 4;
5084
0
    break;
5085
5086
0
  case DATA_TYPE:
5087
0
    bfd_put_32 (stub_bfd, template_sequence[i].data, loc + size);
5088
0
    stub_reloc_idx[nrelocs] = i;
5089
0
    stub_reloc_offset[nrelocs++] = size;
5090
0
    size += 4;
5091
0
    break;
5092
5093
0
  default:
5094
0
    BFD_FAIL ();
5095
0
    return false;
5096
0
  }
5097
0
    }
5098
5099
0
  if (just_allocated)
5100
0
    stub_sec->size += size;
5101
5102
  /* Stub size has already been computed in arm_size_one_stub. Check
5103
     consistency.  */
5104
0
  BFD_ASSERT (size == stub_entry->stub_size);
5105
5106
  /* Destination is Thumb. Force bit 0 to 1 to reflect this.  */
5107
0
  if (stub_entry->branch_type == ST_BRANCH_TO_THUMB)
5108
0
    sym_value |= 1;
5109
5110
  /* Assume non empty slots have at least one and at most MAXRELOCS entries
5111
     to relocate in each stub.  */
5112
0
  removed_sg_veneer =
5113
0
    (size == 0 && stub_entry->stub_type == arm_stub_cmse_branch_thumb_only);
5114
0
  BFD_ASSERT (removed_sg_veneer || (nrelocs != 0 && nrelocs <= MAXRELOCS));
5115
5116
0
  for (i = 0; i < nrelocs; i++)
5117
0
    {
5118
0
      Elf_Internal_Rela rel;
5119
0
      bool unresolved_reloc;
5120
0
      char *error_message;
5121
0
      bfd_vma points_to =
5122
0
  sym_value + template_sequence[stub_reloc_idx[i]].reloc_addend;
5123
5124
0
      rel.r_offset = stub_entry->stub_offset + stub_reloc_offset[i];
5125
0
      rel.r_info = ELF32_R_INFO (0,
5126
0
         template_sequence[stub_reloc_idx[i]].r_type);
5127
0
      rel.r_addend = 0;
5128
5129
0
      if (stub_entry->stub_type == arm_stub_a8_veneer_b_cond && i == 0)
5130
  /* The first relocation in the elf32_arm_stub_a8_veneer_b_cond[]
5131
     template should refer back to the instruction after the original
5132
     branch.  We use target_section as Cortex-A8 erratum workaround stubs
5133
     are only generated when both source and target are in the same
5134
     section.  */
5135
0
  points_to = stub_entry->target_section->output_section->vma
5136
0
        + stub_entry->target_section->output_offset
5137
0
        + stub_entry->source_value;
5138
5139
0
      elf32_arm_final_link_relocate (elf32_arm_howto_from_type
5140
0
    (template_sequence[stub_reloc_idx[i]].r_type),
5141
0
     stub_bfd, info->output_bfd, stub_sec, stub_sec->contents, &rel,
5142
0
     points_to, info, stub_entry->target_section, "", STT_FUNC,
5143
0
     stub_entry->branch_type,
5144
0
     (struct elf_link_hash_entry *) stub_entry->h, &unresolved_reloc,
5145
0
     &error_message);
5146
0
    }
5147
5148
0
  return true;
5149
0
#undef MAXRELOCS
5150
0
}
5151
5152
/* Calculate the template, template size and instruction size for a stub.
5153
   Return value is the instruction size.  */
5154
5155
static unsigned int
5156
find_stub_size_and_template (enum elf32_arm_stub_type stub_type,
5157
           const insn_sequence **stub_template,
5158
           int *stub_template_size)
5159
0
{
5160
0
  const insn_sequence *template_sequence = NULL;
5161
0
  int template_size = 0, i;
5162
0
  unsigned int size;
5163
5164
0
  template_sequence = stub_definitions[stub_type].template_sequence;
5165
0
  if (stub_template)
5166
0
    *stub_template = template_sequence;
5167
5168
0
  template_size = stub_definitions[stub_type].template_size;
5169
0
  if (stub_template_size)
5170
0
    *stub_template_size = template_size;
5171
5172
0
  size = 0;
5173
0
  for (i = 0; i < template_size; i++)
5174
0
    {
5175
0
      switch (template_sequence[i].type)
5176
0
  {
5177
0
  case THUMB16_TYPE:
5178
0
    size += 2;
5179
0
    break;
5180
5181
0
  case ARM_TYPE:
5182
0
  case THUMB32_TYPE:
5183
0
  case DATA_TYPE:
5184
0
    size += 4;
5185
0
    break;
5186
5187
0
  default:
5188
0
    BFD_FAIL ();
5189
0
    return 0;
5190
0
  }
5191
0
    }
5192
5193
0
  return size;
5194
0
}
5195
5196
/* As above, but don't actually build the stub.  Just bump offset so
5197
   we know stub section sizes.  */
5198
5199
static bool
5200
arm_size_one_stub (struct bfd_hash_entry *gen_entry,
5201
       void *in_arg ATTRIBUTE_UNUSED)
5202
0
{
5203
0
  struct elf32_arm_stub_hash_entry *stub_entry;
5204
0
  const insn_sequence *template_sequence;
5205
0
  int template_size, size;
5206
5207
  /* Massage our args to the form they really have.  */
5208
0
  stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
5209
5210
0
  BFD_ASSERT ((stub_entry->stub_type > arm_stub_none)
5211
0
        && stub_entry->stub_type < ARRAY_SIZE (stub_definitions));
5212
5213
0
  size = find_stub_size_and_template (stub_entry->stub_type, &template_sequence,
5214
0
              &template_size);
5215
5216
  /* Initialized to -1.  Null size indicates an empty slot full of zeros.  */
5217
0
  if (stub_entry->stub_template_size)
5218
0
    {
5219
0
      stub_entry->stub_size = size;
5220
0
      stub_entry->stub_template = template_sequence;
5221
0
      stub_entry->stub_template_size = template_size;
5222
0
    }
5223
5224
  /* Already accounted for.  */
5225
0
  if (stub_entry->stub_offset != (bfd_vma) -1)
5226
0
    return true;
5227
5228
0
  size = (size + 7) & ~7;
5229
0
  stub_entry->stub_sec->size += size;
5230
5231
0
  return true;
5232
0
}
5233
5234
/* External entry points for sizing and building linker stubs.  */
5235
5236
/* Set up various things so that we can make a list of input sections
5237
   for each output section included in the link.  Returns -1 on error,
5238
   0 when no stubs will be needed, and 1 on success.  */
5239
5240
int
5241
elf32_arm_setup_section_lists (bfd *output_bfd,
5242
             struct bfd_link_info *info)
5243
0
{
5244
0
  bfd *input_bfd;
5245
0
  unsigned int bfd_count;
5246
0
  unsigned int top_id, top_index;
5247
0
  asection *section;
5248
0
  asection **input_list, **list;
5249
0
  size_t amt;
5250
0
  struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
5251
5252
0
  if (htab == NULL)
5253
0
    return 0;
5254
5255
  /* Count the number of input BFDs and find the top input section id.  */
5256
0
  for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
5257
0
       input_bfd != NULL;
5258
0
       input_bfd = input_bfd->link.next)
5259
0
    {
5260
0
      bfd_count += 1;
5261
0
      for (section = input_bfd->sections;
5262
0
     section != NULL;
5263
0
     section = section->next)
5264
0
  {
5265
0
    if (top_id < section->id)
5266
0
      top_id = section->id;
5267
0
  }
5268
0
    }
5269
0
  htab->bfd_count = bfd_count;
5270
5271
0
  amt = sizeof (struct map_stub) * (top_id + 1);
5272
0
  htab->stub_group = (struct map_stub *) bfd_zmalloc (amt);
5273
0
  if (htab->stub_group == NULL)
5274
0
    return -1;
5275
0
  htab->top_id = top_id;
5276
5277
  /* We can't use output_bfd->section_count here to find the top output
5278
     section index as some sections may have been removed, and
5279
     _bfd_strip_section_from_output doesn't renumber the indices.  */
5280
0
  for (section = output_bfd->sections, top_index = 0;
5281
0
       section != NULL;
5282
0
       section = section->next)
5283
0
    {
5284
0
      if (top_index < section->index)
5285
0
  top_index = section->index;
5286
0
    }
5287
5288
0
  htab->top_index = top_index;
5289
0
  amt = sizeof (asection *) * (top_index + 1);
5290
0
  input_list = (asection **) bfd_malloc (amt);
5291
0
  htab->input_list = input_list;
5292
0
  if (input_list == NULL)
5293
0
    return -1;
5294
5295
  /* For sections we aren't interested in, mark their entries with a
5296
     value we can check later.  */
5297
0
  list = input_list + top_index;
5298
0
  do
5299
0
    *list = bfd_abs_section_ptr;
5300
0
  while (list-- != input_list);
5301
5302
0
  for (section = output_bfd->sections;
5303
0
       section != NULL;
5304
0
       section = section->next)
5305
0
    {
5306
0
      if ((section->flags & SEC_CODE) != 0)
5307
0
  input_list[section->index] = NULL;
5308
0
    }
5309
5310
0
  return 1;
5311
0
}
5312
5313
/* The linker repeatedly calls this function for each input section,
5314
   in the order that input sections are linked into output sections.
5315
   Build lists of input sections to determine groupings between which
5316
   we may insert linker stubs.  */
5317
5318
void
5319
elf32_arm_next_input_section (struct bfd_link_info *info,
5320
            asection *isec)
5321
0
{
5322
0
  struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
5323
5324
0
  if (htab == NULL)
5325
0
    return;
5326
5327
0
  if (isec->output_section->index <= htab->top_index)
5328
0
    {
5329
0
      asection **list = htab->input_list + isec->output_section->index;
5330
5331
0
      if (*list != bfd_abs_section_ptr && (isec->flags & SEC_CODE) != 0)
5332
0
  {
5333
    /* Steal the link_sec pointer for our list.  */
5334
0
#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
5335
    /* This happens to make the list in reverse order,
5336
       which we reverse later.  */
5337
0
    PREV_SEC (isec) = *list;
5338
0
    *list = isec;
5339
0
  }
5340
0
    }
5341
0
}
5342
5343
/* See whether we can group stub sections together.  Grouping stub
5344
   sections may result in fewer stubs.  More importantly, we need to
5345
   put all .init* and .fini* stubs at the end of the .init or
5346
   .fini output sections respectively, because glibc splits the
5347
   _init and _fini functions into multiple parts.  Putting a stub in
5348
   the middle of a function is not a good idea.  */
5349
5350
static void
5351
group_sections (struct elf32_arm_link_hash_table *htab,
5352
    bfd_size_type stub_group_size,
5353
    bool stubs_always_after_branch)
5354
0
{
5355
0
  asection **list = htab->input_list;
5356
5357
0
  do
5358
0
    {
5359
0
      asection *tail = *list;
5360
0
      asection *head;
5361
5362
0
      if (tail == bfd_abs_section_ptr)
5363
0
  continue;
5364
5365
      /* Reverse the list: we must avoid placing stubs at the
5366
   beginning of the section because the beginning of the text
5367
   section may be required for an interrupt vector in bare metal
5368
   code.  */
5369
0
#define NEXT_SEC PREV_SEC
5370
0
      head = NULL;
5371
0
      while (tail != NULL)
5372
0
  {
5373
    /* Pop from tail.  */
5374
0
    asection *item = tail;
5375
0
    tail = PREV_SEC (item);
5376
5377
    /* Push on head.  */
5378
0
    NEXT_SEC (item) = head;
5379
0
    head = item;
5380
0
  }
5381
5382
0
      while (head != NULL)
5383
0
  {
5384
0
    asection *curr;
5385
0
    asection *next;
5386
0
    bfd_vma stub_group_start = head->output_offset;
5387
0
    bfd_vma end_of_next;
5388
5389
0
    curr = head;
5390
0
    while (NEXT_SEC (curr) != NULL)
5391
0
      {
5392
0
        next = NEXT_SEC (curr);
5393
0
        end_of_next = next->output_offset + next->size;
5394
0
        if (end_of_next - stub_group_start >= stub_group_size)
5395
    /* End of NEXT is too far from start, so stop.  */
5396
0
    break;
5397
        /* Add NEXT to the group.  */
5398
0
        curr = next;
5399
0
      }
5400
5401
    /* OK, the size from the start to the start of CURR is less
5402
       than stub_group_size and thus can be handled by one stub
5403
       section.  (Or the head section is itself larger than
5404
       stub_group_size, in which case we may be toast.)
5405
       We should really be keeping track of the total size of
5406
       stubs added here, as stubs contribute to the final output
5407
       section size.  */
5408
0
    do
5409
0
      {
5410
0
        next = NEXT_SEC (head);
5411
        /* Set up this stub group.  */
5412
0
        htab->stub_group[head->id].link_sec = curr;
5413
0
      }
5414
0
    while (head != curr && (head = next) != NULL);
5415
5416
    /* But wait, there's more!  Input sections up to stub_group_size
5417
       bytes after the stub section can be handled by it too.  */
5418
0
    if (!stubs_always_after_branch)
5419
0
      {
5420
0
        stub_group_start = curr->output_offset + curr->size;
5421
5422
0
        while (next != NULL)
5423
0
    {
5424
0
      end_of_next = next->output_offset + next->size;
5425
0
      if (end_of_next - stub_group_start >= stub_group_size)
5426
        /* End of NEXT is too far from stubs, so stop.  */
5427
0
        break;
5428
      /* Add NEXT to the stub group.  */
5429
0
      head = next;
5430
0
      next = NEXT_SEC (head);
5431
0
      htab->stub_group[head->id].link_sec = curr;
5432
0
    }
5433
0
      }
5434
0
    head = next;
5435
0
  }
5436
0
    }
5437
0
  while (list++ != htab->input_list + htab->top_index);
5438
5439
0
  free (htab->input_list);
5440
0
#undef PREV_SEC
5441
0
#undef NEXT_SEC
5442
0
}
5443
5444
/* Comparison function for sorting/searching relocations relating to Cortex-A8
5445
   erratum fix.  */
5446
5447
static int
5448
a8_reloc_compare (const void *a, const void *b)
5449
0
{
5450
0
  const struct a8_erratum_reloc *ra = (const struct a8_erratum_reloc *) a;
5451
0
  const struct a8_erratum_reloc *rb = (const struct a8_erratum_reloc *) b;
5452
5453
0
  if (ra->from < rb->from)
5454
0
    return -1;
5455
0
  else if (ra->from > rb->from)
5456
0
    return 1;
5457
0
  else
5458
0
    return 0;
5459
0
}
5460
5461
static struct elf_link_hash_entry *find_thumb_glue (struct bfd_link_info *,
5462
                const char *, char **);
5463
5464
/* Helper function to scan code for sequences which might trigger the Cortex-A8
5465
   branch/TLB erratum.  Fill in the table described by A8_FIXES_P,
5466
   NUM_A8_FIXES_P, A8_FIX_TABLE_SIZE_P.  Returns true if an error occurs, false
5467
   otherwise.  */
5468
5469
static bool
5470
cortex_a8_erratum_scan (bfd *input_bfd,
5471
      struct bfd_link_info *info,
5472
      struct a8_erratum_fix **a8_fixes_p,
5473
      unsigned int *num_a8_fixes_p,
5474
      unsigned int *a8_fix_table_size_p,
5475
      struct a8_erratum_reloc *a8_relocs,
5476
      unsigned int num_a8_relocs,
5477
      unsigned prev_num_a8_fixes,
5478
      bool *stub_changed_p)
5479
0
{
5480
0
  asection *section;
5481
0
  struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
5482
0
  struct a8_erratum_fix *a8_fixes = *a8_fixes_p;
5483
0
  unsigned int num_a8_fixes = *num_a8_fixes_p;
5484
0
  unsigned int a8_fix_table_size = *a8_fix_table_size_p;
5485
5486
0
  if (htab == NULL)
5487
0
    return false;
5488
5489
0
  for (section = input_bfd->sections;
5490
0
       section != NULL;
5491
0
       section = section->next)
5492
0
    {
5493
0
      bfd_byte *contents = NULL;
5494
0
      struct _arm_elf_section_data *sec_data;
5495
0
      unsigned int span;
5496
0
      bfd_vma base_vma;
5497
5498
0
      if (elf_section_type (section) != SHT_PROGBITS
5499
0
    || (elf_section_flags (section) & SHF_EXECINSTR) == 0
5500
0
    || (section->flags & SEC_EXCLUDE) != 0
5501
0
    || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5502
0
    || (section->output_section == bfd_abs_section_ptr))
5503
0
  continue;
5504
5505
0
      base_vma = section->output_section->vma + section->output_offset;
5506
5507
0
      if (elf_section_data (section)->this_hdr.contents != NULL)
5508
0
  contents = elf_section_data (section)->this_hdr.contents;
5509
0
      else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
5510
0
  return true;
5511
5512
0
      sec_data = elf32_arm_section_data (section);
5513
5514
0
      for (span = 0; span < sec_data->mapcount; span++)
5515
0
  {
5516
0
    unsigned int span_start = sec_data->map[span].vma;
5517
0
    unsigned int span_end = (span == sec_data->mapcount - 1)
5518
0
      ? section->size : sec_data->map[span + 1].vma;
5519
0
    unsigned int i;
5520
0
    char span_type = sec_data->map[span].type;
5521
0
    bool last_was_32bit = false, last_was_branch = false;
5522
5523
0
    if (span_type != 't')
5524
0
      continue;
5525
5526
    /* Span is entirely within a single 4KB region: skip scanning.  */
5527
0
    if (((base_vma + span_start) & ~0xfff)
5528
0
        == ((base_vma + span_end) & ~0xfff))
5529
0
      continue;
5530
5531
    /* Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
5532
5533
         * The opcode is BLX.W, BL.W, B.W, Bcc.W
5534
         * The branch target is in the same 4KB region as the
5535
     first half of the branch.
5536
         * The instruction before the branch is a 32-bit
5537
     length non-branch instruction.  */
5538
0
    for (i = span_start; i < span_end;)
5539
0
      {
5540
0
        unsigned int insn = bfd_getl16 (&contents[i]);
5541
0
        bool insn_32bit = false, is_blx = false, is_b = false;
5542
0
        bool is_bl = false, is_bcc = false, is_32bit_branch;
5543
5544
0
        if ((insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000)
5545
0
    insn_32bit = true;
5546
5547
0
        if (insn_32bit)
5548
0
    {
5549
      /* Load the rest of the insn (in manual-friendly order).  */
5550
0
      insn = (insn << 16) | bfd_getl16 (&contents[i + 2]);
5551
5552
      /* Encoding T4: B<c>.W.  */
5553
0
      is_b = (insn & 0xf800d000) == 0xf0009000;
5554
      /* Encoding T1: BL<c>.W.  */
5555
0
      is_bl = (insn & 0xf800d000) == 0xf000d000;
5556
      /* Encoding T2: BLX<c>.W.  */
5557
0
      is_blx = (insn & 0xf800d000) == 0xf000c000;
5558
      /* Encoding T3: B<c>.W (not permitted in IT block).  */
5559
0
      is_bcc = (insn & 0xf800d000) == 0xf0008000
5560
0
         && (insn & 0x07f00000) != 0x03800000;
5561
0
    }
5562
5563
0
        is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
5564
5565
0
        if (((base_vma + i) & 0xfff) == 0xffe
5566
0
      && insn_32bit
5567
0
      && is_32bit_branch
5568
0
      && last_was_32bit
5569
0
      && ! last_was_branch)
5570
0
    {
5571
0
      bfd_signed_vma offset = 0;
5572
0
      bool force_target_arm = false;
5573
0
      bool force_target_thumb = false;
5574
0
      bfd_vma target;
5575
0
      enum elf32_arm_stub_type stub_type = arm_stub_none;
5576
0
      struct a8_erratum_reloc key, *found;
5577
0
      bool use_plt = false;
5578
5579
0
      key.from = base_vma + i;
5580
0
      found = (struct a8_erratum_reloc *)
5581
0
          bsearch (&key, a8_relocs, num_a8_relocs,
5582
0
             sizeof (struct a8_erratum_reloc),
5583
0
             &a8_reloc_compare);
5584
5585
0
      if (found)
5586
0
        {
5587
0
          char *error_message = NULL;
5588
0
          struct elf_link_hash_entry *entry;
5589
5590
          /* We don't care about the error returned from this
5591
       function, only if there is glue or not.  */
5592
0
          entry = find_thumb_glue (info, found->sym_name,
5593
0
                 &error_message);
5594
5595
0
          if (entry)
5596
0
      found->non_a8_stub = true;
5597
5598
          /* Keep a simpler condition, for the sake of clarity.  */
5599
0
          if (htab->root.splt != NULL && found->hash != NULL
5600
0
        && found->hash->root.plt.offset != (bfd_vma) -1)
5601
0
      use_plt = true;
5602
5603
0
          if (found->r_type == R_ARM_THM_CALL)
5604
0
      {
5605
0
        if (found->branch_type == ST_BRANCH_TO_ARM
5606
0
            || use_plt)
5607
0
          force_target_arm = true;
5608
0
        else
5609
0
          force_target_thumb = true;
5610
0
      }
5611
0
        }
5612
5613
      /* Check if we have an offending branch instruction.  */
5614
5615
0
      if (found && found->non_a8_stub)
5616
        /* We've already made a stub for this instruction, e.g.
5617
           it's a long branch or a Thumb->ARM stub.  Assume that
5618
           stub will suffice to work around the A8 erratum (see
5619
           setting of always_after_branch above).  */
5620
0
        ;
5621
0
      else if (is_bcc)
5622
0
        {
5623
0
          offset = (insn & 0x7ff) << 1;
5624
0
          offset |= (insn & 0x3f0000) >> 4;
5625
0
          offset |= (insn & 0x2000) ? 0x40000 : 0;
5626
0
          offset |= (insn & 0x800) ? 0x80000 : 0;
5627
0
          offset |= (insn & 0x4000000) ? 0x100000 : 0;
5628
0
          if (offset & 0x100000)
5629
0
      offset |= ~ ((bfd_signed_vma) 0xfffff);
5630
0
          stub_type = arm_stub_a8_veneer_b_cond;
5631
0
        }
5632
0
      else if (is_b || is_bl || is_blx)
5633
0
        {
5634
0
          int s = (insn & 0x4000000) != 0;
5635
0
          int j1 = (insn & 0x2000) != 0;
5636
0
          int j2 = (insn & 0x800) != 0;
5637
0
          int i1 = !(j1 ^ s);
5638
0
          int i2 = !(j2 ^ s);
5639
5640
0
          offset = (insn & 0x7ff) << 1;
5641
0
          offset |= (insn & 0x3ff0000) >> 4;
5642
0
          offset |= i2 << 22;
5643
0
          offset |= i1 << 23;
5644
0
          offset |= s << 24;
5645
0
          if (offset & 0x1000000)
5646
0
      offset |= ~ ((bfd_signed_vma) 0xffffff);
5647
5648
0
          if (is_blx)
5649
0
      offset &= ~ ((bfd_signed_vma) 3);
5650
5651
0
          stub_type = is_blx ? arm_stub_a8_veneer_blx :
5652
0
      is_bl ? arm_stub_a8_veneer_bl : arm_stub_a8_veneer_b;
5653
0
        }
5654
5655
0
      if (stub_type != arm_stub_none)
5656
0
        {
5657
0
          bfd_vma pc_for_insn = base_vma + i + 4;
5658
5659
          /* The original instruction is a BL, but the target is
5660
       an ARM instruction.  If we were not making a stub,
5661
       the BL would have been converted to a BLX.  Use the
5662
       BLX stub instead in that case.  */
5663
0
          if (htab->use_blx && force_target_arm
5664
0
        && stub_type == arm_stub_a8_veneer_bl)
5665
0
      {
5666
0
        stub_type = arm_stub_a8_veneer_blx;
5667
0
        is_blx = true;
5668
0
        is_bl = false;
5669
0
      }
5670
          /* Conversely, if the original instruction was
5671
       BLX but the target is Thumb mode, use the BL
5672
       stub.  */
5673
0
          else if (force_target_thumb
5674
0
             && stub_type == arm_stub_a8_veneer_blx)
5675
0
      {
5676
0
        stub_type = arm_stub_a8_veneer_bl;
5677
0
        is_blx = false;
5678
0
        is_bl = true;
5679
0
      }
5680
5681
0
          if (is_blx)
5682
0
      pc_for_insn &= ~ ((bfd_vma) 3);
5683
5684
          /* If we found a relocation, use the proper destination,
5685
       not the offset in the (unrelocated) instruction.
5686
       Note this is always done if we switched the stub type
5687
       above.  */
5688
0
          if (found)
5689
0
      offset =
5690
0
        (bfd_signed_vma) (found->destination - pc_for_insn);
5691
5692
          /* If the stub will use a Thumb-mode branch to a
5693
       PLT target, redirect it to the preceding Thumb
5694
       entry point.  */
5695
0
          if (stub_type != arm_stub_a8_veneer_blx && use_plt)
5696
0
      offset -= PLT_THUMB_STUB_SIZE;
5697
5698
0
          target = pc_for_insn + offset;
5699
5700
          /* The BLX stub is ARM-mode code.  Adjust the offset to
5701
       take the different PC value (+8 instead of +4) into
5702
       account.  */
5703
0
          if (stub_type == arm_stub_a8_veneer_blx)
5704
0
      offset += 4;
5705
5706
0
          if (((base_vma + i) & ~0xfff) == (target & ~0xfff))
5707
0
      {
5708
0
        char *stub_name = NULL;
5709
5710
0
        if (num_a8_fixes == a8_fix_table_size)
5711
0
          {
5712
0
            a8_fix_table_size *= 2;
5713
0
            a8_fixes = (struct a8_erratum_fix *)
5714
0
          bfd_realloc (a8_fixes,
5715
0
                 sizeof (struct a8_erratum_fix)
5716
0
                 * a8_fix_table_size);
5717
0
          }
5718
5719
0
        if (num_a8_fixes < prev_num_a8_fixes)
5720
0
          {
5721
            /* If we're doing a subsequent scan,
5722
         check if we've found the same fix as
5723
         before, and try and reuse the stub
5724
         name.  */
5725
0
            stub_name = a8_fixes[num_a8_fixes].stub_name;
5726
0
            if ((a8_fixes[num_a8_fixes].section != section)
5727
0
          || (a8_fixes[num_a8_fixes].offset != i))
5728
0
        {
5729
0
          free (stub_name);
5730
0
          stub_name = NULL;
5731
0
          *stub_changed_p = true;
5732
0
        }
5733
0
          }
5734
5735
0
        if (!stub_name)
5736
0
          {
5737
0
            stub_name = (char *) bfd_malloc (8 + 1 + 8 + 1);
5738
0
            if (stub_name != NULL)
5739
0
        sprintf (stub_name, "%x:%x", section->id, i);
5740
0
          }
5741
5742
0
        a8_fixes[num_a8_fixes].input_bfd = input_bfd;
5743
0
        a8_fixes[num_a8_fixes].section = section;
5744
0
        a8_fixes[num_a8_fixes].offset = i;
5745
0
        a8_fixes[num_a8_fixes].target_offset =
5746
0
          target - base_vma;
5747
0
        a8_fixes[num_a8_fixes].orig_insn = insn;
5748
0
        a8_fixes[num_a8_fixes].stub_name = stub_name;
5749
0
        a8_fixes[num_a8_fixes].stub_type = stub_type;
5750
0
        a8_fixes[num_a8_fixes].branch_type =
5751
0
          is_blx ? ST_BRANCH_TO_ARM : ST_BRANCH_TO_THUMB;
5752
5753
0
        num_a8_fixes++;
5754
0
      }
5755
0
        }
5756
0
    }
5757
5758
0
        i += insn_32bit ? 4 : 2;
5759
0
        last_was_32bit = insn_32bit;
5760
0
        last_was_branch = is_32bit_branch;
5761
0
      }
5762
0
  }
5763
5764
0
      if (elf_section_data (section)->this_hdr.contents == NULL)
5765
0
  free (contents);
5766
0
    }
5767
5768
0
  *a8_fixes_p = a8_fixes;
5769
0
  *num_a8_fixes_p = num_a8_fixes;
5770
0
  *a8_fix_table_size_p = a8_fix_table_size;
5771
5772
0
  return false;
5773
0
}
5774
5775
/* Create or update a stub entry depending on whether the stub can already be
5776
   found in HTAB.  The stub is identified by:
5777
   - its type STUB_TYPE
5778
   - its source branch (note that several can share the same stub) whose
5779
     section and relocation (if any) are given by SECTION and IRELA
5780
     respectively
5781
   - its target symbol whose input section, hash, name, value and branch type
5782
     are given in SYM_SEC, HASH, SYM_NAME, SYM_VALUE and BRANCH_TYPE
5783
     respectively
5784
5785
   If found, the value of the stub's target symbol is updated from SYM_VALUE
5786
   and *NEW_STUB is set to FALSE.  Otherwise, *NEW_STUB is set to
5787
   TRUE and the stub entry is initialized.
5788
5789
   Returns the stub that was created or updated, or NULL if an error
5790
   occurred.  */
5791
5792
static struct elf32_arm_stub_hash_entry *
5793
elf32_arm_create_stub (struct elf32_arm_link_hash_table *htab,
5794
           enum elf32_arm_stub_type stub_type, asection *section,
5795
           Elf_Internal_Rela *irela, asection *sym_sec,
5796
           struct elf32_arm_link_hash_entry *hash, char *sym_name,
5797
           bfd_vma sym_value, enum arm_st_branch_type branch_type,
5798
           bool *new_stub)
5799
0
{
5800
0
  const asection *id_sec;
5801
0
  char *stub_name;
5802
0
  struct elf32_arm_stub_hash_entry *stub_entry;
5803
0
  unsigned int r_type;
5804
0
  bool sym_claimed = arm_stub_sym_claimed (stub_type);
5805
5806
0
  BFD_ASSERT (stub_type != arm_stub_none);
5807
0
  *new_stub = false;
5808
5809
0
  if (sym_claimed)
5810
0
    stub_name = sym_name;
5811
0
  else
5812
0
    {
5813
0
      BFD_ASSERT (irela);
5814
0
      BFD_ASSERT (section);
5815
0
      BFD_ASSERT (section->id <= htab->top_id);
5816
5817
      /* Support for grouping stub sections.  */
5818
0
      id_sec = htab->stub_group[section->id].link_sec;
5819
5820
      /* Get the name of this stub.  */
5821
0
      stub_name = elf32_arm_stub_name (id_sec, sym_sec, hash, irela,
5822
0
               stub_type);
5823
0
      if (!stub_name)
5824
0
  return NULL;
5825
0
    }
5826
5827
0
  stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name, false,
5828
0
             false);
5829
  /* The proper stub has already been created, just update its value.  */
5830
0
  if (stub_entry != NULL)
5831
0
    {
5832
0
      if (!sym_claimed)
5833
0
  free (stub_name);
5834
0
      stub_entry->target_value = sym_value;
5835
0
      return stub_entry;
5836
0
    }
5837
5838
0
  stub_entry = elf32_arm_add_stub (stub_name, section, htab, stub_type);
5839
0
  if (stub_entry == NULL)
5840
0
    {
5841
0
      if (!sym_claimed)
5842
0
  free (stub_name);
5843
0
      return NULL;
5844
0
    }
5845
5846
0
  stub_entry->target_value = sym_value;
5847
0
  stub_entry->target_section = sym_sec;
5848
0
  stub_entry->stub_type = stub_type;
5849
0
  stub_entry->h = hash;
5850
0
  stub_entry->branch_type = branch_type;
5851
5852
0
  if (sym_claimed)
5853
0
    stub_entry->output_name = sym_name;
5854
0
  else
5855
0
    {
5856
0
      if (sym_name == NULL)
5857
0
  sym_name = "unnamed";
5858
0
      stub_entry->output_name = (char *)
5859
0
  bfd_alloc (htab->stub_bfd, sizeof (THUMB2ARM_GLUE_ENTRY_NAME)
5860
0
           + strlen (sym_name));
5861
0
      if (stub_entry->output_name == NULL)
5862
0
  {
5863
0
    free (stub_name);
5864
0
    return NULL;
5865
0
  }
5866
5867
      /* For historical reasons, use the existing names for ARM-to-Thumb and
5868
   Thumb-to-ARM stubs.  */
5869
0
      r_type = ELF32_R_TYPE (irela->r_info);
5870
0
      if ((r_type == (unsigned int) R_ARM_THM_CALL
5871
0
     || r_type == (unsigned int) R_ARM_THM_JUMP24
5872
0
     || r_type == (unsigned int) R_ARM_THM_JUMP19)
5873
0
    && branch_type == ST_BRANCH_TO_ARM)
5874
0
  sprintf (stub_entry->output_name, THUMB2ARM_GLUE_ENTRY_NAME, sym_name);
5875
0
      else if ((r_type == (unsigned int) R_ARM_CALL
5876
0
    || r_type == (unsigned int) R_ARM_JUMP24)
5877
0
         && branch_type == ST_BRANCH_TO_THUMB)
5878
0
  sprintf (stub_entry->output_name, ARM2THUMB_GLUE_ENTRY_NAME, sym_name);
5879
0
      else
5880
0
  sprintf (stub_entry->output_name, STUB_ENTRY_NAME, sym_name);
5881
0
    }
5882
5883
0
  *new_stub = true;
5884
0
  return stub_entry;
5885
0
}
5886
5887
/* Scan symbols in INPUT_BFD to identify secure entry functions needing a
5888
   gateway veneer to transition from non secure to secure state and create them
5889
   accordingly.
5890
5891
   "ARMv8-M Security Extensions: Requirements on Development Tools" document
5892
   defines the conditions that govern Secure Gateway veneer creation for a
5893
   given symbol <SYM> as follows:
5894
   - it has function type
5895
   - it has non local binding
5896
   - a symbol named __acle_se_<SYM> (called special symbol) exists with the
5897
     same type, binding and value as <SYM> (called normal symbol).
5898
   An entry function can handle secure state transition itself in which case
5899
   its special symbol would have a different value from the normal symbol.
5900
5901
   OUT_ATTR gives the output attributes, SYM_HASHES the symbol index to hash
5902
   entry mapping while HTAB gives the name to hash entry mapping.
5903
   *CMSE_STUB_CREATED is increased by the number of secure gateway veneer
5904
   created.
5905
5906
   The return value gives whether a stub failed to be allocated.  */
5907
5908
static bool
5909
cmse_scan (bfd *input_bfd, struct elf32_arm_link_hash_table *htab,
5910
     obj_attribute *out_attr, struct elf_link_hash_entry **sym_hashes,
5911
     int *cmse_stub_created)
5912
0
{
5913
0
  elf_backend_data *bed = get_elf_backend_data (input_bfd);
5914
0
  Elf_Internal_Shdr *symtab_hdr;
5915
0
  unsigned i, j, sym_count, ext_start;
5916
0
  Elf_Internal_Sym *cmse_sym, *local_syms;
5917
0
  struct elf32_arm_link_hash_entry *hash, *cmse_hash = NULL;
5918
0
  enum arm_st_branch_type branch_type;
5919
0
  char *sym_name, *lsym_name;
5920
0
  bfd_vma sym_value;
5921
0
  asection *section;
5922
0
  struct elf32_arm_stub_hash_entry *stub_entry;
5923
0
  bool is_v8m, new_stub, cmse_invalid, ret = true;
5924
5925
0
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5926
0
  sym_count = symtab_hdr->sh_size / bed->s->sizeof_sym;
5927
0
  ext_start = symtab_hdr->sh_info;
5928
0
  is_v8m = (out_attr[Tag_CPU_arch].i >= TAG_CPU_ARCH_V8M_BASE
5929
0
      && out_attr[Tag_CPU_arch_profile].i == 'M');
5930
5931
0
  local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
5932
0
  if (local_syms == NULL)
5933
0
    local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5934
0
               symtab_hdr->sh_info, 0, NULL, NULL,
5935
0
               NULL);
5936
0
  if (symtab_hdr->sh_info && local_syms == NULL)
5937
0
    return false;
5938
5939
  /* Scan symbols.  */
5940
0
  for (i = 0; i < sym_count; i++)
5941
0
    {
5942
0
      cmse_invalid = false;
5943
5944
0
      if (i < ext_start)
5945
0
  {
5946
0
    cmse_sym = &local_syms[i];
5947
0
    sym_name = bfd_elf_string_from_elf_section (input_bfd,
5948
0
                  symtab_hdr->sh_link,
5949
0
                  cmse_sym->st_name);
5950
0
    if (!sym_name || !startswith (sym_name, CMSE_PREFIX))
5951
0
      continue;
5952
5953
    /* Special symbol with local binding.  */
5954
0
    cmse_invalid = true;
5955
0
  }
5956
0
      else
5957
0
  {
5958
0
    cmse_hash = elf32_arm_hash_entry (sym_hashes[i - ext_start]);
5959
0
    if (cmse_hash == NULL)
5960
0
      continue;
5961
5962
0
    sym_name = (char *) cmse_hash->root.root.root.string;
5963
0
    if (!startswith (sym_name, CMSE_PREFIX))
5964
0
      continue;
5965
5966
    /* Special symbol has incorrect binding or type.  */
5967
0
    if ((cmse_hash->root.root.type != bfd_link_hash_defined
5968
0
         && cmse_hash->root.root.type != bfd_link_hash_defweak)
5969
0
        || cmse_hash->root.type != STT_FUNC)
5970
0
      cmse_invalid = true;
5971
0
  }
5972
5973
0
      if (!is_v8m)
5974
0
  {
5975
0
    _bfd_error_handler (_("%pB: special symbol `%s' only allowed for "
5976
0
        "ARMv8-M architecture or later"),
5977
0
            input_bfd, sym_name);
5978
0
    is_v8m = true; /* Avoid multiple warning.  */
5979
0
    ret = false;
5980
0
  }
5981
5982
0
      if (cmse_invalid)
5983
0
  {
5984
0
    _bfd_error_handler (_("%pB: invalid special symbol `%s'; it must be"
5985
0
        " a global or weak function symbol"),
5986
0
            input_bfd, sym_name);
5987
0
    ret = false;
5988
0
    if (i < ext_start)
5989
0
      continue;
5990
0
  }
5991
5992
0
      sym_name += strlen (CMSE_PREFIX);
5993
0
      hash = (struct elf32_arm_link_hash_entry *)
5994
0
  elf_link_hash_lookup (&(htab)->root, sym_name, false, false, true);
5995
5996
      /* No associated normal symbol or it is neither global nor weak.  */
5997
0
      if (!hash
5998
0
    || (hash->root.root.type != bfd_link_hash_defined
5999
0
        && hash->root.root.type != bfd_link_hash_defweak)
6000
0
    || hash->root.type != STT_FUNC)
6001
0
  {
6002
    /* Initialize here to avoid warning about use of possibly
6003
       uninitialized variable.  */
6004
0
    j = 0;
6005
6006
0
    if (!hash)
6007
0
      {
6008
        /* Searching for a normal symbol with local binding.  */
6009
0
        for (; j < ext_start; j++)
6010
0
    {
6011
0
      lsym_name =
6012
0
        bfd_elf_string_from_elf_section (input_bfd,
6013
0
                 symtab_hdr->sh_link,
6014
0
                 local_syms[j].st_name);
6015
0
      if (!strcmp (sym_name, lsym_name))
6016
0
        break;
6017
0
    }
6018
0
      }
6019
6020
0
    if (hash || j < ext_start)
6021
0
      {
6022
0
        _bfd_error_handler
6023
0
    (_("%pB: invalid standard symbol `%s'; it must be "
6024
0
       "a global or weak function symbol"),
6025
0
     input_bfd, sym_name);
6026
0
      }
6027
0
    else
6028
0
      _bfd_error_handler
6029
0
        (_("%pB: absent standard symbol `%s'"), input_bfd, sym_name);
6030
0
    ret = false;
6031
0
    if (!hash)
6032
0
      continue;
6033
0
  }
6034
6035
0
      sym_value = hash->root.root.u.def.value;
6036
0
      section = hash->root.root.u.def.section;
6037
6038
0
      if (cmse_hash->root.root.u.def.section != section)
6039
0
  {
6040
0
    _bfd_error_handler
6041
0
      (_("%pB: `%s' and its special symbol are in different sections"),
6042
0
       input_bfd, sym_name);
6043
0
    ret = false;
6044
0
  }
6045
0
      if (cmse_hash->root.root.u.def.value != sym_value)
6046
0
  continue; /* Ignore: could be an entry function starting with SG.  */
6047
6048
  /* If this section is a link-once section that will be discarded, then
6049
     don't create any stubs.  */
6050
0
      if (section->output_section == NULL)
6051
0
  {
6052
0
    _bfd_error_handler
6053
0
      (_("%pB: entry function `%s' not output"), input_bfd, sym_name);
6054
0
    continue;
6055
0
  }
6056
6057
0
      if (hash->root.size == 0)
6058
0
  {
6059
0
    _bfd_error_handler
6060
0
      (_("%pB: entry function `%s' is empty"), input_bfd, sym_name);
6061
0
    ret = false;
6062
0
  }
6063
6064
0
      if (!ret)
6065
0
  continue;
6066
0
      branch_type = ARM_GET_SYM_BRANCH_TYPE (hash->root.target_internal);
6067
0
      stub_entry
6068
0
  = elf32_arm_create_stub (htab, arm_stub_cmse_branch_thumb_only,
6069
0
         NULL, NULL, section, hash, sym_name,
6070
0
         sym_value, branch_type, &new_stub);
6071
6072
0
      if (stub_entry == NULL)
6073
0
   ret = false;
6074
0
      else
6075
0
  {
6076
0
    BFD_ASSERT (new_stub);
6077
0
    (*cmse_stub_created)++;
6078
0
  }
6079
0
    }
6080
6081
0
  if (!symtab_hdr->contents)
6082
0
    free (local_syms);
6083
0
  return ret;
6084
0
}
6085
6086
/* Return TRUE iff a symbol identified by its linker HASH entry is a secure
6087
   code entry function, ie can be called from non secure code without using a
6088
   veneer.  */
6089
6090
static bool
6091
cmse_entry_fct_p (struct elf32_arm_link_hash_entry *hash)
6092
0
{
6093
0
  bfd_byte contents[4];
6094
0
  uint32_t first_insn;
6095
0
  asection *section;
6096
0
  file_ptr offset;
6097
0
  bfd *abfd;
6098
6099
  /* Defined symbol of function type.  */
6100
0
  if (hash->root.root.type != bfd_link_hash_defined
6101
0
      && hash->root.root.type != bfd_link_hash_defweak)
6102
0
    return false;
6103
0
  if (hash->root.type != STT_FUNC)
6104
0
    return false;
6105
6106
  /* Read first instruction.  */
6107
0
  section = hash->root.root.u.def.section;
6108
0
  abfd = section->owner;
6109
0
  offset = hash->root.root.u.def.value - section->vma;
6110
0
  if (!bfd_get_section_contents (abfd, section, contents, offset,
6111
0
         sizeof (contents)))
6112
0
    return false;
6113
6114
0
  first_insn = bfd_get_32 (abfd, contents);
6115
6116
  /* Starts by SG instruction.  */
6117
0
  return first_insn == 0xe97fe97f;
6118
0
}
6119
6120
/* Output the name (in symbol table) of the veneer GEN_ENTRY if it is a new
6121
   secure gateway veneers (ie. the veneers was not in the input import library)
6122
   and there is no output import library (GEN_INFO->out_implib_bfd is NULL.  */
6123
6124
static bool
6125
arm_list_new_cmse_stub (struct bfd_hash_entry *gen_entry, void *gen_info)
6126
0
{
6127
0
  struct elf32_arm_stub_hash_entry *stub_entry;
6128
0
  struct bfd_link_info *info;
6129
6130
  /* Massage our args to the form they really have.  */
6131
0
  stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
6132
0
  info = (struct bfd_link_info *) gen_info;
6133
6134
0
  if (info->out_implib_bfd)
6135
0
    return true;
6136
6137
0
  if (stub_entry->stub_type != arm_stub_cmse_branch_thumb_only)
6138
0
    return true;
6139
6140
0
  if (stub_entry->stub_offset == (bfd_vma) -1)
6141
0
    _bfd_error_handler ("  %s", stub_entry->output_name);
6142
6143
0
  return true;
6144
0
}
6145
6146
/* Set offset of each secure gateway veneers so that its address remain
6147
   identical to the one in the input import library referred by
6148
   HTAB->in_implib_bfd.  A warning is issued for veneers that disappeared
6149
   (present in input import library but absent from the executable being
6150
   linked) or if new veneers appeared and there is no output import library
6151
   (INFO->out_implib_bfd is NULL and *CMSE_STUB_CREATED is bigger than the
6152
   number of secure gateway veneers found in the input import library.
6153
6154
   The function returns whether an error occurred.  If no error occurred,
6155
   *CMSE_STUB_CREATED gives the number of SG veneers created by both cmse_scan
6156
   and this function and HTAB->new_cmse_stub_offset is set to the biggest
6157
   veneer observed set for new veneers to be layed out after.  */
6158
6159
static bool
6160
set_cmse_veneer_addr_from_implib (struct bfd_link_info *info,
6161
          struct elf32_arm_link_hash_table *htab,
6162
          int *cmse_stub_created)
6163
0
{
6164
0
  long symsize;
6165
0
  char *sym_name;
6166
0
  flagword flags;
6167
0
  long i, symcount;
6168
0
  bfd *in_implib_bfd;
6169
0
  asection *stub_out_sec;
6170
0
  bool ret = true;
6171
0
  Elf_Internal_Sym *intsym;
6172
0
  const char *out_sec_name;
6173
0
  bfd_size_type cmse_stub_size;
6174
0
  asymbol **sympp = NULL, *sym;
6175
0
  struct elf32_arm_link_hash_entry *hash;
6176
0
  const insn_sequence *cmse_stub_template;
6177
0
  struct elf32_arm_stub_hash_entry *stub_entry;
6178
0
  int cmse_stub_template_size, new_cmse_stubs_created = *cmse_stub_created;
6179
0
  bfd_vma veneer_value, stub_offset, next_cmse_stub_offset;
6180
0
  bfd_vma cmse_stub_array_start = (bfd_vma) -1, cmse_stub_sec_vma = 0;
6181
6182
  /* No input secure gateway import library.  */
6183
0
  if (!htab->in_implib_bfd)
6184
0
    return true;
6185
6186
0
  in_implib_bfd = htab->in_implib_bfd;
6187
0
  if (!htab->cmse_implib)
6188
0
    {
6189
0
      _bfd_error_handler (_("%pB: --in-implib only supported for Secure "
6190
0
          "Gateway import libraries"), in_implib_bfd);
6191
0
      return false;
6192
0
    }
6193
6194
  /* Get symbol table size.  */
6195
0
  symsize = bfd_get_symtab_upper_bound (in_implib_bfd);
6196
0
  if (symsize < 0)
6197
0
    return false;
6198
6199
  /* Read in the input secure gateway import library's symbol table.  */
6200
0
  sympp = (asymbol **) bfd_malloc (symsize);
6201
0
  if (sympp == NULL)
6202
0
    return false;
6203
6204
0
  symcount = bfd_canonicalize_symtab (in_implib_bfd, sympp);
6205
0
  if (symcount < 0)
6206
0
    {
6207
0
      ret = false;
6208
0
      goto free_sym_buf;
6209
0
    }
6210
6211
0
  htab->new_cmse_stub_offset = 0;
6212
0
  cmse_stub_size =
6213
0
    find_stub_size_and_template (arm_stub_cmse_branch_thumb_only,
6214
0
         &cmse_stub_template,
6215
0
         &cmse_stub_template_size);
6216
0
  out_sec_name =
6217
0
    arm_dedicated_stub_output_section_name (arm_stub_cmse_branch_thumb_only);
6218
0
  stub_out_sec =
6219
0
    bfd_get_section_by_name (htab->obfd, out_sec_name);
6220
0
  if (stub_out_sec != NULL)
6221
0
    cmse_stub_sec_vma = stub_out_sec->vma;
6222
6223
  /* Set addresses of veneers mentionned in input secure gateway import
6224
     library's symbol table.  */
6225
0
  for (i = 0; i < symcount; i++)
6226
0
    {
6227
0
      sym = sympp[i];
6228
0
      flags = sym->flags;
6229
0
      sym_name = (char *) bfd_asymbol_name (sym);
6230
0
      intsym = &((elf_symbol_type *) sym)->internal_elf_sym;
6231
6232
0
      if (sym->section != bfd_abs_section_ptr
6233
0
    || !(flags & (BSF_GLOBAL | BSF_WEAK))
6234
0
    || (flags & BSF_FUNCTION) != BSF_FUNCTION
6235
0
    || (ARM_GET_SYM_BRANCH_TYPE (intsym->st_target_internal)
6236
0
        != ST_BRANCH_TO_THUMB))
6237
0
  {
6238
0
    _bfd_error_handler (_("%pB: invalid import library entry: `%s'; "
6239
0
        "symbol should be absolute, global and "
6240
0
        "refer to Thumb functions"),
6241
0
            in_implib_bfd, sym_name);
6242
0
    ret = false;
6243
0
    continue;
6244
0
  }
6245
6246
0
      veneer_value = bfd_asymbol_value (sym);
6247
0
      stub_offset = veneer_value - cmse_stub_sec_vma;
6248
0
      stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, sym_name,
6249
0
           false, false);
6250
0
      hash = (struct elf32_arm_link_hash_entry *)
6251
0
  elf_link_hash_lookup (&(htab)->root, sym_name, false, false, true);
6252
6253
      /* Stub entry should have been created by cmse_scan or the symbol be of
6254
   a secure function callable from non secure code.  */
6255
0
      if (!stub_entry && !hash)
6256
0
  {
6257
0
    bool new_stub;
6258
6259
0
    _bfd_error_handler
6260
0
      (_("entry function `%s' disappeared from secure code"), sym_name);
6261
0
    hash = (struct elf32_arm_link_hash_entry *)
6262
0
      elf_link_hash_lookup (&(htab)->root, sym_name, true, true, true);
6263
0
    stub_entry
6264
0
      = elf32_arm_create_stub (htab, arm_stub_cmse_branch_thumb_only,
6265
0
             NULL, NULL, bfd_abs_section_ptr, hash,
6266
0
             sym_name, veneer_value,
6267
0
             ST_BRANCH_TO_THUMB, &new_stub);
6268
0
    if (stub_entry == NULL)
6269
0
      ret = false;
6270
0
    else
6271
0
    {
6272
0
      BFD_ASSERT (new_stub);
6273
0
      new_cmse_stubs_created++;
6274
0
      (*cmse_stub_created)++;
6275
0
    }
6276
0
    stub_entry->stub_template_size = stub_entry->stub_size = 0;
6277
0
    stub_entry->stub_offset = stub_offset;
6278
0
  }
6279
      /* Symbol found is not callable from non secure code.  */
6280
0
      else if (!stub_entry)
6281
0
  {
6282
0
    if (!cmse_entry_fct_p (hash))
6283
0
      {
6284
0
        _bfd_error_handler (_("`%s' refers to a non entry function"),
6285
0
          sym_name);
6286
0
        ret = false;
6287
0
      }
6288
0
    continue;
6289
0
  }
6290
0
      else
6291
0
  {
6292
    /* Only stubs for SG veneers should have been created.  */
6293
0
    BFD_ASSERT (stub_entry->stub_type == arm_stub_cmse_branch_thumb_only);
6294
6295
    /* Check visibility hasn't changed.  */
6296
0
    if (!!(flags & BSF_GLOBAL)
6297
0
        != (hash->root.root.type == bfd_link_hash_defined))
6298
0
      _bfd_error_handler
6299
0
        (_("%pB: visibility of symbol `%s' has changed"), in_implib_bfd,
6300
0
         sym_name);
6301
6302
0
    stub_entry->stub_offset = stub_offset;
6303
0
  }
6304
6305
      /* Size should match that of a SG veneer.  */
6306
0
      if (intsym->st_size != cmse_stub_size)
6307
0
  {
6308
0
    _bfd_error_handler (_("%pB: incorrect size for symbol `%s'"),
6309
0
            in_implib_bfd, sym_name);
6310
0
    ret = false;
6311
0
  }
6312
6313
      /* Previous veneer address is before current SG veneer section.  */
6314
0
      if (veneer_value < cmse_stub_sec_vma)
6315
0
  {
6316
    /* Avoid offset underflow.  */
6317
0
    if (stub_entry)
6318
0
      stub_entry->stub_offset = 0;
6319
0
    stub_offset = 0;
6320
0
    ret = false;
6321
0
  }
6322
6323
      /* Complain if stub offset not a multiple of stub size.  */
6324
0
      if (stub_offset % cmse_stub_size)
6325
0
  {
6326
0
    _bfd_error_handler
6327
0
      (_("offset of veneer for entry function `%s' not a multiple of "
6328
0
         "its size"), sym_name);
6329
0
    ret = false;
6330
0
  }
6331
6332
0
      if (!ret)
6333
0
  continue;
6334
6335
0
      new_cmse_stubs_created--;
6336
0
      if (veneer_value < cmse_stub_array_start)
6337
0
  cmse_stub_array_start = veneer_value;
6338
0
      next_cmse_stub_offset = stub_offset + ((cmse_stub_size + 7) & ~7);
6339
0
      if (next_cmse_stub_offset > htab->new_cmse_stub_offset)
6340
0
  htab->new_cmse_stub_offset = next_cmse_stub_offset;
6341
0
    }
6342
6343
0
  if (!info->out_implib_bfd && new_cmse_stubs_created != 0)
6344
0
    {
6345
0
      BFD_ASSERT (new_cmse_stubs_created > 0);
6346
0
      _bfd_error_handler
6347
0
  (_("new entry function(s) introduced but no output import library "
6348
0
     "specified:"));
6349
0
      bfd_hash_traverse (&htab->stub_hash_table, arm_list_new_cmse_stub, info);
6350
0
    }
6351
6352
0
  if (cmse_stub_array_start != cmse_stub_sec_vma)
6353
0
    {
6354
0
      _bfd_error_handler
6355
0
  (_("start address of `%s' is different from previous link"),
6356
0
   out_sec_name);
6357
0
      ret = false;
6358
0
    }
6359
6360
0
 free_sym_buf:
6361
0
  free (sympp);
6362
0
  return ret;
6363
0
}
6364
6365
/* Determine and set the size of the stub section for a final link.
6366
6367
   The basic idea here is to examine all the relocations looking for
6368
   PC-relative calls to a target that is unreachable with a "bl"
6369
   instruction.  */
6370
6371
bool
6372
elf32_arm_size_stubs (bfd *output_bfd,
6373
          bfd *stub_bfd,
6374
          struct bfd_link_info *info,
6375
          bfd_signed_vma group_size,
6376
          asection * (*add_stub_section) (const char *, asection *,
6377
                  asection *,
6378
                  unsigned int),
6379
          void (*layout_sections_again) (void))
6380
0
{
6381
0
  bool ret = true;
6382
0
  obj_attribute *out_attr;
6383
0
  int cmse_stub_created = 0;
6384
0
  bfd_size_type stub_group_size;
6385
0
  bool m_profile, stubs_always_after_branch, first_veneer_scan = true;
6386
0
  struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
6387
0
  struct a8_erratum_fix *a8_fixes = NULL;
6388
0
  unsigned int num_a8_fixes = 0, a8_fix_table_size = 10;
6389
0
  struct a8_erratum_reloc *a8_relocs = NULL;
6390
0
  unsigned int num_a8_relocs = 0, a8_reloc_table_size = 10, i;
6391
6392
0
  if (htab == NULL)
6393
0
    return false;
6394
6395
0
  if (htab->fix_cortex_a8)
6396
0
    {
6397
0
      a8_fixes = (struct a8_erratum_fix *)
6398
0
    bfd_zmalloc (sizeof (struct a8_erratum_fix) * a8_fix_table_size);
6399
0
      a8_relocs = (struct a8_erratum_reloc *)
6400
0
    bfd_zmalloc (sizeof (struct a8_erratum_reloc) * a8_reloc_table_size);
6401
0
    }
6402
6403
  /* Propagate mach to stub bfd, because it may not have been
6404
     finalized when we created stub_bfd.  */
6405
0
  bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
6406
0
         bfd_get_mach (output_bfd));
6407
6408
  /* Stash our params away.  */
6409
0
  htab->stub_bfd = stub_bfd;
6410
0
  htab->add_stub_section = add_stub_section;
6411
0
  htab->layout_sections_again = layout_sections_again;
6412
0
  stubs_always_after_branch = group_size < 0;
6413
6414
0
  out_attr = elf_known_obj_attributes_proc (output_bfd);
6415
0
  m_profile = out_attr[Tag_CPU_arch_profile].i == 'M';
6416
6417
  /* The Cortex-A8 erratum fix depends on stubs not being in the same 4K page
6418
     as the first half of a 32-bit branch straddling two 4K pages.  This is a
6419
     crude way of enforcing that.  */
6420
0
  if (htab->fix_cortex_a8)
6421
0
    stubs_always_after_branch = 1;
6422
6423
0
  if (group_size < 0)
6424
0
    stub_group_size = -group_size;
6425
0
  else
6426
0
    stub_group_size = group_size;
6427
6428
0
  if (stub_group_size == 1)
6429
0
    {
6430
      /* Default values.  */
6431
      /* Thumb branch range is +-4MB has to be used as the default
6432
   maximum size (a given section can contain both ARM and Thumb
6433
   code, so the worst case has to be taken into account).
6434
6435
   This value is 24K less than that, which allows for 2025
6436
   12-byte stubs.  If we exceed that, then we will fail to link.
6437
   The user will have to relink with an explicit group size
6438
   option.  */
6439
0
      stub_group_size = 4170000;
6440
0
    }
6441
6442
0
  group_sections (htab, stub_group_size, stubs_always_after_branch);
6443
6444
  /* If we're applying the cortex A8 fix, we need to determine the
6445
     program header size now, because we cannot change it later --
6446
     that could alter section placements.  Notice the A8 erratum fix
6447
     ends up requiring the section addresses to remain unchanged
6448
     modulo the page size.  That's something we cannot represent
6449
     inside BFD, and we don't want to force the section alignment to
6450
     be the page size.  */
6451
0
  if (htab->fix_cortex_a8)
6452
0
    (*htab->layout_sections_again) ();
6453
6454
0
  while (1)
6455
0
    {
6456
0
      bfd *input_bfd;
6457
0
      asection *stub_sec;
6458
0
      enum elf32_arm_stub_type stub_type;
6459
0
      bool stub_changed = false;
6460
0
      unsigned prev_num_a8_fixes = num_a8_fixes;
6461
6462
0
      num_a8_fixes = 0;
6463
0
      for (input_bfd = info->input_bfds;
6464
0
     input_bfd != NULL;
6465
0
     input_bfd = input_bfd->link.next)
6466
0
  {
6467
0
    Elf_Internal_Shdr *symtab_hdr;
6468
0
    asection *section;
6469
0
    Elf_Internal_Sym *local_syms = NULL;
6470
6471
0
    if (!is_arm_elf (input_bfd))
6472
0
      continue;
6473
0
    if ((input_bfd->flags & DYNAMIC) != 0
6474
0
        && (elf_sym_hashes (input_bfd) == NULL
6475
0
      || (elf_dyn_lib_class (input_bfd) & DYN_AS_NEEDED) != 0))
6476
0
      continue;
6477
6478
0
    num_a8_relocs = 0;
6479
6480
    /* We'll need the symbol table in a second.  */
6481
0
    symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6482
0
    if (symtab_hdr->sh_info == 0)
6483
0
      continue;
6484
6485
    /* Limit scan of symbols to object file whose profile is
6486
       Microcontroller to not hinder performance in the general case.  */
6487
0
    if (m_profile && first_veneer_scan)
6488
0
      {
6489
0
        struct elf_link_hash_entry **sym_hashes;
6490
6491
0
        sym_hashes = elf_sym_hashes (input_bfd);
6492
0
        if (!cmse_scan (input_bfd, htab, out_attr, sym_hashes,
6493
0
            &cmse_stub_created))
6494
0
    goto error_ret_free_local;
6495
6496
0
        if (cmse_stub_created != 0)
6497
0
    stub_changed = true;
6498
0
      }
6499
6500
    /* Walk over each section attached to the input bfd.  */
6501
0
    for (section = input_bfd->sections;
6502
0
         section != NULL;
6503
0
         section = section->next)
6504
0
      {
6505
0
        Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
6506
6507
        /* If there aren't any relocs, then there's nothing more
6508
     to do.  */
6509
0
        if ((section->flags & SEC_RELOC) == 0
6510
0
      || section->reloc_count == 0
6511
0
      || (section->flags & SEC_CODE) == 0)
6512
0
    continue;
6513
6514
        /* If this section is a link-once section that will be
6515
     discarded, then don't create any stubs.  */
6516
0
        if (section->output_section == NULL
6517
0
      || section->output_section->owner != output_bfd)
6518
0
    continue;
6519
6520
        /* Get the relocs.  */
6521
0
        internal_relocs
6522
0
    = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
6523
0
               NULL, info->keep_memory);
6524
0
        if (internal_relocs == NULL)
6525
0
    goto error_ret_free_local;
6526
6527
        /* Now examine each relocation.  */
6528
0
        irela = internal_relocs;
6529
0
        irelaend = irela + section->reloc_count;
6530
0
        for (; irela < irelaend; irela++)
6531
0
    {
6532
0
      unsigned int r_type, r_indx;
6533
0
      asection *sym_sec;
6534
0
      bfd_vma sym_value;
6535
0
      bfd_vma destination;
6536
0
      struct elf32_arm_link_hash_entry *hash;
6537
0
      const char *sym_name;
6538
0
      unsigned char st_type;
6539
0
      enum arm_st_branch_type branch_type;
6540
0
      bool created_stub = false;
6541
6542
0
      r_type = ELF32_R_TYPE (irela->r_info);
6543
0
      r_indx = ELF32_R_SYM (irela->r_info);
6544
6545
0
      if (r_type >= (unsigned int) R_ARM_max)
6546
0
        {
6547
0
          bfd_set_error (bfd_error_bad_value);
6548
0
        error_ret_free_internal:
6549
0
          if (elf_section_data (section)->relocs == NULL)
6550
0
      free (internal_relocs);
6551
        /* Fall through.  */
6552
0
        error_ret_free_local:
6553
0
          if (symtab_hdr->contents != (unsigned char *) local_syms)
6554
0
      free (local_syms);
6555
0
          return false;
6556
0
        }
6557
6558
0
      hash = NULL;
6559
0
      if (r_indx >= symtab_hdr->sh_info)
6560
0
        hash = elf32_arm_hash_entry
6561
0
          (elf_sym_hashes (input_bfd)
6562
0
           [r_indx - symtab_hdr->sh_info]);
6563
6564
      /* Only look for stubs on branch instructions, or
6565
         non-relaxed TLSCALL  */
6566
0
      if ((r_type != (unsigned int) R_ARM_CALL)
6567
0
          && (r_type != (unsigned int) R_ARM_THM_CALL)
6568
0
          && (r_type != (unsigned int) R_ARM_JUMP24)
6569
0
          && (r_type != (unsigned int) R_ARM_THM_JUMP19)
6570
0
          && (r_type != (unsigned int) R_ARM_THM_XPC22)
6571
0
          && (r_type != (unsigned int) R_ARM_THM_JUMP24)
6572
0
          && (r_type != (unsigned int) R_ARM_PLT32)
6573
0
          && !((r_type == (unsigned int) R_ARM_TLS_CALL
6574
0
          || r_type == (unsigned int) R_ARM_THM_TLS_CALL)
6575
0
         && r_type == (elf32_arm_tls_transition
6576
0
           (info, r_type,
6577
0
            (struct elf_link_hash_entry *) hash))
6578
0
         && ((hash ? hash->tls_type
6579
0
        : (elf32_arm_local_got_tls_type
6580
0
           (input_bfd)[r_indx]))
6581
0
             & GOT_TLS_GDESC) != 0))
6582
0
        continue;
6583
6584
      /* Now determine the call target, its name, value,
6585
         section.  */
6586
0
      sym_sec = NULL;
6587
0
      sym_value = 0;
6588
0
      destination = 0;
6589
0
      sym_name = NULL;
6590
6591
0
      if (r_type == (unsigned int) R_ARM_TLS_CALL
6592
0
          || r_type == (unsigned int) R_ARM_THM_TLS_CALL)
6593
0
        {
6594
          /* A non-relaxed TLS call.  The target is the
6595
       plt-resident trampoline and nothing to do
6596
       with the symbol.  */
6597
0
          BFD_ASSERT (htab->tls_trampoline > 0);
6598
0
          sym_sec = htab->root.splt;
6599
0
          sym_value = htab->tls_trampoline;
6600
0
          hash = 0;
6601
0
          st_type = STT_FUNC;
6602
0
          branch_type = ST_BRANCH_TO_ARM;
6603
0
        }
6604
0
      else if (!hash)
6605
0
        {
6606
          /* It's a local symbol.  */
6607
0
          Elf_Internal_Sym *sym;
6608
6609
0
          if (local_syms == NULL)
6610
0
      {
6611
0
        local_syms
6612
0
          = (Elf_Internal_Sym *) symtab_hdr->contents;
6613
0
        if (local_syms == NULL)
6614
0
          local_syms
6615
0
            = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
6616
0
                  symtab_hdr->sh_info, 0,
6617
0
                  NULL, NULL, NULL);
6618
0
        if (local_syms == NULL)
6619
0
          goto error_ret_free_internal;
6620
0
      }
6621
6622
0
          sym = local_syms + r_indx;
6623
0
          if (sym->st_shndx == SHN_UNDEF)
6624
0
      sym_sec = bfd_und_section_ptr;
6625
0
          else if (sym->st_shndx == SHN_ABS)
6626
0
      sym_sec = bfd_abs_section_ptr;
6627
0
          else if (sym->st_shndx == SHN_COMMON)
6628
0
      sym_sec = bfd_com_section_ptr;
6629
0
          else
6630
0
      sym_sec =
6631
0
        bfd_section_from_elf_index (input_bfd, sym->st_shndx);
6632
6633
0
          if (!sym_sec)
6634
      /* This is an undefined symbol.  It can never
6635
         be resolved.  */
6636
0
      continue;
6637
6638
0
          if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
6639
0
      sym_value = sym->st_value;
6640
0
          destination = (sym_value + irela->r_addend
6641
0
             + sym_sec->output_offset
6642
0
             + sym_sec->output_section->vma);
6643
0
          st_type = ELF_ST_TYPE (sym->st_info);
6644
0
          branch_type =
6645
0
      ARM_GET_SYM_BRANCH_TYPE (sym->st_target_internal);
6646
0
          sym_name
6647
0
      = bfd_elf_string_from_elf_section (input_bfd,
6648
0
                 symtab_hdr->sh_link,
6649
0
                 sym->st_name);
6650
0
        }
6651
0
      else
6652
0
        {
6653
          /* It's an external symbol.  */
6654
0
          while (hash->root.root.type == bfd_link_hash_indirect
6655
0
           || hash->root.root.type == bfd_link_hash_warning)
6656
0
      hash = ((struct elf32_arm_link_hash_entry *)
6657
0
        hash->root.root.u.i.link);
6658
6659
0
          if (hash->root.root.type == bfd_link_hash_defined
6660
0
        || hash->root.root.type == bfd_link_hash_defweak)
6661
0
      {
6662
0
        sym_sec = hash->root.root.u.def.section;
6663
0
        sym_value = hash->root.root.u.def.value;
6664
6665
0
        struct elf32_arm_link_hash_table *globals =
6666
0
              elf32_arm_hash_table (info);
6667
6668
        /* For a destination in a shared library,
6669
           use the PLT stub as target address to
6670
           decide whether a branch stub is
6671
           needed.  */
6672
0
        if (globals != NULL
6673
0
            && globals->root.splt != NULL
6674
0
            && hash != NULL
6675
0
            && hash->root.plt.offset != (bfd_vma) -1)
6676
0
          {
6677
0
            sym_sec = globals->root.splt;
6678
0
            sym_value = hash->root.plt.offset;
6679
0
            if (sym_sec->output_section != NULL)
6680
0
        destination = (sym_value
6681
0
                 + sym_sec->output_offset
6682
0
                 + sym_sec->output_section->vma);
6683
0
          }
6684
0
        else if (sym_sec->output_section != NULL)
6685
0
          destination = (sym_value + irela->r_addend
6686
0
             + sym_sec->output_offset
6687
0
             + sym_sec->output_section->vma);
6688
0
      }
6689
0
          else if ((hash->root.root.type == bfd_link_hash_undefined)
6690
0
             || (hash->root.root.type == bfd_link_hash_undefweak))
6691
0
      {
6692
        /* For a shared library, use the PLT stub as
6693
           target address to decide whether a long
6694
           branch stub is needed.
6695
           For absolute code, they cannot be handled.  */
6696
0
        struct elf32_arm_link_hash_table *globals =
6697
0
          elf32_arm_hash_table (info);
6698
6699
0
        if (globals != NULL
6700
0
            && globals->root.splt != NULL
6701
0
            && hash != NULL
6702
0
            && hash->root.plt.offset != (bfd_vma) -1)
6703
0
          {
6704
0
            sym_sec = globals->root.splt;
6705
0
            sym_value = hash->root.plt.offset;
6706
0
            if (sym_sec->output_section != NULL)
6707
0
        destination = (sym_value
6708
0
                 + sym_sec->output_offset
6709
0
                 + sym_sec->output_section->vma);
6710
0
          }
6711
0
        else
6712
0
          continue;
6713
0
      }
6714
0
          else
6715
0
      {
6716
0
        bfd_set_error (bfd_error_bad_value);
6717
0
        goto error_ret_free_internal;
6718
0
      }
6719
0
          st_type = hash->root.type;
6720
0
          branch_type =
6721
0
      ARM_GET_SYM_BRANCH_TYPE (hash->root.target_internal);
6722
0
          sym_name = hash->root.root.root.string;
6723
0
        }
6724
6725
0
      do
6726
0
        {
6727
0
          bool new_stub;
6728
0
          struct elf32_arm_stub_hash_entry *stub_entry;
6729
6730
          /* Determine what (if any) linker stub is needed.  */
6731
0
          stub_type = arm_type_of_stub (info, section, irela,
6732
0
                st_type, &branch_type,
6733
0
                hash, destination, sym_sec,
6734
0
                input_bfd, sym_name);
6735
0
          if (stub_type == arm_stub_none)
6736
0
      break;
6737
6738
          /* We've either created a stub for this reloc already,
6739
       or we are about to.  */
6740
0
          stub_entry =
6741
0
      elf32_arm_create_stub (htab, stub_type, section, irela,
6742
0
                 sym_sec, hash,
6743
0
                 (char *) sym_name, sym_value,
6744
0
                 branch_type, &new_stub);
6745
6746
0
          created_stub = stub_entry != NULL;
6747
0
          if (!created_stub)
6748
0
      goto error_ret_free_internal;
6749
0
          else if (!new_stub)
6750
0
      break;
6751
0
          else
6752
0
      stub_changed = true;
6753
0
        }
6754
0
      while (0);
6755
6756
      /* Look for relocations which might trigger Cortex-A8
6757
         erratum.  */
6758
0
      if (htab->fix_cortex_a8
6759
0
          && (r_type == (unsigned int) R_ARM_THM_JUMP24
6760
0
        || r_type == (unsigned int) R_ARM_THM_JUMP19
6761
0
        || r_type == (unsigned int) R_ARM_THM_CALL
6762
0
        || r_type == (unsigned int) R_ARM_THM_XPC22))
6763
0
        {
6764
0
          bfd_vma from = section->output_section->vma
6765
0
             + section->output_offset
6766
0
             + irela->r_offset;
6767
6768
0
          if ((from & 0xfff) == 0xffe)
6769
0
      {
6770
        /* Found a candidate.  Note we haven't checked the
6771
           destination is within 4K here: if we do so (and
6772
           don't create an entry in a8_relocs) we can't tell
6773
           that a branch should have been relocated when
6774
           scanning later.  */
6775
0
        if (num_a8_relocs == a8_reloc_table_size)
6776
0
          {
6777
0
            a8_reloc_table_size *= 2;
6778
0
            a8_relocs = (struct a8_erratum_reloc *)
6779
0
          bfd_realloc (a8_relocs,
6780
0
                 sizeof (struct a8_erratum_reloc)
6781
0
                 * a8_reloc_table_size);
6782
0
          }
6783
6784
0
        a8_relocs[num_a8_relocs].from = from;
6785
0
        a8_relocs[num_a8_relocs].destination = destination;
6786
0
        a8_relocs[num_a8_relocs].r_type = r_type;
6787
0
        a8_relocs[num_a8_relocs].branch_type = branch_type;
6788
0
        a8_relocs[num_a8_relocs].sym_name = sym_name;
6789
0
        a8_relocs[num_a8_relocs].non_a8_stub = created_stub;
6790
0
        a8_relocs[num_a8_relocs].hash = hash;
6791
6792
0
        num_a8_relocs++;
6793
0
      }
6794
0
        }
6795
0
    }
6796
6797
        /* We're done with the internal relocs, free them.  */
6798
0
        if (elf_section_data (section)->relocs == NULL)
6799
0
    free (internal_relocs);
6800
0
      }
6801
6802
0
    if (htab->fix_cortex_a8)
6803
0
      {
6804
        /* Sort relocs which might apply to Cortex-A8 erratum.  */
6805
0
        qsort (a8_relocs, num_a8_relocs,
6806
0
         sizeof (struct a8_erratum_reloc),
6807
0
         &a8_reloc_compare);
6808
6809
        /* Scan for branches which might trigger Cortex-A8 erratum.  */
6810
0
        if (cortex_a8_erratum_scan (input_bfd, info, &a8_fixes,
6811
0
            &num_a8_fixes, &a8_fix_table_size,
6812
0
            a8_relocs, num_a8_relocs,
6813
0
            prev_num_a8_fixes, &stub_changed)
6814
0
      != 0)
6815
0
    goto error_ret_free_local;
6816
0
      }
6817
6818
0
    if (local_syms != NULL
6819
0
        && symtab_hdr->contents != (unsigned char *) local_syms)
6820
0
      {
6821
0
        if (!info->keep_memory)
6822
0
    free (local_syms);
6823
0
        else
6824
0
    symtab_hdr->contents = (unsigned char *) local_syms;
6825
0
      }
6826
0
  }
6827
6828
0
      if (first_veneer_scan
6829
0
    && !set_cmse_veneer_addr_from_implib (info, htab,
6830
0
            &cmse_stub_created))
6831
0
  ret = false;
6832
6833
0
      if (prev_num_a8_fixes != num_a8_fixes)
6834
0
  stub_changed = true;
6835
6836
0
      if (!stub_changed)
6837
0
  break;
6838
6839
      /* OK, we've added some stubs.  Find out the new size of the
6840
   stub sections.  */
6841
0
      for (stub_sec = htab->stub_bfd->sections;
6842
0
     stub_sec != NULL;
6843
0
     stub_sec = stub_sec->next)
6844
0
  {
6845
    /* Ignore non-stub sections.  */
6846
0
    if (!strstr (stub_sec->name, STUB_SUFFIX))
6847
0
      continue;
6848
6849
0
    stub_sec->size = 0;
6850
0
  }
6851
6852
      /* Add new SG veneers after those already in the input import
6853
   library.  */
6854
0
      for (stub_type = arm_stub_none + 1; stub_type < max_stub_type;
6855
0
     stub_type++)
6856
0
  {
6857
0
    bfd_vma *start_offset_p;
6858
0
    asection **stub_sec_p;
6859
6860
0
    start_offset_p = arm_new_stubs_start_offset_ptr (htab, stub_type);
6861
0
    stub_sec_p = arm_dedicated_stub_input_section_ptr (htab, stub_type);
6862
0
    if (start_offset_p == NULL)
6863
0
      continue;
6864
6865
0
    BFD_ASSERT (stub_sec_p != NULL);
6866
0
    if (*stub_sec_p != NULL)
6867
0
      (*stub_sec_p)->size = *start_offset_p;
6868
0
  }
6869
6870
      /* Compute stub section size, considering padding.  */
6871
0
      bfd_hash_traverse (&htab->stub_hash_table, arm_size_one_stub, htab);
6872
0
      for (stub_type = arm_stub_none + 1; stub_type < max_stub_type;
6873
0
     stub_type++)
6874
0
  {
6875
0
    int size, padding;
6876
0
    asection **stub_sec_p;
6877
6878
0
    padding = arm_dedicated_stub_section_padding (stub_type);
6879
0
    stub_sec_p = arm_dedicated_stub_input_section_ptr (htab, stub_type);
6880
    /* Skip if no stub input section or no stub section padding
6881
       required.  */
6882
0
    if ((stub_sec_p != NULL && *stub_sec_p == NULL) || padding == 0)
6883
0
      continue;
6884
    /* Stub section padding required but no dedicated section.  */
6885
0
    BFD_ASSERT (stub_sec_p);
6886
6887
0
    size = (*stub_sec_p)->size;
6888
0
    size = (size + padding - 1) & ~(padding - 1);
6889
0
    (*stub_sec_p)->size = size;
6890
0
  }
6891
6892
      /* Add Cortex-A8 erratum veneers to stub section sizes too.  */
6893
0
      if (htab->fix_cortex_a8)
6894
0
  for (i = 0; i < num_a8_fixes; i++)
6895
0
    {
6896
0
      stub_sec = elf32_arm_create_or_find_stub_sec (NULL,
6897
0
       a8_fixes[i].section, htab, a8_fixes[i].stub_type);
6898
6899
0
      if (stub_sec == NULL)
6900
0
        return false;
6901
6902
0
      stub_sec->size
6903
0
        += find_stub_size_and_template (a8_fixes[i].stub_type, NULL,
6904
0
                NULL);
6905
0
    }
6906
6907
6908
      /* Ask the linker to do its stuff.  */
6909
0
      (*htab->layout_sections_again) ();
6910
0
      first_veneer_scan = false;
6911
0
    }
6912
6913
  /* Add stubs for Cortex-A8 erratum fixes now.  */
6914
0
  if (htab->fix_cortex_a8)
6915
0
    {
6916
0
      for (i = 0; i < num_a8_fixes; i++)
6917
0
  {
6918
0
    struct elf32_arm_stub_hash_entry *stub_entry;
6919
0
    char *stub_name = a8_fixes[i].stub_name;
6920
0
    asection *section = a8_fixes[i].section;
6921
0
    unsigned int section_id = a8_fixes[i].section->id;
6922
0
    asection *link_sec = htab->stub_group[section_id].link_sec;
6923
0
    asection *stub_sec = htab->stub_group[section_id].stub_sec;
6924
0
    const insn_sequence *template_sequence;
6925
0
    int template_size, size = 0;
6926
6927
0
    stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name,
6928
0
               true, false);
6929
0
    if (stub_entry == NULL)
6930
0
      {
6931
0
        _bfd_error_handler (_("%pB: cannot create stub entry %s"),
6932
0
          section->owner, stub_name);
6933
0
        return false;
6934
0
      }
6935
6936
0
    stub_entry->stub_sec = stub_sec;
6937
0
    stub_entry->stub_offset = (bfd_vma) -1;
6938
0
    stub_entry->id_sec = link_sec;
6939
0
    stub_entry->stub_type = a8_fixes[i].stub_type;
6940
0
    stub_entry->source_value = a8_fixes[i].offset;
6941
0
    stub_entry->target_section = a8_fixes[i].section;
6942
0
    stub_entry->target_value = a8_fixes[i].target_offset;
6943
0
    stub_entry->orig_insn = a8_fixes[i].orig_insn;
6944
0
    stub_entry->branch_type = a8_fixes[i].branch_type;
6945
6946
0
    size = find_stub_size_and_template (a8_fixes[i].stub_type,
6947
0
                &template_sequence,
6948
0
                &template_size);
6949
6950
0
    stub_entry->stub_size = size;
6951
0
    stub_entry->stub_template = template_sequence;
6952
0
    stub_entry->stub_template_size = template_size;
6953
0
  }
6954
6955
      /* Stash the Cortex-A8 erratum fix array for use later in
6956
   elf32_arm_write_section().  */
6957
0
      htab->a8_erratum_fixes = a8_fixes;
6958
0
      htab->num_a8_erratum_fixes = num_a8_fixes;
6959
0
    }
6960
0
  else
6961
0
    {
6962
0
      htab->a8_erratum_fixes = NULL;
6963
0
      htab->num_a8_erratum_fixes = 0;
6964
0
    }
6965
0
  return ret;
6966
0
}
6967
6968
/* Build all the stubs associated with the current output file.  The
6969
   stubs are kept in a hash table attached to the main linker hash
6970
   table.  We also set up the .plt entries for statically linked PIC
6971
   functions here.  This function is called via arm_elf_finish in the
6972
   linker.  */
6973
6974
bool
6975
elf32_arm_build_stubs (struct bfd_link_info *info)
6976
0
{
6977
0
  asection *stub_sec;
6978
0
  struct bfd_hash_table *table;
6979
0
  enum elf32_arm_stub_type stub_type;
6980
0
  struct elf32_arm_link_hash_table *htab;
6981
6982
0
  htab = elf32_arm_hash_table (info);
6983
0
  if (htab == NULL)
6984
0
    return false;
6985
6986
0
  for (stub_sec = htab->stub_bfd->sections;
6987
0
       stub_sec != NULL;
6988
0
       stub_sec = stub_sec->next)
6989
0
    {
6990
0
      bfd_size_type size;
6991
6992
      /* Ignore non-stub sections.  */
6993
0
      if (!strstr (stub_sec->name, STUB_SUFFIX))
6994
0
  continue;
6995
6996
      /* Allocate memory to hold the linker stubs.  Zeroing the stub sections
6997
   must at least be done for stub section requiring padding and for SG
6998
   veneers to ensure that a non secure code branching to a removed SG
6999
   veneer causes an error.  */
7000
0
      size = stub_sec->size;
7001
0
      stub_sec->contents = (unsigned char *) bfd_zalloc (htab->stub_bfd, size);
7002
0
      if (stub_sec->contents == NULL && size != 0)
7003
0
  return false;
7004
0
      stub_sec->alloced = 1;
7005
7006
0
      stub_sec->size = 0;
7007
0
    }
7008
7009
  /* Add new SG veneers after those already in the input import library.  */
7010
0
  for (stub_type = arm_stub_none + 1; stub_type < max_stub_type; stub_type++)
7011
0
    {
7012
0
      bfd_vma *start_offset_p;
7013
0
      asection **stub_sec_p;
7014
7015
0
      start_offset_p = arm_new_stubs_start_offset_ptr (htab, stub_type);
7016
0
      stub_sec_p = arm_dedicated_stub_input_section_ptr (htab, stub_type);
7017
0
      if (start_offset_p == NULL)
7018
0
  continue;
7019
7020
0
      BFD_ASSERT (stub_sec_p != NULL);
7021
0
      if (*stub_sec_p != NULL)
7022
0
  (*stub_sec_p)->size = *start_offset_p;
7023
0
    }
7024
7025
  /* Build the stubs as directed by the stub hash table.  */
7026
0
  table = &htab->stub_hash_table;
7027
0
  bfd_hash_traverse (table, arm_build_one_stub, info);
7028
0
  if (htab->fix_cortex_a8)
7029
0
    {
7030
      /* Place the cortex a8 stubs last.  */
7031
0
      htab->fix_cortex_a8 = -1;
7032
0
      bfd_hash_traverse (table, arm_build_one_stub, info);
7033
0
    }
7034
7035
0
  return true;
7036
0
}
7037
7038
/* Locate the Thumb encoded calling stub for NAME.  */
7039
7040
static struct elf_link_hash_entry *
7041
find_thumb_glue (struct bfd_link_info *link_info,
7042
     const char *name,
7043
     char **error_message)
7044
0
{
7045
0
  char *tmp_name;
7046
0
  struct elf_link_hash_entry *hash;
7047
0
  struct elf32_arm_link_hash_table *hash_table;
7048
7049
  /* We need a pointer to the armelf specific hash table.  */
7050
0
  hash_table = elf32_arm_hash_table (link_info);
7051
0
  if (hash_table == NULL)
7052
0
    return NULL;
7053
7054
0
  tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
7055
0
          + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
7056
7057
0
  BFD_ASSERT (tmp_name);
7058
7059
0
  sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
7060
7061
0
  hash = elf_link_hash_lookup
7062
0
    (&(hash_table)->root, tmp_name, false, false, true);
7063
7064
0
  if (hash == NULL)
7065
0
    {
7066
0
      *error_message = bfd_asprintf (_("unable to find %s glue '%s' for '%s'"),
7067
0
             "Thumb", tmp_name, name);
7068
0
      if (*error_message == NULL)
7069
0
  *error_message = (char *) bfd_errmsg (bfd_error_system_call);
7070
0
    }
7071
7072
0
  free (tmp_name);
7073
7074
0
  return hash;
7075
0
}
7076
7077
/* Locate the ARM encoded calling stub for NAME.  */
7078
7079
static struct elf_link_hash_entry *
7080
find_arm_glue (struct bfd_link_info *link_info,
7081
         const char *name,
7082
         char **error_message)
7083
0
{
7084
0
  char *tmp_name;
7085
0
  struct elf_link_hash_entry *myh;
7086
0
  struct elf32_arm_link_hash_table *hash_table;
7087
7088
  /* We need a pointer to the elfarm specific hash table.  */
7089
0
  hash_table = elf32_arm_hash_table (link_info);
7090
0
  if (hash_table == NULL)
7091
0
    return NULL;
7092
7093
0
  tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
7094
0
          + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
7095
0
  BFD_ASSERT (tmp_name);
7096
7097
0
  sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
7098
7099
0
  myh = elf_link_hash_lookup
7100
0
    (&(hash_table)->root, tmp_name, false, false, true);
7101
7102
0
  if (myh == NULL)
7103
0
    {
7104
0
      *error_message = bfd_asprintf (_("unable to find %s glue '%s' for '%s'"),
7105
0
             "ARM", tmp_name, name);
7106
0
      if (*error_message == NULL)
7107
0
  *error_message = (char *) bfd_errmsg (bfd_error_system_call);
7108
0
    }
7109
0
  free (tmp_name);
7110
7111
0
  return myh;
7112
0
}
7113
7114
/* ARM->Thumb glue (static images):
7115
7116
   .arm
7117
   __func_from_arm:
7118
   ldr r12, __func_addr
7119
   bx  r12
7120
   __func_addr:
7121
   .word func    @ behave as if you saw a ARM_32 reloc.
7122
7123
   (v5t static images)
7124
   .arm
7125
   __func_from_arm:
7126
   ldr pc, __func_addr
7127
   __func_addr:
7128
   .word func    @ behave as if you saw a ARM_32 reloc.
7129
7130
   (relocatable images)
7131
   .arm
7132
   __func_from_arm:
7133
   ldr r12, __func_offset
7134
   add r12, r12, pc
7135
   bx  r12
7136
   __func_offset:
7137
   .word func - .   */
7138
7139
0
#define ARM2THUMB_STATIC_GLUE_SIZE 12
7140
static const insn32 a2t1_ldr_insn = 0xe59fc000;
7141
static const insn32 a2t2_bx_r12_insn = 0xe12fff1c;
7142
static const insn32 a2t3_func_addr_insn = 0x00000001;
7143
7144
0
#define ARM2THUMB_V5_STATIC_GLUE_SIZE 8
7145
static const insn32 a2t1v5_ldr_insn = 0xe51ff004;
7146
static const insn32 a2t2v5_func_addr_insn = 0x00000001;
7147
7148
0
#define ARM2THUMB_PIC_GLUE_SIZE 16
7149
static const insn32 a2t1p_ldr_insn = 0xe59fc004;
7150
static const insn32 a2t2p_add_pc_insn = 0xe08cc00f;
7151
static const insn32 a2t3p_bx_r12_insn = 0xe12fff1c;
7152
7153
/* Thumb->ARM:        Thumb->(non-interworking aware) ARM
7154
7155
     .thumb       .thumb
7156
     .align 2       .align 2
7157
 __func_from_thumb:       __func_from_thumb:
7158
     bx pc        push {r6, lr}
7159
     nop        ldr  r6, __func_addr
7160
     .arm       mov  lr, pc
7161
     b func       bx   r6
7162
          .arm
7163
            ;; back_to_thumb
7164
          ldmia r13! {r6, lr}
7165
          bx    lr
7166
            __func_addr:
7167
          .word      func  */
7168
7169
0
#define THUMB2ARM_GLUE_SIZE 8
7170
static const insn16 t2a1_bx_pc_insn = 0x4778;
7171
static const insn16 t2a2_noop_insn = 0x46c0;
7172
static const insn32 t2a3_b_insn = 0xea000000;
7173
7174
0
#define VFP11_ERRATUM_VENEER_SIZE 8
7175
0
#define STM32L4XX_ERRATUM_LDM_VENEER_SIZE 16
7176
0
#define STM32L4XX_ERRATUM_VLDM_VENEER_SIZE 24
7177
7178
0
#define ARM_BX_VENEER_SIZE 12
7179
static const insn32 armbx1_tst_insn = 0xe3100001;
7180
static const insn32 armbx2_moveq_insn = 0x01a0f000;
7181
static const insn32 armbx3_bx_insn = 0xe12fff10;
7182
7183
#ifndef ELFARM_NABI_C_INCLUDED
7184
static void
7185
arm_allocate_glue_section_space (bfd * abfd, bfd_size_type size, const char * name)
7186
0
{
7187
0
  asection * s;
7188
0
  bfd_byte * contents;
7189
7190
0
  if (size == 0)
7191
0
    {
7192
      /* Do not include empty glue sections in the output.  */
7193
0
      if (abfd != NULL)
7194
0
  {
7195
0
    s = bfd_get_linker_section (abfd, name);
7196
0
    if (s != NULL)
7197
0
      s->flags |= SEC_EXCLUDE;
7198
0
  }
7199
0
      return;
7200
0
    }
7201
7202
0
  BFD_ASSERT (abfd != NULL);
7203
7204
0
  s = bfd_get_linker_section (abfd, name);
7205
0
  BFD_ASSERT (s != NULL);
7206
7207
0
  contents = (bfd_byte *) bfd_zalloc (abfd, size);
7208
7209
0
  BFD_ASSERT (s->size == size);
7210
0
  s->contents = contents;
7211
0
  s->alloced = 1;
7212
0
}
7213
7214
bool
7215
bfd_elf32_arm_allocate_interworking_sections (struct bfd_link_info * info)
7216
0
{
7217
0
  struct elf32_arm_link_hash_table * globals;
7218
7219
0
  globals = elf32_arm_hash_table (info);
7220
0
  BFD_ASSERT (globals != NULL);
7221
7222
0
  arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
7223
0
           globals->arm_glue_size,
7224
0
           ARM2THUMB_GLUE_SECTION_NAME);
7225
7226
0
  arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
7227
0
           globals->thumb_glue_size,
7228
0
           THUMB2ARM_GLUE_SECTION_NAME);
7229
7230
0
  arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
7231
0
           globals->vfp11_erratum_glue_size,
7232
0
           VFP11_ERRATUM_VENEER_SECTION_NAME);
7233
7234
0
  arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
7235
0
           globals->stm32l4xx_erratum_glue_size,
7236
0
           STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
7237
7238
0
  arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
7239
0
           globals->bx_glue_size,
7240
0
           ARM_BX_GLUE_SECTION_NAME);
7241
7242
0
  return true;
7243
0
}
7244
7245
/* Allocate space and symbols for calling a Thumb function from Arm mode.
7246
   returns the symbol identifying the stub.  */
7247
7248
static struct elf_link_hash_entry *
7249
record_arm_to_thumb_glue (struct bfd_link_info * link_info,
7250
        struct elf_link_hash_entry * h)
7251
0
{
7252
0
  const char * name = h->root.root.string;
7253
0
  asection * s;
7254
0
  char * tmp_name;
7255
0
  struct elf_link_hash_entry * myh;
7256
0
  struct bfd_link_hash_entry * bh;
7257
0
  struct elf32_arm_link_hash_table * globals;
7258
0
  bfd_vma val;
7259
0
  bfd_size_type size;
7260
7261
0
  globals = elf32_arm_hash_table (link_info);
7262
0
  BFD_ASSERT (globals != NULL);
7263
0
  BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
7264
7265
0
  s = bfd_get_linker_section
7266
0
    (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
7267
7268
0
  BFD_ASSERT (s != NULL);
7269
7270
0
  tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
7271
0
          + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
7272
0
  BFD_ASSERT (tmp_name);
7273
7274
0
  sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
7275
7276
0
  myh = elf_link_hash_lookup
7277
0
    (&(globals)->root, tmp_name, false, false, true);
7278
7279
0
  if (myh != NULL)
7280
0
    {
7281
      /* We've already seen this guy.  */
7282
0
      free (tmp_name);
7283
0
      return myh;
7284
0
    }
7285
7286
  /* The only trick here is using hash_table->arm_glue_size as the value.
7287
     Even though the section isn't allocated yet, this is where we will be
7288
     putting it.  The +1 on the value marks that the stub has not been
7289
     output yet - not that it is a Thumb function.  */
7290
0
  bh = NULL;
7291
0
  val = globals->arm_glue_size + 1;
7292
0
  _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
7293
0
            tmp_name, BSF_GLOBAL, s, val,
7294
0
            NULL, true, false, &bh);
7295
7296
0
  myh = (struct elf_link_hash_entry *) bh;
7297
0
  myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7298
0
  myh->forced_local = 1;
7299
7300
0
  free (tmp_name);
7301
7302
0
  if (bfd_link_pic (link_info)
7303
0
      || globals->pic_veneer)
7304
0
    size = ARM2THUMB_PIC_GLUE_SIZE;
7305
0
  else if (globals->use_blx)
7306
0
    size = ARM2THUMB_V5_STATIC_GLUE_SIZE;
7307
0
  else
7308
0
    size = ARM2THUMB_STATIC_GLUE_SIZE;
7309
7310
0
  s->size += size;
7311
0
  globals->arm_glue_size += size;
7312
7313
0
  return myh;
7314
0
}
7315
7316
/* Allocate space for ARMv4 BX veneers.  */
7317
7318
static void
7319
record_arm_bx_glue (struct bfd_link_info * link_info, int reg)
7320
0
{
7321
0
  asection * s;
7322
0
  struct elf32_arm_link_hash_table *globals;
7323
0
  char *tmp_name;
7324
0
  struct elf_link_hash_entry *myh;
7325
0
  struct bfd_link_hash_entry *bh;
7326
0
  bfd_vma val;
7327
7328
  /* BX PC does not need a veneer.  */
7329
0
  if (reg == 15)
7330
0
    return;
7331
7332
0
  globals = elf32_arm_hash_table (link_info);
7333
0
  BFD_ASSERT (globals != NULL);
7334
0
  BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
7335
7336
  /* Check if this veneer has already been allocated.  */
7337
0
  if (globals->bx_glue_offset[reg])
7338
0
    return;
7339
7340
0
  s = bfd_get_linker_section
7341
0
    (globals->bfd_of_glue_owner, ARM_BX_GLUE_SECTION_NAME);
7342
7343
0
  BFD_ASSERT (s != NULL);
7344
7345
  /* Add symbol for veneer.  */
7346
0
  tmp_name = (char *)
7347
0
      bfd_malloc ((bfd_size_type) strlen (ARM_BX_GLUE_ENTRY_NAME) + 1);
7348
0
  BFD_ASSERT (tmp_name);
7349
7350
0
  sprintf (tmp_name, ARM_BX_GLUE_ENTRY_NAME, reg);
7351
7352
0
  myh = elf_link_hash_lookup
7353
0
    (&(globals)->root, tmp_name, false, false, false);
7354
7355
0
  BFD_ASSERT (myh == NULL);
7356
7357
0
  bh = NULL;
7358
0
  val = globals->bx_glue_size;
7359
0
  _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
7360
0
            tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
7361
0
            NULL, true, false, &bh);
7362
7363
0
  myh = (struct elf_link_hash_entry *) bh;
7364
0
  myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7365
0
  myh->forced_local = 1;
7366
7367
0
  s->size += ARM_BX_VENEER_SIZE;
7368
0
  globals->bx_glue_offset[reg] = globals->bx_glue_size | 2;
7369
0
  globals->bx_glue_size += ARM_BX_VENEER_SIZE;
7370
0
}
7371
7372
7373
/* Add an entry to the code/data map for section SEC.  */
7374
7375
static void
7376
elf32_arm_section_map_add (asection *sec, char type, bfd_vma vma)
7377
0
{
7378
0
  struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
7379
0
  unsigned int newidx;
7380
7381
0
  if (sec_data->map == NULL)
7382
0
    {
7383
0
      sec_data->map = (elf32_arm_section_map *)
7384
0
    bfd_malloc (sizeof (elf32_arm_section_map));
7385
0
      sec_data->mapcount = 0;
7386
0
      sec_data->mapsize = 1;
7387
0
    }
7388
7389
0
  newidx = sec_data->mapcount++;
7390
7391
0
  if (sec_data->mapcount > sec_data->mapsize)
7392
0
    {
7393
0
      sec_data->mapsize *= 2;
7394
0
      sec_data->map = (elf32_arm_section_map *)
7395
0
    bfd_realloc_or_free (sec_data->map, sec_data->mapsize
7396
0
             * sizeof (elf32_arm_section_map));
7397
0
    }
7398
7399
0
  if (sec_data->map)
7400
0
    {
7401
0
      sec_data->map[newidx].vma = vma;
7402
0
      sec_data->map[newidx].type = type;
7403
0
    }
7404
0
}
7405
7406
7407
/* Record information about a VFP11 denorm-erratum veneer.  Only ARM-mode
7408
   veneers are handled for now.  */
7409
7410
static bfd_vma
7411
record_vfp11_erratum_veneer (struct bfd_link_info *link_info,
7412
           elf32_vfp11_erratum_list *branch,
7413
           bfd *branch_bfd,
7414
           asection *branch_sec,
7415
           unsigned int offset)
7416
0
{
7417
0
  asection *s;
7418
0
  struct elf32_arm_link_hash_table *hash_table;
7419
0
  char *tmp_name;
7420
0
  struct elf_link_hash_entry *myh;
7421
0
  struct bfd_link_hash_entry *bh;
7422
0
  bfd_vma val;
7423
0
  struct _arm_elf_section_data *sec_data;
7424
0
  elf32_vfp11_erratum_list *newerr;
7425
7426
0
  hash_table = elf32_arm_hash_table (link_info);
7427
0
  BFD_ASSERT (hash_table != NULL);
7428
0
  BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
7429
7430
0
  s = bfd_get_linker_section
7431
0
    (hash_table->bfd_of_glue_owner, VFP11_ERRATUM_VENEER_SECTION_NAME);
7432
7433
0
  sec_data = elf32_arm_section_data (s);
7434
7435
0
  BFD_ASSERT (s != NULL);
7436
7437
0
  tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
7438
0
          (VFP11_ERRATUM_VENEER_ENTRY_NAME) + 10);
7439
0
  BFD_ASSERT (tmp_name);
7440
7441
0
  sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME,
7442
0
     hash_table->num_vfp11_fixes);
7443
7444
0
  myh = elf_link_hash_lookup
7445
0
    (&(hash_table)->root, tmp_name, false, false, false);
7446
7447
0
  BFD_ASSERT (myh == NULL);
7448
7449
0
  bh = NULL;
7450
0
  val = hash_table->vfp11_erratum_glue_size;
7451
0
  _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
7452
0
            tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
7453
0
            NULL, true, false, &bh);
7454
7455
0
  myh = (struct elf_link_hash_entry *) bh;
7456
0
  myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7457
0
  myh->forced_local = 1;
7458
7459
  /* Link veneer back to calling location.  */
7460
0
  sec_data->erratumcount += 1;
7461
0
  newerr = (elf32_vfp11_erratum_list *)
7462
0
      bfd_zmalloc (sizeof (elf32_vfp11_erratum_list));
7463
7464
0
  newerr->type = VFP11_ERRATUM_ARM_VENEER;
7465
0
  newerr->vma = -1;
7466
0
  newerr->u.v.branch = branch;
7467
0
  newerr->u.v.id = hash_table->num_vfp11_fixes;
7468
0
  branch->u.b.veneer = newerr;
7469
7470
0
  newerr->next = sec_data->erratumlist;
7471
0
  sec_data->erratumlist = newerr;
7472
7473
  /* A symbol for the return from the veneer.  */
7474
0
  sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME "_r",
7475
0
     hash_table->num_vfp11_fixes);
7476
7477
0
  myh = elf_link_hash_lookup
7478
0
    (&(hash_table)->root, tmp_name, false, false, false);
7479
7480
0
  if (myh != NULL)
7481
0
    abort ();
7482
7483
0
  bh = NULL;
7484
0
  val = offset + 4;
7485
0
  _bfd_generic_link_add_one_symbol (link_info, branch_bfd, tmp_name, BSF_LOCAL,
7486
0
            branch_sec, val, NULL, true, false, &bh);
7487
7488
0
  myh = (struct elf_link_hash_entry *) bh;
7489
0
  myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7490
0
  myh->forced_local = 1;
7491
7492
0
  free (tmp_name);
7493
7494
  /* Generate a mapping symbol for the veneer section, and explicitly add an
7495
     entry for that symbol to the code/data map for the section.  */
7496
0
  if (hash_table->vfp11_erratum_glue_size == 0)
7497
0
    {
7498
0
      bh = NULL;
7499
      /* FIXME: Creates an ARM symbol.  Thumb mode will need attention if it
7500
   ever requires this erratum fix.  */
7501
0
      _bfd_generic_link_add_one_symbol (link_info,
7502
0
          hash_table->bfd_of_glue_owner, "$a",
7503
0
          BSF_LOCAL, s, 0, NULL,
7504
0
          true, false, &bh);
7505
7506
0
      myh = (struct elf_link_hash_entry *) bh;
7507
0
      myh->type = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
7508
0
      myh->forced_local = 1;
7509
7510
      /* The elf32_arm_init_maps function only cares about symbols from input
7511
   BFDs.  We must make a note of this generated mapping symbol
7512
   ourselves so that code byteswapping works properly in
7513
   elf32_arm_write_section.  */
7514
0
      elf32_arm_section_map_add (s, 'a', 0);
7515
0
    }
7516
7517
0
  s->size += VFP11_ERRATUM_VENEER_SIZE;
7518
0
  hash_table->vfp11_erratum_glue_size += VFP11_ERRATUM_VENEER_SIZE;
7519
0
  hash_table->num_vfp11_fixes++;
7520
7521
  /* The offset of the veneer.  */
7522
0
  return val;
7523
0
}
7524
7525
/* Record information about a STM32L4XX STM erratum veneer.  Only THUMB-mode
7526
   veneers need to be handled because used only in Cortex-M.  */
7527
7528
static bfd_vma
7529
record_stm32l4xx_erratum_veneer (struct bfd_link_info *link_info,
7530
         elf32_stm32l4xx_erratum_list *branch,
7531
         bfd *branch_bfd,
7532
         asection *branch_sec,
7533
         unsigned int offset,
7534
         bfd_size_type veneer_size)
7535
0
{
7536
0
  asection *s;
7537
0
  struct elf32_arm_link_hash_table *hash_table;
7538
0
  char *tmp_name;
7539
0
  struct elf_link_hash_entry *myh;
7540
0
  struct bfd_link_hash_entry *bh;
7541
0
  bfd_vma val;
7542
0
  struct _arm_elf_section_data *sec_data;
7543
0
  elf32_stm32l4xx_erratum_list *newerr;
7544
7545
0
  hash_table = elf32_arm_hash_table (link_info);
7546
0
  BFD_ASSERT (hash_table != NULL);
7547
0
  BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
7548
7549
0
  s = bfd_get_linker_section
7550
0
    (hash_table->bfd_of_glue_owner, STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
7551
7552
0
  BFD_ASSERT (s != NULL);
7553
7554
0
  sec_data = elf32_arm_section_data (s);
7555
7556
0
  tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
7557
0
          (STM32L4XX_ERRATUM_VENEER_ENTRY_NAME) + 10);
7558
0
  BFD_ASSERT (tmp_name);
7559
7560
0
  sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME,
7561
0
     hash_table->num_stm32l4xx_fixes);
7562
7563
0
  myh = elf_link_hash_lookup
7564
0
    (&(hash_table)->root, tmp_name, false, false, false);
7565
7566
0
  BFD_ASSERT (myh == NULL);
7567
7568
0
  bh = NULL;
7569
0
  val = hash_table->stm32l4xx_erratum_glue_size;
7570
0
  _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
7571
0
            tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
7572
0
            NULL, true, false, &bh);
7573
7574
0
  myh = (struct elf_link_hash_entry *) bh;
7575
0
  myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7576
0
  myh->forced_local = 1;
7577
7578
  /* Link veneer back to calling location.  */
7579
0
  sec_data->stm32l4xx_erratumcount += 1;
7580
0
  newerr = (elf32_stm32l4xx_erratum_list *)
7581
0
      bfd_zmalloc (sizeof (elf32_stm32l4xx_erratum_list));
7582
7583
0
  newerr->type = STM32L4XX_ERRATUM_VENEER;
7584
0
  newerr->vma = -1;
7585
0
  newerr->u.v.branch = branch;
7586
0
  newerr->u.v.id = hash_table->num_stm32l4xx_fixes;
7587
0
  branch->u.b.veneer = newerr;
7588
7589
0
  newerr->next = sec_data->stm32l4xx_erratumlist;
7590
0
  sec_data->stm32l4xx_erratumlist = newerr;
7591
7592
  /* A symbol for the return from the veneer.  */
7593
0
  sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME "_r",
7594
0
     hash_table->num_stm32l4xx_fixes);
7595
7596
0
  myh = elf_link_hash_lookup
7597
0
    (&(hash_table)->root, tmp_name, false, false, false);
7598
7599
0
  if (myh != NULL)
7600
0
    abort ();
7601
7602
0
  bh = NULL;
7603
0
  val = offset + 4;
7604
0
  _bfd_generic_link_add_one_symbol (link_info, branch_bfd, tmp_name, BSF_LOCAL,
7605
0
            branch_sec, val, NULL, true, false, &bh);
7606
7607
0
  myh = (struct elf_link_hash_entry *) bh;
7608
0
  myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7609
0
  myh->forced_local = 1;
7610
7611
0
  free (tmp_name);
7612
7613
  /* Generate a mapping symbol for the veneer section, and explicitly add an
7614
     entry for that symbol to the code/data map for the section.  */
7615
0
  if (hash_table->stm32l4xx_erratum_glue_size == 0)
7616
0
    {
7617
0
      bh = NULL;
7618
      /* Creates a THUMB symbol since there is no other choice.  */
7619
0
      _bfd_generic_link_add_one_symbol (link_info,
7620
0
          hash_table->bfd_of_glue_owner, "$t",
7621
0
          BSF_LOCAL, s, 0, NULL,
7622
0
          true, false, &bh);
7623
7624
0
      myh = (struct elf_link_hash_entry *) bh;
7625
0
      myh->type = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
7626
0
      myh->forced_local = 1;
7627
7628
      /* The elf32_arm_init_maps function only cares about symbols from input
7629
   BFDs.  We must make a note of this generated mapping symbol
7630
   ourselves so that code byteswapping works properly in
7631
   elf32_arm_write_section.  */
7632
0
      elf32_arm_section_map_add (s, 't', 0);
7633
0
    }
7634
7635
0
  s->size += veneer_size;
7636
0
  hash_table->stm32l4xx_erratum_glue_size += veneer_size;
7637
0
  hash_table->num_stm32l4xx_fixes++;
7638
7639
  /* The offset of the veneer.  */
7640
0
  return val;
7641
0
}
7642
7643
#define ARM_GLUE_SECTION_FLAGS \
7644
0
  (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE \
7645
0
   | SEC_READONLY | SEC_LINKER_CREATED)
7646
7647
/* Create a fake section for use by the ARM backend of the linker.  */
7648
7649
static bool
7650
arm_make_glue_section (bfd * abfd, const char * name)
7651
0
{
7652
0
  asection * sec;
7653
7654
0
  sec = bfd_get_linker_section (abfd, name);
7655
0
  if (sec != NULL)
7656
    /* Already made.  */
7657
0
    return true;
7658
7659
0
  sec = bfd_make_section_anyway_with_flags (abfd, name, ARM_GLUE_SECTION_FLAGS);
7660
7661
0
  if (sec == NULL
7662
0
      || !bfd_set_section_alignment (sec, 2))
7663
0
    return false;
7664
7665
  /* Set the gc mark to prevent the section from being removed by garbage
7666
     collection, despite the fact that no relocs refer to this section.  */
7667
0
  sec->gc_mark = 1;
7668
7669
0
  return true;
7670
0
}
7671
7672
/* Set size of .plt entries.  This function is called from the
7673
   linker scripts in ld/emultempl/{armelf}.em.  */
7674
7675
void
7676
bfd_elf32_arm_use_long_plt (void)
7677
0
{
7678
0
  elf32_arm_use_long_plt_entry = true;
7679
0
}
7680
7681
/* Add the glue sections to ABFD.  This function is called from the
7682
   linker scripts in ld/emultempl/{armelf}.em.  */
7683
7684
bool
7685
bfd_elf32_arm_add_glue_sections_to_bfd (bfd *abfd,
7686
          struct bfd_link_info *info)
7687
0
{
7688
0
  struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
7689
0
  bool dostm32l4xx = globals
7690
0
    && globals->stm32l4xx_fix != BFD_ARM_STM32L4XX_FIX_NONE;
7691
0
  bool addglue;
7692
7693
  /* If we are only performing a partial
7694
     link do not bother adding the glue.  */
7695
0
  if (bfd_link_relocatable (info))
7696
0
    return true;
7697
7698
0
  addglue = arm_make_glue_section (abfd, ARM2THUMB_GLUE_SECTION_NAME)
7699
0
    && arm_make_glue_section (abfd, THUMB2ARM_GLUE_SECTION_NAME)
7700
0
    && arm_make_glue_section (abfd, VFP11_ERRATUM_VENEER_SECTION_NAME)
7701
0
    && arm_make_glue_section (abfd, ARM_BX_GLUE_SECTION_NAME);
7702
7703
0
  if (!dostm32l4xx)
7704
0
    return addglue;
7705
7706
0
  return addglue
7707
0
    && arm_make_glue_section (abfd, STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
7708
0
}
7709
7710
/* Mark output sections of veneers needing a dedicated one with SEC_KEEP.  This
7711
   ensures they are not marked for deletion by
7712
   strip_excluded_output_sections () when veneers are going to be created
7713
   later.  Not doing so would trigger assert on empty section size in
7714
   lang_size_sections_1 ().  */
7715
7716
void
7717
bfd_elf32_arm_keep_private_stub_output_sections (struct bfd_link_info *info)
7718
0
{
7719
0
  enum elf32_arm_stub_type stub_type;
7720
7721
  /* If we are only performing a partial
7722
     link do not bother adding the glue.  */
7723
0
  if (bfd_link_relocatable (info))
7724
0
    return;
7725
7726
0
  for (stub_type = arm_stub_none + 1; stub_type < max_stub_type; stub_type++)
7727
0
    {
7728
0
      asection *out_sec;
7729
0
      const char *out_sec_name;
7730
7731
0
      if (!arm_dedicated_stub_output_section_required (stub_type))
7732
0
  continue;
7733
7734
0
     out_sec_name = arm_dedicated_stub_output_section_name (stub_type);
7735
0
     out_sec = bfd_get_section_by_name (info->output_bfd, out_sec_name);
7736
0
     if (out_sec != NULL)
7737
0
  out_sec->flags |= SEC_KEEP;
7738
0
    }
7739
0
}
7740
7741
/* Select a BFD to be used to hold the sections used by the glue code.
7742
   This function is called from the linker scripts in ld/emultempl/
7743
   {armelf/pe}.em.  */
7744
7745
bool
7746
bfd_elf32_arm_get_bfd_for_interworking (bfd *abfd, struct bfd_link_info *info)
7747
0
{
7748
0
  struct elf32_arm_link_hash_table *globals;
7749
7750
  /* If we are only performing a partial link
7751
     do not bother getting a bfd to hold the glue.  */
7752
0
  if (bfd_link_relocatable (info))
7753
0
    return true;
7754
7755
  /* Make sure we don't attach the glue sections to a dynamic object.  */
7756
0
  BFD_ASSERT (!(abfd->flags & DYNAMIC));
7757
7758
0
  globals = elf32_arm_hash_table (info);
7759
0
  BFD_ASSERT (globals != NULL);
7760
7761
0
  if (globals->bfd_of_glue_owner != NULL)
7762
0
    return true;
7763
7764
  /* Save the bfd for later use.  */
7765
0
  globals->bfd_of_glue_owner = abfd;
7766
7767
0
  return true;
7768
0
}
7769
7770
static void
7771
check_use_blx (struct elf32_arm_link_hash_table *globals)
7772
0
{
7773
0
  int cpu_arch;
7774
7775
0
  cpu_arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
7776
0
               Tag_CPU_arch);
7777
7778
0
  if (globals->fix_arm1176)
7779
0
    {
7780
0
      if (cpu_arch == TAG_CPU_ARCH_V6T2 || cpu_arch > TAG_CPU_ARCH_V6K)
7781
0
  globals->use_blx = 1;
7782
0
    }
7783
0
  else
7784
0
    {
7785
0
      if (cpu_arch > TAG_CPU_ARCH_V4T)
7786
0
  globals->use_blx = 1;
7787
0
    }
7788
0
}
7789
7790
bool
7791
bfd_elf32_arm_process_before_allocation (bfd *abfd,
7792
           struct bfd_link_info *link_info)
7793
0
{
7794
0
  Elf_Internal_Shdr *symtab_hdr;
7795
0
  Elf_Internal_Rela *internal_relocs = NULL;
7796
0
  Elf_Internal_Rela *irel, *irelend;
7797
0
  bfd_byte *contents = NULL;
7798
7799
0
  asection *sec;
7800
0
  struct elf32_arm_link_hash_table *globals;
7801
7802
  /* If we are only performing a partial link do not bother
7803
     to construct any glue.  */
7804
0
  if (bfd_link_relocatable (link_info))
7805
0
    return true;
7806
7807
  /* Here we have a bfd that is to be included on the link.  We have a
7808
     hook to do reloc rummaging, before section sizes are nailed down.  */
7809
0
  globals = elf32_arm_hash_table (link_info);
7810
0
  BFD_ASSERT (globals != NULL);
7811
7812
0
  check_use_blx (globals);
7813
7814
0
  if (globals->byteswap_code && !bfd_big_endian (abfd))
7815
0
    {
7816
0
      _bfd_error_handler (_("%pB: BE8 images only valid in big-endian mode"),
7817
0
        abfd);
7818
0
      return false;
7819
0
    }
7820
7821
  /* PR 5398: If we have not decided to include any loadable sections in
7822
     the output then we will not have a glue owner bfd.  This is OK, it
7823
     just means that there is nothing else for us to do here.  */
7824
0
  if (globals->bfd_of_glue_owner == NULL)
7825
0
    return true;
7826
7827
  /* Rummage around all the relocs and map the glue vectors.  */
7828
0
  sec = abfd->sections;
7829
7830
0
  if (sec == NULL)
7831
0
    return true;
7832
7833
0
  for (; sec != NULL; sec = sec->next)
7834
0
    {
7835
0
      if (sec->reloc_count == 0)
7836
0
  continue;
7837
7838
0
      if ((sec->flags & SEC_EXCLUDE) != 0
7839
0
    || (sec->flags & SEC_HAS_CONTENTS) == 0)
7840
0
  continue;
7841
7842
0
      symtab_hdr = & elf_symtab_hdr (abfd);
7843
7844
      /* Load the relocs.  */
7845
0
      internal_relocs
7846
0
  = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, false);
7847
7848
0
      if (internal_relocs == NULL)
7849
0
  goto error_return;
7850
7851
0
      irelend = internal_relocs + sec->reloc_count;
7852
0
      for (irel = internal_relocs; irel < irelend; irel++)
7853
0
  {
7854
0
    long r_type;
7855
0
    unsigned long r_index;
7856
7857
0
    struct elf_link_hash_entry *h;
7858
7859
0
    r_type = ELF32_R_TYPE (irel->r_info);
7860
0
    r_index = ELF32_R_SYM (irel->r_info);
7861
7862
    /* These are the only relocation types we care about.  */
7863
0
    if (   r_type != R_ARM_PC24
7864
0
        && (r_type != R_ARM_V4BX || globals->fix_v4bx < 2))
7865
0
      continue;
7866
7867
    /* Get the section contents if we haven't done so already.  */
7868
0
    if (contents == NULL)
7869
0
      {
7870
        /* Get cached copy if it exists.  */
7871
0
        if (elf_section_data (sec)->this_hdr.contents != NULL)
7872
0
    contents = elf_section_data (sec)->this_hdr.contents;
7873
0
        else
7874
0
    {
7875
      /* Go get them off disk.  */
7876
0
      if (! bfd_malloc_and_get_section (abfd, sec, &contents))
7877
0
        goto error_return;
7878
0
    }
7879
0
      }
7880
7881
0
    if (r_type == R_ARM_V4BX)
7882
0
      {
7883
0
        int reg;
7884
7885
0
        reg = bfd_get_32 (abfd, contents + irel->r_offset) & 0xf;
7886
0
        record_arm_bx_glue (link_info, reg);
7887
0
        continue;
7888
0
      }
7889
7890
    /* If the relocation is not against a symbol it cannot concern us.  */
7891
0
    h = NULL;
7892
7893
    /* We don't care about local symbols.  */
7894
0
    if (r_index < symtab_hdr->sh_info)
7895
0
      continue;
7896
7897
    /* This is an external symbol.  */
7898
0
    r_index -= symtab_hdr->sh_info;
7899
0
    h = (struct elf_link_hash_entry *)
7900
0
      elf_sym_hashes (abfd)[r_index];
7901
7902
    /* If the relocation is against a static symbol it must be within
7903
       the current section and so cannot be a cross ARM/Thumb relocation.  */
7904
0
    if (h == NULL)
7905
0
      continue;
7906
7907
    /* If the call will go through a PLT entry then we do not need
7908
       glue.  */
7909
0
    if (globals->root.splt != NULL && h->plt.offset != (bfd_vma) -1)
7910
0
      continue;
7911
7912
0
    switch (r_type)
7913
0
      {
7914
0
      case R_ARM_PC24:
7915
        /* This one is a call from arm code.  We need to look up
7916
     the target of the call.  If it is a thumb target, we
7917
     insert glue.  */
7918
0
        if (ARM_GET_SYM_BRANCH_TYPE (h->target_internal)
7919
0
      == ST_BRANCH_TO_THUMB)
7920
0
    record_arm_to_thumb_glue (link_info, h);
7921
0
        break;
7922
7923
0
      default:
7924
0
        abort ();
7925
0
      }
7926
0
  }
7927
7928
0
      if (elf_section_data (sec)->this_hdr.contents != contents)
7929
0
  free (contents);
7930
0
      contents = NULL;
7931
7932
0
      if (elf_section_data (sec)->relocs != internal_relocs)
7933
0
  free (internal_relocs);
7934
0
      internal_relocs = NULL;
7935
0
    }
7936
7937
0
  return true;
7938
7939
0
 error_return:
7940
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
7941
0
    free (contents);
7942
0
  if (elf_section_data (sec)->relocs != internal_relocs)
7943
0
    free (internal_relocs);
7944
7945
0
  return false;
7946
0
}
7947
#endif
7948
7949
7950
/* Initialise maps of ARM/Thumb/data for input BFDs.  */
7951
7952
void
7953
bfd_elf32_arm_init_maps (bfd *abfd)
7954
0
{
7955
0
  Elf_Internal_Sym *isymbuf;
7956
0
  Elf_Internal_Shdr *hdr;
7957
0
  unsigned int i, localsyms;
7958
7959
  /* PR 7093: Make sure that we are dealing with an arm elf binary.  */
7960
0
  if (! is_arm_elf (abfd))
7961
0
    return;
7962
7963
0
  if ((abfd->flags & DYNAMIC) != 0)
7964
0
    return;
7965
7966
0
  hdr = & elf_symtab_hdr (abfd);
7967
0
  localsyms = hdr->sh_info;
7968
7969
  /* Obtain a buffer full of symbols for this BFD. The hdr->sh_info field
7970
     should contain the number of local symbols, which should come before any
7971
     global symbols.  Mapping symbols are always local.  */
7972
0
  isymbuf = bfd_elf_get_elf_syms (abfd, hdr, localsyms, 0, NULL, NULL,
7973
0
          NULL);
7974
7975
  /* No internal symbols read?  Skip this BFD.  */
7976
0
  if (isymbuf == NULL)
7977
0
    return;
7978
7979
0
  for (i = 0; i < localsyms; i++)
7980
0
    {
7981
0
      Elf_Internal_Sym *isym = &isymbuf[i];
7982
0
      asection *sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7983
0
      const char *name;
7984
7985
0
      if (sec != NULL
7986
0
    && ELF_ST_BIND (isym->st_info) == STB_LOCAL)
7987
0
  {
7988
0
    name = bfd_elf_string_from_elf_section (abfd,
7989
0
      hdr->sh_link, isym->st_name);
7990
7991
0
    if (bfd_is_arm_special_symbol_name (name,
7992
0
                BFD_ARM_SPECIAL_SYM_TYPE_MAP))
7993
0
      elf32_arm_section_map_add (sec, name[1], isym->st_value);
7994
0
  }
7995
0
    }
7996
0
}
7997
7998
7999
/* Auto-select enabling of Cortex-A8 erratum fix if the user didn't explicitly
8000
   say what they wanted.  */
8001
8002
void
8003
bfd_elf32_arm_set_cortex_a8_fix (bfd *obfd, struct bfd_link_info *link_info)
8004
0
{
8005
0
  struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
8006
0
  obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
8007
8008
0
  if (globals == NULL)
8009
0
    return;
8010
8011
0
  if (globals->fix_cortex_a8 == -1)
8012
0
    {
8013
      /* Turn on Cortex-A8 erratum workaround for ARMv7-A.  */
8014
0
      if (out_attr[Tag_CPU_arch].i == TAG_CPU_ARCH_V7
8015
0
    && (out_attr[Tag_CPU_arch_profile].i == 'A'
8016
0
        || out_attr[Tag_CPU_arch_profile].i == 0))
8017
0
  globals->fix_cortex_a8 = 1;
8018
0
      else
8019
0
  globals->fix_cortex_a8 = 0;
8020
0
    }
8021
0
}
8022
8023
8024
void
8025
bfd_elf32_arm_set_vfp11_fix (bfd *obfd, struct bfd_link_info *link_info)
8026
0
{
8027
0
  struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
8028
0
  obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
8029
8030
0
  if (globals == NULL)
8031
0
    return;
8032
  /* We assume that ARMv7+ does not need the VFP11 denorm erratum fix.  */
8033
0
  if (out_attr[Tag_CPU_arch].i >= TAG_CPU_ARCH_V7)
8034
0
    {
8035
0
      switch (globals->vfp11_fix)
8036
0
  {
8037
0
  case BFD_ARM_VFP11_FIX_DEFAULT:
8038
0
  case BFD_ARM_VFP11_FIX_NONE:
8039
0
    globals->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
8040
0
    break;
8041
8042
0
  default:
8043
    /* Give a warning, but do as the user requests anyway.  */
8044
0
    _bfd_error_handler (_("%pB: warning: selected VFP11 erratum "
8045
0
      "workaround is not necessary for target architecture"), obfd);
8046
0
  }
8047
0
    }
8048
0
  else if (globals->vfp11_fix == BFD_ARM_VFP11_FIX_DEFAULT)
8049
    /* For earlier architectures, we might need the workaround, but do not
8050
       enable it by default.  If users is running with broken hardware, they
8051
       must enable the erratum fix explicitly.  */
8052
0
    globals->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
8053
0
}
8054
8055
void
8056
bfd_elf32_arm_set_stm32l4xx_fix (bfd *obfd, struct bfd_link_info *link_info)
8057
0
{
8058
0
  struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
8059
0
  obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
8060
8061
0
  if (globals == NULL)
8062
0
    return;
8063
8064
  /* We assume only Cortex-M4 may require the fix.  */
8065
0
  if (out_attr[Tag_CPU_arch].i != TAG_CPU_ARCH_V7E_M
8066
0
      || out_attr[Tag_CPU_arch_profile].i != 'M')
8067
0
    {
8068
0
      if (globals->stm32l4xx_fix != BFD_ARM_STM32L4XX_FIX_NONE)
8069
  /* Give a warning, but do as the user requests anyway.  */
8070
0
  _bfd_error_handler
8071
0
    (_("%pB: warning: selected STM32L4XX erratum "
8072
0
       "workaround is not necessary for target architecture"), obfd);
8073
0
    }
8074
0
}
8075
8076
enum bfd_arm_vfp11_pipe
8077
{
8078
  VFP11_FMAC,
8079
  VFP11_LS,
8080
  VFP11_DS,
8081
  VFP11_BAD
8082
};
8083
8084
/* Return a VFP register number.  This is encoded as RX:X for single-precision
8085
   registers, or X:RX for double-precision registers, where RX is the group of
8086
   four bits in the instruction encoding and X is the single extension bit.
8087
   RX and X fields are specified using their lowest (starting) bit.  The return
8088
   value is:
8089
8090
     0...31: single-precision registers s0...s31
8091
     32...63: double-precision registers d0...d31.
8092
8093
   Although X should be zero for VFP11 (encoding d0...d15 only), we might
8094
   encounter VFP3 instructions, so we allow the full range for DP registers.  */
8095
8096
static unsigned int
8097
bfd_arm_vfp11_regno (unsigned int insn, bool is_double, unsigned int rx,
8098
         unsigned int x)
8099
0
{
8100
0
  if (is_double)
8101
0
    return (((insn >> rx) & 0xf) | (((insn >> x) & 1) << 4)) + 32;
8102
0
  else
8103
0
    return (((insn >> rx) & 0xf) << 1) | ((insn >> x) & 1);
8104
0
}
8105
8106
/* Set bits in *WMASK according to a register number REG as encoded by
8107
   bfd_arm_vfp11_regno().  Ignore d16-d31.  */
8108
8109
static void
8110
bfd_arm_vfp11_write_mask (unsigned int *wmask, unsigned int reg)
8111
0
{
8112
0
  if (reg < 32)
8113
0
    *wmask |= 1 << reg;
8114
0
  else if (reg < 48)
8115
0
    *wmask |= 3 << ((reg - 32) * 2);
8116
0
}
8117
8118
/* Return TRUE if WMASK overwrites anything in REGS.  */
8119
8120
static bool
8121
bfd_arm_vfp11_antidependency (unsigned int wmask, int *regs, int numregs)
8122
0
{
8123
0
  int i;
8124
8125
0
  for (i = 0; i < numregs; i++)
8126
0
    {
8127
0
      unsigned int reg = regs[i];
8128
8129
0
      if (reg < 32 && (wmask & (1 << reg)) != 0)
8130
0
  return true;
8131
8132
0
      reg -= 32;
8133
8134
0
      if (reg >= 16)
8135
0
  continue;
8136
8137
0
      if ((wmask & (3 << (reg * 2))) != 0)
8138
0
  return true;
8139
0
    }
8140
8141
0
  return false;
8142
0
}
8143
8144
/* In this function, we're interested in two things: finding input registers
8145
   for VFP data-processing instructions, and finding the set of registers which
8146
   arbitrary VFP instructions may write to.  We use a 32-bit unsigned int to
8147
   hold the written set, so FLDM etc. are easy to deal with (we're only
8148
   interested in 32 SP registers or 16 dp registers, due to the VFP version
8149
   implemented by the chip in question).  DP registers are marked by setting
8150
   both SP registers in the write mask).  */
8151
8152
static enum bfd_arm_vfp11_pipe
8153
bfd_arm_vfp11_insn_decode (unsigned int insn, unsigned int *destmask, int *regs,
8154
         int *numregs)
8155
0
{
8156
0
  enum bfd_arm_vfp11_pipe vpipe = VFP11_BAD;
8157
0
  bool is_double = ((insn & 0xf00) == 0xb00) ? 1 : 0;
8158
8159
0
  if ((insn & 0x0f000e10) == 0x0e000a00)  /* A data-processing insn.  */
8160
0
    {
8161
0
      unsigned int pqrs;
8162
0
      unsigned int fd = bfd_arm_vfp11_regno (insn, is_double, 12, 22);
8163
0
      unsigned int fm = bfd_arm_vfp11_regno (insn, is_double, 0, 5);
8164
8165
0
      pqrs = ((insn & 0x00800000) >> 20)
8166
0
     | ((insn & 0x00300000) >> 19)
8167
0
     | ((insn & 0x00000040) >> 6);
8168
8169
0
      switch (pqrs)
8170
0
  {
8171
0
  case 0: /* fmac[sd].  */
8172
0
  case 1: /* fnmac[sd].  */
8173
0
  case 2: /* fmsc[sd].  */
8174
0
  case 3: /* fnmsc[sd].  */
8175
0
    vpipe = VFP11_FMAC;
8176
0
    bfd_arm_vfp11_write_mask (destmask, fd);
8177
0
    regs[0] = fd;
8178
0
    regs[1] = bfd_arm_vfp11_regno (insn, is_double, 16, 7);  /* Fn.  */
8179
0
    regs[2] = fm;
8180
0
    *numregs = 3;
8181
0
    break;
8182
8183
0
  case 4: /* fmul[sd].  */
8184
0
  case 5: /* fnmul[sd].  */
8185
0
  case 6: /* fadd[sd].  */
8186
0
  case 7: /* fsub[sd].  */
8187
0
    vpipe = VFP11_FMAC;
8188
0
    goto vfp_binop;
8189
8190
0
  case 8: /* fdiv[sd].  */
8191
0
    vpipe = VFP11_DS;
8192
0
    vfp_binop:
8193
0
    bfd_arm_vfp11_write_mask (destmask, fd);
8194
0
    regs[0] = bfd_arm_vfp11_regno (insn, is_double, 16, 7);   /* Fn.  */
8195
0
    regs[1] = fm;
8196
0
    *numregs = 2;
8197
0
    break;
8198
8199
0
  case 15: /* extended opcode.  */
8200
0
    {
8201
0
      unsigned int extn = ((insn >> 15) & 0x1e)
8202
0
            | ((insn >> 7) & 1);
8203
8204
0
      switch (extn)
8205
0
        {
8206
0
        case 0: /* fcpy[sd].  */
8207
0
        case 1: /* fabs[sd].  */
8208
0
        case 2: /* fneg[sd].  */
8209
0
        case 8: /* fcmp[sd].  */
8210
0
        case 9: /* fcmpe[sd].  */
8211
0
        case 10: /* fcmpz[sd].  */
8212
0
        case 11: /* fcmpez[sd].  */
8213
0
        case 16: /* fuito[sd].  */
8214
0
        case 17: /* fsito[sd].  */
8215
0
        case 24: /* ftoui[sd].  */
8216
0
        case 25: /* ftouiz[sd].  */
8217
0
        case 26: /* ftosi[sd].  */
8218
0
        case 27: /* ftosiz[sd].  */
8219
    /* These instructions will not bounce due to underflow.  */
8220
0
    *numregs = 0;
8221
0
    vpipe = VFP11_FMAC;
8222
0
    break;
8223
8224
0
        case 3: /* fsqrt[sd].  */
8225
    /* fsqrt cannot underflow, but it can (perhaps) overwrite
8226
       registers to cause the erratum in previous instructions.  */
8227
0
    bfd_arm_vfp11_write_mask (destmask, fd);
8228
0
    vpipe = VFP11_DS;
8229
0
    break;
8230
8231
0
        case 15: /* fcvt{ds,sd}.  */
8232
0
    {
8233
0
      int rnum = 0;
8234
8235
0
      bfd_arm_vfp11_write_mask (destmask, fd);
8236
8237
      /* Only FCVTSD can underflow.  */
8238
0
      if ((insn & 0x100) != 0)
8239
0
        regs[rnum++] = fm;
8240
8241
0
      *numregs = rnum;
8242
8243
0
      vpipe = VFP11_FMAC;
8244
0
    }
8245
0
    break;
8246
8247
0
        default:
8248
0
    return VFP11_BAD;
8249
0
        }
8250
0
    }
8251
0
    break;
8252
8253
0
  default:
8254
0
    return VFP11_BAD;
8255
0
  }
8256
0
    }
8257
  /* Two-register transfer.  */
8258
0
  else if ((insn & 0x0fe00ed0) == 0x0c400a10)
8259
0
    {
8260
0
      unsigned int fm = bfd_arm_vfp11_regno (insn, is_double, 0, 5);
8261
8262
0
      if ((insn & 0x100000) == 0)
8263
0
  {
8264
0
    if (is_double)
8265
0
      bfd_arm_vfp11_write_mask (destmask, fm);
8266
0
    else
8267
0
      {
8268
0
        bfd_arm_vfp11_write_mask (destmask, fm);
8269
0
        bfd_arm_vfp11_write_mask (destmask, fm + 1);
8270
0
      }
8271
0
  }
8272
8273
0
      vpipe = VFP11_LS;
8274
0
    }
8275
0
  else if ((insn & 0x0e100e00) == 0x0c100a00)  /* A load insn.  */
8276
0
    {
8277
0
      int fd = bfd_arm_vfp11_regno (insn, is_double, 12, 22);
8278
0
      unsigned int puw = ((insn >> 21) & 0x1) | (((insn >> 23) & 3) << 1);
8279
8280
0
      switch (puw)
8281
0
  {
8282
0
  case 0: /* Two-reg transfer.  We should catch these above.  */
8283
0
    abort ();
8284
8285
0
  case 2: /* fldm[sdx].  */
8286
0
  case 3:
8287
0
  case 5:
8288
0
    {
8289
0
      unsigned int i, offset = insn & 0xff;
8290
8291
0
      if (is_double)
8292
0
        offset >>= 1;
8293
8294
0
      for (i = fd; i < fd + offset; i++)
8295
0
        bfd_arm_vfp11_write_mask (destmask, i);
8296
0
    }
8297
0
    break;
8298
8299
0
  case 4: /* fld[sd].  */
8300
0
  case 6:
8301
0
    bfd_arm_vfp11_write_mask (destmask, fd);
8302
0
    break;
8303
8304
0
  default:
8305
0
    return VFP11_BAD;
8306
0
  }
8307
8308
0
      vpipe = VFP11_LS;
8309
0
    }
8310
  /* Single-register transfer. Note L==0.  */
8311
0
  else if ((insn & 0x0f100e10) == 0x0e000a10)
8312
0
    {
8313
0
      unsigned int opcode = (insn >> 21) & 7;
8314
0
      unsigned int fn = bfd_arm_vfp11_regno (insn, is_double, 16, 7);
8315
8316
0
      switch (opcode)
8317
0
  {
8318
0
  case 0: /* fmsr/fmdlr.  */
8319
0
  case 1: /* fmdhr.  */
8320
    /* Mark fmdhr and fmdlr as writing to the whole of the DP
8321
       destination register.  I don't know if this is exactly right,
8322
       but it is the conservative choice.  */
8323
0
    bfd_arm_vfp11_write_mask (destmask, fn);
8324
0
    break;
8325
8326
0
  case 7: /* fmxr.  */
8327
0
    break;
8328
0
  }
8329
8330
0
      vpipe = VFP11_LS;
8331
0
    }
8332
8333
0
  return vpipe;
8334
0
}
8335
8336
8337
static int elf32_arm_compare_mapping (const void * a, const void * b);
8338
8339
8340
/* Look for potentially-troublesome code sequences which might trigger the
8341
   VFP11 denormal/antidependency erratum.  See, e.g., the ARM1136 errata sheet
8342
   (available from ARM) for details of the erratum.  A short version is
8343
   described in ld.texinfo.  */
8344
8345
bool
8346
bfd_elf32_arm_vfp11_erratum_scan (bfd *abfd, struct bfd_link_info *link_info)
8347
0
{
8348
0
  asection *sec;
8349
0
  bfd_byte *contents = NULL;
8350
0
  int state = 0;
8351
0
  int regs[3], numregs = 0;
8352
0
  struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
8353
0
  int use_vector = (globals->vfp11_fix == BFD_ARM_VFP11_FIX_VECTOR);
8354
8355
0
  if (globals == NULL)
8356
0
    return false;
8357
8358
  /* We use a simple FSM to match troublesome VFP11 instruction sequences.
8359
     The states transition as follows:
8360
8361
       0 -> 1 (vector) or 0 -> 2 (scalar)
8362
     A VFP FMAC-pipeline instruction has been seen. Fill
8363
     regs[0]..regs[numregs-1] with its input operands. Remember this
8364
     instruction in 'first_fmac'.
8365
8366
       1 -> 2
8367
     Any instruction, except for a VFP instruction which overwrites
8368
     regs[*].
8369
8370
       1 -> 3 [ -> 0 ]  or
8371
       2 -> 3 [ -> 0 ]
8372
     A VFP instruction has been seen which overwrites any of regs[*].
8373
     We must make a veneer!  Reset state to 0 before examining next
8374
     instruction.
8375
8376
       2 -> 0
8377
     If we fail to match anything in state 2, reset to state 0 and reset
8378
     the instruction pointer to the instruction after 'first_fmac'.
8379
8380
     If the VFP11 vector mode is in use, there must be at least two unrelated
8381
     instructions between anti-dependent VFP11 instructions to properly avoid
8382
     triggering the erratum, hence the use of the extra state 1.  */
8383
8384
  /* If we are only performing a partial link do not bother
8385
     to construct any glue.  */
8386
0
  if (bfd_link_relocatable (link_info))
8387
0
    return true;
8388
8389
  /* Skip if this bfd does not correspond to an ELF image.  */
8390
0
  if (! is_arm_elf (abfd))
8391
0
    return true;
8392
8393
  /* We should have chosen a fix type by the time we get here.  */
8394
0
  BFD_ASSERT (globals->vfp11_fix != BFD_ARM_VFP11_FIX_DEFAULT);
8395
8396
0
  if (globals->vfp11_fix == BFD_ARM_VFP11_FIX_NONE)
8397
0
    return true;
8398
8399
  /* Skip this BFD if it corresponds to an executable or dynamic object.  */
8400
0
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
8401
0
    return true;
8402
8403
0
  for (sec = abfd->sections; sec != NULL; sec = sec->next)
8404
0
    {
8405
0
      unsigned int i, span, first_fmac = 0, veneer_of_insn = 0;
8406
0
      struct _arm_elf_section_data *sec_data;
8407
8408
      /* If we don't have executable progbits, we're not interested in this
8409
   section.  Also skip if section is to be excluded.  */
8410
0
      if (elf_section_type (sec) != SHT_PROGBITS
8411
0
    || (elf_section_flags (sec) & SHF_EXECINSTR) == 0
8412
0
    || (sec->flags & SEC_EXCLUDE) != 0
8413
0
    || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8414
0
    || sec->output_section == bfd_abs_section_ptr
8415
0
    || strcmp (sec->name, VFP11_ERRATUM_VENEER_SECTION_NAME) == 0)
8416
0
  continue;
8417
8418
0
      sec_data = elf32_arm_section_data (sec);
8419
8420
0
      if (sec_data->mapcount == 0)
8421
0
  continue;
8422
8423
0
      if (elf_section_data (sec)->this_hdr.contents != NULL)
8424
0
  contents = elf_section_data (sec)->this_hdr.contents;
8425
0
      else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
8426
0
  goto error_return;
8427
8428
0
      qsort (sec_data->map, sec_data->mapcount, sizeof (elf32_arm_section_map),
8429
0
       elf32_arm_compare_mapping);
8430
8431
0
      for (span = 0; span < sec_data->mapcount; span++)
8432
0
  {
8433
0
    unsigned int span_start = sec_data->map[span].vma;
8434
0
    unsigned int span_end = (span == sec_data->mapcount - 1)
8435
0
          ? sec->size : sec_data->map[span + 1].vma;
8436
0
    char span_type = sec_data->map[span].type;
8437
8438
    /* FIXME: Only ARM mode is supported at present.  We may need to
8439
       support Thumb-2 mode also at some point.  */
8440
0
    if (span_type != 'a')
8441
0
      continue;
8442
8443
0
    for (i = span_start; i < span_end;)
8444
0
      {
8445
0
        unsigned int next_i = i + 4;
8446
0
        unsigned int insn = bfd_big_endian (abfd)
8447
0
    ? (((unsigned) contents[i] << 24)
8448
0
       | (contents[i + 1] << 16)
8449
0
       | (contents[i + 2] << 8)
8450
0
       | contents[i + 3])
8451
0
    : (((unsigned) contents[i + 3] << 24)
8452
0
       | (contents[i + 2] << 16)
8453
0
       | (contents[i + 1] << 8)
8454
0
       | contents[i]);
8455
0
        unsigned int writemask = 0;
8456
0
        enum bfd_arm_vfp11_pipe vpipe;
8457
8458
0
        switch (state)
8459
0
    {
8460
0
    case 0:
8461
0
      vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask, regs,
8462
0
                &numregs);
8463
      /* I'm assuming the VFP11 erratum can trigger with denorm
8464
         operands on either the FMAC or the DS pipeline. This might
8465
         lead to slightly overenthusiastic veneer insertion.  */
8466
0
      if (vpipe == VFP11_FMAC || vpipe == VFP11_DS)
8467
0
        {
8468
0
          state = use_vector ? 1 : 2;
8469
0
          first_fmac = i;
8470
0
          veneer_of_insn = insn;
8471
0
        }
8472
0
      break;
8473
8474
0
    case 1:
8475
0
      {
8476
0
        int other_regs[3], other_numregs;
8477
0
        vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask,
8478
0
                  other_regs,
8479
0
                  &other_numregs);
8480
0
        if (vpipe != VFP11_BAD
8481
0
      && bfd_arm_vfp11_antidependency (writemask, regs,
8482
0
               numregs))
8483
0
          state = 3;
8484
0
        else
8485
0
          state = 2;
8486
0
      }
8487
0
      break;
8488
8489
0
    case 2:
8490
0
      {
8491
0
        int other_regs[3], other_numregs;
8492
0
        vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask,
8493
0
                  other_regs,
8494
0
                  &other_numregs);
8495
0
        if (vpipe != VFP11_BAD
8496
0
      && bfd_arm_vfp11_antidependency (writemask, regs,
8497
0
               numregs))
8498
0
          state = 3;
8499
0
        else
8500
0
          {
8501
0
      state = 0;
8502
0
      next_i = first_fmac + 4;
8503
0
          }
8504
0
      }
8505
0
      break;
8506
8507
0
    case 3:
8508
0
      abort ();  /* Should be unreachable.  */
8509
0
    }
8510
8511
0
        if (state == 3)
8512
0
    {
8513
0
      elf32_vfp11_erratum_list *newerr =(elf32_vfp11_erratum_list *)
8514
0
          bfd_zmalloc (sizeof (elf32_vfp11_erratum_list));
8515
8516
0
      elf32_arm_section_data (sec)->erratumcount += 1;
8517
8518
0
      newerr->u.b.vfp_insn = veneer_of_insn;
8519
8520
0
      switch (span_type)
8521
0
        {
8522
0
        case 'a':
8523
0
          newerr->type = VFP11_ERRATUM_BRANCH_TO_ARM_VENEER;
8524
0
          break;
8525
8526
0
        default:
8527
0
          abort ();
8528
0
        }
8529
8530
0
      record_vfp11_erratum_veneer (link_info, newerr, abfd, sec,
8531
0
                 first_fmac);
8532
8533
0
      newerr->vma = -1;
8534
8535
0
      newerr->next = sec_data->erratumlist;
8536
0
      sec_data->erratumlist = newerr;
8537
8538
0
      state = 0;
8539
0
    }
8540
8541
0
        i = next_i;
8542
0
      }
8543
0
  }
8544
8545
0
      if (elf_section_data (sec)->this_hdr.contents != contents)
8546
0
  free (contents);
8547
0
      contents = NULL;
8548
0
    }
8549
8550
0
  return true;
8551
8552
0
 error_return:
8553
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
8554
0
    free (contents);
8555
8556
0
  return false;
8557
0
}
8558
8559
/* Find virtual-memory addresses for VFP11 erratum veneers and return locations
8560
   after sections have been laid out, using specially-named symbols.  */
8561
8562
void
8563
bfd_elf32_arm_vfp11_fix_veneer_locations (bfd *abfd,
8564
            struct bfd_link_info *link_info)
8565
0
{
8566
0
  asection *sec;
8567
0
  struct elf32_arm_link_hash_table *globals;
8568
0
  char *tmp_name;
8569
8570
0
  if (bfd_link_relocatable (link_info))
8571
0
    return;
8572
8573
  /* Skip if this bfd does not correspond to an ELF image.  */
8574
0
  if (! is_arm_elf (abfd))
8575
0
    return;
8576
8577
0
  globals = elf32_arm_hash_table (link_info);
8578
0
  if (globals == NULL)
8579
0
    return;
8580
8581
0
  tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
8582
0
          (VFP11_ERRATUM_VENEER_ENTRY_NAME) + 10);
8583
0
  BFD_ASSERT (tmp_name);
8584
8585
0
  for (sec = abfd->sections; sec != NULL; sec = sec->next)
8586
0
    {
8587
0
      struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
8588
0
      elf32_vfp11_erratum_list *errnode = sec_data->erratumlist;
8589
8590
0
      for (; errnode != NULL; errnode = errnode->next)
8591
0
  {
8592
0
    struct elf_link_hash_entry *myh;
8593
0
    bfd_vma vma;
8594
8595
0
    switch (errnode->type)
8596
0
      {
8597
0
      case VFP11_ERRATUM_BRANCH_TO_ARM_VENEER:
8598
0
      case VFP11_ERRATUM_BRANCH_TO_THUMB_VENEER:
8599
        /* Find veneer symbol.  */
8600
0
        sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME,
8601
0
           errnode->u.b.veneer->u.v.id);
8602
8603
0
        myh = elf_link_hash_lookup
8604
0
    (&(globals)->root, tmp_name, false, false, true);
8605
8606
0
        if (myh == NULL)
8607
0
    _bfd_error_handler (_("%pB: unable to find %s veneer `%s'"),
8608
0
            abfd, "VFP11", tmp_name);
8609
8610
0
        vma = myh->root.u.def.section->output_section->vma
8611
0
        + myh->root.u.def.section->output_offset
8612
0
        + myh->root.u.def.value;
8613
8614
0
        errnode->u.b.veneer->vma = vma;
8615
0
        break;
8616
8617
0
      case VFP11_ERRATUM_ARM_VENEER:
8618
0
      case VFP11_ERRATUM_THUMB_VENEER:
8619
        /* Find return location.  */
8620
0
        sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME "_r",
8621
0
           errnode->u.v.id);
8622
8623
0
        myh = elf_link_hash_lookup
8624
0
    (&(globals)->root, tmp_name, false, false, true);
8625
8626
0
        if (myh == NULL)
8627
0
    _bfd_error_handler (_("%pB: unable to find %s veneer `%s'"),
8628
0
            abfd, "VFP11", tmp_name);
8629
8630
0
        vma = myh->root.u.def.section->output_section->vma
8631
0
        + myh->root.u.def.section->output_offset
8632
0
        + myh->root.u.def.value;
8633
8634
0
        errnode->u.v.branch->vma = vma;
8635
0
        break;
8636
8637
0
      default:
8638
0
        abort ();
8639
0
      }
8640
0
  }
8641
0
    }
8642
8643
0
  free (tmp_name);
8644
0
}
8645
8646
/* Find virtual-memory addresses for STM32L4XX erratum veneers and
8647
   return locations after sections have been laid out, using
8648
   specially-named symbols.  */
8649
8650
void
8651
bfd_elf32_arm_stm32l4xx_fix_veneer_locations (bfd *abfd,
8652
                struct bfd_link_info *link_info)
8653
0
{
8654
0
  asection *sec;
8655
0
  struct elf32_arm_link_hash_table *globals;
8656
0
  char *tmp_name;
8657
8658
0
  if (bfd_link_relocatable (link_info))
8659
0
    return;
8660
8661
  /* Skip if this bfd does not correspond to an ELF image.  */
8662
0
  if (! is_arm_elf (abfd))
8663
0
    return;
8664
8665
0
  globals = elf32_arm_hash_table (link_info);
8666
0
  if (globals == NULL)
8667
0
    return;
8668
8669
0
  tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
8670
0
          (STM32L4XX_ERRATUM_VENEER_ENTRY_NAME) + 10);
8671
0
  BFD_ASSERT (tmp_name);
8672
8673
0
  for (sec = abfd->sections; sec != NULL; sec = sec->next)
8674
0
    {
8675
0
      struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
8676
0
      elf32_stm32l4xx_erratum_list *errnode = sec_data->stm32l4xx_erratumlist;
8677
8678
0
      for (; errnode != NULL; errnode = errnode->next)
8679
0
  {
8680
0
    struct elf_link_hash_entry *myh;
8681
0
    bfd_vma vma;
8682
8683
0
    switch (errnode->type)
8684
0
      {
8685
0
      case STM32L4XX_ERRATUM_BRANCH_TO_VENEER:
8686
        /* Find veneer symbol.  */
8687
0
        sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME,
8688
0
           errnode->u.b.veneer->u.v.id);
8689
8690
0
        myh = elf_link_hash_lookup
8691
0
    (&(globals)->root, tmp_name, false, false, true);
8692
8693
0
        if (myh == NULL)
8694
0
    _bfd_error_handler (_("%pB: unable to find %s veneer `%s'"),
8695
0
            abfd, "STM32L4XX", tmp_name);
8696
8697
0
        vma = myh->root.u.def.section->output_section->vma
8698
0
    + myh->root.u.def.section->output_offset
8699
0
    + myh->root.u.def.value;
8700
8701
0
        errnode->u.b.veneer->vma = vma;
8702
0
        break;
8703
8704
0
      case STM32L4XX_ERRATUM_VENEER:
8705
        /* Find return location.  */
8706
0
        sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME "_r",
8707
0
           errnode->u.v.id);
8708
8709
0
        myh = elf_link_hash_lookup
8710
0
    (&(globals)->root, tmp_name, false, false, true);
8711
8712
0
        if (myh == NULL)
8713
0
    _bfd_error_handler (_("%pB: unable to find %s veneer `%s'"),
8714
0
            abfd, "STM32L4XX", tmp_name);
8715
8716
0
        vma = myh->root.u.def.section->output_section->vma
8717
0
    + myh->root.u.def.section->output_offset
8718
0
    + myh->root.u.def.value;
8719
8720
0
        errnode->u.v.branch->vma = vma;
8721
0
        break;
8722
8723
0
      default:
8724
0
        abort ();
8725
0
      }
8726
0
  }
8727
0
    }
8728
8729
0
  free (tmp_name);
8730
0
}
8731
8732
static inline bool
8733
is_thumb2_ldmia (const insn32 insn)
8734
0
{
8735
  /* Encoding T2: LDM<c>.W <Rn>{!},<registers>
8736
     1110 - 1000 - 10W1 - rrrr - PM (0) l - llll - llll - llll.  */
8737
0
  return (insn & 0xffd02000) == 0xe8900000;
8738
0
}
8739
8740
static inline bool
8741
is_thumb2_ldmdb (const insn32 insn)
8742
0
{
8743
  /* Encoding T1: LDMDB<c> <Rn>{!},<registers>
8744
     1110 - 1001 - 00W1 - rrrr - PM (0) l - llll - llll - llll.  */
8745
0
  return (insn & 0xffd02000) == 0xe9100000;
8746
0
}
8747
8748
static inline bool
8749
is_thumb2_vldm (const insn32 insn)
8750
0
{
8751
  /* A6.5 Extension register load or store instruction
8752
     A7.7.229
8753
     We look for SP 32-bit and DP 64-bit registers.
8754
     Encoding T1 VLDM{mode}<c> <Rn>{!}, <list>
8755
     <list> is consecutive 64-bit registers
8756
     1110 - 110P - UDW1 - rrrr - vvvv - 1011 - iiii - iiii
8757
     Encoding T2 VLDM{mode}<c> <Rn>{!}, <list>
8758
     <list> is consecutive 32-bit registers
8759
     1110 - 110P - UDW1 - rrrr - vvvv - 1010 - iiii - iiii
8760
     if P==0 && U==1 && W==1 && Rn=1101 VPOP
8761
     if PUW=010 || PUW=011 || PUW=101 VLDM.  */
8762
0
  return
8763
0
    (((insn & 0xfe100f00) == 0xec100b00) ||
8764
0
     ((insn & 0xfe100f00) == 0xec100a00))
8765
0
    && /* (IA without !).  */
8766
0
    (((((insn << 7) >> 28) & 0xd) == 0x4)
8767
     /* (IA with !), includes VPOP (when reg number is SP).  */
8768
0
     || ((((insn << 7) >> 28) & 0xd) == 0x5)
8769
     /* (DB with !).  */
8770
0
     || ((((insn << 7) >> 28) & 0xd) == 0x9));
8771
0
}
8772
8773
/* STM STM32L4XX erratum : This function assumes that it receives an LDM or
8774
   VLDM opcode and:
8775
 - computes the number and the mode of memory accesses
8776
 - decides if the replacement should be done:
8777
   . replaces only if > 8-word accesses
8778
   . or (testing purposes only) replaces all accesses.  */
8779
8780
static bool
8781
stm32l4xx_need_create_replacing_stub (const insn32 insn,
8782
              bfd_arm_stm32l4xx_fix stm32l4xx_fix)
8783
0
{
8784
0
  int nb_words = 0;
8785
8786
  /* The field encoding the register list is the same for both LDMIA
8787
     and LDMDB encodings.  */
8788
0
  if (is_thumb2_ldmia (insn) || is_thumb2_ldmdb (insn))
8789
0
    nb_words = elf32_arm_popcount (insn & 0x0000ffff);
8790
0
  else if (is_thumb2_vldm (insn))
8791
0
   nb_words = (insn & 0xff);
8792
8793
  /* DEFAULT mode accounts for the real bug condition situation,
8794
     ALL mode inserts stubs for each LDM/VLDM instruction (testing).  */
8795
0
  return (stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_DEFAULT
8796
0
    ? nb_words > 8
8797
0
    : stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_ALL);
8798
0
}
8799
8800
/* Look for potentially-troublesome code sequences which might trigger
8801
   the STM STM32L4XX erratum.  */
8802
8803
bool
8804
bfd_elf32_arm_stm32l4xx_erratum_scan (bfd *abfd,
8805
              struct bfd_link_info *link_info)
8806
0
{
8807
0
  asection *sec;
8808
0
  bfd_byte *contents = NULL;
8809
0
  struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
8810
8811
0
  if (globals == NULL)
8812
0
    return false;
8813
8814
  /* If we are only performing a partial link do not bother
8815
     to construct any glue.  */
8816
0
  if (bfd_link_relocatable (link_info))
8817
0
    return true;
8818
8819
  /* Skip if this bfd does not correspond to an ELF image.  */
8820
0
  if (! is_arm_elf (abfd))
8821
0
    return true;
8822
8823
0
  if (globals->stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_NONE)
8824
0
    return true;
8825
8826
  /* Skip this BFD if it corresponds to an executable or dynamic object.  */
8827
0
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
8828
0
    return true;
8829
8830
0
  for (sec = abfd->sections; sec != NULL; sec = sec->next)
8831
0
    {
8832
0
      unsigned int i, span;
8833
0
      struct _arm_elf_section_data *sec_data;
8834
8835
      /* If we don't have executable progbits, we're not interested in this
8836
   section.  Also skip if section is to be excluded.  */
8837
0
      if (elf_section_type (sec) != SHT_PROGBITS
8838
0
    || (elf_section_flags (sec) & SHF_EXECINSTR) == 0
8839
0
    || (sec->flags & SEC_EXCLUDE) != 0
8840
0
    || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8841
0
    || sec->output_section == bfd_abs_section_ptr
8842
0
    || strcmp (sec->name, STM32L4XX_ERRATUM_VENEER_SECTION_NAME) == 0)
8843
0
  continue;
8844
8845
0
      sec_data = elf32_arm_section_data (sec);
8846
8847
0
      if (sec_data->mapcount == 0)
8848
0
  continue;
8849
8850
0
      if (elf_section_data (sec)->this_hdr.contents != NULL)
8851
0
  contents = elf_section_data (sec)->this_hdr.contents;
8852
0
      else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
8853
0
  goto error_return;
8854
8855
0
      qsort (sec_data->map, sec_data->mapcount, sizeof (elf32_arm_section_map),
8856
0
       elf32_arm_compare_mapping);
8857
8858
0
      for (span = 0; span < sec_data->mapcount; span++)
8859
0
  {
8860
0
    unsigned int span_start = sec_data->map[span].vma;
8861
0
    unsigned int span_end = (span == sec_data->mapcount - 1)
8862
0
      ? sec->size : sec_data->map[span + 1].vma;
8863
0
    char span_type = sec_data->map[span].type;
8864
0
    int itblock_current_pos = 0;
8865
8866
    /* Only Thumb2 mode need be supported with this CM4 specific
8867
       code, we should not encounter any arm mode eg span_type
8868
       != 'a'.  */
8869
0
    if (span_type != 't')
8870
0
      continue;
8871
8872
0
    for (i = span_start; i < span_end;)
8873
0
      {
8874
0
        unsigned int insn = bfd_get_16 (abfd, &contents[i]);
8875
0
        bool insn_32bit = false;
8876
0
        bool is_ldm = false;
8877
0
        bool is_vldm = false;
8878
0
        bool is_not_last_in_it_block = false;
8879
8880
        /* The first 16-bits of all 32-bit thumb2 instructions start
8881
     with opcode[15..13]=0b111 and the encoded op1 can be anything
8882
     except opcode[12..11]!=0b00.
8883
     See 32-bit Thumb instruction encoding.  */
8884
0
        if ((insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000)
8885
0
    insn_32bit = true;
8886
8887
        /* Compute the predicate that tells if the instruction
8888
     is concerned by the IT block
8889
     - Creates an error if there is a ldm that is not
8890
       last in the IT block thus cannot be replaced
8891
     - Otherwise we can create a branch at the end of the
8892
       IT block, it will be controlled naturally by IT
8893
       with the proper pseudo-predicate
8894
     - So the only interesting predicate is the one that
8895
       tells that we are not on the last item of an IT
8896
       block.  */
8897
0
        if (itblock_current_pos != 0)
8898
0
      is_not_last_in_it_block = !!--itblock_current_pos;
8899
8900
0
        if (insn_32bit)
8901
0
    {
8902
      /* Load the rest of the insn (in manual-friendly order).  */
8903
0
      insn = (insn << 16) | bfd_get_16 (abfd, &contents[i + 2]);
8904
0
      is_ldm = is_thumb2_ldmia (insn) || is_thumb2_ldmdb (insn);
8905
0
      is_vldm = is_thumb2_vldm (insn);
8906
8907
      /* Veneers are created for (v)ldm depending on
8908
         option flags and memory accesses conditions; but
8909
         if the instruction is not the last instruction of
8910
         an IT block, we cannot create a jump there, so we
8911
         bail out.  */
8912
0
        if ((is_ldm || is_vldm)
8913
0
      && stm32l4xx_need_create_replacing_stub
8914
0
      (insn, globals->stm32l4xx_fix))
8915
0
          {
8916
0
      if (is_not_last_in_it_block)
8917
0
        {
8918
0
          _bfd_error_handler
8919
            /* xgettext:c-format */
8920
0
            (_("%pB(%pA+%#x): error: multiple load detected"
8921
0
         " in non-last IT block instruction:"
8922
0
         " STM32L4XX veneer cannot be generated; "
8923
0
         "use gcc option -mrestrict-it to generate"
8924
0
         " only one instruction per IT block"),
8925
0
             abfd, sec, i);
8926
0
        }
8927
0
      else
8928
0
        {
8929
0
          elf32_stm32l4xx_erratum_list *newerr =
8930
0
            (elf32_stm32l4xx_erratum_list *)
8931
0
            bfd_zmalloc
8932
0
            (sizeof (elf32_stm32l4xx_erratum_list));
8933
8934
0
          elf32_arm_section_data (sec)
8935
0
            ->stm32l4xx_erratumcount += 1;
8936
0
          newerr->u.b.insn = insn;
8937
          /* We create only thumb branches.  */
8938
0
          newerr->type =
8939
0
            STM32L4XX_ERRATUM_BRANCH_TO_VENEER;
8940
0
          record_stm32l4xx_erratum_veneer
8941
0
            (link_info, newerr, abfd, sec,
8942
0
             i,
8943
0
             is_ldm ?
8944
0
             STM32L4XX_ERRATUM_LDM_VENEER_SIZE:
8945
0
             STM32L4XX_ERRATUM_VLDM_VENEER_SIZE);
8946
0
          newerr->vma = -1;
8947
0
          newerr->next = sec_data->stm32l4xx_erratumlist;
8948
0
          sec_data->stm32l4xx_erratumlist = newerr;
8949
0
        }
8950
0
          }
8951
0
    }
8952
0
        else
8953
0
    {
8954
      /* A7.7.37 IT p208
8955
         IT blocks are only encoded in T1
8956
         Encoding T1: IT{x{y{z}}} <firstcond>
8957
         1 0 1 1 - 1 1 1 1 - firstcond - mask
8958
         if mask = '0000' then see 'related encodings'
8959
         We don't deal with UNPREDICTABLE, just ignore these.
8960
         There can be no nested IT blocks so an IT block
8961
         is naturally a new one for which it is worth
8962
         computing its size.  */
8963
0
      bool is_newitblock = ((insn & 0xff00) == 0xbf00)
8964
0
        && ((insn & 0x000f) != 0x0000);
8965
      /* If we have a new IT block we compute its size.  */
8966
0
      if (is_newitblock)
8967
0
        {
8968
          /* Compute the number of instructions controlled
8969
       by the IT block, it will be used to decide
8970
       whether we are inside an IT block or not.  */
8971
0
          unsigned int mask = insn & 0x000f;
8972
0
          itblock_current_pos = 4 - ctz (mask);
8973
0
        }
8974
0
    }
8975
8976
0
        i += insn_32bit ? 4 : 2;
8977
0
      }
8978
0
  }
8979
8980
0
      if (elf_section_data (sec)->this_hdr.contents != contents)
8981
0
  free (contents);
8982
0
      contents = NULL;
8983
0
    }
8984
8985
0
  return true;
8986
8987
0
 error_return:
8988
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
8989
0
    free (contents);
8990
8991
0
  return false;
8992
0
}
8993
8994
/* Set target relocation values needed during linking.  */
8995
8996
void
8997
bfd_elf32_arm_set_target_params (struct bfd *output_bfd,
8998
         struct bfd_link_info *link_info,
8999
         struct elf32_arm_params *params)
9000
0
{
9001
0
  struct elf32_arm_link_hash_table *globals;
9002
9003
0
  globals = elf32_arm_hash_table (link_info);
9004
0
  if (globals == NULL)
9005
0
    return;
9006
9007
0
  globals->target1_is_rel = params->target1_is_rel;
9008
0
  if (globals->fdpic_p)
9009
0
    globals->target2_reloc = R_ARM_GOT32;
9010
0
  else if (strcmp (params->target2_type, "rel") == 0)
9011
0
    globals->target2_reloc = R_ARM_REL32;
9012
0
  else if (strcmp (params->target2_type, "abs") == 0)
9013
0
    globals->target2_reloc = R_ARM_ABS32;
9014
0
  else if (strcmp (params->target2_type, "got-rel") == 0)
9015
0
    globals->target2_reloc = R_ARM_GOT_PREL;
9016
0
  else
9017
0
    {
9018
0
      _bfd_error_handler (_("invalid TARGET2 relocation type '%s'"),
9019
0
        params->target2_type);
9020
0
    }
9021
0
  globals->fix_v4bx = params->fix_v4bx;
9022
0
  globals->use_blx |= params->use_blx;
9023
0
  globals->vfp11_fix = params->vfp11_denorm_fix;
9024
0
  globals->stm32l4xx_fix = params->stm32l4xx_fix;
9025
0
  if (globals->fdpic_p)
9026
0
    globals->pic_veneer = 1;
9027
0
  else
9028
0
    globals->pic_veneer = params->pic_veneer;
9029
0
  globals->fix_cortex_a8 = params->fix_cortex_a8;
9030
0
  globals->fix_arm1176 = params->fix_arm1176;
9031
0
  globals->cmse_implib = params->cmse_implib;
9032
0
  globals->in_implib_bfd = params->in_implib_bfd;
9033
9034
0
  BFD_ASSERT (is_arm_elf (output_bfd));
9035
0
  elf_arm_tdata (output_bfd)->no_enum_size_warning
9036
0
    = params->no_enum_size_warning;
9037
0
  elf_arm_tdata (output_bfd)->no_wchar_size_warning
9038
0
    = params->no_wchar_size_warning;
9039
0
}
9040
9041
/* Replace the target offset of a Thumb bl or b.w instruction.  */
9042
9043
static void
9044
insert_thumb_branch (bfd *abfd, long int offset, bfd_byte *insn)
9045
0
{
9046
0
  bfd_vma upper;
9047
0
  bfd_vma lower;
9048
0
  int reloc_sign;
9049
9050
0
  BFD_ASSERT ((offset & 1) == 0);
9051
9052
0
  upper = bfd_get_16 (abfd, insn);
9053
0
  lower = bfd_get_16 (abfd, insn + 2);
9054
0
  reloc_sign = (offset < 0) ? 1 : 0;
9055
0
  upper = (upper & ~(bfd_vma) 0x7ff)
9056
0
    | ((offset >> 12) & 0x3ff)
9057
0
    | (reloc_sign << 10);
9058
0
  lower = (lower & ~(bfd_vma) 0x2fff)
9059
0
    | (((!((offset >> 23) & 1)) ^ reloc_sign) << 13)
9060
0
    | (((!((offset >> 22) & 1)) ^ reloc_sign) << 11)
9061
0
    | ((offset >> 1) & 0x7ff);
9062
0
  bfd_put_16 (abfd, upper, insn);
9063
0
  bfd_put_16 (abfd, lower, insn + 2);
9064
0
}
9065
9066
/* Thumb code calling an ARM function.  */
9067
9068
static int
9069
elf32_thumb_to_arm_stub (struct bfd_link_info * info,
9070
       const char *   name,
9071
       bfd *      input_bfd,
9072
       bfd *      output_bfd,
9073
       asection *   input_section,
9074
       bfd_byte *   hit_data,
9075
       asection *   sym_sec,
9076
       bfd_vma    offset,
9077
       bfd_signed_vma   addend,
9078
       bfd_vma    val,
9079
       char **error_message)
9080
0
{
9081
0
  asection * s = 0;
9082
0
  bfd_vma my_offset;
9083
0
  long int ret_offset;
9084
0
  struct elf_link_hash_entry * myh;
9085
0
  struct elf32_arm_link_hash_table * globals;
9086
9087
0
  myh = find_thumb_glue (info, name, error_message);
9088
0
  if (myh == NULL)
9089
0
    return false;
9090
9091
0
  globals = elf32_arm_hash_table (info);
9092
0
  BFD_ASSERT (globals != NULL);
9093
0
  BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
9094
9095
0
  my_offset = myh->root.u.def.value;
9096
9097
0
  s = bfd_get_linker_section (globals->bfd_of_glue_owner,
9098
0
            THUMB2ARM_GLUE_SECTION_NAME);
9099
9100
0
  BFD_ASSERT (s != NULL);
9101
0
  BFD_ASSERT (s->contents != NULL);
9102
0
  BFD_ASSERT (s->output_section != NULL);
9103
9104
0
  if ((my_offset & 0x01) == 0x01)
9105
0
    {
9106
0
      if (sym_sec != NULL
9107
0
    && sym_sec->owner != NULL
9108
0
    && !INTERWORK_FLAG (sym_sec->owner))
9109
0
  {
9110
0
    _bfd_error_handler
9111
0
      (_("%pB(%s): warning: interworking not enabled;"
9112
0
         " first occurrence: %pB: %s call to %s"),
9113
0
       sym_sec->owner, name, input_bfd, "Thumb", "ARM");
9114
9115
0
    return false;
9116
0
  }
9117
9118
0
      --my_offset;
9119
0
      myh->root.u.def.value = my_offset;
9120
9121
0
      put_thumb_insn (globals, output_bfd, (bfd_vma) t2a1_bx_pc_insn,
9122
0
          s->contents + my_offset);
9123
9124
0
      put_thumb_insn (globals, output_bfd, (bfd_vma) t2a2_noop_insn,
9125
0
          s->contents + my_offset + 2);
9126
9127
0
      ret_offset =
9128
  /* Address of destination of the stub.  */
9129
0
  ((bfd_signed_vma) val)
9130
0
  - ((bfd_signed_vma)
9131
     /* Offset from the start of the current section
9132
        to the start of the stubs.  */
9133
0
     (s->output_offset
9134
      /* Offset of the start of this stub from the start of the stubs.  */
9135
0
      + my_offset
9136
      /* Address of the start of the current section.  */
9137
0
      + s->output_section->vma)
9138
     /* The branch instruction is 4 bytes into the stub.  */
9139
0
     + 4
9140
     /* ARM branches work from the pc of the instruction + 8.  */
9141
0
     + 8);
9142
9143
0
      put_arm_insn (globals, output_bfd,
9144
0
        (bfd_vma) t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF),
9145
0
        s->contents + my_offset + 4);
9146
0
    }
9147
9148
0
  BFD_ASSERT (my_offset <= globals->thumb_glue_size);
9149
9150
  /* Now go back and fix up the original BL insn to point to here.  */
9151
0
  ret_offset =
9152
    /* Address of where the stub is located.  */
9153
0
    (s->output_section->vma + s->output_offset + my_offset)
9154
     /* Address of where the BL is located.  */
9155
0
    - (input_section->output_section->vma + input_section->output_offset
9156
0
       + offset)
9157
    /* Addend in the relocation.  */
9158
0
    - addend
9159
    /* Biassing for PC-relative addressing.  */
9160
0
    - 8;
9161
9162
0
  insert_thumb_branch (input_bfd, ret_offset, hit_data - input_section->vma);
9163
9164
0
  return true;
9165
0
}
9166
9167
/* Populate an Arm to Thumb stub.  Returns the stub symbol.  */
9168
9169
static struct elf_link_hash_entry *
9170
elf32_arm_create_thumb_stub (struct bfd_link_info * info,
9171
           const char *     name,
9172
           bfd *        input_bfd,
9173
           bfd *        output_bfd,
9174
           asection *       sym_sec,
9175
           bfd_vma        val,
9176
           asection *       s,
9177
           char **        error_message)
9178
0
{
9179
0
  bfd_vma my_offset;
9180
0
  long int ret_offset;
9181
0
  struct elf_link_hash_entry * myh;
9182
0
  struct elf32_arm_link_hash_table * globals;
9183
9184
0
  myh = find_arm_glue (info, name, error_message);
9185
0
  if (myh == NULL)
9186
0
    return NULL;
9187
9188
0
  globals = elf32_arm_hash_table (info);
9189
0
  BFD_ASSERT (globals != NULL);
9190
0
  BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
9191
9192
0
  my_offset = myh->root.u.def.value;
9193
9194
0
  if ((my_offset & 0x01) == 0x01)
9195
0
    {
9196
0
      if (sym_sec != NULL
9197
0
    && sym_sec->owner != NULL
9198
0
    && !INTERWORK_FLAG (sym_sec->owner))
9199
0
  {
9200
0
    _bfd_error_handler
9201
0
      (_("%pB(%s): warning: interworking not enabled;"
9202
0
         " first occurrence: %pB: %s call to %s"),
9203
0
       sym_sec->owner, name, input_bfd, "ARM", "Thumb");
9204
0
  }
9205
9206
0
      --my_offset;
9207
0
      myh->root.u.def.value = my_offset;
9208
9209
0
      if (bfd_link_pic (info)
9210
0
    || globals->pic_veneer)
9211
0
  {
9212
    /* For relocatable objects we can't use absolute addresses,
9213
       so construct the address from a relative offset.  */
9214
    /* TODO: If the offset is small it's probably worth
9215
       constructing the address with adds.  */
9216
0
    put_arm_insn (globals, output_bfd, (bfd_vma) a2t1p_ldr_insn,
9217
0
      s->contents + my_offset);
9218
0
    put_arm_insn (globals, output_bfd, (bfd_vma) a2t2p_add_pc_insn,
9219
0
      s->contents + my_offset + 4);
9220
0
    put_arm_insn (globals, output_bfd, (bfd_vma) a2t3p_bx_r12_insn,
9221
0
      s->contents + my_offset + 8);
9222
    /* Adjust the offset by 4 for the position of the add,
9223
       and 8 for the pipeline offset.  */
9224
0
    ret_offset = (val - (s->output_offset
9225
0
             + s->output_section->vma
9226
0
             + my_offset + 12))
9227
0
           | 1;
9228
0
    bfd_put_32 (output_bfd, ret_offset,
9229
0
          s->contents + my_offset + 12);
9230
0
  }
9231
0
      else if (globals->use_blx)
9232
0
  {
9233
0
    put_arm_insn (globals, output_bfd, (bfd_vma) a2t1v5_ldr_insn,
9234
0
      s->contents + my_offset);
9235
9236
    /* It's a thumb address.  Add the low order bit.  */
9237
0
    bfd_put_32 (output_bfd, val | a2t2v5_func_addr_insn,
9238
0
          s->contents + my_offset + 4);
9239
0
  }
9240
0
      else
9241
0
  {
9242
0
    put_arm_insn (globals, output_bfd, (bfd_vma) a2t1_ldr_insn,
9243
0
      s->contents + my_offset);
9244
9245
0
    put_arm_insn (globals, output_bfd, (bfd_vma) a2t2_bx_r12_insn,
9246
0
      s->contents + my_offset + 4);
9247
9248
    /* It's a thumb address.  Add the low order bit.  */
9249
0
    bfd_put_32 (output_bfd, val | a2t3_func_addr_insn,
9250
0
          s->contents + my_offset + 8);
9251
9252
0
    my_offset += 12;
9253
0
  }
9254
0
    }
9255
9256
0
  BFD_ASSERT (my_offset <= globals->arm_glue_size);
9257
9258
0
  return myh;
9259
0
}
9260
9261
/* Arm code calling a Thumb function.  */
9262
9263
static int
9264
elf32_arm_to_thumb_stub (struct bfd_link_info * info,
9265
       const char *   name,
9266
       bfd *      input_bfd,
9267
       bfd *      output_bfd,
9268
       asection *   input_section,
9269
       bfd_byte *   hit_data,
9270
       asection *   sym_sec,
9271
       bfd_vma    offset,
9272
       bfd_signed_vma   addend,
9273
       bfd_vma    val,
9274
       char **error_message)
9275
0
{
9276
0
  unsigned long int tmp;
9277
0
  bfd_vma my_offset;
9278
0
  asection * s;
9279
0
  long int ret_offset;
9280
0
  struct elf_link_hash_entry * myh;
9281
0
  struct elf32_arm_link_hash_table * globals;
9282
9283
0
  globals = elf32_arm_hash_table (info);
9284
0
  BFD_ASSERT (globals != NULL);
9285
0
  BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
9286
9287
0
  s = bfd_get_linker_section (globals->bfd_of_glue_owner,
9288
0
            ARM2THUMB_GLUE_SECTION_NAME);
9289
0
  BFD_ASSERT (s != NULL);
9290
0
  BFD_ASSERT (s->contents != NULL);
9291
0
  BFD_ASSERT (s->output_section != NULL);
9292
9293
0
  myh = elf32_arm_create_thumb_stub (info, name, input_bfd, output_bfd,
9294
0
             sym_sec, val, s, error_message);
9295
0
  if (!myh)
9296
0
    return false;
9297
9298
0
  my_offset = myh->root.u.def.value;
9299
0
  tmp = bfd_get_32 (input_bfd, hit_data);
9300
0
  tmp = tmp & 0xFF000000;
9301
9302
  /* Somehow these are both 4 too far, so subtract 8.  */
9303
0
  ret_offset = (s->output_offset
9304
0
    + my_offset
9305
0
    + s->output_section->vma
9306
0
    - (input_section->output_offset
9307
0
       + input_section->output_section->vma
9308
0
       + offset + addend)
9309
0
    - 8);
9310
9311
0
  tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF);
9312
9313
0
  bfd_put_32 (output_bfd, (bfd_vma) tmp, hit_data - input_section->vma);
9314
9315
0
  return true;
9316
0
}
9317
9318
/* Populate Arm stub for an exported Thumb function.  */
9319
9320
static bool
9321
elf32_arm_to_thumb_export_stub (struct elf_link_hash_entry *h, void * inf)
9322
0
{
9323
0
  struct bfd_link_info * info = (struct bfd_link_info *) inf;
9324
0
  asection * s;
9325
0
  struct elf_link_hash_entry * myh;
9326
0
  struct elf32_arm_link_hash_entry *eh;
9327
0
  struct elf32_arm_link_hash_table * globals;
9328
0
  asection *sec;
9329
0
  bfd_vma val;
9330
0
  char *error_message;
9331
9332
0
  eh = elf32_arm_hash_entry (h);
9333
  /* Allocate stubs for exported Thumb functions on v4t.  */
9334
0
  if (eh->export_glue == NULL)
9335
0
    return true;
9336
9337
0
  globals = elf32_arm_hash_table (info);
9338
0
  BFD_ASSERT (globals != NULL);
9339
0
  BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
9340
9341
0
  s = bfd_get_linker_section (globals->bfd_of_glue_owner,
9342
0
            ARM2THUMB_GLUE_SECTION_NAME);
9343
0
  BFD_ASSERT (s != NULL);
9344
0
  BFD_ASSERT (s->contents != NULL);
9345
0
  BFD_ASSERT (s->output_section != NULL);
9346
9347
0
  sec = eh->export_glue->root.u.def.section;
9348
9349
0
  BFD_ASSERT (sec->output_section != NULL);
9350
9351
0
  val = eh->export_glue->root.u.def.value + sec->output_offset
9352
0
  + sec->output_section->vma;
9353
9354
0
  myh = elf32_arm_create_thumb_stub (info, h->root.root.string,
9355
0
             h->root.u.def.section->owner,
9356
0
             globals->obfd, sec, val, s,
9357
0
             &error_message);
9358
0
  BFD_ASSERT (myh);
9359
0
  return true;
9360
0
}
9361
9362
/* Populate ARMv4 BX veneers.  Returns the absolute adress of the veneer.  */
9363
9364
static bfd_vma
9365
elf32_arm_bx_glue (struct bfd_link_info * info, int reg)
9366
0
{
9367
0
  bfd_byte *p;
9368
0
  bfd_vma glue_addr;
9369
0
  asection *s;
9370
0
  struct elf32_arm_link_hash_table *globals;
9371
9372
0
  globals = elf32_arm_hash_table (info);
9373
0
  BFD_ASSERT (globals != NULL);
9374
0
  BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
9375
9376
0
  s = bfd_get_linker_section (globals->bfd_of_glue_owner,
9377
0
            ARM_BX_GLUE_SECTION_NAME);
9378
0
  BFD_ASSERT (s != NULL);
9379
0
  BFD_ASSERT (s->contents != NULL);
9380
0
  BFD_ASSERT (s->output_section != NULL);
9381
9382
0
  BFD_ASSERT (globals->bx_glue_offset[reg] & 2);
9383
9384
0
  glue_addr = globals->bx_glue_offset[reg] & ~(bfd_vma)3;
9385
9386
0
  if ((globals->bx_glue_offset[reg] & 1) == 0)
9387
0
    {
9388
0
      p = s->contents + glue_addr;
9389
0
      bfd_put_32 (globals->obfd, armbx1_tst_insn + (reg << 16), p);
9390
0
      bfd_put_32 (globals->obfd, armbx2_moveq_insn + reg, p + 4);
9391
0
      bfd_put_32 (globals->obfd, armbx3_bx_insn + reg, p + 8);
9392
0
      globals->bx_glue_offset[reg] |= 1;
9393
0
    }
9394
9395
0
  return glue_addr + s->output_section->vma + s->output_offset;
9396
0
}
9397
9398
/* Generate Arm stubs for exported Thumb symbols.  */
9399
static void
9400
elf32_arm_begin_write_processing (bfd *abfd ATTRIBUTE_UNUSED,
9401
          struct bfd_link_info *link_info)
9402
3
{
9403
3
  struct elf32_arm_link_hash_table * globals;
9404
9405
3
  if (link_info == NULL)
9406
    /* Ignore this if we are not called by the ELF backend linker.  */
9407
3
    return;
9408
9409
0
  globals = elf32_arm_hash_table (link_info);
9410
0
  if (globals == NULL)
9411
0
    return;
9412
9413
  /* If blx is available then exported Thumb symbols are OK and there is
9414
     nothing to do.  */
9415
0
  if (globals->use_blx)
9416
0
    return;
9417
9418
0
  elf_link_hash_traverse (&globals->root, elf32_arm_to_thumb_export_stub,
9419
0
        link_info);
9420
0
}
9421
9422
/* Reserve space for COUNT dynamic relocations in relocation selection
9423
   SRELOC.  */
9424
9425
static void
9426
elf32_arm_allocate_dynrelocs (struct bfd_link_info *info, asection *sreloc,
9427
            bfd_size_type count)
9428
0
{
9429
0
  struct elf32_arm_link_hash_table *htab;
9430
9431
0
  htab = elf32_arm_hash_table (info);
9432
0
  BFD_ASSERT (htab->root.dynamic_sections_created);
9433
0
  if (sreloc == NULL)
9434
0
    abort ();
9435
0
  sreloc->size += RELOC_SIZE (htab) * count;
9436
0
}
9437
9438
/* Reserve space for COUNT R_ARM_IRELATIVE relocations.  If the link is
9439
   dynamic, the relocations should go in SRELOC, otherwise they should
9440
   go in the special .rel.iplt section.  */
9441
9442
static void
9443
elf32_arm_allocate_irelocs (struct bfd_link_info *info, asection *sreloc,
9444
          bfd_size_type count)
9445
0
{
9446
0
  struct elf32_arm_link_hash_table *htab;
9447
9448
0
  htab = elf32_arm_hash_table (info);
9449
0
  if (!htab->root.dynamic_sections_created)
9450
0
    htab->root.irelplt->size += RELOC_SIZE (htab) * count;
9451
0
  else
9452
0
    {
9453
0
      BFD_ASSERT (sreloc != NULL);
9454
0
      sreloc->size += RELOC_SIZE (htab) * count;
9455
0
    }
9456
0
}
9457
9458
/* Add relocation REL to the end of relocation section SRELOC.  */
9459
9460
static void
9461
elf32_arm_add_dynreloc (bfd *output_bfd, struct bfd_link_info *info,
9462
      asection *sreloc, Elf_Internal_Rela *rel)
9463
0
{
9464
0
  bfd_byte *loc;
9465
0
  struct elf32_arm_link_hash_table *htab;
9466
9467
0
  htab = elf32_arm_hash_table (info);
9468
0
  if (!htab->root.dynamic_sections_created
9469
0
      && ELF32_R_TYPE (rel->r_info) == R_ARM_IRELATIVE)
9470
0
    sreloc = htab->root.irelplt;
9471
0
  if (sreloc == NULL)
9472
0
    abort ();
9473
0
  loc = sreloc->contents;
9474
0
  loc += sreloc->reloc_count++ * RELOC_SIZE (htab);
9475
0
  if (sreloc->reloc_count * RELOC_SIZE (htab) > sreloc->size)
9476
0
    abort ();
9477
0
  SWAP_RELOC_OUT (htab) (output_bfd, rel, loc);
9478
0
}
9479
9480
/* Allocate room for a PLT entry described by ROOT_PLT and ARM_PLT.
9481
   IS_IPLT_ENTRY says whether the entry belongs to .iplt rather than
9482
   to .plt.  */
9483
9484
static void
9485
elf32_arm_allocate_plt_entry (struct bfd_link_info *info,
9486
            bool is_iplt_entry,
9487
            union gotplt_union *root_plt,
9488
            struct arm_plt_info *arm_plt)
9489
0
{
9490
0
  struct elf32_arm_link_hash_table *htab;
9491
0
  asection *splt;
9492
0
  asection *sgotplt;
9493
9494
0
  htab = elf32_arm_hash_table (info);
9495
9496
0
  if (is_iplt_entry)
9497
0
    {
9498
0
      splt = htab->root.iplt;
9499
0
      sgotplt = htab->root.igotplt;
9500
9501
      /* Allocate room for an R_ARM_IRELATIVE relocation in .rel.iplt.  */
9502
0
      elf32_arm_allocate_irelocs (info, htab->root.irelplt, 1);
9503
0
    }
9504
0
  else
9505
0
    {
9506
0
      splt = htab->root.splt;
9507
0
      sgotplt = htab->root.sgotplt;
9508
9509
0
    if (htab->fdpic_p)
9510
0
      {
9511
  /* Allocate room for R_ARM_FUNCDESC_VALUE.  */
9512
  /* For lazy binding, relocations will be put into .rel.plt, in
9513
     .rel.got otherwise.  */
9514
  /* FIXME: today we don't support lazy binding so put it in .rel.got */
9515
0
  if (info->flags & DF_BIND_NOW)
9516
0
    elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
9517
0
  else
9518
0
    elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
9519
0
      }
9520
0
    else
9521
0
      {
9522
  /* Allocate room for an R_JUMP_SLOT relocation in .rel.plt.  */
9523
0
  elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
9524
0
      }
9525
9526
      /* If this is the first .plt entry, make room for the special
9527
   first entry.  */
9528
0
      if (splt->size == 0)
9529
0
  splt->size += htab->plt_header_size;
9530
9531
0
      htab->next_tls_desc_index++;
9532
0
    }
9533
9534
  /* Allocate the PLT entry itself, including any leading Thumb stub.  */
9535
0
  if (elf32_arm_plt_needs_thumb_stub_p (info, arm_plt))
9536
0
    splt->size += PLT_THUMB_STUB_SIZE;
9537
0
  root_plt->offset = splt->size;
9538
0
  splt->size += htab->plt_entry_size;
9539
9540
  /* We also need to make an entry in the .got.plt section, which
9541
     will be placed in the .got section by the linker script.  */
9542
0
  if (is_iplt_entry)
9543
0
    arm_plt->got_offset = sgotplt->size;
9544
0
  else
9545
0
    arm_plt->got_offset = sgotplt->size - 8 * htab->num_tls_desc;
9546
0
  if (htab->fdpic_p)
9547
    /* Function descriptor takes 64 bits in GOT.  */
9548
0
    sgotplt->size += 8;
9549
0
  else
9550
0
    sgotplt->size += 4;
9551
0
}
9552
9553
/* Fill in a PLT entry and its associated GOT slot.  If DYNINDX == -1,
9554
   the entry lives in .iplt and resolves to (*SYM_VALUE)().
9555
   Otherwise, DYNINDX is the index of the symbol in the dynamic
9556
   symbol table and SYM_VALUE is undefined.
9557
9558
   ROOT_PLT points to the offset of the PLT entry from the start of its
9559
   section (.iplt or .plt).  ARM_PLT points to the symbol's ARM-specific
9560
   bookkeeping information.
9561
9562
   Returns FALSE if there was a problem.  */
9563
9564
static bool
9565
elf32_arm_populate_plt_entry (bfd *output_bfd, struct bfd_link_info *info,
9566
            union gotplt_union *root_plt,
9567
            struct arm_plt_info *arm_plt,
9568
            int dynindx, bfd_vma sym_value)
9569
0
{
9570
0
  struct elf32_arm_link_hash_table *htab;
9571
0
  asection *sgot;
9572
0
  asection *splt;
9573
0
  asection *srel;
9574
0
  bfd_byte *loc;
9575
0
  bfd_vma plt_index;
9576
0
  Elf_Internal_Rela rel;
9577
0
  bfd_vma got_header_size;
9578
9579
0
  htab = elf32_arm_hash_table (info);
9580
9581
  /* Pick the appropriate sections and sizes.  */
9582
0
  if (dynindx == -1)
9583
0
    {
9584
0
      splt = htab->root.iplt;
9585
0
      sgot = htab->root.igotplt;
9586
0
      srel = htab->root.irelplt;
9587
9588
      /* There are no reserved entries in .igot.plt, and no special
9589
   first entry in .iplt.  */
9590
0
      got_header_size = 0;
9591
0
    }
9592
0
  else
9593
0
    {
9594
0
      splt = htab->root.splt;
9595
0
      sgot = htab->root.sgotplt;
9596
0
      srel = htab->root.srelplt;
9597
9598
0
      got_header_size = get_elf_backend_data (output_bfd)->got_header_size;
9599
0
    }
9600
0
  BFD_ASSERT (splt != NULL && srel != NULL);
9601
9602
0
  bfd_vma got_offset, got_address, plt_address;
9603
0
  bfd_vma got_displacement, initial_got_entry;
9604
0
  bfd_byte * ptr;
9605
9606
0
  BFD_ASSERT (sgot != NULL);
9607
9608
  /* Get the offset into the .(i)got.plt table of the entry that
9609
     corresponds to this function.  */
9610
0
  got_offset = (arm_plt->got_offset & -2);
9611
9612
  /* Get the index in the procedure linkage table which
9613
     corresponds to this symbol.  This is the index of this symbol
9614
     in all the symbols for which we are making plt entries.
9615
     After the reserved .got.plt entries, all symbols appear in
9616
     the same order as in .plt.  */
9617
0
  if (htab->fdpic_p)
9618
    /* Function descriptor takes 8 bytes.  */
9619
0
    plt_index = (got_offset - got_header_size) / 8;
9620
0
  else
9621
0
    plt_index = (got_offset - got_header_size) / 4;
9622
9623
  /* Calculate the address of the GOT entry.  */
9624
0
  got_address = (sgot->output_section->vma
9625
0
     + sgot->output_offset
9626
0
     + got_offset);
9627
9628
  /* ...and the address of the PLT entry.  */
9629
0
  plt_address = (splt->output_section->vma
9630
0
     + splt->output_offset
9631
0
     + root_plt->offset);
9632
9633
0
  ptr = splt->contents + root_plt->offset;
9634
0
  if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
9635
0
    {
9636
0
      unsigned int i;
9637
0
      bfd_vma val;
9638
9639
0
      for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
9640
0
  {
9641
0
    val = elf32_arm_vxworks_shared_plt_entry[i];
9642
0
    if (i == 2)
9643
0
      val |= got_address - sgot->output_section->vma;
9644
0
    if (i == 5)
9645
0
      val |= plt_index * RELOC_SIZE (htab);
9646
0
    if (i == 2 || i == 5)
9647
0
      bfd_put_32 (output_bfd, val, ptr);
9648
0
    else
9649
0
      put_arm_insn (htab, output_bfd, val, ptr);
9650
0
  }
9651
0
    }
9652
0
  else if (htab->root.target_os == is_vxworks)
9653
0
    {
9654
0
      unsigned int i;
9655
0
      bfd_vma val;
9656
9657
0
      for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
9658
0
  {
9659
0
    val = elf32_arm_vxworks_exec_plt_entry[i];
9660
0
    if (i == 2)
9661
0
      val |= got_address;
9662
0
    if (i == 4)
9663
0
      val |= 0xffffff & -((root_plt->offset + i * 4 + 8) >> 2);
9664
0
    if (i == 5)
9665
0
      val |= plt_index * RELOC_SIZE (htab);
9666
0
    if (i == 2 || i == 5)
9667
0
      bfd_put_32 (output_bfd, val, ptr);
9668
0
    else
9669
0
      put_arm_insn (htab, output_bfd, val, ptr);
9670
0
  }
9671
9672
0
      loc = (htab->srelplt2->contents
9673
0
       + (plt_index * 2 + 1) * RELOC_SIZE (htab));
9674
9675
      /* Create the .rela.plt.unloaded R_ARM_ABS32 relocation
9676
   referencing the GOT for this PLT entry.  */
9677
0
      rel.r_offset = plt_address + 8;
9678
0
      rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
9679
0
      rel.r_addend = got_offset;
9680
0
      SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
9681
0
      loc += RELOC_SIZE (htab);
9682
9683
      /* Create the R_ARM_ABS32 relocation referencing the
9684
   beginning of the PLT for this GOT entry.  */
9685
0
      rel.r_offset = got_address;
9686
0
      rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
9687
0
      rel.r_addend = 0;
9688
0
      SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
9689
0
    }
9690
0
  else if (htab->fdpic_p)
9691
0
    {
9692
0
      const bfd_vma *plt_entry = using_thumb_only (htab)
9693
0
  ? elf32_arm_fdpic_thumb_plt_entry
9694
0
  : elf32_arm_fdpic_plt_entry;
9695
9696
      /* Fill-up Thumb stub if needed.  */
9697
0
      if (elf32_arm_plt_needs_thumb_stub_p (info, arm_plt))
9698
0
  {
9699
0
    put_thumb_insn (htab, output_bfd,
9700
0
        elf32_arm_plt_thumb_stub[0], ptr - 4);
9701
0
    put_thumb_insn (htab, output_bfd,
9702
0
        elf32_arm_plt_thumb_stub[1], ptr - 2);
9703
0
  }
9704
      /* As we are using 32 bit instructions even for the Thumb
9705
   version, we have to use 'put_arm_insn' instead of
9706
   'put_thumb_insn'.  */
9707
0
      put_arm_insn (htab, output_bfd, plt_entry[0], ptr + 0);
9708
0
      put_arm_insn (htab, output_bfd, plt_entry[1], ptr + 4);
9709
0
      put_arm_insn (htab, output_bfd, plt_entry[2], ptr + 8);
9710
0
      put_arm_insn (htab, output_bfd, plt_entry[3], ptr + 12);
9711
0
      bfd_put_32 (output_bfd, got_offset, ptr + 16);
9712
9713
0
      if (!(info->flags & DF_BIND_NOW))
9714
0
  {
9715
    /* funcdesc_value_reloc_offset.  */
9716
0
    bfd_put_32 (output_bfd,
9717
0
          htab->root.srelplt->reloc_count * RELOC_SIZE (htab),
9718
0
          ptr + 20);
9719
0
    put_arm_insn (htab, output_bfd, plt_entry[6], ptr + 24);
9720
0
    put_arm_insn (htab, output_bfd, plt_entry[7], ptr + 28);
9721
0
    put_arm_insn (htab, output_bfd, plt_entry[8], ptr + 32);
9722
0
    put_arm_insn (htab, output_bfd, plt_entry[9], ptr + 36);
9723
0
  }
9724
0
    }
9725
0
  else if (using_thumb_only (htab))
9726
0
    {
9727
      /* PR ld/16017: Generate thumb only PLT entries.  */
9728
0
      if (!using_thumb2 (htab))
9729
0
  {
9730
    /* FIXME: We ought to be able to generate thumb-1 PLT
9731
       instructions...  */
9732
0
    _bfd_error_handler (_("%pB: warning: thumb-1 mode PLT generation not currently supported"),
9733
0
            output_bfd);
9734
0
    return false;
9735
0
  }
9736
9737
      /* Calculate the displacement between the PLT slot and the entry in
9738
   the GOT.  The 12-byte offset accounts for the value produced by
9739
   adding to pc in the 3rd instruction of the PLT stub.  */
9740
0
      got_displacement = got_address - (plt_address + 12);
9741
9742
      /* As we are using 32 bit instructions we have to use 'put_arm_insn'
9743
   instead of 'put_thumb_insn'.  */
9744
0
      put_arm_insn (htab, output_bfd,
9745
0
        elf32_thumb2_plt_entry[0]
9746
0
        | ((got_displacement & 0x000000ff) << 16)
9747
0
        | ((got_displacement & 0x00000700) << 20)
9748
0
        | ((got_displacement & 0x00000800) >>  1)
9749
0
        | ((got_displacement & 0x0000f000) >> 12),
9750
0
        ptr + 0);
9751
0
      put_arm_insn (htab, output_bfd,
9752
0
        elf32_thumb2_plt_entry[1]
9753
0
        | ((got_displacement & 0x00ff0000)      )
9754
0
        | ((got_displacement & 0x07000000) <<  4)
9755
0
        | ((got_displacement & 0x08000000) >> 17)
9756
0
        | ((got_displacement & 0xf0000000) >> 28),
9757
0
        ptr + 4);
9758
0
      put_arm_insn (htab, output_bfd,
9759
0
        elf32_thumb2_plt_entry[2],
9760
0
        ptr + 8);
9761
0
      put_arm_insn (htab, output_bfd,
9762
0
        elf32_thumb2_plt_entry[3],
9763
0
        ptr + 12);
9764
0
    }
9765
0
  else
9766
0
    {
9767
      /* Calculate the displacement between the PLT slot and the
9768
   entry in the GOT.  The eight-byte offset accounts for the
9769
   value produced by adding to pc in the first instruction
9770
   of the PLT stub.  */
9771
0
      got_displacement = got_address - (plt_address + 8);
9772
9773
0
      if (elf32_arm_plt_needs_thumb_stub_p (info, arm_plt))
9774
0
  {
9775
0
    put_thumb_insn (htab, output_bfd,
9776
0
        elf32_arm_plt_thumb_stub[0], ptr - 4);
9777
0
    put_thumb_insn (htab, output_bfd,
9778
0
        elf32_arm_plt_thumb_stub[1], ptr - 2);
9779
0
  }
9780
9781
0
      if (!elf32_arm_use_long_plt_entry)
9782
0
  {
9783
0
    BFD_ASSERT ((got_displacement & 0xf0000000) == 0);
9784
9785
0
    put_arm_insn (htab, output_bfd,
9786
0
      elf32_arm_plt_entry_short[0]
9787
0
      | ((got_displacement & 0x0ff00000) >> 20),
9788
0
      ptr + 0);
9789
0
    put_arm_insn (htab, output_bfd,
9790
0
      elf32_arm_plt_entry_short[1]
9791
0
      | ((got_displacement & 0x000ff000) >> 12),
9792
0
      ptr+ 4);
9793
0
    put_arm_insn (htab, output_bfd,
9794
0
      elf32_arm_plt_entry_short[2]
9795
0
      | (got_displacement & 0x00000fff),
9796
0
      ptr + 8);
9797
#ifdef FOUR_WORD_PLT
9798
    bfd_put_32 (output_bfd, elf32_arm_plt_entry_short[3], ptr + 12);
9799
#endif
9800
0
  }
9801
0
      else
9802
0
  {
9803
0
    put_arm_insn (htab, output_bfd,
9804
0
      elf32_arm_plt_entry_long[0]
9805
0
      | ((got_displacement & 0xf0000000) >> 28),
9806
0
      ptr + 0);
9807
0
    put_arm_insn (htab, output_bfd,
9808
0
      elf32_arm_plt_entry_long[1]
9809
0
      | ((got_displacement & 0x0ff00000) >> 20),
9810
0
      ptr + 4);
9811
0
    put_arm_insn (htab, output_bfd,
9812
0
      elf32_arm_plt_entry_long[2]
9813
0
      | ((got_displacement & 0x000ff000) >> 12),
9814
0
      ptr+ 8);
9815
0
    put_arm_insn (htab, output_bfd,
9816
0
      elf32_arm_plt_entry_long[3]
9817
0
      | (got_displacement & 0x00000fff),
9818
0
      ptr + 12);
9819
0
  }
9820
0
    }
9821
9822
  /* Fill in the entry in the .rel(a).(i)plt section.  */
9823
0
  rel.r_offset = got_address;
9824
0
  rel.r_addend = 0;
9825
0
  if (dynindx == -1)
9826
0
    {
9827
      /* .igot.plt entries use IRELATIVE relocations against SYM_VALUE.
9828
   The dynamic linker or static executable then calls SYM_VALUE
9829
   to determine the correct run-time value of the .igot.plt entry.  */
9830
0
      rel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
9831
0
      initial_got_entry = sym_value;
9832
0
    }
9833
0
  else
9834
0
    {
9835
      /* For FDPIC we will have to resolve a R_ARM_FUNCDESC_VALUE
9836
   used by PLT entry.  */
9837
0
      if (htab->fdpic_p)
9838
0
  {
9839
0
    rel.r_info = ELF32_R_INFO (dynindx, R_ARM_FUNCDESC_VALUE);
9840
0
    initial_got_entry = 0;
9841
0
  }
9842
0
      else
9843
0
  {
9844
0
    rel.r_info = ELF32_R_INFO (dynindx, R_ARM_JUMP_SLOT);
9845
0
    initial_got_entry = (splt->output_section->vma
9846
0
             + splt->output_offset);
9847
9848
    /* PR ld/16017
9849
       When thumb only we need to set the LSB for any address that
9850
       will be used with an interworking branch instruction.  */
9851
0
    if (using_thumb_only (htab))
9852
0
      initial_got_entry |= 1;
9853
0
  }
9854
0
    }
9855
9856
  /* Fill in the entry in the global offset table.  */
9857
0
  bfd_put_32 (output_bfd, initial_got_entry,
9858
0
        sgot->contents + got_offset);
9859
9860
0
  if (htab->fdpic_p && !(info->flags & DF_BIND_NOW))
9861
0
    {
9862
      /* Setup initial funcdesc value.  */
9863
      /* FIXME: we don't support lazy binding because there is a
9864
   race condition between both words getting written and
9865
   some other thread attempting to read them. The ARM
9866
   architecture does not have an atomic 64 bit load/store
9867
   instruction that could be used to prevent it; it is
9868
   recommended that threaded FDPIC applications run with the
9869
   LD_BIND_NOW environment variable set.  */
9870
0
      bfd_put_32 (output_bfd, plt_address + 0x18,
9871
0
      sgot->contents + got_offset);
9872
0
      bfd_put_32 (output_bfd, -1 /*TODO*/,
9873
0
      sgot->contents + got_offset + 4);
9874
0
    }
9875
9876
0
  if (dynindx == -1)
9877
0
    elf32_arm_add_dynreloc (output_bfd, info, srel, &rel);
9878
0
  else
9879
0
    {
9880
0
      if (htab->fdpic_p)
9881
0
  {
9882
    /* For FDPIC we put PLT relocationss into .rel.got when not
9883
       lazy binding otherwise we put them in .rel.plt.  For now,
9884
       we don't support lazy binding so put it in .rel.got.  */
9885
0
    if (info->flags & DF_BIND_NOW)
9886
0
      elf32_arm_add_dynreloc (output_bfd, info, htab->root.srelgot, &rel);
9887
0
    else
9888
0
      elf32_arm_add_dynreloc (output_bfd, info, htab->root.srelplt, &rel);
9889
0
  }
9890
0
      else
9891
0
  {
9892
0
    loc = srel->contents + plt_index * RELOC_SIZE (htab);
9893
0
    SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
9894
0
  }
9895
0
    }
9896
9897
0
  return true;
9898
0
}
9899
9900
/* Some relocations map to different relocations depending on the
9901
   target.  Return the real relocation.  */
9902
9903
static int
9904
arm_real_reloc_type (struct elf32_arm_link_hash_table * globals,
9905
         int r_type)
9906
0
{
9907
0
  switch (r_type)
9908
0
    {
9909
0
    case R_ARM_TARGET1:
9910
0
      if (globals->target1_is_rel)
9911
0
  return R_ARM_REL32;
9912
0
      else
9913
0
  return R_ARM_ABS32;
9914
9915
0
    case R_ARM_TARGET2:
9916
0
      return globals->target2_reloc;
9917
9918
0
    default:
9919
0
      return r_type;
9920
0
    }
9921
0
}
9922
9923
/* Return the base VMA address which should be subtracted from real addresses
9924
   when resolving @dtpoff relocation.
9925
   This is PT_TLS segment p_vaddr.  */
9926
9927
static bfd_vma
9928
dtpoff_base (struct bfd_link_info *info)
9929
0
{
9930
  /* If tls_sec is NULL, we should have signalled an error already.  */
9931
0
  if (elf_hash_table (info)->tls_sec == NULL)
9932
0
    return 0;
9933
0
  return elf_hash_table (info)->tls_sec->vma;
9934
0
}
9935
9936
/* Return the relocation value for @tpoff relocation
9937
   if STT_TLS virtual address is ADDRESS.  */
9938
9939
static bfd_vma
9940
tpoff (struct bfd_link_info *info, bfd_vma address)
9941
0
{
9942
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
9943
0
  bfd_vma base;
9944
9945
  /* If tls_sec is NULL, we should have signalled an error already.  */
9946
0
  if (htab->tls_sec == NULL)
9947
0
    return 0;
9948
0
  base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
9949
0
  return address - htab->tls_sec->vma + base;
9950
0
}
9951
9952
/* Perform an R_ARM_ABS12 relocation on the field pointed to by DATA.
9953
   VALUE is the relocation value.  */
9954
9955
static bfd_reloc_status_type
9956
elf32_arm_abs12_reloc (bfd *abfd, void *data, bfd_vma value)
9957
0
{
9958
0
  if (value > 0xfff)
9959
0
    return bfd_reloc_overflow;
9960
9961
0
  value |= bfd_get_32 (abfd, data) & 0xfffff000;
9962
0
  bfd_put_32 (abfd, value, data);
9963
0
  return bfd_reloc_ok;
9964
0
}
9965
9966
/* Handle TLS relaxations.  Relaxing is possible for symbols that use
9967
   R_ARM_GOTDESC, R_ARM_{,THM_}TLS_CALL or
9968
   R_ARM_{,THM_}TLS_DESCSEQ relocations, during a static link.
9969
9970
   Return bfd_reloc_ok if we're done, bfd_reloc_continue if the caller
9971
   is to then call final_link_relocate.  Return other values in the
9972
   case of error.
9973
9974
   FIXME:When --emit-relocs is in effect, we'll emit relocs describing
9975
   the pre-relaxed code.  It would be nice if the relocs were updated
9976
   to match the optimization.   */
9977
9978
static bfd_reloc_status_type
9979
elf32_arm_tls_relax (struct elf32_arm_link_hash_table *globals,
9980
         bfd *input_bfd, asection *input_sec, bfd_byte *contents,
9981
         Elf_Internal_Rela *rel, unsigned long is_local)
9982
0
{
9983
0
  unsigned long insn;
9984
9985
0
  switch (ELF32_R_TYPE (rel->r_info))
9986
0
    {
9987
0
    default:
9988
0
      return bfd_reloc_notsupported;
9989
9990
0
    case R_ARM_TLS_GOTDESC:
9991
0
      if (is_local)
9992
0
  insn = 0;
9993
0
      else
9994
0
  {
9995
0
    insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
9996
0
    if (insn & 1)
9997
0
      insn -= 5; /* THUMB */
9998
0
    else
9999
0
      insn -= 8; /* ARM */
10000
0
  }
10001
0
      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
10002
0
      return bfd_reloc_continue;
10003
10004
0
    case R_ARM_THM_TLS_DESCSEQ:
10005
      /* Thumb insn.  */
10006
0
      insn = bfd_get_16 (input_bfd, contents + rel->r_offset);
10007
0
      if ((insn & 0xff78) == 0x4478)   /* add rx, pc */
10008
0
  {
10009
0
    if (is_local)
10010
      /* nop */
10011
0
      bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
10012
0
  }
10013
0
      else if ((insn & 0xffc0) == 0x6840)  /* ldr rx,[ry,#4] */
10014
0
  {
10015
0
    if (is_local)
10016
      /* nop */
10017
0
      bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
10018
0
    else
10019
      /* ldr rx,[ry] */
10020
0
      bfd_put_16 (input_bfd, insn & 0xf83f, contents + rel->r_offset);
10021
0
  }
10022
0
      else if ((insn & 0xff87) == 0x4780)  /* blx rx */
10023
0
  {
10024
0
    if (is_local)
10025
      /* nop */
10026
0
      bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
10027
0
    else
10028
      /* mov r0, rx */
10029
0
      bfd_put_16 (input_bfd, 0x4600 | (insn & 0x78),
10030
0
      contents + rel->r_offset);
10031
0
  }
10032
0
      else
10033
0
  {
10034
0
    if ((insn & 0xf000) == 0xf000 || (insn & 0xf800) == 0xe800)
10035
      /* It's a 32 bit instruction, fetch the rest of it for
10036
         error generation.  */
10037
0
      insn = (insn << 16)
10038
0
        | bfd_get_16 (input_bfd, contents + rel->r_offset + 2);
10039
0
    _bfd_error_handler
10040
      /* xgettext:c-format */
10041
0
      (_("%pB(%pA+%#" PRIx64 "): "
10042
0
         "unexpected %s instruction '%#lx' in TLS trampoline"),
10043
0
       input_bfd, input_sec, (uint64_t) rel->r_offset,
10044
0
       "Thumb", insn);
10045
0
    return bfd_reloc_notsupported;
10046
0
  }
10047
0
      break;
10048
10049
0
    case R_ARM_TLS_DESCSEQ:
10050
      /* arm insn.  */
10051
0
      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
10052
0
      if ((insn & 0xffff0ff0) == 0xe08f0000) /* add rx,pc,ry */
10053
0
  {
10054
0
    if (is_local)
10055
      /* mov rx, ry */
10056
0
      bfd_put_32 (input_bfd, 0xe1a00000 | (insn & 0xffff),
10057
0
      contents + rel->r_offset);
10058
0
  }
10059
0
      else if ((insn & 0xfff00fff) == 0xe5900004) /* ldr rx,[ry,#4]*/
10060
0
  {
10061
0
    if (is_local)
10062
      /* nop */
10063
0
      bfd_put_32 (input_bfd, 0xe1a00000, contents + rel->r_offset);
10064
0
    else
10065
      /* ldr rx,[ry] */
10066
0
      bfd_put_32 (input_bfd, insn & 0xfffff000,
10067
0
      contents + rel->r_offset);
10068
0
  }
10069
0
      else if ((insn & 0xfffffff0) == 0xe12fff30) /* blx rx */
10070
0
  {
10071
0
    if (is_local)
10072
      /* nop */
10073
0
      bfd_put_32 (input_bfd, 0xe1a00000, contents + rel->r_offset);
10074
0
    else
10075
      /* mov r0, rx */
10076
0
      bfd_put_32 (input_bfd, 0xe1a00000 | (insn & 0xf),
10077
0
      contents + rel->r_offset);
10078
0
  }
10079
0
      else
10080
0
  {
10081
0
    _bfd_error_handler
10082
      /* xgettext:c-format */
10083
0
      (_("%pB(%pA+%#" PRIx64 "): "
10084
0
         "unexpected %s instruction '%#lx' in TLS trampoline"),
10085
0
       input_bfd, input_sec, (uint64_t) rel->r_offset,
10086
0
       "ARM", insn);
10087
0
    return bfd_reloc_notsupported;
10088
0
  }
10089
0
      break;
10090
10091
0
    case R_ARM_TLS_CALL:
10092
      /* GD->IE relaxation, turn the instruction into 'nop' or
10093
   'ldr r0, [pc,r0]'  */
10094
0
      insn = is_local ? 0xe1a00000 : 0xe79f0000;
10095
0
      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
10096
0
      break;
10097
10098
0
    case R_ARM_THM_TLS_CALL:
10099
      /* GD->IE relaxation.  */
10100
0
      if (!is_local)
10101
  /* add r0,pc; ldr r0, [r0]  */
10102
0
  insn = 0x44786800;
10103
0
      else if (using_thumb2 (globals))
10104
  /* nop.w */
10105
0
  insn = 0xf3af8000;
10106
0
      else
10107
  /* nop; nop */
10108
0
  insn = 0xbf00bf00;
10109
10110
0
      bfd_put_16 (input_bfd, insn >> 16, contents + rel->r_offset);
10111
0
      bfd_put_16 (input_bfd, insn & 0xffff, contents + rel->r_offset + 2);
10112
0
      break;
10113
0
    }
10114
0
  return bfd_reloc_ok;
10115
0
}
10116
10117
/* For a given value of n, calculate the value of G_n as required to
10118
   deal with group relocations.  We return it in the form of an
10119
   encoded constant-and-rotation, together with the final residual.  If n is
10120
   specified as less than zero, then final_residual is filled with the
10121
   input value and no further action is performed.  */
10122
10123
static bfd_vma
10124
calculate_group_reloc_mask (bfd_vma value, int n, bfd_vma *final_residual)
10125
0
{
10126
0
  int current_n;
10127
0
  bfd_vma g_n;
10128
0
  bfd_vma encoded_g_n = 0;
10129
0
  bfd_vma residual = value; /* Also known as Y_n.  */
10130
10131
0
  for (current_n = 0; current_n <= n; current_n++)
10132
0
    {
10133
0
      int shift;
10134
10135
      /* Calculate which part of the value to mask.  */
10136
0
      if (residual == 0)
10137
0
  shift = 0;
10138
0
      else
10139
0
  {
10140
0
    int msb;
10141
10142
    /* Determine the most significant bit in the residual and
10143
       align the resulting value to a 2-bit boundary.  */
10144
0
    for (msb = 30; msb >= 0; msb -= 2)
10145
0
      if (residual & (3u << msb))
10146
0
        break;
10147
10148
    /* The desired shift is now (msb - 6), or zero, whichever
10149
       is the greater.  */
10150
0
    shift = msb - 6;
10151
0
    if (shift < 0)
10152
0
      shift = 0;
10153
0
  }
10154
10155
      /* Calculate g_n in 32-bit as well as encoded constant+rotation form.  */
10156
0
      g_n = residual & (0xff << shift);
10157
0
      encoded_g_n = (g_n >> shift)
10158
0
        | ((g_n <= 0xff ? 0 : (32 - shift) / 2) << 8);
10159
10160
      /* Calculate the residual for the next time around.  */
10161
0
      residual &= ~g_n;
10162
0
    }
10163
10164
0
  *final_residual = residual;
10165
10166
0
  return encoded_g_n;
10167
0
}
10168
10169
/* Given an ARM instruction, determine whether it is an ADD or a SUB.
10170
   Returns 1 if it is an ADD, -1 if it is a SUB, and 0 otherwise.  */
10171
10172
static int
10173
identify_add_or_sub (bfd_vma insn)
10174
0
{
10175
0
  int opcode = insn & 0x1e00000;
10176
10177
0
  if (opcode == 1 << 23) /* ADD */
10178
0
    return 1;
10179
10180
0
  if (opcode == 1 << 22) /* SUB */
10181
0
    return -1;
10182
10183
0
  return 0;
10184
0
}
10185
10186
/* Perform a relocation as part of a final link.  */
10187
10188
static bfd_reloc_status_type
10189
elf32_arm_final_link_relocate (reloc_howto_type *     howto,
10190
             bfd *          input_bfd,
10191
             bfd *          output_bfd,
10192
             asection *       input_section,
10193
             bfd_byte *       contents,
10194
             Elf_Internal_Rela *      rel,
10195
             bfd_vma          value,
10196
             struct bfd_link_info *     info,
10197
             asection *       sym_sec,
10198
             const char *       sym_name,
10199
             unsigned char        st_type,
10200
             enum arm_st_branch_type      branch_type,
10201
             struct elf_link_hash_entry * h,
10202
             bool *         unresolved_reloc_p,
10203
             char **          error_message)
10204
0
{
10205
0
  unsigned long     r_type = howto->type;
10206
0
  unsigned long     r_symndx;
10207
0
  bfd_byte *      hit_data = contents + rel->r_offset;
10208
0
  bfd_vma *     local_got_offsets;
10209
0
  bfd_vma *     local_tlsdesc_gotents;
10210
0
  asection *      sgot;
10211
0
  asection *      splt;
10212
0
  asection *      sreloc = NULL;
10213
0
  asection *      srelgot;
10214
0
  bfd_vma     addend;
10215
0
  bfd_signed_vma    signed_addend;
10216
0
  unsigned char     dynreloc_st_type;
10217
0
  bfd_vma     dynreloc_value;
10218
0
  struct elf32_arm_link_hash_table * globals;
10219
0
  struct elf32_arm_link_hash_entry *eh;
10220
0
  union gotplt_union         *root_plt;
10221
0
  struct arm_plt_info        *arm_plt;
10222
0
  bfd_vma     plt_offset;
10223
0
  bfd_vma     gotplt_offset;
10224
0
  bool        has_iplt_entry;
10225
0
  bool        resolved_to_zero;
10226
10227
0
  globals = elf32_arm_hash_table (info);
10228
0
  if (globals == NULL)
10229
0
    return bfd_reloc_notsupported;
10230
10231
0
  BFD_ASSERT (is_arm_elf (input_bfd));
10232
0
  BFD_ASSERT (howto != NULL);
10233
10234
  /* Some relocation types map to different relocations depending on the
10235
     target.  We pick the right one here.  */
10236
0
  r_type = arm_real_reloc_type (globals, r_type);
10237
10238
  /* It is possible to have linker relaxations on some TLS access
10239
     models.  Update our information here.  */
10240
0
  r_type = elf32_arm_tls_transition (info, r_type, h);
10241
10242
0
  if (r_type != howto->type)
10243
0
    howto = elf32_arm_howto_from_type (r_type);
10244
10245
0
  eh = (struct elf32_arm_link_hash_entry *) h;
10246
0
  sgot = globals->root.sgot;
10247
0
  local_got_offsets = elf_local_got_offsets (input_bfd);
10248
0
  local_tlsdesc_gotents = elf32_arm_local_tlsdesc_gotent (input_bfd);
10249
10250
0
  if (globals->root.dynamic_sections_created)
10251
0
    srelgot = globals->root.srelgot;
10252
0
  else
10253
0
    srelgot = NULL;
10254
10255
0
  r_symndx = ELF32_R_SYM (rel->r_info);
10256
10257
0
  if (globals->use_rel)
10258
0
    {
10259
0
      bfd_vma sign;
10260
10261
0
      switch (bfd_get_reloc_size (howto))
10262
0
  {
10263
0
  case 1: addend = bfd_get_8 (input_bfd, hit_data); break;
10264
0
  case 2: addend = bfd_get_16 (input_bfd, hit_data); break;
10265
0
  case 4: addend = bfd_get_32 (input_bfd, hit_data); break;
10266
0
  default: addend = 0; break;
10267
0
  }
10268
      /* Note: the addend and signed_addend calculated here are
10269
   incorrect for any split field.  */
10270
0
      addend &= howto->src_mask;
10271
0
      sign = howto->src_mask & ~(howto->src_mask >> 1);
10272
0
      signed_addend = (addend ^ sign) - sign;
10273
0
      signed_addend = (bfd_vma) signed_addend << howto->rightshift;
10274
0
      addend <<= howto->rightshift;
10275
0
    }
10276
0
  else
10277
0
    addend = signed_addend = rel->r_addend;
10278
10279
  /* Record the symbol information that should be used in dynamic
10280
     relocations.  */
10281
0
  dynreloc_st_type = st_type;
10282
0
  dynreloc_value = value;
10283
0
  if (branch_type == ST_BRANCH_TO_THUMB)
10284
0
    dynreloc_value |= 1;
10285
10286
  /* Find out whether the symbol has a PLT.  Set ST_VALUE, BRANCH_TYPE and
10287
     VALUE appropriately for relocations that we resolve at link time.  */
10288
0
  has_iplt_entry = false;
10289
0
  if (elf32_arm_get_plt_info (input_bfd, globals, eh, r_symndx, &root_plt,
10290
0
            &arm_plt)
10291
0
      && root_plt->offset != (bfd_vma) -1)
10292
0
    {
10293
0
      plt_offset = root_plt->offset;
10294
0
      gotplt_offset = arm_plt->got_offset;
10295
10296
0
      if (h == NULL || eh->is_iplt)
10297
0
  {
10298
0
    has_iplt_entry = true;
10299
0
    splt = globals->root.iplt;
10300
10301
    /* Populate .iplt entries here, because not all of them will
10302
       be seen by finish_dynamic_symbol.  The lower bit is set if
10303
       we have already populated the entry.  */
10304
0
    if (plt_offset & 1)
10305
0
      plt_offset--;
10306
0
    else
10307
0
      {
10308
0
        if (elf32_arm_populate_plt_entry (output_bfd, info, root_plt, arm_plt,
10309
0
            -1, dynreloc_value))
10310
0
    root_plt->offset |= 1;
10311
0
        else
10312
0
    return bfd_reloc_notsupported;
10313
0
      }
10314
10315
    /* Static relocations always resolve to the .iplt entry.  */
10316
0
    st_type = STT_FUNC;
10317
0
    value = (splt->output_section->vma
10318
0
       + splt->output_offset
10319
0
       + plt_offset);
10320
0
    branch_type = ST_BRANCH_TO_ARM;
10321
10322
    /* If there are non-call relocations that resolve to the .iplt
10323
       entry, then all dynamic ones must too.  */
10324
0
    if (arm_plt->noncall_refcount != 0)
10325
0
      {
10326
0
        dynreloc_st_type = st_type;
10327
0
        dynreloc_value = value;
10328
0
      }
10329
0
  }
10330
0
      else
10331
  /* We populate the .plt entry in finish_dynamic_symbol.  */
10332
0
  splt = globals->root.splt;
10333
0
    }
10334
0
  else
10335
0
    {
10336
0
      splt = NULL;
10337
0
      plt_offset = (bfd_vma) -1;
10338
0
      gotplt_offset = (bfd_vma) -1;
10339
0
    }
10340
10341
  /* ST_BRANCH_TO_ARM is nonsense to thumb-only targets when we are
10342
     resolving a function call relocation.  We want to inform the user
10343
     that something is wrong.  */
10344
0
  if (using_thumb_only (globals)
10345
0
      && (r_type == R_ARM_THM_CALL
10346
0
    || r_type == R_ARM_THM_JUMP24)
10347
0
      && branch_type == ST_BRANCH_TO_ARM
10348
      /* Calls through a PLT are special: the assembly source code
10349
   cannot be annotated with '.type foo(PLT), %function', and
10350
   they handled specifically below anyway. */
10351
0
      && splt == NULL)
10352
0
    {
10353
0
      if (sym_sec == bfd_abs_section_ptr)
10354
0
  {
10355
  /* As an exception, assume that absolute symbols are of the
10356
     right kind (Thumb).  They are presumably defined in the
10357
     linker script, where it is not possible to declare them as
10358
     Thumb (and thus are seen as Arm mode). Inform the user with
10359
     a warning, though. */
10360
0
    branch_type = ST_BRANCH_TO_THUMB;
10361
10362
0
    if (sym_sec->owner)
10363
0
      _bfd_error_handler
10364
0
        (_("warning: %pB(%s): Forcing bramch to absolute symbol in Thumb mode (Thumb-only CPU)"
10365
0
     " in %pB"),
10366
0
         sym_sec->owner, sym_name, input_bfd);
10367
0
    else
10368
0
      _bfd_error_handler
10369
0
        (_("warning: (%s): Forcing branch to absolute symbol in Thumb mode (Thumb-only CPU)"
10370
0
     " in %pB"),
10371
0
         sym_name, input_bfd);
10372
0
  }
10373
0
      else
10374
  /* Otherwise do not silently build a stub, and let the users
10375
     know they have to fix their code.  Indeed, we could decide
10376
     to insert a stub involving Arm code and/or BLX, leading to
10377
     a run-time crash.  */
10378
0
  branch_type = ST_BRANCH_UNKNOWN;
10379
0
    }
10380
10381
  /* Fail early if branch_type is ST_BRANCH_UNKNOWN and we target a
10382
     Thumb-only CPU.  We could emit a warning on Arm-capable targets
10383
     too, but that would be too verbose (a lot of legacy code does not
10384
     use the .type foo, %function directive).  */
10385
0
  if (using_thumb_only (globals)
10386
0
      && (r_type == R_ARM_THM_CALL
10387
0
    || r_type == R_ARM_THM_JUMP24)
10388
0
      && branch_type == ST_BRANCH_UNKNOWN
10389
      /* Exception to the rule above: a branch to an undefined weak
10390
   symbol is turned into a jump to the next instruction unless a
10391
   PLT entry will be created (see below).  */
10392
0
      && !(h && h->root.type == bfd_link_hash_undefweak
10393
0
     && plt_offset == (bfd_vma) -1))
10394
0
    {
10395
0
      if (sym_sec != NULL
10396
0
    && sym_sec->owner != NULL)
10397
0
  _bfd_error_handler
10398
0
    (_("%pB(%s): Unknown destination type (ARM/Thumb) in %pB"),
10399
0
     sym_sec->owner, sym_name, input_bfd);
10400
0
      else
10401
0
  _bfd_error_handler
10402
0
    (_("(%s): Unknown destination type (ARM/Thumb) in %pB"),
10403
0
     sym_name, input_bfd);
10404
10405
0
      return bfd_reloc_notsupported;
10406
0
    }
10407
10408
0
  resolved_to_zero = (h != NULL
10409
0
          && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
10410
10411
0
  switch (r_type)
10412
0
    {
10413
0
    case R_ARM_NONE:
10414
      /* We don't need to find a value for this symbol.  It's just a
10415
   marker.  */
10416
0
      *unresolved_reloc_p = false;
10417
0
      return bfd_reloc_ok;
10418
10419
0
    case R_ARM_ABS12:
10420
0
      if (globals->root.target_os != is_vxworks)
10421
0
  return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
10422
      /* Fall through.  */
10423
10424
0
    case R_ARM_PC24:
10425
0
    case R_ARM_ABS32:
10426
0
    case R_ARM_ABS32_NOI:
10427
0
    case R_ARM_REL32:
10428
0
    case R_ARM_REL32_NOI:
10429
0
    case R_ARM_CALL:
10430
0
    case R_ARM_JUMP24:
10431
0
    case R_ARM_XPC25:
10432
0
    case R_ARM_PREL31:
10433
0
    case R_ARM_PLT32:
10434
      /* Handle relocations which should use the PLT entry.  ABS32/REL32
10435
   will use the symbol's value, which may point to a PLT entry, but we
10436
   don't need to handle that here.  If we created a PLT entry, all
10437
   branches in this object should go to it, except if the PLT is too
10438
   far away, in which case a long branch stub should be inserted.  */
10439
0
      if ((r_type != R_ARM_ABS32 && r_type != R_ARM_REL32
10440
0
     && r_type != R_ARM_ABS32_NOI && r_type != R_ARM_REL32_NOI
10441
0
     && r_type != R_ARM_CALL
10442
0
     && r_type != R_ARM_JUMP24
10443
0
     && r_type != R_ARM_PLT32)
10444
0
    && plt_offset != (bfd_vma) -1)
10445
0
  {
10446
    /* If we've created a .plt section, and assigned a PLT entry
10447
       to this function, it must either be a STT_GNU_IFUNC reference
10448
       or not be known to bind locally.  In other cases, we should
10449
       have cleared the PLT entry by now.  */
10450
0
    BFD_ASSERT (has_iplt_entry || !SYMBOL_CALLS_LOCAL (info, h));
10451
10452
0
    value = (splt->output_section->vma
10453
0
       + splt->output_offset
10454
0
       + plt_offset);
10455
0
    *unresolved_reloc_p = false;
10456
0
    return _bfd_final_link_relocate (howto, input_bfd, input_section,
10457
0
             contents, rel->r_offset, value,
10458
0
             rel->r_addend);
10459
0
  }
10460
10461
      /* When generating a shared library or PIE, these relocations
10462
   are copied into the output file to be resolved at run time.  */
10463
0
      if ((bfd_link_pic (info)
10464
0
     || globals->fdpic_p)
10465
0
    && (input_section->flags & SEC_ALLOC)
10466
0
    && !(globals->root.target_os == is_vxworks
10467
0
         && strcmp (input_section->output_section->name,
10468
0
        ".tls_vars") == 0)
10469
0
    && ((r_type != R_ARM_REL32 && r_type != R_ARM_REL32_NOI)
10470
0
        || !SYMBOL_CALLS_LOCAL (info, h))
10471
0
    && !(input_bfd == globals->stub_bfd
10472
0
         && strstr (input_section->name, STUB_SUFFIX))
10473
0
    && (h == NULL
10474
0
        || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10475
0
      && !resolved_to_zero)
10476
0
        || h->root.type != bfd_link_hash_undefweak)
10477
0
    && r_type != R_ARM_PC24
10478
0
    && r_type != R_ARM_CALL
10479
0
    && r_type != R_ARM_JUMP24
10480
0
    && r_type != R_ARM_PREL31
10481
0
    && r_type != R_ARM_PLT32)
10482
0
  {
10483
0
    Elf_Internal_Rela outrel;
10484
0
    bool skip, relocate;
10485
0
    int isrofixup = 0;
10486
10487
0
    if ((r_type == R_ARM_REL32 || r_type == R_ARM_REL32_NOI)
10488
0
        && !h->def_regular)
10489
0
      {
10490
0
        char *v = _("shared object");
10491
10492
0
        if (bfd_link_executable (info))
10493
0
    v = _("PIE executable");
10494
10495
0
        _bfd_error_handler
10496
0
    (_("%pB: relocation %s against external or undefined symbol `%s'"
10497
0
       " can not be used when making a %s; recompile with -fPIC"), input_bfd,
10498
0
     elf32_arm_howto_table_1[r_type].name, h->root.root.string, v);
10499
0
        return bfd_reloc_notsupported;
10500
0
      }
10501
10502
0
    *unresolved_reloc_p = false;
10503
10504
0
    if (sreloc == NULL && globals->root.dynamic_sections_created)
10505
0
      {
10506
0
        sreloc = _bfd_elf_get_dynamic_reloc_section (input_bfd, input_section,
10507
0
                 ! globals->use_rel);
10508
10509
0
        if (sreloc == NULL)
10510
0
    return bfd_reloc_notsupported;
10511
0
      }
10512
10513
0
    skip = false;
10514
0
    relocate = false;
10515
10516
0
    outrel.r_addend = addend;
10517
0
    outrel.r_offset =
10518
0
      _bfd_elf_section_offset (output_bfd, info, input_section,
10519
0
             rel->r_offset);
10520
0
    if (outrel.r_offset == (bfd_vma) -1)
10521
0
      skip = true;
10522
0
    else if (outrel.r_offset == (bfd_vma) -2)
10523
0
      skip = true, relocate = true;
10524
0
    outrel.r_offset += (input_section->output_section->vma
10525
0
            + input_section->output_offset);
10526
10527
0
    if (skip)
10528
0
      memset (&outrel, 0, sizeof outrel);
10529
0
    else if (h != NULL
10530
0
       && h->dynindx != -1
10531
0
       && (!bfd_link_pic (info)
10532
0
           || !(bfd_link_pie (info)
10533
0
          || SYMBOLIC_BIND (info, h))
10534
0
           || !h->def_regular))
10535
0
      outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
10536
0
    else
10537
0
      {
10538
0
        int symbol;
10539
10540
        /* This symbol is local, or marked to become local.  */
10541
0
        BFD_ASSERT (r_type == R_ARM_ABS32 || r_type == R_ARM_ABS32_NOI
10542
0
        || (globals->fdpic_p && !bfd_link_pic (info)));
10543
        /* On SVR4-ish systems, the dynamic loader cannot
10544
     relocate the text and data segments independently,
10545
     so the symbol does not matter.  */
10546
0
        symbol = 0;
10547
0
        if (dynreloc_st_type == STT_GNU_IFUNC)
10548
    /* We have an STT_GNU_IFUNC symbol that doesn't resolve
10549
       to the .iplt entry.  Instead, every non-call reference
10550
       must use an R_ARM_IRELATIVE relocation to obtain the
10551
       correct run-time address.  */
10552
0
    outrel.r_info = ELF32_R_INFO (symbol, R_ARM_IRELATIVE);
10553
0
        else if (globals->fdpic_p && !bfd_link_pic (info))
10554
0
    isrofixup = 1;
10555
0
        else
10556
0
    outrel.r_info = ELF32_R_INFO (symbol, R_ARM_RELATIVE);
10557
0
        if (globals->use_rel)
10558
0
    relocate = true;
10559
0
        else
10560
0
    outrel.r_addend += dynreloc_value;
10561
0
      }
10562
10563
0
    if (isrofixup)
10564
0
      arm_elf_add_rofixup (output_bfd, globals->srofixup, outrel.r_offset);
10565
0
    else
10566
0
      elf32_arm_add_dynreloc (output_bfd, info, sreloc, &outrel);
10567
10568
    /* If this reloc is against an external symbol, we do not want to
10569
       fiddle with the addend.  Otherwise, we need to include the symbol
10570
       value so that it becomes an addend for the dynamic reloc.  */
10571
0
    if (! relocate)
10572
0
      return bfd_reloc_ok;
10573
10574
0
    return _bfd_final_link_relocate (howto, input_bfd, input_section,
10575
0
             contents, rel->r_offset,
10576
0
             dynreloc_value, (bfd_vma) 0);
10577
0
  }
10578
0
      else switch (r_type)
10579
0
  {
10580
0
  case R_ARM_ABS12:
10581
0
    return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
10582
10583
0
  case R_ARM_XPC25:   /* Arm BLX instruction.  */
10584
0
  case R_ARM_CALL:
10585
0
  case R_ARM_JUMP24:
10586
0
  case R_ARM_PC24:    /* Arm B/BL instruction.  */
10587
0
  case R_ARM_PLT32:
10588
0
    {
10589
0
    struct elf32_arm_stub_hash_entry *stub_entry = NULL;
10590
10591
0
    if (r_type == R_ARM_XPC25)
10592
0
      {
10593
        /* Check for Arm calling Arm function.  */
10594
        /* FIXME: Should we translate the instruction into a BL
10595
     instruction instead ?  */
10596
0
        if (branch_type != ST_BRANCH_TO_THUMB)
10597
0
    _bfd_error_handler
10598
0
      (_("\%pB: warning: %s BLX instruction targets"
10599
0
         " %s function '%s'"),
10600
0
       input_bfd, "ARM",
10601
0
       "ARM", h ? h->root.root.string : "(local)");
10602
0
      }
10603
0
    else if (r_type == R_ARM_PC24)
10604
0
      {
10605
        /* Check for Arm calling Thumb function.  */
10606
0
        if (branch_type == ST_BRANCH_TO_THUMB)
10607
0
    {
10608
0
      if (elf32_arm_to_thumb_stub (info, sym_name, input_bfd,
10609
0
                 output_bfd, input_section,
10610
0
                 hit_data, sym_sec, rel->r_offset,
10611
0
                 signed_addend, value,
10612
0
                 error_message))
10613
0
        return bfd_reloc_ok;
10614
0
      else
10615
0
        return bfd_reloc_dangerous;
10616
0
    }
10617
0
      }
10618
10619
    /* Check if a stub has to be inserted because the
10620
       destination is too far or we are changing mode.  */
10621
0
    if (   r_type == R_ARM_CALL
10622
0
        || r_type == R_ARM_JUMP24
10623
0
        || r_type == R_ARM_PLT32)
10624
0
      {
10625
0
        enum elf32_arm_stub_type stub_type = arm_stub_none;
10626
0
        struct elf32_arm_link_hash_entry *hash;
10627
10628
0
        hash = (struct elf32_arm_link_hash_entry *) h;
10629
0
        stub_type = arm_type_of_stub (info, input_section, rel,
10630
0
              st_type, &branch_type,
10631
0
              hash, value, sym_sec,
10632
0
              input_bfd, sym_name);
10633
10634
0
        if (stub_type != arm_stub_none)
10635
0
    {
10636
      /* The target is out of reach, so redirect the
10637
         branch to the local stub for this function.  */
10638
0
      stub_entry = elf32_arm_get_stub_entry (input_section,
10639
0
               sym_sec, h,
10640
0
               rel, globals,
10641
0
               stub_type);
10642
0
      {
10643
0
        if (stub_entry != NULL)
10644
0
          value = (stub_entry->stub_offset
10645
0
             + stub_entry->stub_sec->output_offset
10646
0
             + stub_entry->stub_sec->output_section->vma);
10647
10648
0
        if (plt_offset != (bfd_vma) -1)
10649
0
          *unresolved_reloc_p = false;
10650
0
      }
10651
0
    }
10652
0
        else
10653
0
    {
10654
      /* If the call goes through a PLT entry, make sure to
10655
         check distance to the right destination address.  */
10656
0
      if (plt_offset != (bfd_vma) -1)
10657
0
        {
10658
0
          value = (splt->output_section->vma
10659
0
             + splt->output_offset
10660
0
             + plt_offset);
10661
0
          *unresolved_reloc_p = false;
10662
          /* The PLT entry is in ARM mode, regardless of the
10663
       target function.  */
10664
0
          branch_type = ST_BRANCH_TO_ARM;
10665
0
        }
10666
0
    }
10667
0
      }
10668
10669
    /* The ARM ELF ABI says that this reloc is computed as: S - P + A
10670
       where:
10671
        S is the address of the symbol in the relocation.
10672
        P is address of the instruction being relocated.
10673
        A is the addend (extracted from the instruction) in bytes.
10674
10675
       S is held in 'value'.
10676
       P is the base address of the section containing the
10677
         instruction plus the offset of the reloc into that
10678
         section, ie:
10679
     (input_section->output_section->vma +
10680
      input_section->output_offset +
10681
      rel->r_offset).
10682
       A is the addend, converted into bytes, ie:
10683
     (signed_addend * 4)
10684
10685
       Note: None of these operations have knowledge of the pipeline
10686
       size of the processor, thus it is up to the assembler to
10687
       encode this information into the addend.  */
10688
0
    value -= (input_section->output_section->vma
10689
0
        + input_section->output_offset);
10690
0
    value -= rel->r_offset;
10691
0
    value += signed_addend;
10692
10693
0
    signed_addend = value;
10694
0
    signed_addend >>= howto->rightshift;
10695
10696
    /* A branch to an undefined weak symbol is turned into a jump to
10697
       the next instruction unless a PLT entry will be created.
10698
       Do the same for local undefined symbols (but not for STN_UNDEF).
10699
       The jump to the next instruction is optimized as a NOP depending
10700
       on the architecture.  */
10701
0
    if (h ? (h->root.type == bfd_link_hash_undefweak
10702
0
       && plt_offset == (bfd_vma) -1)
10703
0
        : r_symndx != STN_UNDEF && bfd_is_und_section (sym_sec))
10704
0
      {
10705
0
        value = (bfd_get_32 (input_bfd, hit_data) & 0xf0000000);
10706
10707
0
        if (arch_has_arm_nop (globals))
10708
0
    value |= 0x0320f000;
10709
0
        else
10710
0
    value |= 0x01a00000; /* Using pre-UAL nop: mov r0, r0.  */
10711
0
      }
10712
0
    else
10713
0
      {
10714
        /* Perform a signed range check.  */
10715
0
        if (   signed_addend >   ((bfd_signed_vma)  (howto->dst_mask >> 1))
10716
0
      || signed_addend < - ((bfd_signed_vma) ((howto->dst_mask + 1) >> 1)))
10717
0
    return bfd_reloc_overflow;
10718
10719
0
        addend = (value & 2);
10720
10721
0
        value = (signed_addend & howto->dst_mask)
10722
0
    | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask));
10723
10724
0
        if (r_type == R_ARM_CALL)
10725
0
    {
10726
      /* Set the H bit in the BLX instruction.  */
10727
0
      if (branch_type == ST_BRANCH_TO_THUMB)
10728
0
        {
10729
0
          if (addend)
10730
0
      value |= (1 << 24);
10731
0
          else
10732
0
      value &= ~(bfd_vma)(1 << 24);
10733
0
        }
10734
10735
      /* Select the correct instruction (BL or BLX).  */
10736
      /* Only if we are not handling a BL to a stub. In this
10737
         case, mode switching is performed by the stub.  */
10738
0
      if (branch_type == ST_BRANCH_TO_THUMB && !stub_entry)
10739
0
        value |= (1 << 28);
10740
0
      else if (stub_entry || branch_type != ST_BRANCH_UNKNOWN)
10741
0
        {
10742
0
          value &= ~(bfd_vma)(1 << 28);
10743
0
          value |= (1 << 24);
10744
0
        }
10745
0
    }
10746
0
      }
10747
0
    }
10748
0
    break;
10749
10750
0
  case R_ARM_ABS32:
10751
0
    value += addend;
10752
0
    if (branch_type == ST_BRANCH_TO_THUMB)
10753
0
      value |= 1;
10754
0
    break;
10755
10756
0
  case R_ARM_ABS32_NOI:
10757
0
    value += addend;
10758
0
    break;
10759
10760
0
  case R_ARM_REL32:
10761
0
    value += addend;
10762
0
    if (branch_type == ST_BRANCH_TO_THUMB)
10763
0
      value |= 1;
10764
0
    value -= (input_section->output_section->vma
10765
0
        + input_section->output_offset + rel->r_offset);
10766
0
    break;
10767
10768
0
  case R_ARM_REL32_NOI:
10769
0
    value += addend;
10770
0
    value -= (input_section->output_section->vma
10771
0
        + input_section->output_offset + rel->r_offset);
10772
0
    break;
10773
10774
0
  case R_ARM_PREL31:
10775
0
    value -= (input_section->output_section->vma
10776
0
        + input_section->output_offset + rel->r_offset);
10777
0
    value += signed_addend;
10778
0
    if (! h || h->root.type != bfd_link_hash_undefweak)
10779
0
      {
10780
        /* Check for overflow.  */
10781
0
        if ((value ^ (value >> 1)) & (1 << 30))
10782
0
    return bfd_reloc_overflow;
10783
0
      }
10784
0
    value &= 0x7fffffff;
10785
0
    value |= (bfd_get_32 (input_bfd, hit_data) & 0x80000000);
10786
0
    if (branch_type == ST_BRANCH_TO_THUMB)
10787
0
      value |= 1;
10788
0
    break;
10789
0
  }
10790
10791
0
      bfd_put_32 (input_bfd, value, hit_data);
10792
0
      return bfd_reloc_ok;
10793
10794
0
    case R_ARM_ABS8:
10795
0
      value += addend;
10796
10797
      /* There is no way to tell whether the user intended to use a signed or
10798
   unsigned addend.  When checking for overflow we accept either,
10799
   as specified by the AAELF.  */
10800
0
      if ((long) value > 0xff || (long) value < -0x80)
10801
0
  return bfd_reloc_overflow;
10802
10803
0
      bfd_put_8 (input_bfd, value, hit_data);
10804
0
      return bfd_reloc_ok;
10805
10806
0
    case R_ARM_ABS16:
10807
0
      value += addend;
10808
10809
      /* See comment for R_ARM_ABS8.  */
10810
0
      if ((long) value > 0xffff || (long) value < -0x8000)
10811
0
  return bfd_reloc_overflow;
10812
10813
0
      bfd_put_16 (input_bfd, value, hit_data);
10814
0
      return bfd_reloc_ok;
10815
10816
0
    case R_ARM_THM_ABS5:
10817
      /* Support ldr and str instructions for the thumb.  */
10818
0
      if (globals->use_rel)
10819
0
  {
10820
    /* Need to refetch addend.  */
10821
0
    addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
10822
    /* ??? Need to determine shift amount from operand size.  */
10823
0
    addend >>= howto->rightshift;
10824
0
  }
10825
0
      value += addend;
10826
10827
      /* ??? Isn't value unsigned?  */
10828
0
      if ((long) value > 0x1f || (long) value < -0x10)
10829
0
  return bfd_reloc_overflow;
10830
10831
      /* ??? Value needs to be properly shifted into place first.  */
10832
0
      value |= bfd_get_16 (input_bfd, hit_data) & 0xf83f;
10833
0
      bfd_put_16 (input_bfd, value, hit_data);
10834
0
      return bfd_reloc_ok;
10835
10836
0
    case R_ARM_THM_ALU_PREL_11_0:
10837
      /* Corresponds to: addw.w reg, pc, #offset (and similarly for subw).  */
10838
0
      {
10839
0
  bfd_vma insn;
10840
0
  bfd_signed_vma relocation;
10841
10842
0
  insn = (bfd_get_16 (input_bfd, hit_data) << 16)
10843
0
       | bfd_get_16 (input_bfd, hit_data + 2);
10844
10845
0
  if (globals->use_rel)
10846
0
    {
10847
0
      signed_addend = (insn & 0xff) | ((insn & 0x7000) >> 4)
10848
0
        | ((insn & (1 << 26)) >> 15);
10849
0
      if (insn & 0xf00000)
10850
0
        signed_addend = -signed_addend;
10851
0
    }
10852
10853
0
  relocation = value + signed_addend;
10854
0
  relocation -= Pa (input_section->output_section->vma
10855
0
        + input_section->output_offset
10856
0
        + rel->r_offset);
10857
10858
  /* PR 21523: Use an absolute value.  The user of this reloc will
10859
     have already selected an ADD or SUB insn appropriately.  */
10860
0
  value = llabs (relocation);
10861
10862
0
  if (value >= 0x1000)
10863
0
    return bfd_reloc_overflow;
10864
10865
  /* Destination is Thumb.  Force bit 0 to 1 to reflect this.  */
10866
0
  if (branch_type == ST_BRANCH_TO_THUMB)
10867
0
    value |= 1;
10868
10869
0
  insn = (insn & 0xfb0f8f00) | (value & 0xff)
10870
0
       | ((value & 0x700) << 4)
10871
0
       | ((value & 0x800) << 15);
10872
0
  if (relocation < 0)
10873
0
    insn |= 0xa00000;
10874
10875
0
  bfd_put_16 (input_bfd, insn >> 16, hit_data);
10876
0
  bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
10877
10878
0
  return bfd_reloc_ok;
10879
0
      }
10880
10881
0
    case R_ARM_THM_PC8:
10882
      /* PR 10073:  This reloc is not generated by the GNU toolchain,
10883
   but it is supported for compatibility with third party libraries
10884
   generated by other compilers, specifically the ARM/IAR.  */
10885
0
      {
10886
0
  bfd_vma insn;
10887
0
  bfd_signed_vma relocation;
10888
10889
0
  insn = bfd_get_16 (input_bfd, hit_data);
10890
10891
0
  if (globals->use_rel)
10892
0
    addend = ((((insn & 0x00ff) << 2) + 4) & 0x3ff) -4;
10893
10894
0
  relocation = value + addend;
10895
0
  relocation -= Pa (input_section->output_section->vma
10896
0
        + input_section->output_offset
10897
0
        + rel->r_offset);
10898
10899
0
  value = relocation;
10900
10901
  /* We do not check for overflow of this reloc.  Although strictly
10902
     speaking this is incorrect, it appears to be necessary in order
10903
     to work with IAR generated relocs.  Since GCC and GAS do not
10904
     generate R_ARM_THM_PC8 relocs, the lack of a check should not be
10905
     a problem for them.  */
10906
0
  value &= 0x3fc;
10907
10908
0
  insn = (insn & 0xff00) | (value >> 2);
10909
10910
0
  bfd_put_16 (input_bfd, insn, hit_data);
10911
10912
0
  return bfd_reloc_ok;
10913
0
      }
10914
10915
0
    case R_ARM_THM_PC12:
10916
      /* Corresponds to: ldr.w reg, [pc, #offset].  */
10917
0
      {
10918
0
  bfd_vma insn;
10919
0
  bfd_signed_vma relocation;
10920
10921
0
  insn = (bfd_get_16 (input_bfd, hit_data) << 16)
10922
0
       | bfd_get_16 (input_bfd, hit_data + 2);
10923
10924
0
  if (globals->use_rel)
10925
0
    {
10926
0
      signed_addend = insn & 0xfff;
10927
0
      if (!(insn & (1 << 23)))
10928
0
        signed_addend = -signed_addend;
10929
0
    }
10930
10931
0
  relocation = value + signed_addend;
10932
0
  relocation -= Pa (input_section->output_section->vma
10933
0
        + input_section->output_offset
10934
0
        + rel->r_offset);
10935
10936
0
  value = relocation;
10937
10938
0
  if (value >= 0x1000)
10939
0
    return bfd_reloc_overflow;
10940
10941
0
  insn = (insn & 0xff7ff000) | value;
10942
0
  if (relocation >= 0)
10943
0
    insn |= (1 << 23);
10944
10945
0
  bfd_put_16 (input_bfd, insn >> 16, hit_data);
10946
0
  bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
10947
10948
0
  return bfd_reloc_ok;
10949
0
      }
10950
10951
0
    case R_ARM_THM_XPC22:
10952
0
    case R_ARM_THM_CALL:
10953
0
    case R_ARM_THM_JUMP24:
10954
      /* Thumb BL (branch long instruction).  */
10955
0
      {
10956
0
  bfd_vma relocation;
10957
0
  bfd_vma reloc_sign;
10958
0
  bool overflow = false;
10959
0
  bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
10960
0
  bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
10961
0
  bfd_signed_vma reloc_signed_max;
10962
0
  bfd_signed_vma reloc_signed_min;
10963
0
  bfd_vma check;
10964
0
  bfd_signed_vma signed_check;
10965
0
  int bitsize;
10966
0
  const int thumb2 = using_thumb2 (globals);
10967
0
  const int thumb2_bl = using_thumb2_bl (globals);
10968
10969
  /* A branch to an undefined weak symbol is turned into a jump to
10970
     the next instruction unless a PLT entry will be created.
10971
     The jump to the next instruction is optimized as a NOP.W for
10972
     Thumb-2 enabled architectures.  */
10973
0
  if (h && h->root.type == bfd_link_hash_undefweak
10974
0
      && plt_offset == (bfd_vma) -1)
10975
0
    {
10976
0
      if (thumb2)
10977
0
        {
10978
0
    bfd_put_16 (input_bfd, 0xf3af, hit_data);
10979
0
    bfd_put_16 (input_bfd, 0x8000, hit_data + 2);
10980
0
        }
10981
0
      else
10982
0
        {
10983
0
    bfd_put_16 (input_bfd, 0xe000, hit_data);
10984
0
    bfd_put_16 (input_bfd, 0xbf00, hit_data + 2);
10985
0
        }
10986
0
      return bfd_reloc_ok;
10987
0
    }
10988
10989
  /* Fetch the addend.  We use the Thumb-2 encoding (backwards compatible
10990
     with Thumb-1) involving the J1 and J2 bits.  */
10991
0
  if (globals->use_rel)
10992
0
    {
10993
0
      bfd_vma s = (upper_insn & (1 << 10)) >> 10;
10994
0
      bfd_vma upper = upper_insn & 0x3ff;
10995
0
      bfd_vma lower = lower_insn & 0x7ff;
10996
0
      bfd_vma j1 = (lower_insn & (1 << 13)) >> 13;
10997
0
      bfd_vma j2 = (lower_insn & (1 << 11)) >> 11;
10998
0
      bfd_vma i1 = j1 ^ s ? 0 : 1;
10999
0
      bfd_vma i2 = j2 ^ s ? 0 : 1;
11000
11001
0
      addend = (i1 << 23) | (i2 << 22) | (upper << 12) | (lower << 1);
11002
      /* Sign extend.  */
11003
0
      addend = (addend | ((s ? 0 : 1) << 24)) - (1 << 24);
11004
11005
0
      signed_addend = addend;
11006
0
    }
11007
11008
0
  if (r_type == R_ARM_THM_XPC22)
11009
0
    {
11010
      /* Check for Thumb to Thumb call.  */
11011
      /* FIXME: Should we translate the instruction into a BL
11012
         instruction instead ?  */
11013
0
      if (branch_type == ST_BRANCH_TO_THUMB)
11014
0
        _bfd_error_handler
11015
0
    (_("%pB: warning: %s BLX instruction targets"
11016
0
       " %s function '%s'"),
11017
0
     input_bfd, "Thumb",
11018
0
     "Thumb", h ? h->root.root.string : "(local)");
11019
0
    }
11020
0
  else
11021
0
    {
11022
      /* If it is not a call to Thumb, assume call to Arm.
11023
         If it is a call relative to a section name, then it is not a
11024
         function call at all, but rather a long jump.  Calls through
11025
         the PLT do not require stubs.  */
11026
0
      if (branch_type == ST_BRANCH_TO_ARM && plt_offset == (bfd_vma) -1)
11027
0
        {
11028
0
    if (globals->use_blx && r_type == R_ARM_THM_CALL)
11029
0
      {
11030
        /* Convert BL to BLX.  */
11031
0
        lower_insn = (lower_insn & ~0x1000) | 0x0800;
11032
0
      }
11033
0
    else if ((   r_type != R_ARM_THM_CALL)
11034
0
       && (r_type != R_ARM_THM_JUMP24))
11035
0
      {
11036
0
        if (elf32_thumb_to_arm_stub
11037
0
      (info, sym_name, input_bfd, output_bfd, input_section,
11038
0
       hit_data, sym_sec, rel->r_offset, signed_addend, value,
11039
0
       error_message))
11040
0
          return bfd_reloc_ok;
11041
0
        else
11042
0
          return bfd_reloc_dangerous;
11043
0
      }
11044
0
        }
11045
0
      else if (branch_type == ST_BRANCH_TO_THUMB
11046
0
         && globals->use_blx
11047
0
         && r_type == R_ARM_THM_CALL)
11048
0
        {
11049
    /* Make sure this is a BL.  */
11050
0
    lower_insn |= 0x1800;
11051
0
        }
11052
0
    }
11053
11054
0
  enum elf32_arm_stub_type stub_type = arm_stub_none;
11055
0
  if (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24)
11056
0
    {
11057
      /* Check if a stub has to be inserted because the destination
11058
         is too far.  */
11059
0
      struct elf32_arm_stub_hash_entry *stub_entry;
11060
0
      struct elf32_arm_link_hash_entry *hash;
11061
11062
0
      hash = (struct elf32_arm_link_hash_entry *) h;
11063
11064
0
      stub_type = arm_type_of_stub (info, input_section, rel,
11065
0
            st_type, &branch_type,
11066
0
            hash, value, sym_sec,
11067
0
            input_bfd, sym_name);
11068
11069
0
      if (stub_type != arm_stub_none)
11070
0
        {
11071
    /* The target is out of reach or we are changing modes, so
11072
       redirect the branch to the local stub for this
11073
       function.  */
11074
0
    stub_entry = elf32_arm_get_stub_entry (input_section,
11075
0
                   sym_sec, h,
11076
0
                   rel, globals,
11077
0
                   stub_type);
11078
0
    if (stub_entry != NULL)
11079
0
      {
11080
0
        value = (stub_entry->stub_offset
11081
0
           + stub_entry->stub_sec->output_offset
11082
0
           + stub_entry->stub_sec->output_section->vma);
11083
11084
0
        if (plt_offset != (bfd_vma) -1)
11085
0
          *unresolved_reloc_p = false;
11086
0
      }
11087
11088
    /* If this call becomes a call to Arm, force BLX.  */
11089
0
    if (globals->use_blx && (r_type == R_ARM_THM_CALL))
11090
0
      {
11091
0
        if ((stub_entry
11092
0
       && !arm_stub_is_thumb (stub_entry->stub_type))
11093
0
      || branch_type != ST_BRANCH_TO_THUMB)
11094
0
          lower_insn = (lower_insn & ~0x1000) | 0x0800;
11095
0
      }
11096
0
        }
11097
0
    }
11098
11099
  /* Handle calls via the PLT.  */
11100
0
  if (stub_type == arm_stub_none && plt_offset != (bfd_vma) -1)
11101
0
    {
11102
0
      value = (splt->output_section->vma
11103
0
         + splt->output_offset
11104
0
         + plt_offset);
11105
11106
0
      if (globals->use_blx
11107
0
    && r_type == R_ARM_THM_CALL
11108
0
    && ! using_thumb_only (globals))
11109
0
        {
11110
    /* If the Thumb BLX instruction is available, convert
11111
       the BL to a BLX instruction to call the ARM-mode
11112
       PLT entry.  */
11113
0
    lower_insn = (lower_insn & ~0x1000) | 0x0800;
11114
0
    branch_type = ST_BRANCH_TO_ARM;
11115
0
        }
11116
0
      else
11117
0
        {
11118
0
    if (! using_thumb_only (globals))
11119
      /* Target the Thumb stub before the ARM PLT entry.  */
11120
0
      value -= PLT_THUMB_STUB_SIZE;
11121
0
    branch_type = ST_BRANCH_TO_THUMB;
11122
0
        }
11123
0
      *unresolved_reloc_p = false;
11124
0
    }
11125
11126
0
  relocation = value + signed_addend;
11127
11128
0
  relocation -= (input_section->output_section->vma
11129
0
           + input_section->output_offset
11130
0
           + rel->r_offset);
11131
11132
0
  check = relocation >> howto->rightshift;
11133
11134
  /* If this is a signed value, the rightshift just dropped
11135
     leading 1 bits (assuming twos complement).  */
11136
0
  if ((bfd_signed_vma) relocation >= 0)
11137
0
    signed_check = check;
11138
0
  else
11139
0
    signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
11140
11141
  /* Calculate the permissable maximum and minimum values for
11142
     this relocation according to whether we're relocating for
11143
     Thumb-2 or not.  */
11144
0
  bitsize = howto->bitsize;
11145
0
  if (!thumb2_bl)
11146
0
    bitsize -= 2;
11147
0
  reloc_signed_max = (1 << (bitsize - 1)) - 1;
11148
0
  reloc_signed_min = ~reloc_signed_max;
11149
11150
  /* Assumes two's complement.  */
11151
0
  if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
11152
0
    overflow = true;
11153
11154
0
  if ((lower_insn & 0x5000) == 0x4000)
11155
    /* For a BLX instruction, make sure that the relocation is rounded up
11156
       to a word boundary.  This follows the semantics of the instruction
11157
       which specifies that bit 1 of the target address will come from bit
11158
       1 of the base address.  */
11159
0
    relocation = (relocation + 2) & ~ 3;
11160
11161
  /* Put RELOCATION back into the insn.  Assumes two's complement.
11162
     We use the Thumb-2 encoding, which is safe even if dealing with
11163
     a Thumb-1 instruction by virtue of our overflow check above.  */
11164
0
  reloc_sign = (signed_check < 0) ? 1 : 0;
11165
0
  upper_insn = (upper_insn & ~(bfd_vma) 0x7ff)
11166
0
         | ((relocation >> 12) & 0x3ff)
11167
0
         | (reloc_sign << 10);
11168
0
  lower_insn = (lower_insn & ~(bfd_vma) 0x2fff)
11169
0
         | (((!((relocation >> 23) & 1)) ^ reloc_sign) << 13)
11170
0
         | (((!((relocation >> 22) & 1)) ^ reloc_sign) << 11)
11171
0
         | ((relocation >> 1) & 0x7ff);
11172
11173
  /* Put the relocated value back in the object file:  */
11174
0
  bfd_put_16 (input_bfd, upper_insn, hit_data);
11175
0
  bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
11176
11177
0
  return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
11178
0
      }
11179
0
      break;
11180
11181
0
    case R_ARM_THM_JUMP19:
11182
      /* Thumb32 conditional branch instruction.  */
11183
0
      {
11184
0
  bfd_vma relocation;
11185
0
  bool overflow = false;
11186
0
  bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
11187
0
  bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
11188
0
  bfd_signed_vma reloc_signed_max = 0xffffe;
11189
0
  bfd_signed_vma reloc_signed_min = -0x100000;
11190
0
  bfd_signed_vma signed_check;
11191
0
  enum elf32_arm_stub_type stub_type = arm_stub_none;
11192
0
  struct elf32_arm_stub_hash_entry *stub_entry;
11193
0
  struct elf32_arm_link_hash_entry *hash;
11194
11195
  /* Need to refetch the addend, reconstruct the top three bits,
11196
     and squish the two 11 bit pieces together.  */
11197
0
  if (globals->use_rel)
11198
0
    {
11199
0
      bfd_vma S     = (upper_insn & 0x0400) >> 10;
11200
0
      bfd_vma upper = (upper_insn & 0x003f);
11201
0
      bfd_vma J1    = (lower_insn & 0x2000) >> 13;
11202
0
      bfd_vma J2    = (lower_insn & 0x0800) >> 11;
11203
0
      bfd_vma lower = (lower_insn & 0x07ff);
11204
11205
0
      upper |= J1 << 6;
11206
0
      upper |= J2 << 7;
11207
0
      upper |= (!S) << 8;
11208
0
      upper -= 0x0100; /* Sign extend.  */
11209
11210
0
      addend = (upper << 12) | (lower << 1);
11211
0
      signed_addend = addend;
11212
0
    }
11213
11214
  /* Handle calls via the PLT.  */
11215
0
  if (plt_offset != (bfd_vma) -1)
11216
0
    {
11217
0
      value = (splt->output_section->vma
11218
0
         + splt->output_offset
11219
0
         + plt_offset);
11220
      /* Target the Thumb stub before the ARM PLT entry.  */
11221
0
      value -= PLT_THUMB_STUB_SIZE;
11222
0
      *unresolved_reloc_p = false;
11223
0
    }
11224
11225
0
  hash = (struct elf32_arm_link_hash_entry *)h;
11226
11227
0
  stub_type = arm_type_of_stub (info, input_section, rel,
11228
0
              st_type, &branch_type,
11229
0
              hash, value, sym_sec,
11230
0
              input_bfd, sym_name);
11231
0
  if (stub_type != arm_stub_none)
11232
0
    {
11233
0
      stub_entry = elf32_arm_get_stub_entry (input_section,
11234
0
               sym_sec, h,
11235
0
               rel, globals,
11236
0
               stub_type);
11237
0
      if (stub_entry != NULL)
11238
0
        {
11239
0
    value = (stub_entry->stub_offset
11240
0
      + stub_entry->stub_sec->output_offset
11241
0
      + stub_entry->stub_sec->output_section->vma);
11242
0
        }
11243
0
    }
11244
11245
0
  relocation = value + signed_addend;
11246
0
  relocation -= (input_section->output_section->vma
11247
0
           + input_section->output_offset
11248
0
           + rel->r_offset);
11249
0
  signed_check = (bfd_signed_vma) relocation;
11250
11251
0
  if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
11252
0
    overflow = true;
11253
11254
  /* Put RELOCATION back into the insn.  */
11255
0
  {
11256
0
    bfd_vma S  = (relocation & 0x00100000) >> 20;
11257
0
    bfd_vma J2 = (relocation & 0x00080000) >> 19;
11258
0
    bfd_vma J1 = (relocation & 0x00040000) >> 18;
11259
0
    bfd_vma hi = (relocation & 0x0003f000) >> 12;
11260
0
    bfd_vma lo = (relocation & 0x00000ffe) >>  1;
11261
11262
0
    upper_insn = (upper_insn & 0xfbc0) | (S << 10) | hi;
11263
0
    lower_insn = (lower_insn & 0xd000) | (J1 << 13) | (J2 << 11) | lo;
11264
0
  }
11265
11266
  /* Put the relocated value back in the object file:  */
11267
0
  bfd_put_16 (input_bfd, upper_insn, hit_data);
11268
0
  bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
11269
11270
0
  return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
11271
0
      }
11272
11273
0
    case R_ARM_THM_JUMP11:
11274
0
    case R_ARM_THM_JUMP8:
11275
0
    case R_ARM_THM_JUMP6:
11276
      /* Thumb B (branch) instruction).  */
11277
0
      {
11278
0
  bfd_signed_vma relocation;
11279
0
  bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
11280
0
  bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
11281
0
  bfd_signed_vma signed_check;
11282
11283
  /* CZB cannot jump backward.  */
11284
0
  if (r_type == R_ARM_THM_JUMP6)
11285
0
    {
11286
0
      reloc_signed_min = 0;
11287
0
      if (globals->use_rel)
11288
0
        signed_addend = ((addend & 0x200) >> 3) | ((addend & 0xf8) >> 2);
11289
0
    }
11290
11291
0
  relocation = value + signed_addend;
11292
11293
0
  relocation -= (input_section->output_section->vma
11294
0
           + input_section->output_offset
11295
0
           + rel->r_offset);
11296
11297
0
  relocation >>= howto->rightshift;
11298
0
  signed_check = relocation;
11299
11300
0
  if (r_type == R_ARM_THM_JUMP6)
11301
0
    relocation = ((relocation & 0x0020) << 4) | ((relocation & 0x001f) << 3);
11302
0
  else
11303
0
    relocation &= howto->dst_mask;
11304
0
  relocation |= (bfd_get_16 (input_bfd, hit_data) & (~ howto->dst_mask));
11305
11306
0
  bfd_put_16 (input_bfd, relocation, hit_data);
11307
11308
  /* Assumes two's complement.  */
11309
0
  if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
11310
0
    return bfd_reloc_overflow;
11311
11312
0
  return bfd_reloc_ok;
11313
0
      }
11314
11315
0
    case R_ARM_ALU_PCREL7_0:
11316
0
    case R_ARM_ALU_PCREL15_8:
11317
0
    case R_ARM_ALU_PCREL23_15:
11318
0
      {
11319
0
  bfd_vma insn;
11320
0
  bfd_vma relocation;
11321
11322
0
  insn = bfd_get_32 (input_bfd, hit_data);
11323
0
  if (globals->use_rel)
11324
0
    {
11325
      /* Extract the addend.  */
11326
0
      addend = (insn & 0xff) << ((insn & 0xf00) >> 7);
11327
0
      signed_addend = addend;
11328
0
    }
11329
0
  relocation = value + signed_addend;
11330
11331
0
  relocation -= (input_section->output_section->vma
11332
0
           + input_section->output_offset
11333
0
           + rel->r_offset);
11334
0
  insn = (insn & ~0xfff)
11335
0
         | ((howto->bitpos << 7) & 0xf00)
11336
0
         | ((relocation >> howto->bitpos) & 0xff);
11337
0
  bfd_put_32 (input_bfd, value, hit_data);
11338
0
      }
11339
0
      return bfd_reloc_ok;
11340
11341
0
    case R_ARM_GNU_VTINHERIT:
11342
0
    case R_ARM_GNU_VTENTRY:
11343
0
      return bfd_reloc_ok;
11344
11345
0
    case R_ARM_GOTOFF32:
11346
      /* Relocation is relative to the start of the
11347
   global offset table.  */
11348
11349
0
      BFD_ASSERT (sgot != NULL);
11350
0
      if (sgot == NULL)
11351
0
  return bfd_reloc_notsupported;
11352
11353
      /* If we are addressing a Thumb function, we need to adjust the
11354
   address by one, so that attempts to call the function pointer will
11355
   correctly interpret it as Thumb code.  */
11356
0
      if (branch_type == ST_BRANCH_TO_THUMB)
11357
0
  value += 1;
11358
11359
      /* Note that sgot->output_offset is not involved in this
11360
   calculation.  We always want the start of .got.  If we
11361
   define _GLOBAL_OFFSET_TABLE in a different way, as is
11362
   permitted by the ABI, we might have to change this
11363
   calculation.  */
11364
0
      value -= sgot->output_section->vma;
11365
0
      return _bfd_final_link_relocate (howto, input_bfd, input_section,
11366
0
               contents, rel->r_offset, value,
11367
0
               rel->r_addend);
11368
11369
0
    case R_ARM_GOTPC:
11370
      /* Use global offset table as symbol value.  */
11371
0
      BFD_ASSERT (sgot != NULL);
11372
11373
0
      if (sgot == NULL)
11374
0
  return bfd_reloc_notsupported;
11375
11376
0
      *unresolved_reloc_p = false;
11377
0
      value = sgot->output_section->vma;
11378
0
      return _bfd_final_link_relocate (howto, input_bfd, input_section,
11379
0
               contents, rel->r_offset, value,
11380
0
               rel->r_addend);
11381
11382
0
    case R_ARM_GOT32:
11383
0
    case R_ARM_GOT_PREL:
11384
      /* Relocation is to the entry for this symbol in the
11385
   global offset table.  */
11386
0
      if (sgot == NULL)
11387
0
  return bfd_reloc_notsupported;
11388
11389
0
      if (dynreloc_st_type == STT_GNU_IFUNC
11390
0
    && plt_offset != (bfd_vma) -1
11391
0
    && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)))
11392
0
  {
11393
    /* We have a relocation against a locally-binding STT_GNU_IFUNC
11394
       symbol, and the relocation resolves directly to the runtime
11395
       target rather than to the .iplt entry.  This means that any
11396
       .got entry would be the same value as the .igot.plt entry,
11397
       so there's no point creating both.  */
11398
0
    sgot = globals->root.igotplt;
11399
0
    value = sgot->output_offset + gotplt_offset;
11400
0
  }
11401
0
      else if (h != NULL)
11402
0
  {
11403
0
    bfd_vma off;
11404
11405
0
    off = h->got.offset;
11406
0
    BFD_ASSERT (off != (bfd_vma) -1);
11407
0
    if ((off & 1) != 0)
11408
0
      {
11409
        /* We have already processsed one GOT relocation against
11410
     this symbol.  */
11411
0
        off &= ~1;
11412
0
        if (globals->root.dynamic_sections_created
11413
0
      && !SYMBOL_REFERENCES_LOCAL (info, h))
11414
0
    *unresolved_reloc_p = false;
11415
0
      }
11416
0
    else
11417
0
      {
11418
0
        Elf_Internal_Rela outrel;
11419
0
        int isrofixup = 0;
11420
11421
0
        if (((h->dynindx != -1) || globals->fdpic_p)
11422
0
      && !SYMBOL_REFERENCES_LOCAL (info, h))
11423
0
    {
11424
      /* If the symbol doesn't resolve locally in a static
11425
         object, we have an undefined reference.  If the
11426
         symbol doesn't resolve locally in a dynamic object,
11427
         it should be resolved by the dynamic linker.  */
11428
0
      if (globals->root.dynamic_sections_created)
11429
0
        {
11430
0
          outrel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
11431
0
          *unresolved_reloc_p = false;
11432
0
        }
11433
0
      else
11434
0
        outrel.r_info = 0;
11435
0
      outrel.r_addend = 0;
11436
0
    }
11437
0
        else
11438
0
    {
11439
0
      if (dynreloc_st_type == STT_GNU_IFUNC)
11440
0
        outrel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
11441
0
      else if (bfd_link_pic (info)
11442
0
         && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
11443
0
        outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
11444
0
      else
11445
0
        {
11446
0
          outrel.r_info = 0;
11447
0
          if (globals->fdpic_p)
11448
0
      isrofixup = 1;
11449
0
        }
11450
0
      outrel.r_addend = dynreloc_value;
11451
0
    }
11452
11453
        /* The GOT entry is initialized to zero by default.
11454
     See if we should install a different value.  */
11455
0
        if (outrel.r_addend != 0
11456
0
      && (globals->use_rel || outrel.r_info == 0))
11457
0
    {
11458
0
      bfd_put_32 (output_bfd, outrel.r_addend,
11459
0
            sgot->contents + off);
11460
0
      outrel.r_addend = 0;
11461
0
    }
11462
11463
0
        if (isrofixup)
11464
0
    arm_elf_add_rofixup (output_bfd,
11465
0
             elf32_arm_hash_table (info)->srofixup,
11466
0
             sgot->output_section->vma
11467
0
             + sgot->output_offset + off);
11468
11469
0
        else if (outrel.r_info != 0)
11470
0
    {
11471
0
      outrel.r_offset = (sgot->output_section->vma
11472
0
             + sgot->output_offset
11473
0
             + off);
11474
0
      elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
11475
0
    }
11476
11477
0
        h->got.offset |= 1;
11478
0
      }
11479
0
    value = sgot->output_offset + off;
11480
0
  }
11481
0
      else
11482
0
  {
11483
0
    bfd_vma off;
11484
11485
0
    BFD_ASSERT (local_got_offsets != NULL
11486
0
          && local_got_offsets[r_symndx] != (bfd_vma) -1);
11487
11488
0
    off = local_got_offsets[r_symndx];
11489
11490
    /* The offset must always be a multiple of 4.  We use the
11491
       least significant bit to record whether we have already
11492
       generated the necessary reloc.  */
11493
0
    if ((off & 1) != 0)
11494
0
      off &= ~1;
11495
0
    else
11496
0
      {
11497
0
        Elf_Internal_Rela outrel;
11498
0
        int isrofixup = 0;
11499
11500
0
        if (dynreloc_st_type == STT_GNU_IFUNC)
11501
0
    outrel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
11502
0
        else if (bfd_link_pic (info))
11503
0
    outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
11504
0
        else
11505
0
    {
11506
0
      outrel.r_info = 0;
11507
0
      if (globals->fdpic_p)
11508
0
        isrofixup = 1;
11509
0
    }
11510
11511
        /* The GOT entry is initialized to zero by default.
11512
     See if we should install a different value.  */
11513
0
        if (globals->use_rel || outrel.r_info == 0)
11514
0
    bfd_put_32 (output_bfd, dynreloc_value, sgot->contents + off);
11515
11516
0
        if (isrofixup)
11517
0
    arm_elf_add_rofixup (output_bfd,
11518
0
             globals->srofixup,
11519
0
             sgot->output_section->vma
11520
0
             + sgot->output_offset + off);
11521
11522
0
        else if (outrel.r_info != 0)
11523
0
    {
11524
0
      outrel.r_addend = addend + dynreloc_value;
11525
0
      outrel.r_offset = (sgot->output_section->vma
11526
0
             + sgot->output_offset
11527
0
             + off);
11528
0
      elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
11529
0
    }
11530
11531
0
        local_got_offsets[r_symndx] |= 1;
11532
0
      }
11533
11534
0
    value = sgot->output_offset + off;
11535
0
  }
11536
0
      if (r_type != R_ARM_GOT32)
11537
0
  value += sgot->output_section->vma;
11538
11539
0
      return _bfd_final_link_relocate (howto, input_bfd, input_section,
11540
0
               contents, rel->r_offset, value,
11541
0
               rel->r_addend);
11542
11543
0
    case R_ARM_TLS_LDO32:
11544
0
      value = value - dtpoff_base (info);
11545
11546
0
      return _bfd_final_link_relocate (howto, input_bfd, input_section,
11547
0
               contents, rel->r_offset, value,
11548
0
               rel->r_addend);
11549
11550
0
    case R_ARM_TLS_LDM32:
11551
0
    case R_ARM_TLS_LDM32_FDPIC:
11552
0
      {
11553
0
  bfd_vma off;
11554
11555
0
  if (sgot == NULL)
11556
0
    abort ();
11557
11558
0
  off = globals->tls_ldm_got.offset;
11559
11560
0
  if ((off & 1) != 0)
11561
0
    off &= ~1;
11562
0
  else
11563
0
    {
11564
      /* If we don't know the module number, create a relocation
11565
         for it.  */
11566
0
      if (bfd_link_dll (info))
11567
0
        {
11568
0
    Elf_Internal_Rela outrel;
11569
11570
0
    if (srelgot == NULL)
11571
0
      abort ();
11572
11573
0
    outrel.r_addend = 0;
11574
0
    outrel.r_offset = (sgot->output_section->vma
11575
0
           + sgot->output_offset + off);
11576
0
    outrel.r_info = ELF32_R_INFO (0, R_ARM_TLS_DTPMOD32);
11577
11578
0
    if (globals->use_rel)
11579
0
      bfd_put_32 (output_bfd, outrel.r_addend,
11580
0
            sgot->contents + off);
11581
11582
0
    elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
11583
0
        }
11584
0
      else
11585
0
        bfd_put_32 (output_bfd, 1, sgot->contents + off);
11586
11587
0
      globals->tls_ldm_got.offset |= 1;
11588
0
    }
11589
11590
0
  if (r_type == R_ARM_TLS_LDM32_FDPIC)
11591
0
    {
11592
0
      bfd_put_32 (output_bfd,
11593
0
      globals->root.sgot->output_offset + off,
11594
0
      contents + rel->r_offset);
11595
11596
0
      return bfd_reloc_ok;
11597
0
    }
11598
0
  else
11599
0
    {
11600
0
      value = sgot->output_section->vma + sgot->output_offset + off
11601
0
        - (input_section->output_section->vma
11602
0
     + input_section->output_offset + rel->r_offset);
11603
11604
0
      return _bfd_final_link_relocate (howto, input_bfd, input_section,
11605
0
               contents, rel->r_offset, value,
11606
0
               rel->r_addend);
11607
0
    }
11608
0
      }
11609
11610
0
    case R_ARM_TLS_CALL:
11611
0
    case R_ARM_THM_TLS_CALL:
11612
0
    case R_ARM_TLS_GD32:
11613
0
    case R_ARM_TLS_GD32_FDPIC:
11614
0
    case R_ARM_TLS_IE32:
11615
0
    case R_ARM_TLS_IE32_FDPIC:
11616
0
    case R_ARM_TLS_GOTDESC:
11617
0
    case R_ARM_TLS_DESCSEQ:
11618
0
    case R_ARM_THM_TLS_DESCSEQ:
11619
0
      {
11620
0
  bfd_vma off, offplt;
11621
0
  int indx = 0;
11622
0
  char tls_type;
11623
11624
0
  BFD_ASSERT (sgot != NULL);
11625
11626
0
  if (h != NULL)
11627
0
    {
11628
0
      bool dyn;
11629
0
      dyn = globals->root.dynamic_sections_created;
11630
0
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
11631
0
             bfd_link_pic (info),
11632
0
             h)
11633
0
    && (!bfd_link_pic (info)
11634
0
        || !SYMBOL_REFERENCES_LOCAL (info, h)))
11635
0
        {
11636
0
    *unresolved_reloc_p = false;
11637
0
    indx = h->dynindx;
11638
0
        }
11639
0
      off = h->got.offset;
11640
0
      offplt = elf32_arm_hash_entry (h)->tlsdesc_got;
11641
0
      tls_type = ((struct elf32_arm_link_hash_entry *) h)->tls_type;
11642
0
    }
11643
0
  else
11644
0
    {
11645
0
      BFD_ASSERT (local_got_offsets != NULL);
11646
11647
0
      if (r_symndx >= elf32_arm_num_entries (input_bfd))
11648
0
        {
11649
0
    _bfd_error_handler (_("\
11650
0
%pB: expected symbol index in range 0..%lu but found local symbol with index %lu"),
11651
0
            input_bfd,
11652
0
            (unsigned long) elf32_arm_num_entries (input_bfd),
11653
0
            r_symndx);
11654
0
    return false;
11655
0
        }
11656
0
      off = local_got_offsets[r_symndx];
11657
0
      offplt = local_tlsdesc_gotents[r_symndx];
11658
0
      tls_type = elf32_arm_local_got_tls_type (input_bfd)[r_symndx];
11659
0
    }
11660
11661
  /* Linker relaxations happens from one of the
11662
     R_ARM_{GOTDESC,CALL,DESCSEQ} relocations to IE or LE.  */
11663
0
  if (ELF32_R_TYPE (rel->r_info) != r_type)
11664
0
    tls_type = GOT_TLS_IE;
11665
11666
0
  BFD_ASSERT (tls_type != GOT_UNKNOWN);
11667
11668
0
  if ((off & 1) != 0)
11669
0
    off &= ~1;
11670
0
  else
11671
0
    {
11672
0
      bool need_relocs = false;
11673
0
      Elf_Internal_Rela outrel;
11674
0
      int cur_off = off;
11675
11676
      /* The GOT entries have not been initialized yet.  Do it
11677
         now, and emit any relocations.  If both an IE GOT and a
11678
         GD GOT are necessary, we emit the GD first.  */
11679
11680
0
      if ((bfd_link_dll (info) || indx != 0)
11681
0
    && (h == NULL
11682
0
        || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
11683
0
      && !resolved_to_zero)
11684
0
        || h->root.type != bfd_link_hash_undefweak))
11685
0
        {
11686
0
    need_relocs = true;
11687
0
    BFD_ASSERT (srelgot != NULL);
11688
0
        }
11689
11690
0
      if (tls_type & GOT_TLS_GDESC)
11691
0
        {
11692
0
    bfd_byte *loc;
11693
11694
    /* We should have relaxed, unless this is an undefined
11695
       weak symbol.  */
11696
0
    BFD_ASSERT ((h && (h->root.type == bfd_link_hash_undefweak))
11697
0
          || bfd_link_dll (info));
11698
0
    BFD_ASSERT (globals->sgotplt_jump_table_size + offplt + 8
11699
0
          <= globals->root.sgotplt->size);
11700
11701
0
    outrel.r_addend = 0;
11702
0
    outrel.r_offset = (globals->root.sgotplt->output_section->vma
11703
0
           + globals->root.sgotplt->output_offset
11704
0
           + offplt
11705
0
           + globals->sgotplt_jump_table_size);
11706
11707
0
    outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DESC);
11708
0
    sreloc = globals->root.srelplt;
11709
0
    loc = sreloc->contents;
11710
0
    loc += globals->next_tls_desc_index++ * RELOC_SIZE (globals);
11711
0
    BFD_ASSERT (loc + RELOC_SIZE (globals)
11712
0
         <= sreloc->contents + sreloc->size);
11713
11714
0
    SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
11715
11716
    /* For globals, the first word in the relocation gets
11717
       the relocation index and the top bit set, or zero,
11718
       if we're binding now.  For locals, it gets the
11719
       symbol's offset in the tls section.  */
11720
0
    bfd_put_32 (output_bfd,
11721
0
          !h ? value - elf_hash_table (info)->tls_sec->vma
11722
0
          : info->flags & DF_BIND_NOW ? 0
11723
0
          : 0x80000000 | ELF32_R_SYM (outrel.r_info),
11724
0
          globals->root.sgotplt->contents + offplt
11725
0
          + globals->sgotplt_jump_table_size);
11726
11727
    /* Second word in the relocation is always zero.  */
11728
0
    bfd_put_32 (output_bfd, 0,
11729
0
          globals->root.sgotplt->contents + offplt
11730
0
          + globals->sgotplt_jump_table_size + 4);
11731
0
        }
11732
0
      if (tls_type & GOT_TLS_GD)
11733
0
        {
11734
0
    if (need_relocs)
11735
0
      {
11736
0
        outrel.r_addend = 0;
11737
0
        outrel.r_offset = (sgot->output_section->vma
11738
0
               + sgot->output_offset
11739
0
               + cur_off);
11740
0
        outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DTPMOD32);
11741
11742
0
        if (globals->use_rel)
11743
0
          bfd_put_32 (output_bfd, outrel.r_addend,
11744
0
          sgot->contents + cur_off);
11745
11746
0
        elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
11747
11748
0
        if (indx == 0)
11749
0
          bfd_put_32 (output_bfd, value - dtpoff_base (info),
11750
0
          sgot->contents + cur_off + 4);
11751
0
        else
11752
0
          {
11753
0
      outrel.r_addend = 0;
11754
0
      outrel.r_info = ELF32_R_INFO (indx,
11755
0
                  R_ARM_TLS_DTPOFF32);
11756
0
      outrel.r_offset += 4;
11757
11758
0
      if (globals->use_rel)
11759
0
        bfd_put_32 (output_bfd, outrel.r_addend,
11760
0
              sgot->contents + cur_off + 4);
11761
11762
0
      elf32_arm_add_dynreloc (output_bfd, info,
11763
0
            srelgot, &outrel);
11764
0
          }
11765
0
      }
11766
0
    else
11767
0
      {
11768
        /* If we are not emitting relocations for a
11769
           general dynamic reference, then we must be in a
11770
           static link or an executable link with the
11771
           symbol binding locally.  Mark it as belonging
11772
           to module 1, the executable.  */
11773
0
        bfd_put_32 (output_bfd, 1,
11774
0
        sgot->contents + cur_off);
11775
0
        bfd_put_32 (output_bfd, value - dtpoff_base (info),
11776
0
        sgot->contents + cur_off + 4);
11777
0
      }
11778
11779
0
    cur_off += 8;
11780
0
        }
11781
11782
0
      if (tls_type & GOT_TLS_IE)
11783
0
        {
11784
0
    if (need_relocs)
11785
0
      {
11786
0
        if (indx == 0)
11787
0
          outrel.r_addend = value - dtpoff_base (info);
11788
0
        else
11789
0
          outrel.r_addend = 0;
11790
0
        outrel.r_offset = (sgot->output_section->vma
11791
0
               + sgot->output_offset
11792
0
               + cur_off);
11793
0
        outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_TPOFF32);
11794
11795
0
        if (globals->use_rel)
11796
0
          bfd_put_32 (output_bfd, outrel.r_addend,
11797
0
          sgot->contents + cur_off);
11798
11799
0
        elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
11800
0
      }
11801
0
    else
11802
0
      bfd_put_32 (output_bfd, tpoff (info, value),
11803
0
            sgot->contents + cur_off);
11804
0
    cur_off += 4;
11805
0
        }
11806
11807
0
      if (h != NULL)
11808
0
        h->got.offset |= 1;
11809
0
      else
11810
0
        local_got_offsets[r_symndx] |= 1;
11811
0
    }
11812
11813
0
  if ((tls_type & GOT_TLS_GD) && r_type != R_ARM_TLS_GD32 && r_type != R_ARM_TLS_GD32_FDPIC)
11814
0
    off += 8;
11815
0
  else if (tls_type & GOT_TLS_GDESC)
11816
0
    off = offplt;
11817
11818
0
  if (ELF32_R_TYPE (rel->r_info) == R_ARM_TLS_CALL
11819
0
      || ELF32_R_TYPE (rel->r_info) == R_ARM_THM_TLS_CALL)
11820
0
    {
11821
0
      bfd_signed_vma offset;
11822
      /* TLS stubs are arm mode.  The original symbol is a
11823
         data object, so branch_type is bogus.  */
11824
0
      branch_type = ST_BRANCH_TO_ARM;
11825
0
      enum elf32_arm_stub_type stub_type
11826
0
        = arm_type_of_stub (info, input_section, rel,
11827
0
          st_type, &branch_type,
11828
0
          (struct elf32_arm_link_hash_entry *)h,
11829
0
          globals->tls_trampoline, globals->root.splt,
11830
0
          input_bfd, sym_name);
11831
11832
0
      if (stub_type != arm_stub_none)
11833
0
        {
11834
0
    struct elf32_arm_stub_hash_entry *stub_entry
11835
0
      = elf32_arm_get_stub_entry
11836
0
      (input_section, globals->root.splt, 0, rel,
11837
0
       globals, stub_type);
11838
0
    offset = (stub_entry->stub_offset
11839
0
        + stub_entry->stub_sec->output_offset
11840
0
        + stub_entry->stub_sec->output_section->vma);
11841
0
        }
11842
0
      else
11843
0
        offset = (globals->root.splt->output_section->vma
11844
0
      + globals->root.splt->output_offset
11845
0
      + globals->tls_trampoline);
11846
11847
0
      if (ELF32_R_TYPE (rel->r_info) == R_ARM_TLS_CALL)
11848
0
        {
11849
0
    unsigned long inst;
11850
11851
0
    offset -= (input_section->output_section->vma
11852
0
         + input_section->output_offset
11853
0
         + rel->r_offset + 8);
11854
11855
0
    inst = offset >> 2;
11856
0
    inst &= 0x00ffffff;
11857
0
    value = inst | (globals->use_blx ? 0xfa000000 : 0xeb000000);
11858
0
        }
11859
0
      else
11860
0
        {
11861
    /* Thumb blx encodes the offset in a complicated
11862
       fashion.  */
11863
0
    unsigned upper_insn, lower_insn;
11864
0
    unsigned neg;
11865
11866
0
    offset -= (input_section->output_section->vma
11867
0
         + input_section->output_offset
11868
0
         + rel->r_offset + 4);
11869
11870
0
    if (stub_type != arm_stub_none
11871
0
        && arm_stub_is_thumb (stub_type))
11872
0
      {
11873
0
        lower_insn = 0xd000;
11874
0
      }
11875
0
    else
11876
0
      {
11877
0
        lower_insn = 0xc000;
11878
        /* Round up the offset to a word boundary.  */
11879
0
        offset = (offset + 2) & ~2;
11880
0
      }
11881
11882
0
    neg = offset < 0;
11883
0
    upper_insn = (0xf000
11884
0
            | ((offset >> 12) & 0x3ff)
11885
0
            | (neg << 10));
11886
0
    lower_insn |= (((!((offset >> 23) & 1)) ^ neg) << 13)
11887
0
            | (((!((offset >> 22) & 1)) ^ neg) << 11)
11888
0
            | ((offset >> 1) & 0x7ff);
11889
0
    bfd_put_16 (input_bfd, upper_insn, hit_data);
11890
0
    bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
11891
0
    return bfd_reloc_ok;
11892
0
        }
11893
0
    }
11894
  /* These relocations needs special care, as besides the fact
11895
     they point somewhere in .gotplt, the addend must be
11896
     adjusted accordingly depending on the type of instruction
11897
     we refer to.  */
11898
0
  else if ((r_type == R_ARM_TLS_GOTDESC) && (tls_type & GOT_TLS_GDESC))
11899
0
    {
11900
0
      unsigned long data, insn;
11901
0
      unsigned thumb;
11902
11903
0
      data = bfd_get_signed_32 (input_bfd, hit_data);
11904
0
      thumb = data & 1;
11905
0
      data &= ~1ul;
11906
11907
0
      if (thumb)
11908
0
        {
11909
0
    insn = bfd_get_16 (input_bfd, contents + rel->r_offset - data);
11910
0
    if ((insn & 0xf000) == 0xf000 || (insn & 0xf800) == 0xe800)
11911
0
      insn = (insn << 16)
11912
0
        | bfd_get_16 (input_bfd,
11913
0
          contents + rel->r_offset - data + 2);
11914
0
    if ((insn & 0xf800c000) == 0xf000c000)
11915
      /* bl/blx */
11916
0
      value = -6;
11917
0
    else if ((insn & 0xffffff00) == 0x4400)
11918
      /* add */
11919
0
      value = -5;
11920
0
    else
11921
0
      {
11922
0
        _bfd_error_handler
11923
          /* xgettext:c-format */
11924
0
          (_("%pB(%pA+%#" PRIx64 "): "
11925
0
       "unexpected %s instruction '%#lx' "
11926
0
       "referenced by TLS_GOTDESC"),
11927
0
           input_bfd, input_section, (uint64_t) rel->r_offset,
11928
0
           "Thumb", insn);
11929
0
        return bfd_reloc_notsupported;
11930
0
      }
11931
0
        }
11932
0
      else
11933
0
        {
11934
0
    insn = bfd_get_32 (input_bfd, contents + rel->r_offset - data);
11935
11936
0
    switch (insn >> 24)
11937
0
      {
11938
0
      case 0xeb:  /* bl */
11939
0
      case 0xfa:  /* blx */
11940
0
        value = -4;
11941
0
        break;
11942
11943
0
      case 0xe0:  /* add */
11944
0
        value = -8;
11945
0
        break;
11946
11947
0
      default:
11948
0
        _bfd_error_handler
11949
          /* xgettext:c-format */
11950
0
          (_("%pB(%pA+%#" PRIx64 "): "
11951
0
       "unexpected %s instruction '%#lx' "
11952
0
       "referenced by TLS_GOTDESC"),
11953
0
           input_bfd, input_section, (uint64_t) rel->r_offset,
11954
0
           "ARM", insn);
11955
0
        return bfd_reloc_notsupported;
11956
0
      }
11957
0
        }
11958
11959
0
      value += ((globals->root.sgotplt->output_section->vma
11960
0
           + globals->root.sgotplt->output_offset + off)
11961
0
          - (input_section->output_section->vma
11962
0
       + input_section->output_offset
11963
0
       + rel->r_offset)
11964
0
          + globals->sgotplt_jump_table_size);
11965
0
    }
11966
0
  else
11967
0
    value = ((globals->root.sgot->output_section->vma
11968
0
        + globals->root.sgot->output_offset + off)
11969
0
       - (input_section->output_section->vma
11970
0
          + input_section->output_offset + rel->r_offset));
11971
11972
0
  if (globals->fdpic_p && (r_type == R_ARM_TLS_GD32_FDPIC ||
11973
0
         r_type == R_ARM_TLS_IE32_FDPIC))
11974
0
    {
11975
      /* For FDPIC relocations, resolve to the offset of the GOT
11976
         entry from the start of GOT.  */
11977
0
      bfd_put_32 (output_bfd,
11978
0
      globals->root.sgot->output_offset + off,
11979
0
      contents + rel->r_offset);
11980
11981
0
      return bfd_reloc_ok;
11982
0
    }
11983
0
  else
11984
0
    {
11985
0
      return _bfd_final_link_relocate (howto, input_bfd, input_section,
11986
0
               contents, rel->r_offset, value,
11987
0
               rel->r_addend);
11988
0
    }
11989
0
      }
11990
11991
0
    case R_ARM_TLS_LE32:
11992
0
      if (bfd_link_dll (info))
11993
0
  {
11994
0
    _bfd_error_handler
11995
      /* xgettext:c-format */
11996
0
      (_("%pB(%pA+%#" PRIx64 "): %s relocation not permitted "
11997
0
         "in shared object"),
11998
0
       input_bfd, input_section, (uint64_t) rel->r_offset, howto->name);
11999
0
    return bfd_reloc_notsupported;
12000
0
  }
12001
0
      else
12002
0
  value = tpoff (info, value);
12003
12004
0
      return _bfd_final_link_relocate (howto, input_bfd, input_section,
12005
0
               contents, rel->r_offset, value,
12006
0
               rel->r_addend);
12007
12008
0
    case R_ARM_V4BX:
12009
0
      if (globals->fix_v4bx)
12010
0
  {
12011
0
    bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12012
12013
    /* Ensure that we have a BX instruction.  */
12014
0
    BFD_ASSERT ((insn & 0x0ffffff0) == 0x012fff10);
12015
12016
0
    if (globals->fix_v4bx == 2 && (insn & 0xf) != 0xf)
12017
0
      {
12018
        /* Branch to veneer.  */
12019
0
        bfd_vma glue_addr;
12020
0
        glue_addr = elf32_arm_bx_glue (info, insn & 0xf);
12021
0
        glue_addr -= input_section->output_section->vma
12022
0
         + input_section->output_offset
12023
0
         + rel->r_offset + 8;
12024
0
        insn = (insn & 0xf0000000) | 0x0a000000
12025
0
         | ((glue_addr >> 2) & 0x00ffffff);
12026
0
      }
12027
0
    else
12028
0
      {
12029
        /* Preserve Rm (lowest four bits) and the condition code
12030
     (highest four bits). Other bits encode MOV PC,Rm.  */
12031
0
        insn = (insn & 0xf000000f) | 0x01a0f000;
12032
0
      }
12033
12034
0
    bfd_put_32 (input_bfd, insn, hit_data);
12035
0
  }
12036
0
      return bfd_reloc_ok;
12037
12038
0
    case R_ARM_MOVW_ABS_NC:
12039
0
    case R_ARM_MOVT_ABS:
12040
0
    case R_ARM_MOVW_PREL_NC:
12041
0
    case R_ARM_MOVT_PREL:
12042
    /* Until we properly support segment-base-relative addressing then
12043
       we assume the segment base to be zero, as for the group relocations.
12044
       Thus R_ARM_MOVW_BREL_NC has the same semantics as R_ARM_MOVW_ABS_NC
12045
       and R_ARM_MOVT_BREL has the same semantics as R_ARM_MOVT_ABS.  */
12046
0
    case R_ARM_MOVW_BREL_NC:
12047
0
    case R_ARM_MOVW_BREL:
12048
0
    case R_ARM_MOVT_BREL:
12049
0
      {
12050
0
  bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12051
12052
0
  if (globals->use_rel)
12053
0
    {
12054
0
      addend = ((insn >> 4) & 0xf000) | (insn & 0xfff);
12055
0
      signed_addend = (addend ^ 0x8000) - 0x8000;
12056
0
    }
12057
12058
0
  value += signed_addend;
12059
12060
0
  if (r_type == R_ARM_MOVW_PREL_NC || r_type == R_ARM_MOVT_PREL)
12061
0
    value -= (input_section->output_section->vma
12062
0
        + input_section->output_offset + rel->r_offset);
12063
12064
0
  if (r_type == R_ARM_MOVW_BREL && value >= 0x10000)
12065
0
    return bfd_reloc_overflow;
12066
12067
0
  if (branch_type == ST_BRANCH_TO_THUMB)
12068
0
    value |= 1;
12069
12070
0
  if (r_type == R_ARM_MOVT_ABS || r_type == R_ARM_MOVT_PREL
12071
0
      || r_type == R_ARM_MOVT_BREL)
12072
0
    value >>= 16;
12073
12074
0
  insn &= 0xfff0f000;
12075
0
  insn |= value & 0xfff;
12076
0
  insn |= (value & 0xf000) << 4;
12077
0
  bfd_put_32 (input_bfd, insn, hit_data);
12078
0
      }
12079
0
      return bfd_reloc_ok;
12080
12081
0
    case R_ARM_THM_MOVW_ABS_NC:
12082
0
    case R_ARM_THM_MOVT_ABS:
12083
0
    case R_ARM_THM_MOVW_PREL_NC:
12084
0
    case R_ARM_THM_MOVT_PREL:
12085
    /* Until we properly support segment-base-relative addressing then
12086
       we assume the segment base to be zero, as for the above relocations.
12087
       Thus R_ARM_THM_MOVW_BREL_NC has the same semantics as
12088
       R_ARM_THM_MOVW_ABS_NC and R_ARM_THM_MOVT_BREL has the same semantics
12089
       as R_ARM_THM_MOVT_ABS.  */
12090
0
    case R_ARM_THM_MOVW_BREL_NC:
12091
0
    case R_ARM_THM_MOVW_BREL:
12092
0
    case R_ARM_THM_MOVT_BREL:
12093
0
      {
12094
0
  bfd_vma insn;
12095
12096
0
  insn = bfd_get_16 (input_bfd, hit_data) << 16;
12097
0
  insn |= bfd_get_16 (input_bfd, hit_data + 2);
12098
12099
0
  if (globals->use_rel)
12100
0
    {
12101
0
      addend = ((insn >> 4)  & 0xf000)
12102
0
       | ((insn >> 15) & 0x0800)
12103
0
       | ((insn >> 4)  & 0x0700)
12104
0
       | (insn     & 0x00ff);
12105
0
      signed_addend = (addend ^ 0x8000) - 0x8000;
12106
0
    }
12107
12108
0
  value += signed_addend;
12109
12110
0
  if (r_type == R_ARM_THM_MOVW_PREL_NC || r_type == R_ARM_THM_MOVT_PREL)
12111
0
    value -= (input_section->output_section->vma
12112
0
        + input_section->output_offset + rel->r_offset);
12113
12114
0
  if (r_type == R_ARM_THM_MOVW_BREL && value >= 0x10000)
12115
0
    return bfd_reloc_overflow;
12116
12117
0
  if (branch_type == ST_BRANCH_TO_THUMB)
12118
0
    value |= 1;
12119
12120
0
  if (r_type == R_ARM_THM_MOVT_ABS || r_type == R_ARM_THM_MOVT_PREL
12121
0
      || r_type == R_ARM_THM_MOVT_BREL)
12122
0
    value >>= 16;
12123
12124
0
  insn &= 0xfbf08f00;
12125
0
  insn |= (value & 0xf000) << 4;
12126
0
  insn |= (value & 0x0800) << 15;
12127
0
  insn |= (value & 0x0700) << 4;
12128
0
  insn |= (value & 0x00ff);
12129
12130
0
  bfd_put_16 (input_bfd, insn >> 16, hit_data);
12131
0
  bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
12132
0
      }
12133
0
      return bfd_reloc_ok;
12134
12135
0
    case R_ARM_ALU_PC_G0_NC:
12136
0
    case R_ARM_ALU_PC_G1_NC:
12137
0
    case R_ARM_ALU_PC_G0:
12138
0
    case R_ARM_ALU_PC_G1:
12139
0
    case R_ARM_ALU_PC_G2:
12140
0
    case R_ARM_ALU_SB_G0_NC:
12141
0
    case R_ARM_ALU_SB_G1_NC:
12142
0
    case R_ARM_ALU_SB_G0:
12143
0
    case R_ARM_ALU_SB_G1:
12144
0
    case R_ARM_ALU_SB_G2:
12145
0
      {
12146
0
  bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12147
0
  bfd_vma pc = input_section->output_section->vma
12148
0
         + input_section->output_offset + rel->r_offset;
12149
  /* sb is the origin of the *segment* containing the symbol.  */
12150
0
  bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
12151
0
  bfd_vma residual;
12152
0
  bfd_vma g_n;
12153
0
  bfd_signed_vma signed_value;
12154
0
  int group = 0;
12155
12156
  /* Determine which group of bits to select.  */
12157
0
  switch (r_type)
12158
0
    {
12159
0
    case R_ARM_ALU_PC_G0_NC:
12160
0
    case R_ARM_ALU_PC_G0:
12161
0
    case R_ARM_ALU_SB_G0_NC:
12162
0
    case R_ARM_ALU_SB_G0:
12163
0
      group = 0;
12164
0
      break;
12165
12166
0
    case R_ARM_ALU_PC_G1_NC:
12167
0
    case R_ARM_ALU_PC_G1:
12168
0
    case R_ARM_ALU_SB_G1_NC:
12169
0
    case R_ARM_ALU_SB_G1:
12170
0
      group = 1;
12171
0
      break;
12172
12173
0
    case R_ARM_ALU_PC_G2:
12174
0
    case R_ARM_ALU_SB_G2:
12175
0
      group = 2;
12176
0
      break;
12177
12178
0
    default:
12179
0
      abort ();
12180
0
    }
12181
12182
  /* If REL, extract the addend from the insn.  If RELA, it will
12183
     have already been fetched for us.  */
12184
0
  if (globals->use_rel)
12185
0
    {
12186
0
      int negative;
12187
0
      bfd_vma constant = insn & 0xff;
12188
0
      bfd_vma rotation = (insn & 0xf00) >> 8;
12189
12190
0
      if (rotation == 0)
12191
0
        signed_addend = constant;
12192
0
      else
12193
0
        {
12194
    /* Compensate for the fact that in the instruction, the
12195
       rotation is stored in multiples of 2 bits.  */
12196
0
    rotation *= 2;
12197
12198
    /* Rotate "constant" right by "rotation" bits.  */
12199
0
    signed_addend = (constant >> rotation) |
12200
0
        (constant << (8 * sizeof (bfd_vma) - rotation));
12201
0
        }
12202
12203
      /* Determine if the instruction is an ADD or a SUB.
12204
         (For REL, this determines the sign of the addend.)  */
12205
0
      negative = identify_add_or_sub (insn);
12206
0
      if (negative == 0)
12207
0
        {
12208
0
    _bfd_error_handler
12209
      /* xgettext:c-format */
12210
0
      (_("%pB(%pA+%#" PRIx64 "): only ADD or SUB instructions "
12211
0
         "are allowed for ALU group relocations"),
12212
0
      input_bfd, input_section, (uint64_t) rel->r_offset);
12213
0
    return bfd_reloc_overflow;
12214
0
        }
12215
12216
0
      signed_addend *= negative;
12217
0
    }
12218
12219
  /* Compute the value (X) to go in the place.  */
12220
0
  if (r_type == R_ARM_ALU_PC_G0_NC
12221
0
      || r_type == R_ARM_ALU_PC_G1_NC
12222
0
      || r_type == R_ARM_ALU_PC_G0
12223
0
      || r_type == R_ARM_ALU_PC_G1
12224
0
      || r_type == R_ARM_ALU_PC_G2)
12225
    /* PC relative.  */
12226
0
    signed_value = value - pc + signed_addend;
12227
0
  else
12228
    /* Section base relative.  */
12229
0
    signed_value = value - sb + signed_addend;
12230
12231
  /* If the target symbol is a Thumb function, then set the
12232
     Thumb bit in the address.  */
12233
0
  if (branch_type == ST_BRANCH_TO_THUMB)
12234
0
    signed_value |= 1;
12235
12236
  /* Calculate the value of the relevant G_n, in encoded
12237
     constant-with-rotation format.  */
12238
0
  g_n = calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
12239
0
            group, &residual);
12240
12241
  /* Check for overflow if required.  */
12242
0
  if ((r_type == R_ARM_ALU_PC_G0
12243
0
       || r_type == R_ARM_ALU_PC_G1
12244
0
       || r_type == R_ARM_ALU_PC_G2
12245
0
       || r_type == R_ARM_ALU_SB_G0
12246
0
       || r_type == R_ARM_ALU_SB_G1
12247
0
       || r_type == R_ARM_ALU_SB_G2) && residual != 0)
12248
0
    {
12249
0
      _bfd_error_handler
12250
        /* xgettext:c-format */
12251
0
        (_("%pB(%pA+%#" PRIx64 "): overflow whilst "
12252
0
     "splitting %#" PRIx64 " for group relocation %s"),
12253
0
         input_bfd, input_section, (uint64_t) rel->r_offset,
12254
0
         (uint64_t) (signed_value < 0 ? -signed_value : signed_value),
12255
0
         howto->name);
12256
0
      return bfd_reloc_overflow;
12257
0
    }
12258
12259
  /* Mask out the value and the ADD/SUB part of the opcode; take care
12260
     not to destroy the S bit.  */
12261
0
  insn &= 0xff1ff000;
12262
12263
  /* Set the opcode according to whether the value to go in the
12264
     place is negative.  */
12265
0
  if (signed_value < 0)
12266
0
    insn |= 1 << 22;
12267
0
  else
12268
0
    insn |= 1 << 23;
12269
12270
  /* Encode the offset.  */
12271
0
  insn |= g_n;
12272
12273
0
  bfd_put_32 (input_bfd, insn, hit_data);
12274
0
      }
12275
0
      return bfd_reloc_ok;
12276
12277
0
    case R_ARM_LDR_PC_G0:
12278
0
    case R_ARM_LDR_PC_G1:
12279
0
    case R_ARM_LDR_PC_G2:
12280
0
    case R_ARM_LDR_SB_G0:
12281
0
    case R_ARM_LDR_SB_G1:
12282
0
    case R_ARM_LDR_SB_G2:
12283
0
      {
12284
0
  bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12285
0
  bfd_vma pc = input_section->output_section->vma
12286
0
         + input_section->output_offset + rel->r_offset;
12287
  /* sb is the origin of the *segment* containing the symbol.  */
12288
0
  bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
12289
0
  bfd_vma residual;
12290
0
  bfd_signed_vma signed_value;
12291
0
  int group = 0;
12292
12293
  /* Determine which groups of bits to calculate.  */
12294
0
  switch (r_type)
12295
0
    {
12296
0
    case R_ARM_LDR_PC_G0:
12297
0
    case R_ARM_LDR_SB_G0:
12298
0
      group = 0;
12299
0
      break;
12300
12301
0
    case R_ARM_LDR_PC_G1:
12302
0
    case R_ARM_LDR_SB_G1:
12303
0
      group = 1;
12304
0
      break;
12305
12306
0
    case R_ARM_LDR_PC_G2:
12307
0
    case R_ARM_LDR_SB_G2:
12308
0
      group = 2;
12309
0
      break;
12310
12311
0
    default:
12312
0
      abort ();
12313
0
    }
12314
12315
  /* If REL, extract the addend from the insn.  If RELA, it will
12316
     have already been fetched for us.  */
12317
0
  if (globals->use_rel)
12318
0
    {
12319
0
      int negative = (insn & (1 << 23)) ? 1 : -1;
12320
0
      signed_addend = negative * (insn & 0xfff);
12321
0
    }
12322
12323
  /* Compute the value (X) to go in the place.  */
12324
0
  if (r_type == R_ARM_LDR_PC_G0
12325
0
      || r_type == R_ARM_LDR_PC_G1
12326
0
      || r_type == R_ARM_LDR_PC_G2)
12327
    /* PC relative.  */
12328
0
    signed_value = value - pc + signed_addend;
12329
0
  else
12330
    /* Section base relative.  */
12331
0
    signed_value = value - sb + signed_addend;
12332
12333
  /* Calculate the value of the relevant G_{n-1} to obtain
12334
     the residual at that stage.  */
12335
0
  calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
12336
0
            group - 1, &residual);
12337
12338
  /* Check for overflow.  */
12339
0
  if (residual >= 0x1000)
12340
0
    {
12341
0
      _bfd_error_handler
12342
        /* xgettext:c-format */
12343
0
        (_("%pB(%pA+%#" PRIx64 "): overflow whilst "
12344
0
     "splitting %#" PRIx64 " for group relocation %s"),
12345
0
         input_bfd, input_section, (uint64_t) rel->r_offset,
12346
0
         (uint64_t) (signed_value < 0 ? -signed_value : signed_value),
12347
0
         howto->name);
12348
0
      return bfd_reloc_overflow;
12349
0
    }
12350
12351
  /* Mask out the value and U bit.  */
12352
0
  insn &= 0xff7ff000;
12353
12354
  /* Set the U bit if the value to go in the place is non-negative.  */
12355
0
  if (signed_value >= 0)
12356
0
    insn |= 1 << 23;
12357
12358
  /* Encode the offset.  */
12359
0
  insn |= residual;
12360
12361
0
  bfd_put_32 (input_bfd, insn, hit_data);
12362
0
      }
12363
0
      return bfd_reloc_ok;
12364
12365
0
    case R_ARM_LDRS_PC_G0:
12366
0
    case R_ARM_LDRS_PC_G1:
12367
0
    case R_ARM_LDRS_PC_G2:
12368
0
    case R_ARM_LDRS_SB_G0:
12369
0
    case R_ARM_LDRS_SB_G1:
12370
0
    case R_ARM_LDRS_SB_G2:
12371
0
      {
12372
0
  bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12373
0
  bfd_vma pc = input_section->output_section->vma
12374
0
         + input_section->output_offset + rel->r_offset;
12375
  /* sb is the origin of the *segment* containing the symbol.  */
12376
0
  bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
12377
0
  bfd_vma residual;
12378
0
  bfd_signed_vma signed_value;
12379
0
  int group = 0;
12380
12381
  /* Determine which groups of bits to calculate.  */
12382
0
  switch (r_type)
12383
0
    {
12384
0
    case R_ARM_LDRS_PC_G0:
12385
0
    case R_ARM_LDRS_SB_G0:
12386
0
      group = 0;
12387
0
      break;
12388
12389
0
    case R_ARM_LDRS_PC_G1:
12390
0
    case R_ARM_LDRS_SB_G1:
12391
0
      group = 1;
12392
0
      break;
12393
12394
0
    case R_ARM_LDRS_PC_G2:
12395
0
    case R_ARM_LDRS_SB_G2:
12396
0
      group = 2;
12397
0
      break;
12398
12399
0
    default:
12400
0
      abort ();
12401
0
    }
12402
12403
  /* If REL, extract the addend from the insn.  If RELA, it will
12404
     have already been fetched for us.  */
12405
0
  if (globals->use_rel)
12406
0
    {
12407
0
      int negative = (insn & (1 << 23)) ? 1 : -1;
12408
0
      signed_addend = negative * (((insn & 0xf00) >> 4) + (insn & 0xf));
12409
0
    }
12410
12411
  /* Compute the value (X) to go in the place.  */
12412
0
  if (r_type == R_ARM_LDRS_PC_G0
12413
0
      || r_type == R_ARM_LDRS_PC_G1
12414
0
      || r_type == R_ARM_LDRS_PC_G2)
12415
    /* PC relative.  */
12416
0
    signed_value = value - pc + signed_addend;
12417
0
  else
12418
    /* Section base relative.  */
12419
0
    signed_value = value - sb + signed_addend;
12420
12421
  /* Calculate the value of the relevant G_{n-1} to obtain
12422
     the residual at that stage.  */
12423
0
  calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
12424
0
            group - 1, &residual);
12425
12426
  /* Check for overflow.  */
12427
0
  if (residual >= 0x100)
12428
0
    {
12429
0
      _bfd_error_handler
12430
        /* xgettext:c-format */
12431
0
        (_("%pB(%pA+%#" PRIx64 "): overflow whilst "
12432
0
     "splitting %#" PRIx64 " for group relocation %s"),
12433
0
         input_bfd, input_section, (uint64_t) rel->r_offset,
12434
0
         (uint64_t) (signed_value < 0 ? -signed_value : signed_value),
12435
0
         howto->name);
12436
0
      return bfd_reloc_overflow;
12437
0
    }
12438
12439
  /* Mask out the value and U bit.  */
12440
0
  insn &= 0xff7ff0f0;
12441
12442
  /* Set the U bit if the value to go in the place is non-negative.  */
12443
0
  if (signed_value >= 0)
12444
0
    insn |= 1 << 23;
12445
12446
  /* Encode the offset.  */
12447
0
  insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
12448
12449
0
  bfd_put_32 (input_bfd, insn, hit_data);
12450
0
      }
12451
0
      return bfd_reloc_ok;
12452
12453
0
    case R_ARM_LDC_PC_G0:
12454
0
    case R_ARM_LDC_PC_G1:
12455
0
    case R_ARM_LDC_PC_G2:
12456
0
    case R_ARM_LDC_SB_G0:
12457
0
    case R_ARM_LDC_SB_G1:
12458
0
    case R_ARM_LDC_SB_G2:
12459
0
      {
12460
0
  bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12461
0
  bfd_vma pc = input_section->output_section->vma
12462
0
         + input_section->output_offset + rel->r_offset;
12463
  /* sb is the origin of the *segment* containing the symbol.  */
12464
0
  bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
12465
0
  bfd_vma residual;
12466
0
  bfd_signed_vma signed_value;
12467
0
  int group = 0;
12468
12469
  /* Determine which groups of bits to calculate.  */
12470
0
  switch (r_type)
12471
0
    {
12472
0
    case R_ARM_LDC_PC_G0:
12473
0
    case R_ARM_LDC_SB_G0:
12474
0
      group = 0;
12475
0
      break;
12476
12477
0
    case R_ARM_LDC_PC_G1:
12478
0
    case R_ARM_LDC_SB_G1:
12479
0
      group = 1;
12480
0
      break;
12481
12482
0
    case R_ARM_LDC_PC_G2:
12483
0
    case R_ARM_LDC_SB_G2:
12484
0
      group = 2;
12485
0
      break;
12486
12487
0
    default:
12488
0
      abort ();
12489
0
    }
12490
12491
  /* If REL, extract the addend from the insn.  If RELA, it will
12492
     have already been fetched for us.  */
12493
0
  if (globals->use_rel)
12494
0
    {
12495
0
      int negative = (insn & (1 << 23)) ? 1 : -1;
12496
0
      signed_addend = negative * ((insn & 0xff) << 2);
12497
0
    }
12498
12499
  /* Compute the value (X) to go in the place.  */
12500
0
  if (r_type == R_ARM_LDC_PC_G0
12501
0
      || r_type == R_ARM_LDC_PC_G1
12502
0
      || r_type == R_ARM_LDC_PC_G2)
12503
    /* PC relative.  */
12504
0
    signed_value = value - pc + signed_addend;
12505
0
  else
12506
    /* Section base relative.  */
12507
0
    signed_value = value - sb + signed_addend;
12508
12509
  /* Calculate the value of the relevant G_{n-1} to obtain
12510
     the residual at that stage.  */
12511
0
  calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
12512
0
            group - 1, &residual);
12513
12514
  /* Check for overflow.  (The absolute value to go in the place must be
12515
     divisible by four and, after having been divided by four, must
12516
     fit in eight bits.)  */
12517
0
  if ((residual & 0x3) != 0 || residual >= 0x400)
12518
0
    {
12519
0
      _bfd_error_handler
12520
        /* xgettext:c-format */
12521
0
        (_("%pB(%pA+%#" PRIx64 "): overflow whilst "
12522
0
     "splitting %#" PRIx64 " for group relocation %s"),
12523
0
         input_bfd, input_section, (uint64_t) rel->r_offset,
12524
0
         (uint64_t) (signed_value < 0 ? -signed_value : signed_value),
12525
0
         howto->name);
12526
0
      return bfd_reloc_overflow;
12527
0
    }
12528
12529
  /* Mask out the value and U bit.  */
12530
0
  insn &= 0xff7fff00;
12531
12532
  /* Set the U bit if the value to go in the place is non-negative.  */
12533
0
  if (signed_value >= 0)
12534
0
    insn |= 1 << 23;
12535
12536
  /* Encode the offset.  */
12537
0
  insn |= residual >> 2;
12538
12539
0
  bfd_put_32 (input_bfd, insn, hit_data);
12540
0
      }
12541
0
      return bfd_reloc_ok;
12542
12543
0
    case R_ARM_THM_ALU_ABS_G0_NC:
12544
0
    case R_ARM_THM_ALU_ABS_G1_NC:
12545
0
    case R_ARM_THM_ALU_ABS_G2_NC:
12546
0
    case R_ARM_THM_ALU_ABS_G3_NC:
12547
0
  {
12548
0
      static const int shift_array[4] = {0, 8, 16, 24};
12549
0
      bfd_vma insn = bfd_get_16 (input_bfd, hit_data);
12550
0
      bfd_vma addr = value;
12551
0
      int shift = shift_array[r_type - R_ARM_THM_ALU_ABS_G0_NC];
12552
12553
      /* Compute address.  */
12554
0
      if (globals->use_rel)
12555
0
    signed_addend = insn & 0xff;
12556
0
      addr += signed_addend;
12557
0
      if (branch_type == ST_BRANCH_TO_THUMB)
12558
0
    addr |= 1;
12559
      /* Clean imm8 insn.  */
12560
0
      insn &= 0xff00;
12561
      /* And update with correct part of address.  */
12562
0
      insn |= (addr >> shift) & 0xff;
12563
      /* Update insn.  */
12564
0
      bfd_put_16 (input_bfd, insn, hit_data);
12565
0
  }
12566
12567
0
  *unresolved_reloc_p = false;
12568
0
  return bfd_reloc_ok;
12569
12570
0
    case R_ARM_GOTOFFFUNCDESC:
12571
0
      {
12572
0
  if (h == NULL)
12573
0
    {
12574
0
      struct fdpic_local *local_fdpic_cnts = elf32_arm_local_fdpic_cnts (input_bfd);
12575
0
      int dynindx = elf_section_data (sym_sec->output_section)->dynindx;
12576
12577
0
      if (r_symndx >= elf32_arm_num_entries (input_bfd))
12578
0
        {
12579
0
    * error_message = _("local symbol index too big");
12580
0
    return bfd_reloc_dangerous;
12581
0
        }
12582
12583
0
      int offset = local_fdpic_cnts[r_symndx].funcdesc_offset & ~1;
12584
0
      bfd_vma addr = dynreloc_value - sym_sec->output_section->vma;
12585
0
      bfd_vma seg = -1;
12586
12587
0
      if (bfd_link_pic (info) && dynindx == 0)
12588
0
        {
12589
0
    * error_message = _("no dynamic index information available");
12590
0
    return bfd_reloc_dangerous;
12591
0
        }
12592
12593
      /* Resolve relocation.  */
12594
0
      bfd_put_32 (output_bfd, (offset + sgot->output_offset)
12595
0
           , contents + rel->r_offset);
12596
      /* Emit R_ARM_FUNCDESC_VALUE or two fixups on funcdesc if
12597
         not done yet.  */
12598
0
      arm_elf_fill_funcdesc (output_bfd, info,
12599
0
           &local_fdpic_cnts[r_symndx].funcdesc_offset,
12600
0
           dynindx, offset, addr, dynreloc_value, seg);
12601
0
    }
12602
0
  else
12603
0
    {
12604
0
      int dynindx;
12605
0
      int offset = eh->fdpic_cnts.funcdesc_offset & ~1;
12606
0
      bfd_vma addr;
12607
0
      bfd_vma seg = -1;
12608
12609
      /* For static binaries, sym_sec can be null.  */
12610
0
      if (sym_sec)
12611
0
        {
12612
0
    dynindx = elf_section_data (sym_sec->output_section)->dynindx;
12613
0
    addr = dynreloc_value - sym_sec->output_section->vma;
12614
0
        }
12615
0
      else
12616
0
        {
12617
0
    dynindx = 0;
12618
0
    addr = 0;
12619
0
        }
12620
12621
0
      if (bfd_link_pic (info) && dynindx == 0)
12622
0
        {
12623
0
    * error_message = _("no dynamic index information available");
12624
0
    return bfd_reloc_dangerous;
12625
0
        }
12626
12627
      /* This case cannot occur since funcdesc is allocated by
12628
         the dynamic loader so we cannot resolve the relocation.  */
12629
0
      if (h->dynindx != -1)
12630
0
        {
12631
0
    * error_message = _("invalid dynamic index");
12632
0
    return bfd_reloc_dangerous;
12633
0
        }
12634
12635
      /* Resolve relocation.  */
12636
0
      bfd_put_32 (output_bfd, (offset + sgot->output_offset),
12637
0
            contents + rel->r_offset);
12638
      /* Emit R_ARM_FUNCDESC_VALUE on funcdesc if not done yet.  */
12639
0
      arm_elf_fill_funcdesc (output_bfd, info,
12640
0
           &eh->fdpic_cnts.funcdesc_offset,
12641
0
           dynindx, offset, addr, dynreloc_value, seg);
12642
0
    }
12643
0
      }
12644
0
      *unresolved_reloc_p = false;
12645
0
      return bfd_reloc_ok;
12646
12647
0
    case R_ARM_GOTFUNCDESC:
12648
0
      {
12649
0
  if (h != NULL)
12650
0
    {
12651
0
      Elf_Internal_Rela outrel;
12652
12653
      /* Resolve relocation.  */
12654
0
      bfd_put_32 (output_bfd, ((eh->fdpic_cnts.gotfuncdesc_offset & ~1)
12655
0
             + sgot->output_offset),
12656
0
      contents + rel->r_offset);
12657
      /* Add funcdesc and associated R_ARM_FUNCDESC_VALUE.  */
12658
0
      if (h->dynindx == -1)
12659
0
        {
12660
0
    int dynindx;
12661
0
    int offset = eh->fdpic_cnts.funcdesc_offset & ~1;
12662
0
    bfd_vma addr;
12663
0
    bfd_vma seg = -1;
12664
12665
    /* For static binaries sym_sec can be null.  */
12666
0
    if (sym_sec)
12667
0
      {
12668
0
        dynindx = elf_section_data (sym_sec->output_section)->dynindx;
12669
0
        addr = dynreloc_value - sym_sec->output_section->vma;
12670
0
      }
12671
0
    else
12672
0
      {
12673
0
        dynindx = 0;
12674
0
        addr = 0;
12675
0
      }
12676
12677
    /* Emit R_ARM_FUNCDESC_VALUE on funcdesc if not done yet.  */
12678
0
    arm_elf_fill_funcdesc (output_bfd, info,
12679
0
               &eh->fdpic_cnts.funcdesc_offset,
12680
0
               dynindx, offset, addr, dynreloc_value, seg);
12681
0
        }
12682
12683
      /* Add a dynamic relocation on GOT entry if not already done.  */
12684
0
      if ((eh->fdpic_cnts.gotfuncdesc_offset & 1) == 0)
12685
0
        {
12686
0
    if (h->dynindx == -1)
12687
0
      {
12688
0
        outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
12689
0
        if (h->root.type == bfd_link_hash_undefweak)
12690
0
          bfd_put_32 (output_bfd, 0, sgot->contents
12691
0
          + (eh->fdpic_cnts.gotfuncdesc_offset & ~1));
12692
0
        else
12693
0
          bfd_put_32 (output_bfd, sgot->output_section->vma
12694
0
          + sgot->output_offset
12695
0
          + (eh->fdpic_cnts.funcdesc_offset & ~1),
12696
0
          sgot->contents
12697
0
          + (eh->fdpic_cnts.gotfuncdesc_offset & ~1));
12698
0
      }
12699
0
    else
12700
0
      {
12701
0
        outrel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_FUNCDESC);
12702
0
      }
12703
0
    outrel.r_offset = sgot->output_section->vma
12704
0
      + sgot->output_offset
12705
0
      + (eh->fdpic_cnts.gotfuncdesc_offset & ~1);
12706
0
    outrel.r_addend = 0;
12707
0
    if (h->dynindx == -1 && !bfd_link_pic (info))
12708
0
      if (h->root.type == bfd_link_hash_undefweak)
12709
0
        arm_elf_add_rofixup (output_bfd, globals->srofixup, -1);
12710
0
      else
12711
0
        arm_elf_add_rofixup (output_bfd, globals->srofixup,
12712
0
           outrel.r_offset);
12713
0
    else
12714
0
      elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
12715
0
    eh->fdpic_cnts.gotfuncdesc_offset |= 1;
12716
0
        }
12717
0
    }
12718
0
  else
12719
0
    {
12720
      /* Such relocation on static function should not have been
12721
         emitted by the compiler.  */
12722
0
      return bfd_reloc_notsupported;
12723
0
    }
12724
0
      }
12725
0
      *unresolved_reloc_p = false;
12726
0
      return bfd_reloc_ok;
12727
12728
0
    case R_ARM_FUNCDESC:
12729
0
      {
12730
0
  if (h == NULL)
12731
0
    {
12732
0
      struct fdpic_local *local_fdpic_cnts = elf32_arm_local_fdpic_cnts (input_bfd);
12733
0
      Elf_Internal_Rela outrel;
12734
0
      int dynindx = elf_section_data (sym_sec->output_section)->dynindx;
12735
12736
0
      if (r_symndx >= elf32_arm_num_entries (input_bfd))
12737
0
        {
12738
0
    * error_message = _("local symbol index too big");
12739
0
    return bfd_reloc_dangerous;
12740
0
        }
12741
12742
0
      int offset = local_fdpic_cnts[r_symndx].funcdesc_offset & ~1;
12743
0
      bfd_vma addr = dynreloc_value - sym_sec->output_section->vma;
12744
0
      bfd_vma seg = -1;
12745
12746
0
      if (bfd_link_pic (info) && dynindx == 0)
12747
0
        {
12748
0
    * error_message = _("dynamic index information not available");
12749
0
    return bfd_reloc_dangerous;
12750
0
        }
12751
12752
      /* Replace static FUNCDESC relocation with a
12753
         R_ARM_RELATIVE dynamic relocation or with a rofixup for
12754
         executable.  */
12755
0
      outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
12756
0
      outrel.r_offset = input_section->output_section->vma
12757
0
        + input_section->output_offset + rel->r_offset;
12758
0
      outrel.r_addend = 0;
12759
0
      if (bfd_link_pic (info))
12760
0
        elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
12761
0
      else
12762
0
        arm_elf_add_rofixup (output_bfd, globals->srofixup, outrel.r_offset);
12763
12764
0
      bfd_put_32 (input_bfd, sgot->output_section->vma
12765
0
      + sgot->output_offset + offset, hit_data);
12766
12767
      /* Emit R_ARM_FUNCDESC_VALUE on funcdesc if not done yet.  */
12768
0
      arm_elf_fill_funcdesc (output_bfd, info,
12769
0
           &local_fdpic_cnts[r_symndx].funcdesc_offset,
12770
0
           dynindx, offset, addr, dynreloc_value, seg);
12771
0
    }
12772
0
  else
12773
0
    {
12774
0
      if (h->dynindx == -1)
12775
0
        {
12776
0
    int dynindx;
12777
0
    int offset = eh->fdpic_cnts.funcdesc_offset & ~1;
12778
0
    bfd_vma addr;
12779
0
    bfd_vma seg = -1;
12780
0
    Elf_Internal_Rela outrel;
12781
12782
    /* For static binaries sym_sec can be null.  */
12783
0
    if (sym_sec)
12784
0
      {
12785
0
        dynindx = elf_section_data (sym_sec->output_section)->dynindx;
12786
0
        addr = dynreloc_value - sym_sec->output_section->vma;
12787
0
      }
12788
0
    else
12789
0
      {
12790
0
        dynindx = 0;
12791
0
        addr = 0;
12792
0
      }
12793
12794
0
    if (bfd_link_pic (info) && dynindx == 0)
12795
0
      abort ();
12796
12797
    /* Replace static FUNCDESC relocation with a
12798
       R_ARM_RELATIVE dynamic relocation.  */
12799
0
    outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
12800
0
    outrel.r_offset = input_section->output_section->vma
12801
0
      + input_section->output_offset + rel->r_offset;
12802
0
    outrel.r_addend = 0;
12803
0
    if (bfd_link_pic (info))
12804
0
      elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
12805
0
    else
12806
0
      arm_elf_add_rofixup (output_bfd, globals->srofixup, outrel.r_offset);
12807
12808
0
    bfd_put_32 (input_bfd, sgot->output_section->vma
12809
0
          + sgot->output_offset + offset, hit_data);
12810
12811
    /* Emit R_ARM_FUNCDESC_VALUE on funcdesc if not done yet.  */
12812
0
    arm_elf_fill_funcdesc (output_bfd, info,
12813
0
               &eh->fdpic_cnts.funcdesc_offset,
12814
0
               dynindx, offset, addr, dynreloc_value, seg);
12815
0
        }
12816
0
      else
12817
0
        {
12818
0
    Elf_Internal_Rela outrel;
12819
12820
    /* Add a dynamic relocation.  */
12821
0
    outrel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_FUNCDESC);
12822
0
    outrel.r_offset = input_section->output_section->vma
12823
0
      + input_section->output_offset + rel->r_offset;
12824
0
    outrel.r_addend = 0;
12825
0
    elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
12826
0
        }
12827
0
    }
12828
0
      }
12829
0
      *unresolved_reloc_p = false;
12830
0
      return bfd_reloc_ok;
12831
12832
0
    case R_ARM_THM_BF16:
12833
0
      {
12834
0
  bfd_vma relocation;
12835
0
  bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
12836
0
  bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
12837
12838
0
  if (globals->use_rel)
12839
0
    {
12840
0
      bfd_vma immA  = (upper_insn & 0x001f);
12841
0
      bfd_vma immB  = (lower_insn & 0x07fe) >> 1;
12842
0
      bfd_vma immC  = (lower_insn & 0x0800) >> 11;
12843
0
      addend  = (immA << 12);
12844
0
      addend |= (immB << 2);
12845
0
      addend |= (immC << 1);
12846
0
      addend |= 1;
12847
      /* Sign extend.  */
12848
0
      signed_addend = (addend & 0x10000) ? addend - (1 << 17) : addend;
12849
0
    }
12850
12851
0
  relocation  = value + signed_addend;
12852
0
  relocation -= (input_section->output_section->vma
12853
0
           + input_section->output_offset
12854
0
           + rel->r_offset);
12855
12856
  /* Put RELOCATION back into the insn.  */
12857
0
  {
12858
0
    bfd_vma immA = (relocation & 0x0001f000) >> 12;
12859
0
    bfd_vma immB = (relocation & 0x00000ffc) >> 2;
12860
0
    bfd_vma immC = (relocation & 0x00000002) >> 1;
12861
12862
0
    upper_insn = (upper_insn & 0xffe0) | immA;
12863
0
    lower_insn = (lower_insn & 0xf001) | (immC << 11) | (immB << 1);
12864
0
  }
12865
12866
  /* Put the relocated value back in the object file:  */
12867
0
  bfd_put_16 (input_bfd, upper_insn, hit_data);
12868
0
  bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
12869
12870
0
  return bfd_reloc_ok;
12871
0
      }
12872
12873
0
    case R_ARM_THM_BF12:
12874
0
      {
12875
0
  bfd_vma relocation;
12876
0
  bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
12877
0
  bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
12878
12879
0
  if (globals->use_rel)
12880
0
    {
12881
0
      bfd_vma immA  = (upper_insn & 0x0001);
12882
0
      bfd_vma immB  = (lower_insn & 0x07fe) >> 1;
12883
0
      bfd_vma immC  = (lower_insn & 0x0800) >> 11;
12884
0
      addend  = (immA << 12);
12885
0
      addend |= (immB << 2);
12886
0
      addend |= (immC << 1);
12887
0
      addend |= 1;
12888
      /* Sign extend.  */
12889
0
      addend = (addend & 0x1000) ? addend - (1 << 13) : addend;
12890
0
      signed_addend = addend;
12891
0
    }
12892
12893
0
  relocation  = value + signed_addend;
12894
0
  relocation -= (input_section->output_section->vma
12895
0
           + input_section->output_offset
12896
0
           + rel->r_offset);
12897
12898
  /* Put RELOCATION back into the insn.  */
12899
0
  {
12900
0
    bfd_vma immA = (relocation & 0x00001000) >> 12;
12901
0
    bfd_vma immB = (relocation & 0x00000ffc) >> 2;
12902
0
    bfd_vma immC = (relocation & 0x00000002) >> 1;
12903
12904
0
    upper_insn = (upper_insn & 0xfffe) | immA;
12905
0
    lower_insn = (lower_insn & 0xf001) | (immC << 11) | (immB << 1);
12906
0
  }
12907
12908
  /* Put the relocated value back in the object file:  */
12909
0
  bfd_put_16 (input_bfd, upper_insn, hit_data);
12910
0
  bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
12911
12912
0
  return bfd_reloc_ok;
12913
0
      }
12914
12915
0
    case R_ARM_THM_BF18:
12916
0
      {
12917
0
  bfd_vma relocation;
12918
0
  bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
12919
0
  bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
12920
12921
0
  if (globals->use_rel)
12922
0
    {
12923
0
      bfd_vma immA  = (upper_insn & 0x007f);
12924
0
      bfd_vma immB  = (lower_insn & 0x07fe) >> 1;
12925
0
      bfd_vma immC  = (lower_insn & 0x0800) >> 11;
12926
0
      addend  = (immA << 12);
12927
0
      addend |= (immB << 2);
12928
0
      addend |= (immC << 1);
12929
0
      addend |= 1;
12930
      /* Sign extend.  */
12931
0
      addend = (addend & 0x40000) ? addend - (1 << 19) : addend;
12932
0
      signed_addend = addend;
12933
0
    }
12934
12935
0
  relocation  = value + signed_addend;
12936
0
  relocation -= (input_section->output_section->vma
12937
0
           + input_section->output_offset
12938
0
           + rel->r_offset);
12939
12940
  /* Put RELOCATION back into the insn.  */
12941
0
  {
12942
0
    bfd_vma immA = (relocation & 0x0007f000) >> 12;
12943
0
    bfd_vma immB = (relocation & 0x00000ffc) >> 2;
12944
0
    bfd_vma immC = (relocation & 0x00000002) >> 1;
12945
12946
0
    upper_insn = (upper_insn & 0xff80) | immA;
12947
0
    lower_insn = (lower_insn & 0xf001) | (immC << 11) | (immB << 1);
12948
0
  }
12949
12950
  /* Put the relocated value back in the object file:  */
12951
0
  bfd_put_16 (input_bfd, upper_insn, hit_data);
12952
0
  bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
12953
12954
0
  return bfd_reloc_ok;
12955
0
      }
12956
12957
0
    default:
12958
0
      return bfd_reloc_notsupported;
12959
0
    }
12960
0
}
12961
12962
/* Add INCREMENT to the reloc (of type HOWTO) at ADDRESS.  */
12963
static void
12964
arm_add_to_rel (bfd *      abfd,
12965
    bfd_byte *     address,
12966
    reloc_howto_type * howto,
12967
    bfd_signed_vma     increment)
12968
0
{
12969
0
  bfd_signed_vma addend;
12970
12971
0
  if (howto->type == R_ARM_THM_CALL
12972
0
      || howto->type == R_ARM_THM_JUMP24)
12973
0
    {
12974
0
      int upper_insn, lower_insn;
12975
0
      int upper, lower;
12976
12977
0
      upper_insn = bfd_get_16 (abfd, address);
12978
0
      lower_insn = bfd_get_16 (abfd, address + 2);
12979
0
      upper = upper_insn & 0x7ff;
12980
0
      lower = lower_insn & 0x7ff;
12981
12982
0
      addend = (upper << 12) | (lower << 1);
12983
0
      addend += increment;
12984
0
      addend >>= 1;
12985
12986
0
      upper_insn = (upper_insn & 0xf800) | ((addend >> 11) & 0x7ff);
12987
0
      lower_insn = (lower_insn & 0xf800) | (addend & 0x7ff);
12988
12989
0
      bfd_put_16 (abfd, (bfd_vma) upper_insn, address);
12990
0
      bfd_put_16 (abfd, (bfd_vma) lower_insn, address + 2);
12991
0
    }
12992
0
  else
12993
0
    {
12994
0
      bfd_vma      contents;
12995
12996
0
      contents = bfd_get_32 (abfd, address);
12997
12998
      /* Get the (signed) value from the instruction.  */
12999
0
      addend = contents & howto->src_mask;
13000
0
      if (addend & ((howto->src_mask + 1) >> 1))
13001
0
  {
13002
0
    bfd_signed_vma mask;
13003
13004
0
    mask = -1;
13005
0
    mask &= ~ howto->src_mask;
13006
0
    addend |= mask;
13007
0
  }
13008
13009
      /* Add in the increment, (which is a byte value).  */
13010
0
      switch (howto->type)
13011
0
  {
13012
0
  default:
13013
0
    addend += increment;
13014
0
    break;
13015
13016
0
  case R_ARM_PC24:
13017
0
  case R_ARM_PLT32:
13018
0
  case R_ARM_CALL:
13019
0
  case R_ARM_JUMP24:
13020
0
    addend *= bfd_get_reloc_size (howto);
13021
0
    addend += increment;
13022
13023
    /* Should we check for overflow here ?  */
13024
13025
    /* Drop any undesired bits.  */
13026
0
    addend >>= howto->rightshift;
13027
0
    break;
13028
0
  }
13029
13030
0
      contents = (contents & ~ howto->dst_mask) | (addend & howto->dst_mask);
13031
13032
0
      bfd_put_32 (abfd, contents, address);
13033
0
    }
13034
0
}
13035
13036
#define IS_ARM_TLS_RELOC(R_TYPE)  \
13037
0
  ((R_TYPE) == R_ARM_TLS_GD32    \
13038
0
   || (R_TYPE) == R_ARM_TLS_GD32_FDPIC  \
13039
0
   || (R_TYPE) == R_ARM_TLS_LDO32  \
13040
0
   || (R_TYPE) == R_ARM_TLS_LDM32  \
13041
0
   || (R_TYPE) == R_ARM_TLS_LDM32_FDPIC  \
13042
0
   || (R_TYPE) == R_ARM_TLS_DTPOFF32  \
13043
0
   || (R_TYPE) == R_ARM_TLS_DTPMOD32  \
13044
0
   || (R_TYPE) == R_ARM_TLS_TPOFF32  \
13045
0
   || (R_TYPE) == R_ARM_TLS_LE32  \
13046
0
   || (R_TYPE) == R_ARM_TLS_IE32  \
13047
0
   || (R_TYPE) == R_ARM_TLS_IE32_FDPIC  \
13048
0
   || IS_ARM_TLS_GNU_RELOC (R_TYPE))
13049
13050
/* Specific set of relocations for the gnu tls dialect.  */
13051
#define IS_ARM_TLS_GNU_RELOC(R_TYPE)  \
13052
0
  ((R_TYPE) == R_ARM_TLS_GOTDESC  \
13053
0
   || (R_TYPE) == R_ARM_TLS_CALL  \
13054
0
   || (R_TYPE) == R_ARM_THM_TLS_CALL  \
13055
0
   || (R_TYPE) == R_ARM_TLS_DESCSEQ  \
13056
0
   || (R_TYPE) == R_ARM_THM_TLS_DESCSEQ)
13057
13058
/* Relocate an ARM ELF section.  */
13059
13060
static int
13061
elf32_arm_relocate_section (bfd *      output_bfd,
13062
          struct bfd_link_info * info,
13063
          bfd *      input_bfd,
13064
          asection *       input_section,
13065
          bfd_byte *       contents,
13066
          Elf_Internal_Rela *    relocs,
13067
          Elf_Internal_Sym *     local_syms,
13068
          asection **      local_sections)
13069
0
{
13070
0
  Elf_Internal_Shdr *symtab_hdr;
13071
0
  struct elf_link_hash_entry **sym_hashes;
13072
0
  Elf_Internal_Rela *rel;
13073
0
  Elf_Internal_Rela *relend;
13074
0
  const char *name;
13075
0
  struct elf32_arm_link_hash_table * globals;
13076
13077
0
  globals = elf32_arm_hash_table (info);
13078
0
  if (globals == NULL)
13079
0
    return false;
13080
13081
0
  symtab_hdr = & elf_symtab_hdr (input_bfd);
13082
0
  sym_hashes = elf_sym_hashes (input_bfd);
13083
13084
0
  rel = relocs;
13085
0
  relend = relocs + input_section->reloc_count;
13086
0
  for (; rel < relend; rel++)
13087
0
    {
13088
0
      int        r_type;
13089
0
      reloc_howto_type *     howto;
13090
0
      unsigned long      r_symndx;
13091
0
      Elf_Internal_Sym *     sym;
13092
0
      asection *       sec;
13093
0
      struct elf_link_hash_entry * h;
13094
0
      bfd_vma        relocation;
13095
0
      bfd_reloc_status_type    r;
13096
0
      arelent        bfd_reloc;
13097
0
      char         sym_type;
13098
0
      bool         unresolved_reloc = false;
13099
0
      char *error_message = NULL;
13100
13101
0
      r_symndx = ELF32_R_SYM (rel->r_info);
13102
0
      r_type   = ELF32_R_TYPE (rel->r_info);
13103
0
      r_type   = arm_real_reloc_type (globals, r_type);
13104
13105
0
      if (   r_type == R_ARM_GNU_VTENTRY
13106
0
    || r_type == R_ARM_GNU_VTINHERIT)
13107
0
  continue;
13108
13109
0
      howto = bfd_reloc.howto = elf32_arm_howto_from_type (r_type);
13110
13111
0
      if (howto == NULL)
13112
0
  return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
13113
13114
0
      h = NULL;
13115
0
      sym = NULL;
13116
0
      sec = NULL;
13117
13118
0
      if (r_symndx < symtab_hdr->sh_info)
13119
0
  {
13120
0
    sym = local_syms + r_symndx;
13121
0
    sym_type = ELF32_ST_TYPE (sym->st_info);
13122
0
    sec = local_sections[r_symndx];
13123
13124
    /* An object file might have a reference to a local
13125
       undefined symbol.  This is a daft object file, but we
13126
       should at least do something about it.  V4BX & NONE
13127
       relocations do not use the symbol and are explicitly
13128
       allowed to use the undefined symbol, so allow those.
13129
       Likewise for relocations against STN_UNDEF.  */
13130
0
    if (r_type != R_ARM_V4BX
13131
0
        && r_type != R_ARM_NONE
13132
0
        && r_symndx != STN_UNDEF
13133
0
        && bfd_is_und_section (sec)
13134
0
        && ELF_ST_BIND (sym->st_info) != STB_WEAK)
13135
0
      (*info->callbacks->undefined_symbol)
13136
0
        (info, bfd_elf_string_from_elf_section
13137
0
         (input_bfd, symtab_hdr->sh_link, sym->st_name),
13138
0
         input_bfd, input_section,
13139
0
         rel->r_offset, true);
13140
13141
0
    if (globals->use_rel)
13142
0
      {
13143
0
        relocation = (sec->output_section->vma
13144
0
          + sec->output_offset
13145
0
          + sym->st_value);
13146
0
        if (!bfd_link_relocatable (info)
13147
0
      && (sec->flags & SEC_MERGE)
13148
0
      && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13149
0
    {
13150
0
      asection *msec;
13151
0
      bfd_vma addend, value;
13152
13153
0
      switch (r_type)
13154
0
        {
13155
0
        case R_ARM_MOVW_ABS_NC:
13156
0
        case R_ARM_MOVT_ABS:
13157
0
          value = bfd_get_32 (input_bfd, contents + rel->r_offset);
13158
0
          addend = ((value & 0xf0000) >> 4) | (value & 0xfff);
13159
0
          addend = (addend ^ 0x8000) - 0x8000;
13160
0
          break;
13161
13162
0
        case R_ARM_THM_MOVW_ABS_NC:
13163
0
        case R_ARM_THM_MOVT_ABS:
13164
0
          value = bfd_get_16 (input_bfd, contents + rel->r_offset)
13165
0
            << 16;
13166
0
          value |= bfd_get_16 (input_bfd,
13167
0
             contents + rel->r_offset + 2);
13168
0
          addend = ((value & 0xf7000) >> 4) | (value & 0xff)
13169
0
             | ((value & 0x04000000) >> 15);
13170
0
          addend = (addend ^ 0x8000) - 0x8000;
13171
0
          break;
13172
13173
0
        default:
13174
0
          if (howto->rightshift
13175
0
        || (howto->src_mask & (howto->src_mask + 1)))
13176
0
      {
13177
0
        _bfd_error_handler
13178
          /* xgettext:c-format */
13179
0
          (_("%pB(%pA+%#" PRIx64 "): "
13180
0
             "%s relocation against SEC_MERGE section"),
13181
0
           input_bfd, input_section,
13182
0
           (uint64_t) rel->r_offset, howto->name);
13183
0
        return false;
13184
0
      }
13185
13186
0
          value = bfd_get_32 (input_bfd, contents + rel->r_offset);
13187
13188
          /* Get the (signed) value from the instruction.  */
13189
0
          addend = value & howto->src_mask;
13190
0
          if (addend & ((howto->src_mask + 1) >> 1))
13191
0
      {
13192
0
        bfd_signed_vma mask;
13193
13194
0
        mask = -1;
13195
0
        mask &= ~ howto->src_mask;
13196
0
        addend |= mask;
13197
0
      }
13198
0
          break;
13199
0
        }
13200
13201
0
      msec = sec;
13202
0
      addend =
13203
0
        _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
13204
0
        - relocation;
13205
0
      addend += msec->output_section->vma + msec->output_offset;
13206
13207
      /* Cases here must match those in the preceding
13208
         switch statement.  */
13209
0
      switch (r_type)
13210
0
        {
13211
0
        case R_ARM_MOVW_ABS_NC:
13212
0
        case R_ARM_MOVT_ABS:
13213
0
          value = (value & 0xfff0f000) | ((addend & 0xf000) << 4)
13214
0
            | (addend & 0xfff);
13215
0
          bfd_put_32 (input_bfd, value, contents + rel->r_offset);
13216
0
          break;
13217
13218
0
        case R_ARM_THM_MOVW_ABS_NC:
13219
0
        case R_ARM_THM_MOVT_ABS:
13220
0
          value = (value & 0xfbf08f00) | ((addend & 0xf700) << 4)
13221
0
            | (addend & 0xff) | ((addend & 0x0800) << 15);
13222
0
          bfd_put_16 (input_bfd, value >> 16,
13223
0
          contents + rel->r_offset);
13224
0
          bfd_put_16 (input_bfd, value,
13225
0
          contents + rel->r_offset + 2);
13226
0
          break;
13227
13228
0
        default:
13229
0
          value = (value & ~ howto->dst_mask)
13230
0
            | (addend & howto->dst_mask);
13231
0
          bfd_put_32 (input_bfd, value, contents + rel->r_offset);
13232
0
          break;
13233
0
        }
13234
0
    }
13235
0
      }
13236
0
    else
13237
0
      relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13238
0
  }
13239
0
      else
13240
0
  {
13241
0
    bool warned, ignored;
13242
13243
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13244
0
           r_symndx, symtab_hdr, sym_hashes,
13245
0
           h, sec, relocation,
13246
0
           unresolved_reloc, warned, ignored);
13247
13248
0
    sym_type = h->type;
13249
0
  }
13250
13251
0
      if (sec != NULL && discarded_section (sec))
13252
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
13253
0
           rel, 1, relend, R_ARM_NONE,
13254
0
           howto, 0, contents);
13255
13256
0
      if (bfd_link_relocatable (info))
13257
0
  {
13258
    /* This is a relocatable link.  We don't have to change
13259
       anything, unless the reloc is against a section symbol,
13260
       in which case we have to adjust according to where the
13261
       section symbol winds up in the output section.  */
13262
0
    if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13263
0
      {
13264
0
        if (globals->use_rel)
13265
0
    arm_add_to_rel (input_bfd, contents + rel->r_offset,
13266
0
        howto, (bfd_signed_vma) sec->output_offset);
13267
0
        else
13268
0
    rel->r_addend += sec->output_offset;
13269
0
      }
13270
0
    continue;
13271
0
  }
13272
13273
0
      if (h != NULL)
13274
0
  name = h->root.root.string;
13275
0
      else
13276
0
  {
13277
0
    name = (bfd_elf_string_from_elf_section
13278
0
      (input_bfd, symtab_hdr->sh_link, sym->st_name));
13279
0
    if (name == NULL || *name == '\0')
13280
0
      name = bfd_section_name (sec);
13281
0
  }
13282
13283
0
      if (r_symndx != STN_UNDEF
13284
0
    && r_type != R_ARM_NONE
13285
0
    && (h == NULL
13286
0
        || h->root.type == bfd_link_hash_defined
13287
0
        || h->root.type == bfd_link_hash_defweak)
13288
0
    && IS_ARM_TLS_RELOC (r_type) != (sym_type == STT_TLS))
13289
0
  {
13290
0
    _bfd_error_handler
13291
0
      ((sym_type == STT_TLS
13292
        /* xgettext:c-format */
13293
0
        ? _("%pB(%pA+%#" PRIx64 "): %s used with TLS symbol %s")
13294
        /* xgettext:c-format */
13295
0
        : _("%pB(%pA+%#" PRIx64 "): %s used with non-TLS symbol %s")),
13296
0
       input_bfd,
13297
0
       input_section,
13298
0
       (uint64_t) rel->r_offset,
13299
0
       howto->name,
13300
0
       name);
13301
0
  }
13302
13303
      /* We call elf32_arm_final_link_relocate unless we're completely
13304
   done, i.e., the relaxation produced the final output we want,
13305
   and we won't let anybody mess with it. Also, we have to do
13306
   addend adjustments in case of a R_ARM_TLS_GOTDESC relocation
13307
   both in relaxed and non-relaxed cases.  */
13308
0
      if ((elf32_arm_tls_transition (info, r_type, h) != (unsigned)r_type)
13309
0
    || (IS_ARM_TLS_GNU_RELOC (r_type)
13310
0
        && !((h ? elf32_arm_hash_entry (h)->tls_type :
13311
0
        elf32_arm_local_got_tls_type (input_bfd)[r_symndx])
13312
0
       & GOT_TLS_GDESC)))
13313
0
  {
13314
0
    r = elf32_arm_tls_relax (globals, input_bfd, input_section,
13315
0
           contents, rel, h == NULL);
13316
    /* This may have been marked unresolved because it came from
13317
       a shared library.  But we've just dealt with that.  */
13318
0
    unresolved_reloc = 0;
13319
0
  }
13320
0
      else
13321
0
  r = bfd_reloc_continue;
13322
13323
0
      if (r == bfd_reloc_continue)
13324
0
  {
13325
0
    unsigned char branch_type =
13326
0
      h ? ARM_GET_SYM_BRANCH_TYPE (h->target_internal)
13327
0
        : ARM_GET_SYM_BRANCH_TYPE (sym->st_target_internal);
13328
13329
0
    r = elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
13330
0
               input_section, contents, rel,
13331
0
               relocation, info, sec, name,
13332
0
               sym_type, branch_type, h,
13333
0
               &unresolved_reloc,
13334
0
               &error_message);
13335
0
  }
13336
13337
      /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13338
   because such sections are not SEC_ALLOC and thus ld.so will
13339
   not process them.  */
13340
0
      if (unresolved_reloc
13341
0
    && !((input_section->flags & SEC_DEBUGGING) != 0
13342
0
         && h->def_dynamic)
13343
0
    && _bfd_elf_section_offset (output_bfd, info, input_section,
13344
0
              rel->r_offset) != (bfd_vma) -1)
13345
0
  {
13346
0
    _bfd_error_handler
13347
      /* xgettext:c-format */
13348
0
      (_("%pB(%pA+%#" PRIx64 "): "
13349
0
         "unresolvable %s relocation against symbol `%s'"),
13350
0
       input_bfd,
13351
0
       input_section,
13352
0
       (uint64_t) rel->r_offset,
13353
0
       howto->name,
13354
0
       h->root.root.string);
13355
0
    return false;
13356
0
  }
13357
13358
0
      if (r != bfd_reloc_ok)
13359
0
  {
13360
0
    switch (r)
13361
0
      {
13362
0
      case bfd_reloc_overflow:
13363
        /* If the overflowing reloc was to an undefined symbol,
13364
     we have already printed one error message and there
13365
     is no point complaining again.  */
13366
0
        if (!h || h->root.type != bfd_link_hash_undefined)
13367
0
    (*info->callbacks->reloc_overflow)
13368
0
      (info, (h ? &h->root : NULL), name, howto->name,
13369
0
       (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
13370
0
        break;
13371
13372
0
      case bfd_reloc_undefined:
13373
0
        (*info->callbacks->undefined_symbol)
13374
0
    (info, name, input_bfd, input_section, rel->r_offset, true);
13375
0
        break;
13376
13377
0
      case bfd_reloc_outofrange:
13378
0
        error_message = _("out of range");
13379
0
        goto common_error;
13380
13381
0
      case bfd_reloc_notsupported:
13382
0
        error_message = _("unsupported relocation");
13383
0
        goto common_error;
13384
13385
0
      case bfd_reloc_dangerous:
13386
        /* error_message should already be set.  */
13387
0
        goto common_error;
13388
13389
0
      default:
13390
0
        error_message = _("unknown error");
13391
        /* Fall through.  */
13392
13393
0
      common_error:
13394
0
        BFD_ASSERT (error_message != NULL);
13395
0
        (*info->callbacks->reloc_dangerous)
13396
0
    (info, error_message, input_bfd, input_section, rel->r_offset);
13397
0
        break;
13398
0
      }
13399
0
  }
13400
0
    }
13401
13402
0
  return true;
13403
0
}
13404
13405
/* Add a new unwind edit to the list described by HEAD, TAIL.  If TINDEX is zero,
13406
   adds the edit to the start of the list.  (The list must be built in order of
13407
   ascending TINDEX: the function's callers are primarily responsible for
13408
   maintaining that condition).  */
13409
13410
static void
13411
add_unwind_table_edit (arm_unwind_table_edit **head,
13412
           arm_unwind_table_edit **tail,
13413
           arm_unwind_edit_type type,
13414
           asection *linked_section,
13415
           unsigned int tindex)
13416
0
{
13417
0
  arm_unwind_table_edit *new_edit = (arm_unwind_table_edit *)
13418
0
      xmalloc (sizeof (arm_unwind_table_edit));
13419
13420
0
  new_edit->type = type;
13421
0
  new_edit->linked_section = linked_section;
13422
0
  new_edit->index = tindex;
13423
13424
0
  if (tindex > 0)
13425
0
    {
13426
0
      new_edit->next = NULL;
13427
13428
0
      if (*tail)
13429
0
  (*tail)->next = new_edit;
13430
13431
0
      (*tail) = new_edit;
13432
13433
0
      if (!*head)
13434
0
  (*head) = new_edit;
13435
0
    }
13436
0
  else
13437
0
    {
13438
0
      new_edit->next = *head;
13439
13440
0
      if (!*tail)
13441
0
  *tail = new_edit;
13442
13443
0
      *head = new_edit;
13444
0
    }
13445
0
}
13446
13447
static _arm_elf_section_data *get_arm_elf_section_data (asection *);
13448
13449
/* Increase the size of EXIDX_SEC by ADJUST bytes.  ADJUST mau be negative.  */
13450
13451
static void
13452
adjust_exidx_size (asection *exidx_sec, int adjust)
13453
0
{
13454
0
  asection *out_sec;
13455
13456
0
  if (!exidx_sec->rawsize)
13457
0
    exidx_sec->rawsize = exidx_sec->size;
13458
13459
0
  bfd_set_section_size (exidx_sec, exidx_sec->size + adjust);
13460
0
  out_sec = exidx_sec->output_section;
13461
  /* Adjust size of output section.  */
13462
0
  bfd_set_section_size (out_sec, out_sec->size + adjust);
13463
0
}
13464
13465
/* Insert an EXIDX_CANTUNWIND marker at the end of a section.  */
13466
13467
static void
13468
insert_cantunwind_after (asection *text_sec, asection *exidx_sec)
13469
0
{
13470
0
  struct _arm_elf_section_data *exidx_arm_data;
13471
13472
0
  exidx_arm_data = get_arm_elf_section_data (exidx_sec);
13473
0
  add_unwind_table_edit
13474
0
    (&exidx_arm_data->u.exidx.unwind_edit_list,
13475
0
     &exidx_arm_data->u.exidx.unwind_edit_tail,
13476
0
     INSERT_EXIDX_CANTUNWIND_AT_END, text_sec, UINT_MAX);
13477
13478
0
  exidx_arm_data->additional_reloc_count++;
13479
13480
0
  adjust_exidx_size (exidx_sec, 8);
13481
0
}
13482
13483
/* Scan .ARM.exidx tables, and create a list describing edits which should be
13484
   made to those tables, such that:
13485
13486
     1. Regions without unwind data are marked with EXIDX_CANTUNWIND entries.
13487
     2. Duplicate entries are merged together (EXIDX_CANTUNWIND, or unwind
13488
  codes which have been inlined into the index).
13489
13490
   If MERGE_EXIDX_ENTRIES is false, duplicate entries are not merged.
13491
13492
   The edits are applied when the tables are written
13493
   (in elf32_arm_write_section).  */
13494
13495
bool
13496
elf32_arm_fix_exidx_coverage (asection **text_section_order,
13497
            unsigned int num_text_sections,
13498
            struct bfd_link_info *info,
13499
            bool merge_exidx_entries)
13500
0
{
13501
0
  bfd *inp;
13502
0
  unsigned int last_second_word = 0, i;
13503
0
  asection *last_exidx_sec = NULL;
13504
0
  asection *last_text_sec = NULL;
13505
0
  int last_unwind_type = -1;
13506
13507
  /* Walk over all EXIDX sections, and create backlinks from the corrsponding
13508
     text sections.  */
13509
0
  for (inp = info->input_bfds; inp != NULL; inp = inp->link.next)
13510
0
    {
13511
0
      asection *sec;
13512
13513
0
      for (sec = inp->sections; sec != NULL; sec = sec->next)
13514
0
  {
13515
0
    struct bfd_elf_section_data *elf_sec = elf_section_data (sec);
13516
0
    Elf_Internal_Shdr *hdr = &elf_sec->this_hdr;
13517
13518
0
    if (!hdr || hdr->sh_type != SHT_ARM_EXIDX)
13519
0
      continue;
13520
13521
0
    if (elf_sec->linked_to)
13522
0
      {
13523
0
        Elf_Internal_Shdr *linked_hdr
13524
0
    = &elf_section_data (elf_sec->linked_to)->this_hdr;
13525
0
        struct _arm_elf_section_data *linked_sec_arm_data
13526
0
    = get_arm_elf_section_data (linked_hdr->bfd_section);
13527
13528
0
        if (linked_sec_arm_data == NULL)
13529
0
    continue;
13530
13531
        /* Link this .ARM.exidx section back from the text section it
13532
     describes.  */
13533
0
        linked_sec_arm_data->u.text.arm_exidx_sec = sec;
13534
0
      }
13535
0
  }
13536
0
    }
13537
13538
  /* Walk all text sections in order of increasing VMA.  Eilminate duplicate
13539
     index table entries (EXIDX_CANTUNWIND and inlined unwind opcodes),
13540
     and add EXIDX_CANTUNWIND entries for sections with no unwind table data.  */
13541
13542
0
  for (i = 0; i < num_text_sections; i++)
13543
0
    {
13544
0
      asection *sec = text_section_order[i];
13545
0
      asection *exidx_sec;
13546
0
      struct _arm_elf_section_data *arm_data = get_arm_elf_section_data (sec);
13547
0
      struct _arm_elf_section_data *exidx_arm_data;
13548
0
      bfd_byte *contents = NULL;
13549
0
      int deleted_exidx_bytes = 0;
13550
0
      bfd_vma j;
13551
0
      arm_unwind_table_edit *unwind_edit_head = NULL;
13552
0
      arm_unwind_table_edit *unwind_edit_tail = NULL;
13553
0
      Elf_Internal_Shdr *hdr;
13554
0
      bfd *ibfd;
13555
13556
0
      if (arm_data == NULL)
13557
0
  continue;
13558
13559
0
      exidx_sec = arm_data->u.text.arm_exidx_sec;
13560
0
      if (exidx_sec == NULL)
13561
0
  {
13562
    /* Section has no unwind data.  */
13563
0
    if (last_unwind_type == 0 || !last_exidx_sec)
13564
0
      continue;
13565
13566
    /* Ignore zero sized sections.  */
13567
0
    if (sec->size == 0)
13568
0
      continue;
13569
13570
0
    insert_cantunwind_after (last_text_sec, last_exidx_sec);
13571
0
    last_unwind_type = 0;
13572
0
    continue;
13573
0
  }
13574
13575
      /* Skip /DISCARD/ sections.  */
13576
0
      if (bfd_is_abs_section (exidx_sec->output_section))
13577
0
  continue;
13578
13579
0
      hdr = &elf_section_data (exidx_sec)->this_hdr;
13580
0
      if (hdr->sh_type != SHT_ARM_EXIDX)
13581
0
  continue;
13582
13583
0
      exidx_arm_data = get_arm_elf_section_data (exidx_sec);
13584
0
      if (exidx_arm_data == NULL)
13585
0
  continue;
13586
13587
0
      ibfd = exidx_sec->owner;
13588
13589
0
      if (hdr->contents != NULL)
13590
0
  contents = hdr->contents;
13591
0
      else if (! bfd_malloc_and_get_section (ibfd, exidx_sec, &contents))
13592
  /* An error?  */
13593
0
  continue;
13594
13595
0
      if (last_unwind_type > 0)
13596
0
  {
13597
0
    unsigned int first_word = bfd_get_32 (ibfd, contents);
13598
    /* Add cantunwind if first unwind item does not match section
13599
       start.  */
13600
0
    if (first_word != sec->vma)
13601
0
      {
13602
0
        insert_cantunwind_after (last_text_sec, last_exidx_sec);
13603
0
        last_unwind_type = 0;
13604
0
      }
13605
0
  }
13606
13607
0
      for (j = 0; j < hdr->sh_size; j += 8)
13608
0
  {
13609
0
    unsigned int second_word = bfd_get_32 (ibfd, contents + j + 4);
13610
0
    int unwind_type;
13611
0
    int elide = 0;
13612
13613
    /* An EXIDX_CANTUNWIND entry.  */
13614
0
    if (second_word == 1)
13615
0
      {
13616
0
        if (last_unwind_type == 0)
13617
0
    elide = 1;
13618
0
        unwind_type = 0;
13619
0
      }
13620
    /* Inlined unwinding data.  Merge if equal to previous.  */
13621
0
    else if ((second_word & 0x80000000) != 0)
13622
0
      {
13623
0
        if (merge_exidx_entries
13624
0
       && last_second_word == second_word && last_unwind_type == 1)
13625
0
    elide = 1;
13626
0
        unwind_type = 1;
13627
0
        last_second_word = second_word;
13628
0
      }
13629
    /* Normal table entry.  In theory we could merge these too,
13630
       but duplicate entries are likely to be much less common.  */
13631
0
    else
13632
0
      unwind_type = 2;
13633
13634
0
    if (elide && !bfd_link_relocatable (info))
13635
0
      {
13636
0
        add_unwind_table_edit (&unwind_edit_head, &unwind_edit_tail,
13637
0
             DELETE_EXIDX_ENTRY, NULL, j / 8);
13638
13639
0
        deleted_exidx_bytes += 8;
13640
0
      }
13641
13642
0
    last_unwind_type = unwind_type;
13643
0
  }
13644
13645
      /* Free contents if we allocated it ourselves.  */
13646
0
      if (contents != hdr->contents)
13647
0
  free (contents);
13648
13649
      /* Record edits to be applied later (in elf32_arm_write_section).  */
13650
0
      exidx_arm_data->u.exidx.unwind_edit_list = unwind_edit_head;
13651
0
      exidx_arm_data->u.exidx.unwind_edit_tail = unwind_edit_tail;
13652
13653
0
      if (deleted_exidx_bytes > 0)
13654
0
  adjust_exidx_size (exidx_sec, - deleted_exidx_bytes);
13655
13656
0
      last_exidx_sec = exidx_sec;
13657
0
      last_text_sec = sec;
13658
0
    }
13659
13660
  /* Add terminating CANTUNWIND entry.  */
13661
0
  if (!bfd_link_relocatable (info) && last_exidx_sec
13662
0
      && last_unwind_type != 0)
13663
0
    insert_cantunwind_after (last_text_sec, last_exidx_sec);
13664
13665
0
  return true;
13666
0
}
13667
13668
static bool
13669
elf32_arm_output_glue_section (struct bfd_link_info *info, bfd *obfd,
13670
             bfd *ibfd, const char *name)
13671
0
{
13672
0
  asection *sec, *osec;
13673
13674
0
  sec = bfd_get_linker_section (ibfd, name);
13675
0
  if (sec == NULL || (sec->flags & SEC_EXCLUDE) != 0)
13676
0
    return true;
13677
13678
0
  osec = sec->output_section;
13679
0
  if (elf32_arm_write_section (obfd, info, sec, sec->contents))
13680
0
    return true;
13681
13682
0
  if (! bfd_set_section_contents (obfd, osec, sec->contents,
13683
0
          sec->output_offset, sec->size))
13684
0
    return false;
13685
13686
0
  return true;
13687
0
}
13688
13689
static bool
13690
elf32_arm_final_link (bfd *abfd, struct bfd_link_info *info)
13691
0
{
13692
0
  struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
13693
0
  asection *sec, *osec;
13694
13695
0
  if (globals == NULL)
13696
0
    return false;
13697
13698
  /* Invoke the regular ELF backend linker to do all the work.  */
13699
0
  if (!_bfd_elf_final_link (abfd, info))
13700
0
    return false;
13701
13702
  /* Process stub sections (eg BE8 encoding, ...).  */
13703
0
  struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
13704
0
  unsigned int i;
13705
0
  for (i=0; i<htab->top_id; i++)
13706
0
    {
13707
0
      sec = htab->stub_group[i].stub_sec;
13708
      /* Only process it once, in its link_sec slot.  */
13709
0
      if (sec && i == htab->stub_group[i].link_sec->id)
13710
0
  {
13711
0
    osec = sec->output_section;
13712
0
    elf32_arm_write_section (abfd, info, sec, sec->contents);
13713
0
    if (! bfd_set_section_contents (abfd, osec, sec->contents,
13714
0
            sec->output_offset, sec->size))
13715
0
      return false;
13716
0
  }
13717
0
    }
13718
13719
  /* Write out any glue sections now that we have created all the
13720
     stubs.  */
13721
0
  if (globals->bfd_of_glue_owner != NULL)
13722
0
    {
13723
0
      if (! elf32_arm_output_glue_section (info, abfd,
13724
0
             globals->bfd_of_glue_owner,
13725
0
             ARM2THUMB_GLUE_SECTION_NAME))
13726
0
  return false;
13727
13728
0
      if (! elf32_arm_output_glue_section (info, abfd,
13729
0
             globals->bfd_of_glue_owner,
13730
0
             THUMB2ARM_GLUE_SECTION_NAME))
13731
0
  return false;
13732
13733
0
      if (! elf32_arm_output_glue_section (info, abfd,
13734
0
             globals->bfd_of_glue_owner,
13735
0
             VFP11_ERRATUM_VENEER_SECTION_NAME))
13736
0
  return false;
13737
13738
0
      if (! elf32_arm_output_glue_section (info, abfd,
13739
0
             globals->bfd_of_glue_owner,
13740
0
             STM32L4XX_ERRATUM_VENEER_SECTION_NAME))
13741
0
  return false;
13742
13743
0
      if (! elf32_arm_output_glue_section (info, abfd,
13744
0
             globals->bfd_of_glue_owner,
13745
0
             ARM_BX_GLUE_SECTION_NAME))
13746
0
  return false;
13747
0
    }
13748
13749
0
  return true;
13750
0
}
13751
13752
/* Return a best guess for the machine number based on the attributes.  */
13753
13754
static unsigned int
13755
bfd_arm_get_mach_from_attributes (bfd * abfd)
13756
815
{
13757
815
  int arch = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC, Tag_CPU_arch);
13758
13759
815
  switch (arch)
13760
815
    {
13761
815
    case TAG_CPU_ARCH_PRE_V4: return bfd_mach_arm_3M;
13762
0
    case TAG_CPU_ARCH_V4: return bfd_mach_arm_4;
13763
0
    case TAG_CPU_ARCH_V4T: return bfd_mach_arm_4T;
13764
0
    case TAG_CPU_ARCH_V5T: return bfd_mach_arm_5T;
13765
13766
0
    case TAG_CPU_ARCH_V5TE:
13767
0
      {
13768
0
  char * name;
13769
13770
0
  BFD_ASSERT (Tag_CPU_name < NUM_KNOWN_OBJ_ATTRIBUTES);
13771
0
  name = elf_known_obj_attributes (abfd) [OBJ_ATTR_PROC][Tag_CPU_name].s;
13772
13773
0
  if (name)
13774
0
    {
13775
0
      if (strcmp (name, "IWMMXT2") == 0)
13776
0
        return bfd_mach_arm_iWMMXt2;
13777
13778
0
      if (strcmp (name, "IWMMXT") == 0)
13779
0
        return bfd_mach_arm_iWMMXt;
13780
13781
0
      if (strcmp (name, "XSCALE") == 0)
13782
0
        {
13783
0
    int wmmx;
13784
13785
0
    BFD_ASSERT (Tag_WMMX_arch < NUM_KNOWN_OBJ_ATTRIBUTES);
13786
0
    wmmx = elf_known_obj_attributes (abfd) [OBJ_ATTR_PROC][Tag_WMMX_arch].i;
13787
0
    switch (wmmx)
13788
0
      {
13789
0
      case 1: return bfd_mach_arm_iWMMXt;
13790
0
      case 2: return bfd_mach_arm_iWMMXt2;
13791
0
      default: return bfd_mach_arm_XScale;
13792
0
      }
13793
0
        }
13794
0
    }
13795
13796
0
  return bfd_mach_arm_5TE;
13797
0
      }
13798
13799
0
    case TAG_CPU_ARCH_V5TEJ:
13800
0
  return bfd_mach_arm_5TEJ;
13801
0
    case TAG_CPU_ARCH_V6:
13802
0
  return bfd_mach_arm_6;
13803
0
    case TAG_CPU_ARCH_V6KZ:
13804
0
  return bfd_mach_arm_6KZ;
13805
0
    case TAG_CPU_ARCH_V6T2:
13806
0
  return bfd_mach_arm_6T2;
13807
0
    case TAG_CPU_ARCH_V6K:
13808
0
  return bfd_mach_arm_6K;
13809
0
    case TAG_CPU_ARCH_V7:
13810
0
  return bfd_mach_arm_7;
13811
0
    case TAG_CPU_ARCH_V6_M:
13812
0
  return bfd_mach_arm_6M;
13813
0
    case TAG_CPU_ARCH_V6S_M:
13814
0
  return bfd_mach_arm_6SM;
13815
0
    case TAG_CPU_ARCH_V7E_M:
13816
0
  return bfd_mach_arm_7EM;
13817
0
    case TAG_CPU_ARCH_V8:
13818
0
  return bfd_mach_arm_8;
13819
0
    case TAG_CPU_ARCH_V8R:
13820
0
  return bfd_mach_arm_8R;
13821
0
    case TAG_CPU_ARCH_V8M_BASE:
13822
0
  return bfd_mach_arm_8M_BASE;
13823
0
    case TAG_CPU_ARCH_V8M_MAIN:
13824
0
  return bfd_mach_arm_8M_MAIN;
13825
0
    case TAG_CPU_ARCH_V8_1M_MAIN:
13826
0
  return bfd_mach_arm_8_1M_MAIN;
13827
0
    case TAG_CPU_ARCH_V9:
13828
0
  return bfd_mach_arm_9;
13829
13830
0
    default:
13831
      /* Force entry to be added for any new known Tag_CPU_arch value.  */
13832
0
      BFD_ASSERT (arch > MAX_TAG_CPU_ARCH);
13833
13834
      /* Unknown Tag_CPU_arch value.  */
13835
0
      return bfd_mach_arm_unknown;
13836
815
    }
13837
815
}
13838
13839
/* Set the right machine number.  */
13840
13841
static bool
13842
elf32_arm_object_p (bfd *abfd)
13843
815
{
13844
815
  unsigned int mach;
13845
13846
815
  mach = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
13847
13848
815
  if (mach == bfd_mach_arm_unknown)
13849
815
    mach = bfd_arm_get_mach_from_attributes (abfd);
13850
13851
815
  bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
13852
815
  return true;
13853
815
}
13854
13855
/* Function to keep ARM specific flags in the ELF header.  */
13856
13857
static bool
13858
elf32_arm_set_private_flags (bfd *abfd, flagword flags)
13859
0
{
13860
0
  if (elf_flags_init (abfd)
13861
0
      && elf_elfheader (abfd)->e_flags != flags)
13862
0
    {
13863
0
      if (EF_ARM_EABI_VERSION (flags) == EF_ARM_EABI_UNKNOWN)
13864
0
  {
13865
0
    if (flags & EF_ARM_INTERWORK)
13866
0
      _bfd_error_handler
13867
0
        (_("warning: not setting interworking flag of %pB since it has already been specified as non-interworking"),
13868
0
         abfd);
13869
0
    else
13870
0
      _bfd_error_handler
13871
0
        (_("warning: clearing the interworking flag of %pB due to outside request"),
13872
0
         abfd);
13873
0
  }
13874
0
    }
13875
0
  else
13876
0
    {
13877
0
      elf_elfheader (abfd)->e_flags = flags;
13878
0
      elf_flags_init (abfd) = true;
13879
0
    }
13880
13881
0
  return true;
13882
0
}
13883
13884
/* Copy backend specific data from one object module to another.  */
13885
13886
static bool
13887
elf32_arm_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
13888
3
{
13889
3
  flagword in_flags;
13890
3
  flagword out_flags;
13891
13892
3
  if (! is_arm_elf (ibfd))
13893
0
    return true;
13894
13895
3
  in_flags  = elf_elfheader (ibfd)->e_flags;
13896
3
  out_flags = elf_elfheader (obfd)->e_flags;
13897
13898
3
  if (elf_flags_init (obfd)
13899
0
      && EF_ARM_EABI_VERSION (out_flags) == EF_ARM_EABI_UNKNOWN
13900
0
      && in_flags != out_flags)
13901
0
    {
13902
      /* Cannot mix APCS26 and APCS32 code.  */
13903
0
      if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
13904
0
  return false;
13905
13906
      /* Cannot mix float APCS and non-float APCS code.  */
13907
0
      if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
13908
0
  return false;
13909
13910
      /* If the src and dest have different interworking flags
13911
   then turn off the interworking bit.  */
13912
0
      if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
13913
0
  {
13914
0
    if (out_flags & EF_ARM_INTERWORK)
13915
0
      _bfd_error_handler
13916
0
        (_("warning: clearing the interworking flag of %pB because non-interworking code in %pB has been linked with it"),
13917
0
         obfd, ibfd);
13918
13919
0
    in_flags &= ~EF_ARM_INTERWORK;
13920
0
  }
13921
13922
      /* Likewise for PIC, though don't warn for this case.  */
13923
0
      if ((in_flags & EF_ARM_PIC) != (out_flags & EF_ARM_PIC))
13924
0
  in_flags &= ~EF_ARM_PIC;
13925
0
    }
13926
13927
3
  elf_elfheader (obfd)->e_flags = in_flags;
13928
3
  elf_flags_init (obfd) = true;
13929
13930
3
  return _bfd_elf_copy_private_bfd_data (ibfd, obfd);
13931
3
}
13932
13933
/* Values for Tag_ABI_PCS_R9_use.  */
13934
enum
13935
{
13936
  AEABI_R9_V6,
13937
  AEABI_R9_SB,
13938
  AEABI_R9_TLS,
13939
  AEABI_R9_unused
13940
};
13941
13942
/* Values for Tag_ABI_PCS_RW_data.  */
13943
enum
13944
{
13945
  AEABI_PCS_RW_data_absolute,
13946
  AEABI_PCS_RW_data_PCrel,
13947
  AEABI_PCS_RW_data_SBrel,
13948
  AEABI_PCS_RW_data_unused
13949
};
13950
13951
/* Values for Tag_ABI_enum_size.  */
13952
enum
13953
{
13954
  AEABI_enum_unused,
13955
  AEABI_enum_short,
13956
  AEABI_enum_wide,
13957
  AEABI_enum_forced_wide
13958
};
13959
13960
/* Determine whether an object attribute tag takes an integer, a
13961
   string or both.  */
13962
13963
static int
13964
elf32_arm_obj_attrs_arg_type (obj_attr_tag_t tag)
13965
0
{
13966
0
  if (tag == Tag_compatibility)
13967
0
    return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL;
13968
0
  else if (tag == Tag_nodefaults)
13969
0
    return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_NO_DEFAULT;
13970
0
  else if (tag == Tag_CPU_raw_name || tag == Tag_CPU_name)
13971
0
    return ATTR_TYPE_FLAG_STR_VAL;
13972
0
  else if (tag < 32)
13973
0
    return ATTR_TYPE_FLAG_INT_VAL;
13974
0
  else
13975
0
    return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
13976
0
}
13977
13978
/* The ABI defines that Tag_conformance should be emitted first, and that
13979
   Tag_nodefaults should be second (if either is defined).  This sets those
13980
   two positions, and bumps up the position of all the remaining tags to
13981
   compensate.  */
13982
static int
13983
elf32_arm_obj_attrs_order (int num)
13984
0
{
13985
0
  if (num == LEAST_KNOWN_OBJ_ATTRIBUTE)
13986
0
    return Tag_conformance;
13987
0
  if (num == LEAST_KNOWN_OBJ_ATTRIBUTE + 1)
13988
0
    return Tag_nodefaults;
13989
0
  if ((num - 2) < Tag_nodefaults)
13990
0
    return num - 2;
13991
0
  if ((num - 1) < Tag_conformance)
13992
0
    return num - 1;
13993
0
  return num;
13994
0
}
13995
13996
/* Attribute numbers >=64 (mod 128) can be safely ignored.  */
13997
static bool
13998
elf32_arm_obj_attrs_handle_unknown (bfd *abfd, int tag)
13999
0
{
14000
0
  if ((tag & 127) < 64)
14001
0
    {
14002
0
      _bfd_error_handler
14003
0
  (_("%pB: unknown mandatory EABI object attribute %d"),
14004
0
   abfd, tag);
14005
0
      bfd_set_error (bfd_error_bad_value);
14006
0
      return false;
14007
0
    }
14008
0
  else
14009
0
    {
14010
0
      _bfd_error_handler
14011
0
  (_("warning: %pB: unknown EABI object attribute %d"),
14012
0
   abfd, tag);
14013
0
      return true;
14014
0
    }
14015
0
}
14016
14017
/* Read the architecture from the Tag_also_compatible_with attribute, if any.
14018
   Returns -1 if no architecture could be read.  */
14019
14020
static int
14021
get_secondary_compatible_arch (bfd *abfd)
14022
0
{
14023
0
  obj_attribute *attr =
14024
0
    &elf_known_obj_attributes_proc (abfd)[Tag_also_compatible_with];
14025
14026
  /* Note: the tag and its argument below are uleb128 values, though
14027
     currently-defined values fit in one byte for each.  */
14028
0
  if (attr->s
14029
0
      && attr->s[0] == Tag_CPU_arch
14030
0
      && (attr->s[1] & 128) != 128
14031
0
      && attr->s[2] == 0)
14032
0
   return attr->s[1];
14033
14034
  /* This tag is "safely ignorable", so don't complain if it looks funny.  */
14035
0
  return -1;
14036
0
}
14037
14038
/* Set, or unset, the architecture of the Tag_also_compatible_with attribute.
14039
   The tag is removed if ARCH is -1.  */
14040
14041
static void
14042
set_secondary_compatible_arch (bfd *abfd, int arch)
14043
0
{
14044
0
  obj_attribute *attr =
14045
0
    &elf_known_obj_attributes_proc (abfd)[Tag_also_compatible_with];
14046
14047
0
  if (arch == -1)
14048
0
    {
14049
0
      attr->s = NULL;
14050
0
      return;
14051
0
    }
14052
14053
  /* Note: the tag and its argument below are uleb128 values, though
14054
     currently-defined values fit in one byte for each.  */
14055
0
  if (!attr->s)
14056
0
    attr->s = (char *) bfd_alloc (abfd, 3);
14057
0
  attr->s[0] = Tag_CPU_arch;
14058
0
  attr->s[1] = arch;
14059
0
  attr->s[2] = '\0';
14060
0
}
14061
14062
/* Combine two values for Tag_CPU_arch, taking secondary compatibility tags
14063
   into account.  */
14064
14065
static int
14066
tag_cpu_arch_combine (bfd *ibfd, int oldtag, int *secondary_compat_out,
14067
          int newtag, int secondary_compat,
14068
          const char *const name_table[])
14069
0
{
14070
0
#define T(X) TAG_CPU_ARCH_##X
14071
0
  int tagl, tagh, result;
14072
0
  static const int v6t2[] =
14073
0
    {
14074
0
      T(V6T2),   /* PRE_V4.  */
14075
0
      T(V6T2),   /* V4.  */
14076
0
      T(V6T2),   /* V4T.  */
14077
0
      T(V6T2),   /* V5T.  */
14078
0
      T(V6T2),   /* V5TE.  */
14079
0
      T(V6T2),   /* V5TEJ.  */
14080
0
      T(V6T2),   /* V6.  */
14081
0
      T(V7),     /* V6KZ.  */
14082
0
      T(V6T2)    /* V6T2.  */
14083
0
    };
14084
0
  static const int v6k[] =
14085
0
    {
14086
0
      T(V6K),    /* PRE_V4.  */
14087
0
      T(V6K),    /* V4.  */
14088
0
      T(V6K),    /* V4T.  */
14089
0
      T(V6K),    /* V5T.  */
14090
0
      T(V6K),    /* V5TE.  */
14091
0
      T(V6K),    /* V5TEJ.  */
14092
0
      T(V6K),    /* V6.  */
14093
0
      T(V6KZ),   /* V6KZ.  */
14094
0
      T(V7),     /* V6T2.  */
14095
0
      T(V6K)     /* V6K.  */
14096
0
    };
14097
0
  static const int v7[] =
14098
0
    {
14099
0
      T(V7),     /* PRE_V4.  */
14100
0
      T(V7),     /* V4.  */
14101
0
      T(V7),     /* V4T.  */
14102
0
      T(V7),     /* V5T.  */
14103
0
      T(V7),     /* V5TE.  */
14104
0
      T(V7),     /* V5TEJ.  */
14105
0
      T(V7),     /* V6.  */
14106
0
      T(V7),     /* V6KZ.  */
14107
0
      T(V7),     /* V6T2.  */
14108
0
      T(V7),     /* V6K.  */
14109
0
      T(V7)      /* V7.  */
14110
0
    };
14111
0
  static const int v6_m[] =
14112
0
    {
14113
0
      -1,  /* PRE_V4.  */
14114
0
      -1,  /* V4.  */
14115
0
      T(V6K),    /* V4T.  */
14116
0
      T(V6K),    /* V5T.  */
14117
0
      T(V6K),    /* V5TE.  */
14118
0
      T(V6K),    /* V5TEJ.  */
14119
0
      T(V6K),    /* V6.  */
14120
0
      T(V6KZ),   /* V6KZ.  */
14121
0
      T(V7),     /* V6T2.  */
14122
0
      T(V6K),    /* V6K.  */
14123
0
      T(V7),     /* V7.  */
14124
0
      T(V6_M)    /* V6_M.  */
14125
0
    };
14126
0
  static const int v6s_m[] =
14127
0
    {
14128
0
      -1,  /* PRE_V4.  */
14129
0
      -1,  /* V4.  */
14130
0
      T(V6K),    /* V4T.  */
14131
0
      T(V6K),    /* V5T.  */
14132
0
      T(V6K),    /* V5TE.  */
14133
0
      T(V6K),    /* V5TEJ.  */
14134
0
      T(V6K),    /* V6.  */
14135
0
      T(V6KZ),   /* V6KZ.  */
14136
0
      T(V7),     /* V6T2.  */
14137
0
      T(V6K),    /* V6K.  */
14138
0
      T(V7),     /* V7.  */
14139
0
      T(V6S_M),  /* V6_M.  */
14140
0
      T(V6S_M)   /* V6S_M.  */
14141
0
    };
14142
0
  static const int v7e_m[] =
14143
0
    {
14144
0
      -1,  /* PRE_V4.  */
14145
0
      -1,  /* V4.  */
14146
0
      T(V7E_M),  /* V4T.  */
14147
0
      T(V7E_M),  /* V5T.  */
14148
0
      T(V7E_M),  /* V5TE.  */
14149
0
      T(V7E_M),  /* V5TEJ.  */
14150
0
      T(V7E_M),  /* V6.  */
14151
0
      T(V7E_M),  /* V6KZ.  */
14152
0
      T(V7E_M),  /* V6T2.  */
14153
0
      T(V7E_M),  /* V6K.  */
14154
0
      T(V7E_M),  /* V7.  */
14155
0
      T(V7E_M),  /* V6_M.  */
14156
0
      T(V7E_M),  /* V6S_M.  */
14157
0
      T(V7E_M)   /* V7E_M.  */
14158
0
    };
14159
0
  static const int v8[] =
14160
0
    {
14161
0
      T(V8),   /* PRE_V4.  */
14162
0
      T(V8),   /* V4.  */
14163
0
      T(V8),   /* V4T.  */
14164
0
      T(V8),   /* V5T.  */
14165
0
      T(V8),   /* V5TE.  */
14166
0
      T(V8),   /* V5TEJ.  */
14167
0
      T(V8),   /* V6.  */
14168
0
      T(V8),   /* V6KZ.  */
14169
0
      T(V8),   /* V6T2.  */
14170
0
      T(V8),   /* V6K.  */
14171
0
      T(V8),   /* V7.  */
14172
0
      T(V8),   /* V6_M.  */
14173
0
      T(V8),   /* V6S_M.  */
14174
0
      T(V8),   /* V7E_M.  */
14175
0
      T(V8),   /* V8.  */
14176
0
      T(V8),   /* V8-R.  */
14177
0
      T(V8),   /* V8-M.BASE.  */
14178
0
      T(V8),   /* V8-M.MAIN.  */
14179
0
      T(V8),   /* V8.1.  */
14180
0
      T(V8),   /* V8.2.  */
14181
0
      T(V8),   /* V8.3.  */
14182
0
      T(V8),   /* V8.1-M.MAIN.  */
14183
0
    };
14184
0
  static const int v8r[] =
14185
0
    {
14186
0
      T(V8R),    /* PRE_V4.  */
14187
0
      T(V8R),    /* V4.  */
14188
0
      T(V8R),    /* V4T.  */
14189
0
      T(V8R),    /* V5T.  */
14190
0
      T(V8R),    /* V5TE.  */
14191
0
      T(V8R),    /* V5TEJ.  */
14192
0
      T(V8R),    /* V6.  */
14193
0
      T(V8R),    /* V6KZ.  */
14194
0
      T(V8R),    /* V6T2.  */
14195
0
      T(V8R),    /* V6K.  */
14196
0
      T(V8R),    /* V7.  */
14197
0
      T(V8R),    /* V6_M.  */
14198
0
      T(V8R),    /* V6S_M.  */
14199
0
      T(V8R),    /* V7E_M.  */
14200
0
      T(V8),   /* V8.  */
14201
0
      T(V8R),    /* V8R.  */
14202
0
    };
14203
0
  static const int v8m_baseline[] =
14204
0
    {
14205
0
      -1,   /* PRE_V4.  */
14206
0
      -1,   /* V4.  */
14207
0
      -1,   /* V4T.  */
14208
0
      -1,   /* V5T.  */
14209
0
      -1,   /* V5TE.  */
14210
0
      -1,   /* V5TEJ.  */
14211
0
      -1,   /* V6.  */
14212
0
      -1,   /* V6KZ.  */
14213
0
      -1,   /* V6T2.  */
14214
0
      -1,   /* V6K.  */
14215
0
      -1,   /* V7.  */
14216
0
      T(V8M_BASE), /* V6_M.  */
14217
0
      T(V8M_BASE), /* V6S_M.  */
14218
0
      -1,   /* V7E_M.  */
14219
0
      -1,   /* V8.  */
14220
0
      -1,   /* V8R.  */
14221
0
      T(V8M_BASE)  /* V8-M BASELINE.  */
14222
0
    };
14223
0
  static const int v8m_mainline[] =
14224
0
    {
14225
0
      -1,   /* PRE_V4.  */
14226
0
      -1,   /* V4.  */
14227
0
      -1,   /* V4T.  */
14228
0
      -1,   /* V5T.  */
14229
0
      -1,   /* V5TE.  */
14230
0
      -1,   /* V5TEJ.  */
14231
0
      -1,   /* V6.  */
14232
0
      -1,   /* V6KZ.  */
14233
0
      -1,   /* V6T2.  */
14234
0
      -1,   /* V6K.  */
14235
0
      T(V8M_MAIN), /* V7.  */
14236
0
      T(V8M_MAIN), /* V6_M.  */
14237
0
      T(V8M_MAIN), /* V6S_M.  */
14238
0
      T(V8M_MAIN), /* V7E_M.  */
14239
0
      -1,   /* V8.  */
14240
0
      -1,   /* V8R.  */
14241
0
      T(V8M_MAIN), /* V8-M BASELINE.  */
14242
0
      T(V8M_MAIN)  /* V8-M MAINLINE.  */
14243
0
    };
14244
0
  static const int v8_1m_mainline[] =
14245
0
    {
14246
0
      -1,   /* PRE_V4.  */
14247
0
      -1,   /* V4.  */
14248
0
      -1,   /* V4T.  */
14249
0
      -1,   /* V5T.  */
14250
0
      -1,   /* V5TE.  */
14251
0
      -1,   /* V5TEJ.  */
14252
0
      -1,   /* V6.  */
14253
0
      -1,   /* V6KZ.  */
14254
0
      -1,   /* V6T2.  */
14255
0
      -1,   /* V6K.  */
14256
0
      T(V8_1M_MAIN), /* V7.  */
14257
0
      T(V8_1M_MAIN), /* V6_M.  */
14258
0
      T(V8_1M_MAIN), /* V6S_M.  */
14259
0
      T(V8_1M_MAIN), /* V7E_M.  */
14260
0
      -1,   /* V8.  */
14261
0
      -1,   /* V8R.  */
14262
0
      T(V8_1M_MAIN), /* V8-M BASELINE.  */
14263
0
      T(V8_1M_MAIN), /* V8-M MAINLINE.  */
14264
0
      -1,   /* Unused (18).  */
14265
0
      -1,   /* Unused (19).  */
14266
0
      -1,   /* Unused (20).  */
14267
0
      T(V8_1M_MAIN)  /* V8.1-M MAINLINE.  */
14268
0
    };
14269
0
  static const int v9[] =
14270
0
    {
14271
0
      T(V9),   /* PRE_V4.  */
14272
0
      T(V9),   /* V4.  */
14273
0
      T(V9),   /* V4T.  */
14274
0
      T(V9),   /* V5T.  */
14275
0
      T(V9),   /* V5TE.  */
14276
0
      T(V9),   /* V5TEJ.  */
14277
0
      T(V9),   /* V6.  */
14278
0
      T(V9),   /* V6KZ.  */
14279
0
      T(V9),   /* V6T2.  */
14280
0
      T(V9),   /* V6K.  */
14281
0
      T(V9),   /* V7.  */
14282
0
      T(V9),   /* V6_M.  */
14283
0
      T(V9),   /* V6S_M.  */
14284
0
      T(V9),   /* V7E_M.  */
14285
0
      T(V9),   /* V8.  */
14286
0
      T(V9),   /* V8-R.  */
14287
0
      T(V9),   /* V8-M.BASE.  */
14288
0
      T(V9),   /* V8-M.MAIN.  */
14289
0
      T(V9),   /* V8.1.  */
14290
0
      T(V9),   /* V8.2.  */
14291
0
      T(V9),   /* V8.3.  */
14292
0
      T(V9),   /* V8.1-M.MAIN.  */
14293
0
      T(V9),   /* V9.  */
14294
0
     };
14295
0
  static const int v4t_plus_v6_m[] =
14296
0
    {
14297
0
      -1,   /* PRE_V4.  */
14298
0
      -1,   /* V4.  */
14299
0
      T(V4T),    /* V4T.  */
14300
0
      T(V5T),    /* V5T.  */
14301
0
      T(V5TE),   /* V5TE.  */
14302
0
      T(V5TEJ),    /* V5TEJ.  */
14303
0
      T(V6),   /* V6.  */
14304
0
      T(V6KZ),   /* V6KZ.  */
14305
0
      T(V6T2),   /* V6T2.  */
14306
0
      T(V6K),    /* V6K.  */
14307
0
      T(V7),   /* V7.  */
14308
0
      T(V6_M),   /* V6_M.  */
14309
0
      T(V6S_M),    /* V6S_M.  */
14310
0
      T(V7E_M),    /* V7E_M.  */
14311
0
      T(V8),   /* V8.  */
14312
0
      -1,   /* V8R.  */
14313
0
      T(V8M_BASE), /* V8-M BASELINE.  */
14314
0
      T(V8M_MAIN), /* V8-M MAINLINE.  */
14315
0
      -1,   /* Unused (18).  */
14316
0
      -1,   /* Unused (19).  */
14317
0
      -1,   /* Unused (20).  */
14318
0
      T(V8_1M_MAIN), /* V8.1-M MAINLINE.  */
14319
0
      T(V9),   /* V9.  */
14320
0
      T(V4T_PLUS_V6_M) /* V4T plus V6_M.  */
14321
0
    };
14322
0
  static const int *const comb[] =
14323
0
    {
14324
0
      v6t2,
14325
0
      v6k,
14326
0
      v7,
14327
0
      v6_m,
14328
0
      v6s_m,
14329
0
      v7e_m,
14330
0
      v8,
14331
0
      v8r,
14332
0
      v8m_baseline,
14333
0
      v8m_mainline,
14334
0
      NULL,
14335
0
      NULL,
14336
0
      NULL,
14337
0
      v8_1m_mainline,
14338
0
      v9,
14339
      /* Pseudo-architecture.  */
14340
0
      v4t_plus_v6_m
14341
0
    };
14342
14343
  /* Check we've not got a higher architecture than we know about.  */
14344
14345
0
  if (oldtag > MAX_TAG_CPU_ARCH || newtag > MAX_TAG_CPU_ARCH)
14346
0
    {
14347
0
      _bfd_error_handler (_("error: %pB: unknown CPU architecture"), ibfd);
14348
0
      return -1;
14349
0
    }
14350
14351
  /* Override old tag if we have a Tag_also_compatible_with on the output.  */
14352
14353
0
  if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
14354
0
      || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
14355
0
    oldtag = T(V4T_PLUS_V6_M);
14356
14357
  /* And override the new tag if we have a Tag_also_compatible_with on the
14358
     input.  */
14359
14360
0
  if ((newtag == T(V6_M) && secondary_compat == T(V4T))
14361
0
      || (newtag == T(V4T) && secondary_compat == T(V6_M)))
14362
0
    newtag = T(V4T_PLUS_V6_M);
14363
14364
0
  tagl = (oldtag < newtag) ? oldtag : newtag;
14365
0
  result = tagh = (oldtag > newtag) ? oldtag : newtag;
14366
14367
  /* Architectures before V6KZ add features monotonically.  */
14368
0
  if (tagh <= TAG_CPU_ARCH_V6KZ)
14369
0
    return result;
14370
14371
0
  result = comb[tagh - T(V6T2)] ? comb[tagh - T(V6T2)][tagl] : -1;
14372
14373
  /* Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
14374
     as the canonical version.  */
14375
0
  if (result == T(V4T_PLUS_V6_M))
14376
0
    {
14377
0
      result = T(V4T);
14378
0
      *secondary_compat_out = T(V6_M);
14379
0
    }
14380
0
  else
14381
0
    *secondary_compat_out = -1;
14382
14383
0
  if (result == -1)
14384
0
    {
14385
0
      _bfd_error_handler (_("error: conflicting CPU architectures %s vs %s in %pB"),
14386
0
        name_table[oldtag], name_table[newtag], ibfd);
14387
0
      return -1;
14388
0
    }
14389
14390
0
  return result;
14391
0
#undef T
14392
0
}
14393
14394
/* Query attributes object to see if integer divide instructions may be
14395
   present in an object.  */
14396
static bool
14397
elf32_arm_attributes_accept_div (const obj_attribute *attr)
14398
0
{
14399
0
  int arch = attr[Tag_CPU_arch].i;
14400
0
  int profile = attr[Tag_CPU_arch_profile].i;
14401
14402
0
  switch (attr[Tag_DIV_use].i)
14403
0
    {
14404
0
    case 0:
14405
      /* Integer divide allowed if instruction contained in archetecture.  */
14406
0
      if (arch == TAG_CPU_ARCH_V7 && (profile == 'R' || profile == 'M'))
14407
0
  return true;
14408
0
      else if (arch >= TAG_CPU_ARCH_V7E_M)
14409
0
  return true;
14410
0
      else
14411
0
  return false;
14412
14413
0
    case 1:
14414
      /* Integer divide explicitly prohibited.  */
14415
0
      return false;
14416
14417
0
    default:
14418
      /* Unrecognised case - treat as allowing divide everywhere.  */
14419
0
    case 2:
14420
      /* Integer divide allowed in ARM state.  */
14421
0
      return true;
14422
0
    }
14423
0
}
14424
14425
/* Query attributes object to see if integer divide instructions are
14426
   forbidden to be in the object.  This is not the inverse of
14427
   elf32_arm_attributes_accept_div.  */
14428
static bool
14429
elf32_arm_attributes_forbid_div (const obj_attribute *attr)
14430
0
{
14431
0
  return attr[Tag_DIV_use].i == 1;
14432
0
}
14433
14434
/* Merge EABI object attributes from IBFD into OBFD.  Raise an error if there
14435
   are conflicting attributes.  */
14436
14437
static bool
14438
elf32_arm_merge_eabi_attributes (bfd *ibfd, struct bfd_link_info *info)
14439
0
{
14440
0
  bfd *obfd = info->output_bfd;
14441
0
  obj_attribute *in_attr;
14442
0
  obj_attribute *out_attr;
14443
  /* Some tags have 0 = don't care, 1 = strong requirement,
14444
     2 = weak requirement.  */
14445
0
  static const int order_021[3] = {0, 2, 1};
14446
0
  int i;
14447
0
  bool result = true;
14448
0
  const char *sec_name = get_elf_backend_data (ibfd)->obj_attrs_section;
14449
14450
  /* Skip the linker stubs file.  This preserves previous behavior
14451
     of accepting unknown attributes in the first input file - but
14452
     is that a bug?  */
14453
0
  if (ibfd->flags & BFD_LINKER_CREATED)
14454
0
    return true;
14455
14456
  /* Skip any input that hasn't attribute section.
14457
     This enables to link object files without attribute section with
14458
     any others.  */
14459
0
  if (bfd_get_section_by_name (ibfd, sec_name) == NULL)
14460
0
    return true;
14461
14462
0
  if (!elf_known_obj_attributes_proc (obfd)[0].i)
14463
0
    {
14464
      /* This is the first object.  Copy the attributes.  */
14465
0
      _bfd_elf_copy_obj_attributes (ibfd, obfd);
14466
14467
0
      out_attr = elf_known_obj_attributes_proc (obfd);
14468
14469
      /* Use the Tag_null value to indicate the attributes have been
14470
   initialized.  */
14471
0
      out_attr[0].i = 1;
14472
14473
      /* We do not output objects with Tag_MPextension_use_legacy - we move
14474
   the attribute's value to Tag_MPextension_use.  */
14475
0
      if (out_attr[Tag_MPextension_use_legacy].i != 0)
14476
0
  {
14477
0
    if (out_attr[Tag_MPextension_use].i != 0
14478
0
        && out_attr[Tag_MPextension_use_legacy].i
14479
0
    != out_attr[Tag_MPextension_use].i)
14480
0
      {
14481
0
        _bfd_error_handler
14482
0
    (_("Error: %pB has both the current and legacy "
14483
0
       "Tag_MPextension_use attributes"), ibfd);
14484
0
        result = false;
14485
0
      }
14486
14487
0
    out_attr[Tag_MPextension_use] =
14488
0
      out_attr[Tag_MPextension_use_legacy];
14489
0
    out_attr[Tag_MPextension_use_legacy].type = 0;
14490
0
    out_attr[Tag_MPextension_use_legacy].i = 0;
14491
0
  }
14492
14493
      /* PR 28859 and 28848:  Handle the case where the first input file,
14494
   eg crti.o, has a Tag_ABI_HardFP_use of 3 but no Tag_FP_arch set.
14495
   Using Tag_ABI_HardFP_use in this way is deprecated, so reset the
14496
   attribute to zero.
14497
   FIXME: Should we handle other non-zero values of Tag_ABI_HardFO_use ? */
14498
0
      if (out_attr[Tag_ABI_HardFP_use].i == 3 && out_attr[Tag_FP_arch].i == 0)
14499
0
  out_attr[Tag_ABI_HardFP_use].i = 0;
14500
14501
0
      return result;
14502
0
    }
14503
14504
0
  in_attr = elf_known_obj_attributes_proc (ibfd);
14505
0
  out_attr = elf_known_obj_attributes_proc (obfd);
14506
  /* This needs to happen before Tag_ABI_FP_number_model is merged.  */
14507
0
  if (in_attr[Tag_ABI_VFP_args].i != out_attr[Tag_ABI_VFP_args].i)
14508
0
    {
14509
      /* Ignore mismatches if the object doesn't use floating point or is
14510
   floating point ABI independent.  */
14511
0
      if (out_attr[Tag_ABI_FP_number_model].i == AEABI_FP_number_model_none
14512
0
    || (in_attr[Tag_ABI_FP_number_model].i != AEABI_FP_number_model_none
14513
0
        && out_attr[Tag_ABI_VFP_args].i == AEABI_VFP_args_compatible))
14514
0
  out_attr[Tag_ABI_VFP_args].i = in_attr[Tag_ABI_VFP_args].i;
14515
0
      else if (in_attr[Tag_ABI_FP_number_model].i != AEABI_FP_number_model_none
14516
0
         && in_attr[Tag_ABI_VFP_args].i != AEABI_VFP_args_compatible)
14517
0
  {
14518
0
    _bfd_error_handler
14519
0
      (_("error: %pB uses VFP register arguments, %pB does not"),
14520
0
       in_attr[Tag_ABI_VFP_args].i ? ibfd : obfd,
14521
0
       in_attr[Tag_ABI_VFP_args].i ? obfd : ibfd);
14522
0
    result = false;
14523
0
  }
14524
0
    }
14525
14526
0
  for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
14527
0
    {
14528
      /* Merge this attribute with existing attributes.  */
14529
0
      switch (i)
14530
0
  {
14531
0
  case Tag_CPU_raw_name:
14532
0
  case Tag_CPU_name:
14533
    /* These are merged after Tag_CPU_arch.  */
14534
0
    break;
14535
14536
0
  case Tag_ABI_optimization_goals:
14537
0
  case Tag_ABI_FP_optimization_goals:
14538
    /* Use the first value seen.  */
14539
0
    break;
14540
14541
0
  case Tag_CPU_arch:
14542
0
    {
14543
0
      int secondary_compat = -1, secondary_compat_out = -1;
14544
0
      unsigned int saved_out_attr = out_attr[i].i;
14545
0
      int arch_attr;
14546
0
      static const char *const name_table[] =
14547
0
        {
14548
    /* These aren't real CPU names, but we can't guess
14549
       that from the architecture version alone.  */
14550
0
    "Pre v4",
14551
0
    "ARM v4",
14552
0
    "ARM v4T",
14553
0
    "ARM v5T",
14554
0
    "ARM v5TE",
14555
0
    "ARM v5TEJ",
14556
0
    "ARM v6",
14557
0
    "ARM v6KZ",
14558
0
    "ARM v6T2",
14559
0
    "ARM v6K",
14560
0
    "ARM v7",
14561
0
    "ARM v6-M",
14562
0
    "ARM v6S-M",
14563
0
    "ARM v7E-M",
14564
0
    "ARM v8",
14565
0
    "ARM v8-R",
14566
0
    "ARM v8-M.baseline",
14567
0
    "ARM v8-M.mainline",
14568
0
    "ARM v8.1-A",
14569
0
    "ARM v8.2-A",
14570
0
    "ARM v8.3-A",
14571
0
    "ARM v8.1-M.mainline",
14572
0
    "ARM v9",
14573
0
      };
14574
14575
      /* Merge Tag_CPU_arch and Tag_also_compatible_with.  */
14576
0
      secondary_compat = get_secondary_compatible_arch (ibfd);
14577
0
      secondary_compat_out = get_secondary_compatible_arch (obfd);
14578
0
      arch_attr = tag_cpu_arch_combine (ibfd, out_attr[i].i,
14579
0
                &secondary_compat_out,
14580
0
                in_attr[i].i,
14581
0
                secondary_compat,
14582
0
                name_table);
14583
14584
      /* Return with error if failed to merge.  */
14585
0
      if (arch_attr == -1)
14586
0
        return false;
14587
14588
0
      out_attr[i].i = arch_attr;
14589
14590
0
      set_secondary_compatible_arch (obfd, secondary_compat_out);
14591
14592
      /* Merge Tag_CPU_name and Tag_CPU_raw_name.  */
14593
0
      if (out_attr[i].i == saved_out_attr)
14594
0
        ; /* Leave the names alone.  */
14595
0
      else if (out_attr[i].i == in_attr[i].i)
14596
0
        {
14597
    /* The output architecture has been changed to match the
14598
       input architecture.  Use the input names.  */
14599
0
    out_attr[Tag_CPU_name].s = in_attr[Tag_CPU_name].s
14600
0
      ? _bfd_elf_attr_strdup (obfd, in_attr[Tag_CPU_name].s)
14601
0
      : NULL;
14602
0
    out_attr[Tag_CPU_raw_name].s = in_attr[Tag_CPU_raw_name].s
14603
0
      ? _bfd_elf_attr_strdup (obfd, in_attr[Tag_CPU_raw_name].s)
14604
0
      : NULL;
14605
0
        }
14606
0
      else
14607
0
        {
14608
0
    out_attr[Tag_CPU_name].s = NULL;
14609
0
    out_attr[Tag_CPU_raw_name].s = NULL;
14610
0
        }
14611
14612
      /* If we still don't have a value for Tag_CPU_name,
14613
         make one up now.  Tag_CPU_raw_name remains blank.  */
14614
0
      if (out_attr[Tag_CPU_name].s == NULL
14615
0
    && out_attr[i].i < ARRAY_SIZE (name_table))
14616
0
        out_attr[Tag_CPU_name].s =
14617
0
    _bfd_elf_attr_strdup (obfd, name_table[out_attr[i].i]);
14618
0
    }
14619
0
    break;
14620
14621
0
  case Tag_ARM_ISA_use:
14622
0
  case Tag_THUMB_ISA_use:
14623
0
  case Tag_WMMX_arch:
14624
0
  case Tag_Advanced_SIMD_arch:
14625
    /* ??? Do Advanced_SIMD (NEON) and WMMX conflict?  */
14626
0
  case Tag_ABI_FP_rounding:
14627
0
  case Tag_ABI_FP_exceptions:
14628
0
  case Tag_ABI_FP_user_exceptions:
14629
0
  case Tag_ABI_FP_number_model:
14630
0
  case Tag_FP_HP_extension:
14631
0
  case Tag_CPU_unaligned_access:
14632
0
  case Tag_T2EE_use:
14633
0
  case Tag_MPextension_use:
14634
0
  case Tag_MVE_arch:
14635
0
  case Tag_PAC_extension:
14636
0
  case Tag_BTI_extension:
14637
0
  case Tag_BTI_use:
14638
0
  case Tag_PACRET_use:
14639
    /* Use the largest value specified.  */
14640
0
    if (in_attr[i].i > out_attr[i].i)
14641
0
      out_attr[i].i = in_attr[i].i;
14642
0
    break;
14643
14644
0
  case Tag_ABI_align_preserved:
14645
0
  case Tag_ABI_PCS_RO_data:
14646
    /* Use the smallest value specified.  */
14647
0
    if (in_attr[i].i < out_attr[i].i)
14648
0
      out_attr[i].i = in_attr[i].i;
14649
0
    break;
14650
14651
0
  case Tag_ABI_align_needed:
14652
0
    if ((in_attr[i].i > 0 || out_attr[i].i > 0)
14653
0
        && (in_attr[Tag_ABI_align_preserved].i == 0
14654
0
      || out_attr[Tag_ABI_align_preserved].i == 0))
14655
0
      {
14656
        /* This error message should be enabled once all non-conformant
14657
     binaries in the toolchain have had the attributes set
14658
     properly.
14659
        _bfd_error_handler
14660
    (_("error: %pB: 8-byte data alignment conflicts with %pB"),
14661
     obfd, ibfd);
14662
        result = false; */
14663
0
      }
14664
    /* Fall through.  */
14665
0
  case Tag_ABI_FP_denormal:
14666
0
  case Tag_ABI_PCS_GOT_use:
14667
    /* Use the "greatest" from the sequence 0, 2, 1, or the largest
14668
       value if greater than 2 (for future-proofing).  */
14669
0
    if ((in_attr[i].i > 2 && in_attr[i].i > out_attr[i].i)
14670
0
        || (in_attr[i].i <= 2 && out_attr[i].i <= 2
14671
0
      && order_021[in_attr[i].i] > order_021[out_attr[i].i]))
14672
0
      out_attr[i].i = in_attr[i].i;
14673
0
    break;
14674
14675
0
  case Tag_Virtualization_use:
14676
    /* The virtualization tag effectively stores two bits of
14677
       information: the intended use of TrustZone (in bit 0), and the
14678
       intended use of Virtualization (in bit 1).  */
14679
0
    if (out_attr[i].i == 0)
14680
0
      out_attr[i].i = in_attr[i].i;
14681
0
    else if (in_attr[i].i != 0
14682
0
       && in_attr[i].i != out_attr[i].i)
14683
0
      {
14684
0
        if (in_attr[i].i <= 3 && out_attr[i].i <= 3)
14685
0
    out_attr[i].i = 3;
14686
0
        else
14687
0
    {
14688
0
      _bfd_error_handler
14689
0
        (_("error: %pB: unable to merge virtualization attributes "
14690
0
           "with %pB"),
14691
0
         obfd, ibfd);
14692
0
      result = false;
14693
0
    }
14694
0
      }
14695
0
    break;
14696
14697
0
  case Tag_CPU_arch_profile:
14698
0
    if (out_attr[i].i != in_attr[i].i)
14699
0
      {
14700
        /* 0 will merge with anything.
14701
     'A' and 'S' merge to 'A'.
14702
     'R' and 'S' merge to 'R'.
14703
     'M' and 'A|R|S' is an error.  */
14704
0
        if (out_attr[i].i == 0
14705
0
      || (out_attr[i].i == 'S'
14706
0
          && (in_attr[i].i == 'A' || in_attr[i].i == 'R')))
14707
0
    out_attr[i].i = in_attr[i].i;
14708
0
        else if (in_attr[i].i == 0
14709
0
           || (in_attr[i].i == 'S'
14710
0
         && (out_attr[i].i == 'A' || out_attr[i].i == 'R')))
14711
0
    ; /* Do nothing.  */
14712
0
        else
14713
0
    {
14714
0
      _bfd_error_handler
14715
0
        (_("error: %pB: conflicting architecture profiles %c/%c"),
14716
0
         ibfd,
14717
0
         in_attr[i].i ? in_attr[i].i : '0',
14718
0
         out_attr[i].i ? out_attr[i].i : '0');
14719
0
      result = false;
14720
0
    }
14721
0
      }
14722
0
    break;
14723
14724
0
  case Tag_DSP_extension:
14725
    /* No need to change output value if any of:
14726
       - pre (<=) ARMv5T input architecture (do not have DSP)
14727
       - M input profile not ARMv7E-M and do not have DSP.  */
14728
0
    if (in_attr[Tag_CPU_arch].i <= 3
14729
0
        || (in_attr[Tag_CPU_arch_profile].i == 'M'
14730
0
      && in_attr[Tag_CPU_arch].i != 13
14731
0
      && in_attr[i].i == 0))
14732
0
      ; /* Do nothing.  */
14733
    /* Output value should be 0 if DSP part of architecture, ie.
14734
       - post (>=) ARMv5te architecture output
14735
       - A, R or S profile output or ARMv7E-M output architecture.  */
14736
0
    else if (out_attr[Tag_CPU_arch].i >= 4
14737
0
       && (out_attr[Tag_CPU_arch_profile].i == 'A'
14738
0
           || out_attr[Tag_CPU_arch_profile].i == 'R'
14739
0
           || out_attr[Tag_CPU_arch_profile].i == 'S'
14740
0
           || out_attr[Tag_CPU_arch].i == 13))
14741
0
      out_attr[i].i = 0;
14742
    /* Otherwise, DSP instructions are added and not part of output
14743
       architecture.  */
14744
0
    else
14745
0
      out_attr[i].i = 1;
14746
0
    break;
14747
14748
0
  case Tag_FP_arch:
14749
0
      {
14750
        /* Tag_ABI_HardFP_use is handled along with Tag_FP_arch since
14751
     the meaning of Tag_ABI_HardFP_use depends on Tag_FP_arch
14752
     when it's 0.  It might mean absence of FP hardware if
14753
     Tag_FP_arch is zero.  */
14754
14755
0
#define VFP_VERSION_COUNT 9
14756
0
        static const struct
14757
0
        {
14758
0
      int ver;
14759
0
      int regs;
14760
0
        } vfp_versions[VFP_VERSION_COUNT] =
14761
0
    {
14762
0
      {0, 0},
14763
0
      {1, 16},
14764
0
      {2, 16},
14765
0
      {3, 32},
14766
0
      {3, 16},
14767
0
      {4, 32},
14768
0
      {4, 16},
14769
0
      {8, 32},
14770
0
      {8, 16}
14771
0
    };
14772
0
        int ver;
14773
0
        int regs;
14774
0
        int newval;
14775
14776
        /* If the output has no requirement about FP hardware,
14777
     follow the requirement of the input.  */
14778
0
        if (out_attr[i].i == 0)
14779
0
    {
14780
      /* This assert is still reasonable, we shouldn't
14781
         produce the suspicious build attribute
14782
         combination (See below for in_attr).  */
14783
0
      BFD_ASSERT (out_attr[Tag_ABI_HardFP_use].i == 0);
14784
0
      out_attr[i].i = in_attr[i].i;
14785
0
      out_attr[Tag_ABI_HardFP_use].i
14786
0
        = in_attr[Tag_ABI_HardFP_use].i;
14787
0
      break;
14788
0
    }
14789
        /* If the input has no requirement about FP hardware, do
14790
     nothing.  */
14791
0
        else if (in_attr[i].i == 0)
14792
0
    {
14793
      /* We used to assert that Tag_ABI_HardFP_use was
14794
         zero here, but we should never assert when
14795
         consuming an object file that has suspicious
14796
         build attributes.  The single precision variant
14797
         of 'no FP architecture' is still 'no FP
14798
         architecture', so we just ignore the tag in this
14799
         case.  */
14800
0
      break;
14801
0
    }
14802
14803
        /* Both the input and the output have nonzero Tag_FP_arch.
14804
     So Tag_ABI_HardFP_use is implied by Tag_FP_arch when it's zero.  */
14805
14806
        /* If both the input and the output have zero Tag_ABI_HardFP_use,
14807
     do nothing.  */
14808
0
        if (in_attr[Tag_ABI_HardFP_use].i == 0
14809
0
      && out_attr[Tag_ABI_HardFP_use].i == 0)
14810
0
    ;
14811
        /* If the input and the output have different Tag_ABI_HardFP_use,
14812
     the combination of them is 0 (implied by Tag_FP_arch).  */
14813
0
        else if (in_attr[Tag_ABI_HardFP_use].i
14814
0
           != out_attr[Tag_ABI_HardFP_use].i)
14815
0
    out_attr[Tag_ABI_HardFP_use].i = 0;
14816
14817
        /* Now we can handle Tag_FP_arch.  */
14818
14819
        /* Values of VFP_VERSION_COUNT or more aren't defined, so just
14820
     pick the biggest.  */
14821
0
        if (in_attr[i].i >= VFP_VERSION_COUNT
14822
0
      && in_attr[i].i > out_attr[i].i)
14823
0
    {
14824
0
      out_attr[i] = in_attr[i];
14825
0
      break;
14826
0
    }
14827
        /* The output uses the superset of input features
14828
     (ISA version) and registers.  */
14829
0
        ver = vfp_versions[in_attr[i].i].ver;
14830
0
        if (ver < vfp_versions[out_attr[i].i].ver)
14831
0
    ver = vfp_versions[out_attr[i].i].ver;
14832
0
        regs = vfp_versions[in_attr[i].i].regs;
14833
0
        if (regs < vfp_versions[out_attr[i].i].regs)
14834
0
    regs = vfp_versions[out_attr[i].i].regs;
14835
        /* This assumes all possible supersets are also a valid
14836
     options.  */
14837
0
        for (newval = VFP_VERSION_COUNT - 1; newval > 0; newval--)
14838
0
    {
14839
0
      if (regs == vfp_versions[newval].regs
14840
0
          && ver == vfp_versions[newval].ver)
14841
0
        break;
14842
0
    }
14843
0
        out_attr[i].i = newval;
14844
0
      }
14845
0
    break;
14846
0
  case Tag_PCS_config:
14847
0
    if (out_attr[i].i == 0)
14848
0
      out_attr[i].i = in_attr[i].i;
14849
0
    else if (in_attr[i].i != 0 && out_attr[i].i != in_attr[i].i)
14850
0
      {
14851
        /* It's sometimes ok to mix different configs, so this is only
14852
     a warning.  */
14853
0
        _bfd_error_handler
14854
0
    (_("warning: %pB: conflicting platform configuration"), ibfd);
14855
0
      }
14856
0
    break;
14857
0
  case Tag_ABI_PCS_R9_use:
14858
0
    if (in_attr[i].i != out_attr[i].i
14859
0
        && out_attr[i].i != AEABI_R9_unused
14860
0
        && in_attr[i].i != AEABI_R9_unused)
14861
0
      {
14862
0
        _bfd_error_handler
14863
0
    (_("error: %pB: conflicting use of R9"), ibfd);
14864
0
        result = false;
14865
0
      }
14866
0
    if (out_attr[i].i == AEABI_R9_unused)
14867
0
      out_attr[i].i = in_attr[i].i;
14868
0
    break;
14869
0
  case Tag_ABI_PCS_RW_data:
14870
0
    if (in_attr[i].i == AEABI_PCS_RW_data_SBrel
14871
0
        && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_SB
14872
0
        && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_unused)
14873
0
      {
14874
0
        _bfd_error_handler
14875
0
    (_("error: %pB: SB relative addressing conflicts with use of R9"),
14876
0
     ibfd);
14877
0
        result = false;
14878
0
      }
14879
    /* Use the smallest value specified.  */
14880
0
    if (in_attr[i].i < out_attr[i].i)
14881
0
      out_attr[i].i = in_attr[i].i;
14882
0
    break;
14883
0
  case Tag_ABI_PCS_wchar_t:
14884
0
    if (out_attr[i].i && in_attr[i].i && out_attr[i].i != in_attr[i].i
14885
0
        && !elf_arm_tdata (obfd)->no_wchar_size_warning)
14886
0
      {
14887
0
        _bfd_error_handler
14888
0
    (_("warning: %pB uses %u-byte wchar_t yet the output is to use %u-byte wchar_t; use of wchar_t values across objects may fail"),
14889
0
     ibfd, in_attr[i].i, out_attr[i].i);
14890
0
      }
14891
0
    else if (in_attr[i].i && !out_attr[i].i)
14892
0
      out_attr[i].i = in_attr[i].i;
14893
0
    break;
14894
0
  case Tag_ABI_enum_size:
14895
0
    if (in_attr[i].i != AEABI_enum_unused)
14896
0
      {
14897
0
        if (out_attr[i].i == AEABI_enum_unused
14898
0
      || out_attr[i].i == AEABI_enum_forced_wide)
14899
0
    {
14900
      /* The existing object is compatible with anything.
14901
         Use whatever requirements the new object has.  */
14902
0
      out_attr[i].i = in_attr[i].i;
14903
0
    }
14904
0
        else if (in_attr[i].i != AEABI_enum_forced_wide
14905
0
           && out_attr[i].i != in_attr[i].i
14906
0
           && !elf_arm_tdata (obfd)->no_enum_size_warning)
14907
0
    {
14908
0
      static const char *aeabi_enum_names[] =
14909
0
        { "", "variable-size", "32-bit", "" };
14910
0
      const char *in_name =
14911
0
        in_attr[i].i < ARRAY_SIZE (aeabi_enum_names)
14912
0
        ? aeabi_enum_names[in_attr[i].i]
14913
0
        : "<unknown>";
14914
0
      const char *out_name =
14915
0
        out_attr[i].i < ARRAY_SIZE (aeabi_enum_names)
14916
0
        ? aeabi_enum_names[out_attr[i].i]
14917
0
        : "<unknown>";
14918
0
      _bfd_error_handler
14919
0
        (_("warning: %pB uses %s enums yet the output is to use %s enums; use of enum values across objects may fail"),
14920
0
         ibfd, in_name, out_name);
14921
0
    }
14922
0
      }
14923
0
    break;
14924
0
  case Tag_ABI_VFP_args:
14925
    /* Aready done.  */
14926
0
    break;
14927
0
  case Tag_ABI_WMMX_args:
14928
0
    if (in_attr[i].i != out_attr[i].i)
14929
0
      {
14930
0
        _bfd_error_handler
14931
0
    (_("error: %pB uses iWMMXt register arguments, %pB does not"),
14932
0
     ibfd, obfd);
14933
0
        result = false;
14934
0
      }
14935
0
    break;
14936
0
  case Tag_compatibility:
14937
    /* Merged in target-independent code.  */
14938
0
    break;
14939
0
  case Tag_ABI_HardFP_use:
14940
    /* This is handled along with Tag_FP_arch.  */
14941
0
    break;
14942
0
  case Tag_ABI_FP_16bit_format:
14943
0
    if (in_attr[i].i != 0 && out_attr[i].i != 0)
14944
0
      {
14945
0
        if (in_attr[i].i != out_attr[i].i)
14946
0
    {
14947
0
      _bfd_error_handler
14948
0
        (_("error: fp16 format mismatch between %pB and %pB"),
14949
0
         ibfd, obfd);
14950
0
      result = false;
14951
0
    }
14952
0
      }
14953
0
    if (in_attr[i].i != 0)
14954
0
      out_attr[i].i = in_attr[i].i;
14955
0
    break;
14956
14957
0
  case Tag_DIV_use:
14958
    /* A value of zero on input means that the divide instruction may
14959
       be used if available in the base architecture as specified via
14960
       Tag_CPU_arch and Tag_CPU_arch_profile.  A value of 1 means that
14961
       the user did not want divide instructions.  A value of 2
14962
       explicitly means that divide instructions were allowed in ARM
14963
       and Thumb state.  */
14964
0
    if (in_attr[i].i == out_attr[i].i)
14965
0
      /* Do nothing.  */ ;
14966
0
    else if (elf32_arm_attributes_forbid_div (in_attr)
14967
0
       && !elf32_arm_attributes_accept_div (out_attr))
14968
0
      out_attr[i].i = 1;
14969
0
    else if (elf32_arm_attributes_forbid_div (out_attr)
14970
0
       && elf32_arm_attributes_accept_div (in_attr))
14971
0
      out_attr[i].i = in_attr[i].i;
14972
0
    else if (in_attr[i].i == 2)
14973
0
      out_attr[i].i = in_attr[i].i;
14974
0
    break;
14975
14976
0
  case Tag_MPextension_use_legacy:
14977
    /* We don't output objects with Tag_MPextension_use_legacy - we
14978
       move the value to Tag_MPextension_use.  */
14979
0
    if (in_attr[i].i != 0 && in_attr[Tag_MPextension_use].i != 0)
14980
0
      {
14981
0
        if (in_attr[Tag_MPextension_use].i != in_attr[i].i)
14982
0
    {
14983
0
      _bfd_error_handler
14984
0
        (_("%pB has both the current and legacy "
14985
0
           "Tag_MPextension_use attributes"),
14986
0
         ibfd);
14987
0
      result = false;
14988
0
    }
14989
0
      }
14990
14991
0
    if (in_attr[i].i > out_attr[Tag_MPextension_use].i)
14992
0
      out_attr[Tag_MPextension_use] = in_attr[i];
14993
14994
0
    break;
14995
14996
0
  case Tag_nodefaults:
14997
    /* This tag is set if it exists, but the value is unused (and is
14998
       typically zero).  We don't actually need to do anything here -
14999
       the merge happens automatically when the type flags are merged
15000
       below.  */
15001
0
    break;
15002
0
  case Tag_also_compatible_with:
15003
    /* Already done in Tag_CPU_arch.  */
15004
0
    break;
15005
0
  case Tag_conformance:
15006
    /* Keep the attribute if it matches.  Throw it away otherwise.
15007
       No attribute means no claim to conform.  */
15008
0
    if (!in_attr[i].s || !out_attr[i].s
15009
0
        || strcmp (in_attr[i].s, out_attr[i].s) != 0)
15010
0
      out_attr[i].s = NULL;
15011
0
    break;
15012
15013
0
  default:
15014
0
    result
15015
0
      = result && _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i);
15016
0
  }
15017
15018
      /* If out_attr was copied from in_attr then it won't have a type yet.  */
15019
0
      if (in_attr[i].type && !out_attr[i].type)
15020
0
  out_attr[i].type = in_attr[i].type;
15021
0
    }
15022
15023
  /* Merge Tag_compatibility attributes and any common GNU ones.  */
15024
0
  if (!_bfd_elf_merge_object_attributes (ibfd, info))
15025
0
    return false;
15026
15027
  /* Check for any attributes not known on ARM.  */
15028
0
  result &= _bfd_elf_merge_unknown_attribute_list (ibfd, obfd);
15029
15030
0
  return result;
15031
0
}
15032
15033
15034
/* Return TRUE if the two EABI versions are incompatible.  */
15035
15036
static bool
15037
elf32_arm_versions_compatible (unsigned iver, unsigned over)
15038
0
{
15039
  /* v4 and v5 are the same spec before and after it was released,
15040
     so allow mixing them.  */
15041
0
  if ((iver == EF_ARM_EABI_VER4 && over == EF_ARM_EABI_VER5)
15042
0
      || (iver == EF_ARM_EABI_VER5 && over == EF_ARM_EABI_VER4))
15043
0
    return true;
15044
15045
0
  return (iver == over);
15046
0
}
15047
15048
/* Merge backend specific data from an object file to the output
15049
   object file when linking.  */
15050
15051
static bool
15052
elf32_arm_merge_private_bfd_data (bfd *, struct bfd_link_info *);
15053
15054
/* Display the flags field.  */
15055
15056
static bool
15057
elf32_arm_print_private_bfd_data (bfd *abfd, void * ptr)
15058
344
{
15059
344
  FILE * file = (FILE *) ptr;
15060
344
  unsigned long flags;
15061
15062
344
  BFD_ASSERT (abfd != NULL && ptr != NULL);
15063
15064
  /* Print normal ELF private data.  */
15065
344
  _bfd_elf_print_private_bfd_data (abfd, ptr);
15066
15067
344
  flags = elf_elfheader (abfd)->e_flags;
15068
  /* Ignore init flag - it may not be set, despite the flags field
15069
     containing valid data.  */
15070
15071
344
  fprintf (file, _("private flags = 0x%lx:"), elf_elfheader (abfd)->e_flags);
15072
15073
344
  switch (EF_ARM_EABI_VERSION (flags))
15074
344
    {
15075
323
    case EF_ARM_EABI_UNKNOWN:
15076
      /* The following flag bits are GNU extensions and not part of the
15077
   official ARM ELF extended ABI.  Hence they are only decoded if
15078
   the EABI version is not set.  */
15079
323
      if (flags & EF_ARM_INTERWORK)
15080
7
  fprintf (file, _(" [interworking enabled]"));
15081
15082
323
      if (flags & EF_ARM_APCS_26)
15083
7
  fprintf (file, " [APCS-26]");
15084
316
      else
15085
316
  fprintf (file, " [APCS-32]");
15086
15087
323
      if (flags & EF_ARM_VFP_FLOAT)
15088
0
  fprintf (file, _(" [VFP float format]"));
15089
323
      else
15090
323
  fprintf (file, _(" [FPA float format]"));
15091
15092
323
      if (flags & EF_ARM_APCS_FLOAT)
15093
7
  fprintf (file, _(" [floats passed in float registers]"));
15094
15095
323
      if (flags & EF_ARM_PIC)
15096
7
  fprintf (file, _(" [position independent]"));
15097
15098
323
      if (flags & EF_ARM_NEW_ABI)
15099
7
  fprintf (file, _(" [new ABI]"));
15100
15101
323
      if (flags & EF_ARM_OLD_ABI)
15102
314
  fprintf (file, _(" [old ABI]"));
15103
15104
323
      if (flags & EF_ARM_SOFT_FLOAT)
15105
0
  fprintf (file, _(" [software FP]"));
15106
15107
323
      flags &= ~(EF_ARM_INTERWORK | EF_ARM_APCS_26 | EF_ARM_APCS_FLOAT
15108
323
     | EF_ARM_PIC | EF_ARM_NEW_ABI | EF_ARM_OLD_ABI
15109
323
     | EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT);
15110
323
      break;
15111
15112
0
    case EF_ARM_EABI_VER1:
15113
0
      fprintf (file, _(" [Version1 EABI]"));
15114
15115
0
      if (flags & EF_ARM_SYMSARESORTED)
15116
0
  fprintf (file, _(" [sorted symbol table]"));
15117
0
      else
15118
0
  fprintf (file, _(" [unsorted symbol table]"));
15119
15120
0
      flags &= ~ EF_ARM_SYMSARESORTED;
15121
0
      break;
15122
15123
0
    case EF_ARM_EABI_VER2:
15124
0
      fprintf (file, _(" [Version2 EABI]"));
15125
15126
0
      if (flags & EF_ARM_SYMSARESORTED)
15127
0
  fprintf (file, _(" [sorted symbol table]"));
15128
0
      else
15129
0
  fprintf (file, _(" [unsorted symbol table]"));
15130
15131
0
      if (flags & EF_ARM_DYNSYMSUSESEGIDX)
15132
0
  fprintf (file, _(" [dynamic symbols use segment index]"));
15133
15134
0
      if (flags & EF_ARM_MAPSYMSFIRST)
15135
0
  fprintf (file, _(" [mapping symbols precede others]"));
15136
15137
0
      flags &= ~(EF_ARM_SYMSARESORTED | EF_ARM_DYNSYMSUSESEGIDX
15138
0
     | EF_ARM_MAPSYMSFIRST);
15139
0
      break;
15140
15141
9
    case EF_ARM_EABI_VER3:
15142
9
      fprintf (file, _(" [Version3 EABI]"));
15143
9
      break;
15144
15145
0
    case EF_ARM_EABI_VER4:
15146
0
      fprintf (file, _(" [Version4 EABI]"));
15147
0
      goto eabi;
15148
15149
1
    case EF_ARM_EABI_VER5:
15150
1
      fprintf (file, _(" [Version5 EABI]"));
15151
15152
1
      if (flags & EF_ARM_ABI_FLOAT_SOFT)
15153
0
  fprintf (file, _(" [soft-float ABI]"));
15154
15155
1
      if (flags & EF_ARM_ABI_FLOAT_HARD)
15156
0
  fprintf (file, _(" [hard-float ABI]"));
15157
15158
1
      flags &= ~(EF_ARM_ABI_FLOAT_SOFT | EF_ARM_ABI_FLOAT_HARD);
15159
15160
1
    eabi:
15161
1
      if (flags & EF_ARM_BE8)
15162
0
  fprintf (file, _(" [BE8]"));
15163
15164
1
      if (flags & EF_ARM_LE8)
15165
0
  fprintf (file, _(" [LE8]"));
15166
15167
1
      flags &= ~(EF_ARM_LE8 | EF_ARM_BE8);
15168
1
      break;
15169
15170
11
    default:
15171
11
      fprintf (file, _(" <EABI version unrecognised>"));
15172
11
      break;
15173
344
    }
15174
15175
344
  flags &= ~ EF_ARM_EABIMASK;
15176
15177
344
  if (flags & EF_ARM_RELEXEC)
15178
9
    fprintf (file, _(" [relocatable executable]"));
15179
15180
344
  if (flags & EF_ARM_PIC)
15181
0
    fprintf (file, _(" [position independent]"));
15182
15183
344
  if (elf_elfheader (abfd)->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC)
15184
0
    fprintf (file, _(" [FDPIC ABI supplement]"));
15185
15186
344
  flags &= ~ (EF_ARM_RELEXEC | EF_ARM_PIC);
15187
15188
344
  if (flags)
15189
342
    fprintf (file, _(" <Unrecognised flag bits set>"));
15190
15191
344
  fputc ('\n', file);
15192
15193
344
  return true;
15194
344
}
15195
15196
static int
15197
elf32_arm_get_symbol_type (Elf_Internal_Sym * elf_sym, int type)
15198
0
{
15199
0
  switch (ELF_ST_TYPE (elf_sym->st_info))
15200
0
    {
15201
0
    case STT_ARM_TFUNC:
15202
0
      return ELF_ST_TYPE (elf_sym->st_info);
15203
15204
0
    case STT_ARM_16BIT:
15205
      /* If the symbol is not an object, return the STT_ARM_16BIT flag.
15206
   This allows us to distinguish between data used by Thumb instructions
15207
   and non-data (which is probably code) inside Thumb regions of an
15208
   executable.  */
15209
0
      if (type != STT_OBJECT && type != STT_TLS)
15210
0
  return ELF_ST_TYPE (elf_sym->st_info);
15211
0
      break;
15212
15213
0
    default:
15214
0
      break;
15215
0
    }
15216
15217
0
  return type;
15218
0
}
15219
15220
static asection *
15221
elf32_arm_gc_mark_hook (asection *sec,
15222
      struct bfd_link_info *info,
15223
      struct elf_reloc_cookie *cookie,
15224
      struct elf_link_hash_entry *h,
15225
      unsigned int symndx)
15226
0
{
15227
0
  if (h != NULL)
15228
0
    switch (ELF32_R_TYPE (cookie->rel->r_info))
15229
0
      {
15230
0
      case R_ARM_GNU_VTINHERIT:
15231
0
      case R_ARM_GNU_VTENTRY:
15232
0
  return NULL;
15233
0
      }
15234
15235
0
  return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
15236
0
}
15237
15238
/* Look through the relocs for a section during the first phase.  */
15239
15240
static bool
15241
elf32_arm_check_relocs (bfd *abfd, struct bfd_link_info *info,
15242
      asection *sec, const Elf_Internal_Rela *relocs)
15243
0
{
15244
0
  Elf_Internal_Shdr *symtab_hdr;
15245
0
  struct elf_link_hash_entry **sym_hashes;
15246
0
  const Elf_Internal_Rela *rel;
15247
0
  const Elf_Internal_Rela *rel_end;
15248
0
  bfd *dynobj;
15249
0
  asection *sreloc;
15250
0
  struct elf32_arm_link_hash_table *htab;
15251
0
  bool call_reloc_p;
15252
0
  bool may_become_dynamic_p;
15253
0
  bool may_need_local_target_p;
15254
0
  unsigned long nsyms;
15255
15256
0
  if (bfd_link_relocatable (info))
15257
0
    return true;
15258
15259
0
  BFD_ASSERT (is_arm_elf (abfd));
15260
15261
0
  htab = elf32_arm_hash_table (info);
15262
0
  if (htab == NULL)
15263
0
    return false;
15264
15265
0
  sreloc = NULL;
15266
15267
0
  if (htab->root.dynobj == NULL)
15268
0
    htab->root.dynobj = abfd;
15269
0
  if (!create_ifunc_sections (info))
15270
0
    return false;
15271
15272
0
  dynobj = htab->root.dynobj;
15273
15274
0
  symtab_hdr = & elf_symtab_hdr (abfd);
15275
0
  sym_hashes = elf_sym_hashes (abfd);
15276
0
  nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
15277
15278
0
  rel_end = relocs + sec->reloc_count;
15279
0
  for (rel = relocs; rel < rel_end; rel++)
15280
0
    {
15281
0
      Elf_Internal_Sym *isym;
15282
0
      struct elf_link_hash_entry *h;
15283
0
      struct elf32_arm_link_hash_entry *eh;
15284
0
      unsigned int r_symndx;
15285
0
      int r_type;
15286
15287
0
      r_symndx = ELF32_R_SYM (rel->r_info);
15288
0
      r_type = ELF32_R_TYPE (rel->r_info);
15289
0
      r_type = arm_real_reloc_type (htab, r_type);
15290
15291
0
      if (r_symndx >= nsyms
15292
    /* PR 9934: It is possible to have relocations that do not
15293
       refer to symbols, thus it is also possible to have an
15294
       object file containing relocations but no symbol table.  */
15295
0
    && (r_symndx > STN_UNDEF || nsyms > 0))
15296
0
  {
15297
0
    _bfd_error_handler (_("%pB: bad symbol index: %d"), abfd,
15298
0
            r_symndx);
15299
0
    return false;
15300
0
  }
15301
15302
0
      h = NULL;
15303
0
      isym = NULL;
15304
0
      if (nsyms > 0)
15305
0
  {
15306
0
    if (r_symndx < symtab_hdr->sh_info)
15307
0
      {
15308
        /* A local symbol.  */
15309
0
        isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
15310
0
              abfd, r_symndx);
15311
0
        if (isym == NULL)
15312
0
    return false;
15313
0
      }
15314
0
    else
15315
0
      {
15316
0
        h = sym_hashes[r_symndx - symtab_hdr->sh_info];
15317
0
        while (h->root.type == bfd_link_hash_indirect
15318
0
         || h->root.type == bfd_link_hash_warning)
15319
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
15320
0
      }
15321
0
  }
15322
15323
0
      eh = (struct elf32_arm_link_hash_entry *) h;
15324
15325
0
      call_reloc_p = false;
15326
0
      may_become_dynamic_p = false;
15327
0
      may_need_local_target_p = false;
15328
15329
      /* Could be done earlier, if h were already available.  */
15330
0
      r_type = elf32_arm_tls_transition (info, r_type, h);
15331
0
      switch (r_type)
15332
0
  {
15333
0
  case R_ARM_GOTOFFFUNCDESC:
15334
0
    {
15335
0
      if (h == NULL)
15336
0
        {
15337
0
    if (!elf32_arm_allocate_local_sym_info (abfd))
15338
0
      return false;
15339
0
    if (r_symndx >= elf32_arm_num_entries (abfd))
15340
0
      return false;
15341
0
    elf32_arm_local_fdpic_cnts (abfd) [r_symndx].gotofffuncdesc_cnt += 1;
15342
0
    elf32_arm_local_fdpic_cnts (abfd) [r_symndx].funcdesc_offset = -1;
15343
0
        }
15344
0
      else
15345
0
        {
15346
0
    eh->fdpic_cnts.gotofffuncdesc_cnt++;
15347
0
        }
15348
0
    }
15349
0
    break;
15350
15351
0
  case R_ARM_GOTFUNCDESC:
15352
0
    {
15353
0
      if (h == NULL)
15354
0
        {
15355
    /* Such a relocation is not supposed to be generated
15356
       by gcc on a static function.  */
15357
    /* Anyway if needed it could be handled.  */
15358
0
    return false;
15359
0
        }
15360
0
      else
15361
0
        {
15362
0
    eh->fdpic_cnts.gotfuncdesc_cnt++;
15363
0
        }
15364
0
    }
15365
0
    break;
15366
15367
0
  case R_ARM_FUNCDESC:
15368
0
    {
15369
0
      if (h == NULL)
15370
0
        {
15371
0
    if (!elf32_arm_allocate_local_sym_info (abfd))
15372
0
      return false;
15373
0
    if (r_symndx >= elf32_arm_num_entries (abfd))
15374
0
      return false;
15375
0
    elf32_arm_local_fdpic_cnts (abfd) [r_symndx].funcdesc_cnt += 1;
15376
0
    elf32_arm_local_fdpic_cnts (abfd) [r_symndx].funcdesc_offset = -1;
15377
0
        }
15378
0
      else
15379
0
        {
15380
0
    eh->fdpic_cnts.funcdesc_cnt++;
15381
0
        }
15382
0
    }
15383
0
    break;
15384
15385
0
    case R_ARM_GOT32:
15386
0
    case R_ARM_GOT_PREL:
15387
0
    case R_ARM_TLS_GD32:
15388
0
    case R_ARM_TLS_GD32_FDPIC:
15389
0
    case R_ARM_TLS_IE32:
15390
0
    case R_ARM_TLS_IE32_FDPIC:
15391
0
    case R_ARM_TLS_GOTDESC:
15392
0
    case R_ARM_TLS_DESCSEQ:
15393
0
    case R_ARM_THM_TLS_DESCSEQ:
15394
0
    case R_ARM_TLS_CALL:
15395
0
    case R_ARM_THM_TLS_CALL:
15396
      /* This symbol requires a global offset table entry.  */
15397
0
      {
15398
0
        int tls_type, old_tls_type;
15399
15400
0
        switch (r_type)
15401
0
    {
15402
0
    case R_ARM_TLS_GD32: tls_type = GOT_TLS_GD; break;
15403
0
    case R_ARM_TLS_GD32_FDPIC: tls_type = GOT_TLS_GD; break;
15404
15405
0
    case R_ARM_TLS_IE32: tls_type = GOT_TLS_IE; break;
15406
0
    case R_ARM_TLS_IE32_FDPIC: tls_type = GOT_TLS_IE; break;
15407
15408
0
    case R_ARM_TLS_GOTDESC:
15409
0
    case R_ARM_TLS_CALL: case R_ARM_THM_TLS_CALL:
15410
0
    case R_ARM_TLS_DESCSEQ: case R_ARM_THM_TLS_DESCSEQ:
15411
0
      tls_type = GOT_TLS_GDESC; break;
15412
15413
0
    default: tls_type = GOT_NORMAL; break;
15414
0
    }
15415
15416
0
        if (!bfd_link_executable (info) && (tls_type & GOT_TLS_IE))
15417
0
    info->flags |= DF_STATIC_TLS;
15418
15419
0
        if (h != NULL)
15420
0
    {
15421
0
      h->got.refcount++;
15422
0
      old_tls_type = elf32_arm_hash_entry (h)->tls_type;
15423
0
    }
15424
0
        else
15425
0
    {
15426
      /* This is a global offset table entry for a local symbol.  */
15427
0
      if (!elf32_arm_allocate_local_sym_info (abfd))
15428
0
        return false;
15429
0
      if (r_symndx >= elf32_arm_num_entries (abfd))
15430
0
        {
15431
0
          _bfd_error_handler (_("%pB: bad symbol index: %d"), abfd,
15432
0
            r_symndx);
15433
0
          return false;
15434
0
        }
15435
15436
0
      elf_local_got_refcounts (abfd)[r_symndx] += 1;
15437
0
      old_tls_type = elf32_arm_local_got_tls_type (abfd) [r_symndx];
15438
0
    }
15439
15440
        /* If a variable is accessed with both tls methods, two
15441
     slots may be created.  */
15442
0
        if (GOT_TLS_GD_ANY_P (old_tls_type)
15443
0
      && GOT_TLS_GD_ANY_P (tls_type))
15444
0
    tls_type |= old_tls_type;
15445
15446
        /* We will already have issued an error message if there
15447
     is a TLS/non-TLS mismatch, based on the symbol
15448
     type.  So just combine any TLS types needed.  */
15449
0
        if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
15450
0
      && tls_type != GOT_NORMAL)
15451
0
    tls_type |= old_tls_type;
15452
15453
        /* If the symbol is accessed in both IE and GDESC
15454
     method, we're able to relax. Turn off the GDESC flag,
15455
     without messing up with any other kind of tls types
15456
     that may be involved.  */
15457
0
        if ((tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_GDESC))
15458
0
    tls_type &= ~GOT_TLS_GDESC;
15459
15460
0
        if (old_tls_type != tls_type)
15461
0
    {
15462
0
      if (h != NULL)
15463
0
        elf32_arm_hash_entry (h)->tls_type = tls_type;
15464
0
      else
15465
0
        elf32_arm_local_got_tls_type (abfd) [r_symndx] = tls_type;
15466
0
    }
15467
0
      }
15468
      /* Fall through.  */
15469
15470
0
    case R_ARM_TLS_LDM32:
15471
0
    case R_ARM_TLS_LDM32_FDPIC:
15472
0
      if (r_type == R_ARM_TLS_LDM32 || r_type == R_ARM_TLS_LDM32_FDPIC)
15473
0
    htab->tls_ldm_got.refcount++;
15474
      /* Fall through.  */
15475
15476
0
    case R_ARM_GOTOFF32:
15477
0
    case R_ARM_GOTPC:
15478
0
      if (htab->root.sgot == NULL
15479
0
    && !create_got_section (htab->root.dynobj, info))
15480
0
        return false;
15481
0
      break;
15482
15483
0
    case R_ARM_PC24:
15484
0
    case R_ARM_PLT32:
15485
0
    case R_ARM_CALL:
15486
0
    case R_ARM_JUMP24:
15487
0
    case R_ARM_PREL31:
15488
0
    case R_ARM_THM_CALL:
15489
0
    case R_ARM_THM_JUMP24:
15490
0
    case R_ARM_THM_JUMP19:
15491
0
      call_reloc_p = true;
15492
0
      may_need_local_target_p = true;
15493
0
      break;
15494
15495
0
    case R_ARM_ABS12:
15496
      /* VxWorks uses dynamic R_ARM_ABS12 relocations for
15497
         ldr __GOTT_INDEX__ offsets.  */
15498
0
      if (htab->root.target_os != is_vxworks)
15499
0
        {
15500
0
    may_need_local_target_p = true;
15501
0
    break;
15502
0
        }
15503
0
      else goto jump_over;
15504
15505
      /* Fall through.  */
15506
15507
0
    case R_ARM_MOVW_ABS_NC:
15508
0
    case R_ARM_MOVT_ABS:
15509
0
    case R_ARM_THM_MOVW_ABS_NC:
15510
0
    case R_ARM_THM_MOVT_ABS:
15511
0
      if (bfd_link_pic (info))
15512
0
        {
15513
0
    _bfd_error_handler
15514
0
      (_("%pB: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
15515
0
       abfd, elf32_arm_howto_table_1[r_type].name,
15516
0
       (h) ? h->root.root.string : "a local symbol");
15517
0
    bfd_set_error (bfd_error_bad_value);
15518
0
    return false;
15519
0
        }
15520
15521
      /* Fall through.  */
15522
0
    case R_ARM_ABS32:
15523
0
    case R_ARM_ABS32_NOI:
15524
0
  jump_over:
15525
0
      if (h != NULL && bfd_link_executable (info))
15526
0
        {
15527
0
    h->pointer_equality_needed = 1;
15528
0
        }
15529
      /* Fall through.  */
15530
0
    case R_ARM_REL32:
15531
0
    case R_ARM_REL32_NOI:
15532
0
    case R_ARM_MOVW_PREL_NC:
15533
0
    case R_ARM_MOVT_PREL:
15534
0
    case R_ARM_THM_MOVW_PREL_NC:
15535
0
    case R_ARM_THM_MOVT_PREL:
15536
15537
      /* Should the interworking branches be listed here?  */
15538
0
      if ((bfd_link_pic (info)
15539
0
     || htab->fdpic_p)
15540
0
    && (sec->flags & SEC_ALLOC) != 0)
15541
0
        {
15542
0
    if (h == NULL
15543
0
        && elf32_arm_howto_from_type (r_type)->pc_relative)
15544
0
      {
15545
        /* In shared libraries and relocatable executables,
15546
           we treat local relative references as calls;
15547
           see the related SYMBOL_CALLS_LOCAL code in
15548
           allocate_dynrelocs.  */
15549
0
        call_reloc_p = true;
15550
0
        may_need_local_target_p = true;
15551
0
      }
15552
0
    else
15553
      /* We are creating a shared library or relocatable
15554
         executable, and this is a reloc against a global symbol,
15555
         or a non-PC-relative reloc against a local symbol.
15556
         We may need to copy the reloc into the output.  */
15557
0
      may_become_dynamic_p = true;
15558
0
        }
15559
0
      else
15560
0
        may_need_local_target_p = true;
15561
0
      break;
15562
15563
  /* This relocation describes the C++ object vtable hierarchy.
15564
     Reconstruct it for later use during GC.  */
15565
0
  case R_ARM_GNU_VTINHERIT:
15566
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
15567
0
      return false;
15568
0
    break;
15569
15570
  /* This relocation describes which C++ vtable entries are actually
15571
     used.  Record for later use during GC.  */
15572
0
  case R_ARM_GNU_VTENTRY:
15573
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
15574
0
      return false;
15575
0
    break;
15576
0
  }
15577
15578
0
      if (h != NULL)
15579
0
  {
15580
0
    if (call_reloc_p)
15581
      /* We may need a .plt entry if the function this reloc
15582
         refers to is in a different object, regardless of the
15583
         symbol's type.  We can't tell for sure yet, because
15584
         something later might force the symbol local.  */
15585
0
      h->needs_plt = 1;
15586
0
    else if (may_need_local_target_p)
15587
      /* If this reloc is in a read-only section, we might
15588
         need a copy reloc.  We can't check reliably at this
15589
         stage whether the section is read-only, as input
15590
         sections have not yet been mapped to output sections.
15591
         Tentatively set the flag for now, and correct in
15592
         adjust_dynamic_symbol.  */
15593
0
      h->non_got_ref = 1;
15594
0
  }
15595
15596
0
      if (may_need_local_target_p
15597
0
    && (h != NULL || ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC))
15598
0
  {
15599
0
    union gotplt_union *root_plt;
15600
0
    struct arm_plt_info *arm_plt;
15601
0
    struct arm_local_iplt_info *local_iplt;
15602
15603
0
    if (h != NULL)
15604
0
      {
15605
0
        root_plt = &h->plt;
15606
0
        arm_plt = &eh->plt;
15607
0
      }
15608
0
    else
15609
0
      {
15610
0
        local_iplt = elf32_arm_create_local_iplt (abfd, r_symndx);
15611
0
        if (local_iplt == NULL)
15612
0
    return false;
15613
0
        root_plt = &local_iplt->root;
15614
0
        arm_plt = &local_iplt->arm;
15615
0
      }
15616
15617
    /* If the symbol is a function that doesn't bind locally,
15618
       this relocation will need a PLT entry.  */
15619
0
    if (root_plt->refcount != -1)
15620
0
      root_plt->refcount += 1;
15621
15622
0
    if (!call_reloc_p)
15623
0
      arm_plt->noncall_refcount++;
15624
15625
    /* It's too early to use htab->use_blx here, so we have to
15626
       record possible blx references separately from
15627
       relocs that definitely need a thumb stub.  */
15628
15629
0
    if (r_type == R_ARM_THM_CALL)
15630
0
      arm_plt->maybe_thumb_refcount += 1;
15631
15632
0
    if (r_type == R_ARM_THM_JUMP24
15633
0
        || r_type == R_ARM_THM_JUMP19)
15634
0
      arm_plt->thumb_refcount += 1;
15635
0
  }
15636
15637
0
      if (may_become_dynamic_p)
15638
0
  {
15639
0
    struct elf_dyn_relocs *p, **head;
15640
15641
    /* Create a reloc section in dynobj.  */
15642
0
    if (sreloc == NULL)
15643
0
      {
15644
0
        sreloc = _bfd_elf_make_dynamic_reloc_section
15645
0
    (sec, dynobj, 2, abfd, ! htab->use_rel);
15646
15647
0
        if (sreloc == NULL)
15648
0
    return false;
15649
0
      }
15650
15651
    /* If this is a global symbol, count the number of
15652
       relocations we need for this symbol.  */
15653
0
    if (h != NULL)
15654
0
      head = &h->dyn_relocs;
15655
0
    else
15656
0
      {
15657
0
        head = elf32_arm_get_local_dynreloc_list (abfd, r_symndx, isym);
15658
0
        if (head == NULL)
15659
0
    return false;
15660
0
      }
15661
15662
0
    p = *head;
15663
0
    if (p == NULL || p->sec != sec)
15664
0
      {
15665
0
        size_t amt = sizeof *p;
15666
15667
0
        p = (struct elf_dyn_relocs *) bfd_alloc (htab->root.dynobj, amt);
15668
0
        if (p == NULL)
15669
0
    return false;
15670
0
        p->next = *head;
15671
0
        *head = p;
15672
0
        p->sec = sec;
15673
0
        p->count = 0;
15674
0
        p->pc_count = 0;
15675
0
      }
15676
15677
0
    if (elf32_arm_howto_from_type (r_type)->pc_relative)
15678
0
      p->pc_count += 1;
15679
0
    p->count += 1;
15680
0
    if (h == NULL && htab->fdpic_p && !bfd_link_pic (info)
15681
0
        && r_type != R_ARM_ABS32 && r_type != R_ARM_ABS32_NOI)
15682
0
      {
15683
        /* Here we only support R_ARM_ABS32 and R_ARM_ABS32_NOI
15684
     that will become rofixup.  */
15685
        /* This is due to the fact that we suppose all will become rofixup.  */
15686
0
        _bfd_error_handler
15687
0
    (_("FDPIC does not yet support %s relocation"
15688
0
       " to become dynamic for executable"),
15689
0
     elf32_arm_howto_table_1[r_type].name);
15690
0
        abort ();
15691
0
      }
15692
0
  }
15693
0
    }
15694
15695
0
  return true;
15696
0
}
15697
15698
static void
15699
elf32_arm_update_relocs (asection *o,
15700
       struct bfd_elf_section_reloc_data *reldata)
15701
0
{
15702
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
15703
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
15704
0
  elf_backend_data *bed;
15705
0
  _arm_elf_section_data *eado;
15706
0
  struct bfd_link_order *p;
15707
0
  bfd_byte *erela_head, *erela;
15708
0
  Elf_Internal_Rela *irela_head, *irela;
15709
0
  Elf_Internal_Shdr *rel_hdr;
15710
0
  bfd *abfd;
15711
0
  unsigned int count;
15712
15713
0
  eado = get_arm_elf_section_data (o);
15714
15715
0
  if (!eado || eado->elf.this_hdr.sh_type != SHT_ARM_EXIDX)
15716
0
    return;
15717
15718
0
  abfd = o->owner;
15719
0
  bed = get_elf_backend_data (abfd);
15720
0
  rel_hdr = reldata->hdr;
15721
15722
0
  if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
15723
0
    {
15724
0
      swap_in = bed->s->swap_reloc_in;
15725
0
      swap_out = bed->s->swap_reloc_out;
15726
0
    }
15727
0
  else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
15728
0
    {
15729
0
      swap_in = bed->s->swap_reloca_in;
15730
0
      swap_out = bed->s->swap_reloca_out;
15731
0
    }
15732
0
  else
15733
0
    abort ();
15734
15735
0
  erela_head = rel_hdr->contents;
15736
0
  irela_head = (Elf_Internal_Rela *) bfd_zmalloc
15737
0
    ((NUM_SHDR_ENTRIES (rel_hdr) + 1) * sizeof (*irela_head));
15738
15739
0
  erela = erela_head;
15740
0
  irela = irela_head;
15741
0
  count = 0;
15742
15743
0
  for (p = o->map_head.link_order; p; p = p->next)
15744
0
    {
15745
0
      if (p->type == bfd_section_reloc_link_order
15746
0
    || p->type == bfd_symbol_reloc_link_order)
15747
0
  {
15748
0
    (*swap_in) (abfd, erela, irela);
15749
0
    erela += rel_hdr->sh_entsize;
15750
0
    irela++;
15751
0
    count++;
15752
0
  }
15753
0
      else if (p->type == bfd_indirect_link_order)
15754
0
  {
15755
0
    struct bfd_elf_section_reloc_data *input_reldata;
15756
0
    arm_unwind_table_edit *edit_list, *edit_tail;
15757
0
    _arm_elf_section_data *eadi;
15758
0
    bfd_size_type j;
15759
0
    bfd_vma offset;
15760
0
    asection *i;
15761
15762
0
    i = p->u.indirect.section;
15763
15764
0
    eadi = get_arm_elf_section_data (i);
15765
0
    edit_list = eadi->u.exidx.unwind_edit_list;
15766
0
    edit_tail = eadi->u.exidx.unwind_edit_tail;
15767
0
    offset = i->output_offset;
15768
15769
0
    if (eadi->elf.rel.hdr &&
15770
0
        eadi->elf.rel.hdr->sh_entsize == rel_hdr->sh_entsize)
15771
0
      input_reldata = &eadi->elf.rel;
15772
0
    else if (eadi->elf.rela.hdr &&
15773
0
       eadi->elf.rela.hdr->sh_entsize == rel_hdr->sh_entsize)
15774
0
      input_reldata = &eadi->elf.rela;
15775
0
    else
15776
0
      abort ();
15777
15778
0
    if (edit_list)
15779
0
      {
15780
0
        for (j = 0; j < NUM_SHDR_ENTRIES (input_reldata->hdr); j++)
15781
0
    {
15782
0
      arm_unwind_table_edit *edit_node, *edit_next;
15783
0
      bfd_vma bias;
15784
0
      bfd_vma reloc_index;
15785
15786
0
      (*swap_in) (abfd, erela, irela);
15787
0
      reloc_index = (irela->r_offset - offset) / 8;
15788
15789
0
      bias = 0;
15790
0
      edit_node = edit_list;
15791
0
      for (edit_next = edit_list;
15792
0
           edit_next && edit_next->index <= reloc_index;
15793
0
           edit_next = edit_node->next)
15794
0
        {
15795
0
          bias++;
15796
0
          edit_node = edit_next;
15797
0
        }
15798
15799
0
      if (edit_node->type != DELETE_EXIDX_ENTRY
15800
0
          || edit_node->index != reloc_index)
15801
0
        {
15802
0
          irela->r_offset -= bias * 8;
15803
0
          irela++;
15804
0
          count++;
15805
0
        }
15806
15807
0
      erela += rel_hdr->sh_entsize;
15808
0
    }
15809
15810
0
        if (edit_tail->type == INSERT_EXIDX_CANTUNWIND_AT_END)
15811
0
    {
15812
      /* New relocation entity.  */
15813
0
      asection *text_sec = edit_tail->linked_section;
15814
0
      asection *text_out = text_sec->output_section;
15815
0
      bfd_vma exidx_offset = offset + i->size - 8;
15816
15817
0
      irela->r_addend = 0;
15818
0
      irela->r_offset = exidx_offset;
15819
0
      irela->r_info = ELF32_R_INFO
15820
0
        (text_out->target_index, R_ARM_PREL31);
15821
0
      irela++;
15822
0
      count++;
15823
0
    }
15824
0
      }
15825
0
    else
15826
0
      {
15827
0
        for (j = 0; j < NUM_SHDR_ENTRIES (input_reldata->hdr); j++)
15828
0
    {
15829
0
      (*swap_in) (abfd, erela, irela);
15830
0
      erela += rel_hdr->sh_entsize;
15831
0
      irela++;
15832
0
    }
15833
15834
0
        count += NUM_SHDR_ENTRIES (input_reldata->hdr);
15835
0
      }
15836
0
  }
15837
0
    }
15838
15839
0
  reldata->count = count;
15840
0
  rel_hdr->sh_size = count * rel_hdr->sh_entsize;
15841
15842
0
  erela = erela_head;
15843
0
  irela = irela_head;
15844
0
  while (count > 0)
15845
0
    {
15846
0
      (*swap_out) (abfd, irela, erela);
15847
0
      erela += rel_hdr->sh_entsize;
15848
0
      irela++;
15849
0
      count--;
15850
0
    }
15851
15852
0
  free (irela_head);
15853
15854
  /* Hashes are no longer valid.  */
15855
0
  free (reldata->hashes);
15856
0
  reldata->hashes = NULL;
15857
0
}
15858
15859
/* Unwinding tables are not referenced directly.  This pass marks them as
15860
   required if the corresponding code section is marked.  Similarly, ARMv8-M
15861
   secure entry functions can only be referenced by SG veneers which are
15862
   created after the GC process. They need to be marked in case they reside in
15863
   their own section (as would be the case if code was compiled with
15864
   -ffunction-sections).  */
15865
15866
static bool
15867
elf32_arm_gc_mark_extra_sections (struct bfd_link_info *info,
15868
          elf_gc_mark_hook_fn gc_mark_hook)
15869
0
{
15870
0
  bfd *sub;
15871
0
  Elf_Internal_Shdr **elf_shdrp;
15872
0
  asection *cmse_sec;
15873
0
  obj_attribute *out_attr;
15874
0
  Elf_Internal_Shdr *symtab_hdr;
15875
0
  unsigned i, sym_count, ext_start;
15876
0
  elf_backend_data *bed;
15877
0
  struct elf_link_hash_entry **sym_hashes;
15878
0
  struct elf32_arm_link_hash_entry *cmse_hash;
15879
0
  bool again, is_v8m, first_bfd_browse = true;
15880
0
  bool extra_marks_added = false;
15881
0
  asection *isec;
15882
15883
0
  _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
15884
15885
0
  out_attr = elf_known_obj_attributes_proc (info->output_bfd);
15886
0
  is_v8m = out_attr[Tag_CPU_arch].i >= TAG_CPU_ARCH_V8M_BASE
15887
0
     && out_attr[Tag_CPU_arch_profile].i == 'M';
15888
15889
  /* Marking EH data may cause additional code sections to be marked,
15890
     requiring multiple passes.  */
15891
0
  again = true;
15892
0
  while (again)
15893
0
    {
15894
0
      again = false;
15895
0
      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
15896
0
  {
15897
0
    asection *o;
15898
15899
0
    if (! is_arm_elf (sub))
15900
0
      continue;
15901
15902
0
    elf_shdrp = elf_elfsections (sub);
15903
0
    for (o = sub->sections; o != NULL; o = o->next)
15904
0
      {
15905
0
        Elf_Internal_Shdr *hdr;
15906
15907
0
        hdr = &elf_section_data (o)->this_hdr;
15908
0
        if (hdr->sh_type == SHT_ARM_EXIDX
15909
0
      && hdr->sh_link
15910
0
      && hdr->sh_link < elf_numsections (sub)
15911
0
      && !o->gc_mark
15912
0
      && elf_shdrp[hdr->sh_link]->bfd_section->gc_mark)
15913
0
    {
15914
0
      again = true;
15915
0
      if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
15916
0
        return false;
15917
0
    }
15918
0
      }
15919
15920
    /* Mark section holding ARMv8-M secure entry functions.  We mark all
15921
       of them so no need for a second browsing.  */
15922
0
    if (is_v8m && first_bfd_browse)
15923
0
      {
15924
0
        bool debug_sec_need_to_be_marked = false;
15925
15926
0
        sym_hashes = elf_sym_hashes (sub);
15927
0
        bed = get_elf_backend_data (sub);
15928
0
        symtab_hdr = &elf_tdata (sub)->symtab_hdr;
15929
0
        sym_count = symtab_hdr->sh_size / bed->s->sizeof_sym;
15930
0
        ext_start = symtab_hdr->sh_info;
15931
15932
        /* Scan symbols.  */
15933
0
        for (i = ext_start; i < sym_count; i++)
15934
0
    {
15935
0
      cmse_hash = elf32_arm_hash_entry (sym_hashes[i - ext_start]);
15936
0
      if (cmse_hash == NULL)
15937
0
        continue;
15938
15939
      /* Assume it is a special symbol.  If not, cmse_scan will
15940
         warn about it and user can do something about it.  */
15941
0
      if (startswith (cmse_hash->root.root.root.string,
15942
0
          CMSE_PREFIX))
15943
0
        {
15944
0
          cmse_sec = cmse_hash->root.root.u.def.section;
15945
0
          if (!cmse_sec->gc_mark
15946
0
        && !_bfd_elf_gc_mark (info, cmse_sec, gc_mark_hook))
15947
0
      return false;
15948
          /* The debug sections related to these secure entry
15949
       functions are marked on enabling below flag.  */
15950
0
          debug_sec_need_to_be_marked = true;
15951
0
        }
15952
0
    }
15953
15954
0
        if (debug_sec_need_to_be_marked)
15955
0
    {
15956
      /* Looping over all the sections of the object file containing
15957
         Armv8-M secure entry functions and marking all the debug
15958
         sections.  */
15959
0
      for (isec = sub->sections; isec != NULL; isec = isec->next)
15960
0
        {
15961
          /* If not a debug sections, skip it.  */
15962
0
          if (!isec->gc_mark && (isec->flags & SEC_DEBUGGING))
15963
0
      {
15964
0
        isec->gc_mark = 1;
15965
0
        extra_marks_added = true;
15966
0
      }
15967
0
        }
15968
0
      debug_sec_need_to_be_marked = false;
15969
0
    }
15970
0
      }
15971
0
  }
15972
15973
0
      first_bfd_browse = false;
15974
0
    }
15975
15976
  /* PR 30354: If we have added extra marks then make sure that any
15977
     dependencies of the newly marked sections are also marked.  */
15978
0
  if (extra_marks_added)
15979
0
    _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
15980
15981
0
  return true;
15982
0
}
15983
15984
/* Treat mapping symbols as special target symbols.  */
15985
15986
static bool
15987
elf32_arm_is_target_special_symbol (bfd * abfd ATTRIBUTE_UNUSED, asymbol * sym)
15988
0
{
15989
0
  return bfd_is_arm_special_symbol_name (sym->name,
15990
0
           BFD_ARM_SPECIAL_SYM_TYPE_ANY);
15991
0
}
15992
15993
/* If the ELF symbol SYM might be a function in SEC, return the
15994
   function size and set *CODE_OFF to the function's entry point,
15995
   otherwise return zero.  */
15996
15997
static bfd_size_type
15998
elf32_arm_maybe_function_sym (const asymbol *sym, asection *sec,
15999
            bfd_vma *code_off)
16000
0
{
16001
0
  bfd_size_type size;
16002
0
  elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
16003
16004
0
  if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
16005
0
         | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
16006
0
      || sym->section != sec)
16007
0
    return 0;
16008
16009
0
  size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
16010
16011
0
  if (!(sym->flags & BSF_SYNTHETIC))
16012
0
    switch (ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info))
16013
0
      {
16014
0
  case STT_NOTYPE:
16015
    /* Ignore symbols created by the annobin plugin for gcc and clang.
16016
       These symbols are hidden, local, notype and have a size of 0.  */
16017
0
    if (size == 0
16018
0
        && sym->flags & BSF_LOCAL
16019
0
        && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
16020
0
      return 0;
16021
    /* Fall through.  */
16022
0
  case STT_FUNC:
16023
0
  case STT_ARM_TFUNC:
16024
    /* FIXME: Allow STT_GNU_IFUNC as well ?  */
16025
0
    break;
16026
0
  default:
16027
0
    return 0;
16028
0
      }
16029
16030
0
  if ((sym->flags & BSF_LOCAL)
16031
0
      && bfd_is_arm_special_symbol_name (sym->name,
16032
0
           BFD_ARM_SPECIAL_SYM_TYPE_ANY))
16033
0
    return 0;
16034
16035
0
  *code_off = sym->value;
16036
16037
  /* Do not return 0 for the function's size.  */
16038
0
  return size ? size : 1;
16039
16040
0
}
16041
16042
static bool
16043
elf32_arm_find_inliner_info (bfd *      abfd,
16044
           const char **  filename_ptr,
16045
           const char **  functionname_ptr,
16046
           unsigned int * line_ptr)
16047
0
{
16048
0
  bool found;
16049
0
  found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
16050
0
           functionname_ptr, line_ptr,
16051
0
           & elf_tdata (abfd)->dwarf2_find_line_info);
16052
0
  return found;
16053
0
}
16054
16055
/* Adjust a symbol defined by a dynamic object and referenced by a
16056
   regular object.  The current definition is in some section of the
16057
   dynamic object, but we're not including those sections.  We have to
16058
   change the definition to something the rest of the link can
16059
   understand.  */
16060
16061
static bool
16062
elf32_arm_adjust_dynamic_symbol (struct bfd_link_info * info,
16063
         struct elf_link_hash_entry * h)
16064
0
{
16065
0
  bfd * dynobj;
16066
0
  asection *s, *srel;
16067
0
  struct elf32_arm_link_hash_entry * eh;
16068
0
  struct elf32_arm_link_hash_table *globals;
16069
16070
0
  globals = elf32_arm_hash_table (info);
16071
0
  if (globals == NULL)
16072
0
    return false;
16073
16074
0
  dynobj = elf_hash_table (info)->dynobj;
16075
16076
  /* Make sure we know what is going on here.  */
16077
0
  BFD_ASSERT (dynobj != NULL
16078
0
        && (h->needs_plt
16079
0
      || h->type == STT_GNU_IFUNC
16080
0
      || h->is_weakalias
16081
0
      || (h->def_dynamic
16082
0
          && h->ref_regular
16083
0
          && !h->def_regular)));
16084
16085
0
  eh = (struct elf32_arm_link_hash_entry *) h;
16086
16087
  /* If this is a function, put it in the procedure linkage table.  We
16088
     will fill in the contents of the procedure linkage table later,
16089
     when we know the address of the .got section.  */
16090
0
  if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
16091
0
    {
16092
      /* Calls to STT_GNU_IFUNC symbols always use a PLT, even if the
16093
   symbol binds locally.  */
16094
0
      if (h->plt.refcount <= 0
16095
0
    || (h->type != STT_GNU_IFUNC
16096
0
        && (SYMBOL_CALLS_LOCAL (info, h)
16097
0
      || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
16098
0
          && h->root.type == bfd_link_hash_undefweak))))
16099
0
  {
16100
    /* This case can occur if we saw a PLT32 reloc in an input
16101
       file, but the symbol was never referred to by a dynamic
16102
       object, or if all references were garbage collected.  In
16103
       such a case, we don't actually need to build a procedure
16104
       linkage table, and we can just do a PC24 reloc instead.  */
16105
0
    h->plt.offset = (bfd_vma) -1;
16106
0
    eh->plt.thumb_refcount = 0;
16107
0
    eh->plt.maybe_thumb_refcount = 0;
16108
0
    eh->plt.noncall_refcount = 0;
16109
0
    h->needs_plt = 0;
16110
0
  }
16111
16112
0
      return true;
16113
0
    }
16114
0
  else
16115
0
    {
16116
      /* It's possible that we incorrectly decided a .plt reloc was
16117
   needed for an R_ARM_PC24 or similar reloc to a non-function sym
16118
   in check_relocs.  We can't decide accurately between function
16119
   and non-function syms in check-relocs; Objects loaded later in
16120
   the link may change h->type.  So fix it now.  */
16121
0
      h->plt.offset = (bfd_vma) -1;
16122
0
      eh->plt.thumb_refcount = 0;
16123
0
      eh->plt.maybe_thumb_refcount = 0;
16124
0
      eh->plt.noncall_refcount = 0;
16125
0
    }
16126
16127
  /* If this is a weak symbol, and there is a real definition, the
16128
     processor independent code will have arranged for us to see the
16129
     real definition first, and we can just use the same value.  */
16130
0
  if (h->is_weakalias)
16131
0
    {
16132
0
      struct elf_link_hash_entry *def = weakdef (h);
16133
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
16134
0
      h->root.u.def.section = def->root.u.def.section;
16135
0
      h->root.u.def.value = def->root.u.def.value;
16136
0
      return true;
16137
0
    }
16138
16139
  /* If there are no non-GOT references, we do not need a copy
16140
     relocation.  */
16141
0
  if (!h->non_got_ref)
16142
0
    return true;
16143
16144
  /* This is a reference to a symbol defined by a dynamic object which
16145
     is not a function.  */
16146
16147
  /* If we are creating a shared library, we must presume that the
16148
     only references to the symbol are via the global offset table.
16149
     For such cases we need not do anything here; the relocations will
16150
     be handled correctly by relocate_section.  */
16151
0
  if (bfd_link_pic (info))
16152
0
    return true;
16153
16154
  /* We must allocate the symbol in our .dynbss section, which will
16155
     become part of the .bss section of the executable.  There will be
16156
     an entry for this symbol in the .dynsym section.  The dynamic
16157
     object will contain position independent code, so all references
16158
     from the dynamic object to this symbol will go through the global
16159
     offset table.  The dynamic linker will use the .dynsym entry to
16160
     determine the address it must put in the global offset table, so
16161
     both the dynamic object and the regular object will refer to the
16162
     same memory location for the variable.  */
16163
  /* If allowed, we must generate a R_ARM_COPY reloc to tell the dynamic
16164
     linker to copy the initial value out of the dynamic object and into
16165
     the runtime process image.  We need to remember the offset into the
16166
     .rel(a).bss section we are going to use.  */
16167
0
  if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
16168
0
    {
16169
0
      s = globals->root.sdynrelro;
16170
0
      srel = globals->root.sreldynrelro;
16171
0
    }
16172
0
  else
16173
0
    {
16174
0
      s = globals->root.sdynbss;
16175
0
      srel = globals->root.srelbss;
16176
0
    }
16177
0
  if (info->nocopyreloc == 0
16178
0
      && (h->root.u.def.section->flags & SEC_ALLOC) != 0
16179
0
      && h->size != 0)
16180
0
    {
16181
0
      elf32_arm_allocate_dynrelocs (info, srel, 1);
16182
0
      h->needs_copy = 1;
16183
0
    }
16184
16185
0
  return _bfd_elf_adjust_dynamic_copy (info, h, s);
16186
0
}
16187
16188
/* Allocate space in .plt, .got and associated reloc sections for
16189
   dynamic relocs.  */
16190
16191
static bool
16192
allocate_dynrelocs_for_symbol (struct elf_link_hash_entry *h, void * inf)
16193
0
{
16194
0
  struct bfd_link_info *info;
16195
0
  struct elf32_arm_link_hash_table *htab;
16196
0
  struct elf32_arm_link_hash_entry *eh;
16197
0
  struct elf_dyn_relocs *p;
16198
16199
0
  if (h->root.type == bfd_link_hash_indirect)
16200
0
    return true;
16201
16202
0
  eh = (struct elf32_arm_link_hash_entry *) h;
16203
16204
0
  info = (struct bfd_link_info *) inf;
16205
0
  htab = elf32_arm_hash_table (info);
16206
0
  if (htab == NULL)
16207
0
    return false;
16208
16209
0
  if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
16210
0
      && h->plt.refcount > 0)
16211
0
    {
16212
      /* Make sure this symbol is output as a dynamic symbol.
16213
   Undefined weak syms won't yet be marked as dynamic.  */
16214
0
      if (h->dynindx == -1 && !h->forced_local
16215
0
    && h->root.type == bfd_link_hash_undefweak)
16216
0
  {
16217
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
16218
0
      return false;
16219
0
  }
16220
16221
      /* If the call in the PLT entry binds locally, the associated
16222
   GOT entry should use an R_ARM_IRELATIVE relocation instead of
16223
   the usual R_ARM_JUMP_SLOT.  Put it in the .iplt section rather
16224
   than the .plt section.  */
16225
0
      if (h->type == STT_GNU_IFUNC && SYMBOL_CALLS_LOCAL (info, h))
16226
0
  {
16227
0
    eh->is_iplt = 1;
16228
0
    if (eh->plt.noncall_refcount == 0
16229
0
        && SYMBOL_REFERENCES_LOCAL (info, h))
16230
      /* All non-call references can be resolved directly.
16231
         This means that they can (and in some cases, must)
16232
         resolve directly to the run-time target, rather than
16233
         to the PLT.  That in turns means that any .got entry
16234
         would be equal to the .igot.plt entry, so there's
16235
         no point having both.  */
16236
0
      h->got.refcount = 0;
16237
0
  }
16238
16239
0
      if (bfd_link_pic (info)
16240
0
    || eh->is_iplt
16241
0
    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
16242
0
  {
16243
0
    elf32_arm_allocate_plt_entry (info, eh->is_iplt, &h->plt, &eh->plt);
16244
16245
    /* If this symbol is not defined in a regular file, and we are
16246
       not generating a shared library, then set the symbol to this
16247
       location in the .plt.  This is required to make function
16248
       pointers compare as equal between the normal executable and
16249
       the shared library.  */
16250
0
    if (! bfd_link_pic (info)
16251
0
        && !h->def_regular)
16252
0
      {
16253
0
        h->root.u.def.section = htab->root.splt;
16254
0
        h->root.u.def.value = h->plt.offset;
16255
16256
        /* Make sure the function is not marked as Thumb, in case
16257
     it is the target of an ABS32 relocation, which will
16258
     point to the PLT entry.  */
16259
0
        ARM_SET_SYM_BRANCH_TYPE (h->target_internal, ST_BRANCH_TO_ARM);
16260
0
      }
16261
16262
    /* VxWorks executables have a second set of relocations for
16263
       each PLT entry.  They go in a separate relocation section,
16264
       which is processed by the kernel loader.  */
16265
0
    if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
16266
0
      {
16267
        /* There is a relocation for the initial PLT entry:
16268
     an R_ARM_32 relocation for _GLOBAL_OFFSET_TABLE_.  */
16269
0
        if (h->plt.offset == htab->plt_header_size)
16270
0
    elf32_arm_allocate_dynrelocs (info, htab->srelplt2, 1);
16271
16272
        /* There are two extra relocations for each subsequent
16273
     PLT entry: an R_ARM_32 relocation for the GOT entry,
16274
     and an R_ARM_32 relocation for the PLT entry.  */
16275
0
        elf32_arm_allocate_dynrelocs (info, htab->srelplt2, 2);
16276
0
      }
16277
0
  }
16278
0
      else
16279
0
  {
16280
0
    h->plt.offset = (bfd_vma) -1;
16281
0
    h->needs_plt = 0;
16282
0
  }
16283
0
    }
16284
0
  else
16285
0
    {
16286
0
      h->plt.offset = (bfd_vma) -1;
16287
0
      h->needs_plt = 0;
16288
0
    }
16289
16290
0
  eh = (struct elf32_arm_link_hash_entry *) h;
16291
0
  eh->tlsdesc_got = (bfd_vma) -1;
16292
16293
0
  if (h->got.refcount > 0)
16294
0
    {
16295
0
      asection *s;
16296
0
      bool dyn;
16297
0
      int tls_type = elf32_arm_hash_entry (h)->tls_type;
16298
0
      int indx;
16299
16300
      /* Make sure this symbol is output as a dynamic symbol.
16301
   Undefined weak syms won't yet be marked as dynamic.  */
16302
0
      if (htab->root.dynamic_sections_created
16303
0
    && h->dynindx == -1
16304
0
    && !h->forced_local
16305
0
    && h->root.type == bfd_link_hash_undefweak)
16306
0
  {
16307
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
16308
0
      return false;
16309
0
  }
16310
16311
0
      s = htab->root.sgot;
16312
0
      h->got.offset = s->size;
16313
16314
0
      if (tls_type == GOT_UNKNOWN)
16315
0
  abort ();
16316
16317
0
      if (tls_type == GOT_NORMAL)
16318
  /* Non-TLS symbols need one GOT slot.  */
16319
0
  s->size += 4;
16320
0
      else
16321
0
  {
16322
0
    if (tls_type & GOT_TLS_GDESC)
16323
0
      {
16324
        /* R_ARM_TLS_DESC needs 2 GOT slots.  */
16325
0
        eh->tlsdesc_got
16326
0
    = (htab->root.sgotplt->size
16327
0
       - elf32_arm_compute_jump_table_size (htab));
16328
0
        htab->root.sgotplt->size += 8;
16329
0
        h->got.offset = (bfd_vma) -2;
16330
        /* plt.got_offset needs to know there's a TLS_DESC
16331
     reloc in the middle of .got.plt.  */
16332
0
        htab->num_tls_desc++;
16333
0
      }
16334
16335
0
    if (tls_type & GOT_TLS_GD)
16336
0
      {
16337
        /* R_ARM_TLS_GD32 and R_ARM_TLS_GD32_FDPIC need two
16338
     consecutive GOT slots.  If the symbol is both GD
16339
     and GDESC, got.offset may have been
16340
     overwritten.  */
16341
0
        h->got.offset = s->size;
16342
0
        s->size += 8;
16343
0
      }
16344
16345
0
    if (tls_type & GOT_TLS_IE)
16346
      /* R_ARM_TLS_IE32/R_ARM_TLS_IE32_FDPIC need one GOT
16347
         slot.  */
16348
0
      s->size += 4;
16349
0
  }
16350
16351
0
      dyn = htab->root.dynamic_sections_created;
16352
16353
0
      indx = 0;
16354
0
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
16355
0
    && (!bfd_link_pic (info)
16356
0
        || !SYMBOL_REFERENCES_LOCAL (info, h)))
16357
0
  indx = h->dynindx;
16358
16359
0
      if (tls_type != GOT_NORMAL
16360
0
    && (bfd_link_dll (info) || indx != 0)
16361
0
    && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
16362
0
        || h->root.type != bfd_link_hash_undefweak))
16363
0
  {
16364
0
    if (tls_type & GOT_TLS_IE)
16365
0
      elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16366
16367
0
    if (tls_type & GOT_TLS_GD)
16368
0
      elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16369
16370
0
    if (tls_type & GOT_TLS_GDESC)
16371
0
      {
16372
0
        elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
16373
        /* GDESC needs a trampoline to jump to.  */
16374
0
        htab->tls_trampoline = -1;
16375
0
      }
16376
16377
    /* Only GD needs it.  GDESC just emits one relocation per
16378
       2 entries.  */
16379
0
    if ((tls_type & GOT_TLS_GD) && indx != 0)
16380
0
      elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16381
0
  }
16382
0
      else if (((indx != -1) || htab->fdpic_p)
16383
0
         && !SYMBOL_REFERENCES_LOCAL (info, h))
16384
0
  {
16385
0
    if (htab->root.dynamic_sections_created)
16386
      /* Reserve room for the GOT entry's R_ARM_GLOB_DAT relocation.  */
16387
0
      elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16388
0
  }
16389
0
      else if (h->type == STT_GNU_IFUNC
16390
0
         && eh->plt.noncall_refcount == 0)
16391
  /* No non-call references resolve the STT_GNU_IFUNC's PLT entry;
16392
     they all resolve dynamically instead.  Reserve room for the
16393
     GOT entry's R_ARM_IRELATIVE relocation.  */
16394
0
  elf32_arm_allocate_irelocs (info, htab->root.srelgot, 1);
16395
0
      else if (bfd_link_pic (info)
16396
0
         && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
16397
  /* Reserve room for the GOT entry's R_ARM_RELATIVE relocation.  */
16398
0
  elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16399
0
      else if (htab->fdpic_p && tls_type == GOT_NORMAL)
16400
  /* Reserve room for rofixup for FDPIC executable.  */
16401
  /* TLS relocs do not need space since they are completely
16402
     resolved.  */
16403
0
  htab->srofixup->size += 4;
16404
0
    }
16405
0
  else
16406
0
    h->got.offset = (bfd_vma) -1;
16407
16408
  /* FDPIC support.  */
16409
0
  if (eh->fdpic_cnts.gotofffuncdesc_cnt > 0)
16410
0
    {
16411
      /* Symbol musn't be exported.  */
16412
0
      if (h->dynindx != -1)
16413
0
  abort ();
16414
16415
      /* We only allocate one function descriptor with its associated
16416
   relocation.  */
16417
0
      if (eh->fdpic_cnts.funcdesc_offset == -1)
16418
0
  {
16419
0
    asection *s = htab->root.sgot;
16420
16421
0
    eh->fdpic_cnts.funcdesc_offset = s->size;
16422
0
    s->size += 8;
16423
    /* We will add an R_ARM_FUNCDESC_VALUE relocation or two rofixups.  */
16424
0
    if (bfd_link_pic (info))
16425
0
      elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16426
0
    else
16427
0
      htab->srofixup->size += 8;
16428
0
  }
16429
0
    }
16430
16431
0
  if (eh->fdpic_cnts.gotfuncdesc_cnt > 0)
16432
0
    {
16433
0
      asection *s = htab->root.sgot;
16434
16435
0
      if (htab->root.dynamic_sections_created && h->dynindx == -1
16436
0
    && !h->forced_local)
16437
0
  if (! bfd_elf_link_record_dynamic_symbol (info, h))
16438
0
    return false;
16439
16440
0
      if (h->dynindx == -1)
16441
0
  {
16442
    /* We only allocate one function descriptor with its
16443
       associated relocation.  */
16444
0
    if (eh->fdpic_cnts.funcdesc_offset == -1)
16445
0
      {
16446
16447
0
        eh->fdpic_cnts.funcdesc_offset = s->size;
16448
0
        s->size += 8;
16449
        /* We will add an R_ARM_FUNCDESC_VALUE relocation or two
16450
     rofixups.  */
16451
0
        if (bfd_link_pic (info))
16452
0
    elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16453
0
        else
16454
0
    htab->srofixup->size += 8;
16455
0
      }
16456
0
  }
16457
16458
      /* Add one entry into the GOT and a R_ARM_FUNCDESC or
16459
   R_ARM_RELATIVE/rofixup relocation on it.  */
16460
0
      eh->fdpic_cnts.gotfuncdesc_offset = s->size;
16461
0
      s->size += 4;
16462
0
      if (h->dynindx == -1 && !bfd_link_pic (info))
16463
0
  htab->srofixup->size += 4;
16464
0
      else
16465
0
  elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16466
0
    }
16467
16468
0
  if (eh->fdpic_cnts.funcdesc_cnt > 0)
16469
0
    {
16470
0
      if (htab->root.dynamic_sections_created && h->dynindx == -1
16471
0
    && !h->forced_local)
16472
0
  if (! bfd_elf_link_record_dynamic_symbol (info, h))
16473
0
    return false;
16474
16475
0
      if (h->dynindx == -1)
16476
0
  {
16477
    /* We only allocate one function descriptor with its
16478
       associated relocation.  */
16479
0
    if (eh->fdpic_cnts.funcdesc_offset == -1)
16480
0
      {
16481
0
        asection *s = htab->root.sgot;
16482
16483
0
        eh->fdpic_cnts.funcdesc_offset = s->size;
16484
0
        s->size += 8;
16485
        /* We will add an R_ARM_FUNCDESC_VALUE relocation or two
16486
     rofixups.  */
16487
0
        if (bfd_link_pic (info))
16488
0
    elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16489
0
        else
16490
0
    htab->srofixup->size += 8;
16491
0
      }
16492
0
  }
16493
0
      if (h->dynindx == -1 && !bfd_link_pic (info))
16494
0
  {
16495
    /* For FDPIC executable we replace R_ARM_RELATIVE with a rofixup.  */
16496
0
    htab->srofixup->size += 4 * eh->fdpic_cnts.funcdesc_cnt;
16497
0
  }
16498
0
      else
16499
0
  {
16500
    /* Will need one dynamic reloc per reference. will be either
16501
       R_ARM_FUNCDESC or R_ARM_RELATIVE for hidden symbols.  */
16502
0
    elf32_arm_allocate_dynrelocs (info, htab->root.srelgot,
16503
0
          eh->fdpic_cnts.funcdesc_cnt);
16504
0
  }
16505
0
    }
16506
16507
  /* Allocate stubs for exported Thumb functions on v4t.  */
16508
0
  if (!htab->use_blx && h->dynindx != -1
16509
0
      && h->def_regular
16510
0
      && ARM_GET_SYM_BRANCH_TYPE (h->target_internal) == ST_BRANCH_TO_THUMB
16511
0
      && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
16512
0
    {
16513
0
      struct elf_link_hash_entry * th;
16514
0
      struct bfd_link_hash_entry * bh;
16515
0
      struct elf_link_hash_entry * myh;
16516
0
      char name[1024];
16517
0
      asection *s;
16518
0
      bh = NULL;
16519
      /* Create a new symbol to regist the real location of the function.  */
16520
0
      s = h->root.u.def.section;
16521
0
      sprintf (name, "__real_%s", h->root.root.string);
16522
0
      _bfd_generic_link_add_one_symbol (info, s->owner,
16523
0
          name, BSF_GLOBAL, s,
16524
0
          h->root.u.def.value,
16525
0
          NULL, true, false, &bh);
16526
16527
0
      myh = (struct elf_link_hash_entry *) bh;
16528
0
      myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
16529
0
      myh->forced_local = 1;
16530
0
      ARM_SET_SYM_BRANCH_TYPE (myh->target_internal, ST_BRANCH_TO_THUMB);
16531
0
      eh->export_glue = myh;
16532
0
      th = record_arm_to_thumb_glue (info, h);
16533
      /* Point the symbol at the stub.  */
16534
0
      h->type = ELF_ST_INFO (ELF_ST_BIND (h->type), STT_FUNC);
16535
0
      ARM_SET_SYM_BRANCH_TYPE (h->target_internal, ST_BRANCH_TO_ARM);
16536
0
      h->root.u.def.section = th->root.u.def.section;
16537
0
      h->root.u.def.value = th->root.u.def.value & ~1;
16538
0
    }
16539
16540
0
  if (h->dyn_relocs == NULL)
16541
0
    return true;
16542
16543
  /* In the shared -Bsymbolic case, discard space allocated for
16544
     dynamic pc-relative relocs against symbols which turn out to be
16545
     defined in regular objects.  For the normal shared case, discard
16546
     space for pc-relative relocs that have become local due to symbol
16547
     visibility changes.  */
16548
16549
0
  if (bfd_link_pic (info)
16550
0
      || htab->fdpic_p)
16551
0
    {
16552
      /* Relocs that use pc_count are PC-relative forms, which will appear
16553
   on something like ".long foo - ." or "movw REG, foo - .".  We want
16554
   calls to protected symbols to resolve directly to the function
16555
   rather than going via the plt.  If people want function pointer
16556
   comparisons to work as expected then they should avoid writing
16557
   assembly like ".long foo - .".  */
16558
0
      if (SYMBOL_CALLS_LOCAL (info, h))
16559
0
  {
16560
0
    struct elf_dyn_relocs **pp;
16561
16562
0
    for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
16563
0
      {
16564
0
        p->count -= p->pc_count;
16565
0
        p->pc_count = 0;
16566
0
        if (p->count == 0)
16567
0
    *pp = p->next;
16568
0
        else
16569
0
    pp = &p->next;
16570
0
      }
16571
0
  }
16572
16573
0
      if (htab->root.target_os == is_vxworks)
16574
0
  {
16575
0
    struct elf_dyn_relocs **pp;
16576
16577
0
    for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
16578
0
      {
16579
0
        if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
16580
0
    *pp = p->next;
16581
0
        else
16582
0
    pp = &p->next;
16583
0
      }
16584
0
  }
16585
16586
      /* Also discard relocs on undefined weak syms with non-default
16587
   visibility.  */
16588
0
      if (h->dyn_relocs != NULL
16589
0
    && h->root.type == bfd_link_hash_undefweak)
16590
0
  {
16591
0
    if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
16592
0
        || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
16593
0
      h->dyn_relocs = NULL;
16594
16595
    /* Make sure undefined weak symbols are output as a dynamic
16596
       symbol in PIEs.  */
16597
0
    else if (htab->root.dynamic_sections_created && h->dynindx == -1
16598
0
       && !h->forced_local)
16599
0
      {
16600
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
16601
0
    return false;
16602
0
      }
16603
0
  }
16604
0
    }
16605
0
  else
16606
0
    {
16607
      /* For the non-shared case, discard space for relocs against
16608
   symbols which turn out to need copy relocs or are not
16609
   dynamic.  */
16610
16611
0
      if (!h->non_got_ref
16612
0
    && ((h->def_dynamic
16613
0
         && !h->def_regular)
16614
0
        || (htab->root.dynamic_sections_created
16615
0
      && (h->root.type == bfd_link_hash_undefweak
16616
0
          || h->root.type == bfd_link_hash_undefined))))
16617
0
  {
16618
    /* Make sure this symbol is output as a dynamic symbol.
16619
       Undefined weak syms won't yet be marked as dynamic.  */
16620
0
    if (h->dynindx == -1 && !h->forced_local
16621
0
        && h->root.type == bfd_link_hash_undefweak)
16622
0
      {
16623
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
16624
0
    return false;
16625
0
      }
16626
16627
    /* If that succeeded, we know we'll be keeping all the
16628
       relocs.  */
16629
0
    if (h->dynindx != -1)
16630
0
      goto keep;
16631
0
  }
16632
16633
0
      h->dyn_relocs = NULL;
16634
16635
0
    keep: ;
16636
0
    }
16637
16638
  /* Finally, allocate space.  */
16639
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
16640
0
    {
16641
0
      asection *sreloc = elf_section_data (p->sec)->sreloc;
16642
16643
0
      if (h->type == STT_GNU_IFUNC
16644
0
    && eh->plt.noncall_refcount == 0
16645
0
    && SYMBOL_REFERENCES_LOCAL (info, h))
16646
0
  elf32_arm_allocate_irelocs (info, sreloc, p->count);
16647
0
      else if (h->dynindx != -1
16648
0
         && (!bfd_link_pic (info) || !info->symbolic || !h->def_regular))
16649
0
  elf32_arm_allocate_dynrelocs (info, sreloc, p->count);
16650
0
      else if (htab->fdpic_p && !bfd_link_pic (info))
16651
0
  htab->srofixup->size += 4 * p->count;
16652
0
      else
16653
0
  elf32_arm_allocate_dynrelocs (info, sreloc, p->count);
16654
0
    }
16655
16656
0
  return true;
16657
0
}
16658
16659
void
16660
bfd_elf32_arm_set_byteswap_code (struct bfd_link_info *info,
16661
         int byteswap_code)
16662
0
{
16663
0
  struct elf32_arm_link_hash_table *globals;
16664
16665
0
  globals = elf32_arm_hash_table (info);
16666
0
  if (globals == NULL)
16667
0
    return;
16668
16669
0
  globals->byteswap_code = byteswap_code;
16670
0
}
16671
16672
/* Set the sizes of the dynamic sections.  */
16673
16674
static bool
16675
elf32_arm_late_size_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
16676
            struct bfd_link_info * info)
16677
0
{
16678
0
  bfd * dynobj;
16679
0
  asection * s;
16680
0
  bool relocs;
16681
0
  bfd *ibfd;
16682
0
  struct elf32_arm_link_hash_table *htab;
16683
16684
0
  htab = elf32_arm_hash_table (info);
16685
0
  if (htab == NULL)
16686
0
    return false;
16687
16688
0
  dynobj = elf_hash_table (info)->dynobj;
16689
0
  if (dynobj == NULL)
16690
0
    return true;
16691
16692
0
  check_use_blx (htab);
16693
16694
0
  if (elf_hash_table (info)->dynamic_sections_created)
16695
0
    {
16696
      /* Set the contents of the .interp section to the interpreter.  */
16697
0
      if (bfd_link_executable (info) && !info->nointerp)
16698
0
  {
16699
0
    s = elf_hash_table (info)->interp;
16700
0
    BFD_ASSERT (s != NULL);
16701
0
    s->size = sizeof ELF_DYNAMIC_INTERPRETER;
16702
0
    s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
16703
0
    s->alloced = 1;
16704
0
  }
16705
0
    }
16706
16707
  /* Set up .got offsets for local syms, and space for local dynamic
16708
     relocs.  */
16709
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
16710
0
    {
16711
0
      bfd_signed_vma *local_got;
16712
0
      bfd_signed_vma *end_local_got;
16713
0
      struct arm_local_iplt_info **local_iplt_ptr, *local_iplt;
16714
0
      char *local_tls_type;
16715
0
      bfd_vma *local_tlsdesc_gotent;
16716
0
      bfd_size_type locsymcount;
16717
0
      Elf_Internal_Shdr *symtab_hdr;
16718
0
      asection *srel;
16719
0
      unsigned int symndx;
16720
0
      struct fdpic_local *local_fdpic_cnts;
16721
16722
0
      if (! is_arm_elf (ibfd))
16723
0
  continue;
16724
16725
0
      for (s = ibfd->sections; s != NULL; s = s->next)
16726
0
  {
16727
0
    struct elf_dyn_relocs *p;
16728
16729
0
    for (p = (struct elf_dyn_relocs *)
16730
0
       elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
16731
0
      {
16732
0
        if (!bfd_is_abs_section (p->sec)
16733
0
      && bfd_is_abs_section (p->sec->output_section))
16734
0
    {
16735
      /* Input section has been discarded, either because
16736
         it is a copy of a linkonce section or due to
16737
         linker script /DISCARD/, so we'll be discarding
16738
         the relocs too.  */
16739
0
    }
16740
0
        else if (htab->root.target_os == is_vxworks
16741
0
           && strcmp (p->sec->output_section->name,
16742
0
          ".tls_vars") == 0)
16743
0
    {
16744
      /* Relocations in vxworks .tls_vars sections are
16745
         handled specially by the loader.  */
16746
0
    }
16747
0
        else if (p->count != 0)
16748
0
    {
16749
0
      srel = elf_section_data (p->sec)->sreloc;
16750
0
      if (htab->fdpic_p && !bfd_link_pic (info))
16751
0
        htab->srofixup->size += 4 * p->count;
16752
0
      else
16753
0
        elf32_arm_allocate_dynrelocs (info, srel, p->count);
16754
0
      if ((p->sec->output_section->flags & SEC_READONLY) != 0)
16755
0
        info->flags |= DF_TEXTREL;
16756
0
    }
16757
0
      }
16758
0
  }
16759
16760
0
      local_got = elf_local_got_refcounts (ibfd);
16761
0
      if (local_got == NULL)
16762
0
  continue;
16763
16764
0
      symtab_hdr = & elf_symtab_hdr (ibfd);
16765
0
      locsymcount = symtab_hdr->sh_info;
16766
0
      end_local_got = local_got + locsymcount;
16767
0
      local_iplt_ptr = elf32_arm_local_iplt (ibfd);
16768
0
      local_tls_type = elf32_arm_local_got_tls_type (ibfd);
16769
0
      local_tlsdesc_gotent = elf32_arm_local_tlsdesc_gotent (ibfd);
16770
0
      local_fdpic_cnts = elf32_arm_local_fdpic_cnts (ibfd);
16771
0
      symndx = 0;
16772
0
      s = htab->root.sgot;
16773
0
      srel = htab->root.srelgot;
16774
0
      for (; local_got < end_local_got;
16775
0
     ++local_got, ++local_iplt_ptr, ++local_tls_type,
16776
0
     ++local_tlsdesc_gotent, ++symndx, ++local_fdpic_cnts)
16777
0
  {
16778
0
    if (symndx >= elf32_arm_num_entries (ibfd))
16779
0
      return false;
16780
16781
0
    *local_tlsdesc_gotent = (bfd_vma) -1;
16782
0
    local_iplt = *local_iplt_ptr;
16783
16784
    /* FDPIC support.  */
16785
0
    if (local_fdpic_cnts->gotofffuncdesc_cnt > 0)
16786
0
      {
16787
0
        if (local_fdpic_cnts->funcdesc_offset == -1)
16788
0
    {
16789
0
      local_fdpic_cnts->funcdesc_offset = s->size;
16790
0
      s->size += 8;
16791
16792
      /* We will add an R_ARM_FUNCDESC_VALUE relocation or two rofixups.  */
16793
0
      if (bfd_link_pic (info))
16794
0
        elf32_arm_allocate_dynrelocs (info, srel, 1);
16795
0
      else
16796
0
        htab->srofixup->size += 8;
16797
0
    }
16798
0
      }
16799
16800
0
    if (local_fdpic_cnts->funcdesc_cnt > 0)
16801
0
      {
16802
0
        if (local_fdpic_cnts->funcdesc_offset == -1)
16803
0
    {
16804
0
      local_fdpic_cnts->funcdesc_offset = s->size;
16805
0
      s->size += 8;
16806
16807
      /* We will add an R_ARM_FUNCDESC_VALUE relocation or two rofixups.  */
16808
0
      if (bfd_link_pic (info))
16809
0
        elf32_arm_allocate_dynrelocs (info, srel, 1);
16810
0
      else
16811
0
        htab->srofixup->size += 8;
16812
0
    }
16813
16814
        /* We will add n R_ARM_RELATIVE relocations or n rofixups.  */
16815
0
        if (bfd_link_pic (info))
16816
0
    elf32_arm_allocate_dynrelocs (info, srel, local_fdpic_cnts->funcdesc_cnt);
16817
0
        else
16818
0
    htab->srofixup->size += 4 * local_fdpic_cnts->funcdesc_cnt;
16819
0
      }
16820
16821
0
    if (local_iplt != NULL)
16822
0
      {
16823
0
        struct elf_dyn_relocs *p;
16824
16825
0
        if (local_iplt->root.refcount > 0)
16826
0
    {
16827
0
      elf32_arm_allocate_plt_entry (info, true,
16828
0
            &local_iplt->root,
16829
0
            &local_iplt->arm);
16830
0
      if (local_iplt->arm.noncall_refcount == 0)
16831
        /* All references to the PLT are calls, so all
16832
           non-call references can resolve directly to the
16833
           run-time target.  This means that the .got entry
16834
           would be the same as the .igot.plt entry, so there's
16835
           no point creating both.  */
16836
0
        *local_got = 0;
16837
0
    }
16838
0
        else
16839
0
    {
16840
0
      BFD_ASSERT (local_iplt->arm.noncall_refcount == 0);
16841
0
      local_iplt->root.offset = (bfd_vma) -1;
16842
0
    }
16843
16844
0
        for (p = local_iplt->dyn_relocs; p != NULL; p = p->next)
16845
0
    {
16846
0
      asection *psrel;
16847
16848
0
      psrel = elf_section_data (p->sec)->sreloc;
16849
0
      if (local_iplt->arm.noncall_refcount == 0)
16850
0
        elf32_arm_allocate_irelocs (info, psrel, p->count);
16851
0
      else
16852
0
        elf32_arm_allocate_dynrelocs (info, psrel, p->count);
16853
0
    }
16854
0
      }
16855
0
    if (*local_got > 0)
16856
0
      {
16857
0
        Elf_Internal_Sym *isym;
16858
16859
0
        *local_got = s->size;
16860
0
        if (*local_tls_type & GOT_TLS_GD)
16861
    /* TLS_GD relocs need an 8-byte structure in the GOT.  */
16862
0
    s->size += 8;
16863
0
        if (*local_tls_type & GOT_TLS_GDESC)
16864
0
    {
16865
0
      *local_tlsdesc_gotent = htab->root.sgotplt->size
16866
0
        - elf32_arm_compute_jump_table_size (htab);
16867
0
      htab->root.sgotplt->size += 8;
16868
0
      *local_got = (bfd_vma) -2;
16869
      /* plt.got_offset needs to know there's a TLS_DESC
16870
         reloc in the middle of .got.plt.  */
16871
0
      htab->num_tls_desc++;
16872
0
    }
16873
0
        if (*local_tls_type & GOT_TLS_IE)
16874
0
    s->size += 4;
16875
16876
0
        if (*local_tls_type & GOT_NORMAL)
16877
0
    {
16878
      /* If the symbol is both GD and GDESC, *local_got
16879
         may have been overwritten.  */
16880
0
      *local_got = s->size;
16881
0
      s->size += 4;
16882
0
    }
16883
16884
0
        isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, ibfd,
16885
0
              symndx);
16886
0
        if (isym == NULL)
16887
0
    return false;
16888
16889
        /* If all references to an STT_GNU_IFUNC PLT are calls,
16890
     then all non-call references, including this GOT entry,
16891
     resolve directly to the run-time target.  */
16892
0
        if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
16893
0
      && (local_iplt == NULL
16894
0
          || local_iplt->arm.noncall_refcount == 0))
16895
0
    elf32_arm_allocate_irelocs (info, srel, 1);
16896
0
        else if (bfd_link_pic (info) || output_bfd->flags & DYNAMIC || htab->fdpic_p)
16897
0
    {
16898
0
      if ((bfd_link_pic (info) && !(*local_tls_type & GOT_TLS_GDESC)))
16899
0
        elf32_arm_allocate_dynrelocs (info, srel, 1);
16900
0
      else if (htab->fdpic_p && *local_tls_type & GOT_NORMAL)
16901
0
        htab->srofixup->size += 4;
16902
16903
0
      if ((bfd_link_pic (info) || htab->fdpic_p)
16904
0
          && *local_tls_type & GOT_TLS_GDESC)
16905
0
        {
16906
0
          elf32_arm_allocate_dynrelocs (info,
16907
0
                htab->root.srelplt, 1);
16908
0
          htab->tls_trampoline = -1;
16909
0
        }
16910
0
    }
16911
0
      }
16912
0
    else
16913
0
      *local_got = (bfd_vma) -1;
16914
0
  }
16915
0
    }
16916
16917
0
  if (htab->tls_ldm_got.refcount > 0)
16918
0
    {
16919
      /* Allocate two GOT entries and one dynamic relocation (if necessary)
16920
   for R_ARM_TLS_LDM32/R_ARM_TLS_LDM32_FDPIC relocations.  */
16921
0
      htab->tls_ldm_got.offset = htab->root.sgot->size;
16922
0
      htab->root.sgot->size += 8;
16923
0
      if (bfd_link_pic (info))
16924
0
  elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16925
0
    }
16926
0
  else
16927
0
    htab->tls_ldm_got.offset = -1;
16928
16929
  /* At the very end of the .rofixup section is a pointer to the GOT,
16930
     reserve space for it. */
16931
0
  if (htab->fdpic_p && htab->srofixup != NULL)
16932
0
    htab->srofixup->size += 4;
16933
16934
  /* Allocate global sym .plt and .got entries, and space for global
16935
     sym dynamic relocs.  */
16936
0
  elf_link_hash_traverse (& htab->root, allocate_dynrelocs_for_symbol, info);
16937
16938
  /* Here we rummage through the found bfds to collect glue information.  */
16939
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
16940
0
    {
16941
0
      if (! is_arm_elf (ibfd))
16942
0
  continue;
16943
16944
      /* Initialise mapping tables for code/data.  */
16945
0
      bfd_elf32_arm_init_maps (ibfd);
16946
16947
0
      if (!bfd_elf32_arm_process_before_allocation (ibfd, info)
16948
0
    || !bfd_elf32_arm_vfp11_erratum_scan (ibfd, info)
16949
0
    || !bfd_elf32_arm_stm32l4xx_erratum_scan (ibfd, info))
16950
0
  _bfd_error_handler (_("errors encountered processing file %pB"), ibfd);
16951
0
    }
16952
16953
  /* Allocate space for the glue sections now that we've sized them.  */
16954
0
  bfd_elf32_arm_allocate_interworking_sections (info);
16955
16956
  /* For every jump slot reserved in the sgotplt, reloc_count is
16957
     incremented.  However, when we reserve space for TLS descriptors,
16958
     it's not incremented, so in order to compute the space reserved
16959
     for them, it suffices to multiply the reloc count by the jump
16960
     slot size.  */
16961
0
  if (htab->root.srelplt)
16962
0
    htab->sgotplt_jump_table_size = elf32_arm_compute_jump_table_size (htab);
16963
16964
0
  if (htab->tls_trampoline)
16965
0
    {
16966
0
      if (htab->root.splt->size == 0)
16967
0
  htab->root.splt->size += htab->plt_header_size;
16968
16969
0
      htab->tls_trampoline = htab->root.splt->size;
16970
0
      htab->root.splt->size += htab->plt_entry_size;
16971
16972
      /* If we're not using lazy TLS relocations, don't generate the
16973
   PLT and GOT entries they require.  */
16974
0
      if ((info->flags & DF_BIND_NOW))
16975
0
  htab->root.tlsdesc_plt = 0;
16976
0
      else
16977
0
  {
16978
0
    htab->root.tlsdesc_got = htab->root.sgot->size;
16979
0
    htab->root.sgot->size += 4;
16980
16981
0
    htab->root.tlsdesc_plt = htab->root.splt->size;
16982
0
    htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
16983
0
  }
16984
0
    }
16985
16986
  /* The check_relocs and adjust_dynamic_symbol entry points have
16987
     determined the sizes of the various dynamic sections.  Allocate
16988
     memory for them.  */
16989
0
  relocs = false;
16990
0
  for (s = dynobj->sections; s != NULL; s = s->next)
16991
0
    {
16992
0
      const char * name;
16993
16994
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
16995
0
  continue;
16996
16997
      /* It's OK to base decisions on the section name, because none
16998
   of the dynobj section names depend upon the input files.  */
16999
0
      name = bfd_section_name (s);
17000
17001
0
      if (s == htab->root.splt)
17002
0
  {
17003
    /* Remember whether there is a PLT.  */
17004
0
    ;
17005
0
  }
17006
0
      else if (startswith (name, ".rel"))
17007
0
  {
17008
0
    if (s->size != 0)
17009
0
      {
17010
        /* Remember whether there are any reloc sections other
17011
     than .rel(a).plt and .rela.plt.unloaded.  */
17012
0
        if (s != htab->root.srelplt && s != htab->srelplt2)
17013
0
    relocs = true;
17014
17015
        /* We use the reloc_count field as a counter if we need
17016
     to copy relocs into the output file.  */
17017
0
        s->reloc_count = 0;
17018
0
      }
17019
0
  }
17020
0
      else if (s != htab->root.sgot
17021
0
         && s != htab->root.sgotplt
17022
0
         && s != htab->root.iplt
17023
0
         && s != htab->root.igotplt
17024
0
         && s != htab->root.sdynbss
17025
0
         && s != htab->root.sdynrelro
17026
0
         && s != htab->srofixup)
17027
0
  {
17028
    /* It's not one of our sections, so don't allocate space.  */
17029
0
    continue;
17030
0
  }
17031
17032
0
      if (s->size == 0)
17033
0
  {
17034
    /* If we don't need this section, strip it from the
17035
       output file.  This is mostly to handle .rel(a).bss and
17036
       .rel(a).plt.  We must create both sections in
17037
       create_dynamic_sections, because they must be created
17038
       before the linker maps input sections to output
17039
       sections.  The linker does that before
17040
       adjust_dynamic_symbol is called, and it is that
17041
       function which decides whether anything needs to go
17042
       into these sections.  */
17043
0
    s->flags |= SEC_EXCLUDE;
17044
0
    continue;
17045
0
  }
17046
17047
0
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
17048
0
  continue;
17049
17050
      /* Allocate memory for the section contents.  */
17051
0
      s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
17052
0
      if (s->contents == NULL)
17053
0
  return false;
17054
0
      s->alloced = 1;
17055
0
    }
17056
17057
0
  return _bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd, info,
17058
0
              relocs);
17059
0
}
17060
17061
/* Size sections even though they're not dynamic.  We use it to setup
17062
   _TLS_MODULE_BASE_, if needed.  */
17063
17064
static bool
17065
elf32_arm_early_size_sections (bfd *output_bfd, struct bfd_link_info *info)
17066
0
{
17067
0
  asection *tls_sec;
17068
0
  struct elf32_arm_link_hash_table *htab;
17069
17070
0
  htab = elf32_arm_hash_table (info);
17071
17072
0
  if (bfd_link_relocatable (info))
17073
0
    return true;
17074
17075
0
  tls_sec = elf_hash_table (info)->tls_sec;
17076
17077
0
  if (tls_sec)
17078
0
    {
17079
0
      struct elf_link_hash_entry *tlsbase;
17080
17081
0
      tlsbase = elf_link_hash_lookup
17082
0
  (elf_hash_table (info), "_TLS_MODULE_BASE_", true, true, false);
17083
17084
0
      if (tlsbase)
17085
0
  {
17086
0
    struct bfd_link_hash_entry *bh = NULL;
17087
0
    elf_backend_data *bed = get_elf_backend_data (output_bfd);
17088
17089
0
    if (!(_bfd_generic_link_add_one_symbol
17090
0
    (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
17091
0
     tls_sec, 0, NULL, false,
17092
0
     bed->collect, &bh)))
17093
0
      return false;
17094
17095
0
    tlsbase->type = STT_TLS;
17096
0
    tlsbase = (struct elf_link_hash_entry *)bh;
17097
0
    tlsbase->def_regular = 1;
17098
0
    tlsbase->other = STV_HIDDEN;
17099
0
    (*bed->elf_backend_hide_symbol) (info, tlsbase, true);
17100
0
  }
17101
0
    }
17102
17103
0
  if (htab->fdpic_p && !bfd_link_relocatable (info)
17104
0
      && !bfd_elf_stack_segment_size (output_bfd, info,
17105
0
              "__stacksize", DEFAULT_STACK_SIZE))
17106
0
    return false;
17107
17108
0
  return true;
17109
0
}
17110
17111
/* Finish up dynamic symbol handling.  We set the contents of various
17112
   dynamic sections here.  */
17113
17114
static bool
17115
elf32_arm_finish_dynamic_symbol (bfd * output_bfd,
17116
         struct bfd_link_info * info,
17117
         struct elf_link_hash_entry * h,
17118
         Elf_Internal_Sym * sym)
17119
0
{
17120
0
  struct elf32_arm_link_hash_table *htab;
17121
0
  struct elf32_arm_link_hash_entry *eh;
17122
17123
0
  htab = elf32_arm_hash_table (info);
17124
17125
0
  eh = (struct elf32_arm_link_hash_entry *) h;
17126
17127
0
  if (h->plt.offset != (bfd_vma) -1)
17128
0
    {
17129
0
      if (!eh->is_iplt)
17130
0
  {
17131
0
    BFD_ASSERT (h->dynindx != -1);
17132
0
    if (! elf32_arm_populate_plt_entry (output_bfd, info, &h->plt, &eh->plt,
17133
0
                h->dynindx, 0))
17134
0
      return false;
17135
0
  }
17136
17137
0
      if (!h->def_regular)
17138
0
  {
17139
    /* Mark the symbol as undefined, rather than as defined in
17140
       the .plt section.  */
17141
0
    sym->st_shndx = SHN_UNDEF;
17142
    /* If the symbol is weak we need to clear the value.
17143
       Otherwise, the PLT entry would provide a definition for
17144
       the symbol even if the symbol wasn't defined anywhere,
17145
       and so the symbol would never be NULL.  Leave the value if
17146
       there were any relocations where pointer equality matters
17147
       (this is a clue for the dynamic linker, to make function
17148
       pointer comparisons work between an application and shared
17149
       library).  */
17150
0
    if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
17151
0
      sym->st_value = 0;
17152
0
  }
17153
0
      else if (eh->is_iplt && eh->plt.noncall_refcount != 0)
17154
0
  {
17155
    /* At least one non-call relocation references this .iplt entry,
17156
       so the .iplt entry is the function's canonical address.  */
17157
0
    sym->st_info = ELF_ST_INFO (ELF_ST_BIND (sym->st_info), STT_FUNC);
17158
0
    ARM_SET_SYM_BRANCH_TYPE (sym->st_target_internal, ST_BRANCH_TO_ARM);
17159
0
    sym->st_shndx = (_bfd_elf_section_from_bfd_section
17160
0
         (output_bfd, htab->root.iplt->output_section));
17161
0
    sym->st_value = (h->plt.offset
17162
0
         + htab->root.iplt->output_section->vma
17163
0
         + htab->root.iplt->output_offset);
17164
0
  }
17165
0
    }
17166
17167
0
  if (h->needs_copy)
17168
0
    {
17169
0
      asection * s;
17170
0
      Elf_Internal_Rela rel;
17171
17172
      /* This symbol needs a copy reloc.  Set it up.  */
17173
0
      BFD_ASSERT (h->dynindx != -1
17174
0
      && (h->root.type == bfd_link_hash_defined
17175
0
          || h->root.type == bfd_link_hash_defweak));
17176
17177
0
      rel.r_addend = 0;
17178
0
      rel.r_offset = (h->root.u.def.value
17179
0
          + h->root.u.def.section->output_section->vma
17180
0
          + h->root.u.def.section->output_offset);
17181
0
      rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_COPY);
17182
0
      if (h->root.u.def.section == htab->root.sdynrelro)
17183
0
  s = htab->root.sreldynrelro;
17184
0
      else
17185
0
  s = htab->root.srelbss;
17186
0
      elf32_arm_add_dynreloc (output_bfd, info, s, &rel);
17187
0
    }
17188
17189
  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  On VxWorks,
17190
     and for FDPIC, the _GLOBAL_OFFSET_TABLE_ symbol is not absolute:
17191
     it is relative to the ".got" section.  */
17192
0
  if (h == htab->root.hdynamic
17193
0
      || (!htab->fdpic_p
17194
0
    && htab->root.target_os != is_vxworks
17195
0
    && h == htab->root.hgot))
17196
0
    sym->st_shndx = SHN_ABS;
17197
17198
0
  return true;
17199
0
}
17200
17201
static void
17202
arm_put_trampoline (struct elf32_arm_link_hash_table *htab, bfd *output_bfd,
17203
        void *contents,
17204
        const unsigned long *template, unsigned count)
17205
0
{
17206
0
  unsigned ix;
17207
17208
0
  for (ix = 0; ix != count; ix++)
17209
0
    {
17210
0
      unsigned long insn = template[ix];
17211
17212
      /* Emit mov pc,rx if bx is not permitted.  */
17213
0
      if (htab->fix_v4bx == 1 && (insn & 0x0ffffff0) == 0x012fff10)
17214
0
  insn = (insn & 0xf000000f) | 0x01a0f000;
17215
0
      put_arm_insn (htab, output_bfd, insn, (char *)contents + ix*4);
17216
0
    }
17217
0
}
17218
17219
/* Finish up the dynamic sections.  */
17220
17221
static bool
17222
elf32_arm_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info,
17223
           bfd_byte *buf ATTRIBUTE_UNUSED)
17224
0
{
17225
0
  bfd * dynobj;
17226
0
  asection * sgot;
17227
0
  asection * sdyn;
17228
0
  struct elf32_arm_link_hash_table *htab;
17229
17230
0
  htab = elf32_arm_hash_table (info);
17231
0
  if (htab == NULL)
17232
0
    return false;
17233
17234
0
  dynobj = elf_hash_table (info)->dynobj;
17235
17236
0
  sgot = htab->root.sgotplt;
17237
  /* A broken linker script might have discarded the dynamic sections.
17238
     Catch this here so that we do not seg-fault later on.  */
17239
0
  if (sgot != NULL && bfd_is_abs_section (sgot->output_section))
17240
0
    return false;
17241
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
17242
17243
0
  if (elf_hash_table (info)->dynamic_sections_created)
17244
0
    {
17245
0
      asection *splt;
17246
0
      Elf32_External_Dyn *dyncon, *dynconend;
17247
17248
0
      splt = htab->root.splt;
17249
0
      BFD_ASSERT (splt != NULL && sdyn != NULL);
17250
0
      BFD_ASSERT (sgot != NULL);
17251
17252
0
      dyncon = (Elf32_External_Dyn *) sdyn->contents;
17253
0
      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
17254
17255
0
      for (; dyncon < dynconend; dyncon++)
17256
0
  {
17257
0
    Elf_Internal_Dyn dyn;
17258
0
    const char * name;
17259
0
    asection * s;
17260
17261
0
    bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
17262
17263
0
    switch (dyn.d_tag)
17264
0
      {
17265
0
      default:
17266
0
        if (htab->root.target_os == is_vxworks
17267
0
      && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
17268
0
    bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
17269
0
        break;
17270
17271
0
      case DT_HASH:
17272
0
      case DT_STRTAB:
17273
0
      case DT_SYMTAB:
17274
0
      case DT_VERSYM:
17275
0
      case DT_VERDEF:
17276
0
      case DT_VERNEED:
17277
0
        break;
17278
17279
0
      case DT_PLTGOT:
17280
0
        name = ".got.plt";
17281
0
        goto get_vma;
17282
0
      case DT_JMPREL:
17283
0
        name = RELOC_SECTION (htab, ".plt");
17284
0
      get_vma:
17285
0
        s = bfd_get_linker_section (dynobj, name);
17286
0
        if (s == NULL)
17287
0
    {
17288
0
      _bfd_error_handler
17289
0
        (_("could not find section %s"), name);
17290
0
      bfd_set_error (bfd_error_invalid_operation);
17291
0
      return false;
17292
0
    }
17293
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
17294
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
17295
0
        break;
17296
17297
0
      case DT_PLTRELSZ:
17298
0
        s = htab->root.srelplt;
17299
0
        BFD_ASSERT (s != NULL);
17300
0
        dyn.d_un.d_val = s->size;
17301
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
17302
0
        break;
17303
17304
0
      case DT_RELSZ:
17305
0
      case DT_RELASZ:
17306
0
      case DT_REL:
17307
0
      case DT_RELA:
17308
0
        break;
17309
17310
0
      case DT_TLSDESC_PLT:
17311
0
        s = htab->root.splt;
17312
0
        dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
17313
0
        + htab->root.tlsdesc_plt);
17314
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
17315
0
        break;
17316
17317
0
      case DT_TLSDESC_GOT:
17318
0
        s = htab->root.sgot;
17319
0
        dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
17320
0
        + htab->root.tlsdesc_got);
17321
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
17322
0
        break;
17323
17324
        /* Set the bottom bit of DT_INIT/FINI if the
17325
     corresponding function is Thumb.  */
17326
0
      case DT_INIT:
17327
0
        name = info->init_function;
17328
0
        goto get_sym;
17329
0
      case DT_FINI:
17330
0
        name = info->fini_function;
17331
0
      get_sym:
17332
        /* If it wasn't set by elf_bfd_final_link
17333
     then there is nothing to adjust.  */
17334
0
        if (dyn.d_un.d_val != 0)
17335
0
    {
17336
0
      struct elf_link_hash_entry * eh;
17337
17338
0
      eh = elf_link_hash_lookup (elf_hash_table (info), name,
17339
0
               false, false, true);
17340
0
      if (eh != NULL
17341
0
          && ARM_GET_SYM_BRANCH_TYPE (eh->target_internal)
17342
0
       == ST_BRANCH_TO_THUMB)
17343
0
        {
17344
0
          dyn.d_un.d_val |= 1;
17345
0
          bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
17346
0
        }
17347
0
    }
17348
0
        break;
17349
0
      }
17350
0
  }
17351
17352
      /* Fill in the first entry in the procedure linkage table.  */
17353
0
      if (splt->size > 0 && htab->plt_header_size)
17354
0
  {
17355
0
    const bfd_vma *plt0_entry;
17356
0
    bfd_vma got_address, plt_address, got_displacement;
17357
17358
    /* Calculate the addresses of the GOT and PLT.  */
17359
0
    got_address = sgot->output_section->vma + sgot->output_offset;
17360
0
    plt_address = splt->output_section->vma + splt->output_offset;
17361
17362
0
    if (htab->root.target_os == is_vxworks)
17363
0
      {
17364
        /* The VxWorks GOT is relocated by the dynamic linker.
17365
     Therefore, we must emit relocations rather than simply
17366
     computing the values now.  */
17367
0
        Elf_Internal_Rela rel;
17368
17369
0
        plt0_entry = elf32_arm_vxworks_exec_plt0_entry;
17370
0
        put_arm_insn (htab, output_bfd, plt0_entry[0],
17371
0
          splt->contents + 0);
17372
0
        put_arm_insn (htab, output_bfd, plt0_entry[1],
17373
0
          splt->contents + 4);
17374
0
        put_arm_insn (htab, output_bfd, plt0_entry[2],
17375
0
          splt->contents + 8);
17376
0
        bfd_put_32 (output_bfd, got_address, splt->contents + 12);
17377
17378
        /* Generate a relocation for _GLOBAL_OFFSET_TABLE_.  */
17379
0
        rel.r_offset = plt_address + 12;
17380
0
        rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
17381
0
        rel.r_addend = 0;
17382
0
        SWAP_RELOC_OUT (htab) (output_bfd, &rel,
17383
0
             htab->srelplt2->contents);
17384
0
      }
17385
0
    else if (using_thumb_only (htab))
17386
0
      {
17387
0
        got_displacement = got_address - (plt_address + 12);
17388
17389
0
        plt0_entry = elf32_thumb2_plt0_entry;
17390
0
        put_arm_insn (htab, output_bfd, plt0_entry[0],
17391
0
          splt->contents + 0);
17392
0
        put_arm_insn (htab, output_bfd, plt0_entry[1],
17393
0
          splt->contents + 4);
17394
0
        put_arm_insn (htab, output_bfd, plt0_entry[2],
17395
0
          splt->contents + 8);
17396
17397
0
        bfd_put_32 (output_bfd, got_displacement, splt->contents + 12);
17398
0
      }
17399
0
    else
17400
0
      {
17401
0
        got_displacement = got_address - (plt_address + 16);
17402
17403
0
        plt0_entry = elf32_arm_plt0_entry;
17404
0
        put_arm_insn (htab, output_bfd, plt0_entry[0],
17405
0
          splt->contents + 0);
17406
0
        put_arm_insn (htab, output_bfd, plt0_entry[1],
17407
0
          splt->contents + 4);
17408
0
        put_arm_insn (htab, output_bfd, plt0_entry[2],
17409
0
          splt->contents + 8);
17410
0
        put_arm_insn (htab, output_bfd, plt0_entry[3],
17411
0
          splt->contents + 12);
17412
17413
#ifdef FOUR_WORD_PLT
17414
        /* The displacement value goes in the otherwise-unused
17415
     last word of the second entry.  */
17416
        bfd_put_32 (output_bfd, got_displacement, splt->contents + 28);
17417
#else
17418
0
        bfd_put_32 (output_bfd, got_displacement, splt->contents + 16);
17419
0
#endif
17420
0
      }
17421
0
  }
17422
17423
      /* UnixWare sets the entsize of .plt to 4, although that doesn't
17424
   really seem like the right value.  */
17425
0
      if (splt->output_section->owner == output_bfd)
17426
0
  elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
17427
17428
0
      if (htab->root.tlsdesc_plt)
17429
0
  {
17430
0
    bfd_vma got_address
17431
0
      = sgot->output_section->vma + sgot->output_offset;
17432
0
    bfd_vma gotplt_address = (htab->root.sgot->output_section->vma
17433
0
            + htab->root.sgot->output_offset);
17434
0
    bfd_vma plt_address
17435
0
      = splt->output_section->vma + splt->output_offset;
17436
17437
0
    arm_put_trampoline (htab, output_bfd,
17438
0
            splt->contents + htab->root.tlsdesc_plt,
17439
0
            dl_tlsdesc_lazy_trampoline, 6);
17440
17441
0
    bfd_put_32 (output_bfd,
17442
0
          gotplt_address + htab->root.tlsdesc_got
17443
0
          - (plt_address + htab->root.tlsdesc_plt)
17444
0
          - dl_tlsdesc_lazy_trampoline[6],
17445
0
          splt->contents + htab->root.tlsdesc_plt + 24);
17446
0
    bfd_put_32 (output_bfd,
17447
0
          got_address - (plt_address + htab->root.tlsdesc_plt)
17448
0
          - dl_tlsdesc_lazy_trampoline[7],
17449
0
          splt->contents + htab->root.tlsdesc_plt + 24 + 4);
17450
0
  }
17451
17452
0
      if (htab->tls_trampoline)
17453
0
  {
17454
0
    arm_put_trampoline (htab, output_bfd,
17455
0
            splt->contents + htab->tls_trampoline,
17456
0
            tls_trampoline, 3);
17457
#ifdef FOUR_WORD_PLT
17458
    bfd_put_32 (output_bfd, 0x00000000,
17459
          splt->contents + htab->tls_trampoline + 12);
17460
#endif
17461
0
  }
17462
17463
0
      if (htab->root.target_os == is_vxworks
17464
0
    && !bfd_link_pic (info)
17465
0
    && htab->root.splt->size > 0)
17466
0
  {
17467
    /* Correct the .rel(a).plt.unloaded relocations.  They will have
17468
       incorrect symbol indexes.  */
17469
0
    int num_plts;
17470
0
    unsigned char *p;
17471
17472
0
    num_plts = ((htab->root.splt->size - htab->plt_header_size)
17473
0
          / htab->plt_entry_size);
17474
0
    p = htab->srelplt2->contents + RELOC_SIZE (htab);
17475
17476
0
    for (; num_plts; num_plts--)
17477
0
      {
17478
0
        Elf_Internal_Rela rel;
17479
17480
0
        SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
17481
0
        rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
17482
0
        SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
17483
0
        p += RELOC_SIZE (htab);
17484
17485
0
        SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
17486
0
        rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
17487
0
        SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
17488
0
        p += RELOC_SIZE (htab);
17489
0
      }
17490
0
  }
17491
0
    }
17492
17493
  /* Fill in the first three entries in the global offset table.  */
17494
0
  if (sgot)
17495
0
    {
17496
0
      if (sgot->size > 0)
17497
0
  {
17498
0
    if (sdyn == NULL)
17499
0
      bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
17500
0
    else
17501
0
      bfd_put_32 (output_bfd,
17502
0
      sdyn->output_section->vma + sdyn->output_offset,
17503
0
      sgot->contents);
17504
0
    bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
17505
0
    bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
17506
0
  }
17507
17508
0
      elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
17509
0
    }
17510
17511
  /* At the very end of the .rofixup section is a pointer to the GOT.  */
17512
0
  if (htab->fdpic_p && htab->srofixup != NULL)
17513
0
    {
17514
0
      struct elf_link_hash_entry *hgot = htab->root.hgot;
17515
17516
0
      bfd_vma got_value = hgot->root.u.def.value
17517
0
  + hgot->root.u.def.section->output_section->vma
17518
0
  + hgot->root.u.def.section->output_offset;
17519
17520
0
      arm_elf_add_rofixup (output_bfd, htab->srofixup, got_value);
17521
17522
      /* Make sure we allocated and generated the same number of fixups.  */
17523
0
      BFD_ASSERT (htab->srofixup->reloc_count * 4 == htab->srofixup->size);
17524
0
    }
17525
17526
0
  return true;
17527
0
}
17528
17529
static bool
17530
elf32_arm_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
17531
3
{
17532
3
  Elf_Internal_Ehdr * i_ehdrp;  /* ELF file header, internal form.  */
17533
3
  struct elf32_arm_link_hash_table *globals;
17534
3
  struct elf_segment_map *m;
17535
17536
3
  if (!_bfd_elf_init_file_header (abfd, link_info))
17537
0
    return false;
17538
17539
3
  i_ehdrp = elf_elfheader (abfd);
17540
17541
3
  if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_UNKNOWN)
17542
2
    i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_ARM;
17543
3
  i_ehdrp->e_ident[EI_ABIVERSION] = ARM_ELF_ABI_VERSION;
17544
17545
3
  if (link_info)
17546
0
    {
17547
0
      globals = elf32_arm_hash_table (link_info);
17548
0
      if (globals != NULL && globals->byteswap_code)
17549
0
  i_ehdrp->e_flags |= EF_ARM_BE8;
17550
17551
0
      if (globals->fdpic_p)
17552
0
  i_ehdrp->e_ident[EI_OSABI] |= ELFOSABI_ARM_FDPIC;
17553
0
    }
17554
17555
3
  if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_VER5
17556
0
      && ((i_ehdrp->e_type == ET_DYN) || (i_ehdrp->e_type == ET_EXEC)))
17557
0
    {
17558
0
      int abi = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC, Tag_ABI_VFP_args);
17559
0
      if (abi == AEABI_VFP_args_vfp)
17560
0
  i_ehdrp->e_flags |= EF_ARM_ABI_FLOAT_HARD;
17561
0
      else
17562
0
  i_ehdrp->e_flags |= EF_ARM_ABI_FLOAT_SOFT;
17563
0
    }
17564
17565
  /* Scan segment to set p_flags attribute if it contains only sections with
17566
     SHF_ARM_PURECODE flag.  */
17567
7
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
17568
4
    {
17569
4
      unsigned int j;
17570
17571
4
      if (m->count == 0)
17572
4
  continue;
17573
0
      for (j = 0; j < m->count; j++)
17574
0
  {
17575
0
    if (!(elf_section_flags (m->sections[j]) & SHF_ARM_PURECODE))
17576
0
      break;
17577
0
  }
17578
0
      if (j == m->count)
17579
0
  {
17580
0
    m->p_flags = PF_X;
17581
0
    m->p_flags_valid = 1;
17582
0
  }
17583
0
    }
17584
3
  return true;
17585
3
}
17586
17587
static enum elf_reloc_type_class
17588
elf32_arm_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
17589
          const asection *rel_sec ATTRIBUTE_UNUSED,
17590
          const Elf_Internal_Rela *rela)
17591
0
{
17592
0
  struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
17593
17594
0
  if (htab->root.dynsym != NULL
17595
0
      && htab->root.dynsym->contents != NULL)
17596
0
    {
17597
      /* Check relocation against STT_GNU_IFUNC symbol if there are
17598
   dynamic symbols.  */
17599
0
      bfd *abfd = info->output_bfd;
17600
0
      elf_backend_data *bed = get_elf_backend_data (abfd);
17601
0
      unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
17602
0
      if (r_symndx != STN_UNDEF)
17603
0
  {
17604
0
    Elf_Internal_Sym sym;
17605
0
    if (!bed->s->swap_symbol_in (abfd,
17606
0
               (htab->root.dynsym->contents
17607
0
          + r_symndx * bed->s->sizeof_sym),
17608
0
               0, &sym))
17609
0
      {
17610
        /* xgettext:c-format */
17611
0
        _bfd_error_handler (_("%pB symbol number %lu references"
17612
0
            " nonexistent SHT_SYMTAB_SHNDX section"),
17613
0
          abfd, r_symndx);
17614
        /* Ideally an error class should be returned here.  */
17615
0
      }
17616
0
    else if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
17617
0
      return reloc_class_ifunc;
17618
0
  }
17619
0
    }
17620
17621
0
  switch ((int) ELF32_R_TYPE (rela->r_info))
17622
0
    {
17623
0
    case R_ARM_RELATIVE:
17624
0
      return reloc_class_relative;
17625
0
    case R_ARM_JUMP_SLOT:
17626
0
      return reloc_class_plt;
17627
0
    case R_ARM_COPY:
17628
0
      return reloc_class_copy;
17629
0
    case R_ARM_IRELATIVE:
17630
0
      return reloc_class_ifunc;
17631
0
    default:
17632
0
      return reloc_class_normal;
17633
0
    }
17634
0
}
17635
17636
static void
17637
arm_final_write_processing (bfd *abfd)
17638
3
{
17639
3
  bfd_arm_update_notes (abfd, ARM_NOTE_SECTION);
17640
3
}
17641
17642
static bool
17643
elf32_arm_final_write_processing (bfd *abfd)
17644
3
{
17645
3
  arm_final_write_processing (abfd);
17646
3
  return _bfd_elf_final_write_processing (abfd);
17647
3
}
17648
17649
/* Return TRUE if this is an unwinding table entry.  */
17650
17651
static bool
17652
is_arm_elf_unwind_section_name (bfd * abfd ATTRIBUTE_UNUSED, const char * name)
17653
3
{
17654
3
  return (startswith (name, ELF_STRING_ARM_unwind)
17655
3
    || startswith (name, ELF_STRING_ARM_unwind_once));
17656
3
}
17657
17658
17659
/* Set the type and flags for an ARM section.  We do this by
17660
   the section name, which is a hack, but ought to work.  */
17661
17662
static bool
17663
elf32_arm_fake_sections (bfd * abfd, Elf_Internal_Shdr * hdr, asection * sec)
17664
3
{
17665
3
  const char * name;
17666
17667
3
  name = bfd_section_name (sec);
17668
17669
3
  if (is_arm_elf_unwind_section_name (abfd, name))
17670
0
    {
17671
0
      hdr->sh_type = SHT_ARM_EXIDX;
17672
0
      hdr->sh_flags |= SHF_LINK_ORDER;
17673
0
    }
17674
17675
3
  if (sec->flags & SEC_ELF_PURECODE)
17676
0
    hdr->sh_flags |= SHF_ARM_PURECODE;
17677
17678
3
  return true;
17679
3
}
17680
17681
/* Handle an ARM specific section when reading an object file.  This is
17682
   called when bfd_section_from_shdr finds a section with an unknown
17683
   type.  */
17684
17685
static bool
17686
elf32_arm_section_from_shdr (bfd *abfd,
17687
           Elf_Internal_Shdr * hdr,
17688
           const char *name,
17689
           int shindex)
17690
430
{
17691
  /* There ought to be a place to keep ELF backend specific flags, but
17692
     at the moment there isn't one.  We just keep track of the
17693
     sections by their name, instead.  Fortunately, the ABI gives
17694
     names for all the ARM specific sections, so we will probably get
17695
     away with this.  */
17696
430
  switch (hdr->sh_type)
17697
430
    {
17698
8
    case SHT_ARM_EXIDX:
17699
11
    case SHT_ARM_PREEMPTMAP:
17700
11
    case SHT_ARM_ATTRIBUTES:
17701
11
      break;
17702
17703
419
    default:
17704
419
      return false;
17705
430
    }
17706
17707
11
  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
17708
0
    return false;
17709
17710
11
  return true;
17711
11
}
17712
17713
static _arm_elf_section_data *
17714
get_arm_elf_section_data (asection * sec)
17715
0
{
17716
0
  if (sec && sec->owner && is_arm_elf (sec->owner))
17717
0
    return elf32_arm_section_data (sec);
17718
0
  else
17719
0
    return NULL;
17720
0
}
17721
17722
typedef struct
17723
{
17724
  void *flaginfo;
17725
  struct bfd_link_info *info;
17726
  asection *sec;
17727
  int sec_shndx;
17728
  int (*func) (void *, const char *, Elf_Internal_Sym *,
17729
         asection *, struct elf_link_hash_entry *);
17730
} output_arch_syminfo;
17731
17732
enum map_symbol_type
17733
{
17734
  ARM_MAP_ARM,
17735
  ARM_MAP_THUMB,
17736
  ARM_MAP_DATA
17737
};
17738
17739
17740
/* Output a single mapping symbol.  */
17741
17742
static bool
17743
elf32_arm_output_map_sym (output_arch_syminfo *osi,
17744
        enum map_symbol_type type,
17745
        bfd_vma offset)
17746
0
{
17747
0
  static const char *names[3] = {"$a", "$t", "$d"};
17748
0
  Elf_Internal_Sym sym;
17749
17750
0
  sym.st_value = osi->sec->output_section->vma
17751
0
     + osi->sec->output_offset
17752
0
     + offset;
17753
0
  sym.st_size = 0;
17754
0
  sym.st_other = 0;
17755
0
  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
17756
0
  sym.st_shndx = osi->sec_shndx;
17757
0
  sym.st_target_internal = ST_BRANCH_TO_ARM;
17758
0
  elf32_arm_section_map_add (osi->sec, names[type][1], offset);
17759
0
  return osi->func (osi->flaginfo, names[type], &sym, osi->sec, NULL) == 1;
17760
0
}
17761
17762
/* Output mapping symbols for the PLT entry described by ROOT_PLT and ARM_PLT.
17763
   IS_IPLT_ENTRY_P says whether the PLT is in .iplt rather than .plt.  */
17764
17765
static bool
17766
elf32_arm_output_plt_map_1 (output_arch_syminfo *osi,
17767
          bool is_iplt_entry_p,
17768
          union gotplt_union *root_plt,
17769
          struct arm_plt_info *arm_plt)
17770
0
{
17771
0
  struct elf32_arm_link_hash_table *htab;
17772
0
  bfd_vma addr, plt_header_size;
17773
17774
0
  if (root_plt->offset == (bfd_vma) -1)
17775
0
    return true;
17776
17777
0
  htab = elf32_arm_hash_table (osi->info);
17778
0
  if (htab == NULL)
17779
0
    return false;
17780
17781
0
  if (is_iplt_entry_p)
17782
0
    {
17783
0
      osi->sec = htab->root.iplt;
17784
0
      plt_header_size = 0;
17785
0
    }
17786
0
  else
17787
0
    {
17788
0
      osi->sec = htab->root.splt;
17789
0
      plt_header_size = htab->plt_header_size;
17790
0
    }
17791
0
  osi->sec_shndx = (_bfd_elf_section_from_bfd_section
17792
0
        (osi->info->output_bfd, osi->sec->output_section));
17793
17794
0
  addr = root_plt->offset & -2;
17795
0
  if (htab->root.target_os == is_vxworks)
17796
0
    {
17797
0
      if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
17798
0
  return false;
17799
0
      if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 8))
17800
0
  return false;
17801
0
      if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr + 12))
17802
0
  return false;
17803
0
      if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 20))
17804
0
  return false;
17805
0
    }
17806
0
  else if (htab->fdpic_p)
17807
0
    {
17808
0
      enum map_symbol_type type = using_thumb_only (htab)
17809
0
  ? ARM_MAP_THUMB
17810
0
  : ARM_MAP_ARM;
17811
17812
0
      if (elf32_arm_plt_needs_thumb_stub_p (osi->info, arm_plt))
17813
0
  if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr - 4))
17814
0
    return false;
17815
0
      if (!elf32_arm_output_map_sym (osi, type, addr))
17816
0
  return false;
17817
0
      if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 16))
17818
0
  return false;
17819
0
      if (htab->plt_entry_size == 4 * ARRAY_SIZE (elf32_arm_fdpic_plt_entry))
17820
0
  if (!elf32_arm_output_map_sym (osi, type, addr + 24))
17821
0
    return false;
17822
0
    }
17823
0
  else if (using_thumb_only (htab))
17824
0
    {
17825
0
      if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr))
17826
0
  return false;
17827
0
    }
17828
0
  else
17829
0
    {
17830
0
      bool thumb_stub_p;
17831
17832
0
      thumb_stub_p = elf32_arm_plt_needs_thumb_stub_p (osi->info, arm_plt);
17833
0
      if (thumb_stub_p)
17834
0
  {
17835
0
    if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr - 4))
17836
0
      return false;
17837
0
  }
17838
#ifdef FOUR_WORD_PLT
17839
      if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
17840
  return false;
17841
      if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 12))
17842
  return false;
17843
#else
17844
      /* A three-word PLT with no Thumb thunk contains only Arm code,
17845
   so only need to output a mapping symbol for the first PLT entry and
17846
   entries with thumb thunks.  */
17847
0
      if (thumb_stub_p || addr == plt_header_size)
17848
0
  {
17849
0
    if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
17850
0
      return false;
17851
0
  }
17852
0
#endif
17853
0
    }
17854
17855
0
  return true;
17856
0
}
17857
17858
/* Output mapping symbols for PLT entries associated with H.  */
17859
17860
static bool
17861
elf32_arm_output_plt_map (struct elf_link_hash_entry *h, void *inf)
17862
0
{
17863
0
  output_arch_syminfo *osi = (output_arch_syminfo *) inf;
17864
0
  struct elf32_arm_link_hash_entry *eh;
17865
17866
0
  if (h->root.type == bfd_link_hash_indirect)
17867
0
    return true;
17868
17869
0
  if (h->root.type == bfd_link_hash_warning)
17870
    /* When warning symbols are created, they **replace** the "real"
17871
       entry in the hash table, thus we never get to see the real
17872
       symbol in a hash traversal.  So look at it now.  */
17873
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
17874
17875
0
  eh = (struct elf32_arm_link_hash_entry *) h;
17876
0
  return elf32_arm_output_plt_map_1 (osi, SYMBOL_CALLS_LOCAL (osi->info, h),
17877
0
             &h->plt, &eh->plt);
17878
0
}
17879
17880
/* Bind a veneered symbol to its veneer identified by its hash entry
17881
   STUB_ENTRY.  The veneered location thus loose its symbol.  */
17882
17883
static void
17884
arm_stub_claim_sym (struct elf32_arm_stub_hash_entry *stub_entry)
17885
0
{
17886
0
  struct elf32_arm_link_hash_entry *hash = stub_entry->h;
17887
17888
0
  BFD_ASSERT (hash);
17889
0
  hash->root.root.u.def.section = stub_entry->stub_sec;
17890
0
  hash->root.root.u.def.value = stub_entry->stub_offset;
17891
0
  hash->root.size = stub_entry->stub_size;
17892
0
}
17893
17894
/* Output a single local symbol for a generated stub.  */
17895
17896
static bool
17897
elf32_arm_output_stub_sym (output_arch_syminfo *osi, const char *name,
17898
         bfd_vma offset, bfd_vma size)
17899
0
{
17900
0
  Elf_Internal_Sym sym;
17901
17902
0
  sym.st_value = osi->sec->output_section->vma
17903
0
     + osi->sec->output_offset
17904
0
     + offset;
17905
0
  sym.st_size = size;
17906
0
  sym.st_other = 0;
17907
0
  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
17908
0
  sym.st_shndx = osi->sec_shndx;
17909
0
  sym.st_target_internal = ST_BRANCH_TO_ARM;
17910
0
  return osi->func (osi->flaginfo, name, &sym, osi->sec, NULL) == 1;
17911
0
}
17912
17913
static bool
17914
arm_map_one_stub (struct bfd_hash_entry * gen_entry,
17915
      void * in_arg)
17916
0
{
17917
0
  struct elf32_arm_stub_hash_entry *stub_entry;
17918
0
  asection *stub_sec;
17919
0
  bfd_vma addr;
17920
0
  char *stub_name;
17921
0
  output_arch_syminfo *osi;
17922
0
  const insn_sequence *template_sequence;
17923
0
  enum stub_insn_type prev_type;
17924
0
  int size;
17925
0
  int i;
17926
0
  enum map_symbol_type sym_type;
17927
17928
  /* Massage our args to the form they really have.  */
17929
0
  stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
17930
0
  osi = (output_arch_syminfo *) in_arg;
17931
17932
0
  stub_sec = stub_entry->stub_sec;
17933
17934
  /* Ensure this stub is attached to the current section being
17935
     processed.  */
17936
0
  if (stub_sec != osi->sec)
17937
0
    return true;
17938
17939
0
  addr = (bfd_vma) stub_entry->stub_offset;
17940
0
  template_sequence = stub_entry->stub_template;
17941
17942
0
  if (arm_stub_sym_claimed (stub_entry->stub_type))
17943
0
    arm_stub_claim_sym (stub_entry);
17944
0
  else
17945
0
    {
17946
0
      stub_name = stub_entry->output_name;
17947
0
      switch (template_sequence[0].type)
17948
0
  {
17949
0
  case ARM_TYPE:
17950
0
    if (!elf32_arm_output_stub_sym (osi, stub_name, addr,
17951
0
            stub_entry->stub_size))
17952
0
      return false;
17953
0
    break;
17954
0
  case THUMB16_TYPE:
17955
0
  case THUMB32_TYPE:
17956
0
    if (!elf32_arm_output_stub_sym (osi, stub_name, addr | 1,
17957
0
            stub_entry->stub_size))
17958
0
      return false;
17959
0
    break;
17960
0
  default:
17961
0
    BFD_FAIL ();
17962
0
    return 0;
17963
0
  }
17964
0
    }
17965
17966
0
  prev_type = DATA_TYPE;
17967
0
  size = 0;
17968
0
  for (i = 0; i < stub_entry->stub_template_size; i++)
17969
0
    {
17970
0
      switch (template_sequence[i].type)
17971
0
  {
17972
0
  case ARM_TYPE:
17973
0
    sym_type = ARM_MAP_ARM;
17974
0
    break;
17975
17976
0
  case THUMB16_TYPE:
17977
0
  case THUMB32_TYPE:
17978
0
    sym_type = ARM_MAP_THUMB;
17979
0
    break;
17980
17981
0
  case DATA_TYPE:
17982
0
    sym_type = ARM_MAP_DATA;
17983
0
    break;
17984
17985
0
  default:
17986
0
    BFD_FAIL ();
17987
0
    return false;
17988
0
  }
17989
17990
0
      if (template_sequence[i].type != prev_type)
17991
0
  {
17992
0
    prev_type = template_sequence[i].type;
17993
0
    if (!elf32_arm_output_map_sym (osi, sym_type, addr + size))
17994
0
      return false;
17995
0
  }
17996
17997
0
      switch (template_sequence[i].type)
17998
0
  {
17999
0
  case ARM_TYPE:
18000
0
  case THUMB32_TYPE:
18001
0
    size += 4;
18002
0
    break;
18003
18004
0
  case THUMB16_TYPE:
18005
0
    size += 2;
18006
0
    break;
18007
18008
0
  case DATA_TYPE:
18009
0
    size += 4;
18010
0
    break;
18011
18012
0
  default:
18013
0
    BFD_FAIL ();
18014
0
    return false;
18015
0
  }
18016
0
    }
18017
18018
0
  return true;
18019
0
}
18020
18021
/* Output mapping symbols for linker generated sections,
18022
   and for those data-only sections that do not have a
18023
   $d.  */
18024
18025
static bool
18026
elf32_arm_output_arch_local_syms (bfd *output_bfd,
18027
          struct bfd_link_info *info,
18028
          void *flaginfo,
18029
          int (*func) (void *, const char *,
18030
                 Elf_Internal_Sym *,
18031
                 asection *,
18032
                 struct elf_link_hash_entry *))
18033
0
{
18034
0
  output_arch_syminfo osi;
18035
0
  struct elf32_arm_link_hash_table *htab;
18036
0
  bfd_vma offset;
18037
0
  bfd_size_type size;
18038
0
  bfd *input_bfd;
18039
18040
0
  if (info->strip == strip_all
18041
0
      && !info->emitrelocations
18042
0
      && !bfd_link_relocatable (info))
18043
0
    return true;
18044
18045
0
  htab = elf32_arm_hash_table (info);
18046
0
  if (htab == NULL)
18047
0
    return false;
18048
18049
0
  check_use_blx (htab);
18050
18051
0
  osi.flaginfo = flaginfo;
18052
0
  osi.info = info;
18053
0
  osi.func = func;
18054
18055
  /* Add a $d mapping symbol to data-only sections that
18056
     don't have any mapping symbol.  This may result in (harmless) redundant
18057
     mapping symbols.  */
18058
0
  for (input_bfd = info->input_bfds;
18059
0
       input_bfd != NULL;
18060
0
       input_bfd = input_bfd->link.next)
18061
0
    {
18062
0
      if ((input_bfd->flags & (BFD_LINKER_CREATED | HAS_SYMS)) == HAS_SYMS)
18063
0
  for (osi.sec = input_bfd->sections;
18064
0
       osi.sec != NULL;
18065
0
       osi.sec = osi.sec->next)
18066
0
    {
18067
0
      if (osi.sec->output_section != NULL
18068
0
    && ((osi.sec->output_section->flags & (SEC_ALLOC | SEC_CODE))
18069
0
        != 0)
18070
0
    && (osi.sec->flags & (SEC_HAS_CONTENTS | SEC_LINKER_CREATED))
18071
0
       == SEC_HAS_CONTENTS
18072
0
    && get_arm_elf_section_data (osi.sec) != NULL
18073
0
    && get_arm_elf_section_data (osi.sec)->mapcount == 0
18074
0
    && osi.sec->size > 0
18075
0
    && (osi.sec->flags & SEC_EXCLUDE) == 0)
18076
0
        {
18077
0
    osi.sec_shndx = _bfd_elf_section_from_bfd_section
18078
0
      (output_bfd, osi.sec->output_section);
18079
0
    if (osi.sec_shndx != (int)SHN_BAD)
18080
0
      elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 0);
18081
0
        }
18082
0
    }
18083
0
    }
18084
18085
  /* ARM->Thumb glue.  */
18086
0
  if (htab->arm_glue_size > 0)
18087
0
    {
18088
0
      osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
18089
0
          ARM2THUMB_GLUE_SECTION_NAME);
18090
18091
0
      osi.sec_shndx = _bfd_elf_section_from_bfd_section
18092
0
    (output_bfd, osi.sec->output_section);
18093
0
      if (bfd_link_pic (info)
18094
0
    || htab->pic_veneer)
18095
0
  size = ARM2THUMB_PIC_GLUE_SIZE;
18096
0
      else if (htab->use_blx)
18097
0
  size = ARM2THUMB_V5_STATIC_GLUE_SIZE;
18098
0
      else
18099
0
  size = ARM2THUMB_STATIC_GLUE_SIZE;
18100
18101
0
      for (offset = 0; offset < htab->arm_glue_size; offset += size)
18102
0
  {
18103
0
    elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, offset);
18104
0
    elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, offset + size - 4);
18105
0
  }
18106
0
    }
18107
18108
  /* Thumb->ARM glue.  */
18109
0
  if (htab->thumb_glue_size > 0)
18110
0
    {
18111
0
      osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
18112
0
          THUMB2ARM_GLUE_SECTION_NAME);
18113
18114
0
      osi.sec_shndx = _bfd_elf_section_from_bfd_section
18115
0
    (output_bfd, osi.sec->output_section);
18116
0
      size = THUMB2ARM_GLUE_SIZE;
18117
18118
0
      for (offset = 0; offset < htab->thumb_glue_size; offset += size)
18119
0
  {
18120
0
    elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, offset);
18121
0
    elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, offset + 4);
18122
0
  }
18123
0
    }
18124
18125
  /* ARMv4 BX veneers.  */
18126
0
  if (htab->bx_glue_size > 0)
18127
0
    {
18128
0
      osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
18129
0
          ARM_BX_GLUE_SECTION_NAME);
18130
18131
0
      osi.sec_shndx = _bfd_elf_section_from_bfd_section
18132
0
    (output_bfd, osi.sec->output_section);
18133
18134
0
      elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0);
18135
0
    }
18136
18137
  /* Long calls stubs.  */
18138
0
  if (htab->stub_bfd && htab->stub_bfd->sections)
18139
0
    {
18140
0
      asection* stub_sec;
18141
18142
0
      for (stub_sec = htab->stub_bfd->sections;
18143
0
     stub_sec != NULL;
18144
0
     stub_sec = stub_sec->next)
18145
0
  {
18146
    /* Ignore non-stub sections.  */
18147
0
    if (!strstr (stub_sec->name, STUB_SUFFIX))
18148
0
      continue;
18149
18150
0
    osi.sec = stub_sec;
18151
18152
0
    osi.sec_shndx = _bfd_elf_section_from_bfd_section
18153
0
      (output_bfd, osi.sec->output_section);
18154
18155
0
    bfd_hash_traverse (&htab->stub_hash_table, arm_map_one_stub, &osi);
18156
0
  }
18157
0
    }
18158
18159
  /* Finally, output mapping symbols for the PLT.  */
18160
0
  if (htab->root.splt && htab->root.splt->size > 0)
18161
0
    {
18162
0
      osi.sec = htab->root.splt;
18163
0
      osi.sec_shndx = (_bfd_elf_section_from_bfd_section
18164
0
           (output_bfd, osi.sec->output_section));
18165
18166
      /* Output mapping symbols for the plt header.  */
18167
0
      if (htab->root.target_os == is_vxworks)
18168
0
  {
18169
    /* VxWorks shared libraries have no PLT header.  */
18170
0
    if (!bfd_link_pic (info))
18171
0
      {
18172
0
        if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
18173
0
    return false;
18174
0
        if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 12))
18175
0
    return false;
18176
0
      }
18177
0
  }
18178
0
      else if (using_thumb_only (htab) && !htab->fdpic_p)
18179
0
  {
18180
0
    if (!elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, 0))
18181
0
      return false;
18182
0
    if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 12))
18183
0
      return false;
18184
0
    if (!elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, 16))
18185
0
      return false;
18186
0
  }
18187
0
      else if (!htab->fdpic_p)
18188
0
  {
18189
0
    if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
18190
0
      return false;
18191
0
#ifndef FOUR_WORD_PLT
18192
0
    if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 16))
18193
0
      return false;
18194
0
#endif
18195
0
  }
18196
0
    }
18197
0
  if ((htab->root.splt && htab->root.splt->size > 0)
18198
0
      || (htab->root.iplt && htab->root.iplt->size > 0))
18199
0
    {
18200
0
      elf_link_hash_traverse (&htab->root, elf32_arm_output_plt_map, &osi);
18201
0
      for (input_bfd = info->input_bfds;
18202
0
     input_bfd != NULL;
18203
0
     input_bfd = input_bfd->link.next)
18204
0
  {
18205
0
    struct arm_local_iplt_info **local_iplt;
18206
0
    unsigned int i, num_syms;
18207
18208
0
    if (!is_arm_elf (input_bfd))
18209
0
      continue;
18210
18211
0
    local_iplt = elf32_arm_local_iplt (input_bfd);
18212
0
    if (local_iplt != NULL)
18213
0
      {
18214
0
        num_syms = elf_symtab_hdr (input_bfd).sh_info;
18215
0
        if (num_syms > elf32_arm_num_entries (input_bfd))
18216
0
    {
18217
0
      _bfd_error_handler (_("\
18218
0
%pB: Number of symbols in input file has increased from %lu to %u\n"),
18219
0
              input_bfd,
18220
0
              (unsigned long) elf32_arm_num_entries (input_bfd),
18221
0
              num_syms);
18222
0
      return false;
18223
0
    }
18224
0
        for (i = 0; i < num_syms; i++)
18225
0
    if (local_iplt[i] != NULL
18226
0
        && !elf32_arm_output_plt_map_1 (&osi, true,
18227
0
                &local_iplt[i]->root,
18228
0
                &local_iplt[i]->arm))
18229
0
      return false;
18230
0
      }
18231
0
  }
18232
0
    }
18233
0
  if (htab->root.tlsdesc_plt != 0)
18234
0
    {
18235
      /* Mapping symbols for the lazy tls trampoline.  */
18236
0
      if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM,
18237
0
             htab->root.tlsdesc_plt))
18238
0
  return false;
18239
18240
0
      if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA,
18241
0
             htab->root.tlsdesc_plt + 24))
18242
0
  return false;
18243
0
    }
18244
0
  if (htab->tls_trampoline != 0)
18245
0
    {
18246
      /* Mapping symbols for the tls trampoline.  */
18247
0
      if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, htab->tls_trampoline))
18248
0
  return false;
18249
#ifdef FOUR_WORD_PLT
18250
      if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA,
18251
             htab->tls_trampoline + 12))
18252
  return false;
18253
#endif
18254
0
    }
18255
18256
0
  return true;
18257
0
}
18258
18259
/* Filter normal symbols of CMSE entry functions of ABFD to include in
18260
   the import library.  All SYMCOUNT symbols of ABFD can be examined
18261
   from their pointers in SYMS.  Pointers of symbols to keep should be
18262
   stored continuously at the beginning of that array.
18263
18264
   Returns the number of symbols to keep.  */
18265
18266
static unsigned int
18267
elf32_arm_filter_cmse_symbols (bfd *abfd ATTRIBUTE_UNUSED,
18268
             struct bfd_link_info *info,
18269
             asymbol **syms, long symcount)
18270
0
{
18271
0
  size_t maxnamelen;
18272
0
  char *cmse_name;
18273
0
  long src_count, dst_count = 0;
18274
0
  struct elf32_arm_link_hash_table *htab;
18275
18276
0
  htab = elf32_arm_hash_table (info);
18277
0
  if (!htab->stub_bfd || !htab->stub_bfd->sections)
18278
0
    symcount = 0;
18279
18280
0
  maxnamelen = 128;
18281
0
  cmse_name = (char *) bfd_malloc (maxnamelen);
18282
0
  BFD_ASSERT (cmse_name);
18283
18284
0
  for (src_count = 0; src_count < symcount; src_count++)
18285
0
    {
18286
0
      struct elf32_arm_link_hash_entry *cmse_hash;
18287
0
      asymbol *sym;
18288
0
      flagword flags;
18289
0
      char *name;
18290
0
      size_t namelen;
18291
18292
0
      sym = syms[src_count];
18293
0
      flags = sym->flags;
18294
0
      name = (char *) bfd_asymbol_name (sym);
18295
18296
0
      if ((flags & BSF_FUNCTION) != BSF_FUNCTION)
18297
0
  continue;
18298
0
      if (!(flags & (BSF_GLOBAL | BSF_WEAK)))
18299
0
  continue;
18300
18301
0
      namelen = strlen (name) + sizeof (CMSE_PREFIX) + 1;
18302
0
      if (namelen > maxnamelen)
18303
0
  {
18304
0
    cmse_name = (char *)
18305
0
      bfd_realloc (cmse_name, namelen);
18306
0
    maxnamelen = namelen;
18307
0
  }
18308
0
      snprintf (cmse_name, maxnamelen, "%s%s", CMSE_PREFIX, name);
18309
0
      cmse_hash = (struct elf32_arm_link_hash_entry *)
18310
0
  elf_link_hash_lookup (&(htab)->root, cmse_name, false, false, true);
18311
18312
0
      if (!cmse_hash
18313
0
    || (cmse_hash->root.root.type != bfd_link_hash_defined
18314
0
        && cmse_hash->root.root.type != bfd_link_hash_defweak)
18315
0
    || cmse_hash->root.type != STT_FUNC)
18316
0
  continue;
18317
18318
0
      syms[dst_count++] = sym;
18319
0
    }
18320
0
  free (cmse_name);
18321
18322
0
  syms[dst_count] = NULL;
18323
18324
0
  return dst_count;
18325
0
}
18326
18327
/* Filter symbols of ABFD to include in the import library.  All
18328
   SYMCOUNT symbols of ABFD can be examined from their pointers in
18329
   SYMS.  Pointers of symbols to keep should be stored continuously at
18330
   the beginning of that array.
18331
18332
   Returns the number of symbols to keep.  */
18333
18334
static unsigned int
18335
elf32_arm_filter_implib_symbols (bfd *abfd ATTRIBUTE_UNUSED,
18336
         struct bfd_link_info *info,
18337
         asymbol **syms, long symcount)
18338
0
{
18339
0
  struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
18340
18341
  /* Requirement 8 of "ARM v8-M Security Extensions: Requirements on
18342
     Development Tools" (ARM-ECM-0359818) mandates Secure Gateway import
18343
     library to be a relocatable object file.  */
18344
0
  BFD_ASSERT (!(bfd_get_file_flags (info->out_implib_bfd) & EXEC_P));
18345
0
  if (globals->cmse_implib)
18346
0
    return elf32_arm_filter_cmse_symbols (abfd, info, syms, symcount);
18347
0
  else
18348
0
    return _bfd_elf_filter_global_symbols (abfd, info, syms, symcount);
18349
0
}
18350
18351
/* Allocate target specific section data.  */
18352
18353
static bool
18354
elf32_arm_new_section_hook (bfd *abfd, asection *sec)
18355
7.27k
{
18356
7.27k
  _arm_elf_section_data *sdata = bfd_zalloc (abfd, sizeof (*sdata));
18357
7.27k
  if (sdata == NULL)
18358
0
    return false;
18359
7.27k
  sec->used_by_bfd = sdata;
18360
18361
7.27k
  return _bfd_elf_new_section_hook (abfd, sec);
18362
7.27k
}
18363
18364
18365
/* Used to order a list of mapping symbols by address.  */
18366
18367
static int
18368
elf32_arm_compare_mapping (const void * a, const void * b)
18369
0
{
18370
0
  const elf32_arm_section_map *amap = (const elf32_arm_section_map *) a;
18371
0
  const elf32_arm_section_map *bmap = (const elf32_arm_section_map *) b;
18372
18373
0
  if (amap->vma > bmap->vma)
18374
0
    return 1;
18375
0
  else if (amap->vma < bmap->vma)
18376
0
    return -1;
18377
0
  else if (amap->type > bmap->type)
18378
    /* Ensure results do not depend on the host qsort for objects with
18379
       multiple mapping symbols at the same address by sorting on type
18380
       after vma.  */
18381
0
    return 1;
18382
0
  else if (amap->type < bmap->type)
18383
0
    return -1;
18384
0
  else
18385
0
    return 0;
18386
0
}
18387
18388
/* Add OFFSET to lower 31 bits of ADDR, leaving other bits unmodified.  */
18389
18390
static unsigned long
18391
offset_prel31 (unsigned long addr, bfd_vma offset)
18392
0
{
18393
0
  return (addr & ~0x7ffffffful) | ((addr + offset) & 0x7ffffffful);
18394
0
}
18395
18396
/* Copy an .ARM.exidx table entry, adding OFFSET to (applied) PREL31
18397
   relocations.  */
18398
18399
static void
18400
copy_exidx_entry (bfd *output_bfd, bfd_byte *to, bfd_byte *from, bfd_vma offset)
18401
0
{
18402
0
  unsigned long first_word = bfd_get_32 (output_bfd, from);
18403
0
  unsigned long second_word = bfd_get_32 (output_bfd, from + 4);
18404
18405
  /* High bit of first word is supposed to be zero.  */
18406
0
  if ((first_word & 0x80000000ul) == 0)
18407
0
    first_word = offset_prel31 (first_word, offset);
18408
18409
  /* If the high bit of the first word is clear, and the bit pattern is not 0x1
18410
     (EXIDX_CANTUNWIND), this is an offset to an .ARM.extab entry.  */
18411
0
  if ((second_word != 0x1) && ((second_word & 0x80000000ul) == 0))
18412
0
    second_word = offset_prel31 (second_word, offset);
18413
18414
0
  bfd_put_32 (output_bfd, first_word, to);
18415
0
  bfd_put_32 (output_bfd, second_word, to + 4);
18416
0
}
18417
18418
/* Data for make_branch_to_a8_stub().  */
18419
18420
struct a8_branch_to_stub_data
18421
{
18422
  asection *writing_section;
18423
  bfd_byte *contents;
18424
};
18425
18426
18427
/* Helper to insert branches to Cortex-A8 erratum stubs in the right
18428
   places for a particular section.  */
18429
18430
static bool
18431
make_branch_to_a8_stub (struct bfd_hash_entry *gen_entry,
18432
           void *in_arg)
18433
0
{
18434
0
  struct elf32_arm_stub_hash_entry *stub_entry;
18435
0
  struct a8_branch_to_stub_data *data;
18436
0
  bfd_byte *contents;
18437
0
  unsigned long branch_insn;
18438
0
  bfd_vma veneered_insn_loc, veneer_entry_loc;
18439
0
  bfd_signed_vma branch_offset;
18440
0
  bfd *abfd;
18441
0
  unsigned int loc;
18442
18443
0
  stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
18444
0
  data = (struct a8_branch_to_stub_data *) in_arg;
18445
18446
0
  if (stub_entry->target_section != data->writing_section
18447
0
      || stub_entry->stub_type < arm_stub_a8_veneer_lwm)
18448
0
    return true;
18449
18450
0
  contents = data->contents;
18451
18452
  /* We use target_section as Cortex-A8 erratum workaround stubs are only
18453
     generated when both source and target are in the same section.  */
18454
0
  veneered_insn_loc = stub_entry->target_section->output_section->vma
18455
0
          + stub_entry->target_section->output_offset
18456
0
          + stub_entry->source_value;
18457
18458
0
  veneer_entry_loc = stub_entry->stub_sec->output_section->vma
18459
0
         + stub_entry->stub_sec->output_offset
18460
0
         + stub_entry->stub_offset;
18461
18462
0
  if (stub_entry->stub_type == arm_stub_a8_veneer_blx)
18463
0
    veneered_insn_loc &= ~3u;
18464
18465
0
  branch_offset = veneer_entry_loc - veneered_insn_loc - 4;
18466
18467
0
  abfd = stub_entry->target_section->owner;
18468
0
  loc = stub_entry->source_value;
18469
18470
  /* We attempt to avoid this condition by setting stubs_always_after_branch
18471
     in elf32_arm_size_stubs if we've enabled the Cortex-A8 erratum workaround.
18472
     This check is just to be on the safe side...  */
18473
0
  if ((veneered_insn_loc & ~0xfff) == (veneer_entry_loc & ~0xfff))
18474
0
    {
18475
0
      _bfd_error_handler (_("%pB: error: Cortex-A8 erratum stub is "
18476
0
          "allocated in unsafe location"), abfd);
18477
0
      return false;
18478
0
    }
18479
18480
0
  switch (stub_entry->stub_type)
18481
0
    {
18482
0
    case arm_stub_a8_veneer_b:
18483
0
    case arm_stub_a8_veneer_b_cond:
18484
0
      branch_insn = 0xf0009000;
18485
0
      goto jump24;
18486
18487
0
    case arm_stub_a8_veneer_blx:
18488
0
      branch_insn = 0xf000e800;
18489
0
      goto jump24;
18490
18491
0
    case arm_stub_a8_veneer_bl:
18492
0
      {
18493
0
  unsigned int i1, j1, i2, j2, s;
18494
18495
0
  branch_insn = 0xf000d000;
18496
18497
0
      jump24:
18498
0
  if (branch_offset < -16777216 || branch_offset > 16777214)
18499
0
    {
18500
      /* There's not much we can do apart from complain if this
18501
         happens.  */
18502
0
      _bfd_error_handler (_("%pB: error: Cortex-A8 erratum stub out "
18503
0
          "of range (input file too large)"), abfd);
18504
0
      return false;
18505
0
    }
18506
18507
  /* i1 = not(j1 eor s), so:
18508
     not i1 = j1 eor s
18509
     j1 = (not i1) eor s.  */
18510
18511
0
  branch_insn |= (branch_offset >> 1) & 0x7ff;
18512
0
  branch_insn |= ((branch_offset >> 12) & 0x3ff) << 16;
18513
0
  i2 = (branch_offset >> 22) & 1;
18514
0
  i1 = (branch_offset >> 23) & 1;
18515
0
  s = (branch_offset >> 24) & 1;
18516
0
  j1 = (!i1) ^ s;
18517
0
  j2 = (!i2) ^ s;
18518
0
  branch_insn |= j2 << 11;
18519
0
  branch_insn |= j1 << 13;
18520
0
  branch_insn |= s << 26;
18521
0
      }
18522
0
      break;
18523
18524
0
    default:
18525
0
      BFD_FAIL ();
18526
0
      return false;
18527
0
    }
18528
18529
0
  bfd_put_16 (abfd, (branch_insn >> 16) & 0xffff, &contents[loc]);
18530
0
  bfd_put_16 (abfd, branch_insn & 0xffff, &contents[loc + 2]);
18531
18532
0
  return true;
18533
0
}
18534
18535
/* Beginning of stm32l4xx work-around.  */
18536
18537
/* Functions encoding instructions necessary for the emission of the
18538
   fix-stm32l4xx-629360.
18539
   Encoding is extracted from the
18540
   ARM (C) Architecture Reference Manual
18541
   ARMv7-A and ARMv7-R edition
18542
   ARM DDI 0406C.b (ID072512).  */
18543
18544
static inline bfd_vma
18545
create_instruction_branch_absolute (int branch_offset)
18546
0
{
18547
  /* A8.8.18 B (A8-334)
18548
     B target_address (Encoding T4).  */
18549
  /* 1111 - 0Sii - iiii - iiii - 10J1 - Jiii - iiii - iiii.  */
18550
  /* jump offset is:  S:I1:I2:imm10:imm11:0.  */
18551
  /* with : I1 = NOT (J1 EOR S) I2 = NOT (J2 EOR S).  */
18552
18553
0
  int s = ((branch_offset & 0x1000000) >> 24);
18554
0
  int j1 = s ^ !((branch_offset & 0x800000) >> 23);
18555
0
  int j2 = s ^ !((branch_offset & 0x400000) >> 22);
18556
18557
0
  if (branch_offset < -(1 << 24) || branch_offset >= (1 << 24))
18558
0
    BFD_ASSERT (0 && "Error: branch out of range.  Cannot create branch.");
18559
18560
0
  bfd_vma patched_inst = 0xf0009000
18561
0
    | s << 26 /* S.  */
18562
0
    | (((unsigned long) (branch_offset) >> 12) & 0x3ff) << 16 /* imm10.  */
18563
0
    | j1 << 13 /* J1.  */
18564
0
    | j2 << 11 /* J2.  */
18565
0
    | (((unsigned long) (branch_offset) >> 1) & 0x7ff); /* imm11.  */
18566
18567
0
  return patched_inst;
18568
0
}
18569
18570
static inline bfd_vma
18571
create_instruction_ldmia (int base_reg, int wback, int reg_mask)
18572
0
{
18573
  /* A8.8.57 LDM/LDMIA/LDMFD (A8-396)
18574
     LDMIA Rn!, {Ra, Rb, Rc, ...} (Encoding T2).  */
18575
0
  bfd_vma patched_inst = 0xe8900000
18576
0
    | (/*W=*/wback << 21)
18577
0
    | (base_reg << 16)
18578
0
    | (reg_mask & 0x0000ffff);
18579
18580
0
  return patched_inst;
18581
0
}
18582
18583
static inline bfd_vma
18584
create_instruction_ldmdb (int base_reg, int wback, int reg_mask)
18585
0
{
18586
  /* A8.8.60 LDMDB/LDMEA (A8-402)
18587
     LDMDB Rn!, {Ra, Rb, Rc, ...} (Encoding T1).  */
18588
0
  bfd_vma patched_inst = 0xe9100000
18589
0
    | (/*W=*/wback << 21)
18590
0
    | (base_reg << 16)
18591
0
    | (reg_mask & 0x0000ffff);
18592
18593
0
  return patched_inst;
18594
0
}
18595
18596
static inline bfd_vma
18597
create_instruction_mov (int target_reg, int source_reg)
18598
0
{
18599
  /* A8.8.103 MOV (register) (A8-486)
18600
     MOV Rd, Rm (Encoding T1).  */
18601
0
  bfd_vma patched_inst = 0x4600
18602
0
    | (target_reg & 0x7)
18603
0
    | ((target_reg & 0x8) >> 3) << 7
18604
0
    | (source_reg << 3);
18605
18606
0
  return patched_inst;
18607
0
}
18608
18609
static inline bfd_vma
18610
create_instruction_sub (int target_reg, int source_reg, int value)
18611
0
{
18612
  /* A8.8.221 SUB (immediate) (A8-708)
18613
     SUB Rd, Rn, #value (Encoding T3).  */
18614
0
  bfd_vma patched_inst = 0xf1a00000
18615
0
    | (target_reg << 8)
18616
0
    | (source_reg << 16)
18617
0
    | (/*S=*/0 << 20)
18618
0
    | ((value & 0x800) >> 11) << 26
18619
0
    | ((value & 0x700) >>  8) << 12
18620
0
    | (value & 0x0ff);
18621
18622
0
  return patched_inst;
18623
0
}
18624
18625
static inline bfd_vma
18626
create_instruction_vldmia (int base_reg, int is_dp, int wback, int num_words,
18627
         int first_reg)
18628
0
{
18629
  /* A8.8.332 VLDM (A8-922)
18630
     VLMD{MODE} Rn{!}, {list} (Encoding T1 or T2).  */
18631
0
  bfd_vma patched_inst = (is_dp ? 0xec900b00 : 0xec900a00)
18632
0
    | (/*W=*/wback << 21)
18633
0
    | (base_reg << 16)
18634
0
    | (num_words & 0x000000ff)
18635
0
    | (((unsigned)first_reg >> 1) & 0x0000000f) << 12
18636
0
    | (first_reg & 0x00000001) << 22;
18637
18638
0
  return patched_inst;
18639
0
}
18640
18641
static inline bfd_vma
18642
create_instruction_vldmdb (int base_reg, int is_dp, int num_words,
18643
         int first_reg)
18644
0
{
18645
  /* A8.8.332 VLDM (A8-922)
18646
     VLMD{MODE} Rn!, {} (Encoding T1 or T2).  */
18647
0
  bfd_vma patched_inst = (is_dp ? 0xed300b00 : 0xed300a00)
18648
0
    | (base_reg << 16)
18649
0
    | (num_words & 0x000000ff)
18650
0
    | (((unsigned)first_reg >>1 ) & 0x0000000f) << 12
18651
0
    | (first_reg & 0x00000001) << 22;
18652
18653
0
  return patched_inst;
18654
0
}
18655
18656
static inline bfd_vma
18657
create_instruction_udf_w (int value)
18658
0
{
18659
  /* A8.8.247 UDF (A8-758)
18660
     Undefined (Encoding T2).  */
18661
0
  bfd_vma patched_inst = 0xf7f0a000
18662
0
    | (value & 0x00000fff)
18663
0
    | (value & 0x000f0000) << 16;
18664
18665
0
  return patched_inst;
18666
0
}
18667
18668
static inline bfd_vma
18669
create_instruction_udf (int value)
18670
0
{
18671
  /* A8.8.247 UDF (A8-758)
18672
     Undefined (Encoding T1).  */
18673
0
  bfd_vma patched_inst = 0xde00
18674
0
    | (value & 0xff);
18675
18676
0
  return patched_inst;
18677
0
}
18678
18679
/* Functions writing an instruction in memory, returning the next
18680
   memory position to write to.  */
18681
18682
static inline bfd_byte *
18683
push_thumb2_insn32 (struct elf32_arm_link_hash_table * htab,
18684
        bfd * output_bfd, bfd_byte *pt, insn32 insn)
18685
0
{
18686
0
  put_thumb2_insn (htab, output_bfd, insn, pt);
18687
0
  return pt + 4;
18688
0
}
18689
18690
static inline bfd_byte *
18691
push_thumb2_insn16 (struct elf32_arm_link_hash_table * htab,
18692
        bfd * output_bfd, bfd_byte *pt, insn32 insn)
18693
0
{
18694
0
  put_thumb_insn (htab, output_bfd, insn, pt);
18695
0
  return pt + 2;
18696
0
}
18697
18698
/* Function filling up a region in memory with T1 and T2 UDFs taking
18699
   care of alignment.  */
18700
18701
static bfd_byte *
18702
stm32l4xx_fill_stub_udf (struct elf32_arm_link_hash_table * htab,
18703
       bfd *       output_bfd,
18704
       const bfd_byte * const  base_stub_contents,
18705
       bfd_byte * const  from_stub_contents,
18706
       const bfd_byte * const  end_stub_contents)
18707
0
{
18708
0
  bfd_byte *current_stub_contents = from_stub_contents;
18709
18710
  /* Fill the remaining of the stub with deterministic contents : UDF
18711
     instructions.
18712
     Check if realignment is needed on modulo 4 frontier using T1, to
18713
     further use T2.  */
18714
0
  if ((current_stub_contents < end_stub_contents)
18715
0
      && !((current_stub_contents - base_stub_contents) % 2)
18716
0
      && ((current_stub_contents - base_stub_contents) % 4))
18717
0
    current_stub_contents =
18718
0
      push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
18719
0
        create_instruction_udf (0));
18720
18721
0
  for (; current_stub_contents < end_stub_contents;)
18722
0
    current_stub_contents =
18723
0
      push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18724
0
        create_instruction_udf_w (0));
18725
18726
0
  return current_stub_contents;
18727
0
}
18728
18729
/* Functions writing the stream of instructions equivalent to the
18730
   derived sequence for ldmia, ldmdb, vldm respectively.  */
18731
18732
static void
18733
stm32l4xx_create_replacing_stub_ldmia (struct elf32_arm_link_hash_table * htab,
18734
               bfd * output_bfd,
18735
               const insn32 initial_insn,
18736
               const bfd_byte *const initial_insn_addr,
18737
               bfd_byte *const base_stub_contents)
18738
0
{
18739
0
  int wback = (initial_insn & 0x00200000) >> 21;
18740
0
  int ri, rn = (initial_insn & 0x000F0000) >> 16;
18741
0
  int insn_all_registers = initial_insn & 0x0000ffff;
18742
0
  int insn_low_registers, insn_high_registers;
18743
0
  int usable_register_mask;
18744
0
  int nb_registers = elf32_arm_popcount (insn_all_registers);
18745
0
  int restore_pc = (insn_all_registers & (1 << 15)) ? 1 : 0;
18746
0
  int restore_rn = (insn_all_registers & (1 << rn)) ? 1 : 0;
18747
0
  bfd_byte *current_stub_contents = base_stub_contents;
18748
18749
0
  BFD_ASSERT (is_thumb2_ldmia (initial_insn));
18750
18751
  /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
18752
     smaller than 8 registers load sequences that do not cause the
18753
     hardware issue.  */
18754
0
  if (nb_registers <= 8)
18755
0
    {
18756
      /* UNTOUCHED : LDMIA Rn{!}, {R-all-register-list}.  */
18757
0
      current_stub_contents =
18758
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18759
0
          initial_insn);
18760
18761
      /* B initial_insn_addr+4.  */
18762
0
      if (!restore_pc)
18763
0
  current_stub_contents =
18764
0
    push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18765
0
            create_instruction_branch_absolute
18766
0
            (initial_insn_addr - current_stub_contents));
18767
18768
      /* Fill the remaining of the stub with deterministic contents.  */
18769
0
      current_stub_contents =
18770
0
  stm32l4xx_fill_stub_udf (htab, output_bfd,
18771
0
         base_stub_contents, current_stub_contents,
18772
0
         base_stub_contents +
18773
0
         STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
18774
18775
0
      return;
18776
0
    }
18777
18778
  /* - reg_list[13] == 0.  */
18779
0
  BFD_ASSERT ((insn_all_registers & (1 << 13))==0);
18780
18781
  /* - reg_list[14] & reg_list[15] != 1.  */
18782
0
  BFD_ASSERT ((insn_all_registers & 0xC000) != 0xC000);
18783
18784
  /* - if (wback==1) reg_list[rn] == 0.  */
18785
0
  BFD_ASSERT (!wback || !restore_rn);
18786
18787
  /* - nb_registers > 8.  */
18788
0
  BFD_ASSERT (elf32_arm_popcount (insn_all_registers) > 8);
18789
18790
  /* At this point, LDMxx initial insn loads between 9 and 14 registers.  */
18791
18792
  /* In the following algorithm, we split this wide LDM using 2 LDM insns:
18793
    - One with the 7 lowest registers (register mask 0x007F)
18794
      This LDM will finally contain between 2 and 7 registers
18795
    - One with the 7 highest registers (register mask 0xDF80)
18796
      This ldm will finally contain between 2 and 7 registers.  */
18797
0
  insn_low_registers = insn_all_registers & 0x007F;
18798
0
  insn_high_registers = insn_all_registers & 0xDF80;
18799
18800
  /* A spare register may be needed during this veneer to temporarily
18801
     handle the base register.  This register will be restored with the
18802
     last LDM operation.
18803
     The usable register may be any general purpose register (that
18804
     excludes PC, SP, LR : register mask is 0x1FFF).  */
18805
0
  usable_register_mask = 0x1FFF;
18806
18807
  /* Generate the stub function.  */
18808
0
  if (wback)
18809
0
    {
18810
      /* LDMIA Rn!, {R-low-register-list} : (Encoding T2).  */
18811
0
      current_stub_contents =
18812
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18813
0
          create_instruction_ldmia
18814
0
          (rn, /*wback=*/1, insn_low_registers));
18815
18816
      /* LDMIA Rn!, {R-high-register-list} : (Encoding T2).  */
18817
0
      current_stub_contents =
18818
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18819
0
          create_instruction_ldmia
18820
0
          (rn, /*wback=*/1, insn_high_registers));
18821
0
      if (!restore_pc)
18822
0
  {
18823
    /* B initial_insn_addr+4.  */
18824
0
    current_stub_contents =
18825
0
      push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18826
0
        create_instruction_branch_absolute
18827
0
        (initial_insn_addr - current_stub_contents));
18828
0
       }
18829
0
    }
18830
0
  else /* if (!wback).  */
18831
0
    {
18832
0
      ri = rn;
18833
18834
      /* If Rn is not part of the high-register-list, move it there.  */
18835
0
      if (!(insn_high_registers & (1 << rn)))
18836
0
  {
18837
    /* Choose a Ri in the high-register-list that will be restored.  */
18838
0
    ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
18839
18840
    /* MOV Ri, Rn.  */
18841
0
    current_stub_contents =
18842
0
      push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
18843
0
        create_instruction_mov (ri, rn));
18844
0
  }
18845
18846
      /* LDMIA Ri!, {R-low-register-list} : (Encoding T2).  */
18847
0
      current_stub_contents =
18848
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18849
0
          create_instruction_ldmia
18850
0
          (ri, /*wback=*/1, insn_low_registers));
18851
18852
      /* LDMIA Ri, {R-high-register-list} : (Encoding T2).  */
18853
0
      current_stub_contents =
18854
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18855
0
          create_instruction_ldmia
18856
0
          (ri, /*wback=*/0, insn_high_registers));
18857
18858
0
      if (!restore_pc)
18859
0
  {
18860
    /* B initial_insn_addr+4.  */
18861
0
    current_stub_contents =
18862
0
      push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18863
0
        create_instruction_branch_absolute
18864
0
        (initial_insn_addr - current_stub_contents));
18865
0
  }
18866
0
    }
18867
18868
  /* Fill the remaining of the stub with deterministic contents.  */
18869
0
  current_stub_contents =
18870
0
    stm32l4xx_fill_stub_udf (htab, output_bfd,
18871
0
           base_stub_contents, current_stub_contents,
18872
0
           base_stub_contents +
18873
0
           STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
18874
0
}
18875
18876
static void
18877
stm32l4xx_create_replacing_stub_ldmdb (struct elf32_arm_link_hash_table * htab,
18878
               bfd * output_bfd,
18879
               const insn32 initial_insn,
18880
               const bfd_byte *const initial_insn_addr,
18881
               bfd_byte *const base_stub_contents)
18882
0
{
18883
0
  int wback = (initial_insn & 0x00200000) >> 21;
18884
0
  int ri, rn = (initial_insn & 0x000f0000) >> 16;
18885
0
  int insn_all_registers = initial_insn & 0x0000ffff;
18886
0
  int insn_low_registers, insn_high_registers;
18887
0
  int usable_register_mask;
18888
0
  int restore_pc = (insn_all_registers & (1 << 15)) ? 1 : 0;
18889
0
  int restore_rn = (insn_all_registers & (1 << rn)) ? 1 : 0;
18890
0
  int nb_registers = elf32_arm_popcount (insn_all_registers);
18891
0
  bfd_byte *current_stub_contents = base_stub_contents;
18892
18893
0
  BFD_ASSERT (is_thumb2_ldmdb (initial_insn));
18894
18895
  /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
18896
     smaller than 8 registers load sequences that do not cause the
18897
     hardware issue.  */
18898
0
  if (nb_registers <= 8)
18899
0
    {
18900
      /* UNTOUCHED : LDMIA Rn{!}, {R-all-register-list}.  */
18901
0
      current_stub_contents =
18902
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18903
0
          initial_insn);
18904
18905
      /* B initial_insn_addr+4.  */
18906
0
      current_stub_contents =
18907
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18908
0
          create_instruction_branch_absolute
18909
0
          (initial_insn_addr - current_stub_contents));
18910
18911
      /* Fill the remaining of the stub with deterministic contents.  */
18912
0
      current_stub_contents =
18913
0
  stm32l4xx_fill_stub_udf (htab, output_bfd,
18914
0
         base_stub_contents, current_stub_contents,
18915
0
         base_stub_contents +
18916
0
         STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
18917
18918
0
      return;
18919
0
    }
18920
18921
  /* - reg_list[13] == 0.  */
18922
0
  BFD_ASSERT ((insn_all_registers & (1 << 13)) == 0);
18923
18924
  /* - reg_list[14] & reg_list[15] != 1.  */
18925
0
  BFD_ASSERT ((insn_all_registers & 0xC000) != 0xC000);
18926
18927
  /* - if (wback==1) reg_list[rn] == 0.  */
18928
0
  BFD_ASSERT (!wback || !restore_rn);
18929
18930
  /* - nb_registers > 8.  */
18931
0
  BFD_ASSERT (elf32_arm_popcount (insn_all_registers) > 8);
18932
18933
  /* At this point, LDMxx initial insn loads between 9 and 14 registers.  */
18934
18935
  /* In the following algorithm, we split this wide LDM using 2 LDM insn:
18936
    - One with the 7 lowest registers (register mask 0x007F)
18937
      This LDM will finally contain between 2 and 7 registers
18938
    - One with the 7 highest registers (register mask 0xDF80)
18939
      This ldm will finally contain between 2 and 7 registers.  */
18940
0
  insn_low_registers = insn_all_registers & 0x007F;
18941
0
  insn_high_registers = insn_all_registers & 0xDF80;
18942
18943
  /* A spare register may be needed during this veneer to temporarily
18944
     handle the base register.  This register will be restored with
18945
     the last LDM operation.
18946
     The usable register may be any general purpose register (that excludes
18947
     PC, SP, LR : register mask is 0x1FFF).  */
18948
0
  usable_register_mask = 0x1FFF;
18949
18950
  /* Generate the stub function.  */
18951
0
  if (!wback && !restore_pc && !restore_rn)
18952
0
    {
18953
      /* Choose a Ri in the low-register-list that will be restored.  */
18954
0
      ri = ctz (insn_low_registers & usable_register_mask & ~(1 << rn));
18955
18956
      /* MOV Ri, Rn.  */
18957
0
      current_stub_contents =
18958
0
  push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
18959
0
          create_instruction_mov (ri, rn));
18960
18961
      /* LDMDB Ri!, {R-high-register-list}.  */
18962
0
      current_stub_contents =
18963
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18964
0
          create_instruction_ldmdb
18965
0
          (ri, /*wback=*/1, insn_high_registers));
18966
18967
      /* LDMDB Ri, {R-low-register-list}.  */
18968
0
      current_stub_contents =
18969
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18970
0
          create_instruction_ldmdb
18971
0
          (ri, /*wback=*/0, insn_low_registers));
18972
18973
      /* B initial_insn_addr+4.  */
18974
0
      current_stub_contents =
18975
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18976
0
          create_instruction_branch_absolute
18977
0
          (initial_insn_addr - current_stub_contents));
18978
0
    }
18979
0
  else if (wback && !restore_pc && !restore_rn)
18980
0
    {
18981
      /* LDMDB Rn!, {R-high-register-list}.  */
18982
0
      current_stub_contents =
18983
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18984
0
          create_instruction_ldmdb
18985
0
          (rn, /*wback=*/1, insn_high_registers));
18986
18987
      /* LDMDB Rn!, {R-low-register-list}.  */
18988
0
      current_stub_contents =
18989
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18990
0
          create_instruction_ldmdb
18991
0
          (rn, /*wback=*/1, insn_low_registers));
18992
18993
      /* B initial_insn_addr+4.  */
18994
0
      current_stub_contents =
18995
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18996
0
          create_instruction_branch_absolute
18997
0
          (initial_insn_addr - current_stub_contents));
18998
0
    }
18999
0
  else if (!wback && restore_pc && !restore_rn)
19000
0
    {
19001
      /* Choose a Ri in the high-register-list that will be restored.  */
19002
0
      ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
19003
19004
      /* SUB Ri, Rn, #(4*nb_registers).  */
19005
0
      current_stub_contents =
19006
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19007
0
          create_instruction_sub (ri, rn, (4 * nb_registers)));
19008
19009
      /* LDMIA Ri!, {R-low-register-list}.  */
19010
0
      current_stub_contents =
19011
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19012
0
          create_instruction_ldmia
19013
0
          (ri, /*wback=*/1, insn_low_registers));
19014
19015
      /* LDMIA Ri, {R-high-register-list}.  */
19016
0
      current_stub_contents =
19017
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19018
0
          create_instruction_ldmia
19019
0
          (ri, /*wback=*/0, insn_high_registers));
19020
0
    }
19021
0
  else if (wback && restore_pc && !restore_rn)
19022
0
    {
19023
      /* Choose a Ri in the high-register-list that will be restored.  */
19024
0
      ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
19025
19026
      /* SUB Rn, Rn, #(4*nb_registers)  */
19027
0
      current_stub_contents =
19028
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19029
0
          create_instruction_sub (rn, rn, (4 * nb_registers)));
19030
19031
      /* MOV Ri, Rn.  */
19032
0
      current_stub_contents =
19033
0
  push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
19034
0
          create_instruction_mov (ri, rn));
19035
19036
      /* LDMIA Ri!, {R-low-register-list}.  */
19037
0
      current_stub_contents =
19038
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19039
0
          create_instruction_ldmia
19040
0
          (ri, /*wback=*/1, insn_low_registers));
19041
19042
      /* LDMIA Ri, {R-high-register-list}.  */
19043
0
      current_stub_contents =
19044
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19045
0
          create_instruction_ldmia
19046
0
          (ri, /*wback=*/0, insn_high_registers));
19047
0
    }
19048
0
  else if (!wback && !restore_pc && restore_rn)
19049
0
    {
19050
0
      ri = rn;
19051
0
      if (!(insn_low_registers & (1 << rn)))
19052
0
  {
19053
    /* Choose a Ri in the low-register-list that will be restored.  */
19054
0
    ri = ctz (insn_low_registers & usable_register_mask & ~(1 << rn));
19055
19056
    /* MOV Ri, Rn.  */
19057
0
    current_stub_contents =
19058
0
      push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
19059
0
        create_instruction_mov (ri, rn));
19060
0
  }
19061
19062
      /* LDMDB Ri!, {R-high-register-list}.  */
19063
0
      current_stub_contents =
19064
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19065
0
          create_instruction_ldmdb
19066
0
          (ri, /*wback=*/1, insn_high_registers));
19067
19068
      /* LDMDB Ri, {R-low-register-list}.  */
19069
0
      current_stub_contents =
19070
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19071
0
          create_instruction_ldmdb
19072
0
          (ri, /*wback=*/0, insn_low_registers));
19073
19074
      /* B initial_insn_addr+4.  */
19075
0
      current_stub_contents =
19076
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19077
0
          create_instruction_branch_absolute
19078
0
          (initial_insn_addr - current_stub_contents));
19079
0
    }
19080
0
  else if (!wback && restore_pc && restore_rn)
19081
0
    {
19082
0
      ri = rn;
19083
0
      if (!(insn_high_registers & (1 << rn)))
19084
0
  {
19085
    /* Choose a Ri in the high-register-list that will be restored.  */
19086
0
    ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
19087
0
  }
19088
19089
      /* SUB Ri, Rn, #(4*nb_registers).  */
19090
0
      current_stub_contents =
19091
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19092
0
          create_instruction_sub (ri, rn, (4 * nb_registers)));
19093
19094
      /* LDMIA Ri!, {R-low-register-list}.  */
19095
0
      current_stub_contents =
19096
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19097
0
          create_instruction_ldmia
19098
0
          (ri, /*wback=*/1, insn_low_registers));
19099
19100
      /* LDMIA Ri, {R-high-register-list}.  */
19101
0
      current_stub_contents =
19102
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19103
0
          create_instruction_ldmia
19104
0
          (ri, /*wback=*/0, insn_high_registers));
19105
0
    }
19106
0
  else if (wback && restore_rn)
19107
0
    {
19108
      /* The assembler should not have accepted to encode this.  */
19109
0
      BFD_ASSERT (0 && "Cannot patch an instruction that has an "
19110
0
  "undefined behavior.\n");
19111
0
    }
19112
19113
  /* Fill the remaining of the stub with deterministic contents.  */
19114
0
  current_stub_contents =
19115
0
    stm32l4xx_fill_stub_udf (htab, output_bfd,
19116
0
           base_stub_contents, current_stub_contents,
19117
0
           base_stub_contents +
19118
0
           STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
19119
19120
0
}
19121
19122
static void
19123
stm32l4xx_create_replacing_stub_vldm (struct elf32_arm_link_hash_table * htab,
19124
              bfd * output_bfd,
19125
              const insn32 initial_insn,
19126
              const bfd_byte *const initial_insn_addr,
19127
              bfd_byte *const base_stub_contents)
19128
0
{
19129
0
  int num_words = initial_insn & 0xff;
19130
0
  bfd_byte *current_stub_contents = base_stub_contents;
19131
19132
0
  BFD_ASSERT (is_thumb2_vldm (initial_insn));
19133
19134
  /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
19135
     smaller than 8 words load sequences that do not cause the
19136
     hardware issue.  */
19137
0
  if (num_words <= 8)
19138
0
    {
19139
      /* Untouched instruction.  */
19140
0
      current_stub_contents =
19141
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19142
0
          initial_insn);
19143
19144
      /* B initial_insn_addr+4.  */
19145
0
      current_stub_contents =
19146
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19147
0
          create_instruction_branch_absolute
19148
0
          (initial_insn_addr - current_stub_contents));
19149
0
    }
19150
0
  else
19151
0
    {
19152
0
      bool is_dp = /* DP encoding.  */
19153
0
  (initial_insn & 0xfe100f00) == 0xec100b00;
19154
0
      bool is_ia_nobang = /* (IA without !).  */
19155
0
  (((initial_insn << 7) >> 28) & 0xd) == 0x4;
19156
0
      bool is_ia_bang = /* (IA with !) - includes VPOP.  */
19157
0
  (((initial_insn << 7) >> 28) & 0xd) == 0x5;
19158
0
      bool is_db_bang = /* (DB with !).  */
19159
0
  (((initial_insn << 7) >> 28) & 0xd) == 0x9;
19160
0
      int base_reg = ((unsigned int) initial_insn << 12) >> 28;
19161
      /* d = UInt (Vd:D);.  */
19162
0
      int first_reg = ((((unsigned int) initial_insn << 16) >> 28) << 1)
19163
0
  | (((unsigned int)initial_insn << 9) >> 31);
19164
19165
      /* Compute the number of 8-words chunks needed to split.  */
19166
0
      int chunks = (num_words % 8) ? (num_words / 8 + 1) : (num_words / 8);
19167
0
      int chunk;
19168
19169
      /* The test coverage has been done assuming the following
19170
   hypothesis that exactly one of the previous is_ predicates is
19171
   true.  */
19172
0
      BFD_ASSERT (    (is_ia_nobang ^ is_ia_bang ^ is_db_bang)
19173
0
      && !(is_ia_nobang & is_ia_bang & is_db_bang));
19174
19175
      /* We treat the cutting of the words in one pass for all
19176
   cases, then we emit the adjustments:
19177
19178
   vldm rx, {...}
19179
   -> vldm rx!, {8_words_or_less} for each needed 8_word
19180
   -> sub rx, rx, #size (list)
19181
19182
   vldm rx!, {...}
19183
   -> vldm rx!, {8_words_or_less} for each needed 8_word
19184
   This also handles vpop instruction (when rx is sp)
19185
19186
   vldmd rx!, {...}
19187
   -> vldmb rx!, {8_words_or_less} for each needed 8_word.  */
19188
0
      for (chunk = 0; chunk < chunks; ++chunk)
19189
0
  {
19190
0
    bfd_vma new_insn = 0;
19191
19192
0
    if (is_ia_nobang || is_ia_bang)
19193
0
      {
19194
0
        new_insn = create_instruction_vldmia
19195
0
    (base_reg,
19196
0
     is_dp,
19197
0
     /*wback= .  */1,
19198
0
     chunks - (chunk + 1) ?
19199
0
     8 : num_words - chunk * 8,
19200
0
     first_reg + chunk * 8);
19201
0
      }
19202
0
    else if (is_db_bang)
19203
0
      {
19204
0
        new_insn = create_instruction_vldmdb
19205
0
    (base_reg,
19206
0
     is_dp,
19207
0
     chunks - (chunk + 1) ?
19208
0
     8 : num_words - chunk * 8,
19209
0
     first_reg + chunk * 8);
19210
0
      }
19211
19212
0
    if (new_insn)
19213
0
      current_stub_contents =
19214
0
        push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19215
0
          new_insn);
19216
0
  }
19217
19218
      /* Only this case requires the base register compensation
19219
   subtract.  */
19220
0
      if (is_ia_nobang)
19221
0
  {
19222
0
    current_stub_contents =
19223
0
      push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19224
0
        create_instruction_sub
19225
0
        (base_reg, base_reg, 4*num_words));
19226
0
  }
19227
19228
      /* B initial_insn_addr+4.  */
19229
0
      current_stub_contents =
19230
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19231
0
          create_instruction_branch_absolute
19232
0
          (initial_insn_addr - current_stub_contents));
19233
0
    }
19234
19235
  /* Fill the remaining of the stub with deterministic contents.  */
19236
0
  current_stub_contents =
19237
0
    stm32l4xx_fill_stub_udf (htab, output_bfd,
19238
0
           base_stub_contents, current_stub_contents,
19239
0
           base_stub_contents +
19240
0
           STM32L4XX_ERRATUM_VLDM_VENEER_SIZE);
19241
0
}
19242
19243
static void
19244
stm32l4xx_create_replacing_stub (struct elf32_arm_link_hash_table * htab,
19245
         bfd * output_bfd,
19246
         const insn32 wrong_insn,
19247
         const bfd_byte *const wrong_insn_addr,
19248
         bfd_byte *const stub_contents)
19249
0
{
19250
0
  if (is_thumb2_ldmia (wrong_insn))
19251
0
    stm32l4xx_create_replacing_stub_ldmia (htab, output_bfd,
19252
0
             wrong_insn, wrong_insn_addr,
19253
0
             stub_contents);
19254
0
  else if (is_thumb2_ldmdb (wrong_insn))
19255
0
    stm32l4xx_create_replacing_stub_ldmdb (htab, output_bfd,
19256
0
             wrong_insn, wrong_insn_addr,
19257
0
             stub_contents);
19258
0
  else if (is_thumb2_vldm (wrong_insn))
19259
0
    stm32l4xx_create_replacing_stub_vldm (htab, output_bfd,
19260
0
            wrong_insn, wrong_insn_addr,
19261
0
            stub_contents);
19262
0
}
19263
19264
/* End of stm32l4xx work-around.  */
19265
19266
19267
/* Do code byteswapping.  Return FALSE afterwards so that the section is
19268
   written out as normal.  */
19269
19270
static bool
19271
elf32_arm_write_section (bfd *output_bfd,
19272
       struct bfd_link_info *link_info,
19273
       asection *sec,
19274
       bfd_byte *contents)
19275
0
{
19276
0
  unsigned int mapcount, errcount;
19277
0
  _arm_elf_section_data *arm_data;
19278
0
  struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
19279
0
  elf32_arm_section_map *map;
19280
0
  elf32_vfp11_erratum_list *errnode;
19281
0
  elf32_stm32l4xx_erratum_list *stm32l4xx_errnode;
19282
0
  bfd_vma ptr;
19283
0
  bfd_vma end;
19284
0
  bfd_vma offset = sec->output_section->vma + sec->output_offset;
19285
0
  bfd_byte tmp;
19286
0
  unsigned int i;
19287
19288
0
  if (globals == NULL)
19289
0
    return false;
19290
19291
  /* If this section has not been allocated an _arm_elf_section_data
19292
     structure then we cannot record anything.  */
19293
0
  arm_data = get_arm_elf_section_data (sec);
19294
0
  if (arm_data == NULL)
19295
0
    return false;
19296
19297
0
  mapcount = arm_data->mapcount;
19298
0
  map = arm_data->map;
19299
0
  errcount = arm_data->erratumcount;
19300
19301
0
  if (errcount != 0)
19302
0
    {
19303
0
      unsigned int endianflip = bfd_big_endian (output_bfd) ? 3 : 0;
19304
19305
0
      for (errnode = arm_data->erratumlist; errnode != 0;
19306
0
     errnode = errnode->next)
19307
0
  {
19308
0
    bfd_vma target = errnode->vma - offset;
19309
19310
0
    switch (errnode->type)
19311
0
      {
19312
0
      case VFP11_ERRATUM_BRANCH_TO_ARM_VENEER:
19313
0
        {
19314
0
    bfd_vma branch_to_veneer;
19315
    /* Original condition code of instruction, plus bit mask for
19316
       ARM B instruction.  */
19317
0
    unsigned int insn = (errnode->u.b.vfp_insn & 0xf0000000)
19318
0
          | 0x0a000000;
19319
19320
    /* The instruction is before the label.  */
19321
0
    target -= 4;
19322
19323
    /* Above offset included in -4 below.  */
19324
0
    branch_to_veneer = errnode->u.b.veneer->vma
19325
0
           - errnode->vma - 4;
19326
19327
0
    if ((signed) branch_to_veneer < -(1 << 25)
19328
0
        || (signed) branch_to_veneer >= (1 << 25))
19329
0
      _bfd_error_handler (_("%pB: error: VFP11 veneer out of "
19330
0
          "range"), output_bfd);
19331
19332
0
    insn |= (branch_to_veneer >> 2) & 0xffffff;
19333
0
    contents[endianflip ^ target] = insn & 0xff;
19334
0
    contents[endianflip ^ (target + 1)] = (insn >> 8) & 0xff;
19335
0
    contents[endianflip ^ (target + 2)] = (insn >> 16) & 0xff;
19336
0
    contents[endianflip ^ (target + 3)] = (insn >> 24) & 0xff;
19337
0
        }
19338
0
        break;
19339
19340
0
      case VFP11_ERRATUM_ARM_VENEER:
19341
0
        {
19342
0
    bfd_vma branch_from_veneer;
19343
0
    unsigned int insn;
19344
19345
    /* Take size of veneer into account.  */
19346
0
    branch_from_veneer = errnode->u.v.branch->vma
19347
0
             - errnode->vma - 12;
19348
19349
0
    if ((signed) branch_from_veneer < -(1 << 25)
19350
0
        || (signed) branch_from_veneer >= (1 << 25))
19351
0
      _bfd_error_handler (_("%pB: error: VFP11 veneer out of "
19352
0
          "range"), output_bfd);
19353
19354
    /* Original instruction.  */
19355
0
    insn = errnode->u.v.branch->u.b.vfp_insn;
19356
0
    contents[endianflip ^ target] = insn & 0xff;
19357
0
    contents[endianflip ^ (target + 1)] = (insn >> 8) & 0xff;
19358
0
    contents[endianflip ^ (target + 2)] = (insn >> 16) & 0xff;
19359
0
    contents[endianflip ^ (target + 3)] = (insn >> 24) & 0xff;
19360
19361
    /* Branch back to insn after original insn.  */
19362
0
    insn = 0xea000000 | ((branch_from_veneer >> 2) & 0xffffff);
19363
0
    contents[endianflip ^ (target + 4)] = insn & 0xff;
19364
0
    contents[endianflip ^ (target + 5)] = (insn >> 8) & 0xff;
19365
0
    contents[endianflip ^ (target + 6)] = (insn >> 16) & 0xff;
19366
0
    contents[endianflip ^ (target + 7)] = (insn >> 24) & 0xff;
19367
0
        }
19368
0
        break;
19369
19370
0
      default:
19371
0
        abort ();
19372
0
      }
19373
0
  }
19374
0
    }
19375
19376
0
  if (arm_data->stm32l4xx_erratumcount != 0)
19377
0
    {
19378
0
      for (stm32l4xx_errnode = arm_data->stm32l4xx_erratumlist;
19379
0
     stm32l4xx_errnode != 0;
19380
0
     stm32l4xx_errnode = stm32l4xx_errnode->next)
19381
0
  {
19382
0
    bfd_vma target = stm32l4xx_errnode->vma - offset;
19383
19384
0
    switch (stm32l4xx_errnode->type)
19385
0
      {
19386
0
      case STM32L4XX_ERRATUM_BRANCH_TO_VENEER:
19387
0
        {
19388
0
    unsigned int insn;
19389
0
    bfd_vma branch_to_veneer =
19390
0
      stm32l4xx_errnode->u.b.veneer->vma - stm32l4xx_errnode->vma;
19391
19392
0
    if ((signed) branch_to_veneer < -(1 << 24)
19393
0
        || (signed) branch_to_veneer >= (1 << 24))
19394
0
      {
19395
0
        bfd_vma out_of_range =
19396
0
          ((signed) branch_to_veneer < -(1 << 24)) ?
19397
0
          - branch_to_veneer - (1 << 24) :
19398
0
          ((signed) branch_to_veneer >= (1 << 24)) ?
19399
0
          branch_to_veneer - (1 << 24) : 0;
19400
19401
0
        _bfd_error_handler
19402
0
          (_("%pB(%#" PRIx64 "): error: "
19403
0
       "cannot create STM32L4XX veneer; "
19404
0
       "jump out of range by %" PRId64 " bytes; "
19405
0
       "cannot encode branch instruction"),
19406
0
           output_bfd,
19407
0
           (uint64_t) (stm32l4xx_errnode->vma - 4),
19408
0
           (int64_t) out_of_range);
19409
0
        continue;
19410
0
      }
19411
19412
0
    insn = create_instruction_branch_absolute
19413
0
      (stm32l4xx_errnode->u.b.veneer->vma - stm32l4xx_errnode->vma);
19414
19415
    /* The instruction is before the label.  */
19416
0
    target -= 4;
19417
19418
0
    put_thumb2_insn (globals, output_bfd,
19419
0
         (bfd_vma) insn, contents + target);
19420
0
        }
19421
0
        break;
19422
19423
0
      case STM32L4XX_ERRATUM_VENEER:
19424
0
        {
19425
0
    bfd_byte * veneer;
19426
0
    bfd_byte * veneer_r;
19427
0
    unsigned int insn;
19428
19429
0
    veneer = contents + target;
19430
0
    veneer_r = veneer
19431
0
      + stm32l4xx_errnode->u.b.veneer->vma
19432
0
      - stm32l4xx_errnode->vma - 4;
19433
19434
0
    if ((signed) (veneer_r - veneer -
19435
0
            STM32L4XX_ERRATUM_VLDM_VENEER_SIZE >
19436
0
            STM32L4XX_ERRATUM_LDM_VENEER_SIZE ?
19437
0
            STM32L4XX_ERRATUM_VLDM_VENEER_SIZE :
19438
0
            STM32L4XX_ERRATUM_LDM_VENEER_SIZE) < -(1 << 24)
19439
0
        || (signed) (veneer_r - veneer) >= (1 << 24))
19440
0
      {
19441
0
        _bfd_error_handler (_("%pB: error: cannot create STM32L4XX "
19442
0
            "veneer"), output_bfd);
19443
0
         continue;
19444
0
      }
19445
19446
    /* Original instruction.  */
19447
0
    insn = stm32l4xx_errnode->u.v.branch->u.b.insn;
19448
19449
0
    stm32l4xx_create_replacing_stub
19450
0
      (globals, output_bfd, insn, (void*)veneer_r, (void*)veneer);
19451
0
        }
19452
0
        break;
19453
19454
0
      default:
19455
0
        abort ();
19456
0
      }
19457
0
  }
19458
0
    }
19459
19460
0
  if (arm_data->elf.this_hdr.sh_type == SHT_ARM_EXIDX)
19461
0
    {
19462
0
      arm_unwind_table_edit *edit_node
19463
0
  = arm_data->u.exidx.unwind_edit_list;
19464
      /* Now, sec->size is the size of the section we will write.  The original
19465
   size (before we merged duplicate entries and inserted EXIDX_CANTUNWIND
19466
   markers) was sec->rawsize.  (This isn't the case if we perform no
19467
   edits, then rawsize will be zero and we should use size).  */
19468
0
      bfd_byte *edited_contents = (bfd_byte *) bfd_malloc (sec->size);
19469
0
      unsigned int input_size = sec->rawsize ? sec->rawsize : sec->size;
19470
0
      unsigned int in_index, out_index;
19471
0
      bfd_vma add_to_offsets = 0;
19472
19473
0
      if (edited_contents == NULL)
19474
0
  return false;
19475
0
      for (in_index = 0, out_index = 0; in_index * 8 < input_size || edit_node;)
19476
0
  {
19477
0
    if (edit_node)
19478
0
      {
19479
0
        unsigned int edit_index = edit_node->index;
19480
19481
0
        if (in_index < edit_index && in_index * 8 < input_size)
19482
0
    {
19483
0
      copy_exidx_entry (output_bfd, edited_contents + out_index * 8,
19484
0
            contents + in_index * 8, add_to_offsets);
19485
0
      out_index++;
19486
0
      in_index++;
19487
0
    }
19488
0
        else if (in_index == edit_index
19489
0
           || (in_index * 8 >= input_size
19490
0
         && edit_index == UINT_MAX))
19491
0
    {
19492
0
      switch (edit_node->type)
19493
0
        {
19494
0
        case DELETE_EXIDX_ENTRY:
19495
0
          in_index++;
19496
0
          add_to_offsets += 8;
19497
0
          break;
19498
19499
0
        case INSERT_EXIDX_CANTUNWIND_AT_END:
19500
0
          {
19501
0
      asection *text_sec = edit_node->linked_section;
19502
0
      bfd_vma text_offset = text_sec->output_section->vma
19503
0
                + text_sec->output_offset
19504
0
                + text_sec->size;
19505
0
      bfd_vma exidx_offset = offset + out_index * 8;
19506
0
      unsigned long prel31_offset;
19507
19508
      /* Note: this is meant to be equivalent to an
19509
         R_ARM_PREL31 relocation.  These synthetic
19510
         EXIDX_CANTUNWIND markers are not relocated by the
19511
         usual BFD method.  */
19512
0
      prel31_offset = (text_offset - exidx_offset)
19513
0
          & 0x7ffffffful;
19514
0
      if (bfd_link_relocatable (link_info))
19515
0
        {
19516
          /* Here relocation for new EXIDX_CANTUNWIND is
19517
             created, so there is no need to
19518
             adjust offset by hand.  */
19519
0
          prel31_offset = text_sec->output_offset
19520
0
              + text_sec->size;
19521
0
        }
19522
19523
      /* First address we can't unwind.  */
19524
0
      bfd_put_32 (output_bfd, prel31_offset,
19525
0
            &edited_contents[out_index * 8]);
19526
19527
      /* Code for EXIDX_CANTUNWIND.  */
19528
0
      bfd_put_32 (output_bfd, 0x1,
19529
0
            &edited_contents[out_index * 8 + 4]);
19530
19531
0
      out_index++;
19532
0
      add_to_offsets -= 8;
19533
0
          }
19534
0
          break;
19535
0
        }
19536
19537
0
      edit_node = edit_node->next;
19538
0
    }
19539
0
      }
19540
0
    else
19541
0
      {
19542
        /* No more edits, copy remaining entries verbatim.  */
19543
0
        copy_exidx_entry (output_bfd, edited_contents + out_index * 8,
19544
0
        contents + in_index * 8, add_to_offsets);
19545
0
        out_index++;
19546
0
        in_index++;
19547
0
      }
19548
0
  }
19549
19550
0
      if (!(sec->flags & SEC_EXCLUDE) && !(sec->flags & SEC_NEVER_LOAD))
19551
0
  bfd_set_section_contents (output_bfd, sec->output_section,
19552
0
          edited_contents,
19553
0
          (file_ptr) sec->output_offset, sec->size);
19554
19555
0
      return true;
19556
0
    }
19557
19558
  /* Fix code to point to Cortex-A8 erratum stubs.  */
19559
0
  if (globals->fix_cortex_a8)
19560
0
    {
19561
0
      struct a8_branch_to_stub_data data;
19562
19563
0
      data.writing_section = sec;
19564
0
      data.contents = contents;
19565
19566
0
      bfd_hash_traverse (& globals->stub_hash_table, make_branch_to_a8_stub,
19567
0
       & data);
19568
0
    }
19569
19570
0
  if (mapcount == 0)
19571
0
    return false;
19572
19573
0
  if (globals->byteswap_code)
19574
0
    {
19575
0
      qsort (map, mapcount, sizeof (* map), elf32_arm_compare_mapping);
19576
19577
0
      ptr = map[0].vma;
19578
0
      for (i = 0; i < mapcount; i++)
19579
0
  {
19580
0
    if (i == mapcount - 1)
19581
0
      end = sec->size;
19582
0
    else
19583
0
      end = map[i + 1].vma;
19584
19585
0
    switch (map[i].type)
19586
0
      {
19587
0
      case 'a':
19588
        /* Byte swap code words.  */
19589
0
        while (ptr + 3 < end)
19590
0
    {
19591
0
      tmp = contents[ptr];
19592
0
      contents[ptr] = contents[ptr + 3];
19593
0
      contents[ptr + 3] = tmp;
19594
0
      tmp = contents[ptr + 1];
19595
0
      contents[ptr + 1] = contents[ptr + 2];
19596
0
      contents[ptr + 2] = tmp;
19597
0
      ptr += 4;
19598
0
    }
19599
0
        break;
19600
19601
0
      case 't':
19602
        /* Byte swap code halfwords.  */
19603
0
        while (ptr + 1 < end)
19604
0
    {
19605
0
      tmp = contents[ptr];
19606
0
      contents[ptr] = contents[ptr + 1];
19607
0
      contents[ptr + 1] = tmp;
19608
0
      ptr += 2;
19609
0
    }
19610
0
        break;
19611
19612
0
      case 'd':
19613
        /* Leave data alone.  */
19614
0
        break;
19615
0
      }
19616
0
    ptr = end;
19617
0
  }
19618
0
    }
19619
19620
0
  free (map);
19621
0
  arm_data->mapcount = -1;
19622
0
  arm_data->mapsize = 0;
19623
0
  arm_data->map = NULL;
19624
19625
0
  return false;
19626
0
}
19627
19628
/* Mangle thumb function symbols as we read them in.  */
19629
19630
static bool
19631
elf32_arm_swap_symbol_in (bfd * abfd,
19632
        const void *psrc,
19633
        const void *pshn,
19634
        Elf_Internal_Sym *dst)
19635
0
{
19636
0
  if (!bfd_elf32_swap_symbol_in (abfd, psrc, pshn, dst))
19637
0
    return false;
19638
0
  dst->st_target_internal = ST_BRANCH_TO_ARM;
19639
19640
  /* New EABI objects mark thumb function symbols by setting the low bit of
19641
     the address.  */
19642
0
  if (ELF_ST_TYPE (dst->st_info) == STT_FUNC
19643
0
      || ELF_ST_TYPE (dst->st_info) == STT_GNU_IFUNC)
19644
0
    {
19645
0
      if (dst->st_value & 1)
19646
0
  {
19647
0
    dst->st_value &= ~(bfd_vma) 1;
19648
0
    ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal,
19649
0
           ST_BRANCH_TO_THUMB);
19650
0
  }
19651
0
      else
19652
0
  ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal, ST_BRANCH_TO_ARM);
19653
0
    }
19654
0
  else if (ELF_ST_TYPE (dst->st_info) == STT_ARM_TFUNC)
19655
0
    {
19656
0
      dst->st_info = ELF_ST_INFO (ELF_ST_BIND (dst->st_info), STT_FUNC);
19657
0
      ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal, ST_BRANCH_TO_THUMB);
19658
0
    }
19659
0
  else if (ELF_ST_TYPE (dst->st_info) == STT_SECTION)
19660
0
    ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal, ST_BRANCH_LONG);
19661
0
  else
19662
0
    ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal, ST_BRANCH_UNKNOWN);
19663
19664
0
  return true;
19665
0
}
19666
19667
19668
/* Mangle thumb function symbols as we write them out.  */
19669
19670
static void
19671
elf32_arm_swap_symbol_out (bfd *abfd,
19672
         const Elf_Internal_Sym *src,
19673
         void *cdst,
19674
         void *shndx)
19675
0
{
19676
0
  Elf_Internal_Sym newsym;
19677
19678
  /* We convert STT_ARM_TFUNC symbols into STT_FUNC with the low bit
19679
     of the address set, as per the new EABI.  We do this unconditionally
19680
     because objcopy does not set the elf header flags until after
19681
     it writes out the symbol table.  */
19682
0
  if (ARM_GET_SYM_BRANCH_TYPE (src->st_target_internal) == ST_BRANCH_TO_THUMB)
19683
0
    {
19684
0
      newsym = *src;
19685
0
      if (ELF_ST_TYPE (src->st_info) != STT_GNU_IFUNC)
19686
0
  newsym.st_info = ELF_ST_INFO (ELF_ST_BIND (src->st_info), STT_FUNC);
19687
0
      if (newsym.st_shndx != SHN_UNDEF)
19688
0
  {
19689
    /* Do this only for defined symbols. At link type, the static
19690
       linker will simulate the work of dynamic linker of resolving
19691
       symbols and will carry over the thumbness of found symbols to
19692
       the output symbol table. It's not clear how it happens, but
19693
       the thumbness of undefined symbols can well be different at
19694
       runtime, and writing '1' for them will be confusing for users
19695
       and possibly for dynamic linker itself.
19696
    */
19697
0
    newsym.st_value |= 1;
19698
0
  }
19699
19700
0
      src = &newsym;
19701
0
    }
19702
0
  bfd_elf32_swap_symbol_out (abfd, src, cdst, shndx);
19703
0
}
19704
19705
/* Add the PT_ARM_EXIDX program header.  */
19706
19707
static bool
19708
elf32_arm_modify_segment_map (bfd *abfd,
19709
            struct bfd_link_info *info ATTRIBUTE_UNUSED)
19710
3
{
19711
3
  struct elf_segment_map *m;
19712
3
  asection *sec;
19713
19714
3
  sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
19715
3
  if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
19716
0
    {
19717
      /* If there is already a PT_ARM_EXIDX header, then we do not
19718
   want to add another one.  This situation arises when running
19719
   "strip"; the input binary already has the header.  */
19720
0
      m = elf_seg_map (abfd);
19721
0
      while (m && m->p_type != PT_ARM_EXIDX)
19722
0
  m = m->next;
19723
0
      if (!m)
19724
0
  {
19725
0
    m = (struct elf_segment_map *)
19726
0
        bfd_zalloc (abfd, sizeof (struct elf_segment_map));
19727
0
    if (m == NULL)
19728
0
      return false;
19729
0
    m->p_type = PT_ARM_EXIDX;
19730
0
    m->count = 1;
19731
0
    m->sections[0] = sec;
19732
19733
0
    m->next = elf_seg_map (abfd);
19734
0
    elf_seg_map (abfd) = m;
19735
0
  }
19736
0
    }
19737
19738
3
  return true;
19739
3
}
19740
19741
/* We may add a PT_ARM_EXIDX program header.  */
19742
19743
static int
19744
elf32_arm_additional_program_headers (bfd *abfd,
19745
              struct bfd_link_info *info ATTRIBUTE_UNUSED)
19746
0
{
19747
0
  asection *sec;
19748
19749
0
  sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
19750
0
  if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
19751
0
    return 1;
19752
0
  else
19753
0
    return 0;
19754
0
}
19755
19756
/* Hook called by the linker routine which adds symbols from an object
19757
   file.  */
19758
19759
static bool
19760
elf32_arm_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
19761
         Elf_Internal_Sym *sym, const char **namep,
19762
         flagword *flagsp, asection **secp, bfd_vma *valp)
19763
0
{
19764
0
  if (elf32_arm_hash_table (info) == NULL)
19765
0
    return false;
19766
19767
0
  if (elf32_arm_hash_table (info)->root.target_os == is_vxworks
19768
0
      && !elf_vxworks_add_symbol_hook (abfd, info, sym, namep,
19769
0
               flagsp, secp, valp))
19770
0
    return false;
19771
19772
0
  return true;
19773
0
}
19774
19775
/* We use this to override swap_symbol_in and swap_symbol_out.  */
19776
static const struct elf_size_info elf32_arm_size_info =
19777
{
19778
  sizeof (Elf32_External_Ehdr),
19779
  sizeof (Elf32_External_Phdr),
19780
  sizeof (Elf32_External_Shdr),
19781
  sizeof (Elf32_External_Rel),
19782
  sizeof (Elf32_External_Rela),
19783
  sizeof (Elf32_External_Sym),
19784
  sizeof (Elf32_External_Dyn),
19785
  sizeof (Elf_External_Note),
19786
  4,
19787
  1,
19788
  32, 2,
19789
  ELFCLASS32, EV_CURRENT,
19790
  bfd_elf32_write_out_phdrs,
19791
  bfd_elf32_write_shdrs_and_ehdr,
19792
  bfd_elf32_checksum_contents,
19793
  bfd_elf32_write_relocs,
19794
  elf32_arm_swap_symbol_in,
19795
  elf32_arm_swap_symbol_out,
19796
  bfd_elf32_slurp_reloc_table,
19797
  bfd_elf32_slurp_symbol_table,
19798
  bfd_elf32_swap_dyn_in,
19799
  bfd_elf32_swap_dyn_out,
19800
  bfd_elf32_swap_reloc_in,
19801
  bfd_elf32_swap_reloc_out,
19802
  bfd_elf32_swap_reloca_in,
19803
  bfd_elf32_swap_reloca_out
19804
};
19805
19806
static bfd_vma
19807
read_code32 (const bfd *abfd, const bfd_byte *addr)
19808
0
{
19809
  /* V7 BE8 code is always little endian.  */
19810
0
  if ((elf_elfheader (abfd)->e_flags & EF_ARM_BE8) != 0)
19811
0
    return bfd_getl32 (addr);
19812
19813
0
  return bfd_get_32 (abfd, addr);
19814
0
}
19815
19816
static bfd_vma
19817
read_code16 (const bfd *abfd, const bfd_byte *addr)
19818
0
{
19819
  /* V7 BE8 code is always little endian.  */
19820
0
  if ((elf_elfheader (abfd)->e_flags & EF_ARM_BE8) != 0)
19821
0
    return bfd_getl16 (addr);
19822
19823
0
  return bfd_get_16 (abfd, addr);
19824
0
}
19825
19826
/* Return size of plt0 entry starting at ADDR
19827
   or (bfd_vma) -1 if size can not be determined.  */
19828
19829
static bfd_vma
19830
elf32_arm_plt0_size (const bfd *abfd, const bfd_byte *addr,
19831
         bfd_size_type data_size)
19832
0
{
19833
0
  bfd_vma first_word;
19834
0
  bfd_vma plt0_size;
19835
19836
0
  if (data_size < 4)
19837
0
    return (bfd_vma) -1;
19838
19839
0
  first_word = read_code32 (abfd, addr);
19840
19841
0
  if (first_word == elf32_arm_plt0_entry[0])
19842
0
    plt0_size = 4 * ARRAY_SIZE (elf32_arm_plt0_entry);
19843
0
  else if (first_word == elf32_thumb2_plt0_entry[0])
19844
0
    plt0_size = 4 * ARRAY_SIZE (elf32_thumb2_plt0_entry);
19845
0
  else
19846
    /* We don't yet handle this PLT format.  */
19847
0
    return (bfd_vma) -1;
19848
19849
0
  return plt0_size;
19850
0
}
19851
19852
/* Return size of plt entry starting at offset OFFSET
19853
   of plt section located at address START
19854
   or (bfd_vma) -1 if size can not be determined.  */
19855
19856
static bfd_vma
19857
elf32_arm_plt_size (const bfd *abfd, const bfd_byte *start, bfd_vma offset,
19858
        bfd_size_type data_size)
19859
0
{
19860
0
  bfd_vma first_insn;
19861
0
  bfd_vma plt_size = 0;
19862
19863
  /* PLT entry size if fixed on Thumb-only platforms.  */
19864
0
  if (read_code32 (abfd, start) == elf32_thumb2_plt0_entry[0])
19865
0
    return 4 * ARRAY_SIZE (elf32_thumb2_plt_entry);
19866
19867
  /* Respect Thumb stub if necessary.  */
19868
0
  if (offset + 2 > data_size)
19869
0
    return (bfd_vma) -1;
19870
0
  if (read_code16 (abfd, start + offset) == elf32_arm_plt_thumb_stub[0])
19871
0
    {
19872
0
      plt_size += 2 * ARRAY_SIZE (elf32_arm_plt_thumb_stub);
19873
0
    }
19874
19875
  /* Strip immediate from first add.  */
19876
0
  if (offset + plt_size + 4 > data_size)
19877
0
    return (bfd_vma) -1;
19878
0
  first_insn = read_code32 (abfd, start + offset + plt_size) & 0xffffff00;
19879
19880
#ifdef FOUR_WORD_PLT
19881
  if (first_insn == elf32_arm_plt_entry[0])
19882
    plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry);
19883
#else
19884
0
  if (first_insn == elf32_arm_plt_entry_long[0])
19885
0
    plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry_long);
19886
0
  else if (first_insn == elf32_arm_plt_entry_short[0])
19887
0
    plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry_short);
19888
0
#endif
19889
0
  else
19890
    /* We don't yet handle this PLT format.  */
19891
0
    return (bfd_vma) -1;
19892
19893
0
  return plt_size;
19894
0
}
19895
19896
/* Implementation is shamelessly borrowed from _bfd_elf_get_synthetic_symtab.  */
19897
19898
static long
19899
elf32_arm_get_synthetic_symtab (bfd *abfd,
19900
             long symcount ATTRIBUTE_UNUSED,
19901
             asymbol **syms ATTRIBUTE_UNUSED,
19902
             long dynsymcount,
19903
             asymbol **dynsyms,
19904
             asymbol **ret)
19905
344
{
19906
344
  asection *relplt;
19907
344
  asymbol *s;
19908
344
  arelent *p;
19909
344
  long count, i, n;
19910
344
  size_t size;
19911
344
  Elf_Internal_Shdr *hdr;
19912
344
  char *names;
19913
344
  asection *plt;
19914
344
  bfd_vma offset;
19915
344
  bfd_byte *data;
19916
19917
344
  *ret = NULL;
19918
19919
344
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
19920
344
    return 0;
19921
19922
0
  if (dynsymcount <= 0)
19923
0
    return 0;
19924
19925
0
  relplt = bfd_get_section_by_name (abfd, ".rel.plt");
19926
0
  if (relplt == NULL)
19927
0
    return 0;
19928
19929
0
  hdr = &elf_section_data (relplt)->this_hdr;
19930
0
  if (hdr->sh_link != elf_dynsymtab (abfd)
19931
0
      || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
19932
0
    return 0;
19933
19934
0
  plt = bfd_get_section_by_name (abfd, ".plt");
19935
0
  if (plt == NULL)
19936
0
    return 0;
19937
19938
0
  if (!elf32_arm_size_info.slurp_reloc_table (abfd, relplt, dynsyms, true))
19939
0
    return -1;
19940
19941
0
  data = NULL;
19942
0
  if (!bfd_get_full_section_contents (abfd, plt, &data))
19943
0
    return -1;
19944
19945
0
  count = NUM_SHDR_ENTRIES (hdr);
19946
0
  size = count * sizeof (asymbol);
19947
0
  p = relplt->relocation;
19948
0
  for (i = 0; i < count; i++, p += elf32_arm_size_info.int_rels_per_ext_rel)
19949
0
    {
19950
0
      size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
19951
0
      if (p->addend != 0)
19952
0
  size += sizeof ("+0x") - 1 + 8;
19953
0
    }
19954
19955
0
  offset = elf32_arm_plt0_size (abfd, data, plt->size);
19956
0
  if (offset == (bfd_vma) -1
19957
0
      || (s = *ret = (asymbol *) bfd_malloc (size)) == NULL)
19958
0
    {
19959
0
      free (data);
19960
0
      return -1;
19961
0
    }
19962
19963
0
  names = (char *) (s + count);
19964
0
  p = relplt->relocation;
19965
0
  n = 0;
19966
0
  for (i = 0; i < count; i++, p += elf32_arm_size_info.int_rels_per_ext_rel)
19967
0
    {
19968
0
      size_t len;
19969
19970
0
      bfd_vma plt_size = elf32_arm_plt_size (abfd, data, offset, plt->size);
19971
0
      if (plt_size == (bfd_vma) -1)
19972
0
  break;
19973
19974
0
      *s = **p->sym_ptr_ptr;
19975
      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
19976
   we are defining a symbol, ensure one of them is set.  */
19977
0
      if ((s->flags & BSF_LOCAL) == 0)
19978
0
  s->flags |= BSF_GLOBAL;
19979
0
      s->flags |= BSF_SYNTHETIC;
19980
0
      s->section = plt;
19981
0
      s->value = offset;
19982
0
      s->name = names;
19983
0
      s->udata.p = NULL;
19984
0
      len = strlen ((*p->sym_ptr_ptr)->name);
19985
0
      memcpy (names, (*p->sym_ptr_ptr)->name, len);
19986
0
      names += len;
19987
0
      if (p->addend != 0)
19988
0
  {
19989
0
    char buf[30], *a;
19990
19991
0
    memcpy (names, "+0x", sizeof ("+0x") - 1);
19992
0
    names += sizeof ("+0x") - 1;
19993
0
    bfd_sprintf_vma (abfd, buf, p->addend);
19994
0
    for (a = buf; *a == '0'; ++a)
19995
0
      ;
19996
0
    len = strlen (a);
19997
0
    memcpy (names, a, len);
19998
0
    names += len;
19999
0
  }
20000
0
      memcpy (names, "@plt", sizeof ("@plt"));
20001
0
      names += sizeof ("@plt");
20002
0
      ++s, ++n;
20003
0
      offset += plt_size;
20004
0
    }
20005
20006
0
  free (data);
20007
0
  return n;
20008
0
}
20009
20010
static bool
20011
elf32_arm_section_flags (const Elf_Internal_Shdr *hdr)
20012
572
{
20013
572
  if (hdr->sh_flags & SHF_ARM_PURECODE)
20014
102
    hdr->bfd_section->flags |= SEC_ELF_PURECODE;
20015
572
  return true;
20016
572
}
20017
20018
static flagword
20019
elf32_arm_lookup_section_flags (char *flag_name)
20020
0
{
20021
0
  if (!strcmp (flag_name, "SHF_ARM_PURECODE"))
20022
0
    return SHF_ARM_PURECODE;
20023
20024
0
  return SEC_NO_FLAGS;
20025
0
}
20026
20027
static unsigned int
20028
elf32_arm_count_additional_relocs (asection *sec)
20029
0
{
20030
0
  struct _arm_elf_section_data *arm_data;
20031
0
  arm_data = get_arm_elf_section_data (sec);
20032
20033
0
  return arm_data == NULL ? 0 : arm_data->additional_reloc_count;
20034
0
}
20035
20036
/* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which
20037
   has a type >= SHT_LOOS.  Returns TRUE if these fields were initialised
20038
   FALSE otherwise.  ISECTION is the best guess matching section from the
20039
   input bfd IBFD, but it might be NULL.  */
20040
20041
static bool
20042
elf32_arm_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
20043
               bfd *obfd ATTRIBUTE_UNUSED,
20044
               const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
20045
               Elf_Internal_Shdr *osection)
20046
0
{
20047
0
  switch (osection->sh_type)
20048
0
    {
20049
0
    case SHT_ARM_EXIDX:
20050
0
      {
20051
0
  Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
20052
0
  Elf_Internal_Shdr **iheaders = elf_elfsections (ibfd);
20053
0
  unsigned i = 0;
20054
20055
0
  osection->sh_flags = SHF_ALLOC | SHF_LINK_ORDER;
20056
0
  osection->sh_info = 0;
20057
20058
  /* The sh_link field must be set to the text section associated with
20059
     this index section.  Unfortunately the ARM EHABI does not specify
20060
     exactly how to determine this association.  Our caller does try
20061
     to match up OSECTION with its corresponding input section however
20062
     so that is a good first guess.  */
20063
0
  if (isection != NULL
20064
0
      && osection->bfd_section != NULL
20065
0
      && isection->bfd_section != NULL
20066
0
      && isection->bfd_section->output_section != NULL
20067
0
      && isection->bfd_section->output_section == osection->bfd_section
20068
0
      && iheaders != NULL
20069
0
      && isection->sh_link > 0
20070
0
      && isection->sh_link < elf_numsections (ibfd)
20071
0
      && iheaders[isection->sh_link]->bfd_section != NULL
20072
0
      && iheaders[isection->sh_link]->bfd_section->output_section != NULL
20073
0
      )
20074
0
    {
20075
0
      for (i = elf_numsections (obfd); i-- > 0;)
20076
0
        if (oheaders[i]->bfd_section
20077
0
      == iheaders[isection->sh_link]->bfd_section->output_section)
20078
0
    break;
20079
0
    }
20080
20081
0
  if (i == 0)
20082
0
    {
20083
      /* Failing that we have to find a matching section ourselves.  If
20084
         we had the output section name available we could compare that
20085
         with input section names.  Unfortunately we don't.  So instead
20086
         we use a simple heuristic and look for the nearest executable
20087
         section before this one.  */
20088
0
      for (i = elf_numsections (obfd); i-- > 0;)
20089
0
        if (oheaders[i] == osection)
20090
0
    break;
20091
0
      if (i == 0)
20092
0
        break;
20093
20094
0
      while (i-- > 0)
20095
0
        if (oheaders[i]->sh_type == SHT_PROGBITS
20096
0
      && (oheaders[i]->sh_flags & (SHF_ALLOC | SHF_EXECINSTR))
20097
0
      == (SHF_ALLOC | SHF_EXECINSTR))
20098
0
    break;
20099
0
    }
20100
20101
0
  if (i)
20102
0
    {
20103
0
      osection->sh_link = i;
20104
      /* If the text section was part of a group
20105
         then the index section should be too.  */
20106
0
      if (oheaders[i]->sh_flags & SHF_GROUP)
20107
0
        osection->sh_flags |= SHF_GROUP;
20108
0
      return true;
20109
0
    }
20110
0
      }
20111
0
      break;
20112
20113
0
    case SHT_ARM_PREEMPTMAP:
20114
0
      osection->sh_flags = SHF_ALLOC;
20115
0
      break;
20116
20117
0
    case SHT_ARM_ATTRIBUTES:
20118
0
    case SHT_ARM_DEBUGOVERLAY:
20119
0
    case SHT_ARM_OVERLAYSECTION:
20120
0
    default:
20121
0
      break;
20122
0
    }
20123
20124
0
  return false;
20125
0
}
20126
20127
/* Returns TRUE if NAME is an ARM mapping symbol.
20128
   Traditionally the symbols $a, $d and $t have been used.
20129
   The ARM ELF standard also defines $x (for A64 code).  It also allows a
20130
   period initiated suffix to be added to the symbol: "$[adtx]\.[:sym_char]+".
20131
   Other tools might also produce $b (Thumb BL), $f, $p, $m and $v, but we do
20132
   not support them here.  $t.x indicates the start of ThumbEE instructions.  */
20133
20134
static bool
20135
is_arm_mapping_symbol (const char * name)
20136
0
{
20137
0
  return name != NULL /* Paranoia.  */
20138
0
    && name[0] == '$' /* Note: if objcopy --prefix-symbols has been used then
20139
       the mapping symbols could have acquired a prefix.
20140
       We do not support this here, since such symbols no
20141
       longer conform to the ARM ELF ABI.  */
20142
0
    && (name[1] == 'a' || name[1] == 'd' || name[1] == 't' || name[1] == 'x')
20143
0
    && (name[2] == 0 || name[2] == '.');
20144
  /* FIXME: Strictly speaking the symbol is only a valid mapping symbol if
20145
     any characters that follow the period are legal characters for the body
20146
     of a symbol's name.  For now we just assume that this is the case.  */
20147
0
}
20148
20149
/* Make sure that mapping symbols in object files are not removed via the
20150
   "strip --strip-unneeded" tool.  These symbols are needed in order to
20151
   correctly generate interworking veneers, and for byte swapping code
20152
   regions.  Once an object file has been linked, it is safe to remove the
20153
   symbols as they will no longer be needed.  */
20154
20155
static void
20156
elf32_arm_backend_symbol_processing (bfd *abfd, asymbol *sym)
20157
0
{
20158
0
  if (((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
20159
0
      && sym->section != bfd_abs_section_ptr
20160
0
      && is_arm_mapping_symbol (sym->name))
20161
0
    sym->flags |= BSF_KEEP;
20162
0
}
20163
20164
#undef  elf_backend_copy_special_section_fields
20165
#define elf_backend_copy_special_section_fields elf32_arm_copy_special_section_fields
20166
20167
#define ELF_ARCH      bfd_arch_arm
20168
#define ELF_TARGET_ID     ARM_ELF_DATA
20169
#define ELF_MACHINE_CODE    EM_ARM
20170
#define ELF_MAXPAGESIZE     0x1000
20171
#define ELF_COMMONPAGESIZE    0x1000
20172
20173
#define bfd_elf32_mkobject      elf32_arm_mkobject
20174
20175
#define bfd_elf32_bfd_copy_private_bfd_data elf32_arm_copy_private_bfd_data
20176
#define bfd_elf32_bfd_merge_private_bfd_data  elf32_arm_merge_private_bfd_data
20177
#define bfd_elf32_bfd_set_private_flags   elf32_arm_set_private_flags
20178
#define bfd_elf32_bfd_print_private_bfd_data  elf32_arm_print_private_bfd_data
20179
#define bfd_elf32_bfd_link_hash_table_create  elf32_arm_link_hash_table_create
20180
#define bfd_elf32_bfd_reloc_type_lookup   elf32_arm_reloc_type_lookup
20181
#define bfd_elf32_bfd_reloc_name_lookup   elf32_arm_reloc_name_lookup
20182
#define bfd_elf32_find_inliner_info   elf32_arm_find_inliner_info
20183
#define bfd_elf32_new_section_hook    elf32_arm_new_section_hook
20184
#define bfd_elf32_bfd_is_target_special_symbol  elf32_arm_is_target_special_symbol
20185
#define bfd_elf32_bfd_final_link    elf32_arm_final_link
20186
#define bfd_elf32_get_synthetic_symtab  elf32_arm_get_synthetic_symtab
20187
20188
#define elf_backend_get_symbol_type   elf32_arm_get_symbol_type
20189
#define elf_backend_maybe_function_sym    elf32_arm_maybe_function_sym
20190
#define elf_backend_gc_mark_hook    elf32_arm_gc_mark_hook
20191
#define elf_backend_gc_mark_extra_sections  elf32_arm_gc_mark_extra_sections
20192
#define elf_backend_check_relocs    elf32_arm_check_relocs
20193
#define elf_backend_update_relocs   elf32_arm_update_relocs
20194
#define elf_backend_relocate_section    elf32_arm_relocate_section
20195
#define elf_backend_write_section   elf32_arm_write_section
20196
#define elf_backend_adjust_dynamic_symbol elf32_arm_adjust_dynamic_symbol
20197
#define elf_backend_create_dynamic_sections elf32_arm_create_dynamic_sections
20198
#define elf_backend_finish_dynamic_symbol elf32_arm_finish_dynamic_symbol
20199
#define elf_backend_finish_dynamic_sections elf32_arm_finish_dynamic_sections
20200
#define elf_backend_late_size_sections    elf32_arm_late_size_sections
20201
#define elf_backend_early_size_sections   elf32_arm_early_size_sections
20202
#define elf_backend_init_index_section    _bfd_elf_init_2_index_sections
20203
#define elf_backend_init_file_header    elf32_arm_init_file_header
20204
#define elf_backend_reloc_type_class    elf32_arm_reloc_type_class
20205
#define elf_backend_object_p      elf32_arm_object_p
20206
#define elf_backend_fake_sections   elf32_arm_fake_sections
20207
#define elf_backend_section_from_shdr   elf32_arm_section_from_shdr
20208
#define elf_backend_final_write_processing  elf32_arm_final_write_processing
20209
#define elf_backend_copy_indirect_symbol  elf32_arm_copy_indirect_symbol
20210
#define elf_backend_size_info     elf32_arm_size_info
20211
#define elf_backend_modify_segment_map    elf32_arm_modify_segment_map
20212
#define elf_backend_additional_program_headers  elf32_arm_additional_program_headers
20213
#define elf_backend_output_arch_local_syms  elf32_arm_output_arch_local_syms
20214
#define elf_backend_filter_implib_symbols elf32_arm_filter_implib_symbols
20215
#define elf_backend_begin_write_processing  elf32_arm_begin_write_processing
20216
#define elf_backend_add_symbol_hook   elf32_arm_add_symbol_hook
20217
#define elf_backend_count_additional_relocs elf32_arm_count_additional_relocs
20218
#define elf_backend_symbol_processing   elf32_arm_backend_symbol_processing
20219
20220
#define elf_backend_can_refcount       1
20221
#define elf_backend_can_gc_sections    1
20222
#define elf_backend_plt_readonly       1
20223
#define elf_backend_want_got_plt       1
20224
#define elf_backend_want_plt_sym       0
20225
#define elf_backend_want_dynrelro      1
20226
#define elf_backend_may_use_rel_p      1
20227
#define elf_backend_may_use_rela_p     0
20228
#define elf_backend_default_use_rela_p 0
20229
#define elf_backend_dtrel_excludes_plt 1
20230
20231
#define elf_backend_got_header_size 12
20232
#define elf_backend_extern_protected_data 0
20233
20234
#undef  elf_backend_obj_attrs_vendor
20235
#define elf_backend_obj_attrs_vendor    "aeabi"
20236
#undef  elf_backend_obj_attrs_section
20237
#define elf_backend_obj_attrs_section   ".ARM.attributes"
20238
#undef  elf_backend_obj_attrs_arg_type
20239
#define elf_backend_obj_attrs_arg_type    elf32_arm_obj_attrs_arg_type
20240
#undef  elf_backend_obj_attrs_section_type
20241
#define elf_backend_obj_attrs_section_type  SHT_ARM_ATTRIBUTES
20242
#define elf_backend_obj_attrs_order   elf32_arm_obj_attrs_order
20243
#define elf_backend_obj_attrs_handle_unknown  elf32_arm_obj_attrs_handle_unknown
20244
20245
#undef  elf_backend_section_flags
20246
#define elf_backend_section_flags   elf32_arm_section_flags
20247
#undef  elf_backend_lookup_section_flags_hook
20248
#define elf_backend_lookup_section_flags_hook elf32_arm_lookup_section_flags
20249
20250
#define elf_backend_linux_prpsinfo32_ugid16 true
20251
20252
#include "elf32-target.h"
20253
20254
/* Reset to defaults.  */
20255
#undef  elf_backend_plt_alignment
20256
#undef  elf_backend_modify_segment_map
20257
#define elf_backend_modify_segment_map    elf32_arm_modify_segment_map
20258
#undef  elf_backend_modify_headers
20259
#undef  elf_backend_final_write_processing
20260
#define elf_backend_final_write_processing  elf32_arm_final_write_processing
20261
#undef  ELF_MINPAGESIZE
20262
#undef  ELF_COMMONPAGESIZE
20263
#define ELF_COMMONPAGESIZE    0x1000
20264
20265
20266
/* FDPIC Targets.  */
20267
20268
#undef  TARGET_LITTLE_SYM
20269
#define TARGET_LITTLE_SYM   arm_elf32_fdpic_le_vec
20270
#undef  TARGET_LITTLE_NAME
20271
#define TARGET_LITTLE_NAME    "elf32-littlearm-fdpic"
20272
#undef  TARGET_BIG_SYM
20273
#define TARGET_BIG_SYM      arm_elf32_fdpic_be_vec
20274
#undef  TARGET_BIG_NAME
20275
#define TARGET_BIG_NAME     "elf32-bigarm-fdpic"
20276
#undef elf_match_priority
20277
#define elf_match_priority    128
20278
#undef ELF_OSABI
20279
#define ELF_OSABI   ELFOSABI_ARM_FDPIC
20280
#undef ELF_OSABI_EXACT
20281
#define ELF_OSABI_EXACT   1
20282
20283
/* Like elf32_arm_link_hash_table_create -- but overrides
20284
   appropriately for FDPIC.  */
20285
20286
static struct bfd_link_hash_table *
20287
elf32_arm_fdpic_link_hash_table_create (bfd *abfd)
20288
0
{
20289
0
  struct bfd_link_hash_table *ret;
20290
20291
0
  ret = elf32_arm_link_hash_table_create (abfd);
20292
0
  if (ret)
20293
0
    {
20294
0
      struct elf32_arm_link_hash_table *htab = (struct elf32_arm_link_hash_table *) ret;
20295
20296
0
      htab->fdpic_p = 1;
20297
0
    }
20298
0
  return ret;
20299
0
}
20300
20301
/* We need dynamic symbols for every section, since segments can
20302
   relocate independently.  */
20303
static bool
20304
elf32_arm_fdpic_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
20305
            struct bfd_link_info *info
20306
            ATTRIBUTE_UNUSED,
20307
            asection *p ATTRIBUTE_UNUSED)
20308
0
{
20309
0
  switch (elf_section_data (p)->this_hdr.sh_type)
20310
0
    {
20311
0
    case SHT_PROGBITS:
20312
0
    case SHT_NOBITS:
20313
      /* If sh_type is yet undecided, assume it could be
20314
   SHT_PROGBITS/SHT_NOBITS.  */
20315
0
    case SHT_NULL:
20316
0
      return false;
20317
20318
      /* There shouldn't be section relative relocations
20319
   against any other section.  */
20320
0
    default:
20321
0
      return true;
20322
0
    }
20323
0
}
20324
20325
#undef  elf32_bed
20326
#define elf32_bed       elf32_arm_fdpic_bed
20327
20328
#undef  bfd_elf32_bfd_link_hash_table_create
20329
#define bfd_elf32_bfd_link_hash_table_create  elf32_arm_fdpic_link_hash_table_create
20330
20331
#undef elf_backend_omit_section_dynsym
20332
#define elf_backend_omit_section_dynsym   elf32_arm_fdpic_omit_section_dynsym
20333
20334
#undef ELF_TARGET_OS
20335
20336
#include "elf32-target.h"
20337
20338
#undef elf_match_priority
20339
#undef ELF_OSABI
20340
#undef ELF_OSABI_EXACT
20341
#undef elf_backend_omit_section_dynsym
20342
20343
/* VxWorks Targets.  */
20344
20345
#undef  TARGET_LITTLE_SYM
20346
#define TARGET_LITTLE_SYM   arm_elf32_vxworks_le_vec
20347
#undef  TARGET_LITTLE_NAME
20348
#define TARGET_LITTLE_NAME    "elf32-littlearm-vxworks"
20349
#undef  TARGET_BIG_SYM
20350
#define TARGET_BIG_SYM      arm_elf32_vxworks_be_vec
20351
#undef  TARGET_BIG_NAME
20352
#define TARGET_BIG_NAME     "elf32-bigarm-vxworks"
20353
20354
/* Like elf32_arm_link_hash_table_create -- but overrides
20355
   appropriately for VxWorks.  */
20356
20357
static struct bfd_link_hash_table *
20358
elf32_arm_vxworks_link_hash_table_create (bfd *abfd)
20359
0
{
20360
0
  struct bfd_link_hash_table *ret;
20361
20362
0
  ret = elf32_arm_link_hash_table_create (abfd);
20363
0
  if (ret)
20364
0
    {
20365
0
      struct elf32_arm_link_hash_table *htab
20366
0
  = (struct elf32_arm_link_hash_table *) ret;
20367
0
      htab->use_rel = 0;
20368
0
    }
20369
0
  return ret;
20370
0
}
20371
20372
static bool
20373
elf32_arm_vxworks_final_write_processing (bfd *abfd)
20374
0
{
20375
0
  arm_final_write_processing (abfd);
20376
0
  return elf_vxworks_final_write_processing (abfd);
20377
0
}
20378
20379
#undef  elf32_bed
20380
#define elf32_bed elf32_arm_vxworks_bed
20381
20382
#undef  bfd_elf32_bfd_link_hash_table_create
20383
#define bfd_elf32_bfd_link_hash_table_create  elf32_arm_vxworks_link_hash_table_create
20384
#undef  elf_backend_final_write_processing
20385
#define elf_backend_final_write_processing  elf32_arm_vxworks_final_write_processing
20386
#undef  elf_backend_emit_relocs
20387
#define elf_backend_emit_relocs     elf_vxworks_emit_relocs
20388
20389
#undef  elf_backend_may_use_rel_p
20390
#define elf_backend_may_use_rel_p 0
20391
#undef  elf_backend_may_use_rela_p
20392
#define elf_backend_may_use_rela_p  1
20393
#undef  elf_backend_default_use_rela_p
20394
#define elf_backend_default_use_rela_p  1
20395
#undef  elf_backend_want_plt_sym
20396
#define elf_backend_want_plt_sym  1
20397
#undef  ELF_MAXPAGESIZE
20398
#define ELF_MAXPAGESIZE     0x1000
20399
#undef ELF_TARGET_OS
20400
#define ELF_TARGET_OS     is_vxworks
20401
20402
#include "elf32-target.h"
20403
20404
20405
/* Merge backend specific data from an object file to the output
20406
   object file when linking.  */
20407
20408
static bool
20409
elf32_arm_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
20410
0
{
20411
0
  bfd *obfd = info->output_bfd;
20412
0
  flagword out_flags;
20413
0
  flagword in_flags;
20414
0
  bool flags_compatible = true;
20415
0
  asection *sec;
20416
20417
  /* Check if we have the same endianness.  */
20418
0
  if (! _bfd_generic_verify_endian_match (ibfd, info))
20419
0
    return false;
20420
20421
0
  if (! is_arm_elf (ibfd))
20422
0
    return true;
20423
20424
0
  if (!elf32_arm_merge_eabi_attributes (ibfd, info))
20425
0
    return false;
20426
20427
  /* The input BFD must have had its flags initialised.  */
20428
  /* The following seems bogus to me -- The flags are initialized in
20429
     the assembler but I don't think an elf_flags_init field is
20430
     written into the object.  */
20431
  /* BFD_ASSERT (elf_flags_init (ibfd)); */
20432
20433
0
  in_flags  = elf_elfheader (ibfd)->e_flags;
20434
0
  out_flags = elf_elfheader (obfd)->e_flags;
20435
20436
  /* In theory there is no reason why we couldn't handle this.  However
20437
     in practice it isn't even close to working and there is no real
20438
     reason to want it.  */
20439
0
  if (EF_ARM_EABI_VERSION (in_flags) >= EF_ARM_EABI_VER4
20440
0
      && !(ibfd->flags & DYNAMIC)
20441
0
      && (in_flags & EF_ARM_BE8))
20442
0
    {
20443
0
      _bfd_error_handler (_("error: %pB is already in final BE8 format"),
20444
0
        ibfd);
20445
0
      return false;
20446
0
    }
20447
20448
0
  if (!elf_flags_init (obfd))
20449
0
    {
20450
      /* If the input has no flags set, then do not set the output flags.
20451
   This will allow future bfds to determine the desired output flags.
20452
   If no input bfds have any flags set, then neither will the output bfd.
20453
20454
   Note - we used to restrict this test to when the input architecture
20455
   variant was the default variant, but this does not allow for
20456
   linker scripts which override the default.  See PR 28910 for an
20457
   example.  */
20458
0
      if (in_flags == 0)
20459
0
  return true;
20460
      
20461
0
      elf_flags_init (obfd) = true;
20462
0
      elf_elfheader (obfd)->e_flags = in_flags;
20463
20464
0
      if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
20465
0
    && bfd_get_arch_info (obfd)->the_default)
20466
0
  return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
20467
20468
0
      return true;
20469
0
    }
20470
20471
  /* Determine what should happen if the input ARM architecture
20472
     does not match the output ARM architecture.  */
20473
0
  if (! bfd_arm_merge_machines (ibfd, obfd))
20474
0
    return false;
20475
20476
  /* Identical flags must be compatible.  */
20477
0
  if (in_flags == out_flags)
20478
0
    return true;
20479
20480
  /* Check to see if the input BFD actually contains any sections.  If
20481
     not, its flags may not have been initialised either, but it
20482
     cannot actually cause any incompatiblity.  Do not short-circuit
20483
     dynamic objects; their section list may be emptied by
20484
    elf_link_add_object_symbols.
20485
20486
    Also check to see if there are no code sections in the input.
20487
    In this case there is no need to check for code specific flags.
20488
    XXX - do we need to worry about floating-point format compatability
20489
    in data sections ?  */
20490
0
  if (!(ibfd->flags & DYNAMIC))
20491
0
    {
20492
0
      bool null_input_bfd = true;
20493
0
      bool only_data_sections = true;
20494
20495
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
20496
0
  {
20497
    /* Ignore synthetic glue sections.  */
20498
0
    if (strcmp (sec->name, ".glue_7")
20499
0
        && strcmp (sec->name, ".glue_7t"))
20500
0
      {
20501
0
        if ((bfd_section_flags (sec)
20502
0
       & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
20503
0
      == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
20504
0
    only_data_sections = false;
20505
20506
0
        null_input_bfd = false;
20507
0
        break;
20508
0
      }
20509
0
  }
20510
20511
0
      if (null_input_bfd || only_data_sections)
20512
0
  return true;
20513
0
    }
20514
20515
  /* Complain about various flag mismatches.  */
20516
0
  if (!elf32_arm_versions_compatible (EF_ARM_EABI_VERSION (in_flags),
20517
0
              EF_ARM_EABI_VERSION (out_flags)))
20518
0
    {
20519
0
      _bfd_error_handler
20520
0
  (_("error: source object %pB has EABI version %d, but target %pB has EABI version %d"),
20521
0
   ibfd, (in_flags & EF_ARM_EABIMASK) >> 24,
20522
0
   obfd, (out_flags & EF_ARM_EABIMASK) >> 24);
20523
0
      return false;
20524
0
    }
20525
20526
  /* Not sure what needs to be checked for EABI versions >= 1.  */
20527
  /* VxWorks libraries do not use these flags.  */
20528
0
  if (get_elf_backend_data (obfd) != &elf32_arm_vxworks_bed
20529
0
      && get_elf_backend_data (ibfd) != &elf32_arm_vxworks_bed
20530
0
      && EF_ARM_EABI_VERSION (in_flags) == EF_ARM_EABI_UNKNOWN)
20531
0
    {
20532
0
      if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
20533
0
  {
20534
0
    _bfd_error_handler
20535
0
      (_("error: %pB is compiled for APCS-%d, whereas target %pB uses APCS-%d"),
20536
0
       ibfd, in_flags & EF_ARM_APCS_26 ? 26 : 32,
20537
0
       obfd, out_flags & EF_ARM_APCS_26 ? 26 : 32);
20538
0
    flags_compatible = false;
20539
0
  }
20540
20541
0
      if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
20542
0
  {
20543
0
    if (in_flags & EF_ARM_APCS_FLOAT)
20544
0
      _bfd_error_handler
20545
0
        (_("error: %pB passes floats in float registers, whereas %pB passes them in integer registers"),
20546
0
         ibfd, obfd);
20547
0
    else
20548
0
      _bfd_error_handler
20549
0
        (_("error: %pB passes floats in integer registers, whereas %pB passes them in float registers"),
20550
0
         ibfd, obfd);
20551
20552
0
    flags_compatible = false;
20553
0
  }
20554
20555
0
      if ((in_flags & EF_ARM_VFP_FLOAT) != (out_flags & EF_ARM_VFP_FLOAT))
20556
0
  {
20557
0
    if (in_flags & EF_ARM_VFP_FLOAT)
20558
0
      _bfd_error_handler
20559
0
        (_("error: %pB uses %s instructions, whereas %pB does not"),
20560
0
         ibfd, "VFP", obfd);
20561
0
    else
20562
0
      _bfd_error_handler
20563
0
        (_("error: %pB uses %s instructions, whereas %pB does not"),
20564
0
         ibfd, "FPA", obfd);
20565
20566
0
    flags_compatible = false;
20567
0
  }
20568
20569
0
#ifdef EF_ARM_SOFT_FLOAT
20570
0
      if ((in_flags & EF_ARM_SOFT_FLOAT) != (out_flags & EF_ARM_SOFT_FLOAT))
20571
0
  {
20572
    /* We can allow interworking between code that is VFP format
20573
       layout, and uses either soft float or integer regs for
20574
       passing floating point arguments and results.  We already
20575
       know that the APCS_FLOAT flags match; similarly for VFP
20576
       flags.  */
20577
0
    if ((in_flags & EF_ARM_APCS_FLOAT) != 0
20578
0
        || (in_flags & EF_ARM_VFP_FLOAT) == 0)
20579
0
      {
20580
0
        if (in_flags & EF_ARM_SOFT_FLOAT)
20581
0
    _bfd_error_handler
20582
0
      (_("error: %pB uses software FP, whereas %pB uses hardware FP"),
20583
0
       ibfd, obfd);
20584
0
        else
20585
0
    _bfd_error_handler
20586
0
      (_("error: %pB uses hardware FP, whereas %pB uses software FP"),
20587
0
       ibfd, obfd);
20588
20589
0
        flags_compatible = false;
20590
0
      }
20591
0
  }
20592
0
#endif
20593
20594
      /* Interworking mismatch is only a warning.  */
20595
0
      if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
20596
0
  {
20597
0
    if (in_flags & EF_ARM_INTERWORK)
20598
0
      {
20599
0
        _bfd_error_handler
20600
0
    (_("warning: %pB supports interworking, whereas %pB does not"),
20601
0
     ibfd, obfd);
20602
0
      }
20603
0
    else
20604
0
      {
20605
0
        _bfd_error_handler
20606
0
    (_("warning: %pB does not support interworking, whereas %pB does"),
20607
0
     ibfd, obfd);
20608
0
      }
20609
0
  }
20610
0
    }
20611
20612
0
  return flags_compatible;
20613
0
}