Coverage Report

Created: 2026-07-25 10:20

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
37
#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
2.34k
{
1960
2.34k
  if (r_type < ARRAY_SIZE (elf32_arm_howto_table_1))
1961
2.15k
    return &elf32_arm_howto_table_1[r_type];
1962
1963
185
  if (r_type >= R_ARM_IRELATIVE
1964
174
      && r_type < R_ARM_IRELATIVE + ARRAY_SIZE (elf32_arm_howto_table_2))
1965
116
    return &elf32_arm_howto_table_2[r_type - R_ARM_IRELATIVE];
1966
1967
69
  if (r_type >= R_ARM_RREL32
1968
36
      && r_type < R_ARM_RREL32 + ARRAY_SIZE (elf32_arm_howto_table_3))
1969
36
    return &elf32_arm_howto_table_3[r_type - R_ARM_RREL32];
1970
1971
33
  return NULL;
1972
69
}
1973
1974
static bool
1975
elf32_arm_info_to_howto (bfd * abfd, arelent * bfd_reloc,
1976
       Elf_Internal_Rela * elf_reloc)
1977
2.34k
{
1978
2.34k
  unsigned int r_type;
1979
1980
2.34k
  r_type = ELF32_R_TYPE (elf_reloc->r_info);
1981
2.34k
  if ((bfd_reloc->howto = elf32_arm_howto_from_type (r_type)) == NULL)
1982
33
    {
1983
      /* xgettext:c-format */
1984
33
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1985
33
        abfd, r_type);
1986
33
      bfd_set_error (bfd_error_bad_value);
1987
33
      return false;
1988
33
    }
1989
2.30k
  return true;
1990
2.34k
}
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_32_GOTOFF,      R_ARM_GOTOFF32},
2023
    {BFD_RELOC_32_GOT_PCREL,       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
37
  (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
3147
37
   && elf_tdata (bfd) != NULL \
3148
37
   && elf_object_id (bfd) == ARM_ELF_DATA)
3149
3150
static bool
3151
elf32_arm_mkobject (bfd *abfd)
3152
142k
{
3153
142k
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_arm_obj_tdata));
3154
142k
}
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_symtab_hdr (abfd).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_symtab_hdr (abfd).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)
3676
0
    && (arm_plt->thumb_refcount != 0
3677
0
        || (!htab->use_blx && arm_plt->maybe_thumb_refcount != 0)));
3678
0
}
3679
3680
/* Return a pointer to the head of the dynamic reloc list that should
3681
   be used for local symbol ISYM, which is symbol number R_SYMNDX in
3682
   ABFD's symbol table.  Return null if an error occurs.  */
3683
3684
static struct elf_dyn_relocs **
3685
elf32_arm_get_local_dynreloc_list (bfd *abfd, unsigned long r_symndx,
3686
           Elf_Internal_Sym *isym)
3687
0
{
3688
0
  if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
3689
0
    {
3690
0
      struct arm_local_iplt_info *local_iplt;
3691
3692
0
      local_iplt = elf32_arm_create_local_iplt (abfd, r_symndx);
3693
0
      if (local_iplt == NULL)
3694
0
  return NULL;
3695
0
      return &local_iplt->dyn_relocs;
3696
0
    }
3697
0
  else
3698
0
    {
3699
      /* Track dynamic relocs needed for local syms too.
3700
   We really need local syms available to do this
3701
   easily.  Oh well.  */
3702
0
      asection *s;
3703
0
      void *vpp;
3704
3705
0
      s = bfd_section_from_elf_index (abfd, isym->st_shndx);
3706
0
      if (s == NULL)
3707
0
  return NULL;
3708
3709
0
      vpp = &elf_section_data (s)->local_dynrel;
3710
0
      return (struct elf_dyn_relocs **) vpp;
3711
0
    }
3712
0
}
3713
3714
/* Initialize an entry in the stub hash table.  */
3715
3716
static struct bfd_hash_entry *
3717
stub_hash_newfunc (struct bfd_hash_entry *entry,
3718
       struct bfd_hash_table *table,
3719
       const char *string)
3720
0
{
3721
  /* Allocate the structure if it has not already been allocated by a
3722
     subclass.  */
3723
0
  if (entry == NULL)
3724
0
    {
3725
0
      entry = (struct bfd_hash_entry *)
3726
0
    bfd_hash_allocate (table, sizeof (struct elf32_arm_stub_hash_entry));
3727
0
      if (entry == NULL)
3728
0
  return entry;
3729
0
    }
3730
3731
  /* Call the allocation method of the superclass.  */
3732
0
  entry = bfd_hash_newfunc (entry, table, string);
3733
0
  if (entry != NULL)
3734
0
    {
3735
0
      struct elf32_arm_stub_hash_entry *eh;
3736
3737
      /* Initialize the local fields.  */
3738
0
      eh = (struct elf32_arm_stub_hash_entry *) entry;
3739
0
      eh->stub_sec = NULL;
3740
0
      eh->stub_offset = (bfd_vma) -1;
3741
0
      eh->source_value = 0;
3742
0
      eh->target_value = 0;
3743
0
      eh->target_section = NULL;
3744
0
      eh->orig_insn = 0;
3745
0
      eh->stub_type = arm_stub_none;
3746
0
      eh->stub_size = 0;
3747
0
      eh->stub_template = NULL;
3748
0
      eh->stub_template_size = -1;
3749
0
      eh->h = NULL;
3750
0
      eh->id_sec = NULL;
3751
0
      eh->output_name = NULL;
3752
0
    }
3753
3754
0
  return entry;
3755
0
}
3756
3757
/* Create .got, .gotplt, and .rel(a).got sections in DYNOBJ, and set up
3758
   shortcuts to them in our hash table.  */
3759
3760
static bool
3761
create_got_section (bfd *dynobj, struct bfd_link_info *info)
3762
0
{
3763
0
  struct elf32_arm_link_hash_table *htab;
3764
3765
0
  htab = elf32_arm_hash_table (info);
3766
0
  if (htab == NULL)
3767
0
    return false;
3768
3769
0
  if (! _bfd_elf_create_got_section (dynobj, info))
3770
0
    return false;
3771
3772
  /* Also create .rofixup.  */
3773
0
  if (htab->fdpic_p)
3774
0
    {
3775
0
      htab->srofixup = bfd_make_section_with_flags (dynobj, ".rofixup",
3776
0
                (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3777
0
                 | SEC_IN_MEMORY | SEC_LINKER_CREATED | SEC_READONLY));
3778
0
      if (htab->srofixup == NULL
3779
0
    || !bfd_set_section_alignment (htab->srofixup, 2))
3780
0
  return false;
3781
0
    }
3782
3783
0
  return true;
3784
0
}
3785
3786
/* Create the .iplt, .rel(a).iplt and .igot.plt sections.  */
3787
3788
static bool
3789
create_ifunc_sections (struct bfd_link_info *info)
3790
0
{
3791
0
  struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
3792
0
  bfd *dynobj = htab->root.dynobj;
3793
0
  elf_backend_data *bed = get_elf_backend_data (dynobj);
3794
0
  asection *s;
3795
0
  flagword flags = bed->dynamic_sec_flags;
3796
3797
0
  if (htab->root.iplt == NULL)
3798
0
    {
3799
0
      s = bfd_make_section_anyway_with_flags (dynobj, ".iplt",
3800
0
                flags | SEC_READONLY | SEC_CODE);
3801
0
      if (s == NULL
3802
0
    || !bfd_set_section_alignment (s, bed->plt_alignment))
3803
0
  return false;
3804
0
      htab->root.iplt = s;
3805
0
    }
3806
3807
0
  if (htab->root.irelplt == NULL)
3808
0
    {
3809
0
      s = bfd_make_section_anyway_with_flags (dynobj,
3810
0
                RELOC_SECTION (htab, ".iplt"),
3811
0
                flags | SEC_READONLY);
3812
0
      if (s == NULL
3813
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
3814
0
  return false;
3815
0
      htab->root.irelplt = s;
3816
0
    }
3817
3818
0
  if (htab->root.igotplt == NULL)
3819
0
    {
3820
0
      s = bfd_make_section_anyway_with_flags (dynobj, ".igot.plt", flags);
3821
0
      if (s == NULL
3822
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
3823
0
  return false;
3824
0
      htab->root.igotplt = s;
3825
0
    }
3826
0
  return true;
3827
0
}
3828
3829
/* Determine if we're dealing with a Thumb only architecture.  */
3830
3831
static bool
3832
using_thumb_only (struct elf32_arm_link_hash_table *globals)
3833
0
{
3834
0
  int arch;
3835
0
  int profile = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3836
0
            Tag_CPU_arch_profile);
3837
3838
0
  if (profile)
3839
0
    return profile == 'M';
3840
3841
0
  arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
3842
3843
  /* Force return logic to be reviewed for each new architecture.  */
3844
0
  BFD_ASSERT (arch <= TAG_CPU_ARCH_V8_1M_MAIN);
3845
3846
0
  if (arch == TAG_CPU_ARCH_V6_M
3847
0
      || arch == TAG_CPU_ARCH_V6S_M
3848
0
      || arch == TAG_CPU_ARCH_V7E_M
3849
0
      || arch == TAG_CPU_ARCH_V8M_BASE
3850
0
      || arch == TAG_CPU_ARCH_V8M_MAIN
3851
0
      || arch == TAG_CPU_ARCH_V8_1M_MAIN)
3852
0
    return true;
3853
3854
0
  return false;
3855
0
}
3856
3857
/* Determine if we're dealing with a Thumb-2 object.  */
3858
3859
static bool
3860
using_thumb2 (struct elf32_arm_link_hash_table *globals)
3861
0
{
3862
0
  int arch;
3863
0
  int thumb_isa = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3864
0
              Tag_THUMB_ISA_use);
3865
3866
  /* No use of thumb permitted, or a legacy thumb-1/2 definition.  */
3867
0
  if (thumb_isa < 3)
3868
0
    return thumb_isa == 2;
3869
3870
  /* Variant of thumb is described by the architecture tag.  */
3871
0
  arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
3872
3873
  /* Force return logic to be reviewed for each new architecture.  */
3874
0
  BFD_ASSERT (arch <= TAG_CPU_ARCH_V8_1M_MAIN);
3875
3876
0
  return (arch == TAG_CPU_ARCH_V6T2
3877
0
    || arch == TAG_CPU_ARCH_V7
3878
0
    || arch == TAG_CPU_ARCH_V7E_M
3879
0
    || arch == TAG_CPU_ARCH_V8A
3880
0
    || arch == TAG_CPU_ARCH_V8R
3881
0
    || arch == TAG_CPU_ARCH_V8M_MAIN
3882
0
    || arch == TAG_CPU_ARCH_V8_1M_MAIN);
3883
0
}
3884
3885
/* Determine whether Thumb-2 BL instruction is available.  */
3886
3887
static bool
3888
using_thumb2_bl (struct elf32_arm_link_hash_table *globals)
3889
0
{
3890
0
  int arch =
3891
0
    bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
3892
3893
  /* Force return logic to be reviewed for each new architecture.  */
3894
0
  BFD_ASSERT (arch <= TAG_CPU_ARCH_V9A);
3895
3896
  /* Architecture was introduced after ARMv6T2 (eg. ARMv6-M).  */
3897
0
  return (arch == TAG_CPU_ARCH_V6T2
3898
0
    || arch >= TAG_CPU_ARCH_V7);
3899
0
}
3900
3901
/* Create .plt, .rel(a).plt, .got, .got.plt, .rel(a).got, .dynbss, and
3902
   .rel(a).bss sections in DYNOBJ, and set up shortcuts to them in our
3903
   hash table.  */
3904
3905
static bool
3906
elf32_arm_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
3907
0
{
3908
0
  struct elf32_arm_link_hash_table *htab;
3909
3910
0
  htab = elf32_arm_hash_table (info);
3911
0
  if (htab == NULL)
3912
0
    return false;
3913
3914
0
  if (!htab->root.sgot && !create_got_section (dynobj, info))
3915
0
    return false;
3916
3917
0
  if (!_bfd_elf_create_dynamic_sections (dynobj, info))
3918
0
    return false;
3919
3920
0
#ifdef OBJ_MAYBE_ELF_VXWORKS
3921
0
  if (htab->root.target_os == is_vxworks
3922
0
      && !elf_vxworks_create_dynamic_sections (dynobj, info, &htab->srelplt2))
3923
0
    return false;
3924
0
#endif
3925
3926
0
  return true;
3927
0
}
3928
3929
/* Copy the extra info we tack onto an elf_link_hash_entry.  */
3930
3931
static void
3932
elf32_arm_copy_indirect_symbol (struct bfd_link_info *info,
3933
        struct elf_link_hash_entry *dir,
3934
        struct elf_link_hash_entry *ind)
3935
0
{
3936
0
  struct elf32_arm_link_hash_entry *edir, *eind;
3937
3938
0
  edir = (struct elf32_arm_link_hash_entry *) dir;
3939
0
  eind = (struct elf32_arm_link_hash_entry *) ind;
3940
3941
0
  if (ind->root.type == bfd_link_hash_indirect)
3942
0
    {
3943
      /* Copy over PLT info.  */
3944
0
      edir->plt.thumb_refcount += eind->plt.thumb_refcount;
3945
0
      eind->plt.thumb_refcount = 0;
3946
0
      edir->plt.maybe_thumb_refcount += eind->plt.maybe_thumb_refcount;
3947
0
      eind->plt.maybe_thumb_refcount = 0;
3948
0
      edir->plt.noncall_refcount += eind->plt.noncall_refcount;
3949
0
      eind->plt.noncall_refcount = 0;
3950
3951
      /* Copy FDPIC counters.  */
3952
0
      edir->fdpic_cnts.gotofffuncdesc_cnt += eind->fdpic_cnts.gotofffuncdesc_cnt;
3953
0
      edir->fdpic_cnts.gotfuncdesc_cnt += eind->fdpic_cnts.gotfuncdesc_cnt;
3954
0
      edir->fdpic_cnts.funcdesc_cnt += eind->fdpic_cnts.funcdesc_cnt;
3955
3956
      /* We should only allocate a function to .iplt once the final
3957
   symbol information is known.  */
3958
0
      BFD_ASSERT (!eind->is_iplt);
3959
3960
0
      if (dir->got.refcount <= 0)
3961
0
  {
3962
0
    edir->tls_type = eind->tls_type;
3963
0
    eind->tls_type = GOT_UNKNOWN;
3964
0
  }
3965
0
    }
3966
3967
0
  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3968
0
}
3969
3970
/* Destroy an ARM elf linker hash table.  */
3971
3972
static void
3973
elf32_arm_link_hash_table_free (bfd *obfd)
3974
0
{
3975
0
  struct elf32_arm_link_hash_table *ret
3976
0
    = (struct elf32_arm_link_hash_table *) obfd->link.hash;
3977
3978
0
  bfd_hash_table_free (&ret->stub_hash_table);
3979
0
  _bfd_elf_link_hash_table_free (obfd);
3980
0
}
3981
3982
/* Create an ARM elf linker hash table.  */
3983
3984
static struct bfd_link_hash_table *
3985
elf32_arm_link_hash_table_create (bfd *abfd)
3986
0
{
3987
0
  struct elf32_arm_link_hash_table *ret;
3988
0
  size_t amt = sizeof (struct elf32_arm_link_hash_table);
3989
3990
0
  ret = (struct elf32_arm_link_hash_table *) bfd_zmalloc (amt);
3991
0
  if (ret == NULL)
3992
0
    return NULL;
3993
3994
0
  if (!_bfd_elf_link_hash_table_init (& ret->root, abfd,
3995
0
              elf32_arm_link_hash_newfunc,
3996
0
              sizeof (struct elf32_arm_link_hash_entry)))
3997
0
    {
3998
0
      free (ret);
3999
0
      return NULL;
4000
0
    }
4001
4002
0
  ret->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
4003
0
  ret->stm32l4xx_fix = BFD_ARM_STM32L4XX_FIX_NONE;
4004
#ifdef FOUR_WORD_PLT
4005
  ret->plt_header_size = 16;
4006
  ret->plt_entry_size = 16;
4007
#else
4008
0
  ret->plt_header_size = 20;
4009
0
  ret->plt_entry_size = elf32_arm_use_long_plt_entry ? 16 : 12;
4010
0
#endif
4011
0
  ret->use_rel = true;
4012
0
  ret->obfd = abfd;
4013
0
  ret->fdpic_p = 0;
4014
4015
0
  if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
4016
0
          sizeof (struct elf32_arm_stub_hash_entry)))
4017
0
    {
4018
0
      _bfd_elf_link_hash_table_free (abfd);
4019
0
      return NULL;
4020
0
    }
4021
0
  ret->root.root.hash_table_free = elf32_arm_link_hash_table_free;
4022
4023
0
  return &ret->root.root;
4024
0
}
4025
4026
/* Determine what kind of NOPs are available.  */
4027
4028
static bool
4029
arch_has_arm_nop (struct elf32_arm_link_hash_table *globals)
4030
0
{
4031
0
  const int arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
4032
0
               Tag_CPU_arch);
4033
4034
  /* Force return logic to be reviewed for each new architecture.  */
4035
0
  BFD_ASSERT (arch <= TAG_CPU_ARCH_V9A);
4036
4037
0
  return (arch == TAG_CPU_ARCH_V6T2
4038
0
    || arch == TAG_CPU_ARCH_V6K
4039
0
    || arch == TAG_CPU_ARCH_V7
4040
0
    || arch == TAG_CPU_ARCH_V8A
4041
0
    || arch == TAG_CPU_ARCH_V8R
4042
0
    || arch == TAG_CPU_ARCH_V9A);
4043
0
}
4044
4045
static bool
4046
arm_stub_is_thumb (enum elf32_arm_stub_type stub_type)
4047
0
{
4048
0
  switch (stub_type)
4049
0
    {
4050
0
    case arm_stub_long_branch_thumb_only:
4051
0
    case arm_stub_long_branch_thumb2_only:
4052
0
    case arm_stub_long_branch_thumb2_only_pure:
4053
0
    case arm_stub_long_branch_v4t_thumb_arm:
4054
0
    case arm_stub_short_branch_v4t_thumb_arm:
4055
0
    case arm_stub_long_branch_v4t_thumb_arm_pic:
4056
0
    case arm_stub_long_branch_v4t_thumb_tls_pic:
4057
0
    case arm_stub_long_branch_thumb_only_pic:
4058
0
    case arm_stub_cmse_branch_thumb_only:
4059
0
      return true;
4060
0
    case arm_stub_none:
4061
0
      BFD_FAIL ();
4062
0
      return false;
4063
0
      break;
4064
0
    default:
4065
0
      return false;
4066
0
    }
4067
0
}
4068
4069
/* Determine the type of stub needed, if any, for a call.  */
4070
4071
static enum elf32_arm_stub_type
4072
arm_type_of_stub (struct bfd_link_info *info,
4073
      asection *input_sec,
4074
      const Elf_Internal_Rela *rel,
4075
      unsigned char st_type,
4076
      enum arm_st_branch_type *actual_branch_type,
4077
      struct elf32_arm_link_hash_entry *hash,
4078
      bfd_vma destination,
4079
      asection *sym_sec,
4080
      bfd *input_bfd,
4081
      const char *name)
4082
0
{
4083
0
  bfd_vma location;
4084
0
  bfd_signed_vma branch_offset;
4085
0
  unsigned int r_type;
4086
0
  struct elf32_arm_link_hash_table * globals;
4087
0
  bool thumb2, thumb2_bl, thumb_only;
4088
0
  enum elf32_arm_stub_type stub_type = arm_stub_none;
4089
0
  int use_plt = 0;
4090
0
  enum arm_st_branch_type branch_type = *actual_branch_type;
4091
0
  union gotplt_union *root_plt;
4092
0
  struct arm_plt_info *arm_plt;
4093
0
  int arch;
4094
0
  int thumb2_movw;
4095
4096
0
  if (branch_type == ST_BRANCH_LONG)
4097
0
    return stub_type;
4098
4099
0
  globals = elf32_arm_hash_table (info);
4100
0
  if (globals == NULL)
4101
0
    return stub_type;
4102
4103
0
  thumb_only = using_thumb_only (globals);
4104
0
  thumb2 = using_thumb2 (globals);
4105
0
  thumb2_bl = using_thumb2_bl (globals);
4106
4107
0
  arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
4108
4109
  /* True for architectures that implement the thumb2 movw instruction.  */
4110
0
  thumb2_movw = thumb2 || (arch  == TAG_CPU_ARCH_V8M_BASE);
4111
4112
  /* Determine where the call point is.  */
4113
0
  location = (input_sec->output_offset
4114
0
        + input_sec->output_section->vma
4115
0
        + rel->r_offset);
4116
4117
0
  r_type = ELF32_R_TYPE (rel->r_info);
4118
4119
  /* Don't pretend we know what stub to use (if any) when we target a
4120
     Thumb-only target and we don't know the actual destination
4121
     type.  */
4122
0
  if (branch_type == ST_BRANCH_UNKNOWN && thumb_only)
4123
0
    return stub_type;
4124
4125
  /* ST_BRANCH_TO_ARM is nonsense to thumb-only targets when we
4126
     are considering a function call relocation.  */
4127
0
  if (thumb_only && (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24
4128
0
         || r_type == R_ARM_THM_JUMP19)
4129
0
      && branch_type == ST_BRANCH_TO_ARM)
4130
0
    {
4131
0
      if (sym_sec == bfd_abs_section_ptr)
4132
  /* As an exception, assume that absolute symbols are of the
4133
     right kind (Thumb).  They are presumably defined in the
4134
     linker script, where it is not possible to declare them as
4135
     Thumb (and thus are seen as Arm mode). We'll inform the
4136
     user with a warning, though, in
4137
     elf32_arm_final_link_relocate. */
4138
0
  branch_type = ST_BRANCH_TO_THUMB;
4139
0
      else
4140
  /* Otherwise do not silently build a stub, and let the users
4141
     know they have to fix their code.  Indeed, we could decide
4142
     to insert a stub involving Arm code and/or BLX, leading to
4143
     a run-time crash.  */
4144
0
  return stub_type;
4145
0
    }
4146
4147
  /* For TLS call relocs, it is the caller's responsibility to provide
4148
     the address of the appropriate trampoline.  */
4149
0
  if (r_type != R_ARM_TLS_CALL
4150
0
      && r_type != R_ARM_THM_TLS_CALL
4151
0
      && elf32_arm_get_plt_info (input_bfd, globals, hash,
4152
0
         ELF32_R_SYM (rel->r_info), &root_plt,
4153
0
         &arm_plt)
4154
0
      && root_plt->offset != (bfd_vma) -1)
4155
0
    {
4156
0
      asection *splt;
4157
4158
0
      if (hash == NULL || hash->is_iplt)
4159
0
  splt = globals->root.iplt;
4160
0
      else
4161
0
  splt = globals->root.splt;
4162
0
      if (splt != NULL)
4163
0
  {
4164
0
    use_plt = 1;
4165
4166
    /* Note when dealing with PLT entries: the main PLT stub is in
4167
       ARM mode, so if the branch is in Thumb mode, another
4168
       Thumb->ARM stub will be inserted later just before the ARM
4169
       PLT stub. If a long branch stub is needed, we'll add a
4170
       Thumb->Arm one and branch directly to the ARM PLT entry.
4171
       Here, we have to check if a pre-PLT Thumb->ARM stub
4172
       is needed and if it will be close enough.  */
4173
4174
0
    destination = (splt->output_section->vma
4175
0
       + splt->output_offset
4176
0
       + root_plt->offset);
4177
0
    st_type = STT_FUNC;
4178
4179
    /* Thumb branch/call to PLT: it can become a branch to ARM
4180
       or to Thumb. We must perform the same checks and
4181
       corrections as in elf32_arm_final_link_relocate.  */
4182
0
    if ((r_type == R_ARM_THM_CALL)
4183
0
        || (r_type == R_ARM_THM_JUMP24))
4184
0
      {
4185
0
        if (globals->use_blx
4186
0
      && r_type == R_ARM_THM_CALL
4187
0
      && !thumb_only)
4188
0
    {
4189
      /* If the Thumb BLX instruction is available, convert
4190
         the BL to a BLX instruction to call the ARM-mode
4191
         PLT entry.  */
4192
0
      branch_type = ST_BRANCH_TO_ARM;
4193
0
    }
4194
0
        else
4195
0
    {
4196
0
      if (!thumb_only)
4197
        /* Target the Thumb stub before the ARM PLT entry.  */
4198
0
        destination -= PLT_THUMB_STUB_SIZE;
4199
0
      branch_type = ST_BRANCH_TO_THUMB;
4200
0
    }
4201
0
      }
4202
0
    else
4203
0
      {
4204
0
        branch_type = ST_BRANCH_TO_ARM;
4205
0
      }
4206
0
  }
4207
0
    }
4208
  /* Calls to STT_GNU_IFUNC symbols should go through a PLT.  */
4209
0
  BFD_ASSERT (st_type != STT_GNU_IFUNC);
4210
4211
0
  branch_offset = (bfd_signed_vma)(destination - location);
4212
4213
0
  if (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24
4214
0
      || r_type == R_ARM_THM_TLS_CALL || r_type == R_ARM_THM_JUMP19)
4215
0
    {
4216
      /* Handle cases where:
4217
   - this call goes too far (different Thumb/Thumb2 max
4218
     distance)
4219
   - it's a Thumb->Arm call and blx is not available, or it's a
4220
     Thumb->Arm branch (not bl). A stub is needed in this case,
4221
     but only if this call is not through a PLT entry. Indeed,
4222
     PLT stubs handle mode switching already.  */
4223
0
      if ((!thumb2_bl
4224
0
      && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
4225
0
    || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
4226
0
    || (thumb2_bl
4227
0
        && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
4228
0
      || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
4229
0
    || (thumb2
4230
0
        && (branch_offset > THM2_MAX_FWD_COND_BRANCH_OFFSET
4231
0
      || (branch_offset < THM2_MAX_BWD_COND_BRANCH_OFFSET))
4232
0
        && (r_type == R_ARM_THM_JUMP19))
4233
0
    || (branch_type == ST_BRANCH_TO_ARM
4234
0
        && (((r_type == R_ARM_THM_CALL
4235
0
        || r_type == R_ARM_THM_TLS_CALL) && !globals->use_blx)
4236
0
      || (r_type == R_ARM_THM_JUMP24)
4237
0
      || (r_type == R_ARM_THM_JUMP19))
4238
0
        && !use_plt))
4239
0
  {
4240
    /* If we need to insert a Thumb-Thumb long branch stub to a
4241
       PLT, use one that branches directly to the ARM PLT
4242
       stub. If we pretended we'd use the pre-PLT Thumb->ARM
4243
       stub, undo this now.  */
4244
0
    if ((branch_type == ST_BRANCH_TO_THUMB) && use_plt && !thumb_only)
4245
0
      {
4246
0
        branch_type = ST_BRANCH_TO_ARM;
4247
0
        branch_offset += PLT_THUMB_STUB_SIZE;
4248
0
      }
4249
4250
0
    if (branch_type == ST_BRANCH_TO_THUMB)
4251
0
      {
4252
        /* Thumb to thumb.  */
4253
0
        if (!thumb_only)
4254
0
    {
4255
0
      if (input_sec->flags & SEC_ELF_PURECODE)
4256
0
        _bfd_error_handler
4257
0
          (_("%pB(%pA): warning: long branch veneers used in"
4258
0
       " section with SHF_ARM_PURECODE section"
4259
0
       " attribute is only supported for M-profile"
4260
0
       " targets that implement the movw instruction"),
4261
0
           input_bfd, input_sec);
4262
4263
0
      stub_type = (bfd_link_pic (info) | globals->pic_veneer)
4264
        /* PIC stubs.  */
4265
0
        ? ((globals->use_blx
4266
0
      && (r_type == R_ARM_THM_CALL))
4267
           /* V5T and above. Stub starts with ARM code, so
4268
        we must be able to switch mode before
4269
        reaching it, which is only possible for 'bl'
4270
        (ie R_ARM_THM_CALL relocation).  */
4271
0
           ? arm_stub_long_branch_any_thumb_pic
4272
           /* On V4T, use Thumb code only.  */
4273
0
           : arm_stub_long_branch_v4t_thumb_thumb_pic)
4274
4275
        /* non-PIC stubs.  */
4276
0
        : ((globals->use_blx
4277
0
      && (r_type == R_ARM_THM_CALL))
4278
           /* V5T and above.  */
4279
0
           ? arm_stub_long_branch_any_any
4280
           /* V4T.  */
4281
0
           : arm_stub_long_branch_v4t_thumb_thumb);
4282
0
    }
4283
0
        else
4284
0
    {
4285
0
      if (thumb2_movw && (input_sec->flags & SEC_ELF_PURECODE))
4286
0
          stub_type = arm_stub_long_branch_thumb2_only_pure;
4287
0
      else
4288
0
        {
4289
0
          if (input_sec->flags & SEC_ELF_PURECODE)
4290
0
      _bfd_error_handler
4291
0
        (_("%pB(%pA): warning: long branch veneers used in"
4292
0
           " section with SHF_ARM_PURECODE section"
4293
0
           " attribute is only supported for M-profile"
4294
0
           " targets that implement the movw instruction"),
4295
0
         input_bfd, input_sec);
4296
4297
0
          stub_type = (bfd_link_pic (info) | globals->pic_veneer)
4298
      /* PIC stub.  */
4299
0
      ? arm_stub_long_branch_thumb_only_pic
4300
      /* non-PIC stub.  */
4301
0
      : (thumb2 ? arm_stub_long_branch_thumb2_only
4302
0
          : arm_stub_long_branch_thumb_only);
4303
0
        }
4304
0
    }
4305
0
      }
4306
0
    else
4307
0
      {
4308
0
        if (input_sec->flags & SEC_ELF_PURECODE)
4309
0
    _bfd_error_handler
4310
0
      (_("%pB(%pA): warning: long branch veneers used in"
4311
0
         " section with SHF_ARM_PURECODE section"
4312
0
         " attribute is only supported" " for M-profile"
4313
0
         " targets that implement the movw instruction"),
4314
0
       input_bfd, input_sec);
4315
4316
        /* Thumb to arm.  */
4317
0
        if (sym_sec != NULL
4318
0
      && sym_sec->owner != NULL
4319
0
      && !INTERWORK_FLAG (sym_sec->owner))
4320
0
    {
4321
0
      _bfd_error_handler
4322
0
        (_("%pB(%s): warning: interworking not enabled;"
4323
0
           " first occurrence: %pB: %s call to %s"),
4324
0
         sym_sec->owner, name, input_bfd, "Thumb", "ARM");
4325
0
    }
4326
4327
0
        stub_type =
4328
0
    (bfd_link_pic (info) | globals->pic_veneer)
4329
    /* PIC stubs.  */
4330
0
    ? (r_type == R_ARM_THM_TLS_CALL
4331
       /* TLS PIC stubs.  */
4332
0
       ? (globals->use_blx ? arm_stub_long_branch_any_tls_pic
4333
0
          : arm_stub_long_branch_v4t_thumb_tls_pic)
4334
0
       : ((globals->use_blx && r_type == R_ARM_THM_CALL)
4335
          /* V5T PIC and above.  */
4336
0
          ? arm_stub_long_branch_any_arm_pic
4337
          /* V4T PIC stub.  */
4338
0
          : arm_stub_long_branch_v4t_thumb_arm_pic))
4339
4340
    /* non-PIC stubs.  */
4341
0
    : ((globals->use_blx && r_type == R_ARM_THM_CALL)
4342
       /* V5T and above.  */
4343
0
       ? arm_stub_long_branch_any_any
4344
       /* V4T.  */
4345
0
       : arm_stub_long_branch_v4t_thumb_arm);
4346
4347
        /* Handle v4t short branches.  */
4348
0
        if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
4349
0
      && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
4350
0
      && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
4351
0
    stub_type = arm_stub_short_branch_v4t_thumb_arm;
4352
0
      }
4353
0
  }
4354
0
    }
4355
0
  else if (r_type == R_ARM_CALL
4356
0
     || r_type == R_ARM_JUMP24
4357
0
     || r_type == R_ARM_PLT32
4358
0
     || r_type == R_ARM_TLS_CALL)
4359
0
    {
4360
0
      if (input_sec->flags & SEC_ELF_PURECODE)
4361
0
  _bfd_error_handler
4362
0
    (_("%pB(%pA): warning: long branch veneers used in"
4363
0
       " section with SHF_ARM_PURECODE section"
4364
0
       " attribute is only supported for M-profile"
4365
0
       " targets that implement the movw instruction"),
4366
0
     input_bfd, input_sec);
4367
0
      if (branch_type == ST_BRANCH_TO_THUMB)
4368
0
  {
4369
    /* Arm to thumb.  */
4370
4371
0
    if (sym_sec != NULL
4372
0
        && sym_sec->owner != NULL
4373
0
        && !INTERWORK_FLAG (sym_sec->owner))
4374
0
      {
4375
0
        _bfd_error_handler
4376
0
    (_("%pB(%s): warning: interworking not enabled;"
4377
0
       " first occurrence: %pB: %s call to %s"),
4378
0
     sym_sec->owner, name, input_bfd, "ARM", "Thumb");
4379
0
      }
4380
4381
    /* We have an extra 2-bytes reach because of
4382
       the mode change (bit 24 (H) of BLX encoding).  */
4383
0
    if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
4384
0
        || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
4385
0
        || (r_type == R_ARM_CALL && !globals->use_blx)
4386
0
        || (r_type == R_ARM_JUMP24)
4387
0
        || (r_type == R_ARM_PLT32))
4388
0
      {
4389
0
        stub_type = (bfd_link_pic (info) | globals->pic_veneer)
4390
    /* PIC stubs.  */
4391
0
    ? ((globals->use_blx)
4392
       /* V5T and above.  */
4393
0
       ? arm_stub_long_branch_any_thumb_pic
4394
       /* V4T stub.  */
4395
0
       : arm_stub_long_branch_v4t_arm_thumb_pic)
4396
4397
    /* non-PIC stubs.  */
4398
0
    : ((globals->use_blx)
4399
       /* V5T and above.  */
4400
0
       ? arm_stub_long_branch_any_any
4401
       /* V4T.  */
4402
0
       : arm_stub_long_branch_v4t_arm_thumb);
4403
0
      }
4404
0
  }
4405
0
      else
4406
0
  {
4407
    /* Arm to arm.  */
4408
0
    if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
4409
0
        || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
4410
0
      {
4411
0
        stub_type =
4412
0
    (bfd_link_pic (info) | globals->pic_veneer)
4413
    /* PIC stubs.  */
4414
0
    ? (r_type == R_ARM_TLS_CALL
4415
       /* TLS PIC Stub.  */
4416
0
       ? arm_stub_long_branch_any_tls_pic
4417
0
       : arm_stub_long_branch_any_arm_pic)
4418
    /* non-PIC stubs.  */
4419
0
    : arm_stub_long_branch_any_any;
4420
0
      }
4421
0
  }
4422
0
    }
4423
4424
  /* If a stub is needed, record the actual destination type.  */
4425
0
  if (stub_type != arm_stub_none)
4426
0
    *actual_branch_type = branch_type;
4427
4428
0
  return stub_type;
4429
0
}
4430
4431
/* Build a name for an entry in the stub hash table.  */
4432
4433
static char *
4434
elf32_arm_stub_name (const asection *input_section,
4435
         const asection *sym_sec,
4436
         const struct elf32_arm_link_hash_entry *hash,
4437
         const Elf_Internal_Rela *rel,
4438
         enum elf32_arm_stub_type stub_type)
4439
0
{
4440
0
  char *stub_name;
4441
0
  bfd_size_type len;
4442
4443
0
  if (hash)
4444
0
    {
4445
0
      len = 8 + 1 + strlen (hash->root.root.root.string) + 1 + 8 + 1 + 2 + 1;
4446
0
      stub_name = (char *) bfd_malloc (len);
4447
0
      if (stub_name != NULL)
4448
0
  sprintf (stub_name, "%08x_%s+%x_%d",
4449
0
     input_section->id & 0xffffffff,
4450
0
     hash->root.root.root.string,
4451
0
     (int) rel->r_addend & 0xffffffff,
4452
0
     (int) stub_type);
4453
0
    }
4454
0
  else
4455
0
    {
4456
0
      len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1 + 2 + 1;
4457
0
      stub_name = (char *) bfd_malloc (len);
4458
0
      if (stub_name != NULL)
4459
0
  sprintf (stub_name, "%08x_%x:%x+%x_%d",
4460
0
     input_section->id & 0xffffffff,
4461
0
     sym_sec->id & 0xffffffff,
4462
0
     ELF32_R_TYPE (rel->r_info) == R_ARM_TLS_CALL
4463
0
     || ELF32_R_TYPE (rel->r_info) == R_ARM_THM_TLS_CALL
4464
0
     ? 0 : (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
4465
0
     (int) rel->r_addend & 0xffffffff,
4466
0
     (int) stub_type);
4467
0
    }
4468
4469
0
  return stub_name;
4470
0
}
4471
4472
/* Look up an entry in the stub hash.  Stub entries are cached because
4473
   creating the stub name takes a bit of time.  */
4474
4475
static struct elf32_arm_stub_hash_entry *
4476
elf32_arm_get_stub_entry (const asection *input_section,
4477
        const asection *sym_sec,
4478
        struct elf_link_hash_entry *hash,
4479
        const Elf_Internal_Rela *rel,
4480
        struct elf32_arm_link_hash_table *htab,
4481
        enum elf32_arm_stub_type stub_type)
4482
0
{
4483
0
  struct elf32_arm_stub_hash_entry *stub_entry;
4484
0
  struct elf32_arm_link_hash_entry *h = (struct elf32_arm_link_hash_entry *) hash;
4485
0
  const asection *id_sec;
4486
4487
0
  if ((input_section->flags & SEC_CODE) == 0)
4488
0
    return NULL;
4489
4490
  /* If the input section is the CMSE stubs one and it needs a long
4491
     branch stub to reach it's final destination, give up with an
4492
     error message: this is not supported.  See PR ld/24709.  */
4493
0
  if (!strncmp (input_section->name, CMSE_STUB_NAME, strlen (CMSE_STUB_NAME)))
4494
0
    {
4495
0
      bfd *output_bfd = htab->obfd;
4496
0
      asection *out_sec = bfd_get_section_by_name (output_bfd, CMSE_STUB_NAME);
4497
4498
0
      _bfd_error_handler (_("ERROR: CMSE stub (%s section) too far "
4499
0
          "(%#" PRIx64 ") from destination (%#" PRIx64 ")"),
4500
0
        CMSE_STUB_NAME,
4501
0
        (uint64_t)out_sec->output_section->vma
4502
0
          + out_sec->output_offset,
4503
0
        (uint64_t)sym_sec->output_section->vma
4504
0
          + sym_sec->output_offset
4505
0
          + h->root.root.u.def.value);
4506
      /* Exit, rather than leave incompletely processed
4507
   relocations.  */
4508
0
      xexit (1);
4509
0
    }
4510
4511
  /* If this input section is part of a group of sections sharing one
4512
     stub section, then use the id of the first section in the group.
4513
     Stub names need to include a section id, as there may well be
4514
     more than one stub used to reach say, printf, and we need to
4515
     distinguish between them.  */
4516
0
  BFD_ASSERT (input_section->id <= htab->top_id);
4517
0
  id_sec = htab->stub_group[input_section->id].link_sec;
4518
4519
0
  if (h != NULL && h->stub_cache != NULL
4520
0
      && h->stub_cache->h == h
4521
0
      && h->stub_cache->id_sec == id_sec
4522
0
      && h->stub_cache->stub_type == stub_type)
4523
0
    {
4524
0
      stub_entry = h->stub_cache;
4525
0
    }
4526
0
  else
4527
0
    {
4528
0
      char *stub_name;
4529
4530
0
      stub_name = elf32_arm_stub_name (id_sec, sym_sec, h, rel, stub_type);
4531
0
      if (stub_name == NULL)
4532
0
  return NULL;
4533
4534
0
      stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table,
4535
0
          stub_name, false, false);
4536
0
      if (h != NULL)
4537
0
  h->stub_cache = stub_entry;
4538
4539
0
      free (stub_name);
4540
0
    }
4541
4542
0
  return stub_entry;
4543
0
}
4544
4545
/* Whether veneers of type STUB_TYPE require to be in a dedicated output
4546
   section.  */
4547
4548
static bool
4549
arm_dedicated_stub_output_section_required (enum elf32_arm_stub_type stub_type)
4550
0
{
4551
0
  if (stub_type >= max_stub_type)
4552
0
    abort ();  /* Should be unreachable.  */
4553
4554
0
  switch (stub_type)
4555
0
    {
4556
0
    case arm_stub_cmse_branch_thumb_only:
4557
0
      return true;
4558
4559
0
    default:
4560
0
      return false;
4561
0
    }
4562
4563
0
  abort ();  /* Should be unreachable.  */
4564
0
}
4565
4566
/* Required alignment (as a power of 2) for the dedicated section holding
4567
   veneers of type STUB_TYPE, or 0 if veneers of this type are interspersed
4568
   with input sections.  */
4569
4570
static int
4571
arm_dedicated_stub_output_section_required_alignment
4572
  (enum elf32_arm_stub_type stub_type)
4573
0
{
4574
0
  if (stub_type >= max_stub_type)
4575
0
    abort ();  /* Should be unreachable.  */
4576
4577
0
  switch (stub_type)
4578
0
    {
4579
    /* Vectors of Secure Gateway veneers must be aligned on 32byte
4580
       boundary.  */
4581
0
    case arm_stub_cmse_branch_thumb_only:
4582
0
      return 5;
4583
4584
0
    default:
4585
0
      BFD_ASSERT (!arm_dedicated_stub_output_section_required (stub_type));
4586
0
      return 0;
4587
0
    }
4588
4589
0
  abort ();  /* Should be unreachable.  */
4590
0
}
4591
4592
/* Name of the dedicated output section to put veneers of type STUB_TYPE, or
4593
   NULL if veneers of this type are interspersed with input sections.  */
4594
4595
static const char *
4596
arm_dedicated_stub_output_section_name (enum elf32_arm_stub_type stub_type)
4597
0
{
4598
0
  if (stub_type >= max_stub_type)
4599
0
    abort ();  /* Should be unreachable.  */
4600
4601
0
  switch (stub_type)
4602
0
    {
4603
0
    case arm_stub_cmse_branch_thumb_only:
4604
0
      return CMSE_STUB_NAME;
4605
4606
0
    default:
4607
0
      BFD_ASSERT (!arm_dedicated_stub_output_section_required (stub_type));
4608
0
      return NULL;
4609
0
    }
4610
4611
0
  abort ();  /* Should be unreachable.  */
4612
0
}
4613
4614
/* If veneers of type STUB_TYPE should go in a dedicated output section,
4615
   returns the address of the hash table field in HTAB holding a pointer to the
4616
   corresponding input section.  Otherwise, returns NULL.  */
4617
4618
static asection **
4619
arm_dedicated_stub_input_section_ptr (struct elf32_arm_link_hash_table *htab,
4620
              enum elf32_arm_stub_type stub_type)
4621
0
{
4622
0
  if (stub_type >= max_stub_type)
4623
0
    abort ();  /* Should be unreachable.  */
4624
4625
0
  switch (stub_type)
4626
0
    {
4627
0
    case arm_stub_cmse_branch_thumb_only:
4628
0
      return &htab->cmse_stub_sec;
4629
4630
0
    default:
4631
0
      BFD_ASSERT (!arm_dedicated_stub_output_section_required (stub_type));
4632
0
      return NULL;
4633
0
    }
4634
4635
0
  abort ();  /* Should be unreachable.  */
4636
0
}
4637
4638
/* Find or create a stub section to contain a stub of type STUB_TYPE.  SECTION
4639
   is the section that branch into veneer and can be NULL if stub should go in
4640
   a dedicated output section.  Returns a pointer to the stub section, and the
4641
   section to which the stub section will be attached (in *LINK_SEC_P).
4642
   LINK_SEC_P may be NULL.  */
4643
4644
static asection *
4645
elf32_arm_create_or_find_stub_sec (asection **link_sec_p, asection *section,
4646
           struct elf32_arm_link_hash_table *htab,
4647
           enum elf32_arm_stub_type stub_type)
4648
0
{
4649
0
  asection *link_sec, *out_sec, **stub_sec_p;
4650
0
  const char *stub_sec_prefix;
4651
0
  bool dedicated_output_section =
4652
0
    arm_dedicated_stub_output_section_required (stub_type);
4653
0
  int align;
4654
4655
0
  if (dedicated_output_section)
4656
0
    {
4657
0
      bfd *output_bfd = htab->obfd;
4658
0
      const char *out_sec_name =
4659
0
  arm_dedicated_stub_output_section_name (stub_type);
4660
0
      link_sec = NULL;
4661
0
      stub_sec_p = arm_dedicated_stub_input_section_ptr (htab, stub_type);
4662
0
      stub_sec_prefix = out_sec_name;
4663
0
      align = arm_dedicated_stub_output_section_required_alignment (stub_type);
4664
0
      out_sec = bfd_get_section_by_name (output_bfd, out_sec_name);
4665
0
      if (out_sec == NULL)
4666
0
  {
4667
0
    _bfd_error_handler (_("no address assigned to the veneers output "
4668
0
        "section %s"), out_sec_name);
4669
0
    return NULL;
4670
0
  }
4671
0
    }
4672
0
  else
4673
0
    {
4674
0
      BFD_ASSERT (section->id <= htab->top_id);
4675
0
      link_sec = htab->stub_group[section->id].link_sec;
4676
0
      BFD_ASSERT (link_sec != NULL);
4677
0
      stub_sec_p = &htab->stub_group[section->id].stub_sec;
4678
0
      if (*stub_sec_p == NULL)
4679
0
  stub_sec_p = &htab->stub_group[link_sec->id].stub_sec;
4680
0
      stub_sec_prefix = link_sec->name;
4681
0
      out_sec = link_sec->output_section;
4682
0
      align = 3;
4683
0
    }
4684
4685
0
  if (*stub_sec_p == NULL)
4686
0
    {
4687
0
      size_t namelen;
4688
0
      bfd_size_type len;
4689
0
      char *s_name;
4690
4691
0
      namelen = strlen (stub_sec_prefix);
4692
0
      len = namelen + sizeof (STUB_SUFFIX);
4693
0
      s_name = (char *) bfd_alloc (htab->stub_bfd, len);
4694
0
      if (s_name == NULL)
4695
0
  return NULL;
4696
4697
0
      memcpy (s_name, stub_sec_prefix, namelen);
4698
0
      memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4699
0
      *stub_sec_p = (*htab->add_stub_section) (s_name, out_sec, link_sec,
4700
0
                 align);
4701
0
      if (*stub_sec_p == NULL)
4702
0
  return NULL;
4703
4704
0
      out_sec->flags |= SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
4705
0
      | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY
4706
0
      | SEC_KEEP;
4707
0
    }
4708
4709
0
  if (!dedicated_output_section)
4710
0
    htab->stub_group[section->id].stub_sec = *stub_sec_p;
4711
4712
0
  if (link_sec_p)
4713
0
    *link_sec_p = link_sec;
4714
4715
0
  return *stub_sec_p;
4716
0
}
4717
4718
/* Add a new stub entry to the stub hash.  Not all fields of the new
4719
   stub entry are initialised.  */
4720
4721
static struct elf32_arm_stub_hash_entry *
4722
elf32_arm_add_stub (const char *stub_name, asection *section,
4723
        struct elf32_arm_link_hash_table *htab,
4724
        enum elf32_arm_stub_type stub_type)
4725
0
{
4726
0
  asection *link_sec;
4727
0
  asection *stub_sec;
4728
0
  struct elf32_arm_stub_hash_entry *stub_entry;
4729
4730
0
  stub_sec = elf32_arm_create_or_find_stub_sec (&link_sec, section, htab,
4731
0
            stub_type);
4732
0
  if (stub_sec == NULL)
4733
0
    return NULL;
4734
4735
  /* Enter this entry into the linker stub hash table.  */
4736
0
  stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4737
0
             true, false);
4738
0
  if (stub_entry == NULL)
4739
0
    {
4740
0
      if (section == NULL)
4741
0
  section = stub_sec;
4742
0
      _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4743
0
        section->owner, stub_name);
4744
0
      return NULL;
4745
0
    }
4746
4747
0
  stub_entry->stub_sec = stub_sec;
4748
0
  stub_entry->stub_offset = (bfd_vma) -1;
4749
0
  stub_entry->id_sec = link_sec;
4750
4751
0
  return stub_entry;
4752
0
}
4753
4754
/* Store an Arm insn into an output section not processed by
4755
   elf32_arm_write_section.  */
4756
4757
static void
4758
put_arm_insn (struct elf32_arm_link_hash_table * htab,
4759
        bfd * output_bfd, bfd_vma val, void * ptr)
4760
0
{
4761
0
  if (htab->byteswap_code != bfd_little_endian (output_bfd))
4762
0
    bfd_putl32 (val, ptr);
4763
0
  else
4764
0
    bfd_putb32 (val, ptr);
4765
0
}
4766
4767
/* Store a 16-bit Thumb insn into an output section not processed by
4768
   elf32_arm_write_section.  */
4769
4770
static void
4771
put_thumb_insn (struct elf32_arm_link_hash_table * htab,
4772
    bfd * output_bfd, bfd_vma val, void * ptr)
4773
0
{
4774
0
  if (htab->byteswap_code != bfd_little_endian (output_bfd))
4775
0
    bfd_putl16 (val, ptr);
4776
0
  else
4777
0
    bfd_putb16 (val, ptr);
4778
0
}
4779
4780
/* Store a Thumb2 insn into an output section not processed by
4781
   elf32_arm_write_section.  */
4782
4783
static void
4784
put_thumb2_insn (struct elf32_arm_link_hash_table * htab,
4785
     bfd * output_bfd, bfd_vma val, bfd_byte * ptr)
4786
0
{
4787
  /* T2 instructions are 16-bit streamed.  */
4788
0
  if (htab->byteswap_code != bfd_little_endian (output_bfd))
4789
0
    {
4790
0
      bfd_putl16 ((val >> 16) & 0xffff, ptr);
4791
0
      bfd_putl16 ((val & 0xffff), ptr + 2);
4792
0
    }
4793
0
  else
4794
0
    {
4795
0
      bfd_putb16 ((val >> 16) & 0xffff, ptr);
4796
0
      bfd_putb16 ((val & 0xffff), ptr + 2);
4797
0
    }
4798
0
}
4799
4800
/* If it's possible to change R_TYPE to a more efficient access
4801
   model, return the new reloc type.  */
4802
4803
static unsigned
4804
elf32_arm_tls_transition (struct bfd_link_info *info, int r_type,
4805
        struct elf_link_hash_entry *h)
4806
0
{
4807
0
  int is_local = (h == NULL);
4808
4809
0
  if (bfd_link_dll (info)
4810
0
      || (h && h->root.type == bfd_link_hash_undefweak))
4811
0
    return r_type;
4812
4813
  /* We do not support relaxations for Old TLS models.  */
4814
0
  switch (r_type)
4815
0
    {
4816
0
    case R_ARM_TLS_GOTDESC:
4817
0
    case R_ARM_TLS_CALL:
4818
0
    case R_ARM_THM_TLS_CALL:
4819
0
    case R_ARM_TLS_DESCSEQ:
4820
0
    case R_ARM_THM_TLS_DESCSEQ:
4821
0
      return is_local ? R_ARM_TLS_LE32 : R_ARM_TLS_IE32;
4822
0
    }
4823
4824
0
  return r_type;
4825
0
}
4826
4827
static bfd_reloc_status_type elf32_arm_final_link_relocate
4828
  (reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *,
4829
   Elf_Internal_Rela *, bfd_vma, struct bfd_link_info *, asection *,
4830
   const char *, unsigned char, enum arm_st_branch_type,
4831
   struct elf_link_hash_entry *, bool *, char **);
4832
4833
static unsigned int
4834
arm_stub_required_alignment (enum elf32_arm_stub_type stub_type)
4835
0
{
4836
0
  switch (stub_type)
4837
0
    {
4838
0
    case arm_stub_a8_veneer_b_cond:
4839
0
    case arm_stub_a8_veneer_b:
4840
0
    case arm_stub_a8_veneer_bl:
4841
0
      return 2;
4842
4843
0
    case arm_stub_long_branch_any_any:
4844
0
    case arm_stub_long_branch_v4t_arm_thumb:
4845
0
    case arm_stub_long_branch_thumb_only:
4846
0
    case arm_stub_long_branch_thumb2_only:
4847
0
    case arm_stub_long_branch_thumb2_only_pure:
4848
0
    case arm_stub_long_branch_v4t_thumb_thumb:
4849
0
    case arm_stub_long_branch_v4t_thumb_arm:
4850
0
    case arm_stub_short_branch_v4t_thumb_arm:
4851
0
    case arm_stub_long_branch_any_arm_pic:
4852
0
    case arm_stub_long_branch_any_thumb_pic:
4853
0
    case arm_stub_long_branch_v4t_thumb_thumb_pic:
4854
0
    case arm_stub_long_branch_v4t_arm_thumb_pic:
4855
0
    case arm_stub_long_branch_v4t_thumb_arm_pic:
4856
0
    case arm_stub_long_branch_thumb_only_pic:
4857
0
    case arm_stub_long_branch_any_tls_pic:
4858
0
    case arm_stub_long_branch_v4t_thumb_tls_pic:
4859
0
    case arm_stub_cmse_branch_thumb_only:
4860
0
    case arm_stub_a8_veneer_blx:
4861
0
      return 4;
4862
4863
0
    default:
4864
0
      abort ();  /* Should be unreachable.  */
4865
0
    }
4866
0
}
4867
4868
/* Returns whether stubs of type STUB_TYPE take over the symbol they are
4869
   veneering (TRUE) or have their own symbol (FALSE).  */
4870
4871
static bool
4872
arm_stub_sym_claimed (enum elf32_arm_stub_type stub_type)
4873
0
{
4874
0
  if (stub_type >= max_stub_type)
4875
0
    abort ();  /* Should be unreachable.  */
4876
4877
0
  switch (stub_type)
4878
0
    {
4879
0
    case arm_stub_cmse_branch_thumb_only:
4880
0
      return true;
4881
4882
0
    default:
4883
0
      return false;
4884
0
    }
4885
4886
0
  abort ();  /* Should be unreachable.  */
4887
0
}
4888
4889
/* Returns the padding needed for the dedicated section used stubs of type
4890
   STUB_TYPE.  */
4891
4892
static int
4893
arm_dedicated_stub_section_padding (enum elf32_arm_stub_type stub_type)
4894
0
{
4895
0
  if (stub_type >= max_stub_type)
4896
0
    abort ();  /* Should be unreachable.  */
4897
4898
0
  switch (stub_type)
4899
0
    {
4900
0
    case arm_stub_cmse_branch_thumb_only:
4901
0
      return 32;
4902
4903
0
    default:
4904
0
      return 0;
4905
0
    }
4906
4907
0
  abort ();  /* Should be unreachable.  */
4908
0
}
4909
4910
/* If veneers of type STUB_TYPE should go in a dedicated output section,
4911
   returns the address of the hash table field in HTAB holding the offset at
4912
   which new veneers should be layed out in the stub section.  */
4913
4914
static bfd_vma*
4915
arm_new_stubs_start_offset_ptr (struct elf32_arm_link_hash_table *htab,
4916
        enum elf32_arm_stub_type stub_type)
4917
0
{
4918
0
  switch (stub_type)
4919
0
    {
4920
0
    case arm_stub_cmse_branch_thumb_only:
4921
0
      return &htab->new_cmse_stub_offset;
4922
4923
0
    default:
4924
0
      BFD_ASSERT (!arm_dedicated_stub_output_section_required (stub_type));
4925
0
      return NULL;
4926
0
    }
4927
0
}
4928
4929
static bool
4930
arm_build_one_stub (struct bfd_hash_entry *gen_entry,
4931
        void * in_arg)
4932
0
{
4933
0
#define MAXRELOCS 3
4934
0
  bool removed_sg_veneer;
4935
0
  struct elf32_arm_stub_hash_entry *stub_entry;
4936
0
  struct elf32_arm_link_hash_table *globals;
4937
0
  struct bfd_link_info *info;
4938
0
  asection *stub_sec;
4939
0
  bfd *stub_bfd;
4940
0
  bfd_byte *loc;
4941
0
  bfd_vma sym_value;
4942
0
  int template_size;
4943
0
  int size;
4944
0
  const insn_sequence *template_sequence;
4945
0
  int i;
4946
0
  int stub_reloc_idx[MAXRELOCS] = {-1, -1};
4947
0
  int stub_reloc_offset[MAXRELOCS] = {0, 0};
4948
0
  int nrelocs = 0;
4949
0
  int just_allocated = 0;
4950
4951
  /* Massage our args to the form they really have.  */
4952
0
  stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
4953
0
  info = (struct bfd_link_info *) in_arg;
4954
4955
  /* Fail if the target section could not be assigned to an output
4956
     section.  The user should fix his linker script.  */
4957
0
  if (stub_entry->target_section->output_section == NULL
4958
0
      && info->non_contiguous_regions)
4959
0
    info->callbacks->fatal (_("%P: Could not assign `%pA' to an output section. "
4960
0
            "Retry without --enable-non-contiguous-regions.\n"),
4961
0
          stub_entry->target_section);
4962
4963
0
  globals = elf32_arm_hash_table (info);
4964
0
  if (globals == NULL)
4965
0
    return false;
4966
4967
0
  stub_sec = stub_entry->stub_sec;
4968
4969
0
  if ((globals->fix_cortex_a8 < 0)
4970
0
      != (arm_stub_required_alignment (stub_entry->stub_type) == 2))
4971
    /* We have to do less-strictly-aligned fixes last.  */
4972
0
    return true;
4973
4974
  /* Assign a slot at the end of section if none assigned yet.  */
4975
0
  if (stub_entry->stub_offset == (bfd_vma) -1)
4976
0
    {
4977
0
      stub_entry->stub_offset = stub_sec->size;
4978
0
      just_allocated = 1;
4979
0
    }
4980
0
  loc = stub_sec->contents + stub_entry->stub_offset;
4981
4982
0
  stub_bfd = stub_sec->owner;
4983
4984
  /* This is the address of the stub destination.  */
4985
0
  sym_value = (stub_entry->target_value
4986
0
         + stub_entry->target_section->output_offset
4987
0
         + stub_entry->target_section->output_section->vma);
4988
4989
0
  template_sequence = stub_entry->stub_template;
4990
0
  template_size = stub_entry->stub_template_size;
4991
4992
0
  size = 0;
4993
0
  for (i = 0; i < template_size; i++)
4994
0
    {
4995
0
      switch (template_sequence[i].type)
4996
0
  {
4997
0
  case THUMB16_TYPE:
4998
0
    {
4999
0
      bfd_vma data = (bfd_vma) template_sequence[i].data;
5000
0
      if (template_sequence[i].reloc_addend != 0)
5001
0
        {
5002
    /* We've borrowed the reloc_addend field to mean we should
5003
       insert a condition code into this (Thumb-1 branch)
5004
       instruction.  See THUMB16_BCOND_INSN.  */
5005
0
    BFD_ASSERT ((data & 0xff00) == 0xd000);
5006
0
    data |= ((stub_entry->orig_insn >> 22) & 0xf) << 8;
5007
0
        }
5008
0
      bfd_put_16 (stub_bfd, data, loc + size);
5009
0
      size += 2;
5010
0
    }
5011
0
    break;
5012
5013
0
  case THUMB32_TYPE:
5014
0
    bfd_put_16 (stub_bfd,
5015
0
          (template_sequence[i].data >> 16) & 0xffff,
5016
0
          loc + size);
5017
0
    bfd_put_16 (stub_bfd, template_sequence[i].data & 0xffff,
5018
0
          loc + size + 2);
5019
0
    if (template_sequence[i].r_type != R_ARM_NONE)
5020
0
      {
5021
0
        stub_reloc_idx[nrelocs] = i;
5022
0
        stub_reloc_offset[nrelocs++] = size;
5023
0
      }
5024
0
    size += 4;
5025
0
    break;
5026
5027
0
  case ARM_TYPE:
5028
0
    bfd_put_32 (stub_bfd, template_sequence[i].data,
5029
0
          loc + size);
5030
    /* Handle cases where the target is encoded within the
5031
       instruction.  */
5032
0
    if (template_sequence[i].r_type == R_ARM_JUMP24)
5033
0
      {
5034
0
        stub_reloc_idx[nrelocs] = i;
5035
0
        stub_reloc_offset[nrelocs++] = size;
5036
0
      }
5037
0
    size += 4;
5038
0
    break;
5039
5040
0
  case DATA_TYPE:
5041
0
    bfd_put_32 (stub_bfd, template_sequence[i].data, loc + size);
5042
0
    stub_reloc_idx[nrelocs] = i;
5043
0
    stub_reloc_offset[nrelocs++] = size;
5044
0
    size += 4;
5045
0
    break;
5046
5047
0
  default:
5048
0
    BFD_FAIL ();
5049
0
    return false;
5050
0
  }
5051
0
    }
5052
5053
0
  if (just_allocated)
5054
0
    stub_sec->size += size;
5055
5056
  /* Stub size has already been computed in arm_size_one_stub. Check
5057
     consistency.  */
5058
0
  BFD_ASSERT (size == stub_entry->stub_size);
5059
5060
  /* Destination is Thumb. Force bit 0 to 1 to reflect this.  */
5061
0
  if (stub_entry->branch_type == ST_BRANCH_TO_THUMB)
5062
0
    sym_value |= 1;
5063
5064
  /* Assume non empty slots have at least one and at most MAXRELOCS entries
5065
     to relocate in each stub.  */
5066
0
  removed_sg_veneer =
5067
0
    (size == 0 && stub_entry->stub_type == arm_stub_cmse_branch_thumb_only);
5068
0
  BFD_ASSERT (removed_sg_veneer || (nrelocs != 0 && nrelocs <= MAXRELOCS));
5069
5070
0
  for (i = 0; i < nrelocs; i++)
5071
0
    {
5072
0
      Elf_Internal_Rela rel;
5073
0
      bool unresolved_reloc;
5074
0
      char *error_message;
5075
0
      bfd_vma points_to =
5076
0
  sym_value + template_sequence[stub_reloc_idx[i]].reloc_addend;
5077
5078
0
      rel.r_offset = stub_entry->stub_offset + stub_reloc_offset[i];
5079
0
      rel.r_info = ELF32_R_INFO (0,
5080
0
         template_sequence[stub_reloc_idx[i]].r_type);
5081
0
      rel.r_addend = 0;
5082
5083
0
      if (stub_entry->stub_type == arm_stub_a8_veneer_b_cond && i == 0)
5084
  /* The first relocation in the elf32_arm_stub_a8_veneer_b_cond[]
5085
     template should refer back to the instruction after the original
5086
     branch.  We use target_section as Cortex-A8 erratum workaround stubs
5087
     are only generated when both source and target are in the same
5088
     section.  */
5089
0
  points_to = stub_entry->target_section->output_section->vma
5090
0
        + stub_entry->target_section->output_offset
5091
0
        + stub_entry->source_value;
5092
5093
0
      elf32_arm_final_link_relocate (elf32_arm_howto_from_type
5094
0
    (template_sequence[stub_reloc_idx[i]].r_type),
5095
0
     stub_bfd, info->output_bfd, stub_sec, stub_sec->contents, &rel,
5096
0
     points_to, info, stub_entry->target_section, "", STT_FUNC,
5097
0
     stub_entry->branch_type,
5098
0
     (struct elf_link_hash_entry *) stub_entry->h, &unresolved_reloc,
5099
0
     &error_message);
5100
0
    }
5101
5102
0
  return true;
5103
0
#undef MAXRELOCS
5104
0
}
5105
5106
/* Calculate the template, template size and instruction size for a stub.
5107
   Return value is the instruction size.  */
5108
5109
static unsigned int
5110
find_stub_size_and_template (enum elf32_arm_stub_type stub_type,
5111
           const insn_sequence **stub_template,
5112
           int *stub_template_size)
5113
0
{
5114
0
  const insn_sequence *template_sequence = NULL;
5115
0
  int template_size = 0, i;
5116
0
  unsigned int size;
5117
5118
0
  template_sequence = stub_definitions[stub_type].template_sequence;
5119
0
  if (stub_template)
5120
0
    *stub_template = template_sequence;
5121
5122
0
  template_size = stub_definitions[stub_type].template_size;
5123
0
  if (stub_template_size)
5124
0
    *stub_template_size = template_size;
5125
5126
0
  size = 0;
5127
0
  for (i = 0; i < template_size; i++)
5128
0
    {
5129
0
      switch (template_sequence[i].type)
5130
0
  {
5131
0
  case THUMB16_TYPE:
5132
0
    size += 2;
5133
0
    break;
5134
5135
0
  case ARM_TYPE:
5136
0
  case THUMB32_TYPE:
5137
0
  case DATA_TYPE:
5138
0
    size += 4;
5139
0
    break;
5140
5141
0
  default:
5142
0
    BFD_FAIL ();
5143
0
    return 0;
5144
0
  }
5145
0
    }
5146
5147
0
  return size;
5148
0
}
5149
5150
/* As above, but don't actually build the stub.  Just bump offset so
5151
   we know stub section sizes.  */
5152
5153
static bool
5154
arm_size_one_stub (struct bfd_hash_entry *gen_entry,
5155
       void *in_arg ATTRIBUTE_UNUSED)
5156
0
{
5157
0
  struct elf32_arm_stub_hash_entry *stub_entry;
5158
0
  const insn_sequence *template_sequence;
5159
0
  int template_size, size;
5160
5161
  /* Massage our args to the form they really have.  */
5162
0
  stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
5163
5164
0
  BFD_ASSERT ((stub_entry->stub_type > arm_stub_none)
5165
0
        && stub_entry->stub_type < ARRAY_SIZE (stub_definitions));
5166
5167
0
  size = find_stub_size_and_template (stub_entry->stub_type, &template_sequence,
5168
0
              &template_size);
5169
5170
  /* Initialized to -1.  Null size indicates an empty slot full of zeros.  */
5171
0
  if (stub_entry->stub_template_size)
5172
0
    {
5173
0
      stub_entry->stub_size = size;
5174
0
      stub_entry->stub_template = template_sequence;
5175
0
      stub_entry->stub_template_size = template_size;
5176
0
    }
5177
5178
  /* Already accounted for.  */
5179
0
  if (stub_entry->stub_offset != (bfd_vma) -1)
5180
0
    return true;
5181
5182
0
  size = (size + 7) & ~7;
5183
0
  stub_entry->stub_sec->size += size;
5184
5185
0
  return true;
5186
0
}
5187
5188
/* External entry points for sizing and building linker stubs.  */
5189
5190
/* Set up various things so that we can make a list of input sections
5191
   for each output section included in the link.  Returns -1 on error,
5192
   0 when no stubs will be needed, and 1 on success.  */
5193
5194
int
5195
elf32_arm_setup_section_lists (bfd *output_bfd,
5196
             struct bfd_link_info *info)
5197
0
{
5198
0
  bfd *input_bfd;
5199
0
  unsigned int bfd_count;
5200
0
  unsigned int top_id, top_index;
5201
0
  asection *section;
5202
0
  asection **input_list, **list;
5203
0
  size_t amt;
5204
0
  struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
5205
5206
0
  if (htab == NULL)
5207
0
    return 0;
5208
5209
  /* Count the number of input BFDs and find the top input section id.  */
5210
0
  for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
5211
0
       input_bfd != NULL;
5212
0
       input_bfd = input_bfd->link.next)
5213
0
    {
5214
0
      bfd_count += 1;
5215
0
      for (section = input_bfd->sections;
5216
0
     section != NULL;
5217
0
     section = section->next)
5218
0
  {
5219
0
    if (top_id < section->id)
5220
0
      top_id = section->id;
5221
0
  }
5222
0
    }
5223
0
  htab->bfd_count = bfd_count;
5224
5225
0
  amt = sizeof (struct map_stub) * (top_id + 1);
5226
0
  htab->stub_group = (struct map_stub *) bfd_zmalloc (amt);
5227
0
  if (htab->stub_group == NULL)
5228
0
    return -1;
5229
0
  htab->top_id = top_id;
5230
5231
  /* We can't use output_bfd->section_count here to find the top output
5232
     section index as some sections may have been removed, and
5233
     _bfd_strip_section_from_output doesn't renumber the indices.  */
5234
0
  for (section = output_bfd->sections, top_index = 0;
5235
0
       section != NULL;
5236
0
       section = section->next)
5237
0
    {
5238
0
      if (top_index < section->index)
5239
0
  top_index = section->index;
5240
0
    }
5241
5242
0
  htab->top_index = top_index;
5243
0
  amt = sizeof (asection *) * (top_index + 1);
5244
0
  input_list = (asection **) bfd_malloc (amt);
5245
0
  htab->input_list = input_list;
5246
0
  if (input_list == NULL)
5247
0
    return -1;
5248
5249
  /* For sections we aren't interested in, mark their entries with a
5250
     value we can check later.  */
5251
0
  list = input_list + top_index;
5252
0
  do
5253
0
    *list = bfd_abs_section_ptr;
5254
0
  while (list-- != input_list);
5255
5256
0
  for (section = output_bfd->sections;
5257
0
       section != NULL;
5258
0
       section = section->next)
5259
0
    {
5260
0
      if ((section->flags & SEC_CODE) != 0)
5261
0
  input_list[section->index] = NULL;
5262
0
    }
5263
5264
0
  return 1;
5265
0
}
5266
5267
/* The linker repeatedly calls this function for each input section,
5268
   in the order that input sections are linked into output sections.
5269
   Build lists of input sections to determine groupings between which
5270
   we may insert linker stubs.  */
5271
5272
void
5273
elf32_arm_next_input_section (struct bfd_link_info *info,
5274
            asection *isec)
5275
0
{
5276
0
  struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
5277
5278
0
  if (htab == NULL)
5279
0
    return;
5280
5281
0
  if (isec->output_section->index <= htab->top_index)
5282
0
    {
5283
0
      asection **list = htab->input_list + isec->output_section->index;
5284
5285
0
      if (*list != bfd_abs_section_ptr && (isec->flags & SEC_CODE) != 0)
5286
0
  {
5287
    /* Steal the link_sec pointer for our list.  */
5288
0
#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
5289
    /* This happens to make the list in reverse order,
5290
       which we reverse later.  */
5291
0
    PREV_SEC (isec) = *list;
5292
0
    *list = isec;
5293
0
  }
5294
0
    }
5295
0
}
5296
5297
/* See whether we can group stub sections together.  Grouping stub
5298
   sections may result in fewer stubs.  More importantly, we need to
5299
   put all .init* and .fini* stubs at the end of the .init or
5300
   .fini output sections respectively, because glibc splits the
5301
   _init and _fini functions into multiple parts.  Putting a stub in
5302
   the middle of a function is not a good idea.  */
5303
5304
static void
5305
group_sections (struct elf32_arm_link_hash_table *htab,
5306
    bfd_size_type stub_group_size,
5307
    bool stubs_always_after_branch)
5308
0
{
5309
0
  asection **list = htab->input_list;
5310
5311
0
  do
5312
0
    {
5313
0
      asection *tail = *list;
5314
0
      asection *head;
5315
5316
0
      if (tail == bfd_abs_section_ptr)
5317
0
  continue;
5318
5319
      /* Reverse the list: we must avoid placing stubs at the
5320
   beginning of the section because the beginning of the text
5321
   section may be required for an interrupt vector in bare metal
5322
   code.  */
5323
0
#define NEXT_SEC PREV_SEC
5324
0
      head = NULL;
5325
0
      while (tail != NULL)
5326
0
  {
5327
    /* Pop from tail.  */
5328
0
    asection *item = tail;
5329
0
    tail = PREV_SEC (item);
5330
5331
    /* Push on head.  */
5332
0
    NEXT_SEC (item) = head;
5333
0
    head = item;
5334
0
  }
5335
5336
0
      while (head != NULL)
5337
0
  {
5338
0
    asection *curr;
5339
0
    asection *next;
5340
0
    bfd_vma stub_group_start = head->output_offset;
5341
0
    bfd_vma end_of_next;
5342
5343
0
    curr = head;
5344
0
    while (NEXT_SEC (curr) != NULL)
5345
0
      {
5346
0
        next = NEXT_SEC (curr);
5347
0
        end_of_next = next->output_offset + next->size;
5348
0
        if (end_of_next - stub_group_start >= stub_group_size)
5349
    /* End of NEXT is too far from start, so stop.  */
5350
0
    break;
5351
        /* Add NEXT to the group.  */
5352
0
        curr = next;
5353
0
      }
5354
5355
    /* OK, the size from the start to the start of CURR is less
5356
       than stub_group_size and thus can be handled by one stub
5357
       section.  (Or the head section is itself larger than
5358
       stub_group_size, in which case we may be toast.)
5359
       We should really be keeping track of the total size of
5360
       stubs added here, as stubs contribute to the final output
5361
       section size.  */
5362
0
    do
5363
0
      {
5364
0
        next = NEXT_SEC (head);
5365
        /* Set up this stub group.  */
5366
0
        htab->stub_group[head->id].link_sec = curr;
5367
0
      }
5368
0
    while (head != curr && (head = next) != NULL);
5369
5370
    /* But wait, there's more!  Input sections up to stub_group_size
5371
       bytes after the stub section can be handled by it too.  */
5372
0
    if (!stubs_always_after_branch)
5373
0
      {
5374
0
        stub_group_start = curr->output_offset + curr->size;
5375
5376
0
        while (next != NULL)
5377
0
    {
5378
0
      end_of_next = next->output_offset + next->size;
5379
0
      if (end_of_next - stub_group_start >= stub_group_size)
5380
        /* End of NEXT is too far from stubs, so stop.  */
5381
0
        break;
5382
      /* Add NEXT to the stub group.  */
5383
0
      head = next;
5384
0
      next = NEXT_SEC (head);
5385
0
      htab->stub_group[head->id].link_sec = curr;
5386
0
    }
5387
0
      }
5388
0
    head = next;
5389
0
  }
5390
0
    }
5391
0
  while (list++ != htab->input_list + htab->top_index);
5392
5393
0
  free (htab->input_list);
5394
0
#undef PREV_SEC
5395
0
#undef NEXT_SEC
5396
0
}
5397
5398
/* Comparison function for sorting/searching relocations relating to Cortex-A8
5399
   erratum fix.  */
5400
5401
static int
5402
a8_reloc_compare (const void *a, const void *b)
5403
0
{
5404
0
  const struct a8_erratum_reloc *ra = (const struct a8_erratum_reloc *) a;
5405
0
  const struct a8_erratum_reloc *rb = (const struct a8_erratum_reloc *) b;
5406
5407
0
  if (ra->from < rb->from)
5408
0
    return -1;
5409
0
  else if (ra->from > rb->from)
5410
0
    return 1;
5411
0
  else
5412
0
    return 0;
5413
0
}
5414
5415
static struct elf_link_hash_entry *find_thumb_glue (struct bfd_link_info *,
5416
                const char *, char **);
5417
5418
/* Helper function to scan code for sequences which might trigger the Cortex-A8
5419
   branch/TLB erratum.  Fill in the table described by A8_FIXES_P,
5420
   NUM_A8_FIXES_P, A8_FIX_TABLE_SIZE_P.  Returns true if an error occurs, false
5421
   otherwise.  */
5422
5423
static bool
5424
cortex_a8_erratum_scan (bfd *input_bfd,
5425
      struct bfd_link_info *info,
5426
      struct a8_erratum_fix **a8_fixes_p,
5427
      unsigned int *num_a8_fixes_p,
5428
      unsigned int *a8_fix_table_size_p,
5429
      struct a8_erratum_reloc *a8_relocs,
5430
      unsigned int num_a8_relocs,
5431
      unsigned prev_num_a8_fixes,
5432
      bool *stub_changed_p)
5433
0
{
5434
0
  asection *section;
5435
0
  struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
5436
0
  struct a8_erratum_fix *a8_fixes = *a8_fixes_p;
5437
0
  unsigned int num_a8_fixes = *num_a8_fixes_p;
5438
0
  unsigned int a8_fix_table_size = *a8_fix_table_size_p;
5439
5440
0
  if (htab == NULL)
5441
0
    return false;
5442
5443
0
  for (section = input_bfd->sections;
5444
0
       section != NULL;
5445
0
       section = section->next)
5446
0
    {
5447
0
      bfd_byte *contents = NULL;
5448
0
      struct _arm_elf_section_data *sec_data;
5449
0
      unsigned int span;
5450
0
      bfd_vma base_vma;
5451
5452
0
      if (elf_section_type (section) != SHT_PROGBITS
5453
0
    || (elf_section_flags (section) & SHF_EXECINSTR) == 0
5454
0
    || (section->flags & SEC_EXCLUDE) != 0
5455
0
    || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5456
0
    || (section->output_section == bfd_abs_section_ptr))
5457
0
  continue;
5458
5459
0
      base_vma = section->output_section->vma + section->output_offset;
5460
5461
0
      if (elf_section_data (section)->this_hdr.contents != NULL)
5462
0
  contents = elf_section_data (section)->this_hdr.contents;
5463
0
      else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
5464
0
  return true;
5465
5466
0
      sec_data = elf32_arm_section_data (section);
5467
5468
0
      for (span = 0; span < sec_data->mapcount; span++)
5469
0
  {
5470
0
    unsigned int span_start = sec_data->map[span].vma;
5471
0
    unsigned int span_end = (span == sec_data->mapcount - 1)
5472
0
      ? section->size : sec_data->map[span + 1].vma;
5473
0
    unsigned int i;
5474
0
    char span_type = sec_data->map[span].type;
5475
0
    bool last_was_32bit = false, last_was_branch = false;
5476
5477
0
    if (span_type != 't')
5478
0
      continue;
5479
5480
    /* Span is entirely within a single 4KB region: skip scanning.  */
5481
0
    if (((base_vma + span_start) & ~0xfff)
5482
0
        == ((base_vma + span_end) & ~0xfff))
5483
0
      continue;
5484
5485
    /* Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
5486
5487
         * The opcode is BLX.W, BL.W, B.W, Bcc.W
5488
         * The branch target is in the same 4KB region as the
5489
     first half of the branch.
5490
         * The instruction before the branch is a 32-bit
5491
     length non-branch instruction.  */
5492
0
    for (i = span_start; i < span_end;)
5493
0
      {
5494
0
        unsigned int insn = bfd_getl16 (&contents[i]);
5495
0
        bool insn_32bit = false, is_blx = false, is_b = false;
5496
0
        bool is_bl = false, is_bcc = false, is_32bit_branch;
5497
5498
0
        if ((insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000)
5499
0
    insn_32bit = true;
5500
5501
0
        if (insn_32bit)
5502
0
    {
5503
      /* Load the rest of the insn (in manual-friendly order).  */
5504
0
      insn = (insn << 16) | bfd_getl16 (&contents[i + 2]);
5505
5506
      /* Encoding T4: B<c>.W.  */
5507
0
      is_b = (insn & 0xf800d000) == 0xf0009000;
5508
      /* Encoding T1: BL<c>.W.  */
5509
0
      is_bl = (insn & 0xf800d000) == 0xf000d000;
5510
      /* Encoding T2: BLX<c>.W.  */
5511
0
      is_blx = (insn & 0xf800d000) == 0xf000c000;
5512
      /* Encoding T3: B<c>.W (not permitted in IT block).  */
5513
0
      is_bcc = (insn & 0xf800d000) == 0xf0008000
5514
0
         && (insn & 0x07f00000) != 0x03800000;
5515
0
    }
5516
5517
0
        is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
5518
5519
0
        if (((base_vma + i) & 0xfff) == 0xffe
5520
0
      && insn_32bit
5521
0
      && is_32bit_branch
5522
0
      && last_was_32bit
5523
0
      && ! last_was_branch)
5524
0
    {
5525
0
      bfd_signed_vma offset = 0;
5526
0
      bool force_target_arm = false;
5527
0
      bool force_target_thumb = false;
5528
0
      bfd_vma target;
5529
0
      enum elf32_arm_stub_type stub_type = arm_stub_none;
5530
0
      struct a8_erratum_reloc key, *found;
5531
0
      bool use_plt = false;
5532
5533
0
      key.from = base_vma + i;
5534
0
      found = (struct a8_erratum_reloc *)
5535
0
          bsearch (&key, a8_relocs, num_a8_relocs,
5536
0
             sizeof (struct a8_erratum_reloc),
5537
0
             &a8_reloc_compare);
5538
5539
0
      if (found)
5540
0
        {
5541
0
          char *error_message = NULL;
5542
0
          struct elf_link_hash_entry *entry;
5543
5544
          /* We don't care about the error returned from this
5545
       function, only if there is glue or not.  */
5546
0
          entry = find_thumb_glue (info, found->sym_name,
5547
0
                 &error_message);
5548
5549
0
          if (entry)
5550
0
      found->non_a8_stub = true;
5551
5552
          /* Keep a simpler condition, for the sake of clarity.  */
5553
0
          if (htab->root.splt != NULL && found->hash != NULL
5554
0
        && found->hash->root.plt.offset != (bfd_vma) -1)
5555
0
      use_plt = true;
5556
5557
0
          if (found->r_type == R_ARM_THM_CALL)
5558
0
      {
5559
0
        if (found->branch_type == ST_BRANCH_TO_ARM
5560
0
            || use_plt)
5561
0
          force_target_arm = true;
5562
0
        else
5563
0
          force_target_thumb = true;
5564
0
      }
5565
0
        }
5566
5567
      /* Check if we have an offending branch instruction.  */
5568
5569
0
      if (found && found->non_a8_stub)
5570
        /* We've already made a stub for this instruction, e.g.
5571
           it's a long branch or a Thumb->ARM stub.  Assume that
5572
           stub will suffice to work around the A8 erratum (see
5573
           setting of always_after_branch above).  */
5574
0
        ;
5575
0
      else if (is_bcc)
5576
0
        {
5577
0
          offset = (insn & 0x7ff) << 1;
5578
0
          offset |= (insn & 0x3f0000) >> 4;
5579
0
          offset |= (insn & 0x2000) ? 0x40000 : 0;
5580
0
          offset |= (insn & 0x800) ? 0x80000 : 0;
5581
0
          offset |= (insn & 0x4000000) ? 0x100000 : 0;
5582
0
          if (offset & 0x100000)
5583
0
      offset |= ~ ((bfd_signed_vma) 0xfffff);
5584
0
          stub_type = arm_stub_a8_veneer_b_cond;
5585
0
        }
5586
0
      else if (is_b || is_bl || is_blx)
5587
0
        {
5588
0
          int s = (insn & 0x4000000) != 0;
5589
0
          int j1 = (insn & 0x2000) != 0;
5590
0
          int j2 = (insn & 0x800) != 0;
5591
0
          int i1 = !(j1 ^ s);
5592
0
          int i2 = !(j2 ^ s);
5593
5594
0
          offset = (insn & 0x7ff) << 1;
5595
0
          offset |= (insn & 0x3ff0000) >> 4;
5596
0
          offset |= i2 << 22;
5597
0
          offset |= i1 << 23;
5598
0
          offset |= s << 24;
5599
0
          if (offset & 0x1000000)
5600
0
      offset |= ~ ((bfd_signed_vma) 0xffffff);
5601
5602
0
          if (is_blx)
5603
0
      offset &= ~ ((bfd_signed_vma) 3);
5604
5605
0
          stub_type = is_blx ? arm_stub_a8_veneer_blx :
5606
0
      is_bl ? arm_stub_a8_veneer_bl : arm_stub_a8_veneer_b;
5607
0
        }
5608
5609
0
      if (stub_type != arm_stub_none)
5610
0
        {
5611
0
          bfd_vma pc_for_insn = base_vma + i + 4;
5612
5613
          /* The original instruction is a BL, but the target is
5614
       an ARM instruction.  If we were not making a stub,
5615
       the BL would have been converted to a BLX.  Use the
5616
       BLX stub instead in that case.  */
5617
0
          if (htab->use_blx && force_target_arm
5618
0
        && stub_type == arm_stub_a8_veneer_bl)
5619
0
      {
5620
0
        stub_type = arm_stub_a8_veneer_blx;
5621
0
        is_blx = true;
5622
0
        is_bl = false;
5623
0
      }
5624
          /* Conversely, if the original instruction was
5625
       BLX but the target is Thumb mode, use the BL
5626
       stub.  */
5627
0
          else if (force_target_thumb
5628
0
             && stub_type == arm_stub_a8_veneer_blx)
5629
0
      {
5630
0
        stub_type = arm_stub_a8_veneer_bl;
5631
0
        is_blx = false;
5632
0
        is_bl = true;
5633
0
      }
5634
5635
0
          if (is_blx)
5636
0
      pc_for_insn &= ~ ((bfd_vma) 3);
5637
5638
          /* If we found a relocation, use the proper destination,
5639
       not the offset in the (unrelocated) instruction.
5640
       Note this is always done if we switched the stub type
5641
       above.  */
5642
0
          if (found)
5643
0
      offset =
5644
0
        (bfd_signed_vma) (found->destination - pc_for_insn);
5645
5646
          /* If the stub will use a Thumb-mode branch to a
5647
       PLT target, redirect it to the preceding Thumb
5648
       entry point.  */
5649
0
          if (stub_type != arm_stub_a8_veneer_blx && use_plt)
5650
0
      offset -= PLT_THUMB_STUB_SIZE;
5651
5652
0
          target = pc_for_insn + offset;
5653
5654
          /* The BLX stub is ARM-mode code.  Adjust the offset to
5655
       take the different PC value (+8 instead of +4) into
5656
       account.  */
5657
0
          if (stub_type == arm_stub_a8_veneer_blx)
5658
0
      offset += 4;
5659
5660
0
          if (((base_vma + i) & ~0xfff) == (target & ~0xfff))
5661
0
      {
5662
0
        char *stub_name = NULL;
5663
5664
0
        if (num_a8_fixes == a8_fix_table_size)
5665
0
          {
5666
0
            a8_fix_table_size *= 2;
5667
0
            a8_fixes = (struct a8_erratum_fix *)
5668
0
          bfd_realloc (a8_fixes,
5669
0
                 sizeof (struct a8_erratum_fix)
5670
0
                 * a8_fix_table_size);
5671
0
          }
5672
5673
0
        if (num_a8_fixes < prev_num_a8_fixes)
5674
0
          {
5675
            /* If we're doing a subsequent scan,
5676
         check if we've found the same fix as
5677
         before, and try and reuse the stub
5678
         name.  */
5679
0
            stub_name = a8_fixes[num_a8_fixes].stub_name;
5680
0
            if ((a8_fixes[num_a8_fixes].section != section)
5681
0
          || (a8_fixes[num_a8_fixes].offset != i))
5682
0
        {
5683
0
          free (stub_name);
5684
0
          stub_name = NULL;
5685
0
          *stub_changed_p = true;
5686
0
        }
5687
0
          }
5688
5689
0
        if (!stub_name)
5690
0
          {
5691
0
            stub_name = (char *) bfd_malloc (8 + 1 + 8 + 1);
5692
0
            if (stub_name != NULL)
5693
0
        sprintf (stub_name, "%x:%x", section->id, i);
5694
0
          }
5695
5696
0
        a8_fixes[num_a8_fixes].input_bfd = input_bfd;
5697
0
        a8_fixes[num_a8_fixes].section = section;
5698
0
        a8_fixes[num_a8_fixes].offset = i;
5699
0
        a8_fixes[num_a8_fixes].target_offset =
5700
0
          target - base_vma;
5701
0
        a8_fixes[num_a8_fixes].orig_insn = insn;
5702
0
        a8_fixes[num_a8_fixes].stub_name = stub_name;
5703
0
        a8_fixes[num_a8_fixes].stub_type = stub_type;
5704
0
        a8_fixes[num_a8_fixes].branch_type =
5705
0
          is_blx ? ST_BRANCH_TO_ARM : ST_BRANCH_TO_THUMB;
5706
5707
0
        num_a8_fixes++;
5708
0
      }
5709
0
        }
5710
0
    }
5711
5712
0
        i += insn_32bit ? 4 : 2;
5713
0
        last_was_32bit = insn_32bit;
5714
0
        last_was_branch = is_32bit_branch;
5715
0
      }
5716
0
  }
5717
5718
0
      if (elf_section_data (section)->this_hdr.contents == NULL)
5719
0
  free (contents);
5720
0
    }
5721
5722
0
  *a8_fixes_p = a8_fixes;
5723
0
  *num_a8_fixes_p = num_a8_fixes;
5724
0
  *a8_fix_table_size_p = a8_fix_table_size;
5725
5726
0
  return false;
5727
0
}
5728
5729
/* Create or update a stub entry depending on whether the stub can already be
5730
   found in HTAB.  The stub is identified by:
5731
   - its type STUB_TYPE
5732
   - its source branch (note that several can share the same stub) whose
5733
     section and relocation (if any) are given by SECTION and IRELA
5734
     respectively
5735
   - its target symbol whose input section, hash, name, value and branch type
5736
     are given in SYM_SEC, HASH, SYM_NAME, SYM_VALUE and BRANCH_TYPE
5737
     respectively
5738
5739
   If found, the value of the stub's target symbol is updated from SYM_VALUE
5740
   and *NEW_STUB is set to FALSE.  Otherwise, *NEW_STUB is set to
5741
   TRUE and the stub entry is initialized.
5742
5743
   Returns the stub that was created or updated, or NULL if an error
5744
   occurred.  */
5745
5746
static struct elf32_arm_stub_hash_entry *
5747
elf32_arm_create_stub (struct elf32_arm_link_hash_table *htab,
5748
           enum elf32_arm_stub_type stub_type, asection *section,
5749
           Elf_Internal_Rela *irela, asection *sym_sec,
5750
           struct elf32_arm_link_hash_entry *hash, char *sym_name,
5751
           bfd_vma sym_value, enum arm_st_branch_type branch_type,
5752
           bool *new_stub)
5753
0
{
5754
0
  const asection *id_sec;
5755
0
  char *stub_name;
5756
0
  struct elf32_arm_stub_hash_entry *stub_entry;
5757
0
  unsigned int r_type;
5758
0
  bool sym_claimed = arm_stub_sym_claimed (stub_type);
5759
5760
0
  BFD_ASSERT (stub_type != arm_stub_none);
5761
0
  *new_stub = false;
5762
5763
0
  if (sym_claimed)
5764
0
    stub_name = sym_name;
5765
0
  else
5766
0
    {
5767
0
      BFD_ASSERT (irela);
5768
0
      BFD_ASSERT (section);
5769
0
      BFD_ASSERT (section->id <= htab->top_id);
5770
5771
      /* Support for grouping stub sections.  */
5772
0
      id_sec = htab->stub_group[section->id].link_sec;
5773
5774
      /* Get the name of this stub.  */
5775
0
      stub_name = elf32_arm_stub_name (id_sec, sym_sec, hash, irela,
5776
0
               stub_type);
5777
0
      if (!stub_name)
5778
0
  return NULL;
5779
0
    }
5780
5781
0
  stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name, false,
5782
0
             false);
5783
  /* The proper stub has already been created, just update its value.  */
5784
0
  if (stub_entry != NULL)
5785
0
    {
5786
0
      if (!sym_claimed)
5787
0
  free (stub_name);
5788
0
      stub_entry->target_value = sym_value;
5789
0
      return stub_entry;
5790
0
    }
5791
5792
0
  stub_entry = elf32_arm_add_stub (stub_name, section, htab, stub_type);
5793
0
  if (stub_entry == NULL)
5794
0
    {
5795
0
      if (!sym_claimed)
5796
0
  free (stub_name);
5797
0
      return NULL;
5798
0
    }
5799
5800
0
  stub_entry->target_value = sym_value;
5801
0
  stub_entry->target_section = sym_sec;
5802
0
  stub_entry->stub_type = stub_type;
5803
0
  stub_entry->h = hash;
5804
0
  stub_entry->branch_type = branch_type;
5805
5806
0
  if (sym_claimed)
5807
0
    stub_entry->output_name = sym_name;
5808
0
  else
5809
0
    {
5810
0
      if (sym_name == NULL)
5811
0
  sym_name = "unnamed";
5812
0
      stub_entry->output_name = (char *)
5813
0
  bfd_alloc (htab->stub_bfd, sizeof (THUMB2ARM_GLUE_ENTRY_NAME)
5814
0
           + strlen (sym_name));
5815
0
      if (stub_entry->output_name == NULL)
5816
0
  {
5817
0
    free (stub_name);
5818
0
    return NULL;
5819
0
  }
5820
5821
      /* For historical reasons, use the existing names for ARM-to-Thumb and
5822
   Thumb-to-ARM stubs.  */
5823
0
      r_type = ELF32_R_TYPE (irela->r_info);
5824
0
      if ((r_type == (unsigned int) R_ARM_THM_CALL
5825
0
     || r_type == (unsigned int) R_ARM_THM_JUMP24
5826
0
     || r_type == (unsigned int) R_ARM_THM_JUMP19)
5827
0
    && branch_type == ST_BRANCH_TO_ARM)
5828
0
  sprintf (stub_entry->output_name, THUMB2ARM_GLUE_ENTRY_NAME, sym_name);
5829
0
      else if ((r_type == (unsigned int) R_ARM_CALL
5830
0
    || r_type == (unsigned int) R_ARM_JUMP24)
5831
0
         && branch_type == ST_BRANCH_TO_THUMB)
5832
0
  sprintf (stub_entry->output_name, ARM2THUMB_GLUE_ENTRY_NAME, sym_name);
5833
0
      else
5834
0
  sprintf (stub_entry->output_name, STUB_ENTRY_NAME, sym_name);
5835
0
    }
5836
5837
0
  *new_stub = true;
5838
0
  return stub_entry;
5839
0
}
5840
5841
/* Scan symbols in INPUT_BFD to identify secure entry functions needing a
5842
   gateway veneer to transition from non secure to secure state and create them
5843
   accordingly.
5844
5845
   "ARMv8-M Security Extensions: Requirements on Development Tools" document
5846
   defines the conditions that govern Secure Gateway veneer creation for a
5847
   given symbol <SYM> as follows:
5848
   - it has function type
5849
   - it has non local binding
5850
   - a symbol named __acle_se_<SYM> (called special symbol) exists with the
5851
     same type, binding and value as <SYM> (called normal symbol).
5852
   An entry function can handle secure state transition itself in which case
5853
   its special symbol would have a different value from the normal symbol.
5854
5855
   OUT_ATTR gives the output attributes, SYM_HASHES the symbol index to hash
5856
   entry mapping while HTAB gives the name to hash entry mapping.
5857
   *CMSE_STUB_CREATED is increased by the number of secure gateway veneer
5858
   created.
5859
5860
   The return value gives whether a stub failed to be allocated.  */
5861
5862
static bool
5863
cmse_scan (bfd *input_bfd, struct elf32_arm_link_hash_table *htab,
5864
     obj_attribute *out_attr, struct elf_link_hash_entry **sym_hashes,
5865
     int *cmse_stub_created)
5866
0
{
5867
0
  elf_backend_data *bed = get_elf_backend_data (input_bfd);
5868
0
  Elf_Internal_Shdr *symtab_hdr;
5869
0
  unsigned i, j, sym_count, ext_start;
5870
0
  Elf_Internal_Sym *cmse_sym, *local_syms;
5871
0
  struct elf32_arm_link_hash_entry *hash, *cmse_hash = NULL;
5872
0
  enum arm_st_branch_type branch_type;
5873
0
  char *sym_name, *lsym_name;
5874
0
  bfd_vma sym_value;
5875
0
  asection *section;
5876
0
  struct elf32_arm_stub_hash_entry *stub_entry;
5877
0
  bool is_v8m, new_stub, cmse_invalid, ret = true;
5878
5879
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
5880
0
  sym_count = symtab_hdr->sh_size / bed->s->sizeof_sym;
5881
0
  ext_start = symtab_hdr->sh_info;
5882
0
  is_v8m = (out_attr[Tag_CPU_arch].i >= TAG_CPU_ARCH_V8M_BASE
5883
0
      && out_attr[Tag_CPU_arch_profile].i == 'M');
5884
5885
0
  local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
5886
0
  if (local_syms == NULL)
5887
0
    local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5888
0
               symtab_hdr->sh_info, 0, NULL, NULL,
5889
0
               NULL);
5890
0
  if (symtab_hdr->sh_info && local_syms == NULL)
5891
0
    return false;
5892
5893
  /* Scan symbols.  */
5894
0
  for (i = 0; i < sym_count; i++)
5895
0
    {
5896
0
      cmse_invalid = false;
5897
5898
0
      if (i < ext_start)
5899
0
  {
5900
0
    cmse_sym = &local_syms[i];
5901
0
    sym_name = bfd_elf_string_from_elf_section (input_bfd,
5902
0
                  symtab_hdr->sh_link,
5903
0
                  cmse_sym->st_name);
5904
0
    if (!sym_name || !startswith (sym_name, CMSE_PREFIX))
5905
0
      continue;
5906
5907
    /* Special symbol with local binding.  */
5908
0
    cmse_invalid = true;
5909
0
  }
5910
0
      else
5911
0
  {
5912
0
    cmse_hash = elf32_arm_hash_entry (sym_hashes[i - ext_start]);
5913
0
    if (cmse_hash == NULL)
5914
0
      continue;
5915
5916
0
    sym_name = (char *) cmse_hash->root.root.root.string;
5917
0
    if (!startswith (sym_name, CMSE_PREFIX))
5918
0
      continue;
5919
5920
    /* Special symbol has incorrect binding or type.  */
5921
0
    if ((cmse_hash->root.root.type != bfd_link_hash_defined
5922
0
         && cmse_hash->root.root.type != bfd_link_hash_defweak)
5923
0
        || cmse_hash->root.type != STT_FUNC)
5924
0
      cmse_invalid = true;
5925
0
  }
5926
5927
0
      if (!is_v8m)
5928
0
  {
5929
0
    _bfd_error_handler (_("%pB: special symbol `%s' only allowed for "
5930
0
        "ARMv8-M architecture or later"),
5931
0
            input_bfd, sym_name);
5932
0
    is_v8m = true; /* Avoid multiple warning.  */
5933
0
    ret = false;
5934
0
  }
5935
5936
0
      if (cmse_invalid)
5937
0
  {
5938
0
    _bfd_error_handler (_("%pB: invalid special symbol `%s'; it must be"
5939
0
        " a global or weak function symbol"),
5940
0
            input_bfd, sym_name);
5941
0
    ret = false;
5942
0
    if (i < ext_start)
5943
0
      continue;
5944
0
  }
5945
5946
0
      sym_name += strlen (CMSE_PREFIX);
5947
0
      hash = (struct elf32_arm_link_hash_entry *)
5948
0
  elf_link_hash_lookup (&(htab)->root, sym_name, false, false, true);
5949
5950
      /* No associated normal symbol or it is neither global nor weak.  */
5951
0
      if (!hash
5952
0
    || (hash->root.root.type != bfd_link_hash_defined
5953
0
        && hash->root.root.type != bfd_link_hash_defweak)
5954
0
    || hash->root.type != STT_FUNC)
5955
0
  {
5956
    /* Initialize here to avoid warning about use of possibly
5957
       uninitialized variable.  */
5958
0
    j = 0;
5959
5960
0
    if (!hash)
5961
0
      {
5962
        /* Searching for a normal symbol with local binding.  */
5963
0
        for (; j < ext_start; j++)
5964
0
    {
5965
0
      lsym_name =
5966
0
        bfd_elf_string_from_elf_section (input_bfd,
5967
0
                 symtab_hdr->sh_link,
5968
0
                 local_syms[j].st_name);
5969
0
      if (!strcmp (sym_name, lsym_name))
5970
0
        break;
5971
0
    }
5972
0
      }
5973
5974
0
    if (hash || j < ext_start)
5975
0
      {
5976
0
        _bfd_error_handler
5977
0
    (_("%pB: invalid standard symbol `%s'; it must be "
5978
0
       "a global or weak function symbol"),
5979
0
     input_bfd, sym_name);
5980
0
      }
5981
0
    else
5982
0
      _bfd_error_handler
5983
0
        (_("%pB: absent standard symbol `%s'"), input_bfd, sym_name);
5984
0
    ret = false;
5985
0
    if (!hash)
5986
0
      continue;
5987
0
  }
5988
5989
0
      sym_value = hash->root.root.u.def.value;
5990
0
      section = hash->root.root.u.def.section;
5991
5992
0
      if (cmse_hash->root.root.u.def.section != section)
5993
0
  {
5994
0
    _bfd_error_handler
5995
0
      (_("%pB: `%s' and its special symbol are in different sections"),
5996
0
       input_bfd, sym_name);
5997
0
    ret = false;
5998
0
  }
5999
0
      if (cmse_hash->root.root.u.def.value != sym_value)
6000
0
  continue; /* Ignore: could be an entry function starting with SG.  */
6001
6002
  /* If this section is a link-once section that will be discarded, then
6003
     don't create any stubs.  */
6004
0
      if (section->output_section == NULL)
6005
0
  {
6006
0
    _bfd_error_handler
6007
0
      (_("%pB: entry function `%s' not output"), input_bfd, sym_name);
6008
0
    continue;
6009
0
  }
6010
6011
0
      if (hash->root.size == 0)
6012
0
  {
6013
0
    _bfd_error_handler
6014
0
      (_("%pB: entry function `%s' is empty"), input_bfd, sym_name);
6015
0
    ret = false;
6016
0
  }
6017
6018
0
      if (!ret)
6019
0
  continue;
6020
0
      branch_type = ARM_GET_SYM_BRANCH_TYPE (hash->root.target_internal);
6021
0
      stub_entry
6022
0
  = elf32_arm_create_stub (htab, arm_stub_cmse_branch_thumb_only,
6023
0
         NULL, NULL, section, hash, sym_name,
6024
0
         sym_value, branch_type, &new_stub);
6025
6026
0
      if (stub_entry == NULL)
6027
0
   ret = false;
6028
0
      else
6029
0
  {
6030
0
    BFD_ASSERT (new_stub);
6031
0
    (*cmse_stub_created)++;
6032
0
  }
6033
0
    }
6034
6035
0
  if (!symtab_hdr->contents)
6036
0
    free (local_syms);
6037
0
  return ret;
6038
0
}
6039
6040
/* Return TRUE iff a symbol identified by its linker HASH entry is a secure
6041
   code entry function, ie can be called from non secure code without using a
6042
   veneer.  */
6043
6044
static bool
6045
cmse_entry_fct_p (struct elf32_arm_link_hash_entry *hash)
6046
0
{
6047
0
  bfd_byte contents[4];
6048
0
  uint32_t first_insn;
6049
0
  asection *section;
6050
0
  file_ptr offset;
6051
0
  bfd *abfd;
6052
6053
  /* Defined symbol of function type.  */
6054
0
  if (hash->root.root.type != bfd_link_hash_defined
6055
0
      && hash->root.root.type != bfd_link_hash_defweak)
6056
0
    return false;
6057
0
  if (hash->root.type != STT_FUNC)
6058
0
    return false;
6059
6060
  /* Read first instruction.  */
6061
0
  section = hash->root.root.u.def.section;
6062
0
  abfd = section->owner;
6063
0
  offset = hash->root.root.u.def.value - section->vma;
6064
0
  if (!bfd_get_section_contents (abfd, section, contents, offset,
6065
0
         sizeof (contents)))
6066
0
    return false;
6067
6068
0
  first_insn = bfd_get_32 (abfd, contents);
6069
6070
  /* Starts by SG instruction.  */
6071
0
  return first_insn == 0xe97fe97f;
6072
0
}
6073
6074
/* Output the name (in symbol table) of the veneer GEN_ENTRY if it is a new
6075
   secure gateway veneers (ie. the veneers was not in the input import library)
6076
   and there is no output import library (GEN_INFO->out_implib_bfd is NULL.  */
6077
6078
static bool
6079
arm_list_new_cmse_stub (struct bfd_hash_entry *gen_entry, void *gen_info)
6080
0
{
6081
0
  struct elf32_arm_stub_hash_entry *stub_entry;
6082
0
  struct bfd_link_info *info;
6083
6084
  /* Massage our args to the form they really have.  */
6085
0
  stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
6086
0
  info = (struct bfd_link_info *) gen_info;
6087
6088
0
  if (info->out_implib_bfd)
6089
0
    return true;
6090
6091
0
  if (stub_entry->stub_type != arm_stub_cmse_branch_thumb_only)
6092
0
    return true;
6093
6094
0
  if (stub_entry->stub_offset == (bfd_vma) -1)
6095
0
    _bfd_error_handler ("  %s", stub_entry->output_name);
6096
6097
0
  return true;
6098
0
}
6099
6100
/* Set offset of each secure gateway veneers so that its address remain
6101
   identical to the one in the input import library referred by
6102
   HTAB->in_implib_bfd.  A warning is issued for veneers that disappeared
6103
   (present in input import library but absent from the executable being
6104
   linked) or if new veneers appeared and there is no output import library
6105
   (INFO->out_implib_bfd is NULL and *CMSE_STUB_CREATED is bigger than the
6106
   number of secure gateway veneers found in the input import library.
6107
6108
   The function returns whether an error occurred.  If no error occurred,
6109
   *CMSE_STUB_CREATED gives the number of SG veneers created by both cmse_scan
6110
   and this function and HTAB->new_cmse_stub_offset is set to the biggest
6111
   veneer observed set for new veneers to be layed out after.  */
6112
6113
static bool
6114
set_cmse_veneer_addr_from_implib (struct bfd_link_info *info,
6115
          struct elf32_arm_link_hash_table *htab,
6116
          int *cmse_stub_created)
6117
0
{
6118
0
  long symsize;
6119
0
  char *sym_name;
6120
0
  flagword flags;
6121
0
  long i, symcount;
6122
0
  bfd *in_implib_bfd;
6123
0
  asection *stub_out_sec;
6124
0
  bool ret = true;
6125
0
  Elf_Internal_Sym *intsym;
6126
0
  const char *out_sec_name;
6127
0
  bfd_size_type cmse_stub_size;
6128
0
  asymbol **sympp = NULL, *sym;
6129
0
  struct elf32_arm_link_hash_entry *hash;
6130
0
  const insn_sequence *cmse_stub_template;
6131
0
  struct elf32_arm_stub_hash_entry *stub_entry;
6132
0
  int cmse_stub_template_size, new_cmse_stubs_created = *cmse_stub_created;
6133
0
  bfd_vma veneer_value, stub_offset, next_cmse_stub_offset;
6134
0
  bfd_vma cmse_stub_array_start = (bfd_vma) -1, cmse_stub_sec_vma = 0;
6135
6136
  /* No input secure gateway import library.  */
6137
0
  if (!htab->in_implib_bfd)
6138
0
    return true;
6139
6140
0
  in_implib_bfd = htab->in_implib_bfd;
6141
0
  if (!htab->cmse_implib)
6142
0
    {
6143
0
      _bfd_error_handler (_("%pB: --in-implib only supported for Secure "
6144
0
          "Gateway import libraries"), in_implib_bfd);
6145
0
      return false;
6146
0
    }
6147
6148
  /* Get symbol table size.  */
6149
0
  symsize = bfd_get_symtab_upper_bound (in_implib_bfd);
6150
0
  if (symsize < 0)
6151
0
    return false;
6152
6153
  /* Read in the input secure gateway import library's symbol table.  */
6154
0
  sympp = (asymbol **) bfd_malloc (symsize);
6155
0
  if (sympp == NULL)
6156
0
    return false;
6157
6158
0
  symcount = bfd_canonicalize_symtab (in_implib_bfd, sympp);
6159
0
  if (symcount < 0)
6160
0
    {
6161
0
      ret = false;
6162
0
      goto free_sym_buf;
6163
0
    }
6164
6165
0
  htab->new_cmse_stub_offset = 0;
6166
0
  cmse_stub_size =
6167
0
    find_stub_size_and_template (arm_stub_cmse_branch_thumb_only,
6168
0
         &cmse_stub_template,
6169
0
         &cmse_stub_template_size);
6170
0
  out_sec_name =
6171
0
    arm_dedicated_stub_output_section_name (arm_stub_cmse_branch_thumb_only);
6172
0
  stub_out_sec =
6173
0
    bfd_get_section_by_name (htab->obfd, out_sec_name);
6174
0
  if (stub_out_sec != NULL)
6175
0
    cmse_stub_sec_vma = stub_out_sec->vma;
6176
6177
  /* Set addresses of veneers mentionned in input secure gateway import
6178
     library's symbol table.  */
6179
0
  for (i = 0; i < symcount; i++)
6180
0
    {
6181
0
      sym = sympp[i];
6182
0
      flags = sym->flags;
6183
0
      sym_name = (char *) bfd_asymbol_name (sym);
6184
0
      intsym = &((elf_symbol_type *) sym)->internal_elf_sym;
6185
6186
0
      if (sym->section != bfd_abs_section_ptr
6187
0
    || !(flags & (BSF_GLOBAL | BSF_WEAK))
6188
0
    || (flags & BSF_FUNCTION) != BSF_FUNCTION
6189
0
    || (ARM_GET_SYM_BRANCH_TYPE (intsym->st_target_internal)
6190
0
        != ST_BRANCH_TO_THUMB))
6191
0
  {
6192
0
    _bfd_error_handler (_("%pB: invalid import library entry: `%s'; "
6193
0
        "symbol should be absolute, global and "
6194
0
        "refer to Thumb functions"),
6195
0
            in_implib_bfd, sym_name);
6196
0
    ret = false;
6197
0
    continue;
6198
0
  }
6199
6200
0
      veneer_value = bfd_asymbol_value (sym);
6201
0
      stub_offset = veneer_value - cmse_stub_sec_vma;
6202
0
      stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, sym_name,
6203
0
           false, false);
6204
0
      hash = (struct elf32_arm_link_hash_entry *)
6205
0
  elf_link_hash_lookup (&(htab)->root, sym_name, false, false, true);
6206
6207
      /* Stub entry should have been created by cmse_scan or the symbol be of
6208
   a secure function callable from non secure code.  */
6209
0
      if (!stub_entry && !hash)
6210
0
  {
6211
0
    bool new_stub;
6212
6213
0
    _bfd_error_handler
6214
0
      (_("entry function `%s' disappeared from secure code"), sym_name);
6215
0
    hash = (struct elf32_arm_link_hash_entry *)
6216
0
      elf_link_hash_lookup (&(htab)->root, sym_name, true, true, true);
6217
0
    stub_entry
6218
0
      = elf32_arm_create_stub (htab, arm_stub_cmse_branch_thumb_only,
6219
0
             NULL, NULL, bfd_abs_section_ptr, hash,
6220
0
             sym_name, veneer_value,
6221
0
             ST_BRANCH_TO_THUMB, &new_stub);
6222
0
    if (stub_entry == NULL)
6223
0
      ret = false;
6224
0
    else
6225
0
    {
6226
0
      BFD_ASSERT (new_stub);
6227
0
      new_cmse_stubs_created++;
6228
0
      (*cmse_stub_created)++;
6229
0
    }
6230
0
    stub_entry->stub_template_size = stub_entry->stub_size = 0;
6231
0
    stub_entry->stub_offset = stub_offset;
6232
0
  }
6233
      /* Symbol found is not callable from non secure code.  */
6234
0
      else if (!stub_entry)
6235
0
  {
6236
0
    if (!cmse_entry_fct_p (hash))
6237
0
      {
6238
0
        _bfd_error_handler (_("`%s' refers to a non entry function"),
6239
0
          sym_name);
6240
0
        ret = false;
6241
0
      }
6242
0
    continue;
6243
0
  }
6244
0
      else
6245
0
  {
6246
    /* Only stubs for SG veneers should have been created.  */
6247
0
    BFD_ASSERT (stub_entry->stub_type == arm_stub_cmse_branch_thumb_only);
6248
6249
    /* Check visibility hasn't changed.  */
6250
0
    if (!!(flags & BSF_GLOBAL)
6251
0
        != (hash->root.root.type == bfd_link_hash_defined))
6252
0
      _bfd_error_handler
6253
0
        (_("%pB: visibility of symbol `%s' has changed"), in_implib_bfd,
6254
0
         sym_name);
6255
6256
0
    stub_entry->stub_offset = stub_offset;
6257
0
  }
6258
6259
      /* Size should match that of a SG veneer.  */
6260
0
      if (intsym->st_size != cmse_stub_size)
6261
0
  {
6262
0
    _bfd_error_handler (_("%pB: incorrect size for symbol `%s'"),
6263
0
            in_implib_bfd, sym_name);
6264
0
    ret = false;
6265
0
  }
6266
6267
      /* Previous veneer address is before current SG veneer section.  */
6268
0
      if (veneer_value < cmse_stub_sec_vma)
6269
0
  {
6270
    /* Avoid offset underflow.  */
6271
0
    if (stub_entry)
6272
0
      stub_entry->stub_offset = 0;
6273
0
    stub_offset = 0;
6274
0
    ret = false;
6275
0
  }
6276
6277
      /* Complain if stub offset not a multiple of stub size.  */
6278
0
      if (stub_offset % cmse_stub_size)
6279
0
  {
6280
0
    _bfd_error_handler
6281
0
      (_("offset of veneer for entry function `%s' not a multiple of "
6282
0
         "its size"), sym_name);
6283
0
    ret = false;
6284
0
  }
6285
6286
0
      if (!ret)
6287
0
  continue;
6288
6289
0
      new_cmse_stubs_created--;
6290
0
      if (veneer_value < cmse_stub_array_start)
6291
0
  cmse_stub_array_start = veneer_value;
6292
0
      next_cmse_stub_offset = stub_offset + ((cmse_stub_size + 7) & ~7);
6293
0
      if (next_cmse_stub_offset > htab->new_cmse_stub_offset)
6294
0
  htab->new_cmse_stub_offset = next_cmse_stub_offset;
6295
0
    }
6296
6297
0
  if (!info->out_implib_bfd && new_cmse_stubs_created != 0)
6298
0
    {
6299
0
      BFD_ASSERT (new_cmse_stubs_created > 0);
6300
0
      _bfd_error_handler
6301
0
  (_("new entry function(s) introduced but no output import library "
6302
0
     "specified:"));
6303
0
      bfd_hash_traverse (&htab->stub_hash_table, arm_list_new_cmse_stub, info);
6304
0
    }
6305
6306
0
  if (cmse_stub_array_start != cmse_stub_sec_vma)
6307
0
    {
6308
0
      _bfd_error_handler
6309
0
  (_("start address of `%s' is different from previous link"),
6310
0
   out_sec_name);
6311
0
      ret = false;
6312
0
    }
6313
6314
0
 free_sym_buf:
6315
0
  free (sympp);
6316
0
  return ret;
6317
0
}
6318
6319
/* Determine and set the size of the stub section for a final link.
6320
6321
   The basic idea here is to examine all the relocations looking for
6322
   PC-relative calls to a target that is unreachable with a "bl"
6323
   instruction.  */
6324
6325
bool
6326
elf32_arm_size_stubs (bfd *output_bfd,
6327
          bfd *stub_bfd,
6328
          struct bfd_link_info *info,
6329
          bfd_signed_vma group_size,
6330
          asection * (*add_stub_section) (const char *, asection *,
6331
                  asection *,
6332
                  unsigned int),
6333
          void (*layout_sections_again) (void))
6334
0
{
6335
0
  bool ret = true;
6336
0
  obj_attribute *out_attr;
6337
0
  int cmse_stub_created = 0;
6338
0
  bfd_size_type stub_group_size;
6339
0
  bool m_profile, stubs_always_after_branch, first_veneer_scan = true;
6340
0
  struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
6341
0
  struct a8_erratum_fix *a8_fixes = NULL;
6342
0
  unsigned int num_a8_fixes = 0, a8_fix_table_size = 10;
6343
0
  struct a8_erratum_reloc *a8_relocs = NULL;
6344
0
  unsigned int num_a8_relocs = 0, a8_reloc_table_size = 10, i;
6345
6346
0
  if (htab == NULL)
6347
0
    return false;
6348
6349
0
  if (htab->fix_cortex_a8)
6350
0
    {
6351
0
      a8_fixes = (struct a8_erratum_fix *)
6352
0
    bfd_zmalloc (sizeof (struct a8_erratum_fix) * a8_fix_table_size);
6353
0
      a8_relocs = (struct a8_erratum_reloc *)
6354
0
    bfd_zmalloc (sizeof (struct a8_erratum_reloc) * a8_reloc_table_size);
6355
0
    }
6356
6357
  /* Propagate mach to stub bfd, because it may not have been
6358
     finalized when we created stub_bfd.  */
6359
0
  bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
6360
0
         bfd_get_mach (output_bfd));
6361
6362
  /* Stash our params away.  */
6363
0
  htab->add_stub_section = add_stub_section;
6364
0
  htab->layout_sections_again = layout_sections_again;
6365
0
  stubs_always_after_branch = group_size < 0;
6366
6367
0
  out_attr = elf_known_obj_attributes_proc (output_bfd);
6368
0
  m_profile = out_attr[Tag_CPU_arch_profile].i == 'M';
6369
6370
  /* The Cortex-A8 erratum fix depends on stubs not being in the same 4K page
6371
     as the first half of a 32-bit branch straddling two 4K pages.  This is a
6372
     crude way of enforcing that.  */
6373
0
  if (htab->fix_cortex_a8)
6374
0
    stubs_always_after_branch = 1;
6375
6376
0
  if (group_size < 0)
6377
0
    stub_group_size = -group_size;
6378
0
  else
6379
0
    stub_group_size = group_size;
6380
6381
0
  if (stub_group_size == 1)
6382
0
    {
6383
      /* Default values.  */
6384
      /* Thumb branch range is +-4MB has to be used as the default
6385
   maximum size (a given section can contain both ARM and Thumb
6386
   code, so the worst case has to be taken into account).
6387
6388
   This value is 24K less than that, which allows for 2025
6389
   12-byte stubs.  If we exceed that, then we will fail to link.
6390
   The user will have to relink with an explicit group size
6391
   option.  */
6392
0
      stub_group_size = 4170000;
6393
0
    }
6394
6395
0
  group_sections (htab, stub_group_size, stubs_always_after_branch);
6396
6397
  /* If we're applying the cortex A8 fix, we need to determine the
6398
     program header size now, because we cannot change it later --
6399
     that could alter section placements.  Notice the A8 erratum fix
6400
     ends up requiring the section addresses to remain unchanged
6401
     modulo the page size.  That's something we cannot represent
6402
     inside BFD, and we don't want to force the section alignment to
6403
     be the page size.  */
6404
0
  if (htab->fix_cortex_a8)
6405
0
    (*htab->layout_sections_again) ();
6406
6407
0
  while (1)
6408
0
    {
6409
0
      bfd *input_bfd;
6410
0
      asection *stub_sec;
6411
0
      enum elf32_arm_stub_type stub_type;
6412
0
      bool stub_changed = false;
6413
0
      unsigned prev_num_a8_fixes = num_a8_fixes;
6414
6415
0
      num_a8_fixes = 0;
6416
0
      for (input_bfd = info->input_bfds;
6417
0
     input_bfd != NULL;
6418
0
     input_bfd = input_bfd->link.next)
6419
0
  {
6420
0
    Elf_Internal_Shdr *symtab_hdr;
6421
0
    asection *section;
6422
0
    Elf_Internal_Sym *local_syms = NULL;
6423
6424
0
    if (!is_arm_elf (input_bfd))
6425
0
      continue;
6426
0
    if ((input_bfd->flags & DYNAMIC) != 0
6427
0
        && (elf_sym_hashes (input_bfd) == NULL
6428
0
      || (elf_dyn_lib_class (input_bfd) & DYN_AS_NEEDED) != 0))
6429
0
      continue;
6430
6431
0
    num_a8_relocs = 0;
6432
6433
    /* We'll need the symbol table in a second.  */
6434
0
    symtab_hdr = &elf_symtab_hdr (input_bfd);
6435
0
    if (symtab_hdr->sh_info == 0)
6436
0
      continue;
6437
6438
    /* Limit scan of symbols to object file whose profile is
6439
       Microcontroller to not hinder performance in the general case.  */
6440
0
    if (m_profile && first_veneer_scan)
6441
0
      {
6442
0
        struct elf_link_hash_entry **sym_hashes;
6443
6444
0
        sym_hashes = elf_sym_hashes (input_bfd);
6445
0
        if (!cmse_scan (input_bfd, htab, out_attr, sym_hashes,
6446
0
            &cmse_stub_created))
6447
0
    goto error_ret_free_local;
6448
6449
0
        if (cmse_stub_created != 0)
6450
0
    stub_changed = true;
6451
0
      }
6452
6453
    /* Walk over each section attached to the input bfd.  */
6454
0
    for (section = input_bfd->sections;
6455
0
         section != NULL;
6456
0
         section = section->next)
6457
0
      {
6458
0
        Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
6459
6460
        /* If there aren't any relocs, then there's nothing more
6461
     to do.  */
6462
0
        if ((section->flags & SEC_RELOC) == 0
6463
0
      || section->reloc_count == 0
6464
0
      || (section->flags & SEC_CODE) == 0)
6465
0
    continue;
6466
6467
        /* If this section is a link-once section that will be
6468
     discarded, then don't create any stubs.  */
6469
0
        if (section->output_section == NULL
6470
0
      || section->output_section->owner != output_bfd)
6471
0
    continue;
6472
6473
        /* Get the relocs.  */
6474
0
        internal_relocs
6475
0
    = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
6476
0
               NULL, info->keep_memory);
6477
0
        if (internal_relocs == NULL)
6478
0
    goto error_ret_free_local;
6479
6480
        /* Now examine each relocation.  */
6481
0
        irela = internal_relocs;
6482
0
        irelaend = irela + section->reloc_count;
6483
0
        for (; irela < irelaend; irela++)
6484
0
    {
6485
0
      unsigned int r_type, r_indx;
6486
0
      asection *sym_sec;
6487
0
      bfd_vma sym_value;
6488
0
      bfd_vma destination;
6489
0
      struct elf32_arm_link_hash_entry *hash;
6490
0
      const char *sym_name;
6491
0
      unsigned char st_type;
6492
0
      enum arm_st_branch_type branch_type;
6493
0
      bool created_stub = false;
6494
6495
0
      r_type = ELF32_R_TYPE (irela->r_info);
6496
0
      r_indx = ELF32_R_SYM (irela->r_info);
6497
6498
0
      if (r_type >= (unsigned int) R_ARM_max)
6499
0
        {
6500
0
          bfd_set_error (bfd_error_bad_value);
6501
0
        error_ret_free_internal:
6502
0
          if (elf_section_data (section)->relocs == NULL)
6503
0
      free (internal_relocs);
6504
        /* Fall through.  */
6505
0
        error_ret_free_local:
6506
0
          if (symtab_hdr->contents != (unsigned char *) local_syms)
6507
0
      free (local_syms);
6508
0
          return false;
6509
0
        }
6510
6511
0
      hash = NULL;
6512
0
      if (r_indx >= symtab_hdr->sh_info)
6513
0
        hash = elf32_arm_hash_entry
6514
0
          (elf_sym_hashes (input_bfd)
6515
0
           [r_indx - symtab_hdr->sh_info]);
6516
6517
      /* Only look for stubs on branch instructions, or
6518
         non-relaxed TLSCALL  */
6519
0
      if ((r_type != (unsigned int) R_ARM_CALL)
6520
0
          && (r_type != (unsigned int) R_ARM_THM_CALL)
6521
0
          && (r_type != (unsigned int) R_ARM_JUMP24)
6522
0
          && (r_type != (unsigned int) R_ARM_THM_JUMP19)
6523
0
          && (r_type != (unsigned int) R_ARM_THM_XPC22)
6524
0
          && (r_type != (unsigned int) R_ARM_THM_JUMP24)
6525
0
          && (r_type != (unsigned int) R_ARM_PLT32)
6526
0
          && !((r_type == (unsigned int) R_ARM_TLS_CALL
6527
0
          || r_type == (unsigned int) R_ARM_THM_TLS_CALL)
6528
0
         && r_type == (elf32_arm_tls_transition
6529
0
           (info, r_type,
6530
0
            (struct elf_link_hash_entry *) hash))
6531
0
         && ((hash ? hash->tls_type
6532
0
        : (elf32_arm_local_got_tls_type
6533
0
           (input_bfd)[r_indx]))
6534
0
             & GOT_TLS_GDESC) != 0))
6535
0
        continue;
6536
6537
      /* Now determine the call target, its name, value,
6538
         section.  */
6539
0
      sym_sec = NULL;
6540
0
      sym_value = 0;
6541
0
      destination = 0;
6542
0
      sym_name = NULL;
6543
6544
0
      if (r_type == (unsigned int) R_ARM_TLS_CALL
6545
0
          || r_type == (unsigned int) R_ARM_THM_TLS_CALL)
6546
0
        {
6547
          /* A non-relaxed TLS call.  The target is the
6548
       plt-resident trampoline and nothing to do
6549
       with the symbol.  */
6550
0
          BFD_ASSERT (htab->tls_trampoline > 0);
6551
0
          sym_sec = htab->root.splt;
6552
0
          sym_value = htab->tls_trampoline;
6553
0
          hash = 0;
6554
0
          st_type = STT_FUNC;
6555
0
          branch_type = ST_BRANCH_TO_ARM;
6556
0
        }
6557
0
      else if (!hash)
6558
0
        {
6559
          /* It's a local symbol.  */
6560
0
          Elf_Internal_Sym *sym;
6561
6562
0
          if (local_syms == NULL)
6563
0
      {
6564
0
        local_syms
6565
0
          = (Elf_Internal_Sym *) symtab_hdr->contents;
6566
0
        if (local_syms == NULL)
6567
0
          local_syms
6568
0
            = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
6569
0
                  symtab_hdr->sh_info, 0,
6570
0
                  NULL, NULL, NULL);
6571
0
        if (local_syms == NULL)
6572
0
          goto error_ret_free_internal;
6573
0
      }
6574
6575
0
          sym = local_syms + r_indx;
6576
0
          if (sym->st_shndx == SHN_UNDEF)
6577
0
      sym_sec = bfd_und_section_ptr;
6578
0
          else if (sym->st_shndx == SHN_ABS)
6579
0
      sym_sec = bfd_abs_section_ptr;
6580
0
          else if (sym->st_shndx == SHN_COMMON)
6581
0
      sym_sec = bfd_com_section_ptr;
6582
0
          else
6583
0
      sym_sec =
6584
0
        bfd_section_from_elf_index (input_bfd, sym->st_shndx);
6585
6586
0
          if (!sym_sec)
6587
      /* This is an undefined symbol.  It can never
6588
         be resolved.  */
6589
0
      continue;
6590
6591
0
          if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
6592
0
      sym_value = sym->st_value;
6593
0
          destination = (sym_value + irela->r_addend
6594
0
             + sym_sec->output_offset
6595
0
             + sym_sec->output_section->vma);
6596
0
          st_type = ELF_ST_TYPE (sym->st_info);
6597
0
          branch_type =
6598
0
      ARM_GET_SYM_BRANCH_TYPE (sym->st_target_internal);
6599
0
          sym_name
6600
0
      = bfd_elf_string_from_elf_section (input_bfd,
6601
0
                 symtab_hdr->sh_link,
6602
0
                 sym->st_name);
6603
0
        }
6604
0
      else
6605
0
        {
6606
          /* It's an external symbol.  */
6607
0
          while (hash->root.root.type == bfd_link_hash_indirect
6608
0
           || hash->root.root.type == bfd_link_hash_warning)
6609
0
      hash = ((struct elf32_arm_link_hash_entry *)
6610
0
        hash->root.root.u.i.link);
6611
6612
0
          if (hash->root.root.type == bfd_link_hash_defined
6613
0
        || hash->root.root.type == bfd_link_hash_defweak)
6614
0
      {
6615
0
        sym_sec = hash->root.root.u.def.section;
6616
0
        sym_value = hash->root.root.u.def.value;
6617
6618
0
        struct elf32_arm_link_hash_table *globals =
6619
0
              elf32_arm_hash_table (info);
6620
6621
        /* For a destination in a shared library,
6622
           use the PLT stub as target address to
6623
           decide whether a branch stub is
6624
           needed.  */
6625
0
        if (globals != NULL
6626
0
            && globals->root.splt != NULL
6627
0
            && hash != NULL
6628
0
            && hash->root.plt.offset != (bfd_vma) -1)
6629
0
          {
6630
0
            sym_sec = globals->root.splt;
6631
0
            sym_value = hash->root.plt.offset;
6632
0
            if (sym_sec->output_section != NULL)
6633
0
        destination = (sym_value
6634
0
                 + sym_sec->output_offset
6635
0
                 + sym_sec->output_section->vma);
6636
0
          }
6637
0
        else if (sym_sec->output_section != NULL)
6638
0
          destination = (sym_value + irela->r_addend
6639
0
             + sym_sec->output_offset
6640
0
             + sym_sec->output_section->vma);
6641
0
      }
6642
0
          else if ((hash->root.root.type == bfd_link_hash_undefined)
6643
0
             || (hash->root.root.type == bfd_link_hash_undefweak))
6644
0
      {
6645
        /* For a shared library, use the PLT stub as
6646
           target address to decide whether a long
6647
           branch stub is needed.
6648
           For absolute code, they cannot be handled.  */
6649
0
        struct elf32_arm_link_hash_table *globals =
6650
0
          elf32_arm_hash_table (info);
6651
6652
0
        if (globals != NULL
6653
0
            && globals->root.splt != NULL
6654
0
            && hash != NULL
6655
0
            && hash->root.plt.offset != (bfd_vma) -1)
6656
0
          {
6657
0
            sym_sec = globals->root.splt;
6658
0
            sym_value = hash->root.plt.offset;
6659
0
            if (sym_sec->output_section != NULL)
6660
0
        destination = (sym_value
6661
0
                 + sym_sec->output_offset
6662
0
                 + sym_sec->output_section->vma);
6663
0
          }
6664
0
        else
6665
0
          continue;
6666
0
      }
6667
0
          else
6668
0
      {
6669
0
        bfd_set_error (bfd_error_bad_value);
6670
0
        goto error_ret_free_internal;
6671
0
      }
6672
0
          st_type = hash->root.type;
6673
0
          branch_type =
6674
0
      ARM_GET_SYM_BRANCH_TYPE (hash->root.target_internal);
6675
0
          sym_name = hash->root.root.root.string;
6676
0
        }
6677
6678
0
      do
6679
0
        {
6680
0
          bool new_stub;
6681
0
          struct elf32_arm_stub_hash_entry *stub_entry;
6682
6683
          /* Determine what (if any) linker stub is needed.  */
6684
0
          stub_type = arm_type_of_stub (info, section, irela,
6685
0
                st_type, &branch_type,
6686
0
                hash, destination, sym_sec,
6687
0
                input_bfd, sym_name);
6688
0
          if (stub_type == arm_stub_none)
6689
0
      break;
6690
6691
          /* We've either created a stub for this reloc already,
6692
       or we are about to.  */
6693
0
          stub_entry =
6694
0
      elf32_arm_create_stub (htab, stub_type, section, irela,
6695
0
                 sym_sec, hash,
6696
0
                 (char *) sym_name, sym_value,
6697
0
                 branch_type, &new_stub);
6698
6699
0
          created_stub = stub_entry != NULL;
6700
0
          if (!created_stub)
6701
0
      goto error_ret_free_internal;
6702
0
          else if (!new_stub)
6703
0
      break;
6704
0
          else
6705
0
      stub_changed = true;
6706
0
        }
6707
0
      while (0);
6708
6709
      /* Look for relocations which might trigger Cortex-A8
6710
         erratum.  */
6711
0
      if (htab->fix_cortex_a8
6712
0
          && (r_type == (unsigned int) R_ARM_THM_JUMP24
6713
0
        || r_type == (unsigned int) R_ARM_THM_JUMP19
6714
0
        || r_type == (unsigned int) R_ARM_THM_CALL
6715
0
        || r_type == (unsigned int) R_ARM_THM_XPC22))
6716
0
        {
6717
0
          bfd_vma from = section->output_section->vma
6718
0
             + section->output_offset
6719
0
             + irela->r_offset;
6720
6721
0
          if ((from & 0xfff) == 0xffe)
6722
0
      {
6723
        /* Found a candidate.  Note we haven't checked the
6724
           destination is within 4K here: if we do so (and
6725
           don't create an entry in a8_relocs) we can't tell
6726
           that a branch should have been relocated when
6727
           scanning later.  */
6728
0
        if (num_a8_relocs == a8_reloc_table_size)
6729
0
          {
6730
0
            a8_reloc_table_size *= 2;
6731
0
            a8_relocs = (struct a8_erratum_reloc *)
6732
0
          bfd_realloc (a8_relocs,
6733
0
                 sizeof (struct a8_erratum_reloc)
6734
0
                 * a8_reloc_table_size);
6735
0
          }
6736
6737
0
        a8_relocs[num_a8_relocs].from = from;
6738
0
        a8_relocs[num_a8_relocs].destination = destination;
6739
0
        a8_relocs[num_a8_relocs].r_type = r_type;
6740
0
        a8_relocs[num_a8_relocs].branch_type = branch_type;
6741
0
        a8_relocs[num_a8_relocs].sym_name = sym_name;
6742
0
        a8_relocs[num_a8_relocs].non_a8_stub = created_stub;
6743
0
        a8_relocs[num_a8_relocs].hash = hash;
6744
6745
0
        num_a8_relocs++;
6746
0
      }
6747
0
        }
6748
0
    }
6749
6750
        /* We're done with the internal relocs, free them.  */
6751
0
        if (elf_section_data (section)->relocs == NULL)
6752
0
    free (internal_relocs);
6753
0
      }
6754
6755
0
    if (htab->fix_cortex_a8)
6756
0
      {
6757
        /* Sort relocs which might apply to Cortex-A8 erratum.  */
6758
0
        qsort (a8_relocs, num_a8_relocs,
6759
0
         sizeof (struct a8_erratum_reloc),
6760
0
         &a8_reloc_compare);
6761
6762
        /* Scan for branches which might trigger Cortex-A8 erratum.  */
6763
0
        if (cortex_a8_erratum_scan (input_bfd, info, &a8_fixes,
6764
0
            &num_a8_fixes, &a8_fix_table_size,
6765
0
            a8_relocs, num_a8_relocs,
6766
0
            prev_num_a8_fixes, &stub_changed)
6767
0
      != 0)
6768
0
    goto error_ret_free_local;
6769
0
      }
6770
6771
0
    if (local_syms != NULL
6772
0
        && symtab_hdr->contents != (unsigned char *) local_syms)
6773
0
      {
6774
0
        if (!info->keep_memory)
6775
0
    free (local_syms);
6776
0
        else
6777
0
    symtab_hdr->contents = (unsigned char *) local_syms;
6778
0
      }
6779
0
  }
6780
6781
0
      if (first_veneer_scan
6782
0
    && !set_cmse_veneer_addr_from_implib (info, htab,
6783
0
            &cmse_stub_created))
6784
0
  ret = false;
6785
6786
0
      if (prev_num_a8_fixes != num_a8_fixes)
6787
0
  stub_changed = true;
6788
6789
0
      if (!stub_changed)
6790
0
  break;
6791
6792
      /* OK, we've added some stubs.  Find out the new size of the
6793
   stub sections.  */
6794
0
      for (stub_sec = htab->stub_bfd->sections;
6795
0
     stub_sec != NULL;
6796
0
     stub_sec = stub_sec->next)
6797
0
  {
6798
    /* Ignore non-stub sections.  */
6799
0
    if (!strstr (stub_sec->name, STUB_SUFFIX))
6800
0
      continue;
6801
6802
0
    stub_sec->size = 0;
6803
0
  }
6804
6805
      /* Add new SG veneers after those already in the input import
6806
   library.  */
6807
0
      for (stub_type = arm_stub_none + 1; stub_type < max_stub_type;
6808
0
     stub_type++)
6809
0
  {
6810
0
    bfd_vma *start_offset_p;
6811
0
    asection **stub_sec_p;
6812
6813
0
    start_offset_p = arm_new_stubs_start_offset_ptr (htab, stub_type);
6814
0
    stub_sec_p = arm_dedicated_stub_input_section_ptr (htab, stub_type);
6815
0
    if (start_offset_p == NULL)
6816
0
      continue;
6817
6818
0
    BFD_ASSERT (stub_sec_p != NULL);
6819
0
    if (*stub_sec_p != NULL)
6820
0
      (*stub_sec_p)->size = *start_offset_p;
6821
0
  }
6822
6823
      /* Compute stub section size, considering padding.  */
6824
0
      bfd_hash_traverse (&htab->stub_hash_table, arm_size_one_stub, htab);
6825
0
      for (stub_type = arm_stub_none + 1; stub_type < max_stub_type;
6826
0
     stub_type++)
6827
0
  {
6828
0
    int size, padding;
6829
0
    asection **stub_sec_p;
6830
6831
0
    padding = arm_dedicated_stub_section_padding (stub_type);
6832
0
    stub_sec_p = arm_dedicated_stub_input_section_ptr (htab, stub_type);
6833
    /* Skip if no stub input section or no stub section padding
6834
       required.  */
6835
0
    if ((stub_sec_p != NULL && *stub_sec_p == NULL) || padding == 0)
6836
0
      continue;
6837
    /* Stub section padding required but no dedicated section.  */
6838
0
    BFD_ASSERT (stub_sec_p);
6839
6840
0
    size = (*stub_sec_p)->size;
6841
0
    size = (size + padding - 1) & ~(padding - 1);
6842
0
    (*stub_sec_p)->size = size;
6843
0
  }
6844
6845
      /* Add Cortex-A8 erratum veneers to stub section sizes too.  */
6846
0
      if (htab->fix_cortex_a8)
6847
0
  for (i = 0; i < num_a8_fixes; i++)
6848
0
    {
6849
0
      stub_sec = elf32_arm_create_or_find_stub_sec (NULL,
6850
0
       a8_fixes[i].section, htab, a8_fixes[i].stub_type);
6851
6852
0
      if (stub_sec == NULL)
6853
0
        return false;
6854
6855
0
      stub_sec->size
6856
0
        += find_stub_size_and_template (a8_fixes[i].stub_type, NULL,
6857
0
                NULL);
6858
0
    }
6859
6860
6861
      /* Ask the linker to do its stuff.  */
6862
0
      (*htab->layout_sections_again) ();
6863
0
      first_veneer_scan = false;
6864
0
    }
6865
6866
  /* Add stubs for Cortex-A8 erratum fixes now.  */
6867
0
  if (htab->fix_cortex_a8)
6868
0
    {
6869
0
      for (i = 0; i < num_a8_fixes; i++)
6870
0
  {
6871
0
    struct elf32_arm_stub_hash_entry *stub_entry;
6872
0
    char *stub_name = a8_fixes[i].stub_name;
6873
0
    asection *section = a8_fixes[i].section;
6874
0
    unsigned int section_id = a8_fixes[i].section->id;
6875
0
    asection *link_sec = htab->stub_group[section_id].link_sec;
6876
0
    asection *stub_sec = htab->stub_group[section_id].stub_sec;
6877
0
    const insn_sequence *template_sequence;
6878
0
    int template_size, size = 0;
6879
6880
0
    stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name,
6881
0
               true, false);
6882
0
    if (stub_entry == NULL)
6883
0
      {
6884
0
        _bfd_error_handler (_("%pB: cannot create stub entry %s"),
6885
0
          section->owner, stub_name);
6886
0
        return false;
6887
0
      }
6888
6889
0
    stub_entry->stub_sec = stub_sec;
6890
0
    stub_entry->stub_offset = (bfd_vma) -1;
6891
0
    stub_entry->id_sec = link_sec;
6892
0
    stub_entry->stub_type = a8_fixes[i].stub_type;
6893
0
    stub_entry->source_value = a8_fixes[i].offset;
6894
0
    stub_entry->target_section = a8_fixes[i].section;
6895
0
    stub_entry->target_value = a8_fixes[i].target_offset;
6896
0
    stub_entry->orig_insn = a8_fixes[i].orig_insn;
6897
0
    stub_entry->branch_type = a8_fixes[i].branch_type;
6898
6899
0
    size = find_stub_size_and_template (a8_fixes[i].stub_type,
6900
0
                &template_sequence,
6901
0
                &template_size);
6902
6903
0
    stub_entry->stub_size = size;
6904
0
    stub_entry->stub_template = template_sequence;
6905
0
    stub_entry->stub_template_size = template_size;
6906
0
  }
6907
6908
      /* Stash the Cortex-A8 erratum fix array for use later in
6909
   elf32_arm_write_section().  */
6910
0
      htab->a8_erratum_fixes = a8_fixes;
6911
0
      htab->num_a8_erratum_fixes = num_a8_fixes;
6912
0
    }
6913
0
  else
6914
0
    {
6915
0
      htab->a8_erratum_fixes = NULL;
6916
0
      htab->num_a8_erratum_fixes = 0;
6917
0
    }
6918
0
  return ret;
6919
0
}
6920
6921
/* Build all the stubs associated with the current output file.  The
6922
   stubs are kept in a hash table attached to the main linker hash
6923
   table.  We also set up the .plt entries for statically linked PIC
6924
   functions here.  This function is called via arm_elf_finish in the
6925
   linker.  */
6926
6927
bool
6928
elf32_arm_build_stubs (struct bfd_link_info *info)
6929
0
{
6930
0
  asection *stub_sec;
6931
0
  struct bfd_hash_table *table;
6932
0
  enum elf32_arm_stub_type stub_type;
6933
0
  struct elf32_arm_link_hash_table *htab;
6934
6935
0
  htab = elf32_arm_hash_table (info);
6936
0
  if (htab == NULL)
6937
0
    return false;
6938
6939
0
  for (stub_sec = htab->stub_bfd->sections;
6940
0
       stub_sec != NULL;
6941
0
       stub_sec = stub_sec->next)
6942
0
    {
6943
0
      bfd_size_type size;
6944
6945
      /* Ignore non-stub sections.  */
6946
0
      if (!strstr (stub_sec->name, STUB_SUFFIX))
6947
0
  continue;
6948
6949
      /* Allocate memory to hold the linker stubs.  Zeroing the stub sections
6950
   must at least be done for stub section requiring padding and for SG
6951
   veneers to ensure that a non secure code branching to a removed SG
6952
   veneer causes an error.  */
6953
0
      size = stub_sec->size;
6954
0
      stub_sec->contents = (unsigned char *) bfd_zalloc (htab->stub_bfd, size);
6955
0
      if (stub_sec->contents == NULL && size != 0)
6956
0
  return false;
6957
0
      stub_sec->alloced = 1;
6958
6959
0
      stub_sec->size = 0;
6960
0
    }
6961
6962
  /* Add new SG veneers after those already in the input import library.  */
6963
0
  for (stub_type = arm_stub_none + 1; stub_type < max_stub_type; stub_type++)
6964
0
    {
6965
0
      bfd_vma *start_offset_p;
6966
0
      asection **stub_sec_p;
6967
6968
0
      start_offset_p = arm_new_stubs_start_offset_ptr (htab, stub_type);
6969
0
      stub_sec_p = arm_dedicated_stub_input_section_ptr (htab, stub_type);
6970
0
      if (start_offset_p == NULL)
6971
0
  continue;
6972
6973
0
      BFD_ASSERT (stub_sec_p != NULL);
6974
0
      if (*stub_sec_p != NULL)
6975
0
  (*stub_sec_p)->size = *start_offset_p;
6976
0
    }
6977
6978
  /* Build the stubs as directed by the stub hash table.  */
6979
0
  table = &htab->stub_hash_table;
6980
0
  bfd_hash_traverse (table, arm_build_one_stub, info);
6981
0
  if (htab->fix_cortex_a8)
6982
0
    {
6983
      /* Place the cortex a8 stubs last.  */
6984
0
      htab->fix_cortex_a8 = -1;
6985
0
      bfd_hash_traverse (table, arm_build_one_stub, info);
6986
0
    }
6987
6988
0
  return true;
6989
0
}
6990
6991
/* Locate the Thumb encoded calling stub for NAME.  */
6992
6993
static struct elf_link_hash_entry *
6994
find_thumb_glue (struct bfd_link_info *link_info,
6995
     const char *name,
6996
     char **error_message)
6997
0
{
6998
0
  char *tmp_name;
6999
0
  struct elf_link_hash_entry *hash;
7000
0
  struct elf32_arm_link_hash_table *hash_table;
7001
7002
  /* We need a pointer to the armelf specific hash table.  */
7003
0
  hash_table = elf32_arm_hash_table (link_info);
7004
0
  if (hash_table == NULL)
7005
0
    return NULL;
7006
7007
0
  tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
7008
0
          + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
7009
7010
0
  BFD_ASSERT (tmp_name);
7011
7012
0
  sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
7013
7014
0
  hash = elf_link_hash_lookup
7015
0
    (&(hash_table)->root, tmp_name, false, false, true);
7016
7017
0
  if (hash == NULL)
7018
0
    {
7019
0
      *error_message = bfd_asprintf (_("unable to find %s glue '%s' for '%s'"),
7020
0
             "Thumb", tmp_name, name);
7021
0
      if (*error_message == NULL)
7022
0
  *error_message = (char *) bfd_errmsg (bfd_error_system_call);
7023
0
    }
7024
7025
0
  free (tmp_name);
7026
7027
0
  return hash;
7028
0
}
7029
7030
/* Locate the ARM encoded calling stub for NAME.  */
7031
7032
static struct elf_link_hash_entry *
7033
find_arm_glue (struct bfd_link_info *link_info,
7034
         const char *name,
7035
         char **error_message)
7036
0
{
7037
0
  char *tmp_name;
7038
0
  struct elf_link_hash_entry *myh;
7039
0
  struct elf32_arm_link_hash_table *hash_table;
7040
7041
  /* We need a pointer to the elfarm specific hash table.  */
7042
0
  hash_table = elf32_arm_hash_table (link_info);
7043
0
  if (hash_table == NULL)
7044
0
    return NULL;
7045
7046
0
  tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
7047
0
          + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
7048
0
  BFD_ASSERT (tmp_name);
7049
7050
0
  sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
7051
7052
0
  myh = elf_link_hash_lookup
7053
0
    (&(hash_table)->root, tmp_name, false, false, true);
7054
7055
0
  if (myh == NULL)
7056
0
    {
7057
0
      *error_message = bfd_asprintf (_("unable to find %s glue '%s' for '%s'"),
7058
0
             "ARM", tmp_name, name);
7059
0
      if (*error_message == NULL)
7060
0
  *error_message = (char *) bfd_errmsg (bfd_error_system_call);
7061
0
    }
7062
0
  free (tmp_name);
7063
7064
0
  return myh;
7065
0
}
7066
7067
/* ARM->Thumb glue (static images):
7068
7069
   .arm
7070
   __func_from_arm:
7071
   ldr r12, __func_addr
7072
   bx  r12
7073
   __func_addr:
7074
   .word func    @ behave as if you saw a ARM_32 reloc.
7075
7076
   (v5t static images)
7077
   .arm
7078
   __func_from_arm:
7079
   ldr pc, __func_addr
7080
   __func_addr:
7081
   .word func    @ behave as if you saw a ARM_32 reloc.
7082
7083
   (relocatable images)
7084
   .arm
7085
   __func_from_arm:
7086
   ldr r12, __func_offset
7087
   add r12, r12, pc
7088
   bx  r12
7089
   __func_offset:
7090
   .word func - .   */
7091
7092
0
#define ARM2THUMB_STATIC_GLUE_SIZE 12
7093
static const insn32 a2t1_ldr_insn = 0xe59fc000;
7094
static const insn32 a2t2_bx_r12_insn = 0xe12fff1c;
7095
static const insn32 a2t3_func_addr_insn = 0x00000001;
7096
7097
0
#define ARM2THUMB_V5_STATIC_GLUE_SIZE 8
7098
static const insn32 a2t1v5_ldr_insn = 0xe51ff004;
7099
static const insn32 a2t2v5_func_addr_insn = 0x00000001;
7100
7101
0
#define ARM2THUMB_PIC_GLUE_SIZE 16
7102
static const insn32 a2t1p_ldr_insn = 0xe59fc004;
7103
static const insn32 a2t2p_add_pc_insn = 0xe08cc00f;
7104
static const insn32 a2t3p_bx_r12_insn = 0xe12fff1c;
7105
7106
/* Thumb->ARM:        Thumb->(non-interworking aware) ARM
7107
7108
     .thumb       .thumb
7109
     .align 2       .align 2
7110
 __func_from_thumb:       __func_from_thumb:
7111
     bx pc        push {r6, lr}
7112
     nop        ldr  r6, __func_addr
7113
     .arm       mov  lr, pc
7114
     b func       bx   r6
7115
          .arm
7116
            ;; back_to_thumb
7117
          ldmia r13! {r6, lr}
7118
          bx    lr
7119
            __func_addr:
7120
          .word      func  */
7121
7122
0
#define THUMB2ARM_GLUE_SIZE 8
7123
static const insn16 t2a1_bx_pc_insn = 0x4778;
7124
static const insn16 t2a2_noop_insn = 0x46c0;
7125
static const insn32 t2a3_b_insn = 0xea000000;
7126
7127
0
#define VFP11_ERRATUM_VENEER_SIZE 8
7128
0
#define STM32L4XX_ERRATUM_LDM_VENEER_SIZE 16
7129
0
#define STM32L4XX_ERRATUM_VLDM_VENEER_SIZE 24
7130
7131
0
#define ARM_BX_VENEER_SIZE 12
7132
static const insn32 armbx1_tst_insn = 0xe3100001;
7133
static const insn32 armbx2_moveq_insn = 0x01a0f000;
7134
static const insn32 armbx3_bx_insn = 0xe12fff10;
7135
7136
#ifndef ELFARM_NABI_C_INCLUDED
7137
static void
7138
arm_allocate_glue_section_space (bfd * abfd, bfd_size_type size, const char * name)
7139
0
{
7140
0
  asection * s;
7141
0
  bfd_byte * contents;
7142
7143
0
  if (size == 0)
7144
0
    {
7145
      /* Do not include empty glue sections in the output.  */
7146
0
      if (abfd != NULL)
7147
0
  {
7148
0
    s = bfd_get_linker_section (abfd, name);
7149
0
    if (s != NULL)
7150
0
      s->flags |= SEC_EXCLUDE;
7151
0
  }
7152
0
      return;
7153
0
    }
7154
7155
0
  BFD_ASSERT (abfd != NULL);
7156
7157
0
  s = bfd_get_linker_section (abfd, name);
7158
0
  BFD_ASSERT (s != NULL);
7159
7160
0
  contents = (bfd_byte *) bfd_zalloc (abfd, size);
7161
7162
0
  BFD_ASSERT (s->size == size);
7163
0
  s->contents = contents;
7164
0
  s->alloced = 1;
7165
0
}
7166
7167
bool
7168
bfd_elf32_arm_allocate_interworking_sections (struct bfd_link_info * info)
7169
0
{
7170
0
  struct elf32_arm_link_hash_table * globals;
7171
7172
0
  globals = elf32_arm_hash_table (info);
7173
0
  BFD_ASSERT (globals != NULL);
7174
7175
0
  arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
7176
0
           globals->arm_glue_size,
7177
0
           ARM2THUMB_GLUE_SECTION_NAME);
7178
7179
0
  arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
7180
0
           globals->thumb_glue_size,
7181
0
           THUMB2ARM_GLUE_SECTION_NAME);
7182
7183
0
  arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
7184
0
           globals->vfp11_erratum_glue_size,
7185
0
           VFP11_ERRATUM_VENEER_SECTION_NAME);
7186
7187
0
  arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
7188
0
           globals->stm32l4xx_erratum_glue_size,
7189
0
           STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
7190
7191
0
  arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
7192
0
           globals->bx_glue_size,
7193
0
           ARM_BX_GLUE_SECTION_NAME);
7194
7195
0
  return true;
7196
0
}
7197
7198
/* Allocate space and symbols for calling a Thumb function from Arm mode.
7199
   returns the symbol identifying the stub.  */
7200
7201
static struct elf_link_hash_entry *
7202
record_arm_to_thumb_glue (struct bfd_link_info * link_info,
7203
        struct elf_link_hash_entry * h)
7204
0
{
7205
0
  const char * name = h->root.root.string;
7206
0
  asection * s;
7207
0
  char * tmp_name;
7208
0
  struct elf_link_hash_entry * myh;
7209
0
  struct bfd_link_hash_entry * bh;
7210
0
  struct elf32_arm_link_hash_table * globals;
7211
0
  bfd_vma val;
7212
0
  bfd_size_type size;
7213
7214
0
  globals = elf32_arm_hash_table (link_info);
7215
0
  BFD_ASSERT (globals != NULL);
7216
0
  BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
7217
7218
0
  s = bfd_get_linker_section
7219
0
    (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
7220
7221
0
  BFD_ASSERT (s != NULL);
7222
7223
0
  tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
7224
0
          + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
7225
0
  BFD_ASSERT (tmp_name);
7226
7227
0
  sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
7228
7229
0
  myh = elf_link_hash_lookup
7230
0
    (&(globals)->root, tmp_name, false, false, true);
7231
7232
0
  if (myh != NULL)
7233
0
    {
7234
      /* We've already seen this guy.  */
7235
0
      free (tmp_name);
7236
0
      return myh;
7237
0
    }
7238
7239
  /* The only trick here is using hash_table->arm_glue_size as the value.
7240
     Even though the section isn't allocated yet, this is where we will be
7241
     putting it.  The +1 on the value marks that the stub has not been
7242
     output yet - not that it is a Thumb function.  */
7243
0
  bh = NULL;
7244
0
  val = globals->arm_glue_size + 1;
7245
0
  _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
7246
0
            tmp_name, BSF_GLOBAL, s, val,
7247
0
            NULL, true, false, &bh);
7248
7249
0
  myh = (struct elf_link_hash_entry *) bh;
7250
0
  myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7251
0
  myh->forced_local = 1;
7252
7253
0
  free (tmp_name);
7254
7255
0
  if (bfd_link_pic (link_info)
7256
0
      || globals->pic_veneer)
7257
0
    size = ARM2THUMB_PIC_GLUE_SIZE;
7258
0
  else if (globals->use_blx)
7259
0
    size = ARM2THUMB_V5_STATIC_GLUE_SIZE;
7260
0
  else
7261
0
    size = ARM2THUMB_STATIC_GLUE_SIZE;
7262
7263
0
  s->size += size;
7264
0
  globals->arm_glue_size += size;
7265
7266
0
  return myh;
7267
0
}
7268
7269
/* Allocate space for ARMv4 BX veneers.  */
7270
7271
static void
7272
record_arm_bx_glue (struct bfd_link_info * link_info, int reg)
7273
0
{
7274
0
  asection * s;
7275
0
  struct elf32_arm_link_hash_table *globals;
7276
0
  char *tmp_name;
7277
0
  struct elf_link_hash_entry *myh;
7278
0
  struct bfd_link_hash_entry *bh;
7279
0
  bfd_vma val;
7280
7281
  /* BX PC does not need a veneer.  */
7282
0
  if (reg == 15)
7283
0
    return;
7284
7285
0
  globals = elf32_arm_hash_table (link_info);
7286
0
  BFD_ASSERT (globals != NULL);
7287
0
  BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
7288
7289
  /* Check if this veneer has already been allocated.  */
7290
0
  if (globals->bx_glue_offset[reg])
7291
0
    return;
7292
7293
0
  s = bfd_get_linker_section
7294
0
    (globals->bfd_of_glue_owner, ARM_BX_GLUE_SECTION_NAME);
7295
7296
0
  BFD_ASSERT (s != NULL);
7297
7298
  /* Add symbol for veneer.  */
7299
0
  tmp_name = (char *)
7300
0
      bfd_malloc ((bfd_size_type) strlen (ARM_BX_GLUE_ENTRY_NAME) + 1);
7301
0
  BFD_ASSERT (tmp_name);
7302
7303
0
  sprintf (tmp_name, ARM_BX_GLUE_ENTRY_NAME, reg);
7304
7305
0
  myh = elf_link_hash_lookup
7306
0
    (&(globals)->root, tmp_name, false, false, false);
7307
7308
0
  BFD_ASSERT (myh == NULL);
7309
7310
0
  bh = NULL;
7311
0
  val = globals->bx_glue_size;
7312
0
  _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
7313
0
            tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
7314
0
            NULL, true, false, &bh);
7315
7316
0
  myh = (struct elf_link_hash_entry *) bh;
7317
0
  myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7318
0
  myh->forced_local = 1;
7319
7320
0
  s->size += ARM_BX_VENEER_SIZE;
7321
0
  globals->bx_glue_offset[reg] = globals->bx_glue_size | 2;
7322
0
  globals->bx_glue_size += ARM_BX_VENEER_SIZE;
7323
0
}
7324
7325
7326
/* Add an entry to the code/data map for section SEC.  */
7327
7328
static void
7329
elf32_arm_section_map_add (asection *sec, char type, bfd_vma vma)
7330
0
{
7331
0
  struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
7332
0
  unsigned int newidx;
7333
7334
0
  if (sec_data->map == NULL)
7335
0
    {
7336
0
      sec_data->map = (elf32_arm_section_map *)
7337
0
    bfd_malloc (sizeof (elf32_arm_section_map));
7338
0
      sec_data->mapcount = 0;
7339
0
      sec_data->mapsize = 1;
7340
0
    }
7341
7342
0
  newidx = sec_data->mapcount++;
7343
7344
0
  if (sec_data->mapcount > sec_data->mapsize)
7345
0
    {
7346
0
      sec_data->mapsize *= 2;
7347
0
      sec_data->map = (elf32_arm_section_map *)
7348
0
    bfd_realloc_or_free (sec_data->map, sec_data->mapsize
7349
0
             * sizeof (elf32_arm_section_map));
7350
0
    }
7351
7352
0
  if (sec_data->map)
7353
0
    {
7354
0
      sec_data->map[newidx].vma = vma;
7355
0
      sec_data->map[newidx].type = type;
7356
0
    }
7357
0
}
7358
7359
7360
/* Record information about a VFP11 denorm-erratum veneer.  Only ARM-mode
7361
   veneers are handled for now.  */
7362
7363
static bfd_vma
7364
record_vfp11_erratum_veneer (struct bfd_link_info *link_info,
7365
           elf32_vfp11_erratum_list *branch,
7366
           bfd *branch_bfd,
7367
           asection *branch_sec,
7368
           unsigned int offset)
7369
0
{
7370
0
  asection *s;
7371
0
  struct elf32_arm_link_hash_table *hash_table;
7372
0
  char *tmp_name;
7373
0
  struct elf_link_hash_entry *myh;
7374
0
  struct bfd_link_hash_entry *bh;
7375
0
  bfd_vma val;
7376
0
  struct _arm_elf_section_data *sec_data;
7377
0
  elf32_vfp11_erratum_list *newerr;
7378
7379
0
  hash_table = elf32_arm_hash_table (link_info);
7380
0
  BFD_ASSERT (hash_table != NULL);
7381
0
  BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
7382
7383
0
  s = bfd_get_linker_section
7384
0
    (hash_table->bfd_of_glue_owner, VFP11_ERRATUM_VENEER_SECTION_NAME);
7385
7386
0
  sec_data = elf32_arm_section_data (s);
7387
7388
0
  BFD_ASSERT (s != NULL);
7389
7390
0
  tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
7391
0
          (VFP11_ERRATUM_VENEER_ENTRY_NAME) + 10);
7392
0
  BFD_ASSERT (tmp_name);
7393
7394
0
  sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME,
7395
0
     hash_table->num_vfp11_fixes);
7396
7397
0
  myh = elf_link_hash_lookup
7398
0
    (&(hash_table)->root, tmp_name, false, false, false);
7399
7400
0
  BFD_ASSERT (myh == NULL);
7401
7402
0
  bh = NULL;
7403
0
  val = hash_table->vfp11_erratum_glue_size;
7404
0
  _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
7405
0
            tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
7406
0
            NULL, true, false, &bh);
7407
7408
0
  myh = (struct elf_link_hash_entry *) bh;
7409
0
  myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7410
0
  myh->forced_local = 1;
7411
7412
  /* Link veneer back to calling location.  */
7413
0
  sec_data->erratumcount += 1;
7414
0
  newerr = (elf32_vfp11_erratum_list *)
7415
0
      bfd_zmalloc (sizeof (elf32_vfp11_erratum_list));
7416
7417
0
  newerr->type = VFP11_ERRATUM_ARM_VENEER;
7418
0
  newerr->vma = -1;
7419
0
  newerr->u.v.branch = branch;
7420
0
  newerr->u.v.id = hash_table->num_vfp11_fixes;
7421
0
  branch->u.b.veneer = newerr;
7422
7423
0
  newerr->next = sec_data->erratumlist;
7424
0
  sec_data->erratumlist = newerr;
7425
7426
  /* A symbol for the return from the veneer.  */
7427
0
  sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME "_r",
7428
0
     hash_table->num_vfp11_fixes);
7429
7430
0
  myh = elf_link_hash_lookup
7431
0
    (&(hash_table)->root, tmp_name, false, false, false);
7432
7433
0
  if (myh != NULL)
7434
0
    abort ();
7435
7436
0
  bh = NULL;
7437
0
  val = offset + 4;
7438
0
  _bfd_generic_link_add_one_symbol (link_info, branch_bfd, tmp_name, BSF_LOCAL,
7439
0
            branch_sec, val, NULL, true, false, &bh);
7440
7441
0
  myh = (struct elf_link_hash_entry *) bh;
7442
0
  myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7443
0
  myh->forced_local = 1;
7444
7445
0
  free (tmp_name);
7446
7447
  /* Generate a mapping symbol for the veneer section, and explicitly add an
7448
     entry for that symbol to the code/data map for the section.  */
7449
0
  if (hash_table->vfp11_erratum_glue_size == 0)
7450
0
    {
7451
0
      bh = NULL;
7452
      /* FIXME: Creates an ARM symbol.  Thumb mode will need attention if it
7453
   ever requires this erratum fix.  */
7454
0
      _bfd_generic_link_add_one_symbol (link_info,
7455
0
          hash_table->bfd_of_glue_owner, "$a",
7456
0
          BSF_LOCAL, s, 0, NULL,
7457
0
          true, false, &bh);
7458
7459
0
      myh = (struct elf_link_hash_entry *) bh;
7460
0
      myh->type = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
7461
0
      myh->forced_local = 1;
7462
7463
      /* The elf32_arm_init_maps function only cares about symbols from input
7464
   BFDs.  We must make a note of this generated mapping symbol
7465
   ourselves so that code byteswapping works properly in
7466
   elf32_arm_write_section.  */
7467
0
      elf32_arm_section_map_add (s, 'a', 0);
7468
0
    }
7469
7470
0
  s->size += VFP11_ERRATUM_VENEER_SIZE;
7471
0
  hash_table->vfp11_erratum_glue_size += VFP11_ERRATUM_VENEER_SIZE;
7472
0
  hash_table->num_vfp11_fixes++;
7473
7474
  /* The offset of the veneer.  */
7475
0
  return val;
7476
0
}
7477
7478
/* Record information about a STM32L4XX STM erratum veneer.  Only THUMB-mode
7479
   veneers need to be handled because used only in Cortex-M.  */
7480
7481
static bfd_vma
7482
record_stm32l4xx_erratum_veneer (struct bfd_link_info *link_info,
7483
         elf32_stm32l4xx_erratum_list *branch,
7484
         bfd *branch_bfd,
7485
         asection *branch_sec,
7486
         unsigned int offset,
7487
         bfd_size_type veneer_size)
7488
0
{
7489
0
  asection *s;
7490
0
  struct elf32_arm_link_hash_table *hash_table;
7491
0
  char *tmp_name;
7492
0
  struct elf_link_hash_entry *myh;
7493
0
  struct bfd_link_hash_entry *bh;
7494
0
  bfd_vma val;
7495
0
  struct _arm_elf_section_data *sec_data;
7496
0
  elf32_stm32l4xx_erratum_list *newerr;
7497
7498
0
  hash_table = elf32_arm_hash_table (link_info);
7499
0
  BFD_ASSERT (hash_table != NULL);
7500
0
  BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
7501
7502
0
  s = bfd_get_linker_section
7503
0
    (hash_table->bfd_of_glue_owner, STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
7504
7505
0
  BFD_ASSERT (s != NULL);
7506
7507
0
  sec_data = elf32_arm_section_data (s);
7508
7509
0
  tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
7510
0
          (STM32L4XX_ERRATUM_VENEER_ENTRY_NAME) + 10);
7511
0
  BFD_ASSERT (tmp_name);
7512
7513
0
  sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME,
7514
0
     hash_table->num_stm32l4xx_fixes);
7515
7516
0
  myh = elf_link_hash_lookup
7517
0
    (&(hash_table)->root, tmp_name, false, false, false);
7518
7519
0
  BFD_ASSERT (myh == NULL);
7520
7521
0
  bh = NULL;
7522
0
  val = hash_table->stm32l4xx_erratum_glue_size;
7523
0
  _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
7524
0
            tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
7525
0
            NULL, true, false, &bh);
7526
7527
0
  myh = (struct elf_link_hash_entry *) bh;
7528
0
  myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7529
0
  myh->forced_local = 1;
7530
7531
  /* Link veneer back to calling location.  */
7532
0
  sec_data->stm32l4xx_erratumcount += 1;
7533
0
  newerr = (elf32_stm32l4xx_erratum_list *)
7534
0
      bfd_zmalloc (sizeof (elf32_stm32l4xx_erratum_list));
7535
7536
0
  newerr->type = STM32L4XX_ERRATUM_VENEER;
7537
0
  newerr->vma = -1;
7538
0
  newerr->u.v.branch = branch;
7539
0
  newerr->u.v.id = hash_table->num_stm32l4xx_fixes;
7540
0
  branch->u.b.veneer = newerr;
7541
7542
0
  newerr->next = sec_data->stm32l4xx_erratumlist;
7543
0
  sec_data->stm32l4xx_erratumlist = newerr;
7544
7545
  /* A symbol for the return from the veneer.  */
7546
0
  sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME "_r",
7547
0
     hash_table->num_stm32l4xx_fixes);
7548
7549
0
  myh = elf_link_hash_lookup
7550
0
    (&(hash_table)->root, tmp_name, false, false, false);
7551
7552
0
  if (myh != NULL)
7553
0
    abort ();
7554
7555
0
  bh = NULL;
7556
0
  val = offset + 4;
7557
0
  _bfd_generic_link_add_one_symbol (link_info, branch_bfd, tmp_name, BSF_LOCAL,
7558
0
            branch_sec, val, NULL, true, false, &bh);
7559
7560
0
  myh = (struct elf_link_hash_entry *) bh;
7561
0
  myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7562
0
  myh->forced_local = 1;
7563
7564
0
  free (tmp_name);
7565
7566
  /* Generate a mapping symbol for the veneer section, and explicitly add an
7567
     entry for that symbol to the code/data map for the section.  */
7568
0
  if (hash_table->stm32l4xx_erratum_glue_size == 0)
7569
0
    {
7570
0
      bh = NULL;
7571
      /* Creates a THUMB symbol since there is no other choice.  */
7572
0
      _bfd_generic_link_add_one_symbol (link_info,
7573
0
          hash_table->bfd_of_glue_owner, "$t",
7574
0
          BSF_LOCAL, s, 0, NULL,
7575
0
          true, false, &bh);
7576
7577
0
      myh = (struct elf_link_hash_entry *) bh;
7578
0
      myh->type = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
7579
0
      myh->forced_local = 1;
7580
7581
      /* The elf32_arm_init_maps function only cares about symbols from input
7582
   BFDs.  We must make a note of this generated mapping symbol
7583
   ourselves so that code byteswapping works properly in
7584
   elf32_arm_write_section.  */
7585
0
      elf32_arm_section_map_add (s, 't', 0);
7586
0
    }
7587
7588
0
  s->size += veneer_size;
7589
0
  hash_table->stm32l4xx_erratum_glue_size += veneer_size;
7590
0
  hash_table->num_stm32l4xx_fixes++;
7591
7592
  /* The offset of the veneer.  */
7593
0
  return val;
7594
0
}
7595
7596
#define ARM_GLUE_SECTION_FLAGS \
7597
0
  (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE \
7598
0
   | SEC_READONLY | SEC_LINKER_CREATED)
7599
7600
/* Create a fake section for use by the ARM backend of the linker.  */
7601
7602
static bool
7603
arm_make_glue_section (bfd * abfd, const char * name)
7604
0
{
7605
0
  asection * sec;
7606
7607
0
  sec = bfd_get_linker_section (abfd, name);
7608
0
  if (sec != NULL)
7609
    /* Already made.  */
7610
0
    return true;
7611
7612
0
  sec = bfd_make_section_anyway_with_flags (abfd, name, ARM_GLUE_SECTION_FLAGS);
7613
7614
0
  if (sec == NULL
7615
0
      || !bfd_set_section_alignment (sec, 2))
7616
0
    return false;
7617
7618
  /* Set the gc mark to prevent the section from being removed by garbage
7619
     collection, despite the fact that no relocs refer to this section.  */
7620
0
  sec->gc_mark = 1;
7621
7622
0
  return true;
7623
0
}
7624
7625
/* Set size of .plt entries.  This function is called from the
7626
   linker scripts in ld/emultempl/{armelf}.em.  */
7627
7628
void
7629
bfd_elf32_arm_use_long_plt (void)
7630
0
{
7631
0
  elf32_arm_use_long_plt_entry = true;
7632
0
}
7633
7634
/* Add the glue sections to the stub bfd.  */
7635
7636
static bool
7637
bfd_elf32_arm_add_glue_sections_to_bfd (bfd *abfd,
7638
          struct bfd_link_info *info)
7639
0
{
7640
0
  struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
7641
0
  bool dostm32l4xx = globals
7642
0
    && globals->stm32l4xx_fix != BFD_ARM_STM32L4XX_FIX_NONE;
7643
0
  bool addglue;
7644
7645
  /* If we are only performing a partial
7646
     link do not bother adding the glue.  */
7647
0
  if (bfd_link_relocatable (info))
7648
0
    return true;
7649
7650
0
  addglue = arm_make_glue_section (abfd, ARM2THUMB_GLUE_SECTION_NAME)
7651
0
    && arm_make_glue_section (abfd, THUMB2ARM_GLUE_SECTION_NAME)
7652
0
    && arm_make_glue_section (abfd, VFP11_ERRATUM_VENEER_SECTION_NAME)
7653
0
    && arm_make_glue_section (abfd, ARM_BX_GLUE_SECTION_NAME);
7654
7655
0
  if (!dostm32l4xx)
7656
0
    return addglue;
7657
7658
0
  return addglue
7659
0
    && arm_make_glue_section (abfd, STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
7660
0
}
7661
7662
/* Mark output sections of veneers needing a dedicated one with SEC_KEEP.  This
7663
   ensures they are not marked for deletion by
7664
   strip_excluded_output_sections () when veneers are going to be created
7665
   later.  Not doing so would trigger assert on empty section size in
7666
   lang_size_sections_1 ().  */
7667
7668
void
7669
bfd_elf32_arm_keep_private_stub_output_sections (struct bfd_link_info *info)
7670
0
{
7671
0
  enum elf32_arm_stub_type stub_type;
7672
7673
  /* If we are only performing a partial
7674
     link do not bother adding the glue.  */
7675
0
  if (bfd_link_relocatable (info))
7676
0
    return;
7677
7678
0
  for (stub_type = arm_stub_none + 1; stub_type < max_stub_type; stub_type++)
7679
0
    {
7680
0
      asection *out_sec;
7681
0
      const char *out_sec_name;
7682
7683
0
      if (!arm_dedicated_stub_output_section_required (stub_type))
7684
0
  continue;
7685
7686
0
     out_sec_name = arm_dedicated_stub_output_section_name (stub_type);
7687
0
     out_sec = bfd_get_section_by_name (info->output_bfd, out_sec_name);
7688
0
     if (out_sec != NULL)
7689
0
  out_sec->flags |= SEC_KEEP;
7690
0
    }
7691
0
}
7692
7693
/* Select a BFD to be used to hold the sections used by the glue code.  */
7694
7695
static bool
7696
bfd_elf32_arm_get_bfd_for_interworking (bfd *abfd, struct bfd_link_info *info)
7697
0
{
7698
0
  struct elf32_arm_link_hash_table *globals;
7699
7700
  /* If we are only performing a partial link
7701
     do not bother getting a bfd to hold the glue.  */
7702
0
  if (bfd_link_relocatable (info))
7703
0
    return true;
7704
7705
  /* Make sure we don't attach the glue sections to a dynamic object.  */
7706
0
  BFD_ASSERT (!(abfd->flags & DYNAMIC));
7707
7708
0
  globals = elf32_arm_hash_table (info);
7709
0
  BFD_ASSERT (globals != NULL);
7710
7711
0
  if (globals->bfd_of_glue_owner != NULL)
7712
0
    return true;
7713
7714
  /* Save the bfd for later use.  */
7715
0
  globals->bfd_of_glue_owner = abfd;
7716
7717
0
  return true;
7718
0
}
7719
7720
static void
7721
check_use_blx (struct elf32_arm_link_hash_table *globals)
7722
0
{
7723
0
  int cpu_arch;
7724
7725
0
  cpu_arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
7726
0
               Tag_CPU_arch);
7727
7728
0
  if (globals->fix_arm1176)
7729
0
    {
7730
0
      if (cpu_arch == TAG_CPU_ARCH_V6T2 || cpu_arch > TAG_CPU_ARCH_V6K)
7731
0
  globals->use_blx = 1;
7732
0
    }
7733
0
  else
7734
0
    {
7735
0
      if (cpu_arch > TAG_CPU_ARCH_V4T)
7736
0
  globals->use_blx = 1;
7737
0
    }
7738
0
}
7739
7740
bool
7741
bfd_elf32_arm_process_before_allocation (bfd *abfd,
7742
           struct bfd_link_info *link_info)
7743
0
{
7744
0
  Elf_Internal_Shdr *symtab_hdr;
7745
0
  Elf_Internal_Rela *internal_relocs = NULL;
7746
0
  Elf_Internal_Rela *irel, *irelend;
7747
0
  bfd_byte *contents = NULL;
7748
7749
0
  asection *sec;
7750
0
  struct elf32_arm_link_hash_table *globals;
7751
7752
  /* If we are only performing a partial link do not bother
7753
     to construct any glue.  */
7754
0
  if (bfd_link_relocatable (link_info))
7755
0
    return true;
7756
7757
  /* Here we have a bfd that is to be included on the link.  We have a
7758
     hook to do reloc rummaging, before section sizes are nailed down.  */
7759
0
  globals = elf32_arm_hash_table (link_info);
7760
0
  BFD_ASSERT (globals != NULL);
7761
7762
0
  check_use_blx (globals);
7763
7764
0
  if (globals->byteswap_code && !bfd_big_endian (abfd))
7765
0
    {
7766
0
      _bfd_error_handler (_("%pB: BE8 images only valid in big-endian mode"),
7767
0
        abfd);
7768
0
      return false;
7769
0
    }
7770
7771
  /* PR 5398: If we have not decided to include any loadable sections in
7772
     the output then we will not have a glue owner bfd.  This is OK, it
7773
     just means that there is nothing else for us to do here.  */
7774
0
  if (globals->bfd_of_glue_owner == NULL)
7775
0
    return true;
7776
7777
  /* Rummage around all the relocs and map the glue vectors.  */
7778
0
  sec = abfd->sections;
7779
7780
0
  if (sec == NULL)
7781
0
    return true;
7782
7783
0
  for (; sec != NULL; sec = sec->next)
7784
0
    {
7785
0
      if (sec->reloc_count == 0)
7786
0
  continue;
7787
7788
0
      if ((sec->flags & SEC_EXCLUDE) != 0
7789
0
    || (sec->flags & SEC_HAS_CONTENTS) == 0)
7790
0
  continue;
7791
7792
0
      symtab_hdr = & elf_symtab_hdr (abfd);
7793
7794
      /* Load the relocs.  */
7795
0
      internal_relocs
7796
0
  = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, false);
7797
7798
0
      if (internal_relocs == NULL)
7799
0
  goto error_return;
7800
7801
0
      irelend = internal_relocs + sec->reloc_count;
7802
0
      for (irel = internal_relocs; irel < irelend; irel++)
7803
0
  {
7804
0
    long r_type;
7805
0
    unsigned long r_index;
7806
7807
0
    struct elf_link_hash_entry *h;
7808
7809
0
    r_type = ELF32_R_TYPE (irel->r_info);
7810
0
    r_index = ELF32_R_SYM (irel->r_info);
7811
7812
    /* These are the only relocation types we care about.  */
7813
0
    if (   r_type != R_ARM_PC24
7814
0
        && (r_type != R_ARM_V4BX || globals->fix_v4bx < 2))
7815
0
      continue;
7816
7817
    /* Get the section contents if we haven't done so already.  */
7818
0
    if (contents == NULL)
7819
0
      {
7820
        /* Get cached copy if it exists.  */
7821
0
        if (elf_section_data (sec)->this_hdr.contents != NULL)
7822
0
    contents = elf_section_data (sec)->this_hdr.contents;
7823
0
        else
7824
0
    {
7825
      /* Go get them off disk.  */
7826
0
      if (! bfd_malloc_and_get_section (abfd, sec, &contents))
7827
0
        goto error_return;
7828
0
    }
7829
0
      }
7830
7831
0
    if (r_type == R_ARM_V4BX)
7832
0
      {
7833
0
        int reg;
7834
7835
0
        reg = bfd_get_32 (abfd, contents + irel->r_offset) & 0xf;
7836
0
        record_arm_bx_glue (link_info, reg);
7837
0
        continue;
7838
0
      }
7839
7840
    /* If the relocation is not against a symbol it cannot concern us.  */
7841
0
    h = NULL;
7842
7843
    /* We don't care about local symbols.  */
7844
0
    if (r_index < symtab_hdr->sh_info)
7845
0
      continue;
7846
7847
    /* This is an external symbol.  */
7848
0
    r_index -= symtab_hdr->sh_info;
7849
0
    h = (struct elf_link_hash_entry *)
7850
0
      elf_sym_hashes (abfd)[r_index];
7851
7852
    /* If the relocation is against a static symbol it must be within
7853
       the current section and so cannot be a cross ARM/Thumb relocation.  */
7854
0
    if (h == NULL)
7855
0
      continue;
7856
7857
    /* If the call will go through a PLT entry then we do not need
7858
       glue.  */
7859
0
    if (globals->root.splt != NULL && h->plt.offset != (bfd_vma) -1)
7860
0
      continue;
7861
7862
0
    switch (r_type)
7863
0
      {
7864
0
      case R_ARM_PC24:
7865
        /* This one is a call from arm code.  We need to look up
7866
     the target of the call.  If it is a thumb target, we
7867
     insert glue.  */
7868
0
        if (ARM_GET_SYM_BRANCH_TYPE (h->target_internal)
7869
0
      == ST_BRANCH_TO_THUMB)
7870
0
    record_arm_to_thumb_glue (link_info, h);
7871
0
        break;
7872
7873
0
      default:
7874
0
        abort ();
7875
0
      }
7876
0
  }
7877
7878
0
      if (elf_section_data (sec)->this_hdr.contents != contents)
7879
0
  free (contents);
7880
0
      contents = NULL;
7881
7882
0
      if (elf_section_data (sec)->relocs != internal_relocs)
7883
0
  free (internal_relocs);
7884
0
      internal_relocs = NULL;
7885
0
    }
7886
7887
0
  return true;
7888
7889
0
 error_return:
7890
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
7891
0
    free (contents);
7892
0
  if (elf_section_data (sec)->relocs != internal_relocs)
7893
0
    free (internal_relocs);
7894
7895
0
  return false;
7896
0
}
7897
#endif
7898
7899
7900
/* Initialise maps of ARM/Thumb/data for input BFDs.  */
7901
7902
void
7903
bfd_elf32_arm_init_maps (bfd *abfd)
7904
0
{
7905
0
  Elf_Internal_Sym *isymbuf;
7906
0
  Elf_Internal_Shdr *hdr;
7907
0
  unsigned int i, localsyms;
7908
7909
  /* PR 7093: Make sure that we are dealing with an arm elf binary.  */
7910
0
  if (! is_arm_elf (abfd))
7911
0
    return;
7912
7913
0
  if ((abfd->flags & DYNAMIC) != 0)
7914
0
    return;
7915
7916
0
  hdr = & elf_symtab_hdr (abfd);
7917
0
  localsyms = hdr->sh_info;
7918
7919
  /* Obtain a buffer full of symbols for this BFD. The hdr->sh_info field
7920
     should contain the number of local symbols, which should come before any
7921
     global symbols.  Mapping symbols are always local.  */
7922
0
  isymbuf = bfd_elf_get_elf_syms (abfd, hdr, localsyms, 0, NULL, NULL,
7923
0
          NULL);
7924
7925
  /* No internal symbols read?  Skip this BFD.  */
7926
0
  if (isymbuf == NULL)
7927
0
    return;
7928
7929
0
  for (i = 0; i < localsyms; i++)
7930
0
    {
7931
0
      Elf_Internal_Sym *isym = &isymbuf[i];
7932
0
      asection *sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7933
0
      const char *name;
7934
7935
0
      if (sec != NULL
7936
0
    && ELF_ST_BIND (isym->st_info) == STB_LOCAL)
7937
0
  {
7938
0
    name = bfd_elf_string_from_elf_section (abfd,
7939
0
      hdr->sh_link, isym->st_name);
7940
7941
0
    if (bfd_is_arm_special_symbol_name (name,
7942
0
                BFD_ARM_SPECIAL_SYM_TYPE_MAP))
7943
0
      elf32_arm_section_map_add (sec, name[1], isym->st_value);
7944
0
  }
7945
0
    }
7946
0
}
7947
7948
7949
/* Auto-select enabling of Cortex-A8 erratum fix if the user didn't explicitly
7950
   say what they wanted.  */
7951
7952
void
7953
bfd_elf32_arm_set_cortex_a8_fix (bfd *obfd, struct bfd_link_info *link_info)
7954
0
{
7955
0
  struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
7956
0
  obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
7957
7958
0
  if (globals == NULL)
7959
0
    return;
7960
7961
0
  if (globals->fix_cortex_a8 == -1)
7962
0
    {
7963
      /* Turn on Cortex-A8 erratum workaround for ARMv7-A.  */
7964
0
      if (out_attr[Tag_CPU_arch].i == TAG_CPU_ARCH_V7
7965
0
    && (out_attr[Tag_CPU_arch_profile].i == 'A'
7966
0
        || out_attr[Tag_CPU_arch_profile].i == 0))
7967
0
  globals->fix_cortex_a8 = 1;
7968
0
      else
7969
0
  globals->fix_cortex_a8 = 0;
7970
0
    }
7971
0
}
7972
7973
7974
void
7975
bfd_elf32_arm_set_vfp11_fix (bfd *obfd, struct bfd_link_info *link_info)
7976
0
{
7977
0
  struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
7978
0
  obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
7979
7980
0
  if (globals == NULL)
7981
0
    return;
7982
  /* We assume that ARMv7+ does not need the VFP11 denorm erratum fix.  */
7983
0
  if (out_attr[Tag_CPU_arch].i >= TAG_CPU_ARCH_V7)
7984
0
    {
7985
0
      switch (globals->vfp11_fix)
7986
0
  {
7987
0
  case BFD_ARM_VFP11_FIX_DEFAULT:
7988
0
  case BFD_ARM_VFP11_FIX_NONE:
7989
0
    globals->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
7990
0
    break;
7991
7992
0
  default:
7993
    /* Give a warning, but do as the user requests anyway.  */
7994
0
    _bfd_error_handler (_("%pB: warning: selected VFP11 erratum "
7995
0
      "workaround is not necessary for target architecture"), obfd);
7996
0
  }
7997
0
    }
7998
0
  else if (globals->vfp11_fix == BFD_ARM_VFP11_FIX_DEFAULT)
7999
    /* For earlier architectures, we might need the workaround, but do not
8000
       enable it by default.  If users is running with broken hardware, they
8001
       must enable the erratum fix explicitly.  */
8002
0
    globals->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
8003
0
}
8004
8005
void
8006
bfd_elf32_arm_set_stm32l4xx_fix (bfd *obfd, struct bfd_link_info *link_info)
8007
0
{
8008
0
  struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
8009
0
  obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
8010
8011
0
  if (globals == NULL)
8012
0
    return;
8013
8014
  /* We assume only Cortex-M4 may require the fix.  */
8015
0
  if (out_attr[Tag_CPU_arch].i != TAG_CPU_ARCH_V7E_M
8016
0
      || out_attr[Tag_CPU_arch_profile].i != 'M')
8017
0
    {
8018
0
      if (globals->stm32l4xx_fix != BFD_ARM_STM32L4XX_FIX_NONE)
8019
  /* Give a warning, but do as the user requests anyway.  */
8020
0
  _bfd_error_handler
8021
0
    (_("%pB: warning: selected STM32L4XX erratum "
8022
0
       "workaround is not necessary for target architecture"), obfd);
8023
0
    }
8024
0
}
8025
8026
enum bfd_arm_vfp11_pipe
8027
{
8028
  VFP11_FMAC,
8029
  VFP11_LS,
8030
  VFP11_DS,
8031
  VFP11_BAD
8032
};
8033
8034
/* Return a VFP register number.  This is encoded as RX:X for single-precision
8035
   registers, or X:RX for double-precision registers, where RX is the group of
8036
   four bits in the instruction encoding and X is the single extension bit.
8037
   RX and X fields are specified using their lowest (starting) bit.  The return
8038
   value is:
8039
8040
     0...31: single-precision registers s0...s31
8041
     32...63: double-precision registers d0...d31.
8042
8043
   Although X should be zero for VFP11 (encoding d0...d15 only), we might
8044
   encounter VFP3 instructions, so we allow the full range for DP registers.  */
8045
8046
static unsigned int
8047
bfd_arm_vfp11_regno (unsigned int insn, bool is_double, unsigned int rx,
8048
         unsigned int x)
8049
0
{
8050
0
  if (is_double)
8051
0
    return (((insn >> rx) & 0xf) | (((insn >> x) & 1) << 4)) + 32;
8052
0
  else
8053
0
    return (((insn >> rx) & 0xf) << 1) | ((insn >> x) & 1);
8054
0
}
8055
8056
/* Set bits in *WMASK according to a register number REG as encoded by
8057
   bfd_arm_vfp11_regno().  Ignore d16-d31.  */
8058
8059
static void
8060
bfd_arm_vfp11_write_mask (unsigned int *wmask, unsigned int reg)
8061
0
{
8062
0
  if (reg < 32)
8063
0
    *wmask |= 1 << reg;
8064
0
  else if (reg < 48)
8065
0
    *wmask |= 3 << ((reg - 32) * 2);
8066
0
}
8067
8068
/* Return TRUE if WMASK overwrites anything in REGS.  */
8069
8070
static bool
8071
bfd_arm_vfp11_antidependency (unsigned int wmask, int *regs, int numregs)
8072
0
{
8073
0
  int i;
8074
8075
0
  for (i = 0; i < numregs; i++)
8076
0
    {
8077
0
      unsigned int reg = regs[i];
8078
8079
0
      if (reg < 32 && (wmask & (1 << reg)) != 0)
8080
0
  return true;
8081
8082
0
      reg -= 32;
8083
8084
0
      if (reg >= 16)
8085
0
  continue;
8086
8087
0
      if ((wmask & (3 << (reg * 2))) != 0)
8088
0
  return true;
8089
0
    }
8090
8091
0
  return false;
8092
0
}
8093
8094
/* In this function, we're interested in two things: finding input registers
8095
   for VFP data-processing instructions, and finding the set of registers which
8096
   arbitrary VFP instructions may write to.  We use a 32-bit unsigned int to
8097
   hold the written set, so FLDM etc. are easy to deal with (we're only
8098
   interested in 32 SP registers or 16 dp registers, due to the VFP version
8099
   implemented by the chip in question).  DP registers are marked by setting
8100
   both SP registers in the write mask).  */
8101
8102
static enum bfd_arm_vfp11_pipe
8103
bfd_arm_vfp11_insn_decode (unsigned int insn, unsigned int *destmask, int *regs,
8104
         int *numregs)
8105
0
{
8106
0
  enum bfd_arm_vfp11_pipe vpipe = VFP11_BAD;
8107
0
  bool is_double = ((insn & 0xf00) == 0xb00) ? 1 : 0;
8108
8109
0
  if ((insn & 0x0f000e10) == 0x0e000a00)  /* A data-processing insn.  */
8110
0
    {
8111
0
      unsigned int pqrs;
8112
0
      unsigned int fd = bfd_arm_vfp11_regno (insn, is_double, 12, 22);
8113
0
      unsigned int fm = bfd_arm_vfp11_regno (insn, is_double, 0, 5);
8114
8115
0
      pqrs = ((insn & 0x00800000) >> 20)
8116
0
     | ((insn & 0x00300000) >> 19)
8117
0
     | ((insn & 0x00000040) >> 6);
8118
8119
0
      switch (pqrs)
8120
0
  {
8121
0
  case 0: /* fmac[sd].  */
8122
0
  case 1: /* fnmac[sd].  */
8123
0
  case 2: /* fmsc[sd].  */
8124
0
  case 3: /* fnmsc[sd].  */
8125
0
    vpipe = VFP11_FMAC;
8126
0
    bfd_arm_vfp11_write_mask (destmask, fd);
8127
0
    regs[0] = fd;
8128
0
    regs[1] = bfd_arm_vfp11_regno (insn, is_double, 16, 7);  /* Fn.  */
8129
0
    regs[2] = fm;
8130
0
    *numregs = 3;
8131
0
    break;
8132
8133
0
  case 4: /* fmul[sd].  */
8134
0
  case 5: /* fnmul[sd].  */
8135
0
  case 6: /* fadd[sd].  */
8136
0
  case 7: /* fsub[sd].  */
8137
0
    vpipe = VFP11_FMAC;
8138
0
    goto vfp_binop;
8139
8140
0
  case 8: /* fdiv[sd].  */
8141
0
    vpipe = VFP11_DS;
8142
0
    vfp_binop:
8143
0
    bfd_arm_vfp11_write_mask (destmask, fd);
8144
0
    regs[0] = bfd_arm_vfp11_regno (insn, is_double, 16, 7);   /* Fn.  */
8145
0
    regs[1] = fm;
8146
0
    *numregs = 2;
8147
0
    break;
8148
8149
0
  case 15: /* extended opcode.  */
8150
0
    {
8151
0
      unsigned int extn = ((insn >> 15) & 0x1e)
8152
0
            | ((insn >> 7) & 1);
8153
8154
0
      switch (extn)
8155
0
        {
8156
0
        case 0: /* fcpy[sd].  */
8157
0
        case 1: /* fabs[sd].  */
8158
0
        case 2: /* fneg[sd].  */
8159
0
        case 8: /* fcmp[sd].  */
8160
0
        case 9: /* fcmpe[sd].  */
8161
0
        case 10: /* fcmpz[sd].  */
8162
0
        case 11: /* fcmpez[sd].  */
8163
0
        case 16: /* fuito[sd].  */
8164
0
        case 17: /* fsito[sd].  */
8165
0
        case 24: /* ftoui[sd].  */
8166
0
        case 25: /* ftouiz[sd].  */
8167
0
        case 26: /* ftosi[sd].  */
8168
0
        case 27: /* ftosiz[sd].  */
8169
    /* These instructions will not bounce due to underflow.  */
8170
0
    *numregs = 0;
8171
0
    vpipe = VFP11_FMAC;
8172
0
    break;
8173
8174
0
        case 3: /* fsqrt[sd].  */
8175
    /* fsqrt cannot underflow, but it can (perhaps) overwrite
8176
       registers to cause the erratum in previous instructions.  */
8177
0
    bfd_arm_vfp11_write_mask (destmask, fd);
8178
0
    vpipe = VFP11_DS;
8179
0
    break;
8180
8181
0
        case 15: /* fcvt{ds,sd}.  */
8182
0
    {
8183
0
      int rnum = 0;
8184
8185
0
      bfd_arm_vfp11_write_mask (destmask, fd);
8186
8187
      /* Only FCVTSD can underflow.  */
8188
0
      if ((insn & 0x100) != 0)
8189
0
        regs[rnum++] = fm;
8190
8191
0
      *numregs = rnum;
8192
8193
0
      vpipe = VFP11_FMAC;
8194
0
    }
8195
0
    break;
8196
8197
0
        default:
8198
0
    return VFP11_BAD;
8199
0
        }
8200
0
    }
8201
0
    break;
8202
8203
0
  default:
8204
0
    return VFP11_BAD;
8205
0
  }
8206
0
    }
8207
  /* Two-register transfer.  */
8208
0
  else if ((insn & 0x0fe00ed0) == 0x0c400a10)
8209
0
    {
8210
0
      unsigned int fm = bfd_arm_vfp11_regno (insn, is_double, 0, 5);
8211
8212
0
      if ((insn & 0x100000) == 0)
8213
0
  {
8214
0
    if (is_double)
8215
0
      bfd_arm_vfp11_write_mask (destmask, fm);
8216
0
    else
8217
0
      {
8218
0
        bfd_arm_vfp11_write_mask (destmask, fm);
8219
0
        bfd_arm_vfp11_write_mask (destmask, fm + 1);
8220
0
      }
8221
0
  }
8222
8223
0
      vpipe = VFP11_LS;
8224
0
    }
8225
0
  else if ((insn & 0x0e100e00) == 0x0c100a00)  /* A load insn.  */
8226
0
    {
8227
0
      int fd = bfd_arm_vfp11_regno (insn, is_double, 12, 22);
8228
0
      unsigned int puw = ((insn >> 21) & 0x1) | (((insn >> 23) & 3) << 1);
8229
8230
0
      switch (puw)
8231
0
  {
8232
0
  case 0: /* Two-reg transfer.  We should catch these above.  */
8233
0
    abort ();
8234
8235
0
  case 2: /* fldm[sdx].  */
8236
0
  case 3:
8237
0
  case 5:
8238
0
    {
8239
0
      unsigned int i, offset = insn & 0xff;
8240
8241
0
      if (is_double)
8242
0
        offset >>= 1;
8243
8244
0
      for (i = fd; i < fd + offset; i++)
8245
0
        bfd_arm_vfp11_write_mask (destmask, i);
8246
0
    }
8247
0
    break;
8248
8249
0
  case 4: /* fld[sd].  */
8250
0
  case 6:
8251
0
    bfd_arm_vfp11_write_mask (destmask, fd);
8252
0
    break;
8253
8254
0
  default:
8255
0
    return VFP11_BAD;
8256
0
  }
8257
8258
0
      vpipe = VFP11_LS;
8259
0
    }
8260
  /* Single-register transfer. Note L==0.  */
8261
0
  else if ((insn & 0x0f100e10) == 0x0e000a10)
8262
0
    {
8263
0
      unsigned int opcode = (insn >> 21) & 7;
8264
0
      unsigned int fn = bfd_arm_vfp11_regno (insn, is_double, 16, 7);
8265
8266
0
      switch (opcode)
8267
0
  {
8268
0
  case 0: /* fmsr/fmdlr.  */
8269
0
  case 1: /* fmdhr.  */
8270
    /* Mark fmdhr and fmdlr as writing to the whole of the DP
8271
       destination register.  I don't know if this is exactly right,
8272
       but it is the conservative choice.  */
8273
0
    bfd_arm_vfp11_write_mask (destmask, fn);
8274
0
    break;
8275
8276
0
  case 7: /* fmxr.  */
8277
0
    break;
8278
0
  }
8279
8280
0
      vpipe = VFP11_LS;
8281
0
    }
8282
8283
0
  return vpipe;
8284
0
}
8285
8286
8287
static int elf32_arm_compare_mapping (const void * a, const void * b);
8288
8289
8290
/* Look for potentially-troublesome code sequences which might trigger the
8291
   VFP11 denormal/antidependency erratum.  See, e.g., the ARM1136 errata sheet
8292
   (available from ARM) for details of the erratum.  A short version is
8293
   described in ld.texinfo.  */
8294
8295
bool
8296
bfd_elf32_arm_vfp11_erratum_scan (bfd *abfd, struct bfd_link_info *link_info)
8297
0
{
8298
0
  asection *sec;
8299
0
  bfd_byte *contents = NULL;
8300
0
  int state = 0;
8301
0
  int regs[3], numregs = 0;
8302
0
  struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
8303
0
  int use_vector = (globals->vfp11_fix == BFD_ARM_VFP11_FIX_VECTOR);
8304
8305
0
  if (globals == NULL)
8306
0
    return false;
8307
8308
  /* We use a simple FSM to match troublesome VFP11 instruction sequences.
8309
     The states transition as follows:
8310
8311
       0 -> 1 (vector) or 0 -> 2 (scalar)
8312
     A VFP FMAC-pipeline instruction has been seen. Fill
8313
     regs[0]..regs[numregs-1] with its input operands. Remember this
8314
     instruction in 'first_fmac'.
8315
8316
       1 -> 2
8317
     Any instruction, except for a VFP instruction which overwrites
8318
     regs[*].
8319
8320
       1 -> 3 [ -> 0 ]  or
8321
       2 -> 3 [ -> 0 ]
8322
     A VFP instruction has been seen which overwrites any of regs[*].
8323
     We must make a veneer!  Reset state to 0 before examining next
8324
     instruction.
8325
8326
       2 -> 0
8327
     If we fail to match anything in state 2, reset to state 0 and reset
8328
     the instruction pointer to the instruction after 'first_fmac'.
8329
8330
     If the VFP11 vector mode is in use, there must be at least two unrelated
8331
     instructions between anti-dependent VFP11 instructions to properly avoid
8332
     triggering the erratum, hence the use of the extra state 1.  */
8333
8334
  /* If we are only performing a partial link do not bother
8335
     to construct any glue.  */
8336
0
  if (bfd_link_relocatable (link_info))
8337
0
    return true;
8338
8339
  /* Skip if this bfd does not correspond to an ELF image.  */
8340
0
  if (! is_arm_elf (abfd))
8341
0
    return true;
8342
8343
  /* We should have chosen a fix type by the time we get here.  */
8344
0
  BFD_ASSERT (globals->vfp11_fix != BFD_ARM_VFP11_FIX_DEFAULT);
8345
8346
0
  if (globals->vfp11_fix == BFD_ARM_VFP11_FIX_NONE)
8347
0
    return true;
8348
8349
  /* Skip this BFD if it corresponds to an executable or dynamic object.  */
8350
0
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
8351
0
    return true;
8352
8353
0
  for (sec = abfd->sections; sec != NULL; sec = sec->next)
8354
0
    {
8355
0
      unsigned int i, span, first_fmac = 0, veneer_of_insn = 0;
8356
0
      struct _arm_elf_section_data *sec_data;
8357
8358
      /* If we don't have executable progbits, we're not interested in this
8359
   section.  Also skip if section is to be excluded.  */
8360
0
      if (elf_section_type (sec) != SHT_PROGBITS
8361
0
    || (elf_section_flags (sec) & SHF_EXECINSTR) == 0
8362
0
    || (sec->flags & SEC_EXCLUDE) != 0
8363
0
    || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8364
0
    || sec->output_section == bfd_abs_section_ptr
8365
0
    || strcmp (sec->name, VFP11_ERRATUM_VENEER_SECTION_NAME) == 0)
8366
0
  continue;
8367
8368
0
      sec_data = elf32_arm_section_data (sec);
8369
8370
0
      if (sec_data->mapcount == 0)
8371
0
  continue;
8372
8373
0
      if (elf_section_data (sec)->this_hdr.contents != NULL)
8374
0
  contents = elf_section_data (sec)->this_hdr.contents;
8375
0
      else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
8376
0
  goto error_return;
8377
8378
0
      qsort (sec_data->map, sec_data->mapcount, sizeof (elf32_arm_section_map),
8379
0
       elf32_arm_compare_mapping);
8380
8381
0
      for (span = 0; span < sec_data->mapcount; span++)
8382
0
  {
8383
0
    unsigned int span_start = sec_data->map[span].vma;
8384
0
    unsigned int span_end = (span == sec_data->mapcount - 1)
8385
0
          ? sec->size : sec_data->map[span + 1].vma;
8386
0
    char span_type = sec_data->map[span].type;
8387
8388
    /* FIXME: Only ARM mode is supported at present.  We may need to
8389
       support Thumb-2 mode also at some point.  */
8390
0
    if (span_type != 'a')
8391
0
      continue;
8392
8393
0
    for (i = span_start; i < span_end;)
8394
0
      {
8395
0
        unsigned int next_i = i + 4;
8396
0
        unsigned int insn = bfd_big_endian (abfd)
8397
0
    ? (((unsigned) contents[i] << 24)
8398
0
       | (contents[i + 1] << 16)
8399
0
       | (contents[i + 2] << 8)
8400
0
       | contents[i + 3])
8401
0
    : (((unsigned) contents[i + 3] << 24)
8402
0
       | (contents[i + 2] << 16)
8403
0
       | (contents[i + 1] << 8)
8404
0
       | contents[i]);
8405
0
        unsigned int writemask = 0;
8406
0
        enum bfd_arm_vfp11_pipe vpipe;
8407
8408
0
        switch (state)
8409
0
    {
8410
0
    case 0:
8411
0
      vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask, regs,
8412
0
                &numregs);
8413
      /* I'm assuming the VFP11 erratum can trigger with denorm
8414
         operands on either the FMAC or the DS pipeline. This might
8415
         lead to slightly overenthusiastic veneer insertion.  */
8416
0
      if (vpipe == VFP11_FMAC || vpipe == VFP11_DS)
8417
0
        {
8418
0
          state = use_vector ? 1 : 2;
8419
0
          first_fmac = i;
8420
0
          veneer_of_insn = insn;
8421
0
        }
8422
0
      break;
8423
8424
0
    case 1:
8425
0
      {
8426
0
        int other_regs[3], other_numregs;
8427
0
        vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask,
8428
0
                  other_regs,
8429
0
                  &other_numregs);
8430
0
        if (vpipe != VFP11_BAD
8431
0
      && bfd_arm_vfp11_antidependency (writemask, regs,
8432
0
               numregs))
8433
0
          state = 3;
8434
0
        else
8435
0
          state = 2;
8436
0
      }
8437
0
      break;
8438
8439
0
    case 2:
8440
0
      {
8441
0
        int other_regs[3], other_numregs;
8442
0
        vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask,
8443
0
                  other_regs,
8444
0
                  &other_numregs);
8445
0
        if (vpipe != VFP11_BAD
8446
0
      && bfd_arm_vfp11_antidependency (writemask, regs,
8447
0
               numregs))
8448
0
          state = 3;
8449
0
        else
8450
0
          {
8451
0
      state = 0;
8452
0
      next_i = first_fmac + 4;
8453
0
          }
8454
0
      }
8455
0
      break;
8456
8457
0
    case 3:
8458
0
      abort ();  /* Should be unreachable.  */
8459
0
    }
8460
8461
0
        if (state == 3)
8462
0
    {
8463
0
      elf32_vfp11_erratum_list *newerr =(elf32_vfp11_erratum_list *)
8464
0
          bfd_zmalloc (sizeof (elf32_vfp11_erratum_list));
8465
8466
0
      elf32_arm_section_data (sec)->erratumcount += 1;
8467
8468
0
      newerr->u.b.vfp_insn = veneer_of_insn;
8469
8470
0
      switch (span_type)
8471
0
        {
8472
0
        case 'a':
8473
0
          newerr->type = VFP11_ERRATUM_BRANCH_TO_ARM_VENEER;
8474
0
          break;
8475
8476
0
        default:
8477
0
          abort ();
8478
0
        }
8479
8480
0
      record_vfp11_erratum_veneer (link_info, newerr, abfd, sec,
8481
0
                 first_fmac);
8482
8483
0
      newerr->vma = -1;
8484
8485
0
      newerr->next = sec_data->erratumlist;
8486
0
      sec_data->erratumlist = newerr;
8487
8488
0
      state = 0;
8489
0
    }
8490
8491
0
        i = next_i;
8492
0
      }
8493
0
  }
8494
8495
0
      if (elf_section_data (sec)->this_hdr.contents != contents)
8496
0
  free (contents);
8497
0
      contents = NULL;
8498
0
    }
8499
8500
0
  return true;
8501
8502
0
 error_return:
8503
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
8504
0
    free (contents);
8505
8506
0
  return false;
8507
0
}
8508
8509
/* Find virtual-memory addresses for VFP11 erratum veneers and return locations
8510
   after sections have been laid out, using specially-named symbols.  */
8511
8512
void
8513
bfd_elf32_arm_vfp11_fix_veneer_locations (bfd *abfd,
8514
            struct bfd_link_info *link_info)
8515
0
{
8516
0
  asection *sec;
8517
0
  struct elf32_arm_link_hash_table *globals;
8518
0
  char *tmp_name;
8519
8520
0
  if (bfd_link_relocatable (link_info))
8521
0
    return;
8522
8523
  /* Skip if this bfd does not correspond to an ELF image.  */
8524
0
  if (! is_arm_elf (abfd))
8525
0
    return;
8526
8527
0
  globals = elf32_arm_hash_table (link_info);
8528
0
  if (globals == NULL)
8529
0
    return;
8530
8531
0
  tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
8532
0
          (VFP11_ERRATUM_VENEER_ENTRY_NAME) + 10);
8533
0
  BFD_ASSERT (tmp_name);
8534
8535
0
  for (sec = abfd->sections; sec != NULL; sec = sec->next)
8536
0
    {
8537
0
      struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
8538
0
      elf32_vfp11_erratum_list *errnode = sec_data->erratumlist;
8539
8540
0
      for (; errnode != NULL; errnode = errnode->next)
8541
0
  {
8542
0
    struct elf_link_hash_entry *myh;
8543
0
    bfd_vma vma;
8544
8545
0
    switch (errnode->type)
8546
0
      {
8547
0
      case VFP11_ERRATUM_BRANCH_TO_ARM_VENEER:
8548
0
      case VFP11_ERRATUM_BRANCH_TO_THUMB_VENEER:
8549
        /* Find veneer symbol.  */
8550
0
        sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME,
8551
0
           errnode->u.b.veneer->u.v.id);
8552
8553
0
        myh = elf_link_hash_lookup
8554
0
    (&(globals)->root, tmp_name, false, false, true);
8555
8556
0
        if (myh == NULL)
8557
0
    _bfd_error_handler (_("%pB: unable to find %s veneer `%s'"),
8558
0
            abfd, "VFP11", tmp_name);
8559
8560
0
        vma = myh->root.u.def.section->output_section->vma
8561
0
        + myh->root.u.def.section->output_offset
8562
0
        + myh->root.u.def.value;
8563
8564
0
        errnode->u.b.veneer->vma = vma;
8565
0
        break;
8566
8567
0
      case VFP11_ERRATUM_ARM_VENEER:
8568
0
      case VFP11_ERRATUM_THUMB_VENEER:
8569
        /* Find return location.  */
8570
0
        sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME "_r",
8571
0
           errnode->u.v.id);
8572
8573
0
        myh = elf_link_hash_lookup
8574
0
    (&(globals)->root, tmp_name, false, false, true);
8575
8576
0
        if (myh == NULL)
8577
0
    _bfd_error_handler (_("%pB: unable to find %s veneer `%s'"),
8578
0
            abfd, "VFP11", tmp_name);
8579
8580
0
        vma = myh->root.u.def.section->output_section->vma
8581
0
        + myh->root.u.def.section->output_offset
8582
0
        + myh->root.u.def.value;
8583
8584
0
        errnode->u.v.branch->vma = vma;
8585
0
        break;
8586
8587
0
      default:
8588
0
        abort ();
8589
0
      }
8590
0
  }
8591
0
    }
8592
8593
0
  free (tmp_name);
8594
0
}
8595
8596
/* Find virtual-memory addresses for STM32L4XX erratum veneers and
8597
   return locations after sections have been laid out, using
8598
   specially-named symbols.  */
8599
8600
void
8601
bfd_elf32_arm_stm32l4xx_fix_veneer_locations (bfd *abfd,
8602
                struct bfd_link_info *link_info)
8603
0
{
8604
0
  asection *sec;
8605
0
  struct elf32_arm_link_hash_table *globals;
8606
0
  char *tmp_name;
8607
8608
0
  if (bfd_link_relocatable (link_info))
8609
0
    return;
8610
8611
  /* Skip if this bfd does not correspond to an ELF image.  */
8612
0
  if (! is_arm_elf (abfd))
8613
0
    return;
8614
8615
0
  globals = elf32_arm_hash_table (link_info);
8616
0
  if (globals == NULL)
8617
0
    return;
8618
8619
0
  tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
8620
0
          (STM32L4XX_ERRATUM_VENEER_ENTRY_NAME) + 10);
8621
0
  BFD_ASSERT (tmp_name);
8622
8623
0
  for (sec = abfd->sections; sec != NULL; sec = sec->next)
8624
0
    {
8625
0
      struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
8626
0
      elf32_stm32l4xx_erratum_list *errnode = sec_data->stm32l4xx_erratumlist;
8627
8628
0
      for (; errnode != NULL; errnode = errnode->next)
8629
0
  {
8630
0
    struct elf_link_hash_entry *myh;
8631
0
    bfd_vma vma;
8632
8633
0
    switch (errnode->type)
8634
0
      {
8635
0
      case STM32L4XX_ERRATUM_BRANCH_TO_VENEER:
8636
        /* Find veneer symbol.  */
8637
0
        sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME,
8638
0
           errnode->u.b.veneer->u.v.id);
8639
8640
0
        myh = elf_link_hash_lookup
8641
0
    (&(globals)->root, tmp_name, false, false, true);
8642
8643
0
        if (myh == NULL)
8644
0
    _bfd_error_handler (_("%pB: unable to find %s veneer `%s'"),
8645
0
            abfd, "STM32L4XX", tmp_name);
8646
8647
0
        vma = myh->root.u.def.section->output_section->vma
8648
0
    + myh->root.u.def.section->output_offset
8649
0
    + myh->root.u.def.value;
8650
8651
0
        errnode->u.b.veneer->vma = vma;
8652
0
        break;
8653
8654
0
      case STM32L4XX_ERRATUM_VENEER:
8655
        /* Find return location.  */
8656
0
        sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME "_r",
8657
0
           errnode->u.v.id);
8658
8659
0
        myh = elf_link_hash_lookup
8660
0
    (&(globals)->root, tmp_name, false, false, true);
8661
8662
0
        if (myh == NULL)
8663
0
    _bfd_error_handler (_("%pB: unable to find %s veneer `%s'"),
8664
0
            abfd, "STM32L4XX", tmp_name);
8665
8666
0
        vma = myh->root.u.def.section->output_section->vma
8667
0
    + myh->root.u.def.section->output_offset
8668
0
    + myh->root.u.def.value;
8669
8670
0
        errnode->u.v.branch->vma = vma;
8671
0
        break;
8672
8673
0
      default:
8674
0
        abort ();
8675
0
      }
8676
0
  }
8677
0
    }
8678
8679
0
  free (tmp_name);
8680
0
}
8681
8682
static inline bool
8683
is_thumb2_ldmia (const insn32 insn)
8684
0
{
8685
  /* Encoding T2: LDM<c>.W <Rn>{!},<registers>
8686
     1110 - 1000 - 10W1 - rrrr - PM (0) l - llll - llll - llll.  */
8687
0
  return (insn & 0xffd02000) == 0xe8900000;
8688
0
}
8689
8690
static inline bool
8691
is_thumb2_ldmdb (const insn32 insn)
8692
0
{
8693
  /* Encoding T1: LDMDB<c> <Rn>{!},<registers>
8694
     1110 - 1001 - 00W1 - rrrr - PM (0) l - llll - llll - llll.  */
8695
0
  return (insn & 0xffd02000) == 0xe9100000;
8696
0
}
8697
8698
static inline bool
8699
is_thumb2_vldm (const insn32 insn)
8700
0
{
8701
  /* A6.5 Extension register load or store instruction
8702
     A7.7.229
8703
     We look for SP 32-bit and DP 64-bit registers.
8704
     Encoding T1 VLDM{mode}<c> <Rn>{!}, <list>
8705
     <list> is consecutive 64-bit registers
8706
     1110 - 110P - UDW1 - rrrr - vvvv - 1011 - iiii - iiii
8707
     Encoding T2 VLDM{mode}<c> <Rn>{!}, <list>
8708
     <list> is consecutive 32-bit registers
8709
     1110 - 110P - UDW1 - rrrr - vvvv - 1010 - iiii - iiii
8710
     if P==0 && U==1 && W==1 && Rn=1101 VPOP
8711
     if PUW=010 || PUW=011 || PUW=101 VLDM.  */
8712
0
  return
8713
0
    (((insn & 0xfe100f00) == 0xec100b00) ||
8714
0
     ((insn & 0xfe100f00) == 0xec100a00))
8715
0
    && /* (IA without !).  */
8716
0
    (((((insn << 7) >> 28) & 0xd) == 0x4)
8717
     /* (IA with !), includes VPOP (when reg number is SP).  */
8718
0
     || ((((insn << 7) >> 28) & 0xd) == 0x5)
8719
     /* (DB with !).  */
8720
0
     || ((((insn << 7) >> 28) & 0xd) == 0x9));
8721
0
}
8722
8723
/* STM STM32L4XX erratum : This function assumes that it receives an LDM or
8724
   VLDM opcode and:
8725
 - computes the number and the mode of memory accesses
8726
 - decides if the replacement should be done:
8727
   . replaces only if > 8-word accesses
8728
   . or (testing purposes only) replaces all accesses.  */
8729
8730
static bool
8731
stm32l4xx_need_create_replacing_stub (const insn32 insn,
8732
              bfd_arm_stm32l4xx_fix stm32l4xx_fix)
8733
0
{
8734
0
  int nb_words = 0;
8735
8736
  /* The field encoding the register list is the same for both LDMIA
8737
     and LDMDB encodings.  */
8738
0
  if (is_thumb2_ldmia (insn) || is_thumb2_ldmdb (insn))
8739
0
    nb_words = elf32_arm_popcount (insn & 0x0000ffff);
8740
0
  else if (is_thumb2_vldm (insn))
8741
0
   nb_words = (insn & 0xff);
8742
8743
  /* DEFAULT mode accounts for the real bug condition situation,
8744
     ALL mode inserts stubs for each LDM/VLDM instruction (testing).  */
8745
0
  return (stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_DEFAULT
8746
0
    ? nb_words > 8
8747
0
    : stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_ALL);
8748
0
}
8749
8750
/* Look for potentially-troublesome code sequences which might trigger
8751
   the STM STM32L4XX erratum.  */
8752
8753
bool
8754
bfd_elf32_arm_stm32l4xx_erratum_scan (bfd *abfd,
8755
              struct bfd_link_info *link_info)
8756
0
{
8757
0
  asection *sec;
8758
0
  bfd_byte *contents = NULL;
8759
0
  struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
8760
8761
0
  if (globals == NULL)
8762
0
    return false;
8763
8764
  /* If we are only performing a partial link do not bother
8765
     to construct any glue.  */
8766
0
  if (bfd_link_relocatable (link_info))
8767
0
    return true;
8768
8769
  /* Skip if this bfd does not correspond to an ELF image.  */
8770
0
  if (! is_arm_elf (abfd))
8771
0
    return true;
8772
8773
0
  if (globals->stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_NONE)
8774
0
    return true;
8775
8776
  /* Skip this BFD if it corresponds to an executable or dynamic object.  */
8777
0
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
8778
0
    return true;
8779
8780
0
  for (sec = abfd->sections; sec != NULL; sec = sec->next)
8781
0
    {
8782
0
      unsigned int i, span;
8783
0
      struct _arm_elf_section_data *sec_data;
8784
8785
      /* If we don't have executable progbits, we're not interested in this
8786
   section.  Also skip if section is to be excluded.  */
8787
0
      if (elf_section_type (sec) != SHT_PROGBITS
8788
0
    || (elf_section_flags (sec) & SHF_EXECINSTR) == 0
8789
0
    || (sec->flags & SEC_EXCLUDE) != 0
8790
0
    || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8791
0
    || sec->output_section == bfd_abs_section_ptr
8792
0
    || strcmp (sec->name, STM32L4XX_ERRATUM_VENEER_SECTION_NAME) == 0)
8793
0
  continue;
8794
8795
0
      sec_data = elf32_arm_section_data (sec);
8796
8797
0
      if (sec_data->mapcount == 0)
8798
0
  continue;
8799
8800
0
      if (elf_section_data (sec)->this_hdr.contents != NULL)
8801
0
  contents = elf_section_data (sec)->this_hdr.contents;
8802
0
      else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
8803
0
  goto error_return;
8804
8805
0
      qsort (sec_data->map, sec_data->mapcount, sizeof (elf32_arm_section_map),
8806
0
       elf32_arm_compare_mapping);
8807
8808
0
      for (span = 0; span < sec_data->mapcount; span++)
8809
0
  {
8810
0
    unsigned int span_start = sec_data->map[span].vma;
8811
0
    unsigned int span_end = (span == sec_data->mapcount - 1)
8812
0
      ? sec->size : sec_data->map[span + 1].vma;
8813
0
    char span_type = sec_data->map[span].type;
8814
0
    int itblock_current_pos = 0;
8815
8816
    /* Only Thumb2 mode need be supported with this CM4 specific
8817
       code, we should not encounter any arm mode eg span_type
8818
       != 'a'.  */
8819
0
    if (span_type != 't')
8820
0
      continue;
8821
8822
0
    for (i = span_start; i < span_end;)
8823
0
      {
8824
0
        unsigned int insn = bfd_get_16 (abfd, &contents[i]);
8825
0
        bool insn_32bit = false;
8826
0
        bool is_ldm = false;
8827
0
        bool is_vldm = false;
8828
0
        bool is_not_last_in_it_block = false;
8829
8830
        /* The first 16-bits of all 32-bit thumb2 instructions start
8831
     with opcode[15..13]=0b111 and the encoded op1 can be anything
8832
     except opcode[12..11]!=0b00.
8833
     See 32-bit Thumb instruction encoding.  */
8834
0
        if ((insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000)
8835
0
    insn_32bit = true;
8836
8837
        /* Compute the predicate that tells if the instruction
8838
     is concerned by the IT block
8839
     - Creates an error if there is a ldm that is not
8840
       last in the IT block thus cannot be replaced
8841
     - Otherwise we can create a branch at the end of the
8842
       IT block, it will be controlled naturally by IT
8843
       with the proper pseudo-predicate
8844
     - So the only interesting predicate is the one that
8845
       tells that we are not on the last item of an IT
8846
       block.  */
8847
0
        if (itblock_current_pos != 0)
8848
0
      is_not_last_in_it_block = !!--itblock_current_pos;
8849
8850
0
        if (insn_32bit)
8851
0
    {
8852
      /* Load the rest of the insn (in manual-friendly order).  */
8853
0
      insn = (insn << 16) | bfd_get_16 (abfd, &contents[i + 2]);
8854
0
      is_ldm = is_thumb2_ldmia (insn) || is_thumb2_ldmdb (insn);
8855
0
      is_vldm = is_thumb2_vldm (insn);
8856
8857
      /* Veneers are created for (v)ldm depending on
8858
         option flags and memory accesses conditions; but
8859
         if the instruction is not the last instruction of
8860
         an IT block, we cannot create a jump there, so we
8861
         bail out.  */
8862
0
        if ((is_ldm || is_vldm)
8863
0
      && stm32l4xx_need_create_replacing_stub
8864
0
      (insn, globals->stm32l4xx_fix))
8865
0
          {
8866
0
      if (is_not_last_in_it_block)
8867
0
        {
8868
0
          _bfd_error_handler
8869
            /* xgettext:c-format */
8870
0
            (_("%pB(%pA+%#x): error: multiple load detected"
8871
0
         " in non-last IT block instruction:"
8872
0
         " STM32L4XX veneer cannot be generated; "
8873
0
         "use gcc option -mrestrict-it to generate"
8874
0
         " only one instruction per IT block"),
8875
0
             abfd, sec, i);
8876
0
        }
8877
0
      else
8878
0
        {
8879
0
          elf32_stm32l4xx_erratum_list *newerr =
8880
0
            (elf32_stm32l4xx_erratum_list *)
8881
0
            bfd_zmalloc
8882
0
            (sizeof (elf32_stm32l4xx_erratum_list));
8883
8884
0
          elf32_arm_section_data (sec)
8885
0
            ->stm32l4xx_erratumcount += 1;
8886
0
          newerr->u.b.insn = insn;
8887
          /* We create only thumb branches.  */
8888
0
          newerr->type =
8889
0
            STM32L4XX_ERRATUM_BRANCH_TO_VENEER;
8890
0
          record_stm32l4xx_erratum_veneer
8891
0
            (link_info, newerr, abfd, sec,
8892
0
             i,
8893
0
             is_ldm ?
8894
0
             STM32L4XX_ERRATUM_LDM_VENEER_SIZE:
8895
0
             STM32L4XX_ERRATUM_VLDM_VENEER_SIZE);
8896
0
          newerr->vma = -1;
8897
0
          newerr->next = sec_data->stm32l4xx_erratumlist;
8898
0
          sec_data->stm32l4xx_erratumlist = newerr;
8899
0
        }
8900
0
          }
8901
0
    }
8902
0
        else
8903
0
    {
8904
      /* A7.7.37 IT p208
8905
         IT blocks are only encoded in T1
8906
         Encoding T1: IT{x{y{z}}} <firstcond>
8907
         1 0 1 1 - 1 1 1 1 - firstcond - mask
8908
         if mask = '0000' then see 'related encodings'
8909
         We don't deal with UNPREDICTABLE, just ignore these.
8910
         There can be no nested IT blocks so an IT block
8911
         is naturally a new one for which it is worth
8912
         computing its size.  */
8913
0
      bool is_newitblock = ((insn & 0xff00) == 0xbf00)
8914
0
        && ((insn & 0x000f) != 0x0000);
8915
      /* If we have a new IT block we compute its size.  */
8916
0
      if (is_newitblock)
8917
0
        {
8918
          /* Compute the number of instructions controlled
8919
       by the IT block, it will be used to decide
8920
       whether we are inside an IT block or not.  */
8921
0
          unsigned int mask = insn & 0x000f;
8922
0
          itblock_current_pos = 4 - ctz (mask);
8923
0
        }
8924
0
    }
8925
8926
0
        i += insn_32bit ? 4 : 2;
8927
0
      }
8928
0
  }
8929
8930
0
      if (elf_section_data (sec)->this_hdr.contents != contents)
8931
0
  free (contents);
8932
0
      contents = NULL;
8933
0
    }
8934
8935
0
  return true;
8936
8937
0
 error_return:
8938
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
8939
0
    free (contents);
8940
8941
0
  return false;
8942
0
}
8943
8944
/* Set target relocation values needed during linking.  */
8945
8946
void
8947
bfd_elf32_arm_set_target_params (struct bfd_link_info *link_info,
8948
         const struct elf32_arm_params *params,
8949
         struct bfd *stub_bfd)
8950
0
{
8951
0
  struct elf32_arm_link_hash_table *globals;
8952
8953
0
  globals = elf32_arm_hash_table (link_info);
8954
0
  if (globals == NULL)
8955
0
    return;
8956
8957
0
  globals->target1_is_rel = params->target1_is_rel;
8958
0
  if (globals->fdpic_p)
8959
0
    globals->target2_reloc = R_ARM_GOT32;
8960
0
  else if (strcmp (params->target2_type, "rel") == 0)
8961
0
    globals->target2_reloc = R_ARM_REL32;
8962
0
  else if (strcmp (params->target2_type, "abs") == 0)
8963
0
    globals->target2_reloc = R_ARM_ABS32;
8964
0
  else if (strcmp (params->target2_type, "got-rel") == 0)
8965
0
    globals->target2_reloc = R_ARM_GOT_PREL;
8966
0
  else
8967
0
    {
8968
0
      _bfd_error_handler (_("invalid TARGET2 relocation type '%s'"),
8969
0
        params->target2_type);
8970
0
    }
8971
0
  globals->fix_v4bx = params->fix_v4bx;
8972
0
  globals->use_blx |= params->use_blx;
8973
0
  globals->vfp11_fix = params->vfp11_denorm_fix;
8974
0
  globals->stm32l4xx_fix = params->stm32l4xx_fix;
8975
0
  if (globals->fdpic_p)
8976
0
    globals->pic_veneer = 1;
8977
0
  else
8978
0
    globals->pic_veneer = params->pic_veneer;
8979
0
  globals->fix_cortex_a8 = params->fix_cortex_a8;
8980
0
  globals->fix_arm1176 = params->fix_arm1176;
8981
0
  globals->cmse_implib = params->cmse_implib;
8982
0
  globals->in_implib_bfd = params->in_implib_bfd;
8983
8984
0
  BFD_ASSERT (is_arm_elf (link_info->output_bfd));
8985
0
  elf_arm_tdata (link_info->output_bfd)->no_enum_size_warning
8986
0
    = params->no_enum_size_warning;
8987
0
  elf_arm_tdata (link_info->output_bfd)->no_wchar_size_warning
8988
0
    = params->no_wchar_size_warning;
8989
8990
0
  globals->stub_bfd = stub_bfd;
8991
0
  stub_bfd->flags |= BFD_LINKER_CREATED;
8992
0
  elf_elfheader (stub_bfd)->e_ident[EI_CLASS] = ELFCLASS32;
8993
8994
  /* Also use the stub file for stubs placed in a single output section.  */
8995
0
  bfd_elf32_arm_add_glue_sections_to_bfd (stub_bfd, link_info);
8996
0
  bfd_elf32_arm_get_bfd_for_interworking (stub_bfd, link_info);
8997
0
}
8998
8999
/* Replace the target offset of a Thumb bl or b.w instruction.  */
9000
9001
static void
9002
insert_thumb_branch (bfd *abfd, long int offset, bfd_byte *insn)
9003
0
{
9004
0
  bfd_vma upper;
9005
0
  bfd_vma lower;
9006
0
  int reloc_sign;
9007
9008
0
  BFD_ASSERT ((offset & 1) == 0);
9009
9010
0
  upper = bfd_get_16 (abfd, insn);
9011
0
  lower = bfd_get_16 (abfd, insn + 2);
9012
0
  reloc_sign = (offset < 0) ? 1 : 0;
9013
0
  upper = (upper & ~(bfd_vma) 0x7ff)
9014
0
    | ((offset >> 12) & 0x3ff)
9015
0
    | (reloc_sign << 10);
9016
0
  lower = (lower & ~(bfd_vma) 0x2fff)
9017
0
    | (((!((offset >> 23) & 1)) ^ reloc_sign) << 13)
9018
0
    | (((!((offset >> 22) & 1)) ^ reloc_sign) << 11)
9019
0
    | ((offset >> 1) & 0x7ff);
9020
0
  bfd_put_16 (abfd, upper, insn);
9021
0
  bfd_put_16 (abfd, lower, insn + 2);
9022
0
}
9023
9024
/* Thumb code calling an ARM function.  */
9025
9026
static int
9027
elf32_thumb_to_arm_stub (struct bfd_link_info * info,
9028
       const char *   name,
9029
       bfd *      input_bfd,
9030
       bfd *      output_bfd,
9031
       asection *   input_section,
9032
       bfd_byte *   hit_data,
9033
       asection *   sym_sec,
9034
       bfd_vma    offset,
9035
       bfd_signed_vma   addend,
9036
       bfd_vma    val,
9037
       char **error_message)
9038
0
{
9039
0
  asection * s = 0;
9040
0
  bfd_vma my_offset;
9041
0
  long int ret_offset;
9042
0
  struct elf_link_hash_entry * myh;
9043
0
  struct elf32_arm_link_hash_table * globals;
9044
9045
0
  myh = find_thumb_glue (info, name, error_message);
9046
0
  if (myh == NULL)
9047
0
    return false;
9048
9049
0
  globals = elf32_arm_hash_table (info);
9050
0
  BFD_ASSERT (globals != NULL);
9051
0
  BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
9052
9053
0
  my_offset = myh->root.u.def.value;
9054
9055
0
  s = bfd_get_linker_section (globals->bfd_of_glue_owner,
9056
0
            THUMB2ARM_GLUE_SECTION_NAME);
9057
9058
0
  BFD_ASSERT (s != NULL);
9059
0
  BFD_ASSERT (s->contents != NULL);
9060
0
  BFD_ASSERT (s->output_section != NULL);
9061
9062
0
  if ((my_offset & 0x01) == 0x01)
9063
0
    {
9064
0
      if (sym_sec != NULL
9065
0
    && sym_sec->owner != NULL
9066
0
    && !INTERWORK_FLAG (sym_sec->owner))
9067
0
  {
9068
0
    _bfd_error_handler
9069
0
      (_("%pB(%s): warning: interworking not enabled;"
9070
0
         " first occurrence: %pB: %s call to %s"),
9071
0
       sym_sec->owner, name, input_bfd, "Thumb", "ARM");
9072
9073
0
    return false;
9074
0
  }
9075
9076
0
      --my_offset;
9077
0
      myh->root.u.def.value = my_offset;
9078
9079
0
      put_thumb_insn (globals, output_bfd, (bfd_vma) t2a1_bx_pc_insn,
9080
0
          s->contents + my_offset);
9081
9082
0
      put_thumb_insn (globals, output_bfd, (bfd_vma) t2a2_noop_insn,
9083
0
          s->contents + my_offset + 2);
9084
9085
0
      ret_offset =
9086
  /* Address of destination of the stub.  */
9087
0
  ((bfd_signed_vma) val)
9088
0
  - ((bfd_signed_vma)
9089
     /* Offset from the start of the current section
9090
        to the start of the stubs.  */
9091
0
     (s->output_offset
9092
      /* Offset of the start of this stub from the start of the stubs.  */
9093
0
      + my_offset
9094
      /* Address of the start of the current section.  */
9095
0
      + s->output_section->vma)
9096
     /* The branch instruction is 4 bytes into the stub.  */
9097
0
     + 4
9098
     /* ARM branches work from the pc of the instruction + 8.  */
9099
0
     + 8);
9100
9101
0
      put_arm_insn (globals, output_bfd,
9102
0
        (bfd_vma) t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF),
9103
0
        s->contents + my_offset + 4);
9104
0
    }
9105
9106
0
  BFD_ASSERT (my_offset <= globals->thumb_glue_size);
9107
9108
  /* Now go back and fix up the original BL insn to point to here.  */
9109
0
  ret_offset =
9110
    /* Address of where the stub is located.  */
9111
0
    (s->output_section->vma + s->output_offset + my_offset)
9112
     /* Address of where the BL is located.  */
9113
0
    - (input_section->output_section->vma + input_section->output_offset
9114
0
       + offset)
9115
    /* Addend in the relocation.  */
9116
0
    - addend
9117
    /* Biassing for PC-relative addressing.  */
9118
0
    - 8;
9119
9120
0
  insert_thumb_branch (input_bfd, ret_offset, hit_data - input_section->vma);
9121
9122
0
  return true;
9123
0
}
9124
9125
/* Populate an Arm to Thumb stub.  Returns the stub symbol.  */
9126
9127
static struct elf_link_hash_entry *
9128
elf32_arm_create_thumb_stub (struct bfd_link_info * info,
9129
           const char *     name,
9130
           bfd *        input_bfd,
9131
           bfd *        output_bfd,
9132
           asection *       sym_sec,
9133
           bfd_vma        val,
9134
           asection *       s,
9135
           char **        error_message)
9136
0
{
9137
0
  bfd_vma my_offset;
9138
0
  long int ret_offset;
9139
0
  struct elf_link_hash_entry * myh;
9140
0
  struct elf32_arm_link_hash_table * globals;
9141
9142
0
  myh = find_arm_glue (info, name, error_message);
9143
0
  if (myh == NULL)
9144
0
    return NULL;
9145
9146
0
  globals = elf32_arm_hash_table (info);
9147
0
  BFD_ASSERT (globals != NULL);
9148
0
  BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
9149
9150
0
  my_offset = myh->root.u.def.value;
9151
9152
0
  if ((my_offset & 0x01) == 0x01)
9153
0
    {
9154
0
      if (sym_sec != NULL
9155
0
    && sym_sec->owner != NULL
9156
0
    && !INTERWORK_FLAG (sym_sec->owner))
9157
0
  {
9158
0
    _bfd_error_handler
9159
0
      (_("%pB(%s): warning: interworking not enabled;"
9160
0
         " first occurrence: %pB: %s call to %s"),
9161
0
       sym_sec->owner, name, input_bfd, "ARM", "Thumb");
9162
0
  }
9163
9164
0
      --my_offset;
9165
0
      myh->root.u.def.value = my_offset;
9166
9167
0
      if (bfd_link_pic (info)
9168
0
    || globals->pic_veneer)
9169
0
  {
9170
    /* For relocatable objects we can't use absolute addresses,
9171
       so construct the address from a relative offset.  */
9172
    /* TODO: If the offset is small it's probably worth
9173
       constructing the address with adds.  */
9174
0
    put_arm_insn (globals, output_bfd, (bfd_vma) a2t1p_ldr_insn,
9175
0
      s->contents + my_offset);
9176
0
    put_arm_insn (globals, output_bfd, (bfd_vma) a2t2p_add_pc_insn,
9177
0
      s->contents + my_offset + 4);
9178
0
    put_arm_insn (globals, output_bfd, (bfd_vma) a2t3p_bx_r12_insn,
9179
0
      s->contents + my_offset + 8);
9180
    /* Adjust the offset by 4 for the position of the add,
9181
       and 8 for the pipeline offset.  */
9182
0
    ret_offset = (val - (s->output_offset
9183
0
             + s->output_section->vma
9184
0
             + my_offset + 12))
9185
0
           | 1;
9186
0
    bfd_put_32 (output_bfd, ret_offset,
9187
0
          s->contents + my_offset + 12);
9188
0
  }
9189
0
      else if (globals->use_blx)
9190
0
  {
9191
0
    put_arm_insn (globals, output_bfd, (bfd_vma) a2t1v5_ldr_insn,
9192
0
      s->contents + my_offset);
9193
9194
    /* It's a thumb address.  Add the low order bit.  */
9195
0
    bfd_put_32 (output_bfd, val | a2t2v5_func_addr_insn,
9196
0
          s->contents + my_offset + 4);
9197
0
  }
9198
0
      else
9199
0
  {
9200
0
    put_arm_insn (globals, output_bfd, (bfd_vma) a2t1_ldr_insn,
9201
0
      s->contents + my_offset);
9202
9203
0
    put_arm_insn (globals, output_bfd, (bfd_vma) a2t2_bx_r12_insn,
9204
0
      s->contents + my_offset + 4);
9205
9206
    /* It's a thumb address.  Add the low order bit.  */
9207
0
    bfd_put_32 (output_bfd, val | a2t3_func_addr_insn,
9208
0
          s->contents + my_offset + 8);
9209
9210
0
    my_offset += 12;
9211
0
  }
9212
0
    }
9213
9214
0
  BFD_ASSERT (my_offset <= globals->arm_glue_size);
9215
9216
0
  return myh;
9217
0
}
9218
9219
/* Arm code calling a Thumb function.  */
9220
9221
static int
9222
elf32_arm_to_thumb_stub (struct bfd_link_info * info,
9223
       const char *   name,
9224
       bfd *      input_bfd,
9225
       bfd *      output_bfd,
9226
       asection *   input_section,
9227
       bfd_byte *   hit_data,
9228
       asection *   sym_sec,
9229
       bfd_vma    offset,
9230
       bfd_signed_vma   addend,
9231
       bfd_vma    val,
9232
       char **error_message)
9233
0
{
9234
0
  unsigned long int tmp;
9235
0
  bfd_vma my_offset;
9236
0
  asection * s;
9237
0
  long int ret_offset;
9238
0
  struct elf_link_hash_entry * myh;
9239
0
  struct elf32_arm_link_hash_table * globals;
9240
9241
0
  globals = elf32_arm_hash_table (info);
9242
0
  BFD_ASSERT (globals != NULL);
9243
0
  BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
9244
9245
0
  s = bfd_get_linker_section (globals->bfd_of_glue_owner,
9246
0
            ARM2THUMB_GLUE_SECTION_NAME);
9247
0
  BFD_ASSERT (s != NULL);
9248
0
  BFD_ASSERT (s->contents != NULL);
9249
0
  BFD_ASSERT (s->output_section != NULL);
9250
9251
0
  myh = elf32_arm_create_thumb_stub (info, name, input_bfd, output_bfd,
9252
0
             sym_sec, val, s, error_message);
9253
0
  if (!myh)
9254
0
    return false;
9255
9256
0
  my_offset = myh->root.u.def.value;
9257
0
  tmp = bfd_get_32 (input_bfd, hit_data);
9258
0
  tmp = tmp & 0xFF000000;
9259
9260
  /* Somehow these are both 4 too far, so subtract 8.  */
9261
0
  ret_offset = (s->output_offset
9262
0
    + my_offset
9263
0
    + s->output_section->vma
9264
0
    - (input_section->output_offset
9265
0
       + input_section->output_section->vma
9266
0
       + offset + addend)
9267
0
    - 8);
9268
9269
0
  tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF);
9270
9271
0
  bfd_put_32 (output_bfd, (bfd_vma) tmp, hit_data - input_section->vma);
9272
9273
0
  return true;
9274
0
}
9275
9276
/* Populate Arm stub for an exported Thumb function.  */
9277
9278
static bool
9279
elf32_arm_to_thumb_export_stub (struct elf_link_hash_entry *h, void * inf)
9280
0
{
9281
0
  struct bfd_link_info * info = (struct bfd_link_info *) inf;
9282
0
  asection * s;
9283
0
  struct elf_link_hash_entry * myh;
9284
0
  struct elf32_arm_link_hash_entry *eh;
9285
0
  struct elf32_arm_link_hash_table * globals;
9286
0
  asection *sec;
9287
0
  bfd_vma val;
9288
0
  char *error_message;
9289
9290
0
  eh = elf32_arm_hash_entry (h);
9291
  /* Allocate stubs for exported Thumb functions on v4t.  */
9292
0
  if (eh->export_glue == NULL)
9293
0
    return true;
9294
9295
0
  globals = elf32_arm_hash_table (info);
9296
0
  BFD_ASSERT (globals != NULL);
9297
0
  BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
9298
9299
0
  s = bfd_get_linker_section (globals->bfd_of_glue_owner,
9300
0
            ARM2THUMB_GLUE_SECTION_NAME);
9301
0
  BFD_ASSERT (s != NULL);
9302
0
  BFD_ASSERT (s->contents != NULL);
9303
0
  BFD_ASSERT (s->output_section != NULL);
9304
9305
0
  sec = eh->export_glue->root.u.def.section;
9306
9307
0
  BFD_ASSERT (sec->output_section != NULL);
9308
9309
0
  val = eh->export_glue->root.u.def.value + sec->output_offset
9310
0
  + sec->output_section->vma;
9311
9312
0
  myh = elf32_arm_create_thumb_stub (info, h->root.root.string,
9313
0
             h->root.u.def.section->owner,
9314
0
             globals->obfd, sec, val, s,
9315
0
             &error_message);
9316
0
  BFD_ASSERT (myh);
9317
0
  return true;
9318
0
}
9319
9320
/* Populate ARMv4 BX veneers.  Returns the absolute adress of the veneer.  */
9321
9322
static bfd_vma
9323
elf32_arm_bx_glue (struct bfd_link_info * info, int reg)
9324
0
{
9325
0
  bfd_byte *p;
9326
0
  bfd_vma glue_addr;
9327
0
  asection *s;
9328
0
  struct elf32_arm_link_hash_table *globals;
9329
9330
0
  globals = elf32_arm_hash_table (info);
9331
0
  BFD_ASSERT (globals != NULL);
9332
0
  BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
9333
9334
0
  s = bfd_get_linker_section (globals->bfd_of_glue_owner,
9335
0
            ARM_BX_GLUE_SECTION_NAME);
9336
0
  BFD_ASSERT (s != NULL);
9337
0
  BFD_ASSERT (s->contents != NULL);
9338
0
  BFD_ASSERT (s->output_section != NULL);
9339
9340
0
  BFD_ASSERT (globals->bx_glue_offset[reg] & 2);
9341
9342
0
  glue_addr = globals->bx_glue_offset[reg] & ~(bfd_vma)3;
9343
9344
0
  if ((globals->bx_glue_offset[reg] & 1) == 0)
9345
0
    {
9346
0
      p = s->contents + glue_addr;
9347
0
      bfd_put_32 (globals->obfd, armbx1_tst_insn + (reg << 16), p);
9348
0
      bfd_put_32 (globals->obfd, armbx2_moveq_insn + reg, p + 4);
9349
0
      bfd_put_32 (globals->obfd, armbx3_bx_insn + reg, p + 8);
9350
0
      globals->bx_glue_offset[reg] |= 1;
9351
0
    }
9352
9353
0
  return glue_addr + s->output_section->vma + s->output_offset;
9354
0
}
9355
9356
/* Generate Arm stubs for exported Thumb symbols.  */
9357
static void
9358
elf32_arm_begin_write_processing (bfd *abfd ATTRIBUTE_UNUSED,
9359
          struct bfd_link_info *link_info)
9360
37
{
9361
37
  struct elf32_arm_link_hash_table * globals;
9362
9363
37
  if (link_info == NULL)
9364
    /* Ignore this if we are not called by the ELF backend linker.  */
9365
37
    return;
9366
9367
0
  globals = elf32_arm_hash_table (link_info);
9368
0
  if (globals == NULL)
9369
0
    return;
9370
9371
  /* If blx is available then exported Thumb symbols are OK and there is
9372
     nothing to do.  */
9373
0
  if (globals->use_blx)
9374
0
    return;
9375
9376
0
  elf_link_hash_traverse (&globals->root, elf32_arm_to_thumb_export_stub,
9377
0
        link_info);
9378
0
}
9379
9380
/* Reserve space for COUNT dynamic relocations in relocation selection
9381
   SRELOC.  */
9382
9383
static void
9384
elf32_arm_allocate_dynrelocs (struct bfd_link_info *info, asection *sreloc,
9385
            bfd_size_type count)
9386
0
{
9387
0
  struct elf32_arm_link_hash_table *htab;
9388
9389
0
  htab = elf32_arm_hash_table (info);
9390
0
  BFD_ASSERT (htab->root.dynamic_sections_created);
9391
0
  if (sreloc == NULL)
9392
0
    abort ();
9393
0
  sreloc->size += RELOC_SIZE (htab) * count;
9394
0
}
9395
9396
/* Reserve space for COUNT R_ARM_IRELATIVE relocations.  If the link is
9397
   dynamic, the relocations should go in SRELOC, otherwise they should
9398
   go in the special .rel.iplt section.  */
9399
9400
static void
9401
elf32_arm_allocate_irelocs (struct bfd_link_info *info, asection *sreloc,
9402
          bfd_size_type count)
9403
0
{
9404
0
  struct elf32_arm_link_hash_table *htab;
9405
9406
0
  htab = elf32_arm_hash_table (info);
9407
0
  if (!htab->root.dynamic_sections_created)
9408
0
    htab->root.irelplt->size += RELOC_SIZE (htab) * count;
9409
0
  else
9410
0
    {
9411
0
      BFD_ASSERT (sreloc != NULL);
9412
0
      sreloc->size += RELOC_SIZE (htab) * count;
9413
0
    }
9414
0
}
9415
9416
/* Add relocation REL to the end of relocation section SRELOC.  */
9417
9418
static void
9419
elf32_arm_add_dynreloc (bfd *output_bfd, struct bfd_link_info *info,
9420
      asection *sreloc, Elf_Internal_Rela *rel)
9421
0
{
9422
0
  bfd_byte *loc;
9423
0
  struct elf32_arm_link_hash_table *htab;
9424
9425
0
  htab = elf32_arm_hash_table (info);
9426
0
  if (!htab->root.dynamic_sections_created
9427
0
      && ELF32_R_TYPE (rel->r_info) == R_ARM_IRELATIVE)
9428
0
    sreloc = htab->root.irelplt;
9429
0
  if (sreloc == NULL)
9430
0
    abort ();
9431
0
  loc = sreloc->contents;
9432
0
  loc += sreloc->reloc_count++ * RELOC_SIZE (htab);
9433
0
  if (sreloc->reloc_count * RELOC_SIZE (htab) > sreloc->size)
9434
0
    abort ();
9435
0
  SWAP_RELOC_OUT (htab) (output_bfd, rel, loc);
9436
0
}
9437
9438
/* Allocate room for a PLT entry described by ROOT_PLT and ARM_PLT.
9439
   IS_IPLT_ENTRY says whether the entry belongs to .iplt rather than
9440
   to .plt.  */
9441
9442
static void
9443
elf32_arm_allocate_plt_entry (struct bfd_link_info *info,
9444
            bool is_iplt_entry,
9445
            union gotplt_union *root_plt,
9446
            struct arm_plt_info *arm_plt)
9447
0
{
9448
0
  struct elf32_arm_link_hash_table *htab;
9449
0
  asection *splt;
9450
0
  asection *sgotplt;
9451
9452
0
  htab = elf32_arm_hash_table (info);
9453
9454
0
  if (is_iplt_entry)
9455
0
    {
9456
0
      splt = htab->root.iplt;
9457
0
      sgotplt = htab->root.igotplt;
9458
9459
      /* Allocate room for an R_ARM_IRELATIVE relocation in .rel.iplt.  */
9460
0
      elf32_arm_allocate_irelocs (info, htab->root.irelplt, 1);
9461
0
    }
9462
0
  else
9463
0
    {
9464
0
      splt = htab->root.splt;
9465
0
      sgotplt = htab->root.sgotplt;
9466
9467
0
    if (htab->fdpic_p)
9468
0
      {
9469
  /* Allocate room for R_ARM_FUNCDESC_VALUE.  */
9470
  /* For lazy binding, relocations will be put into .rel.plt, in
9471
     .rel.got otherwise.  */
9472
  /* FIXME: today we don't support lazy binding so put it in .rel.got */
9473
0
  if (info->flags & DF_BIND_NOW)
9474
0
    elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
9475
0
  else
9476
0
    elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
9477
0
      }
9478
0
    else
9479
0
      {
9480
  /* Allocate room for an R_JUMP_SLOT relocation in .rel.plt.  */
9481
0
  elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
9482
0
      }
9483
9484
      /* If this is the first .plt entry, make room for the special
9485
   first entry.  */
9486
0
      if (splt->size == 0)
9487
0
  splt->size += htab->plt_header_size;
9488
9489
0
      htab->next_tls_desc_index++;
9490
0
    }
9491
9492
  /* Allocate the PLT entry itself, including any leading Thumb stub.  */
9493
0
  if (elf32_arm_plt_needs_thumb_stub_p (info, arm_plt))
9494
0
    splt->size += PLT_THUMB_STUB_SIZE;
9495
0
  root_plt->offset = splt->size;
9496
0
  splt->size += htab->plt_entry_size;
9497
9498
  /* We also need to make an entry in the .got.plt section, which
9499
     will be placed in the .got section by the linker script.  */
9500
0
  if (is_iplt_entry)
9501
0
    arm_plt->got_offset = sgotplt->size;
9502
0
  else
9503
0
    arm_plt->got_offset = sgotplt->size - 8 * htab->num_tls_desc;
9504
0
  if (htab->fdpic_p)
9505
    /* Function descriptor takes 64 bits in GOT.  */
9506
0
    sgotplt->size += 8;
9507
0
  else
9508
0
    sgotplt->size += 4;
9509
0
}
9510
9511
/* Fill in a PLT entry and its associated GOT slot.  If DYNINDX == -1,
9512
   the entry lives in .iplt and resolves to (*SYM_VALUE)().
9513
   Otherwise, DYNINDX is the index of the symbol in the dynamic
9514
   symbol table and SYM_VALUE is undefined.
9515
9516
   ROOT_PLT points to the offset of the PLT entry from the start of its
9517
   section (.iplt or .plt).  ARM_PLT points to the symbol's ARM-specific
9518
   bookkeeping information.
9519
9520
   Returns FALSE if there was a problem.  */
9521
9522
static bool
9523
elf32_arm_populate_plt_entry (bfd *output_bfd, struct bfd_link_info *info,
9524
            union gotplt_union *root_plt,
9525
            struct arm_plt_info *arm_plt,
9526
            int dynindx, bfd_vma sym_value)
9527
0
{
9528
0
  struct elf32_arm_link_hash_table *htab;
9529
0
  asection *sgot;
9530
0
  asection *splt;
9531
0
  asection *srel;
9532
0
  bfd_byte *loc;
9533
0
  bfd_vma plt_index;
9534
0
  Elf_Internal_Rela rel;
9535
0
  bfd_vma got_header_size;
9536
9537
0
  htab = elf32_arm_hash_table (info);
9538
9539
  /* Pick the appropriate sections and sizes.  */
9540
0
  if (dynindx == -1)
9541
0
    {
9542
0
      splt = htab->root.iplt;
9543
0
      sgot = htab->root.igotplt;
9544
0
      srel = htab->root.irelplt;
9545
9546
      /* There are no reserved entries in .igot.plt, and no special
9547
   first entry in .iplt.  */
9548
0
      got_header_size = 0;
9549
0
    }
9550
0
  else
9551
0
    {
9552
0
      splt = htab->root.splt;
9553
0
      sgot = htab->root.sgotplt;
9554
0
      srel = htab->root.srelplt;
9555
9556
0
      got_header_size = get_elf_backend_data (output_bfd)->got_header_size;
9557
0
    }
9558
0
  BFD_ASSERT (splt != NULL && srel != NULL);
9559
9560
0
  bfd_vma got_offset, got_address, plt_address;
9561
0
  bfd_vma got_displacement, initial_got_entry;
9562
0
  bfd_byte * ptr;
9563
9564
0
  BFD_ASSERT (sgot != NULL);
9565
9566
  /* Get the offset into the .(i)got.plt table of the entry that
9567
     corresponds to this function.  */
9568
0
  got_offset = (arm_plt->got_offset & -2);
9569
9570
  /* Get the index in the procedure linkage table which
9571
     corresponds to this symbol.  This is the index of this symbol
9572
     in all the symbols for which we are making plt entries.
9573
     After the reserved .got.plt entries, all symbols appear in
9574
     the same order as in .plt.  */
9575
0
  if (htab->fdpic_p)
9576
    /* Function descriptor takes 8 bytes.  */
9577
0
    plt_index = (got_offset - got_header_size) / 8;
9578
0
  else
9579
0
    plt_index = (got_offset - got_header_size) / 4;
9580
9581
  /* Calculate the address of the GOT entry.  */
9582
0
  got_address = (sgot->output_section->vma
9583
0
     + sgot->output_offset
9584
0
     + got_offset);
9585
9586
  /* ...and the address of the PLT entry.  */
9587
0
  plt_address = (splt->output_section->vma
9588
0
     + splt->output_offset
9589
0
     + root_plt->offset);
9590
9591
0
  ptr = splt->contents + root_plt->offset;
9592
0
  if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
9593
0
    {
9594
0
      unsigned int i;
9595
0
      bfd_vma val;
9596
9597
0
      for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
9598
0
  {
9599
0
    val = elf32_arm_vxworks_shared_plt_entry[i];
9600
0
    if (i == 2)
9601
0
      val |= got_address - sgot->output_section->vma;
9602
0
    if (i == 5)
9603
0
      val |= plt_index * RELOC_SIZE (htab);
9604
0
    if (i == 2 || i == 5)
9605
0
      bfd_put_32 (output_bfd, val, ptr);
9606
0
    else
9607
0
      put_arm_insn (htab, output_bfd, val, ptr);
9608
0
  }
9609
0
    }
9610
0
  else if (htab->root.target_os == is_vxworks)
9611
0
    {
9612
0
      unsigned int i;
9613
0
      bfd_vma val;
9614
9615
0
      for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
9616
0
  {
9617
0
    val = elf32_arm_vxworks_exec_plt_entry[i];
9618
0
    if (i == 2)
9619
0
      val |= got_address;
9620
0
    if (i == 4)
9621
0
      val |= 0xffffff & -((root_plt->offset + i * 4 + 8) >> 2);
9622
0
    if (i == 5)
9623
0
      val |= plt_index * RELOC_SIZE (htab);
9624
0
    if (i == 2 || i == 5)
9625
0
      bfd_put_32 (output_bfd, val, ptr);
9626
0
    else
9627
0
      put_arm_insn (htab, output_bfd, val, ptr);
9628
0
  }
9629
9630
0
      loc = (htab->srelplt2->contents
9631
0
       + (plt_index * 2 + 1) * RELOC_SIZE (htab));
9632
9633
      /* Create the .rela.plt.unloaded R_ARM_ABS32 relocation
9634
   referencing the GOT for this PLT entry.  */
9635
0
      rel.r_offset = plt_address + 8;
9636
0
      rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
9637
0
      rel.r_addend = got_offset;
9638
0
      SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
9639
0
      loc += RELOC_SIZE (htab);
9640
9641
      /* Create the R_ARM_ABS32 relocation referencing the
9642
   beginning of the PLT for this GOT entry.  */
9643
0
      rel.r_offset = got_address;
9644
0
      rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
9645
0
      rel.r_addend = 0;
9646
0
      SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
9647
0
    }
9648
0
  else if (htab->fdpic_p)
9649
0
    {
9650
0
      const bfd_vma *plt_entry = using_thumb_only (htab)
9651
0
  ? elf32_arm_fdpic_thumb_plt_entry
9652
0
  : elf32_arm_fdpic_plt_entry;
9653
9654
      /* Fill-up Thumb stub if needed.  */
9655
0
      if (elf32_arm_plt_needs_thumb_stub_p (info, arm_plt))
9656
0
  {
9657
0
    put_thumb_insn (htab, output_bfd,
9658
0
        elf32_arm_plt_thumb_stub[0], ptr - 4);
9659
0
    put_thumb_insn (htab, output_bfd,
9660
0
        elf32_arm_plt_thumb_stub[1], ptr - 2);
9661
0
  }
9662
      /* As we are using 32 bit instructions even for the Thumb
9663
   version, we have to use 'put_arm_insn' instead of
9664
   'put_thumb_insn'.  */
9665
0
      put_arm_insn (htab, output_bfd, plt_entry[0], ptr + 0);
9666
0
      put_arm_insn (htab, output_bfd, plt_entry[1], ptr + 4);
9667
0
      put_arm_insn (htab, output_bfd, plt_entry[2], ptr + 8);
9668
0
      put_arm_insn (htab, output_bfd, plt_entry[3], ptr + 12);
9669
0
      bfd_put_32 (output_bfd, got_offset, ptr + 16);
9670
9671
0
      if (!(info->flags & DF_BIND_NOW))
9672
0
  {
9673
    /* funcdesc_value_reloc_offset.  */
9674
0
    bfd_put_32 (output_bfd,
9675
0
          htab->root.srelplt->reloc_count * RELOC_SIZE (htab),
9676
0
          ptr + 20);
9677
0
    put_arm_insn (htab, output_bfd, plt_entry[6], ptr + 24);
9678
0
    put_arm_insn (htab, output_bfd, plt_entry[7], ptr + 28);
9679
0
    put_arm_insn (htab, output_bfd, plt_entry[8], ptr + 32);
9680
0
    put_arm_insn (htab, output_bfd, plt_entry[9], ptr + 36);
9681
0
  }
9682
0
    }
9683
0
  else if (using_thumb_only (htab))
9684
0
    {
9685
      /* PR ld/16017: Generate thumb only PLT entries.  */
9686
0
      if (!using_thumb2 (htab))
9687
0
  {
9688
    /* FIXME: We ought to be able to generate thumb-1 PLT
9689
       instructions...  */
9690
0
    _bfd_error_handler (_("%pB: warning: thumb-1 mode PLT generation not currently supported"),
9691
0
            output_bfd);
9692
0
    return false;
9693
0
  }
9694
9695
      /* Calculate the displacement between the PLT slot and the entry in
9696
   the GOT.  The 12-byte offset accounts for the value produced by
9697
   adding to pc in the 3rd instruction of the PLT stub.  */
9698
0
      got_displacement = got_address - (plt_address + 12);
9699
9700
      /* As we are using 32 bit instructions we have to use 'put_arm_insn'
9701
   instead of 'put_thumb_insn'.  */
9702
0
      put_arm_insn (htab, output_bfd,
9703
0
        elf32_thumb2_plt_entry[0]
9704
0
        | ((got_displacement & 0x000000ff) << 16)
9705
0
        | ((got_displacement & 0x00000700) << 20)
9706
0
        | ((got_displacement & 0x00000800) >>  1)
9707
0
        | ((got_displacement & 0x0000f000) >> 12),
9708
0
        ptr + 0);
9709
0
      put_arm_insn (htab, output_bfd,
9710
0
        elf32_thumb2_plt_entry[1]
9711
0
        | ((got_displacement & 0x00ff0000)      )
9712
0
        | ((got_displacement & 0x07000000) <<  4)
9713
0
        | ((got_displacement & 0x08000000) >> 17)
9714
0
        | ((got_displacement & 0xf0000000) >> 28),
9715
0
        ptr + 4);
9716
0
      put_arm_insn (htab, output_bfd,
9717
0
        elf32_thumb2_plt_entry[2],
9718
0
        ptr + 8);
9719
0
      put_arm_insn (htab, output_bfd,
9720
0
        elf32_thumb2_plt_entry[3],
9721
0
        ptr + 12);
9722
0
    }
9723
0
  else
9724
0
    {
9725
      /* Calculate the displacement between the PLT slot and the
9726
   entry in the GOT.  The eight-byte offset accounts for the
9727
   value produced by adding to pc in the first instruction
9728
   of the PLT stub.  */
9729
0
      got_displacement = got_address - (plt_address + 8);
9730
9731
0
      if (elf32_arm_plt_needs_thumb_stub_p (info, arm_plt))
9732
0
  {
9733
0
    put_thumb_insn (htab, output_bfd,
9734
0
        elf32_arm_plt_thumb_stub[0], ptr - 4);
9735
0
    put_thumb_insn (htab, output_bfd,
9736
0
        elf32_arm_plt_thumb_stub[1], ptr - 2);
9737
0
  }
9738
9739
0
      if (!elf32_arm_use_long_plt_entry)
9740
0
  {
9741
0
    BFD_ASSERT ((got_displacement & 0xf0000000) == 0);
9742
9743
0
    put_arm_insn (htab, output_bfd,
9744
0
      elf32_arm_plt_entry_short[0]
9745
0
      | ((got_displacement & 0x0ff00000) >> 20),
9746
0
      ptr + 0);
9747
0
    put_arm_insn (htab, output_bfd,
9748
0
      elf32_arm_plt_entry_short[1]
9749
0
      | ((got_displacement & 0x000ff000) >> 12),
9750
0
      ptr+ 4);
9751
0
    put_arm_insn (htab, output_bfd,
9752
0
      elf32_arm_plt_entry_short[2]
9753
0
      | (got_displacement & 0x00000fff),
9754
0
      ptr + 8);
9755
#ifdef FOUR_WORD_PLT
9756
    bfd_put_32 (output_bfd, elf32_arm_plt_entry_short[3], ptr + 12);
9757
#endif
9758
0
  }
9759
0
      else
9760
0
  {
9761
0
    put_arm_insn (htab, output_bfd,
9762
0
      elf32_arm_plt_entry_long[0]
9763
0
      | ((got_displacement & 0xf0000000) >> 28),
9764
0
      ptr + 0);
9765
0
    put_arm_insn (htab, output_bfd,
9766
0
      elf32_arm_plt_entry_long[1]
9767
0
      | ((got_displacement & 0x0ff00000) >> 20),
9768
0
      ptr + 4);
9769
0
    put_arm_insn (htab, output_bfd,
9770
0
      elf32_arm_plt_entry_long[2]
9771
0
      | ((got_displacement & 0x000ff000) >> 12),
9772
0
      ptr+ 8);
9773
0
    put_arm_insn (htab, output_bfd,
9774
0
      elf32_arm_plt_entry_long[3]
9775
0
      | (got_displacement & 0x00000fff),
9776
0
      ptr + 12);
9777
0
  }
9778
0
    }
9779
9780
  /* Fill in the entry in the .rel(a).(i)plt section.  */
9781
0
  rel.r_offset = got_address;
9782
0
  rel.r_addend = 0;
9783
0
  if (dynindx == -1)
9784
0
    {
9785
      /* .igot.plt entries use IRELATIVE relocations against SYM_VALUE.
9786
   The dynamic linker or static executable then calls SYM_VALUE
9787
   to determine the correct run-time value of the .igot.plt entry.  */
9788
0
      rel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
9789
0
      initial_got_entry = sym_value;
9790
0
    }
9791
0
  else
9792
0
    {
9793
      /* For FDPIC we will have to resolve a R_ARM_FUNCDESC_VALUE
9794
   used by PLT entry.  */
9795
0
      if (htab->fdpic_p)
9796
0
  {
9797
0
    rel.r_info = ELF32_R_INFO (dynindx, R_ARM_FUNCDESC_VALUE);
9798
0
    initial_got_entry = 0;
9799
0
  }
9800
0
      else
9801
0
  {
9802
0
    rel.r_info = ELF32_R_INFO (dynindx, R_ARM_JUMP_SLOT);
9803
0
    initial_got_entry = (splt->output_section->vma
9804
0
             + splt->output_offset);
9805
9806
    /* PR ld/16017
9807
       When thumb only we need to set the LSB for any address that
9808
       will be used with an interworking branch instruction.  */
9809
0
    if (using_thumb_only (htab))
9810
0
      initial_got_entry |= 1;
9811
0
  }
9812
0
    }
9813
9814
  /* Fill in the entry in the global offset table.  */
9815
0
  bfd_put_32 (output_bfd, initial_got_entry,
9816
0
        sgot->contents + got_offset);
9817
9818
0
  if (htab->fdpic_p && !(info->flags & DF_BIND_NOW))
9819
0
    {
9820
      /* Setup initial funcdesc value.  */
9821
      /* FIXME: we don't support lazy binding because there is a
9822
   race condition between both words getting written and
9823
   some other thread attempting to read them. The ARM
9824
   architecture does not have an atomic 64 bit load/store
9825
   instruction that could be used to prevent it; it is
9826
   recommended that threaded FDPIC applications run with the
9827
   LD_BIND_NOW environment variable set.  */
9828
0
      bfd_put_32 (output_bfd, plt_address + 0x18,
9829
0
      sgot->contents + got_offset);
9830
0
      bfd_put_32 (output_bfd, -1 /*TODO*/,
9831
0
      sgot->contents + got_offset + 4);
9832
0
    }
9833
9834
0
  if (dynindx == -1)
9835
0
    elf32_arm_add_dynreloc (output_bfd, info, srel, &rel);
9836
0
  else
9837
0
    {
9838
0
      if (htab->fdpic_p)
9839
0
  {
9840
    /* For FDPIC we put PLT relocationss into .rel.got when not
9841
       lazy binding otherwise we put them in .rel.plt.  For now,
9842
       we don't support lazy binding so put it in .rel.got.  */
9843
0
    if (info->flags & DF_BIND_NOW)
9844
0
      elf32_arm_add_dynreloc (output_bfd, info, htab->root.srelgot, &rel);
9845
0
    else
9846
0
      elf32_arm_add_dynreloc (output_bfd, info, htab->root.srelplt, &rel);
9847
0
  }
9848
0
      else
9849
0
  {
9850
0
    loc = srel->contents + plt_index * RELOC_SIZE (htab);
9851
0
    SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
9852
0
  }
9853
0
    }
9854
9855
0
  return true;
9856
0
}
9857
9858
/* Some relocations map to different relocations depending on the
9859
   target.  Return the real relocation.  */
9860
9861
static int
9862
arm_real_reloc_type (struct elf32_arm_link_hash_table * globals,
9863
         int r_type)
9864
0
{
9865
0
  switch (r_type)
9866
0
    {
9867
0
    case R_ARM_TARGET1:
9868
0
      if (globals->target1_is_rel)
9869
0
  return R_ARM_REL32;
9870
0
      else
9871
0
  return R_ARM_ABS32;
9872
9873
0
    case R_ARM_TARGET2:
9874
0
      return globals->target2_reloc;
9875
9876
0
    default:
9877
0
      return r_type;
9878
0
    }
9879
0
}
9880
9881
/* Return the base VMA address which should be subtracted from real addresses
9882
   when resolving @dtpoff relocation.
9883
   This is PT_TLS segment p_vaddr.  */
9884
9885
static bfd_vma
9886
dtpoff_base (struct bfd_link_info *info)
9887
0
{
9888
  /* If tls_sec is NULL, we should have signalled an error already.  */
9889
0
  if (elf_hash_table (info)->tls_sec == NULL)
9890
0
    return 0;
9891
0
  return elf_hash_table (info)->tls_sec->vma;
9892
0
}
9893
9894
/* Return the relocation value for @tpoff relocation
9895
   if STT_TLS virtual address is ADDRESS.  */
9896
9897
static bfd_vma
9898
tpoff (struct bfd_link_info *info, bfd_vma address)
9899
0
{
9900
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
9901
0
  bfd_vma base;
9902
9903
  /* If tls_sec is NULL, we should have signalled an error already.  */
9904
0
  if (htab->tls_sec == NULL)
9905
0
    return 0;
9906
0
  base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
9907
0
  return address - htab->tls_sec->vma + base;
9908
0
}
9909
9910
/* Perform an R_ARM_ABS12 relocation on the field pointed to by DATA.
9911
   VALUE is the relocation value.  */
9912
9913
static bfd_reloc_status_type
9914
elf32_arm_abs12_reloc (bfd *abfd, void *data, bfd_vma value)
9915
0
{
9916
0
  if (value > 0xfff)
9917
0
    return bfd_reloc_overflow;
9918
9919
0
  value |= bfd_get_32 (abfd, data) & 0xfffff000;
9920
0
  bfd_put_32 (abfd, value, data);
9921
0
  return bfd_reloc_ok;
9922
0
}
9923
9924
/* Handle TLS relaxations.  Relaxing is possible for symbols that use
9925
   R_ARM_GOTDESC, R_ARM_{,THM_}TLS_CALL or
9926
   R_ARM_{,THM_}TLS_DESCSEQ relocations, during a static link.
9927
9928
   Return bfd_reloc_ok if we're done, bfd_reloc_continue if the caller
9929
   is to then call final_link_relocate.  Return other values in the
9930
   case of error.
9931
9932
   FIXME:When --emit-relocs is in effect, we'll emit relocs describing
9933
   the pre-relaxed code.  It would be nice if the relocs were updated
9934
   to match the optimization.   */
9935
9936
static bfd_reloc_status_type
9937
elf32_arm_tls_relax (struct elf32_arm_link_hash_table *globals,
9938
         bfd *input_bfd, asection *input_sec, bfd_byte *contents,
9939
         Elf_Internal_Rela *rel, unsigned long is_local)
9940
0
{
9941
0
  unsigned long insn;
9942
9943
0
  switch (ELF32_R_TYPE (rel->r_info))
9944
0
    {
9945
0
    default:
9946
0
      return bfd_reloc_notsupported;
9947
9948
0
    case R_ARM_TLS_GOTDESC:
9949
0
      if (is_local)
9950
0
  insn = 0;
9951
0
      else
9952
0
  {
9953
0
    insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
9954
0
    if (insn & 1)
9955
0
      insn -= 5; /* THUMB */
9956
0
    else
9957
0
      insn -= 8; /* ARM */
9958
0
  }
9959
0
      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
9960
0
      return bfd_reloc_continue;
9961
9962
0
    case R_ARM_THM_TLS_DESCSEQ:
9963
      /* Thumb insn.  */
9964
0
      insn = bfd_get_16 (input_bfd, contents + rel->r_offset);
9965
0
      if ((insn & 0xff78) == 0x4478)   /* add rx, pc */
9966
0
  {
9967
0
    if (is_local)
9968
      /* nop */
9969
0
      bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
9970
0
  }
9971
0
      else if ((insn & 0xffc0) == 0x6840)  /* ldr rx,[ry,#4] */
9972
0
  {
9973
0
    if (is_local)
9974
      /* nop */
9975
0
      bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
9976
0
    else
9977
      /* ldr rx,[ry] */
9978
0
      bfd_put_16 (input_bfd, insn & 0xf83f, contents + rel->r_offset);
9979
0
  }
9980
0
      else if ((insn & 0xff87) == 0x4780)  /* blx rx */
9981
0
  {
9982
0
    if (is_local)
9983
      /* nop */
9984
0
      bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
9985
0
    else
9986
      /* mov r0, rx */
9987
0
      bfd_put_16 (input_bfd, 0x4600 | (insn & 0x78),
9988
0
      contents + rel->r_offset);
9989
0
  }
9990
0
      else
9991
0
  {
9992
0
    if ((insn & 0xf000) == 0xf000 || (insn & 0xf800) == 0xe800)
9993
      /* It's a 32 bit instruction, fetch the rest of it for
9994
         error generation.  */
9995
0
      insn = (insn << 16)
9996
0
        | bfd_get_16 (input_bfd, contents + rel->r_offset + 2);
9997
0
    _bfd_error_handler
9998
      /* xgettext:c-format */
9999
0
      (_("%pB(%pA+%#" PRIx64 "): "
10000
0
         "unexpected %s instruction '%#lx' in TLS trampoline"),
10001
0
       input_bfd, input_sec, (uint64_t) rel->r_offset,
10002
0
       "Thumb", insn);
10003
0
    return bfd_reloc_notsupported;
10004
0
  }
10005
0
      break;
10006
10007
0
    case R_ARM_TLS_DESCSEQ:
10008
      /* arm insn.  */
10009
0
      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
10010
0
      if ((insn & 0xffff0ff0) == 0xe08f0000) /* add rx,pc,ry */
10011
0
  {
10012
0
    if (is_local)
10013
      /* mov rx, ry */
10014
0
      bfd_put_32 (input_bfd, 0xe1a00000 | (insn & 0xffff),
10015
0
      contents + rel->r_offset);
10016
0
  }
10017
0
      else if ((insn & 0xfff00fff) == 0xe5900004) /* ldr rx,[ry,#4]*/
10018
0
  {
10019
0
    if (is_local)
10020
      /* nop */
10021
0
      bfd_put_32 (input_bfd, 0xe1a00000, contents + rel->r_offset);
10022
0
    else
10023
      /* ldr rx,[ry] */
10024
0
      bfd_put_32 (input_bfd, insn & 0xfffff000,
10025
0
      contents + rel->r_offset);
10026
0
  }
10027
0
      else if ((insn & 0xfffffff0) == 0xe12fff30) /* blx rx */
10028
0
  {
10029
0
    if (is_local)
10030
      /* nop */
10031
0
      bfd_put_32 (input_bfd, 0xe1a00000, contents + rel->r_offset);
10032
0
    else
10033
      /* mov r0, rx */
10034
0
      bfd_put_32 (input_bfd, 0xe1a00000 | (insn & 0xf),
10035
0
      contents + rel->r_offset);
10036
0
  }
10037
0
      else
10038
0
  {
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
       "ARM", insn);
10045
0
    return bfd_reloc_notsupported;
10046
0
  }
10047
0
      break;
10048
10049
0
    case R_ARM_TLS_CALL:
10050
      /* GD->IE relaxation, turn the instruction into 'nop' or
10051
   'ldr r0, [pc,r0]'  */
10052
0
      insn = is_local ? 0xe1a00000 : 0xe79f0000;
10053
0
      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
10054
0
      break;
10055
10056
0
    case R_ARM_THM_TLS_CALL:
10057
      /* GD->IE relaxation.  */
10058
0
      if (!is_local)
10059
  /* add r0,pc; ldr r0, [r0]  */
10060
0
  insn = 0x44786800;
10061
0
      else if (using_thumb2 (globals))
10062
  /* nop.w */
10063
0
  insn = 0xf3af8000;
10064
0
      else
10065
  /* nop; nop */
10066
0
  insn = 0xbf00bf00;
10067
10068
0
      bfd_put_16 (input_bfd, insn >> 16, contents + rel->r_offset);
10069
0
      bfd_put_16 (input_bfd, insn & 0xffff, contents + rel->r_offset + 2);
10070
0
      break;
10071
0
    }
10072
0
  return bfd_reloc_ok;
10073
0
}
10074
10075
/* For a given value of n, calculate the value of G_n as required to
10076
   deal with group relocations.  We return it in the form of an
10077
   encoded constant-and-rotation, together with the final residual.  If n is
10078
   specified as less than zero, then final_residual is filled with the
10079
   input value and no further action is performed.  */
10080
10081
static bfd_vma
10082
calculate_group_reloc_mask (bfd_vma value, int n, bfd_vma *final_residual)
10083
0
{
10084
0
  int current_n;
10085
0
  bfd_vma g_n;
10086
0
  bfd_vma encoded_g_n = 0;
10087
0
  bfd_vma residual = value; /* Also known as Y_n.  */
10088
10089
0
  for (current_n = 0; current_n <= n; current_n++)
10090
0
    {
10091
0
      int shift;
10092
10093
      /* Calculate which part of the value to mask.  */
10094
0
      if (residual == 0)
10095
0
  shift = 0;
10096
0
      else
10097
0
  {
10098
0
    int msb;
10099
10100
    /* Determine the most significant bit in the residual and
10101
       align the resulting value to a 2-bit boundary.  */
10102
0
    for (msb = 30; msb >= 0; msb -= 2)
10103
0
      if (residual & (3u << msb))
10104
0
        break;
10105
10106
    /* The desired shift is now (msb - 6), or zero, whichever
10107
       is the greater.  */
10108
0
    shift = msb - 6;
10109
0
    if (shift < 0)
10110
0
      shift = 0;
10111
0
  }
10112
10113
      /* Calculate g_n in 32-bit as well as encoded constant+rotation form.  */
10114
0
      g_n = residual & (0xff << shift);
10115
0
      encoded_g_n = (g_n >> shift)
10116
0
        | ((g_n <= 0xff ? 0 : (32 - shift) / 2) << 8);
10117
10118
      /* Calculate the residual for the next time around.  */
10119
0
      residual &= ~g_n;
10120
0
    }
10121
10122
0
  *final_residual = residual;
10123
10124
0
  return encoded_g_n;
10125
0
}
10126
10127
/* Given an ARM instruction, determine whether it is an ADD or a SUB.
10128
   Returns 1 if it is an ADD, -1 if it is a SUB, and 0 otherwise.  */
10129
10130
static int
10131
identify_add_or_sub (bfd_vma insn)
10132
0
{
10133
0
  int opcode = insn & 0x1e00000;
10134
10135
0
  if (opcode == 1 << 23) /* ADD */
10136
0
    return 1;
10137
10138
0
  if (opcode == 1 << 22) /* SUB */
10139
0
    return -1;
10140
10141
0
  return 0;
10142
0
}
10143
10144
/* Perform a relocation as part of a final link.  */
10145
10146
static bfd_reloc_status_type
10147
elf32_arm_final_link_relocate (reloc_howto_type *     howto,
10148
             bfd *          input_bfd,
10149
             bfd *          output_bfd,
10150
             asection *       input_section,
10151
             bfd_byte *       contents,
10152
             Elf_Internal_Rela *      rel,
10153
             bfd_vma          value,
10154
             struct bfd_link_info *     info,
10155
             asection *       sym_sec,
10156
             const char *       sym_name,
10157
             unsigned char        st_type,
10158
             enum arm_st_branch_type      branch_type,
10159
             struct elf_link_hash_entry * h,
10160
             bool *         unresolved_reloc_p,
10161
             char **          error_message)
10162
0
{
10163
0
  unsigned long     r_type = howto->type;
10164
0
  unsigned long     r_symndx;
10165
0
  bfd_byte *      hit_data = contents + rel->r_offset;
10166
0
  bfd_vma *     local_got_offsets;
10167
0
  bfd_vma *     local_tlsdesc_gotents;
10168
0
  asection *      sgot;
10169
0
  asection *      splt;
10170
0
  asection *      sreloc;
10171
0
  asection *      srelgot;
10172
0
  bfd_vma     addend;
10173
0
  bfd_signed_vma    signed_addend;
10174
0
  unsigned char     dynreloc_st_type;
10175
0
  bfd_vma     dynreloc_value;
10176
0
  struct elf32_arm_link_hash_table * globals;
10177
0
  struct elf32_arm_link_hash_entry *eh;
10178
0
  union gotplt_union         *root_plt;
10179
0
  struct arm_plt_info        *arm_plt;
10180
0
  bfd_vma     plt_offset;
10181
0
  bfd_vma     gotplt_offset;
10182
0
  bool        has_iplt_entry;
10183
0
  bool        resolved_to_zero;
10184
10185
0
  globals = elf32_arm_hash_table (info);
10186
0
  if (globals == NULL)
10187
0
    return bfd_reloc_notsupported;
10188
10189
0
  BFD_ASSERT (is_arm_elf (input_bfd));
10190
0
  BFD_ASSERT (howto != NULL);
10191
10192
  /* Some relocation types map to different relocations depending on the
10193
     target.  We pick the right one here.  */
10194
0
  r_type = arm_real_reloc_type (globals, r_type);
10195
10196
  /* It is possible to have linker relaxations on some TLS access
10197
     models.  Update our information here.  */
10198
0
  r_type = elf32_arm_tls_transition (info, r_type, h);
10199
10200
0
  if (r_type != howto->type)
10201
0
    howto = elf32_arm_howto_from_type (r_type);
10202
10203
0
  eh = (struct elf32_arm_link_hash_entry *) h;
10204
0
  sgot = globals->root.sgot;
10205
0
  local_got_offsets = elf_local_got_offsets (input_bfd);
10206
0
  local_tlsdesc_gotents = elf32_arm_local_tlsdesc_gotent (input_bfd);
10207
10208
0
  if (globals->root.dynamic_sections_created)
10209
0
    srelgot = globals->root.srelgot;
10210
0
  else
10211
0
    srelgot = NULL;
10212
10213
0
  r_symndx = ELF32_R_SYM (rel->r_info);
10214
10215
0
  if (globals->use_rel)
10216
0
    {
10217
0
      bfd_vma sign;
10218
10219
0
      switch (bfd_get_reloc_size (howto))
10220
0
  {
10221
0
  case 1: addend = bfd_get_8 (input_bfd, hit_data); break;
10222
0
  case 2: addend = bfd_get_16 (input_bfd, hit_data); break;
10223
0
  case 4: addend = bfd_get_32 (input_bfd, hit_data); break;
10224
0
  default: addend = 0; break;
10225
0
  }
10226
      /* Note: the addend and signed_addend calculated here are
10227
   incorrect for any split field.  */
10228
0
      addend &= howto->src_mask;
10229
0
      sign = howto->src_mask & ~(howto->src_mask >> 1);
10230
0
      signed_addend = (addend ^ sign) - sign;
10231
0
      signed_addend = (bfd_vma) signed_addend << howto->rightshift;
10232
0
      addend <<= howto->rightshift;
10233
0
    }
10234
0
  else
10235
0
    addend = signed_addend = rel->r_addend;
10236
10237
  /* Record the symbol information that should be used in dynamic
10238
     relocations.  */
10239
0
  dynreloc_st_type = st_type;
10240
0
  dynreloc_value = value;
10241
0
  if (branch_type == ST_BRANCH_TO_THUMB)
10242
0
    dynreloc_value |= 1;
10243
10244
  /* Find out whether the symbol has a PLT.  Set ST_VALUE, BRANCH_TYPE and
10245
     VALUE appropriately for relocations that we resolve at link time.  */
10246
0
  has_iplt_entry = false;
10247
0
  if (elf32_arm_get_plt_info (input_bfd, globals, eh, r_symndx, &root_plt,
10248
0
            &arm_plt)
10249
0
      && root_plt->offset != (bfd_vma) -1)
10250
0
    {
10251
0
      plt_offset = root_plt->offset;
10252
0
      gotplt_offset = arm_plt->got_offset;
10253
10254
0
      if (h == NULL || eh->is_iplt)
10255
0
  {
10256
0
    has_iplt_entry = true;
10257
0
    splt = globals->root.iplt;
10258
10259
    /* Populate .iplt entries here, because not all of them will
10260
       be seen by finish_dynamic_symbol.  The lower bit is set if
10261
       we have already populated the entry.  */
10262
0
    if (plt_offset & 1)
10263
0
      plt_offset--;
10264
0
    else
10265
0
      {
10266
0
        if (elf32_arm_populate_plt_entry (output_bfd, info, root_plt, arm_plt,
10267
0
            -1, dynreloc_value))
10268
0
    root_plt->offset |= 1;
10269
0
        else
10270
0
    return bfd_reloc_notsupported;
10271
0
      }
10272
10273
    /* Static relocations always resolve to the .iplt entry.  */
10274
0
    st_type = STT_FUNC;
10275
0
    value = (splt->output_section->vma
10276
0
       + splt->output_offset
10277
0
       + plt_offset);
10278
0
    branch_type = ST_BRANCH_TO_ARM;
10279
10280
    /* If there are non-call relocations that resolve to the .iplt
10281
       entry, then all dynamic ones must too.  */
10282
0
    if (arm_plt->noncall_refcount != 0)
10283
0
      {
10284
0
        dynreloc_st_type = st_type;
10285
0
        dynreloc_value = value;
10286
0
      }
10287
0
  }
10288
0
      else
10289
  /* We populate the .plt entry in finish_dynamic_symbol.  */
10290
0
  splt = globals->root.splt;
10291
0
    }
10292
0
  else
10293
0
    {
10294
0
      splt = NULL;
10295
0
      plt_offset = (bfd_vma) -1;
10296
0
      gotplt_offset = (bfd_vma) -1;
10297
0
    }
10298
10299
  /* ST_BRANCH_TO_ARM is nonsense to thumb-only targets when we are
10300
     resolving a function call relocation.  We want to inform the user
10301
     that something is wrong.  */
10302
0
  if (using_thumb_only (globals)
10303
0
      && (r_type == R_ARM_THM_CALL
10304
0
    || r_type == R_ARM_THM_JUMP24)
10305
0
      && branch_type == ST_BRANCH_TO_ARM
10306
      /* Calls through a PLT are special: the assembly source code
10307
   cannot be annotated with '.type foo(PLT), %function', and
10308
   they handled specifically below anyway. */
10309
0
      && splt == NULL)
10310
0
    {
10311
0
      if (sym_sec == bfd_abs_section_ptr)
10312
0
  {
10313
  /* As an exception, assume that absolute symbols are of the
10314
     right kind (Thumb).  They are presumably defined in the
10315
     linker script, where it is not possible to declare them as
10316
     Thumb (and thus are seen as Arm mode). Inform the user with
10317
     a warning, though. */
10318
0
    branch_type = ST_BRANCH_TO_THUMB;
10319
10320
0
    if (sym_sec->owner)
10321
0
      _bfd_error_handler
10322
0
        (_("warning: %pB(%s): Forcing bramch to absolute symbol in Thumb mode (Thumb-only CPU)"
10323
0
     " in %pB"),
10324
0
         sym_sec->owner, sym_name, input_bfd);
10325
0
    else
10326
0
      _bfd_error_handler
10327
0
        (_("warning: (%s): Forcing branch to absolute symbol in Thumb mode (Thumb-only CPU)"
10328
0
     " in %pB"),
10329
0
         sym_name, input_bfd);
10330
0
  }
10331
0
      else
10332
  /* Otherwise do not silently build a stub, and let the users
10333
     know they have to fix their code.  Indeed, we could decide
10334
     to insert a stub involving Arm code and/or BLX, leading to
10335
     a run-time crash.  */
10336
0
  branch_type = ST_BRANCH_UNKNOWN;
10337
0
    }
10338
10339
  /* Fail early if branch_type is ST_BRANCH_UNKNOWN and we target a
10340
     Thumb-only CPU.  We could emit a warning on Arm-capable targets
10341
     too, but that would be too verbose (a lot of legacy code does not
10342
     use the .type foo, %function directive).  */
10343
0
  if (using_thumb_only (globals)
10344
0
      && (r_type == R_ARM_THM_CALL
10345
0
    || r_type == R_ARM_THM_JUMP24)
10346
0
      && branch_type == ST_BRANCH_UNKNOWN
10347
      /* Exception to the rule above: a branch to an undefined weak
10348
   symbol is turned into a jump to the next instruction unless a
10349
   PLT entry will be created (see below).  */
10350
0
      && !(h && h->root.type == bfd_link_hash_undefweak
10351
0
     && plt_offset == (bfd_vma) -1))
10352
0
    {
10353
0
      if (sym_sec != NULL
10354
0
    && sym_sec->owner != NULL)
10355
0
  _bfd_error_handler
10356
0
    (_("%pB(%s): Unknown destination type (ARM/Thumb) in %pB"),
10357
0
     sym_sec->owner, sym_name, input_bfd);
10358
0
      else
10359
0
  _bfd_error_handler
10360
0
    (_("(%s): Unknown destination type (ARM/Thumb) in %pB"),
10361
0
     sym_name, input_bfd);
10362
10363
0
      return bfd_reloc_notsupported;
10364
0
    }
10365
10366
0
  resolved_to_zero = (h != NULL
10367
0
          && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
10368
10369
0
  switch (r_type)
10370
0
    {
10371
0
    case R_ARM_NONE:
10372
      /* We don't need to find a value for this symbol.  It's just a
10373
   marker.  */
10374
0
      *unresolved_reloc_p = false;
10375
0
      return bfd_reloc_ok;
10376
10377
0
    case R_ARM_ABS12:
10378
0
      if (globals->root.target_os != is_vxworks)
10379
0
  return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
10380
      /* Fall through.  */
10381
10382
0
    case R_ARM_PC24:
10383
0
    case R_ARM_ABS32:
10384
0
    case R_ARM_ABS32_NOI:
10385
0
    case R_ARM_REL32:
10386
0
    case R_ARM_REL32_NOI:
10387
0
    case R_ARM_CALL:
10388
0
    case R_ARM_JUMP24:
10389
0
    case R_ARM_XPC25:
10390
0
    case R_ARM_PREL31:
10391
0
    case R_ARM_PLT32:
10392
      /* Handle relocations which should use the PLT entry.  ABS32/REL32
10393
   will use the symbol's value, which may point to a PLT entry, but we
10394
   don't need to handle that here.  If we created a PLT entry, all
10395
   branches in this object should go to it, except if the PLT is too
10396
   far away, in which case a long branch stub should be inserted.  */
10397
0
      if ((r_type != R_ARM_ABS32 && r_type != R_ARM_REL32
10398
0
     && r_type != R_ARM_ABS32_NOI && r_type != R_ARM_REL32_NOI
10399
0
     && r_type != R_ARM_CALL
10400
0
     && r_type != R_ARM_JUMP24
10401
0
     && r_type != R_ARM_PLT32)
10402
0
    && plt_offset != (bfd_vma) -1)
10403
0
  {
10404
    /* If we've created a .plt section, and assigned a PLT entry
10405
       to this function, it must either be a STT_GNU_IFUNC reference
10406
       or not be known to bind locally.  In other cases, we should
10407
       have cleared the PLT entry by now.  */
10408
0
    BFD_ASSERT (has_iplt_entry || !SYMBOL_CALLS_LOCAL (info, h));
10409
10410
0
    value = (splt->output_section->vma
10411
0
       + splt->output_offset
10412
0
       + plt_offset);
10413
0
    *unresolved_reloc_p = false;
10414
0
    return _bfd_final_link_relocate (howto, input_bfd, input_section,
10415
0
             contents, rel->r_offset, value,
10416
0
             rel->r_addend);
10417
0
  }
10418
10419
      /* When generating a shared library or PIE, these relocations
10420
   are copied into the output file to be resolved at run time.  */
10421
0
      if ((bfd_link_pic (info)
10422
0
     || globals->fdpic_p)
10423
0
    && (input_section->flags & SEC_ALLOC)
10424
0
    && !(globals->root.target_os == is_vxworks
10425
0
         && strcmp (input_section->output_section->name,
10426
0
        ".tls_vars") == 0)
10427
0
    && ((r_type != R_ARM_REL32 && r_type != R_ARM_REL32_NOI)
10428
0
        || !SYMBOL_CALLS_LOCAL (info, h))
10429
0
    && !(input_bfd == globals->stub_bfd
10430
0
         && strstr (input_section->name, STUB_SUFFIX))
10431
0
    && (h == NULL
10432
0
        || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10433
0
      && !resolved_to_zero)
10434
0
        || h->root.type != bfd_link_hash_undefweak)
10435
0
    && r_type != R_ARM_PC24
10436
0
    && r_type != R_ARM_CALL
10437
0
    && r_type != R_ARM_JUMP24
10438
0
    && r_type != R_ARM_PREL31
10439
0
    && r_type != R_ARM_PLT32)
10440
0
  {
10441
0
    Elf_Internal_Rela outrel;
10442
0
    bool skip, relocate;
10443
0
    int isrofixup = 0;
10444
10445
0
    if ((r_type == R_ARM_REL32 || r_type == R_ARM_REL32_NOI)
10446
0
        && !h->def_regular)
10447
0
      {
10448
0
        char *v = _("shared object");
10449
10450
0
        if (bfd_link_executable (info))
10451
0
    v = _("PIE executable");
10452
10453
0
        _bfd_error_handler
10454
0
    (_("%pB: relocation %s against external or undefined symbol `%s'"
10455
0
       " can not be used when making a %s; recompile with -fPIC"), input_bfd,
10456
0
     elf32_arm_howto_table_1[r_type].name, h->root.root.string, v);
10457
0
        return bfd_reloc_notsupported;
10458
0
      }
10459
10460
0
    *unresolved_reloc_p = false;
10461
0
    skip = false;
10462
0
    relocate = false;
10463
10464
0
    outrel.r_addend = addend;
10465
0
    outrel.r_offset =
10466
0
      _bfd_elf_section_offset (output_bfd, info, input_section,
10467
0
             rel->r_offset);
10468
0
    if (outrel.r_offset == (bfd_vma) -1)
10469
0
      skip = true;
10470
0
    else if (outrel.r_offset == (bfd_vma) -2)
10471
0
      skip = true, relocate = true;
10472
0
    outrel.r_offset += (input_section->output_section->vma
10473
0
            + input_section->output_offset);
10474
10475
0
    if (skip)
10476
0
      memset (&outrel, 0, sizeof outrel);
10477
0
    else if (h != NULL
10478
0
       && h->dynindx != -1
10479
0
       && (!bfd_link_pic (info)
10480
0
           || !(bfd_link_pie (info)
10481
0
          || SYMBOLIC_BIND (info, h))
10482
0
           || !h->def_regular))
10483
0
      outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
10484
0
    else
10485
0
      {
10486
0
        int symbol;
10487
10488
        /* This symbol is local, or marked to become local.  */
10489
0
        BFD_ASSERT (r_type == R_ARM_ABS32 || r_type == R_ARM_ABS32_NOI
10490
0
        || (globals->fdpic_p && !bfd_link_pic (info)));
10491
        /* On SVR4-ish systems, the dynamic loader cannot
10492
     relocate the text and data segments independently,
10493
     so the symbol does not matter.  */
10494
0
        symbol = 0;
10495
0
        if (dynreloc_st_type == STT_GNU_IFUNC)
10496
    /* We have an STT_GNU_IFUNC symbol that doesn't resolve
10497
       to the .iplt entry.  Instead, every non-call reference
10498
       must use an R_ARM_IRELATIVE relocation to obtain the
10499
       correct run-time address.  */
10500
0
    outrel.r_info = ELF32_R_INFO (symbol, R_ARM_IRELATIVE);
10501
0
        else if (globals->fdpic_p && !bfd_link_pic (info))
10502
0
    isrofixup = 1;
10503
0
        else
10504
0
    outrel.r_info = ELF32_R_INFO (symbol, R_ARM_RELATIVE);
10505
0
        if (globals->use_rel)
10506
0
    relocate = true;
10507
0
        else
10508
0
    outrel.r_addend += dynreloc_value;
10509
0
      }
10510
10511
0
    if (isrofixup)
10512
0
      arm_elf_add_rofixup (output_bfd, globals->srofixup, outrel.r_offset);
10513
0
    else
10514
0
      {
10515
0
        sreloc = elf_section_data (input_section)->sreloc;
10516
0
        if (sreloc == NULL)
10517
0
    return bfd_reloc_notsupported;
10518
0
        elf32_arm_add_dynreloc (output_bfd, info, sreloc, &outrel);
10519
0
      }
10520
10521
    /* If this reloc is against an external symbol, we do not want to
10522
       fiddle with the addend.  Otherwise, we need to include the symbol
10523
       value so that it becomes an addend for the dynamic reloc.  */
10524
0
    if (! relocate)
10525
0
      return bfd_reloc_ok;
10526
10527
0
    return _bfd_final_link_relocate (howto, input_bfd, input_section,
10528
0
             contents, rel->r_offset,
10529
0
             dynreloc_value, (bfd_vma) 0);
10530
0
  }
10531
0
      else switch (r_type)
10532
0
  {
10533
0
  case R_ARM_ABS12:
10534
0
    return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
10535
10536
0
  case R_ARM_XPC25:   /* Arm BLX instruction.  */
10537
0
  case R_ARM_CALL:
10538
0
  case R_ARM_JUMP24:
10539
0
  case R_ARM_PC24:    /* Arm B/BL instruction.  */
10540
0
  case R_ARM_PLT32:
10541
0
    {
10542
0
    struct elf32_arm_stub_hash_entry *stub_entry = NULL;
10543
10544
0
    if (r_type == R_ARM_XPC25)
10545
0
      {
10546
        /* Check for Arm calling Arm function.  */
10547
        /* FIXME: Should we translate the instruction into a BL
10548
     instruction instead ?  */
10549
0
        if (branch_type != ST_BRANCH_TO_THUMB)
10550
0
    _bfd_error_handler
10551
0
      (_("%pB: warning: %s BLX instruction targets"
10552
0
         " %s function '%s'"),
10553
0
       input_bfd, "ARM",
10554
0
       "ARM", h ? h->root.root.string : "(local)");
10555
0
      }
10556
0
    else if (r_type == R_ARM_PC24)
10557
0
      {
10558
        /* Check for Arm calling Thumb function.  */
10559
0
        if (branch_type == ST_BRANCH_TO_THUMB)
10560
0
    {
10561
0
      if (elf32_arm_to_thumb_stub (info, sym_name, input_bfd,
10562
0
                 output_bfd, input_section,
10563
0
                 hit_data, sym_sec, rel->r_offset,
10564
0
                 signed_addend, value,
10565
0
                 error_message))
10566
0
        return bfd_reloc_ok;
10567
0
      else
10568
0
        return bfd_reloc_dangerous;
10569
0
    }
10570
0
      }
10571
10572
    /* Check if a stub has to be inserted because the
10573
       destination is too far or we are changing mode.  */
10574
0
    if (   r_type == R_ARM_CALL
10575
0
        || r_type == R_ARM_JUMP24
10576
0
        || r_type == R_ARM_PLT32)
10577
0
      {
10578
0
        enum elf32_arm_stub_type stub_type = arm_stub_none;
10579
0
        struct elf32_arm_link_hash_entry *hash;
10580
10581
0
        hash = (struct elf32_arm_link_hash_entry *) h;
10582
0
        stub_type = arm_type_of_stub (info, input_section, rel,
10583
0
              st_type, &branch_type,
10584
0
              hash, value, sym_sec,
10585
0
              input_bfd, sym_name);
10586
10587
0
        if (stub_type != arm_stub_none)
10588
0
    {
10589
      /* The target is out of reach, so redirect the
10590
         branch to the local stub for this function.  */
10591
0
      stub_entry = elf32_arm_get_stub_entry (input_section,
10592
0
               sym_sec, h,
10593
0
               rel, globals,
10594
0
               stub_type);
10595
0
      {
10596
0
        if (stub_entry != NULL)
10597
0
          value = (stub_entry->stub_offset
10598
0
             + stub_entry->stub_sec->output_offset
10599
0
             + stub_entry->stub_sec->output_section->vma);
10600
10601
0
        if (plt_offset != (bfd_vma) -1)
10602
0
          *unresolved_reloc_p = false;
10603
0
      }
10604
0
    }
10605
0
        else
10606
0
    {
10607
      /* If the call goes through a PLT entry, make sure to
10608
         check distance to the right destination address.  */
10609
0
      if (plt_offset != (bfd_vma) -1)
10610
0
        {
10611
0
          value = (splt->output_section->vma
10612
0
             + splt->output_offset
10613
0
             + plt_offset);
10614
0
          *unresolved_reloc_p = false;
10615
          /* The PLT entry is in ARM mode, regardless of the
10616
       target function.  */
10617
0
          branch_type = ST_BRANCH_TO_ARM;
10618
0
        }
10619
0
    }
10620
0
      }
10621
10622
    /* The ARM ELF ABI says that this reloc is computed as: S - P + A
10623
       where:
10624
        S is the address of the symbol in the relocation.
10625
        P is address of the instruction being relocated.
10626
        A is the addend (extracted from the instruction) in bytes.
10627
10628
       S is held in 'value'.
10629
       P is the base address of the section containing the
10630
         instruction plus the offset of the reloc into that
10631
         section, ie:
10632
     (input_section->output_section->vma +
10633
      input_section->output_offset +
10634
      rel->r_offset).
10635
       A is the addend, converted into bytes, ie:
10636
     (signed_addend * 4)
10637
10638
       Note: None of these operations have knowledge of the pipeline
10639
       size of the processor, thus it is up to the assembler to
10640
       encode this information into the addend.  */
10641
0
    value -= (input_section->output_section->vma
10642
0
        + input_section->output_offset);
10643
0
    value -= rel->r_offset;
10644
0
    value += signed_addend;
10645
10646
0
    signed_addend = value;
10647
0
    signed_addend >>= howto->rightshift;
10648
10649
    /* A branch to an undefined weak symbol is turned into a jump to
10650
       the next instruction unless a PLT entry will be created.
10651
       Do the same for local undefined symbols (but not for STN_UNDEF).
10652
       The jump to the next instruction is optimized as a NOP depending
10653
       on the architecture.  */
10654
0
    if (h ? (h->root.type == bfd_link_hash_undefweak
10655
0
       && plt_offset == (bfd_vma) -1)
10656
0
        : r_symndx != STN_UNDEF && bfd_is_und_section (sym_sec))
10657
0
      {
10658
0
        value = (bfd_get_32 (input_bfd, hit_data) & 0xf0000000);
10659
10660
0
        if (arch_has_arm_nop (globals))
10661
0
    value |= 0x0320f000;
10662
0
        else
10663
0
    value |= 0x01a00000; /* Using pre-UAL nop: mov r0, r0.  */
10664
0
      }
10665
0
    else
10666
0
      {
10667
        /* Perform a signed range check.  */
10668
0
        if (   signed_addend >   ((bfd_signed_vma)  (howto->dst_mask >> 1))
10669
0
      || signed_addend < - ((bfd_signed_vma) ((howto->dst_mask + 1) >> 1)))
10670
0
    return bfd_reloc_overflow;
10671
10672
0
        addend = (value & 2);
10673
10674
0
        value = (signed_addend & howto->dst_mask)
10675
0
    | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask));
10676
10677
0
        if (r_type == R_ARM_CALL)
10678
0
    {
10679
      /* Set the H bit in the BLX instruction.  */
10680
0
      if (branch_type == ST_BRANCH_TO_THUMB)
10681
0
        {
10682
0
          if (addend)
10683
0
      value |= (1 << 24);
10684
0
          else
10685
0
      value &= ~(bfd_vma)(1 << 24);
10686
0
        }
10687
10688
      /* Select the correct instruction (BL or BLX).  */
10689
      /* Only if we are not handling a BL to a stub. In this
10690
         case, mode switching is performed by the stub.  */
10691
0
      if (branch_type == ST_BRANCH_TO_THUMB && !stub_entry)
10692
0
        value |= (1 << 28);
10693
0
      else if (stub_entry || branch_type != ST_BRANCH_UNKNOWN)
10694
0
        {
10695
0
          value &= ~(bfd_vma)(1 << 28);
10696
0
          value |= (1 << 24);
10697
0
        }
10698
0
    }
10699
0
      }
10700
0
    }
10701
0
    break;
10702
10703
0
  case R_ARM_ABS32:
10704
0
    value += addend;
10705
0
    if (branch_type == ST_BRANCH_TO_THUMB)
10706
0
      value |= 1;
10707
0
    break;
10708
10709
0
  case R_ARM_ABS32_NOI:
10710
0
    value += addend;
10711
0
    break;
10712
10713
0
  case R_ARM_REL32:
10714
0
    value += addend;
10715
0
    if (branch_type == ST_BRANCH_TO_THUMB)
10716
0
      value |= 1;
10717
0
    value -= (input_section->output_section->vma
10718
0
        + input_section->output_offset + rel->r_offset);
10719
0
    break;
10720
10721
0
  case R_ARM_REL32_NOI:
10722
0
    value += addend;
10723
0
    value -= (input_section->output_section->vma
10724
0
        + input_section->output_offset + rel->r_offset);
10725
0
    break;
10726
10727
0
  case R_ARM_PREL31:
10728
0
    value -= (input_section->output_section->vma
10729
0
        + input_section->output_offset + rel->r_offset);
10730
0
    value += signed_addend;
10731
0
    if (! h || h->root.type != bfd_link_hash_undefweak)
10732
0
      {
10733
        /* Check for overflow.  */
10734
0
        if ((value ^ (value >> 1)) & (1 << 30))
10735
0
    return bfd_reloc_overflow;
10736
0
      }
10737
0
    value &= 0x7fffffff;
10738
0
    value |= (bfd_get_32 (input_bfd, hit_data) & 0x80000000);
10739
0
    if (branch_type == ST_BRANCH_TO_THUMB)
10740
0
      value |= 1;
10741
0
    break;
10742
0
  }
10743
10744
0
      bfd_put_32 (input_bfd, value, hit_data);
10745
0
      return bfd_reloc_ok;
10746
10747
0
    case R_ARM_ABS8:
10748
0
      value += addend;
10749
10750
      /* There is no way to tell whether the user intended to use a signed or
10751
   unsigned addend.  When checking for overflow we accept either,
10752
   as specified by the AAELF.  */
10753
0
      if ((long) value > 0xff || (long) value < -0x80)
10754
0
  return bfd_reloc_overflow;
10755
10756
0
      bfd_put_8 (input_bfd, value, hit_data);
10757
0
      return bfd_reloc_ok;
10758
10759
0
    case R_ARM_ABS16:
10760
0
      value += addend;
10761
10762
      /* See comment for R_ARM_ABS8.  */
10763
0
      if ((long) value > 0xffff || (long) value < -0x8000)
10764
0
  return bfd_reloc_overflow;
10765
10766
0
      bfd_put_16 (input_bfd, value, hit_data);
10767
0
      return bfd_reloc_ok;
10768
10769
0
    case R_ARM_THM_ABS5:
10770
      /* Support ldr and str instructions for the thumb.  */
10771
0
      if (globals->use_rel)
10772
0
  {
10773
    /* Need to refetch addend.  */
10774
0
    addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
10775
    /* ??? Need to determine shift amount from operand size.  */
10776
0
    addend >>= howto->rightshift;
10777
0
  }
10778
0
      value += addend;
10779
10780
      /* ??? Isn't value unsigned?  */
10781
0
      if ((long) value > 0x1f || (long) value < -0x10)
10782
0
  return bfd_reloc_overflow;
10783
10784
      /* ??? Value needs to be properly shifted into place first.  */
10785
0
      value |= bfd_get_16 (input_bfd, hit_data) & 0xf83f;
10786
0
      bfd_put_16 (input_bfd, value, hit_data);
10787
0
      return bfd_reloc_ok;
10788
10789
0
    case R_ARM_THM_ALU_PREL_11_0:
10790
      /* Corresponds to: addw.w reg, pc, #offset (and similarly for subw).  */
10791
0
      {
10792
0
  bfd_vma insn;
10793
0
  bfd_signed_vma relocation;
10794
10795
0
  insn = (bfd_get_16 (input_bfd, hit_data) << 16)
10796
0
       | bfd_get_16 (input_bfd, hit_data + 2);
10797
10798
0
  if (globals->use_rel)
10799
0
    {
10800
0
      signed_addend = (insn & 0xff) | ((insn & 0x7000) >> 4)
10801
0
        | ((insn & (1 << 26)) >> 15);
10802
0
      if (insn & 0xf00000)
10803
0
        signed_addend = -signed_addend;
10804
0
    }
10805
10806
0
  relocation = value + signed_addend;
10807
0
  relocation -= Pa (input_section->output_section->vma
10808
0
        + input_section->output_offset
10809
0
        + rel->r_offset);
10810
10811
  /* PR 21523: Use an absolute value.  The user of this reloc will
10812
     have already selected an ADD or SUB insn appropriately.  */
10813
0
  value = llabs (relocation);
10814
10815
0
  if (value >= 0x1000)
10816
0
    return bfd_reloc_overflow;
10817
10818
  /* Destination is Thumb.  Force bit 0 to 1 to reflect this.  */
10819
0
  if (branch_type == ST_BRANCH_TO_THUMB)
10820
0
    value |= 1;
10821
10822
0
  insn = (insn & 0xfb0f8f00) | (value & 0xff)
10823
0
       | ((value & 0x700) << 4)
10824
0
       | ((value & 0x800) << 15);
10825
0
  if (relocation < 0)
10826
0
    insn |= 0xa00000;
10827
10828
0
  bfd_put_16 (input_bfd, insn >> 16, hit_data);
10829
0
  bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
10830
10831
0
  return bfd_reloc_ok;
10832
0
      }
10833
10834
0
    case R_ARM_THM_PC8:
10835
      /* PR 10073:  This reloc is not generated by the GNU toolchain,
10836
   but it is supported for compatibility with third party libraries
10837
   generated by other compilers, specifically the ARM/IAR.  */
10838
0
      {
10839
0
  bfd_vma insn;
10840
0
  bfd_signed_vma relocation;
10841
10842
0
  insn = bfd_get_16 (input_bfd, hit_data);
10843
10844
0
  if (globals->use_rel)
10845
0
    addend = ((((insn & 0x00ff) << 2) + 4) & 0x3ff) -4;
10846
10847
0
  relocation = value + addend;
10848
0
  relocation -= Pa (input_section->output_section->vma
10849
0
        + input_section->output_offset
10850
0
        + rel->r_offset);
10851
10852
0
  value = relocation;
10853
10854
  /* We do not check for overflow of this reloc.  Although strictly
10855
     speaking this is incorrect, it appears to be necessary in order
10856
     to work with IAR generated relocs.  Since GCC and GAS do not
10857
     generate R_ARM_THM_PC8 relocs, the lack of a check should not be
10858
     a problem for them.  */
10859
0
  value &= 0x3fc;
10860
10861
0
  insn = (insn & 0xff00) | (value >> 2);
10862
10863
0
  bfd_put_16 (input_bfd, insn, hit_data);
10864
10865
0
  return bfd_reloc_ok;
10866
0
      }
10867
10868
0
    case R_ARM_THM_PC12:
10869
      /* Corresponds to: ldr.w reg, [pc, #offset].  */
10870
0
      {
10871
0
  bfd_vma insn;
10872
0
  bfd_signed_vma relocation;
10873
10874
0
  insn = (bfd_get_16 (input_bfd, hit_data) << 16)
10875
0
       | bfd_get_16 (input_bfd, hit_data + 2);
10876
10877
0
  if (globals->use_rel)
10878
0
    {
10879
0
      signed_addend = insn & 0xfff;
10880
0
      if (!(insn & (1 << 23)))
10881
0
        signed_addend = -signed_addend;
10882
0
    }
10883
10884
0
  relocation = value + signed_addend;
10885
0
  relocation -= Pa (input_section->output_section->vma
10886
0
        + input_section->output_offset
10887
0
        + rel->r_offset);
10888
10889
0
  value = relocation;
10890
10891
0
  if (value >= 0x1000)
10892
0
    return bfd_reloc_overflow;
10893
10894
0
  insn = (insn & 0xff7ff000) | value;
10895
0
  if (relocation >= 0)
10896
0
    insn |= (1 << 23);
10897
10898
0
  bfd_put_16 (input_bfd, insn >> 16, hit_data);
10899
0
  bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
10900
10901
0
  return bfd_reloc_ok;
10902
0
      }
10903
10904
0
    case R_ARM_THM_XPC22:
10905
0
    case R_ARM_THM_CALL:
10906
0
    case R_ARM_THM_JUMP24:
10907
      /* Thumb BL (branch long instruction).  */
10908
0
      {
10909
0
  bfd_vma relocation;
10910
0
  bfd_vma reloc_sign;
10911
0
  bool overflow = false;
10912
0
  bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
10913
0
  bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
10914
0
  bfd_signed_vma reloc_signed_max;
10915
0
  bfd_signed_vma reloc_signed_min;
10916
0
  bfd_vma check;
10917
0
  bfd_signed_vma signed_check;
10918
0
  int bitsize;
10919
0
  const int thumb2 = using_thumb2 (globals);
10920
0
  const int thumb2_bl = using_thumb2_bl (globals);
10921
10922
  /* A branch to an undefined weak symbol is turned into a jump to
10923
     the next instruction unless a PLT entry will be created.
10924
     The jump to the next instruction is optimized as a NOP.W for
10925
     Thumb-2 enabled architectures.  */
10926
0
  if (h && h->root.type == bfd_link_hash_undefweak
10927
0
      && plt_offset == (bfd_vma) -1)
10928
0
    {
10929
0
      if (thumb2)
10930
0
        {
10931
0
    bfd_put_16 (input_bfd, 0xf3af, hit_data);
10932
0
    bfd_put_16 (input_bfd, 0x8000, hit_data + 2);
10933
0
        }
10934
0
      else
10935
0
        {
10936
0
    bfd_put_16 (input_bfd, 0xe000, hit_data);
10937
0
    bfd_put_16 (input_bfd, 0xbf00, hit_data + 2);
10938
0
        }
10939
0
      return bfd_reloc_ok;
10940
0
    }
10941
10942
  /* Fetch the addend.  We use the Thumb-2 encoding (backwards compatible
10943
     with Thumb-1) involving the J1 and J2 bits.  */
10944
0
  if (globals->use_rel)
10945
0
    {
10946
0
      bfd_vma s = (upper_insn & (1 << 10)) >> 10;
10947
0
      bfd_vma upper = upper_insn & 0x3ff;
10948
0
      bfd_vma lower = lower_insn & 0x7ff;
10949
0
      bfd_vma j1 = (lower_insn & (1 << 13)) >> 13;
10950
0
      bfd_vma j2 = (lower_insn & (1 << 11)) >> 11;
10951
0
      bfd_vma i1 = j1 ^ s ? 0 : 1;
10952
0
      bfd_vma i2 = j2 ^ s ? 0 : 1;
10953
10954
0
      addend = (i1 << 23) | (i2 << 22) | (upper << 12) | (lower << 1);
10955
      /* Sign extend.  */
10956
0
      addend = (addend | ((s ? 0 : 1) << 24)) - (1 << 24);
10957
10958
0
      signed_addend = addend;
10959
0
    }
10960
10961
0
  if (r_type == R_ARM_THM_XPC22)
10962
0
    {
10963
      /* Check for Thumb to Thumb call.  */
10964
      /* FIXME: Should we translate the instruction into a BL
10965
         instruction instead ?  */
10966
0
      if (branch_type == ST_BRANCH_TO_THUMB)
10967
0
        _bfd_error_handler
10968
0
    (_("%pB: warning: %s BLX instruction targets"
10969
0
       " %s function '%s'"),
10970
0
     input_bfd, "Thumb",
10971
0
     "Thumb", h ? h->root.root.string : "(local)");
10972
0
    }
10973
0
  else
10974
0
    {
10975
      /* If it is not a call to Thumb, assume call to Arm.
10976
         If it is a call relative to a section name, then it is not a
10977
         function call at all, but rather a long jump.  Calls through
10978
         the PLT do not require stubs.  */
10979
0
      if (branch_type == ST_BRANCH_TO_ARM && plt_offset == (bfd_vma) -1)
10980
0
        {
10981
0
    if (globals->use_blx && r_type == R_ARM_THM_CALL)
10982
0
      {
10983
        /* Convert BL to BLX.  */
10984
0
        lower_insn = (lower_insn & ~0x1000) | 0x0800;
10985
0
      }
10986
0
    else if ((   r_type != R_ARM_THM_CALL)
10987
0
       && (r_type != R_ARM_THM_JUMP24))
10988
0
      {
10989
0
        if (elf32_thumb_to_arm_stub
10990
0
      (info, sym_name, input_bfd, output_bfd, input_section,
10991
0
       hit_data, sym_sec, rel->r_offset, signed_addend, value,
10992
0
       error_message))
10993
0
          return bfd_reloc_ok;
10994
0
        else
10995
0
          return bfd_reloc_dangerous;
10996
0
      }
10997
0
        }
10998
0
      else if (branch_type == ST_BRANCH_TO_THUMB
10999
0
         && globals->use_blx
11000
0
         && r_type == R_ARM_THM_CALL)
11001
0
        {
11002
    /* Make sure this is a BL.  */
11003
0
    lower_insn |= 0x1800;
11004
0
        }
11005
0
    }
11006
11007
0
  enum elf32_arm_stub_type stub_type = arm_stub_none;
11008
0
  if (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24)
11009
0
    {
11010
      /* Check if a stub has to be inserted because the destination
11011
         is too far.  */
11012
0
      struct elf32_arm_stub_hash_entry *stub_entry;
11013
0
      struct elf32_arm_link_hash_entry *hash;
11014
11015
0
      hash = (struct elf32_arm_link_hash_entry *) h;
11016
11017
0
      stub_type = arm_type_of_stub (info, input_section, rel,
11018
0
            st_type, &branch_type,
11019
0
            hash, value, sym_sec,
11020
0
            input_bfd, sym_name);
11021
11022
0
      if (stub_type != arm_stub_none)
11023
0
        {
11024
    /* The target is out of reach or we are changing modes, so
11025
       redirect the branch to the local stub for this
11026
       function.  */
11027
0
    stub_entry = elf32_arm_get_stub_entry (input_section,
11028
0
                   sym_sec, h,
11029
0
                   rel, globals,
11030
0
                   stub_type);
11031
0
    if (stub_entry != NULL)
11032
0
      {
11033
0
        value = (stub_entry->stub_offset
11034
0
           + stub_entry->stub_sec->output_offset
11035
0
           + stub_entry->stub_sec->output_section->vma);
11036
11037
0
        if (plt_offset != (bfd_vma) -1)
11038
0
          *unresolved_reloc_p = false;
11039
0
      }
11040
11041
    /* If this call becomes a call to Arm, force BLX.  */
11042
0
    if (globals->use_blx && (r_type == R_ARM_THM_CALL))
11043
0
      {
11044
0
        if ((stub_entry
11045
0
       && !arm_stub_is_thumb (stub_entry->stub_type))
11046
0
      || branch_type != ST_BRANCH_TO_THUMB)
11047
0
          lower_insn = (lower_insn & ~0x1000) | 0x0800;
11048
0
      }
11049
0
        }
11050
0
    }
11051
11052
  /* Handle calls via the PLT.  */
11053
0
  if (stub_type == arm_stub_none && plt_offset != (bfd_vma) -1)
11054
0
    {
11055
0
      value = (splt->output_section->vma
11056
0
         + splt->output_offset
11057
0
         + plt_offset);
11058
11059
0
      if (globals->use_blx
11060
0
    && r_type == R_ARM_THM_CALL
11061
0
    && ! using_thumb_only (globals))
11062
0
        {
11063
    /* If the Thumb BLX instruction is available, convert
11064
       the BL to a BLX instruction to call the ARM-mode
11065
       PLT entry.  */
11066
0
    lower_insn = (lower_insn & ~0x1000) | 0x0800;
11067
0
    branch_type = ST_BRANCH_TO_ARM;
11068
0
        }
11069
0
      else
11070
0
        {
11071
0
    if (! using_thumb_only (globals))
11072
      /* Target the Thumb stub before the ARM PLT entry.  */
11073
0
      value -= PLT_THUMB_STUB_SIZE;
11074
0
    branch_type = ST_BRANCH_TO_THUMB;
11075
0
        }
11076
0
      *unresolved_reloc_p = false;
11077
0
    }
11078
11079
0
  relocation = value + signed_addend;
11080
11081
0
  relocation -= (input_section->output_section->vma
11082
0
           + input_section->output_offset
11083
0
           + rel->r_offset);
11084
11085
0
  check = relocation >> howto->rightshift;
11086
11087
  /* If this is a signed value, the rightshift just dropped
11088
     leading 1 bits (assuming twos complement).  */
11089
0
  if ((bfd_signed_vma) relocation >= 0)
11090
0
    signed_check = check;
11091
0
  else
11092
0
    signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
11093
11094
  /* Calculate the permissable maximum and minimum values for
11095
     this relocation according to whether we're relocating for
11096
     Thumb-2 or not.  */
11097
0
  bitsize = howto->bitsize;
11098
0
  if (!thumb2_bl)
11099
0
    bitsize -= 2;
11100
0
  reloc_signed_max = (1 << (bitsize - 1)) - 1;
11101
0
  reloc_signed_min = ~reloc_signed_max;
11102
11103
  /* Assumes two's complement.  */
11104
0
  if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
11105
0
    overflow = true;
11106
11107
0
  if ((lower_insn & 0x5000) == 0x4000)
11108
    /* For a BLX instruction, make sure that the relocation is rounded up
11109
       to a word boundary.  This follows the semantics of the instruction
11110
       which specifies that bit 1 of the target address will come from bit
11111
       1 of the base address.  */
11112
0
    relocation = (relocation + 2) & ~ 3;
11113
11114
  /* Put RELOCATION back into the insn.  Assumes two's complement.
11115
     We use the Thumb-2 encoding, which is safe even if dealing with
11116
     a Thumb-1 instruction by virtue of our overflow check above.  */
11117
0
  reloc_sign = (signed_check < 0) ? 1 : 0;
11118
0
  upper_insn = (upper_insn & ~(bfd_vma) 0x7ff)
11119
0
         | ((relocation >> 12) & 0x3ff)
11120
0
         | (reloc_sign << 10);
11121
0
  lower_insn = (lower_insn & ~(bfd_vma) 0x2fff)
11122
0
         | (((!((relocation >> 23) & 1)) ^ reloc_sign) << 13)
11123
0
         | (((!((relocation >> 22) & 1)) ^ reloc_sign) << 11)
11124
0
         | ((relocation >> 1) & 0x7ff);
11125
11126
  /* Put the relocated value back in the object file:  */
11127
0
  bfd_put_16 (input_bfd, upper_insn, hit_data);
11128
0
  bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
11129
11130
0
  return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
11131
0
      }
11132
0
      break;
11133
11134
0
    case R_ARM_THM_JUMP19:
11135
      /* Thumb32 conditional branch instruction.  */
11136
0
      {
11137
0
  bfd_vma relocation;
11138
0
  bool overflow = false;
11139
0
  bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
11140
0
  bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
11141
0
  bfd_signed_vma reloc_signed_max = 0xffffe;
11142
0
  bfd_signed_vma reloc_signed_min = -0x100000;
11143
0
  bfd_signed_vma signed_check;
11144
0
  enum elf32_arm_stub_type stub_type = arm_stub_none;
11145
0
  struct elf32_arm_stub_hash_entry *stub_entry;
11146
0
  struct elf32_arm_link_hash_entry *hash;
11147
11148
  /* Need to refetch the addend, reconstruct the top three bits,
11149
     and squish the two 11 bit pieces together.  */
11150
0
  if (globals->use_rel)
11151
0
    {
11152
0
      bfd_vma S     = (upper_insn & 0x0400) >> 10;
11153
0
      bfd_vma upper = (upper_insn & 0x003f);
11154
0
      bfd_vma J1    = (lower_insn & 0x2000) >> 13;
11155
0
      bfd_vma J2    = (lower_insn & 0x0800) >> 11;
11156
0
      bfd_vma lower = (lower_insn & 0x07ff);
11157
11158
0
      upper |= J1 << 6;
11159
0
      upper |= J2 << 7;
11160
0
      upper |= (!S) << 8;
11161
0
      upper -= 0x0100; /* Sign extend.  */
11162
11163
0
      addend = (upper << 12) | (lower << 1);
11164
0
      signed_addend = addend;
11165
0
    }
11166
11167
  /* Handle calls via the PLT.  */
11168
0
  if (plt_offset != (bfd_vma) -1)
11169
0
    {
11170
0
      value = (splt->output_section->vma
11171
0
         + splt->output_offset
11172
0
         + plt_offset);
11173
      /* Target the Thumb stub before the ARM PLT entry.  */
11174
0
      value -= PLT_THUMB_STUB_SIZE;
11175
0
      *unresolved_reloc_p = false;
11176
0
    }
11177
11178
0
  hash = (struct elf32_arm_link_hash_entry *)h;
11179
11180
0
  stub_type = arm_type_of_stub (info, input_section, rel,
11181
0
              st_type, &branch_type,
11182
0
              hash, value, sym_sec,
11183
0
              input_bfd, sym_name);
11184
0
  if (stub_type != arm_stub_none)
11185
0
    {
11186
0
      stub_entry = elf32_arm_get_stub_entry (input_section,
11187
0
               sym_sec, h,
11188
0
               rel, globals,
11189
0
               stub_type);
11190
0
      if (stub_entry != NULL)
11191
0
        {
11192
0
    value = (stub_entry->stub_offset
11193
0
      + stub_entry->stub_sec->output_offset
11194
0
      + stub_entry->stub_sec->output_section->vma);
11195
0
        }
11196
0
    }
11197
11198
0
  relocation = value + signed_addend;
11199
0
  relocation -= (input_section->output_section->vma
11200
0
           + input_section->output_offset
11201
0
           + rel->r_offset);
11202
0
  signed_check = (bfd_signed_vma) relocation;
11203
11204
0
  if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
11205
0
    overflow = true;
11206
11207
  /* Put RELOCATION back into the insn.  */
11208
0
  {
11209
0
    bfd_vma S  = (relocation & 0x00100000) >> 20;
11210
0
    bfd_vma J2 = (relocation & 0x00080000) >> 19;
11211
0
    bfd_vma J1 = (relocation & 0x00040000) >> 18;
11212
0
    bfd_vma hi = (relocation & 0x0003f000) >> 12;
11213
0
    bfd_vma lo = (relocation & 0x00000ffe) >>  1;
11214
11215
0
    upper_insn = (upper_insn & 0xfbc0) | (S << 10) | hi;
11216
0
    lower_insn = (lower_insn & 0xd000) | (J1 << 13) | (J2 << 11) | lo;
11217
0
  }
11218
11219
  /* Put the relocated value back in the object file:  */
11220
0
  bfd_put_16 (input_bfd, upper_insn, hit_data);
11221
0
  bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
11222
11223
0
  return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
11224
0
      }
11225
11226
0
    case R_ARM_THM_JUMP11:
11227
0
    case R_ARM_THM_JUMP8:
11228
0
    case R_ARM_THM_JUMP6:
11229
      /* Thumb B (branch) instruction).  */
11230
0
      {
11231
0
  bfd_signed_vma relocation;
11232
0
  bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
11233
0
  bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
11234
0
  bfd_signed_vma signed_check;
11235
11236
  /* CZB cannot jump backward.  */
11237
0
  if (r_type == R_ARM_THM_JUMP6)
11238
0
    {
11239
0
      reloc_signed_min = 0;
11240
0
      if (globals->use_rel)
11241
0
        signed_addend = ((addend & 0x200) >> 3) | ((addend & 0xf8) >> 2);
11242
0
    }
11243
11244
0
  relocation = value + signed_addend;
11245
11246
0
  relocation -= (input_section->output_section->vma
11247
0
           + input_section->output_offset
11248
0
           + rel->r_offset);
11249
11250
0
  relocation >>= howto->rightshift;
11251
0
  signed_check = relocation;
11252
11253
0
  if (r_type == R_ARM_THM_JUMP6)
11254
0
    relocation = ((relocation & 0x0020) << 4) | ((relocation & 0x001f) << 3);
11255
0
  else
11256
0
    relocation &= howto->dst_mask;
11257
0
  relocation |= (bfd_get_16 (input_bfd, hit_data) & (~ howto->dst_mask));
11258
11259
0
  bfd_put_16 (input_bfd, relocation, hit_data);
11260
11261
  /* Assumes two's complement.  */
11262
0
  if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
11263
0
    return bfd_reloc_overflow;
11264
11265
0
  return bfd_reloc_ok;
11266
0
      }
11267
11268
0
    case R_ARM_ALU_PCREL7_0:
11269
0
    case R_ARM_ALU_PCREL15_8:
11270
0
    case R_ARM_ALU_PCREL23_15:
11271
0
      {
11272
0
  bfd_vma insn;
11273
0
  bfd_vma relocation;
11274
11275
0
  insn = bfd_get_32 (input_bfd, hit_data);
11276
0
  if (globals->use_rel)
11277
0
    {
11278
      /* Extract the addend.  */
11279
0
      addend = (insn & 0xff) << ((insn & 0xf00) >> 7);
11280
0
      signed_addend = addend;
11281
0
    }
11282
0
  relocation = value + signed_addend;
11283
11284
0
  relocation -= (input_section->output_section->vma
11285
0
           + input_section->output_offset
11286
0
           + rel->r_offset);
11287
0
  insn = (insn & ~0xfff)
11288
0
         | ((howto->bitpos << 7) & 0xf00)
11289
0
         | ((relocation >> howto->bitpos) & 0xff);
11290
0
  bfd_put_32 (input_bfd, value, hit_data);
11291
0
      }
11292
0
      return bfd_reloc_ok;
11293
11294
0
    case R_ARM_GNU_VTINHERIT:
11295
0
    case R_ARM_GNU_VTENTRY:
11296
0
      return bfd_reloc_ok;
11297
11298
0
    case R_ARM_GOTOFF32:
11299
      /* Relocation is relative to the start of the
11300
   global offset table.  */
11301
11302
0
      BFD_ASSERT (sgot != NULL);
11303
0
      if (sgot == NULL)
11304
0
  return bfd_reloc_notsupported;
11305
11306
      /* If we are addressing a Thumb function, we need to adjust the
11307
   address by one, so that attempts to call the function pointer will
11308
   correctly interpret it as Thumb code.  */
11309
0
      if (branch_type == ST_BRANCH_TO_THUMB)
11310
0
  value += 1;
11311
11312
      /* Note that sgot->output_offset is not involved in this
11313
   calculation.  We always want the start of .got.  If we
11314
   define _GLOBAL_OFFSET_TABLE in a different way, as is
11315
   permitted by the ABI, we might have to change this
11316
   calculation.  */
11317
0
      value -= sgot->output_section->vma;
11318
0
      return _bfd_final_link_relocate (howto, input_bfd, input_section,
11319
0
               contents, rel->r_offset, value,
11320
0
               rel->r_addend);
11321
11322
0
    case R_ARM_GOTPC:
11323
      /* Use global offset table as symbol value.  */
11324
0
      BFD_ASSERT (sgot != NULL);
11325
11326
0
      if (sgot == NULL)
11327
0
  return bfd_reloc_notsupported;
11328
11329
0
      *unresolved_reloc_p = false;
11330
0
      value = sgot->output_section->vma;
11331
0
      return _bfd_final_link_relocate (howto, input_bfd, input_section,
11332
0
               contents, rel->r_offset, value,
11333
0
               rel->r_addend);
11334
11335
0
    case R_ARM_GOT32:
11336
0
    case R_ARM_GOT_PREL:
11337
      /* Relocation is to the entry for this symbol in the
11338
   global offset table.  */
11339
0
      if (sgot == NULL)
11340
0
  return bfd_reloc_notsupported;
11341
11342
0
      if (dynreloc_st_type == STT_GNU_IFUNC
11343
0
    && plt_offset != (bfd_vma) -1
11344
0
    && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)))
11345
0
  {
11346
    /* We have a relocation against a locally-binding STT_GNU_IFUNC
11347
       symbol, and the relocation resolves directly to the runtime
11348
       target rather than to the .iplt entry.  This means that any
11349
       .got entry would be the same value as the .igot.plt entry,
11350
       so there's no point creating both.  */
11351
0
    sgot = globals->root.igotplt;
11352
0
    value = sgot->output_offset + gotplt_offset;
11353
0
  }
11354
0
      else if (h != NULL)
11355
0
  {
11356
0
    bfd_vma off;
11357
11358
0
    off = h->got.offset;
11359
0
    BFD_ASSERT (off != (bfd_vma) -1);
11360
0
    if ((off & 1) != 0)
11361
0
      {
11362
        /* We have already processsed one GOT relocation against
11363
     this symbol.  */
11364
0
        off &= ~1;
11365
0
        if (globals->root.dynamic_sections_created
11366
0
      && !SYMBOL_REFERENCES_LOCAL (info, h))
11367
0
    *unresolved_reloc_p = false;
11368
0
      }
11369
0
    else
11370
0
      {
11371
0
        Elf_Internal_Rela outrel;
11372
0
        int isrofixup = 0;
11373
11374
0
        if (((h->dynindx != -1) || globals->fdpic_p)
11375
0
      && !SYMBOL_REFERENCES_LOCAL (info, h))
11376
0
    {
11377
      /* If the symbol doesn't resolve locally in a static
11378
         object, we have an undefined reference.  If the
11379
         symbol doesn't resolve locally in a dynamic object,
11380
         it should be resolved by the dynamic linker.  */
11381
0
      if (globals->root.dynamic_sections_created)
11382
0
        {
11383
0
          outrel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
11384
0
          *unresolved_reloc_p = false;
11385
0
        }
11386
0
      else
11387
0
        outrel.r_info = 0;
11388
0
      outrel.r_addend = 0;
11389
0
    }
11390
0
        else
11391
0
    {
11392
0
      if (dynreloc_st_type == STT_GNU_IFUNC)
11393
0
        outrel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
11394
0
      else if (bfd_link_pic (info)
11395
0
         && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
11396
0
        outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
11397
0
      else
11398
0
        {
11399
0
          outrel.r_info = 0;
11400
0
          if (globals->fdpic_p)
11401
0
      isrofixup = 1;
11402
0
        }
11403
0
      outrel.r_addend = dynreloc_value;
11404
0
    }
11405
11406
        /* The GOT entry is initialized to zero by default.
11407
     See if we should install a different value.  */
11408
0
        if (outrel.r_addend != 0
11409
0
      && (globals->use_rel || outrel.r_info == 0))
11410
0
    {
11411
0
      bfd_put_32 (output_bfd, outrel.r_addend,
11412
0
            sgot->contents + off);
11413
0
      outrel.r_addend = 0;
11414
0
    }
11415
11416
0
        if (isrofixup)
11417
0
    arm_elf_add_rofixup (output_bfd,
11418
0
             elf32_arm_hash_table (info)->srofixup,
11419
0
             sgot->output_section->vma
11420
0
             + sgot->output_offset + off);
11421
11422
0
        else if (outrel.r_info != 0)
11423
0
    {
11424
0
      outrel.r_offset = (sgot->output_section->vma
11425
0
             + sgot->output_offset
11426
0
             + off);
11427
0
      elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
11428
0
    }
11429
11430
0
        h->got.offset |= 1;
11431
0
      }
11432
0
    value = sgot->output_offset + off;
11433
0
  }
11434
0
      else
11435
0
  {
11436
0
    bfd_vma off;
11437
11438
0
    BFD_ASSERT (local_got_offsets != NULL
11439
0
          && local_got_offsets[r_symndx] != (bfd_vma) -1);
11440
11441
0
    off = local_got_offsets[r_symndx];
11442
11443
    /* The offset must always be a multiple of 4.  We use the
11444
       least significant bit to record whether we have already
11445
       generated the necessary reloc.  */
11446
0
    if ((off & 1) != 0)
11447
0
      off &= ~1;
11448
0
    else
11449
0
      {
11450
0
        Elf_Internal_Rela outrel;
11451
0
        int isrofixup = 0;
11452
11453
0
        if (dynreloc_st_type == STT_GNU_IFUNC)
11454
0
    outrel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
11455
0
        else if (bfd_link_pic (info))
11456
0
    outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
11457
0
        else
11458
0
    {
11459
0
      outrel.r_info = 0;
11460
0
      if (globals->fdpic_p)
11461
0
        isrofixup = 1;
11462
0
    }
11463
11464
        /* The GOT entry is initialized to zero by default.
11465
     See if we should install a different value.  */
11466
0
        if (globals->use_rel || outrel.r_info == 0)
11467
0
    bfd_put_32 (output_bfd, dynreloc_value, sgot->contents + off);
11468
11469
0
        if (isrofixup)
11470
0
    arm_elf_add_rofixup (output_bfd,
11471
0
             globals->srofixup,
11472
0
             sgot->output_section->vma
11473
0
             + sgot->output_offset + off);
11474
11475
0
        else if (outrel.r_info != 0)
11476
0
    {
11477
0
      outrel.r_addend = addend + dynreloc_value;
11478
0
      outrel.r_offset = (sgot->output_section->vma
11479
0
             + sgot->output_offset
11480
0
             + off);
11481
0
      elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
11482
0
    }
11483
11484
0
        local_got_offsets[r_symndx] |= 1;
11485
0
      }
11486
11487
0
    value = sgot->output_offset + off;
11488
0
  }
11489
0
      if (r_type != R_ARM_GOT32)
11490
0
  value += sgot->output_section->vma;
11491
11492
0
      return _bfd_final_link_relocate (howto, input_bfd, input_section,
11493
0
               contents, rel->r_offset, value,
11494
0
               rel->r_addend);
11495
11496
0
    case R_ARM_TLS_LDO32:
11497
0
      value = value - dtpoff_base (info);
11498
11499
0
      return _bfd_final_link_relocate (howto, input_bfd, input_section,
11500
0
               contents, rel->r_offset, value,
11501
0
               rel->r_addend);
11502
11503
0
    case R_ARM_TLS_LDM32:
11504
0
    case R_ARM_TLS_LDM32_FDPIC:
11505
0
      {
11506
0
  bfd_vma off;
11507
11508
0
  if (sgot == NULL)
11509
0
    abort ();
11510
11511
0
  off = globals->tls_ldm_got.offset;
11512
11513
0
  if ((off & 1) != 0)
11514
0
    off &= ~1;
11515
0
  else
11516
0
    {
11517
      /* If we don't know the module number, create a relocation
11518
         for it.  */
11519
0
      if (bfd_link_dll (info))
11520
0
        {
11521
0
    Elf_Internal_Rela outrel;
11522
11523
0
    if (srelgot == NULL)
11524
0
      abort ();
11525
11526
0
    outrel.r_addend = 0;
11527
0
    outrel.r_offset = (sgot->output_section->vma
11528
0
           + sgot->output_offset + off);
11529
0
    outrel.r_info = ELF32_R_INFO (0, R_ARM_TLS_DTPMOD32);
11530
11531
0
    if (globals->use_rel)
11532
0
      bfd_put_32 (output_bfd, outrel.r_addend,
11533
0
            sgot->contents + off);
11534
11535
0
    elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
11536
0
        }
11537
0
      else
11538
0
        bfd_put_32 (output_bfd, 1, sgot->contents + off);
11539
11540
0
      globals->tls_ldm_got.offset |= 1;
11541
0
    }
11542
11543
0
  if (r_type == R_ARM_TLS_LDM32_FDPIC)
11544
0
    {
11545
0
      bfd_put_32 (output_bfd,
11546
0
      globals->root.sgot->output_offset + off,
11547
0
      contents + rel->r_offset);
11548
11549
0
      return bfd_reloc_ok;
11550
0
    }
11551
0
  else
11552
0
    {
11553
0
      value = sgot->output_section->vma + sgot->output_offset + off
11554
0
        - (input_section->output_section->vma
11555
0
     + input_section->output_offset + rel->r_offset);
11556
11557
0
      return _bfd_final_link_relocate (howto, input_bfd, input_section,
11558
0
               contents, rel->r_offset, value,
11559
0
               rel->r_addend);
11560
0
    }
11561
0
      }
11562
11563
0
    case R_ARM_TLS_CALL:
11564
0
    case R_ARM_THM_TLS_CALL:
11565
0
    case R_ARM_TLS_GD32:
11566
0
    case R_ARM_TLS_GD32_FDPIC:
11567
0
    case R_ARM_TLS_IE32:
11568
0
    case R_ARM_TLS_IE32_FDPIC:
11569
0
    case R_ARM_TLS_GOTDESC:
11570
0
    case R_ARM_TLS_DESCSEQ:
11571
0
    case R_ARM_THM_TLS_DESCSEQ:
11572
0
      {
11573
0
  bfd_vma off, offplt;
11574
0
  int indx = 0;
11575
0
  char tls_type;
11576
11577
0
  BFD_ASSERT (sgot != NULL);
11578
11579
0
  if (h != NULL)
11580
0
    {
11581
0
      bool dyn;
11582
0
      dyn = globals->root.dynamic_sections_created;
11583
0
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
11584
0
             bfd_link_pic (info),
11585
0
             h)
11586
0
    && (!bfd_link_pic (info)
11587
0
        || !SYMBOL_REFERENCES_LOCAL (info, h)))
11588
0
        {
11589
0
    *unresolved_reloc_p = false;
11590
0
    indx = h->dynindx;
11591
0
        }
11592
0
      off = h->got.offset;
11593
0
      offplt = elf32_arm_hash_entry (h)->tlsdesc_got;
11594
0
      tls_type = ((struct elf32_arm_link_hash_entry *) h)->tls_type;
11595
0
    }
11596
0
  else
11597
0
    {
11598
0
      BFD_ASSERT (local_got_offsets != NULL);
11599
11600
0
      if (r_symndx >= elf32_arm_num_entries (input_bfd))
11601
0
        {
11602
0
    _bfd_error_handler (_("\
11603
0
%pB: expected symbol index in range 0..%lu but found local symbol with index %lu"),
11604
0
            input_bfd,
11605
0
            (unsigned long) elf32_arm_num_entries (input_bfd),
11606
0
            r_symndx);
11607
0
    return false;
11608
0
        }
11609
0
      off = local_got_offsets[r_symndx];
11610
0
      offplt = local_tlsdesc_gotents[r_symndx];
11611
0
      tls_type = elf32_arm_local_got_tls_type (input_bfd)[r_symndx];
11612
0
    }
11613
11614
  /* Linker relaxations happens from one of the
11615
     R_ARM_{GOTDESC,CALL,DESCSEQ} relocations to IE or LE.  */
11616
0
  if (ELF32_R_TYPE (rel->r_info) != r_type)
11617
0
    tls_type = GOT_TLS_IE;
11618
11619
0
  BFD_ASSERT (tls_type != GOT_UNKNOWN);
11620
11621
0
  if ((off & 1) != 0)
11622
0
    off &= ~1;
11623
0
  else
11624
0
    {
11625
0
      bool need_relocs = false;
11626
0
      Elf_Internal_Rela outrel;
11627
0
      int cur_off = off;
11628
11629
      /* The GOT entries have not been initialized yet.  Do it
11630
         now, and emit any relocations.  If both an IE GOT and a
11631
         GD GOT are necessary, we emit the GD first.  */
11632
11633
0
      if ((bfd_link_dll (info) || indx != 0)
11634
0
    && (h == NULL
11635
0
        || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
11636
0
      && !resolved_to_zero)
11637
0
        || h->root.type != bfd_link_hash_undefweak))
11638
0
        {
11639
0
    need_relocs = true;
11640
0
    BFD_ASSERT (srelgot != NULL);
11641
0
        }
11642
11643
0
      if (tls_type & GOT_TLS_GDESC)
11644
0
        {
11645
0
    bfd_byte *loc;
11646
11647
    /* We should have relaxed, unless this is an undefined
11648
       weak symbol.  */
11649
0
    BFD_ASSERT ((h && (h->root.type == bfd_link_hash_undefweak))
11650
0
          || bfd_link_dll (info));
11651
0
    BFD_ASSERT (globals->sgotplt_jump_table_size + offplt + 8
11652
0
          <= globals->root.sgotplt->size);
11653
11654
0
    outrel.r_addend = 0;
11655
0
    outrel.r_offset = (globals->root.sgotplt->output_section->vma
11656
0
           + globals->root.sgotplt->output_offset
11657
0
           + offplt
11658
0
           + globals->sgotplt_jump_table_size);
11659
11660
0
    outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DESC);
11661
0
    sreloc = globals->root.srelplt;
11662
0
    loc = sreloc->contents;
11663
0
    loc += globals->next_tls_desc_index++ * RELOC_SIZE (globals);
11664
0
    BFD_ASSERT (loc + RELOC_SIZE (globals)
11665
0
         <= sreloc->contents + sreloc->size);
11666
11667
0
    SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
11668
11669
    /* For globals, the first word in the relocation gets
11670
       the relocation index and the top bit set, or zero,
11671
       if we're binding now.  For locals, it gets the
11672
       symbol's offset in the tls section.  */
11673
0
    bfd_put_32 (output_bfd,
11674
0
          !h ? value - elf_hash_table (info)->tls_sec->vma
11675
0
          : info->flags & DF_BIND_NOW ? 0
11676
0
          : 0x80000000 | ELF32_R_SYM (outrel.r_info),
11677
0
          globals->root.sgotplt->contents + offplt
11678
0
          + globals->sgotplt_jump_table_size);
11679
11680
    /* Second word in the relocation is always zero.  */
11681
0
    bfd_put_32 (output_bfd, 0,
11682
0
          globals->root.sgotplt->contents + offplt
11683
0
          + globals->sgotplt_jump_table_size + 4);
11684
0
        }
11685
0
      if (tls_type & GOT_TLS_GD)
11686
0
        {
11687
0
    if (need_relocs)
11688
0
      {
11689
0
        outrel.r_addend = 0;
11690
0
        outrel.r_offset = (sgot->output_section->vma
11691
0
               + sgot->output_offset
11692
0
               + cur_off);
11693
0
        outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DTPMOD32);
11694
11695
0
        if (globals->use_rel)
11696
0
          bfd_put_32 (output_bfd, outrel.r_addend,
11697
0
          sgot->contents + cur_off);
11698
11699
0
        elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
11700
11701
0
        if (indx == 0)
11702
0
          bfd_put_32 (output_bfd, value - dtpoff_base (info),
11703
0
          sgot->contents + cur_off + 4);
11704
0
        else
11705
0
          {
11706
0
      outrel.r_addend = 0;
11707
0
      outrel.r_info = ELF32_R_INFO (indx,
11708
0
                  R_ARM_TLS_DTPOFF32);
11709
0
      outrel.r_offset += 4;
11710
11711
0
      if (globals->use_rel)
11712
0
        bfd_put_32 (output_bfd, outrel.r_addend,
11713
0
              sgot->contents + cur_off + 4);
11714
11715
0
      elf32_arm_add_dynreloc (output_bfd, info,
11716
0
            srelgot, &outrel);
11717
0
          }
11718
0
      }
11719
0
    else
11720
0
      {
11721
        /* If we are not emitting relocations for a
11722
           general dynamic reference, then we must be in a
11723
           static link or an executable link with the
11724
           symbol binding locally.  Mark it as belonging
11725
           to module 1, the executable.  */
11726
0
        bfd_put_32 (output_bfd, 1,
11727
0
        sgot->contents + cur_off);
11728
0
        bfd_put_32 (output_bfd, value - dtpoff_base (info),
11729
0
        sgot->contents + cur_off + 4);
11730
0
      }
11731
11732
0
    cur_off += 8;
11733
0
        }
11734
11735
0
      if (tls_type & GOT_TLS_IE)
11736
0
        {
11737
0
    if (need_relocs)
11738
0
      {
11739
0
        if (indx == 0)
11740
0
          outrel.r_addend = value - dtpoff_base (info);
11741
0
        else
11742
0
          outrel.r_addend = 0;
11743
0
        outrel.r_offset = (sgot->output_section->vma
11744
0
               + sgot->output_offset
11745
0
               + cur_off);
11746
0
        outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_TPOFF32);
11747
11748
0
        if (globals->use_rel)
11749
0
          bfd_put_32 (output_bfd, outrel.r_addend,
11750
0
          sgot->contents + cur_off);
11751
11752
0
        elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
11753
0
      }
11754
0
    else
11755
0
      bfd_put_32 (output_bfd, tpoff (info, value),
11756
0
            sgot->contents + cur_off);
11757
0
    cur_off += 4;
11758
0
        }
11759
11760
0
      if (h != NULL)
11761
0
        h->got.offset |= 1;
11762
0
      else
11763
0
        local_got_offsets[r_symndx] |= 1;
11764
0
    }
11765
11766
0
  if ((tls_type & GOT_TLS_GD) && r_type != R_ARM_TLS_GD32 && r_type != R_ARM_TLS_GD32_FDPIC)
11767
0
    off += 8;
11768
0
  else if (tls_type & GOT_TLS_GDESC)
11769
0
    off = offplt;
11770
11771
0
  if (ELF32_R_TYPE (rel->r_info) == R_ARM_TLS_CALL
11772
0
      || ELF32_R_TYPE (rel->r_info) == R_ARM_THM_TLS_CALL)
11773
0
    {
11774
0
      bfd_signed_vma offset;
11775
      /* TLS stubs are arm mode.  The original symbol is a
11776
         data object, so branch_type is bogus.  */
11777
0
      branch_type = ST_BRANCH_TO_ARM;
11778
0
      enum elf32_arm_stub_type stub_type
11779
0
        = arm_type_of_stub (info, input_section, rel,
11780
0
          st_type, &branch_type,
11781
0
          (struct elf32_arm_link_hash_entry *)h,
11782
0
          globals->tls_trampoline, globals->root.splt,
11783
0
          input_bfd, sym_name);
11784
11785
0
      if (stub_type != arm_stub_none)
11786
0
        {
11787
0
    struct elf32_arm_stub_hash_entry *stub_entry
11788
0
      = elf32_arm_get_stub_entry
11789
0
      (input_section, globals->root.splt, 0, rel,
11790
0
       globals, stub_type);
11791
0
    offset = (stub_entry->stub_offset
11792
0
        + stub_entry->stub_sec->output_offset
11793
0
        + stub_entry->stub_sec->output_section->vma);
11794
0
        }
11795
0
      else
11796
0
        offset = (globals->root.splt->output_section->vma
11797
0
      + globals->root.splt->output_offset
11798
0
      + globals->tls_trampoline);
11799
11800
0
      if (ELF32_R_TYPE (rel->r_info) == R_ARM_TLS_CALL)
11801
0
        {
11802
0
    unsigned long inst;
11803
11804
0
    offset -= (input_section->output_section->vma
11805
0
         + input_section->output_offset
11806
0
         + rel->r_offset + 8);
11807
11808
0
    inst = offset >> 2;
11809
0
    inst &= 0x00ffffff;
11810
0
    value = inst | (globals->use_blx ? 0xfa000000 : 0xeb000000);
11811
0
        }
11812
0
      else
11813
0
        {
11814
    /* Thumb blx encodes the offset in a complicated
11815
       fashion.  */
11816
0
    unsigned upper_insn, lower_insn;
11817
0
    unsigned neg;
11818
11819
0
    offset -= (input_section->output_section->vma
11820
0
         + input_section->output_offset
11821
0
         + rel->r_offset + 4);
11822
11823
0
    if (stub_type != arm_stub_none
11824
0
        && arm_stub_is_thumb (stub_type))
11825
0
      {
11826
0
        lower_insn = 0xd000;
11827
0
      }
11828
0
    else
11829
0
      {
11830
0
        lower_insn = 0xc000;
11831
        /* Round up the offset to a word boundary.  */
11832
0
        offset = (offset + 2) & ~2;
11833
0
      }
11834
11835
0
    neg = offset < 0;
11836
0
    upper_insn = (0xf000
11837
0
            | ((offset >> 12) & 0x3ff)
11838
0
            | (neg << 10));
11839
0
    lower_insn |= (((!((offset >> 23) & 1)) ^ neg) << 13)
11840
0
            | (((!((offset >> 22) & 1)) ^ neg) << 11)
11841
0
            | ((offset >> 1) & 0x7ff);
11842
0
    bfd_put_16 (input_bfd, upper_insn, hit_data);
11843
0
    bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
11844
0
    return bfd_reloc_ok;
11845
0
        }
11846
0
    }
11847
  /* These relocations needs special care, as besides the fact
11848
     they point somewhere in .gotplt, the addend must be
11849
     adjusted accordingly depending on the type of instruction
11850
     we refer to.  */
11851
0
  else if ((r_type == R_ARM_TLS_GOTDESC) && (tls_type & GOT_TLS_GDESC))
11852
0
    {
11853
0
      unsigned long data, insn;
11854
0
      unsigned thumb;
11855
11856
0
      data = bfd_get_signed_32 (input_bfd, hit_data);
11857
0
      thumb = data & 1;
11858
0
      data &= ~1ul;
11859
11860
0
      if (thumb)
11861
0
        {
11862
0
    insn = bfd_get_16 (input_bfd, contents + rel->r_offset - data);
11863
0
    if ((insn & 0xf000) == 0xf000 || (insn & 0xf800) == 0xe800)
11864
0
      insn = (insn << 16)
11865
0
        | bfd_get_16 (input_bfd,
11866
0
          contents + rel->r_offset - data + 2);
11867
0
    if ((insn & 0xf800c000) == 0xf000c000)
11868
      /* bl/blx */
11869
0
      value = -6;
11870
0
    else if ((insn & 0xffffff00) == 0x4400)
11871
      /* add */
11872
0
      value = -5;
11873
0
    else
11874
0
      {
11875
0
        _bfd_error_handler
11876
          /* xgettext:c-format */
11877
0
          (_("%pB(%pA+%#" PRIx64 "): "
11878
0
       "unexpected %s instruction '%#lx' "
11879
0
       "referenced by TLS_GOTDESC"),
11880
0
           input_bfd, input_section, (uint64_t) rel->r_offset,
11881
0
           "Thumb", insn);
11882
0
        return bfd_reloc_notsupported;
11883
0
      }
11884
0
        }
11885
0
      else
11886
0
        {
11887
0
    insn = bfd_get_32 (input_bfd, contents + rel->r_offset - data);
11888
11889
0
    switch (insn >> 24)
11890
0
      {
11891
0
      case 0xeb:  /* bl */
11892
0
      case 0xfa:  /* blx */
11893
0
        value = -4;
11894
0
        break;
11895
11896
0
      case 0xe0:  /* add */
11897
0
        value = -8;
11898
0
        break;
11899
11900
0
      default:
11901
0
        _bfd_error_handler
11902
          /* xgettext:c-format */
11903
0
          (_("%pB(%pA+%#" PRIx64 "): "
11904
0
       "unexpected %s instruction '%#lx' "
11905
0
       "referenced by TLS_GOTDESC"),
11906
0
           input_bfd, input_section, (uint64_t) rel->r_offset,
11907
0
           "ARM", insn);
11908
0
        return bfd_reloc_notsupported;
11909
0
      }
11910
0
        }
11911
11912
0
      value += ((globals->root.sgotplt->output_section->vma
11913
0
           + globals->root.sgotplt->output_offset + off)
11914
0
          - (input_section->output_section->vma
11915
0
       + input_section->output_offset
11916
0
       + rel->r_offset)
11917
0
          + globals->sgotplt_jump_table_size);
11918
0
    }
11919
0
  else
11920
0
    value = ((globals->root.sgot->output_section->vma
11921
0
        + globals->root.sgot->output_offset + off)
11922
0
       - (input_section->output_section->vma
11923
0
          + input_section->output_offset + rel->r_offset));
11924
11925
0
  if (globals->fdpic_p && (r_type == R_ARM_TLS_GD32_FDPIC ||
11926
0
         r_type == R_ARM_TLS_IE32_FDPIC))
11927
0
    {
11928
      /* For FDPIC relocations, resolve to the offset of the GOT
11929
         entry from the start of GOT.  */
11930
0
      bfd_put_32 (output_bfd,
11931
0
      globals->root.sgot->output_offset + off,
11932
0
      contents + rel->r_offset);
11933
11934
0
      return bfd_reloc_ok;
11935
0
    }
11936
0
  else
11937
0
    {
11938
0
      return _bfd_final_link_relocate (howto, input_bfd, input_section,
11939
0
               contents, rel->r_offset, value,
11940
0
               rel->r_addend);
11941
0
    }
11942
0
      }
11943
11944
0
    case R_ARM_TLS_LE32:
11945
0
      if (bfd_link_dll (info))
11946
0
  {
11947
0
    _bfd_error_handler
11948
      /* xgettext:c-format */
11949
0
      (_("%pB(%pA+%#" PRIx64 "): %s relocation not permitted "
11950
0
         "in shared object"),
11951
0
       input_bfd, input_section, (uint64_t) rel->r_offset, howto->name);
11952
0
    return bfd_reloc_notsupported;
11953
0
  }
11954
0
      else
11955
0
  value = tpoff (info, value);
11956
11957
0
      return _bfd_final_link_relocate (howto, input_bfd, input_section,
11958
0
               contents, rel->r_offset, value,
11959
0
               rel->r_addend);
11960
11961
0
    case R_ARM_V4BX:
11962
0
      if (globals->fix_v4bx)
11963
0
  {
11964
0
    bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
11965
11966
    /* Ensure that we have a BX instruction.  */
11967
0
    BFD_ASSERT ((insn & 0x0ffffff0) == 0x012fff10);
11968
11969
0
    if (globals->fix_v4bx == 2 && (insn & 0xf) != 0xf)
11970
0
      {
11971
        /* Branch to veneer.  */
11972
0
        bfd_vma glue_addr;
11973
0
        glue_addr = elf32_arm_bx_glue (info, insn & 0xf);
11974
0
        glue_addr -= input_section->output_section->vma
11975
0
         + input_section->output_offset
11976
0
         + rel->r_offset + 8;
11977
0
        insn = (insn & 0xf0000000) | 0x0a000000
11978
0
         | ((glue_addr >> 2) & 0x00ffffff);
11979
0
      }
11980
0
    else
11981
0
      {
11982
        /* Preserve Rm (lowest four bits) and the condition code
11983
     (highest four bits). Other bits encode MOV PC,Rm.  */
11984
0
        insn = (insn & 0xf000000f) | 0x01a0f000;
11985
0
      }
11986
11987
0
    bfd_put_32 (input_bfd, insn, hit_data);
11988
0
  }
11989
0
      return bfd_reloc_ok;
11990
11991
0
    case R_ARM_MOVW_ABS_NC:
11992
0
    case R_ARM_MOVT_ABS:
11993
0
    case R_ARM_MOVW_PREL_NC:
11994
0
    case R_ARM_MOVT_PREL:
11995
    /* Until we properly support segment-base-relative addressing then
11996
       we assume the segment base to be zero, as for the group relocations.
11997
       Thus R_ARM_MOVW_BREL_NC has the same semantics as R_ARM_MOVW_ABS_NC
11998
       and R_ARM_MOVT_BREL has the same semantics as R_ARM_MOVT_ABS.  */
11999
0
    case R_ARM_MOVW_BREL_NC:
12000
0
    case R_ARM_MOVW_BREL:
12001
0
    case R_ARM_MOVT_BREL:
12002
0
      {
12003
0
  bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12004
12005
0
  if (globals->use_rel)
12006
0
    {
12007
0
      addend = ((insn >> 4) & 0xf000) | (insn & 0xfff);
12008
0
      signed_addend = (addend ^ 0x8000) - 0x8000;
12009
0
    }
12010
12011
0
  value += signed_addend;
12012
12013
0
  if (r_type == R_ARM_MOVW_PREL_NC || r_type == R_ARM_MOVT_PREL)
12014
0
    value -= (input_section->output_section->vma
12015
0
        + input_section->output_offset + rel->r_offset);
12016
12017
0
  if (r_type == R_ARM_MOVW_BREL && value >= 0x10000)
12018
0
    return bfd_reloc_overflow;
12019
12020
0
  if (branch_type == ST_BRANCH_TO_THUMB)
12021
0
    value |= 1;
12022
12023
0
  if (r_type == R_ARM_MOVT_ABS || r_type == R_ARM_MOVT_PREL
12024
0
      || r_type == R_ARM_MOVT_BREL)
12025
0
    value >>= 16;
12026
12027
0
  insn &= 0xfff0f000;
12028
0
  insn |= value & 0xfff;
12029
0
  insn |= (value & 0xf000) << 4;
12030
0
  bfd_put_32 (input_bfd, insn, hit_data);
12031
0
      }
12032
0
      return bfd_reloc_ok;
12033
12034
0
    case R_ARM_THM_MOVW_ABS_NC:
12035
0
    case R_ARM_THM_MOVT_ABS:
12036
0
    case R_ARM_THM_MOVW_PREL_NC:
12037
0
    case R_ARM_THM_MOVT_PREL:
12038
    /* Until we properly support segment-base-relative addressing then
12039
       we assume the segment base to be zero, as for the above relocations.
12040
       Thus R_ARM_THM_MOVW_BREL_NC has the same semantics as
12041
       R_ARM_THM_MOVW_ABS_NC and R_ARM_THM_MOVT_BREL has the same semantics
12042
       as R_ARM_THM_MOVT_ABS.  */
12043
0
    case R_ARM_THM_MOVW_BREL_NC:
12044
0
    case R_ARM_THM_MOVW_BREL:
12045
0
    case R_ARM_THM_MOVT_BREL:
12046
0
      {
12047
0
  bfd_vma insn;
12048
12049
0
  insn = bfd_get_16 (input_bfd, hit_data) << 16;
12050
0
  insn |= bfd_get_16 (input_bfd, hit_data + 2);
12051
12052
0
  if (globals->use_rel)
12053
0
    {
12054
0
      addend = ((insn >> 4)  & 0xf000)
12055
0
       | ((insn >> 15) & 0x0800)
12056
0
       | ((insn >> 4)  & 0x0700)
12057
0
       | (insn     & 0x00ff);
12058
0
      signed_addend = (addend ^ 0x8000) - 0x8000;
12059
0
    }
12060
12061
0
  value += signed_addend;
12062
12063
0
  if (r_type == R_ARM_THM_MOVW_PREL_NC || r_type == R_ARM_THM_MOVT_PREL)
12064
0
    value -= (input_section->output_section->vma
12065
0
        + input_section->output_offset + rel->r_offset);
12066
12067
0
  if (r_type == R_ARM_THM_MOVW_BREL && value >= 0x10000)
12068
0
    return bfd_reloc_overflow;
12069
12070
0
  if (branch_type == ST_BRANCH_TO_THUMB)
12071
0
    value |= 1;
12072
12073
0
  if (r_type == R_ARM_THM_MOVT_ABS || r_type == R_ARM_THM_MOVT_PREL
12074
0
      || r_type == R_ARM_THM_MOVT_BREL)
12075
0
    value >>= 16;
12076
12077
0
  insn &= 0xfbf08f00;
12078
0
  insn |= (value & 0xf000) << 4;
12079
0
  insn |= (value & 0x0800) << 15;
12080
0
  insn |= (value & 0x0700) << 4;
12081
0
  insn |= (value & 0x00ff);
12082
12083
0
  bfd_put_16 (input_bfd, insn >> 16, hit_data);
12084
0
  bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
12085
0
      }
12086
0
      return bfd_reloc_ok;
12087
12088
0
    case R_ARM_ALU_PC_G0_NC:
12089
0
    case R_ARM_ALU_PC_G1_NC:
12090
0
    case R_ARM_ALU_PC_G0:
12091
0
    case R_ARM_ALU_PC_G1:
12092
0
    case R_ARM_ALU_PC_G2:
12093
0
    case R_ARM_ALU_SB_G0_NC:
12094
0
    case R_ARM_ALU_SB_G1_NC:
12095
0
    case R_ARM_ALU_SB_G0:
12096
0
    case R_ARM_ALU_SB_G1:
12097
0
    case R_ARM_ALU_SB_G2:
12098
0
      {
12099
0
  bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12100
0
  bfd_vma pc = input_section->output_section->vma
12101
0
         + input_section->output_offset + rel->r_offset;
12102
  /* sb is the origin of the *segment* containing the symbol.  */
12103
0
  bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
12104
0
  bfd_vma residual;
12105
0
  bfd_vma g_n;
12106
0
  bfd_signed_vma signed_value;
12107
0
  int group = 0;
12108
12109
  /* Determine which group of bits to select.  */
12110
0
  switch (r_type)
12111
0
    {
12112
0
    case R_ARM_ALU_PC_G0_NC:
12113
0
    case R_ARM_ALU_PC_G0:
12114
0
    case R_ARM_ALU_SB_G0_NC:
12115
0
    case R_ARM_ALU_SB_G0:
12116
0
      group = 0;
12117
0
      break;
12118
12119
0
    case R_ARM_ALU_PC_G1_NC:
12120
0
    case R_ARM_ALU_PC_G1:
12121
0
    case R_ARM_ALU_SB_G1_NC:
12122
0
    case R_ARM_ALU_SB_G1:
12123
0
      group = 1;
12124
0
      break;
12125
12126
0
    case R_ARM_ALU_PC_G2:
12127
0
    case R_ARM_ALU_SB_G2:
12128
0
      group = 2;
12129
0
      break;
12130
12131
0
    default:
12132
0
      abort ();
12133
0
    }
12134
12135
  /* If REL, extract the addend from the insn.  If RELA, it will
12136
     have already been fetched for us.  */
12137
0
  if (globals->use_rel)
12138
0
    {
12139
0
      int negative;
12140
0
      bfd_vma constant = insn & 0xff;
12141
0
      bfd_vma rotation = (insn & 0xf00) >> 8;
12142
12143
0
      if (rotation == 0)
12144
0
        signed_addend = constant;
12145
0
      else
12146
0
        {
12147
    /* Compensate for the fact that in the instruction, the
12148
       rotation is stored in multiples of 2 bits.  */
12149
0
    rotation *= 2;
12150
12151
    /* Rotate "constant" right by "rotation" bits.  */
12152
0
    signed_addend = (constant >> rotation) |
12153
0
        (constant << (8 * sizeof (bfd_vma) - rotation));
12154
0
        }
12155
12156
      /* Determine if the instruction is an ADD or a SUB.
12157
         (For REL, this determines the sign of the addend.)  */
12158
0
      negative = identify_add_or_sub (insn);
12159
0
      if (negative == 0)
12160
0
        {
12161
0
    _bfd_error_handler
12162
      /* xgettext:c-format */
12163
0
      (_("%pB(%pA+%#" PRIx64 "): only ADD or SUB instructions "
12164
0
         "are allowed for ALU group relocations"),
12165
0
      input_bfd, input_section, (uint64_t) rel->r_offset);
12166
0
    return bfd_reloc_overflow;
12167
0
        }
12168
12169
0
      signed_addend *= negative;
12170
0
    }
12171
12172
  /* Compute the value (X) to go in the place.  */
12173
0
  if (r_type == R_ARM_ALU_PC_G0_NC
12174
0
      || r_type == R_ARM_ALU_PC_G1_NC
12175
0
      || r_type == R_ARM_ALU_PC_G0
12176
0
      || r_type == R_ARM_ALU_PC_G1
12177
0
      || r_type == R_ARM_ALU_PC_G2)
12178
    /* PC relative.  */
12179
0
    signed_value = value - pc + signed_addend;
12180
0
  else
12181
    /* Section base relative.  */
12182
0
    signed_value = value - sb + signed_addend;
12183
12184
  /* If the target symbol is a Thumb function, then set the
12185
     Thumb bit in the address.  */
12186
0
  if (branch_type == ST_BRANCH_TO_THUMB)
12187
0
    signed_value |= 1;
12188
12189
  /* Calculate the value of the relevant G_n, in encoded
12190
     constant-with-rotation format.  */
12191
0
  g_n = calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
12192
0
            group, &residual);
12193
12194
  /* Check for overflow if required.  */
12195
0
  if ((r_type == R_ARM_ALU_PC_G0
12196
0
       || r_type == R_ARM_ALU_PC_G1
12197
0
       || r_type == R_ARM_ALU_PC_G2
12198
0
       || r_type == R_ARM_ALU_SB_G0
12199
0
       || r_type == R_ARM_ALU_SB_G1
12200
0
       || r_type == R_ARM_ALU_SB_G2) && residual != 0)
12201
0
    {
12202
0
      _bfd_error_handler
12203
        /* xgettext:c-format */
12204
0
        (_("%pB(%pA+%#" PRIx64 "): overflow whilst "
12205
0
     "splitting %#" PRIx64 " for group relocation %s"),
12206
0
         input_bfd, input_section, (uint64_t) rel->r_offset,
12207
0
         (uint64_t) (signed_value < 0 ? -signed_value : signed_value),
12208
0
         howto->name);
12209
0
      return bfd_reloc_overflow;
12210
0
    }
12211
12212
  /* Mask out the value and the ADD/SUB part of the opcode; take care
12213
     not to destroy the S bit.  */
12214
0
  insn &= 0xff1ff000;
12215
12216
  /* Set the opcode according to whether the value to go in the
12217
     place is negative.  */
12218
0
  if (signed_value < 0)
12219
0
    insn |= 1 << 22;
12220
0
  else
12221
0
    insn |= 1 << 23;
12222
12223
  /* Encode the offset.  */
12224
0
  insn |= g_n;
12225
12226
0
  bfd_put_32 (input_bfd, insn, hit_data);
12227
0
      }
12228
0
      return bfd_reloc_ok;
12229
12230
0
    case R_ARM_LDR_PC_G0:
12231
0
    case R_ARM_LDR_PC_G1:
12232
0
    case R_ARM_LDR_PC_G2:
12233
0
    case R_ARM_LDR_SB_G0:
12234
0
    case R_ARM_LDR_SB_G1:
12235
0
    case R_ARM_LDR_SB_G2:
12236
0
      {
12237
0
  bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12238
0
  bfd_vma pc = input_section->output_section->vma
12239
0
         + input_section->output_offset + rel->r_offset;
12240
  /* sb is the origin of the *segment* containing the symbol.  */
12241
0
  bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
12242
0
  bfd_vma residual;
12243
0
  bfd_signed_vma signed_value;
12244
0
  int group = 0;
12245
12246
  /* Determine which groups of bits to calculate.  */
12247
0
  switch (r_type)
12248
0
    {
12249
0
    case R_ARM_LDR_PC_G0:
12250
0
    case R_ARM_LDR_SB_G0:
12251
0
      group = 0;
12252
0
      break;
12253
12254
0
    case R_ARM_LDR_PC_G1:
12255
0
    case R_ARM_LDR_SB_G1:
12256
0
      group = 1;
12257
0
      break;
12258
12259
0
    case R_ARM_LDR_PC_G2:
12260
0
    case R_ARM_LDR_SB_G2:
12261
0
      group = 2;
12262
0
      break;
12263
12264
0
    default:
12265
0
      abort ();
12266
0
    }
12267
12268
  /* If REL, extract the addend from the insn.  If RELA, it will
12269
     have already been fetched for us.  */
12270
0
  if (globals->use_rel)
12271
0
    {
12272
0
      int negative = (insn & (1 << 23)) ? 1 : -1;
12273
0
      signed_addend = negative * (insn & 0xfff);
12274
0
    }
12275
12276
  /* Compute the value (X) to go in the place.  */
12277
0
  if (r_type == R_ARM_LDR_PC_G0
12278
0
      || r_type == R_ARM_LDR_PC_G1
12279
0
      || r_type == R_ARM_LDR_PC_G2)
12280
    /* PC relative.  */
12281
0
    signed_value = value - pc + signed_addend;
12282
0
  else
12283
    /* Section base relative.  */
12284
0
    signed_value = value - sb + signed_addend;
12285
12286
  /* Calculate the value of the relevant G_{n-1} to obtain
12287
     the residual at that stage.  */
12288
0
  calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
12289
0
            group - 1, &residual);
12290
12291
  /* Check for overflow.  */
12292
0
  if (residual >= 0x1000)
12293
0
    {
12294
0
      _bfd_error_handler
12295
        /* xgettext:c-format */
12296
0
        (_("%pB(%pA+%#" PRIx64 "): overflow whilst "
12297
0
     "splitting %#" PRIx64 " for group relocation %s"),
12298
0
         input_bfd, input_section, (uint64_t) rel->r_offset,
12299
0
         (uint64_t) (signed_value < 0 ? -signed_value : signed_value),
12300
0
         howto->name);
12301
0
      return bfd_reloc_overflow;
12302
0
    }
12303
12304
  /* Mask out the value and U bit.  */
12305
0
  insn &= 0xff7ff000;
12306
12307
  /* Set the U bit if the value to go in the place is non-negative.  */
12308
0
  if (signed_value >= 0)
12309
0
    insn |= 1 << 23;
12310
12311
  /* Encode the offset.  */
12312
0
  insn |= residual;
12313
12314
0
  bfd_put_32 (input_bfd, insn, hit_data);
12315
0
      }
12316
0
      return bfd_reloc_ok;
12317
12318
0
    case R_ARM_LDRS_PC_G0:
12319
0
    case R_ARM_LDRS_PC_G1:
12320
0
    case R_ARM_LDRS_PC_G2:
12321
0
    case R_ARM_LDRS_SB_G0:
12322
0
    case R_ARM_LDRS_SB_G1:
12323
0
    case R_ARM_LDRS_SB_G2:
12324
0
      {
12325
0
  bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12326
0
  bfd_vma pc = input_section->output_section->vma
12327
0
         + input_section->output_offset + rel->r_offset;
12328
  /* sb is the origin of the *segment* containing the symbol.  */
12329
0
  bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
12330
0
  bfd_vma residual;
12331
0
  bfd_signed_vma signed_value;
12332
0
  int group = 0;
12333
12334
  /* Determine which groups of bits to calculate.  */
12335
0
  switch (r_type)
12336
0
    {
12337
0
    case R_ARM_LDRS_PC_G0:
12338
0
    case R_ARM_LDRS_SB_G0:
12339
0
      group = 0;
12340
0
      break;
12341
12342
0
    case R_ARM_LDRS_PC_G1:
12343
0
    case R_ARM_LDRS_SB_G1:
12344
0
      group = 1;
12345
0
      break;
12346
12347
0
    case R_ARM_LDRS_PC_G2:
12348
0
    case R_ARM_LDRS_SB_G2:
12349
0
      group = 2;
12350
0
      break;
12351
12352
0
    default:
12353
0
      abort ();
12354
0
    }
12355
12356
  /* If REL, extract the addend from the insn.  If RELA, it will
12357
     have already been fetched for us.  */
12358
0
  if (globals->use_rel)
12359
0
    {
12360
0
      int negative = (insn & (1 << 23)) ? 1 : -1;
12361
0
      signed_addend = negative * (((insn & 0xf00) >> 4) + (insn & 0xf));
12362
0
    }
12363
12364
  /* Compute the value (X) to go in the place.  */
12365
0
  if (r_type == R_ARM_LDRS_PC_G0
12366
0
      || r_type == R_ARM_LDRS_PC_G1
12367
0
      || r_type == R_ARM_LDRS_PC_G2)
12368
    /* PC relative.  */
12369
0
    signed_value = value - pc + signed_addend;
12370
0
  else
12371
    /* Section base relative.  */
12372
0
    signed_value = value - sb + signed_addend;
12373
12374
  /* Calculate the value of the relevant G_{n-1} to obtain
12375
     the residual at that stage.  */
12376
0
  calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
12377
0
            group - 1, &residual);
12378
12379
  /* Check for overflow.  */
12380
0
  if (residual >= 0x100)
12381
0
    {
12382
0
      _bfd_error_handler
12383
        /* xgettext:c-format */
12384
0
        (_("%pB(%pA+%#" PRIx64 "): overflow whilst "
12385
0
     "splitting %#" PRIx64 " for group relocation %s"),
12386
0
         input_bfd, input_section, (uint64_t) rel->r_offset,
12387
0
         (uint64_t) (signed_value < 0 ? -signed_value : signed_value),
12388
0
         howto->name);
12389
0
      return bfd_reloc_overflow;
12390
0
    }
12391
12392
  /* Mask out the value and U bit.  */
12393
0
  insn &= 0xff7ff0f0;
12394
12395
  /* Set the U bit if the value to go in the place is non-negative.  */
12396
0
  if (signed_value >= 0)
12397
0
    insn |= 1 << 23;
12398
12399
  /* Encode the offset.  */
12400
0
  insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
12401
12402
0
  bfd_put_32 (input_bfd, insn, hit_data);
12403
0
      }
12404
0
      return bfd_reloc_ok;
12405
12406
0
    case R_ARM_LDC_PC_G0:
12407
0
    case R_ARM_LDC_PC_G1:
12408
0
    case R_ARM_LDC_PC_G2:
12409
0
    case R_ARM_LDC_SB_G0:
12410
0
    case R_ARM_LDC_SB_G1:
12411
0
    case R_ARM_LDC_SB_G2:
12412
0
      {
12413
0
  bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12414
0
  bfd_vma pc = input_section->output_section->vma
12415
0
         + input_section->output_offset + rel->r_offset;
12416
  /* sb is the origin of the *segment* containing the symbol.  */
12417
0
  bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
12418
0
  bfd_vma residual;
12419
0
  bfd_signed_vma signed_value;
12420
0
  int group = 0;
12421
12422
  /* Determine which groups of bits to calculate.  */
12423
0
  switch (r_type)
12424
0
    {
12425
0
    case R_ARM_LDC_PC_G0:
12426
0
    case R_ARM_LDC_SB_G0:
12427
0
      group = 0;
12428
0
      break;
12429
12430
0
    case R_ARM_LDC_PC_G1:
12431
0
    case R_ARM_LDC_SB_G1:
12432
0
      group = 1;
12433
0
      break;
12434
12435
0
    case R_ARM_LDC_PC_G2:
12436
0
    case R_ARM_LDC_SB_G2:
12437
0
      group = 2;
12438
0
      break;
12439
12440
0
    default:
12441
0
      abort ();
12442
0
    }
12443
12444
  /* If REL, extract the addend from the insn.  If RELA, it will
12445
     have already been fetched for us.  */
12446
0
  if (globals->use_rel)
12447
0
    {
12448
0
      int negative = (insn & (1 << 23)) ? 1 : -1;
12449
0
      signed_addend = negative * ((insn & 0xff) << 2);
12450
0
    }
12451
12452
  /* Compute the value (X) to go in the place.  */
12453
0
  if (r_type == R_ARM_LDC_PC_G0
12454
0
      || r_type == R_ARM_LDC_PC_G1
12455
0
      || r_type == R_ARM_LDC_PC_G2)
12456
    /* PC relative.  */
12457
0
    signed_value = value - pc + signed_addend;
12458
0
  else
12459
    /* Section base relative.  */
12460
0
    signed_value = value - sb + signed_addend;
12461
12462
  /* Calculate the value of the relevant G_{n-1} to obtain
12463
     the residual at that stage.  */
12464
0
  calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
12465
0
            group - 1, &residual);
12466
12467
  /* Check for overflow.  (The absolute value to go in the place must be
12468
     divisible by four and, after having been divided by four, must
12469
     fit in eight bits.)  */
12470
0
  if ((residual & 0x3) != 0 || residual >= 0x400)
12471
0
    {
12472
0
      _bfd_error_handler
12473
        /* xgettext:c-format */
12474
0
        (_("%pB(%pA+%#" PRIx64 "): overflow whilst "
12475
0
     "splitting %#" PRIx64 " for group relocation %s"),
12476
0
         input_bfd, input_section, (uint64_t) rel->r_offset,
12477
0
         (uint64_t) (signed_value < 0 ? -signed_value : signed_value),
12478
0
         howto->name);
12479
0
      return bfd_reloc_overflow;
12480
0
    }
12481
12482
  /* Mask out the value and U bit.  */
12483
0
  insn &= 0xff7fff00;
12484
12485
  /* Set the U bit if the value to go in the place is non-negative.  */
12486
0
  if (signed_value >= 0)
12487
0
    insn |= 1 << 23;
12488
12489
  /* Encode the offset.  */
12490
0
  insn |= residual >> 2;
12491
12492
0
  bfd_put_32 (input_bfd, insn, hit_data);
12493
0
      }
12494
0
      return bfd_reloc_ok;
12495
12496
0
    case R_ARM_THM_ALU_ABS_G0_NC:
12497
0
    case R_ARM_THM_ALU_ABS_G1_NC:
12498
0
    case R_ARM_THM_ALU_ABS_G2_NC:
12499
0
    case R_ARM_THM_ALU_ABS_G3_NC:
12500
0
  {
12501
0
      static const int shift_array[4] = {0, 8, 16, 24};
12502
0
      bfd_vma insn = bfd_get_16 (input_bfd, hit_data);
12503
0
      bfd_vma addr = value;
12504
0
      int shift = shift_array[r_type - R_ARM_THM_ALU_ABS_G0_NC];
12505
12506
      /* Compute address.  */
12507
0
      if (globals->use_rel)
12508
0
    signed_addend = insn & 0xff;
12509
0
      addr += signed_addend;
12510
0
      if (branch_type == ST_BRANCH_TO_THUMB)
12511
0
    addr |= 1;
12512
      /* Clean imm8 insn.  */
12513
0
      insn &= 0xff00;
12514
      /* And update with correct part of address.  */
12515
0
      insn |= (addr >> shift) & 0xff;
12516
      /* Update insn.  */
12517
0
      bfd_put_16 (input_bfd, insn, hit_data);
12518
0
  }
12519
12520
0
  *unresolved_reloc_p = false;
12521
0
  return bfd_reloc_ok;
12522
12523
0
    case R_ARM_GOTOFFFUNCDESC:
12524
0
      {
12525
0
  if (h == NULL)
12526
0
    {
12527
0
      struct fdpic_local *local_fdpic_cnts = elf32_arm_local_fdpic_cnts (input_bfd);
12528
0
      int dynindx = elf_section_data (sym_sec->output_section)->dynindx;
12529
12530
0
      if (r_symndx >= elf32_arm_num_entries (input_bfd))
12531
0
        {
12532
0
    * error_message = _("local symbol index too big");
12533
0
    return bfd_reloc_dangerous;
12534
0
        }
12535
12536
0
      int offset = local_fdpic_cnts[r_symndx].funcdesc_offset & ~1;
12537
0
      bfd_vma addr = dynreloc_value - sym_sec->output_section->vma;
12538
0
      bfd_vma seg = -1;
12539
12540
0
      if (bfd_link_pic (info) && dynindx == 0)
12541
0
        {
12542
0
    * error_message = _("no dynamic index information available");
12543
0
    return bfd_reloc_dangerous;
12544
0
        }
12545
12546
      /* Resolve relocation.  */
12547
0
      bfd_put_32 (output_bfd, (offset + sgot->output_offset)
12548
0
           , contents + rel->r_offset);
12549
      /* Emit R_ARM_FUNCDESC_VALUE or two fixups on funcdesc if
12550
         not done yet.  */
12551
0
      arm_elf_fill_funcdesc (output_bfd, info,
12552
0
           &local_fdpic_cnts[r_symndx].funcdesc_offset,
12553
0
           dynindx, offset, addr, dynreloc_value, seg);
12554
0
    }
12555
0
  else
12556
0
    {
12557
0
      int dynindx;
12558
0
      int offset = eh->fdpic_cnts.funcdesc_offset & ~1;
12559
0
      bfd_vma addr;
12560
0
      bfd_vma seg = -1;
12561
12562
      /* For static binaries, sym_sec can be null.  */
12563
0
      if (sym_sec)
12564
0
        {
12565
0
    dynindx = elf_section_data (sym_sec->output_section)->dynindx;
12566
0
    addr = dynreloc_value - sym_sec->output_section->vma;
12567
0
        }
12568
0
      else
12569
0
        {
12570
0
    dynindx = 0;
12571
0
    addr = 0;
12572
0
        }
12573
12574
0
      if (bfd_link_pic (info) && dynindx == 0)
12575
0
        {
12576
0
    * error_message = _("no dynamic index information available");
12577
0
    return bfd_reloc_dangerous;
12578
0
        }
12579
12580
      /* This case cannot occur since funcdesc is allocated by
12581
         the dynamic loader so we cannot resolve the relocation.  */
12582
0
      if (h->dynindx != -1)
12583
0
        {
12584
0
    * error_message = _("invalid dynamic index");
12585
0
    return bfd_reloc_dangerous;
12586
0
        }
12587
12588
      /* Resolve relocation.  */
12589
0
      bfd_put_32 (output_bfd, (offset + sgot->output_offset),
12590
0
            contents + rel->r_offset);
12591
      /* Emit R_ARM_FUNCDESC_VALUE on funcdesc if not done yet.  */
12592
0
      arm_elf_fill_funcdesc (output_bfd, info,
12593
0
           &eh->fdpic_cnts.funcdesc_offset,
12594
0
           dynindx, offset, addr, dynreloc_value, seg);
12595
0
    }
12596
0
      }
12597
0
      *unresolved_reloc_p = false;
12598
0
      return bfd_reloc_ok;
12599
12600
0
    case R_ARM_GOTFUNCDESC:
12601
0
      {
12602
0
  if (h != NULL)
12603
0
    {
12604
0
      Elf_Internal_Rela outrel;
12605
12606
      /* Resolve relocation.  */
12607
0
      bfd_put_32 (output_bfd, ((eh->fdpic_cnts.gotfuncdesc_offset & ~1)
12608
0
             + sgot->output_offset),
12609
0
      contents + rel->r_offset);
12610
      /* Add funcdesc and associated R_ARM_FUNCDESC_VALUE.  */
12611
0
      if (h->dynindx == -1)
12612
0
        {
12613
0
    int dynindx;
12614
0
    int offset = eh->fdpic_cnts.funcdesc_offset & ~1;
12615
0
    bfd_vma addr;
12616
0
    bfd_vma seg = -1;
12617
12618
    /* For static binaries sym_sec can be null.  */
12619
0
    if (sym_sec)
12620
0
      {
12621
0
        dynindx = elf_section_data (sym_sec->output_section)->dynindx;
12622
0
        addr = dynreloc_value - sym_sec->output_section->vma;
12623
0
      }
12624
0
    else
12625
0
      {
12626
0
        dynindx = 0;
12627
0
        addr = 0;
12628
0
      }
12629
12630
    /* Emit R_ARM_FUNCDESC_VALUE on funcdesc if not done yet.  */
12631
0
    arm_elf_fill_funcdesc (output_bfd, info,
12632
0
               &eh->fdpic_cnts.funcdesc_offset,
12633
0
               dynindx, offset, addr, dynreloc_value, seg);
12634
0
        }
12635
12636
      /* Add a dynamic relocation on GOT entry if not already done.  */
12637
0
      if ((eh->fdpic_cnts.gotfuncdesc_offset & 1) == 0)
12638
0
        {
12639
0
    if (h->dynindx == -1)
12640
0
      {
12641
0
        outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
12642
0
        if (h->root.type == bfd_link_hash_undefweak)
12643
0
          bfd_put_32 (output_bfd, 0, sgot->contents
12644
0
          + (eh->fdpic_cnts.gotfuncdesc_offset & ~1));
12645
0
        else
12646
0
          bfd_put_32 (output_bfd, sgot->output_section->vma
12647
0
          + sgot->output_offset
12648
0
          + (eh->fdpic_cnts.funcdesc_offset & ~1),
12649
0
          sgot->contents
12650
0
          + (eh->fdpic_cnts.gotfuncdesc_offset & ~1));
12651
0
      }
12652
0
    else
12653
0
      {
12654
0
        outrel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_FUNCDESC);
12655
0
      }
12656
0
    outrel.r_offset = sgot->output_section->vma
12657
0
      + sgot->output_offset
12658
0
      + (eh->fdpic_cnts.gotfuncdesc_offset & ~1);
12659
0
    outrel.r_addend = 0;
12660
0
    if (h->dynindx == -1 && !bfd_link_pic (info))
12661
0
      if (h->root.type == bfd_link_hash_undefweak)
12662
0
        arm_elf_add_rofixup (output_bfd, globals->srofixup, -1);
12663
0
      else
12664
0
        arm_elf_add_rofixup (output_bfd, globals->srofixup,
12665
0
           outrel.r_offset);
12666
0
    else
12667
0
      elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
12668
0
    eh->fdpic_cnts.gotfuncdesc_offset |= 1;
12669
0
        }
12670
0
    }
12671
0
  else
12672
0
    {
12673
      /* Such relocation on static function should not have been
12674
         emitted by the compiler.  */
12675
0
      return bfd_reloc_notsupported;
12676
0
    }
12677
0
      }
12678
0
      *unresolved_reloc_p = false;
12679
0
      return bfd_reloc_ok;
12680
12681
0
    case R_ARM_FUNCDESC:
12682
0
      {
12683
0
  if (h == NULL)
12684
0
    {
12685
0
      struct fdpic_local *local_fdpic_cnts = elf32_arm_local_fdpic_cnts (input_bfd);
12686
0
      Elf_Internal_Rela outrel;
12687
0
      int dynindx = elf_section_data (sym_sec->output_section)->dynindx;
12688
12689
0
      if (r_symndx >= elf32_arm_num_entries (input_bfd))
12690
0
        {
12691
0
    * error_message = _("local symbol index too big");
12692
0
    return bfd_reloc_dangerous;
12693
0
        }
12694
12695
0
      int offset = local_fdpic_cnts[r_symndx].funcdesc_offset & ~1;
12696
0
      bfd_vma addr = dynreloc_value - sym_sec->output_section->vma;
12697
0
      bfd_vma seg = -1;
12698
12699
0
      if (bfd_link_pic (info) && dynindx == 0)
12700
0
        {
12701
0
    * error_message = _("dynamic index information not available");
12702
0
    return bfd_reloc_dangerous;
12703
0
        }
12704
12705
      /* Replace static FUNCDESC relocation with a
12706
         R_ARM_RELATIVE dynamic relocation or with a rofixup for
12707
         executable.  */
12708
0
      outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
12709
0
      outrel.r_offset = input_section->output_section->vma
12710
0
        + input_section->output_offset + rel->r_offset;
12711
0
      outrel.r_addend = 0;
12712
0
      if (bfd_link_pic (info))
12713
0
        elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
12714
0
      else
12715
0
        arm_elf_add_rofixup (output_bfd, globals->srofixup, outrel.r_offset);
12716
12717
0
      bfd_put_32 (input_bfd, sgot->output_section->vma
12718
0
      + sgot->output_offset + offset, hit_data);
12719
12720
      /* Emit R_ARM_FUNCDESC_VALUE on funcdesc if not done yet.  */
12721
0
      arm_elf_fill_funcdesc (output_bfd, info,
12722
0
           &local_fdpic_cnts[r_symndx].funcdesc_offset,
12723
0
           dynindx, offset, addr, dynreloc_value, seg);
12724
0
    }
12725
0
  else
12726
0
    {
12727
0
      if (h->dynindx == -1)
12728
0
        {
12729
0
    int dynindx;
12730
0
    int offset = eh->fdpic_cnts.funcdesc_offset & ~1;
12731
0
    bfd_vma addr;
12732
0
    bfd_vma seg = -1;
12733
0
    Elf_Internal_Rela outrel;
12734
12735
    /* For static binaries sym_sec can be null.  */
12736
0
    if (sym_sec)
12737
0
      {
12738
0
        dynindx = elf_section_data (sym_sec->output_section)->dynindx;
12739
0
        addr = dynreloc_value - sym_sec->output_section->vma;
12740
0
      }
12741
0
    else
12742
0
      {
12743
0
        dynindx = 0;
12744
0
        addr = 0;
12745
0
      }
12746
12747
0
    if (bfd_link_pic (info) && dynindx == 0)
12748
0
      abort ();
12749
12750
    /* Replace static FUNCDESC relocation with a
12751
       R_ARM_RELATIVE dynamic relocation.  */
12752
0
    outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
12753
0
    outrel.r_offset = input_section->output_section->vma
12754
0
      + input_section->output_offset + rel->r_offset;
12755
0
    outrel.r_addend = 0;
12756
0
    if (bfd_link_pic (info))
12757
0
      elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
12758
0
    else
12759
0
      arm_elf_add_rofixup (output_bfd, globals->srofixup, outrel.r_offset);
12760
12761
0
    bfd_put_32 (input_bfd, sgot->output_section->vma
12762
0
          + sgot->output_offset + offset, hit_data);
12763
12764
    /* Emit R_ARM_FUNCDESC_VALUE on funcdesc if not done yet.  */
12765
0
    arm_elf_fill_funcdesc (output_bfd, info,
12766
0
               &eh->fdpic_cnts.funcdesc_offset,
12767
0
               dynindx, offset, addr, dynreloc_value, seg);
12768
0
        }
12769
0
      else
12770
0
        {
12771
0
    Elf_Internal_Rela outrel;
12772
12773
    /* Add a dynamic relocation.  */
12774
0
    outrel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_FUNCDESC);
12775
0
    outrel.r_offset = input_section->output_section->vma
12776
0
      + input_section->output_offset + rel->r_offset;
12777
0
    outrel.r_addend = 0;
12778
0
    elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
12779
0
        }
12780
0
    }
12781
0
      }
12782
0
      *unresolved_reloc_p = false;
12783
0
      return bfd_reloc_ok;
12784
12785
0
    case R_ARM_THM_BF16:
12786
0
      {
12787
0
  bfd_vma relocation;
12788
0
  bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
12789
0
  bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
12790
12791
0
  if (globals->use_rel)
12792
0
    {
12793
0
      bfd_vma immA  = (upper_insn & 0x001f);
12794
0
      bfd_vma immB  = (lower_insn & 0x07fe) >> 1;
12795
0
      bfd_vma immC  = (lower_insn & 0x0800) >> 11;
12796
0
      addend  = (immA << 12);
12797
0
      addend |= (immB << 2);
12798
0
      addend |= (immC << 1);
12799
0
      addend |= 1;
12800
      /* Sign extend.  */
12801
0
      signed_addend = (addend & 0x10000) ? addend - (1 << 17) : addend;
12802
0
    }
12803
12804
0
  relocation  = value + signed_addend;
12805
0
  relocation -= (input_section->output_section->vma
12806
0
           + input_section->output_offset
12807
0
           + rel->r_offset);
12808
12809
  /* Put RELOCATION back into the insn.  */
12810
0
  {
12811
0
    bfd_vma immA = (relocation & 0x0001f000) >> 12;
12812
0
    bfd_vma immB = (relocation & 0x00000ffc) >> 2;
12813
0
    bfd_vma immC = (relocation & 0x00000002) >> 1;
12814
12815
0
    upper_insn = (upper_insn & 0xffe0) | immA;
12816
0
    lower_insn = (lower_insn & 0xf001) | (immC << 11) | (immB << 1);
12817
0
  }
12818
12819
  /* Put the relocated value back in the object file:  */
12820
0
  bfd_put_16 (input_bfd, upper_insn, hit_data);
12821
0
  bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
12822
12823
0
  return bfd_reloc_ok;
12824
0
      }
12825
12826
0
    case R_ARM_THM_BF12:
12827
0
      {
12828
0
  bfd_vma relocation;
12829
0
  bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
12830
0
  bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
12831
12832
0
  if (globals->use_rel)
12833
0
    {
12834
0
      bfd_vma immA  = (upper_insn & 0x0001);
12835
0
      bfd_vma immB  = (lower_insn & 0x07fe) >> 1;
12836
0
      bfd_vma immC  = (lower_insn & 0x0800) >> 11;
12837
0
      addend  = (immA << 12);
12838
0
      addend |= (immB << 2);
12839
0
      addend |= (immC << 1);
12840
0
      addend |= 1;
12841
      /* Sign extend.  */
12842
0
      addend = (addend & 0x1000) ? addend - (1 << 13) : addend;
12843
0
      signed_addend = addend;
12844
0
    }
12845
12846
0
  relocation  = value + signed_addend;
12847
0
  relocation -= (input_section->output_section->vma
12848
0
           + input_section->output_offset
12849
0
           + rel->r_offset);
12850
12851
  /* Put RELOCATION back into the insn.  */
12852
0
  {
12853
0
    bfd_vma immA = (relocation & 0x00001000) >> 12;
12854
0
    bfd_vma immB = (relocation & 0x00000ffc) >> 2;
12855
0
    bfd_vma immC = (relocation & 0x00000002) >> 1;
12856
12857
0
    upper_insn = (upper_insn & 0xfffe) | immA;
12858
0
    lower_insn = (lower_insn & 0xf001) | (immC << 11) | (immB << 1);
12859
0
  }
12860
12861
  /* Put the relocated value back in the object file:  */
12862
0
  bfd_put_16 (input_bfd, upper_insn, hit_data);
12863
0
  bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
12864
12865
0
  return bfd_reloc_ok;
12866
0
      }
12867
12868
0
    case R_ARM_THM_BF18:
12869
0
      {
12870
0
  bfd_vma relocation;
12871
0
  bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
12872
0
  bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
12873
12874
0
  if (globals->use_rel)
12875
0
    {
12876
0
      bfd_vma immA  = (upper_insn & 0x007f);
12877
0
      bfd_vma immB  = (lower_insn & 0x07fe) >> 1;
12878
0
      bfd_vma immC  = (lower_insn & 0x0800) >> 11;
12879
0
      addend  = (immA << 12);
12880
0
      addend |= (immB << 2);
12881
0
      addend |= (immC << 1);
12882
0
      addend |= 1;
12883
      /* Sign extend.  */
12884
0
      addend = (addend & 0x40000) ? addend - (1 << 19) : addend;
12885
0
      signed_addend = addend;
12886
0
    }
12887
12888
0
  relocation  = value + signed_addend;
12889
0
  relocation -= (input_section->output_section->vma
12890
0
           + input_section->output_offset
12891
0
           + rel->r_offset);
12892
12893
  /* Put RELOCATION back into the insn.  */
12894
0
  {
12895
0
    bfd_vma immA = (relocation & 0x0007f000) >> 12;
12896
0
    bfd_vma immB = (relocation & 0x00000ffc) >> 2;
12897
0
    bfd_vma immC = (relocation & 0x00000002) >> 1;
12898
12899
0
    upper_insn = (upper_insn & 0xff80) | immA;
12900
0
    lower_insn = (lower_insn & 0xf001) | (immC << 11) | (immB << 1);
12901
0
  }
12902
12903
  /* Put the relocated value back in the object file:  */
12904
0
  bfd_put_16 (input_bfd, upper_insn, hit_data);
12905
0
  bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
12906
12907
0
  return bfd_reloc_ok;
12908
0
      }
12909
12910
0
    default:
12911
0
      return bfd_reloc_notsupported;
12912
0
    }
12913
0
}
12914
12915
/* Add INCREMENT to the reloc (of type HOWTO) at ADDRESS.  */
12916
static void
12917
arm_add_to_rel (bfd *      abfd,
12918
    bfd_byte *     address,
12919
    reloc_howto_type * howto,
12920
    bfd_signed_vma     increment)
12921
0
{
12922
0
  bfd_signed_vma addend;
12923
12924
0
  if (howto->type == R_ARM_THM_CALL
12925
0
      || howto->type == R_ARM_THM_JUMP24)
12926
0
    {
12927
0
      int upper_insn, lower_insn;
12928
0
      int upper, lower;
12929
12930
0
      upper_insn = bfd_get_16 (abfd, address);
12931
0
      lower_insn = bfd_get_16 (abfd, address + 2);
12932
0
      upper = upper_insn & 0x7ff;
12933
0
      lower = lower_insn & 0x7ff;
12934
12935
0
      addend = (upper << 12) | (lower << 1);
12936
0
      addend += increment;
12937
0
      addend >>= 1;
12938
12939
0
      upper_insn = (upper_insn & 0xf800) | ((addend >> 11) & 0x7ff);
12940
0
      lower_insn = (lower_insn & 0xf800) | (addend & 0x7ff);
12941
12942
0
      bfd_put_16 (abfd, (bfd_vma) upper_insn, address);
12943
0
      bfd_put_16 (abfd, (bfd_vma) lower_insn, address + 2);
12944
0
    }
12945
0
  else
12946
0
    {
12947
0
      bfd_vma      contents;
12948
12949
0
      contents = bfd_get_32 (abfd, address);
12950
12951
      /* Get the (signed) value from the instruction.  */
12952
0
      addend = contents & howto->src_mask;
12953
0
      if (addend & ((howto->src_mask + 1) >> 1))
12954
0
  {
12955
0
    bfd_signed_vma mask;
12956
12957
0
    mask = -1;
12958
0
    mask &= ~ howto->src_mask;
12959
0
    addend |= mask;
12960
0
  }
12961
12962
      /* Add in the increment, (which is a byte value).  */
12963
0
      switch (howto->type)
12964
0
  {
12965
0
  default:
12966
0
    addend += increment;
12967
0
    break;
12968
12969
0
  case R_ARM_PC24:
12970
0
  case R_ARM_PLT32:
12971
0
  case R_ARM_CALL:
12972
0
  case R_ARM_JUMP24:
12973
0
    addend *= bfd_get_reloc_size (howto);
12974
0
    addend += increment;
12975
12976
    /* Should we check for overflow here ?  */
12977
12978
    /* Drop any undesired bits.  */
12979
0
    addend >>= howto->rightshift;
12980
0
    break;
12981
0
  }
12982
12983
0
      contents = (contents & ~ howto->dst_mask) | (addend & howto->dst_mask);
12984
12985
0
      bfd_put_32 (abfd, contents, address);
12986
0
    }
12987
0
}
12988
12989
#define IS_ARM_TLS_RELOC(R_TYPE)  \
12990
0
  ((R_TYPE) == R_ARM_TLS_GD32    \
12991
0
   || (R_TYPE) == R_ARM_TLS_GD32_FDPIC  \
12992
0
   || (R_TYPE) == R_ARM_TLS_LDO32  \
12993
0
   || (R_TYPE) == R_ARM_TLS_LDM32  \
12994
0
   || (R_TYPE) == R_ARM_TLS_LDM32_FDPIC  \
12995
0
   || (R_TYPE) == R_ARM_TLS_DTPOFF32  \
12996
0
   || (R_TYPE) == R_ARM_TLS_DTPMOD32  \
12997
0
   || (R_TYPE) == R_ARM_TLS_TPOFF32  \
12998
0
   || (R_TYPE) == R_ARM_TLS_LE32  \
12999
0
   || (R_TYPE) == R_ARM_TLS_IE32  \
13000
0
   || (R_TYPE) == R_ARM_TLS_IE32_FDPIC  \
13001
0
   || IS_ARM_TLS_GNU_RELOC (R_TYPE))
13002
13003
/* Specific set of relocations for the gnu tls dialect.  */
13004
#define IS_ARM_TLS_GNU_RELOC(R_TYPE)  \
13005
0
  ((R_TYPE) == R_ARM_TLS_GOTDESC  \
13006
0
   || (R_TYPE) == R_ARM_TLS_CALL  \
13007
0
   || (R_TYPE) == R_ARM_THM_TLS_CALL  \
13008
0
   || (R_TYPE) == R_ARM_TLS_DESCSEQ  \
13009
0
   || (R_TYPE) == R_ARM_THM_TLS_DESCSEQ)
13010
13011
/* Relocate an ARM ELF section.  */
13012
13013
static int
13014
elf32_arm_relocate_section (struct bfd_link_info * info,
13015
          bfd *      input_bfd,
13016
          asection *       input_section,
13017
          bfd_byte *       contents,
13018
          Elf_Internal_Rela *    relocs,
13019
          Elf_Internal_Sym *     local_syms,
13020
          asection **      local_sections)
13021
0
{
13022
0
  Elf_Internal_Shdr *symtab_hdr;
13023
0
  struct elf_link_hash_entry **sym_hashes;
13024
0
  Elf_Internal_Rela *rel;
13025
0
  Elf_Internal_Rela *relend;
13026
0
  const char *name;
13027
0
  struct elf32_arm_link_hash_table * globals;
13028
13029
0
  globals = elf32_arm_hash_table (info);
13030
0
  if (globals == NULL)
13031
0
    return false;
13032
13033
0
  symtab_hdr = & elf_symtab_hdr (input_bfd);
13034
0
  sym_hashes = elf_sym_hashes (input_bfd);
13035
13036
0
  rel = relocs;
13037
0
  relend = relocs + input_section->reloc_count;
13038
0
  for (; rel < relend; rel++)
13039
0
    {
13040
0
      int        r_type;
13041
0
      reloc_howto_type *     howto;
13042
0
      unsigned long      r_symndx;
13043
0
      Elf_Internal_Sym *     sym;
13044
0
      asection *       sec;
13045
0
      struct elf_link_hash_entry * h;
13046
0
      bfd_vma        relocation;
13047
0
      bfd_reloc_status_type    r;
13048
0
      arelent        bfd_reloc;
13049
0
      char         sym_type;
13050
0
      bool         unresolved_reloc = false;
13051
0
      char *error_message = NULL;
13052
13053
0
      r_symndx = ELF32_R_SYM (rel->r_info);
13054
0
      r_type   = ELF32_R_TYPE (rel->r_info);
13055
0
      r_type   = arm_real_reloc_type (globals, r_type);
13056
13057
0
      if (   r_type == R_ARM_GNU_VTENTRY
13058
0
    || r_type == R_ARM_GNU_VTINHERIT)
13059
0
  continue;
13060
13061
0
      howto = bfd_reloc.howto = elf32_arm_howto_from_type (r_type);
13062
13063
0
      if (howto == NULL)
13064
0
  return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
13065
13066
0
      h = NULL;
13067
0
      sym = NULL;
13068
0
      sec = NULL;
13069
13070
0
      if (r_symndx < symtab_hdr->sh_info)
13071
0
  {
13072
0
    sym = local_syms + r_symndx;
13073
0
    sym_type = ELF32_ST_TYPE (sym->st_info);
13074
0
    sec = local_sections[r_symndx];
13075
13076
    /* An object file might have a reference to a local
13077
       undefined symbol.  This is a daft object file, but we
13078
       should at least do something about it.  V4BX & NONE
13079
       relocations do not use the symbol and are explicitly
13080
       allowed to use the undefined symbol, so allow those.
13081
       Likewise for relocations against STN_UNDEF.  */
13082
0
    if (r_type != R_ARM_V4BX
13083
0
        && r_type != R_ARM_NONE
13084
0
        && r_symndx != STN_UNDEF
13085
0
        && bfd_is_und_section (sec)
13086
0
        && ELF_ST_BIND (sym->st_info) != STB_WEAK)
13087
0
      (*info->callbacks->undefined_symbol)
13088
0
        (info, bfd_elf_string_from_elf_section
13089
0
         (input_bfd, symtab_hdr->sh_link, sym->st_name),
13090
0
         input_bfd, input_section,
13091
0
         rel->r_offset, true);
13092
13093
0
    if (globals->use_rel)
13094
0
      {
13095
0
        relocation = (sec->output_section->vma
13096
0
          + sec->output_offset
13097
0
          + sym->st_value);
13098
0
        if (!bfd_link_relocatable (info)
13099
0
      && (sec->flags & SEC_MERGE)
13100
0
      && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13101
0
    {
13102
0
      asection *msec;
13103
0
      bfd_vma addend, value;
13104
13105
0
      switch (r_type)
13106
0
        {
13107
0
        case R_ARM_MOVW_ABS_NC:
13108
0
        case R_ARM_MOVT_ABS:
13109
0
          value = bfd_get_32 (input_bfd, contents + rel->r_offset);
13110
0
          addend = ((value & 0xf0000) >> 4) | (value & 0xfff);
13111
0
          addend = (addend ^ 0x8000) - 0x8000;
13112
0
          break;
13113
13114
0
        case R_ARM_THM_MOVW_ABS_NC:
13115
0
        case R_ARM_THM_MOVT_ABS:
13116
0
          value = bfd_get_16 (input_bfd, contents + rel->r_offset)
13117
0
            << 16;
13118
0
          value |= bfd_get_16 (input_bfd,
13119
0
             contents + rel->r_offset + 2);
13120
0
          addend = ((value & 0xf7000) >> 4) | (value & 0xff)
13121
0
             | ((value & 0x04000000) >> 15);
13122
0
          addend = (addend ^ 0x8000) - 0x8000;
13123
0
          break;
13124
13125
0
        default:
13126
0
          if (howto->rightshift
13127
0
        || (howto->src_mask & (howto->src_mask + 1)))
13128
0
      {
13129
0
        _bfd_error_handler
13130
          /* xgettext:c-format */
13131
0
          (_("%pB(%pA+%#" PRIx64 "): "
13132
0
             "%s relocation against SEC_MERGE section"),
13133
0
           input_bfd, input_section,
13134
0
           (uint64_t) rel->r_offset, howto->name);
13135
0
        return false;
13136
0
      }
13137
13138
0
          value = bfd_get_32 (input_bfd, contents + rel->r_offset);
13139
13140
          /* Get the (signed) value from the instruction.  */
13141
0
          addend = value & howto->src_mask;
13142
0
          if (addend & ((howto->src_mask + 1) >> 1))
13143
0
      {
13144
0
        bfd_signed_vma mask;
13145
13146
0
        mask = -1;
13147
0
        mask &= ~ howto->src_mask;
13148
0
        addend |= mask;
13149
0
      }
13150
0
          break;
13151
0
        }
13152
13153
0
      msec = sec;
13154
0
      addend = (_bfd_elf_rel_local_sym (info->output_bfd,
13155
0
                sym, &msec, addend)
13156
0
          - relocation);
13157
0
      addend += msec->output_section->vma + msec->output_offset;
13158
13159
      /* Cases here must match those in the preceding
13160
         switch statement.  */
13161
0
      switch (r_type)
13162
0
        {
13163
0
        case R_ARM_MOVW_ABS_NC:
13164
0
        case R_ARM_MOVT_ABS:
13165
0
          value = (value & 0xfff0f000) | ((addend & 0xf000) << 4)
13166
0
            | (addend & 0xfff);
13167
0
          bfd_put_32 (input_bfd, value, contents + rel->r_offset);
13168
0
          break;
13169
13170
0
        case R_ARM_THM_MOVW_ABS_NC:
13171
0
        case R_ARM_THM_MOVT_ABS:
13172
0
          value = (value & 0xfbf08f00) | ((addend & 0xf700) << 4)
13173
0
            | (addend & 0xff) | ((addend & 0x0800) << 15);
13174
0
          bfd_put_16 (input_bfd, value >> 16,
13175
0
          contents + rel->r_offset);
13176
0
          bfd_put_16 (input_bfd, value,
13177
0
          contents + rel->r_offset + 2);
13178
0
          break;
13179
13180
0
        default:
13181
0
          value = (value & ~ howto->dst_mask)
13182
0
            | (addend & howto->dst_mask);
13183
0
          bfd_put_32 (input_bfd, value, contents + rel->r_offset);
13184
0
          break;
13185
0
        }
13186
0
    }
13187
0
      }
13188
0
    else
13189
0
      relocation = _bfd_elf_rela_local_sym (info->output_bfd,
13190
0
              sym, &sec, rel);
13191
0
  }
13192
0
      else
13193
0
  {
13194
0
    bool warned, ignored;
13195
13196
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13197
0
           r_symndx, symtab_hdr, sym_hashes,
13198
0
           h, sec, relocation,
13199
0
           unresolved_reloc, warned, ignored);
13200
13201
0
    sym_type = h->type;
13202
0
  }
13203
13204
0
      if (sec != NULL && discarded_section (sec))
13205
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
13206
0
           rel, 1, relend, R_ARM_NONE,
13207
0
           howto, 0, contents);
13208
13209
0
      if (bfd_link_relocatable (info))
13210
0
  {
13211
    /* This is a relocatable link.  We don't have to change
13212
       anything, unless the reloc is against a section symbol,
13213
       in which case we have to adjust according to where the
13214
       section symbol winds up in the output section.  */
13215
0
    if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13216
0
      {
13217
0
        if (globals->use_rel)
13218
0
    arm_add_to_rel (input_bfd, contents + rel->r_offset,
13219
0
        howto, (bfd_signed_vma) sec->output_offset);
13220
0
        else
13221
0
    rel->r_addend += sec->output_offset;
13222
0
      }
13223
0
    continue;
13224
0
  }
13225
13226
0
      if (h != NULL)
13227
0
  name = h->root.root.string;
13228
0
      else
13229
0
  {
13230
0
    name = (bfd_elf_string_from_elf_section
13231
0
      (input_bfd, symtab_hdr->sh_link, sym->st_name));
13232
0
    if (name == NULL || *name == '\0')
13233
0
      name = bfd_section_name (sec);
13234
0
  }
13235
13236
0
      if (r_symndx != STN_UNDEF
13237
0
    && r_type != R_ARM_NONE
13238
0
    && (h == NULL
13239
0
        || h->root.type == bfd_link_hash_defined
13240
0
        || h->root.type == bfd_link_hash_defweak)
13241
0
    && IS_ARM_TLS_RELOC (r_type) != (sym_type == STT_TLS))
13242
0
  {
13243
0
    _bfd_error_handler
13244
0
      ((sym_type == STT_TLS
13245
        /* xgettext:c-format */
13246
0
        ? _("%pB(%pA+%#" PRIx64 "): %s used with TLS symbol %s")
13247
        /* xgettext:c-format */
13248
0
        : _("%pB(%pA+%#" PRIx64 "): %s used with non-TLS symbol %s")),
13249
0
       input_bfd,
13250
0
       input_section,
13251
0
       (uint64_t) rel->r_offset,
13252
0
       howto->name,
13253
0
       name);
13254
0
  }
13255
13256
      /* We call elf32_arm_final_link_relocate unless we're completely
13257
   done, i.e., the relaxation produced the final output we want,
13258
   and we won't let anybody mess with it. Also, we have to do
13259
   addend adjustments in case of a R_ARM_TLS_GOTDESC relocation
13260
   both in relaxed and non-relaxed cases.  */
13261
0
      if ((elf32_arm_tls_transition (info, r_type, h) != (unsigned)r_type)
13262
0
    || (IS_ARM_TLS_GNU_RELOC (r_type)
13263
0
        && !((h ? elf32_arm_hash_entry (h)->tls_type :
13264
0
        elf32_arm_local_got_tls_type (input_bfd)[r_symndx])
13265
0
       & GOT_TLS_GDESC)))
13266
0
  {
13267
0
    r = elf32_arm_tls_relax (globals, input_bfd, input_section,
13268
0
           contents, rel, h == NULL);
13269
    /* This may have been marked unresolved because it came from
13270
       a shared library.  But we've just dealt with that.  */
13271
0
    unresolved_reloc = 0;
13272
0
  }
13273
0
      else
13274
0
  r = bfd_reloc_continue;
13275
13276
0
      if (r == bfd_reloc_continue)
13277
0
  {
13278
0
    unsigned char branch_type =
13279
0
      h ? ARM_GET_SYM_BRANCH_TYPE (h->target_internal)
13280
0
        : ARM_GET_SYM_BRANCH_TYPE (sym->st_target_internal);
13281
13282
0
    r = elf32_arm_final_link_relocate (howto, input_bfd, info->output_bfd,
13283
0
               input_section, contents, rel,
13284
0
               relocation, info, sec, name,
13285
0
               sym_type, branch_type, h,
13286
0
               &unresolved_reloc,
13287
0
               &error_message);
13288
0
  }
13289
13290
      /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13291
   because such sections are not SEC_ALLOC and thus ld.so will
13292
   not process them.  */
13293
0
      if (unresolved_reloc
13294
0
    && !((input_section->flags & SEC_DEBUGGING) != 0
13295
0
         && h->def_dynamic)
13296
0
    && _bfd_elf_section_offset (info->output_bfd, info, input_section,
13297
0
              rel->r_offset) != (bfd_vma) -1)
13298
0
  {
13299
0
    _bfd_error_handler
13300
      /* xgettext:c-format */
13301
0
      (_("%pB(%pA+%#" PRIx64 "): "
13302
0
         "unresolvable %s relocation against symbol `%s'"),
13303
0
       input_bfd,
13304
0
       input_section,
13305
0
       (uint64_t) rel->r_offset,
13306
0
       howto->name,
13307
0
       h->root.root.string);
13308
0
    return false;
13309
0
  }
13310
13311
0
      if (r != bfd_reloc_ok)
13312
0
  {
13313
0
    switch (r)
13314
0
      {
13315
0
      case bfd_reloc_overflow:
13316
        /* If the overflowing reloc was to an undefined symbol,
13317
     we have already printed one error message and there
13318
     is no point complaining again.  */
13319
0
        if (!h || h->root.type != bfd_link_hash_undefined)
13320
0
    (*info->callbacks->reloc_overflow)
13321
0
      (info, (h ? &h->root : NULL), name, howto->name,
13322
0
       (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
13323
0
        break;
13324
13325
0
      case bfd_reloc_undefined:
13326
0
        (*info->callbacks->undefined_symbol)
13327
0
    (info, name, input_bfd, input_section, rel->r_offset, true);
13328
0
        break;
13329
13330
0
      case bfd_reloc_outofrange:
13331
0
        error_message = _("out of range");
13332
0
        goto common_error;
13333
13334
0
      case bfd_reloc_notsupported:
13335
0
        error_message = _("unsupported relocation");
13336
0
        goto common_error;
13337
13338
0
      case bfd_reloc_dangerous:
13339
        /* error_message should already be set.  */
13340
0
        goto common_error;
13341
13342
0
      default:
13343
0
        error_message = _("unknown error");
13344
        /* Fall through.  */
13345
13346
0
      common_error:
13347
0
        BFD_ASSERT (error_message != NULL);
13348
0
        (*info->callbacks->reloc_dangerous)
13349
0
    (info, error_message, input_bfd, input_section, rel->r_offset);
13350
0
        break;
13351
0
      }
13352
0
  }
13353
0
    }
13354
13355
0
  return true;
13356
0
}
13357
13358
/* Add a new unwind edit to the list described by HEAD, TAIL.  If TINDEX is zero,
13359
   adds the edit to the start of the list.  (The list must be built in order of
13360
   ascending TINDEX: the function's callers are primarily responsible for
13361
   maintaining that condition).  */
13362
13363
static void
13364
add_unwind_table_edit (arm_unwind_table_edit **head,
13365
           arm_unwind_table_edit **tail,
13366
           arm_unwind_edit_type type,
13367
           asection *linked_section,
13368
           unsigned int tindex)
13369
0
{
13370
0
  arm_unwind_table_edit *new_edit = (arm_unwind_table_edit *)
13371
0
      xmalloc (sizeof (arm_unwind_table_edit));
13372
13373
0
  new_edit->type = type;
13374
0
  new_edit->linked_section = linked_section;
13375
0
  new_edit->index = tindex;
13376
13377
0
  if (tindex > 0)
13378
0
    {
13379
0
      new_edit->next = NULL;
13380
13381
0
      if (*tail)
13382
0
  (*tail)->next = new_edit;
13383
13384
0
      (*tail) = new_edit;
13385
13386
0
      if (!*head)
13387
0
  (*head) = new_edit;
13388
0
    }
13389
0
  else
13390
0
    {
13391
0
      new_edit->next = *head;
13392
13393
0
      if (!*tail)
13394
0
  *tail = new_edit;
13395
13396
0
      *head = new_edit;
13397
0
    }
13398
0
}
13399
13400
static _arm_elf_section_data *get_arm_elf_section_data (asection *);
13401
13402
/* Increase the size of EXIDX_SEC by ADJUST bytes.  ADJUST mau be negative.  */
13403
13404
static void
13405
adjust_exidx_size (asection *exidx_sec, int adjust)
13406
0
{
13407
0
  asection *out_sec;
13408
13409
0
  if (!exidx_sec->rawsize)
13410
0
    exidx_sec->rawsize = exidx_sec->size;
13411
13412
0
  bfd_set_section_size (exidx_sec, exidx_sec->size + adjust);
13413
0
  out_sec = exidx_sec->output_section;
13414
  /* Adjust size of output section.  */
13415
0
  bfd_set_section_size (out_sec, out_sec->size + adjust);
13416
0
}
13417
13418
/* Insert an EXIDX_CANTUNWIND marker at the end of a section.  */
13419
13420
static void
13421
insert_cantunwind_after (asection *text_sec, asection *exidx_sec)
13422
0
{
13423
0
  struct _arm_elf_section_data *exidx_arm_data;
13424
13425
0
  exidx_arm_data = get_arm_elf_section_data (exidx_sec);
13426
0
  add_unwind_table_edit
13427
0
    (&exidx_arm_data->u.exidx.unwind_edit_list,
13428
0
     &exidx_arm_data->u.exidx.unwind_edit_tail,
13429
0
     INSERT_EXIDX_CANTUNWIND_AT_END, text_sec, UINT_MAX);
13430
13431
0
  exidx_arm_data->additional_reloc_count++;
13432
13433
0
  adjust_exidx_size (exidx_sec, 8);
13434
0
}
13435
13436
/* Scan .ARM.exidx tables, and create a list describing edits which should be
13437
   made to those tables, such that:
13438
13439
     1. Regions without unwind data are marked with EXIDX_CANTUNWIND entries.
13440
     2. Duplicate entries are merged together (EXIDX_CANTUNWIND, or unwind
13441
  codes which have been inlined into the index).
13442
13443
   If MERGE_EXIDX_ENTRIES is false, duplicate entries are not merged.
13444
13445
   The edits are applied when the tables are written
13446
   (in elf32_arm_write_section).  */
13447
13448
bool
13449
elf32_arm_fix_exidx_coverage (asection **text_section_order,
13450
            unsigned int num_text_sections,
13451
            struct bfd_link_info *info,
13452
            bool merge_exidx_entries)
13453
0
{
13454
0
  bfd *inp;
13455
0
  unsigned int last_second_word = 0, i;
13456
0
  asection *last_exidx_sec = NULL;
13457
0
  asection *last_text_sec = NULL;
13458
0
  int last_unwind_type = -1;
13459
13460
  /* Walk over all EXIDX sections, and create backlinks from the corrsponding
13461
     text sections.  */
13462
0
  for (inp = info->input_bfds; inp != NULL; inp = inp->link.next)
13463
0
    {
13464
0
      asection *sec;
13465
13466
0
      for (sec = inp->sections; sec != NULL; sec = sec->next)
13467
0
  {
13468
0
    struct bfd_elf_section_data *elf_sec = elf_section_data (sec);
13469
0
    Elf_Internal_Shdr *hdr = &elf_sec->this_hdr;
13470
13471
0
    if (!hdr || hdr->sh_type != SHT_ARM_EXIDX)
13472
0
      continue;
13473
13474
0
    if (elf_sec->linked_to)
13475
0
      {
13476
0
        Elf_Internal_Shdr *linked_hdr
13477
0
    = &elf_section_data (elf_sec->linked_to)->this_hdr;
13478
0
        struct _arm_elf_section_data *linked_sec_arm_data
13479
0
    = get_arm_elf_section_data (linked_hdr->bfd_section);
13480
13481
0
        if (linked_sec_arm_data == NULL)
13482
0
    continue;
13483
13484
        /* Link this .ARM.exidx section back from the text section it
13485
     describes.  */
13486
0
        linked_sec_arm_data->u.text.arm_exidx_sec = sec;
13487
0
      }
13488
0
  }
13489
0
    }
13490
13491
  /* Walk all text sections in order of increasing VMA.  Eilminate duplicate
13492
     index table entries (EXIDX_CANTUNWIND and inlined unwind opcodes),
13493
     and add EXIDX_CANTUNWIND entries for sections with no unwind table data.  */
13494
13495
0
  for (i = 0; i < num_text_sections; i++)
13496
0
    {
13497
0
      asection *sec = text_section_order[i];
13498
0
      asection *exidx_sec;
13499
0
      struct _arm_elf_section_data *arm_data = get_arm_elf_section_data (sec);
13500
0
      struct _arm_elf_section_data *exidx_arm_data;
13501
0
      bfd_byte *contents = NULL;
13502
0
      int deleted_exidx_bytes = 0;
13503
0
      bfd_vma j;
13504
0
      arm_unwind_table_edit *unwind_edit_head = NULL;
13505
0
      arm_unwind_table_edit *unwind_edit_tail = NULL;
13506
0
      Elf_Internal_Shdr *hdr;
13507
0
      bfd *ibfd;
13508
13509
0
      if (arm_data == NULL)
13510
0
  continue;
13511
13512
0
      exidx_sec = arm_data->u.text.arm_exidx_sec;
13513
0
      if (exidx_sec == NULL)
13514
0
  {
13515
    /* Section has no unwind data.  */
13516
0
    if (last_unwind_type == 0 || !last_exidx_sec)
13517
0
      continue;
13518
13519
    /* Ignore zero sized sections.  */
13520
0
    if (sec->size == 0)
13521
0
      continue;
13522
13523
0
    insert_cantunwind_after (last_text_sec, last_exidx_sec);
13524
0
    last_unwind_type = 0;
13525
0
    continue;
13526
0
  }
13527
13528
      /* Skip /DISCARD/ sections.  */
13529
0
      if (bfd_is_abs_section (exidx_sec->output_section))
13530
0
  continue;
13531
13532
0
      hdr = &elf_section_data (exidx_sec)->this_hdr;
13533
0
      if (hdr->sh_type != SHT_ARM_EXIDX)
13534
0
  continue;
13535
13536
0
      exidx_arm_data = get_arm_elf_section_data (exidx_sec);
13537
0
      if (exidx_arm_data == NULL)
13538
0
  continue;
13539
13540
0
      ibfd = exidx_sec->owner;
13541
13542
0
      if (hdr->contents != NULL)
13543
0
  contents = hdr->contents;
13544
0
      else if (! bfd_malloc_and_get_section (ibfd, exidx_sec, &contents))
13545
  /* An error?  */
13546
0
  continue;
13547
13548
0
      if (last_unwind_type > 0)
13549
0
  {
13550
0
    unsigned int first_word = bfd_get_32 (ibfd, contents);
13551
    /* Add cantunwind if first unwind item does not match section
13552
       start.  */
13553
0
    if (first_word != sec->vma)
13554
0
      {
13555
0
        insert_cantunwind_after (last_text_sec, last_exidx_sec);
13556
0
        last_unwind_type = 0;
13557
0
      }
13558
0
  }
13559
13560
0
      for (j = 0; j < hdr->sh_size; j += 8)
13561
0
  {
13562
0
    unsigned int second_word = bfd_get_32 (ibfd, contents + j + 4);
13563
0
    int unwind_type;
13564
0
    int elide = 0;
13565
13566
    /* An EXIDX_CANTUNWIND entry.  */
13567
0
    if (second_word == 1)
13568
0
      {
13569
0
        if (last_unwind_type == 0)
13570
0
    elide = 1;
13571
0
        unwind_type = 0;
13572
0
      }
13573
    /* Inlined unwinding data.  Merge if equal to previous.  */
13574
0
    else if ((second_word & 0x80000000) != 0)
13575
0
      {
13576
0
        if (merge_exidx_entries
13577
0
       && last_second_word == second_word && last_unwind_type == 1)
13578
0
    elide = 1;
13579
0
        unwind_type = 1;
13580
0
        last_second_word = second_word;
13581
0
      }
13582
    /* Normal table entry.  In theory we could merge these too,
13583
       but duplicate entries are likely to be much less common.  */
13584
0
    else
13585
0
      unwind_type = 2;
13586
13587
0
    if (elide && !bfd_link_relocatable (info))
13588
0
      {
13589
0
        add_unwind_table_edit (&unwind_edit_head, &unwind_edit_tail,
13590
0
             DELETE_EXIDX_ENTRY, NULL, j / 8);
13591
13592
0
        deleted_exidx_bytes += 8;
13593
0
      }
13594
13595
0
    last_unwind_type = unwind_type;
13596
0
  }
13597
13598
      /* Free contents if we allocated it ourselves.  */
13599
0
      if (contents != hdr->contents)
13600
0
  free (contents);
13601
13602
      /* Record edits to be applied later (in elf32_arm_write_section).  */
13603
0
      exidx_arm_data->u.exidx.unwind_edit_list = unwind_edit_head;
13604
0
      exidx_arm_data->u.exidx.unwind_edit_tail = unwind_edit_tail;
13605
13606
0
      if (deleted_exidx_bytes > 0)
13607
0
  adjust_exidx_size (exidx_sec, - deleted_exidx_bytes);
13608
13609
0
      last_exidx_sec = exidx_sec;
13610
0
      last_text_sec = sec;
13611
0
    }
13612
13613
  /* Add terminating CANTUNWIND entry.  */
13614
0
  if (!bfd_link_relocatable (info) && last_exidx_sec
13615
0
      && last_unwind_type != 0)
13616
0
    insert_cantunwind_after (last_text_sec, last_exidx_sec);
13617
13618
0
  return true;
13619
0
}
13620
13621
static bool
13622
elf32_arm_output_glue_section (struct bfd_link_info *info, bfd *obfd,
13623
             bfd *ibfd, const char *name)
13624
0
{
13625
0
  asection *sec, *osec;
13626
13627
0
  sec = bfd_get_linker_section (ibfd, name);
13628
0
  if (sec == NULL || (sec->flags & SEC_EXCLUDE) != 0)
13629
0
    return true;
13630
13631
0
  osec = sec->output_section;
13632
0
  if (elf32_arm_write_section (obfd, info, sec, sec->contents))
13633
0
    return true;
13634
13635
0
  if (! bfd_set_section_contents (obfd, osec, sec->contents,
13636
0
          sec->output_offset, sec->size))
13637
0
    return false;
13638
13639
0
  return true;
13640
0
}
13641
13642
static bool
13643
elf32_arm_final_link (bfd *abfd, struct bfd_link_info *info)
13644
0
{
13645
0
  struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
13646
0
  asection *sec, *osec;
13647
13648
0
  if (globals == NULL)
13649
0
    return false;
13650
13651
  /* Invoke the regular ELF backend linker to do all the work.  */
13652
0
  if (!_bfd_elf_final_link (abfd, info))
13653
0
    return false;
13654
13655
  /* Process stub sections (eg BE8 encoding, ...).  */
13656
0
  struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
13657
0
  unsigned int i;
13658
0
  for (i=0; i<htab->top_id; i++)
13659
0
    {
13660
0
      sec = htab->stub_group[i].stub_sec;
13661
      /* Only process it once, in its link_sec slot.  */
13662
0
      if (sec && i == htab->stub_group[i].link_sec->id)
13663
0
  {
13664
0
    osec = sec->output_section;
13665
0
    elf32_arm_write_section (abfd, info, sec, sec->contents);
13666
0
    if (! bfd_set_section_contents (abfd, osec, sec->contents,
13667
0
            sec->output_offset, sec->size))
13668
0
      return false;
13669
0
  }
13670
0
    }
13671
13672
  /* Write out any glue sections now that we have created all the
13673
     stubs.  */
13674
0
  if (globals->bfd_of_glue_owner != NULL)
13675
0
    {
13676
0
      if (! elf32_arm_output_glue_section (info, abfd,
13677
0
             globals->bfd_of_glue_owner,
13678
0
             ARM2THUMB_GLUE_SECTION_NAME))
13679
0
  return false;
13680
13681
0
      if (! elf32_arm_output_glue_section (info, abfd,
13682
0
             globals->bfd_of_glue_owner,
13683
0
             THUMB2ARM_GLUE_SECTION_NAME))
13684
0
  return false;
13685
13686
0
      if (! elf32_arm_output_glue_section (info, abfd,
13687
0
             globals->bfd_of_glue_owner,
13688
0
             VFP11_ERRATUM_VENEER_SECTION_NAME))
13689
0
  return false;
13690
13691
0
      if (! elf32_arm_output_glue_section (info, abfd,
13692
0
             globals->bfd_of_glue_owner,
13693
0
             STM32L4XX_ERRATUM_VENEER_SECTION_NAME))
13694
0
  return false;
13695
13696
0
      if (! elf32_arm_output_glue_section (info, abfd,
13697
0
             globals->bfd_of_glue_owner,
13698
0
             ARM_BX_GLUE_SECTION_NAME))
13699
0
  return false;
13700
0
    }
13701
13702
0
  return true;
13703
0
}
13704
13705
/* Return a best guess for the machine number based on the attributes.  */
13706
13707
static unsigned int
13708
bfd_arm_get_mach_from_attributes (bfd * abfd)
13709
1.48k
{
13710
1.48k
  int arch = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC, Tag_CPU_arch);
13711
13712
1.48k
  switch (arch)
13713
1.48k
    {
13714
1.42k
    case TAG_CPU_ARCH_PRE_V4: return bfd_mach_arm_3M;
13715
2
    case TAG_CPU_ARCH_V4: return bfd_mach_arm_4;
13716
2
    case TAG_CPU_ARCH_V4T: return bfd_mach_arm_4T;
13717
2
    case TAG_CPU_ARCH_V5T: return bfd_mach_arm_5T;
13718
13719
6
    case TAG_CPU_ARCH_V5TE:
13720
6
      {
13721
6
  char * name;
13722
13723
6
  BFD_ASSERT (Tag_CPU_name < NUM_KNOWN_OBJ_ATTRIBUTES);
13724
6
  name = elf_known_obj_attributes (abfd) [OBJ_ATTR_PROC][Tag_CPU_name].s;
13725
13726
6
  if (name)
13727
4
    {
13728
4
      if (strcmp (name, "IWMMXT2") == 0)
13729
0
        return bfd_mach_arm_iWMMXt2;
13730
13731
4
      if (strcmp (name, "IWMMXT") == 0)
13732
0
        return bfd_mach_arm_iWMMXt;
13733
13734
4
      if (strcmp (name, "XSCALE") == 0)
13735
0
        {
13736
0
    int wmmx;
13737
13738
0
    BFD_ASSERT (Tag_WMMX_arch < NUM_KNOWN_OBJ_ATTRIBUTES);
13739
0
    wmmx = elf_known_obj_attributes (abfd) [OBJ_ATTR_PROC][Tag_WMMX_arch].i;
13740
0
    switch (wmmx)
13741
0
      {
13742
0
      case 1: return bfd_mach_arm_iWMMXt;
13743
0
      case 2: return bfd_mach_arm_iWMMXt2;
13744
0
      default: return bfd_mach_arm_XScale;
13745
0
      }
13746
0
        }
13747
4
    }
13748
13749
6
  return bfd_mach_arm_5TE;
13750
6
      }
13751
13752
0
    case TAG_CPU_ARCH_V5TEJ:
13753
0
  return bfd_mach_arm_5TEJ;
13754
0
    case TAG_CPU_ARCH_V6:
13755
0
  return bfd_mach_arm_6;
13756
0
    case TAG_CPU_ARCH_V6KZ:
13757
0
  return bfd_mach_arm_6KZ;
13758
0
    case TAG_CPU_ARCH_V6T2:
13759
0
  return bfd_mach_arm_6T2;
13760
2
    case TAG_CPU_ARCH_V6K:
13761
2
  return bfd_mach_arm_6K;
13762
24
    case TAG_CPU_ARCH_V7:
13763
24
  return bfd_mach_arm_7;
13764
6
    case TAG_CPU_ARCH_V6_M:
13765
6
  return bfd_mach_arm_6M;
13766
0
    case TAG_CPU_ARCH_V6S_M:
13767
0
  return bfd_mach_arm_6SM;
13768
0
    case TAG_CPU_ARCH_V7E_M:
13769
0
  return bfd_mach_arm_7EM;
13770
0
    case TAG_CPU_ARCH_V8A:
13771
0
  return bfd_mach_arm_8A;
13772
2
    case TAG_CPU_ARCH_V8R:
13773
2
  return bfd_mach_arm_8R;
13774
0
    case TAG_CPU_ARCH_V8M_BASE:
13775
0
  return bfd_mach_arm_8M_BASE;
13776
0
    case TAG_CPU_ARCH_V8M_MAIN:
13777
0
  return bfd_mach_arm_8M_MAIN;
13778
0
    case TAG_CPU_ARCH_V8_1M_MAIN:
13779
0
  return bfd_mach_arm_8_1M_MAIN;
13780
2
    case TAG_CPU_ARCH_V9A:
13781
2
  return bfd_mach_arm_9A;
13782
13783
10
    default:
13784
      /* Force entry to be added for any new known Tag_CPU_arch value.  */
13785
10
      BFD_ASSERT (arch > MAX_TAG_CPU_ARCH);
13786
13787
      /* Unknown Tag_CPU_arch value.  */
13788
10
      return bfd_mach_arm_unknown;
13789
1.48k
    }
13790
1.48k
}
13791
13792
/* Set the right machine number.  */
13793
13794
static bool
13795
elf32_arm_object_p (bfd *abfd)
13796
1.48k
{
13797
1.48k
  unsigned int mach;
13798
13799
1.48k
  mach = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
13800
13801
1.48k
  if (mach == bfd_mach_arm_unknown)
13802
1.48k
    mach = bfd_arm_get_mach_from_attributes (abfd);
13803
13804
1.48k
  bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
13805
1.48k
  return true;
13806
1.48k
}
13807
13808
/* Function to keep ARM specific flags in the ELF header.  */
13809
13810
static bool
13811
elf32_arm_set_private_flags (bfd *abfd, flagword flags)
13812
0
{
13813
0
  if (elf_flags_init (abfd)
13814
0
      && elf_elfheader (abfd)->e_flags != flags)
13815
0
    {
13816
0
      if (EF_ARM_EABI_VERSION (flags) == EF_ARM_EABI_UNKNOWN)
13817
0
  {
13818
0
    if (flags & EF_ARM_INTERWORK)
13819
0
      _bfd_error_handler
13820
0
        (_("warning: not setting interworking flag of %pB since it has already been specified as non-interworking"),
13821
0
         abfd);
13822
0
    else
13823
0
      _bfd_error_handler
13824
0
        (_("warning: clearing the interworking flag of %pB due to outside request"),
13825
0
         abfd);
13826
0
  }
13827
0
    }
13828
0
  else
13829
0
    {
13830
0
      elf_elfheader (abfd)->e_flags = flags;
13831
0
      elf_flags_init (abfd) = true;
13832
0
    }
13833
13834
0
  return true;
13835
0
}
13836
13837
/* Copy backend specific data from one object module to another.  */
13838
13839
static bool
13840
elf32_arm_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
13841
37
{
13842
37
  flagword in_flags;
13843
37
  flagword out_flags;
13844
13845
37
  if (! is_arm_elf (ibfd))
13846
0
    return true;
13847
13848
37
  in_flags  = elf_elfheader (ibfd)->e_flags;
13849
37
  out_flags = elf_elfheader (obfd)->e_flags;
13850
13851
37
  if (elf_flags_init (obfd)
13852
0
      && EF_ARM_EABI_VERSION (out_flags) == EF_ARM_EABI_UNKNOWN
13853
0
      && in_flags != out_flags)
13854
0
    {
13855
      /* Cannot mix APCS26 and APCS32 code.  */
13856
0
      if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
13857
0
  return false;
13858
13859
      /* Cannot mix float APCS and non-float APCS code.  */
13860
0
      if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
13861
0
  return false;
13862
13863
      /* If the src and dest have different interworking flags
13864
   then turn off the interworking bit.  */
13865
0
      if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
13866
0
  {
13867
0
    if (out_flags & EF_ARM_INTERWORK)
13868
0
      _bfd_error_handler
13869
0
        (_("warning: clearing the interworking flag of %pB because non-interworking code in %pB has been linked with it"),
13870
0
         obfd, ibfd);
13871
13872
0
    in_flags &= ~EF_ARM_INTERWORK;
13873
0
  }
13874
13875
      /* Likewise for PIC, though don't warn for this case.  */
13876
0
      if ((in_flags & EF_ARM_PIC) != (out_flags & EF_ARM_PIC))
13877
0
  in_flags &= ~EF_ARM_PIC;
13878
0
    }
13879
13880
37
  elf_elfheader (obfd)->e_flags = in_flags;
13881
37
  elf_flags_init (obfd) = true;
13882
13883
37
  return _bfd_elf_copy_private_bfd_data (ibfd, obfd);
13884
37
}
13885
13886
/* Values for Tag_ABI_PCS_R9_use.  */
13887
enum
13888
{
13889
  AEABI_R9_V6,
13890
  AEABI_R9_SB,
13891
  AEABI_R9_TLS,
13892
  AEABI_R9_unused
13893
};
13894
13895
/* Values for Tag_ABI_PCS_RW_data.  */
13896
enum
13897
{
13898
  AEABI_PCS_RW_data_absolute,
13899
  AEABI_PCS_RW_data_PCrel,
13900
  AEABI_PCS_RW_data_SBrel,
13901
  AEABI_PCS_RW_data_unused
13902
};
13903
13904
/* Values for Tag_ABI_enum_size.  */
13905
enum
13906
{
13907
  AEABI_enum_unused,
13908
  AEABI_enum_short,
13909
  AEABI_enum_wide,
13910
  AEABI_enum_forced_wide
13911
};
13912
13913
/* Determine whether an object attribute tag takes an integer, a
13914
   string or both.  */
13915
13916
static int
13917
elf32_arm_obj_attrs_arg_type (obj_attr_tag_t tag)
13918
2.99k
{
13919
2.99k
  if (tag == Tag_compatibility)
13920
48
    return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL;
13921
2.94k
  else if (tag == Tag_nodefaults)
13922
32
    return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_NO_DEFAULT;
13923
2.91k
  else if (tag == Tag_CPU_raw_name || tag == Tag_CPU_name)
13924
468
    return ATTR_TYPE_FLAG_STR_VAL;
13925
2.44k
  else if (tag < 32)
13926
1.69k
    return ATTR_TYPE_FLAG_INT_VAL;
13927
756
  else
13928
756
    return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
13929
2.99k
}
13930
13931
/* The ABI defines that Tag_conformance should be emitted first, and that
13932
   Tag_nodefaults should be second (if either is defined).  This sets those
13933
   two positions, and bumps up the position of all the remaining tags to
13934
   compensate.  */
13935
static int
13936
elf32_arm_obj_attrs_order (int num)
13937
0
{
13938
0
  if (num == LEAST_KNOWN_OBJ_ATTRIBUTE)
13939
0
    return Tag_conformance;
13940
0
  if (num == LEAST_KNOWN_OBJ_ATTRIBUTE + 1)
13941
0
    return Tag_nodefaults;
13942
0
  if ((num - 2) < Tag_nodefaults)
13943
0
    return num - 2;
13944
0
  if ((num - 1) < Tag_conformance)
13945
0
    return num - 1;
13946
0
  return num;
13947
0
}
13948
13949
/* Attribute numbers >=64 (mod 128) can be safely ignored.  */
13950
static bool
13951
elf32_arm_obj_attrs_handle_unknown (bfd *abfd, int tag)
13952
0
{
13953
0
  if ((tag & 127) < 64)
13954
0
    {
13955
0
      _bfd_error_handler
13956
0
  (_("%pB: unknown mandatory EABI object attribute %d"),
13957
0
   abfd, tag);
13958
0
      bfd_set_error (bfd_error_bad_value);
13959
0
      return false;
13960
0
    }
13961
0
  else
13962
0
    {
13963
0
      _bfd_error_handler
13964
0
  (_("warning: %pB: unknown EABI object attribute %d"),
13965
0
   abfd, tag);
13966
0
      return true;
13967
0
    }
13968
0
}
13969
13970
/* Read the architecture from the Tag_also_compatible_with attribute, if any.
13971
   Returns -1 if no architecture could be read.  */
13972
13973
static int
13974
get_secondary_compatible_arch (bfd *abfd)
13975
0
{
13976
0
  obj_attribute *attr =
13977
0
    &elf_known_obj_attributes_proc (abfd)[Tag_also_compatible_with];
13978
13979
  /* Note: the tag and its argument below are uleb128 values, though
13980
     currently-defined values fit in one byte for each.  */
13981
0
  if (attr->s
13982
0
      && attr->s[0] == Tag_CPU_arch
13983
0
      && (attr->s[1] & 128) != 128
13984
0
      && attr->s[2] == 0)
13985
0
   return attr->s[1];
13986
13987
  /* This tag is "safely ignorable", so don't complain if it looks funny.  */
13988
0
  return -1;
13989
0
}
13990
13991
/* Set, or unset, the architecture of the Tag_also_compatible_with attribute.
13992
   The tag is removed if ARCH is -1.  */
13993
13994
static void
13995
set_secondary_compatible_arch (bfd *abfd, int arch)
13996
0
{
13997
0
  obj_attribute *attr =
13998
0
    &elf_known_obj_attributes_proc (abfd)[Tag_also_compatible_with];
13999
14000
0
  if (arch == -1)
14001
0
    {
14002
0
      attr->s = NULL;
14003
0
      return;
14004
0
    }
14005
14006
  /* Note: the tag and its argument below are uleb128 values, though
14007
     currently-defined values fit in one byte for each.  */
14008
0
  if (!attr->s)
14009
0
    attr->s = (char *) bfd_alloc (abfd, 3);
14010
0
  attr->s[0] = Tag_CPU_arch;
14011
0
  attr->s[1] = arch;
14012
0
  attr->s[2] = '\0';
14013
0
}
14014
14015
/* Combine two values for Tag_CPU_arch, taking secondary compatibility tags
14016
   into account.  */
14017
14018
static int
14019
tag_cpu_arch_combine (bfd *ibfd, int oldtag, int *secondary_compat_out,
14020
          int newtag, int secondary_compat,
14021
          const char *const name_table[])
14022
0
{
14023
0
#define T(X) TAG_CPU_ARCH_##X
14024
0
  int tagl, tagh, result;
14025
0
  static const int v6t2[] =
14026
0
    {
14027
0
      T(V6T2),   /* PRE_V4.  */
14028
0
      T(V6T2),   /* V4.  */
14029
0
      T(V6T2),   /* V4T.  */
14030
0
      T(V6T2),   /* V5T.  */
14031
0
      T(V6T2),   /* V5TE.  */
14032
0
      T(V6T2),   /* V5TEJ.  */
14033
0
      T(V6T2),   /* V6.  */
14034
0
      T(V7),     /* V6KZ.  */
14035
0
      T(V6T2)    /* V6T2.  */
14036
0
    };
14037
0
  static const int v6k[] =
14038
0
    {
14039
0
      T(V6K),    /* PRE_V4.  */
14040
0
      T(V6K),    /* V4.  */
14041
0
      T(V6K),    /* V4T.  */
14042
0
      T(V6K),    /* V5T.  */
14043
0
      T(V6K),    /* V5TE.  */
14044
0
      T(V6K),    /* V5TEJ.  */
14045
0
      T(V6K),    /* V6.  */
14046
0
      T(V6KZ),   /* V6KZ.  */
14047
0
      T(V7),     /* V6T2.  */
14048
0
      T(V6K)     /* V6K.  */
14049
0
    };
14050
0
  static const int v7[] =
14051
0
    {
14052
0
      T(V7),     /* PRE_V4.  */
14053
0
      T(V7),     /* V4.  */
14054
0
      T(V7),     /* V4T.  */
14055
0
      T(V7),     /* V5T.  */
14056
0
      T(V7),     /* V5TE.  */
14057
0
      T(V7),     /* V5TEJ.  */
14058
0
      T(V7),     /* V6.  */
14059
0
      T(V7),     /* V6KZ.  */
14060
0
      T(V7),     /* V6T2.  */
14061
0
      T(V7),     /* V6K.  */
14062
0
      T(V7)      /* V7.  */
14063
0
    };
14064
0
  static const int v6_m[] =
14065
0
    {
14066
0
      -1,  /* PRE_V4.  */
14067
0
      -1,  /* V4.  */
14068
0
      T(V6K),    /* V4T.  */
14069
0
      T(V6K),    /* V5T.  */
14070
0
      T(V6K),    /* V5TE.  */
14071
0
      T(V6K),    /* V5TEJ.  */
14072
0
      T(V6K),    /* V6.  */
14073
0
      T(V6KZ),   /* V6KZ.  */
14074
0
      T(V7),     /* V6T2.  */
14075
0
      T(V6K),    /* V6K.  */
14076
0
      T(V7),     /* V7.  */
14077
0
      T(V6_M)    /* V6_M.  */
14078
0
    };
14079
0
  static const int v6s_m[] =
14080
0
    {
14081
0
      -1,  /* PRE_V4.  */
14082
0
      -1,  /* V4.  */
14083
0
      T(V6K),    /* V4T.  */
14084
0
      T(V6K),    /* V5T.  */
14085
0
      T(V6K),    /* V5TE.  */
14086
0
      T(V6K),    /* V5TEJ.  */
14087
0
      T(V6K),    /* V6.  */
14088
0
      T(V6KZ),   /* V6KZ.  */
14089
0
      T(V7),     /* V6T2.  */
14090
0
      T(V6K),    /* V6K.  */
14091
0
      T(V7),     /* V7.  */
14092
0
      T(V6S_M),  /* V6_M.  */
14093
0
      T(V6S_M)   /* V6S_M.  */
14094
0
    };
14095
0
  static const int v7e_m[] =
14096
0
    {
14097
0
      -1,  /* PRE_V4.  */
14098
0
      -1,  /* V4.  */
14099
0
      T(V7E_M),  /* V4T.  */
14100
0
      T(V7E_M),  /* V5T.  */
14101
0
      T(V7E_M),  /* V5TE.  */
14102
0
      T(V7E_M),  /* V5TEJ.  */
14103
0
      T(V7E_M),  /* V6.  */
14104
0
      T(V7E_M),  /* V6KZ.  */
14105
0
      T(V7E_M),  /* V6T2.  */
14106
0
      T(V7E_M),  /* V6K.  */
14107
0
      T(V7E_M),  /* V7.  */
14108
0
      T(V7E_M),  /* V6_M.  */
14109
0
      T(V7E_M),  /* V6S_M.  */
14110
0
      T(V7E_M)   /* V7E_M.  */
14111
0
    };
14112
0
  static const int v8[] =
14113
0
    {
14114
0
      T(V8A),    /* PRE_V4.  */
14115
0
      T(V8A),    /* V4.  */
14116
0
      T(V8A),    /* V4T.  */
14117
0
      T(V8A),    /* V5T.  */
14118
0
      T(V8A),    /* V5TE.  */
14119
0
      T(V8A),    /* V5TEJ.  */
14120
0
      T(V8A),    /* V6.  */
14121
0
      T(V8A),    /* V6KZ.  */
14122
0
      T(V8A),    /* V6T2.  */
14123
0
      T(V8A),    /* V6K.  */
14124
0
      T(V8A),    /* V7.  */
14125
0
      T(V8A),    /* V6_M.  */
14126
0
      T(V8A),    /* V6S_M.  */
14127
0
      T(V8A),    /* V7E_M.  */
14128
0
      T(V8A),    /* V8A.  */
14129
0
      T(V8A),    /* V8-R.  */
14130
0
      T(V8A),    /* V8-M.BASE.  */
14131
0
      T(V8A),    /* V8-M.MAIN.  */
14132
0
      T(V8A),    /* V8.1.  */
14133
0
      T(V8A),    /* V8.2.  */
14134
0
      T(V8A),    /* V8.3.  */
14135
0
      T(V8A),    /* V8.1-M.MAIN.  */
14136
0
    };
14137
0
  static const int v8r[] =
14138
0
    {
14139
0
      T(V8R),    /* PRE_V4.  */
14140
0
      T(V8R),    /* V4.  */
14141
0
      T(V8R),    /* V4T.  */
14142
0
      T(V8R),    /* V5T.  */
14143
0
      T(V8R),    /* V5TE.  */
14144
0
      T(V8R),    /* V5TEJ.  */
14145
0
      T(V8R),    /* V6.  */
14146
0
      T(V8R),    /* V6KZ.  */
14147
0
      T(V8R),    /* V6T2.  */
14148
0
      T(V8R),    /* V6K.  */
14149
0
      T(V8R),    /* V7.  */
14150
0
      T(V8R),    /* V6_M.  */
14151
0
      T(V8R),    /* V6S_M.  */
14152
0
      T(V8R),    /* V7E_M.  */
14153
0
      T(V8A),    /* V8A.  */
14154
0
      T(V8R),    /* V8R.  */
14155
0
    };
14156
0
  static const int v8m_baseline[] =
14157
0
    {
14158
0
      -1,   /* PRE_V4.  */
14159
0
      -1,   /* V4.  */
14160
0
      -1,   /* V4T.  */
14161
0
      -1,   /* V5T.  */
14162
0
      -1,   /* V5TE.  */
14163
0
      -1,   /* V5TEJ.  */
14164
0
      -1,   /* V6.  */
14165
0
      -1,   /* V6KZ.  */
14166
0
      -1,   /* V6T2.  */
14167
0
      -1,   /* V6K.  */
14168
0
      -1,   /* V7.  */
14169
0
      T(V8M_BASE), /* V6_M.  */
14170
0
      T(V8M_BASE), /* V6S_M.  */
14171
0
      -1,   /* V7E_M.  */
14172
0
      -1,   /* V8A.  */
14173
0
      -1,   /* V8R.  */
14174
0
      T(V8M_BASE)  /* V8-M BASELINE.  */
14175
0
    };
14176
0
  static const int v8m_mainline[] =
14177
0
    {
14178
0
      -1,   /* PRE_V4.  */
14179
0
      -1,   /* V4.  */
14180
0
      -1,   /* V4T.  */
14181
0
      -1,   /* V5T.  */
14182
0
      -1,   /* V5TE.  */
14183
0
      -1,   /* V5TEJ.  */
14184
0
      -1,   /* V6.  */
14185
0
      -1,   /* V6KZ.  */
14186
0
      -1,   /* V6T2.  */
14187
0
      -1,   /* V6K.  */
14188
0
      T(V8M_MAIN), /* V7.  */
14189
0
      T(V8M_MAIN), /* V6_M.  */
14190
0
      T(V8M_MAIN), /* V6S_M.  */
14191
0
      T(V8M_MAIN), /* V7E_M.  */
14192
0
      -1,   /* V8A.  */
14193
0
      -1,   /* V8R.  */
14194
0
      T(V8M_MAIN), /* V8-M BASELINE.  */
14195
0
      T(V8M_MAIN)  /* V8-M MAINLINE.  */
14196
0
    };
14197
0
  static const int v8_1m_mainline[] =
14198
0
    {
14199
0
      -1,   /* PRE_V4.  */
14200
0
      -1,   /* V4.  */
14201
0
      -1,   /* V4T.  */
14202
0
      -1,   /* V5T.  */
14203
0
      -1,   /* V5TE.  */
14204
0
      -1,   /* V5TEJ.  */
14205
0
      -1,   /* V6.  */
14206
0
      -1,   /* V6KZ.  */
14207
0
      -1,   /* V6T2.  */
14208
0
      -1,   /* V6K.  */
14209
0
      T(V8_1M_MAIN), /* V7.  */
14210
0
      T(V8_1M_MAIN), /* V6_M.  */
14211
0
      T(V8_1M_MAIN), /* V6S_M.  */
14212
0
      T(V8_1M_MAIN), /* V7E_M.  */
14213
0
      -1,   /* V8A.  */
14214
0
      -1,   /* V8R.  */
14215
0
      T(V8_1M_MAIN), /* V8-M BASELINE.  */
14216
0
      T(V8_1M_MAIN), /* V8-M MAINLINE.  */
14217
0
      -1,   /* Unused (18).  */
14218
0
      -1,   /* Unused (19).  */
14219
0
      -1,   /* Unused (20).  */
14220
0
      T(V8_1M_MAIN)  /* V8.1-M MAINLINE.  */
14221
0
    };
14222
0
  static const int v9[] =
14223
0
    {
14224
0
      T(V9A),    /* PRE_V4.  */
14225
0
      T(V9A),    /* V4.  */
14226
0
      T(V9A),    /* V4T.  */
14227
0
      T(V9A),    /* V5T.  */
14228
0
      T(V9A),    /* V5TE.  */
14229
0
      T(V9A),    /* V5TEJ.  */
14230
0
      T(V9A),    /* V6.  */
14231
0
      T(V9A),    /* V6KZ.  */
14232
0
      T(V9A),    /* V6T2.  */
14233
0
      T(V9A),    /* V6K.  */
14234
0
      T(V9A),    /* V7.  */
14235
0
      T(V9A),    /* V6_M.  */
14236
0
      T(V9A),    /* V6S_M.  */
14237
0
      T(V9A),    /* V7E_M.  */
14238
0
      T(V9A),    /* V8A.  */
14239
0
      T(V9A),    /* V8-R.  */
14240
0
      T(V9A),    /* V8-M.BASE.  */
14241
0
      T(V9A),    /* V8-M.MAIN.  */
14242
0
      T(V9A),    /* V8.1.  */
14243
0
      T(V9A),    /* V8.2.  */
14244
0
      T(V9A),    /* V8.3.  */
14245
0
      T(V9A),    /* V8.1-M.MAIN.  */
14246
0
      T(V9A),    /* V9A.  */
14247
0
     };
14248
0
  static const int v4t_plus_v6_m[] =
14249
0
    {
14250
0
      -1,   /* PRE_V4.  */
14251
0
      -1,   /* V4.  */
14252
0
      T(V4T),    /* V4T.  */
14253
0
      T(V5T),    /* V5T.  */
14254
0
      T(V5TE),   /* V5TE.  */
14255
0
      T(V5TEJ),    /* V5TEJ.  */
14256
0
      T(V6),   /* V6.  */
14257
0
      T(V6KZ),   /* V6KZ.  */
14258
0
      T(V6T2),   /* V6T2.  */
14259
0
      T(V6K),    /* V6K.  */
14260
0
      T(V7),   /* V7.  */
14261
0
      T(V6_M),   /* V6_M.  */
14262
0
      T(V6S_M),    /* V6S_M.  */
14263
0
      T(V7E_M),    /* V7E_M.  */
14264
0
      T(V8A),    /* V8A.  */
14265
0
      -1,   /* V8R.  */
14266
0
      T(V8M_BASE), /* V8-M BASELINE.  */
14267
0
      T(V8M_MAIN), /* V8-M MAINLINE.  */
14268
0
      -1,   /* Unused (18).  */
14269
0
      -1,   /* Unused (19).  */
14270
0
      -1,   /* Unused (20).  */
14271
0
      T(V8_1M_MAIN), /* V8.1-M MAINLINE.  */
14272
0
      T(V9A),    /* V9A.  */
14273
0
      T(V4T_PLUS_V6_M) /* V4T plus V6_M.  */
14274
0
    };
14275
0
  static const int *const comb[] =
14276
0
    {
14277
0
      v6t2,
14278
0
      v6k,
14279
0
      v7,
14280
0
      v6_m,
14281
0
      v6s_m,
14282
0
      v7e_m,
14283
0
      v8,
14284
0
      v8r,
14285
0
      v8m_baseline,
14286
0
      v8m_mainline,
14287
0
      NULL,
14288
0
      NULL,
14289
0
      NULL,
14290
0
      v8_1m_mainline,
14291
0
      v9,
14292
      /* Pseudo-architecture.  */
14293
0
      v4t_plus_v6_m
14294
0
    };
14295
14296
  /* Check we've not got a higher architecture than we know about.  */
14297
14298
0
  if (oldtag > MAX_TAG_CPU_ARCH || newtag > MAX_TAG_CPU_ARCH)
14299
0
    {
14300
0
      _bfd_error_handler (_("error: %pB: unknown CPU architecture"), ibfd);
14301
0
      return -1;
14302
0
    }
14303
14304
  /* Override old tag if we have a Tag_also_compatible_with on the output.  */
14305
14306
0
  if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
14307
0
      || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
14308
0
    oldtag = T(V4T_PLUS_V6_M);
14309
14310
  /* And override the new tag if we have a Tag_also_compatible_with on the
14311
     input.  */
14312
14313
0
  if ((newtag == T(V6_M) && secondary_compat == T(V4T))
14314
0
      || (newtag == T(V4T) && secondary_compat == T(V6_M)))
14315
0
    newtag = T(V4T_PLUS_V6_M);
14316
14317
0
  tagl = (oldtag < newtag) ? oldtag : newtag;
14318
0
  result = tagh = (oldtag > newtag) ? oldtag : newtag;
14319
14320
  /* Architectures before V6KZ add features monotonically.  */
14321
0
  if (tagh <= TAG_CPU_ARCH_V6KZ)
14322
0
    return result;
14323
14324
0
  result = comb[tagh - T(V6T2)] ? comb[tagh - T(V6T2)][tagl] : -1;
14325
14326
  /* Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
14327
     as the canonical version.  */
14328
0
  if (result == T(V4T_PLUS_V6_M))
14329
0
    {
14330
0
      result = T(V4T);
14331
0
      *secondary_compat_out = T(V6_M);
14332
0
    }
14333
0
  else
14334
0
    *secondary_compat_out = -1;
14335
14336
0
  if (result == -1)
14337
0
    {
14338
0
      _bfd_error_handler (_("error: conflicting CPU architectures %s vs %s in %pB"),
14339
0
        name_table[oldtag], name_table[newtag], ibfd);
14340
0
      return -1;
14341
0
    }
14342
14343
0
  return result;
14344
0
#undef T
14345
0
}
14346
14347
/* Query attributes object to see if integer divide instructions may be
14348
   present in an object.  */
14349
static bool
14350
elf32_arm_attributes_accept_div (const obj_attribute *attr)
14351
0
{
14352
0
  int arch = attr[Tag_CPU_arch].i;
14353
0
  int profile = attr[Tag_CPU_arch_profile].i;
14354
14355
0
  switch (attr[Tag_DIV_use].i)
14356
0
    {
14357
0
    case 0:
14358
      /* Integer divide allowed if instruction contained in archetecture.  */
14359
0
      if (arch == TAG_CPU_ARCH_V7 && (profile == 'R' || profile == 'M'))
14360
0
  return true;
14361
0
      else if (arch >= TAG_CPU_ARCH_V7E_M)
14362
0
  return true;
14363
0
      else
14364
0
  return false;
14365
14366
0
    case 1:
14367
      /* Integer divide explicitly prohibited.  */
14368
0
      return false;
14369
14370
0
    default:
14371
      /* Unrecognised case - treat as allowing divide everywhere.  */
14372
0
    case 2:
14373
      /* Integer divide allowed in ARM state.  */
14374
0
      return true;
14375
0
    }
14376
0
}
14377
14378
/* Query attributes object to see if integer divide instructions are
14379
   forbidden to be in the object.  This is not the inverse of
14380
   elf32_arm_attributes_accept_div.  */
14381
static bool
14382
elf32_arm_attributes_forbid_div (const obj_attribute *attr)
14383
0
{
14384
0
  return attr[Tag_DIV_use].i == 1;
14385
0
}
14386
14387
/* Merge EABI object attributes from IBFD into OBFD.  Raise an error if there
14388
   are conflicting attributes.  */
14389
14390
static bool
14391
elf32_arm_merge_eabi_attributes (bfd *ibfd, struct bfd_link_info *info)
14392
0
{
14393
0
  bfd *obfd = info->output_bfd;
14394
0
  obj_attribute *in_attr;
14395
0
  obj_attribute *out_attr;
14396
  /* Some tags have 0 = don't care, 1 = strong requirement,
14397
     2 = weak requirement.  */
14398
0
  static const int order_021[3] = {0, 2, 1};
14399
0
  int i;
14400
0
  bool result = true;
14401
0
  const char *sec_name = get_elf_backend_data (ibfd)->obj_attrs_section;
14402
14403
  /* Skip the linker stubs file.  This preserves previous behavior
14404
     of accepting unknown attributes in the first input file - but
14405
     is that a bug?  */
14406
0
  if (ibfd->flags & BFD_LINKER_CREATED)
14407
0
    return true;
14408
14409
  /* Skip any input that hasn't attribute section.
14410
     This enables to link object files without attribute section with
14411
     any others.  */
14412
0
  if (bfd_get_section_by_name (ibfd, sec_name) == NULL)
14413
0
    return true;
14414
14415
0
  if (!elf_known_obj_attributes_proc (obfd)[0].i)
14416
0
    {
14417
      /* This is the first object.  Copy the attributes.  */
14418
0
      _bfd_elf_copy_obj_attributes (ibfd, obfd);
14419
14420
0
      out_attr = elf_known_obj_attributes_proc (obfd);
14421
14422
      /* Use the Tag_null value to indicate the attributes have been
14423
   initialized.  */
14424
0
      out_attr[0].i = 1;
14425
14426
      /* We do not output objects with Tag_MPextension_use_legacy - we move
14427
   the attribute's value to Tag_MPextension_use.  */
14428
0
      if (out_attr[Tag_MPextension_use_legacy].i != 0)
14429
0
  {
14430
0
    if (out_attr[Tag_MPextension_use].i != 0
14431
0
        && out_attr[Tag_MPextension_use_legacy].i
14432
0
    != out_attr[Tag_MPextension_use].i)
14433
0
      {
14434
0
        _bfd_error_handler
14435
0
    (_("Error: %pB has both the current and legacy "
14436
0
       "Tag_MPextension_use attributes"), ibfd);
14437
0
        result = false;
14438
0
      }
14439
14440
0
    out_attr[Tag_MPextension_use] =
14441
0
      out_attr[Tag_MPextension_use_legacy];
14442
0
    out_attr[Tag_MPextension_use_legacy].type = 0;
14443
0
    out_attr[Tag_MPextension_use_legacy].i = 0;
14444
0
  }
14445
14446
      /* PR 28859 and 28848:  Handle the case where the first input file,
14447
   eg crti.o, has a Tag_ABI_HardFP_use of 3 but no Tag_FP_arch set.
14448
   Using Tag_ABI_HardFP_use in this way is deprecated, so reset the
14449
   attribute to zero.
14450
   FIXME: Should we handle other non-zero values of Tag_ABI_HardFO_use ? */
14451
0
      if (out_attr[Tag_ABI_HardFP_use].i == 3 && out_attr[Tag_FP_arch].i == 0)
14452
0
  out_attr[Tag_ABI_HardFP_use].i = 0;
14453
14454
0
      return result;
14455
0
    }
14456
14457
0
  in_attr = elf_known_obj_attributes_proc (ibfd);
14458
0
  out_attr = elf_known_obj_attributes_proc (obfd);
14459
  /* This needs to happen before Tag_ABI_FP_number_model is merged.  */
14460
0
  if (in_attr[Tag_ABI_VFP_args].i != out_attr[Tag_ABI_VFP_args].i)
14461
0
    {
14462
      /* Ignore mismatches if the object doesn't use floating point or is
14463
   floating point ABI independent.  */
14464
0
      if (out_attr[Tag_ABI_FP_number_model].i == AEABI_FP_number_model_none
14465
0
    || (in_attr[Tag_ABI_FP_number_model].i != AEABI_FP_number_model_none
14466
0
        && out_attr[Tag_ABI_VFP_args].i == AEABI_VFP_args_compatible))
14467
0
  out_attr[Tag_ABI_VFP_args].i = in_attr[Tag_ABI_VFP_args].i;
14468
0
      else if (in_attr[Tag_ABI_FP_number_model].i != AEABI_FP_number_model_none
14469
0
         && in_attr[Tag_ABI_VFP_args].i != AEABI_VFP_args_compatible)
14470
0
  {
14471
0
    _bfd_error_handler
14472
0
      (_("error: %pB uses VFP register arguments, %pB does not"),
14473
0
       in_attr[Tag_ABI_VFP_args].i ? ibfd : obfd,
14474
0
       in_attr[Tag_ABI_VFP_args].i ? obfd : ibfd);
14475
0
    result = false;
14476
0
  }
14477
0
    }
14478
14479
0
  for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
14480
0
    {
14481
      /* Merge this attribute with existing attributes.  */
14482
0
      switch (i)
14483
0
  {
14484
0
  case Tag_CPU_raw_name:
14485
0
  case Tag_CPU_name:
14486
    /* These are merged after Tag_CPU_arch.  */
14487
0
    break;
14488
14489
0
  case Tag_ABI_optimization_goals:
14490
0
  case Tag_ABI_FP_optimization_goals:
14491
    /* Use the first value seen.  */
14492
0
    break;
14493
14494
0
  case Tag_CPU_arch:
14495
0
    {
14496
0
      int secondary_compat = -1, secondary_compat_out = -1;
14497
0
      unsigned int saved_out_attr = out_attr[i].i;
14498
0
      int arch_attr;
14499
0
      static const char *const name_table[] =
14500
0
        {
14501
    /* These aren't real CPU names, but we can't guess
14502
       that from the architecture version alone.  */
14503
0
    "Pre v4",
14504
0
    "ARM v4",
14505
0
    "ARM v4T",
14506
0
    "ARM v5T",
14507
0
    "ARM v5TE",
14508
0
    "ARM v5TEJ",
14509
0
    "ARM v6",
14510
0
    "ARM v6KZ",
14511
0
    "ARM v6T2",
14512
0
    "ARM v6K",
14513
0
    "ARM v7",
14514
0
    "ARM v6-M",
14515
0
    "ARM v6S-M",
14516
0
    "ARM v7E-M",
14517
0
    "ARM v8",
14518
0
    "ARM v8-R",
14519
0
    "ARM v8-M.baseline",
14520
0
    "ARM v8-M.mainline",
14521
0
    "ARM v8.1-A",
14522
0
    "ARM v8.2-A",
14523
0
    "ARM v8.3-A",
14524
0
    "ARM v8.1-M.mainline",
14525
0
    "ARM v9",
14526
0
      };
14527
14528
      /* Merge Tag_CPU_arch and Tag_also_compatible_with.  */
14529
0
      secondary_compat = get_secondary_compatible_arch (ibfd);
14530
0
      secondary_compat_out = get_secondary_compatible_arch (obfd);
14531
0
      arch_attr = tag_cpu_arch_combine (ibfd, out_attr[i].i,
14532
0
                &secondary_compat_out,
14533
0
                in_attr[i].i,
14534
0
                secondary_compat,
14535
0
                name_table);
14536
14537
      /* Return with error if failed to merge.  */
14538
0
      if (arch_attr == -1)
14539
0
        return false;
14540
14541
0
      out_attr[i].i = arch_attr;
14542
14543
0
      set_secondary_compatible_arch (obfd, secondary_compat_out);
14544
14545
      /* Merge Tag_CPU_name and Tag_CPU_raw_name.  */
14546
0
      if (out_attr[i].i == saved_out_attr)
14547
0
        ; /* Leave the names alone.  */
14548
0
      else if (out_attr[i].i == in_attr[i].i)
14549
0
        {
14550
    /* The output architecture has been changed to match the
14551
       input architecture.  Use the input names.  */
14552
0
    out_attr[Tag_CPU_name].s = in_attr[Tag_CPU_name].s
14553
0
      ? _bfd_elf_attr_strdup (obfd, in_attr[Tag_CPU_name].s)
14554
0
      : NULL;
14555
0
    out_attr[Tag_CPU_raw_name].s = in_attr[Tag_CPU_raw_name].s
14556
0
      ? _bfd_elf_attr_strdup (obfd, in_attr[Tag_CPU_raw_name].s)
14557
0
      : NULL;
14558
0
        }
14559
0
      else
14560
0
        {
14561
0
    out_attr[Tag_CPU_name].s = NULL;
14562
0
    out_attr[Tag_CPU_raw_name].s = NULL;
14563
0
        }
14564
14565
      /* If we still don't have a value for Tag_CPU_name,
14566
         make one up now.  Tag_CPU_raw_name remains blank.  */
14567
0
      if (out_attr[Tag_CPU_name].s == NULL
14568
0
    && out_attr[i].i < ARRAY_SIZE (name_table))
14569
0
        out_attr[Tag_CPU_name].s =
14570
0
    _bfd_elf_attr_strdup (obfd, name_table[out_attr[i].i]);
14571
0
    }
14572
0
    break;
14573
14574
0
  case Tag_ARM_ISA_use:
14575
0
  case Tag_THUMB_ISA_use:
14576
0
  case Tag_WMMX_arch:
14577
0
  case Tag_Advanced_SIMD_arch:
14578
    /* ??? Do Advanced_SIMD (NEON) and WMMX conflict?  */
14579
0
  case Tag_ABI_FP_rounding:
14580
0
  case Tag_ABI_FP_exceptions:
14581
0
  case Tag_ABI_FP_user_exceptions:
14582
0
  case Tag_ABI_FP_number_model:
14583
0
  case Tag_FP_HP_extension:
14584
0
  case Tag_CPU_unaligned_access:
14585
0
  case Tag_T2EE_use:
14586
0
  case Tag_MPextension_use:
14587
0
  case Tag_MVE_arch:
14588
0
  case Tag_PAC_extension:
14589
0
  case Tag_BTI_extension:
14590
0
  case Tag_BTI_use:
14591
0
  case Tag_PACRET_use:
14592
    /* Use the largest value specified.  */
14593
0
    if (in_attr[i].i > out_attr[i].i)
14594
0
      out_attr[i].i = in_attr[i].i;
14595
0
    break;
14596
14597
0
  case Tag_ABI_align_preserved:
14598
0
  case Tag_ABI_PCS_RO_data:
14599
    /* Use the smallest value specified.  */
14600
0
    if (in_attr[i].i < out_attr[i].i)
14601
0
      out_attr[i].i = in_attr[i].i;
14602
0
    break;
14603
14604
0
  case Tag_ABI_align_needed:
14605
0
    if ((in_attr[i].i > 0 || out_attr[i].i > 0)
14606
0
        && (in_attr[Tag_ABI_align_preserved].i == 0
14607
0
      || out_attr[Tag_ABI_align_preserved].i == 0))
14608
0
      {
14609
        /* This error message should be enabled once all non-conformant
14610
     binaries in the toolchain have had the attributes set
14611
     properly.
14612
        _bfd_error_handler
14613
    (_("error: %pB: 8-byte data alignment conflicts with %pB"),
14614
     obfd, ibfd);
14615
        result = false; */
14616
0
      }
14617
    /* Fall through.  */
14618
0
  case Tag_ABI_FP_denormal:
14619
0
  case Tag_ABI_PCS_GOT_use:
14620
    /* Use the "greatest" from the sequence 0, 2, 1, or the largest
14621
       value if greater than 2 (for future-proofing).  */
14622
0
    if ((in_attr[i].i > 2 && in_attr[i].i > out_attr[i].i)
14623
0
        || (in_attr[i].i <= 2 && out_attr[i].i <= 2
14624
0
      && order_021[in_attr[i].i] > order_021[out_attr[i].i]))
14625
0
      out_attr[i].i = in_attr[i].i;
14626
0
    break;
14627
14628
0
  case Tag_Virtualization_use:
14629
    /* The virtualization tag effectively stores two bits of
14630
       information: the intended use of TrustZone (in bit 0), and the
14631
       intended use of Virtualization (in bit 1).  */
14632
0
    if (out_attr[i].i == 0)
14633
0
      out_attr[i].i = in_attr[i].i;
14634
0
    else if (in_attr[i].i != 0
14635
0
       && in_attr[i].i != out_attr[i].i)
14636
0
      {
14637
0
        if (in_attr[i].i <= 3 && out_attr[i].i <= 3)
14638
0
    out_attr[i].i = 3;
14639
0
        else
14640
0
    {
14641
0
      _bfd_error_handler
14642
0
        (_("error: %pB: unable to merge virtualization attributes "
14643
0
           "with %pB"),
14644
0
         obfd, ibfd);
14645
0
      result = false;
14646
0
    }
14647
0
      }
14648
0
    break;
14649
14650
0
  case Tag_CPU_arch_profile:
14651
0
    if (out_attr[i].i != in_attr[i].i)
14652
0
      {
14653
        /* 0 will merge with anything.
14654
     'A' and 'S' merge to 'A'.
14655
     'R' and 'S' merge to 'R'.
14656
     'M' and 'A|R|S' is an error.  */
14657
0
        if (out_attr[i].i == 0
14658
0
      || (out_attr[i].i == 'S'
14659
0
          && (in_attr[i].i == 'A' || in_attr[i].i == 'R')))
14660
0
    out_attr[i].i = in_attr[i].i;
14661
0
        else if (in_attr[i].i == 0
14662
0
           || (in_attr[i].i == 'S'
14663
0
         && (out_attr[i].i == 'A' || out_attr[i].i == 'R')))
14664
0
    ; /* Do nothing.  */
14665
0
        else
14666
0
    {
14667
0
      _bfd_error_handler
14668
0
        (_("error: %pB: conflicting architecture profiles %c/%c"),
14669
0
         ibfd,
14670
0
         in_attr[i].i ? in_attr[i].i : '0',
14671
0
         out_attr[i].i ? out_attr[i].i : '0');
14672
0
      result = false;
14673
0
    }
14674
0
      }
14675
0
    break;
14676
14677
0
  case Tag_DSP_extension:
14678
    /* No need to change output value if any of:
14679
       - pre (<=) ARMv5T input architecture (do not have DSP)
14680
       - M input profile not ARMv7E-M and do not have DSP.  */
14681
0
    if (in_attr[Tag_CPU_arch].i <= 3
14682
0
        || (in_attr[Tag_CPU_arch_profile].i == 'M'
14683
0
      && in_attr[Tag_CPU_arch].i != 13
14684
0
      && in_attr[i].i == 0))
14685
0
      ; /* Do nothing.  */
14686
    /* Output value should be 0 if DSP part of architecture, ie.
14687
       - post (>=) ARMv5te architecture output
14688
       - A, R or S profile output or ARMv7E-M output architecture.  */
14689
0
    else if (out_attr[Tag_CPU_arch].i >= 4
14690
0
       && (out_attr[Tag_CPU_arch_profile].i == 'A'
14691
0
           || out_attr[Tag_CPU_arch_profile].i == 'R'
14692
0
           || out_attr[Tag_CPU_arch_profile].i == 'S'
14693
0
           || out_attr[Tag_CPU_arch].i == 13))
14694
0
      out_attr[i].i = 0;
14695
    /* Otherwise, DSP instructions are added and not part of output
14696
       architecture.  */
14697
0
    else
14698
0
      out_attr[i].i = 1;
14699
0
    break;
14700
14701
0
  case Tag_FP_arch:
14702
0
      {
14703
        /* Tag_ABI_HardFP_use is handled along with Tag_FP_arch since
14704
     the meaning of Tag_ABI_HardFP_use depends on Tag_FP_arch
14705
     when it's 0.  It might mean absence of FP hardware if
14706
     Tag_FP_arch is zero.  */
14707
14708
0
#define VFP_VERSION_COUNT 9
14709
0
        static const struct
14710
0
        {
14711
0
      int ver;
14712
0
      int regs;
14713
0
        } vfp_versions[VFP_VERSION_COUNT] =
14714
0
    {
14715
0
      {0, 0},
14716
0
      {1, 16},
14717
0
      {2, 16},
14718
0
      {3, 32},
14719
0
      {3, 16},
14720
0
      {4, 32},
14721
0
      {4, 16},
14722
0
      {8, 32},
14723
0
      {8, 16}
14724
0
    };
14725
0
        int ver;
14726
0
        int regs;
14727
0
        int newval;
14728
14729
        /* If the output has no requirement about FP hardware,
14730
     follow the requirement of the input.  */
14731
0
        if (out_attr[i].i == 0)
14732
0
    {
14733
      /* This assert is still reasonable, we shouldn't
14734
         produce the suspicious build attribute
14735
         combination (See below for in_attr).  */
14736
0
      BFD_ASSERT (out_attr[Tag_ABI_HardFP_use].i == 0);
14737
0
      out_attr[i].i = in_attr[i].i;
14738
0
      out_attr[Tag_ABI_HardFP_use].i
14739
0
        = in_attr[Tag_ABI_HardFP_use].i;
14740
0
      break;
14741
0
    }
14742
        /* If the input has no requirement about FP hardware, do
14743
     nothing.  */
14744
0
        else if (in_attr[i].i == 0)
14745
0
    {
14746
      /* We used to assert that Tag_ABI_HardFP_use was
14747
         zero here, but we should never assert when
14748
         consuming an object file that has suspicious
14749
         build attributes.  The single precision variant
14750
         of 'no FP architecture' is still 'no FP
14751
         architecture', so we just ignore the tag in this
14752
         case.  */
14753
0
      break;
14754
0
    }
14755
14756
        /* Both the input and the output have nonzero Tag_FP_arch.
14757
     So Tag_ABI_HardFP_use is implied by Tag_FP_arch when it's zero.  */
14758
14759
        /* If both the input and the output have zero Tag_ABI_HardFP_use,
14760
     do nothing.  */
14761
0
        if (in_attr[Tag_ABI_HardFP_use].i == 0
14762
0
      && out_attr[Tag_ABI_HardFP_use].i == 0)
14763
0
    ;
14764
        /* If the input and the output have different Tag_ABI_HardFP_use,
14765
     the combination of them is 0 (implied by Tag_FP_arch).  */
14766
0
        else if (in_attr[Tag_ABI_HardFP_use].i
14767
0
           != out_attr[Tag_ABI_HardFP_use].i)
14768
0
    out_attr[Tag_ABI_HardFP_use].i = 0;
14769
14770
        /* Now we can handle Tag_FP_arch.  */
14771
14772
        /* Values of VFP_VERSION_COUNT or more aren't defined, so just
14773
     pick the biggest.  */
14774
0
        if (in_attr[i].i >= VFP_VERSION_COUNT
14775
0
      && in_attr[i].i > out_attr[i].i)
14776
0
    {
14777
0
      out_attr[i] = in_attr[i];
14778
0
      break;
14779
0
    }
14780
        /* The output uses the superset of input features
14781
     (ISA version) and registers.  */
14782
0
        ver = vfp_versions[in_attr[i].i].ver;
14783
0
        if (ver < vfp_versions[out_attr[i].i].ver)
14784
0
    ver = vfp_versions[out_attr[i].i].ver;
14785
0
        regs = vfp_versions[in_attr[i].i].regs;
14786
0
        if (regs < vfp_versions[out_attr[i].i].regs)
14787
0
    regs = vfp_versions[out_attr[i].i].regs;
14788
        /* This assumes all possible supersets are also a valid
14789
     options.  */
14790
0
        for (newval = VFP_VERSION_COUNT - 1; newval > 0; newval--)
14791
0
    {
14792
0
      if (regs == vfp_versions[newval].regs
14793
0
          && ver == vfp_versions[newval].ver)
14794
0
        break;
14795
0
    }
14796
0
        out_attr[i].i = newval;
14797
0
      }
14798
0
    break;
14799
0
  case Tag_PCS_config:
14800
0
    if (out_attr[i].i == 0)
14801
0
      out_attr[i].i = in_attr[i].i;
14802
0
    else if (in_attr[i].i != 0 && out_attr[i].i != in_attr[i].i)
14803
0
      {
14804
        /* It's sometimes ok to mix different configs, so this is only
14805
     a warning.  */
14806
0
        _bfd_error_handler
14807
0
    (_("warning: %pB: conflicting platform configuration"), ibfd);
14808
0
      }
14809
0
    break;
14810
0
  case Tag_ABI_PCS_R9_use:
14811
0
    if (in_attr[i].i != out_attr[i].i
14812
0
        && out_attr[i].i != AEABI_R9_unused
14813
0
        && in_attr[i].i != AEABI_R9_unused)
14814
0
      {
14815
0
        _bfd_error_handler
14816
0
    (_("error: %pB: conflicting use of R9"), ibfd);
14817
0
        result = false;
14818
0
      }
14819
0
    if (out_attr[i].i == AEABI_R9_unused)
14820
0
      out_attr[i].i = in_attr[i].i;
14821
0
    break;
14822
0
  case Tag_ABI_PCS_RW_data:
14823
0
    if (in_attr[i].i == AEABI_PCS_RW_data_SBrel
14824
0
        && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_SB
14825
0
        && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_unused)
14826
0
      {
14827
0
        _bfd_error_handler
14828
0
    (_("error: %pB: SB relative addressing conflicts with use of R9"),
14829
0
     ibfd);
14830
0
        result = false;
14831
0
      }
14832
    /* Use the smallest value specified.  */
14833
0
    if (in_attr[i].i < out_attr[i].i)
14834
0
      out_attr[i].i = in_attr[i].i;
14835
0
    break;
14836
0
  case Tag_ABI_PCS_wchar_t:
14837
0
    if (out_attr[i].i && in_attr[i].i && out_attr[i].i != in_attr[i].i
14838
0
        && !elf_arm_tdata (obfd)->no_wchar_size_warning)
14839
0
      {
14840
0
        _bfd_error_handler
14841
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"),
14842
0
     ibfd, in_attr[i].i, out_attr[i].i);
14843
0
      }
14844
0
    else if (in_attr[i].i && !out_attr[i].i)
14845
0
      out_attr[i].i = in_attr[i].i;
14846
0
    break;
14847
0
  case Tag_ABI_enum_size:
14848
0
    if (in_attr[i].i != AEABI_enum_unused)
14849
0
      {
14850
0
        if (out_attr[i].i == AEABI_enum_unused
14851
0
      || out_attr[i].i == AEABI_enum_forced_wide)
14852
0
    {
14853
      /* The existing object is compatible with anything.
14854
         Use whatever requirements the new object has.  */
14855
0
      out_attr[i].i = in_attr[i].i;
14856
0
    }
14857
0
        else if (in_attr[i].i != AEABI_enum_forced_wide
14858
0
           && out_attr[i].i != in_attr[i].i
14859
0
           && !elf_arm_tdata (obfd)->no_enum_size_warning)
14860
0
    {
14861
0
      static const char *aeabi_enum_names[] =
14862
0
        { "", "variable-size", "32-bit", "" };
14863
0
      const char *in_name =
14864
0
        in_attr[i].i < ARRAY_SIZE (aeabi_enum_names)
14865
0
        ? aeabi_enum_names[in_attr[i].i]
14866
0
        : "<unknown>";
14867
0
      const char *out_name =
14868
0
        out_attr[i].i < ARRAY_SIZE (aeabi_enum_names)
14869
0
        ? aeabi_enum_names[out_attr[i].i]
14870
0
        : "<unknown>";
14871
0
      _bfd_error_handler
14872
0
        (_("warning: %pB uses %s enums yet the output is to use %s enums; use of enum values across objects may fail"),
14873
0
         ibfd, in_name, out_name);
14874
0
    }
14875
0
      }
14876
0
    break;
14877
0
  case Tag_ABI_VFP_args:
14878
    /* Aready done.  */
14879
0
    break;
14880
0
  case Tag_ABI_WMMX_args:
14881
0
    if (in_attr[i].i != out_attr[i].i)
14882
0
      {
14883
0
        _bfd_error_handler
14884
0
    (_("error: %pB uses iWMMXt register arguments, %pB does not"),
14885
0
     ibfd, obfd);
14886
0
        result = false;
14887
0
      }
14888
0
    break;
14889
0
  case Tag_compatibility:
14890
    /* Merged in target-independent code.  */
14891
0
    break;
14892
0
  case Tag_ABI_HardFP_use:
14893
    /* This is handled along with Tag_FP_arch.  */
14894
0
    break;
14895
0
  case Tag_ABI_FP_16bit_format:
14896
0
    if (in_attr[i].i != 0 && out_attr[i].i != 0)
14897
0
      {
14898
0
        if (in_attr[i].i != out_attr[i].i)
14899
0
    {
14900
0
      _bfd_error_handler
14901
0
        (_("error: fp16 format mismatch between %pB and %pB"),
14902
0
         ibfd, obfd);
14903
0
      result = false;
14904
0
    }
14905
0
      }
14906
0
    if (in_attr[i].i != 0)
14907
0
      out_attr[i].i = in_attr[i].i;
14908
0
    break;
14909
14910
0
  case Tag_DIV_use:
14911
    /* A value of zero on input means that the divide instruction may
14912
       be used if available in the base architecture as specified via
14913
       Tag_CPU_arch and Tag_CPU_arch_profile.  A value of 1 means that
14914
       the user did not want divide instructions.  A value of 2
14915
       explicitly means that divide instructions were allowed in ARM
14916
       and Thumb state.  */
14917
0
    if (in_attr[i].i == out_attr[i].i)
14918
0
      /* Do nothing.  */ ;
14919
0
    else if (elf32_arm_attributes_forbid_div (in_attr)
14920
0
       && !elf32_arm_attributes_accept_div (out_attr))
14921
0
      out_attr[i].i = 1;
14922
0
    else if (elf32_arm_attributes_forbid_div (out_attr)
14923
0
       && elf32_arm_attributes_accept_div (in_attr))
14924
0
      out_attr[i].i = in_attr[i].i;
14925
0
    else if (in_attr[i].i == 2)
14926
0
      out_attr[i].i = in_attr[i].i;
14927
0
    break;
14928
14929
0
  case Tag_MPextension_use_legacy:
14930
    /* We don't output objects with Tag_MPextension_use_legacy - we
14931
       move the value to Tag_MPextension_use.  */
14932
0
    if (in_attr[i].i != 0 && in_attr[Tag_MPextension_use].i != 0)
14933
0
      {
14934
0
        if (in_attr[Tag_MPextension_use].i != in_attr[i].i)
14935
0
    {
14936
0
      _bfd_error_handler
14937
0
        (_("%pB has both the current and legacy "
14938
0
           "Tag_MPextension_use attributes"),
14939
0
         ibfd);
14940
0
      result = false;
14941
0
    }
14942
0
      }
14943
14944
0
    if (in_attr[i].i > out_attr[Tag_MPextension_use].i)
14945
0
      out_attr[Tag_MPextension_use] = in_attr[i];
14946
14947
0
    break;
14948
14949
0
  case Tag_nodefaults:
14950
    /* This tag is set if it exists, but the value is unused (and is
14951
       typically zero).  We don't actually need to do anything here -
14952
       the merge happens automatically when the type flags are merged
14953
       below.  */
14954
0
    break;
14955
0
  case Tag_also_compatible_with:
14956
    /* Already done in Tag_CPU_arch.  */
14957
0
    break;
14958
0
  case Tag_conformance:
14959
    /* Keep the attribute if it matches.  Throw it away otherwise.
14960
       No attribute means no claim to conform.  */
14961
0
    if (!in_attr[i].s || !out_attr[i].s
14962
0
        || strcmp (in_attr[i].s, out_attr[i].s) != 0)
14963
0
      out_attr[i].s = NULL;
14964
0
    break;
14965
14966
0
  default:
14967
0
    result
14968
0
      = result && _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i);
14969
0
  }
14970
14971
      /* If out_attr was copied from in_attr then it won't have a type yet.  */
14972
0
      if (in_attr[i].type && !out_attr[i].type)
14973
0
  out_attr[i].type = in_attr[i].type;
14974
0
    }
14975
14976
  /* Merge Tag_compatibility attributes and any common GNU ones.  */
14977
0
  if (!_bfd_elf_merge_object_attributes (ibfd, info))
14978
0
    return false;
14979
14980
  /* Check for any attributes not known on ARM.  */
14981
0
  result &= _bfd_elf_merge_unknown_attribute_list (ibfd, obfd);
14982
14983
0
  return result;
14984
0
}
14985
14986
14987
/* Return TRUE if the two EABI versions are incompatible.  */
14988
14989
static bool
14990
elf32_arm_versions_compatible (unsigned iver, unsigned over)
14991
0
{
14992
  /* v4 and v5 are the same spec before and after it was released,
14993
     so allow mixing them.  */
14994
0
  if ((iver == EF_ARM_EABI_VER4 && over == EF_ARM_EABI_VER5)
14995
0
      || (iver == EF_ARM_EABI_VER5 && over == EF_ARM_EABI_VER4))
14996
0
    return true;
14997
14998
0
  return (iver == over);
14999
0
}
15000
15001
/* Merge backend specific data from an object file to the output
15002
   object file when linking.  */
15003
15004
static bool
15005
elf32_arm_merge_private_bfd_data (bfd *, struct bfd_link_info *);
15006
15007
/* Display the flags field.  */
15008
15009
static bool
15010
elf32_arm_print_private_bfd_data (bfd *abfd, void * ptr)
15011
522
{
15012
522
  FILE * file = (FILE *) ptr;
15013
522
  unsigned long flags;
15014
15015
522
  BFD_ASSERT (abfd != NULL && ptr != NULL);
15016
15017
  /* Print normal ELF private data.  */
15018
522
  _bfd_elf_print_private_bfd_data (abfd, ptr);
15019
15020
522
  flags = elf_elfheader (abfd)->e_flags;
15021
  /* Ignore init flag - it may not be set, despite the flags field
15022
     containing valid data.  */
15023
15024
522
  fprintf (file, _("private flags = 0x%lx:"), elf_elfheader (abfd)->e_flags);
15025
15026
522
  switch (EF_ARM_EABI_VERSION (flags))
15027
522
    {
15028
457
    case EF_ARM_EABI_UNKNOWN:
15029
      /* The following flag bits are GNU extensions and not part of the
15030
   official ARM ELF extended ABI.  Hence they are only decoded if
15031
   the EABI version is not set.  */
15032
457
      if (flags & EF_ARM_INTERWORK)
15033
88
  fprintf (file, _(" [interworking enabled]"));
15034
15035
457
      if (flags & EF_ARM_APCS_26)
15036
88
  fprintf (file, " [APCS-26]");
15037
369
      else
15038
369
  fprintf (file, " [APCS-32]");
15039
15040
457
      if (flags & EF_ARM_VFP_FLOAT)
15041
39
  fprintf (file, _(" [VFP float format]"));
15042
418
      else
15043
418
  fprintf (file, _(" [FPA float format]"));
15044
15045
457
      if (flags & EF_ARM_APCS_FLOAT)
15046
46
  fprintf (file, _(" [floats passed in float registers]"));
15047
15048
457
      if (flags & EF_ARM_PIC)
15049
62
  fprintf (file, _(" [position independent]"));
15050
15051
457
      if (flags & EF_ARM_NEW_ABI)
15052
46
  fprintf (file, _(" [new ABI]"));
15053
15054
457
      if (flags & EF_ARM_OLD_ABI)
15055
362
  fprintf (file, _(" [old ABI]"));
15056
15057
457
      if (flags & EF_ARM_SOFT_FLOAT)
15058
13
  fprintf (file, _(" [software FP]"));
15059
15060
457
      flags &= ~(EF_ARM_INTERWORK | EF_ARM_APCS_26 | EF_ARM_APCS_FLOAT
15061
457
     | EF_ARM_PIC | EF_ARM_NEW_ABI | EF_ARM_OLD_ABI
15062
457
     | EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT);
15063
457
      break;
15064
15065
2
    case EF_ARM_EABI_VER1:
15066
2
      fprintf (file, _(" [Version1 EABI]"));
15067
15068
2
      if (flags & EF_ARM_SYMSARESORTED)
15069
0
  fprintf (file, _(" [sorted symbol table]"));
15070
2
      else
15071
2
  fprintf (file, _(" [unsorted symbol table]"));
15072
15073
2
      flags &= ~ EF_ARM_SYMSARESORTED;
15074
2
      break;
15075
15076
3
    case EF_ARM_EABI_VER2:
15077
3
      fprintf (file, _(" [Version2 EABI]"));
15078
15079
3
      if (flags & EF_ARM_SYMSARESORTED)
15080
0
  fprintf (file, _(" [sorted symbol table]"));
15081
3
      else
15082
3
  fprintf (file, _(" [unsorted symbol table]"));
15083
15084
3
      if (flags & EF_ARM_DYNSYMSUSESEGIDX)
15085
0
  fprintf (file, _(" [dynamic symbols use segment index]"));
15086
15087
3
      if (flags & EF_ARM_MAPSYMSFIRST)
15088
0
  fprintf (file, _(" [mapping symbols precede others]"));
15089
15090
3
      flags &= ~(EF_ARM_SYMSARESORTED | EF_ARM_DYNSYMSUSESEGIDX
15091
3
     | EF_ARM_MAPSYMSFIRST);
15092
3
      break;
15093
15094
35
    case EF_ARM_EABI_VER3:
15095
35
      fprintf (file, _(" [Version3 EABI]"));
15096
35
      break;
15097
15098
0
    case EF_ARM_EABI_VER4:
15099
0
      fprintf (file, _(" [Version4 EABI]"));
15100
0
      goto eabi;
15101
15102
0
    case EF_ARM_EABI_VER5:
15103
0
      fprintf (file, _(" [Version5 EABI]"));
15104
15105
0
      if (flags & EF_ARM_ABI_FLOAT_SOFT)
15106
0
  fprintf (file, _(" [soft-float ABI]"));
15107
15108
0
      if (flags & EF_ARM_ABI_FLOAT_HARD)
15109
0
  fprintf (file, _(" [hard-float ABI]"));
15110
15111
0
      flags &= ~(EF_ARM_ABI_FLOAT_SOFT | EF_ARM_ABI_FLOAT_HARD);
15112
15113
0
    eabi:
15114
0
      if (flags & EF_ARM_BE8)
15115
0
  fprintf (file, _(" [BE8]"));
15116
15117
0
      if (flags & EF_ARM_LE8)
15118
0
  fprintf (file, _(" [LE8]"));
15119
15120
0
      flags &= ~(EF_ARM_LE8 | EF_ARM_BE8);
15121
0
      break;
15122
15123
25
    default:
15124
25
      fprintf (file, _(" <EABI version unrecognised>"));
15125
25
      break;
15126
522
    }
15127
15128
522
  flags &= ~ EF_ARM_EABIMASK;
15129
15130
522
  if (flags & EF_ARM_RELEXEC)
15131
89
    fprintf (file, _(" [relocatable executable]"));
15132
15133
522
  if (flags & EF_ARM_PIC)
15134
2
    fprintf (file, _(" [position independent]"));
15135
15136
522
  if (elf_elfheader (abfd)->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC)
15137
0
    fprintf (file, _(" [FDPIC ABI supplement]"));
15138
15139
522
  flags &= ~ (EF_ARM_RELEXEC | EF_ARM_PIC);
15140
15141
522
  if (flags)
15142
475
    fprintf (file, _(" <Unrecognised flag bits set>"));
15143
15144
522
  fputc ('\n', file);
15145
15146
522
  return true;
15147
522
}
15148
15149
static int
15150
elf32_arm_get_symbol_type (Elf_Internal_Sym * elf_sym, int type)
15151
18
{
15152
18
  switch (ELF_ST_TYPE (elf_sym->st_info))
15153
18
    {
15154
0
    case STT_ARM_TFUNC:
15155
0
      return ELF_ST_TYPE (elf_sym->st_info);
15156
15157
1
    case STT_ARM_16BIT:
15158
      /* If the symbol is not an object, return the STT_ARM_16BIT flag.
15159
   This allows us to distinguish between data used by Thumb instructions
15160
   and non-data (which is probably code) inside Thumb regions of an
15161
   executable.  */
15162
1
      if (type != STT_OBJECT && type != STT_TLS)
15163
1
  return ELF_ST_TYPE (elf_sym->st_info);
15164
0
      break;
15165
15166
17
    default:
15167
17
      break;
15168
18
    }
15169
15170
17
  return type;
15171
18
}
15172
15173
static asection *
15174
elf32_arm_gc_mark_hook (asection *sec,
15175
      struct bfd_link_info *info,
15176
      struct elf_reloc_cookie *cookie,
15177
      struct elf_link_hash_entry *h,
15178
      unsigned int symndx)
15179
0
{
15180
0
  if (h != NULL)
15181
0
    switch (ELF32_R_TYPE (cookie->rel->r_info))
15182
0
      {
15183
0
      case R_ARM_GNU_VTINHERIT:
15184
0
      case R_ARM_GNU_VTENTRY:
15185
0
  return NULL;
15186
0
      }
15187
15188
0
  return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
15189
0
}
15190
15191
/* Look through the relocs for a section during the first phase.  */
15192
15193
static bool
15194
elf32_arm_check_relocs (bfd *abfd, struct bfd_link_info *info,
15195
      asection *sec, const Elf_Internal_Rela *relocs)
15196
0
{
15197
0
  Elf_Internal_Shdr *symtab_hdr;
15198
0
  struct elf_link_hash_entry **sym_hashes;
15199
0
  const Elf_Internal_Rela *rel;
15200
0
  const Elf_Internal_Rela *rel_end;
15201
0
  bfd *dynobj;
15202
0
  asection *sreloc;
15203
0
  struct elf32_arm_link_hash_table *htab;
15204
0
  bool call_reloc_p;
15205
0
  bool may_become_dynamic_p;
15206
0
  bool may_need_local_target_p;
15207
0
  unsigned long nsyms;
15208
15209
0
  if (bfd_link_relocatable (info))
15210
0
    return true;
15211
15212
0
  BFD_ASSERT (is_arm_elf (abfd));
15213
15214
0
  htab = elf32_arm_hash_table (info);
15215
0
  if (htab == NULL)
15216
0
    return false;
15217
15218
0
  sreloc = NULL;
15219
15220
0
  if (htab->root.dynobj == NULL)
15221
0
    htab->root.dynobj = abfd;
15222
0
  if (!create_ifunc_sections (info))
15223
0
    return false;
15224
15225
0
  dynobj = htab->root.dynobj;
15226
15227
0
  symtab_hdr = & elf_symtab_hdr (abfd);
15228
0
  sym_hashes = elf_sym_hashes (abfd);
15229
0
  nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
15230
15231
0
  rel_end = relocs + sec->reloc_count;
15232
0
  for (rel = relocs; rel < rel_end; rel++)
15233
0
    {
15234
0
      Elf_Internal_Sym *isym;
15235
0
      struct elf_link_hash_entry *h;
15236
0
      struct elf32_arm_link_hash_entry *eh;
15237
0
      unsigned int r_symndx;
15238
0
      int r_type;
15239
15240
0
      r_symndx = ELF32_R_SYM (rel->r_info);
15241
0
      r_type = ELF32_R_TYPE (rel->r_info);
15242
0
      r_type = arm_real_reloc_type (htab, r_type);
15243
15244
0
      if (r_symndx >= nsyms
15245
    /* PR 9934: It is possible to have relocations that do not
15246
       refer to symbols, thus it is also possible to have an
15247
       object file containing relocations but no symbol table.  */
15248
0
    && (r_symndx > STN_UNDEF || nsyms > 0))
15249
0
  {
15250
0
    _bfd_error_handler (_("%pB: bad symbol index: %d"), abfd,
15251
0
            r_symndx);
15252
0
    return false;
15253
0
  }
15254
15255
0
      h = NULL;
15256
0
      isym = NULL;
15257
0
      if (nsyms > 0)
15258
0
  {
15259
0
    if (r_symndx < symtab_hdr->sh_info)
15260
0
      {
15261
        /* A local symbol.  */
15262
0
        isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
15263
0
              abfd, r_symndx);
15264
0
        if (isym == NULL)
15265
0
    return false;
15266
0
      }
15267
0
    else
15268
0
      {
15269
0
        h = sym_hashes[r_symndx - symtab_hdr->sh_info];
15270
0
        while (h->root.type == bfd_link_hash_indirect
15271
0
         || h->root.type == bfd_link_hash_warning)
15272
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
15273
0
      }
15274
0
  }
15275
15276
0
      eh = (struct elf32_arm_link_hash_entry *) h;
15277
15278
0
      call_reloc_p = false;
15279
0
      may_become_dynamic_p = false;
15280
0
      may_need_local_target_p = false;
15281
15282
      /* Could be done earlier, if h were already available.  */
15283
0
      r_type = elf32_arm_tls_transition (info, r_type, h);
15284
0
      switch (r_type)
15285
0
  {
15286
0
  case R_ARM_GOTOFFFUNCDESC:
15287
0
    {
15288
0
      if (h == NULL)
15289
0
        {
15290
0
    if (!elf32_arm_allocate_local_sym_info (abfd))
15291
0
      return false;
15292
0
    if (r_symndx >= elf32_arm_num_entries (abfd))
15293
0
      return false;
15294
0
    elf32_arm_local_fdpic_cnts (abfd) [r_symndx].gotofffuncdesc_cnt += 1;
15295
0
    elf32_arm_local_fdpic_cnts (abfd) [r_symndx].funcdesc_offset = -1;
15296
0
        }
15297
0
      else
15298
0
        {
15299
0
    eh->fdpic_cnts.gotofffuncdesc_cnt++;
15300
0
        }
15301
0
    }
15302
0
    break;
15303
15304
0
  case R_ARM_GOTFUNCDESC:
15305
0
    {
15306
0
      if (h == NULL)
15307
0
        {
15308
    /* Such a relocation is not supposed to be generated
15309
       by gcc on a static function.  */
15310
    /* Anyway if needed it could be handled.  */
15311
0
    return false;
15312
0
        }
15313
0
      else
15314
0
        {
15315
0
    eh->fdpic_cnts.gotfuncdesc_cnt++;
15316
0
        }
15317
0
    }
15318
0
    break;
15319
15320
0
  case R_ARM_FUNCDESC:
15321
0
    {
15322
0
      if (h == NULL)
15323
0
        {
15324
0
    if (!elf32_arm_allocate_local_sym_info (abfd))
15325
0
      return false;
15326
0
    if (r_symndx >= elf32_arm_num_entries (abfd))
15327
0
      return false;
15328
0
    elf32_arm_local_fdpic_cnts (abfd) [r_symndx].funcdesc_cnt += 1;
15329
0
    elf32_arm_local_fdpic_cnts (abfd) [r_symndx].funcdesc_offset = -1;
15330
0
        }
15331
0
      else
15332
0
        {
15333
0
    eh->fdpic_cnts.funcdesc_cnt++;
15334
0
        }
15335
0
    }
15336
0
    break;
15337
15338
0
    case R_ARM_GOT32:
15339
0
    case R_ARM_GOT_PREL:
15340
0
    case R_ARM_TLS_GD32:
15341
0
    case R_ARM_TLS_GD32_FDPIC:
15342
0
    case R_ARM_TLS_IE32:
15343
0
    case R_ARM_TLS_IE32_FDPIC:
15344
0
    case R_ARM_TLS_GOTDESC:
15345
0
    case R_ARM_TLS_DESCSEQ:
15346
0
    case R_ARM_THM_TLS_DESCSEQ:
15347
0
    case R_ARM_TLS_CALL:
15348
0
    case R_ARM_THM_TLS_CALL:
15349
      /* This symbol requires a global offset table entry.  */
15350
0
      {
15351
0
        int tls_type, old_tls_type;
15352
15353
0
        switch (r_type)
15354
0
    {
15355
0
    case R_ARM_TLS_GD32: tls_type = GOT_TLS_GD; break;
15356
0
    case R_ARM_TLS_GD32_FDPIC: tls_type = GOT_TLS_GD; break;
15357
15358
0
    case R_ARM_TLS_IE32: tls_type = GOT_TLS_IE; break;
15359
0
    case R_ARM_TLS_IE32_FDPIC: tls_type = GOT_TLS_IE; break;
15360
15361
0
    case R_ARM_TLS_GOTDESC:
15362
0
    case R_ARM_TLS_CALL: case R_ARM_THM_TLS_CALL:
15363
0
    case R_ARM_TLS_DESCSEQ: case R_ARM_THM_TLS_DESCSEQ:
15364
0
      tls_type = GOT_TLS_GDESC; break;
15365
15366
0
    default: tls_type = GOT_NORMAL; break;
15367
0
    }
15368
15369
0
        if (!bfd_link_executable (info) && (tls_type & GOT_TLS_IE))
15370
0
    info->flags |= DF_STATIC_TLS;
15371
15372
0
        if (h != NULL)
15373
0
    {
15374
0
      h->got.refcount++;
15375
0
      old_tls_type = elf32_arm_hash_entry (h)->tls_type;
15376
0
    }
15377
0
        else
15378
0
    {
15379
      /* This is a global offset table entry for a local symbol.  */
15380
0
      if (!elf32_arm_allocate_local_sym_info (abfd))
15381
0
        return false;
15382
0
      if (r_symndx >= elf32_arm_num_entries (abfd))
15383
0
        {
15384
0
          _bfd_error_handler (_("%pB: bad symbol index: %d"), abfd,
15385
0
            r_symndx);
15386
0
          return false;
15387
0
        }
15388
15389
0
      elf_local_got_refcounts (abfd)[r_symndx] += 1;
15390
0
      old_tls_type = elf32_arm_local_got_tls_type (abfd) [r_symndx];
15391
0
    }
15392
15393
        /* If a variable is accessed with both tls methods, two
15394
     slots may be created.  */
15395
0
        if (GOT_TLS_GD_ANY_P (old_tls_type)
15396
0
      && GOT_TLS_GD_ANY_P (tls_type))
15397
0
    tls_type |= old_tls_type;
15398
15399
        /* We will already have issued an error message if there
15400
     is a TLS/non-TLS mismatch, based on the symbol
15401
     type.  So just combine any TLS types needed.  */
15402
0
        if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
15403
0
      && tls_type != GOT_NORMAL)
15404
0
    tls_type |= old_tls_type;
15405
15406
        /* If the symbol is accessed in both IE and GDESC
15407
     method, we're able to relax. Turn off the GDESC flag,
15408
     without messing up with any other kind of tls types
15409
     that may be involved.  */
15410
0
        if ((tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_GDESC))
15411
0
    tls_type &= ~GOT_TLS_GDESC;
15412
15413
0
        if (old_tls_type != tls_type)
15414
0
    {
15415
0
      if (h != NULL)
15416
0
        elf32_arm_hash_entry (h)->tls_type = tls_type;
15417
0
      else
15418
0
        elf32_arm_local_got_tls_type (abfd) [r_symndx] = tls_type;
15419
0
    }
15420
0
      }
15421
      /* Fall through.  */
15422
15423
0
    case R_ARM_TLS_LDM32:
15424
0
    case R_ARM_TLS_LDM32_FDPIC:
15425
0
      if (r_type == R_ARM_TLS_LDM32 || r_type == R_ARM_TLS_LDM32_FDPIC)
15426
0
    htab->tls_ldm_got.refcount++;
15427
      /* Fall through.  */
15428
15429
0
    case R_ARM_GOTOFF32:
15430
0
    case R_ARM_GOTPC:
15431
0
      if (htab->root.sgot == NULL
15432
0
    && !create_got_section (htab->root.dynobj, info))
15433
0
        return false;
15434
0
      break;
15435
15436
0
    case R_ARM_PC24:
15437
0
    case R_ARM_PLT32:
15438
0
    case R_ARM_CALL:
15439
0
    case R_ARM_JUMP24:
15440
0
    case R_ARM_PREL31:
15441
0
    case R_ARM_THM_CALL:
15442
0
    case R_ARM_THM_JUMP24:
15443
0
    case R_ARM_THM_JUMP19:
15444
0
      call_reloc_p = true;
15445
0
      may_need_local_target_p = true;
15446
0
      break;
15447
15448
0
    case R_ARM_ABS12:
15449
      /* VxWorks uses dynamic R_ARM_ABS12 relocations for
15450
         ldr __GOTT_INDEX__ offsets.  */
15451
0
      if (htab->root.target_os != is_vxworks)
15452
0
        {
15453
0
    may_need_local_target_p = true;
15454
0
    break;
15455
0
        }
15456
0
      else goto jump_over;
15457
15458
      /* Fall through.  */
15459
15460
0
    case R_ARM_MOVW_ABS_NC:
15461
0
    case R_ARM_MOVT_ABS:
15462
0
    case R_ARM_THM_MOVW_ABS_NC:
15463
0
    case R_ARM_THM_MOVT_ABS:
15464
0
      if (bfd_link_pic (info))
15465
0
        {
15466
0
    _bfd_error_handler
15467
0
      (_("%pB: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
15468
0
       abfd, elf32_arm_howto_table_1[r_type].name,
15469
0
       (h) ? h->root.root.string : "a local symbol");
15470
0
    bfd_set_error (bfd_error_bad_value);
15471
0
    return false;
15472
0
        }
15473
15474
      /* Fall through.  */
15475
0
    case R_ARM_ABS32:
15476
0
    case R_ARM_ABS32_NOI:
15477
0
  jump_over:
15478
0
      if (h != NULL && bfd_link_executable (info))
15479
0
        {
15480
0
    h->pointer_equality_needed = 1;
15481
0
        }
15482
      /* Fall through.  */
15483
0
    case R_ARM_REL32:
15484
0
    case R_ARM_REL32_NOI:
15485
0
    case R_ARM_MOVW_PREL_NC:
15486
0
    case R_ARM_MOVT_PREL:
15487
0
    case R_ARM_THM_MOVW_PREL_NC:
15488
0
    case R_ARM_THM_MOVT_PREL:
15489
15490
      /* Should the interworking branches be listed here?  */
15491
0
      if ((bfd_link_pic (info)
15492
0
     || htab->fdpic_p)
15493
0
    && (sec->flags & SEC_ALLOC) != 0)
15494
0
        {
15495
0
    if (h == NULL
15496
0
        && elf32_arm_howto_from_type (r_type)->pc_relative)
15497
0
      {
15498
        /* In shared libraries and relocatable executables,
15499
           we treat local relative references as calls;
15500
           see the related SYMBOL_CALLS_LOCAL code in
15501
           allocate_dynrelocs.  */
15502
0
        call_reloc_p = true;
15503
0
        may_need_local_target_p = true;
15504
0
      }
15505
0
    else
15506
      /* We are creating a shared library or relocatable
15507
         executable, and this is a reloc against a global symbol,
15508
         or a non-PC-relative reloc against a local symbol.
15509
         We may need to copy the reloc into the output.  */
15510
0
      may_become_dynamic_p = true;
15511
0
        }
15512
0
      else
15513
0
        may_need_local_target_p = true;
15514
0
      break;
15515
15516
  /* This relocation describes the C++ object vtable hierarchy.
15517
     Reconstruct it for later use during GC.  */
15518
0
  case R_ARM_GNU_VTINHERIT:
15519
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
15520
0
      return false;
15521
0
    break;
15522
15523
  /* This relocation describes which C++ vtable entries are actually
15524
     used.  Record for later use during GC.  */
15525
0
  case R_ARM_GNU_VTENTRY:
15526
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
15527
0
      return false;
15528
0
    break;
15529
0
  }
15530
15531
0
      if (h != NULL)
15532
0
  {
15533
0
    if (call_reloc_p)
15534
      /* We may need a .plt entry if the function this reloc
15535
         refers to is in a different object, regardless of the
15536
         symbol's type.  We can't tell for sure yet, because
15537
         something later might force the symbol local.  */
15538
0
      h->needs_plt = 1;
15539
0
    else if (may_need_local_target_p)
15540
      /* If this reloc is in a read-only section, we might
15541
         need a copy reloc.  We can't check reliably at this
15542
         stage whether the section is read-only, as input
15543
         sections have not yet been mapped to output sections.
15544
         Tentatively set the flag for now, and correct in
15545
         adjust_dynamic_symbol.  */
15546
0
      h->non_got_ref = 1;
15547
0
  }
15548
15549
0
      if (may_need_local_target_p
15550
0
    && (h != NULL || ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC))
15551
0
  {
15552
0
    union gotplt_union *root_plt;
15553
0
    struct arm_plt_info *arm_plt;
15554
0
    struct arm_local_iplt_info *local_iplt;
15555
15556
0
    if (h != NULL)
15557
0
      {
15558
0
        root_plt = &h->plt;
15559
0
        arm_plt = &eh->plt;
15560
0
      }
15561
0
    else
15562
0
      {
15563
0
        local_iplt = elf32_arm_create_local_iplt (abfd, r_symndx);
15564
0
        if (local_iplt == NULL)
15565
0
    return false;
15566
0
        root_plt = &local_iplt->root;
15567
0
        arm_plt = &local_iplt->arm;
15568
0
      }
15569
15570
    /* If the symbol is a function that doesn't bind locally,
15571
       this relocation will need a PLT entry.  */
15572
0
    if (root_plt->refcount != -1)
15573
0
      root_plt->refcount += 1;
15574
15575
0
    if (!call_reloc_p)
15576
0
      arm_plt->noncall_refcount++;
15577
15578
    /* It's too early to use htab->use_blx here, so we have to
15579
       record possible blx references separately from
15580
       relocs that definitely need a thumb stub.  */
15581
15582
0
    if (r_type == R_ARM_THM_CALL)
15583
0
      arm_plt->maybe_thumb_refcount += 1;
15584
15585
0
    if (r_type == R_ARM_THM_JUMP24
15586
0
        || r_type == R_ARM_THM_JUMP19)
15587
0
      arm_plt->thumb_refcount += 1;
15588
0
  }
15589
15590
0
      if (may_become_dynamic_p)
15591
0
  {
15592
0
    struct elf_dyn_relocs *p, **head;
15593
15594
    /* Create a reloc section in dynobj.  */
15595
0
    if (sreloc == NULL)
15596
0
      {
15597
0
        sreloc = _bfd_elf_make_dynamic_reloc_section
15598
0
    (sec, dynobj, 2, abfd, ! htab->use_rel);
15599
15600
0
        if (sreloc == NULL)
15601
0
    return false;
15602
0
      }
15603
15604
    /* If this is a global symbol, count the number of
15605
       relocations we need for this symbol.  */
15606
0
    if (h != NULL)
15607
0
      head = &h->dyn_relocs;
15608
0
    else
15609
0
      {
15610
0
        head = elf32_arm_get_local_dynreloc_list (abfd, r_symndx, isym);
15611
0
        if (head == NULL)
15612
0
    return false;
15613
0
      }
15614
15615
0
    p = *head;
15616
0
    if (p == NULL || p->sec != sec)
15617
0
      {
15618
0
        size_t amt = sizeof *p;
15619
15620
0
        p = (struct elf_dyn_relocs *) bfd_alloc (htab->root.dynobj, amt);
15621
0
        if (p == NULL)
15622
0
    return false;
15623
0
        p->next = *head;
15624
0
        *head = p;
15625
0
        p->sec = sec;
15626
0
        p->count = 0;
15627
0
        p->pc_count = 0;
15628
0
      }
15629
15630
0
    if (elf32_arm_howto_from_type (r_type)->pc_relative)
15631
0
      p->pc_count += 1;
15632
0
    p->count += 1;
15633
0
    if (h == NULL && htab->fdpic_p && !bfd_link_pic (info)
15634
0
        && r_type != R_ARM_ABS32 && r_type != R_ARM_ABS32_NOI)
15635
0
      {
15636
        /* Here we only support R_ARM_ABS32 and R_ARM_ABS32_NOI
15637
     that will become rofixup.  */
15638
        /* This is due to the fact that we suppose all will become rofixup.  */
15639
0
        _bfd_error_handler
15640
0
    (_("FDPIC does not yet support %s relocation"
15641
0
       " to become dynamic for executable"),
15642
0
     elf32_arm_howto_table_1[r_type].name);
15643
0
        abort ();
15644
0
      }
15645
0
  }
15646
0
    }
15647
15648
0
  return true;
15649
0
}
15650
15651
static void
15652
elf32_arm_update_relocs (asection *o,
15653
       struct bfd_elf_section_reloc_data *reldata)
15654
0
{
15655
0
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
15656
0
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
15657
0
  elf_backend_data *bed;
15658
0
  _arm_elf_section_data *eado;
15659
0
  struct bfd_link_order *p;
15660
0
  bfd_byte *erela_head, *erela;
15661
0
  Elf_Internal_Rela *irela_head, *irela;
15662
0
  Elf_Internal_Shdr *rel_hdr;
15663
0
  bfd *abfd;
15664
0
  unsigned int count;
15665
15666
0
  eado = get_arm_elf_section_data (o);
15667
15668
0
  if (!eado || eado->elf.this_hdr.sh_type != SHT_ARM_EXIDX)
15669
0
    return;
15670
15671
0
  abfd = o->owner;
15672
0
  bed = get_elf_backend_data (abfd);
15673
0
  rel_hdr = reldata->hdr;
15674
15675
0
  if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
15676
0
    {
15677
0
      swap_in = bed->s->swap_reloc_in;
15678
0
      swap_out = bed->s->swap_reloc_out;
15679
0
    }
15680
0
  else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
15681
0
    {
15682
0
      swap_in = bed->s->swap_reloca_in;
15683
0
      swap_out = bed->s->swap_reloca_out;
15684
0
    }
15685
0
  else
15686
0
    abort ();
15687
15688
0
  erela_head = rel_hdr->contents;
15689
0
  irela_head = (Elf_Internal_Rela *) bfd_zmalloc
15690
0
    ((NUM_SHDR_ENTRIES (rel_hdr) + 1) * sizeof (*irela_head));
15691
15692
0
  erela = erela_head;
15693
0
  irela = irela_head;
15694
0
  count = 0;
15695
15696
0
  for (p = o->map_head.link_order; p; p = p->next)
15697
0
    {
15698
0
      if (p->type == bfd_section_reloc_link_order
15699
0
    || p->type == bfd_symbol_reloc_link_order)
15700
0
  {
15701
0
    (*swap_in) (abfd, erela, irela);
15702
0
    erela += rel_hdr->sh_entsize;
15703
0
    irela++;
15704
0
    count++;
15705
0
  }
15706
0
      else if (p->type == bfd_indirect_link_order)
15707
0
  {
15708
0
    struct bfd_elf_section_reloc_data *input_reldata;
15709
0
    arm_unwind_table_edit *edit_list, *edit_tail;
15710
0
    _arm_elf_section_data *eadi;
15711
0
    bfd_size_type j;
15712
0
    bfd_vma offset;
15713
0
    asection *i;
15714
15715
0
    i = p->u.indirect.section;
15716
15717
0
    eadi = get_arm_elf_section_data (i);
15718
0
    edit_list = eadi->u.exidx.unwind_edit_list;
15719
0
    edit_tail = eadi->u.exidx.unwind_edit_tail;
15720
0
    offset = i->output_offset;
15721
15722
0
    if (eadi->elf.rel.hdr &&
15723
0
        eadi->elf.rel.hdr->sh_entsize == rel_hdr->sh_entsize)
15724
0
      input_reldata = &eadi->elf.rel;
15725
0
    else if (eadi->elf.rela.hdr &&
15726
0
       eadi->elf.rela.hdr->sh_entsize == rel_hdr->sh_entsize)
15727
0
      input_reldata = &eadi->elf.rela;
15728
0
    else
15729
0
      abort ();
15730
15731
0
    if (edit_list)
15732
0
      {
15733
0
        for (j = 0; j < NUM_SHDR_ENTRIES (input_reldata->hdr); j++)
15734
0
    {
15735
0
      arm_unwind_table_edit *edit_node, *edit_next;
15736
0
      bfd_vma bias;
15737
0
      bfd_vma reloc_index;
15738
15739
0
      (*swap_in) (abfd, erela, irela);
15740
0
      reloc_index = (irela->r_offset - offset) / 8;
15741
15742
0
      bias = 0;
15743
0
      edit_node = edit_list;
15744
0
      for (edit_next = edit_list;
15745
0
           edit_next && edit_next->index <= reloc_index;
15746
0
           edit_next = edit_node->next)
15747
0
        {
15748
0
          bias++;
15749
0
          edit_node = edit_next;
15750
0
        }
15751
15752
0
      if (edit_node->type != DELETE_EXIDX_ENTRY
15753
0
          || edit_node->index != reloc_index)
15754
0
        {
15755
0
          irela->r_offset -= bias * 8;
15756
0
          irela++;
15757
0
          count++;
15758
0
        }
15759
15760
0
      erela += rel_hdr->sh_entsize;
15761
0
    }
15762
15763
0
        if (edit_tail->type == INSERT_EXIDX_CANTUNWIND_AT_END)
15764
0
    {
15765
      /* New relocation entity.  */
15766
0
      asection *text_sec = edit_tail->linked_section;
15767
0
      asection *text_out = text_sec->output_section;
15768
0
      bfd_vma exidx_offset = offset + i->size - 8;
15769
15770
0
      irela->r_addend = 0;
15771
0
      irela->r_offset = exidx_offset;
15772
0
      irela->r_info = ELF32_R_INFO
15773
0
        (text_out->target_index, R_ARM_PREL31);
15774
0
      irela++;
15775
0
      count++;
15776
0
    }
15777
0
      }
15778
0
    else
15779
0
      {
15780
0
        for (j = 0; j < NUM_SHDR_ENTRIES (input_reldata->hdr); j++)
15781
0
    {
15782
0
      (*swap_in) (abfd, erela, irela);
15783
0
      erela += rel_hdr->sh_entsize;
15784
0
      irela++;
15785
0
    }
15786
15787
0
        count += NUM_SHDR_ENTRIES (input_reldata->hdr);
15788
0
      }
15789
0
  }
15790
0
    }
15791
15792
0
  reldata->count = count;
15793
0
  rel_hdr->sh_size = count * rel_hdr->sh_entsize;
15794
15795
0
  erela = erela_head;
15796
0
  irela = irela_head;
15797
0
  while (count > 0)
15798
0
    {
15799
0
      (*swap_out) (abfd, irela, erela);
15800
0
      erela += rel_hdr->sh_entsize;
15801
0
      irela++;
15802
0
      count--;
15803
0
    }
15804
15805
0
  free (irela_head);
15806
15807
  /* Hashes are no longer valid.  */
15808
0
  free (reldata->hashes);
15809
0
  reldata->hashes = NULL;
15810
0
}
15811
15812
/* Unwinding tables are not referenced directly.  This pass marks them as
15813
   required if the corresponding code section is marked.  Similarly, ARMv8-M
15814
   secure entry functions can only be referenced by SG veneers which are
15815
   created after the GC process. They need to be marked in case they reside in
15816
   their own section (as would be the case if code was compiled with
15817
   -ffunction-sections).  */
15818
15819
static bool
15820
elf32_arm_gc_mark_extra_sections (struct bfd_link_info *info,
15821
          elf_gc_mark_hook_fn gc_mark_hook)
15822
0
{
15823
0
  bfd *sub;
15824
0
  Elf_Internal_Shdr **elf_shdrp;
15825
0
  asection *cmse_sec;
15826
0
  obj_attribute *out_attr;
15827
0
  Elf_Internal_Shdr *symtab_hdr;
15828
0
  unsigned i, sym_count, ext_start;
15829
0
  elf_backend_data *bed;
15830
0
  struct elf_link_hash_entry **sym_hashes;
15831
0
  struct elf32_arm_link_hash_entry *cmse_hash;
15832
0
  bool again, is_v8m, first_bfd_browse = true;
15833
0
  bool extra_marks_added = false;
15834
0
  asection *isec;
15835
15836
0
  _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
15837
15838
0
  out_attr = elf_known_obj_attributes_proc (info->output_bfd);
15839
0
  is_v8m = out_attr[Tag_CPU_arch].i >= TAG_CPU_ARCH_V8M_BASE
15840
0
     && out_attr[Tag_CPU_arch_profile].i == 'M';
15841
15842
  /* Marking EH data may cause additional code sections to be marked,
15843
     requiring multiple passes.  */
15844
0
  again = true;
15845
0
  while (again)
15846
0
    {
15847
0
      again = false;
15848
0
      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
15849
0
  {
15850
0
    asection *o;
15851
15852
0
    if (! is_arm_elf (sub))
15853
0
      continue;
15854
15855
0
    elf_shdrp = elf_elfsections (sub);
15856
0
    for (o = sub->sections; o != NULL; o = o->next)
15857
0
      {
15858
0
        Elf_Internal_Shdr *hdr;
15859
15860
0
        hdr = &elf_section_data (o)->this_hdr;
15861
0
        if (hdr->sh_type == SHT_ARM_EXIDX
15862
0
      && hdr->sh_link
15863
0
      && hdr->sh_link < elf_numsections (sub)
15864
0
      && !o->gc_mark
15865
0
      && elf_shdrp[hdr->sh_link]->bfd_section->gc_mark)
15866
0
    {
15867
0
      again = true;
15868
0
      if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
15869
0
        return false;
15870
0
    }
15871
0
      }
15872
15873
    /* Mark section holding ARMv8-M secure entry functions.  We mark all
15874
       of them so no need for a second browsing.  */
15875
0
    if (is_v8m && first_bfd_browse)
15876
0
      {
15877
0
        bool debug_sec_need_to_be_marked = false;
15878
15879
0
        sym_hashes = elf_sym_hashes (sub);
15880
0
        bed = get_elf_backend_data (sub);
15881
0
        symtab_hdr = &elf_symtab_hdr (sub);
15882
0
        sym_count = symtab_hdr->sh_size / bed->s->sizeof_sym;
15883
0
        ext_start = symtab_hdr->sh_info;
15884
15885
        /* Scan symbols.  */
15886
0
        for (i = ext_start; i < sym_count; i++)
15887
0
    {
15888
0
      cmse_hash = elf32_arm_hash_entry (sym_hashes[i - ext_start]);
15889
0
      if (cmse_hash == NULL)
15890
0
        continue;
15891
15892
      /* Assume it is a special symbol.  If not, cmse_scan will
15893
         warn about it and user can do something about it.  */
15894
0
      if (startswith (cmse_hash->root.root.root.string,
15895
0
          CMSE_PREFIX))
15896
0
        {
15897
0
          cmse_sec = cmse_hash->root.root.u.def.section;
15898
0
          if (!cmse_sec->gc_mark
15899
0
        && !_bfd_elf_gc_mark (info, cmse_sec, gc_mark_hook))
15900
0
      return false;
15901
          /* The debug sections related to these secure entry
15902
       functions are marked on enabling below flag.  */
15903
0
          debug_sec_need_to_be_marked = true;
15904
0
        }
15905
0
    }
15906
15907
0
        if (debug_sec_need_to_be_marked)
15908
0
    {
15909
      /* Looping over all the sections of the object file containing
15910
         Armv8-M secure entry functions and marking all the debug
15911
         sections.  */
15912
0
      for (isec = sub->sections; isec != NULL; isec = isec->next)
15913
0
        {
15914
          /* If not a debug sections, skip it.  */
15915
0
          if (!isec->gc_mark && (isec->flags & SEC_DEBUGGING))
15916
0
      {
15917
0
        isec->gc_mark = 1;
15918
0
        extra_marks_added = true;
15919
0
      }
15920
0
        }
15921
0
      debug_sec_need_to_be_marked = false;
15922
0
    }
15923
0
      }
15924
0
  }
15925
15926
0
      first_bfd_browse = false;
15927
0
    }
15928
15929
  /* PR 30354: If we have added extra marks then make sure that any
15930
     dependencies of the newly marked sections are also marked.  */
15931
0
  if (extra_marks_added)
15932
0
    _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
15933
15934
0
  return true;
15935
0
}
15936
15937
/* Treat mapping symbols as special target symbols.  */
15938
15939
static bool
15940
elf32_arm_is_target_special_symbol (bfd * abfd ATTRIBUTE_UNUSED, asymbol * sym)
15941
177
{
15942
177
  return bfd_is_arm_special_symbol_name (sym->name,
15943
177
           BFD_ARM_SPECIAL_SYM_TYPE_ANY);
15944
177
}
15945
15946
/* If the ELF symbol SYM might be a function in SEC, return the
15947
   function size and set *CODE_OFF to the function's entry point,
15948
   otherwise return zero.  */
15949
15950
static bfd_size_type
15951
elf32_arm_maybe_function_sym (const asymbol *sym, asection *sec,
15952
            bfd_vma *code_off)
15953
812
{
15954
812
  bfd_size_type size;
15955
812
  elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
15956
15957
812
  if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
15958
812
         | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
15959
657
      || sym->section != sec)
15960
700
    return 0;
15961
15962
112
  size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
15963
15964
112
  if (!(sym->flags & BSF_SYNTHETIC))
15965
112
    switch (ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info))
15966
112
      {
15967
22
  case STT_NOTYPE:
15968
    /* Ignore symbols created by the annobin plugin for gcc and clang.
15969
       These symbols are hidden, local, notype and have a size of 0.  */
15970
22
    if (size == 0
15971
6
        && sym->flags & BSF_LOCAL
15972
6
        && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
15973
0
      return 0;
15974
    /* Fall through.  */
15975
100
  case STT_FUNC:
15976
100
  case STT_ARM_TFUNC:
15977
    /* FIXME: Allow STT_GNU_IFUNC as well ?  */
15978
100
    break;
15979
12
  default:
15980
12
    return 0;
15981
112
      }
15982
15983
100
  if ((sym->flags & BSF_LOCAL)
15984
20
      && bfd_is_arm_special_symbol_name (sym->name,
15985
20
           BFD_ARM_SPECIAL_SYM_TYPE_ANY))
15986
0
    return 0;
15987
15988
100
  *code_off = sym->value;
15989
15990
  /* Do not return 0 for the function's size.  */
15991
100
  return size ? size : 1;
15992
15993
100
}
15994
15995
static bool
15996
elf32_arm_find_inliner_info (bfd *      abfd,
15997
           const char **  filename_ptr,
15998
           const char **  functionname_ptr,
15999
           unsigned int * line_ptr)
16000
0
{
16001
0
  bool found;
16002
0
  found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
16003
0
           functionname_ptr, line_ptr,
16004
0
           & elf_tdata (abfd)->dwarf2_find_line_info);
16005
0
  return found;
16006
0
}
16007
16008
/* Adjust a symbol defined by a dynamic object and referenced by a
16009
   regular object.  The current definition is in some section of the
16010
   dynamic object, but we're not including those sections.  We have to
16011
   change the definition to something the rest of the link can
16012
   understand.  */
16013
16014
static bool
16015
elf32_arm_adjust_dynamic_symbol (struct bfd_link_info * info,
16016
         struct elf_link_hash_entry * h)
16017
0
{
16018
0
  bfd * dynobj;
16019
0
  asection *s, *srel;
16020
0
  struct elf32_arm_link_hash_entry * eh;
16021
0
  struct elf32_arm_link_hash_table *globals;
16022
16023
0
  globals = elf32_arm_hash_table (info);
16024
0
  if (globals == NULL)
16025
0
    return false;
16026
16027
0
  dynobj = elf_hash_table (info)->dynobj;
16028
16029
  /* Make sure we know what is going on here.  */
16030
0
  BFD_ASSERT (dynobj != NULL
16031
0
        && (h->needs_plt
16032
0
      || h->type == STT_GNU_IFUNC
16033
0
      || h->is_weakalias
16034
0
      || (h->def_dynamic
16035
0
          && h->ref_regular
16036
0
          && !h->def_regular)));
16037
16038
0
  eh = (struct elf32_arm_link_hash_entry *) h;
16039
16040
  /* If this is a function, put it in the procedure linkage table.  We
16041
     will fill in the contents of the procedure linkage table later,
16042
     when we know the address of the .got section.  */
16043
0
  if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
16044
0
    {
16045
      /* Calls to STT_GNU_IFUNC symbols always use a PLT, even if the
16046
   symbol binds locally.  */
16047
0
      if (h->plt.refcount <= 0
16048
0
    || (h->type != STT_GNU_IFUNC
16049
0
        && (SYMBOL_CALLS_LOCAL (info, h)
16050
0
      || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
16051
0
          && h->root.type == bfd_link_hash_undefweak))))
16052
0
  {
16053
    /* This case can occur if we saw a PLT32 reloc in an input
16054
       file, but the symbol was never referred to by a dynamic
16055
       object, or if all references were garbage collected.  In
16056
       such a case, we don't actually need to build a procedure
16057
       linkage table, and we can just do a PC24 reloc instead.  */
16058
0
    h->plt.offset = (bfd_vma) -1;
16059
0
    eh->plt.thumb_refcount = 0;
16060
0
    eh->plt.maybe_thumb_refcount = 0;
16061
0
    eh->plt.noncall_refcount = 0;
16062
0
    h->needs_plt = 0;
16063
0
  }
16064
16065
0
      return true;
16066
0
    }
16067
0
  else
16068
0
    {
16069
      /* It's possible that we incorrectly decided a .plt reloc was
16070
   needed for an R_ARM_PC24 or similar reloc to a non-function sym
16071
   in check_relocs.  We can't decide accurately between function
16072
   and non-function syms in check-relocs; Objects loaded later in
16073
   the link may change h->type.  So fix it now.  */
16074
0
      h->plt.offset = (bfd_vma) -1;
16075
0
      eh->plt.thumb_refcount = 0;
16076
0
      eh->plt.maybe_thumb_refcount = 0;
16077
0
      eh->plt.noncall_refcount = 0;
16078
0
    }
16079
16080
  /* If this is a weak symbol, and there is a real definition, the
16081
     processor independent code will have arranged for us to see the
16082
     real definition first, and we can just use the same value.  */
16083
0
  if (h->is_weakalias)
16084
0
    {
16085
0
      struct elf_link_hash_entry *def = weakdef (h);
16086
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
16087
0
      h->root.u.def.section = def->root.u.def.section;
16088
0
      h->root.u.def.value = def->root.u.def.value;
16089
0
      return true;
16090
0
    }
16091
16092
  /* If there are no non-GOT references, we do not need a copy
16093
     relocation.  */
16094
0
  if (!h->non_got_ref)
16095
0
    return true;
16096
16097
  /* This is a reference to a symbol defined by a dynamic object which
16098
     is not a function.  */
16099
16100
  /* If we are creating a shared library, we must presume that the
16101
     only references to the symbol are via the global offset table.
16102
     For such cases we need not do anything here; the relocations will
16103
     be handled correctly by relocate_section.  */
16104
0
  if (bfd_link_pic (info))
16105
0
    return true;
16106
16107
  /* We must allocate the symbol in our .dynbss section, which will
16108
     become part of the .bss section of the executable.  There will be
16109
     an entry for this symbol in the .dynsym section.  The dynamic
16110
     object will contain position independent code, so all references
16111
     from the dynamic object to this symbol will go through the global
16112
     offset table.  The dynamic linker will use the .dynsym entry to
16113
     determine the address it must put in the global offset table, so
16114
     both the dynamic object and the regular object will refer to the
16115
     same memory location for the variable.  */
16116
  /* If allowed, we must generate a R_ARM_COPY reloc to tell the dynamic
16117
     linker to copy the initial value out of the dynamic object and into
16118
     the runtime process image.  We need to remember the offset into the
16119
     .rel(a).bss section we are going to use.  */
16120
0
  if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
16121
0
    {
16122
0
      s = globals->root.sdynrelro;
16123
0
      srel = globals->root.sreldynrelro;
16124
0
    }
16125
0
  else
16126
0
    {
16127
0
      s = globals->root.sdynbss;
16128
0
      srel = globals->root.srelbss;
16129
0
    }
16130
0
  if (info->nocopyreloc == 0
16131
0
      && (h->root.u.def.section->flags & SEC_ALLOC) != 0
16132
0
      && h->size != 0)
16133
0
    {
16134
0
      elf32_arm_allocate_dynrelocs (info, srel, 1);
16135
0
      h->needs_copy = 1;
16136
0
    }
16137
16138
0
  return _bfd_elf_adjust_dynamic_copy (info, h, s);
16139
0
}
16140
16141
/* Allocate space in .plt, .got and associated reloc sections for
16142
   dynamic relocs.  */
16143
16144
static bool
16145
allocate_dynrelocs_for_symbol (struct elf_link_hash_entry *h, void * inf)
16146
0
{
16147
0
  struct bfd_link_info *info;
16148
0
  struct elf32_arm_link_hash_table *htab;
16149
0
  struct elf32_arm_link_hash_entry *eh;
16150
0
  struct elf_dyn_relocs *p;
16151
16152
0
  if (h->root.type == bfd_link_hash_indirect)
16153
0
    return true;
16154
16155
0
  eh = (struct elf32_arm_link_hash_entry *) h;
16156
16157
0
  info = (struct bfd_link_info *) inf;
16158
0
  htab = elf32_arm_hash_table (info);
16159
0
  if (htab == NULL)
16160
0
    return false;
16161
16162
0
  if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
16163
0
      && h->plt.refcount > 0)
16164
0
    {
16165
      /* Make sure this symbol is output as a dynamic symbol.
16166
   Undefined weak syms won't yet be marked as dynamic.  */
16167
0
      if (h->dynindx == -1 && !h->forced_local
16168
0
    && h->root.type == bfd_link_hash_undefweak)
16169
0
  {
16170
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
16171
0
      return false;
16172
0
  }
16173
16174
      /* If the call in the PLT entry binds locally, the associated
16175
   GOT entry should use an R_ARM_IRELATIVE relocation instead of
16176
   the usual R_ARM_JUMP_SLOT.  Put it in the .iplt section rather
16177
   than the .plt section.  */
16178
0
      if (h->type == STT_GNU_IFUNC && SYMBOL_CALLS_LOCAL (info, h))
16179
0
  {
16180
0
    eh->is_iplt = 1;
16181
0
    if (eh->plt.noncall_refcount == 0
16182
0
        && SYMBOL_REFERENCES_LOCAL (info, h))
16183
      /* All non-call references can be resolved directly.
16184
         This means that they can (and in some cases, must)
16185
         resolve directly to the run-time target, rather than
16186
         to the PLT.  That in turns means that any .got entry
16187
         would be equal to the .igot.plt entry, so there's
16188
         no point having both.  */
16189
0
      h->got.refcount = 0;
16190
0
  }
16191
16192
0
      if (bfd_link_pic (info)
16193
0
    || eh->is_iplt
16194
0
    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
16195
0
  {
16196
0
    elf32_arm_allocate_plt_entry (info, eh->is_iplt, &h->plt, &eh->plt);
16197
16198
    /* If this symbol is not defined in a regular file, and we are
16199
       not generating a shared library, then set the symbol to this
16200
       location in the .plt.  This is required to make function
16201
       pointers compare as equal between the normal executable and
16202
       the shared library.  */
16203
0
    if (! bfd_link_pic (info)
16204
0
        && !h->def_regular)
16205
0
      {
16206
0
        h->root.u.def.section = htab->root.splt;
16207
0
        h->root.u.def.value = h->plt.offset;
16208
16209
        /* Make sure the function is not marked as Thumb, in case
16210
     it is the target of an ABS32 relocation, which will
16211
     point to the PLT entry.  */
16212
0
        ARM_SET_SYM_BRANCH_TYPE (h->target_internal, ST_BRANCH_TO_ARM);
16213
0
      }
16214
16215
    /* VxWorks executables have a second set of relocations for
16216
       each PLT entry.  They go in a separate relocation section,
16217
       which is processed by the kernel loader.  */
16218
0
    if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
16219
0
      {
16220
        /* There is a relocation for the initial PLT entry:
16221
     an R_ARM_32 relocation for _GLOBAL_OFFSET_TABLE_.  */
16222
0
        if (h->plt.offset == htab->plt_header_size)
16223
0
    elf32_arm_allocate_dynrelocs (info, htab->srelplt2, 1);
16224
16225
        /* There are two extra relocations for each subsequent
16226
     PLT entry: an R_ARM_32 relocation for the GOT entry,
16227
     and an R_ARM_32 relocation for the PLT entry.  */
16228
0
        elf32_arm_allocate_dynrelocs (info, htab->srelplt2, 2);
16229
0
      }
16230
0
  }
16231
0
      else
16232
0
  {
16233
0
    h->plt.offset = (bfd_vma) -1;
16234
0
    h->needs_plt = 0;
16235
0
  }
16236
0
    }
16237
0
  else
16238
0
    {
16239
0
      h->plt.offset = (bfd_vma) -1;
16240
0
      h->needs_plt = 0;
16241
0
    }
16242
16243
0
  eh = (struct elf32_arm_link_hash_entry *) h;
16244
0
  eh->tlsdesc_got = (bfd_vma) -1;
16245
16246
0
  if (h->got.refcount > 0)
16247
0
    {
16248
0
      asection *s;
16249
0
      bool dyn;
16250
0
      int tls_type = elf32_arm_hash_entry (h)->tls_type;
16251
0
      int indx;
16252
16253
      /* Make sure this symbol is output as a dynamic symbol.
16254
   Undefined weak syms won't yet be marked as dynamic.  */
16255
0
      if (htab->root.dynamic_sections_created
16256
0
    && h->dynindx == -1
16257
0
    && !h->forced_local
16258
0
    && h->root.type == bfd_link_hash_undefweak)
16259
0
  {
16260
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
16261
0
      return false;
16262
0
  }
16263
16264
0
      s = htab->root.sgot;
16265
0
      h->got.offset = s->size;
16266
16267
0
      if (tls_type == GOT_UNKNOWN)
16268
0
  abort ();
16269
16270
0
      if (tls_type == GOT_NORMAL)
16271
  /* Non-TLS symbols need one GOT slot.  */
16272
0
  s->size += 4;
16273
0
      else
16274
0
  {
16275
0
    if (tls_type & GOT_TLS_GDESC)
16276
0
      {
16277
        /* R_ARM_TLS_DESC needs 2 GOT slots.  */
16278
0
        eh->tlsdesc_got
16279
0
    = (htab->root.sgotplt->size
16280
0
       - elf32_arm_compute_jump_table_size (htab));
16281
0
        htab->root.sgotplt->size += 8;
16282
0
        h->got.offset = (bfd_vma) -2;
16283
        /* plt.got_offset needs to know there's a TLS_DESC
16284
     reloc in the middle of .got.plt.  */
16285
0
        htab->num_tls_desc++;
16286
0
      }
16287
16288
0
    if (tls_type & GOT_TLS_GD)
16289
0
      {
16290
        /* R_ARM_TLS_GD32 and R_ARM_TLS_GD32_FDPIC need two
16291
     consecutive GOT slots.  If the symbol is both GD
16292
     and GDESC, got.offset may have been
16293
     overwritten.  */
16294
0
        h->got.offset = s->size;
16295
0
        s->size += 8;
16296
0
      }
16297
16298
0
    if (tls_type & GOT_TLS_IE)
16299
      /* R_ARM_TLS_IE32/R_ARM_TLS_IE32_FDPIC need one GOT
16300
         slot.  */
16301
0
      s->size += 4;
16302
0
  }
16303
16304
0
      dyn = htab->root.dynamic_sections_created;
16305
16306
0
      indx = 0;
16307
0
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
16308
0
    && (!bfd_link_pic (info)
16309
0
        || !SYMBOL_REFERENCES_LOCAL (info, h)))
16310
0
  indx = h->dynindx;
16311
16312
0
      if (tls_type != GOT_NORMAL
16313
0
    && (bfd_link_dll (info) || indx != 0)
16314
0
    && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
16315
0
        || h->root.type != bfd_link_hash_undefweak))
16316
0
  {
16317
0
    if (tls_type & GOT_TLS_IE)
16318
0
      elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16319
16320
0
    if (tls_type & GOT_TLS_GD)
16321
0
      elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16322
16323
0
    if (tls_type & GOT_TLS_GDESC)
16324
0
      {
16325
0
        elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
16326
        /* GDESC needs a trampoline to jump to.  */
16327
0
        htab->tls_trampoline = -1;
16328
0
      }
16329
16330
    /* Only GD needs it.  GDESC just emits one relocation per
16331
       2 entries.  */
16332
0
    if ((tls_type & GOT_TLS_GD) && indx != 0)
16333
0
      elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16334
0
  }
16335
0
      else if (((indx != -1) || htab->fdpic_p)
16336
0
         && !SYMBOL_REFERENCES_LOCAL (info, h))
16337
0
  {
16338
0
    if (htab->root.dynamic_sections_created)
16339
      /* Reserve room for the GOT entry's R_ARM_GLOB_DAT relocation.  */
16340
0
      elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16341
0
  }
16342
0
      else if (h->type == STT_GNU_IFUNC
16343
0
         && eh->plt.noncall_refcount == 0)
16344
  /* No non-call references resolve the STT_GNU_IFUNC's PLT entry;
16345
     they all resolve dynamically instead.  Reserve room for the
16346
     GOT entry's R_ARM_IRELATIVE relocation.  */
16347
0
  elf32_arm_allocate_irelocs (info, htab->root.srelgot, 1);
16348
0
      else if (bfd_link_pic (info)
16349
0
         && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
16350
  /* Reserve room for the GOT entry's R_ARM_RELATIVE relocation.  */
16351
0
  elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16352
0
      else if (htab->fdpic_p && tls_type == GOT_NORMAL)
16353
  /* Reserve room for rofixup for FDPIC executable.  */
16354
  /* TLS relocs do not need space since they are completely
16355
     resolved.  */
16356
0
  htab->srofixup->size += 4;
16357
0
    }
16358
0
  else
16359
0
    h->got.offset = (bfd_vma) -1;
16360
16361
  /* FDPIC support.  */
16362
0
  if (eh->fdpic_cnts.gotofffuncdesc_cnt > 0)
16363
0
    {
16364
      /* Symbol musn't be exported.  */
16365
0
      if (h->dynindx != -1)
16366
0
  abort ();
16367
16368
      /* We only allocate one function descriptor with its associated
16369
   relocation.  */
16370
0
      if (eh->fdpic_cnts.funcdesc_offset == -1)
16371
0
  {
16372
0
    asection *s = htab->root.sgot;
16373
16374
0
    eh->fdpic_cnts.funcdesc_offset = s->size;
16375
0
    s->size += 8;
16376
    /* We will add an R_ARM_FUNCDESC_VALUE relocation or two rofixups.  */
16377
0
    if (bfd_link_pic (info))
16378
0
      elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16379
0
    else
16380
0
      htab->srofixup->size += 8;
16381
0
  }
16382
0
    }
16383
16384
0
  if (eh->fdpic_cnts.gotfuncdesc_cnt > 0)
16385
0
    {
16386
0
      asection *s = htab->root.sgot;
16387
16388
0
      if (htab->root.dynamic_sections_created && h->dynindx == -1
16389
0
    && !h->forced_local)
16390
0
  if (! bfd_elf_link_record_dynamic_symbol (info, h))
16391
0
    return false;
16392
16393
0
      if (h->dynindx == -1)
16394
0
  {
16395
    /* We only allocate one function descriptor with its
16396
       associated relocation.  */
16397
0
    if (eh->fdpic_cnts.funcdesc_offset == -1)
16398
0
      {
16399
16400
0
        eh->fdpic_cnts.funcdesc_offset = s->size;
16401
0
        s->size += 8;
16402
        /* We will add an R_ARM_FUNCDESC_VALUE relocation or two
16403
     rofixups.  */
16404
0
        if (bfd_link_pic (info))
16405
0
    elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16406
0
        else
16407
0
    htab->srofixup->size += 8;
16408
0
      }
16409
0
  }
16410
16411
      /* Add one entry into the GOT and a R_ARM_FUNCDESC or
16412
   R_ARM_RELATIVE/rofixup relocation on it.  */
16413
0
      eh->fdpic_cnts.gotfuncdesc_offset = s->size;
16414
0
      s->size += 4;
16415
0
      if (h->dynindx == -1 && !bfd_link_pic (info))
16416
0
  htab->srofixup->size += 4;
16417
0
      else
16418
0
  elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16419
0
    }
16420
16421
0
  if (eh->fdpic_cnts.funcdesc_cnt > 0)
16422
0
    {
16423
0
      if (htab->root.dynamic_sections_created && h->dynindx == -1
16424
0
    && !h->forced_local)
16425
0
  if (! bfd_elf_link_record_dynamic_symbol (info, h))
16426
0
    return false;
16427
16428
0
      if (h->dynindx == -1)
16429
0
  {
16430
    /* We only allocate one function descriptor with its
16431
       associated relocation.  */
16432
0
    if (eh->fdpic_cnts.funcdesc_offset == -1)
16433
0
      {
16434
0
        asection *s = htab->root.sgot;
16435
16436
0
        eh->fdpic_cnts.funcdesc_offset = s->size;
16437
0
        s->size += 8;
16438
        /* We will add an R_ARM_FUNCDESC_VALUE relocation or two
16439
     rofixups.  */
16440
0
        if (bfd_link_pic (info))
16441
0
    elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16442
0
        else
16443
0
    htab->srofixup->size += 8;
16444
0
      }
16445
0
  }
16446
0
      if (h->dynindx == -1 && !bfd_link_pic (info))
16447
0
  {
16448
    /* For FDPIC executable we replace R_ARM_RELATIVE with a rofixup.  */
16449
0
    htab->srofixup->size += 4 * eh->fdpic_cnts.funcdesc_cnt;
16450
0
  }
16451
0
      else
16452
0
  {
16453
    /* Will need one dynamic reloc per reference. will be either
16454
       R_ARM_FUNCDESC or R_ARM_RELATIVE for hidden symbols.  */
16455
0
    elf32_arm_allocate_dynrelocs (info, htab->root.srelgot,
16456
0
          eh->fdpic_cnts.funcdesc_cnt);
16457
0
  }
16458
0
    }
16459
16460
  /* Allocate stubs for exported Thumb functions on v4t.  */
16461
0
  if (!htab->use_blx && h->dynindx != -1
16462
0
      && h->def_regular
16463
0
      && ARM_GET_SYM_BRANCH_TYPE (h->target_internal) == ST_BRANCH_TO_THUMB
16464
0
      && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
16465
0
    {
16466
0
      struct elf_link_hash_entry * th;
16467
0
      struct bfd_link_hash_entry * bh;
16468
0
      struct elf_link_hash_entry * myh;
16469
0
      char name[1024];
16470
0
      asection *s;
16471
0
      bh = NULL;
16472
      /* Create a new symbol to regist the real location of the function.  */
16473
0
      s = h->root.u.def.section;
16474
0
      sprintf (name, "__real_%s", h->root.root.string);
16475
0
      _bfd_generic_link_add_one_symbol (info, s->owner,
16476
0
          name, BSF_GLOBAL, s,
16477
0
          h->root.u.def.value,
16478
0
          NULL, true, false, &bh);
16479
16480
0
      myh = (struct elf_link_hash_entry *) bh;
16481
0
      myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
16482
0
      myh->forced_local = 1;
16483
0
      ARM_SET_SYM_BRANCH_TYPE (myh->target_internal, ST_BRANCH_TO_THUMB);
16484
0
      eh->export_glue = myh;
16485
0
      th = record_arm_to_thumb_glue (info, h);
16486
      /* Point the symbol at the stub.  */
16487
0
      h->type = ELF_ST_INFO (ELF_ST_BIND (h->type), STT_FUNC);
16488
0
      ARM_SET_SYM_BRANCH_TYPE (h->target_internal, ST_BRANCH_TO_ARM);
16489
0
      h->root.u.def.section = th->root.u.def.section;
16490
0
      h->root.u.def.value = th->root.u.def.value & ~1;
16491
0
    }
16492
16493
0
  if (h->dyn_relocs == NULL)
16494
0
    return true;
16495
16496
  /* In the shared -Bsymbolic case, discard space allocated for
16497
     dynamic pc-relative relocs against symbols which turn out to be
16498
     defined in regular objects.  For the normal shared case, discard
16499
     space for pc-relative relocs that have become local due to symbol
16500
     visibility changes.  */
16501
16502
0
  if (bfd_link_pic (info)
16503
0
      || htab->fdpic_p)
16504
0
    {
16505
      /* Relocs that use pc_count are PC-relative forms, which will appear
16506
   on something like ".long foo - ." or "movw REG, foo - .".  We want
16507
   calls to protected symbols to resolve directly to the function
16508
   rather than going via the plt.  If people want function pointer
16509
   comparisons to work as expected then they should avoid writing
16510
   assembly like ".long foo - .".  */
16511
0
      if (SYMBOL_CALLS_LOCAL (info, h))
16512
0
  {
16513
0
    struct elf_dyn_relocs **pp;
16514
16515
0
    for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
16516
0
      {
16517
0
        p->count -= p->pc_count;
16518
0
        p->pc_count = 0;
16519
0
        if (p->count == 0)
16520
0
    *pp = p->next;
16521
0
        else
16522
0
    pp = &p->next;
16523
0
      }
16524
0
  }
16525
16526
0
      if (htab->root.target_os == is_vxworks)
16527
0
  {
16528
0
    struct elf_dyn_relocs **pp;
16529
16530
0
    for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
16531
0
      {
16532
0
        if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
16533
0
    *pp = p->next;
16534
0
        else
16535
0
    pp = &p->next;
16536
0
      }
16537
0
  }
16538
16539
      /* Also discard relocs on undefined weak syms with non-default
16540
   visibility.  */
16541
0
      if (h->dyn_relocs != NULL
16542
0
    && h->root.type == bfd_link_hash_undefweak)
16543
0
  {
16544
0
    if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
16545
0
        || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
16546
0
      h->dyn_relocs = NULL;
16547
16548
    /* Make sure undefined weak symbols are output as a dynamic
16549
       symbol in PIEs.  */
16550
0
    else if (htab->root.dynamic_sections_created && h->dynindx == -1
16551
0
       && !h->forced_local)
16552
0
      {
16553
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
16554
0
    return false;
16555
0
      }
16556
0
  }
16557
0
    }
16558
0
  else
16559
0
    {
16560
      /* For the non-shared case, discard space for relocs against
16561
   symbols which turn out to need copy relocs or are not
16562
   dynamic.  */
16563
16564
0
      if (!h->non_got_ref
16565
0
    && ((h->def_dynamic
16566
0
         && !h->def_regular)
16567
0
        || (htab->root.dynamic_sections_created
16568
0
      && (h->root.type == bfd_link_hash_undefweak
16569
0
          || h->root.type == bfd_link_hash_undefined))))
16570
0
  {
16571
    /* Make sure this symbol is output as a dynamic symbol.
16572
       Undefined weak syms won't yet be marked as dynamic.  */
16573
0
    if (h->dynindx == -1 && !h->forced_local
16574
0
        && h->root.type == bfd_link_hash_undefweak)
16575
0
      {
16576
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
16577
0
    return false;
16578
0
      }
16579
16580
    /* If that succeeded, we know we'll be keeping all the
16581
       relocs.  */
16582
0
    if (h->dynindx != -1)
16583
0
      goto keep;
16584
0
  }
16585
16586
0
      h->dyn_relocs = NULL;
16587
16588
0
    keep: ;
16589
0
    }
16590
16591
  /* Finally, allocate space.  */
16592
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
16593
0
    {
16594
0
      asection *sreloc = elf_section_data (p->sec)->sreloc;
16595
16596
0
      if (h->type == STT_GNU_IFUNC
16597
0
    && eh->plt.noncall_refcount == 0
16598
0
    && SYMBOL_REFERENCES_LOCAL (info, h))
16599
0
  elf32_arm_allocate_irelocs (info, sreloc, p->count);
16600
0
      else if (h->dynindx != -1
16601
0
         && (!bfd_link_pic (info) || !info->symbolic || !h->def_regular))
16602
0
  elf32_arm_allocate_dynrelocs (info, sreloc, p->count);
16603
0
      else if (htab->fdpic_p && !bfd_link_pic (info))
16604
0
  htab->srofixup->size += 4 * p->count;
16605
0
      else
16606
0
  elf32_arm_allocate_dynrelocs (info, sreloc, p->count);
16607
0
    }
16608
16609
0
  return true;
16610
0
}
16611
16612
void
16613
bfd_elf32_arm_set_byteswap_code (struct bfd_link_info *info,
16614
         int byteswap_code)
16615
0
{
16616
0
  struct elf32_arm_link_hash_table *globals;
16617
16618
0
  globals = elf32_arm_hash_table (info);
16619
0
  if (globals == NULL)
16620
0
    return;
16621
16622
0
  globals->byteswap_code = byteswap_code;
16623
0
}
16624
16625
/* Set the sizes of the dynamic sections.  */
16626
16627
static bool
16628
elf32_arm_late_size_sections (struct bfd_link_info *info)
16629
0
{
16630
0
  bfd * dynobj;
16631
0
  asection * s;
16632
0
  bool relocs;
16633
0
  bfd *ibfd;
16634
0
  struct elf32_arm_link_hash_table *htab;
16635
16636
0
  htab = elf32_arm_hash_table (info);
16637
0
  if (htab == NULL)
16638
0
    return false;
16639
16640
0
  dynobj = elf_hash_table (info)->dynobj;
16641
0
  if (dynobj == NULL)
16642
0
    return true;
16643
16644
0
  check_use_blx (htab);
16645
16646
0
  if (elf_hash_table (info)->dynamic_sections_created)
16647
0
    {
16648
      /* Set the contents of the .interp section to the interpreter.  */
16649
0
      if (bfd_link_executable (info) && !info->nointerp)
16650
0
  {
16651
0
    s = elf_hash_table (info)->interp;
16652
0
    BFD_ASSERT (s != NULL);
16653
0
    s->size = sizeof ELF_DYNAMIC_INTERPRETER;
16654
0
    s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
16655
0
    s->alloced = 1;
16656
0
  }
16657
16658
0
#ifdef OBJ_MAYBE_ELF_VXWORKS
16659
0
      if (htab->root.target_os == is_vxworks)
16660
0
  {
16661
0
    if (bfd_link_pic (info))
16662
0
      {
16663
0
        htab->plt_header_size = 0;
16664
0
        htab->plt_entry_size
16665
0
    = 4 * ARRAY_SIZE (elf32_arm_vxworks_shared_plt_entry);
16666
0
      }
16667
0
    else
16668
0
      {
16669
0
        htab->plt_header_size
16670
0
    = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt0_entry);
16671
0
        htab->plt_entry_size
16672
0
    = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt_entry);
16673
0
      }
16674
0
  }
16675
0
      else
16676
0
#endif /* OBJ_MAYBE_ELF_VXWORKS */
16677
0
      if (htab->fdpic_p)
16678
0
  {
16679
0
    htab->plt_header_size = 0;
16680
0
    htab->plt_entry_size = 4 * ARRAY_SIZE (elf32_arm_fdpic_plt_entry);
16681
0
    if (info->flags & DF_BIND_NOW)
16682
0
      htab->plt_entry_size -= 4 * 5;
16683
0
  }
16684
0
      else if (using_thumb_only (htab))
16685
0
  {
16686
0
    htab->plt_header_size = 4 * ARRAY_SIZE (elf32_thumb2_plt0_entry);
16687
0
    htab->plt_entry_size  = 4 * ARRAY_SIZE (elf32_thumb2_plt_entry);
16688
0
  }
16689
0
    }
16690
16691
  /* Set up .got offsets for local syms, and space for local dynamic
16692
     relocs.  */
16693
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
16694
0
    {
16695
0
      bfd_signed_vma *local_got;
16696
0
      bfd_signed_vma *end_local_got;
16697
0
      struct arm_local_iplt_info **local_iplt_ptr, *local_iplt;
16698
0
      char *local_tls_type;
16699
0
      bfd_vma *local_tlsdesc_gotent;
16700
0
      bfd_size_type locsymcount;
16701
0
      Elf_Internal_Shdr *symtab_hdr;
16702
0
      asection *srel;
16703
0
      unsigned int symndx;
16704
0
      struct fdpic_local *local_fdpic_cnts;
16705
16706
0
      if (! is_arm_elf (ibfd))
16707
0
  continue;
16708
16709
0
      for (s = ibfd->sections; s != NULL; s = s->next)
16710
0
  {
16711
0
    struct elf_dyn_relocs *p;
16712
16713
0
    for (p = (struct elf_dyn_relocs *)
16714
0
       elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
16715
0
      {
16716
0
        if (!bfd_is_abs_section (p->sec)
16717
0
      && bfd_is_abs_section (p->sec->output_section))
16718
0
    {
16719
      /* Input section has been discarded, either because
16720
         it is a copy of a linkonce section or due to
16721
         linker script /DISCARD/, so we'll be discarding
16722
         the relocs too.  */
16723
0
    }
16724
0
        else if (htab->root.target_os == is_vxworks
16725
0
           && strcmp (p->sec->output_section->name,
16726
0
          ".tls_vars") == 0)
16727
0
    {
16728
      /* Relocations in vxworks .tls_vars sections are
16729
         handled specially by the loader.  */
16730
0
    }
16731
0
        else if (p->count != 0)
16732
0
    {
16733
0
      srel = elf_section_data (p->sec)->sreloc;
16734
0
      if (htab->fdpic_p && !bfd_link_pic (info))
16735
0
        htab->srofixup->size += 4 * p->count;
16736
0
      else
16737
0
        elf32_arm_allocate_dynrelocs (info, srel, p->count);
16738
0
      if ((p->sec->output_section->flags & SEC_READONLY) != 0)
16739
0
        info->flags |= DF_TEXTREL;
16740
0
    }
16741
0
      }
16742
0
  }
16743
16744
0
      local_got = elf_local_got_refcounts (ibfd);
16745
0
      if (local_got == NULL)
16746
0
  continue;
16747
16748
0
      symtab_hdr = & elf_symtab_hdr (ibfd);
16749
0
      locsymcount = symtab_hdr->sh_info;
16750
0
      end_local_got = local_got + locsymcount;
16751
0
      local_iplt_ptr = elf32_arm_local_iplt (ibfd);
16752
0
      local_tls_type = elf32_arm_local_got_tls_type (ibfd);
16753
0
      local_tlsdesc_gotent = elf32_arm_local_tlsdesc_gotent (ibfd);
16754
0
      local_fdpic_cnts = elf32_arm_local_fdpic_cnts (ibfd);
16755
0
      symndx = 0;
16756
0
      s = htab->root.sgot;
16757
0
      srel = htab->root.srelgot;
16758
0
      for (; local_got < end_local_got;
16759
0
     ++local_got, ++local_iplt_ptr, ++local_tls_type,
16760
0
     ++local_tlsdesc_gotent, ++symndx, ++local_fdpic_cnts)
16761
0
  {
16762
0
    if (symndx >= elf32_arm_num_entries (ibfd))
16763
0
      return false;
16764
16765
0
    *local_tlsdesc_gotent = (bfd_vma) -1;
16766
0
    local_iplt = *local_iplt_ptr;
16767
16768
    /* FDPIC support.  */
16769
0
    if (local_fdpic_cnts->gotofffuncdesc_cnt > 0)
16770
0
      {
16771
0
        if (local_fdpic_cnts->funcdesc_offset == -1)
16772
0
    {
16773
0
      local_fdpic_cnts->funcdesc_offset = s->size;
16774
0
      s->size += 8;
16775
16776
      /* We will add an R_ARM_FUNCDESC_VALUE relocation or two rofixups.  */
16777
0
      if (bfd_link_pic (info))
16778
0
        elf32_arm_allocate_dynrelocs (info, srel, 1);
16779
0
      else
16780
0
        htab->srofixup->size += 8;
16781
0
    }
16782
0
      }
16783
16784
0
    if (local_fdpic_cnts->funcdesc_cnt > 0)
16785
0
      {
16786
0
        if (local_fdpic_cnts->funcdesc_offset == -1)
16787
0
    {
16788
0
      local_fdpic_cnts->funcdesc_offset = s->size;
16789
0
      s->size += 8;
16790
16791
      /* We will add an R_ARM_FUNCDESC_VALUE relocation or two rofixups.  */
16792
0
      if (bfd_link_pic (info))
16793
0
        elf32_arm_allocate_dynrelocs (info, srel, 1);
16794
0
      else
16795
0
        htab->srofixup->size += 8;
16796
0
    }
16797
16798
        /* We will add n R_ARM_RELATIVE relocations or n rofixups.  */
16799
0
        if (bfd_link_pic (info))
16800
0
    elf32_arm_allocate_dynrelocs (info, srel, local_fdpic_cnts->funcdesc_cnt);
16801
0
        else
16802
0
    htab->srofixup->size += 4 * local_fdpic_cnts->funcdesc_cnt;
16803
0
      }
16804
16805
0
    if (local_iplt != NULL)
16806
0
      {
16807
0
        struct elf_dyn_relocs *p;
16808
16809
0
        if (local_iplt->root.refcount > 0)
16810
0
    {
16811
0
      elf32_arm_allocate_plt_entry (info, true,
16812
0
            &local_iplt->root,
16813
0
            &local_iplt->arm);
16814
0
      if (local_iplt->arm.noncall_refcount == 0)
16815
        /* All references to the PLT are calls, so all
16816
           non-call references can resolve directly to the
16817
           run-time target.  This means that the .got entry
16818
           would be the same as the .igot.plt entry, so there's
16819
           no point creating both.  */
16820
0
        *local_got = 0;
16821
0
    }
16822
0
        else
16823
0
    {
16824
0
      BFD_ASSERT (local_iplt->arm.noncall_refcount == 0);
16825
0
      local_iplt->root.offset = (bfd_vma) -1;
16826
0
    }
16827
16828
0
        for (p = local_iplt->dyn_relocs; p != NULL; p = p->next)
16829
0
    {
16830
0
      asection *psrel;
16831
16832
0
      psrel = elf_section_data (p->sec)->sreloc;
16833
0
      if (local_iplt->arm.noncall_refcount == 0)
16834
0
        elf32_arm_allocate_irelocs (info, psrel, p->count);
16835
0
      else
16836
0
        elf32_arm_allocate_dynrelocs (info, psrel, p->count);
16837
0
    }
16838
0
      }
16839
0
    if (*local_got > 0)
16840
0
      {
16841
0
        Elf_Internal_Sym *isym;
16842
16843
0
        *local_got = s->size;
16844
0
        if (*local_tls_type & GOT_TLS_GD)
16845
    /* TLS_GD relocs need an 8-byte structure in the GOT.  */
16846
0
    s->size += 8;
16847
0
        if (*local_tls_type & GOT_TLS_GDESC)
16848
0
    {
16849
0
      *local_tlsdesc_gotent = htab->root.sgotplt->size
16850
0
        - elf32_arm_compute_jump_table_size (htab);
16851
0
      htab->root.sgotplt->size += 8;
16852
0
      *local_got = (bfd_vma) -2;
16853
      /* plt.got_offset needs to know there's a TLS_DESC
16854
         reloc in the middle of .got.plt.  */
16855
0
      htab->num_tls_desc++;
16856
0
    }
16857
0
        if (*local_tls_type & GOT_TLS_IE)
16858
0
    s->size += 4;
16859
16860
0
        if (*local_tls_type & GOT_NORMAL)
16861
0
    {
16862
      /* If the symbol is both GD and GDESC, *local_got
16863
         may have been overwritten.  */
16864
0
      *local_got = s->size;
16865
0
      s->size += 4;
16866
0
    }
16867
16868
0
        isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, ibfd,
16869
0
              symndx);
16870
0
        if (isym == NULL)
16871
0
    return false;
16872
16873
        /* If all references to an STT_GNU_IFUNC PLT are calls,
16874
     then all non-call references, including this GOT entry,
16875
     resolve directly to the run-time target.  */
16876
0
        if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
16877
0
      && (local_iplt == NULL
16878
0
          || local_iplt->arm.noncall_refcount == 0))
16879
0
    elf32_arm_allocate_irelocs (info, srel, 1);
16880
0
        else if (bfd_link_pic (info)
16881
0
           || (info->output_bfd->flags & DYNAMIC) != 0
16882
0
           || htab->fdpic_p)
16883
0
    {
16884
0
      if ((bfd_link_pic (info) && !(*local_tls_type & GOT_TLS_GDESC)))
16885
0
        elf32_arm_allocate_dynrelocs (info, srel, 1);
16886
0
      else if (htab->fdpic_p && *local_tls_type & GOT_NORMAL)
16887
0
        htab->srofixup->size += 4;
16888
16889
0
      if ((bfd_link_pic (info) || htab->fdpic_p)
16890
0
          && *local_tls_type & GOT_TLS_GDESC)
16891
0
        {
16892
0
          elf32_arm_allocate_dynrelocs (info,
16893
0
                htab->root.srelplt, 1);
16894
0
          htab->tls_trampoline = -1;
16895
0
        }
16896
0
    }
16897
0
      }
16898
0
    else
16899
0
      *local_got = (bfd_vma) -1;
16900
0
  }
16901
0
    }
16902
16903
0
  if (htab->tls_ldm_got.refcount > 0)
16904
0
    {
16905
      /* Allocate two GOT entries and one dynamic relocation (if necessary)
16906
   for R_ARM_TLS_LDM32/R_ARM_TLS_LDM32_FDPIC relocations.  */
16907
0
      htab->tls_ldm_got.offset = htab->root.sgot->size;
16908
0
      htab->root.sgot->size += 8;
16909
0
      if (bfd_link_pic (info))
16910
0
  elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16911
0
    }
16912
0
  else
16913
0
    htab->tls_ldm_got.offset = -1;
16914
16915
  /* At the very end of the .rofixup section is a pointer to the GOT,
16916
     reserve space for it. */
16917
0
  if (htab->fdpic_p && htab->srofixup != NULL)
16918
0
    htab->srofixup->size += 4;
16919
16920
  /* Allocate global sym .plt and .got entries, and space for global
16921
     sym dynamic relocs.  */
16922
0
  elf_link_hash_traverse (& htab->root, allocate_dynrelocs_for_symbol, info);
16923
16924
  /* Here we rummage through the found bfds to collect glue information.  */
16925
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
16926
0
    {
16927
0
      if (! is_arm_elf (ibfd))
16928
0
  continue;
16929
16930
      /* Initialise mapping tables for code/data.  */
16931
0
      bfd_elf32_arm_init_maps (ibfd);
16932
16933
0
      if (!bfd_elf32_arm_process_before_allocation (ibfd, info)
16934
0
    || !bfd_elf32_arm_vfp11_erratum_scan (ibfd, info)
16935
0
    || !bfd_elf32_arm_stm32l4xx_erratum_scan (ibfd, info))
16936
0
  _bfd_error_handler (_("errors encountered processing file %pB"), ibfd);
16937
0
    }
16938
16939
  /* Allocate space for the glue sections now that we've sized them.  */
16940
0
  bfd_elf32_arm_allocate_interworking_sections (info);
16941
16942
  /* For every jump slot reserved in the sgotplt, reloc_count is
16943
     incremented.  However, when we reserve space for TLS descriptors,
16944
     it's not incremented, so in order to compute the space reserved
16945
     for them, it suffices to multiply the reloc count by the jump
16946
     slot size.  */
16947
0
  if (htab->root.srelplt)
16948
0
    htab->sgotplt_jump_table_size = elf32_arm_compute_jump_table_size (htab);
16949
16950
0
  if (htab->tls_trampoline)
16951
0
    {
16952
0
      if (htab->root.splt->size == 0)
16953
0
  htab->root.splt->size += htab->plt_header_size;
16954
16955
0
      htab->tls_trampoline = htab->root.splt->size;
16956
0
      htab->root.splt->size += htab->plt_entry_size;
16957
16958
      /* If we're not using lazy TLS relocations, don't generate the
16959
   PLT and GOT entries they require.  */
16960
0
      if ((info->flags & DF_BIND_NOW))
16961
0
  htab->root.tlsdesc_plt = 0;
16962
0
      else
16963
0
  {
16964
0
    htab->root.tlsdesc_got = htab->root.sgot->size;
16965
0
    htab->root.sgot->size += 4;
16966
16967
0
    htab->root.tlsdesc_plt = htab->root.splt->size;
16968
0
    htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
16969
0
  }
16970
0
    }
16971
16972
  /* The check_relocs and adjust_dynamic_symbol entry points have
16973
     determined the sizes of the various dynamic sections.  Allocate
16974
     memory for them.  */
16975
0
  relocs = false;
16976
0
  for (s = dynobj->sections; s != NULL; s = s->next)
16977
0
    {
16978
0
      const char * name;
16979
16980
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
16981
0
  continue;
16982
16983
      /* It's OK to base decisions on the section name, because none
16984
   of the dynobj section names depend upon the input files.  */
16985
0
      name = bfd_section_name (s);
16986
16987
0
      if (s == htab->root.splt)
16988
0
  {
16989
    /* Remember whether there is a PLT.  */
16990
0
    ;
16991
0
  }
16992
0
      else if (startswith (name, ".rel"))
16993
0
  {
16994
0
    if (s->size != 0)
16995
0
      {
16996
        /* Remember whether there are any reloc sections other
16997
     than .rel(a).plt and .rela.plt.unloaded.  */
16998
0
        if (s != htab->root.srelplt && s != htab->srelplt2)
16999
0
    relocs = true;
17000
17001
        /* We use the reloc_count field as a counter if we need
17002
     to copy relocs into the output file.  */
17003
0
        s->reloc_count = 0;
17004
0
      }
17005
0
  }
17006
0
      else if (s != htab->root.sgot
17007
0
         && s != htab->root.sgotplt
17008
0
         && s != htab->root.iplt
17009
0
         && s != htab->root.igotplt
17010
0
         && s != htab->root.sdynbss
17011
0
         && s != htab->root.sdynrelro
17012
0
         && s != htab->srofixup)
17013
0
  {
17014
    /* It's not one of our sections, so don't allocate space.  */
17015
0
    continue;
17016
0
  }
17017
17018
0
      if (s->size == 0)
17019
0
  {
17020
    /* If we don't need this section, strip it from the
17021
       output file.  This is mostly to handle .rel(a).bss and
17022
       .rel(a).plt.  We must create both sections in
17023
       create_dynamic_sections, because they must be created
17024
       before the linker maps input sections to output
17025
       sections.  The linker does that before
17026
       adjust_dynamic_symbol is called, and it is that
17027
       function which decides whether anything needs to go
17028
       into these sections.  */
17029
0
    s->flags |= SEC_EXCLUDE;
17030
0
    continue;
17031
0
  }
17032
17033
0
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
17034
0
  continue;
17035
17036
      /* Allocate memory for the section contents.  */
17037
0
      s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
17038
0
      if (s->contents == NULL)
17039
0
  return false;
17040
0
      s->alloced = 1;
17041
0
    }
17042
17043
0
  return _bfd_elf_maybe_vxworks_add_dynamic_tags (info, relocs);
17044
0
}
17045
17046
/* Size sections even though they're not dynamic.  We use it to setup
17047
   _TLS_MODULE_BASE_, if needed.  */
17048
17049
static bool
17050
elf32_arm_early_size_sections (struct bfd_link_info *info)
17051
0
{
17052
0
  asection *tls_sec;
17053
0
  struct elf32_arm_link_hash_table *htab;
17054
17055
0
  htab = elf32_arm_hash_table (info);
17056
17057
0
  if (bfd_link_relocatable (info))
17058
0
    return true;
17059
17060
0
  tls_sec = elf_hash_table (info)->tls_sec;
17061
17062
0
  if (tls_sec)
17063
0
    {
17064
0
      struct elf_link_hash_entry *tlsbase;
17065
17066
0
      tlsbase = elf_link_hash_lookup
17067
0
  (elf_hash_table (info), "_TLS_MODULE_BASE_", true, true, false);
17068
17069
0
      if (tlsbase)
17070
0
  {
17071
0
    struct bfd_link_hash_entry *bh = NULL;
17072
0
    elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
17073
17074
0
    if (!(_bfd_generic_link_add_one_symbol
17075
0
    (info, info->output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
17076
0
     tls_sec, 0, NULL, false,
17077
0
     bed->collect, &bh)))
17078
0
      return false;
17079
17080
0
    tlsbase->type = STT_TLS;
17081
0
    tlsbase = (struct elf_link_hash_entry *)bh;
17082
0
    tlsbase->def_regular = 1;
17083
0
    tlsbase->other = STV_HIDDEN;
17084
0
    (*bed->elf_backend_hide_symbol) (info, tlsbase, true);
17085
0
  }
17086
0
    }
17087
17088
0
  if (htab->fdpic_p && !bfd_link_relocatable (info)
17089
0
      && !bfd_elf_stack_segment_size (info, "__stacksize", DEFAULT_STACK_SIZE))
17090
0
    return false;
17091
17092
0
  return true;
17093
0
}
17094
17095
/* Finish up dynamic symbol handling.  We set the contents of various
17096
   dynamic sections here.  */
17097
17098
static bool
17099
elf32_arm_finish_dynamic_symbol (struct bfd_link_info * info,
17100
         struct elf_link_hash_entry * h,
17101
         Elf_Internal_Sym * sym)
17102
0
{
17103
0
  struct elf32_arm_link_hash_table *htab;
17104
0
  struct elf32_arm_link_hash_entry *eh;
17105
17106
0
  htab = elf32_arm_hash_table (info);
17107
17108
0
  eh = (struct elf32_arm_link_hash_entry *) h;
17109
17110
0
  if (h->plt.offset != (bfd_vma) -1)
17111
0
    {
17112
0
      if (!eh->is_iplt)
17113
0
  {
17114
0
    BFD_ASSERT (h->dynindx != -1);
17115
0
    if (! elf32_arm_populate_plt_entry (info->output_bfd, info, &h->plt,
17116
0
                &eh->plt, h->dynindx, 0))
17117
0
      return false;
17118
0
  }
17119
17120
0
      if (!h->def_regular)
17121
0
  {
17122
    /* Mark the symbol as undefined, rather than as defined in
17123
       the .plt section.  */
17124
0
    sym->st_shndx = SHN_UNDEF;
17125
    /* If the symbol is weak we need to clear the value.
17126
       Otherwise, the PLT entry would provide a definition for
17127
       the symbol even if the symbol wasn't defined anywhere,
17128
       and so the symbol would never be NULL.  Leave the value if
17129
       there were any relocations where pointer equality matters
17130
       (this is a clue for the dynamic linker, to make function
17131
       pointer comparisons work between an application and shared
17132
       library).  */
17133
0
    if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
17134
0
      sym->st_value = 0;
17135
0
  }
17136
0
      else if (eh->is_iplt && eh->plt.noncall_refcount != 0)
17137
0
  {
17138
    /* At least one non-call relocation references this .iplt entry,
17139
       so the .iplt entry is the function's canonical address.  */
17140
0
    sym->st_info = ELF_ST_INFO (ELF_ST_BIND (sym->st_info), STT_FUNC);
17141
0
    ARM_SET_SYM_BRANCH_TYPE (sym->st_target_internal, ST_BRANCH_TO_ARM);
17142
0
    sym->st_shndx = (_bfd_elf_section_from_bfd_section
17143
0
         (info->output_bfd, htab->root.iplt->output_section));
17144
0
    sym->st_value = (h->plt.offset
17145
0
         + htab->root.iplt->output_section->vma
17146
0
         + htab->root.iplt->output_offset);
17147
0
  }
17148
0
    }
17149
17150
0
  if (h->needs_copy)
17151
0
    {
17152
0
      asection * s;
17153
0
      Elf_Internal_Rela rel;
17154
17155
      /* This symbol needs a copy reloc.  Set it up.  */
17156
0
      BFD_ASSERT (h->dynindx != -1
17157
0
      && (h->root.type == bfd_link_hash_defined
17158
0
          || h->root.type == bfd_link_hash_defweak));
17159
17160
0
      rel.r_addend = 0;
17161
0
      rel.r_offset = (h->root.u.def.value
17162
0
          + h->root.u.def.section->output_section->vma
17163
0
          + h->root.u.def.section->output_offset);
17164
0
      rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_COPY);
17165
0
      if (h->root.u.def.section == htab->root.sdynrelro)
17166
0
  s = htab->root.sreldynrelro;
17167
0
      else
17168
0
  s = htab->root.srelbss;
17169
0
      elf32_arm_add_dynreloc (info->output_bfd, info, s, &rel);
17170
0
    }
17171
17172
  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  On VxWorks,
17173
     and for FDPIC, the _GLOBAL_OFFSET_TABLE_ symbol is not absolute:
17174
     it is relative to the ".got" section.  */
17175
0
  if (h == htab->root.hdynamic
17176
0
      || (!htab->fdpic_p
17177
0
    && htab->root.target_os != is_vxworks
17178
0
    && h == htab->root.hgot))
17179
0
    sym->st_shndx = SHN_ABS;
17180
17181
0
  return true;
17182
0
}
17183
17184
static void
17185
arm_put_trampoline (struct elf32_arm_link_hash_table *htab, bfd *output_bfd,
17186
        void *contents,
17187
        const unsigned long *template, unsigned count)
17188
0
{
17189
0
  unsigned ix;
17190
17191
0
  for (ix = 0; ix != count; ix++)
17192
0
    {
17193
0
      unsigned long insn = template[ix];
17194
17195
      /* Emit mov pc,rx if bx is not permitted.  */
17196
0
      if (htab->fix_v4bx == 1 && (insn & 0x0ffffff0) == 0x012fff10)
17197
0
  insn = (insn & 0xf000000f) | 0x01a0f000;
17198
0
      put_arm_insn (htab, output_bfd, insn, (char *)contents + ix*4);
17199
0
    }
17200
0
}
17201
17202
/* Finish up the dynamic sections.  */
17203
17204
static bool
17205
elf32_arm_finish_dynamic_sections (struct bfd_link_info *info,
17206
           bfd_byte *buf ATTRIBUTE_UNUSED)
17207
0
{
17208
0
  bfd * dynobj;
17209
0
  asection * sgot;
17210
0
  asection * sdyn;
17211
0
  struct elf32_arm_link_hash_table *htab;
17212
17213
0
  htab = elf32_arm_hash_table (info);
17214
0
  if (htab == NULL)
17215
0
    return false;
17216
17217
0
  dynobj = elf_hash_table (info)->dynobj;
17218
17219
0
  sgot = htab->root.sgotplt;
17220
  /* A broken linker script might have discarded the dynamic sections.
17221
     Catch this here so that we do not seg-fault later on.  */
17222
0
  if (sgot != NULL && bfd_is_abs_section (sgot->output_section))
17223
0
    return false;
17224
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
17225
17226
0
  if (elf_hash_table (info)->dynamic_sections_created)
17227
0
    {
17228
0
      asection *splt;
17229
0
      Elf32_External_Dyn *dyncon, *dynconend;
17230
17231
0
      splt = htab->root.splt;
17232
0
      BFD_ASSERT (splt != NULL && sdyn != NULL);
17233
0
      BFD_ASSERT (sgot != NULL);
17234
17235
0
      dyncon = (Elf32_External_Dyn *) sdyn->contents;
17236
0
      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
17237
17238
0
      for (; dyncon < dynconend; dyncon++)
17239
0
  {
17240
0
    Elf_Internal_Dyn dyn;
17241
0
    const char * name;
17242
0
    asection * s;
17243
17244
0
    bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
17245
17246
0
    switch (dyn.d_tag)
17247
0
      {
17248
0
      default:
17249
0
#ifdef OBJ_MAYBE_ELF_VXWORKS
17250
0
        if (htab->root.target_os == is_vxworks
17251
0
      && elf_vxworks_finish_dynamic_entry (info->output_bfd, &dyn))
17252
0
    bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
17253
0
#endif /* OBJ_MAYBE_ELF_VXWORKS */
17254
0
        break;
17255
17256
0
      case DT_HASH:
17257
0
      case DT_STRTAB:
17258
0
      case DT_SYMTAB:
17259
0
      case DT_VERSYM:
17260
0
      case DT_VERDEF:
17261
0
      case DT_VERNEED:
17262
0
        break;
17263
17264
0
      case DT_PLTGOT:
17265
0
        name = ".got.plt";
17266
0
        goto get_vma;
17267
0
      case DT_JMPREL:
17268
0
        name = RELOC_SECTION (htab, ".plt");
17269
0
      get_vma:
17270
0
        s = bfd_get_linker_section (dynobj, name);
17271
0
        if (s == NULL)
17272
0
    {
17273
0
      _bfd_error_handler
17274
0
        (_("could not find section %s"), name);
17275
0
      bfd_set_error (bfd_error_invalid_operation);
17276
0
      return false;
17277
0
    }
17278
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
17279
0
        bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
17280
0
        break;
17281
17282
0
      case DT_PLTRELSZ:
17283
0
        s = htab->root.srelplt;
17284
0
        BFD_ASSERT (s != NULL);
17285
0
        dyn.d_un.d_val = s->size;
17286
0
        bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
17287
0
        break;
17288
17289
0
      case DT_RELSZ:
17290
0
      case DT_RELASZ:
17291
0
      case DT_REL:
17292
0
      case DT_RELA:
17293
0
        break;
17294
17295
0
      case DT_TLSDESC_PLT:
17296
0
        s = htab->root.splt;
17297
0
        dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
17298
0
        + htab->root.tlsdesc_plt);
17299
0
        bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
17300
0
        break;
17301
17302
0
      case DT_TLSDESC_GOT:
17303
0
        s = htab->root.sgot;
17304
0
        dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
17305
0
        + htab->root.tlsdesc_got);
17306
0
        bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
17307
0
        break;
17308
17309
        /* Set the bottom bit of DT_INIT/FINI if the
17310
     corresponding function is Thumb.  */
17311
0
      case DT_INIT:
17312
0
        name = info->init_function;
17313
0
        goto get_sym;
17314
0
      case DT_FINI:
17315
0
        name = info->fini_function;
17316
0
      get_sym:
17317
        /* If it wasn't set by elf_bfd_final_link
17318
     then there is nothing to adjust.  */
17319
0
        if (dyn.d_un.d_val != 0)
17320
0
    {
17321
0
      struct elf_link_hash_entry * eh;
17322
17323
0
      eh = elf_link_hash_lookup (elf_hash_table (info), name,
17324
0
               false, false, true);
17325
0
      if (eh != NULL
17326
0
          && ARM_GET_SYM_BRANCH_TYPE (eh->target_internal)
17327
0
       == ST_BRANCH_TO_THUMB)
17328
0
        {
17329
0
          dyn.d_un.d_val |= 1;
17330
0
          bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
17331
0
        }
17332
0
    }
17333
0
        break;
17334
0
      }
17335
0
  }
17336
17337
      /* Fill in the first entry in the procedure linkage table.  */
17338
0
      if (splt->size > 0 && htab->plt_header_size)
17339
0
  {
17340
0
    const bfd_vma *plt0_entry;
17341
0
    bfd_vma got_address, plt_address, got_displacement;
17342
17343
    /* Calculate the addresses of the GOT and PLT.  */
17344
0
    got_address = sgot->output_section->vma + sgot->output_offset;
17345
0
    plt_address = splt->output_section->vma + splt->output_offset;
17346
17347
0
    if (htab->root.target_os == is_vxworks)
17348
0
      {
17349
        /* The VxWorks GOT is relocated by the dynamic linker.
17350
     Therefore, we must emit relocations rather than simply
17351
     computing the values now.  */
17352
0
        Elf_Internal_Rela rel;
17353
17354
0
        plt0_entry = elf32_arm_vxworks_exec_plt0_entry;
17355
0
        put_arm_insn (htab, info->output_bfd, plt0_entry[0],
17356
0
          splt->contents + 0);
17357
0
        put_arm_insn (htab, info->output_bfd, plt0_entry[1],
17358
0
          splt->contents + 4);
17359
0
        put_arm_insn (htab, info->output_bfd, plt0_entry[2],
17360
0
          splt->contents + 8);
17361
0
        bfd_put_32 (info->output_bfd, got_address, splt->contents + 12);
17362
17363
        /* Generate a relocation for _GLOBAL_OFFSET_TABLE_.  */
17364
0
        rel.r_offset = plt_address + 12;
17365
0
        rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
17366
0
        rel.r_addend = 0;
17367
0
        SWAP_RELOC_OUT (htab) (info->output_bfd, &rel,
17368
0
             htab->srelplt2->contents);
17369
0
      }
17370
0
    else if (using_thumb_only (htab))
17371
0
      {
17372
0
        got_displacement = got_address - (plt_address + 12);
17373
17374
0
        plt0_entry = elf32_thumb2_plt0_entry;
17375
0
        put_arm_insn (htab, info->output_bfd, plt0_entry[0],
17376
0
          splt->contents + 0);
17377
0
        put_arm_insn (htab, info->output_bfd, plt0_entry[1],
17378
0
          splt->contents + 4);
17379
0
        put_arm_insn (htab, info->output_bfd, plt0_entry[2],
17380
0
          splt->contents + 8);
17381
17382
0
        bfd_put_32 (info->output_bfd, got_displacement,
17383
0
        splt->contents + 12);
17384
0
      }
17385
0
    else
17386
0
      {
17387
0
        got_displacement = got_address - (plt_address + 16);
17388
17389
0
        plt0_entry = elf32_arm_plt0_entry;
17390
0
        put_arm_insn (htab, info->output_bfd, plt0_entry[0],
17391
0
          splt->contents + 0);
17392
0
        put_arm_insn (htab, info->output_bfd, plt0_entry[1],
17393
0
          splt->contents + 4);
17394
0
        put_arm_insn (htab, info->output_bfd, plt0_entry[2],
17395
0
          splt->contents + 8);
17396
0
        put_arm_insn (htab, info->output_bfd, plt0_entry[3],
17397
0
          splt->contents + 12);
17398
17399
#ifdef FOUR_WORD_PLT
17400
        /* The displacement value goes in the otherwise-unused
17401
     last word of the second entry.  */
17402
        bfd_put_32 (info->output_bfd, got_displacement,
17403
        splt->contents + 28);
17404
#else
17405
0
        bfd_put_32 (info->output_bfd, got_displacement,
17406
0
        splt->contents + 16);
17407
0
#endif
17408
0
      }
17409
0
  }
17410
17411
      /* UnixWare sets the entsize of .plt to 4, although that doesn't
17412
   really seem like the right value.  */
17413
0
      if (splt->output_section->owner == info->output_bfd)
17414
0
  elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
17415
17416
0
      if (htab->root.tlsdesc_plt)
17417
0
  {
17418
0
    bfd_vma got_address
17419
0
      = sgot->output_section->vma + sgot->output_offset;
17420
0
    bfd_vma gotplt_address = (htab->root.sgot->output_section->vma
17421
0
            + htab->root.sgot->output_offset);
17422
0
    bfd_vma plt_address
17423
0
      = splt->output_section->vma + splt->output_offset;
17424
17425
0
    arm_put_trampoline (htab, info->output_bfd,
17426
0
            splt->contents + htab->root.tlsdesc_plt,
17427
0
            dl_tlsdesc_lazy_trampoline, 6);
17428
17429
0
    bfd_put_32 (info->output_bfd,
17430
0
          gotplt_address + htab->root.tlsdesc_got
17431
0
          - (plt_address + htab->root.tlsdesc_plt)
17432
0
          - dl_tlsdesc_lazy_trampoline[6],
17433
0
          splt->contents + htab->root.tlsdesc_plt + 24);
17434
0
    bfd_put_32 (info->output_bfd,
17435
0
          got_address - (plt_address + htab->root.tlsdesc_plt)
17436
0
          - dl_tlsdesc_lazy_trampoline[7],
17437
0
          splt->contents + htab->root.tlsdesc_plt + 24 + 4);
17438
0
  }
17439
17440
0
      if (htab->tls_trampoline)
17441
0
  {
17442
0
    arm_put_trampoline (htab, info->output_bfd,
17443
0
            splt->contents + htab->tls_trampoline,
17444
0
            tls_trampoline, 3);
17445
#ifdef FOUR_WORD_PLT
17446
    bfd_put_32 (info->output_bfd, 0x00000000,
17447
          splt->contents + htab->tls_trampoline + 12);
17448
#endif
17449
0
  }
17450
17451
0
      if (htab->root.target_os == is_vxworks
17452
0
    && !bfd_link_pic (info)
17453
0
    && htab->root.splt->size > 0)
17454
0
  {
17455
    /* Correct the .rel(a).plt.unloaded relocations.  They will have
17456
       incorrect symbol indexes.  */
17457
0
    int num_plts;
17458
0
    unsigned char *p;
17459
17460
0
    num_plts = ((htab->root.splt->size - htab->plt_header_size)
17461
0
          / htab->plt_entry_size);
17462
0
    p = htab->srelplt2->contents + RELOC_SIZE (htab);
17463
17464
0
    for (; num_plts; num_plts--)
17465
0
      {
17466
0
        Elf_Internal_Rela rel;
17467
17468
0
        SWAP_RELOC_IN (htab) (info->output_bfd, p, &rel);
17469
0
        rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
17470
0
        SWAP_RELOC_OUT (htab) (info->output_bfd, &rel, p);
17471
0
        p += RELOC_SIZE (htab);
17472
17473
0
        SWAP_RELOC_IN (htab) (info->output_bfd, p, &rel);
17474
0
        rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
17475
0
        SWAP_RELOC_OUT (htab) (info->output_bfd, &rel, p);
17476
0
        p += RELOC_SIZE (htab);
17477
0
      }
17478
0
  }
17479
0
    }
17480
17481
  /* Fill in the first three entries in the global offset table.  */
17482
0
  if (sgot)
17483
0
    {
17484
0
      if (sgot->size > 0)
17485
0
  {
17486
0
    if (sdyn == NULL)
17487
0
      bfd_put_32 (info->output_bfd, 0, sgot->contents);
17488
0
    else
17489
0
      bfd_put_32 (info->output_bfd,
17490
0
      sdyn->output_section->vma + sdyn->output_offset,
17491
0
      sgot->contents);
17492
0
    bfd_put_32 (info->output_bfd, 0, sgot->contents + 4);
17493
0
    bfd_put_32 (info->output_bfd, 0, sgot->contents + 8);
17494
0
  }
17495
17496
0
      elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
17497
0
    }
17498
17499
  /* At the very end of the .rofixup section is a pointer to the GOT.  */
17500
0
  if (htab->fdpic_p && htab->srofixup != NULL)
17501
0
    {
17502
0
      struct elf_link_hash_entry *hgot = htab->root.hgot;
17503
17504
0
      bfd_vma got_value = hgot->root.u.def.value
17505
0
  + hgot->root.u.def.section->output_section->vma
17506
0
  + hgot->root.u.def.section->output_offset;
17507
17508
0
      arm_elf_add_rofixup (info->output_bfd, htab->srofixup, got_value);
17509
17510
      /* Make sure we allocated and generated the same number of fixups.  */
17511
0
      BFD_ASSERT (htab->srofixup->reloc_count * 4 == htab->srofixup->size);
17512
0
    }
17513
17514
0
  return true;
17515
0
}
17516
17517
static bool
17518
elf32_arm_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
17519
37
{
17520
37
  Elf_Internal_Ehdr * i_ehdrp;  /* ELF file header, internal form.  */
17521
37
  struct elf32_arm_link_hash_table *globals;
17522
37
  struct elf_segment_map *m;
17523
17524
37
  if (!_bfd_elf_init_file_header (abfd, link_info))
17525
0
    return false;
17526
17527
37
  i_ehdrp = elf_elfheader (abfd);
17528
17529
37
  if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_UNKNOWN)
17530
34
    i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_ARM;
17531
37
  i_ehdrp->e_ident[EI_ABIVERSION] = ARM_ELF_ABI_VERSION;
17532
17533
37
  if (link_info)
17534
0
    {
17535
0
      globals = elf32_arm_hash_table (link_info);
17536
0
      if (globals != NULL && globals->byteswap_code)
17537
0
  i_ehdrp->e_flags |= EF_ARM_BE8;
17538
17539
0
      if (globals->fdpic_p)
17540
0
  i_ehdrp->e_ident[EI_OSABI] |= ELFOSABI_ARM_FDPIC;
17541
0
    }
17542
17543
37
  if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_VER5
17544
1
      && ((i_ehdrp->e_type == ET_DYN) || (i_ehdrp->e_type == ET_EXEC)))
17545
0
    {
17546
0
      int abi = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC, Tag_ABI_VFP_args);
17547
0
      if (abi == AEABI_VFP_args_vfp)
17548
0
  i_ehdrp->e_flags |= EF_ARM_ABI_FLOAT_HARD;
17549
0
      else
17550
0
  i_ehdrp->e_flags |= EF_ARM_ABI_FLOAT_SOFT;
17551
0
    }
17552
17553
  /* Scan segment to set p_flags attribute if it contains only sections with
17554
     SHF_ARM_PURECODE flag.  */
17555
48
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
17556
11
    {
17557
11
      unsigned int j;
17558
17559
11
      if (m->count == 0)
17560
7
  continue;
17561
6
      for (j = 0; j < m->count; j++)
17562
6
  {
17563
6
    if (!(elf_section_flags (m->sections[j]) & SHF_ARM_PURECODE))
17564
4
      break;
17565
6
  }
17566
4
      if (j == m->count)
17567
0
  {
17568
0
    m->p_flags = PF_X;
17569
0
    m->p_flags_valid = 1;
17570
0
  }
17571
4
    }
17572
37
  return true;
17573
37
}
17574
17575
static enum elf_reloc_type_class
17576
elf32_arm_reloc_type_class (const struct bfd_link_info *info,
17577
          const asection *rel_sec ATTRIBUTE_UNUSED,
17578
          const Elf_Internal_Rela *rela)
17579
0
{
17580
0
  struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
17581
17582
0
  if (htab->root.dynsym != NULL
17583
0
      && htab->root.dynsym->contents != NULL)
17584
0
    {
17585
      /* Check relocation against STT_GNU_IFUNC symbol if there are
17586
   dynamic symbols.  */
17587
0
      bfd *abfd = info->output_bfd;
17588
0
      elf_backend_data *bed = get_elf_backend_data (abfd);
17589
0
      unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
17590
0
      if (r_symndx != STN_UNDEF)
17591
0
  {
17592
0
    Elf_Internal_Sym sym;
17593
0
    if (!bed->s->swap_symbol_in (abfd,
17594
0
               (htab->root.dynsym->contents
17595
0
          + r_symndx * bed->s->sizeof_sym),
17596
0
               0, &sym))
17597
0
      {
17598
        /* xgettext:c-format */
17599
0
        _bfd_error_handler (_("%pB symbol number %lu references"
17600
0
            " nonexistent SHT_SYMTAB_SHNDX section"),
17601
0
          abfd, r_symndx);
17602
        /* Ideally an error class should be returned here.  */
17603
0
      }
17604
0
    else if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
17605
0
      return reloc_class_ifunc;
17606
0
  }
17607
0
    }
17608
17609
0
  switch ((int) ELF32_R_TYPE (rela->r_info))
17610
0
    {
17611
0
    case R_ARM_RELATIVE:
17612
0
      return reloc_class_relative;
17613
0
    case R_ARM_JUMP_SLOT:
17614
0
      return reloc_class_plt;
17615
0
    case R_ARM_COPY:
17616
0
      return reloc_class_copy;
17617
0
    case R_ARM_IRELATIVE:
17618
0
      return reloc_class_ifunc;
17619
0
    default:
17620
0
      return reloc_class_normal;
17621
0
    }
17622
0
}
17623
17624
static void
17625
arm_final_write_processing (bfd *abfd)
17626
37
{
17627
37
  bfd_arm_update_notes (abfd, ARM_NOTE_SECTION);
17628
37
}
17629
17630
static bool
17631
elf32_arm_final_write_processing (bfd *abfd)
17632
37
{
17633
37
  arm_final_write_processing (abfd);
17634
37
  return _bfd_elf_final_write_processing (abfd);
17635
37
}
17636
17637
/* Return TRUE if this is an unwinding table entry.  */
17638
17639
static bool
17640
is_arm_elf_unwind_section_name (bfd * abfd ATTRIBUTE_UNUSED, const char * name)
17641
142
{
17642
142
  return (startswith (name, ELF_STRING_ARM_unwind)
17643
142
    || startswith (name, ELF_STRING_ARM_unwind_once));
17644
142
}
17645
17646
17647
/* Set the type and flags for an ARM section.  We do this by
17648
   the section name, which is a hack, but ought to work.  */
17649
17650
static bool
17651
elf32_arm_fake_sections (bfd * abfd, Elf_Internal_Shdr * hdr, asection * sec)
17652
142
{
17653
142
  const char * name;
17654
17655
142
  name = bfd_section_name (sec);
17656
17657
142
  if (is_arm_elf_unwind_section_name (abfd, name))
17658
0
    {
17659
0
      hdr->sh_type = SHT_ARM_EXIDX;
17660
0
      hdr->sh_flags |= SHF_LINK_ORDER;
17661
0
    }
17662
17663
142
  if (sec->flags & SEC_ELF_PURECODE)
17664
14
    hdr->sh_flags |= SHF_ARM_PURECODE;
17665
17666
142
  return true;
17667
142
}
17668
17669
/* Handle an ARM specific section when reading an object file.  This is
17670
   called when bfd_section_from_shdr finds a section with an unknown
17671
   type.  */
17672
17673
static bool
17674
elf32_arm_section_from_shdr (bfd *abfd,
17675
           Elf_Internal_Shdr * hdr,
17676
           const char *name,
17677
           int shindex)
17678
818
{
17679
  /* There ought to be a place to keep ELF backend specific flags, but
17680
     at the moment there isn't one.  We just keep track of the
17681
     sections by their name, instead.  Fortunately, the ABI gives
17682
     names for all the ARM specific sections, so we will probably get
17683
     away with this.  */
17684
818
  switch (hdr->sh_type)
17685
818
    {
17686
15
    case SHT_ARM_EXIDX:
17687
21
    case SHT_ARM_PREEMPTMAP:
17688
21
    case SHT_ARM_ATTRIBUTES:
17689
21
      break;
17690
17691
797
    default:
17692
797
      return false;
17693
818
    }
17694
17695
21
  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
17696
0
    return false;
17697
17698
21
  return true;
17699
21
}
17700
17701
static _arm_elf_section_data *
17702
get_arm_elf_section_data (asection * sec)
17703
0
{
17704
0
  if (sec && sec->owner && is_arm_elf (sec->owner))
17705
0
    return elf32_arm_section_data (sec);
17706
0
  else
17707
0
    return NULL;
17708
0
}
17709
17710
typedef struct
17711
{
17712
  void *flaginfo;
17713
  struct bfd_link_info *info;
17714
  asection *sec;
17715
  int sec_shndx;
17716
  int (*func) (void *, const char *, Elf_Internal_Sym *,
17717
         asection *, struct elf_link_hash_entry *);
17718
} output_arch_syminfo;
17719
17720
enum map_symbol_type
17721
{
17722
  ARM_MAP_ARM,
17723
  ARM_MAP_THUMB,
17724
  ARM_MAP_DATA
17725
};
17726
17727
17728
/* Output a single mapping symbol.  */
17729
17730
static bool
17731
elf32_arm_output_map_sym (output_arch_syminfo *osi,
17732
        enum map_symbol_type type,
17733
        bfd_vma offset)
17734
0
{
17735
0
  static const char *names[3] = {"$a", "$t", "$d"};
17736
0
  Elf_Internal_Sym sym;
17737
17738
0
  sym.st_value = osi->sec->output_section->vma
17739
0
     + osi->sec->output_offset
17740
0
     + offset;
17741
0
  sym.st_size = 0;
17742
0
  sym.st_other = 0;
17743
0
  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
17744
0
  sym.st_shndx = osi->sec_shndx;
17745
0
  sym.st_target_internal = ST_BRANCH_TO_ARM;
17746
0
  elf32_arm_section_map_add (osi->sec, names[type][1], offset);
17747
0
  return osi->func (osi->flaginfo, names[type], &sym, osi->sec, NULL) == 1;
17748
0
}
17749
17750
/* Output mapping symbols for the PLT entry described by ROOT_PLT and ARM_PLT.
17751
   IS_IPLT_ENTRY_P says whether the PLT is in .iplt rather than .plt.  */
17752
17753
static bool
17754
elf32_arm_output_plt_map_1 (output_arch_syminfo *osi,
17755
          bool is_iplt_entry_p,
17756
          union gotplt_union *root_plt,
17757
          struct arm_plt_info *arm_plt)
17758
0
{
17759
0
  struct elf32_arm_link_hash_table *htab;
17760
0
  bfd_vma addr, plt_header_size;
17761
17762
0
  if (root_plt->offset == (bfd_vma) -1)
17763
0
    return true;
17764
17765
0
  htab = elf32_arm_hash_table (osi->info);
17766
0
  if (htab == NULL)
17767
0
    return false;
17768
17769
0
  if (is_iplt_entry_p)
17770
0
    {
17771
0
      osi->sec = htab->root.iplt;
17772
0
      plt_header_size = 0;
17773
0
    }
17774
0
  else
17775
0
    {
17776
0
      osi->sec = htab->root.splt;
17777
0
      plt_header_size = htab->plt_header_size;
17778
0
    }
17779
0
  osi->sec_shndx = (_bfd_elf_section_from_bfd_section
17780
0
        (osi->info->output_bfd, osi->sec->output_section));
17781
17782
0
  addr = root_plt->offset & -2;
17783
0
  if (htab->root.target_os == is_vxworks)
17784
0
    {
17785
0
      if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
17786
0
  return false;
17787
0
      if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 8))
17788
0
  return false;
17789
0
      if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr + 12))
17790
0
  return false;
17791
0
      if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 20))
17792
0
  return false;
17793
0
    }
17794
0
  else if (htab->fdpic_p)
17795
0
    {
17796
0
      enum map_symbol_type type = using_thumb_only (htab)
17797
0
  ? ARM_MAP_THUMB
17798
0
  : ARM_MAP_ARM;
17799
17800
0
      if (elf32_arm_plt_needs_thumb_stub_p (osi->info, arm_plt))
17801
0
  if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr - 4))
17802
0
    return false;
17803
0
      if (!elf32_arm_output_map_sym (osi, type, addr))
17804
0
  return false;
17805
0
      if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 16))
17806
0
  return false;
17807
0
      if (htab->plt_entry_size == 4 * ARRAY_SIZE (elf32_arm_fdpic_plt_entry))
17808
0
  if (!elf32_arm_output_map_sym (osi, type, addr + 24))
17809
0
    return false;
17810
0
    }
17811
0
  else if (using_thumb_only (htab))
17812
0
    {
17813
0
      if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr))
17814
0
  return false;
17815
0
    }
17816
0
  else
17817
0
    {
17818
0
      bool thumb_stub_p;
17819
17820
0
      thumb_stub_p = elf32_arm_plt_needs_thumb_stub_p (osi->info, arm_plt);
17821
0
      if (thumb_stub_p)
17822
0
  {
17823
0
    if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr - 4))
17824
0
      return false;
17825
0
  }
17826
#ifdef FOUR_WORD_PLT
17827
      if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
17828
  return false;
17829
      if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 12))
17830
  return false;
17831
#else
17832
      /* A three-word PLT with no Thumb thunk contains only Arm code,
17833
   so only need to output a mapping symbol for the first PLT entry and
17834
   entries with thumb thunks.  */
17835
0
      if (thumb_stub_p || addr == plt_header_size)
17836
0
  {
17837
0
    if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
17838
0
      return false;
17839
0
  }
17840
0
#endif
17841
0
    }
17842
17843
0
  return true;
17844
0
}
17845
17846
/* Output mapping symbols for PLT entries associated with H.  */
17847
17848
static bool
17849
elf32_arm_output_plt_map (struct elf_link_hash_entry *h, void *inf)
17850
0
{
17851
0
  output_arch_syminfo *osi = (output_arch_syminfo *) inf;
17852
0
  struct elf32_arm_link_hash_entry *eh;
17853
17854
0
  if (h->root.type == bfd_link_hash_indirect)
17855
0
    return true;
17856
17857
0
  if (h->root.type == bfd_link_hash_warning)
17858
    /* When warning symbols are created, they **replace** the "real"
17859
       entry in the hash table, thus we never get to see the real
17860
       symbol in a hash traversal.  So look at it now.  */
17861
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
17862
17863
0
  eh = (struct elf32_arm_link_hash_entry *) h;
17864
0
  return elf32_arm_output_plt_map_1 (osi, SYMBOL_CALLS_LOCAL (osi->info, h),
17865
0
             &h->plt, &eh->plt);
17866
0
}
17867
17868
/* Bind a veneered symbol to its veneer identified by its hash entry
17869
   STUB_ENTRY.  The veneered location thus loose its symbol.  */
17870
17871
static void
17872
arm_stub_claim_sym (struct elf32_arm_stub_hash_entry *stub_entry)
17873
0
{
17874
0
  struct elf32_arm_link_hash_entry *hash = stub_entry->h;
17875
17876
0
  BFD_ASSERT (hash);
17877
0
  hash->root.root.u.def.section = stub_entry->stub_sec;
17878
0
  hash->root.root.u.def.value = stub_entry->stub_offset;
17879
0
  hash->root.size = stub_entry->stub_size;
17880
0
}
17881
17882
/* Output a single local symbol for a generated stub.  */
17883
17884
static bool
17885
elf32_arm_output_stub_sym (output_arch_syminfo *osi, const char *name,
17886
         bfd_vma offset, bfd_vma size)
17887
0
{
17888
0
  Elf_Internal_Sym sym;
17889
17890
0
  sym.st_value = osi->sec->output_section->vma
17891
0
     + osi->sec->output_offset
17892
0
     + offset;
17893
0
  sym.st_size = size;
17894
0
  sym.st_other = 0;
17895
0
  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
17896
0
  sym.st_shndx = osi->sec_shndx;
17897
0
  sym.st_target_internal = ST_BRANCH_TO_ARM;
17898
0
  return osi->func (osi->flaginfo, name, &sym, osi->sec, NULL) == 1;
17899
0
}
17900
17901
static bool
17902
arm_map_one_stub (struct bfd_hash_entry * gen_entry,
17903
      void * in_arg)
17904
0
{
17905
0
  struct elf32_arm_stub_hash_entry *stub_entry;
17906
0
  asection *stub_sec;
17907
0
  bfd_vma addr;
17908
0
  char *stub_name;
17909
0
  output_arch_syminfo *osi;
17910
0
  const insn_sequence *template_sequence;
17911
0
  enum stub_insn_type prev_type;
17912
0
  int size;
17913
0
  int i;
17914
0
  enum map_symbol_type sym_type;
17915
17916
  /* Massage our args to the form they really have.  */
17917
0
  stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
17918
0
  osi = (output_arch_syminfo *) in_arg;
17919
17920
0
  stub_sec = stub_entry->stub_sec;
17921
17922
  /* Ensure this stub is attached to the current section being
17923
     processed.  */
17924
0
  if (stub_sec != osi->sec)
17925
0
    return true;
17926
17927
0
  addr = (bfd_vma) stub_entry->stub_offset;
17928
0
  template_sequence = stub_entry->stub_template;
17929
17930
0
  if (arm_stub_sym_claimed (stub_entry->stub_type))
17931
0
    arm_stub_claim_sym (stub_entry);
17932
0
  else
17933
0
    {
17934
0
      stub_name = stub_entry->output_name;
17935
0
      switch (template_sequence[0].type)
17936
0
  {
17937
0
  case ARM_TYPE:
17938
0
    if (!elf32_arm_output_stub_sym (osi, stub_name, addr,
17939
0
            stub_entry->stub_size))
17940
0
      return false;
17941
0
    break;
17942
0
  case THUMB16_TYPE:
17943
0
  case THUMB32_TYPE:
17944
0
    if (!elf32_arm_output_stub_sym (osi, stub_name, addr | 1,
17945
0
            stub_entry->stub_size))
17946
0
      return false;
17947
0
    break;
17948
0
  default:
17949
0
    BFD_FAIL ();
17950
0
    return 0;
17951
0
  }
17952
0
    }
17953
17954
0
  prev_type = DATA_TYPE;
17955
0
  size = 0;
17956
0
  for (i = 0; i < stub_entry->stub_template_size; i++)
17957
0
    {
17958
0
      switch (template_sequence[i].type)
17959
0
  {
17960
0
  case ARM_TYPE:
17961
0
    sym_type = ARM_MAP_ARM;
17962
0
    break;
17963
17964
0
  case THUMB16_TYPE:
17965
0
  case THUMB32_TYPE:
17966
0
    sym_type = ARM_MAP_THUMB;
17967
0
    break;
17968
17969
0
  case DATA_TYPE:
17970
0
    sym_type = ARM_MAP_DATA;
17971
0
    break;
17972
17973
0
  default:
17974
0
    BFD_FAIL ();
17975
0
    return false;
17976
0
  }
17977
17978
0
      if (template_sequence[i].type != prev_type)
17979
0
  {
17980
0
    prev_type = template_sequence[i].type;
17981
0
    if (!elf32_arm_output_map_sym (osi, sym_type, addr + size))
17982
0
      return false;
17983
0
  }
17984
17985
0
      switch (template_sequence[i].type)
17986
0
  {
17987
0
  case ARM_TYPE:
17988
0
  case THUMB32_TYPE:
17989
0
    size += 4;
17990
0
    break;
17991
17992
0
  case THUMB16_TYPE:
17993
0
    size += 2;
17994
0
    break;
17995
17996
0
  case DATA_TYPE:
17997
0
    size += 4;
17998
0
    break;
17999
18000
0
  default:
18001
0
    BFD_FAIL ();
18002
0
    return false;
18003
0
  }
18004
0
    }
18005
18006
0
  return true;
18007
0
}
18008
18009
/* Output mapping symbols for linker generated sections,
18010
   and for those data-only sections that do not have a
18011
   $d.  */
18012
18013
static bool
18014
elf32_arm_output_arch_local_syms (struct bfd_link_info *info,
18015
          void *flaginfo,
18016
          int (*func) (void *, const char *,
18017
                 Elf_Internal_Sym *,
18018
                 asection *,
18019
                 struct elf_link_hash_entry *))
18020
0
{
18021
0
  output_arch_syminfo osi;
18022
0
  struct elf32_arm_link_hash_table *htab;
18023
0
  bfd_vma offset;
18024
0
  bfd_size_type size;
18025
0
  bfd *input_bfd;
18026
18027
0
  if (info->strip == strip_all
18028
0
      && !info->emitrelocations
18029
0
      && !bfd_link_relocatable (info))
18030
0
    return true;
18031
18032
0
  htab = elf32_arm_hash_table (info);
18033
0
  if (htab == NULL)
18034
0
    return false;
18035
18036
0
  check_use_blx (htab);
18037
18038
0
  osi.flaginfo = flaginfo;
18039
0
  osi.info = info;
18040
0
  osi.func = func;
18041
18042
  /* Add a $d mapping symbol to data-only sections that
18043
     don't have any mapping symbol.  This may result in (harmless) redundant
18044
     mapping symbols.  */
18045
0
  for (input_bfd = info->input_bfds;
18046
0
       input_bfd != NULL;
18047
0
       input_bfd = input_bfd->link.next)
18048
0
    {
18049
0
      if ((input_bfd->flags & (BFD_LINKER_CREATED | HAS_SYMS)) == HAS_SYMS)
18050
0
  for (osi.sec = input_bfd->sections;
18051
0
       osi.sec != NULL;
18052
0
       osi.sec = osi.sec->next)
18053
0
    {
18054
0
      if (osi.sec->output_section != NULL
18055
0
    && ((osi.sec->output_section->flags & (SEC_ALLOC | SEC_CODE))
18056
0
        != 0)
18057
0
    && (osi.sec->flags & (SEC_HAS_CONTENTS | SEC_LINKER_CREATED))
18058
0
       == SEC_HAS_CONTENTS
18059
0
    && get_arm_elf_section_data (osi.sec) != NULL
18060
0
    && get_arm_elf_section_data (osi.sec)->mapcount == 0
18061
0
    && osi.sec->size > 0
18062
0
    && (osi.sec->flags & SEC_EXCLUDE) == 0)
18063
0
        {
18064
0
    osi.sec_shndx = _bfd_elf_section_from_bfd_section
18065
0
      (info->output_bfd, osi.sec->output_section);
18066
0
    if (osi.sec_shndx != (int)SHN_BAD)
18067
0
      elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 0);
18068
0
        }
18069
0
    }
18070
0
    }
18071
18072
  /* ARM->Thumb glue.  */
18073
0
  if (htab->arm_glue_size > 0)
18074
0
    {
18075
0
      osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
18076
0
          ARM2THUMB_GLUE_SECTION_NAME);
18077
18078
0
      osi.sec_shndx = _bfd_elf_section_from_bfd_section
18079
0
    (info->output_bfd, osi.sec->output_section);
18080
0
      if (bfd_link_pic (info)
18081
0
    || htab->pic_veneer)
18082
0
  size = ARM2THUMB_PIC_GLUE_SIZE;
18083
0
      else if (htab->use_blx)
18084
0
  size = ARM2THUMB_V5_STATIC_GLUE_SIZE;
18085
0
      else
18086
0
  size = ARM2THUMB_STATIC_GLUE_SIZE;
18087
18088
0
      for (offset = 0; offset < htab->arm_glue_size; offset += size)
18089
0
  {
18090
0
    elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, offset);
18091
0
    elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, offset + size - 4);
18092
0
  }
18093
0
    }
18094
18095
  /* Thumb->ARM glue.  */
18096
0
  if (htab->thumb_glue_size > 0)
18097
0
    {
18098
0
      osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
18099
0
          THUMB2ARM_GLUE_SECTION_NAME);
18100
18101
0
      osi.sec_shndx = _bfd_elf_section_from_bfd_section
18102
0
    (info->output_bfd, osi.sec->output_section);
18103
0
      size = THUMB2ARM_GLUE_SIZE;
18104
18105
0
      for (offset = 0; offset < htab->thumb_glue_size; offset += size)
18106
0
  {
18107
0
    elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, offset);
18108
0
    elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, offset + 4);
18109
0
  }
18110
0
    }
18111
18112
  /* ARMv4 BX veneers.  */
18113
0
  if (htab->bx_glue_size > 0)
18114
0
    {
18115
0
      osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
18116
0
          ARM_BX_GLUE_SECTION_NAME);
18117
18118
0
      osi.sec_shndx = _bfd_elf_section_from_bfd_section
18119
0
    (info->output_bfd, osi.sec->output_section);
18120
18121
0
      elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0);
18122
0
    }
18123
18124
  /* Long calls stubs.  */
18125
0
  if (htab->stub_bfd && htab->stub_bfd->sections)
18126
0
    {
18127
0
      asection* stub_sec;
18128
18129
0
      for (stub_sec = htab->stub_bfd->sections;
18130
0
     stub_sec != NULL;
18131
0
     stub_sec = stub_sec->next)
18132
0
  {
18133
    /* Ignore non-stub sections.  */
18134
0
    if (!strstr (stub_sec->name, STUB_SUFFIX))
18135
0
      continue;
18136
18137
0
    osi.sec = stub_sec;
18138
18139
0
    osi.sec_shndx = _bfd_elf_section_from_bfd_section
18140
0
      (info->output_bfd, osi.sec->output_section);
18141
18142
0
    bfd_hash_traverse (&htab->stub_hash_table, arm_map_one_stub, &osi);
18143
0
  }
18144
0
    }
18145
18146
  /* Finally, output mapping symbols for the PLT.  */
18147
0
  if (htab->root.splt && htab->root.splt->size > 0)
18148
0
    {
18149
0
      osi.sec = htab->root.splt;
18150
0
      osi.sec_shndx = (_bfd_elf_section_from_bfd_section
18151
0
           (info->output_bfd, osi.sec->output_section));
18152
18153
      /* Output mapping symbols for the plt header.  */
18154
0
      if (htab->root.target_os == is_vxworks)
18155
0
  {
18156
    /* VxWorks shared libraries have no PLT header.  */
18157
0
    if (!bfd_link_pic (info))
18158
0
      {
18159
0
        if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
18160
0
    return false;
18161
0
        if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 12))
18162
0
    return false;
18163
0
      }
18164
0
  }
18165
0
      else if (using_thumb_only (htab) && !htab->fdpic_p)
18166
0
  {
18167
0
    if (!elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, 0))
18168
0
      return false;
18169
0
    if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 12))
18170
0
      return false;
18171
0
    if (!elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, 16))
18172
0
      return false;
18173
0
  }
18174
0
      else if (!htab->fdpic_p)
18175
0
  {
18176
0
    if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
18177
0
      return false;
18178
0
#ifndef FOUR_WORD_PLT
18179
0
    if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 16))
18180
0
      return false;
18181
0
#endif
18182
0
  }
18183
0
    }
18184
0
  if ((htab->root.splt && htab->root.splt->size > 0)
18185
0
      || (htab->root.iplt && htab->root.iplt->size > 0))
18186
0
    {
18187
0
      elf_link_hash_traverse (&htab->root, elf32_arm_output_plt_map, &osi);
18188
0
      for (input_bfd = info->input_bfds;
18189
0
     input_bfd != NULL;
18190
0
     input_bfd = input_bfd->link.next)
18191
0
  {
18192
0
    struct arm_local_iplt_info **local_iplt;
18193
0
    unsigned int i, num_syms;
18194
18195
0
    if (!is_arm_elf (input_bfd))
18196
0
      continue;
18197
18198
0
    local_iplt = elf32_arm_local_iplt (input_bfd);
18199
0
    if (local_iplt != NULL)
18200
0
      {
18201
0
        num_syms = elf_symtab_hdr (input_bfd).sh_info;
18202
0
        if (num_syms > elf32_arm_num_entries (input_bfd))
18203
0
    {
18204
0
      _bfd_error_handler (_("\
18205
0
%pB: Number of symbols in input file has increased from %lu to %u\n"),
18206
0
              input_bfd,
18207
0
              (unsigned long) elf32_arm_num_entries (input_bfd),
18208
0
              num_syms);
18209
0
      return false;
18210
0
    }
18211
0
        for (i = 0; i < num_syms; i++)
18212
0
    if (local_iplt[i] != NULL
18213
0
        && !elf32_arm_output_plt_map_1 (&osi, true,
18214
0
                &local_iplt[i]->root,
18215
0
                &local_iplt[i]->arm))
18216
0
      return false;
18217
0
      }
18218
0
  }
18219
0
    }
18220
0
  if (htab->root.tlsdesc_plt != 0)
18221
0
    {
18222
      /* Mapping symbols for the lazy tls trampoline.  */
18223
0
      if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM,
18224
0
             htab->root.tlsdesc_plt))
18225
0
  return false;
18226
18227
0
      if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA,
18228
0
             htab->root.tlsdesc_plt + 24))
18229
0
  return false;
18230
0
    }
18231
0
  if (htab->tls_trampoline != 0)
18232
0
    {
18233
      /* Mapping symbols for the tls trampoline.  */
18234
0
      if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, htab->tls_trampoline))
18235
0
  return false;
18236
#ifdef FOUR_WORD_PLT
18237
      if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA,
18238
             htab->tls_trampoline + 12))
18239
  return false;
18240
#endif
18241
0
    }
18242
18243
0
  return true;
18244
0
}
18245
18246
/* Filter normal symbols of CMSE entry functions to include in the
18247
   import library.  SYMCOUNT symbols can be examined from their
18248
   pointers in SYMS.  Pointers of symbols to keep should be stored
18249
   continuously at the beginning of that array.
18250
18251
   Returns the number of symbols to keep.  */
18252
18253
static size_t
18254
elf32_arm_filter_cmse_symbols (struct bfd_link_info *info,
18255
             asymbol **syms, size_t symcount)
18256
0
{
18257
0
  size_t maxnamelen;
18258
0
  char *cmse_name;
18259
0
  size_t src_count, dst_count = 0;
18260
0
  struct elf32_arm_link_hash_table *htab;
18261
18262
0
  htab = elf32_arm_hash_table (info);
18263
0
  if (!htab->stub_bfd || !htab->stub_bfd->sections)
18264
0
    symcount = 0;
18265
18266
0
  maxnamelen = 128;
18267
0
  cmse_name = bfd_malloc (maxnamelen);
18268
0
  BFD_ASSERT (cmse_name);
18269
18270
0
  for (src_count = 0; src_count < symcount; src_count++)
18271
0
    {
18272
0
      struct elf32_arm_link_hash_entry *cmse_hash;
18273
0
      asymbol *sym;
18274
0
      flagword flags;
18275
0
      const char *name;
18276
0
      size_t namelen;
18277
18278
0
      sym = syms[src_count];
18279
0
      flags = sym->flags;
18280
0
      name = bfd_asymbol_name (sym);
18281
18282
0
      if ((flags & BSF_FUNCTION) != BSF_FUNCTION)
18283
0
  continue;
18284
0
      if (!(flags & (BSF_GLOBAL | BSF_WEAK)))
18285
0
  continue;
18286
18287
0
      namelen = strlen (name) + sizeof (CMSE_PREFIX) + 1;
18288
0
      if (namelen > maxnamelen)
18289
0
  {
18290
0
    cmse_name = bfd_realloc (cmse_name, namelen);
18291
0
    maxnamelen = namelen;
18292
0
  }
18293
0
      snprintf (cmse_name, maxnamelen, "%s%s", CMSE_PREFIX, name);
18294
0
      cmse_hash = (struct elf32_arm_link_hash_entry *)
18295
0
  elf_link_hash_lookup (&(htab)->root, cmse_name, false, false, true);
18296
18297
0
      if (!cmse_hash
18298
0
    || (cmse_hash->root.root.type != bfd_link_hash_defined
18299
0
        && cmse_hash->root.root.type != bfd_link_hash_defweak)
18300
0
    || cmse_hash->root.type != STT_FUNC)
18301
0
  continue;
18302
18303
0
      syms[dst_count++] = sym;
18304
0
    }
18305
0
  free (cmse_name);
18306
18307
0
  syms[dst_count] = NULL;
18308
18309
0
  return dst_count;
18310
0
}
18311
18312
/* Filter symbols of INFO->OUTPUT_BFD to include in the import library.
18313
   SYMCOUNT symbols can be examined from their pointers in SYMS.
18314
   Pointers of symbols to keep should be stored continuously at
18315
   the beginning of that array.
18316
18317
   Returns the number of symbols to keep.  */
18318
18319
static size_t
18320
elf32_arm_filter_implib_symbols (struct bfd_link_info *info,
18321
         asymbol **syms, size_t symcount)
18322
0
{
18323
0
  struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
18324
18325
  /* Requirement 8 of "ARM v8-M Security Extensions: Requirements on
18326
     Development Tools" (ARM-ECM-0359818) mandates Secure Gateway import
18327
     library to be a relocatable object file.  */
18328
0
  BFD_ASSERT (!(bfd_get_file_flags (info->out_implib_bfd) & EXEC_P));
18329
0
  if (globals->cmse_implib)
18330
0
    return elf32_arm_filter_cmse_symbols (info, syms, symcount);
18331
0
  else
18332
0
    return _bfd_elf_filter_implib_symbols (info, syms, symcount);
18333
0
}
18334
18335
/* Allocate target specific section data.  */
18336
18337
static bool
18338
elf32_arm_new_section_hook (bfd *abfd, asection *sec)
18339
13.7k
{
18340
13.7k
  _arm_elf_section_data *sdata = bfd_zalloc (abfd, sizeof (*sdata));
18341
13.7k
  if (sdata == NULL)
18342
0
    return false;
18343
13.7k
  sec->used_by_bfd = sdata;
18344
18345
13.7k
  return _bfd_elf_new_section_hook (abfd, sec);
18346
13.7k
}
18347
18348
18349
/* Used to order a list of mapping symbols by address.  */
18350
18351
static int
18352
elf32_arm_compare_mapping (const void * a, const void * b)
18353
0
{
18354
0
  const elf32_arm_section_map *amap = (const elf32_arm_section_map *) a;
18355
0
  const elf32_arm_section_map *bmap = (const elf32_arm_section_map *) b;
18356
18357
0
  if (amap->vma > bmap->vma)
18358
0
    return 1;
18359
0
  else if (amap->vma < bmap->vma)
18360
0
    return -1;
18361
0
  else if (amap->type > bmap->type)
18362
    /* Ensure results do not depend on the host qsort for objects with
18363
       multiple mapping symbols at the same address by sorting on type
18364
       after vma.  */
18365
0
    return 1;
18366
0
  else if (amap->type < bmap->type)
18367
0
    return -1;
18368
0
  else
18369
0
    return 0;
18370
0
}
18371
18372
/* Add OFFSET to lower 31 bits of ADDR, leaving other bits unmodified.  */
18373
18374
static unsigned long
18375
offset_prel31 (unsigned long addr, bfd_vma offset)
18376
0
{
18377
0
  return (addr & ~0x7ffffffful) | ((addr + offset) & 0x7ffffffful);
18378
0
}
18379
18380
/* Copy an .ARM.exidx table entry, adding OFFSET to (applied) PREL31
18381
   relocations.  */
18382
18383
static void
18384
copy_exidx_entry (bfd *output_bfd, bfd_byte *to, bfd_byte *from, bfd_vma offset)
18385
0
{
18386
0
  unsigned long first_word = bfd_get_32 (output_bfd, from);
18387
0
  unsigned long second_word = bfd_get_32 (output_bfd, from + 4);
18388
18389
  /* High bit of first word is supposed to be zero.  */
18390
0
  if ((first_word & 0x80000000ul) == 0)
18391
0
    first_word = offset_prel31 (first_word, offset);
18392
18393
  /* If the high bit of the first word is clear, and the bit pattern is not 0x1
18394
     (EXIDX_CANTUNWIND), this is an offset to an .ARM.extab entry.  */
18395
0
  if ((second_word != 0x1) && ((second_word & 0x80000000ul) == 0))
18396
0
    second_word = offset_prel31 (second_word, offset);
18397
18398
0
  bfd_put_32 (output_bfd, first_word, to);
18399
0
  bfd_put_32 (output_bfd, second_word, to + 4);
18400
0
}
18401
18402
/* Data for make_branch_to_a8_stub().  */
18403
18404
struct a8_branch_to_stub_data
18405
{
18406
  asection *writing_section;
18407
  bfd_byte *contents;
18408
};
18409
18410
18411
/* Helper to insert branches to Cortex-A8 erratum stubs in the right
18412
   places for a particular section.  */
18413
18414
static bool
18415
make_branch_to_a8_stub (struct bfd_hash_entry *gen_entry,
18416
           void *in_arg)
18417
0
{
18418
0
  struct elf32_arm_stub_hash_entry *stub_entry;
18419
0
  struct a8_branch_to_stub_data *data;
18420
0
  bfd_byte *contents;
18421
0
  unsigned long branch_insn;
18422
0
  bfd_vma veneered_insn_loc, veneer_entry_loc;
18423
0
  bfd_signed_vma branch_offset;
18424
0
  bfd *abfd;
18425
0
  unsigned int loc;
18426
18427
0
  stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
18428
0
  data = (struct a8_branch_to_stub_data *) in_arg;
18429
18430
0
  if (stub_entry->target_section != data->writing_section
18431
0
      || stub_entry->stub_type < arm_stub_a8_veneer_lwm)
18432
0
    return true;
18433
18434
0
  contents = data->contents;
18435
18436
  /* We use target_section as Cortex-A8 erratum workaround stubs are only
18437
     generated when both source and target are in the same section.  */
18438
0
  veneered_insn_loc = stub_entry->target_section->output_section->vma
18439
0
          + stub_entry->target_section->output_offset
18440
0
          + stub_entry->source_value;
18441
18442
0
  veneer_entry_loc = stub_entry->stub_sec->output_section->vma
18443
0
         + stub_entry->stub_sec->output_offset
18444
0
         + stub_entry->stub_offset;
18445
18446
0
  if (stub_entry->stub_type == arm_stub_a8_veneer_blx)
18447
0
    veneered_insn_loc &= ~3u;
18448
18449
0
  branch_offset = veneer_entry_loc - veneered_insn_loc - 4;
18450
18451
0
  abfd = stub_entry->target_section->owner;
18452
0
  loc = stub_entry->source_value;
18453
18454
  /* We attempt to avoid this condition by setting stubs_always_after_branch
18455
     in elf32_arm_size_stubs if we've enabled the Cortex-A8 erratum workaround.
18456
     This check is just to be on the safe side...  */
18457
0
  if ((veneered_insn_loc & ~0xfff) == (veneer_entry_loc & ~0xfff))
18458
0
    {
18459
0
      _bfd_error_handler (_("%pB: error: Cortex-A8 erratum stub is "
18460
0
          "allocated in unsafe location"), abfd);
18461
0
      return false;
18462
0
    }
18463
18464
0
  switch (stub_entry->stub_type)
18465
0
    {
18466
0
    case arm_stub_a8_veneer_b:
18467
0
    case arm_stub_a8_veneer_b_cond:
18468
0
      branch_insn = 0xf0009000;
18469
0
      goto jump24;
18470
18471
0
    case arm_stub_a8_veneer_blx:
18472
0
      branch_insn = 0xf000e800;
18473
0
      goto jump24;
18474
18475
0
    case arm_stub_a8_veneer_bl:
18476
0
      {
18477
0
  unsigned int i1, j1, i2, j2, s;
18478
18479
0
  branch_insn = 0xf000d000;
18480
18481
0
      jump24:
18482
0
  if (branch_offset < -16777216 || branch_offset > 16777214)
18483
0
    {
18484
      /* There's not much we can do apart from complain if this
18485
         happens.  */
18486
0
      _bfd_error_handler (_("%pB: error: Cortex-A8 erratum stub out "
18487
0
          "of range (input file too large)"), abfd);
18488
0
      return false;
18489
0
    }
18490
18491
  /* i1 = not(j1 eor s), so:
18492
     not i1 = j1 eor s
18493
     j1 = (not i1) eor s.  */
18494
18495
0
  branch_insn |= (branch_offset >> 1) & 0x7ff;
18496
0
  branch_insn |= ((branch_offset >> 12) & 0x3ff) << 16;
18497
0
  i2 = (branch_offset >> 22) & 1;
18498
0
  i1 = (branch_offset >> 23) & 1;
18499
0
  s = (branch_offset >> 24) & 1;
18500
0
  j1 = (!i1) ^ s;
18501
0
  j2 = (!i2) ^ s;
18502
0
  branch_insn |= j2 << 11;
18503
0
  branch_insn |= j1 << 13;
18504
0
  branch_insn |= s << 26;
18505
0
      }
18506
0
      break;
18507
18508
0
    default:
18509
0
      BFD_FAIL ();
18510
0
      return false;
18511
0
    }
18512
18513
0
  bfd_put_16 (abfd, (branch_insn >> 16) & 0xffff, &contents[loc]);
18514
0
  bfd_put_16 (abfd, branch_insn & 0xffff, &contents[loc + 2]);
18515
18516
0
  return true;
18517
0
}
18518
18519
/* Beginning of stm32l4xx work-around.  */
18520
18521
/* Functions encoding instructions necessary for the emission of the
18522
   fix-stm32l4xx-629360.
18523
   Encoding is extracted from the
18524
   ARM (C) Architecture Reference Manual
18525
   ARMv7-A and ARMv7-R edition
18526
   ARM DDI 0406C.b (ID072512).  */
18527
18528
static inline bfd_vma
18529
create_instruction_branch_absolute (int branch_offset)
18530
0
{
18531
  /* A8.8.18 B (A8-334)
18532
     B target_address (Encoding T4).  */
18533
  /* 1111 - 0Sii - iiii - iiii - 10J1 - Jiii - iiii - iiii.  */
18534
  /* jump offset is:  S:I1:I2:imm10:imm11:0.  */
18535
  /* with : I1 = NOT (J1 EOR S) I2 = NOT (J2 EOR S).  */
18536
18537
0
  int s = ((branch_offset & 0x1000000) >> 24);
18538
0
  int j1 = s ^ !((branch_offset & 0x800000) >> 23);
18539
0
  int j2 = s ^ !((branch_offset & 0x400000) >> 22);
18540
18541
0
  if (branch_offset < -(1 << 24) || branch_offset >= (1 << 24))
18542
0
    BFD_ASSERT (0 && "Error: branch out of range.  Cannot create branch.");
18543
18544
0
  bfd_vma patched_inst = 0xf0009000
18545
0
    | s << 26 /* S.  */
18546
0
    | (((unsigned long) (branch_offset) >> 12) & 0x3ff) << 16 /* imm10.  */
18547
0
    | j1 << 13 /* J1.  */
18548
0
    | j2 << 11 /* J2.  */
18549
0
    | (((unsigned long) (branch_offset) >> 1) & 0x7ff); /* imm11.  */
18550
18551
0
  return patched_inst;
18552
0
}
18553
18554
static inline bfd_vma
18555
create_instruction_ldmia (int base_reg, int wback, int reg_mask)
18556
0
{
18557
  /* A8.8.57 LDM/LDMIA/LDMFD (A8-396)
18558
     LDMIA Rn!, {Ra, Rb, Rc, ...} (Encoding T2).  */
18559
0
  bfd_vma patched_inst = 0xe8900000
18560
0
    | (/*W=*/wback << 21)
18561
0
    | (base_reg << 16)
18562
0
    | (reg_mask & 0x0000ffff);
18563
18564
0
  return patched_inst;
18565
0
}
18566
18567
static inline bfd_vma
18568
create_instruction_ldmdb (int base_reg, int wback, int reg_mask)
18569
0
{
18570
  /* A8.8.60 LDMDB/LDMEA (A8-402)
18571
     LDMDB Rn!, {Ra, Rb, Rc, ...} (Encoding T1).  */
18572
0
  bfd_vma patched_inst = 0xe9100000
18573
0
    | (/*W=*/wback << 21)
18574
0
    | (base_reg << 16)
18575
0
    | (reg_mask & 0x0000ffff);
18576
18577
0
  return patched_inst;
18578
0
}
18579
18580
static inline bfd_vma
18581
create_instruction_mov (int target_reg, int source_reg)
18582
0
{
18583
  /* A8.8.103 MOV (register) (A8-486)
18584
     MOV Rd, Rm (Encoding T1).  */
18585
0
  bfd_vma patched_inst = 0x4600
18586
0
    | (target_reg & 0x7)
18587
0
    | ((target_reg & 0x8) >> 3) << 7
18588
0
    | (source_reg << 3);
18589
18590
0
  return patched_inst;
18591
0
}
18592
18593
static inline bfd_vma
18594
create_instruction_sub (int target_reg, int source_reg, int value)
18595
0
{
18596
  /* A8.8.221 SUB (immediate) (A8-708)
18597
     SUB Rd, Rn, #value (Encoding T3).  */
18598
0
  bfd_vma patched_inst = 0xf1a00000
18599
0
    | (target_reg << 8)
18600
0
    | (source_reg << 16)
18601
0
    | (/*S=*/0 << 20)
18602
0
    | ((value & 0x800) >> 11) << 26
18603
0
    | ((value & 0x700) >>  8) << 12
18604
0
    | (value & 0x0ff);
18605
18606
0
  return patched_inst;
18607
0
}
18608
18609
static inline bfd_vma
18610
create_instruction_vldmia (int base_reg, int is_dp, int wback, int num_words,
18611
         int first_reg)
18612
0
{
18613
  /* A8.8.332 VLDM (A8-922)
18614
     VLMD{MODE} Rn{!}, {list} (Encoding T1 or T2).  */
18615
0
  bfd_vma patched_inst = (is_dp ? 0xec900b00 : 0xec900a00)
18616
0
    | (/*W=*/wback << 21)
18617
0
    | (base_reg << 16)
18618
0
    | (num_words & 0x000000ff)
18619
0
    | (((unsigned)first_reg >> 1) & 0x0000000f) << 12
18620
0
    | (first_reg & 0x00000001) << 22;
18621
18622
0
  return patched_inst;
18623
0
}
18624
18625
static inline bfd_vma
18626
create_instruction_vldmdb (int base_reg, int is_dp, int num_words,
18627
         int first_reg)
18628
0
{
18629
  /* A8.8.332 VLDM (A8-922)
18630
     VLMD{MODE} Rn!, {} (Encoding T1 or T2).  */
18631
0
  bfd_vma patched_inst = (is_dp ? 0xed300b00 : 0xed300a00)
18632
0
    | (base_reg << 16)
18633
0
    | (num_words & 0x000000ff)
18634
0
    | (((unsigned)first_reg >>1 ) & 0x0000000f) << 12
18635
0
    | (first_reg & 0x00000001) << 22;
18636
18637
0
  return patched_inst;
18638
0
}
18639
18640
static inline bfd_vma
18641
create_instruction_udf_w (int value)
18642
0
{
18643
  /* A8.8.247 UDF (A8-758)
18644
     Undefined (Encoding T2).  */
18645
0
  bfd_vma patched_inst = 0xf7f0a000
18646
0
    | (value & 0x00000fff)
18647
0
    | (value & 0x000f0000) << 16;
18648
18649
0
  return patched_inst;
18650
0
}
18651
18652
static inline bfd_vma
18653
create_instruction_udf (int value)
18654
0
{
18655
  /* A8.8.247 UDF (A8-758)
18656
     Undefined (Encoding T1).  */
18657
0
  bfd_vma patched_inst = 0xde00
18658
0
    | (value & 0xff);
18659
18660
0
  return patched_inst;
18661
0
}
18662
18663
/* Functions writing an instruction in memory, returning the next
18664
   memory position to write to.  */
18665
18666
static inline bfd_byte *
18667
push_thumb2_insn32 (struct elf32_arm_link_hash_table * htab,
18668
        bfd * output_bfd, bfd_byte *pt, insn32 insn)
18669
0
{
18670
0
  put_thumb2_insn (htab, output_bfd, insn, pt);
18671
0
  return pt + 4;
18672
0
}
18673
18674
static inline bfd_byte *
18675
push_thumb2_insn16 (struct elf32_arm_link_hash_table * htab,
18676
        bfd * output_bfd, bfd_byte *pt, insn32 insn)
18677
0
{
18678
0
  put_thumb_insn (htab, output_bfd, insn, pt);
18679
0
  return pt + 2;
18680
0
}
18681
18682
/* Function filling up a region in memory with T1 and T2 UDFs taking
18683
   care of alignment.  */
18684
18685
static bfd_byte *
18686
stm32l4xx_fill_stub_udf (struct elf32_arm_link_hash_table * htab,
18687
       bfd *       output_bfd,
18688
       const bfd_byte * const  base_stub_contents,
18689
       bfd_byte * const  from_stub_contents,
18690
       const bfd_byte * const  end_stub_contents)
18691
0
{
18692
0
  bfd_byte *current_stub_contents = from_stub_contents;
18693
18694
  /* Fill the remaining of the stub with deterministic contents : UDF
18695
     instructions.
18696
     Check if realignment is needed on modulo 4 frontier using T1, to
18697
     further use T2.  */
18698
0
  if ((current_stub_contents < end_stub_contents)
18699
0
      && !((current_stub_contents - base_stub_contents) % 2)
18700
0
      && ((current_stub_contents - base_stub_contents) % 4))
18701
0
    current_stub_contents =
18702
0
      push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
18703
0
        create_instruction_udf (0));
18704
18705
0
  for (; current_stub_contents < end_stub_contents;)
18706
0
    current_stub_contents =
18707
0
      push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18708
0
        create_instruction_udf_w (0));
18709
18710
0
  return current_stub_contents;
18711
0
}
18712
18713
/* Functions writing the stream of instructions equivalent to the
18714
   derived sequence for ldmia, ldmdb, vldm respectively.  */
18715
18716
static void
18717
stm32l4xx_create_replacing_stub_ldmia (struct elf32_arm_link_hash_table * htab,
18718
               bfd * output_bfd,
18719
               const insn32 initial_insn,
18720
               const bfd_byte *const initial_insn_addr,
18721
               bfd_byte *const base_stub_contents)
18722
0
{
18723
0
  int wback = (initial_insn & 0x00200000) >> 21;
18724
0
  int ri, rn = (initial_insn & 0x000F0000) >> 16;
18725
0
  int insn_all_registers = initial_insn & 0x0000ffff;
18726
0
  int insn_low_registers, insn_high_registers;
18727
0
  int usable_register_mask;
18728
0
  int nb_registers = elf32_arm_popcount (insn_all_registers);
18729
0
  int restore_pc = (insn_all_registers & (1 << 15)) ? 1 : 0;
18730
0
  int restore_rn = (insn_all_registers & (1 << rn)) ? 1 : 0;
18731
0
  bfd_byte *current_stub_contents = base_stub_contents;
18732
18733
0
  BFD_ASSERT (is_thumb2_ldmia (initial_insn));
18734
18735
  /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
18736
     smaller than 8 registers load sequences that do not cause the
18737
     hardware issue.  */
18738
0
  if (nb_registers <= 8)
18739
0
    {
18740
      /* UNTOUCHED : LDMIA Rn{!}, {R-all-register-list}.  */
18741
0
      current_stub_contents =
18742
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18743
0
          initial_insn);
18744
18745
      /* B initial_insn_addr+4.  */
18746
0
      if (!restore_pc)
18747
0
  current_stub_contents =
18748
0
    push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18749
0
            create_instruction_branch_absolute
18750
0
            (initial_insn_addr - current_stub_contents));
18751
18752
      /* Fill the remaining of the stub with deterministic contents.  */
18753
0
      current_stub_contents =
18754
0
  stm32l4xx_fill_stub_udf (htab, output_bfd,
18755
0
         base_stub_contents, current_stub_contents,
18756
0
         base_stub_contents +
18757
0
         STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
18758
18759
0
      return;
18760
0
    }
18761
18762
  /* - reg_list[13] == 0.  */
18763
0
  BFD_ASSERT ((insn_all_registers & (1 << 13))==0);
18764
18765
  /* - reg_list[14] & reg_list[15] != 1.  */
18766
0
  BFD_ASSERT ((insn_all_registers & 0xC000) != 0xC000);
18767
18768
  /* - if (wback==1) reg_list[rn] == 0.  */
18769
0
  BFD_ASSERT (!wback || !restore_rn);
18770
18771
  /* - nb_registers > 8.  */
18772
0
  BFD_ASSERT (elf32_arm_popcount (insn_all_registers) > 8);
18773
18774
  /* At this point, LDMxx initial insn loads between 9 and 14 registers.  */
18775
18776
  /* In the following algorithm, we split this wide LDM using 2 LDM insns:
18777
    - One with the 7 lowest registers (register mask 0x007F)
18778
      This LDM will finally contain between 2 and 7 registers
18779
    - One with the 7 highest registers (register mask 0xDF80)
18780
      This ldm will finally contain between 2 and 7 registers.  */
18781
0
  insn_low_registers = insn_all_registers & 0x007F;
18782
0
  insn_high_registers = insn_all_registers & 0xDF80;
18783
18784
  /* A spare register may be needed during this veneer to temporarily
18785
     handle the base register.  This register will be restored with the
18786
     last LDM operation.
18787
     The usable register may be any general purpose register (that
18788
     excludes PC, SP, LR : register mask is 0x1FFF).  */
18789
0
  usable_register_mask = 0x1FFF;
18790
18791
  /* Generate the stub function.  */
18792
0
  if (wback)
18793
0
    {
18794
      /* LDMIA Rn!, {R-low-register-list} : (Encoding T2).  */
18795
0
      current_stub_contents =
18796
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18797
0
          create_instruction_ldmia
18798
0
          (rn, /*wback=*/1, insn_low_registers));
18799
18800
      /* LDMIA Rn!, {R-high-register-list} : (Encoding T2).  */
18801
0
      current_stub_contents =
18802
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18803
0
          create_instruction_ldmia
18804
0
          (rn, /*wback=*/1, insn_high_registers));
18805
0
      if (!restore_pc)
18806
0
  {
18807
    /* B initial_insn_addr+4.  */
18808
0
    current_stub_contents =
18809
0
      push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18810
0
        create_instruction_branch_absolute
18811
0
        (initial_insn_addr - current_stub_contents));
18812
0
       }
18813
0
    }
18814
0
  else /* if (!wback).  */
18815
0
    {
18816
0
      ri = rn;
18817
18818
      /* If Rn is not part of the high-register-list, move it there.  */
18819
0
      if (!(insn_high_registers & (1 << rn)))
18820
0
  {
18821
    /* Choose a Ri in the high-register-list that will be restored.  */
18822
0
    ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
18823
18824
    /* MOV Ri, Rn.  */
18825
0
    current_stub_contents =
18826
0
      push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
18827
0
        create_instruction_mov (ri, rn));
18828
0
  }
18829
18830
      /* LDMIA Ri!, {R-low-register-list} : (Encoding T2).  */
18831
0
      current_stub_contents =
18832
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18833
0
          create_instruction_ldmia
18834
0
          (ri, /*wback=*/1, insn_low_registers));
18835
18836
      /* LDMIA Ri, {R-high-register-list} : (Encoding T2).  */
18837
0
      current_stub_contents =
18838
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18839
0
          create_instruction_ldmia
18840
0
          (ri, /*wback=*/0, insn_high_registers));
18841
18842
0
      if (!restore_pc)
18843
0
  {
18844
    /* B initial_insn_addr+4.  */
18845
0
    current_stub_contents =
18846
0
      push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18847
0
        create_instruction_branch_absolute
18848
0
        (initial_insn_addr - current_stub_contents));
18849
0
  }
18850
0
    }
18851
18852
  /* Fill the remaining of the stub with deterministic contents.  */
18853
0
  current_stub_contents =
18854
0
    stm32l4xx_fill_stub_udf (htab, output_bfd,
18855
0
           base_stub_contents, current_stub_contents,
18856
0
           base_stub_contents +
18857
0
           STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
18858
0
}
18859
18860
static void
18861
stm32l4xx_create_replacing_stub_ldmdb (struct elf32_arm_link_hash_table * htab,
18862
               bfd * output_bfd,
18863
               const insn32 initial_insn,
18864
               const bfd_byte *const initial_insn_addr,
18865
               bfd_byte *const base_stub_contents)
18866
0
{
18867
0
  int wback = (initial_insn & 0x00200000) >> 21;
18868
0
  int ri, rn = (initial_insn & 0x000f0000) >> 16;
18869
0
  int insn_all_registers = initial_insn & 0x0000ffff;
18870
0
  int insn_low_registers, insn_high_registers;
18871
0
  int usable_register_mask;
18872
0
  int restore_pc = (insn_all_registers & (1 << 15)) ? 1 : 0;
18873
0
  int restore_rn = (insn_all_registers & (1 << rn)) ? 1 : 0;
18874
0
  int nb_registers = elf32_arm_popcount (insn_all_registers);
18875
0
  bfd_byte *current_stub_contents = base_stub_contents;
18876
18877
0
  BFD_ASSERT (is_thumb2_ldmdb (initial_insn));
18878
18879
  /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
18880
     smaller than 8 registers load sequences that do not cause the
18881
     hardware issue.  */
18882
0
  if (nb_registers <= 8)
18883
0
    {
18884
      /* UNTOUCHED : LDMIA Rn{!}, {R-all-register-list}.  */
18885
0
      current_stub_contents =
18886
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18887
0
          initial_insn);
18888
18889
      /* B initial_insn_addr+4.  */
18890
0
      current_stub_contents =
18891
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18892
0
          create_instruction_branch_absolute
18893
0
          (initial_insn_addr - current_stub_contents));
18894
18895
      /* Fill the remaining of the stub with deterministic contents.  */
18896
0
      current_stub_contents =
18897
0
  stm32l4xx_fill_stub_udf (htab, output_bfd,
18898
0
         base_stub_contents, current_stub_contents,
18899
0
         base_stub_contents +
18900
0
         STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
18901
18902
0
      return;
18903
0
    }
18904
18905
  /* - reg_list[13] == 0.  */
18906
0
  BFD_ASSERT ((insn_all_registers & (1 << 13)) == 0);
18907
18908
  /* - reg_list[14] & reg_list[15] != 1.  */
18909
0
  BFD_ASSERT ((insn_all_registers & 0xC000) != 0xC000);
18910
18911
  /* - if (wback==1) reg_list[rn] == 0.  */
18912
0
  BFD_ASSERT (!wback || !restore_rn);
18913
18914
  /* - nb_registers > 8.  */
18915
0
  BFD_ASSERT (elf32_arm_popcount (insn_all_registers) > 8);
18916
18917
  /* At this point, LDMxx initial insn loads between 9 and 14 registers.  */
18918
18919
  /* In the following algorithm, we split this wide LDM using 2 LDM insn:
18920
    - One with the 7 lowest registers (register mask 0x007F)
18921
      This LDM will finally contain between 2 and 7 registers
18922
    - One with the 7 highest registers (register mask 0xDF80)
18923
      This ldm will finally contain between 2 and 7 registers.  */
18924
0
  insn_low_registers = insn_all_registers & 0x007F;
18925
0
  insn_high_registers = insn_all_registers & 0xDF80;
18926
18927
  /* A spare register may be needed during this veneer to temporarily
18928
     handle the base register.  This register will be restored with
18929
     the last LDM operation.
18930
     The usable register may be any general purpose register (that excludes
18931
     PC, SP, LR : register mask is 0x1FFF).  */
18932
0
  usable_register_mask = 0x1FFF;
18933
18934
  /* Generate the stub function.  */
18935
0
  if (!wback && !restore_pc && !restore_rn)
18936
0
    {
18937
      /* Choose a Ri in the low-register-list that will be restored.  */
18938
0
      ri = ctz (insn_low_registers & usable_register_mask & ~(1 << rn));
18939
18940
      /* MOV Ri, Rn.  */
18941
0
      current_stub_contents =
18942
0
  push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
18943
0
          create_instruction_mov (ri, rn));
18944
18945
      /* LDMDB Ri!, {R-high-register-list}.  */
18946
0
      current_stub_contents =
18947
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18948
0
          create_instruction_ldmdb
18949
0
          (ri, /*wback=*/1, insn_high_registers));
18950
18951
      /* LDMDB Ri, {R-low-register-list}.  */
18952
0
      current_stub_contents =
18953
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18954
0
          create_instruction_ldmdb
18955
0
          (ri, /*wback=*/0, insn_low_registers));
18956
18957
      /* B initial_insn_addr+4.  */
18958
0
      current_stub_contents =
18959
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18960
0
          create_instruction_branch_absolute
18961
0
          (initial_insn_addr - current_stub_contents));
18962
0
    }
18963
0
  else if (wback && !restore_pc && !restore_rn)
18964
0
    {
18965
      /* LDMDB Rn!, {R-high-register-list}.  */
18966
0
      current_stub_contents =
18967
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18968
0
          create_instruction_ldmdb
18969
0
          (rn, /*wback=*/1, insn_high_registers));
18970
18971
      /* LDMDB Rn!, {R-low-register-list}.  */
18972
0
      current_stub_contents =
18973
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18974
0
          create_instruction_ldmdb
18975
0
          (rn, /*wback=*/1, insn_low_registers));
18976
18977
      /* B initial_insn_addr+4.  */
18978
0
      current_stub_contents =
18979
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18980
0
          create_instruction_branch_absolute
18981
0
          (initial_insn_addr - current_stub_contents));
18982
0
    }
18983
0
  else if (!wback && restore_pc && !restore_rn)
18984
0
    {
18985
      /* Choose a Ri in the high-register-list that will be restored.  */
18986
0
      ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
18987
18988
      /* SUB Ri, Rn, #(4*nb_registers).  */
18989
0
      current_stub_contents =
18990
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18991
0
          create_instruction_sub (ri, rn, (4 * nb_registers)));
18992
18993
      /* LDMIA Ri!, {R-low-register-list}.  */
18994
0
      current_stub_contents =
18995
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18996
0
          create_instruction_ldmia
18997
0
          (ri, /*wback=*/1, insn_low_registers));
18998
18999
      /* LDMIA Ri, {R-high-register-list}.  */
19000
0
      current_stub_contents =
19001
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19002
0
          create_instruction_ldmia
19003
0
          (ri, /*wback=*/0, insn_high_registers));
19004
0
    }
19005
0
  else if (wback && restore_pc && !restore_rn)
19006
0
    {
19007
      /* Choose a Ri in the high-register-list that will be restored.  */
19008
0
      ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
19009
19010
      /* SUB Rn, Rn, #(4*nb_registers)  */
19011
0
      current_stub_contents =
19012
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19013
0
          create_instruction_sub (rn, rn, (4 * nb_registers)));
19014
19015
      /* MOV Ri, Rn.  */
19016
0
      current_stub_contents =
19017
0
  push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
19018
0
          create_instruction_mov (ri, rn));
19019
19020
      /* LDMIA Ri!, {R-low-register-list}.  */
19021
0
      current_stub_contents =
19022
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19023
0
          create_instruction_ldmia
19024
0
          (ri, /*wback=*/1, insn_low_registers));
19025
19026
      /* LDMIA Ri, {R-high-register-list}.  */
19027
0
      current_stub_contents =
19028
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19029
0
          create_instruction_ldmia
19030
0
          (ri, /*wback=*/0, insn_high_registers));
19031
0
    }
19032
0
  else if (!wback && !restore_pc && restore_rn)
19033
0
    {
19034
0
      ri = rn;
19035
0
      if (!(insn_low_registers & (1 << rn)))
19036
0
  {
19037
    /* Choose a Ri in the low-register-list that will be restored.  */
19038
0
    ri = ctz (insn_low_registers & usable_register_mask & ~(1 << rn));
19039
19040
    /* MOV Ri, Rn.  */
19041
0
    current_stub_contents =
19042
0
      push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
19043
0
        create_instruction_mov (ri, rn));
19044
0
  }
19045
19046
      /* LDMDB Ri!, {R-high-register-list}.  */
19047
0
      current_stub_contents =
19048
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19049
0
          create_instruction_ldmdb
19050
0
          (ri, /*wback=*/1, insn_high_registers));
19051
19052
      /* LDMDB Ri, {R-low-register-list}.  */
19053
0
      current_stub_contents =
19054
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19055
0
          create_instruction_ldmdb
19056
0
          (ri, /*wback=*/0, insn_low_registers));
19057
19058
      /* B initial_insn_addr+4.  */
19059
0
      current_stub_contents =
19060
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19061
0
          create_instruction_branch_absolute
19062
0
          (initial_insn_addr - current_stub_contents));
19063
0
    }
19064
0
  else if (!wback && restore_pc && restore_rn)
19065
0
    {
19066
0
      ri = rn;
19067
0
      if (!(insn_high_registers & (1 << rn)))
19068
0
  {
19069
    /* Choose a Ri in the high-register-list that will be restored.  */
19070
0
    ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
19071
0
  }
19072
19073
      /* SUB Ri, Rn, #(4*nb_registers).  */
19074
0
      current_stub_contents =
19075
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19076
0
          create_instruction_sub (ri, rn, (4 * nb_registers)));
19077
19078
      /* LDMIA Ri!, {R-low-register-list}.  */
19079
0
      current_stub_contents =
19080
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19081
0
          create_instruction_ldmia
19082
0
          (ri, /*wback=*/1, insn_low_registers));
19083
19084
      /* LDMIA Ri, {R-high-register-list}.  */
19085
0
      current_stub_contents =
19086
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19087
0
          create_instruction_ldmia
19088
0
          (ri, /*wback=*/0, insn_high_registers));
19089
0
    }
19090
0
  else if (wback && restore_rn)
19091
0
    {
19092
      /* The assembler should not have accepted to encode this.  */
19093
0
      BFD_ASSERT (0 && "Cannot patch an instruction that has an "
19094
0
  "undefined behavior.\n");
19095
0
    }
19096
19097
  /* Fill the remaining of the stub with deterministic contents.  */
19098
0
  current_stub_contents =
19099
0
    stm32l4xx_fill_stub_udf (htab, output_bfd,
19100
0
           base_stub_contents, current_stub_contents,
19101
0
           base_stub_contents +
19102
0
           STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
19103
19104
0
}
19105
19106
static void
19107
stm32l4xx_create_replacing_stub_vldm (struct elf32_arm_link_hash_table * htab,
19108
              bfd * output_bfd,
19109
              const insn32 initial_insn,
19110
              const bfd_byte *const initial_insn_addr,
19111
              bfd_byte *const base_stub_contents)
19112
0
{
19113
0
  int num_words = initial_insn & 0xff;
19114
0
  bfd_byte *current_stub_contents = base_stub_contents;
19115
19116
0
  BFD_ASSERT (is_thumb2_vldm (initial_insn));
19117
19118
  /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
19119
     smaller than 8 words load sequences that do not cause the
19120
     hardware issue.  */
19121
0
  if (num_words <= 8)
19122
0
    {
19123
      /* Untouched instruction.  */
19124
0
      current_stub_contents =
19125
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19126
0
          initial_insn);
19127
19128
      /* B initial_insn_addr+4.  */
19129
0
      current_stub_contents =
19130
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19131
0
          create_instruction_branch_absolute
19132
0
          (initial_insn_addr - current_stub_contents));
19133
0
    }
19134
0
  else
19135
0
    {
19136
0
      bool is_dp = /* DP encoding.  */
19137
0
  (initial_insn & 0xfe100f00) == 0xec100b00;
19138
0
      bool is_ia_nobang = /* (IA without !).  */
19139
0
  (((initial_insn << 7) >> 28) & 0xd) == 0x4;
19140
0
      bool is_ia_bang = /* (IA with !) - includes VPOP.  */
19141
0
  (((initial_insn << 7) >> 28) & 0xd) == 0x5;
19142
0
      bool is_db_bang = /* (DB with !).  */
19143
0
  (((initial_insn << 7) >> 28) & 0xd) == 0x9;
19144
0
      int base_reg = ((unsigned int) initial_insn << 12) >> 28;
19145
      /* d = UInt (Vd:D);.  */
19146
0
      int first_reg = ((((unsigned int) initial_insn << 16) >> 28) << 1)
19147
0
  | (((unsigned int)initial_insn << 9) >> 31);
19148
19149
      /* Compute the number of 8-words chunks needed to split.  */
19150
0
      int chunks = (num_words % 8) ? (num_words / 8 + 1) : (num_words / 8);
19151
0
      int chunk;
19152
19153
      /* The test coverage has been done assuming the following
19154
   hypothesis that exactly one of the previous is_ predicates is
19155
   true.  */
19156
0
      BFD_ASSERT (    (is_ia_nobang ^ is_ia_bang ^ is_db_bang)
19157
0
      && !(is_ia_nobang & is_ia_bang & is_db_bang));
19158
19159
      /* We treat the cutting of the words in one pass for all
19160
   cases, then we emit the adjustments:
19161
19162
   vldm rx, {...}
19163
   -> vldm rx!, {8_words_or_less} for each needed 8_word
19164
   -> sub rx, rx, #size (list)
19165
19166
   vldm rx!, {...}
19167
   -> vldm rx!, {8_words_or_less} for each needed 8_word
19168
   This also handles vpop instruction (when rx is sp)
19169
19170
   vldmd rx!, {...}
19171
   -> vldmb rx!, {8_words_or_less} for each needed 8_word.  */
19172
0
      for (chunk = 0; chunk < chunks; ++chunk)
19173
0
  {
19174
0
    bfd_vma new_insn = 0;
19175
19176
0
    if (is_ia_nobang || is_ia_bang)
19177
0
      {
19178
0
        new_insn = create_instruction_vldmia
19179
0
    (base_reg,
19180
0
     is_dp,
19181
0
     /*wback= .  */1,
19182
0
     chunks - (chunk + 1) ?
19183
0
     8 : num_words - chunk * 8,
19184
0
     first_reg + chunk * 8);
19185
0
      }
19186
0
    else if (is_db_bang)
19187
0
      {
19188
0
        new_insn = create_instruction_vldmdb
19189
0
    (base_reg,
19190
0
     is_dp,
19191
0
     chunks - (chunk + 1) ?
19192
0
     8 : num_words - chunk * 8,
19193
0
     first_reg + chunk * 8);
19194
0
      }
19195
19196
0
    if (new_insn)
19197
0
      current_stub_contents =
19198
0
        push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19199
0
          new_insn);
19200
0
  }
19201
19202
      /* Only this case requires the base register compensation
19203
   subtract.  */
19204
0
      if (is_ia_nobang)
19205
0
  {
19206
0
    current_stub_contents =
19207
0
      push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19208
0
        create_instruction_sub
19209
0
        (base_reg, base_reg, 4*num_words));
19210
0
  }
19211
19212
      /* B initial_insn_addr+4.  */
19213
0
      current_stub_contents =
19214
0
  push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19215
0
          create_instruction_branch_absolute
19216
0
          (initial_insn_addr - current_stub_contents));
19217
0
    }
19218
19219
  /* Fill the remaining of the stub with deterministic contents.  */
19220
0
  current_stub_contents =
19221
0
    stm32l4xx_fill_stub_udf (htab, output_bfd,
19222
0
           base_stub_contents, current_stub_contents,
19223
0
           base_stub_contents +
19224
0
           STM32L4XX_ERRATUM_VLDM_VENEER_SIZE);
19225
0
}
19226
19227
static void
19228
stm32l4xx_create_replacing_stub (struct elf32_arm_link_hash_table * htab,
19229
         bfd * output_bfd,
19230
         const insn32 wrong_insn,
19231
         const bfd_byte *const wrong_insn_addr,
19232
         bfd_byte *const stub_contents)
19233
0
{
19234
0
  if (is_thumb2_ldmia (wrong_insn))
19235
0
    stm32l4xx_create_replacing_stub_ldmia (htab, output_bfd,
19236
0
             wrong_insn, wrong_insn_addr,
19237
0
             stub_contents);
19238
0
  else if (is_thumb2_ldmdb (wrong_insn))
19239
0
    stm32l4xx_create_replacing_stub_ldmdb (htab, output_bfd,
19240
0
             wrong_insn, wrong_insn_addr,
19241
0
             stub_contents);
19242
0
  else if (is_thumb2_vldm (wrong_insn))
19243
0
    stm32l4xx_create_replacing_stub_vldm (htab, output_bfd,
19244
0
            wrong_insn, wrong_insn_addr,
19245
0
            stub_contents);
19246
0
}
19247
19248
/* End of stm32l4xx work-around.  */
19249
19250
19251
/* Do code byteswapping.  Return FALSE afterwards so that the section is
19252
   written out as normal.  */
19253
19254
static bool
19255
elf32_arm_write_section (bfd *output_bfd,
19256
       struct bfd_link_info *link_info,
19257
       asection *sec,
19258
       bfd_byte *contents)
19259
0
{
19260
0
  unsigned int mapcount, errcount;
19261
0
  _arm_elf_section_data *arm_data;
19262
0
  struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
19263
0
  elf32_arm_section_map *map;
19264
0
  elf32_vfp11_erratum_list *errnode;
19265
0
  elf32_stm32l4xx_erratum_list *stm32l4xx_errnode;
19266
0
  bfd_vma ptr;
19267
0
  bfd_vma end;
19268
0
  bfd_vma offset = sec->output_section->vma + sec->output_offset;
19269
0
  bfd_byte tmp;
19270
0
  unsigned int i;
19271
19272
0
  if (globals == NULL)
19273
0
    return false;
19274
19275
  /* If this section has not been allocated an _arm_elf_section_data
19276
     structure then we cannot record anything.  */
19277
0
  arm_data = get_arm_elf_section_data (sec);
19278
0
  if (arm_data == NULL)
19279
0
    return false;
19280
19281
0
  mapcount = arm_data->mapcount;
19282
0
  map = arm_data->map;
19283
0
  errcount = arm_data->erratumcount;
19284
19285
0
  if (errcount != 0)
19286
0
    {
19287
0
      unsigned int endianflip = bfd_big_endian (output_bfd) ? 3 : 0;
19288
19289
0
      for (errnode = arm_data->erratumlist; errnode != 0;
19290
0
     errnode = errnode->next)
19291
0
  {
19292
0
    bfd_vma target = errnode->vma - offset;
19293
19294
0
    switch (errnode->type)
19295
0
      {
19296
0
      case VFP11_ERRATUM_BRANCH_TO_ARM_VENEER:
19297
0
        {
19298
0
    bfd_vma branch_to_veneer;
19299
    /* Original condition code of instruction, plus bit mask for
19300
       ARM B instruction.  */
19301
0
    unsigned int insn = (errnode->u.b.vfp_insn & 0xf0000000)
19302
0
          | 0x0a000000;
19303
19304
    /* The instruction is before the label.  */
19305
0
    target -= 4;
19306
19307
    /* Above offset included in -4 below.  */
19308
0
    branch_to_veneer = errnode->u.b.veneer->vma
19309
0
           - errnode->vma - 4;
19310
19311
0
    if ((signed) branch_to_veneer < -(1 << 25)
19312
0
        || (signed) branch_to_veneer >= (1 << 25))
19313
0
      _bfd_error_handler (_("%pB: error: VFP11 veneer out of "
19314
0
          "range"), output_bfd);
19315
19316
0
    insn |= (branch_to_veneer >> 2) & 0xffffff;
19317
0
    contents[endianflip ^ target] = insn & 0xff;
19318
0
    contents[endianflip ^ (target + 1)] = (insn >> 8) & 0xff;
19319
0
    contents[endianflip ^ (target + 2)] = (insn >> 16) & 0xff;
19320
0
    contents[endianflip ^ (target + 3)] = (insn >> 24) & 0xff;
19321
0
        }
19322
0
        break;
19323
19324
0
      case VFP11_ERRATUM_ARM_VENEER:
19325
0
        {
19326
0
    bfd_vma branch_from_veneer;
19327
0
    unsigned int insn;
19328
19329
    /* Take size of veneer into account.  */
19330
0
    branch_from_veneer = errnode->u.v.branch->vma
19331
0
             - errnode->vma - 12;
19332
19333
0
    if ((signed) branch_from_veneer < -(1 << 25)
19334
0
        || (signed) branch_from_veneer >= (1 << 25))
19335
0
      _bfd_error_handler (_("%pB: error: VFP11 veneer out of "
19336
0
          "range"), output_bfd);
19337
19338
    /* Original instruction.  */
19339
0
    insn = errnode->u.v.branch->u.b.vfp_insn;
19340
0
    contents[endianflip ^ target] = insn & 0xff;
19341
0
    contents[endianflip ^ (target + 1)] = (insn >> 8) & 0xff;
19342
0
    contents[endianflip ^ (target + 2)] = (insn >> 16) & 0xff;
19343
0
    contents[endianflip ^ (target + 3)] = (insn >> 24) & 0xff;
19344
19345
    /* Branch back to insn after original insn.  */
19346
0
    insn = 0xea000000 | ((branch_from_veneer >> 2) & 0xffffff);
19347
0
    contents[endianflip ^ (target + 4)] = insn & 0xff;
19348
0
    contents[endianflip ^ (target + 5)] = (insn >> 8) & 0xff;
19349
0
    contents[endianflip ^ (target + 6)] = (insn >> 16) & 0xff;
19350
0
    contents[endianflip ^ (target + 7)] = (insn >> 24) & 0xff;
19351
0
        }
19352
0
        break;
19353
19354
0
      default:
19355
0
        abort ();
19356
0
      }
19357
0
  }
19358
0
    }
19359
19360
0
  if (arm_data->stm32l4xx_erratumcount != 0)
19361
0
    {
19362
0
      for (stm32l4xx_errnode = arm_data->stm32l4xx_erratumlist;
19363
0
     stm32l4xx_errnode != 0;
19364
0
     stm32l4xx_errnode = stm32l4xx_errnode->next)
19365
0
  {
19366
0
    bfd_vma target = stm32l4xx_errnode->vma - offset;
19367
19368
0
    switch (stm32l4xx_errnode->type)
19369
0
      {
19370
0
      case STM32L4XX_ERRATUM_BRANCH_TO_VENEER:
19371
0
        {
19372
0
    unsigned int insn;
19373
0
    bfd_vma branch_to_veneer =
19374
0
      stm32l4xx_errnode->u.b.veneer->vma - stm32l4xx_errnode->vma;
19375
19376
0
    if ((signed) branch_to_veneer < -(1 << 24)
19377
0
        || (signed) branch_to_veneer >= (1 << 24))
19378
0
      {
19379
0
        bfd_vma out_of_range =
19380
0
          ((signed) branch_to_veneer < -(1 << 24)) ?
19381
0
          - branch_to_veneer - (1 << 24) :
19382
0
          ((signed) branch_to_veneer >= (1 << 24)) ?
19383
0
          branch_to_veneer - (1 << 24) : 0;
19384
19385
0
        _bfd_error_handler
19386
0
          (_("%pB(%#" PRIx64 "): error: "
19387
0
       "cannot create STM32L4XX veneer; "
19388
0
       "jump out of range by %" PRId64 " bytes; "
19389
0
       "cannot encode branch instruction"),
19390
0
           output_bfd,
19391
0
           (uint64_t) (stm32l4xx_errnode->vma - 4),
19392
0
           (int64_t) out_of_range);
19393
0
        continue;
19394
0
      }
19395
19396
0
    insn = create_instruction_branch_absolute
19397
0
      (stm32l4xx_errnode->u.b.veneer->vma - stm32l4xx_errnode->vma);
19398
19399
    /* The instruction is before the label.  */
19400
0
    target -= 4;
19401
19402
0
    put_thumb2_insn (globals, output_bfd,
19403
0
         (bfd_vma) insn, contents + target);
19404
0
        }
19405
0
        break;
19406
19407
0
      case STM32L4XX_ERRATUM_VENEER:
19408
0
        {
19409
0
    bfd_byte * veneer;
19410
0
    bfd_byte * veneer_r;
19411
0
    unsigned int insn;
19412
19413
0
    veneer = contents + target;
19414
0
    veneer_r = veneer
19415
0
      + stm32l4xx_errnode->u.b.veneer->vma
19416
0
      - stm32l4xx_errnode->vma - 4;
19417
19418
0
    if ((signed) (veneer_r - veneer -
19419
0
            STM32L4XX_ERRATUM_VLDM_VENEER_SIZE >
19420
0
            STM32L4XX_ERRATUM_LDM_VENEER_SIZE ?
19421
0
            STM32L4XX_ERRATUM_VLDM_VENEER_SIZE :
19422
0
            STM32L4XX_ERRATUM_LDM_VENEER_SIZE) < -(1 << 24)
19423
0
        || (signed) (veneer_r - veneer) >= (1 << 24))
19424
0
      {
19425
0
        _bfd_error_handler (_("%pB: error: cannot create STM32L4XX "
19426
0
            "veneer"), output_bfd);
19427
0
         continue;
19428
0
      }
19429
19430
    /* Original instruction.  */
19431
0
    insn = stm32l4xx_errnode->u.v.branch->u.b.insn;
19432
19433
0
    stm32l4xx_create_replacing_stub
19434
0
      (globals, output_bfd, insn, (void*)veneer_r, (void*)veneer);
19435
0
        }
19436
0
        break;
19437
19438
0
      default:
19439
0
        abort ();
19440
0
      }
19441
0
  }
19442
0
    }
19443
19444
0
  if (arm_data->elf.this_hdr.sh_type == SHT_ARM_EXIDX)
19445
0
    {
19446
0
      arm_unwind_table_edit *edit_node
19447
0
  = arm_data->u.exidx.unwind_edit_list;
19448
      /* Now, sec->size is the size of the section we will write.  The original
19449
   size (before we merged duplicate entries and inserted EXIDX_CANTUNWIND
19450
   markers) was sec->rawsize.  (This isn't the case if we perform no
19451
   edits, then rawsize will be zero and we should use size).  */
19452
0
      bfd_byte *edited_contents = (bfd_byte *) bfd_malloc (sec->size);
19453
0
      unsigned int input_size = sec->rawsize ? sec->rawsize : sec->size;
19454
0
      unsigned int in_index, out_index;
19455
0
      bfd_vma add_to_offsets = 0;
19456
19457
0
      if (edited_contents == NULL)
19458
0
  return false;
19459
0
      for (in_index = 0, out_index = 0; in_index * 8 < input_size || edit_node;)
19460
0
  {
19461
0
    if (edit_node)
19462
0
      {
19463
0
        unsigned int edit_index = edit_node->index;
19464
19465
0
        if (in_index < edit_index && in_index * 8 < input_size)
19466
0
    {
19467
0
      copy_exidx_entry (output_bfd, edited_contents + out_index * 8,
19468
0
            contents + in_index * 8, add_to_offsets);
19469
0
      out_index++;
19470
0
      in_index++;
19471
0
    }
19472
0
        else if (in_index == edit_index
19473
0
           || (in_index * 8 >= input_size
19474
0
         && edit_index == UINT_MAX))
19475
0
    {
19476
0
      switch (edit_node->type)
19477
0
        {
19478
0
        case DELETE_EXIDX_ENTRY:
19479
0
          in_index++;
19480
0
          add_to_offsets += 8;
19481
0
          break;
19482
19483
0
        case INSERT_EXIDX_CANTUNWIND_AT_END:
19484
0
          {
19485
0
      asection *text_sec = edit_node->linked_section;
19486
0
      bfd_vma text_offset = text_sec->output_section->vma
19487
0
                + text_sec->output_offset
19488
0
                + text_sec->size;
19489
0
      bfd_vma exidx_offset = offset + out_index * 8;
19490
0
      unsigned long prel31_offset;
19491
19492
      /* Note: this is meant to be equivalent to an
19493
         R_ARM_PREL31 relocation.  These synthetic
19494
         EXIDX_CANTUNWIND markers are not relocated by the
19495
         usual BFD method.  */
19496
0
      prel31_offset = (text_offset - exidx_offset)
19497
0
          & 0x7ffffffful;
19498
0
      if (bfd_link_relocatable (link_info))
19499
0
        {
19500
          /* Here relocation for new EXIDX_CANTUNWIND is
19501
             created, so there is no need to
19502
             adjust offset by hand.  */
19503
0
          prel31_offset = text_sec->output_offset
19504
0
              + text_sec->size;
19505
0
        }
19506
19507
      /* First address we can't unwind.  */
19508
0
      bfd_put_32 (output_bfd, prel31_offset,
19509
0
            &edited_contents[out_index * 8]);
19510
19511
      /* Code for EXIDX_CANTUNWIND.  */
19512
0
      bfd_put_32 (output_bfd, 0x1,
19513
0
            &edited_contents[out_index * 8 + 4]);
19514
19515
0
      out_index++;
19516
0
      add_to_offsets -= 8;
19517
0
          }
19518
0
          break;
19519
0
        }
19520
19521
0
      edit_node = edit_node->next;
19522
0
    }
19523
0
      }
19524
0
    else
19525
0
      {
19526
        /* No more edits, copy remaining entries verbatim.  */
19527
0
        copy_exidx_entry (output_bfd, edited_contents + out_index * 8,
19528
0
        contents + in_index * 8, add_to_offsets);
19529
0
        out_index++;
19530
0
        in_index++;
19531
0
      }
19532
0
  }
19533
19534
0
      if (!(sec->flags & SEC_EXCLUDE) && !(sec->flags & SEC_NEVER_LOAD))
19535
0
  bfd_set_section_contents (output_bfd, sec->output_section,
19536
0
          edited_contents,
19537
0
          (file_ptr) sec->output_offset, sec->size);
19538
19539
0
      return true;
19540
0
    }
19541
19542
  /* Fix code to point to Cortex-A8 erratum stubs.  */
19543
0
  if (globals->fix_cortex_a8)
19544
0
    {
19545
0
      struct a8_branch_to_stub_data data;
19546
19547
0
      data.writing_section = sec;
19548
0
      data.contents = contents;
19549
19550
0
      bfd_hash_traverse (& globals->stub_hash_table, make_branch_to_a8_stub,
19551
0
       & data);
19552
0
    }
19553
19554
0
  if (mapcount == 0)
19555
0
    return false;
19556
19557
0
  if (globals->byteswap_code)
19558
0
    {
19559
0
      qsort (map, mapcount, sizeof (* map), elf32_arm_compare_mapping);
19560
19561
0
      ptr = map[0].vma;
19562
0
      for (i = 0; i < mapcount; i++)
19563
0
  {
19564
0
    if (i == mapcount - 1)
19565
0
      end = sec->size;
19566
0
    else
19567
0
      end = map[i + 1].vma;
19568
19569
0
    switch (map[i].type)
19570
0
      {
19571
0
      case 'a':
19572
        /* Byte swap code words.  */
19573
0
        while (ptr + 3 < end)
19574
0
    {
19575
0
      tmp = contents[ptr];
19576
0
      contents[ptr] = contents[ptr + 3];
19577
0
      contents[ptr + 3] = tmp;
19578
0
      tmp = contents[ptr + 1];
19579
0
      contents[ptr + 1] = contents[ptr + 2];
19580
0
      contents[ptr + 2] = tmp;
19581
0
      ptr += 4;
19582
0
    }
19583
0
        break;
19584
19585
0
      case 't':
19586
        /* Byte swap code halfwords.  */
19587
0
        while (ptr + 1 < end)
19588
0
    {
19589
0
      tmp = contents[ptr];
19590
0
      contents[ptr] = contents[ptr + 1];
19591
0
      contents[ptr + 1] = tmp;
19592
0
      ptr += 2;
19593
0
    }
19594
0
        break;
19595
19596
0
      case 'd':
19597
        /* Leave data alone.  */
19598
0
        break;
19599
0
      }
19600
0
    ptr = end;
19601
0
  }
19602
0
    }
19603
19604
0
  free (map);
19605
0
  arm_data->mapcount = -1;
19606
0
  arm_data->mapsize = 0;
19607
0
  arm_data->map = NULL;
19608
19609
0
  return false;
19610
0
}
19611
19612
/* Mangle thumb function symbols as we read them in.  */
19613
19614
static bool
19615
elf32_arm_swap_symbol_in (bfd * abfd,
19616
        const void *psrc,
19617
        const void *pshn,
19618
        Elf_Internal_Sym *dst)
19619
943
{
19620
943
  if (!bfd_elf32_swap_symbol_in (abfd, psrc, pshn, dst))
19621
6
    return false;
19622
937
  dst->st_target_internal = ST_BRANCH_TO_ARM;
19623
19624
  /* New EABI objects mark thumb function symbols by setting the low bit of
19625
     the address.  */
19626
937
  if (ELF_ST_TYPE (dst->st_info) == STT_FUNC
19627
676
      || ELF_ST_TYPE (dst->st_info) == STT_GNU_IFUNC)
19628
352
    {
19629
352
      if (dst->st_value & 1)
19630
24
  {
19631
24
    dst->st_value &= ~(bfd_vma) 1;
19632
24
    ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal,
19633
24
           ST_BRANCH_TO_THUMB);
19634
24
  }
19635
328
      else
19636
328
  ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal, ST_BRANCH_TO_ARM);
19637
352
    }
19638
585
  else if (ELF_ST_TYPE (dst->st_info) == STT_ARM_TFUNC)
19639
48
    {
19640
48
      dst->st_info = ELF_ST_INFO (ELF_ST_BIND (dst->st_info), STT_FUNC);
19641
48
      ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal, ST_BRANCH_TO_THUMB);
19642
48
    }
19643
537
  else if (ELF_ST_TYPE (dst->st_info) == STT_SECTION)
19644
80
    ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal, ST_BRANCH_LONG);
19645
457
  else
19646
457
    ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal, ST_BRANCH_UNKNOWN);
19647
19648
937
  return true;
19649
943
}
19650
19651
19652
/* Mangle thumb function symbols as we write them out.  */
19653
19654
static void
19655
elf32_arm_swap_symbol_out (bfd *abfd,
19656
         const Elf_Internal_Sym *src,
19657
         void *cdst,
19658
         void *shndx)
19659
21
{
19660
21
  Elf_Internal_Sym newsym;
19661
19662
  /* We convert STT_ARM_TFUNC symbols into STT_FUNC with the low bit
19663
     of the address set, as per the new EABI.  We do this unconditionally
19664
     because objcopy does not set the elf header flags until after
19665
     it writes out the symbol table.  */
19666
21
  if (ARM_GET_SYM_BRANCH_TYPE (src->st_target_internal) == ST_BRANCH_TO_THUMB)
19667
2
    {
19668
2
      newsym = *src;
19669
2
      if (ELF_ST_TYPE (src->st_info) != STT_GNU_IFUNC)
19670
2
  newsym.st_info = ELF_ST_INFO (ELF_ST_BIND (src->st_info), STT_FUNC);
19671
2
      if (newsym.st_shndx != SHN_UNDEF)
19672
1
  {
19673
    /* Do this only for defined symbols. At link type, the static
19674
       linker will simulate the work of dynamic linker of resolving
19675
       symbols and will carry over the thumbness of found symbols to
19676
       the output symbol table. It's not clear how it happens, but
19677
       the thumbness of undefined symbols can well be different at
19678
       runtime, and writing '1' for them will be confusing for users
19679
       and possibly for dynamic linker itself.
19680
    */
19681
1
    newsym.st_value |= 1;
19682
1
  }
19683
19684
2
      src = &newsym;
19685
2
    }
19686
21
  bfd_elf32_swap_symbol_out (abfd, src, cdst, shndx);
19687
21
}
19688
19689
/* Add the PT_ARM_EXIDX program header.  */
19690
19691
static bool
19692
elf32_arm_modify_segment_map (bfd *abfd,
19693
            struct bfd_link_info *info ATTRIBUTE_UNUSED)
19694
15
{
19695
15
  struct elf_segment_map *m;
19696
15
  asection *sec;
19697
19698
15
  sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
19699
15
  if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
19700
0
    {
19701
      /* If there is already a PT_ARM_EXIDX header, then we do not
19702
   want to add another one.  This situation arises when running
19703
   "strip"; the input binary already has the header.  */
19704
0
      m = elf_seg_map (abfd);
19705
0
      while (m && m->p_type != PT_ARM_EXIDX)
19706
0
  m = m->next;
19707
0
      if (!m)
19708
0
  {
19709
0
    m = (struct elf_segment_map *)
19710
0
        bfd_zalloc (abfd, sizeof (struct elf_segment_map));
19711
0
    if (m == NULL)
19712
0
      return false;
19713
0
    m->p_type = PT_ARM_EXIDX;
19714
0
    m->count = 1;
19715
0
    m->sections[0] = sec;
19716
19717
0
    m->next = elf_seg_map (abfd);
19718
0
    elf_seg_map (abfd) = m;
19719
0
  }
19720
0
    }
19721
19722
15
  return true;
19723
15
}
19724
19725
/* We may add a PT_ARM_EXIDX program header.  */
19726
19727
static int
19728
elf32_arm_additional_program_headers (bfd *abfd,
19729
              struct bfd_link_info *info ATTRIBUTE_UNUSED)
19730
7
{
19731
7
  asection *sec;
19732
19733
7
  sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
19734
7
  if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
19735
0
    return 1;
19736
7
  else
19737
7
    return 0;
19738
7
}
19739
19740
/* Hook called by the linker routine which adds symbols from an object
19741
   file.  */
19742
19743
static bool
19744
elf32_arm_add_symbol_hook (bfd *abfd ATTRIBUTE_UNUSED,
19745
         struct bfd_link_info *info,
19746
         Elf_Internal_Sym *sym ATTRIBUTE_UNUSED,
19747
         const char **namep ATTRIBUTE_UNUSED,
19748
         flagword *flagsp ATTRIBUTE_UNUSED,
19749
         asection **secp ATTRIBUTE_UNUSED,
19750
         bfd_vma *valp ATTRIBUTE_UNUSED)
19751
0
{
19752
0
  if (elf32_arm_hash_table (info) == NULL)
19753
0
    return false;
19754
19755
0
#ifdef OBJ_MAYBE_ELF_VXWORKS
19756
0
  if (elf32_arm_hash_table (info)->root.target_os == is_vxworks
19757
0
      && !elf_vxworks_add_symbol_hook (abfd, info, sym, namep,
19758
0
               flagsp, secp, valp))
19759
0
    return false;
19760
0
#endif /* OBJ_MAYBE_ELF_VXWORKS */
19761
19762
0
  return true;
19763
0
}
19764
19765
/* We use this to override swap_symbol_in and swap_symbol_out.  */
19766
static const struct elf_size_info elf32_arm_size_info =
19767
{
19768
  sizeof (Elf32_External_Ehdr),
19769
  sizeof (Elf32_External_Phdr),
19770
  sizeof (Elf32_External_Shdr),
19771
  sizeof (Elf32_External_Rel),
19772
  sizeof (Elf32_External_Rela),
19773
  sizeof (Elf32_External_Sym),
19774
  sizeof (Elf32_External_Dyn),
19775
  sizeof (Elf_External_Note),
19776
  4,
19777
  1,
19778
  32, 2,
19779
  ELFCLASS32, EV_CURRENT,
19780
  bfd_elf32_write_out_phdrs,
19781
  bfd_elf32_write_shdrs_and_ehdr,
19782
  bfd_elf32_checksum_contents,
19783
  bfd_elf32_write_relocs,
19784
  elf32_arm_swap_symbol_in,
19785
  elf32_arm_swap_symbol_out,
19786
  bfd_elf32_slurp_reloc_table,
19787
  bfd_elf32_slurp_symbol_table,
19788
  bfd_elf32_swap_dyn_in,
19789
  bfd_elf32_swap_dyn_out,
19790
  bfd_elf32_swap_reloc_in,
19791
  bfd_elf32_swap_reloc_out,
19792
  bfd_elf32_swap_reloca_in,
19793
  bfd_elf32_swap_reloca_out
19794
};
19795
19796
static bfd_vma
19797
read_code32 (const bfd *abfd, const bfd_byte *addr)
19798
0
{
19799
  /* V7 BE8 code is always little endian.  */
19800
0
  if ((elf_elfheader (abfd)->e_flags & EF_ARM_BE8) != 0)
19801
0
    return bfd_getl32 (addr);
19802
19803
0
  return bfd_get_32 (abfd, addr);
19804
0
}
19805
19806
static bfd_vma
19807
read_code16 (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_getl16 (addr);
19812
19813
0
  return bfd_get_16 (abfd, addr);
19814
0
}
19815
19816
/* Return size of plt0 entry starting at ADDR
19817
   or (bfd_vma) -1 if size can not be determined.  */
19818
19819
static bfd_vma
19820
elf32_arm_plt0_size (const bfd *abfd, const bfd_byte *addr,
19821
         bfd_size_type data_size)
19822
0
{
19823
0
  bfd_vma first_word;
19824
0
  bfd_vma plt0_size;
19825
19826
0
  if (data_size < 4)
19827
0
    return (bfd_vma) -1;
19828
19829
0
  first_word = read_code32 (abfd, addr);
19830
19831
0
  if (first_word == elf32_arm_plt0_entry[0])
19832
0
    plt0_size = 4 * ARRAY_SIZE (elf32_arm_plt0_entry);
19833
0
  else if (first_word == elf32_thumb2_plt0_entry[0])
19834
0
    plt0_size = 4 * ARRAY_SIZE (elf32_thumb2_plt0_entry);
19835
0
  else
19836
    /* We don't yet handle this PLT format.  */
19837
0
    return (bfd_vma) -1;
19838
19839
0
  return plt0_size;
19840
0
}
19841
19842
/* Return size of plt entry starting at offset OFFSET
19843
   of plt section located at address START
19844
   or (bfd_vma) -1 if size can not be determined.  */
19845
19846
static bfd_vma
19847
elf32_arm_plt_size (const bfd *abfd, const bfd_byte *start, bfd_vma offset,
19848
        bfd_size_type data_size)
19849
0
{
19850
0
  bfd_vma first_insn;
19851
0
  bfd_vma plt_size = 0;
19852
19853
  /* PLT entry size if fixed on Thumb-only platforms.  */
19854
0
  if (read_code32 (abfd, start) == elf32_thumb2_plt0_entry[0])
19855
0
    return 4 * ARRAY_SIZE (elf32_thumb2_plt_entry);
19856
19857
  /* Respect Thumb stub if necessary.  */
19858
0
  if (offset + 2 > data_size)
19859
0
    return (bfd_vma) -1;
19860
0
  if (read_code16 (abfd, start + offset) == elf32_arm_plt_thumb_stub[0])
19861
0
    {
19862
0
      plt_size += 2 * ARRAY_SIZE (elf32_arm_plt_thumb_stub);
19863
0
    }
19864
19865
  /* Strip immediate from first add.  */
19866
0
  if (offset + plt_size + 4 > data_size)
19867
0
    return (bfd_vma) -1;
19868
0
  first_insn = read_code32 (abfd, start + offset + plt_size) & 0xffffff00;
19869
19870
#ifdef FOUR_WORD_PLT
19871
  if (first_insn == elf32_arm_plt_entry[0])
19872
    plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry);
19873
#else
19874
0
  if (first_insn == elf32_arm_plt_entry_long[0])
19875
0
    plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry_long);
19876
0
  else if (first_insn == elf32_arm_plt_entry_short[0])
19877
0
    plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry_short);
19878
0
#endif
19879
0
  else
19880
    /* We don't yet handle this PLT format.  */
19881
0
    return (bfd_vma) -1;
19882
19883
0
  return plt_size;
19884
0
}
19885
19886
/* Implementation is shamelessly borrowed from _bfd_elf_get_synthetic_symtab.  */
19887
19888
static long
19889
elf32_arm_get_synthetic_symtab (bfd *abfd,
19890
             long symcount ATTRIBUTE_UNUSED,
19891
             asymbol **syms ATTRIBUTE_UNUSED,
19892
             long dynsymcount,
19893
             asymbol **dynsyms,
19894
             asymbol **ret)
19895
522
{
19896
522
  asection *relplt;
19897
522
  asymbol *s;
19898
522
  arelent *p;
19899
522
  long count, i, n;
19900
522
  size_t size;
19901
522
  Elf_Internal_Shdr *hdr;
19902
522
  char *names;
19903
522
  asection *plt;
19904
522
  bfd_vma offset;
19905
522
  bfd_byte *data;
19906
19907
522
  *ret = NULL;
19908
19909
522
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
19910
522
    return 0;
19911
19912
0
  if (dynsymcount <= 0)
19913
0
    return 0;
19914
19915
0
  relplt = bfd_get_section_by_name (abfd, ".rel.plt");
19916
0
  if (relplt == NULL)
19917
0
    return 0;
19918
19919
0
  hdr = &elf_section_data (relplt)->this_hdr;
19920
0
  if (hdr->sh_link != elf_dynsymtab (abfd)
19921
0
      || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
19922
0
    return 0;
19923
19924
0
  plt = bfd_get_section_by_name (abfd, ".plt");
19925
0
  if (plt == NULL)
19926
0
    return 0;
19927
19928
0
  if (!elf32_arm_size_info.slurp_reloc_table (abfd, relplt, dynsyms, true))
19929
0
    return -1;
19930
19931
0
  data = NULL;
19932
0
  if (!bfd_get_full_section_contents (abfd, plt, &data))
19933
0
    return -1;
19934
19935
0
  count = NUM_SHDR_ENTRIES (hdr);
19936
0
  size = count * sizeof (asymbol);
19937
0
  p = relplt->relocation;
19938
0
  for (i = 0; i < count; i++, p += elf32_arm_size_info.int_rels_per_ext_rel)
19939
0
    {
19940
0
      size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
19941
0
      if (p->addend != 0)
19942
0
  size += sizeof ("+0x") - 1 + 8;
19943
0
    }
19944
19945
0
  offset = elf32_arm_plt0_size (abfd, data, plt->size);
19946
0
  if (offset == (bfd_vma) -1
19947
0
      || (s = *ret = (asymbol *) bfd_malloc (size)) == NULL)
19948
0
    {
19949
0
      free (data);
19950
0
      return -1;
19951
0
    }
19952
19953
0
  names = (char *) (s + count);
19954
0
  p = relplt->relocation;
19955
0
  n = 0;
19956
0
  for (i = 0; i < count; i++, p += elf32_arm_size_info.int_rels_per_ext_rel)
19957
0
    {
19958
0
      size_t len;
19959
19960
0
      bfd_vma plt_size = elf32_arm_plt_size (abfd, data, offset, plt->size);
19961
0
      if (plt_size == (bfd_vma) -1)
19962
0
  break;
19963
19964
0
      *s = **p->sym_ptr_ptr;
19965
      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
19966
   we are defining a symbol, ensure one of them is set.  */
19967
0
      if ((s->flags & BSF_LOCAL) == 0)
19968
0
  s->flags |= BSF_GLOBAL;
19969
0
      s->flags |= BSF_SYNTHETIC;
19970
0
      s->section = plt;
19971
0
      s->value = offset;
19972
0
      s->name = names;
19973
0
      s->udata.p = NULL;
19974
0
      len = strlen ((*p->sym_ptr_ptr)->name);
19975
0
      memcpy (names, (*p->sym_ptr_ptr)->name, len);
19976
0
      names += len;
19977
0
      if (p->addend != 0)
19978
0
  {
19979
0
    char buf[30], *a;
19980
19981
0
    memcpy (names, "+0x", sizeof ("+0x") - 1);
19982
0
    names += sizeof ("+0x") - 1;
19983
0
    bfd_sprintf_vma (abfd, buf, p->addend);
19984
0
    for (a = buf; *a == '0'; ++a)
19985
0
      ;
19986
0
    len = strlen (a);
19987
0
    memcpy (names, a, len);
19988
0
    names += len;
19989
0
  }
19990
0
      memcpy (names, "@plt", sizeof ("@plt"));
19991
0
      names += sizeof ("@plt");
19992
0
      ++s, ++n;
19993
0
      offset += plt_size;
19994
0
    }
19995
19996
0
  free (data);
19997
0
  return n;
19998
0
}
19999
20000
static bool
20001
elf32_arm_section_flags (const Elf_Internal_Shdr *hdr)
20002
3.37k
{
20003
3.37k
  if (hdr->sh_flags & SHF_ARM_PURECODE)
20004
223
    hdr->bfd_section->flags |= SEC_ELF_PURECODE;
20005
3.37k
  return true;
20006
3.37k
}
20007
20008
static flagword
20009
elf32_arm_lookup_section_flags (char *flag_name)
20010
0
{
20011
0
  if (!strcmp (flag_name, "SHF_ARM_PURECODE"))
20012
0
    return SHF_ARM_PURECODE;
20013
20014
0
  return SEC_NO_FLAGS;
20015
0
}
20016
20017
static unsigned int
20018
elf32_arm_count_additional_relocs (asection *sec)
20019
0
{
20020
0
  struct _arm_elf_section_data *arm_data;
20021
0
  arm_data = get_arm_elf_section_data (sec);
20022
20023
0
  return arm_data == NULL ? 0 : arm_data->additional_reloc_count;
20024
0
}
20025
20026
/* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which
20027
   has a type >= SHT_LOOS.  Returns TRUE if these fields were initialised
20028
   FALSE otherwise.  ISECTION is the best guess matching section from the
20029
   input bfd IBFD, but it might be NULL.  */
20030
20031
static bool
20032
elf32_arm_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
20033
               bfd *obfd ATTRIBUTE_UNUSED,
20034
               const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
20035
               Elf_Internal_Shdr *osection)
20036
66
{
20037
66
  switch (osection->sh_type)
20038
66
    {
20039
0
    case SHT_ARM_EXIDX:
20040
0
      {
20041
0
  Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
20042
0
  Elf_Internal_Shdr **iheaders = elf_elfsections (ibfd);
20043
0
  unsigned i = 0;
20044
20045
0
  osection->sh_flags = SHF_ALLOC | SHF_LINK_ORDER;
20046
0
  osection->sh_info = 0;
20047
20048
  /* The sh_link field must be set to the text section associated with
20049
     this index section.  Unfortunately the ARM EHABI does not specify
20050
     exactly how to determine this association.  Our caller does try
20051
     to match up OSECTION with its corresponding input section however
20052
     so that is a good first guess.  */
20053
0
  if (isection != NULL
20054
0
      && osection->bfd_section != NULL
20055
0
      && isection->bfd_section != NULL
20056
0
      && isection->bfd_section->output_section != NULL
20057
0
      && isection->bfd_section->output_section == osection->bfd_section
20058
0
      && iheaders != NULL
20059
0
      && isection->sh_link > 0
20060
0
      && isection->sh_link < elf_numsections (ibfd)
20061
0
      && iheaders[isection->sh_link]->bfd_section != NULL
20062
0
      && iheaders[isection->sh_link]->bfd_section->output_section != NULL
20063
0
      )
20064
0
    {
20065
0
      for (i = elf_numsections (obfd); --i != 0;)
20066
0
        if (oheaders[i]->bfd_section
20067
0
      == iheaders[isection->sh_link]->bfd_section->output_section)
20068
0
    break;
20069
0
    }
20070
20071
0
  if (i == 0)
20072
0
    {
20073
      /* Failing that we have to find a matching section ourselves.  If
20074
         we had the output section name available we could compare that
20075
         with input section names.  Unfortunately we don't.  So instead
20076
         we use a simple heuristic and look for the nearest executable
20077
         section before this one.  */
20078
0
      for (i = elf_numsections (obfd); --i != 0;)
20079
0
        if (oheaders[i] == osection)
20080
0
    break;
20081
0
      if (i == 0)
20082
0
        break;
20083
20084
0
      while (--i != 0)
20085
0
        if (oheaders[i]->sh_type == SHT_PROGBITS
20086
0
      && ((oheaders[i]->sh_flags & (SHF_ALLOC | SHF_EXECINSTR))
20087
0
          == (SHF_ALLOC | SHF_EXECINSTR)))
20088
0
    break;
20089
0
    }
20090
20091
0
  if (i)
20092
0
    {
20093
0
      osection->sh_link = i;
20094
      /* If the text section was part of a group
20095
         then the index section should be too.  */
20096
0
      if (oheaders[i]->sh_flags & SHF_GROUP)
20097
0
        osection->sh_flags |= SHF_GROUP;
20098
0
      return true;
20099
0
    }
20100
0
      }
20101
0
      break;
20102
20103
0
    case SHT_ARM_PREEMPTMAP:
20104
0
      osection->sh_flags = SHF_ALLOC;
20105
0
      break;
20106
20107
0
    case SHT_ARM_ATTRIBUTES:
20108
0
    case SHT_ARM_DEBUGOVERLAY:
20109
0
    case SHT_ARM_OVERLAYSECTION:
20110
66
    default:
20111
66
      break;
20112
66
    }
20113
20114
66
  return false;
20115
66
}
20116
20117
/* Returns TRUE if NAME is an ARM mapping symbol.
20118
   Traditionally the symbols $a, $d and $t have been used.
20119
   The ARM ELF standard also defines $x (for A64 code).  It also allows a
20120
   period initiated suffix to be added to the symbol: "$[adtx]\.[:sym_char]+".
20121
   Other tools might also produce $b (Thumb BL), $f, $p, $m and $v, but we do
20122
   not support them here.  $t.x indicates the start of ThumbEE instructions.  */
20123
20124
static bool
20125
is_arm_mapping_symbol (const char * name)
20126
400
{
20127
400
  return name != NULL /* Paranoia.  */
20128
400
    && name[0] == '$' /* Note: if objcopy --prefix-symbols has been used then
20129
       the mapping symbols could have acquired a prefix.
20130
       We do not support this here, since such symbols no
20131
       longer conform to the ARM ELF ABI.  */
20132
6
    && (name[1] == 'a' || name[1] == 'd' || name[1] == 't' || name[1] == 'x')
20133
4
    && (name[2] == 0 || name[2] == '.');
20134
  /* FIXME: Strictly speaking the symbol is only a valid mapping symbol if
20135
     any characters that follow the period are legal characters for the body
20136
     of a symbol's name.  For now we just assume that this is the case.  */
20137
400
}
20138
20139
/* Make sure that mapping symbols in object files are not removed via the
20140
   "strip --strip-unneeded" tool.  These symbols are needed in order to
20141
   correctly generate interworking veneers, and for byte swapping code
20142
   regions.  Once an object file has been linked, it is safe to remove the
20143
   symbols as they will no longer be needed.  */
20144
20145
static void
20146
elf32_arm_backend_symbol_processing (bfd *abfd, asymbol *sym)
20147
700
{
20148
700
  if (((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
20149
618
      && sym->section != bfd_abs_section_ptr
20150
400
      && is_arm_mapping_symbol (sym->name))
20151
0
    sym->flags |= BSF_KEEP;
20152
700
}
20153
20154
#undef  elf_backend_copy_special_section_fields
20155
#define elf_backend_copy_special_section_fields elf32_arm_copy_special_section_fields
20156
20157
#define ELF_ARCH      bfd_arch_arm
20158
#define ELF_TARGET_ID     ARM_ELF_DATA
20159
#define ELF_MACHINE_CODE    EM_ARM
20160
#define ELF_MAXPAGESIZE     0x1000
20161
#define ELF_COMMONPAGESIZE    0x1000
20162
20163
#define bfd_elf32_mkobject      elf32_arm_mkobject
20164
20165
#define bfd_elf32_bfd_copy_private_bfd_data elf32_arm_copy_private_bfd_data
20166
#define bfd_elf32_bfd_merge_private_bfd_data  elf32_arm_merge_private_bfd_data
20167
#define bfd_elf32_bfd_set_private_flags   elf32_arm_set_private_flags
20168
#define bfd_elf32_bfd_print_private_bfd_data  elf32_arm_print_private_bfd_data
20169
#define bfd_elf32_bfd_link_hash_table_create  elf32_arm_link_hash_table_create
20170
#define bfd_elf32_bfd_reloc_type_lookup   elf32_arm_reloc_type_lookup
20171
#define bfd_elf32_bfd_reloc_name_lookup   elf32_arm_reloc_name_lookup
20172
#define bfd_elf32_find_inliner_info   elf32_arm_find_inliner_info
20173
#define bfd_elf32_new_section_hook    elf32_arm_new_section_hook
20174
#define bfd_elf32_bfd_is_target_special_symbol  elf32_arm_is_target_special_symbol
20175
#define bfd_elf32_bfd_final_link    elf32_arm_final_link
20176
#define bfd_elf32_get_synthetic_symtab  elf32_arm_get_synthetic_symtab
20177
20178
#define elf_backend_get_symbol_type   elf32_arm_get_symbol_type
20179
#define elf_backend_maybe_function_sym    elf32_arm_maybe_function_sym
20180
#define elf_backend_gc_mark_hook    elf32_arm_gc_mark_hook
20181
#define elf_backend_gc_mark_extra_sections  elf32_arm_gc_mark_extra_sections
20182
#define elf_backend_check_relocs    elf32_arm_check_relocs
20183
#define elf_backend_update_relocs   elf32_arm_update_relocs
20184
#define elf_backend_relocate_section    elf32_arm_relocate_section
20185
#define elf_backend_write_section   elf32_arm_write_section
20186
#define elf_backend_adjust_dynamic_symbol elf32_arm_adjust_dynamic_symbol
20187
#define elf_backend_create_dynamic_sections elf32_arm_create_dynamic_sections
20188
#define elf_backend_finish_dynamic_symbol elf32_arm_finish_dynamic_symbol
20189
#define elf_backend_finish_dynamic_sections elf32_arm_finish_dynamic_sections
20190
#define elf_backend_late_size_sections    elf32_arm_late_size_sections
20191
#define elf_backend_early_size_sections   elf32_arm_early_size_sections
20192
#define elf_backend_init_index_section    _bfd_elf_init_2_index_sections
20193
#define elf_backend_init_file_header    elf32_arm_init_file_header
20194
#define elf_backend_reloc_type_class    elf32_arm_reloc_type_class
20195
#define elf_backend_object_p      elf32_arm_object_p
20196
#define elf_backend_fake_sections   elf32_arm_fake_sections
20197
#define elf_backend_section_from_shdr   elf32_arm_section_from_shdr
20198
#define elf_backend_final_write_processing  elf32_arm_final_write_processing
20199
#define elf_backend_copy_indirect_symbol  elf32_arm_copy_indirect_symbol
20200
#define elf_backend_size_info     elf32_arm_size_info
20201
#define elf_backend_modify_segment_map    elf32_arm_modify_segment_map
20202
#define elf_backend_additional_program_headers  elf32_arm_additional_program_headers
20203
#define elf_backend_output_arch_local_syms  elf32_arm_output_arch_local_syms
20204
#define elf_backend_filter_implib_symbols elf32_arm_filter_implib_symbols
20205
#define elf_backend_begin_write_processing  elf32_arm_begin_write_processing
20206
#define elf_backend_add_symbol_hook   elf32_arm_add_symbol_hook
20207
#define elf_backend_count_additional_relocs elf32_arm_count_additional_relocs
20208
#define elf_backend_symbol_processing   elf32_arm_backend_symbol_processing
20209
20210
#define elf_backend_can_refcount       1
20211
#define elf_backend_can_gc_sections    1
20212
#define elf_backend_plt_readonly       1
20213
#define elf_backend_want_got_plt       1
20214
#define elf_backend_want_plt_sym       0
20215
#define elf_backend_want_dynrelro      1
20216
#define elf_backend_may_use_rel_p      1
20217
#define elf_backend_may_use_rela_p     0
20218
#define elf_backend_default_use_rela_p 0
20219
#define elf_backend_dtrel_excludes_plt 1
20220
20221
#define elf_backend_got_header_size 12
20222
#define elf_backend_extern_protected_data 0
20223
20224
#undef  elf_backend_obj_attrs_vendor
20225
#define elf_backend_obj_attrs_vendor    "aeabi"
20226
#undef  elf_backend_obj_attrs_section
20227
#define elf_backend_obj_attrs_section   ".ARM.attributes"
20228
#undef  elf_backend_obj_attrs_arg_type
20229
#define elf_backend_obj_attrs_arg_type    elf32_arm_obj_attrs_arg_type
20230
#undef  elf_backend_obj_attrs_section_type
20231
#define elf_backend_obj_attrs_section_type  SHT_ARM_ATTRIBUTES
20232
#define elf_backend_obj_attrs_order   elf32_arm_obj_attrs_order
20233
#define elf_backend_obj_attrs_handle_unknown  elf32_arm_obj_attrs_handle_unknown
20234
20235
#undef  elf_backend_section_flags
20236
#define elf_backend_section_flags   elf32_arm_section_flags
20237
#undef  elf_backend_lookup_section_flags_hook
20238
#define elf_backend_lookup_section_flags_hook elf32_arm_lookup_section_flags
20239
20240
#define elf_backend_linux_prpsinfo32_ugid16 true
20241
20242
#include "elf32-target.h"
20243
20244
/* Reset to defaults.  */
20245
#undef  elf_backend_plt_alignment
20246
#undef  elf_backend_modify_segment_map
20247
#define elf_backend_modify_segment_map    elf32_arm_modify_segment_map
20248
#undef  elf_backend_modify_headers
20249
#undef  elf_backend_final_write_processing
20250
#define elf_backend_final_write_processing  elf32_arm_final_write_processing
20251
#undef  ELF_MINPAGESIZE
20252
#undef  ELF_COMMONPAGESIZE
20253
#define ELF_COMMONPAGESIZE    0x1000
20254
20255
20256
/* FDPIC Targets.  */
20257
20258
#undef  TARGET_LITTLE_SYM
20259
#define TARGET_LITTLE_SYM   arm_elf32_fdpic_le_vec
20260
#undef  TARGET_LITTLE_NAME
20261
#define TARGET_LITTLE_NAME    "elf32-littlearm-fdpic"
20262
#undef  TARGET_BIG_SYM
20263
#define TARGET_BIG_SYM      arm_elf32_fdpic_be_vec
20264
#undef  TARGET_BIG_NAME
20265
#define TARGET_BIG_NAME     "elf32-bigarm-fdpic"
20266
#undef elf_match_priority
20267
#define elf_match_priority    128
20268
#undef ELF_OSABI
20269
#define ELF_OSABI   ELFOSABI_ARM_FDPIC
20270
#undef ELF_OSABI_EXACT
20271
#define ELF_OSABI_EXACT   1
20272
20273
/* Like elf32_arm_link_hash_table_create -- but overrides
20274
   appropriately for FDPIC.  */
20275
20276
static struct bfd_link_hash_table *
20277
elf32_arm_fdpic_link_hash_table_create (bfd *abfd)
20278
0
{
20279
0
  struct bfd_link_hash_table *ret;
20280
20281
0
  ret = elf32_arm_link_hash_table_create (abfd);
20282
0
  if (ret)
20283
0
    {
20284
0
      struct elf32_arm_link_hash_table *htab = (struct elf32_arm_link_hash_table *) ret;
20285
20286
0
      htab->fdpic_p = 1;
20287
0
    }
20288
0
  return ret;
20289
0
}
20290
20291
/* We need dynamic symbols for every section, since segments can
20292
   relocate independently.  */
20293
static bool
20294
elf32_arm_fdpic_omit_section_dynsym (struct bfd_link_info *info
20295
             ATTRIBUTE_UNUSED,
20296
             asection *p ATTRIBUTE_UNUSED)
20297
0
{
20298
0
  switch (elf_section_data (p)->this_hdr.sh_type)
20299
0
    {
20300
0
    case SHT_PROGBITS:
20301
0
    case SHT_NOBITS:
20302
      /* If sh_type is yet undecided, assume it could be
20303
   SHT_PROGBITS/SHT_NOBITS.  */
20304
0
    case SHT_NULL:
20305
0
      return false;
20306
20307
      /* There shouldn't be section relative relocations
20308
   against any other section.  */
20309
0
    default:
20310
0
      return true;
20311
0
    }
20312
0
}
20313
20314
#undef  elf32_bed
20315
#define elf32_bed       elf32_arm_fdpic_bed
20316
20317
#undef  bfd_elf32_bfd_link_hash_table_create
20318
#define bfd_elf32_bfd_link_hash_table_create  elf32_arm_fdpic_link_hash_table_create
20319
20320
#undef elf_backend_omit_section_dynsym
20321
#define elf_backend_omit_section_dynsym   elf32_arm_fdpic_omit_section_dynsym
20322
20323
#undef ELF_TARGET_OS
20324
20325
#include "elf32-target.h"
20326
20327
#undef elf_match_priority
20328
#undef ELF_OSABI
20329
#undef ELF_OSABI_EXACT
20330
#undef elf_backend_omit_section_dynsym
20331
20332
#ifdef OBJ_MAYBE_ELF_VXWORKS
20333
20334
/* VxWorks Targets.  */
20335
20336
#undef  TARGET_LITTLE_SYM
20337
#define TARGET_LITTLE_SYM   arm_elf32_vxworks_le_vec
20338
#undef  TARGET_LITTLE_NAME
20339
#define TARGET_LITTLE_NAME    "elf32-littlearm-vxworks"
20340
#undef  TARGET_BIG_SYM
20341
#define TARGET_BIG_SYM      arm_elf32_vxworks_be_vec
20342
#undef  TARGET_BIG_NAME
20343
#define TARGET_BIG_NAME     "elf32-bigarm-vxworks"
20344
20345
/* Like elf32_arm_link_hash_table_create -- but overrides
20346
   appropriately for VxWorks.  */
20347
20348
static struct bfd_link_hash_table *
20349
elf32_arm_vxworks_link_hash_table_create (bfd *abfd)
20350
0
{
20351
0
  struct bfd_link_hash_table *ret;
20352
20353
0
  ret = elf32_arm_link_hash_table_create (abfd);
20354
0
  if (ret)
20355
0
    {
20356
0
      struct elf32_arm_link_hash_table *htab
20357
0
  = (struct elf32_arm_link_hash_table *) ret;
20358
0
      htab->use_rel = 0;
20359
0
    }
20360
0
  return ret;
20361
0
}
20362
20363
static bool
20364
elf32_arm_vxworks_final_write_processing (bfd *abfd)
20365
0
{
20366
0
  arm_final_write_processing (abfd);
20367
0
  return elf_vxworks_final_write_processing (abfd);
20368
0
}
20369
20370
#undef  elf32_bed
20371
#define elf32_bed elf32_arm_vxworks_bed
20372
20373
#undef  bfd_elf32_bfd_link_hash_table_create
20374
#define bfd_elf32_bfd_link_hash_table_create  elf32_arm_vxworks_link_hash_table_create
20375
#undef  elf_backend_final_write_processing
20376
#define elf_backend_final_write_processing  elf32_arm_vxworks_final_write_processing
20377
#undef  elf_backend_emit_relocs
20378
#define elf_backend_emit_relocs     elf_vxworks_emit_relocs
20379
20380
#undef  elf_backend_may_use_rel_p
20381
#define elf_backend_may_use_rel_p 0
20382
#undef  elf_backend_may_use_rela_p
20383
#define elf_backend_may_use_rela_p  1
20384
#undef  elf_backend_default_use_rela_p
20385
#define elf_backend_default_use_rela_p  1
20386
#undef  elf_backend_want_plt_sym
20387
#define elf_backend_want_plt_sym  1
20388
#undef  ELF_MAXPAGESIZE
20389
#define ELF_MAXPAGESIZE     0x1000
20390
#undef ELF_TARGET_OS
20391
#define ELF_TARGET_OS     is_vxworks
20392
20393
#include "elf32-target.h"
20394
20395
#endif /* OBJ_MAYBE_ELF_VXWORKS */
20396
20397
/* Merge backend specific data from an object file to the output
20398
   object file when linking.  */
20399
20400
static bool
20401
elf32_arm_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
20402
0
{
20403
0
  bfd *obfd = info->output_bfd;
20404
0
  flagword out_flags;
20405
0
  flagword in_flags;
20406
0
  bool flags_compatible = true;
20407
0
  asection *sec;
20408
20409
  /* Check if we have the same endianness.  */
20410
0
  if (! _bfd_generic_verify_endian_match (ibfd, info))
20411
0
    return false;
20412
20413
0
  if (! is_arm_elf (ibfd))
20414
0
    return true;
20415
20416
0
  if (!elf32_arm_merge_eabi_attributes (ibfd, info))
20417
0
    return false;
20418
20419
  /* The input BFD must have had its flags initialised.  */
20420
  /* The following seems bogus to me -- The flags are initialized in
20421
     the assembler but I don't think an elf_flags_init field is
20422
     written into the object.  */
20423
  /* BFD_ASSERT (elf_flags_init (ibfd)); */
20424
20425
0
  in_flags  = elf_elfheader (ibfd)->e_flags;
20426
0
  out_flags = elf_elfheader (obfd)->e_flags;
20427
20428
  /* In theory there is no reason why we couldn't handle this.  However
20429
     in practice it isn't even close to working and there is no real
20430
     reason to want it.  */
20431
0
  if (EF_ARM_EABI_VERSION (in_flags) >= EF_ARM_EABI_VER4
20432
0
      && !(ibfd->flags & DYNAMIC)
20433
0
      && (in_flags & EF_ARM_BE8))
20434
0
    {
20435
0
      _bfd_error_handler (_("error: %pB is already in final BE8 format"),
20436
0
        ibfd);
20437
0
      return false;
20438
0
    }
20439
20440
0
  if (!elf_flags_init (obfd))
20441
0
    {
20442
      /* If the input has no flags set, then do not set the output flags.
20443
   This will allow future bfds to determine the desired output flags.
20444
   If no input bfds have any flags set, then neither will the output bfd.
20445
20446
   Note - we used to restrict this test to when the input architecture
20447
   variant was the default variant, but this does not allow for
20448
   linker scripts which override the default.  See PR 28910 for an
20449
   example.  */
20450
0
      if (in_flags == 0)
20451
0
  return true;
20452
      
20453
0
      elf_flags_init (obfd) = true;
20454
0
      elf_elfheader (obfd)->e_flags = in_flags;
20455
20456
0
      if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
20457
0
    && bfd_get_arch_info (obfd)->the_default)
20458
0
  return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
20459
20460
0
      return true;
20461
0
    }
20462
20463
  /* Determine what should happen if the input ARM architecture
20464
     does not match the output ARM architecture.  */
20465
0
  if (! bfd_arm_merge_machines (ibfd, obfd))
20466
0
    return false;
20467
20468
  /* Identical flags must be compatible.  */
20469
0
  if (in_flags == out_flags)
20470
0
    return true;
20471
20472
  /* Check to see if the input BFD actually contains any sections.  If
20473
     not, its flags may not have been initialised either, but it
20474
     cannot actually cause any incompatiblity.  Do not short-circuit
20475
     dynamic objects; their section list may be emptied by
20476
    elf_link_add_object_symbols.
20477
20478
    Also check to see if there are no code sections in the input.
20479
    In this case there is no need to check for code specific flags.
20480
    XXX - do we need to worry about floating-point format compatability
20481
    in data sections ?  */
20482
0
  if (!(ibfd->flags & DYNAMIC))
20483
0
    {
20484
0
      bool null_input_bfd = true;
20485
0
      bool only_data_sections = true;
20486
20487
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
20488
0
  {
20489
    /* Ignore synthetic glue sections.  */
20490
0
    if (strcmp (sec->name, ".glue_7")
20491
0
        && strcmp (sec->name, ".glue_7t"))
20492
0
      {
20493
0
        if ((bfd_section_flags (sec)
20494
0
       & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
20495
0
      == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
20496
0
    only_data_sections = false;
20497
20498
0
        null_input_bfd = false;
20499
0
        break;
20500
0
      }
20501
0
  }
20502
20503
0
      if (null_input_bfd || only_data_sections)
20504
0
  return true;
20505
0
    }
20506
20507
  /* Complain about various flag mismatches.  */
20508
0
  if (!elf32_arm_versions_compatible (EF_ARM_EABI_VERSION (in_flags),
20509
0
              EF_ARM_EABI_VERSION (out_flags)))
20510
0
    {
20511
0
      _bfd_error_handler
20512
0
  (_("error: source object %pB has EABI version %d, but target %pB has EABI version %d"),
20513
0
   ibfd, (in_flags & EF_ARM_EABIMASK) >> 24,
20514
0
   obfd, (out_flags & EF_ARM_EABIMASK) >> 24);
20515
0
      return false;
20516
0
    }
20517
20518
  /* Not sure what needs to be checked for EABI versions >= 1.  */
20519
0
  if (EF_ARM_EABI_VERSION (in_flags) == EF_ARM_EABI_UNKNOWN
20520
0
#ifdef OBJ_MAYBE_ELF_VXWORKS
20521
      /* VxWorks libraries do not use these flags.  */
20522
0
      && get_elf_backend_data (obfd) != &elf32_arm_vxworks_bed
20523
0
      && get_elf_backend_data (ibfd) != &elf32_arm_vxworks_bed
20524
0
#endif
20525
0
      )
20526
0
    {
20527
0
      if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
20528
0
  {
20529
0
    _bfd_error_handler
20530
0
      (_("error: %pB is compiled for APCS-%d, whereas target %pB uses APCS-%d"),
20531
0
       ibfd, in_flags & EF_ARM_APCS_26 ? 26 : 32,
20532
0
       obfd, out_flags & EF_ARM_APCS_26 ? 26 : 32);
20533
0
    flags_compatible = false;
20534
0
  }
20535
20536
0
      if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
20537
0
  {
20538
0
    if (in_flags & EF_ARM_APCS_FLOAT)
20539
0
      _bfd_error_handler
20540
0
        (_("error: %pB passes floats in float registers, whereas %pB passes them in integer registers"),
20541
0
         ibfd, obfd);
20542
0
    else
20543
0
      _bfd_error_handler
20544
0
        (_("error: %pB passes floats in integer registers, whereas %pB passes them in float registers"),
20545
0
         ibfd, obfd);
20546
20547
0
    flags_compatible = false;
20548
0
  }
20549
20550
0
      if ((in_flags & EF_ARM_VFP_FLOAT) != (out_flags & EF_ARM_VFP_FLOAT))
20551
0
  {
20552
0
    if (in_flags & EF_ARM_VFP_FLOAT)
20553
0
      _bfd_error_handler
20554
0
        (_("error: %pB uses %s instructions, whereas %pB does not"),
20555
0
         ibfd, "VFP", obfd);
20556
0
    else
20557
0
      _bfd_error_handler
20558
0
        (_("error: %pB uses %s instructions, whereas %pB does not"),
20559
0
         ibfd, "FPA", obfd);
20560
20561
0
    flags_compatible = false;
20562
0
  }
20563
20564
0
#ifdef EF_ARM_SOFT_FLOAT
20565
0
      if ((in_flags & EF_ARM_SOFT_FLOAT) != (out_flags & EF_ARM_SOFT_FLOAT))
20566
0
  {
20567
    /* We can allow interworking between code that is VFP format
20568
       layout, and uses either soft float or integer regs for
20569
       passing floating point arguments and results.  We already
20570
       know that the APCS_FLOAT flags match; similarly for VFP
20571
       flags.  */
20572
0
    if ((in_flags & EF_ARM_APCS_FLOAT) != 0
20573
0
        || (in_flags & EF_ARM_VFP_FLOAT) == 0)
20574
0
      {
20575
0
        if (in_flags & EF_ARM_SOFT_FLOAT)
20576
0
    _bfd_error_handler
20577
0
      (_("error: %pB uses software FP, whereas %pB uses hardware FP"),
20578
0
       ibfd, obfd);
20579
0
        else
20580
0
    _bfd_error_handler
20581
0
      (_("error: %pB uses hardware FP, whereas %pB uses software FP"),
20582
0
       ibfd, obfd);
20583
20584
0
        flags_compatible = false;
20585
0
      }
20586
0
  }
20587
0
#endif
20588
20589
      /* Interworking mismatch is only a warning.  */
20590
0
      if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
20591
0
  {
20592
0
    if (in_flags & EF_ARM_INTERWORK)
20593
0
      {
20594
0
        _bfd_error_handler
20595
0
    (_("warning: %pB supports interworking, whereas %pB does not"),
20596
0
     ibfd, obfd);
20597
0
      }
20598
0
    else
20599
0
      {
20600
0
        _bfd_error_handler
20601
0
    (_("warning: %pB does not support interworking, whereas %pB does"),
20602
0
     ibfd, obfd);
20603
0
      }
20604
0
  }
20605
0
    }
20606
20607
0
  return flags_compatible;
20608
0
}