Fix diagnostic errors
[external/binutils.git] / bfd / elf32-arm.c
1 /* 32-bit ELF support for ARM
2    Copyright (C) 1998-2018 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 "bfd_stdint.h"
26 #include "libiberty.h"
27 #include "libbfd.h"
28 #include "elf-bfd.h"
29 #include "elf-nacl.h"
30 #include "elf-vxworks.h"
31 #include "elf/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   ((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   ((HTAB)->use_rel \
42    ? sizeof (Elf32_External_Rel) \
43    : 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   ((HTAB)->use_rel \
49    ? bfd_elf32_swap_reloc_in \
50    : 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   ((HTAB)->use_rel \
56    ? bfd_elf32_swap_reloc_out \
57    : 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 #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 #define Pa(X) ((X) & 0xfffffffc)
67
68 static bfd_boolean 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          3,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          0,                     /* size (0 = byte, 1 = short, 2 = long) */
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          1,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          1,                     /* size (0 = byte, 1 = short, 2 = long) */
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          0,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          1,                     /* size (0 = byte, 1 = short, 2 = long) */
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          1,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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 (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          1,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1390          0,                     /* bitsize */
1391          FALSE,                 /* pc_relative */
1392          0,                     /* bitpos */
1393          complain_overflow_bitfield,/* 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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          1,                     /* size (0 = byte, 1 = short, 2 = long) */
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          1,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1682          0,                     /* bitsize */
1683          FALSE,                 /* pc_relative */
1684          0,                     /* bitpos */
1685          complain_overflow_bitfield,/* 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          1,                     /* size (0 = byte, 1 = short, 2 = long).  */
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          1,                     /* size (0 = byte, 1 = short, 2 = long).  */
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          1,                     /* size (0 = byte, 1 = short, 2 = long).  */
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          1,                     /* size (0 = byte, 1 = short, 2 = long).  */
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 };
1747
1748 /* 160 onwards: */
1749 static reloc_howto_type elf32_arm_howto_table_2[8] =
1750 {
1751   HOWTO (R_ARM_IRELATIVE,       /* type */
1752          0,                     /* rightshift */
1753          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1754          32,                    /* bitsize */
1755          FALSE,                 /* pc_relative */
1756          0,                     /* bitpos */
1757          complain_overflow_bitfield,/* complain_on_overflow */
1758          bfd_elf_generic_reloc, /* special_function */
1759          "R_ARM_IRELATIVE",     /* name */
1760          TRUE,                  /* partial_inplace */
1761          0xffffffff,            /* src_mask */
1762          0xffffffff,            /* dst_mask */
1763          FALSE),                /* pcrel_offset */
1764   HOWTO (R_ARM_GOTFUNCDESC,     /* type */
1765          0,                     /* rightshift */
1766          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1767          32,                    /* bitsize */
1768          FALSE,                 /* pc_relative */
1769          0,                     /* bitpos */
1770          complain_overflow_bitfield,/* complain_on_overflow */
1771          bfd_elf_generic_reloc, /* special_function */
1772          "R_ARM_GOTFUNCDESC",   /* name */
1773          FALSE,                 /* partial_inplace */
1774          0,                     /* src_mask */
1775          0xffffffff,            /* dst_mask */
1776          FALSE),                /* pcrel_offset */
1777   HOWTO (R_ARM_GOTOFFFUNCDESC, /* type */
1778          0,                     /* rightshift */
1779          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1780          32,                    /* bitsize */
1781          FALSE,                 /* pc_relative */
1782          0,                     /* bitpos */
1783          complain_overflow_bitfield,/* complain_on_overflow */
1784          bfd_elf_generic_reloc, /* special_function */
1785          "R_ARM_GOTOFFFUNCDESC",/* name */
1786          FALSE,                 /* partial_inplace */
1787          0,                     /* src_mask */
1788          0xffffffff,            /* dst_mask */
1789          FALSE),                /* pcrel_offset */
1790   HOWTO (R_ARM_FUNCDESC,        /* type */
1791          0,                     /* rightshift */
1792          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1793          32,                    /* bitsize */
1794          FALSE,                 /* pc_relative */
1795          0,                     /* bitpos */
1796          complain_overflow_bitfield,/* complain_on_overflow */
1797          bfd_elf_generic_reloc, /* special_function */
1798          "R_ARM_FUNCDESC",      /* name */
1799          FALSE,                 /* partial_inplace */
1800          0,                     /* src_mask */
1801          0xffffffff,            /* dst_mask */
1802          FALSE),                /* pcrel_offset */
1803   HOWTO (R_ARM_FUNCDESC_VALUE,  /* type */
1804          0,                     /* rightshift */
1805          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1806          64,                    /* bitsize */
1807          FALSE,                 /* pc_relative */
1808          0,                     /* bitpos */
1809          complain_overflow_bitfield,/* complain_on_overflow */
1810          bfd_elf_generic_reloc, /* special_function */
1811          "R_ARM_FUNCDESC_VALUE",/* name */
1812          FALSE,                 /* partial_inplace */
1813          0,                     /* src_mask */
1814          0xffffffff,            /* dst_mask */
1815          FALSE),                /* pcrel_offset */
1816   HOWTO (R_ARM_TLS_GD32_FDPIC,  /* type */
1817          0,                     /* rightshift */
1818          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1819          32,                    /* bitsize */
1820          FALSE,                 /* pc_relative */
1821          0,                     /* bitpos */
1822          complain_overflow_bitfield,/* complain_on_overflow */
1823          bfd_elf_generic_reloc, /* special_function */
1824          "R_ARM_TLS_GD32_FDPIC",/* name */
1825          FALSE,                 /* partial_inplace */
1826          0,                     /* src_mask */
1827          0xffffffff,            /* dst_mask */
1828          FALSE),                /* pcrel_offset */
1829   HOWTO (R_ARM_TLS_LDM32_FDPIC, /* type */
1830          0,                     /* rightshift */
1831          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1832          32,                    /* bitsize */
1833          FALSE,                 /* pc_relative */
1834          0,                     /* bitpos */
1835          complain_overflow_bitfield,/* complain_on_overflow */
1836          bfd_elf_generic_reloc, /* special_function */
1837          "R_ARM_TLS_LDM32_FDPIC",/* name */
1838          FALSE,                 /* partial_inplace */
1839          0,                     /* src_mask */
1840          0xffffffff,            /* dst_mask */
1841          FALSE),                /* pcrel_offset */
1842   HOWTO (R_ARM_TLS_IE32_FDPIC,  /* type */
1843          0,                     /* rightshift */
1844          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1845          32,                    /* bitsize */
1846          FALSE,                 /* pc_relative */
1847          0,                     /* bitpos */
1848          complain_overflow_bitfield,/* complain_on_overflow */
1849          bfd_elf_generic_reloc, /* special_function */
1850          "R_ARM_TLS_IE32_FDPIC",/* name */
1851          FALSE,                 /* partial_inplace */
1852          0,                     /* src_mask */
1853          0xffffffff,            /* dst_mask */
1854          FALSE),                /* pcrel_offset */
1855 };
1856
1857 /* 249-255 extended, currently unused, relocations:  */
1858 static reloc_howto_type elf32_arm_howto_table_3[4] =
1859 {
1860   HOWTO (R_ARM_RREL32,          /* type */
1861          0,                     /* rightshift */
1862          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1863          0,                     /* bitsize */
1864          FALSE,                 /* pc_relative */
1865          0,                     /* bitpos */
1866          complain_overflow_dont,/* complain_on_overflow */
1867          bfd_elf_generic_reloc, /* special_function */
1868          "R_ARM_RREL32",        /* name */
1869          FALSE,                 /* partial_inplace */
1870          0,                     /* src_mask */
1871          0,                     /* dst_mask */
1872          FALSE),                /* pcrel_offset */
1873
1874   HOWTO (R_ARM_RABS32,          /* type */
1875          0,                     /* rightshift */
1876          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1877          0,                     /* bitsize */
1878          FALSE,                 /* pc_relative */
1879          0,                     /* bitpos */
1880          complain_overflow_dont,/* complain_on_overflow */
1881          bfd_elf_generic_reloc, /* special_function */
1882          "R_ARM_RABS32",        /* name */
1883          FALSE,                 /* partial_inplace */
1884          0,                     /* src_mask */
1885          0,                     /* dst_mask */
1886          FALSE),                /* pcrel_offset */
1887
1888   HOWTO (R_ARM_RPC24,           /* type */
1889          0,                     /* rightshift */
1890          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1891          0,                     /* bitsize */
1892          FALSE,                 /* pc_relative */
1893          0,                     /* bitpos */
1894          complain_overflow_dont,/* complain_on_overflow */
1895          bfd_elf_generic_reloc, /* special_function */
1896          "R_ARM_RPC24",         /* name */
1897          FALSE,                 /* partial_inplace */
1898          0,                     /* src_mask */
1899          0,                     /* dst_mask */
1900          FALSE),                /* pcrel_offset */
1901
1902   HOWTO (R_ARM_RBASE,           /* type */
1903          0,                     /* rightshift */
1904          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1905          0,                     /* bitsize */
1906          FALSE,                 /* pc_relative */
1907          0,                     /* bitpos */
1908          complain_overflow_dont,/* complain_on_overflow */
1909          bfd_elf_generic_reloc, /* special_function */
1910          "R_ARM_RBASE",         /* name */
1911          FALSE,                 /* partial_inplace */
1912          0,                     /* src_mask */
1913          0,                     /* dst_mask */
1914          FALSE)                 /* pcrel_offset */
1915 };
1916
1917 static reloc_howto_type *
1918 elf32_arm_howto_from_type (unsigned int r_type)
1919 {
1920   if (r_type < ARRAY_SIZE (elf32_arm_howto_table_1))
1921     return &elf32_arm_howto_table_1[r_type];
1922
1923   if (r_type >= R_ARM_IRELATIVE
1924       && r_type < R_ARM_IRELATIVE + ARRAY_SIZE (elf32_arm_howto_table_2))
1925     return &elf32_arm_howto_table_2[r_type - R_ARM_IRELATIVE];
1926
1927   if (r_type >= R_ARM_RREL32
1928       && r_type < R_ARM_RREL32 + ARRAY_SIZE (elf32_arm_howto_table_3))
1929     return &elf32_arm_howto_table_3[r_type - R_ARM_RREL32];
1930
1931   return NULL;
1932 }
1933
1934 static bfd_boolean
1935 elf32_arm_info_to_howto (bfd * abfd, arelent * bfd_reloc,
1936                          Elf_Internal_Rela * elf_reloc)
1937 {
1938   unsigned int r_type;
1939
1940   r_type = ELF32_R_TYPE (elf_reloc->r_info);
1941   if ((bfd_reloc->howto = elf32_arm_howto_from_type (r_type)) == NULL)
1942     {
1943       /* xgettext:c-format */
1944       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1945                           abfd, r_type);
1946       bfd_set_error (bfd_error_bad_value);
1947       return FALSE;
1948     }
1949   return TRUE;
1950 }
1951
1952 struct elf32_arm_reloc_map
1953   {
1954     bfd_reloc_code_real_type  bfd_reloc_val;
1955     unsigned char             elf_reloc_val;
1956   };
1957
1958 /* All entries in this list must also be present in elf32_arm_howto_table.  */
1959 static const struct elf32_arm_reloc_map elf32_arm_reloc_map[] =
1960   {
1961     {BFD_RELOC_NONE,                 R_ARM_NONE},
1962     {BFD_RELOC_ARM_PCREL_BRANCH,     R_ARM_PC24},
1963     {BFD_RELOC_ARM_PCREL_CALL,       R_ARM_CALL},
1964     {BFD_RELOC_ARM_PCREL_JUMP,       R_ARM_JUMP24},
1965     {BFD_RELOC_ARM_PCREL_BLX,        R_ARM_XPC25},
1966     {BFD_RELOC_THUMB_PCREL_BLX,      R_ARM_THM_XPC22},
1967     {BFD_RELOC_32,                   R_ARM_ABS32},
1968     {BFD_RELOC_32_PCREL,             R_ARM_REL32},
1969     {BFD_RELOC_8,                    R_ARM_ABS8},
1970     {BFD_RELOC_16,                   R_ARM_ABS16},
1971     {BFD_RELOC_ARM_OFFSET_IMM,       R_ARM_ABS12},
1972     {BFD_RELOC_ARM_THUMB_OFFSET,     R_ARM_THM_ABS5},
1973     {BFD_RELOC_THUMB_PCREL_BRANCH25, R_ARM_THM_JUMP24},
1974     {BFD_RELOC_THUMB_PCREL_BRANCH23, R_ARM_THM_CALL},
1975     {BFD_RELOC_THUMB_PCREL_BRANCH12, R_ARM_THM_JUMP11},
1976     {BFD_RELOC_THUMB_PCREL_BRANCH20, R_ARM_THM_JUMP19},
1977     {BFD_RELOC_THUMB_PCREL_BRANCH9,  R_ARM_THM_JUMP8},
1978     {BFD_RELOC_THUMB_PCREL_BRANCH7,  R_ARM_THM_JUMP6},
1979     {BFD_RELOC_ARM_GLOB_DAT,         R_ARM_GLOB_DAT},
1980     {BFD_RELOC_ARM_JUMP_SLOT,        R_ARM_JUMP_SLOT},
1981     {BFD_RELOC_ARM_RELATIVE,         R_ARM_RELATIVE},
1982     {BFD_RELOC_ARM_GOTOFF,           R_ARM_GOTOFF32},
1983     {BFD_RELOC_ARM_GOTPC,            R_ARM_GOTPC},
1984     {BFD_RELOC_ARM_GOT_PREL,         R_ARM_GOT_PREL},
1985     {BFD_RELOC_ARM_GOT32,            R_ARM_GOT32},
1986     {BFD_RELOC_ARM_PLT32,            R_ARM_PLT32},
1987     {BFD_RELOC_ARM_TARGET1,          R_ARM_TARGET1},
1988     {BFD_RELOC_ARM_ROSEGREL32,       R_ARM_ROSEGREL32},
1989     {BFD_RELOC_ARM_SBREL32,          R_ARM_SBREL32},
1990     {BFD_RELOC_ARM_PREL31,           R_ARM_PREL31},
1991     {BFD_RELOC_ARM_TARGET2,          R_ARM_TARGET2},
1992     {BFD_RELOC_ARM_PLT32,            R_ARM_PLT32},
1993     {BFD_RELOC_ARM_TLS_GOTDESC,      R_ARM_TLS_GOTDESC},
1994     {BFD_RELOC_ARM_TLS_CALL,         R_ARM_TLS_CALL},
1995     {BFD_RELOC_ARM_THM_TLS_CALL,     R_ARM_THM_TLS_CALL},
1996     {BFD_RELOC_ARM_TLS_DESCSEQ,      R_ARM_TLS_DESCSEQ},
1997     {BFD_RELOC_ARM_THM_TLS_DESCSEQ,  R_ARM_THM_TLS_DESCSEQ},
1998     {BFD_RELOC_ARM_TLS_DESC,         R_ARM_TLS_DESC},
1999     {BFD_RELOC_ARM_TLS_GD32,         R_ARM_TLS_GD32},
2000     {BFD_RELOC_ARM_TLS_LDO32,        R_ARM_TLS_LDO32},
2001     {BFD_RELOC_ARM_TLS_LDM32,        R_ARM_TLS_LDM32},
2002     {BFD_RELOC_ARM_TLS_DTPMOD32,     R_ARM_TLS_DTPMOD32},
2003     {BFD_RELOC_ARM_TLS_DTPOFF32,     R_ARM_TLS_DTPOFF32},
2004     {BFD_RELOC_ARM_TLS_TPOFF32,      R_ARM_TLS_TPOFF32},
2005     {BFD_RELOC_ARM_TLS_IE32,         R_ARM_TLS_IE32},
2006     {BFD_RELOC_ARM_TLS_LE32,         R_ARM_TLS_LE32},
2007     {BFD_RELOC_ARM_IRELATIVE,        R_ARM_IRELATIVE},
2008     {BFD_RELOC_ARM_GOTFUNCDESC,      R_ARM_GOTFUNCDESC},
2009     {BFD_RELOC_ARM_GOTOFFFUNCDESC,   R_ARM_GOTOFFFUNCDESC},
2010     {BFD_RELOC_ARM_FUNCDESC,         R_ARM_FUNCDESC},
2011     {BFD_RELOC_ARM_FUNCDESC_VALUE,   R_ARM_FUNCDESC_VALUE},
2012     {BFD_RELOC_ARM_TLS_GD32_FDPIC,   R_ARM_TLS_GD32_FDPIC},
2013     {BFD_RELOC_ARM_TLS_LDM32_FDPIC,  R_ARM_TLS_LDM32_FDPIC},
2014     {BFD_RELOC_ARM_TLS_IE32_FDPIC,   R_ARM_TLS_IE32_FDPIC},
2015     {BFD_RELOC_VTABLE_INHERIT,       R_ARM_GNU_VTINHERIT},
2016     {BFD_RELOC_VTABLE_ENTRY,         R_ARM_GNU_VTENTRY},
2017     {BFD_RELOC_ARM_MOVW,             R_ARM_MOVW_ABS_NC},
2018     {BFD_RELOC_ARM_MOVT,             R_ARM_MOVT_ABS},
2019     {BFD_RELOC_ARM_MOVW_PCREL,       R_ARM_MOVW_PREL_NC},
2020     {BFD_RELOC_ARM_MOVT_PCREL,       R_ARM_MOVT_PREL},
2021     {BFD_RELOC_ARM_THUMB_MOVW,       R_ARM_THM_MOVW_ABS_NC},
2022     {BFD_RELOC_ARM_THUMB_MOVT,       R_ARM_THM_MOVT_ABS},
2023     {BFD_RELOC_ARM_THUMB_MOVW_PCREL, R_ARM_THM_MOVW_PREL_NC},
2024     {BFD_RELOC_ARM_THUMB_MOVT_PCREL, R_ARM_THM_MOVT_PREL},
2025     {BFD_RELOC_ARM_ALU_PC_G0_NC, R_ARM_ALU_PC_G0_NC},
2026     {BFD_RELOC_ARM_ALU_PC_G0, R_ARM_ALU_PC_G0},
2027     {BFD_RELOC_ARM_ALU_PC_G1_NC, R_ARM_ALU_PC_G1_NC},
2028     {BFD_RELOC_ARM_ALU_PC_G1, R_ARM_ALU_PC_G1},
2029     {BFD_RELOC_ARM_ALU_PC_G2, R_ARM_ALU_PC_G2},
2030     {BFD_RELOC_ARM_LDR_PC_G0, R_ARM_LDR_PC_G0},
2031     {BFD_RELOC_ARM_LDR_PC_G1, R_ARM_LDR_PC_G1},
2032     {BFD_RELOC_ARM_LDR_PC_G2, R_ARM_LDR_PC_G2},
2033     {BFD_RELOC_ARM_LDRS_PC_G0, R_ARM_LDRS_PC_G0},
2034     {BFD_RELOC_ARM_LDRS_PC_G1, R_ARM_LDRS_PC_G1},
2035     {BFD_RELOC_ARM_LDRS_PC_G2, R_ARM_LDRS_PC_G2},
2036     {BFD_RELOC_ARM_LDC_PC_G0, R_ARM_LDC_PC_G0},
2037     {BFD_RELOC_ARM_LDC_PC_G1, R_ARM_LDC_PC_G1},
2038     {BFD_RELOC_ARM_LDC_PC_G2, R_ARM_LDC_PC_G2},
2039     {BFD_RELOC_ARM_ALU_SB_G0_NC, R_ARM_ALU_SB_G0_NC},
2040     {BFD_RELOC_ARM_ALU_SB_G0, R_ARM_ALU_SB_G0},
2041     {BFD_RELOC_ARM_ALU_SB_G1_NC, R_ARM_ALU_SB_G1_NC},
2042     {BFD_RELOC_ARM_ALU_SB_G1, R_ARM_ALU_SB_G1},
2043     {BFD_RELOC_ARM_ALU_SB_G2, R_ARM_ALU_SB_G2},
2044     {BFD_RELOC_ARM_LDR_SB_G0, R_ARM_LDR_SB_G0},
2045     {BFD_RELOC_ARM_LDR_SB_G1, R_ARM_LDR_SB_G1},
2046     {BFD_RELOC_ARM_LDR_SB_G2, R_ARM_LDR_SB_G2},
2047     {BFD_RELOC_ARM_LDRS_SB_G0, R_ARM_LDRS_SB_G0},
2048     {BFD_RELOC_ARM_LDRS_SB_G1, R_ARM_LDRS_SB_G1},
2049     {BFD_RELOC_ARM_LDRS_SB_G2, R_ARM_LDRS_SB_G2},
2050     {BFD_RELOC_ARM_LDC_SB_G0, R_ARM_LDC_SB_G0},
2051     {BFD_RELOC_ARM_LDC_SB_G1, R_ARM_LDC_SB_G1},
2052     {BFD_RELOC_ARM_LDC_SB_G2, R_ARM_LDC_SB_G2},
2053     {BFD_RELOC_ARM_V4BX,             R_ARM_V4BX},
2054     {BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC, R_ARM_THM_ALU_ABS_G3_NC},
2055     {BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC, R_ARM_THM_ALU_ABS_G2_NC},
2056     {BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC, R_ARM_THM_ALU_ABS_G1_NC},
2057     {BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC, R_ARM_THM_ALU_ABS_G0_NC}
2058   };
2059
2060 static reloc_howto_type *
2061 elf32_arm_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2062                              bfd_reloc_code_real_type code)
2063 {
2064   unsigned int i;
2065
2066   for (i = 0; i < ARRAY_SIZE (elf32_arm_reloc_map); i ++)
2067     if (elf32_arm_reloc_map[i].bfd_reloc_val == code)
2068       return elf32_arm_howto_from_type (elf32_arm_reloc_map[i].elf_reloc_val);
2069
2070   return NULL;
2071 }
2072
2073 static reloc_howto_type *
2074 elf32_arm_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2075                              const char *r_name)
2076 {
2077   unsigned int i;
2078
2079   for (i = 0; i < ARRAY_SIZE (elf32_arm_howto_table_1); i++)
2080     if (elf32_arm_howto_table_1[i].name != NULL
2081         && strcasecmp (elf32_arm_howto_table_1[i].name, r_name) == 0)
2082       return &elf32_arm_howto_table_1[i];
2083
2084   for (i = 0; i < ARRAY_SIZE (elf32_arm_howto_table_2); i++)
2085     if (elf32_arm_howto_table_2[i].name != NULL
2086         && strcasecmp (elf32_arm_howto_table_2[i].name, r_name) == 0)
2087       return &elf32_arm_howto_table_2[i];
2088
2089   for (i = 0; i < ARRAY_SIZE (elf32_arm_howto_table_3); i++)
2090     if (elf32_arm_howto_table_3[i].name != NULL
2091         && strcasecmp (elf32_arm_howto_table_3[i].name, r_name) == 0)
2092       return &elf32_arm_howto_table_3[i];
2093
2094   return NULL;
2095 }
2096
2097 /* Support for core dump NOTE sections.  */
2098
2099 static bfd_boolean
2100 elf32_arm_nabi_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2101 {
2102   int offset;
2103   size_t size;
2104
2105   switch (note->descsz)
2106     {
2107       default:
2108         return FALSE;
2109
2110       case 148:         /* Linux/ARM 32-bit.  */
2111         /* pr_cursig */
2112         elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2113
2114         /* pr_pid */
2115         elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
2116
2117         /* pr_reg */
2118         offset = 72;
2119         size = 72;
2120
2121         break;
2122     }
2123
2124   /* Make a ".reg/999" section.  */
2125   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2126                                           size, note->descpos + offset);
2127 }
2128
2129 static bfd_boolean
2130 elf32_arm_nabi_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2131 {
2132   switch (note->descsz)
2133     {
2134       default:
2135         return FALSE;
2136
2137       case 124:         /* Linux/ARM elf_prpsinfo.  */
2138         elf_tdata (abfd)->core->pid
2139          = bfd_get_32 (abfd, note->descdata + 12);
2140         elf_tdata (abfd)->core->program
2141          = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
2142         elf_tdata (abfd)->core->command
2143          = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
2144     }
2145
2146   /* Note that for some reason, a spurious space is tacked
2147      onto the end of the args in some (at least one anyway)
2148      implementations, so strip it off if it exists.  */
2149   {
2150     char *command = elf_tdata (abfd)->core->command;
2151     int n = strlen (command);
2152
2153     if (0 < n && command[n - 1] == ' ')
2154       command[n - 1] = '\0';
2155   }
2156
2157   return TRUE;
2158 }
2159
2160 static char *
2161 elf32_arm_nabi_write_core_note (bfd *abfd, char *buf, int *bufsiz,
2162                                 int note_type, ...)
2163 {
2164   switch (note_type)
2165     {
2166     default:
2167       return NULL;
2168
2169     case NT_PRPSINFO:
2170       {
2171         char data[124] ATTRIBUTE_NONSTRING;
2172         va_list ap;
2173
2174         va_start (ap, note_type);
2175         memset (data, 0, sizeof (data));
2176         strncpy (data + 28, va_arg (ap, const char *), 16);
2177 #if GCC_VERSION == 8001
2178         DIAGNOSTIC_PUSH;
2179         /* GCC 8.1 warns about 80 equals destination size with
2180            -Wstringop-truncation:
2181            https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
2182          */
2183         DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
2184 #endif
2185         strncpy (data + 44, va_arg (ap, const char *), 80);
2186 #if GCC_VERSION == 8001
2187         DIAGNOSTIC_POP;
2188 #endif
2189         va_end (ap);
2190
2191         return elfcore_write_note (abfd, buf, bufsiz,
2192                                    "CORE", note_type, data, sizeof (data));
2193       }
2194
2195     case NT_PRSTATUS:
2196       {
2197         char data[148];
2198         va_list ap;
2199         long pid;
2200         int cursig;
2201         const void *greg;
2202
2203         va_start (ap, note_type);
2204         memset (data, 0, sizeof (data));
2205         pid = va_arg (ap, long);
2206         bfd_put_32 (abfd, pid, data + 24);
2207         cursig = va_arg (ap, int);
2208         bfd_put_16 (abfd, cursig, data + 12);
2209         greg = va_arg (ap, const void *);
2210         memcpy (data + 72, greg, 72);
2211         va_end (ap);
2212
2213         return elfcore_write_note (abfd, buf, bufsiz,
2214                                    "CORE", note_type, data, sizeof (data));
2215       }
2216     }
2217 }
2218
2219 #define TARGET_LITTLE_SYM               arm_elf32_le_vec
2220 #define TARGET_LITTLE_NAME              "elf32-littlearm"
2221 #define TARGET_BIG_SYM                  arm_elf32_be_vec
2222 #define TARGET_BIG_NAME                 "elf32-bigarm"
2223
2224 #define elf_backend_grok_prstatus       elf32_arm_nabi_grok_prstatus
2225 #define elf_backend_grok_psinfo         elf32_arm_nabi_grok_psinfo
2226 #define elf_backend_write_core_note     elf32_arm_nabi_write_core_note
2227
2228 typedef unsigned long int insn32;
2229 typedef unsigned short int insn16;
2230
2231 /* In lieu of proper flags, assume all EABIv4 or later objects are
2232    interworkable.  */
2233 #define INTERWORK_FLAG(abfd)  \
2234   (EF_ARM_EABI_VERSION (elf_elfheader (abfd)->e_flags) >= EF_ARM_EABI_VER4 \
2235   || (elf_elfheader (abfd)->e_flags & EF_ARM_INTERWORK) \
2236   || ((abfd)->flags & BFD_LINKER_CREATED))
2237
2238 /* The linker script knows the section names for placement.
2239    The entry_names are used to do simple name mangling on the stubs.
2240    Given a function name, and its type, the stub can be found. The
2241    name can be changed. The only requirement is the %s be present.  */
2242 #define THUMB2ARM_GLUE_SECTION_NAME ".glue_7t"
2243 #define THUMB2ARM_GLUE_ENTRY_NAME   "__%s_from_thumb"
2244
2245 #define ARM2THUMB_GLUE_SECTION_NAME ".glue_7"
2246 #define ARM2THUMB_GLUE_ENTRY_NAME   "__%s_from_arm"
2247
2248 #define VFP11_ERRATUM_VENEER_SECTION_NAME ".vfp11_veneer"
2249 #define VFP11_ERRATUM_VENEER_ENTRY_NAME   "__vfp11_veneer_%x"
2250
2251 #define STM32L4XX_ERRATUM_VENEER_SECTION_NAME ".text.stm32l4xx_veneer"
2252 #define STM32L4XX_ERRATUM_VENEER_ENTRY_NAME   "__stm32l4xx_veneer_%x"
2253
2254 #define ARM_BX_GLUE_SECTION_NAME ".v4_bx"
2255 #define ARM_BX_GLUE_ENTRY_NAME   "__bx_r%d"
2256
2257 #define STUB_ENTRY_NAME   "__%s_veneer"
2258
2259 #define CMSE_PREFIX "__acle_se_"
2260
2261 /* The name of the dynamic interpreter.  This is put in the .interp
2262    section.  */
2263 #define ELF_DYNAMIC_INTERPRETER     "/usr/lib/ld.so.1"
2264
2265 /* FDPIC default stack size.  */
2266 #define DEFAULT_STACK_SIZE 0x8000
2267
2268 static const unsigned long tls_trampoline [] =
2269 {
2270   0xe08e0000,           /* add r0, lr, r0 */
2271   0xe5901004,           /* ldr r1, [r0,#4] */
2272   0xe12fff11,           /* bx  r1 */
2273 };
2274
2275 static const unsigned long dl_tlsdesc_lazy_trampoline [] =
2276 {
2277   0xe52d2004, /*        push    {r2}                    */
2278   0xe59f200c, /*      ldr     r2, [pc, #3f - . - 8]     */
2279   0xe59f100c, /*      ldr     r1, [pc, #4f - . - 8]     */
2280   0xe79f2002, /* 1:   ldr     r2, [pc, r2]              */
2281   0xe081100f, /* 2:   add     r1, pc                    */
2282   0xe12fff12, /*      bx      r2                        */
2283   0x00000014, /* 3:   .word  _GLOBAL_OFFSET_TABLE_ - 1b - 8
2284                                 + dl_tlsdesc_lazy_resolver(GOT)   */
2285   0x00000018, /* 4:   .word  _GLOBAL_OFFSET_TABLE_ - 2b - 8 */
2286 };
2287
2288 /* ARM FDPIC PLT entry.  */
2289 /* The last 5 words contain PLT lazy fragment code and data.  */
2290 static const bfd_vma elf32_arm_fdpic_plt_entry [] =
2291   {
2292     0xe59fc008,    /* ldr     r12, .L1 */
2293     0xe08cc009,    /* add     r12, r12, r9 */
2294     0xe59c9004,    /* ldr     r9, [r12, #4] */
2295     0xe59cf000,    /* ldr     pc, [r12] */
2296     0x00000000,    /* L1.     .word   foo(GOTOFFFUNCDESC) */
2297     0x00000000,    /* L1.     .word   foo(funcdesc_value_reloc_offset) */
2298     0xe51fc00c,    /* ldr     r12, [pc, #-12] */
2299     0xe92d1000,    /* push    {r12} */
2300     0xe599c004,    /* ldr     r12, [r9, #4] */
2301     0xe599f000,    /* ldr     pc, [r9] */
2302   };
2303
2304 /* Thumb FDPIC PLT entry.  */
2305 /* The last 5 words contain PLT lazy fragment code and data.  */
2306 static const bfd_vma elf32_arm_fdpic_thumb_plt_entry [] =
2307   {
2308     0xc00cf8df,    /* ldr.w   r12, .L1 */
2309     0x0c09eb0c,    /* add.w   r12, r12, r9 */
2310     0x9004f8dc,    /* ldr.w   r9, [r12, #4] */
2311     0xf000f8dc,    /* ldr.w   pc, [r12] */
2312     0x00000000,    /* .L1     .word   foo(GOTOFFFUNCDESC) */
2313     0x00000000,    /* .L2     .word   foo(funcdesc_value_reloc_offset) */
2314     0xc008f85f,    /* ldr.w   r12, .L2 */
2315     0xcd04f84d,    /* push    {r12} */
2316     0xc004f8d9,    /* ldr.w   r12, [r9, #4] */
2317     0xf000f8d9,    /* ldr.w   pc, [r9] */
2318   };
2319
2320 #ifdef FOUR_WORD_PLT
2321
2322 /* The first entry in a procedure linkage table looks like
2323    this.  It is set up so that any shared library function that is
2324    called before the relocation has been set up calls the dynamic
2325    linker first.  */
2326 static const bfd_vma elf32_arm_plt0_entry [] =
2327 {
2328   0xe52de004,           /* str   lr, [sp, #-4]! */
2329   0xe59fe010,           /* ldr   lr, [pc, #16]  */
2330   0xe08fe00e,           /* add   lr, pc, lr     */
2331   0xe5bef008,           /* ldr   pc, [lr, #8]!  */
2332 };
2333
2334 /* Subsequent entries in a procedure linkage table look like
2335    this.  */
2336 static const bfd_vma elf32_arm_plt_entry [] =
2337 {
2338   0xe28fc600,           /* add   ip, pc, #NN    */
2339   0xe28cca00,           /* add   ip, ip, #NN    */
2340   0xe5bcf000,           /* ldr   pc, [ip, #NN]! */
2341   0x00000000,           /* unused               */
2342 };
2343
2344 #else /* not FOUR_WORD_PLT */
2345
2346 /* The first entry in a procedure linkage table looks like
2347    this.  It is set up so that any shared library function that is
2348    called before the relocation has been set up calls the dynamic
2349    linker first.  */
2350 static const bfd_vma elf32_arm_plt0_entry [] =
2351 {
2352   0xe52de004,           /* str   lr, [sp, #-4]! */
2353   0xe59fe004,           /* ldr   lr, [pc, #4]   */
2354   0xe08fe00e,           /* add   lr, pc, lr     */
2355   0xe5bef008,           /* ldr   pc, [lr, #8]!  */
2356   0x00000000,           /* &GOT[0] - .          */
2357 };
2358
2359 /* By default subsequent entries in a procedure linkage table look like
2360    this. Offsets that don't fit into 28 bits will cause link error.  */
2361 static const bfd_vma elf32_arm_plt_entry_short [] =
2362 {
2363   0xe28fc600,           /* add   ip, pc, #0xNN00000 */
2364   0xe28cca00,           /* add   ip, ip, #0xNN000   */
2365   0xe5bcf000,           /* ldr   pc, [ip, #0xNNN]!  */
2366 };
2367
2368 /* When explicitly asked, we'll use this "long" entry format
2369    which can cope with arbitrary displacements.  */
2370 static const bfd_vma elf32_arm_plt_entry_long [] =
2371 {
2372   0xe28fc200,           /* add   ip, pc, #0xN0000000 */
2373   0xe28cc600,           /* add   ip, ip, #0xNN00000  */
2374   0xe28cca00,           /* add   ip, ip, #0xNN000    */
2375   0xe5bcf000,           /* ldr   pc, [ip, #0xNNN]!   */
2376 };
2377
2378 static bfd_boolean elf32_arm_use_long_plt_entry = FALSE;
2379
2380 #endif /* not FOUR_WORD_PLT */
2381
2382 /* The first entry in a procedure linkage table looks like this.
2383    It is set up so that any shared library function that is called before the
2384    relocation has been set up calls the dynamic linker first.  */
2385 static const bfd_vma elf32_thumb2_plt0_entry [] =
2386 {
2387   /* NOTE: As this is a mixture of 16-bit and 32-bit instructions,
2388      an instruction maybe encoded to one or two array elements.  */
2389   0xf8dfb500,           /* push    {lr}          */
2390   0x44fee008,           /* ldr.w   lr, [pc, #8]  */
2391                         /* add     lr, pc        */
2392   0xff08f85e,           /* ldr.w   pc, [lr, #8]! */
2393   0x00000000,           /* &GOT[0] - .           */
2394 };
2395
2396 /* Subsequent entries in a procedure linkage table for thumb only target
2397    look like this.  */
2398 static const bfd_vma elf32_thumb2_plt_entry [] =
2399 {
2400   /* NOTE: As this is a mixture of 16-bit and 32-bit instructions,
2401      an instruction maybe encoded to one or two array elements.  */
2402   0x0c00f240,           /* movw    ip, #0xNNNN    */
2403   0x0c00f2c0,           /* movt    ip, #0xNNNN    */
2404   0xf8dc44fc,           /* add     ip, pc         */
2405   0xbf00f000            /* ldr.w   pc, [ip]       */
2406                         /* nop                    */
2407 };
2408
2409 /* The format of the first entry in the procedure linkage table
2410    for a VxWorks executable.  */
2411 static const bfd_vma elf32_arm_vxworks_exec_plt0_entry[] =
2412 {
2413   0xe52dc008,           /* str    ip,[sp,#-8]!                  */
2414   0xe59fc000,           /* ldr    ip,[pc]                       */
2415   0xe59cf008,           /* ldr    pc,[ip,#8]                    */
2416   0x00000000,           /* .long  _GLOBAL_OFFSET_TABLE_         */
2417 };
2418
2419 /* The format of subsequent entries in a VxWorks executable.  */
2420 static const bfd_vma elf32_arm_vxworks_exec_plt_entry[] =
2421 {
2422   0xe59fc000,         /* ldr    ip,[pc]                 */
2423   0xe59cf000,         /* ldr    pc,[ip]                 */
2424   0x00000000,         /* .long  @got                            */
2425   0xe59fc000,         /* ldr    ip,[pc]                 */
2426   0xea000000,         /* b      _PLT                            */
2427   0x00000000,         /* .long  @pltindex*sizeof(Elf32_Rela)    */
2428 };
2429
2430 /* The format of entries in a VxWorks shared library.  */
2431 static const bfd_vma elf32_arm_vxworks_shared_plt_entry[] =
2432 {
2433   0xe59fc000,         /* ldr    ip,[pc]                 */
2434   0xe79cf009,         /* ldr    pc,[ip,r9]                      */
2435   0x00000000,         /* .long  @got                            */
2436   0xe59fc000,         /* ldr    ip,[pc]                 */
2437   0xe599f008,         /* ldr    pc,[r9,#8]                      */
2438   0x00000000,         /* .long  @pltindex*sizeof(Elf32_Rela)    */
2439 };
2440
2441 /* An initial stub used if the PLT entry is referenced from Thumb code.  */
2442 #define PLT_THUMB_STUB_SIZE 4
2443 static const bfd_vma elf32_arm_plt_thumb_stub [] =
2444 {
2445   0x4778,               /* bx pc */
2446   0x46c0                /* nop   */
2447 };
2448
2449 /* The entries in a PLT when using a DLL-based target with multiple
2450    address spaces.  */
2451 static const bfd_vma elf32_arm_symbian_plt_entry [] =
2452 {
2453   0xe51ff004,         /* ldr   pc, [pc, #-4] */
2454   0x00000000,         /* dcd   R_ARM_GLOB_DAT(X) */
2455 };
2456
2457 /* The first entry in a procedure linkage table looks like
2458    this.  It is set up so that any shared library function that is
2459    called before the relocation has been set up calls the dynamic
2460    linker first.  */
2461 static const bfd_vma elf32_arm_nacl_plt0_entry [] =
2462 {
2463   /* First bundle: */
2464   0xe300c000,           /* movw ip, #:lower16:&GOT[2]-.+8       */
2465   0xe340c000,           /* movt ip, #:upper16:&GOT[2]-.+8       */
2466   0xe08cc00f,           /* add  ip, ip, pc                      */
2467   0xe52dc008,           /* str  ip, [sp, #-8]!                  */
2468   /* Second bundle: */
2469   0xe3ccc103,           /* bic  ip, ip, #0xc0000000             */
2470   0xe59cc000,           /* ldr  ip, [ip]                        */
2471   0xe3ccc13f,           /* bic  ip, ip, #0xc000000f             */
2472   0xe12fff1c,           /* bx   ip                              */
2473   /* Third bundle: */
2474   0xe320f000,           /* nop                                  */
2475   0xe320f000,           /* nop                                  */
2476   0xe320f000,           /* nop                                  */
2477   /* .Lplt_tail: */
2478   0xe50dc004,           /* str  ip, [sp, #-4]                   */
2479   /* Fourth bundle: */
2480   0xe3ccc103,           /* bic  ip, ip, #0xc0000000             */
2481   0xe59cc000,           /* ldr  ip, [ip]                        */
2482   0xe3ccc13f,           /* bic  ip, ip, #0xc000000f             */
2483   0xe12fff1c,           /* bx   ip                              */
2484 };
2485 #define ARM_NACL_PLT_TAIL_OFFSET        (11 * 4)
2486
2487 /* Subsequent entries in a procedure linkage table look like this.  */
2488 static const bfd_vma elf32_arm_nacl_plt_entry [] =
2489 {
2490   0xe300c000,           /* movw ip, #:lower16:&GOT[n]-.+8       */
2491   0xe340c000,           /* movt ip, #:upper16:&GOT[n]-.+8       */
2492   0xe08cc00f,           /* add  ip, ip, pc                      */
2493   0xea000000,           /* b    .Lplt_tail                      */
2494 };
2495
2496 #define ARM_MAX_FWD_BRANCH_OFFSET  ((((1 << 23) - 1) << 2) + 8)
2497 #define ARM_MAX_BWD_BRANCH_OFFSET  ((-((1 << 23) << 2)) + 8)
2498 #define THM_MAX_FWD_BRANCH_OFFSET  ((1 << 22) -2 + 4)
2499 #define THM_MAX_BWD_BRANCH_OFFSET  (-(1 << 22) + 4)
2500 #define THM2_MAX_FWD_BRANCH_OFFSET (((1 << 24) - 2) + 4)
2501 #define THM2_MAX_BWD_BRANCH_OFFSET (-(1 << 24) + 4)
2502 #define THM2_MAX_FWD_COND_BRANCH_OFFSET (((1 << 20) -2) + 4)
2503 #define THM2_MAX_BWD_COND_BRANCH_OFFSET (-(1 << 20) + 4)
2504
2505 enum stub_insn_type
2506 {
2507   THUMB16_TYPE = 1,
2508   THUMB32_TYPE,
2509   ARM_TYPE,
2510   DATA_TYPE
2511 };
2512
2513 #define THUMB16_INSN(X)         {(X), THUMB16_TYPE, R_ARM_NONE, 0}
2514 /* A bit of a hack.  A Thumb conditional branch, in which the proper condition
2515    is inserted in arm_build_one_stub().  */
2516 #define THUMB16_BCOND_INSN(X)   {(X), THUMB16_TYPE, R_ARM_NONE, 1}
2517 #define THUMB32_INSN(X)         {(X), THUMB32_TYPE, R_ARM_NONE, 0}
2518 #define THUMB32_MOVT(X)         {(X), THUMB32_TYPE, R_ARM_THM_MOVT_ABS, 0}
2519 #define THUMB32_MOVW(X)         {(X), THUMB32_TYPE, R_ARM_THM_MOVW_ABS_NC, 0}
2520 #define THUMB32_B_INSN(X, Z)    {(X), THUMB32_TYPE, R_ARM_THM_JUMP24, (Z)}
2521 #define ARM_INSN(X)             {(X), ARM_TYPE, R_ARM_NONE, 0}
2522 #define ARM_REL_INSN(X, Z)      {(X), ARM_TYPE, R_ARM_JUMP24, (Z)}
2523 #define DATA_WORD(X,Y,Z)        {(X), DATA_TYPE, (Y), (Z)}
2524
2525 typedef struct
2526 {
2527   bfd_vma              data;
2528   enum stub_insn_type  type;
2529   unsigned int         r_type;
2530   int                  reloc_addend;
2531 }  insn_sequence;
2532
2533 /* Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2534    to reach the stub if necessary.  */
2535 static const insn_sequence elf32_arm_stub_long_branch_any_any[] =
2536 {
2537   ARM_INSN (0xe51ff004),            /* ldr   pc, [pc, #-4] */
2538   DATA_WORD (0, R_ARM_ABS32, 0),    /* dcd   R_ARM_ABS32(X) */
2539 };
2540
2541 /* V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2542    available.  */
2543 static const insn_sequence elf32_arm_stub_long_branch_v4t_arm_thumb[] =
2544 {
2545   ARM_INSN (0xe59fc000),            /* ldr   ip, [pc, #0] */
2546   ARM_INSN (0xe12fff1c),            /* bx    ip */
2547   DATA_WORD (0, R_ARM_ABS32, 0),    /* dcd   R_ARM_ABS32(X) */
2548 };
2549
2550 /* Thumb -> Thumb long branch stub. Used on M-profile architectures.  */
2551 static const insn_sequence elf32_arm_stub_long_branch_thumb_only[] =
2552 {
2553   THUMB16_INSN (0xb401),             /* push {r0} */
2554   THUMB16_INSN (0x4802),             /* ldr  r0, [pc, #8] */
2555   THUMB16_INSN (0x4684),             /* mov  ip, r0 */
2556   THUMB16_INSN (0xbc01),             /* pop  {r0} */
2557   THUMB16_INSN (0x4760),             /* bx   ip */
2558   THUMB16_INSN (0xbf00),             /* nop */
2559   DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd  R_ARM_ABS32(X) */
2560 };
2561
2562 /* Thumb -> Thumb long branch stub in thumb2 encoding.  Used on armv7.  */
2563 static const insn_sequence elf32_arm_stub_long_branch_thumb2_only[] =
2564 {
2565   THUMB32_INSN (0xf85ff000),         /* ldr.w  pc, [pc, #-0] */
2566   DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd  R_ARM_ABS32(x) */
2567 };
2568
2569 /* Thumb -> Thumb long branch stub. Used for PureCode sections on Thumb2
2570    M-profile architectures.  */
2571 static const insn_sequence elf32_arm_stub_long_branch_thumb2_only_pure[] =
2572 {
2573   THUMB32_MOVW (0xf2400c00),         /* mov.w ip, R_ARM_MOVW_ABS_NC */
2574   THUMB32_MOVT (0xf2c00c00),         /* movt  ip, R_ARM_MOVT_ABS << 16 */
2575   THUMB16_INSN (0x4760),             /* bx   ip */
2576 };
2577
2578 /* V4T Thumb -> Thumb long branch stub. Using the stack is not
2579    allowed.  */
2580 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
2581 {
2582   THUMB16_INSN (0x4778),             /* bx   pc */
2583   THUMB16_INSN (0x46c0),             /* nop */
2584   ARM_INSN (0xe59fc000),             /* ldr  ip, [pc, #0] */
2585   ARM_INSN (0xe12fff1c),             /* bx   ip */
2586   DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd  R_ARM_ABS32(X) */
2587 };
2588
2589 /* V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2590    available.  */
2591 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_arm[] =
2592 {
2593   THUMB16_INSN (0x4778),             /* bx   pc */
2594   THUMB16_INSN (0x46c0),             /* nop   */
2595   ARM_INSN (0xe51ff004),             /* ldr   pc, [pc, #-4] */
2596   DATA_WORD (0, R_ARM_ABS32, 0),     /* dcd   R_ARM_ABS32(X) */
2597 };
2598
2599 /* V4T Thumb -> ARM short branch stub. Shorter variant of the above
2600    one, when the destination is close enough.  */
2601 static const insn_sequence elf32_arm_stub_short_branch_v4t_thumb_arm[] =
2602 {
2603   THUMB16_INSN (0x4778),             /* bx   pc */
2604   THUMB16_INSN (0x46c0),             /* nop   */
2605   ARM_REL_INSN (0xea000000, -8),     /* b    (X-8) */
2606 };
2607
2608 /* ARM/Thumb -> ARM long branch stub, PIC.  On V5T and above, use
2609    blx to reach the stub if necessary.  */
2610 static const insn_sequence elf32_arm_stub_long_branch_any_arm_pic[] =
2611 {
2612   ARM_INSN (0xe59fc000),             /* ldr   ip, [pc] */
2613   ARM_INSN (0xe08ff00c),             /* add   pc, pc, ip */
2614   DATA_WORD (0, R_ARM_REL32, -4),    /* dcd   R_ARM_REL32(X-4) */
2615 };
2616
2617 /* ARM/Thumb -> Thumb long branch stub, PIC.  On V5T and above, use
2618    blx to reach the stub if necessary.  We can not add into pc;
2619    it is not guaranteed to mode switch (different in ARMv6 and
2620    ARMv7).  */
2621 static const insn_sequence elf32_arm_stub_long_branch_any_thumb_pic[] =
2622 {
2623   ARM_INSN (0xe59fc004),             /* ldr   ip, [pc, #4] */
2624   ARM_INSN (0xe08fc00c),             /* add   ip, pc, ip */
2625   ARM_INSN (0xe12fff1c),             /* bx    ip */
2626   DATA_WORD (0, R_ARM_REL32, 0),     /* dcd   R_ARM_REL32(X) */
2627 };
2628
2629 /* V4T ARM -> ARM long branch stub, PIC.  */
2630 static const insn_sequence elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
2631 {
2632   ARM_INSN (0xe59fc004),             /* ldr   ip, [pc, #4] */
2633   ARM_INSN (0xe08fc00c),             /* add   ip, pc, ip */
2634   ARM_INSN (0xe12fff1c),             /* bx    ip */
2635   DATA_WORD (0, R_ARM_REL32, 0),     /* dcd   R_ARM_REL32(X) */
2636 };
2637
2638 /* V4T Thumb -> ARM long branch stub, PIC.  */
2639 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
2640 {
2641   THUMB16_INSN (0x4778),             /* bx   pc */
2642   THUMB16_INSN (0x46c0),             /* nop  */
2643   ARM_INSN (0xe59fc000),             /* ldr  ip, [pc, #0] */
2644   ARM_INSN (0xe08cf00f),             /* add  pc, ip, pc */
2645   DATA_WORD (0, R_ARM_REL32, -4),     /* dcd  R_ARM_REL32(X) */
2646 };
2647
2648 /* Thumb -> Thumb long branch stub, PIC. Used on M-profile
2649    architectures.  */
2650 static const insn_sequence elf32_arm_stub_long_branch_thumb_only_pic[] =
2651 {
2652   THUMB16_INSN (0xb401),             /* push {r0} */
2653   THUMB16_INSN (0x4802),             /* ldr  r0, [pc, #8] */
2654   THUMB16_INSN (0x46fc),             /* mov  ip, pc */
2655   THUMB16_INSN (0x4484),             /* add  ip, r0 */
2656   THUMB16_INSN (0xbc01),             /* pop  {r0} */
2657   THUMB16_INSN (0x4760),             /* bx   ip */
2658   DATA_WORD (0, R_ARM_REL32, 4),     /* dcd  R_ARM_REL32(X) */
2659 };
2660
2661 /* V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
2662    allowed.  */
2663 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
2664 {
2665   THUMB16_INSN (0x4778),             /* bx   pc */
2666   THUMB16_INSN (0x46c0),             /* nop */
2667   ARM_INSN (0xe59fc004),             /* ldr  ip, [pc, #4] */
2668   ARM_INSN (0xe08fc00c),             /* add   ip, pc, ip */
2669   ARM_INSN (0xe12fff1c),             /* bx   ip */
2670   DATA_WORD (0, R_ARM_REL32, 0),     /* dcd  R_ARM_REL32(X) */
2671 };
2672
2673 /* Thumb2/ARM -> TLS trampoline.  Lowest common denominator, which is a
2674    long PIC stub.  We can use r1 as a scratch -- and cannot use ip.  */
2675 static const insn_sequence elf32_arm_stub_long_branch_any_tls_pic[] =
2676 {
2677   ARM_INSN (0xe59f1000),             /* ldr   r1, [pc] */
2678   ARM_INSN (0xe08ff001),             /* add   pc, pc, r1 */
2679   DATA_WORD (0, R_ARM_REL32, -4),    /* dcd   R_ARM_REL32(X-4) */
2680 };
2681
2682 /* V4T Thumb -> TLS trampoline.  lowest common denominator, which is a
2683    long PIC stub.  We can use r1 as a scratch -- and cannot use ip.  */
2684 static const insn_sequence elf32_arm_stub_long_branch_v4t_thumb_tls_pic[] =
2685 {
2686   THUMB16_INSN (0x4778),             /* bx   pc */
2687   THUMB16_INSN (0x46c0),             /* nop */
2688   ARM_INSN (0xe59f1000),             /* ldr  r1, [pc, #0] */
2689   ARM_INSN (0xe081f00f),             /* add  pc, r1, pc */
2690   DATA_WORD (0, R_ARM_REL32, -4),    /* dcd  R_ARM_REL32(X) */
2691 };
2692
2693 /* NaCl ARM -> ARM long branch stub.  */
2694 static const insn_sequence elf32_arm_stub_long_branch_arm_nacl[] =
2695 {
2696   ARM_INSN (0xe59fc00c),                /* ldr  ip, [pc, #12] */
2697   ARM_INSN (0xe3ccc13f),                /* bic  ip, ip, #0xc000000f */
2698   ARM_INSN (0xe12fff1c),                /* bx   ip */
2699   ARM_INSN (0xe320f000),                /* nop */
2700   ARM_INSN (0xe125be70),                /* bkpt 0x5be0 */
2701   DATA_WORD (0, R_ARM_ABS32, 0),        /* dcd  R_ARM_ABS32(X) */
2702   DATA_WORD (0, R_ARM_NONE, 0),         /* .word 0 */
2703   DATA_WORD (0, R_ARM_NONE, 0),         /* .word 0 */
2704 };
2705
2706 /* NaCl ARM -> ARM long branch stub, PIC.  */
2707 static const insn_sequence elf32_arm_stub_long_branch_arm_nacl_pic[] =
2708 {
2709   ARM_INSN (0xe59fc00c),                /* ldr  ip, [pc, #12] */
2710   ARM_INSN (0xe08cc00f),                /* add  ip, ip, pc */
2711   ARM_INSN (0xe3ccc13f),                /* bic  ip, ip, #0xc000000f */
2712   ARM_INSN (0xe12fff1c),                /* bx   ip */
2713   ARM_INSN (0xe125be70),                /* bkpt 0x5be0 */
2714   DATA_WORD (0, R_ARM_REL32, 8),        /* dcd  R_ARM_REL32(X+8) */
2715   DATA_WORD (0, R_ARM_NONE, 0),         /* .word 0 */
2716   DATA_WORD (0, R_ARM_NONE, 0),         /* .word 0 */
2717 };
2718
2719 /* Stub used for transition to secure state (aka SG veneer).  */
2720 static const insn_sequence elf32_arm_stub_cmse_branch_thumb_only[] =
2721 {
2722   THUMB32_INSN (0xe97fe97f),            /* sg.  */
2723   THUMB32_B_INSN (0xf000b800, -4),      /* b.w original_branch_dest.  */
2724 };
2725
2726
2727 /* Cortex-A8 erratum-workaround stubs.  */
2728
2729 /* Stub used for conditional branches (which may be beyond +/-1MB away, so we
2730    can't use a conditional branch to reach this stub).  */
2731
2732 static const insn_sequence elf32_arm_stub_a8_veneer_b_cond[] =
2733 {
2734   THUMB16_BCOND_INSN (0xd001),         /* b<cond>.n true.  */
2735   THUMB32_B_INSN (0xf000b800, -4),     /* b.w insn_after_original_branch.  */
2736   THUMB32_B_INSN (0xf000b800, -4)      /* true: b.w original_branch_dest.  */
2737 };
2738
2739 /* Stub used for b.w and bl.w instructions.  */
2740
2741 static const insn_sequence elf32_arm_stub_a8_veneer_b[] =
2742 {
2743   THUMB32_B_INSN (0xf000b800, -4)       /* b.w original_branch_dest.  */
2744 };
2745
2746 static const insn_sequence elf32_arm_stub_a8_veneer_bl[] =
2747 {
2748   THUMB32_B_INSN (0xf000b800, -4)       /* b.w original_branch_dest.  */
2749 };
2750
2751 /* Stub used for Thumb-2 blx.w instructions.  We modified the original blx.w
2752    instruction (which switches to ARM mode) to point to this stub.  Jump to the
2753    real destination using an ARM-mode branch.  */
2754
2755 static const insn_sequence elf32_arm_stub_a8_veneer_blx[] =
2756 {
2757   ARM_REL_INSN (0xea000000, -8) /* b original_branch_dest.  */
2758 };
2759
2760 /* For each section group there can be a specially created linker section
2761    to hold the stubs for that group.  The name of the stub section is based
2762    upon the name of another section within that group with the suffix below
2763    applied.
2764
2765    PR 13049: STUB_SUFFIX used to be ".stub", but this allowed the user to
2766    create what appeared to be a linker stub section when it actually
2767    contained user code/data.  For example, consider this fragment:
2768
2769      const char * stubborn_problems[] = { "np" };
2770
2771    If this is compiled with "-fPIC -fdata-sections" then gcc produces a
2772    section called:
2773
2774      .data.rel.local.stubborn_problems
2775
2776    This then causes problems in arm32_arm_build_stubs() as it triggers:
2777
2778       // Ignore non-stub sections.
2779       if (!strstr (stub_sec->name, STUB_SUFFIX))
2780         continue;
2781
2782    And so the section would be ignored instead of being processed.  Hence
2783    the change in definition of STUB_SUFFIX to a name that cannot be a valid
2784    C identifier.  */
2785 #define STUB_SUFFIX ".__stub"
2786
2787 /* One entry per long/short branch stub defined above.  */
2788 #define DEF_STUBS \
2789   DEF_STUB(long_branch_any_any) \
2790   DEF_STUB(long_branch_v4t_arm_thumb) \
2791   DEF_STUB(long_branch_thumb_only) \
2792   DEF_STUB(long_branch_v4t_thumb_thumb) \
2793   DEF_STUB(long_branch_v4t_thumb_arm) \
2794   DEF_STUB(short_branch_v4t_thumb_arm) \
2795   DEF_STUB(long_branch_any_arm_pic) \
2796   DEF_STUB(long_branch_any_thumb_pic) \
2797   DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
2798   DEF_STUB(long_branch_v4t_arm_thumb_pic) \
2799   DEF_STUB(long_branch_v4t_thumb_arm_pic) \
2800   DEF_STUB(long_branch_thumb_only_pic) \
2801   DEF_STUB(long_branch_any_tls_pic) \
2802   DEF_STUB(long_branch_v4t_thumb_tls_pic) \
2803   DEF_STUB(long_branch_arm_nacl) \
2804   DEF_STUB(long_branch_arm_nacl_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   ((_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   bfd_boolean 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   unsigned int funcdesc_cnt;
3095   unsigned int gotofffuncdesc_cnt;
3096   int funcdesc_offset;
3097 };
3098
3099 struct elf_arm_obj_tdata
3100 {
3101   struct elf_obj_tdata root;
3102
3103   /* tls_type for each local got entry.  */
3104   char *local_got_tls_type;
3105
3106   /* GOTPLT entries for TLS descriptors.  */
3107   bfd_vma *local_tlsdesc_gotent;
3108
3109   /* Information for local symbols that need entries in .iplt.  */
3110   struct arm_local_iplt_info **local_iplt;
3111
3112   /* Zero to warn when linking objects with incompatible enum sizes.  */
3113   int no_enum_size_warning;
3114
3115   /* Zero to warn when linking objects with incompatible wchar_t sizes.  */
3116   int no_wchar_size_warning;
3117
3118   /* Maintains FDPIC counters and funcdesc info.  */
3119   struct fdpic_local *local_fdpic_cnts;
3120 };
3121
3122 #define elf_arm_tdata(bfd) \
3123   ((struct elf_arm_obj_tdata *) (bfd)->tdata.any)
3124
3125 #define elf32_arm_local_got_tls_type(bfd) \
3126   (elf_arm_tdata (bfd)->local_got_tls_type)
3127
3128 #define elf32_arm_local_tlsdesc_gotent(bfd) \
3129   (elf_arm_tdata (bfd)->local_tlsdesc_gotent)
3130
3131 #define elf32_arm_local_iplt(bfd) \
3132   (elf_arm_tdata (bfd)->local_iplt)
3133
3134 #define elf32_arm_local_fdpic_cnts(bfd) \
3135   (elf_arm_tdata (bfd)->local_fdpic_cnts)
3136
3137 #define is_arm_elf(bfd) \
3138   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
3139    && elf_tdata (bfd) != NULL \
3140    && elf_object_id (bfd) == ARM_ELF_DATA)
3141
3142 static bfd_boolean
3143 elf32_arm_mkobject (bfd *abfd)
3144 {
3145   return bfd_elf_allocate_object (abfd, sizeof (struct elf_arm_obj_tdata),
3146                                   ARM_ELF_DATA);
3147 }
3148
3149 #define elf32_arm_hash_entry(ent) ((struct elf32_arm_link_hash_entry *)(ent))
3150
3151 /* Structure to handle FDPIC support for extern functions.  */
3152 struct fdpic_global {
3153   unsigned int gotofffuncdesc_cnt;
3154   unsigned int gotfuncdesc_cnt;
3155   unsigned int funcdesc_cnt;
3156   int funcdesc_offset;
3157   int gotfuncdesc_offset;
3158 };
3159
3160 /* Arm ELF linker hash entry.  */
3161 struct elf32_arm_link_hash_entry
3162 {
3163   struct elf_link_hash_entry root;
3164
3165   /* Track dynamic relocs copied for this symbol.  */
3166   struct elf_dyn_relocs *dyn_relocs;
3167
3168   /* ARM-specific PLT information.  */
3169   struct arm_plt_info plt;
3170
3171 #define GOT_UNKNOWN     0
3172 #define GOT_NORMAL      1
3173 #define GOT_TLS_GD      2
3174 #define GOT_TLS_IE      4
3175 #define GOT_TLS_GDESC   8
3176 #define GOT_TLS_GD_ANY_P(type)  ((type & GOT_TLS_GD) || (type & GOT_TLS_GDESC))
3177   unsigned int tls_type : 8;
3178
3179   /* True if the symbol's PLT entry is in .iplt rather than .plt.  */
3180   unsigned int is_iplt : 1;
3181
3182   unsigned int unused : 23;
3183
3184   /* Offset of the GOTPLT entry reserved for the TLS descriptor,
3185      starting at the end of the jump table.  */
3186   bfd_vma tlsdesc_got;
3187
3188   /* The symbol marking the real symbol location for exported thumb
3189      symbols with Arm stubs.  */
3190   struct elf_link_hash_entry *export_glue;
3191
3192   /* A pointer to the most recently used stub hash entry against this
3193      symbol.  */
3194   struct elf32_arm_stub_hash_entry *stub_cache;
3195
3196   /* Counter for FDPIC relocations against this symbol.  */
3197   struct fdpic_global fdpic_cnts;
3198 };
3199
3200 /* Traverse an arm ELF linker hash table.  */
3201 #define elf32_arm_link_hash_traverse(table, func, info)                 \
3202   (elf_link_hash_traverse                                               \
3203    (&(table)->root,                                                     \
3204     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),    \
3205     (info)))
3206
3207 /* Get the ARM elf linker hash table from a link_info structure.  */
3208 #define elf32_arm_hash_table(info) \
3209   (elf_hash_table_id ((struct elf_link_hash_table *) ((info)->hash)) \
3210   == ARM_ELF_DATA ? ((struct elf32_arm_link_hash_table *) ((info)->hash)) : NULL)
3211
3212 #define arm_stub_hash_lookup(table, string, create, copy) \
3213   ((struct elf32_arm_stub_hash_entry *) \
3214    bfd_hash_lookup ((table), (string), (create), (copy)))
3215
3216 /* Array to keep track of which stub sections have been created, and
3217    information on stub grouping.  */
3218 struct map_stub
3219 {
3220   /* This is the section to which stubs in the group will be
3221      attached.  */
3222   asection *link_sec;
3223   /* The stub section.  */
3224   asection *stub_sec;
3225 };
3226
3227 #define elf32_arm_compute_jump_table_size(htab) \
3228   ((htab)->next_tls_desc_index * 4)
3229
3230 /* ARM ELF linker hash table.  */
3231 struct elf32_arm_link_hash_table
3232 {
3233   /* The main hash table.  */
3234   struct elf_link_hash_table root;
3235
3236   /* The size in bytes of the section containing the Thumb-to-ARM glue.  */
3237   bfd_size_type thumb_glue_size;
3238
3239   /* The size in bytes of the section containing the ARM-to-Thumb glue.  */
3240   bfd_size_type arm_glue_size;
3241
3242   /* The size in bytes of section containing the ARMv4 BX veneers.  */
3243   bfd_size_type bx_glue_size;
3244
3245   /* Offsets of ARMv4 BX veneers.  Bit1 set if present, and Bit0 set when
3246      veneer has been populated.  */
3247   bfd_vma bx_glue_offset[15];
3248
3249   /* The size in bytes of the section containing glue for VFP11 erratum
3250      veneers.  */
3251   bfd_size_type vfp11_erratum_glue_size;
3252
3253  /* The size in bytes of the section containing glue for STM32L4XX erratum
3254      veneers.  */
3255   bfd_size_type stm32l4xx_erratum_glue_size;
3256
3257   /* A table of fix locations for Cortex-A8 Thumb-2 branch/TLB erratum.  This
3258      holds Cortex-A8 erratum fix locations between elf32_arm_size_stubs() and
3259      elf32_arm_write_section().  */
3260   struct a8_erratum_fix *a8_erratum_fixes;
3261   unsigned int num_a8_erratum_fixes;
3262
3263   /* An arbitrary input BFD chosen to hold the glue sections.  */
3264   bfd * bfd_of_glue_owner;
3265
3266   /* Nonzero to output a BE8 image.  */
3267   int byteswap_code;
3268
3269   /* Zero if R_ARM_TARGET1 means R_ARM_ABS32.
3270      Nonzero if R_ARM_TARGET1 means R_ARM_REL32.  */
3271   int target1_is_rel;
3272
3273   /* The relocation to use for R_ARM_TARGET2 relocations.  */
3274   int target2_reloc;
3275
3276   /* 0 = Ignore R_ARM_V4BX.
3277      1 = Convert BX to MOV PC.
3278      2 = Generate v4 interworing stubs.  */
3279   int fix_v4bx;
3280
3281   /* Whether we should fix the Cortex-A8 Thumb-2 branch/TLB erratum.  */
3282   int fix_cortex_a8;
3283
3284   /* Whether we should fix the ARM1176 BLX immediate issue.  */
3285   int fix_arm1176;
3286
3287   /* Nonzero if the ARM/Thumb BLX instructions are available for use.  */
3288   int use_blx;
3289
3290   /* What sort of code sequences we should look for which may trigger the
3291      VFP11 denorm erratum.  */
3292   bfd_arm_vfp11_fix vfp11_fix;
3293
3294   /* Global counter for the number of fixes we have emitted.  */
3295   int num_vfp11_fixes;
3296
3297   /* What sort of code sequences we should look for which may trigger the
3298      STM32L4XX erratum.  */
3299   bfd_arm_stm32l4xx_fix stm32l4xx_fix;
3300
3301   /* Global counter for the number of fixes we have emitted.  */
3302   int num_stm32l4xx_fixes;
3303
3304   /* Nonzero to force PIC branch veneers.  */
3305   int pic_veneer;
3306
3307   /* The number of bytes in the initial entry in the PLT.  */
3308   bfd_size_type plt_header_size;
3309
3310   /* The number of bytes in the subsequent PLT etries.  */
3311   bfd_size_type plt_entry_size;
3312
3313   /* True if the target system is VxWorks.  */
3314   int vxworks_p;
3315
3316   /* True if the target system is Symbian OS.  */
3317   int symbian_p;
3318
3319   /* True if the target system is Native Client.  */
3320   int nacl_p;
3321
3322   /* True if the target uses REL relocations.  */
3323   bfd_boolean use_rel;
3324
3325   /* Nonzero if import library must be a secure gateway import library
3326      as per ARMv8-M Security Extensions.  */
3327   int cmse_implib;
3328
3329   /* The import library whose symbols' address must remain stable in
3330      the import library generated.  */
3331   bfd *in_implib_bfd;
3332
3333   /* The index of the next unused R_ARM_TLS_DESC slot in .rel.plt.  */
3334   bfd_vma next_tls_desc_index;
3335
3336   /* How many R_ARM_TLS_DESC relocations were generated so far.  */
3337   bfd_vma num_tls_desc;
3338
3339   /* The (unloaded but important) VxWorks .rela.plt.unloaded section.  */
3340   asection *srelplt2;
3341
3342   /* The offset into splt of the PLT entry for the TLS descriptor
3343      resolver.  Special values are 0, if not necessary (or not found
3344      to be necessary yet), and -1 if needed but not determined
3345      yet.  */
3346   bfd_vma dt_tlsdesc_plt;
3347
3348   /* The offset into sgot of the GOT entry used by the PLT entry
3349      above.  */
3350   bfd_vma dt_tlsdesc_got;
3351
3352   /* Offset in .plt section of tls_arm_trampoline.  */
3353   bfd_vma tls_trampoline;
3354
3355   /* Data for R_ARM_TLS_LDM32/R_ARM_TLS_LDM32_FDPIC relocations.  */
3356   union
3357   {
3358     bfd_signed_vma refcount;
3359     bfd_vma offset;
3360   } tls_ldm_got;
3361
3362   /* Small local sym cache.  */
3363   struct sym_cache sym_cache;
3364
3365   /* For convenience in allocate_dynrelocs.  */
3366   bfd * obfd;
3367
3368   /* The amount of space used by the reserved portion of the sgotplt
3369      section, plus whatever space is used by the jump slots.  */
3370   bfd_vma sgotplt_jump_table_size;
3371
3372   /* The stub hash table.  */
3373   struct bfd_hash_table stub_hash_table;
3374
3375   /* Linker stub bfd.  */
3376   bfd *stub_bfd;
3377
3378   /* Linker call-backs.  */
3379   asection * (*add_stub_section) (const char *, asection *, asection *,
3380                                   unsigned int);
3381   void (*layout_sections_again) (void);
3382
3383   /* Array to keep track of which stub sections have been created, and
3384      information on stub grouping.  */
3385   struct map_stub *stub_group;
3386
3387   /* Input stub section holding secure gateway veneers.  */
3388   asection *cmse_stub_sec;
3389
3390   /* Offset in cmse_stub_sec where new SG veneers (not in input import library)
3391      start to be allocated.  */
3392   bfd_vma new_cmse_stub_offset;
3393
3394   /* Number of elements in stub_group.  */
3395   unsigned int top_id;
3396
3397   /* Assorted information used by elf32_arm_size_stubs.  */
3398   unsigned int bfd_count;
3399   unsigned int top_index;
3400   asection **input_list;
3401
3402   /* True if the target system uses FDPIC. */
3403   int fdpic_p;
3404
3405   /* Fixup section. Used for FDPIC.  */
3406   asection *srofixup;
3407 };
3408
3409 /* Add an FDPIC read-only fixup.  */
3410 static void
3411 arm_elf_add_rofixup (bfd *output_bfd, asection *srofixup, bfd_vma offset)
3412 {
3413   bfd_vma fixup_offset;
3414
3415   fixup_offset = srofixup->reloc_count++ * 4;
3416   BFD_ASSERT (fixup_offset < srofixup->size);
3417   bfd_put_32 (output_bfd, offset, srofixup->contents + fixup_offset);
3418 }
3419
3420 static inline int
3421 ctz (unsigned int mask)
3422 {
3423 #if GCC_VERSION >= 3004
3424   return __builtin_ctz (mask);
3425 #else
3426   unsigned int i;
3427
3428   for (i = 0; i < 8 * sizeof (mask); i++)
3429     {
3430       if (mask & 0x1)
3431         break;
3432       mask = (mask >> 1);
3433     }
3434   return i;
3435 #endif
3436 }
3437
3438 static inline int
3439 elf32_arm_popcount (unsigned int mask)
3440 {
3441 #if GCC_VERSION >= 3004
3442   return __builtin_popcount (mask);
3443 #else
3444   unsigned int i;
3445   int sum = 0;
3446
3447   for (i = 0; i < 8 * sizeof (mask); i++)
3448     {
3449       if (mask & 0x1)
3450         sum++;
3451       mask = (mask >> 1);
3452     }
3453   return sum;
3454 #endif
3455 }
3456
3457 static void elf32_arm_add_dynreloc (bfd *output_bfd, struct bfd_link_info *info,
3458                                     asection *sreloc, Elf_Internal_Rela *rel);
3459
3460 static void
3461 arm_elf_fill_funcdesc(bfd *output_bfd,
3462                       struct bfd_link_info *info,
3463                       int *funcdesc_offset,
3464                       int dynindx,
3465                       int offset,
3466                       bfd_vma addr,
3467                       bfd_vma dynreloc_value,
3468                       bfd_vma seg)
3469 {
3470   if ((*funcdesc_offset & 1) == 0)
3471     {
3472       struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
3473       asection *sgot = globals->root.sgot;
3474
3475       if (bfd_link_pic(info))
3476         {
3477           asection *srelgot = globals->root.srelgot;
3478           Elf_Internal_Rela outrel;
3479
3480           outrel.r_info = ELF32_R_INFO (dynindx, R_ARM_FUNCDESC_VALUE);
3481           outrel.r_offset = sgot->output_section->vma + sgot->output_offset + offset;
3482           outrel.r_addend = 0;
3483
3484           elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
3485           bfd_put_32 (output_bfd, addr, sgot->contents + offset);
3486           bfd_put_32 (output_bfd, seg, sgot->contents + offset + 4);
3487         }
3488       else
3489         {
3490           struct elf_link_hash_entry *hgot = globals->root.hgot;
3491           bfd_vma got_value = hgot->root.u.def.value
3492             + hgot->root.u.def.section->output_section->vma
3493             + hgot->root.u.def.section->output_offset;
3494
3495           arm_elf_add_rofixup(output_bfd, globals->srofixup,
3496                               sgot->output_section->vma + sgot->output_offset
3497                               + offset);
3498           arm_elf_add_rofixup(output_bfd, globals->srofixup,
3499                               sgot->output_section->vma + sgot->output_offset
3500                               + offset + 4);
3501           bfd_put_32 (output_bfd, dynreloc_value, sgot->contents + offset);
3502           bfd_put_32 (output_bfd, got_value, sgot->contents + offset + 4);
3503         }
3504       *funcdesc_offset |= 1;
3505     }
3506 }
3507
3508 /* Create an entry in an ARM ELF linker hash table.  */
3509
3510 static struct bfd_hash_entry *
3511 elf32_arm_link_hash_newfunc (struct bfd_hash_entry * entry,
3512                              struct bfd_hash_table * table,
3513                              const char * string)
3514 {
3515   struct elf32_arm_link_hash_entry * ret =
3516     (struct elf32_arm_link_hash_entry *) entry;
3517
3518   /* Allocate the structure if it has not already been allocated by a
3519      subclass.  */
3520   if (ret == NULL)
3521     ret = (struct elf32_arm_link_hash_entry *)
3522         bfd_hash_allocate (table, sizeof (struct elf32_arm_link_hash_entry));
3523   if (ret == NULL)
3524     return (struct bfd_hash_entry *) ret;
3525
3526   /* Call the allocation method of the superclass.  */
3527   ret = ((struct elf32_arm_link_hash_entry *)
3528          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3529                                      table, string));
3530   if (ret != NULL)
3531     {
3532       ret->dyn_relocs = NULL;
3533       ret->tls_type = GOT_UNKNOWN;
3534       ret->tlsdesc_got = (bfd_vma) -1;
3535       ret->plt.thumb_refcount = 0;
3536       ret->plt.maybe_thumb_refcount = 0;
3537       ret->plt.noncall_refcount = 0;
3538       ret->plt.got_offset = -1;
3539       ret->is_iplt = FALSE;
3540       ret->export_glue = NULL;
3541
3542       ret->stub_cache = NULL;
3543
3544       ret->fdpic_cnts.gotofffuncdesc_cnt = 0;
3545       ret->fdpic_cnts.gotfuncdesc_cnt = 0;
3546       ret->fdpic_cnts.funcdesc_cnt = 0;
3547       ret->fdpic_cnts.funcdesc_offset = -1;
3548       ret->fdpic_cnts.gotfuncdesc_offset = -1;
3549     }
3550
3551   return (struct bfd_hash_entry *) ret;
3552 }
3553
3554 /* Ensure that we have allocated bookkeeping structures for ABFD's local
3555    symbols.  */
3556
3557 static bfd_boolean
3558 elf32_arm_allocate_local_sym_info (bfd *abfd)
3559 {
3560   if (elf_local_got_refcounts (abfd) == NULL)
3561     {
3562       bfd_size_type num_syms;
3563       bfd_size_type size;
3564       char *data;
3565
3566       num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
3567       size = num_syms * (sizeof (bfd_signed_vma)
3568                          + sizeof (struct arm_local_iplt_info *)
3569                          + sizeof (bfd_vma)
3570                          + sizeof (char)
3571                          + sizeof (struct fdpic_local));
3572       data = bfd_zalloc (abfd, size);
3573       if (data == NULL)
3574         return FALSE;
3575
3576       elf32_arm_local_fdpic_cnts (abfd) = (struct fdpic_local *) data;
3577       data += num_syms * sizeof (struct fdpic_local);
3578
3579       elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
3580       data += num_syms * sizeof (bfd_signed_vma);
3581
3582       elf32_arm_local_iplt (abfd) = (struct arm_local_iplt_info **) data;
3583       data += num_syms * sizeof (struct arm_local_iplt_info *);
3584
3585       elf32_arm_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
3586       data += num_syms * sizeof (bfd_vma);
3587
3588       elf32_arm_local_got_tls_type (abfd) = data;
3589     }
3590   return TRUE;
3591 }
3592
3593 /* Return the .iplt information for local symbol R_SYMNDX, which belongs
3594    to input bfd ABFD.  Create the information if it doesn't already exist.
3595    Return null if an allocation fails.  */
3596
3597 static struct arm_local_iplt_info *
3598 elf32_arm_create_local_iplt (bfd *abfd, unsigned long r_symndx)
3599 {
3600   struct arm_local_iplt_info **ptr;
3601
3602   if (!elf32_arm_allocate_local_sym_info (abfd))
3603     return NULL;
3604
3605   BFD_ASSERT (r_symndx < elf_tdata (abfd)->symtab_hdr.sh_info);
3606   ptr = &elf32_arm_local_iplt (abfd)[r_symndx];
3607   if (*ptr == NULL)
3608     *ptr = bfd_zalloc (abfd, sizeof (**ptr));
3609   return *ptr;
3610 }
3611
3612 /* Try to obtain PLT information for the symbol with index R_SYMNDX
3613    in ABFD's symbol table.  If the symbol is global, H points to its
3614    hash table entry, otherwise H is null.
3615
3616    Return true if the symbol does have PLT information.  When returning
3617    true, point *ROOT_PLT at the target-independent reference count/offset
3618    union and *ARM_PLT at the ARM-specific information.  */
3619
3620 static bfd_boolean
3621 elf32_arm_get_plt_info (bfd *abfd, struct elf32_arm_link_hash_table *globals,
3622                         struct elf32_arm_link_hash_entry *h,
3623                         unsigned long r_symndx, union gotplt_union **root_plt,
3624                         struct arm_plt_info **arm_plt)
3625 {
3626   struct arm_local_iplt_info *local_iplt;
3627
3628   if (globals->root.splt == NULL && globals->root.iplt == NULL)
3629     return FALSE;
3630
3631   if (h != NULL)
3632     {
3633       *root_plt = &h->root.plt;
3634       *arm_plt = &h->plt;
3635       return TRUE;
3636     }
3637
3638   if (elf32_arm_local_iplt (abfd) == NULL)
3639     return FALSE;
3640
3641   local_iplt = elf32_arm_local_iplt (abfd)[r_symndx];
3642   if (local_iplt == NULL)
3643     return FALSE;
3644
3645   *root_plt = &local_iplt->root;
3646   *arm_plt = &local_iplt->arm;
3647   return TRUE;
3648 }
3649
3650 static bfd_boolean using_thumb_only (struct elf32_arm_link_hash_table *globals);
3651
3652 /* Return true if the PLT described by ARM_PLT requires a Thumb stub
3653    before it.  */
3654
3655 static bfd_boolean
3656 elf32_arm_plt_needs_thumb_stub_p (struct bfd_link_info *info,
3657                                   struct arm_plt_info *arm_plt)
3658 {
3659   struct elf32_arm_link_hash_table *htab;
3660
3661   htab = elf32_arm_hash_table (info);
3662
3663   return (!using_thumb_only(htab) && (arm_plt->thumb_refcount != 0
3664           || (!htab->use_blx && arm_plt->maybe_thumb_refcount != 0)));
3665 }
3666
3667 /* Return a pointer to the head of the dynamic reloc list that should
3668    be used for local symbol ISYM, which is symbol number R_SYMNDX in
3669    ABFD's symbol table.  Return null if an error occurs.  */
3670
3671 static struct elf_dyn_relocs **
3672 elf32_arm_get_local_dynreloc_list (bfd *abfd, unsigned long r_symndx,
3673                                    Elf_Internal_Sym *isym)
3674 {
3675   if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
3676     {
3677       struct arm_local_iplt_info *local_iplt;
3678
3679       local_iplt = elf32_arm_create_local_iplt (abfd, r_symndx);
3680       if (local_iplt == NULL)
3681         return NULL;
3682       return &local_iplt->dyn_relocs;
3683     }
3684   else
3685     {
3686       /* Track dynamic relocs needed for local syms too.
3687          We really need local syms available to do this
3688          easily.  Oh well.  */
3689       asection *s;
3690       void *vpp;
3691
3692       s = bfd_section_from_elf_index (abfd, isym->st_shndx);
3693       if (s == NULL)
3694         abort ();
3695
3696       vpp = &elf_section_data (s)->local_dynrel;
3697       return (struct elf_dyn_relocs **) vpp;
3698     }
3699 }
3700
3701 /* Initialize an entry in the stub hash table.  */
3702
3703 static struct bfd_hash_entry *
3704 stub_hash_newfunc (struct bfd_hash_entry *entry,
3705                    struct bfd_hash_table *table,
3706                    const char *string)
3707 {
3708   /* Allocate the structure if it has not already been allocated by a
3709      subclass.  */
3710   if (entry == NULL)
3711     {
3712       entry = (struct bfd_hash_entry *)
3713           bfd_hash_allocate (table, sizeof (struct elf32_arm_stub_hash_entry));
3714       if (entry == NULL)
3715         return entry;
3716     }
3717
3718   /* Call the allocation method of the superclass.  */
3719   entry = bfd_hash_newfunc (entry, table, string);
3720   if (entry != NULL)
3721     {
3722       struct elf32_arm_stub_hash_entry *eh;
3723
3724       /* Initialize the local fields.  */
3725       eh = (struct elf32_arm_stub_hash_entry *) entry;
3726       eh->stub_sec = NULL;
3727       eh->stub_offset = (bfd_vma) -1;
3728       eh->source_value = 0;
3729       eh->target_value = 0;
3730       eh->target_section = NULL;
3731       eh->orig_insn = 0;
3732       eh->stub_type = arm_stub_none;
3733       eh->stub_size = 0;
3734       eh->stub_template = NULL;
3735       eh->stub_template_size = -1;
3736       eh->h = NULL;
3737       eh->id_sec = NULL;
3738       eh->output_name = NULL;
3739     }
3740
3741   return entry;
3742 }
3743
3744 /* Create .got, .gotplt, and .rel(a).got sections in DYNOBJ, and set up
3745    shortcuts to them in our hash table.  */
3746
3747 static bfd_boolean
3748 create_got_section (bfd *dynobj, struct bfd_link_info *info)
3749 {
3750   struct elf32_arm_link_hash_table *htab;
3751
3752   htab = elf32_arm_hash_table (info);
3753   if (htab == NULL)
3754     return FALSE;
3755
3756   /* BPABI objects never have a GOT, or associated sections.  */
3757   if (htab->symbian_p)
3758     return TRUE;
3759
3760   if (! _bfd_elf_create_got_section (dynobj, info))
3761     return FALSE;
3762
3763   /* Also create .rofixup.  */
3764   if (htab->fdpic_p)
3765     {
3766       htab->srofixup = bfd_make_section_with_flags (dynobj, ".rofixup",
3767                                                     (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3768                                                      | SEC_IN_MEMORY | SEC_LINKER_CREATED | SEC_READONLY));
3769       if (htab->srofixup == NULL || ! bfd_set_section_alignment (dynobj, htab->srofixup, 2))
3770         return FALSE;
3771     }
3772
3773   return TRUE;
3774 }
3775
3776 /* Create the .iplt, .rel(a).iplt and .igot.plt sections.  */
3777
3778 static bfd_boolean
3779 create_ifunc_sections (struct bfd_link_info *info)
3780 {
3781   struct elf32_arm_link_hash_table *htab;
3782   const struct elf_backend_data *bed;
3783   bfd *dynobj;
3784   asection *s;
3785   flagword flags;
3786
3787   htab = elf32_arm_hash_table (info);
3788   dynobj = htab->root.dynobj;
3789   bed = get_elf_backend_data (dynobj);
3790   flags = bed->dynamic_sec_flags;
3791
3792   if (htab->root.iplt == NULL)
3793     {
3794       s = bfd_make_section_anyway_with_flags (dynobj, ".iplt",
3795                                               flags | SEC_READONLY | SEC_CODE);
3796       if (s == NULL
3797           || !bfd_set_section_alignment (dynobj, s, bed->plt_alignment))
3798         return FALSE;
3799       htab->root.iplt = s;
3800     }
3801
3802   if (htab->root.irelplt == NULL)
3803     {
3804       s = bfd_make_section_anyway_with_flags (dynobj,
3805                                               RELOC_SECTION (htab, ".iplt"),
3806                                               flags | SEC_READONLY);
3807       if (s == NULL
3808           || !bfd_set_section_alignment (dynobj, s, bed->s->log_file_align))
3809         return FALSE;
3810       htab->root.irelplt = s;
3811     }
3812
3813   if (htab->root.igotplt == NULL)
3814     {
3815       s = bfd_make_section_anyway_with_flags (dynobj, ".igot.plt", flags);
3816       if (s == NULL
3817           || !bfd_set_section_alignment (dynobj, s, bed->s->log_file_align))
3818         return FALSE;
3819       htab->root.igotplt = s;
3820     }
3821   return TRUE;
3822 }
3823
3824 /* Determine if we're dealing with a Thumb only architecture.  */
3825
3826 static bfd_boolean
3827 using_thumb_only (struct elf32_arm_link_hash_table *globals)
3828 {
3829   int arch;
3830   int profile = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3831                                           Tag_CPU_arch_profile);
3832
3833   if (profile)
3834     return profile == 'M';
3835
3836   arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
3837
3838   /* Force return logic to be reviewed for each new architecture.  */
3839   BFD_ASSERT (arch <= TAG_CPU_ARCH_V8M_MAIN);
3840
3841   if (arch == TAG_CPU_ARCH_V6_M
3842       || arch == TAG_CPU_ARCH_V6S_M
3843       || arch == TAG_CPU_ARCH_V7E_M
3844       || arch == TAG_CPU_ARCH_V8M_BASE
3845       || arch == TAG_CPU_ARCH_V8M_MAIN)
3846     return TRUE;
3847
3848   return FALSE;
3849 }
3850
3851 /* Determine if we're dealing with a Thumb-2 object.  */
3852
3853 static bfd_boolean
3854 using_thumb2 (struct elf32_arm_link_hash_table *globals)
3855 {
3856   int arch;
3857   int thumb_isa = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
3858                                             Tag_THUMB_ISA_use);
3859
3860   if (thumb_isa)
3861     return thumb_isa == 2;
3862
3863   arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
3864
3865   /* Force return logic to be reviewed for each new architecture.  */
3866   BFD_ASSERT (arch <= TAG_CPU_ARCH_V8M_MAIN);
3867
3868   return (arch == TAG_CPU_ARCH_V6T2
3869           || arch == TAG_CPU_ARCH_V7
3870           || arch == TAG_CPU_ARCH_V7E_M
3871           || arch == TAG_CPU_ARCH_V8
3872           || arch == TAG_CPU_ARCH_V8R
3873           || arch == TAG_CPU_ARCH_V8M_MAIN);
3874 }
3875
3876 /* Determine whether Thumb-2 BL instruction is available.  */
3877
3878 static bfd_boolean
3879 using_thumb2_bl (struct elf32_arm_link_hash_table *globals)
3880 {
3881   int arch =
3882     bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
3883
3884   /* Force return logic to be reviewed for each new architecture.  */
3885   BFD_ASSERT (arch <= TAG_CPU_ARCH_V8M_MAIN);
3886
3887   /* Architecture was introduced after ARMv6T2 (eg. ARMv6-M).  */
3888   return (arch == TAG_CPU_ARCH_V6T2
3889           || arch >= TAG_CPU_ARCH_V7);
3890 }
3891
3892 /* Create .plt, .rel(a).plt, .got, .got.plt, .rel(a).got, .dynbss, and
3893    .rel(a).bss sections in DYNOBJ, and set up shortcuts to them in our
3894    hash table.  */
3895
3896 static bfd_boolean
3897 elf32_arm_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
3898 {
3899   struct elf32_arm_link_hash_table *htab;
3900
3901   htab = elf32_arm_hash_table (info);
3902   if (htab == NULL)
3903     return FALSE;
3904
3905   if (!htab->root.sgot && !create_got_section (dynobj, info))
3906     return FALSE;
3907
3908   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
3909     return FALSE;
3910
3911   if (htab->vxworks_p)
3912     {
3913       if (!elf_vxworks_create_dynamic_sections (dynobj, info, &htab->srelplt2))
3914         return FALSE;
3915
3916       if (bfd_link_pic (info))
3917         {
3918           htab->plt_header_size = 0;
3919           htab->plt_entry_size
3920             = 4 * ARRAY_SIZE (elf32_arm_vxworks_shared_plt_entry);
3921         }
3922       else
3923         {
3924           htab->plt_header_size
3925             = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt0_entry);
3926           htab->plt_entry_size
3927             = 4 * ARRAY_SIZE (elf32_arm_vxworks_exec_plt_entry);
3928         }
3929
3930       if (elf_elfheader (dynobj))
3931         elf_elfheader (dynobj)->e_ident[EI_CLASS] = ELFCLASS32;
3932     }
3933   else
3934     {
3935       /* PR ld/16017
3936          Test for thumb only architectures.  Note - we cannot just call
3937          using_thumb_only() as the attributes in the output bfd have not been
3938          initialised at this point, so instead we use the input bfd.  */
3939       bfd * saved_obfd = htab->obfd;
3940
3941       htab->obfd = dynobj;
3942       if (using_thumb_only (htab))
3943         {
3944           htab->plt_header_size = 4 * ARRAY_SIZE (elf32_thumb2_plt0_entry);
3945           htab->plt_entry_size  = 4 * ARRAY_SIZE (elf32_thumb2_plt_entry);
3946         }
3947       htab->obfd = saved_obfd;
3948     }
3949
3950   if (htab->fdpic_p) {
3951     htab->plt_header_size = 0;
3952     if (info->flags & DF_BIND_NOW)
3953       htab->plt_entry_size = 4 * (ARRAY_SIZE(elf32_arm_fdpic_plt_entry) - 5);
3954     else
3955       htab->plt_entry_size = 4 * ARRAY_SIZE(elf32_arm_fdpic_plt_entry);
3956   }
3957
3958   if (!htab->root.splt
3959       || !htab->root.srelplt
3960       || !htab->root.sdynbss
3961       || (!bfd_link_pic (info) && !htab->root.srelbss))
3962     abort ();
3963
3964   return TRUE;
3965 }
3966
3967 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
3968
3969 static void
3970 elf32_arm_copy_indirect_symbol (struct bfd_link_info *info,
3971                                 struct elf_link_hash_entry *dir,
3972                                 struct elf_link_hash_entry *ind)
3973 {
3974   struct elf32_arm_link_hash_entry *edir, *eind;
3975
3976   edir = (struct elf32_arm_link_hash_entry *) dir;
3977   eind = (struct elf32_arm_link_hash_entry *) ind;
3978
3979   if (eind->dyn_relocs != NULL)
3980     {
3981       if (edir->dyn_relocs != NULL)
3982         {
3983           struct elf_dyn_relocs **pp;
3984           struct elf_dyn_relocs *p;
3985
3986           /* Add reloc counts against the indirect sym to the direct sym
3987              list.  Merge any entries against the same section.  */
3988           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3989             {
3990               struct elf_dyn_relocs *q;
3991
3992               for (q = edir->dyn_relocs; q != NULL; q = q->next)
3993                 if (q->sec == p->sec)
3994                   {
3995                     q->pc_count += p->pc_count;
3996                     q->count += p->count;
3997                     *pp = p->next;
3998                     break;
3999                   }
4000               if (q == NULL)
4001                 pp = &p->next;
4002             }
4003           *pp = edir->dyn_relocs;
4004         }
4005
4006       edir->dyn_relocs = eind->dyn_relocs;
4007       eind->dyn_relocs = NULL;
4008     }
4009
4010   if (ind->root.type == bfd_link_hash_indirect)
4011     {
4012       /* Copy over PLT info.  */
4013       edir->plt.thumb_refcount += eind->plt.thumb_refcount;
4014       eind->plt.thumb_refcount = 0;
4015       edir->plt.maybe_thumb_refcount += eind->plt.maybe_thumb_refcount;
4016       eind->plt.maybe_thumb_refcount = 0;
4017       edir->plt.noncall_refcount += eind->plt.noncall_refcount;
4018       eind->plt.noncall_refcount = 0;
4019
4020       /* Copy FDPIC counters.  */
4021       edir->fdpic_cnts.gotofffuncdesc_cnt += eind->fdpic_cnts.gotofffuncdesc_cnt;
4022       edir->fdpic_cnts.gotfuncdesc_cnt += eind->fdpic_cnts.gotfuncdesc_cnt;
4023       edir->fdpic_cnts.funcdesc_cnt += eind->fdpic_cnts.funcdesc_cnt;
4024
4025       /* We should only allocate a function to .iplt once the final
4026          symbol information is known.  */
4027       BFD_ASSERT (!eind->is_iplt);
4028
4029       if (dir->got.refcount <= 0)
4030         {
4031           edir->tls_type = eind->tls_type;
4032           eind->tls_type = GOT_UNKNOWN;
4033         }
4034     }
4035
4036   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
4037 }
4038
4039 /* Destroy an ARM elf linker hash table.  */
4040
4041 static void
4042 elf32_arm_link_hash_table_free (bfd *obfd)
4043 {
4044   struct elf32_arm_link_hash_table *ret
4045     = (struct elf32_arm_link_hash_table *) obfd->link.hash;
4046
4047   bfd_hash_table_free (&ret->stub_hash_table);
4048   _bfd_elf_link_hash_table_free (obfd);
4049 }
4050
4051 /* Create an ARM elf linker hash table.  */
4052
4053 static struct bfd_link_hash_table *
4054 elf32_arm_link_hash_table_create (bfd *abfd)
4055 {
4056   struct elf32_arm_link_hash_table *ret;
4057   bfd_size_type amt = sizeof (struct elf32_arm_link_hash_table);
4058
4059   ret = (struct elf32_arm_link_hash_table *) bfd_zmalloc (amt);
4060   if (ret == NULL)
4061     return NULL;
4062
4063   if (!_bfd_elf_link_hash_table_init (& ret->root, abfd,
4064                                       elf32_arm_link_hash_newfunc,
4065                                       sizeof (struct elf32_arm_link_hash_entry),
4066                                       ARM_ELF_DATA))
4067     {
4068       free (ret);
4069       return NULL;
4070     }
4071
4072   ret->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
4073   ret->stm32l4xx_fix = BFD_ARM_STM32L4XX_FIX_NONE;
4074 #ifdef FOUR_WORD_PLT
4075   ret->plt_header_size = 16;
4076   ret->plt_entry_size = 16;
4077 #else
4078   ret->plt_header_size = 20;
4079   ret->plt_entry_size = elf32_arm_use_long_plt_entry ? 16 : 12;
4080 #endif
4081   ret->use_rel = TRUE;
4082   ret->obfd = abfd;
4083   ret->fdpic_p = 0;
4084
4085   if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
4086                             sizeof (struct elf32_arm_stub_hash_entry)))
4087     {
4088       _bfd_elf_link_hash_table_free (abfd);
4089       return NULL;
4090     }
4091   ret->root.root.hash_table_free = elf32_arm_link_hash_table_free;
4092
4093   return &ret->root.root;
4094 }
4095
4096 /* Determine what kind of NOPs are available.  */
4097
4098 static bfd_boolean
4099 arch_has_arm_nop (struct elf32_arm_link_hash_table *globals)
4100 {
4101   const int arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
4102                                              Tag_CPU_arch);
4103
4104   /* Force return logic to be reviewed for each new architecture.  */
4105   BFD_ASSERT (arch <= TAG_CPU_ARCH_V8M_MAIN);
4106
4107   return (arch == TAG_CPU_ARCH_V6T2
4108           || arch == TAG_CPU_ARCH_V6K
4109           || arch == TAG_CPU_ARCH_V7
4110           || arch == TAG_CPU_ARCH_V8
4111           || arch == TAG_CPU_ARCH_V8R);
4112 }
4113
4114 static bfd_boolean
4115 arm_stub_is_thumb (enum elf32_arm_stub_type stub_type)
4116 {
4117   switch (stub_type)
4118     {
4119     case arm_stub_long_branch_thumb_only:
4120     case arm_stub_long_branch_thumb2_only:
4121     case arm_stub_long_branch_thumb2_only_pure:
4122     case arm_stub_long_branch_v4t_thumb_arm:
4123     case arm_stub_short_branch_v4t_thumb_arm:
4124     case arm_stub_long_branch_v4t_thumb_arm_pic:
4125     case arm_stub_long_branch_v4t_thumb_tls_pic:
4126     case arm_stub_long_branch_thumb_only_pic:
4127     case arm_stub_cmse_branch_thumb_only:
4128       return TRUE;
4129     case arm_stub_none:
4130       BFD_FAIL ();
4131       return FALSE;
4132       break;
4133     default:
4134       return FALSE;
4135     }
4136 }
4137
4138 /* Determine the type of stub needed, if any, for a call.  */
4139
4140 static enum elf32_arm_stub_type
4141 arm_type_of_stub (struct bfd_link_info *info,
4142                   asection *input_sec,
4143                   const Elf_Internal_Rela *rel,
4144                   unsigned char st_type,
4145                   enum arm_st_branch_type *actual_branch_type,
4146                   struct elf32_arm_link_hash_entry *hash,
4147                   bfd_vma destination,
4148                   asection *sym_sec,
4149                   bfd *input_bfd,
4150                   const char *name)
4151 {
4152   bfd_vma location;
4153   bfd_signed_vma branch_offset;
4154   unsigned int r_type;
4155   struct elf32_arm_link_hash_table * globals;
4156   bfd_boolean thumb2, thumb2_bl, thumb_only;
4157   enum elf32_arm_stub_type stub_type = arm_stub_none;
4158   int use_plt = 0;
4159   enum arm_st_branch_type branch_type = *actual_branch_type;
4160   union gotplt_union *root_plt;
4161   struct arm_plt_info *arm_plt;
4162   int arch;
4163   int thumb2_movw;
4164
4165   if (branch_type == ST_BRANCH_LONG)
4166     return stub_type;
4167
4168   globals = elf32_arm_hash_table (info);
4169   if (globals == NULL)
4170     return stub_type;
4171
4172   thumb_only = using_thumb_only (globals);
4173   thumb2 = using_thumb2 (globals);
4174   thumb2_bl = using_thumb2_bl (globals);
4175
4176   arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC, Tag_CPU_arch);
4177
4178   /* True for architectures that implement the thumb2 movw instruction.  */
4179   thumb2_movw = thumb2 || (arch  == TAG_CPU_ARCH_V8M_BASE);
4180
4181   /* Determine where the call point is.  */
4182   location = (input_sec->output_offset
4183               + input_sec->output_section->vma
4184               + rel->r_offset);
4185
4186   r_type = ELF32_R_TYPE (rel->r_info);
4187
4188   /* ST_BRANCH_TO_ARM is nonsense to thumb-only targets when we
4189      are considering a function call relocation.  */
4190   if (thumb_only && (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24
4191                      || r_type == R_ARM_THM_JUMP19)
4192       && branch_type == ST_BRANCH_TO_ARM)
4193     branch_type = ST_BRANCH_TO_THUMB;
4194
4195   /* For TLS call relocs, it is the caller's responsibility to provide
4196      the address of the appropriate trampoline.  */
4197   if (r_type != R_ARM_TLS_CALL
4198       && r_type != R_ARM_THM_TLS_CALL
4199       && elf32_arm_get_plt_info (input_bfd, globals, hash,
4200                                  ELF32_R_SYM (rel->r_info), &root_plt,
4201                                  &arm_plt)
4202       && root_plt->offset != (bfd_vma) -1)
4203     {
4204       asection *splt;
4205
4206       if (hash == NULL || hash->is_iplt)
4207         splt = globals->root.iplt;
4208       else
4209         splt = globals->root.splt;
4210       if (splt != NULL)
4211         {
4212           use_plt = 1;
4213
4214           /* Note when dealing with PLT entries: the main PLT stub is in
4215              ARM mode, so if the branch is in Thumb mode, another
4216              Thumb->ARM stub will be inserted later just before the ARM
4217              PLT stub. If a long branch stub is needed, we'll add a
4218              Thumb->Arm one and branch directly to the ARM PLT entry.
4219              Here, we have to check if a pre-PLT Thumb->ARM stub
4220              is needed and if it will be close enough.  */
4221
4222           destination = (splt->output_section->vma
4223                          + splt->output_offset
4224                          + root_plt->offset);
4225           st_type = STT_FUNC;
4226
4227           /* Thumb branch/call to PLT: it can become a branch to ARM
4228              or to Thumb. We must perform the same checks and
4229              corrections as in elf32_arm_final_link_relocate.  */
4230           if ((r_type == R_ARM_THM_CALL)
4231               || (r_type == R_ARM_THM_JUMP24))
4232             {
4233               if (globals->use_blx
4234                   && r_type == R_ARM_THM_CALL
4235                   && !thumb_only)
4236                 {
4237                   /* If the Thumb BLX instruction is available, convert
4238                      the BL to a BLX instruction to call the ARM-mode
4239                      PLT entry.  */
4240                   branch_type = ST_BRANCH_TO_ARM;
4241                 }
4242               else
4243                 {
4244                   if (!thumb_only)
4245                     /* Target the Thumb stub before the ARM PLT entry.  */
4246                     destination -= PLT_THUMB_STUB_SIZE;
4247                   branch_type = ST_BRANCH_TO_THUMB;
4248                 }
4249             }
4250           else
4251             {
4252               branch_type = ST_BRANCH_TO_ARM;
4253             }
4254         }
4255     }
4256   /* Calls to STT_GNU_IFUNC symbols should go through a PLT.  */
4257   BFD_ASSERT (st_type != STT_GNU_IFUNC);
4258
4259   branch_offset = (bfd_signed_vma)(destination - location);
4260
4261   if (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24
4262       || r_type == R_ARM_THM_TLS_CALL || r_type == R_ARM_THM_JUMP19)
4263     {
4264       /* Handle cases where:
4265          - this call goes too far (different Thumb/Thumb2 max
4266            distance)
4267          - it's a Thumb->Arm call and blx is not available, or it's a
4268            Thumb->Arm branch (not bl). A stub is needed in this case,
4269            but only if this call is not through a PLT entry. Indeed,
4270            PLT stubs handle mode switching already.  */
4271       if ((!thumb2_bl
4272             && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
4273                 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
4274           || (thumb2_bl
4275               && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
4276                   || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
4277           || (thumb2
4278               && (branch_offset > THM2_MAX_FWD_COND_BRANCH_OFFSET
4279                   || (branch_offset < THM2_MAX_BWD_COND_BRANCH_OFFSET))
4280               && (r_type == R_ARM_THM_JUMP19))
4281           || (branch_type == ST_BRANCH_TO_ARM
4282               && (((r_type == R_ARM_THM_CALL
4283                     || r_type == R_ARM_THM_TLS_CALL) && !globals->use_blx)
4284                   || (r_type == R_ARM_THM_JUMP24)
4285                   || (r_type == R_ARM_THM_JUMP19))
4286               && !use_plt))
4287         {
4288           /* If we need to insert a Thumb-Thumb long branch stub to a
4289              PLT, use one that branches directly to the ARM PLT
4290              stub. If we pretended we'd use the pre-PLT Thumb->ARM
4291              stub, undo this now.  */
4292           if ((branch_type == ST_BRANCH_TO_THUMB) && use_plt && !thumb_only)
4293             {
4294               branch_type = ST_BRANCH_TO_ARM;
4295               branch_offset += PLT_THUMB_STUB_SIZE;
4296             }
4297
4298           if (branch_type == ST_BRANCH_TO_THUMB)
4299             {
4300               /* Thumb to thumb.  */
4301               if (!thumb_only)
4302                 {
4303                   if (input_sec->flags & SEC_ELF_PURECODE)
4304                     _bfd_error_handler
4305                       (_("%pB(%pA): warning: long branch veneers used in"
4306                          " section with SHF_ARM_PURECODE section"
4307                          " attribute is only supported for M-profile"
4308                          " targets that implement the movw instruction"),
4309                        input_bfd, input_sec);
4310
4311                   stub_type = (bfd_link_pic (info) | globals->pic_veneer)
4312                     /* PIC stubs.  */
4313                     ? ((globals->use_blx
4314                         && (r_type == R_ARM_THM_CALL))
4315                        /* V5T and above. Stub starts with ARM code, so
4316                           we must be able to switch mode before
4317                           reaching it, which is only possible for 'bl'
4318                           (ie R_ARM_THM_CALL relocation).  */
4319                        ? arm_stub_long_branch_any_thumb_pic
4320                        /* On V4T, use Thumb code only.  */
4321                        : arm_stub_long_branch_v4t_thumb_thumb_pic)
4322
4323                     /* non-PIC stubs.  */
4324                     : ((globals->use_blx
4325                         && (r_type == R_ARM_THM_CALL))
4326                        /* V5T and above.  */
4327                        ? arm_stub_long_branch_any_any
4328                        /* V4T.  */
4329                        : arm_stub_long_branch_v4t_thumb_thumb);
4330                 }
4331               else
4332                 {
4333                   if (thumb2_movw && (input_sec->flags & SEC_ELF_PURECODE))
4334                       stub_type = arm_stub_long_branch_thumb2_only_pure;
4335                   else
4336                     {
4337                       if (input_sec->flags & SEC_ELF_PURECODE)
4338                         _bfd_error_handler
4339                           (_("%pB(%pA): warning: long branch veneers used in"
4340                              " section with SHF_ARM_PURECODE section"
4341                              " attribute is only supported for M-profile"
4342                              " targets that implement the movw instruction"),
4343                            input_bfd, input_sec);
4344
4345                       stub_type = (bfd_link_pic (info) | globals->pic_veneer)
4346                         /* PIC stub.  */
4347                         ? arm_stub_long_branch_thumb_only_pic
4348                         /* non-PIC stub.  */
4349                         : (thumb2 ? arm_stub_long_branch_thumb2_only
4350                                   : arm_stub_long_branch_thumb_only);
4351                     }
4352                 }
4353             }
4354           else
4355             {
4356               if (input_sec->flags & SEC_ELF_PURECODE)
4357                 _bfd_error_handler
4358                   (_("%pB(%pA): warning: long branch veneers used in"
4359                      " section with SHF_ARM_PURECODE section"
4360                      " attribute is only supported" " for M-profile"
4361                      " targets that implement the movw instruction"),
4362                    input_bfd, input_sec);
4363
4364               /* Thumb to arm.  */
4365               if (sym_sec != NULL
4366                   && sym_sec->owner != NULL
4367                   && !INTERWORK_FLAG (sym_sec->owner))
4368                 {
4369                   _bfd_error_handler
4370                     (_("%pB(%s): warning: interworking not enabled;"
4371                        " first occurrence: %pB: %s call to %s"),
4372                      sym_sec->owner, name, input_bfd, "Thumb", "ARM");
4373                 }
4374
4375               stub_type =
4376                 (bfd_link_pic (info) | globals->pic_veneer)
4377                 /* PIC stubs.  */
4378                 ? (r_type == R_ARM_THM_TLS_CALL
4379                    /* TLS PIC stubs.  */
4380                    ? (globals->use_blx ? arm_stub_long_branch_any_tls_pic
4381                       : arm_stub_long_branch_v4t_thumb_tls_pic)
4382                    : ((globals->use_blx && r_type == R_ARM_THM_CALL)
4383                       /* V5T PIC and above.  */
4384                       ? arm_stub_long_branch_any_arm_pic
4385                       /* V4T PIC stub.  */
4386                       : arm_stub_long_branch_v4t_thumb_arm_pic))
4387
4388                 /* non-PIC stubs.  */
4389                 : ((globals->use_blx && r_type == R_ARM_THM_CALL)
4390                    /* V5T and above.  */
4391                    ? arm_stub_long_branch_any_any
4392                    /* V4T.  */
4393                    : arm_stub_long_branch_v4t_thumb_arm);
4394
4395               /* Handle v4t short branches.  */
4396               if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
4397                   && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
4398                   && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
4399                 stub_type = arm_stub_short_branch_v4t_thumb_arm;
4400             }
4401         }
4402     }
4403   else if (r_type == R_ARM_CALL
4404            || r_type == R_ARM_JUMP24
4405            || r_type == R_ARM_PLT32
4406            || r_type == R_ARM_TLS_CALL)
4407     {
4408       if (input_sec->flags & SEC_ELF_PURECODE)
4409         _bfd_error_handler
4410           (_("%pB(%pA): warning: long branch veneers used in"
4411              " section with SHF_ARM_PURECODE section"
4412              " attribute is only supported for M-profile"
4413              " targets that implement the movw instruction"),
4414            input_bfd, input_sec);
4415       if (branch_type == ST_BRANCH_TO_THUMB)
4416         {
4417           /* Arm to thumb.  */
4418
4419           if (sym_sec != NULL
4420               && sym_sec->owner != NULL
4421               && !INTERWORK_FLAG (sym_sec->owner))
4422             {
4423               _bfd_error_handler
4424                 (_("%pB(%s): warning: interworking not enabled;"
4425                    " first occurrence: %pB: %s call to %s"),
4426                  sym_sec->owner, name, input_bfd, "ARM", "Thumb");
4427             }
4428
4429           /* We have an extra 2-bytes reach because of
4430              the mode change (bit 24 (H) of BLX encoding).  */
4431           if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
4432               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
4433               || (r_type == R_ARM_CALL && !globals->use_blx)
4434               || (r_type == R_ARM_JUMP24)
4435               || (r_type == R_ARM_PLT32))
4436             {
4437               stub_type = (bfd_link_pic (info) | globals->pic_veneer)
4438                 /* PIC stubs.  */
4439                 ? ((globals->use_blx)
4440                    /* V5T and above.  */
4441                    ? arm_stub_long_branch_any_thumb_pic
4442                    /* V4T stub.  */
4443                    : arm_stub_long_branch_v4t_arm_thumb_pic)
4444
4445                 /* non-PIC stubs.  */
4446                 : ((globals->use_blx)
4447                    /* V5T and above.  */
4448                    ? arm_stub_long_branch_any_any
4449                    /* V4T.  */
4450                    : arm_stub_long_branch_v4t_arm_thumb);
4451             }
4452         }
4453       else
4454         {
4455           /* Arm to arm.  */
4456           if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
4457               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
4458             {
4459               stub_type =
4460                 (bfd_link_pic (info) | globals->pic_veneer)
4461                 /* PIC stubs.  */
4462                 ? (r_type == R_ARM_TLS_CALL
4463                    /* TLS PIC Stub.  */
4464                    ? arm_stub_long_branch_any_tls_pic
4465                    : (globals->nacl_p
4466                       ? arm_stub_long_branch_arm_nacl_pic
4467                       : arm_stub_long_branch_any_arm_pic))
4468                 /* non-PIC stubs.  */
4469                 : (globals->nacl_p
4470                    ? arm_stub_long_branch_arm_nacl
4471                    : arm_stub_long_branch_any_any);
4472             }
4473         }
4474     }
4475
4476   /* If a stub is needed, record the actual destination type.  */
4477   if (stub_type != arm_stub_none)
4478     *actual_branch_type = branch_type;
4479
4480   return stub_type;
4481 }
4482
4483 /* Build a name for an entry in the stub hash table.  */
4484
4485 static char *
4486 elf32_arm_stub_name (const asection *input_section,
4487                      const asection *sym_sec,
4488                      const struct elf32_arm_link_hash_entry *hash,
4489                      const Elf_Internal_Rela *rel,
4490                      enum elf32_arm_stub_type stub_type)
4491 {
4492   char *stub_name;
4493   bfd_size_type len;
4494
4495   if (hash)
4496     {
4497       len = 8 + 1 + strlen (hash->root.root.root.string) + 1 + 8 + 1 + 2 + 1;
4498       stub_name = (char *) bfd_malloc (len);
4499       if (stub_name != NULL)
4500         sprintf (stub_name, "%08x_%s+%x_%d",
4501                  input_section->id & 0xffffffff,
4502                  hash->root.root.root.string,
4503                  (int) rel->r_addend & 0xffffffff,
4504                  (int) stub_type);
4505     }
4506   else
4507     {
4508       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1 + 2 + 1;
4509       stub_name = (char *) bfd_malloc (len);
4510       if (stub_name != NULL)
4511         sprintf (stub_name, "%08x_%x:%x+%x_%d",
4512                  input_section->id & 0xffffffff,
4513                  sym_sec->id & 0xffffffff,
4514                  ELF32_R_TYPE (rel->r_info) == R_ARM_TLS_CALL
4515                  || ELF32_R_TYPE (rel->r_info) == R_ARM_THM_TLS_CALL
4516                  ? 0 : (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
4517                  (int) rel->r_addend & 0xffffffff,
4518                  (int) stub_type);
4519     }
4520
4521   return stub_name;
4522 }
4523
4524 /* Look up an entry in the stub hash.  Stub entries are cached because
4525    creating the stub name takes a bit of time.  */
4526
4527 static struct elf32_arm_stub_hash_entry *
4528 elf32_arm_get_stub_entry (const asection *input_section,
4529                           const asection *sym_sec,
4530                           struct elf_link_hash_entry *hash,
4531                           const Elf_Internal_Rela *rel,
4532                           struct elf32_arm_link_hash_table *htab,
4533                           enum elf32_arm_stub_type stub_type)
4534 {
4535   struct elf32_arm_stub_hash_entry *stub_entry;
4536   struct elf32_arm_link_hash_entry *h = (struct elf32_arm_link_hash_entry *) hash;
4537   const asection *id_sec;
4538
4539   if ((input_section->flags & SEC_CODE) == 0)
4540     return NULL;
4541
4542   /* If this input section is part of a group of sections sharing one
4543      stub section, then use the id of the first section in the group.
4544      Stub names need to include a section id, as there may well be
4545      more than one stub used to reach say, printf, and we need to
4546      distinguish between them.  */
4547   BFD_ASSERT (input_section->id <= htab->top_id);
4548   id_sec = htab->stub_group[input_section->id].link_sec;
4549
4550   if (h != NULL && h->stub_cache != NULL
4551       && h->stub_cache->h == h
4552       && h->stub_cache->id_sec == id_sec
4553       && h->stub_cache->stub_type == stub_type)
4554     {
4555       stub_entry = h->stub_cache;
4556     }
4557   else
4558     {
4559       char *stub_name;
4560
4561       stub_name = elf32_arm_stub_name (id_sec, sym_sec, h, rel, stub_type);
4562       if (stub_name == NULL)
4563         return NULL;
4564
4565       stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table,
4566                                         stub_name, FALSE, FALSE);
4567       if (h != NULL)
4568         h->stub_cache = stub_entry;
4569
4570       free (stub_name);
4571     }
4572
4573   return stub_entry;
4574 }
4575
4576 /* Whether veneers of type STUB_TYPE require to be in a dedicated output
4577    section.  */
4578
4579 static bfd_boolean
4580 arm_dedicated_stub_output_section_required (enum elf32_arm_stub_type stub_type)
4581 {
4582   if (stub_type >= max_stub_type)
4583     abort ();  /* Should be unreachable.  */
4584
4585   switch (stub_type)
4586     {
4587     case arm_stub_cmse_branch_thumb_only:
4588       return TRUE;
4589
4590     default:
4591       return FALSE;
4592     }
4593
4594   abort ();  /* Should be unreachable.  */
4595 }
4596
4597 /* Required alignment (as a power of 2) for the dedicated section holding
4598    veneers of type STUB_TYPE, or 0 if veneers of this type are interspersed
4599    with input sections.  */
4600
4601 static int
4602 arm_dedicated_stub_output_section_required_alignment
4603   (enum elf32_arm_stub_type stub_type)
4604 {
4605   if (stub_type >= max_stub_type)
4606     abort ();  /* Should be unreachable.  */
4607
4608   switch (stub_type)
4609     {
4610     /* Vectors of Secure Gateway veneers must be aligned on 32byte
4611        boundary.  */
4612     case arm_stub_cmse_branch_thumb_only:
4613       return 5;
4614
4615     default:
4616       BFD_ASSERT (!arm_dedicated_stub_output_section_required (stub_type));
4617       return 0;
4618     }
4619
4620   abort ();  /* Should be unreachable.  */
4621 }
4622
4623 /* Name of the dedicated output section to put veneers of type STUB_TYPE, or
4624    NULL if veneers of this type are interspersed with input sections.  */
4625
4626 static const char *
4627 arm_dedicated_stub_output_section_name (enum elf32_arm_stub_type stub_type)
4628 {
4629   if (stub_type >= max_stub_type)
4630     abort ();  /* Should be unreachable.  */
4631
4632   switch (stub_type)
4633     {
4634     case arm_stub_cmse_branch_thumb_only:
4635       return ".gnu.sgstubs";
4636
4637     default:
4638       BFD_ASSERT (!arm_dedicated_stub_output_section_required (stub_type));
4639       return NULL;
4640     }
4641
4642   abort ();  /* Should be unreachable.  */
4643 }
4644
4645 /* If veneers of type STUB_TYPE should go in a dedicated output section,
4646    returns the address of the hash table field in HTAB holding a pointer to the
4647    corresponding input section.  Otherwise, returns NULL.  */
4648
4649 static asection **
4650 arm_dedicated_stub_input_section_ptr (struct elf32_arm_link_hash_table *htab,
4651                                       enum elf32_arm_stub_type stub_type)
4652 {
4653   if (stub_type >= max_stub_type)
4654     abort ();  /* Should be unreachable.  */
4655
4656   switch (stub_type)
4657     {
4658     case arm_stub_cmse_branch_thumb_only:
4659       return &htab->cmse_stub_sec;
4660
4661     default:
4662       BFD_ASSERT (!arm_dedicated_stub_output_section_required (stub_type));
4663       return NULL;
4664     }
4665
4666   abort ();  /* Should be unreachable.  */
4667 }
4668
4669 /* Find or create a stub section to contain a stub of type STUB_TYPE.  SECTION
4670    is the section that branch into veneer and can be NULL if stub should go in
4671    a dedicated output section.  Returns a pointer to the stub section, and the
4672    section to which the stub section will be attached (in *LINK_SEC_P).
4673    LINK_SEC_P may be NULL.  */
4674
4675 static asection *
4676 elf32_arm_create_or_find_stub_sec (asection **link_sec_p, asection *section,
4677                                    struct elf32_arm_link_hash_table *htab,
4678                                    enum elf32_arm_stub_type stub_type)
4679 {
4680   asection *link_sec, *out_sec, **stub_sec_p;
4681   const char *stub_sec_prefix;
4682   bfd_boolean dedicated_output_section =
4683     arm_dedicated_stub_output_section_required (stub_type);
4684   int align;
4685
4686   if (dedicated_output_section)
4687     {
4688       bfd *output_bfd = htab->obfd;
4689       const char *out_sec_name =
4690         arm_dedicated_stub_output_section_name (stub_type);
4691       link_sec = NULL;
4692       stub_sec_p = arm_dedicated_stub_input_section_ptr (htab, stub_type);
4693       stub_sec_prefix = out_sec_name;
4694       align = arm_dedicated_stub_output_section_required_alignment (stub_type);
4695       out_sec = bfd_get_section_by_name (output_bfd, out_sec_name);
4696       if (out_sec == NULL)
4697         {
4698           _bfd_error_handler (_("no address assigned to the veneers output "
4699                                 "section %s"), out_sec_name);
4700           return NULL;
4701         }
4702     }
4703   else
4704     {
4705       BFD_ASSERT (section->id <= htab->top_id);
4706       link_sec = htab->stub_group[section->id].link_sec;
4707       BFD_ASSERT (link_sec != NULL);
4708       stub_sec_p = &htab->stub_group[section->id].stub_sec;
4709       if (*stub_sec_p == NULL)
4710         stub_sec_p = &htab->stub_group[link_sec->id].stub_sec;
4711       stub_sec_prefix = link_sec->name;
4712       out_sec = link_sec->output_section;
4713       align = htab->nacl_p ? 4 : 3;
4714     }
4715
4716   if (*stub_sec_p == NULL)
4717     {
4718       size_t namelen;
4719       bfd_size_type len;
4720       char *s_name;
4721
4722       namelen = strlen (stub_sec_prefix);
4723       len = namelen + sizeof (STUB_SUFFIX);
4724       s_name = (char *) bfd_alloc (htab->stub_bfd, len);
4725       if (s_name == NULL)
4726         return NULL;
4727
4728       memcpy (s_name, stub_sec_prefix, namelen);
4729       memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4730       *stub_sec_p = (*htab->add_stub_section) (s_name, out_sec, link_sec,
4731                                                align);
4732       if (*stub_sec_p == NULL)
4733         return NULL;
4734
4735       out_sec->flags |= SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
4736                         | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY
4737                         | SEC_KEEP;
4738     }
4739
4740   if (!dedicated_output_section)
4741     htab->stub_group[section->id].stub_sec = *stub_sec_p;
4742
4743   if (link_sec_p)
4744     *link_sec_p = link_sec;
4745
4746   return *stub_sec_p;
4747 }
4748
4749 /* Add a new stub entry to the stub hash.  Not all fields of the new
4750    stub entry are initialised.  */
4751
4752 static struct elf32_arm_stub_hash_entry *
4753 elf32_arm_add_stub (const char *stub_name, asection *section,
4754                     struct elf32_arm_link_hash_table *htab,
4755                     enum elf32_arm_stub_type stub_type)
4756 {
4757   asection *link_sec;
4758   asection *stub_sec;
4759   struct elf32_arm_stub_hash_entry *stub_entry;
4760
4761   stub_sec = elf32_arm_create_or_find_stub_sec (&link_sec, section, htab,
4762                                                 stub_type);
4763   if (stub_sec == NULL)
4764     return NULL;
4765
4766   /* Enter this entry into the linker stub hash table.  */
4767   stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4768                                      TRUE, FALSE);
4769   if (stub_entry == NULL)
4770     {
4771       if (section == NULL)
4772         section = stub_sec;
4773       _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4774                           section->owner, stub_name);
4775       return NULL;
4776     }
4777
4778   stub_entry->stub_sec = stub_sec;
4779   stub_entry->stub_offset = (bfd_vma) -1;
4780   stub_entry->id_sec = link_sec;
4781
4782   return stub_entry;
4783 }
4784
4785 /* Store an Arm insn into an output section not processed by
4786    elf32_arm_write_section.  */
4787
4788 static void
4789 put_arm_insn (struct elf32_arm_link_hash_table * htab,
4790               bfd * output_bfd, bfd_vma val, void * ptr)
4791 {
4792   if (htab->byteswap_code != bfd_little_endian (output_bfd))
4793     bfd_putl32 (val, ptr);
4794   else
4795     bfd_putb32 (val, ptr);
4796 }
4797
4798 /* Store a 16-bit Thumb insn into an output section not processed by
4799    elf32_arm_write_section.  */
4800
4801 static void
4802 put_thumb_insn (struct elf32_arm_link_hash_table * htab,
4803                 bfd * output_bfd, bfd_vma val, void * ptr)
4804 {
4805   if (htab->byteswap_code != bfd_little_endian (output_bfd))
4806     bfd_putl16 (val, ptr);
4807   else
4808     bfd_putb16 (val, ptr);
4809 }
4810
4811 /* Store a Thumb2 insn into an output section not processed by
4812    elf32_arm_write_section.  */
4813
4814 static void
4815 put_thumb2_insn (struct elf32_arm_link_hash_table * htab,
4816                  bfd * output_bfd, bfd_vma val, bfd_byte * ptr)
4817 {
4818   /* T2 instructions are 16-bit streamed.  */
4819   if (htab->byteswap_code != bfd_little_endian (output_bfd))
4820     {
4821       bfd_putl16 ((val >> 16) & 0xffff, ptr);
4822       bfd_putl16 ((val & 0xffff), ptr + 2);
4823     }
4824   else
4825     {
4826       bfd_putb16 ((val >> 16) & 0xffff, ptr);
4827       bfd_putb16 ((val & 0xffff), ptr + 2);
4828     }
4829 }
4830
4831 /* If it's possible to change R_TYPE to a more efficient access
4832    model, return the new reloc type.  */
4833
4834 static unsigned
4835 elf32_arm_tls_transition (struct bfd_link_info *info, int r_type,
4836                           struct elf_link_hash_entry *h)
4837 {
4838   int is_local = (h == NULL);
4839
4840   if (bfd_link_pic (info)
4841       || (h && h->root.type == bfd_link_hash_undefweak))
4842     return r_type;
4843
4844   /* We do not support relaxations for Old TLS models.  */
4845   switch (r_type)
4846     {
4847     case R_ARM_TLS_GOTDESC:
4848     case R_ARM_TLS_CALL:
4849     case R_ARM_THM_TLS_CALL:
4850     case R_ARM_TLS_DESCSEQ:
4851     case R_ARM_THM_TLS_DESCSEQ:
4852       return is_local ? R_ARM_TLS_LE32 : R_ARM_TLS_IE32;
4853     }
4854
4855   return r_type;
4856 }
4857
4858 static bfd_reloc_status_type elf32_arm_final_link_relocate
4859   (reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *,
4860    Elf_Internal_Rela *, bfd_vma, struct bfd_link_info *, asection *,
4861    const char *, unsigned char, enum arm_st_branch_type,
4862    struct elf_link_hash_entry *, bfd_boolean *, char **);
4863
4864 static unsigned int
4865 arm_stub_required_alignment (enum elf32_arm_stub_type stub_type)
4866 {
4867   switch (stub_type)
4868     {
4869     case arm_stub_a8_veneer_b_cond:
4870     case arm_stub_a8_veneer_b:
4871     case arm_stub_a8_veneer_bl:
4872       return 2;
4873
4874     case arm_stub_long_branch_any_any:
4875     case arm_stub_long_branch_v4t_arm_thumb:
4876     case arm_stub_long_branch_thumb_only:
4877     case arm_stub_long_branch_thumb2_only:
4878     case arm_stub_long_branch_thumb2_only_pure:
4879     case arm_stub_long_branch_v4t_thumb_thumb:
4880     case arm_stub_long_branch_v4t_thumb_arm:
4881     case arm_stub_short_branch_v4t_thumb_arm:
4882     case arm_stub_long_branch_any_arm_pic:
4883     case arm_stub_long_branch_any_thumb_pic:
4884     case arm_stub_long_branch_v4t_thumb_thumb_pic:
4885     case arm_stub_long_branch_v4t_arm_thumb_pic:
4886     case arm_stub_long_branch_v4t_thumb_arm_pic:
4887     case arm_stub_long_branch_thumb_only_pic:
4888     case arm_stub_long_branch_any_tls_pic:
4889     case arm_stub_long_branch_v4t_thumb_tls_pic:
4890     case arm_stub_cmse_branch_thumb_only:
4891     case arm_stub_a8_veneer_blx:
4892       return 4;
4893
4894     case arm_stub_long_branch_arm_nacl:
4895     case arm_stub_long_branch_arm_nacl_pic:
4896       return 16;
4897
4898     default:
4899       abort ();  /* Should be unreachable.  */
4900     }
4901 }
4902
4903 /* Returns whether stubs of type STUB_TYPE take over the symbol they are
4904    veneering (TRUE) or have their own symbol (FALSE).  */
4905
4906 static bfd_boolean
4907 arm_stub_sym_claimed (enum elf32_arm_stub_type stub_type)
4908 {
4909   if (stub_type >= max_stub_type)
4910     abort ();  /* Should be unreachable.  */
4911
4912   switch (stub_type)
4913     {
4914     case arm_stub_cmse_branch_thumb_only:
4915       return TRUE;
4916
4917     default:
4918       return FALSE;
4919     }
4920
4921   abort ();  /* Should be unreachable.  */
4922 }
4923
4924 /* Returns the padding needed for the dedicated section used stubs of type
4925    STUB_TYPE.  */
4926
4927 static int
4928 arm_dedicated_stub_section_padding (enum elf32_arm_stub_type stub_type)
4929 {
4930   if (stub_type >= max_stub_type)
4931     abort ();  /* Should be unreachable.  */
4932
4933   switch (stub_type)
4934     {
4935     case arm_stub_cmse_branch_thumb_only:
4936       return 32;
4937
4938     default:
4939       return 0;
4940     }
4941
4942   abort ();  /* Should be unreachable.  */
4943 }
4944
4945 /* If veneers of type STUB_TYPE should go in a dedicated output section,
4946    returns the address of the hash table field in HTAB holding the offset at
4947    which new veneers should be layed out in the stub section.  */
4948
4949 static bfd_vma*
4950 arm_new_stubs_start_offset_ptr (struct elf32_arm_link_hash_table *htab,
4951                                 enum elf32_arm_stub_type stub_type)
4952 {
4953   switch (stub_type)
4954     {
4955     case arm_stub_cmse_branch_thumb_only:
4956       return &htab->new_cmse_stub_offset;
4957
4958     default:
4959       BFD_ASSERT (!arm_dedicated_stub_output_section_required (stub_type));
4960       return NULL;
4961     }
4962 }
4963
4964 static bfd_boolean
4965 arm_build_one_stub (struct bfd_hash_entry *gen_entry,
4966                     void * in_arg)
4967 {
4968 #define MAXRELOCS 3
4969   bfd_boolean removed_sg_veneer;
4970   struct elf32_arm_stub_hash_entry *stub_entry;
4971   struct elf32_arm_link_hash_table *globals;
4972   struct bfd_link_info *info;
4973   asection *stub_sec;
4974   bfd *stub_bfd;
4975   bfd_byte *loc;
4976   bfd_vma sym_value;
4977   int template_size;
4978   int size;
4979   const insn_sequence *template_sequence;
4980   int i;
4981   int stub_reloc_idx[MAXRELOCS] = {-1, -1};
4982   int stub_reloc_offset[MAXRELOCS] = {0, 0};
4983   int nrelocs = 0;
4984   int just_allocated = 0;
4985
4986   /* Massage our args to the form they really have.  */
4987   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
4988   info = (struct bfd_link_info *) in_arg;
4989
4990   globals = elf32_arm_hash_table (info);
4991   if (globals == NULL)
4992     return FALSE;
4993
4994   stub_sec = stub_entry->stub_sec;
4995
4996   if ((globals->fix_cortex_a8 < 0)
4997       != (arm_stub_required_alignment (stub_entry->stub_type) == 2))
4998     /* We have to do less-strictly-aligned fixes last.  */
4999     return TRUE;
5000
5001   /* Assign a slot at the end of section if none assigned yet.  */
5002   if (stub_entry->stub_offset == (bfd_vma) -1)
5003     {
5004       stub_entry->stub_offset = stub_sec->size;
5005       just_allocated = 1;
5006     }
5007   loc = stub_sec->contents + stub_entry->stub_offset;
5008
5009   stub_bfd = stub_sec->owner;
5010
5011   /* This is the address of the stub destination.  */
5012   sym_value = (stub_entry->target_value
5013                + stub_entry->target_section->output_offset
5014                + stub_entry->target_section->output_section->vma);
5015
5016   template_sequence = stub_entry->stub_template;
5017   template_size = stub_entry->stub_template_size;
5018
5019   size = 0;
5020   for (i = 0; i < template_size; i++)
5021     {
5022       switch (template_sequence[i].type)
5023         {
5024         case THUMB16_TYPE:
5025           {
5026             bfd_vma data = (bfd_vma) template_sequence[i].data;
5027             if (template_sequence[i].reloc_addend != 0)
5028               {
5029                 /* We've borrowed the reloc_addend field to mean we should
5030                    insert a condition code into this (Thumb-1 branch)
5031                    instruction.  See THUMB16_BCOND_INSN.  */
5032                 BFD_ASSERT ((data & 0xff00) == 0xd000);
5033                 data |= ((stub_entry->orig_insn >> 22) & 0xf) << 8;
5034               }
5035             bfd_put_16 (stub_bfd, data, loc + size);
5036             size += 2;
5037           }
5038           break;
5039
5040         case THUMB32_TYPE:
5041           bfd_put_16 (stub_bfd,
5042                       (template_sequence[i].data >> 16) & 0xffff,
5043                       loc + size);
5044           bfd_put_16 (stub_bfd, template_sequence[i].data & 0xffff,
5045                       loc + size + 2);
5046           if (template_sequence[i].r_type != R_ARM_NONE)
5047             {
5048               stub_reloc_idx[nrelocs] = i;
5049               stub_reloc_offset[nrelocs++] = size;
5050             }
5051           size += 4;
5052           break;
5053
5054         case ARM_TYPE:
5055           bfd_put_32 (stub_bfd, template_sequence[i].data,
5056                       loc + size);
5057           /* Handle cases where the target is encoded within the
5058              instruction.  */
5059           if (template_sequence[i].r_type == R_ARM_JUMP24)
5060             {
5061               stub_reloc_idx[nrelocs] = i;
5062               stub_reloc_offset[nrelocs++] = size;
5063             }
5064           size += 4;
5065           break;
5066
5067         case DATA_TYPE:
5068           bfd_put_32 (stub_bfd, template_sequence[i].data, loc + size);
5069           stub_reloc_idx[nrelocs] = i;
5070           stub_reloc_offset[nrelocs++] = size;
5071           size += 4;
5072           break;
5073
5074         default:
5075           BFD_FAIL ();
5076           return FALSE;
5077         }
5078     }
5079
5080   if (just_allocated)
5081     stub_sec->size += size;
5082
5083   /* Stub size has already been computed in arm_size_one_stub. Check
5084      consistency.  */
5085   BFD_ASSERT (size == stub_entry->stub_size);
5086
5087   /* Destination is Thumb. Force bit 0 to 1 to reflect this.  */
5088   if (stub_entry->branch_type == ST_BRANCH_TO_THUMB)
5089     sym_value |= 1;
5090
5091   /* Assume non empty slots have at least one and at most MAXRELOCS entries
5092      to relocate in each stub.  */
5093   removed_sg_veneer =
5094     (size == 0 && stub_entry->stub_type == arm_stub_cmse_branch_thumb_only);
5095   BFD_ASSERT (removed_sg_veneer || (nrelocs != 0 && nrelocs <= MAXRELOCS));
5096
5097   for (i = 0; i < nrelocs; i++)
5098     {
5099       Elf_Internal_Rela rel;
5100       bfd_boolean unresolved_reloc;
5101       char *error_message;
5102       bfd_vma points_to =
5103         sym_value + template_sequence[stub_reloc_idx[i]].reloc_addend;
5104
5105       rel.r_offset = stub_entry->stub_offset + stub_reloc_offset[i];
5106       rel.r_info = ELF32_R_INFO (0,
5107                                  template_sequence[stub_reloc_idx[i]].r_type);
5108       rel.r_addend = 0;
5109
5110       if (stub_entry->stub_type == arm_stub_a8_veneer_b_cond && i == 0)
5111         /* The first relocation in the elf32_arm_stub_a8_veneer_b_cond[]
5112            template should refer back to the instruction after the original
5113            branch.  We use target_section as Cortex-A8 erratum workaround stubs
5114            are only generated when both source and target are in the same
5115            section.  */
5116         points_to = stub_entry->target_section->output_section->vma
5117                     + stub_entry->target_section->output_offset
5118                     + stub_entry->source_value;
5119
5120       elf32_arm_final_link_relocate (elf32_arm_howto_from_type
5121           (template_sequence[stub_reloc_idx[i]].r_type),
5122            stub_bfd, info->output_bfd, stub_sec, stub_sec->contents, &rel,
5123            points_to, info, stub_entry->target_section, "", STT_FUNC,
5124            stub_entry->branch_type,
5125            (struct elf_link_hash_entry *) stub_entry->h, &unresolved_reloc,
5126            &error_message);
5127     }
5128
5129   return TRUE;
5130 #undef MAXRELOCS
5131 }
5132
5133 /* Calculate the template, template size and instruction size for a stub.
5134    Return value is the instruction size.  */
5135
5136 static unsigned int
5137 find_stub_size_and_template (enum elf32_arm_stub_type stub_type,
5138                              const insn_sequence **stub_template,
5139                              int *stub_template_size)
5140 {
5141   const insn_sequence *template_sequence = NULL;
5142   int template_size = 0, i;
5143   unsigned int size;
5144
5145   template_sequence = stub_definitions[stub_type].template_sequence;
5146   if (stub_template)
5147     *stub_template = template_sequence;
5148
5149   template_size = stub_definitions[stub_type].template_size;
5150   if (stub_template_size)
5151     *stub_template_size = template_size;
5152
5153   size = 0;
5154   for (i = 0; i < template_size; i++)
5155     {
5156       switch (template_sequence[i].type)
5157         {
5158         case THUMB16_TYPE:
5159           size += 2;
5160           break;
5161
5162         case ARM_TYPE:
5163         case THUMB32_TYPE:
5164         case DATA_TYPE:
5165           size += 4;
5166           break;
5167
5168         default:
5169           BFD_FAIL ();
5170           return 0;
5171         }
5172     }
5173
5174   return size;
5175 }
5176
5177 /* As above, but don't actually build the stub.  Just bump offset so
5178    we know stub section sizes.  */
5179
5180 static bfd_boolean
5181 arm_size_one_stub (struct bfd_hash_entry *gen_entry,
5182                    void *in_arg ATTRIBUTE_UNUSED)
5183 {
5184   struct elf32_arm_stub_hash_entry *stub_entry;
5185   const insn_sequence *template_sequence;
5186   int template_size, size;
5187
5188   /* Massage our args to the form they really have.  */
5189   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
5190
5191   BFD_ASSERT((stub_entry->stub_type > arm_stub_none)
5192              && stub_entry->stub_type < ARRAY_SIZE(stub_definitions));
5193
5194   size = find_stub_size_and_template (stub_entry->stub_type, &template_sequence,
5195                                       &template_size);
5196
5197   /* Initialized to -1.  Null size indicates an empty slot full of zeros.  */
5198   if (stub_entry->stub_template_size)
5199     {
5200       stub_entry->stub_size = size;
5201       stub_entry->stub_template = template_sequence;
5202       stub_entry->stub_template_size = template_size;
5203     }
5204
5205   /* Already accounted for.  */
5206   if (stub_entry->stub_offset != (bfd_vma) -1)
5207     return TRUE;
5208
5209   size = (size + 7) & ~7;
5210   stub_entry->stub_sec->size += size;
5211
5212   return TRUE;
5213 }
5214
5215 /* External entry points for sizing and building linker stubs.  */
5216
5217 /* Set up various things so that we can make a list of input sections
5218    for each output section included in the link.  Returns -1 on error,
5219    0 when no stubs will be needed, and 1 on success.  */
5220
5221 int
5222 elf32_arm_setup_section_lists (bfd *output_bfd,
5223                                struct bfd_link_info *info)
5224 {
5225   bfd *input_bfd;
5226   unsigned int bfd_count;
5227   unsigned int top_id, top_index;
5228   asection *section;
5229   asection **input_list, **list;
5230   bfd_size_type amt;
5231   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
5232
5233   if (htab == NULL)
5234     return 0;
5235   if (! is_elf_hash_table (htab))
5236     return 0;
5237
5238   /* Count the number of input BFDs and find the top input section id.  */
5239   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
5240        input_bfd != NULL;
5241        input_bfd = input_bfd->link.next)
5242     {
5243       bfd_count += 1;
5244       for (section = input_bfd->sections;
5245            section != NULL;
5246            section = section->next)
5247         {
5248           if (top_id < section->id)
5249             top_id = section->id;
5250         }
5251     }
5252   htab->bfd_count = bfd_count;
5253
5254   amt = sizeof (struct map_stub) * (top_id + 1);
5255   htab->stub_group = (struct map_stub *) bfd_zmalloc (amt);
5256   if (htab->stub_group == NULL)
5257     return -1;
5258   htab->top_id = top_id;
5259
5260   /* We can't use output_bfd->section_count here to find the top output
5261      section index as some sections may have been removed, and
5262      _bfd_strip_section_from_output doesn't renumber the indices.  */
5263   for (section = output_bfd->sections, top_index = 0;
5264        section != NULL;
5265        section = section->next)
5266     {
5267       if (top_index < section->index)
5268         top_index = section->index;
5269     }
5270
5271   htab->top_index = top_index;
5272   amt = sizeof (asection *) * (top_index + 1);
5273   input_list = (asection **) bfd_malloc (amt);
5274   htab->input_list = input_list;
5275   if (input_list == NULL)
5276     return -1;
5277
5278   /* For sections we aren't interested in, mark their entries with a
5279      value we can check later.  */
5280   list = input_list + top_index;
5281   do
5282     *list = bfd_abs_section_ptr;
5283   while (list-- != input_list);
5284
5285   for (section = output_bfd->sections;
5286        section != NULL;
5287        section = section->next)
5288     {
5289       if ((section->flags & SEC_CODE) != 0)
5290         input_list[section->index] = NULL;
5291     }
5292
5293   return 1;
5294 }
5295
5296 /* The linker repeatedly calls this function for each input section,
5297    in the order that input sections are linked into output sections.
5298    Build lists of input sections to determine groupings between which
5299    we may insert linker stubs.  */
5300
5301 void
5302 elf32_arm_next_input_section (struct bfd_link_info *info,
5303                               asection *isec)
5304 {
5305   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
5306
5307   if (htab == NULL)
5308     return;
5309
5310   if (isec->output_section->index <= htab->top_index)
5311     {
5312       asection **list = htab->input_list + isec->output_section->index;
5313
5314       if (*list != bfd_abs_section_ptr && (isec->flags & SEC_CODE) != 0)
5315         {
5316           /* Steal the link_sec pointer for our list.  */
5317 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
5318           /* This happens to make the list in reverse order,
5319              which we reverse later.  */
5320           PREV_SEC (isec) = *list;
5321           *list = isec;
5322         }
5323     }
5324 }
5325
5326 /* See whether we can group stub sections together.  Grouping stub
5327    sections may result in fewer stubs.  More importantly, we need to
5328    put all .init* and .fini* stubs at the end of the .init or
5329    .fini output sections respectively, because glibc splits the
5330    _init and _fini functions into multiple parts.  Putting a stub in
5331    the middle of a function is not a good idea.  */
5332
5333 static void
5334 group_sections (struct elf32_arm_link_hash_table *htab,
5335                 bfd_size_type stub_group_size,
5336                 bfd_boolean stubs_always_after_branch)
5337 {
5338   asection **list = htab->input_list;
5339
5340   do
5341     {
5342       asection *tail = *list;
5343       asection *head;
5344
5345       if (tail == bfd_abs_section_ptr)
5346         continue;
5347
5348       /* Reverse the list: we must avoid placing stubs at the
5349          beginning of the section because the beginning of the text
5350          section may be required for an interrupt vector in bare metal
5351          code.  */
5352 #define NEXT_SEC PREV_SEC
5353       head = NULL;
5354       while (tail != NULL)
5355         {
5356           /* Pop from tail.  */
5357           asection *item = tail;
5358           tail = PREV_SEC (item);
5359
5360           /* Push on head.  */
5361           NEXT_SEC (item) = head;
5362           head = item;
5363         }
5364
5365       while (head != NULL)
5366         {
5367           asection *curr;
5368           asection *next;
5369           bfd_vma stub_group_start = head->output_offset;
5370           bfd_vma end_of_next;
5371
5372           curr = head;
5373           while (NEXT_SEC (curr) != NULL)
5374             {
5375               next = NEXT_SEC (curr);
5376               end_of_next = next->output_offset + next->size;
5377               if (end_of_next - stub_group_start >= stub_group_size)
5378                 /* End of NEXT is too far from start, so stop.  */
5379                 break;
5380               /* Add NEXT to the group.  */
5381               curr = next;
5382             }
5383
5384           /* OK, the size from the start to the start of CURR is less
5385              than stub_group_size and thus can be handled by one stub
5386              section.  (Or the head section is itself larger than
5387              stub_group_size, in which case we may be toast.)
5388              We should really be keeping track of the total size of
5389              stubs added here, as stubs contribute to the final output
5390              section size.  */
5391           do
5392             {
5393               next = NEXT_SEC (head);
5394               /* Set up this stub group.  */
5395               htab->stub_group[head->id].link_sec = curr;
5396             }
5397           while (head != curr && (head = next) != NULL);
5398
5399           /* But wait, there's more!  Input sections up to stub_group_size
5400              bytes after the stub section can be handled by it too.  */
5401           if (!stubs_always_after_branch)
5402             {
5403               stub_group_start = curr->output_offset + curr->size;
5404
5405               while (next != NULL)
5406                 {
5407                   end_of_next = next->output_offset + next->size;
5408                   if (end_of_next - stub_group_start >= stub_group_size)
5409                     /* End of NEXT is too far from stubs, so stop.  */
5410                     break;
5411                   /* Add NEXT to the stub group.  */
5412                   head = next;
5413                   next = NEXT_SEC (head);
5414                   htab->stub_group[head->id].link_sec = curr;
5415                 }
5416             }
5417           head = next;
5418         }
5419     }
5420   while (list++ != htab->input_list + htab->top_index);
5421
5422   free (htab->input_list);
5423 #undef PREV_SEC
5424 #undef NEXT_SEC
5425 }
5426
5427 /* Comparison function for sorting/searching relocations relating to Cortex-A8
5428    erratum fix.  */
5429
5430 static int
5431 a8_reloc_compare (const void *a, const void *b)
5432 {
5433   const struct a8_erratum_reloc *ra = (const struct a8_erratum_reloc *) a;
5434   const struct a8_erratum_reloc *rb = (const struct a8_erratum_reloc *) b;
5435
5436   if (ra->from < rb->from)
5437     return -1;
5438   else if (ra->from > rb->from)
5439     return 1;
5440   else
5441     return 0;
5442 }
5443
5444 static struct elf_link_hash_entry *find_thumb_glue (struct bfd_link_info *,
5445                                                     const char *, char **);
5446
5447 /* Helper function to scan code for sequences which might trigger the Cortex-A8
5448    branch/TLB erratum.  Fill in the table described by A8_FIXES_P,
5449    NUM_A8_FIXES_P, A8_FIX_TABLE_SIZE_P.  Returns true if an error occurs, false
5450    otherwise.  */
5451
5452 static bfd_boolean
5453 cortex_a8_erratum_scan (bfd *input_bfd,
5454                         struct bfd_link_info *info,
5455                         struct a8_erratum_fix **a8_fixes_p,
5456                         unsigned int *num_a8_fixes_p,
5457                         unsigned int *a8_fix_table_size_p,
5458                         struct a8_erratum_reloc *a8_relocs,
5459                         unsigned int num_a8_relocs,
5460                         unsigned prev_num_a8_fixes,
5461                         bfd_boolean *stub_changed_p)
5462 {
5463   asection *section;
5464   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
5465   struct a8_erratum_fix *a8_fixes = *a8_fixes_p;
5466   unsigned int num_a8_fixes = *num_a8_fixes_p;
5467   unsigned int a8_fix_table_size = *a8_fix_table_size_p;
5468
5469   if (htab == NULL)
5470     return FALSE;
5471
5472   for (section = input_bfd->sections;
5473        section != NULL;
5474        section = section->next)
5475     {
5476       bfd_byte *contents = NULL;
5477       struct _arm_elf_section_data *sec_data;
5478       unsigned int span;
5479       bfd_vma base_vma;
5480
5481       if (elf_section_type (section) != SHT_PROGBITS
5482           || (elf_section_flags (section) & SHF_EXECINSTR) == 0
5483           || (section->flags & SEC_EXCLUDE) != 0
5484           || (section->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5485           || (section->output_section == bfd_abs_section_ptr))
5486         continue;
5487
5488       base_vma = section->output_section->vma + section->output_offset;
5489
5490       if (elf_section_data (section)->this_hdr.contents != NULL)
5491         contents = elf_section_data (section)->this_hdr.contents;
5492       else if (! bfd_malloc_and_get_section (input_bfd, section, &contents))
5493         return TRUE;
5494
5495       sec_data = elf32_arm_section_data (section);
5496
5497       for (span = 0; span < sec_data->mapcount; span++)
5498         {
5499           unsigned int span_start = sec_data->map[span].vma;
5500           unsigned int span_end = (span == sec_data->mapcount - 1)
5501             ? section->size : sec_data->map[span + 1].vma;
5502           unsigned int i;
5503           char span_type = sec_data->map[span].type;
5504           bfd_boolean last_was_32bit = FALSE, last_was_branch = FALSE;
5505
5506           if (span_type != 't')
5507             continue;
5508
5509           /* Span is entirely within a single 4KB region: skip scanning.  */
5510           if (((base_vma + span_start) & ~0xfff)
5511               == ((base_vma + span_end) & ~0xfff))
5512             continue;
5513
5514           /* Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
5515
5516                * The opcode is BLX.W, BL.W, B.W, Bcc.W
5517                * The branch target is in the same 4KB region as the
5518                  first half of the branch.
5519                * The instruction before the branch is a 32-bit
5520                  length non-branch instruction.  */
5521           for (i = span_start; i < span_end;)
5522             {
5523               unsigned int insn = bfd_getl16 (&contents[i]);
5524               bfd_boolean insn_32bit = FALSE, is_blx = FALSE, is_b = FALSE;
5525               bfd_boolean is_bl = FALSE, is_bcc = FALSE, is_32bit_branch;
5526
5527               if ((insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000)
5528                 insn_32bit = TRUE;
5529
5530               if (insn_32bit)
5531                 {
5532                   /* Load the rest of the insn (in manual-friendly order).  */
5533                   insn = (insn << 16) | bfd_getl16 (&contents[i + 2]);
5534
5535                   /* Encoding T4: B<c>.W.  */
5536                   is_b = (insn & 0xf800d000) == 0xf0009000;
5537                   /* Encoding T1: BL<c>.W.  */
5538                   is_bl = (insn & 0xf800d000) == 0xf000d000;
5539                   /* Encoding T2: BLX<c>.W.  */
5540                   is_blx = (insn & 0xf800d000) == 0xf000c000;
5541                   /* Encoding T3: B<c>.W (not permitted in IT block).  */
5542                   is_bcc = (insn & 0xf800d000) == 0xf0008000
5543                            && (insn & 0x07f00000) != 0x03800000;
5544                 }
5545
5546               is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
5547
5548               if (((base_vma + i) & 0xfff) == 0xffe
5549                   && insn_32bit
5550                   && is_32bit_branch
5551                   && last_was_32bit
5552                   && ! last_was_branch)
5553                 {
5554                   bfd_signed_vma offset = 0;
5555                   bfd_boolean force_target_arm = FALSE;
5556                   bfd_boolean force_target_thumb = FALSE;
5557                   bfd_vma target;
5558                   enum elf32_arm_stub_type stub_type = arm_stub_none;
5559                   struct a8_erratum_reloc key, *found;
5560                   bfd_boolean use_plt = FALSE;
5561
5562                   key.from = base_vma + i;
5563                   found = (struct a8_erratum_reloc *)
5564                       bsearch (&key, a8_relocs, num_a8_relocs,
5565                                sizeof (struct a8_erratum_reloc),
5566                                &a8_reloc_compare);
5567
5568                   if (found)
5569                     {
5570                       char *error_message = NULL;
5571                       struct elf_link_hash_entry *entry;
5572
5573                       /* We don't care about the error returned from this
5574                          function, only if there is glue or not.  */
5575                       entry = find_thumb_glue (info, found->sym_name,
5576                                                &error_message);
5577
5578                       if (entry)
5579                         found->non_a8_stub = TRUE;
5580
5581                       /* Keep a simpler condition, for the sake of clarity.  */
5582                       if (htab->root.splt != NULL && found->hash != NULL
5583                           && found->hash->root.plt.offset != (bfd_vma) -1)
5584                         use_plt = TRUE;
5585
5586                       if (found->r_type == R_ARM_THM_CALL)
5587                         {
5588                           if (found->branch_type == ST_BRANCH_TO_ARM
5589                               || use_plt)
5590                             force_target_arm = TRUE;
5591                           else
5592                             force_target_thumb = TRUE;
5593                         }
5594                     }
5595
5596                   /* Check if we have an offending branch instruction.  */
5597
5598                   if (found && found->non_a8_stub)
5599                     /* We've already made a stub for this instruction, e.g.
5600                        it's a long branch or a Thumb->ARM stub.  Assume that
5601                        stub will suffice to work around the A8 erratum (see
5602                        setting of always_after_branch above).  */
5603                     ;
5604                   else if (is_bcc)
5605                     {
5606                       offset = (insn & 0x7ff) << 1;
5607                       offset |= (insn & 0x3f0000) >> 4;
5608                       offset |= (insn & 0x2000) ? 0x40000 : 0;
5609                       offset |= (insn & 0x800) ? 0x80000 : 0;
5610                       offset |= (insn & 0x4000000) ? 0x100000 : 0;
5611                       if (offset & 0x100000)
5612                         offset |= ~ ((bfd_signed_vma) 0xfffff);
5613                       stub_type = arm_stub_a8_veneer_b_cond;
5614                     }
5615                   else if (is_b || is_bl || is_blx)
5616                     {
5617                       int s = (insn & 0x4000000) != 0;
5618                       int j1 = (insn & 0x2000) != 0;
5619                       int j2 = (insn & 0x800) != 0;
5620                       int i1 = !(j1 ^ s);
5621                       int i2 = !(j2 ^ s);
5622
5623                       offset = (insn & 0x7ff) << 1;
5624                       offset |= (insn & 0x3ff0000) >> 4;
5625                       offset |= i2 << 22;
5626                       offset |= i1 << 23;
5627                       offset |= s << 24;
5628                       if (offset & 0x1000000)
5629                         offset |= ~ ((bfd_signed_vma) 0xffffff);
5630
5631                       if (is_blx)
5632                         offset &= ~ ((bfd_signed_vma) 3);
5633
5634                       stub_type = is_blx ? arm_stub_a8_veneer_blx :
5635                         is_bl ? arm_stub_a8_veneer_bl : arm_stub_a8_veneer_b;
5636                     }
5637
5638                   if (stub_type != arm_stub_none)
5639                     {
5640                       bfd_vma pc_for_insn = base_vma + i + 4;
5641
5642                       /* The original instruction is a BL, but the target is
5643                          an ARM instruction.  If we were not making a stub,
5644                          the BL would have been converted to a BLX.  Use the
5645                          BLX stub instead in that case.  */
5646                       if (htab->use_blx && force_target_arm
5647                           && stub_type == arm_stub_a8_veneer_bl)
5648                         {
5649                           stub_type = arm_stub_a8_veneer_blx;
5650                           is_blx = TRUE;
5651                           is_bl = FALSE;
5652                         }
5653                       /* Conversely, if the original instruction was
5654                          BLX but the target is Thumb mode, use the BL
5655                          stub.  */
5656                       else if (force_target_thumb
5657                                && stub_type == arm_stub_a8_veneer_blx)
5658                         {
5659                           stub_type = arm_stub_a8_veneer_bl;
5660                           is_blx = FALSE;
5661                           is_bl = TRUE;
5662                         }
5663
5664                       if (is_blx)
5665                         pc_for_insn &= ~ ((bfd_vma) 3);
5666
5667                       /* If we found a relocation, use the proper destination,
5668                          not the offset in the (unrelocated) instruction.
5669                          Note this is always done if we switched the stub type
5670                          above.  */
5671                       if (found)
5672                         offset =
5673                           (bfd_signed_vma) (found->destination - pc_for_insn);
5674
5675                       /* If the stub will use a Thumb-mode branch to a
5676                          PLT target, redirect it to the preceding Thumb
5677                          entry point.  */
5678                       if (stub_type != arm_stub_a8_veneer_blx && use_plt)
5679                         offset -= PLT_THUMB_STUB_SIZE;
5680
5681                       target = pc_for_insn + offset;
5682
5683                       /* The BLX stub is ARM-mode code.  Adjust the offset to
5684                          take the different PC value (+8 instead of +4) into
5685                          account.  */
5686                       if (stub_type == arm_stub_a8_veneer_blx)
5687                         offset += 4;
5688
5689                       if (((base_vma + i) & ~0xfff) == (target & ~0xfff))
5690                         {
5691                           char *stub_name = NULL;
5692
5693                           if (num_a8_fixes == a8_fix_table_size)
5694                             {
5695                               a8_fix_table_size *= 2;
5696                               a8_fixes = (struct a8_erratum_fix *)
5697                                   bfd_realloc (a8_fixes,
5698                                                sizeof (struct a8_erratum_fix)
5699                                                * a8_fix_table_size);
5700                             }
5701
5702                           if (num_a8_fixes < prev_num_a8_fixes)
5703                             {
5704                               /* If we're doing a subsequent scan,
5705                                  check if we've found the same fix as
5706                                  before, and try and reuse the stub
5707                                  name.  */
5708                               stub_name = a8_fixes[num_a8_fixes].stub_name;
5709                               if ((a8_fixes[num_a8_fixes].section != section)
5710                                   || (a8_fixes[num_a8_fixes].offset != i))
5711                                 {
5712                                   free (stub_name);
5713                                   stub_name = NULL;
5714                                   *stub_changed_p = TRUE;
5715                                 }
5716                             }
5717
5718                           if (!stub_name)
5719                             {
5720                               stub_name = (char *) bfd_malloc (8 + 1 + 8 + 1);
5721                               if (stub_name != NULL)
5722                                 sprintf (stub_name, "%x:%x", section->id, i);
5723                             }
5724
5725                           a8_fixes[num_a8_fixes].input_bfd = input_bfd;
5726                           a8_fixes[num_a8_fixes].section = section;
5727                           a8_fixes[num_a8_fixes].offset = i;
5728                           a8_fixes[num_a8_fixes].target_offset =
5729                             target - base_vma;
5730                           a8_fixes[num_a8_fixes].orig_insn = insn;
5731                           a8_fixes[num_a8_fixes].stub_name = stub_name;
5732                           a8_fixes[num_a8_fixes].stub_type = stub_type;
5733                           a8_fixes[num_a8_fixes].branch_type =
5734                             is_blx ? ST_BRANCH_TO_ARM : ST_BRANCH_TO_THUMB;
5735
5736                           num_a8_fixes++;
5737                         }
5738                     }
5739                 }
5740
5741               i += insn_32bit ? 4 : 2;
5742               last_was_32bit = insn_32bit;
5743               last_was_branch = is_32bit_branch;
5744             }
5745         }
5746
5747       if (elf_section_data (section)->this_hdr.contents == NULL)
5748         free (contents);
5749     }
5750
5751   *a8_fixes_p = a8_fixes;
5752   *num_a8_fixes_p = num_a8_fixes;
5753   *a8_fix_table_size_p = a8_fix_table_size;
5754
5755   return FALSE;
5756 }
5757
5758 /* Create or update a stub entry depending on whether the stub can already be
5759    found in HTAB.  The stub is identified by:
5760    - its type STUB_TYPE
5761    - its source branch (note that several can share the same stub) whose
5762      section and relocation (if any) are given by SECTION and IRELA
5763      respectively
5764    - its target symbol whose input section, hash, name, value and branch type
5765      are given in SYM_SEC, HASH, SYM_NAME, SYM_VALUE and BRANCH_TYPE
5766      respectively
5767
5768    If found, the value of the stub's target symbol is updated from SYM_VALUE
5769    and *NEW_STUB is set to FALSE.  Otherwise, *NEW_STUB is set to
5770    TRUE and the stub entry is initialized.
5771
5772    Returns the stub that was created or updated, or NULL if an error
5773    occurred.  */
5774
5775 static struct elf32_arm_stub_hash_entry *
5776 elf32_arm_create_stub (struct elf32_arm_link_hash_table *htab,
5777                        enum elf32_arm_stub_type stub_type, asection *section,
5778                        Elf_Internal_Rela *irela, asection *sym_sec,
5779                        struct elf32_arm_link_hash_entry *hash, char *sym_name,
5780                        bfd_vma sym_value, enum arm_st_branch_type branch_type,
5781                        bfd_boolean *new_stub)
5782 {
5783   const asection *id_sec;
5784   char *stub_name;
5785   struct elf32_arm_stub_hash_entry *stub_entry;
5786   unsigned int r_type;
5787   bfd_boolean sym_claimed = arm_stub_sym_claimed (stub_type);
5788
5789   BFD_ASSERT (stub_type != arm_stub_none);
5790   *new_stub = FALSE;
5791
5792   if (sym_claimed)
5793     stub_name = sym_name;
5794   else
5795     {
5796       BFD_ASSERT (irela);
5797       BFD_ASSERT (section);
5798       BFD_ASSERT (section->id <= htab->top_id);
5799
5800       /* Support for grouping stub sections.  */
5801       id_sec = htab->stub_group[section->id].link_sec;
5802
5803       /* Get the name of this stub.  */
5804       stub_name = elf32_arm_stub_name (id_sec, sym_sec, hash, irela,
5805                                        stub_type);
5806       if (!stub_name)
5807         return NULL;
5808     }
5809
5810   stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name, FALSE,
5811                                      FALSE);
5812   /* The proper stub has already been created, just update its value.  */
5813   if (stub_entry != NULL)
5814     {
5815       if (!sym_claimed)
5816         free (stub_name);
5817       stub_entry->target_value = sym_value;
5818       return stub_entry;
5819     }
5820
5821   stub_entry = elf32_arm_add_stub (stub_name, section, htab, stub_type);
5822   if (stub_entry == NULL)
5823     {
5824       if (!sym_claimed)
5825         free (stub_name);
5826       return NULL;
5827     }
5828
5829   stub_entry->target_value = sym_value;
5830   stub_entry->target_section = sym_sec;
5831   stub_entry->stub_type = stub_type;
5832   stub_entry->h = hash;
5833   stub_entry->branch_type = branch_type;
5834
5835   if (sym_claimed)
5836     stub_entry->output_name = sym_name;
5837   else
5838     {
5839       if (sym_name == NULL)
5840         sym_name = "unnamed";
5841       stub_entry->output_name = (char *)
5842         bfd_alloc (htab->stub_bfd, sizeof (THUMB2ARM_GLUE_ENTRY_NAME)
5843                                    + strlen (sym_name));
5844       if (stub_entry->output_name == NULL)
5845         {
5846           free (stub_name);
5847           return NULL;
5848         }
5849
5850       /* For historical reasons, use the existing names for ARM-to-Thumb and
5851          Thumb-to-ARM stubs.  */
5852       r_type = ELF32_R_TYPE (irela->r_info);
5853       if ((r_type == (unsigned int) R_ARM_THM_CALL
5854            || r_type == (unsigned int) R_ARM_THM_JUMP24
5855            || r_type == (unsigned int) R_ARM_THM_JUMP19)
5856           && branch_type == ST_BRANCH_TO_ARM)
5857         sprintf (stub_entry->output_name, THUMB2ARM_GLUE_ENTRY_NAME, sym_name);
5858       else if ((r_type == (unsigned int) R_ARM_CALL
5859                 || r_type == (unsigned int) R_ARM_JUMP24)
5860                && branch_type == ST_BRANCH_TO_THUMB)
5861         sprintf (stub_entry->output_name, ARM2THUMB_GLUE_ENTRY_NAME, sym_name);
5862       else
5863         sprintf (stub_entry->output_name, STUB_ENTRY_NAME, sym_name);
5864     }
5865
5866   *new_stub = TRUE;
5867   return stub_entry;
5868 }
5869
5870 /* Scan symbols in INPUT_BFD to identify secure entry functions needing a
5871    gateway veneer to transition from non secure to secure state and create them
5872    accordingly.
5873
5874    "ARMv8-M Security Extensions: Requirements on Development Tools" document
5875    defines the conditions that govern Secure Gateway veneer creation for a
5876    given symbol <SYM> as follows:
5877    - it has function type
5878    - it has non local binding
5879    - a symbol named __acle_se_<SYM> (called special symbol) exists with the
5880      same type, binding and value as <SYM> (called normal symbol).
5881    An entry function can handle secure state transition itself in which case
5882    its special symbol would have a different value from the normal symbol.
5883
5884    OUT_ATTR gives the output attributes, SYM_HASHES the symbol index to hash
5885    entry mapping while HTAB gives the name to hash entry mapping.
5886    *CMSE_STUB_CREATED is increased by the number of secure gateway veneer
5887    created.
5888
5889    The return value gives whether a stub failed to be allocated.  */
5890
5891 static bfd_boolean
5892 cmse_scan (bfd *input_bfd, struct elf32_arm_link_hash_table *htab,
5893            obj_attribute *out_attr, struct elf_link_hash_entry **sym_hashes,
5894            int *cmse_stub_created)
5895 {
5896   const struct elf_backend_data *bed;
5897   Elf_Internal_Shdr *symtab_hdr;
5898   unsigned i, j, sym_count, ext_start;
5899   Elf_Internal_Sym *cmse_sym, *local_syms;
5900   struct elf32_arm_link_hash_entry *hash, *cmse_hash = NULL;
5901   enum arm_st_branch_type branch_type;
5902   char *sym_name, *lsym_name;
5903   bfd_vma sym_value;
5904   asection *section;
5905   struct elf32_arm_stub_hash_entry *stub_entry;
5906   bfd_boolean is_v8m, new_stub, cmse_invalid, ret = TRUE;
5907
5908   bed = get_elf_backend_data (input_bfd);
5909   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5910   sym_count = symtab_hdr->sh_size / bed->s->sizeof_sym;
5911   ext_start = symtab_hdr->sh_info;
5912   is_v8m = (out_attr[Tag_CPU_arch].i >= TAG_CPU_ARCH_V8M_BASE
5913             && out_attr[Tag_CPU_arch_profile].i == 'M');
5914
5915   local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
5916   if (local_syms == NULL)
5917     local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5918                                        symtab_hdr->sh_info, 0, NULL, NULL,
5919                                        NULL);
5920   if (symtab_hdr->sh_info && local_syms == NULL)
5921     return FALSE;
5922
5923   /* Scan symbols.  */
5924   for (i = 0; i < sym_count; i++)
5925     {
5926       cmse_invalid = FALSE;
5927
5928       if (i < ext_start)
5929         {
5930           cmse_sym = &local_syms[i];
5931           /* Not a special symbol.  */
5932           if (!ARM_GET_SYM_CMSE_SPCL (cmse_sym->st_target_internal))
5933             continue;
5934           sym_name = bfd_elf_string_from_elf_section (input_bfd,
5935                                                       symtab_hdr->sh_link,
5936                                                       cmse_sym->st_name);
5937           /* Special symbol with local binding.  */
5938           cmse_invalid = TRUE;
5939         }
5940       else
5941         {
5942           cmse_hash = elf32_arm_hash_entry (sym_hashes[i - ext_start]);
5943           sym_name = (char *) cmse_hash->root.root.root.string;
5944
5945           /* Not a special symbol.  */
5946           if (!ARM_GET_SYM_CMSE_SPCL (cmse_hash->root.target_internal))
5947             continue;
5948
5949           /* Special symbol has incorrect binding or type.  */
5950           if ((cmse_hash->root.root.type != bfd_link_hash_defined
5951                && cmse_hash->root.root.type != bfd_link_hash_defweak)
5952               || cmse_hash->root.type != STT_FUNC)
5953             cmse_invalid = TRUE;
5954         }
5955
5956       if (!is_v8m)
5957         {
5958           _bfd_error_handler (_("%pB: special symbol `%s' only allowed for "
5959                                 "ARMv8-M architecture or later"),
5960                               input_bfd, sym_name);
5961           is_v8m = TRUE; /* Avoid multiple warning.  */
5962           ret = FALSE;
5963         }
5964
5965       if (cmse_invalid)
5966         {
5967           _bfd_error_handler (_("%pB: invalid special symbol `%s'; it must be"
5968                                 " a global or weak function symbol"),
5969                               input_bfd, sym_name);
5970           ret = FALSE;
5971           if (i < ext_start)
5972             continue;
5973         }
5974
5975       sym_name += strlen (CMSE_PREFIX);
5976       hash = (struct elf32_arm_link_hash_entry *)
5977         elf_link_hash_lookup (&(htab)->root, sym_name, FALSE, FALSE, TRUE);
5978
5979       /* No associated normal symbol or it is neither global nor weak.  */
5980       if (!hash
5981           || (hash->root.root.type != bfd_link_hash_defined
5982               && hash->root.root.type != bfd_link_hash_defweak)
5983           || hash->root.type != STT_FUNC)
5984         {
5985           /* Initialize here to avoid warning about use of possibly
5986              uninitialized variable.  */
5987           j = 0;
5988
5989           if (!hash)
5990             {
5991               /* Searching for a normal symbol with local binding.  */
5992               for (; j < ext_start; j++)
5993                 {
5994                   lsym_name =
5995                     bfd_elf_string_from_elf_section (input_bfd,
5996                                                      symtab_hdr->sh_link,
5997                                                      local_syms[j].st_name);
5998                   if (!strcmp (sym_name, lsym_name))
5999                     break;
6000                 }
6001             }
6002
6003           if (hash || j < ext_start)
6004             {
6005               _bfd_error_handler
6006                 (_("%pB: invalid standard symbol `%s'; it must be "
6007                    "a global or weak function symbol"),
6008                  input_bfd, sym_name);
6009             }
6010           else
6011             _bfd_error_handler
6012               (_("%pB: absent standard symbol `%s'"), input_bfd, sym_name);
6013           ret = FALSE;
6014           if (!hash)
6015             continue;
6016         }
6017
6018       sym_value = hash->root.root.u.def.value;
6019       section = hash->root.root.u.def.section;
6020
6021       if (cmse_hash->root.root.u.def.section != section)
6022         {
6023           _bfd_error_handler
6024             (_("%pB: `%s' and its special symbol are in different sections"),
6025              input_bfd, sym_name);
6026           ret = FALSE;
6027         }
6028       if (cmse_hash->root.root.u.def.value != sym_value)
6029         continue; /* Ignore: could be an entry function starting with SG.  */
6030
6031         /* If this section is a link-once section that will be discarded, then
6032            don't create any stubs.  */
6033       if (section->output_section == NULL)
6034         {
6035           _bfd_error_handler
6036             (_("%pB: entry function `%s' not output"), input_bfd, sym_name);
6037           continue;
6038         }
6039
6040       if (hash->root.size == 0)
6041         {
6042           _bfd_error_handler
6043             (_("%pB: entry function `%s' is empty"), input_bfd, sym_name);
6044           ret = FALSE;
6045         }
6046
6047       if (!ret)
6048         continue;
6049       branch_type = ARM_GET_SYM_BRANCH_TYPE (hash->root.target_internal);
6050       stub_entry
6051         = elf32_arm_create_stub (htab, arm_stub_cmse_branch_thumb_only,
6052                                  NULL, NULL, section, hash, sym_name,
6053                                  sym_value, branch_type, &new_stub);
6054
6055       if (stub_entry == NULL)
6056          ret = FALSE;
6057       else
6058         {
6059           BFD_ASSERT (new_stub);
6060           (*cmse_stub_created)++;
6061         }
6062     }
6063
6064   if (!symtab_hdr->contents)
6065     free (local_syms);
6066   return ret;
6067 }
6068
6069 /* Return TRUE iff a symbol identified by its linker HASH entry is a secure
6070    code entry function, ie can be called from non secure code without using a
6071    veneer.  */
6072
6073 static bfd_boolean
6074 cmse_entry_fct_p (struct elf32_arm_link_hash_entry *hash)
6075 {
6076   bfd_byte contents[4];
6077   uint32_t first_insn;
6078   asection *section;
6079   file_ptr offset;
6080   bfd *abfd;
6081
6082   /* Defined symbol of function type.  */
6083   if (hash->root.root.type != bfd_link_hash_defined
6084       && hash->root.root.type != bfd_link_hash_defweak)
6085     return FALSE;
6086   if (hash->root.type != STT_FUNC)
6087     return FALSE;
6088
6089   /* Read first instruction.  */
6090   section = hash->root.root.u.def.section;
6091   abfd = section->owner;
6092   offset = hash->root.root.u.def.value - section->vma;
6093   if (!bfd_get_section_contents (abfd, section, contents, offset,
6094                                  sizeof (contents)))
6095     return FALSE;
6096
6097   first_insn = bfd_get_32 (abfd, contents);
6098
6099   /* Starts by SG instruction.  */
6100   return first_insn == 0xe97fe97f;
6101 }
6102
6103 /* Output the name (in symbol table) of the veneer GEN_ENTRY if it is a new
6104    secure gateway veneers (ie. the veneers was not in the input import library)
6105    and there is no output import library (GEN_INFO->out_implib_bfd is NULL.  */
6106
6107 static bfd_boolean
6108 arm_list_new_cmse_stub (struct bfd_hash_entry *gen_entry, void *gen_info)
6109 {
6110   struct elf32_arm_stub_hash_entry *stub_entry;
6111   struct bfd_link_info *info;
6112
6113   /* Massage our args to the form they really have.  */
6114   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
6115   info = (struct bfd_link_info *) gen_info;
6116
6117   if (info->out_implib_bfd)
6118     return TRUE;
6119
6120   if (stub_entry->stub_type != arm_stub_cmse_branch_thumb_only)
6121     return TRUE;
6122
6123   if (stub_entry->stub_offset == (bfd_vma) -1)
6124     _bfd_error_handler ("  %s", stub_entry->output_name);
6125
6126   return TRUE;
6127 }
6128
6129 /* Set offset of each secure gateway veneers so that its address remain
6130    identical to the one in the input import library referred by
6131    HTAB->in_implib_bfd.  A warning is issued for veneers that disappeared
6132    (present in input import library but absent from the executable being
6133    linked) or if new veneers appeared and there is no output import library
6134    (INFO->out_implib_bfd is NULL and *CMSE_STUB_CREATED is bigger than the
6135    number of secure gateway veneers found in the input import library.
6136
6137    The function returns whether an error occurred.  If no error occurred,
6138    *CMSE_STUB_CREATED gives the number of SG veneers created by both cmse_scan
6139    and this function and HTAB->new_cmse_stub_offset is set to the biggest
6140    veneer observed set for new veneers to be layed out after.  */
6141
6142 static bfd_boolean
6143 set_cmse_veneer_addr_from_implib (struct bfd_link_info *info,
6144                                   struct elf32_arm_link_hash_table *htab,
6145                                   int *cmse_stub_created)
6146 {
6147   long symsize;
6148   char *sym_name;
6149   flagword flags;
6150   long i, symcount;
6151   bfd *in_implib_bfd;
6152   asection *stub_out_sec;
6153   bfd_boolean ret = TRUE;
6154   Elf_Internal_Sym *intsym;
6155   const char *out_sec_name;
6156   bfd_size_type cmse_stub_size;
6157   asymbol **sympp = NULL, *sym;
6158   struct elf32_arm_link_hash_entry *hash;
6159   const insn_sequence *cmse_stub_template;
6160   struct elf32_arm_stub_hash_entry *stub_entry;
6161   int cmse_stub_template_size, new_cmse_stubs_created = *cmse_stub_created;
6162   bfd_vma veneer_value, stub_offset, next_cmse_stub_offset;
6163   bfd_vma cmse_stub_array_start = (bfd_vma) -1, cmse_stub_sec_vma = 0;
6164
6165   /* No input secure gateway import library.  */
6166   if (!htab->in_implib_bfd)
6167     return TRUE;
6168
6169   in_implib_bfd = htab->in_implib_bfd;
6170   if (!htab->cmse_implib)
6171     {
6172       _bfd_error_handler (_("%pB: --in-implib only supported for Secure "
6173                             "Gateway import libraries"), in_implib_bfd);
6174       return FALSE;
6175     }
6176
6177   /* Get symbol table size.  */
6178   symsize = bfd_get_symtab_upper_bound (in_implib_bfd);
6179   if (symsize < 0)
6180     return FALSE;
6181
6182   /* Read in the input secure gateway import library's symbol table.  */
6183   sympp = (asymbol **) xmalloc (symsize);
6184   symcount = bfd_canonicalize_symtab (in_implib_bfd, sympp);
6185   if (symcount < 0)
6186     {
6187       ret = FALSE;
6188       goto free_sym_buf;
6189     }
6190
6191   htab->new_cmse_stub_offset = 0;
6192   cmse_stub_size =
6193     find_stub_size_and_template (arm_stub_cmse_branch_thumb_only,
6194                                  &cmse_stub_template,
6195                                  &cmse_stub_template_size);
6196   out_sec_name =
6197     arm_dedicated_stub_output_section_name (arm_stub_cmse_branch_thumb_only);
6198   stub_out_sec =
6199     bfd_get_section_by_name (htab->obfd, out_sec_name);
6200   if (stub_out_sec != NULL)
6201     cmse_stub_sec_vma = stub_out_sec->vma;
6202
6203   /* Set addresses of veneers mentionned in input secure gateway import
6204      library's symbol table.  */
6205   for (i = 0; i < symcount; i++)
6206     {
6207       sym = sympp[i];
6208       flags = sym->flags;
6209       sym_name = (char *) bfd_asymbol_name (sym);
6210       intsym = &((elf_symbol_type *) sym)->internal_elf_sym;
6211
6212       if (sym->section != bfd_abs_section_ptr
6213           || !(flags & (BSF_GLOBAL | BSF_WEAK))
6214           || (flags & BSF_FUNCTION) != BSF_FUNCTION
6215           || (ARM_GET_SYM_BRANCH_TYPE (intsym->st_target_internal)
6216               != ST_BRANCH_TO_THUMB))
6217         {
6218           _bfd_error_handler (_("%pB: invalid import library entry: `%s'; "
6219                                 "symbol should be absolute, global and "
6220                                 "refer to Thumb functions"),
6221                               in_implib_bfd, sym_name);
6222           ret = FALSE;
6223           continue;
6224         }
6225
6226       veneer_value = bfd_asymbol_value (sym);
6227       stub_offset = veneer_value - cmse_stub_sec_vma;
6228       stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, sym_name,
6229                                          FALSE, FALSE);
6230       hash = (struct elf32_arm_link_hash_entry *)
6231         elf_link_hash_lookup (&(htab)->root, sym_name, FALSE, FALSE, TRUE);
6232
6233       /* Stub entry should have been created by cmse_scan or the symbol be of
6234          a secure function callable from non secure code.  */
6235       if (!stub_entry && !hash)
6236         {
6237           bfd_boolean new_stub;
6238
6239           _bfd_error_handler
6240             (_("entry function `%s' disappeared from secure code"), sym_name);
6241           hash = (struct elf32_arm_link_hash_entry *)
6242             elf_link_hash_lookup (&(htab)->root, sym_name, TRUE, TRUE, TRUE);
6243           stub_entry
6244             = elf32_arm_create_stub (htab, arm_stub_cmse_branch_thumb_only,
6245                                      NULL, NULL, bfd_abs_section_ptr, hash,
6246                                      sym_name, veneer_value,
6247                                      ST_BRANCH_TO_THUMB, &new_stub);
6248           if (stub_entry == NULL)
6249             ret = FALSE;
6250           else
6251           {
6252             BFD_ASSERT (new_stub);
6253             new_cmse_stubs_created++;
6254             (*cmse_stub_created)++;
6255           }
6256           stub_entry->stub_template_size = stub_entry->stub_size = 0;
6257           stub_entry->stub_offset = stub_offset;
6258         }
6259       /* Symbol found is not callable from non secure code.  */
6260       else if (!stub_entry)
6261         {
6262           if (!cmse_entry_fct_p (hash))
6263             {
6264               _bfd_error_handler (_("`%s' refers to a non entry function"),
6265                                   sym_name);
6266               ret = FALSE;
6267             }
6268           continue;
6269         }
6270       else
6271         {
6272           /* Only stubs for SG veneers should have been created.  */
6273           BFD_ASSERT (stub_entry->stub_type == arm_stub_cmse_branch_thumb_only);
6274
6275           /* Check visibility hasn't changed.  */
6276           if (!!(flags & BSF_GLOBAL)
6277               != (hash->root.root.type == bfd_link_hash_defined))
6278             _bfd_error_handler
6279               (_("%pB: visibility of symbol `%s' has changed"), in_implib_bfd,
6280                sym_name);
6281
6282           stub_entry->stub_offset = stub_offset;
6283         }
6284
6285       /* Size should match that of a SG veneer.  */
6286       if (intsym->st_size != cmse_stub_size)
6287         {
6288           _bfd_error_handler (_("%pB: incorrect size for symbol `%s'"),
6289                               in_implib_bfd, sym_name);
6290           ret = FALSE;
6291         }
6292
6293       /* Previous veneer address is before current SG veneer section.  */
6294       if (veneer_value < cmse_stub_sec_vma)
6295         {
6296           /* Avoid offset underflow.  */
6297           if (stub_entry)
6298             stub_entry->stub_offset = 0;
6299           stub_offset = 0;
6300           ret = FALSE;
6301         }
6302
6303       /* Complain if stub offset not a multiple of stub size.  */
6304       if (stub_offset % cmse_stub_size)
6305         {
6306           _bfd_error_handler
6307             (_("offset of veneer for entry function `%s' not a multiple of "
6308                "its size"), sym_name);
6309           ret = FALSE;
6310         }
6311
6312       if (!ret)
6313         continue;
6314
6315       new_cmse_stubs_created--;
6316       if (veneer_value < cmse_stub_array_start)
6317         cmse_stub_array_start = veneer_value;
6318       next_cmse_stub_offset = stub_offset + ((cmse_stub_size + 7) & ~7);
6319       if (next_cmse_stub_offset > htab->new_cmse_stub_offset)
6320         htab->new_cmse_stub_offset = next_cmse_stub_offset;
6321     }
6322
6323   if (!info->out_implib_bfd && new_cmse_stubs_created != 0)
6324     {
6325       BFD_ASSERT (new_cmse_stubs_created > 0);
6326       _bfd_error_handler
6327         (_("new entry function(s) introduced but no output import library "
6328            "specified:"));
6329       bfd_hash_traverse (&htab->stub_hash_table, arm_list_new_cmse_stub, info);
6330     }
6331
6332   if (cmse_stub_array_start != cmse_stub_sec_vma)
6333     {
6334       _bfd_error_handler
6335         (_("start address of `%s' is different from previous link"),
6336          out_sec_name);
6337       ret = FALSE;
6338     }
6339
6340 free_sym_buf:
6341   free (sympp);
6342   return ret;
6343 }
6344
6345 /* Determine and set the size of the stub section for a final link.
6346
6347    The basic idea here is to examine all the relocations looking for
6348    PC-relative calls to a target that is unreachable with a "bl"
6349    instruction.  */
6350
6351 bfd_boolean
6352 elf32_arm_size_stubs (bfd *output_bfd,
6353                       bfd *stub_bfd,
6354                       struct bfd_link_info *info,
6355                       bfd_signed_vma group_size,
6356                       asection * (*add_stub_section) (const char *, asection *,
6357                                                       asection *,
6358                                                       unsigned int),
6359                       void (*layout_sections_again) (void))
6360 {
6361   bfd_boolean ret = TRUE;
6362   obj_attribute *out_attr;
6363   int cmse_stub_created = 0;
6364   bfd_size_type stub_group_size;
6365   bfd_boolean m_profile, stubs_always_after_branch, first_veneer_scan = TRUE;
6366   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
6367   struct a8_erratum_fix *a8_fixes = NULL;
6368   unsigned int num_a8_fixes = 0, a8_fix_table_size = 10;
6369   struct a8_erratum_reloc *a8_relocs = NULL;
6370   unsigned int num_a8_relocs = 0, a8_reloc_table_size = 10, i;
6371
6372   if (htab == NULL)
6373     return FALSE;
6374
6375   if (htab->fix_cortex_a8)
6376     {
6377       a8_fixes = (struct a8_erratum_fix *)
6378           bfd_zmalloc (sizeof (struct a8_erratum_fix) * a8_fix_table_size);
6379       a8_relocs = (struct a8_erratum_reloc *)
6380           bfd_zmalloc (sizeof (struct a8_erratum_reloc) * a8_reloc_table_size);
6381     }
6382
6383   /* Propagate mach to stub bfd, because it may not have been
6384      finalized when we created stub_bfd.  */
6385   bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
6386                      bfd_get_mach (output_bfd));
6387
6388   /* Stash our params away.  */
6389   htab->stub_bfd = stub_bfd;
6390   htab->add_stub_section = add_stub_section;
6391   htab->layout_sections_again = layout_sections_again;
6392   stubs_always_after_branch = group_size < 0;
6393
6394   out_attr = elf_known_obj_attributes_proc (output_bfd);
6395   m_profile = out_attr[Tag_CPU_arch_profile].i == 'M';
6396
6397   /* The Cortex-A8 erratum fix depends on stubs not being in the same 4K page
6398      as the first half of a 32-bit branch straddling two 4K pages.  This is a
6399      crude way of enforcing that.  */
6400   if (htab->fix_cortex_a8)
6401     stubs_always_after_branch = 1;
6402
6403   if (group_size < 0)
6404     stub_group_size = -group_size;
6405   else
6406     stub_group_size = group_size;
6407
6408   if (stub_group_size == 1)
6409     {
6410       /* Default values.  */
6411       /* Thumb branch range is +-4MB has to be used as the default
6412          maximum size (a given section can contain both ARM and Thumb
6413          code, so the worst case has to be taken into account).
6414
6415          This value is 24K less than that, which allows for 2025
6416          12-byte stubs.  If we exceed that, then we will fail to link.
6417          The user will have to relink with an explicit group size
6418          option.  */
6419       stub_group_size = 4170000;
6420     }
6421
6422   group_sections (htab, stub_group_size, stubs_always_after_branch);
6423
6424   /* If we're applying the cortex A8 fix, we need to determine the
6425      program header size now, because we cannot change it later --
6426      that could alter section placements.  Notice the A8 erratum fix
6427      ends up requiring the section addresses to remain unchanged
6428      modulo the page size.  That's something we cannot represent
6429      inside BFD, and we don't want to force the section alignment to
6430      be the page size.  */
6431   if (htab->fix_cortex_a8)
6432     (*htab->layout_sections_again) ();
6433
6434   while (1)
6435     {
6436       bfd *input_bfd;
6437       unsigned int bfd_indx;
6438       asection *stub_sec;
6439       enum elf32_arm_stub_type stub_type;
6440       bfd_boolean stub_changed = FALSE;
6441       unsigned prev_num_a8_fixes = num_a8_fixes;
6442
6443       num_a8_fixes = 0;
6444       for (input_bfd = info->input_bfds, bfd_indx = 0;
6445            input_bfd != NULL;
6446            input_bfd = input_bfd->link.next, bfd_indx++)
6447         {
6448           Elf_Internal_Shdr *symtab_hdr;
6449           asection *section;
6450           Elf_Internal_Sym *local_syms = NULL;
6451
6452           if (!is_arm_elf (input_bfd))
6453             continue;
6454
6455           num_a8_relocs = 0;
6456
6457           /* We'll need the symbol table in a second.  */
6458           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6459           if (symtab_hdr->sh_info == 0)
6460             continue;
6461
6462           /* Limit scan of symbols to object file whose profile is
6463              Microcontroller to not hinder performance in the general case.  */
6464           if (m_profile && first_veneer_scan)
6465             {
6466               struct elf_link_hash_entry **sym_hashes;
6467
6468               sym_hashes = elf_sym_hashes (input_bfd);
6469               if (!cmse_scan (input_bfd, htab, out_attr, sym_hashes,
6470                               &cmse_stub_created))
6471                 goto error_ret_free_local;
6472
6473               if (cmse_stub_created != 0)
6474                 stub_changed = TRUE;
6475             }
6476
6477           /* Walk over each section attached to the input bfd.  */
6478           for (section = input_bfd->sections;
6479                section != NULL;
6480                section = section->next)
6481             {
6482               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
6483
6484               /* If there aren't any relocs, then there's nothing more
6485                  to do.  */
6486               if ((section->flags & SEC_RELOC) == 0
6487                   || section->reloc_count == 0
6488                   || (section->flags & SEC_CODE) == 0)
6489                 continue;
6490
6491               /* If this section is a link-once section that will be
6492                  discarded, then don't create any stubs.  */
6493               if (section->output_section == NULL
6494                   || section->output_section->owner != output_bfd)
6495                 continue;
6496
6497               /* Get the relocs.  */
6498               internal_relocs
6499                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
6500                                              NULL, info->keep_memory);
6501               if (internal_relocs == NULL)
6502                 goto error_ret_free_local;
6503
6504               /* Now examine each relocation.  */
6505               irela = internal_relocs;
6506               irelaend = irela + section->reloc_count;
6507               for (; irela < irelaend; irela++)
6508                 {
6509                   unsigned int r_type, r_indx;
6510                   asection *sym_sec;
6511                   bfd_vma sym_value;
6512                   bfd_vma destination;
6513                   struct elf32_arm_link_hash_entry *hash;
6514                   const char *sym_name;
6515                   unsigned char st_type;
6516                   enum arm_st_branch_type branch_type;
6517                   bfd_boolean created_stub = FALSE;
6518
6519                   r_type = ELF32_R_TYPE (irela->r_info);
6520                   r_indx = ELF32_R_SYM (irela->r_info);
6521
6522                   if (r_type >= (unsigned int) R_ARM_max)
6523                     {
6524                       bfd_set_error (bfd_error_bad_value);
6525                     error_ret_free_internal:
6526                       if (elf_section_data (section)->relocs == NULL)
6527                         free (internal_relocs);
6528                     /* Fall through.  */
6529                     error_ret_free_local:
6530                       if (local_syms != NULL
6531                           && (symtab_hdr->contents
6532                               != (unsigned char *) local_syms))
6533                         free (local_syms);
6534                       return FALSE;
6535                     }
6536
6537                   hash = NULL;
6538                   if (r_indx >= symtab_hdr->sh_info)
6539                     hash = elf32_arm_hash_entry
6540                       (elf_sym_hashes (input_bfd)
6541                        [r_indx - symtab_hdr->sh_info]);
6542
6543                   /* Only look for stubs on branch instructions, or
6544                      non-relaxed TLSCALL  */
6545                   if ((r_type != (unsigned int) R_ARM_CALL)
6546                       && (r_type != (unsigned int) R_ARM_THM_CALL)
6547                       && (r_type != (unsigned int) R_ARM_JUMP24)
6548                       && (r_type != (unsigned int) R_ARM_THM_JUMP19)
6549                       && (r_type != (unsigned int) R_ARM_THM_XPC22)
6550                       && (r_type != (unsigned int) R_ARM_THM_JUMP24)
6551                       && (r_type != (unsigned int) R_ARM_PLT32)
6552                       && !((r_type == (unsigned int) R_ARM_TLS_CALL
6553                             || r_type == (unsigned int) R_ARM_THM_TLS_CALL)
6554                            && r_type == elf32_arm_tls_transition
6555                                (info, r_type, &hash->root)
6556                            && ((hash ? hash->tls_type
6557                                 : (elf32_arm_local_got_tls_type
6558                                    (input_bfd)[r_indx]))
6559                                & GOT_TLS_GDESC) != 0))
6560                     continue;
6561
6562                   /* Now determine the call target, its name, value,
6563                      section.  */
6564                   sym_sec = NULL;
6565                   sym_value = 0;
6566                   destination = 0;
6567                   sym_name = NULL;
6568
6569                   if (r_type == (unsigned int) R_ARM_TLS_CALL
6570                       || r_type == (unsigned int) R_ARM_THM_TLS_CALL)
6571                     {
6572                       /* A non-relaxed TLS call.  The target is the
6573                          plt-resident trampoline and nothing to do
6574                          with the symbol.  */
6575                       BFD_ASSERT (htab->tls_trampoline > 0);
6576                       sym_sec = htab->root.splt;
6577                       sym_value = htab->tls_trampoline;
6578                       hash = 0;
6579                       st_type = STT_FUNC;
6580                       branch_type = ST_BRANCH_TO_ARM;
6581                     }
6582                   else if (!hash)
6583                     {
6584                       /* It's a local symbol.  */
6585                       Elf_Internal_Sym *sym;
6586
6587                       if (local_syms == NULL)
6588                         {
6589                           local_syms
6590                             = (Elf_Internal_Sym *) symtab_hdr->contents;
6591                           if (local_syms == NULL)
6592                             local_syms
6593                               = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
6594                                                       symtab_hdr->sh_info, 0,
6595                                                       NULL, NULL, NULL);
6596                           if (local_syms == NULL)
6597                             goto error_ret_free_internal;
6598                         }
6599
6600                       sym = local_syms + r_indx;
6601                       if (sym->st_shndx == SHN_UNDEF)
6602                         sym_sec = bfd_und_section_ptr;
6603                       else if (sym->st_shndx == SHN_ABS)
6604                         sym_sec = bfd_abs_section_ptr;
6605                       else if (sym->st_shndx == SHN_COMMON)
6606                         sym_sec = bfd_com_section_ptr;
6607                       else
6608                         sym_sec =
6609                           bfd_section_from_elf_index (input_bfd, sym->st_shndx);
6610
6611                       if (!sym_sec)
6612                         /* This is an undefined symbol.  It can never
6613                            be resolved.  */
6614                         continue;
6615
6616                       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
6617                         sym_value = sym->st_value;
6618                       destination = (sym_value + irela->r_addend
6619                                      + sym_sec->output_offset
6620                                      + sym_sec->output_section->vma);
6621                       st_type = ELF_ST_TYPE (sym->st_info);
6622                       branch_type =
6623                         ARM_GET_SYM_BRANCH_TYPE (sym->st_target_internal);
6624                       sym_name
6625                         = bfd_elf_string_from_elf_section (input_bfd,
6626                                                            symtab_hdr->sh_link,
6627                                                            sym->st_name);
6628                     }
6629                   else
6630                     {
6631                       /* It's an external symbol.  */
6632                       while (hash->root.root.type == bfd_link_hash_indirect
6633                              || hash->root.root.type == bfd_link_hash_warning)
6634                         hash = ((struct elf32_arm_link_hash_entry *)
6635                                 hash->root.root.u.i.link);
6636
6637                       if (hash->root.root.type == bfd_link_hash_defined
6638                           || hash->root.root.type == bfd_link_hash_defweak)
6639                         {
6640                           sym_sec = hash->root.root.u.def.section;
6641                           sym_value = hash->root.root.u.def.value;
6642
6643                           struct elf32_arm_link_hash_table *globals =
6644                                                   elf32_arm_hash_table (info);
6645
6646                           /* For a destination in a shared library,
6647                              use the PLT stub as target address to
6648                              decide whether a branch stub is
6649                              needed.  */
6650                           if (globals != NULL
6651                               && globals->root.splt != NULL
6652                               && hash != NULL
6653                               && hash->root.plt.offset != (bfd_vma) -1)
6654                             {
6655                               sym_sec = globals->root.splt;
6656                               sym_value = hash->root.plt.offset;
6657                               if (sym_sec->output_section != NULL)
6658                                 destination = (sym_value
6659                                                + sym_sec->output_offset
6660                                                + sym_sec->output_section->vma);
6661                             }
6662                           else if (sym_sec->output_section != NULL)
6663                             destination = (sym_value + irela->r_addend
6664                                            + sym_sec->output_offset
6665                                            + sym_sec->output_section->vma);
6666                         }
6667                       else if ((hash->root.root.type == bfd_link_hash_undefined)
6668                                || (hash->root.root.type == bfd_link_hash_undefweak))
6669                         {
6670                           /* For a shared library, use the PLT stub as
6671                              target address to decide whether a long
6672                              branch stub is needed.
6673                              For absolute code, they cannot be handled.  */
6674                           struct elf32_arm_link_hash_table *globals =
6675                             elf32_arm_hash_table (info);
6676
6677                           if (globals != NULL
6678                               && globals->root.splt != NULL
6679                               && hash != NULL
6680                               && hash->root.plt.offset != (bfd_vma) -1)
6681                             {
6682                               sym_sec = globals->root.splt;
6683                               sym_value = hash->root.plt.offset;
6684                               if (sym_sec->output_section != NULL)
6685                                 destination = (sym_value
6686                                                + sym_sec->output_offset
6687                                                + sym_sec->output_section->vma);
6688                             }
6689                           else
6690                             continue;
6691                         }
6692                       else
6693                         {
6694                           bfd_set_error (bfd_error_bad_value);
6695                           goto error_ret_free_internal;
6696                         }
6697                       st_type = hash->root.type;
6698                       branch_type =
6699                         ARM_GET_SYM_BRANCH_TYPE (hash->root.target_internal);
6700                       sym_name = hash->root.root.root.string;
6701                     }
6702
6703                   do
6704                     {
6705                       bfd_boolean new_stub;
6706                       struct elf32_arm_stub_hash_entry *stub_entry;
6707
6708                       /* Determine what (if any) linker stub is needed.  */
6709                       stub_type = arm_type_of_stub (info, section, irela,
6710                                                     st_type, &branch_type,
6711                                                     hash, destination, sym_sec,
6712                                                     input_bfd, sym_name);
6713                       if (stub_type == arm_stub_none)
6714                         break;
6715
6716                       /* We've either created a stub for this reloc already,
6717                          or we are about to.  */
6718                       stub_entry =
6719                         elf32_arm_create_stub (htab, stub_type, section, irela,
6720                                                sym_sec, hash,
6721                                                (char *) sym_name, sym_value,
6722                                                branch_type, &new_stub);
6723
6724                       created_stub = stub_entry != NULL;
6725                       if (!created_stub)
6726                         goto error_ret_free_internal;
6727                       else if (!new_stub)
6728                         break;
6729                       else
6730                         stub_changed = TRUE;
6731                     }
6732                   while (0);
6733
6734                   /* Look for relocations which might trigger Cortex-A8
6735                      erratum.  */
6736                   if (htab->fix_cortex_a8
6737                       && (r_type == (unsigned int) R_ARM_THM_JUMP24
6738                           || r_type == (unsigned int) R_ARM_THM_JUMP19
6739                           || r_type == (unsigned int) R_ARM_THM_CALL
6740                           || r_type == (unsigned int) R_ARM_THM_XPC22))
6741                     {
6742                       bfd_vma from = section->output_section->vma
6743                                      + section->output_offset
6744                                      + irela->r_offset;
6745
6746                       if ((from & 0xfff) == 0xffe)
6747                         {
6748                           /* Found a candidate.  Note we haven't checked the
6749                              destination is within 4K here: if we do so (and
6750                              don't create an entry in a8_relocs) we can't tell
6751                              that a branch should have been relocated when
6752                              scanning later.  */
6753                           if (num_a8_relocs == a8_reloc_table_size)
6754                             {
6755                               a8_reloc_table_size *= 2;
6756                               a8_relocs = (struct a8_erratum_reloc *)
6757                                   bfd_realloc (a8_relocs,
6758                                                sizeof (struct a8_erratum_reloc)
6759                                                * a8_reloc_table_size);
6760                             }
6761
6762                           a8_relocs[num_a8_relocs].from = from;
6763                           a8_relocs[num_a8_relocs].destination = destination;
6764                           a8_relocs[num_a8_relocs].r_type = r_type;
6765                           a8_relocs[num_a8_relocs].branch_type = branch_type;
6766                           a8_relocs[num_a8_relocs].sym_name = sym_name;
6767                           a8_relocs[num_a8_relocs].non_a8_stub = created_stub;
6768                           a8_relocs[num_a8_relocs].hash = hash;
6769
6770                           num_a8_relocs++;
6771                         }
6772                     }
6773                 }
6774
6775               /* We're done with the internal relocs, free them.  */
6776               if (elf_section_data (section)->relocs == NULL)
6777                 free (internal_relocs);
6778             }
6779
6780           if (htab->fix_cortex_a8)
6781             {
6782               /* Sort relocs which might apply to Cortex-A8 erratum.  */
6783               qsort (a8_relocs, num_a8_relocs,
6784                      sizeof (struct a8_erratum_reloc),
6785                      &a8_reloc_compare);
6786
6787               /* Scan for branches which might trigger Cortex-A8 erratum.  */
6788               if (cortex_a8_erratum_scan (input_bfd, info, &a8_fixes,
6789                                           &num_a8_fixes, &a8_fix_table_size,
6790                                           a8_relocs, num_a8_relocs,
6791                                           prev_num_a8_fixes, &stub_changed)
6792                   != 0)
6793                 goto error_ret_free_local;
6794             }
6795
6796           if (local_syms != NULL
6797               && symtab_hdr->contents != (unsigned char *) local_syms)
6798             {
6799               if (!info->keep_memory)
6800                 free (local_syms);
6801               else
6802                 symtab_hdr->contents = (unsigned char *) local_syms;
6803             }
6804         }
6805
6806       if (first_veneer_scan
6807           && !set_cmse_veneer_addr_from_implib (info, htab,
6808                                                 &cmse_stub_created))
6809         ret = FALSE;
6810
6811       if (prev_num_a8_fixes != num_a8_fixes)
6812         stub_changed = TRUE;
6813
6814       if (!stub_changed)
6815         break;
6816
6817       /* OK, we've added some stubs.  Find out the new size of the
6818          stub sections.  */
6819       for (stub_sec = htab->stub_bfd->sections;
6820            stub_sec != NULL;
6821            stub_sec = stub_sec->next)
6822         {
6823           /* Ignore non-stub sections.  */
6824           if (!strstr (stub_sec->name, STUB_SUFFIX))
6825             continue;
6826
6827           stub_sec->size = 0;
6828         }
6829
6830       /* Add new SG veneers after those already in the input import
6831          library.  */
6832       for (stub_type = arm_stub_none + 1; stub_type < max_stub_type;
6833            stub_type++)
6834         {
6835           bfd_vma *start_offset_p;
6836           asection **stub_sec_p;
6837
6838           start_offset_p = arm_new_stubs_start_offset_ptr (htab, stub_type);
6839           stub_sec_p = arm_dedicated_stub_input_section_ptr (htab, stub_type);
6840           if (start_offset_p == NULL)
6841             continue;
6842
6843           BFD_ASSERT (stub_sec_p != NULL);
6844           if (*stub_sec_p != NULL)
6845             (*stub_sec_p)->size = *start_offset_p;
6846         }
6847
6848       /* Compute stub section size, considering padding.  */
6849       bfd_hash_traverse (&htab->stub_hash_table, arm_size_one_stub, htab);
6850       for (stub_type = arm_stub_none + 1; stub_type < max_stub_type;
6851            stub_type++)
6852         {
6853           int size, padding;
6854           asection **stub_sec_p;
6855
6856           padding = arm_dedicated_stub_section_padding (stub_type);
6857           stub_sec_p = arm_dedicated_stub_input_section_ptr (htab, stub_type);
6858           /* Skip if no stub input section or no stub section padding
6859              required.  */
6860           if ((stub_sec_p != NULL && *stub_sec_p == NULL) || padding == 0)
6861             continue;
6862           /* Stub section padding required but no dedicated section.  */
6863           BFD_ASSERT (stub_sec_p);
6864
6865           size = (*stub_sec_p)->size;
6866           size = (size + padding - 1) & ~(padding - 1);
6867           (*stub_sec_p)->size = size;
6868         }
6869
6870       /* Add Cortex-A8 erratum veneers to stub section sizes too.  */
6871       if (htab->fix_cortex_a8)
6872         for (i = 0; i < num_a8_fixes; i++)
6873           {
6874             stub_sec = elf32_arm_create_or_find_stub_sec (NULL,
6875                          a8_fixes[i].section, htab, a8_fixes[i].stub_type);
6876
6877             if (stub_sec == NULL)
6878               return FALSE;
6879
6880             stub_sec->size
6881               += find_stub_size_and_template (a8_fixes[i].stub_type, NULL,
6882                                               NULL);
6883           }
6884
6885
6886       /* Ask the linker to do its stuff.  */
6887       (*htab->layout_sections_again) ();
6888       first_veneer_scan = FALSE;
6889     }
6890
6891   /* Add stubs for Cortex-A8 erratum fixes now.  */
6892   if (htab->fix_cortex_a8)
6893     {
6894       for (i = 0; i < num_a8_fixes; i++)
6895         {
6896           struct elf32_arm_stub_hash_entry *stub_entry;
6897           char *stub_name = a8_fixes[i].stub_name;
6898           asection *section = a8_fixes[i].section;
6899           unsigned int section_id = a8_fixes[i].section->id;
6900           asection *link_sec = htab->stub_group[section_id].link_sec;
6901           asection *stub_sec = htab->stub_group[section_id].stub_sec;
6902           const insn_sequence *template_sequence;
6903           int template_size, size = 0;
6904
6905           stub_entry = arm_stub_hash_lookup (&htab->stub_hash_table, stub_name,
6906                                              TRUE, FALSE);
6907           if (stub_entry == NULL)
6908             {
6909               _bfd_error_handler (_("%pB: cannot create stub entry %s"),
6910                                   section->owner, stub_name);
6911               return FALSE;
6912             }
6913
6914           stub_entry->stub_sec = stub_sec;
6915           stub_entry->stub_offset = (bfd_vma) -1;
6916           stub_entry->id_sec = link_sec;
6917           stub_entry->stub_type = a8_fixes[i].stub_type;
6918           stub_entry->source_value = a8_fixes[i].offset;
6919           stub_entry->target_section = a8_fixes[i].section;
6920           stub_entry->target_value = a8_fixes[i].target_offset;
6921           stub_entry->orig_insn = a8_fixes[i].orig_insn;
6922           stub_entry->branch_type = a8_fixes[i].branch_type;
6923
6924           size = find_stub_size_and_template (a8_fixes[i].stub_type,
6925                                               &template_sequence,
6926                                               &template_size);
6927
6928           stub_entry->stub_size = size;
6929           stub_entry->stub_template = template_sequence;
6930           stub_entry->stub_template_size = template_size;
6931         }
6932
6933       /* Stash the Cortex-A8 erratum fix array for use later in
6934          elf32_arm_write_section().  */
6935       htab->a8_erratum_fixes = a8_fixes;
6936       htab->num_a8_erratum_fixes = num_a8_fixes;
6937     }
6938   else
6939     {
6940       htab->a8_erratum_fixes = NULL;
6941       htab->num_a8_erratum_fixes = 0;
6942     }
6943   return ret;
6944 }
6945
6946 /* Build all the stubs associated with the current output file.  The
6947    stubs are kept in a hash table attached to the main linker hash
6948    table.  We also set up the .plt entries for statically linked PIC
6949    functions here.  This function is called via arm_elf_finish in the
6950    linker.  */
6951
6952 bfd_boolean
6953 elf32_arm_build_stubs (struct bfd_link_info *info)
6954 {
6955   asection *stub_sec;
6956   struct bfd_hash_table *table;
6957   enum elf32_arm_stub_type stub_type;
6958   struct elf32_arm_link_hash_table *htab;
6959
6960   htab = elf32_arm_hash_table (info);
6961   if (htab == NULL)
6962     return FALSE;
6963
6964   for (stub_sec = htab->stub_bfd->sections;
6965        stub_sec != NULL;
6966        stub_sec = stub_sec->next)
6967     {
6968       bfd_size_type size;
6969
6970       /* Ignore non-stub sections.  */
6971       if (!strstr (stub_sec->name, STUB_SUFFIX))
6972         continue;
6973
6974       /* Allocate memory to hold the linker stubs.  Zeroing the stub sections
6975          must at least be done for stub section requiring padding and for SG
6976          veneers to ensure that a non secure code branching to a removed SG
6977          veneer causes an error.  */
6978       size = stub_sec->size;
6979       stub_sec->contents = (unsigned char *) bfd_zalloc (htab->stub_bfd, size);
6980       if (stub_sec->contents == NULL && size != 0)
6981         return FALSE;
6982
6983       stub_sec->size = 0;
6984     }
6985
6986   /* Add new SG veneers after those already in the input import library.  */
6987   for (stub_type = arm_stub_none + 1; stub_type < max_stub_type; stub_type++)
6988     {
6989       bfd_vma *start_offset_p;
6990       asection **stub_sec_p;
6991
6992       start_offset_p = arm_new_stubs_start_offset_ptr (htab, stub_type);
6993       stub_sec_p = arm_dedicated_stub_input_section_ptr (htab, stub_type);
6994       if (start_offset_p == NULL)
6995         continue;
6996
6997       BFD_ASSERT (stub_sec_p != NULL);
6998       if (*stub_sec_p != NULL)
6999         (*stub_sec_p)->size = *start_offset_p;
7000     }
7001
7002   /* Build the stubs as directed by the stub hash table.  */
7003   table = &htab->stub_hash_table;
7004   bfd_hash_traverse (table, arm_build_one_stub, info);
7005   if (htab->fix_cortex_a8)
7006     {
7007       /* Place the cortex a8 stubs last.  */
7008       htab->fix_cortex_a8 = -1;
7009       bfd_hash_traverse (table, arm_build_one_stub, info);
7010     }
7011
7012   return TRUE;
7013 }
7014
7015 /* Locate the Thumb encoded calling stub for NAME.  */
7016
7017 static struct elf_link_hash_entry *
7018 find_thumb_glue (struct bfd_link_info *link_info,
7019                  const char *name,
7020                  char **error_message)
7021 {
7022   char *tmp_name;
7023   struct elf_link_hash_entry *hash;
7024   struct elf32_arm_link_hash_table *hash_table;
7025
7026   /* We need a pointer to the armelf specific hash table.  */
7027   hash_table = elf32_arm_hash_table (link_info);
7028   if (hash_table == NULL)
7029     return NULL;
7030
7031   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
7032                                   + strlen (THUMB2ARM_GLUE_ENTRY_NAME) + 1);
7033
7034   BFD_ASSERT (tmp_name);
7035
7036   sprintf (tmp_name, THUMB2ARM_GLUE_ENTRY_NAME, name);
7037
7038   hash = elf_link_hash_lookup
7039     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
7040
7041   if (hash == NULL
7042       && asprintf (error_message, _("unable to find %s glue '%s' for '%s'"),
7043                    "Thumb", tmp_name, name) == -1)
7044     *error_message = (char *) bfd_errmsg (bfd_error_system_call);
7045
7046   free (tmp_name);
7047
7048   return hash;
7049 }
7050
7051 /* Locate the ARM encoded calling stub for NAME.  */
7052
7053 static struct elf_link_hash_entry *
7054 find_arm_glue (struct bfd_link_info *link_info,
7055                const char *name,
7056                char **error_message)
7057 {
7058   char *tmp_name;
7059   struct elf_link_hash_entry *myh;
7060   struct elf32_arm_link_hash_table *hash_table;
7061
7062   /* We need a pointer to the elfarm specific hash table.  */
7063   hash_table = elf32_arm_hash_table (link_info);
7064   if (hash_table == NULL)
7065     return NULL;
7066
7067   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
7068                                   + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
7069
7070   BFD_ASSERT (tmp_name);
7071
7072   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
7073
7074   myh = elf_link_hash_lookup
7075     (&(hash_table)->root, tmp_name, FALSE, FALSE, TRUE);
7076
7077   if (myh == NULL
7078       && asprintf (error_message, _("unable to find %s glue '%s' for '%s'"),
7079                    "ARM", tmp_name, name) == -1)
7080     *error_message = (char *) bfd_errmsg (bfd_error_system_call);
7081
7082   free (tmp_name);
7083
7084   return myh;
7085 }
7086
7087 /* ARM->Thumb glue (static images):
7088
7089    .arm
7090    __func_from_arm:
7091    ldr r12, __func_addr
7092    bx  r12
7093    __func_addr:
7094    .word func    @ behave as if you saw a ARM_32 reloc.
7095
7096    (v5t static images)
7097    .arm
7098    __func_from_arm:
7099    ldr pc, __func_addr
7100    __func_addr:
7101    .word func    @ behave as if you saw a ARM_32 reloc.
7102
7103    (relocatable images)
7104    .arm
7105    __func_from_arm:
7106    ldr r12, __func_offset
7107    add r12, r12, pc
7108    bx  r12
7109    __func_offset:
7110    .word func - .   */
7111
7112 #define ARM2THUMB_STATIC_GLUE_SIZE 12
7113 static const insn32 a2t1_ldr_insn = 0xe59fc000;
7114 static const insn32 a2t2_bx_r12_insn = 0xe12fff1c;
7115 static const insn32 a2t3_func_addr_insn = 0x00000001;
7116
7117 #define ARM2THUMB_V5_STATIC_GLUE_SIZE 8
7118 static const insn32 a2t1v5_ldr_insn = 0xe51ff004;
7119 static const insn32 a2t2v5_func_addr_insn = 0x00000001;
7120
7121 #define ARM2THUMB_PIC_GLUE_SIZE 16
7122 static const insn32 a2t1p_ldr_insn = 0xe59fc004;
7123 static const insn32 a2t2p_add_pc_insn = 0xe08cc00f;
7124 static const insn32 a2t3p_bx_r12_insn = 0xe12fff1c;
7125
7126 /* Thumb->ARM:                          Thumb->(non-interworking aware) ARM
7127
7128      .thumb                             .thumb
7129      .align 2                           .align 2
7130  __func_from_thumb:                 __func_from_thumb:
7131      bx pc                              push {r6, lr}
7132      nop                                ldr  r6, __func_addr
7133      .arm                               mov  lr, pc
7134      b func                             bx   r6
7135                                         .arm
7136                                     ;; back_to_thumb
7137                                         ldmia r13! {r6, lr}
7138                                         bx    lr
7139                                     __func_addr:
7140                                         .word        func  */
7141
7142 #define THUMB2ARM_GLUE_SIZE 8
7143 static const insn16 t2a1_bx_pc_insn = 0x4778;
7144 static const insn16 t2a2_noop_insn = 0x46c0;
7145 static const insn32 t2a3_b_insn = 0xea000000;
7146
7147 #define VFP11_ERRATUM_VENEER_SIZE 8
7148 #define STM32L4XX_ERRATUM_LDM_VENEER_SIZE 16
7149 #define STM32L4XX_ERRATUM_VLDM_VENEER_SIZE 24
7150
7151 #define ARM_BX_VENEER_SIZE 12
7152 static const insn32 armbx1_tst_insn = 0xe3100001;
7153 static const insn32 armbx2_moveq_insn = 0x01a0f000;
7154 static const insn32 armbx3_bx_insn = 0xe12fff10;
7155
7156 #ifndef ELFARM_NABI_C_INCLUDED
7157 static void
7158 arm_allocate_glue_section_space (bfd * abfd, bfd_size_type size, const char * name)
7159 {
7160   asection * s;
7161   bfd_byte * contents;
7162
7163   if (size == 0)
7164     {
7165       /* Do not include empty glue sections in the output.  */
7166       if (abfd != NULL)
7167         {
7168           s = bfd_get_linker_section (abfd, name);
7169           if (s != NULL)
7170             s->flags |= SEC_EXCLUDE;
7171         }
7172       return;
7173     }
7174
7175   BFD_ASSERT (abfd != NULL);
7176
7177   s = bfd_get_linker_section (abfd, name);
7178   BFD_ASSERT (s != NULL);
7179
7180   contents = (bfd_byte *) bfd_alloc (abfd, size);
7181
7182   BFD_ASSERT (s->size == size);
7183   s->contents = contents;
7184 }
7185
7186 bfd_boolean
7187 bfd_elf32_arm_allocate_interworking_sections (struct bfd_link_info * info)
7188 {
7189   struct elf32_arm_link_hash_table * globals;
7190
7191   globals = elf32_arm_hash_table (info);
7192   BFD_ASSERT (globals != NULL);
7193
7194   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
7195                                    globals->arm_glue_size,
7196                                    ARM2THUMB_GLUE_SECTION_NAME);
7197
7198   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
7199                                    globals->thumb_glue_size,
7200                                    THUMB2ARM_GLUE_SECTION_NAME);
7201
7202   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
7203                                    globals->vfp11_erratum_glue_size,
7204                                    VFP11_ERRATUM_VENEER_SECTION_NAME);
7205
7206   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
7207                                    globals->stm32l4xx_erratum_glue_size,
7208                                    STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
7209
7210   arm_allocate_glue_section_space (globals->bfd_of_glue_owner,
7211                                    globals->bx_glue_size,
7212                                    ARM_BX_GLUE_SECTION_NAME);
7213
7214   return TRUE;
7215 }
7216
7217 /* Allocate space and symbols for calling a Thumb function from Arm mode.
7218    returns the symbol identifying the stub.  */
7219
7220 static struct elf_link_hash_entry *
7221 record_arm_to_thumb_glue (struct bfd_link_info * link_info,
7222                           struct elf_link_hash_entry * h)
7223 {
7224   const char * name = h->root.root.string;
7225   asection * s;
7226   char * tmp_name;
7227   struct elf_link_hash_entry * myh;
7228   struct bfd_link_hash_entry * bh;
7229   struct elf32_arm_link_hash_table * globals;
7230   bfd_vma val;
7231   bfd_size_type size;
7232
7233   globals = elf32_arm_hash_table (link_info);
7234   BFD_ASSERT (globals != NULL);
7235   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
7236
7237   s = bfd_get_linker_section
7238     (globals->bfd_of_glue_owner, ARM2THUMB_GLUE_SECTION_NAME);
7239
7240   BFD_ASSERT (s != NULL);
7241
7242   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen (name)
7243                                   + strlen (ARM2THUMB_GLUE_ENTRY_NAME) + 1);
7244
7245   BFD_ASSERT (tmp_name);
7246
7247   sprintf (tmp_name, ARM2THUMB_GLUE_ENTRY_NAME, name);
7248
7249   myh = elf_link_hash_lookup
7250     (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
7251
7252   if (myh != NULL)
7253     {
7254       /* We've already seen this guy.  */
7255       free (tmp_name);
7256       return myh;
7257     }
7258
7259   /* The only trick here is using hash_table->arm_glue_size as the value.
7260      Even though the section isn't allocated yet, this is where we will be
7261      putting it.  The +1 on the value marks that the stub has not been
7262      output yet - not that it is a Thumb function.  */
7263   bh = NULL;
7264   val = globals->arm_glue_size + 1;
7265   _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
7266                                     tmp_name, BSF_GLOBAL, s, val,
7267                                     NULL, TRUE, FALSE, &bh);
7268
7269   myh = (struct elf_link_hash_entry *) bh;
7270   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7271   myh->forced_local = 1;
7272
7273   free (tmp_name);
7274
7275   if (bfd_link_pic (link_info)
7276       || globals->root.is_relocatable_executable
7277       || globals->pic_veneer)
7278     size = ARM2THUMB_PIC_GLUE_SIZE;
7279   else if (globals->use_blx)
7280     size = ARM2THUMB_V5_STATIC_GLUE_SIZE;
7281   else
7282     size = ARM2THUMB_STATIC_GLUE_SIZE;
7283
7284   s->size += size;
7285   globals->arm_glue_size += size;
7286
7287   return myh;
7288 }
7289
7290 /* Allocate space for ARMv4 BX veneers.  */
7291
7292 static void
7293 record_arm_bx_glue (struct bfd_link_info * link_info, int reg)
7294 {
7295   asection * s;
7296   struct elf32_arm_link_hash_table *globals;
7297   char *tmp_name;
7298   struct elf_link_hash_entry *myh;
7299   struct bfd_link_hash_entry *bh;
7300   bfd_vma val;
7301
7302   /* BX PC does not need a veneer.  */
7303   if (reg == 15)
7304     return;
7305
7306   globals = elf32_arm_hash_table (link_info);
7307   BFD_ASSERT (globals != NULL);
7308   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
7309
7310   /* Check if this veneer has already been allocated.  */
7311   if (globals->bx_glue_offset[reg])
7312     return;
7313
7314   s = bfd_get_linker_section
7315     (globals->bfd_of_glue_owner, ARM_BX_GLUE_SECTION_NAME);
7316
7317   BFD_ASSERT (s != NULL);
7318
7319   /* Add symbol for veneer.  */
7320   tmp_name = (char *)
7321       bfd_malloc ((bfd_size_type) strlen (ARM_BX_GLUE_ENTRY_NAME) + 1);
7322
7323   BFD_ASSERT (tmp_name);
7324
7325   sprintf (tmp_name, ARM_BX_GLUE_ENTRY_NAME, reg);
7326
7327   myh = elf_link_hash_lookup
7328     (&(globals)->root, tmp_name, FALSE, FALSE, FALSE);
7329
7330   BFD_ASSERT (myh == NULL);
7331
7332   bh = NULL;
7333   val = globals->bx_glue_size;
7334   _bfd_generic_link_add_one_symbol (link_info, globals->bfd_of_glue_owner,
7335                                     tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
7336                                     NULL, TRUE, FALSE, &bh);
7337
7338   myh = (struct elf_link_hash_entry *) bh;
7339   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7340   myh->forced_local = 1;
7341
7342   s->size += ARM_BX_VENEER_SIZE;
7343   globals->bx_glue_offset[reg] = globals->bx_glue_size | 2;
7344   globals->bx_glue_size += ARM_BX_VENEER_SIZE;
7345 }
7346
7347
7348 /* Add an entry to the code/data map for section SEC.  */
7349
7350 static void
7351 elf32_arm_section_map_add (asection *sec, char type, bfd_vma vma)
7352 {
7353   struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
7354   unsigned int newidx;
7355
7356   if (sec_data->map == NULL)
7357     {
7358       sec_data->map = (elf32_arm_section_map *)
7359           bfd_malloc (sizeof (elf32_arm_section_map));
7360       sec_data->mapcount = 0;
7361       sec_data->mapsize = 1;
7362     }
7363
7364   newidx = sec_data->mapcount++;
7365
7366   if (sec_data->mapcount > sec_data->mapsize)
7367     {
7368       sec_data->mapsize *= 2;
7369       sec_data->map = (elf32_arm_section_map *)
7370           bfd_realloc_or_free (sec_data->map, sec_data->mapsize
7371                                * sizeof (elf32_arm_section_map));
7372     }
7373
7374   if (sec_data->map)
7375     {
7376       sec_data->map[newidx].vma = vma;
7377       sec_data->map[newidx].type = type;
7378     }
7379 }
7380
7381
7382 /* Record information about a VFP11 denorm-erratum veneer.  Only ARM-mode
7383    veneers are handled for now.  */
7384
7385 static bfd_vma
7386 record_vfp11_erratum_veneer (struct bfd_link_info *link_info,
7387                              elf32_vfp11_erratum_list *branch,
7388                              bfd *branch_bfd,
7389                              asection *branch_sec,
7390                              unsigned int offset)
7391 {
7392   asection *s;
7393   struct elf32_arm_link_hash_table *hash_table;
7394   char *tmp_name;
7395   struct elf_link_hash_entry *myh;
7396   struct bfd_link_hash_entry *bh;
7397   bfd_vma val;
7398   struct _arm_elf_section_data *sec_data;
7399   elf32_vfp11_erratum_list *newerr;
7400
7401   hash_table = elf32_arm_hash_table (link_info);
7402   BFD_ASSERT (hash_table != NULL);
7403   BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
7404
7405   s = bfd_get_linker_section
7406     (hash_table->bfd_of_glue_owner, VFP11_ERRATUM_VENEER_SECTION_NAME);
7407
7408   sec_data = elf32_arm_section_data (s);
7409
7410   BFD_ASSERT (s != NULL);
7411
7412   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
7413                                   (VFP11_ERRATUM_VENEER_ENTRY_NAME) + 10);
7414
7415   BFD_ASSERT (tmp_name);
7416
7417   sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME,
7418            hash_table->num_vfp11_fixes);
7419
7420   myh = elf_link_hash_lookup
7421     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
7422
7423   BFD_ASSERT (myh == NULL);
7424
7425   bh = NULL;
7426   val = hash_table->vfp11_erratum_glue_size;
7427   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
7428                                     tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
7429                                     NULL, TRUE, FALSE, &bh);
7430
7431   myh = (struct elf_link_hash_entry *) bh;
7432   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7433   myh->forced_local = 1;
7434
7435   /* Link veneer back to calling location.  */
7436   sec_data->erratumcount += 1;
7437   newerr = (elf32_vfp11_erratum_list *)
7438       bfd_zmalloc (sizeof (elf32_vfp11_erratum_list));
7439
7440   newerr->type = VFP11_ERRATUM_ARM_VENEER;
7441   newerr->vma = -1;
7442   newerr->u.v.branch = branch;
7443   newerr->u.v.id = hash_table->num_vfp11_fixes;
7444   branch->u.b.veneer = newerr;
7445
7446   newerr->next = sec_data->erratumlist;
7447   sec_data->erratumlist = newerr;
7448
7449   /* A symbol for the return from the veneer.  */
7450   sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME "_r",
7451            hash_table->num_vfp11_fixes);
7452
7453   myh = elf_link_hash_lookup
7454     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
7455
7456   if (myh != NULL)
7457     abort ();
7458
7459   bh = NULL;
7460   val = offset + 4;
7461   _bfd_generic_link_add_one_symbol (link_info, branch_bfd, tmp_name, BSF_LOCAL,
7462                                     branch_sec, val, NULL, TRUE, FALSE, &bh);
7463
7464   myh = (struct elf_link_hash_entry *) bh;
7465   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7466   myh->forced_local = 1;
7467
7468   free (tmp_name);
7469
7470   /* Generate a mapping symbol for the veneer section, and explicitly add an
7471      entry for that symbol to the code/data map for the section.  */
7472   if (hash_table->vfp11_erratum_glue_size == 0)
7473     {
7474       bh = NULL;
7475       /* FIXME: Creates an ARM symbol.  Thumb mode will need attention if it
7476          ever requires this erratum fix.  */
7477       _bfd_generic_link_add_one_symbol (link_info,
7478                                         hash_table->bfd_of_glue_owner, "$a",
7479                                         BSF_LOCAL, s, 0, NULL,
7480                                         TRUE, FALSE, &bh);
7481
7482       myh = (struct elf_link_hash_entry *) bh;
7483       myh->type = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
7484       myh->forced_local = 1;
7485
7486       /* The elf32_arm_init_maps function only cares about symbols from input
7487          BFDs.  We must make a note of this generated mapping symbol
7488          ourselves so that code byteswapping works properly in
7489          elf32_arm_write_section.  */
7490       elf32_arm_section_map_add (s, 'a', 0);
7491     }
7492
7493   s->size += VFP11_ERRATUM_VENEER_SIZE;
7494   hash_table->vfp11_erratum_glue_size += VFP11_ERRATUM_VENEER_SIZE;
7495   hash_table->num_vfp11_fixes++;
7496
7497   /* The offset of the veneer.  */
7498   return val;
7499 }
7500
7501 /* Record information about a STM32L4XX STM erratum veneer.  Only THUMB-mode
7502    veneers need to be handled because used only in Cortex-M.  */
7503
7504 static bfd_vma
7505 record_stm32l4xx_erratum_veneer (struct bfd_link_info *link_info,
7506                                  elf32_stm32l4xx_erratum_list *branch,
7507                                  bfd *branch_bfd,
7508                                  asection *branch_sec,
7509                                  unsigned int offset,
7510                                  bfd_size_type veneer_size)
7511 {
7512   asection *s;
7513   struct elf32_arm_link_hash_table *hash_table;
7514   char *tmp_name;
7515   struct elf_link_hash_entry *myh;
7516   struct bfd_link_hash_entry *bh;
7517   bfd_vma val;
7518   struct _arm_elf_section_data *sec_data;
7519   elf32_stm32l4xx_erratum_list *newerr;
7520
7521   hash_table = elf32_arm_hash_table (link_info);
7522   BFD_ASSERT (hash_table != NULL);
7523   BFD_ASSERT (hash_table->bfd_of_glue_owner != NULL);
7524
7525   s = bfd_get_linker_section
7526     (hash_table->bfd_of_glue_owner, STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
7527
7528   BFD_ASSERT (s != NULL);
7529
7530   sec_data = elf32_arm_section_data (s);
7531
7532   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
7533                                   (STM32L4XX_ERRATUM_VENEER_ENTRY_NAME) + 10);
7534
7535   BFD_ASSERT (tmp_name);
7536
7537   sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME,
7538            hash_table->num_stm32l4xx_fixes);
7539
7540   myh = elf_link_hash_lookup
7541     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
7542
7543   BFD_ASSERT (myh == NULL);
7544
7545   bh = NULL;
7546   val = hash_table->stm32l4xx_erratum_glue_size;
7547   _bfd_generic_link_add_one_symbol (link_info, hash_table->bfd_of_glue_owner,
7548                                     tmp_name, BSF_FUNCTION | BSF_LOCAL, s, val,
7549                                     NULL, TRUE, FALSE, &bh);
7550
7551   myh = (struct elf_link_hash_entry *) bh;
7552   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7553   myh->forced_local = 1;
7554
7555   /* Link veneer back to calling location.  */
7556   sec_data->stm32l4xx_erratumcount += 1;
7557   newerr = (elf32_stm32l4xx_erratum_list *)
7558       bfd_zmalloc (sizeof (elf32_stm32l4xx_erratum_list));
7559
7560   newerr->type = STM32L4XX_ERRATUM_VENEER;
7561   newerr->vma = -1;
7562   newerr->u.v.branch = branch;
7563   newerr->u.v.id = hash_table->num_stm32l4xx_fixes;
7564   branch->u.b.veneer = newerr;
7565
7566   newerr->next = sec_data->stm32l4xx_erratumlist;
7567   sec_data->stm32l4xx_erratumlist = newerr;
7568
7569   /* A symbol for the return from the veneer.  */
7570   sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME "_r",
7571            hash_table->num_stm32l4xx_fixes);
7572
7573   myh = elf_link_hash_lookup
7574     (&(hash_table)->root, tmp_name, FALSE, FALSE, FALSE);
7575
7576   if (myh != NULL)
7577     abort ();
7578
7579   bh = NULL;
7580   val = offset + 4;
7581   _bfd_generic_link_add_one_symbol (link_info, branch_bfd, tmp_name, BSF_LOCAL,
7582                                     branch_sec, val, NULL, TRUE, FALSE, &bh);
7583
7584   myh = (struct elf_link_hash_entry *) bh;
7585   myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
7586   myh->forced_local = 1;
7587
7588   free (tmp_name);
7589
7590   /* Generate a mapping symbol for the veneer section, and explicitly add an
7591      entry for that symbol to the code/data map for the section.  */
7592   if (hash_table->stm32l4xx_erratum_glue_size == 0)
7593     {
7594       bh = NULL;
7595       /* Creates a THUMB symbol since there is no other choice.  */
7596       _bfd_generic_link_add_one_symbol (link_info,
7597                                         hash_table->bfd_of_glue_owner, "$t",
7598                                         BSF_LOCAL, s, 0, NULL,
7599                                         TRUE, FALSE, &bh);
7600
7601       myh = (struct elf_link_hash_entry *) bh;
7602       myh->type = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
7603       myh->forced_local = 1;
7604
7605       /* The elf32_arm_init_maps function only cares about symbols from input
7606          BFDs.  We must make a note of this generated mapping symbol
7607          ourselves so that code byteswapping works properly in
7608          elf32_arm_write_section.  */
7609       elf32_arm_section_map_add (s, 't', 0);
7610     }
7611
7612   s->size += veneer_size;
7613   hash_table->stm32l4xx_erratum_glue_size += veneer_size;
7614   hash_table->num_stm32l4xx_fixes++;
7615
7616   /* The offset of the veneer.  */
7617   return val;
7618 }
7619
7620 #define ARM_GLUE_SECTION_FLAGS \
7621   (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_CODE \
7622    | SEC_READONLY | SEC_LINKER_CREATED)
7623
7624 /* Create a fake section for use by the ARM backend of the linker.  */
7625
7626 static bfd_boolean
7627 arm_make_glue_section (bfd * abfd, const char * name)
7628 {
7629   asection * sec;
7630
7631   sec = bfd_get_linker_section (abfd, name);
7632   if (sec != NULL)
7633     /* Already made.  */
7634     return TRUE;
7635
7636   sec = bfd_make_section_anyway_with_flags (abfd, name, ARM_GLUE_SECTION_FLAGS);
7637
7638   if (sec == NULL
7639       || !bfd_set_section_alignment (abfd, sec, 2))
7640     return FALSE;
7641
7642   /* Set the gc mark to prevent the section from being removed by garbage
7643      collection, despite the fact that no relocs refer to this section.  */
7644   sec->gc_mark = 1;
7645
7646   return TRUE;
7647 }
7648
7649 /* Set size of .plt entries.  This function is called from the
7650    linker scripts in ld/emultempl/{armelf}.em.  */
7651
7652 void
7653 bfd_elf32_arm_use_long_plt (void)
7654 {
7655   elf32_arm_use_long_plt_entry = TRUE;
7656 }
7657
7658 /* Add the glue sections to ABFD.  This function is called from the
7659    linker scripts in ld/emultempl/{armelf}.em.  */
7660
7661 bfd_boolean
7662 bfd_elf32_arm_add_glue_sections_to_bfd (bfd *abfd,
7663                                         struct bfd_link_info *info)
7664 {
7665   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
7666   bfd_boolean dostm32l4xx = globals
7667     && globals->stm32l4xx_fix != BFD_ARM_STM32L4XX_FIX_NONE;
7668   bfd_boolean addglue;
7669
7670   /* If we are only performing a partial
7671      link do not bother adding the glue.  */
7672   if (bfd_link_relocatable (info))
7673     return TRUE;
7674
7675   addglue = arm_make_glue_section (abfd, ARM2THUMB_GLUE_SECTION_NAME)
7676     && arm_make_glue_section (abfd, THUMB2ARM_GLUE_SECTION_NAME)
7677     && arm_make_glue_section (abfd, VFP11_ERRATUM_VENEER_SECTION_NAME)
7678     && arm_make_glue_section (abfd, ARM_BX_GLUE_SECTION_NAME);
7679
7680   if (!dostm32l4xx)
7681     return addglue;
7682
7683   return addglue
7684     && arm_make_glue_section (abfd, STM32L4XX_ERRATUM_VENEER_SECTION_NAME);
7685 }
7686
7687 /* Mark output sections of veneers needing a dedicated one with SEC_KEEP.  This
7688    ensures they are not marked for deletion by
7689    strip_excluded_output_sections () when veneers are going to be created
7690    later.  Not doing so would trigger assert on empty section size in
7691    lang_size_sections_1 ().  */
7692
7693 void
7694 bfd_elf32_arm_keep_private_stub_output_sections (struct bfd_link_info *info)
7695 {
7696   enum elf32_arm_stub_type stub_type;
7697
7698   /* If we are only performing a partial
7699      link do not bother adding the glue.  */
7700   if (bfd_link_relocatable (info))
7701     return;
7702
7703   for (stub_type = arm_stub_none + 1; stub_type < max_stub_type; stub_type++)
7704     {
7705       asection *out_sec;
7706       const char *out_sec_name;
7707
7708       if (!arm_dedicated_stub_output_section_required (stub_type))
7709         continue;
7710
7711      out_sec_name = arm_dedicated_stub_output_section_name (stub_type);
7712      out_sec = bfd_get_section_by_name (info->output_bfd, out_sec_name);
7713      if (out_sec != NULL)
7714         out_sec->flags |= SEC_KEEP;
7715     }
7716 }
7717
7718 /* Select a BFD to be used to hold the sections used by the glue code.
7719    This function is called from the linker scripts in ld/emultempl/
7720    {armelf/pe}.em.  */
7721
7722 bfd_boolean
7723 bfd_elf32_arm_get_bfd_for_interworking (bfd *abfd, struct bfd_link_info *info)
7724 {
7725   struct elf32_arm_link_hash_table *globals;
7726
7727   /* If we are only performing a partial link
7728      do not bother getting a bfd to hold the glue.  */
7729   if (bfd_link_relocatable (info))
7730     return TRUE;
7731
7732   /* Make sure we don't attach the glue sections to a dynamic object.  */
7733   BFD_ASSERT (!(abfd->flags & DYNAMIC));
7734
7735   globals = elf32_arm_hash_table (info);
7736   BFD_ASSERT (globals != NULL);
7737
7738   if (globals->bfd_of_glue_owner != NULL)
7739     return TRUE;
7740
7741   /* Save the bfd for later use.  */
7742   globals->bfd_of_glue_owner = abfd;
7743
7744   return TRUE;
7745 }
7746
7747 static void
7748 check_use_blx (struct elf32_arm_link_hash_table *globals)
7749 {
7750   int cpu_arch;
7751
7752   cpu_arch = bfd_elf_get_obj_attr_int (globals->obfd, OBJ_ATTR_PROC,
7753                                        Tag_CPU_arch);
7754
7755   if (globals->fix_arm1176)
7756     {
7757       if (cpu_arch == TAG_CPU_ARCH_V6T2 || cpu_arch > TAG_CPU_ARCH_V6K)
7758         globals->use_blx = 1;
7759     }
7760   else
7761     {
7762       if (cpu_arch > TAG_CPU_ARCH_V4T)
7763         globals->use_blx = 1;
7764     }
7765 }
7766
7767 bfd_boolean
7768 bfd_elf32_arm_process_before_allocation (bfd *abfd,
7769                                          struct bfd_link_info *link_info)
7770 {
7771   Elf_Internal_Shdr *symtab_hdr;
7772   Elf_Internal_Rela *internal_relocs = NULL;
7773   Elf_Internal_Rela *irel, *irelend;
7774   bfd_byte *contents = NULL;
7775
7776   asection *sec;
7777   struct elf32_arm_link_hash_table *globals;
7778
7779   /* If we are only performing a partial link do not bother
7780      to construct any glue.  */
7781   if (bfd_link_relocatable (link_info))
7782     return TRUE;
7783
7784   /* Here we have a bfd that is to be included on the link.  We have a
7785      hook to do reloc rummaging, before section sizes are nailed down.  */
7786   globals = elf32_arm_hash_table (link_info);
7787   BFD_ASSERT (globals != NULL);
7788
7789   check_use_blx (globals);
7790
7791   if (globals->byteswap_code && !bfd_big_endian (abfd))
7792     {
7793       _bfd_error_handler (_("%pB: BE8 images only valid in big-endian mode"),
7794                           abfd);
7795       return FALSE;
7796     }
7797
7798   /* PR 5398: If we have not decided to include any loadable sections in
7799      the output then we will not have a glue owner bfd.  This is OK, it
7800      just means that there is nothing else for us to do here.  */
7801   if (globals->bfd_of_glue_owner == NULL)
7802     return TRUE;
7803
7804   /* Rummage around all the relocs and map the glue vectors.  */
7805   sec = abfd->sections;
7806
7807   if (sec == NULL)
7808     return TRUE;
7809
7810   for (; sec != NULL; sec = sec->next)
7811     {
7812       if (sec->reloc_count == 0)
7813         continue;
7814
7815       if ((sec->flags & SEC_EXCLUDE) != 0)
7816         continue;
7817
7818       symtab_hdr = & elf_symtab_hdr (abfd);
7819
7820       /* Load the relocs.  */
7821       internal_relocs
7822         = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, FALSE);
7823
7824       if (internal_relocs == NULL)
7825         goto error_return;
7826
7827       irelend = internal_relocs + sec->reloc_count;
7828       for (irel = internal_relocs; irel < irelend; irel++)
7829         {
7830           long r_type;
7831           unsigned long r_index;
7832
7833           struct elf_link_hash_entry *h;
7834
7835           r_type = ELF32_R_TYPE (irel->r_info);
7836           r_index = ELF32_R_SYM (irel->r_info);
7837
7838           /* These are the only relocation types we care about.  */
7839           if (   r_type != R_ARM_PC24
7840               && (r_type != R_ARM_V4BX || globals->fix_v4bx < 2))
7841             continue;
7842
7843           /* Get the section contents if we haven't done so already.  */
7844           if (contents == NULL)
7845             {
7846               /* Get cached copy if it exists.  */
7847               if (elf_section_data (sec)->this_hdr.contents != NULL)
7848                 contents = elf_section_data (sec)->this_hdr.contents;
7849               else
7850                 {
7851                   /* Go get them off disk.  */
7852                   if (! bfd_malloc_and_get_section (abfd, sec, &contents))
7853                     goto error_return;
7854                 }
7855             }
7856
7857           if (r_type == R_ARM_V4BX)
7858             {
7859               int reg;
7860
7861               reg = bfd_get_32 (abfd, contents + irel->r_offset) & 0xf;
7862               record_arm_bx_glue (link_info, reg);
7863               continue;
7864             }
7865
7866           /* If the relocation is not against a symbol it cannot concern us.  */
7867           h = NULL;
7868
7869           /* We don't care about local symbols.  */
7870           if (r_index < symtab_hdr->sh_info)
7871             continue;
7872
7873           /* This is an external symbol.  */
7874           r_index -= symtab_hdr->sh_info;
7875           h = (struct elf_link_hash_entry *)
7876             elf_sym_hashes (abfd)[r_index];
7877
7878           /* If the relocation is against a static symbol it must be within
7879              the current section and so cannot be a cross ARM/Thumb relocation.  */
7880           if (h == NULL)
7881             continue;
7882
7883           /* If the call will go through a PLT entry then we do not need
7884              glue.  */
7885           if (globals->root.splt != NULL && h->plt.offset != (bfd_vma) -1)
7886             continue;
7887
7888           switch (r_type)
7889             {
7890             case R_ARM_PC24:
7891               /* This one is a call from arm code.  We need to look up
7892                  the target of the call.  If it is a thumb target, we
7893                  insert glue.  */
7894               if (ARM_GET_SYM_BRANCH_TYPE (h->target_internal)
7895                   == ST_BRANCH_TO_THUMB)
7896                 record_arm_to_thumb_glue (link_info, h);
7897               break;
7898
7899             default:
7900               abort ();
7901             }
7902         }
7903
7904       if (contents != NULL
7905           && elf_section_data (sec)->this_hdr.contents != contents)
7906         free (contents);
7907       contents = NULL;
7908
7909       if (internal_relocs != NULL
7910           && elf_section_data (sec)->relocs != internal_relocs)
7911         free (internal_relocs);
7912       internal_relocs = NULL;
7913     }
7914
7915   return TRUE;
7916
7917 error_return:
7918   if (contents != NULL
7919       && elf_section_data (sec)->this_hdr.contents != contents)
7920     free (contents);
7921   if (internal_relocs != NULL
7922       && elf_section_data (sec)->relocs != internal_relocs)
7923     free (internal_relocs);
7924
7925   return FALSE;
7926 }
7927 #endif
7928
7929
7930 /* Initialise maps of ARM/Thumb/data for input BFDs.  */
7931
7932 void
7933 bfd_elf32_arm_init_maps (bfd *abfd)
7934 {
7935   Elf_Internal_Sym *isymbuf;
7936   Elf_Internal_Shdr *hdr;
7937   unsigned int i, localsyms;
7938
7939   /* PR 7093: Make sure that we are dealing with an arm elf binary.  */
7940   if (! is_arm_elf (abfd))
7941     return;
7942
7943   if ((abfd->flags & DYNAMIC) != 0)
7944     return;
7945
7946   hdr = & elf_symtab_hdr (abfd);
7947   localsyms = hdr->sh_info;
7948
7949   /* Obtain a buffer full of symbols for this BFD. The hdr->sh_info field
7950      should contain the number of local symbols, which should come before any
7951      global symbols.  Mapping symbols are always local.  */
7952   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, localsyms, 0, NULL, NULL,
7953                                   NULL);
7954
7955   /* No internal symbols read?  Skip this BFD.  */
7956   if (isymbuf == NULL)
7957     return;
7958
7959   for (i = 0; i < localsyms; i++)
7960     {
7961       Elf_Internal_Sym *isym = &isymbuf[i];
7962       asection *sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7963       const char *name;
7964
7965       if (sec != NULL
7966           && ELF_ST_BIND (isym->st_info) == STB_LOCAL)
7967         {
7968           name = bfd_elf_string_from_elf_section (abfd,
7969             hdr->sh_link, isym->st_name);
7970
7971           if (bfd_is_arm_special_symbol_name (name,
7972                                               BFD_ARM_SPECIAL_SYM_TYPE_MAP))
7973             elf32_arm_section_map_add (sec, name[1], isym->st_value);
7974         }
7975     }
7976 }
7977
7978
7979 /* Auto-select enabling of Cortex-A8 erratum fix if the user didn't explicitly
7980    say what they wanted.  */
7981
7982 void
7983 bfd_elf32_arm_set_cortex_a8_fix (bfd *obfd, struct bfd_link_info *link_info)
7984 {
7985   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
7986   obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
7987
7988   if (globals == NULL)
7989     return;
7990
7991   if (globals->fix_cortex_a8 == -1)
7992     {
7993       /* Turn on Cortex-A8 erratum workaround for ARMv7-A.  */
7994       if (out_attr[Tag_CPU_arch].i == TAG_CPU_ARCH_V7
7995           && (out_attr[Tag_CPU_arch_profile].i == 'A'
7996               || out_attr[Tag_CPU_arch_profile].i == 0))
7997         globals->fix_cortex_a8 = 1;
7998       else
7999         globals->fix_cortex_a8 = 0;
8000     }
8001 }
8002
8003
8004 void
8005 bfd_elf32_arm_set_vfp11_fix (bfd *obfd, struct bfd_link_info *link_info)
8006 {
8007   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
8008   obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
8009
8010   if (globals == NULL)
8011     return;
8012   /* We assume that ARMv7+ does not need the VFP11 denorm erratum fix.  */
8013   if (out_attr[Tag_CPU_arch].i >= TAG_CPU_ARCH_V7)
8014     {
8015       switch (globals->vfp11_fix)
8016         {
8017         case BFD_ARM_VFP11_FIX_DEFAULT:
8018         case BFD_ARM_VFP11_FIX_NONE:
8019           globals->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
8020           break;
8021
8022         default:
8023           /* Give a warning, but do as the user requests anyway.  */
8024           _bfd_error_handler (_("%pB: warning: selected VFP11 erratum "
8025             "workaround is not necessary for target architecture"), obfd);
8026         }
8027     }
8028   else if (globals->vfp11_fix == BFD_ARM_VFP11_FIX_DEFAULT)
8029     /* For earlier architectures, we might need the workaround, but do not
8030        enable it by default.  If users is running with broken hardware, they
8031        must enable the erratum fix explicitly.  */
8032     globals->vfp11_fix = BFD_ARM_VFP11_FIX_NONE;
8033 }
8034
8035 void
8036 bfd_elf32_arm_set_stm32l4xx_fix (bfd *obfd, struct bfd_link_info *link_info)
8037 {
8038   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
8039   obj_attribute *out_attr = elf_known_obj_attributes_proc (obfd);
8040
8041   if (globals == NULL)
8042     return;
8043
8044   /* We assume only Cortex-M4 may require the fix.  */
8045   if (out_attr[Tag_CPU_arch].i != TAG_CPU_ARCH_V7E_M
8046       || out_attr[Tag_CPU_arch_profile].i != 'M')
8047     {
8048       if (globals->stm32l4xx_fix != BFD_ARM_STM32L4XX_FIX_NONE)
8049         /* Give a warning, but do as the user requests anyway.  */
8050         _bfd_error_handler
8051           (_("%pB: warning: selected STM32L4XX erratum "
8052              "workaround is not necessary for target architecture"), obfd);
8053     }
8054 }
8055
8056 enum bfd_arm_vfp11_pipe
8057 {
8058   VFP11_FMAC,
8059   VFP11_LS,
8060   VFP11_DS,
8061   VFP11_BAD
8062 };
8063
8064 /* Return a VFP register number.  This is encoded as RX:X for single-precision
8065    registers, or X:RX for double-precision registers, where RX is the group of
8066    four bits in the instruction encoding and X is the single extension bit.
8067    RX and X fields are specified using their lowest (starting) bit.  The return
8068    value is:
8069
8070      0...31: single-precision registers s0...s31
8071      32...63: double-precision registers d0...d31.
8072
8073    Although X should be zero for VFP11 (encoding d0...d15 only), we might
8074    encounter VFP3 instructions, so we allow the full range for DP registers.  */
8075
8076 static unsigned int
8077 bfd_arm_vfp11_regno (unsigned int insn, bfd_boolean is_double, unsigned int rx,
8078                      unsigned int x)
8079 {
8080   if (is_double)
8081     return (((insn >> rx) & 0xf) | (((insn >> x) & 1) << 4)) + 32;
8082   else
8083     return (((insn >> rx) & 0xf) << 1) | ((insn >> x) & 1);
8084 }
8085
8086 /* Set bits in *WMASK according to a register number REG as encoded by
8087    bfd_arm_vfp11_regno().  Ignore d16-d31.  */
8088
8089 static void
8090 bfd_arm_vfp11_write_mask (unsigned int *wmask, unsigned int reg)
8091 {
8092   if (reg < 32)
8093     *wmask |= 1 << reg;
8094   else if (reg < 48)
8095     *wmask |= 3 << ((reg - 32) * 2);
8096 }
8097
8098 /* Return TRUE if WMASK overwrites anything in REGS.  */
8099
8100 static bfd_boolean
8101 bfd_arm_vfp11_antidependency (unsigned int wmask, int *regs, int numregs)
8102 {
8103   int i;
8104
8105   for (i = 0; i < numregs; i++)
8106     {
8107       unsigned int reg = regs[i];
8108
8109       if (reg < 32 && (wmask & (1 << reg)) != 0)
8110         return TRUE;
8111
8112       reg -= 32;
8113
8114       if (reg >= 16)
8115         continue;
8116
8117       if ((wmask & (3 << (reg * 2))) != 0)
8118         return TRUE;
8119     }
8120
8121   return FALSE;
8122 }
8123
8124 /* In this function, we're interested in two things: finding input registers
8125    for VFP data-processing instructions, and finding the set of registers which
8126    arbitrary VFP instructions may write to.  We use a 32-bit unsigned int to
8127    hold the written set, so FLDM etc. are easy to deal with (we're only
8128    interested in 32 SP registers or 16 dp registers, due to the VFP version
8129    implemented by the chip in question).  DP registers are marked by setting
8130    both SP registers in the write mask).  */
8131
8132 static enum bfd_arm_vfp11_pipe
8133 bfd_arm_vfp11_insn_decode (unsigned int insn, unsigned int *destmask, int *regs,
8134                            int *numregs)
8135 {
8136   enum bfd_arm_vfp11_pipe vpipe = VFP11_BAD;
8137   bfd_boolean is_double = ((insn & 0xf00) == 0xb00) ? 1 : 0;
8138
8139   if ((insn & 0x0f000e10) == 0x0e000a00)  /* A data-processing insn.  */
8140     {
8141       unsigned int pqrs;
8142       unsigned int fd = bfd_arm_vfp11_regno (insn, is_double, 12, 22);
8143       unsigned int fm = bfd_arm_vfp11_regno (insn, is_double, 0, 5);
8144
8145       pqrs = ((insn & 0x00800000) >> 20)
8146            | ((insn & 0x00300000) >> 19)
8147            | ((insn & 0x00000040) >> 6);
8148
8149       switch (pqrs)
8150         {
8151         case 0: /* fmac[sd].  */
8152         case 1: /* fnmac[sd].  */
8153         case 2: /* fmsc[sd].  */
8154         case 3: /* fnmsc[sd].  */
8155           vpipe = VFP11_FMAC;
8156           bfd_arm_vfp11_write_mask (destmask, fd);
8157           regs[0] = fd;
8158           regs[1] = bfd_arm_vfp11_regno (insn, is_double, 16, 7);  /* Fn.  */
8159           regs[2] = fm;
8160           *numregs = 3;
8161           break;
8162
8163         case 4: /* fmul[sd].  */
8164         case 5: /* fnmul[sd].  */
8165         case 6: /* fadd[sd].  */
8166         case 7: /* fsub[sd].  */
8167           vpipe = VFP11_FMAC;
8168           goto vfp_binop;
8169
8170         case 8: /* fdiv[sd].  */
8171           vpipe = VFP11_DS;
8172           vfp_binop:
8173           bfd_arm_vfp11_write_mask (destmask, fd);
8174           regs[0] = bfd_arm_vfp11_regno (insn, is_double, 16, 7);   /* Fn.  */
8175           regs[1] = fm;
8176           *numregs = 2;
8177           break;
8178
8179         case 15: /* extended opcode.  */
8180           {
8181             unsigned int extn = ((insn >> 15) & 0x1e)
8182                               | ((insn >> 7) & 1);
8183
8184             switch (extn)
8185               {
8186               case 0: /* fcpy[sd].  */
8187               case 1: /* fabs[sd].  */
8188               case 2: /* fneg[sd].  */
8189               case 8: /* fcmp[sd].  */
8190               case 9: /* fcmpe[sd].  */
8191               case 10: /* fcmpz[sd].  */
8192               case 11: /* fcmpez[sd].  */
8193               case 16: /* fuito[sd].  */
8194               case 17: /* fsito[sd].  */
8195               case 24: /* ftoui[sd].  */
8196               case 25: /* ftouiz[sd].  */
8197               case 26: /* ftosi[sd].  */
8198               case 27: /* ftosiz[sd].  */
8199                 /* These instructions will not bounce due to underflow.  */
8200                 *numregs = 0;
8201                 vpipe = VFP11_FMAC;
8202                 break;
8203
8204               case 3: /* fsqrt[sd].  */
8205                 /* fsqrt cannot underflow, but it can (perhaps) overwrite
8206                    registers to cause the erratum in previous instructions.  */
8207                 bfd_arm_vfp11_write_mask (destmask, fd);
8208                 vpipe = VFP11_DS;
8209                 break;
8210
8211               case 15: /* fcvt{ds,sd}.  */
8212                 {
8213                   int rnum = 0;
8214
8215                   bfd_arm_vfp11_write_mask (destmask, fd);
8216
8217                   /* Only FCVTSD can underflow.  */
8218                   if ((insn & 0x100) != 0)
8219                     regs[rnum++] = fm;
8220
8221                   *numregs = rnum;
8222
8223                   vpipe = VFP11_FMAC;
8224                 }
8225                 break;
8226
8227               default:
8228                 return VFP11_BAD;
8229               }
8230           }
8231           break;
8232
8233         default:
8234           return VFP11_BAD;
8235         }
8236     }
8237   /* Two-register transfer.  */
8238   else if ((insn & 0x0fe00ed0) == 0x0c400a10)
8239     {
8240       unsigned int fm = bfd_arm_vfp11_regno (insn, is_double, 0, 5);
8241
8242       if ((insn & 0x100000) == 0)
8243         {
8244           if (is_double)
8245             bfd_arm_vfp11_write_mask (destmask, fm);
8246           else
8247             {
8248               bfd_arm_vfp11_write_mask (destmask, fm);
8249               bfd_arm_vfp11_write_mask (destmask, fm + 1);
8250             }
8251         }
8252
8253       vpipe = VFP11_LS;
8254     }
8255   else if ((insn & 0x0e100e00) == 0x0c100a00)  /* A load insn.  */
8256     {
8257       int fd = bfd_arm_vfp11_regno (insn, is_double, 12, 22);
8258       unsigned int puw = ((insn >> 21) & 0x1) | (((insn >> 23) & 3) << 1);
8259
8260       switch (puw)
8261         {
8262         case 0: /* Two-reg transfer.  We should catch these above.  */
8263           abort ();
8264
8265         case 2: /* fldm[sdx].  */
8266         case 3:
8267         case 5:
8268           {
8269             unsigned int i, offset = insn & 0xff;
8270
8271             if (is_double)
8272               offset >>= 1;
8273
8274             for (i = fd; i < fd + offset; i++)
8275               bfd_arm_vfp11_write_mask (destmask, i);
8276           }
8277           break;
8278
8279         case 4: /* fld[sd].  */
8280         case 6:
8281           bfd_arm_vfp11_write_mask (destmask, fd);
8282           break;
8283
8284         default:
8285           return VFP11_BAD;
8286         }
8287
8288       vpipe = VFP11_LS;
8289     }
8290   /* Single-register transfer. Note L==0.  */
8291   else if ((insn & 0x0f100e10) == 0x0e000a10)
8292     {
8293       unsigned int opcode = (insn >> 21) & 7;
8294       unsigned int fn = bfd_arm_vfp11_regno (insn, is_double, 16, 7);
8295
8296       switch (opcode)
8297         {
8298         case 0: /* fmsr/fmdlr.  */
8299         case 1: /* fmdhr.  */
8300           /* Mark fmdhr and fmdlr as writing to the whole of the DP
8301              destination register.  I don't know if this is exactly right,
8302              but it is the conservative choice.  */
8303           bfd_arm_vfp11_write_mask (destmask, fn);
8304           break;
8305
8306         case 7: /* fmxr.  */
8307           break;
8308         }
8309
8310       vpipe = VFP11_LS;
8311     }
8312
8313   return vpipe;
8314 }
8315
8316
8317 static int elf32_arm_compare_mapping (const void * a, const void * b);
8318
8319
8320 /* Look for potentially-troublesome code sequences which might trigger the
8321    VFP11 denormal/antidependency erratum.  See, e.g., the ARM1136 errata sheet
8322    (available from ARM) for details of the erratum.  A short version is
8323    described in ld.texinfo.  */
8324
8325 bfd_boolean
8326 bfd_elf32_arm_vfp11_erratum_scan (bfd *abfd, struct bfd_link_info *link_info)
8327 {
8328   asection *sec;
8329   bfd_byte *contents = NULL;
8330   int state = 0;
8331   int regs[3], numregs = 0;
8332   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
8333   int use_vector = (globals->vfp11_fix == BFD_ARM_VFP11_FIX_VECTOR);
8334
8335   if (globals == NULL)
8336     return FALSE;
8337
8338   /* We use a simple FSM to match troublesome VFP11 instruction sequences.
8339      The states transition as follows:
8340
8341        0 -> 1 (vector) or 0 -> 2 (scalar)
8342            A VFP FMAC-pipeline instruction has been seen. Fill
8343            regs[0]..regs[numregs-1] with its input operands. Remember this
8344            instruction in 'first_fmac'.
8345
8346        1 -> 2
8347            Any instruction, except for a VFP instruction which overwrites
8348            regs[*].
8349
8350        1 -> 3 [ -> 0 ]  or
8351        2 -> 3 [ -> 0 ]
8352            A VFP instruction has been seen which overwrites any of regs[*].
8353            We must make a veneer!  Reset state to 0 before examining next
8354            instruction.
8355
8356        2 -> 0
8357            If we fail to match anything in state 2, reset to state 0 and reset
8358            the instruction pointer to the instruction after 'first_fmac'.
8359
8360      If the VFP11 vector mode is in use, there must be at least two unrelated
8361      instructions between anti-dependent VFP11 instructions to properly avoid
8362      triggering the erratum, hence the use of the extra state 1.  */
8363
8364   /* If we are only performing a partial link do not bother
8365      to construct any glue.  */
8366   if (bfd_link_relocatable (link_info))
8367     return TRUE;
8368
8369   /* Skip if this bfd does not correspond to an ELF image.  */
8370   if (! is_arm_elf (abfd))
8371     return TRUE;
8372
8373   /* We should have chosen a fix type by the time we get here.  */
8374   BFD_ASSERT (globals->vfp11_fix != BFD_ARM_VFP11_FIX_DEFAULT);
8375
8376   if (globals->vfp11_fix == BFD_ARM_VFP11_FIX_NONE)
8377     return TRUE;
8378
8379   /* Skip this BFD if it corresponds to an executable or dynamic object.  */
8380   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
8381     return TRUE;
8382
8383   for (sec = abfd->sections; sec != NULL; sec = sec->next)
8384     {
8385       unsigned int i, span, first_fmac = 0, veneer_of_insn = 0;
8386       struct _arm_elf_section_data *sec_data;
8387
8388       /* If we don't have executable progbits, we're not interested in this
8389          section.  Also skip if section is to be excluded.  */
8390       if (elf_section_type (sec) != SHT_PROGBITS
8391           || (elf_section_flags (sec) & SHF_EXECINSTR) == 0
8392           || (sec->flags & SEC_EXCLUDE) != 0
8393           || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8394           || sec->output_section == bfd_abs_section_ptr
8395           || strcmp (sec->name, VFP11_ERRATUM_VENEER_SECTION_NAME) == 0)
8396         continue;
8397
8398       sec_data = elf32_arm_section_data (sec);
8399
8400       if (sec_data->mapcount == 0)
8401         continue;
8402
8403       if (elf_section_data (sec)->this_hdr.contents != NULL)
8404         contents = elf_section_data (sec)->this_hdr.contents;
8405       else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
8406         goto error_return;
8407
8408       qsort (sec_data->map, sec_data->mapcount, sizeof (elf32_arm_section_map),
8409              elf32_arm_compare_mapping);
8410
8411       for (span = 0; span < sec_data->mapcount; span++)
8412         {
8413           unsigned int span_start = sec_data->map[span].vma;
8414           unsigned int span_end = (span == sec_data->mapcount - 1)
8415                                   ? sec->size : sec_data->map[span + 1].vma;
8416           char span_type = sec_data->map[span].type;
8417
8418           /* FIXME: Only ARM mode is supported at present.  We may need to
8419              support Thumb-2 mode also at some point.  */
8420           if (span_type != 'a')
8421             continue;
8422
8423           for (i = span_start; i < span_end;)
8424             {
8425               unsigned int next_i = i + 4;
8426               unsigned int insn = bfd_big_endian (abfd)
8427                 ? (contents[i] << 24)
8428                   | (contents[i + 1] << 16)
8429                   | (contents[i + 2] << 8)
8430                   | contents[i + 3]
8431                 : (contents[i + 3] << 24)
8432                   | (contents[i + 2] << 16)
8433                   | (contents[i + 1] << 8)
8434                   | contents[i];
8435               unsigned int writemask = 0;
8436               enum bfd_arm_vfp11_pipe vpipe;
8437
8438               switch (state)
8439                 {
8440                 case 0:
8441                   vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask, regs,
8442                                                     &numregs);
8443                   /* I'm assuming the VFP11 erratum can trigger with denorm
8444                      operands on either the FMAC or the DS pipeline. This might
8445                      lead to slightly overenthusiastic veneer insertion.  */
8446                   if (vpipe == VFP11_FMAC || vpipe == VFP11_DS)
8447                     {
8448                       state = use_vector ? 1 : 2;
8449                       first_fmac = i;
8450                       veneer_of_insn = insn;
8451                     }
8452                   break;
8453
8454                 case 1:
8455                   {
8456                     int other_regs[3], other_numregs;
8457                     vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask,
8458                                                       other_regs,
8459                                                       &other_numregs);
8460                     if (vpipe != VFP11_BAD
8461                         && bfd_arm_vfp11_antidependency (writemask, regs,
8462                                                          numregs))
8463                       state = 3;
8464                     else
8465                       state = 2;
8466                   }
8467                   break;
8468
8469                 case 2:
8470                   {
8471                     int other_regs[3], other_numregs;
8472                     vpipe = bfd_arm_vfp11_insn_decode (insn, &writemask,
8473                                                       other_regs,
8474                                                       &other_numregs);
8475                     if (vpipe != VFP11_BAD
8476                         && bfd_arm_vfp11_antidependency (writemask, regs,
8477                                                          numregs))
8478                       state = 3;
8479                     else
8480                       {
8481                         state = 0;
8482                         next_i = first_fmac + 4;
8483                       }
8484                   }
8485                   break;
8486
8487                 case 3:
8488                   abort ();  /* Should be unreachable.  */
8489                 }
8490
8491               if (state == 3)
8492                 {
8493                   elf32_vfp11_erratum_list *newerr =(elf32_vfp11_erratum_list *)
8494                       bfd_zmalloc (sizeof (elf32_vfp11_erratum_list));
8495
8496                   elf32_arm_section_data (sec)->erratumcount += 1;
8497
8498                   newerr->u.b.vfp_insn = veneer_of_insn;
8499
8500                   switch (span_type)
8501                     {
8502                     case 'a':
8503                       newerr->type = VFP11_ERRATUM_BRANCH_TO_ARM_VENEER;
8504                       break;
8505
8506                     default:
8507                       abort ();
8508                     }
8509
8510                   record_vfp11_erratum_veneer (link_info, newerr, abfd, sec,
8511                                                first_fmac);
8512
8513                   newerr->vma = -1;
8514
8515                   newerr->next = sec_data->erratumlist;
8516                   sec_data->erratumlist = newerr;
8517
8518                   state = 0;
8519                 }
8520
8521               i = next_i;
8522             }
8523         }
8524
8525       if (contents != NULL
8526           && elf_section_data (sec)->this_hdr.contents != contents)
8527         free (contents);
8528       contents = NULL;
8529     }
8530
8531   return TRUE;
8532
8533 error_return:
8534   if (contents != NULL
8535       && elf_section_data (sec)->this_hdr.contents != contents)
8536     free (contents);
8537
8538   return FALSE;
8539 }
8540
8541 /* Find virtual-memory addresses for VFP11 erratum veneers and return locations
8542    after sections have been laid out, using specially-named symbols.  */
8543
8544 void
8545 bfd_elf32_arm_vfp11_fix_veneer_locations (bfd *abfd,
8546                                           struct bfd_link_info *link_info)
8547 {
8548   asection *sec;
8549   struct elf32_arm_link_hash_table *globals;
8550   char *tmp_name;
8551
8552   if (bfd_link_relocatable (link_info))
8553     return;
8554
8555   /* Skip if this bfd does not correspond to an ELF image.  */
8556   if (! is_arm_elf (abfd))
8557     return;
8558
8559   globals = elf32_arm_hash_table (link_info);
8560   if (globals == NULL)
8561     return;
8562
8563   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
8564                                   (VFP11_ERRATUM_VENEER_ENTRY_NAME) + 10);
8565
8566   for (sec = abfd->sections; sec != NULL; sec = sec->next)
8567     {
8568       struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
8569       elf32_vfp11_erratum_list *errnode = sec_data->erratumlist;
8570
8571       for (; errnode != NULL; errnode = errnode->next)
8572         {
8573           struct elf_link_hash_entry *myh;
8574           bfd_vma vma;
8575
8576           switch (errnode->type)
8577             {
8578             case VFP11_ERRATUM_BRANCH_TO_ARM_VENEER:
8579             case VFP11_ERRATUM_BRANCH_TO_THUMB_VENEER:
8580               /* Find veneer symbol.  */
8581               sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME,
8582                        errnode->u.b.veneer->u.v.id);
8583
8584               myh = elf_link_hash_lookup
8585                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
8586
8587               if (myh == NULL)
8588                 _bfd_error_handler (_("%pB: unable to find %s veneer `%s'"),
8589                                     abfd, "VFP11", tmp_name);
8590
8591               vma = myh->root.u.def.section->output_section->vma
8592                     + myh->root.u.def.section->output_offset
8593                     + myh->root.u.def.value;
8594
8595               errnode->u.b.veneer->vma = vma;
8596               break;
8597
8598             case VFP11_ERRATUM_ARM_VENEER:
8599             case VFP11_ERRATUM_THUMB_VENEER:
8600               /* Find return location.  */
8601               sprintf (tmp_name, VFP11_ERRATUM_VENEER_ENTRY_NAME "_r",
8602                        errnode->u.v.id);
8603
8604               myh = elf_link_hash_lookup
8605                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
8606
8607               if (myh == NULL)
8608                 _bfd_error_handler (_("%pB: unable to find %s veneer `%s'"),
8609                                     abfd, "VFP11", tmp_name);
8610
8611               vma = myh->root.u.def.section->output_section->vma
8612                     + myh->root.u.def.section->output_offset
8613                     + myh->root.u.def.value;
8614
8615               errnode->u.v.branch->vma = vma;
8616               break;
8617
8618             default:
8619               abort ();
8620             }
8621         }
8622     }
8623
8624   free (tmp_name);
8625 }
8626
8627 /* Find virtual-memory addresses for STM32L4XX erratum veneers and
8628    return locations after sections have been laid out, using
8629    specially-named symbols.  */
8630
8631 void
8632 bfd_elf32_arm_stm32l4xx_fix_veneer_locations (bfd *abfd,
8633                                               struct bfd_link_info *link_info)
8634 {
8635   asection *sec;
8636   struct elf32_arm_link_hash_table *globals;
8637   char *tmp_name;
8638
8639   if (bfd_link_relocatable (link_info))
8640     return;
8641
8642   /* Skip if this bfd does not correspond to an ELF image.  */
8643   if (! is_arm_elf (abfd))
8644     return;
8645
8646   globals = elf32_arm_hash_table (link_info);
8647   if (globals == NULL)
8648     return;
8649
8650   tmp_name = (char *) bfd_malloc ((bfd_size_type) strlen
8651                                   (STM32L4XX_ERRATUM_VENEER_ENTRY_NAME) + 10);
8652
8653   for (sec = abfd->sections; sec != NULL; sec = sec->next)
8654     {
8655       struct _arm_elf_section_data *sec_data = elf32_arm_section_data (sec);
8656       elf32_stm32l4xx_erratum_list *errnode = sec_data->stm32l4xx_erratumlist;
8657
8658       for (; errnode != NULL; errnode = errnode->next)
8659         {
8660           struct elf_link_hash_entry *myh;
8661           bfd_vma vma;
8662
8663           switch (errnode->type)
8664             {
8665             case STM32L4XX_ERRATUM_BRANCH_TO_VENEER:
8666               /* Find veneer symbol.  */
8667               sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME,
8668                        errnode->u.b.veneer->u.v.id);
8669
8670               myh = elf_link_hash_lookup
8671                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
8672
8673               if (myh == NULL)
8674                 _bfd_error_handler (_("%pB: unable to find %s veneer `%s'"),
8675                                     abfd, "STM32L4XX", tmp_name);
8676
8677               vma = myh->root.u.def.section->output_section->vma
8678                 + myh->root.u.def.section->output_offset
8679                 + myh->root.u.def.value;
8680
8681               errnode->u.b.veneer->vma = vma;
8682               break;
8683
8684             case STM32L4XX_ERRATUM_VENEER:
8685               /* Find return location.  */
8686               sprintf (tmp_name, STM32L4XX_ERRATUM_VENEER_ENTRY_NAME "_r",
8687                        errnode->u.v.id);
8688
8689               myh = elf_link_hash_lookup
8690                 (&(globals)->root, tmp_name, FALSE, FALSE, TRUE);
8691
8692               if (myh == NULL)
8693                 _bfd_error_handler (_("%pB: unable to find %s veneer `%s'"),
8694                                     abfd, "STM32L4XX", tmp_name);
8695
8696               vma = myh->root.u.def.section->output_section->vma
8697                 + myh->root.u.def.section->output_offset
8698                 + myh->root.u.def.value;
8699
8700               errnode->u.v.branch->vma = vma;
8701               break;
8702
8703             default:
8704               abort ();
8705             }
8706         }
8707     }
8708
8709   free (tmp_name);
8710 }
8711
8712 static inline bfd_boolean
8713 is_thumb2_ldmia (const insn32 insn)
8714 {
8715   /* Encoding T2: LDM<c>.W <Rn>{!},<registers>
8716      1110 - 1000 - 10W1 - rrrr - PM (0) l - llll - llll - llll.  */
8717   return (insn & 0xffd02000) == 0xe8900000;
8718 }
8719
8720 static inline bfd_boolean
8721 is_thumb2_ldmdb (const insn32 insn)
8722 {
8723   /* Encoding T1: LDMDB<c> <Rn>{!},<registers>
8724      1110 - 1001 - 00W1 - rrrr - PM (0) l - llll - llll - llll.  */
8725   return (insn & 0xffd02000) == 0xe9100000;
8726 }
8727
8728 static inline bfd_boolean
8729 is_thumb2_vldm (const insn32 insn)
8730 {
8731   /* A6.5 Extension register load or store instruction
8732      A7.7.229
8733      We look for SP 32-bit and DP 64-bit registers.
8734      Encoding T1 VLDM{mode}<c> <Rn>{!}, <list>
8735      <list> is consecutive 64-bit registers
8736      1110 - 110P - UDW1 - rrrr - vvvv - 1011 - iiii - iiii
8737      Encoding T2 VLDM{mode}<c> <Rn>{!}, <list>
8738      <list> is consecutive 32-bit registers
8739      1110 - 110P - UDW1 - rrrr - vvvv - 1010 - iiii - iiii
8740      if P==0 && U==1 && W==1 && Rn=1101 VPOP
8741      if PUW=010 || PUW=011 || PUW=101 VLDM.  */
8742   return
8743     (((insn & 0xfe100f00) == 0xec100b00) ||
8744      ((insn & 0xfe100f00) == 0xec100a00))
8745     && /* (IA without !).  */
8746     (((((insn << 7) >> 28) & 0xd) == 0x4)
8747      /* (IA with !), includes VPOP (when reg number is SP).  */
8748      || ((((insn << 7) >> 28) & 0xd) == 0x5)
8749      /* (DB with !).  */
8750      || ((((insn << 7) >> 28) & 0xd) == 0x9));
8751 }
8752
8753 /* STM STM32L4XX erratum : This function assumes that it receives an LDM or
8754    VLDM opcode and:
8755  - computes the number and the mode of memory accesses
8756  - decides if the replacement should be done:
8757    . replaces only if > 8-word accesses
8758    . or (testing purposes only) replaces all accesses.  */
8759
8760 static bfd_boolean
8761 stm32l4xx_need_create_replacing_stub (const insn32 insn,
8762                                       bfd_arm_stm32l4xx_fix stm32l4xx_fix)
8763 {
8764   int nb_words = 0;
8765
8766   /* The field encoding the register list is the same for both LDMIA
8767      and LDMDB encodings.  */
8768   if (is_thumb2_ldmia (insn) || is_thumb2_ldmdb (insn))
8769     nb_words = elf32_arm_popcount (insn & 0x0000ffff);
8770   else if (is_thumb2_vldm (insn))
8771    nb_words = (insn & 0xff);
8772
8773   /* DEFAULT mode accounts for the real bug condition situation,
8774      ALL mode inserts stubs for each LDM/VLDM instruction (testing).  */
8775   return
8776     (stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_DEFAULT) ? nb_words > 8 :
8777     (stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_ALL) ? TRUE : FALSE;
8778 }
8779
8780 /* Look for potentially-troublesome code sequences which might trigger
8781    the STM STM32L4XX erratum.  */
8782
8783 bfd_boolean
8784 bfd_elf32_arm_stm32l4xx_erratum_scan (bfd *abfd,
8785                                       struct bfd_link_info *link_info)
8786 {
8787   asection *sec;
8788   bfd_byte *contents = NULL;
8789   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
8790
8791   if (globals == NULL)
8792     return FALSE;
8793
8794   /* If we are only performing a partial link do not bother
8795      to construct any glue.  */
8796   if (bfd_link_relocatable (link_info))
8797     return TRUE;
8798
8799   /* Skip if this bfd does not correspond to an ELF image.  */
8800   if (! is_arm_elf (abfd))
8801     return TRUE;
8802
8803   if (globals->stm32l4xx_fix == BFD_ARM_STM32L4XX_FIX_NONE)
8804     return TRUE;
8805
8806   /* Skip this BFD if it corresponds to an executable or dynamic object.  */
8807   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
8808     return TRUE;
8809
8810   for (sec = abfd->sections; sec != NULL; sec = sec->next)
8811     {
8812       unsigned int i, span;
8813       struct _arm_elf_section_data *sec_data;
8814
8815       /* If we don't have executable progbits, we're not interested in this
8816          section.  Also skip if section is to be excluded.  */
8817       if (elf_section_type (sec) != SHT_PROGBITS
8818           || (elf_section_flags (sec) & SHF_EXECINSTR) == 0
8819           || (sec->flags & SEC_EXCLUDE) != 0
8820           || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8821           || sec->output_section == bfd_abs_section_ptr
8822           || strcmp (sec->name, STM32L4XX_ERRATUM_VENEER_SECTION_NAME) == 0)
8823         continue;
8824
8825       sec_data = elf32_arm_section_data (sec);
8826
8827       if (sec_data->mapcount == 0)
8828         continue;
8829
8830       if (elf_section_data (sec)->this_hdr.contents != NULL)
8831         contents = elf_section_data (sec)->this_hdr.contents;
8832       else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
8833         goto error_return;
8834
8835       qsort (sec_data->map, sec_data->mapcount, sizeof (elf32_arm_section_map),
8836              elf32_arm_compare_mapping);
8837
8838       for (span = 0; span < sec_data->mapcount; span++)
8839         {
8840           unsigned int span_start = sec_data->map[span].vma;
8841           unsigned int span_end = (span == sec_data->mapcount - 1)
8842             ? sec->size : sec_data->map[span + 1].vma;
8843           char span_type = sec_data->map[span].type;
8844           int itblock_current_pos = 0;
8845
8846           /* Only Thumb2 mode need be supported with this CM4 specific
8847              code, we should not encounter any arm mode eg span_type
8848              != 'a'.  */
8849           if (span_type != 't')
8850             continue;
8851
8852           for (i = span_start; i < span_end;)
8853             {
8854               unsigned int insn = bfd_get_16 (abfd, &contents[i]);
8855               bfd_boolean insn_32bit = FALSE;
8856               bfd_boolean is_ldm = FALSE;
8857               bfd_boolean is_vldm = FALSE;
8858               bfd_boolean is_not_last_in_it_block = FALSE;
8859
8860               /* The first 16-bits of all 32-bit thumb2 instructions start
8861                  with opcode[15..13]=0b111 and the encoded op1 can be anything
8862                  except opcode[12..11]!=0b00.
8863                  See 32-bit Thumb instruction encoding.  */
8864               if ((insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000)
8865                 insn_32bit = TRUE;
8866
8867               /* Compute the predicate that tells if the instruction
8868                  is concerned by the IT block
8869                  - Creates an error if there is a ldm that is not
8870                    last in the IT block thus cannot be replaced
8871                  - Otherwise we can create a branch at the end of the
8872                    IT block, it will be controlled naturally by IT
8873                    with the proper pseudo-predicate
8874                  - So the only interesting predicate is the one that
8875                    tells that we are not on the last item of an IT
8876                    block.  */
8877               if (itblock_current_pos != 0)
8878                   is_not_last_in_it_block = !!--itblock_current_pos;
8879
8880               if (insn_32bit)
8881                 {
8882                   /* Load the rest of the insn (in manual-friendly order).  */
8883                   insn = (insn << 16) | bfd_get_16 (abfd, &contents[i + 2]);
8884                   is_ldm = is_thumb2_ldmia (insn) || is_thumb2_ldmdb (insn);
8885                   is_vldm = is_thumb2_vldm (insn);
8886
8887                   /* Veneers are created for (v)ldm depending on
8888                      option flags and memory accesses conditions; but
8889                      if the instruction is not the last instruction of
8890                      an IT block, we cannot create a jump there, so we
8891                      bail out.  */
8892                     if ((is_ldm || is_vldm)
8893                         && stm32l4xx_need_create_replacing_stub
8894                         (insn, globals->stm32l4xx_fix))
8895                       {
8896                         if (is_not_last_in_it_block)
8897                           {
8898                             _bfd_error_handler
8899                               /* xgettext:c-format */
8900                               (_("%pB(%pA+%#x): error: multiple load detected"
8901                                  " in non-last IT block instruction:"
8902                                  " STM32L4XX veneer cannot be generated; "
8903                                  "use gcc option -mrestrict-it to generate"
8904                                  " only one instruction per IT block"),
8905                                abfd, sec, i);
8906                           }
8907                         else
8908                           {
8909                             elf32_stm32l4xx_erratum_list *newerr =
8910                               (elf32_stm32l4xx_erratum_list *)
8911                               bfd_zmalloc
8912                               (sizeof (elf32_stm32l4xx_erratum_list));
8913
8914                             elf32_arm_section_data (sec)
8915                               ->stm32l4xx_erratumcount += 1;
8916                             newerr->u.b.insn = insn;
8917                             /* We create only thumb branches.  */
8918                             newerr->type =
8919                               STM32L4XX_ERRATUM_BRANCH_TO_VENEER;
8920                             record_stm32l4xx_erratum_veneer
8921                               (link_info, newerr, abfd, sec,
8922                                i,
8923                                is_ldm ?
8924                                STM32L4XX_ERRATUM_LDM_VENEER_SIZE:
8925                                STM32L4XX_ERRATUM_VLDM_VENEER_SIZE);
8926                             newerr->vma = -1;
8927                             newerr->next = sec_data->stm32l4xx_erratumlist;
8928                             sec_data->stm32l4xx_erratumlist = newerr;
8929                           }
8930                       }
8931                 }
8932               else
8933                 {
8934                   /* A7.7.37 IT p208
8935                      IT blocks are only encoded in T1
8936                      Encoding T1: IT{x{y{z}}} <firstcond>
8937                      1 0 1 1 - 1 1 1 1 - firstcond - mask
8938                      if mask = '0000' then see 'related encodings'
8939                      We don't deal with UNPREDICTABLE, just ignore these.
8940                      There can be no nested IT blocks so an IT block
8941                      is naturally a new one for which it is worth
8942                      computing its size.  */
8943                   bfd_boolean is_newitblock = ((insn & 0xff00) == 0xbf00)
8944                     && ((insn & 0x000f) != 0x0000);
8945                   /* If we have a new IT block we compute its size.  */
8946                   if (is_newitblock)
8947                     {
8948                       /* Compute the number of instructions controlled
8949                          by the IT block, it will be used to decide
8950                          whether we are inside an IT block or not.  */
8951                       unsigned int mask = insn & 0x000f;
8952                       itblock_current_pos = 4 - ctz (mask);
8953                     }
8954                 }
8955
8956               i += insn_32bit ? 4 : 2;
8957             }
8958         }
8959
8960       if (contents != NULL
8961           && elf_section_data (sec)->this_hdr.contents != contents)
8962         free (contents);
8963       contents = NULL;
8964     }
8965
8966   return TRUE;
8967
8968 error_return:
8969   if (contents != NULL
8970       && elf_section_data (sec)->this_hdr.contents != contents)
8971     free (contents);
8972
8973   return FALSE;
8974 }
8975
8976 /* Set target relocation values needed during linking.  */
8977
8978 void
8979 bfd_elf32_arm_set_target_params (struct bfd *output_bfd,
8980                                  struct bfd_link_info *link_info,
8981                                  struct elf32_arm_params *params)
8982 {
8983   struct elf32_arm_link_hash_table *globals;
8984
8985   globals = elf32_arm_hash_table (link_info);
8986   if (globals == NULL)
8987     return;
8988
8989   globals->target1_is_rel = params->target1_is_rel;
8990   if (globals->fdpic_p)
8991     globals->target2_reloc = R_ARM_GOT32;
8992   else if (strcmp (params->target2_type, "rel") == 0)
8993     globals->target2_reloc = R_ARM_REL32;
8994   else if (strcmp (params->target2_type, "abs") == 0)
8995     globals->target2_reloc = R_ARM_ABS32;
8996   else if (strcmp (params->target2_type, "got-rel") == 0)
8997     globals->target2_reloc = R_ARM_GOT_PREL;
8998   else
8999     {
9000       _bfd_error_handler (_("invalid TARGET2 relocation type '%s'"),
9001                           params->target2_type);
9002     }
9003   globals->fix_v4bx = params->fix_v4bx;
9004   globals->use_blx |= params->use_blx;
9005   globals->vfp11_fix = params->vfp11_denorm_fix;
9006   globals->stm32l4xx_fix = params->stm32l4xx_fix;
9007   if (globals->fdpic_p)
9008     globals->pic_veneer = 1;
9009   else
9010     globals->pic_veneer = params->pic_veneer;
9011   globals->fix_cortex_a8 = params->fix_cortex_a8;
9012   globals->fix_arm1176 = params->fix_arm1176;
9013   globals->cmse_implib = params->cmse_implib;
9014   globals->in_implib_bfd = params->in_implib_bfd;
9015
9016   BFD_ASSERT (is_arm_elf (output_bfd));
9017   elf_arm_tdata (output_bfd)->no_enum_size_warning
9018     = params->no_enum_size_warning;
9019   elf_arm_tdata (output_bfd)->no_wchar_size_warning
9020     = params->no_wchar_size_warning;
9021 }
9022
9023 /* Replace the target offset of a Thumb bl or b.w instruction.  */
9024
9025 static void
9026 insert_thumb_branch (bfd *abfd, long int offset, bfd_byte *insn)
9027 {
9028   bfd_vma upper;
9029   bfd_vma lower;
9030   int reloc_sign;
9031
9032   BFD_ASSERT ((offset & 1) == 0);
9033
9034   upper = bfd_get_16 (abfd, insn);
9035   lower = bfd_get_16 (abfd, insn + 2);
9036   reloc_sign = (offset < 0) ? 1 : 0;
9037   upper = (upper & ~(bfd_vma) 0x7ff)
9038           | ((offset >> 12) & 0x3ff)
9039           | (reloc_sign << 10);
9040   lower = (lower & ~(bfd_vma) 0x2fff)
9041           | (((!((offset >> 23) & 1)) ^ reloc_sign) << 13)
9042           | (((!((offset >> 22) & 1)) ^ reloc_sign) << 11)
9043           | ((offset >> 1) & 0x7ff);
9044   bfd_put_16 (abfd, upper, insn);
9045   bfd_put_16 (abfd, lower, insn + 2);
9046 }
9047
9048 /* Thumb code calling an ARM function.  */
9049
9050 static int
9051 elf32_thumb_to_arm_stub (struct bfd_link_info * info,
9052                          const char *           name,
9053                          bfd *                  input_bfd,
9054                          bfd *                  output_bfd,
9055                          asection *             input_section,
9056                          bfd_byte *             hit_data,
9057                          asection *             sym_sec,
9058                          bfd_vma                offset,
9059                          bfd_signed_vma         addend,
9060                          bfd_vma                val,
9061                          char **error_message)
9062 {
9063   asection * s = 0;
9064   bfd_vma my_offset;
9065   long int ret_offset;
9066   struct elf_link_hash_entry * myh;
9067   struct elf32_arm_link_hash_table * globals;
9068
9069   myh = find_thumb_glue (info, name, error_message);
9070   if (myh == NULL)
9071     return FALSE;
9072
9073   globals = elf32_arm_hash_table (info);
9074   BFD_ASSERT (globals != NULL);
9075   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
9076
9077   my_offset = myh->root.u.def.value;
9078
9079   s = bfd_get_linker_section (globals->bfd_of_glue_owner,
9080                               THUMB2ARM_GLUE_SECTION_NAME);
9081
9082   BFD_ASSERT (s != NULL);
9083   BFD_ASSERT (s->contents != NULL);
9084   BFD_ASSERT (s->output_section != NULL);
9085
9086   if ((my_offset & 0x01) == 0x01)
9087     {
9088       if (sym_sec != NULL
9089           && sym_sec->owner != NULL
9090           && !INTERWORK_FLAG (sym_sec->owner))
9091         {
9092           _bfd_error_handler
9093             (_("%pB(%s): warning: interworking not enabled;"
9094                " first occurrence: %pB: %s call to %s"),
9095              sym_sec->owner, name, input_bfd, "Thumb", "ARM");
9096
9097           return FALSE;
9098         }
9099
9100       --my_offset;
9101       myh->root.u.def.value = my_offset;
9102
9103       put_thumb_insn (globals, output_bfd, (bfd_vma) t2a1_bx_pc_insn,
9104                       s->contents + my_offset);
9105
9106       put_thumb_insn (globals, output_bfd, (bfd_vma) t2a2_noop_insn,
9107                       s->contents + my_offset + 2);
9108
9109       ret_offset =
9110         /* Address of destination of the stub.  */
9111         ((bfd_signed_vma) val)
9112         - ((bfd_signed_vma)
9113            /* Offset from the start of the current section
9114               to the start of the stubs.  */
9115            (s->output_offset
9116             /* Offset of the start of this stub from the start of the stubs.  */
9117             + my_offset
9118             /* Address of the start of the current section.  */
9119             + s->output_section->vma)
9120            /* The branch instruction is 4 bytes into the stub.  */
9121            + 4
9122            /* ARM branches work from the pc of the instruction + 8.  */
9123            + 8);
9124
9125       put_arm_insn (globals, output_bfd,
9126                     (bfd_vma) t2a3_b_insn | ((ret_offset >> 2) & 0x00FFFFFF),
9127                     s->contents + my_offset + 4);
9128     }
9129
9130   BFD_ASSERT (my_offset <= globals->thumb_glue_size);
9131
9132   /* Now go back and fix up the original BL insn to point to here.  */
9133   ret_offset =
9134     /* Address of where the stub is located.  */
9135     (s->output_section->vma + s->output_offset + my_offset)
9136      /* Address of where the BL is located.  */
9137     - (input_section->output_section->vma + input_section->output_offset
9138        + offset)
9139     /* Addend in the relocation.  */
9140     - addend
9141     /* Biassing for PC-relative addressing.  */
9142     - 8;
9143
9144   insert_thumb_branch (input_bfd, ret_offset, hit_data - input_section->vma);
9145
9146   return TRUE;
9147 }
9148
9149 /* Populate an Arm to Thumb stub.  Returns the stub symbol.  */
9150
9151 static struct elf_link_hash_entry *
9152 elf32_arm_create_thumb_stub (struct bfd_link_info * info,
9153                              const char *           name,
9154                              bfd *                  input_bfd,
9155                              bfd *                  output_bfd,
9156                              asection *             sym_sec,
9157                              bfd_vma                val,
9158                              asection *             s,
9159                              char **                error_message)
9160 {
9161   bfd_vma my_offset;
9162   long int ret_offset;
9163   struct elf_link_hash_entry * myh;
9164   struct elf32_arm_link_hash_table * globals;
9165
9166   myh = find_arm_glue (info, name, error_message);
9167   if (myh == NULL)
9168     return NULL;
9169
9170   globals = elf32_arm_hash_table (info);
9171   BFD_ASSERT (globals != NULL);
9172   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
9173
9174   my_offset = myh->root.u.def.value;
9175
9176   if ((my_offset & 0x01) == 0x01)
9177     {
9178       if (sym_sec != NULL
9179           && sym_sec->owner != NULL
9180           && !INTERWORK_FLAG (sym_sec->owner))
9181         {
9182           _bfd_error_handler
9183             (_("%pB(%s): warning: interworking not enabled;"
9184                " first occurrence: %pB: %s call to %s"),
9185              sym_sec->owner, name, input_bfd, "ARM", "Thumb");
9186         }
9187
9188       --my_offset;
9189       myh->root.u.def.value = my_offset;
9190
9191       if (bfd_link_pic (info)
9192           || globals->root.is_relocatable_executable
9193           || globals->pic_veneer)
9194         {
9195           /* For relocatable objects we can't use absolute addresses,
9196              so construct the address from a relative offset.  */
9197           /* TODO: If the offset is small it's probably worth
9198              constructing the address with adds.  */
9199           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1p_ldr_insn,
9200                         s->contents + my_offset);
9201           put_arm_insn (globals, output_bfd, (bfd_vma) a2t2p_add_pc_insn,
9202                         s->contents + my_offset + 4);
9203           put_arm_insn (globals, output_bfd, (bfd_vma) a2t3p_bx_r12_insn,
9204                         s->contents + my_offset + 8);
9205           /* Adjust the offset by 4 for the position of the add,
9206              and 8 for the pipeline offset.  */
9207           ret_offset = (val - (s->output_offset
9208                                + s->output_section->vma
9209                                + my_offset + 12))
9210                        | 1;
9211           bfd_put_32 (output_bfd, ret_offset,
9212                       s->contents + my_offset + 12);
9213         }
9214       else if (globals->use_blx)
9215         {
9216           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1v5_ldr_insn,
9217                         s->contents + my_offset);
9218
9219           /* It's a thumb address.  Add the low order bit.  */
9220           bfd_put_32 (output_bfd, val | a2t2v5_func_addr_insn,
9221                       s->contents + my_offset + 4);
9222         }
9223       else
9224         {
9225           put_arm_insn (globals, output_bfd, (bfd_vma) a2t1_ldr_insn,
9226                         s->contents + my_offset);
9227
9228           put_arm_insn (globals, output_bfd, (bfd_vma) a2t2_bx_r12_insn,
9229                         s->contents + my_offset + 4);
9230
9231           /* It's a thumb address.  Add the low order bit.  */
9232           bfd_put_32 (output_bfd, val | a2t3_func_addr_insn,
9233                       s->contents + my_offset + 8);
9234
9235           my_offset += 12;
9236         }
9237     }
9238
9239   BFD_ASSERT (my_offset <= globals->arm_glue_size);
9240
9241   return myh;
9242 }
9243
9244 /* Arm code calling a Thumb function.  */
9245
9246 static int
9247 elf32_arm_to_thumb_stub (struct bfd_link_info * info,
9248                          const char *           name,
9249                          bfd *                  input_bfd,
9250                          bfd *                  output_bfd,
9251                          asection *             input_section,
9252                          bfd_byte *             hit_data,
9253                          asection *             sym_sec,
9254                          bfd_vma                offset,
9255                          bfd_signed_vma         addend,
9256                          bfd_vma                val,
9257                          char **error_message)
9258 {
9259   unsigned long int tmp;
9260   bfd_vma my_offset;
9261   asection * s;
9262   long int ret_offset;
9263   struct elf_link_hash_entry * myh;
9264   struct elf32_arm_link_hash_table * globals;
9265
9266   globals = elf32_arm_hash_table (info);
9267   BFD_ASSERT (globals != NULL);
9268   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
9269
9270   s = bfd_get_linker_section (globals->bfd_of_glue_owner,
9271                               ARM2THUMB_GLUE_SECTION_NAME);
9272   BFD_ASSERT (s != NULL);
9273   BFD_ASSERT (s->contents != NULL);
9274   BFD_ASSERT (s->output_section != NULL);
9275
9276   myh = elf32_arm_create_thumb_stub (info, name, input_bfd, output_bfd,
9277                                      sym_sec, val, s, error_message);
9278   if (!myh)
9279     return FALSE;
9280
9281   my_offset = myh->root.u.def.value;
9282   tmp = bfd_get_32 (input_bfd, hit_data);
9283   tmp = tmp & 0xFF000000;
9284
9285   /* Somehow these are both 4 too far, so subtract 8.  */
9286   ret_offset = (s->output_offset
9287                 + my_offset
9288                 + s->output_section->vma
9289                 - (input_section->output_offset
9290                    + input_section->output_section->vma
9291                    + offset + addend)
9292                 - 8);
9293
9294   tmp = tmp | ((ret_offset >> 2) & 0x00FFFFFF);
9295
9296   bfd_put_32 (output_bfd, (bfd_vma) tmp, hit_data - input_section->vma);
9297
9298   return TRUE;
9299 }
9300
9301 /* Populate Arm stub for an exported Thumb function.  */
9302
9303 static bfd_boolean
9304 elf32_arm_to_thumb_export_stub (struct elf_link_hash_entry *h, void * inf)
9305 {
9306   struct bfd_link_info * info = (struct bfd_link_info *) inf;
9307   asection * s;
9308   struct elf_link_hash_entry * myh;
9309   struct elf32_arm_link_hash_entry *eh;
9310   struct elf32_arm_link_hash_table * globals;
9311   asection *sec;
9312   bfd_vma val;
9313   char *error_message;
9314
9315   eh = elf32_arm_hash_entry (h);
9316   /* Allocate stubs for exported Thumb functions on v4t.  */
9317   if (eh->export_glue == NULL)
9318     return TRUE;
9319
9320   globals = elf32_arm_hash_table (info);
9321   BFD_ASSERT (globals != NULL);
9322   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
9323
9324   s = bfd_get_linker_section (globals->bfd_of_glue_owner,
9325                               ARM2THUMB_GLUE_SECTION_NAME);
9326   BFD_ASSERT (s != NULL);
9327   BFD_ASSERT (s->contents != NULL);
9328   BFD_ASSERT (s->output_section != NULL);
9329
9330   sec = eh->export_glue->root.u.def.section;
9331
9332   BFD_ASSERT (sec->output_section != NULL);
9333
9334   val = eh->export_glue->root.u.def.value + sec->output_offset
9335         + sec->output_section->vma;
9336
9337   myh = elf32_arm_create_thumb_stub (info, h->root.root.string,
9338                                      h->root.u.def.section->owner,
9339                                      globals->obfd, sec, val, s,
9340                                      &error_message);
9341   BFD_ASSERT (myh);
9342   return TRUE;
9343 }
9344
9345 /* Populate ARMv4 BX veneers.  Returns the absolute adress of the veneer.  */
9346
9347 static bfd_vma
9348 elf32_arm_bx_glue (struct bfd_link_info * info, int reg)
9349 {
9350   bfd_byte *p;
9351   bfd_vma glue_addr;
9352   asection *s;
9353   struct elf32_arm_link_hash_table *globals;
9354
9355   globals = elf32_arm_hash_table (info);
9356   BFD_ASSERT (globals != NULL);
9357   BFD_ASSERT (globals->bfd_of_glue_owner != NULL);
9358
9359   s = bfd_get_linker_section (globals->bfd_of_glue_owner,
9360                               ARM_BX_GLUE_SECTION_NAME);
9361   BFD_ASSERT (s != NULL);
9362   BFD_ASSERT (s->contents != NULL);
9363   BFD_ASSERT (s->output_section != NULL);
9364
9365   BFD_ASSERT (globals->bx_glue_offset[reg] & 2);
9366
9367   glue_addr = globals->bx_glue_offset[reg] & ~(bfd_vma)3;
9368
9369   if ((globals->bx_glue_offset[reg] & 1) == 0)
9370     {
9371       p = s->contents + glue_addr;
9372       bfd_put_32 (globals->obfd, armbx1_tst_insn + (reg << 16), p);
9373       bfd_put_32 (globals->obfd, armbx2_moveq_insn + reg, p + 4);
9374       bfd_put_32 (globals->obfd, armbx3_bx_insn + reg, p + 8);
9375       globals->bx_glue_offset[reg] |= 1;
9376     }
9377
9378   return glue_addr + s->output_section->vma + s->output_offset;
9379 }
9380
9381 /* Generate Arm stubs for exported Thumb symbols.  */
9382 static void
9383 elf32_arm_begin_write_processing (bfd *abfd ATTRIBUTE_UNUSED,
9384                                   struct bfd_link_info *link_info)
9385 {
9386   struct elf32_arm_link_hash_table * globals;
9387
9388   if (link_info == NULL)
9389     /* Ignore this if we are not called by the ELF backend linker.  */
9390     return;
9391
9392   globals = elf32_arm_hash_table (link_info);
9393   if (globals == NULL)
9394     return;
9395
9396   /* If blx is available then exported Thumb symbols are OK and there is
9397      nothing to do.  */
9398   if (globals->use_blx)
9399     return;
9400
9401   elf_link_hash_traverse (&globals->root, elf32_arm_to_thumb_export_stub,
9402                           link_info);
9403 }
9404
9405 /* Reserve space for COUNT dynamic relocations in relocation selection
9406    SRELOC.  */
9407
9408 static void
9409 elf32_arm_allocate_dynrelocs (struct bfd_link_info *info, asection *sreloc,
9410                               bfd_size_type count)
9411 {
9412   struct elf32_arm_link_hash_table *htab;
9413
9414   htab = elf32_arm_hash_table (info);
9415   BFD_ASSERT (htab->root.dynamic_sections_created);
9416   if (sreloc == NULL)
9417     abort ();
9418   sreloc->size += RELOC_SIZE (htab) * count;
9419 }
9420
9421 /* Reserve space for COUNT R_ARM_IRELATIVE relocations.  If the link is
9422    dynamic, the relocations should go in SRELOC, otherwise they should
9423    go in the special .rel.iplt section.  */
9424
9425 static void
9426 elf32_arm_allocate_irelocs (struct bfd_link_info *info, asection *sreloc,
9427                             bfd_size_type count)
9428 {
9429   struct elf32_arm_link_hash_table *htab;
9430
9431   htab = elf32_arm_hash_table (info);
9432   if (!htab->root.dynamic_sections_created)
9433     htab->root.irelplt->size += RELOC_SIZE (htab) * count;
9434   else
9435     {
9436       BFD_ASSERT (sreloc != NULL);
9437       sreloc->size += RELOC_SIZE (htab) * count;
9438     }
9439 }
9440
9441 /* Add relocation REL to the end of relocation section SRELOC.  */
9442
9443 static void
9444 elf32_arm_add_dynreloc (bfd *output_bfd, struct bfd_link_info *info,
9445                         asection *sreloc, Elf_Internal_Rela *rel)
9446 {
9447   bfd_byte *loc;
9448   struct elf32_arm_link_hash_table *htab;
9449
9450   htab = elf32_arm_hash_table (info);
9451   if (!htab->root.dynamic_sections_created
9452       && ELF32_R_TYPE (rel->r_info) == R_ARM_IRELATIVE)
9453     sreloc = htab->root.irelplt;
9454   if (sreloc == NULL)
9455     abort ();
9456   loc = sreloc->contents;
9457   loc += sreloc->reloc_count++ * RELOC_SIZE (htab);
9458   if (sreloc->reloc_count * RELOC_SIZE (htab) > sreloc->size)
9459     abort ();
9460   SWAP_RELOC_OUT (htab) (output_bfd, rel, loc);
9461 }
9462
9463 /* Allocate room for a PLT entry described by ROOT_PLT and ARM_PLT.
9464    IS_IPLT_ENTRY says whether the entry belongs to .iplt rather than
9465    to .plt.  */
9466
9467 static void
9468 elf32_arm_allocate_plt_entry (struct bfd_link_info *info,
9469                               bfd_boolean is_iplt_entry,
9470                               union gotplt_union *root_plt,
9471                               struct arm_plt_info *arm_plt)
9472 {
9473   struct elf32_arm_link_hash_table *htab;
9474   asection *splt;
9475   asection *sgotplt;
9476
9477   htab = elf32_arm_hash_table (info);
9478
9479   if (is_iplt_entry)
9480     {
9481       splt = htab->root.iplt;
9482       sgotplt = htab->root.igotplt;
9483
9484       /* NaCl uses a special first entry in .iplt too.  */
9485       if (htab->nacl_p && splt->size == 0)
9486         splt->size += htab->plt_header_size;
9487
9488       /* Allocate room for an R_ARM_IRELATIVE relocation in .rel.iplt.  */
9489       elf32_arm_allocate_irelocs (info, htab->root.irelplt, 1);
9490     }
9491   else
9492     {
9493       splt = htab->root.splt;
9494       sgotplt = htab->root.sgotplt;
9495
9496     if (htab->fdpic_p)
9497       {
9498         /* Allocate room for R_ARM_FUNCDESC_VALUE.  */
9499         /* For lazy binding, relocations will be put into .rel.plt, in
9500            .rel.got otherwise.  */
9501         /* FIXME: today we don't support lazy binding so put it in .rel.got */
9502         if (info->flags & DF_BIND_NOW)
9503           elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
9504         else
9505           elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
9506       }
9507     else
9508       {
9509         /* Allocate room for an R_JUMP_SLOT relocation in .rel.plt.  */
9510         elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
9511       }
9512
9513       /* If this is the first .plt entry, make room for the special
9514          first entry.  */
9515       if (splt->size == 0)
9516         splt->size += htab->plt_header_size;
9517
9518       htab->next_tls_desc_index++;
9519     }
9520
9521   /* Allocate the PLT entry itself, including any leading Thumb stub.  */
9522   if (elf32_arm_plt_needs_thumb_stub_p (info, arm_plt))
9523     splt->size += PLT_THUMB_STUB_SIZE;
9524   root_plt->offset = splt->size;
9525   splt->size += htab->plt_entry_size;
9526
9527   if (!htab->symbian_p)
9528     {
9529       /* We also need to make an entry in the .got.plt section, which
9530          will be placed in the .got section by the linker script.  */
9531       if (is_iplt_entry)
9532         arm_plt->got_offset = sgotplt->size;
9533       else
9534         arm_plt->got_offset = sgotplt->size - 8 * htab->num_tls_desc;
9535       if (htab->fdpic_p)
9536         /* Function descriptor takes 64 bits in GOT.  */
9537         sgotplt->size += 8;
9538       else
9539         sgotplt->size += 4;
9540     }
9541 }
9542
9543 static bfd_vma
9544 arm_movw_immediate (bfd_vma value)
9545 {
9546   return (value & 0x00000fff) | ((value & 0x0000f000) << 4);
9547 }
9548
9549 static bfd_vma
9550 arm_movt_immediate (bfd_vma value)
9551 {
9552   return ((value & 0x0fff0000) >> 16) | ((value & 0xf0000000) >> 12);
9553 }
9554
9555 /* Fill in a PLT entry and its associated GOT slot.  If DYNINDX == -1,
9556    the entry lives in .iplt and resolves to (*SYM_VALUE)().
9557    Otherwise, DYNINDX is the index of the symbol in the dynamic
9558    symbol table and SYM_VALUE is undefined.
9559
9560    ROOT_PLT points to the offset of the PLT entry from the start of its
9561    section (.iplt or .plt).  ARM_PLT points to the symbol's ARM-specific
9562    bookkeeping information.
9563
9564    Returns FALSE if there was a problem.  */
9565
9566 static bfd_boolean
9567 elf32_arm_populate_plt_entry (bfd *output_bfd, struct bfd_link_info *info,
9568                               union gotplt_union *root_plt,
9569                               struct arm_plt_info *arm_plt,
9570                               int dynindx, bfd_vma sym_value)
9571 {
9572   struct elf32_arm_link_hash_table *htab;
9573   asection *sgot;
9574   asection *splt;
9575   asection *srel;
9576   bfd_byte *loc;
9577   bfd_vma plt_index;
9578   Elf_Internal_Rela rel;
9579   bfd_vma plt_header_size;
9580   bfd_vma got_header_size;
9581
9582   htab = elf32_arm_hash_table (info);
9583
9584   /* Pick the appropriate sections and sizes.  */
9585   if (dynindx == -1)
9586     {
9587       splt = htab->root.iplt;
9588       sgot = htab->root.igotplt;
9589       srel = htab->root.irelplt;
9590
9591       /* There are no reserved entries in .igot.plt, and no special
9592          first entry in .iplt.  */
9593       got_header_size = 0;
9594       plt_header_size = 0;
9595     }
9596   else
9597     {
9598       splt = htab->root.splt;
9599       sgot = htab->root.sgotplt;
9600       srel = htab->root.srelplt;
9601
9602       got_header_size = get_elf_backend_data (output_bfd)->got_header_size;
9603       plt_header_size = htab->plt_header_size;
9604     }
9605   BFD_ASSERT (splt != NULL && srel != NULL);
9606
9607   /* Fill in the entry in the procedure linkage table.  */
9608   if (htab->symbian_p)
9609     {
9610       BFD_ASSERT (dynindx >= 0);
9611       put_arm_insn (htab, output_bfd,
9612                     elf32_arm_symbian_plt_entry[0],
9613                     splt->contents + root_plt->offset);
9614       bfd_put_32 (output_bfd,
9615                   elf32_arm_symbian_plt_entry[1],
9616                   splt->contents + root_plt->offset + 4);
9617
9618       /* Fill in the entry in the .rel.plt section.  */
9619       rel.r_offset = (splt->output_section->vma
9620                       + splt->output_offset
9621                       + root_plt->offset + 4);
9622       rel.r_info = ELF32_R_INFO (dynindx, R_ARM_GLOB_DAT);
9623
9624       /* Get the index in the procedure linkage table which
9625          corresponds to this symbol.  This is the index of this symbol
9626          in all the symbols for which we are making plt entries.  The
9627          first entry in the procedure linkage table is reserved.  */
9628       plt_index = ((root_plt->offset - plt_header_size)
9629                    / htab->plt_entry_size);
9630     }
9631   else
9632     {
9633       bfd_vma got_offset, got_address, plt_address;
9634       bfd_vma got_displacement, initial_got_entry;
9635       bfd_byte * ptr;
9636
9637       BFD_ASSERT (sgot != NULL);
9638
9639       /* Get the offset into the .(i)got.plt table of the entry that
9640          corresponds to this function.  */
9641       got_offset = (arm_plt->got_offset & -2);
9642
9643       /* Get the index in the procedure linkage table which
9644          corresponds to this symbol.  This is the index of this symbol
9645          in all the symbols for which we are making plt entries.
9646          After the reserved .got.plt entries, all symbols appear in
9647          the same order as in .plt.  */
9648       if (htab->fdpic_p)
9649         /* Function descriptor takes 8 bytes.  */
9650         plt_index = (got_offset - got_header_size) / 8;
9651       else
9652         plt_index = (got_offset - got_header_size) / 4;
9653
9654       /* Calculate the address of the GOT entry.  */
9655       got_address = (sgot->output_section->vma
9656                      + sgot->output_offset
9657                      + got_offset);
9658
9659       /* ...and the address of the PLT entry.  */
9660       plt_address = (splt->output_section->vma
9661                      + splt->output_offset
9662                      + root_plt->offset);
9663
9664       ptr = splt->contents + root_plt->offset;
9665       if (htab->vxworks_p && bfd_link_pic (info))
9666         {
9667           unsigned int i;
9668           bfd_vma val;
9669
9670           for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
9671             {
9672               val = elf32_arm_vxworks_shared_plt_entry[i];
9673               if (i == 2)
9674                 val |= got_address - sgot->output_section->vma;
9675               if (i == 5)
9676                 val |= plt_index * RELOC_SIZE (htab);
9677               if (i == 2 || i == 5)
9678                 bfd_put_32 (output_bfd, val, ptr);
9679               else
9680                 put_arm_insn (htab, output_bfd, val, ptr);
9681             }
9682         }
9683       else if (htab->vxworks_p)
9684         {
9685           unsigned int i;
9686           bfd_vma val;
9687
9688           for (i = 0; i != htab->plt_entry_size / 4; i++, ptr += 4)
9689             {
9690               val = elf32_arm_vxworks_exec_plt_entry[i];
9691               if (i == 2)
9692                 val |= got_address;
9693               if (i == 4)
9694                 val |= 0xffffff & -((root_plt->offset + i * 4 + 8) >> 2);
9695               if (i == 5)
9696                 val |= plt_index * RELOC_SIZE (htab);
9697               if (i == 2 || i == 5)
9698                 bfd_put_32 (output_bfd, val, ptr);
9699               else
9700                 put_arm_insn (htab, output_bfd, val, ptr);
9701             }
9702
9703           loc = (htab->srelplt2->contents
9704                  + (plt_index * 2 + 1) * RELOC_SIZE (htab));
9705
9706           /* Create the .rela.plt.unloaded R_ARM_ABS32 relocation
9707              referencing the GOT for this PLT entry.  */
9708           rel.r_offset = plt_address + 8;
9709           rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
9710           rel.r_addend = got_offset;
9711           SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
9712           loc += RELOC_SIZE (htab);
9713
9714           /* Create the R_ARM_ABS32 relocation referencing the
9715              beginning of the PLT for this GOT entry.  */
9716           rel.r_offset = got_address;
9717           rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
9718           rel.r_addend = 0;
9719           SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
9720         }
9721       else if (htab->nacl_p)
9722         {
9723           /* Calculate the displacement between the PLT slot and the
9724              common tail that's part of the special initial PLT slot.  */
9725           int32_t tail_displacement
9726             = ((splt->output_section->vma + splt->output_offset
9727                 + ARM_NACL_PLT_TAIL_OFFSET)
9728                - (plt_address + htab->plt_entry_size + 4));
9729           BFD_ASSERT ((tail_displacement & 3) == 0);
9730           tail_displacement >>= 2;
9731
9732           BFD_ASSERT ((tail_displacement & 0xff000000) == 0
9733                       || (-tail_displacement & 0xff000000) == 0);
9734
9735           /* Calculate the displacement between the PLT slot and the entry
9736              in the GOT.  The offset accounts for the value produced by
9737              adding to pc in the penultimate instruction of the PLT stub.  */
9738           got_displacement = (got_address
9739                               - (plt_address + htab->plt_entry_size));
9740
9741           /* NaCl does not support interworking at all.  */
9742           BFD_ASSERT (!elf32_arm_plt_needs_thumb_stub_p (info, arm_plt));
9743
9744           put_arm_insn (htab, output_bfd,
9745                         elf32_arm_nacl_plt_entry[0]
9746                         | arm_movw_immediate (got_displacement),
9747                         ptr + 0);
9748           put_arm_insn (htab, output_bfd,
9749                         elf32_arm_nacl_plt_entry[1]
9750                         | arm_movt_immediate (got_displacement),
9751                         ptr + 4);
9752           put_arm_insn (htab, output_bfd,
9753                         elf32_arm_nacl_plt_entry[2],
9754                         ptr + 8);
9755           put_arm_insn (htab, output_bfd,
9756                         elf32_arm_nacl_plt_entry[3]
9757                         | (tail_displacement & 0x00ffffff),
9758                         ptr + 12);
9759         }
9760       else if (htab->fdpic_p)
9761         {
9762           const bfd_vma *plt_entry = using_thumb_only(htab)
9763             ? elf32_arm_fdpic_thumb_plt_entry
9764             : elf32_arm_fdpic_plt_entry;
9765
9766           /* Fill-up Thumb stub if needed.  */
9767           if (elf32_arm_plt_needs_thumb_stub_p (info, arm_plt))
9768             {
9769               put_thumb_insn (htab, output_bfd,
9770                               elf32_arm_plt_thumb_stub[0], ptr - 4);
9771               put_thumb_insn (htab, output_bfd,
9772                               elf32_arm_plt_thumb_stub[1], ptr - 2);
9773             }
9774           /* As we are using 32 bit instructions even for the Thumb
9775              version, we have to use 'put_arm_insn' instead of
9776              'put_thumb_insn'.  */
9777           put_arm_insn(htab, output_bfd, plt_entry[0], ptr + 0);
9778           put_arm_insn(htab, output_bfd, plt_entry[1], ptr + 4);
9779           put_arm_insn(htab, output_bfd, plt_entry[2], ptr + 8);
9780           put_arm_insn(htab, output_bfd, plt_entry[3], ptr + 12);
9781           bfd_put_32 (output_bfd, got_offset, ptr + 16);
9782
9783           if (!(info->flags & DF_BIND_NOW))
9784             {
9785               /* funcdesc_value_reloc_offset.  */
9786               bfd_put_32 (output_bfd,
9787                           htab->root.srelplt->reloc_count * RELOC_SIZE (htab),
9788                           ptr + 20);
9789               put_arm_insn(htab, output_bfd, plt_entry[6], ptr + 24);
9790               put_arm_insn(htab, output_bfd, plt_entry[7], ptr + 28);
9791               put_arm_insn(htab, output_bfd, plt_entry[8], ptr + 32);
9792               put_arm_insn(htab, output_bfd, plt_entry[9], ptr + 36);
9793             }
9794         }
9795       else if (using_thumb_only (htab))
9796         {
9797           /* PR ld/16017: Generate thumb only PLT entries.  */
9798           if (!using_thumb2 (htab))
9799             {
9800               /* FIXME: We ought to be able to generate thumb-1 PLT
9801                  instructions...  */
9802               _bfd_error_handler (_("%pB: warning: thumb-1 mode PLT generation not currently supported"),
9803                                   output_bfd);
9804               return FALSE;
9805             }
9806
9807           /* Calculate the displacement between the PLT slot and the entry in
9808              the GOT.  The 12-byte offset accounts for the value produced by
9809              adding to pc in the 3rd instruction of the PLT stub.  */
9810           got_displacement = got_address - (plt_address + 12);
9811
9812           /* As we are using 32 bit instructions we have to use 'put_arm_insn'
9813              instead of 'put_thumb_insn'.  */
9814           put_arm_insn (htab, output_bfd,
9815                         elf32_thumb2_plt_entry[0]
9816                         | ((got_displacement & 0x000000ff) << 16)
9817                         | ((got_displacement & 0x00000700) << 20)
9818                         | ((got_displacement & 0x00000800) >>  1)
9819                         | ((got_displacement & 0x0000f000) >> 12),
9820                         ptr + 0);
9821           put_arm_insn (htab, output_bfd,
9822                         elf32_thumb2_plt_entry[1]
9823                         | ((got_displacement & 0x00ff0000)      )
9824                         | ((got_displacement & 0x07000000) <<  4)
9825                         | ((got_displacement & 0x08000000) >> 17)
9826                         | ((got_displacement & 0xf0000000) >> 28),
9827                         ptr + 4);
9828           put_arm_insn (htab, output_bfd,
9829                         elf32_thumb2_plt_entry[2],
9830                         ptr + 8);
9831           put_arm_insn (htab, output_bfd,
9832                         elf32_thumb2_plt_entry[3],
9833                         ptr + 12);
9834         }
9835       else
9836         {
9837           /* Calculate the displacement between the PLT slot and the
9838              entry in the GOT.  The eight-byte offset accounts for the
9839              value produced by adding to pc in the first instruction
9840              of the PLT stub.  */
9841           got_displacement = got_address - (plt_address + 8);
9842
9843           if (elf32_arm_plt_needs_thumb_stub_p (info, arm_plt))
9844             {
9845               put_thumb_insn (htab, output_bfd,
9846                               elf32_arm_plt_thumb_stub[0], ptr - 4);
9847               put_thumb_insn (htab, output_bfd,
9848                               elf32_arm_plt_thumb_stub[1], ptr - 2);
9849             }
9850
9851           if (!elf32_arm_use_long_plt_entry)
9852             {
9853               BFD_ASSERT ((got_displacement & 0xf0000000) == 0);
9854
9855               put_arm_insn (htab, output_bfd,
9856                             elf32_arm_plt_entry_short[0]
9857                             | ((got_displacement & 0x0ff00000) >> 20),
9858                             ptr + 0);
9859               put_arm_insn (htab, output_bfd,
9860                             elf32_arm_plt_entry_short[1]
9861                             | ((got_displacement & 0x000ff000) >> 12),
9862                             ptr+ 4);
9863               put_arm_insn (htab, output_bfd,
9864                             elf32_arm_plt_entry_short[2]
9865                             | (got_displacement & 0x00000fff),
9866                             ptr + 8);
9867 #ifdef FOUR_WORD_PLT
9868               bfd_put_32 (output_bfd, elf32_arm_plt_entry_short[3], ptr + 12);
9869 #endif
9870             }
9871           else
9872             {
9873               put_arm_insn (htab, output_bfd,
9874                             elf32_arm_plt_entry_long[0]
9875                             | ((got_displacement & 0xf0000000) >> 28),
9876                             ptr + 0);
9877               put_arm_insn (htab, output_bfd,
9878                             elf32_arm_plt_entry_long[1]
9879                             | ((got_displacement & 0x0ff00000) >> 20),
9880                             ptr + 4);
9881               put_arm_insn (htab, output_bfd,
9882                             elf32_arm_plt_entry_long[2]
9883                             | ((got_displacement & 0x000ff000) >> 12),
9884                             ptr+ 8);
9885               put_arm_insn (htab, output_bfd,
9886                             elf32_arm_plt_entry_long[3]
9887                             | (got_displacement & 0x00000fff),
9888                             ptr + 12);
9889             }
9890         }
9891
9892       /* Fill in the entry in the .rel(a).(i)plt section.  */
9893       rel.r_offset = got_address;
9894       rel.r_addend = 0;
9895       if (dynindx == -1)
9896         {
9897           /* .igot.plt entries use IRELATIVE relocations against SYM_VALUE.
9898              The dynamic linker or static executable then calls SYM_VALUE
9899              to determine the correct run-time value of the .igot.plt entry.  */
9900           rel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
9901           initial_got_entry = sym_value;
9902         }
9903       else
9904         {
9905           /* For FDPIC we will have to resolve a R_ARM_FUNCDESC_VALUE
9906              used by PLT entry.  */
9907           if (htab->fdpic_p)
9908             {
9909               rel.r_info = ELF32_R_INFO (dynindx, R_ARM_FUNCDESC_VALUE);
9910               initial_got_entry = 0;
9911             }
9912           else
9913             {
9914               rel.r_info = ELF32_R_INFO (dynindx, R_ARM_JUMP_SLOT);
9915               initial_got_entry = (splt->output_section->vma
9916                                    + splt->output_offset);
9917             }
9918         }
9919
9920       /* Fill in the entry in the global offset table.  */
9921       bfd_put_32 (output_bfd, initial_got_entry,
9922                   sgot->contents + got_offset);
9923
9924       if (htab->fdpic_p && !(info->flags & DF_BIND_NOW))
9925         {
9926           /* Setup initial funcdesc value.  */
9927           /* FIXME: we don't support lazy binding because there is a
9928              race condition between both words getting written and
9929              some other thread attempting to read them. The ARM
9930              architecture does not have an atomic 64 bit load/store
9931              instruction that could be used to prevent it; it is
9932              recommended that threaded FDPIC applications run with the
9933              LD_BIND_NOW environment variable set.  */
9934           bfd_put_32(output_bfd, plt_address + 0x18,
9935                      sgot->contents + got_offset);
9936           bfd_put_32(output_bfd, -1 /*TODO*/,
9937                      sgot->contents + got_offset + 4);
9938         }
9939     }
9940
9941   if (dynindx == -1)
9942     elf32_arm_add_dynreloc (output_bfd, info, srel, &rel);
9943   else
9944     {
9945       if (htab->fdpic_p)
9946         {
9947           /* For FDPIC we put PLT relocationss into .rel.got when not
9948              lazy binding otherwise we put them in .rel.plt.  For now,
9949              we don't support lazy binding so put it in .rel.got.  */
9950           if (info->flags & DF_BIND_NOW)
9951             elf32_arm_add_dynreloc(output_bfd, info, htab->root.srelgot, &rel);
9952           else
9953             elf32_arm_add_dynreloc(output_bfd, info, htab->root.srelplt, &rel);
9954         }
9955       else
9956         {
9957           loc = srel->contents + plt_index * RELOC_SIZE (htab);
9958           SWAP_RELOC_OUT (htab) (output_bfd, &rel, loc);
9959         }
9960     }
9961
9962   return TRUE;
9963 }
9964
9965 /* Some relocations map to different relocations depending on the
9966    target.  Return the real relocation.  */
9967
9968 static int
9969 arm_real_reloc_type (struct elf32_arm_link_hash_table * globals,
9970                      int r_type)
9971 {
9972   switch (r_type)
9973     {
9974     case R_ARM_TARGET1:
9975       if (globals->target1_is_rel)
9976         return R_ARM_REL32;
9977       else
9978         return R_ARM_ABS32;
9979
9980     case R_ARM_TARGET2:
9981       return globals->target2_reloc;
9982
9983     default:
9984       return r_type;
9985     }
9986 }
9987
9988 /* Return the base VMA address which should be subtracted from real addresses
9989    when resolving @dtpoff relocation.
9990    This is PT_TLS segment p_vaddr.  */
9991
9992 static bfd_vma
9993 dtpoff_base (struct bfd_link_info *info)
9994 {
9995   /* If tls_sec is NULL, we should have signalled an error already.  */
9996   if (elf_hash_table (info)->tls_sec == NULL)
9997     return 0;
9998   return elf_hash_table (info)->tls_sec->vma;
9999 }
10000
10001 /* Return the relocation value for @tpoff relocation
10002    if STT_TLS virtual address is ADDRESS.  */
10003
10004 static bfd_vma
10005 tpoff (struct bfd_link_info *info, bfd_vma address)
10006 {
10007   struct elf_link_hash_table *htab = elf_hash_table (info);
10008   bfd_vma base;
10009
10010   /* If tls_sec is NULL, we should have signalled an error already.  */
10011   if (htab->tls_sec == NULL)
10012     return 0;
10013   base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
10014   return address - htab->tls_sec->vma + base;
10015 }
10016
10017 /* Perform an R_ARM_ABS12 relocation on the field pointed to by DATA.
10018    VALUE is the relocation value.  */
10019
10020 static bfd_reloc_status_type
10021 elf32_arm_abs12_reloc (bfd *abfd, void *data, bfd_vma value)
10022 {
10023   if (value > 0xfff)
10024     return bfd_reloc_overflow;
10025
10026   value |= bfd_get_32 (abfd, data) & 0xfffff000;
10027   bfd_put_32 (abfd, value, data);
10028   return bfd_reloc_ok;
10029 }
10030
10031 /* Handle TLS relaxations.  Relaxing is possible for symbols that use
10032    R_ARM_GOTDESC, R_ARM_{,THM_}TLS_CALL or
10033    R_ARM_{,THM_}TLS_DESCSEQ relocations, during a static link.
10034
10035    Return bfd_reloc_ok if we're done, bfd_reloc_continue if the caller
10036    is to then call final_link_relocate.  Return other values in the
10037    case of error.
10038
10039    FIXME:When --emit-relocs is in effect, we'll emit relocs describing
10040    the pre-relaxed code.  It would be nice if the relocs were updated
10041    to match the optimization.   */
10042
10043 static bfd_reloc_status_type
10044 elf32_arm_tls_relax (struct elf32_arm_link_hash_table *globals,
10045                      bfd *input_bfd, asection *input_sec, bfd_byte *contents,
10046                      Elf_Internal_Rela *rel, unsigned long is_local)
10047 {
10048   unsigned long insn;
10049
10050   switch (ELF32_R_TYPE (rel->r_info))
10051     {
10052     default:
10053       return bfd_reloc_notsupported;
10054
10055     case R_ARM_TLS_GOTDESC:
10056       if (is_local)
10057         insn = 0;
10058       else
10059         {
10060           insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
10061           if (insn & 1)
10062             insn -= 5; /* THUMB */
10063           else
10064             insn -= 8; /* ARM */
10065         }
10066       bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
10067       return bfd_reloc_continue;
10068
10069     case R_ARM_THM_TLS_DESCSEQ:
10070       /* Thumb insn.  */
10071       insn = bfd_get_16 (input_bfd, contents + rel->r_offset);
10072       if ((insn & 0xff78) == 0x4478)      /* add rx, pc */
10073         {
10074           if (is_local)
10075             /* nop */
10076             bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
10077         }
10078       else if ((insn & 0xffc0) == 0x6840)  /* ldr rx,[ry,#4] */
10079         {
10080           if (is_local)
10081             /* nop */
10082             bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
10083           else
10084             /* ldr rx,[ry] */
10085             bfd_put_16 (input_bfd, insn & 0xf83f, contents + rel->r_offset);
10086         }
10087       else if ((insn & 0xff87) == 0x4780)  /* blx rx */
10088         {
10089           if (is_local)
10090             /* nop */
10091             bfd_put_16 (input_bfd, 0x46c0, contents + rel->r_offset);
10092           else
10093             /* mov r0, rx */
10094             bfd_put_16 (input_bfd, 0x4600 | (insn & 0x78),
10095                         contents + rel->r_offset);
10096         }
10097       else
10098         {
10099           if ((insn & 0xf000) == 0xf000 || (insn & 0xf800) == 0xe800)
10100             /* It's a 32 bit instruction, fetch the rest of it for
10101                error generation.  */
10102             insn = (insn << 16)
10103               | bfd_get_16 (input_bfd, contents + rel->r_offset + 2);
10104           _bfd_error_handler
10105             /* xgettext:c-format */
10106             (_("%pB(%pA+%#" PRIx64 "): "
10107                "unexpected %s instruction '%#lx' in TLS trampoline"),
10108              input_bfd, input_sec, (uint64_t) rel->r_offset,
10109              "Thumb", insn);
10110           return bfd_reloc_notsupported;
10111         }
10112       break;
10113
10114     case R_ARM_TLS_DESCSEQ:
10115       /* arm insn.  */
10116       insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
10117       if ((insn & 0xffff0ff0) == 0xe08f0000) /* add rx,pc,ry */
10118         {
10119           if (is_local)
10120             /* mov rx, ry */
10121             bfd_put_32 (input_bfd, 0xe1a00000 | (insn & 0xffff),
10122                         contents + rel->r_offset);
10123         }
10124       else if ((insn & 0xfff00fff) == 0xe5900004) /* ldr rx,[ry,#4]*/
10125         {
10126           if (is_local)
10127             /* nop */
10128             bfd_put_32 (input_bfd, 0xe1a00000, contents + rel->r_offset);
10129           else
10130             /* ldr rx,[ry] */
10131             bfd_put_32 (input_bfd, insn & 0xfffff000,
10132                         contents + rel->r_offset);
10133         }
10134       else if ((insn & 0xfffffff0) == 0xe12fff30) /* blx rx */
10135         {
10136           if (is_local)
10137             /* nop */
10138             bfd_put_32 (input_bfd, 0xe1a00000, contents + rel->r_offset);
10139           else
10140             /* mov r0, rx */
10141             bfd_put_32 (input_bfd, 0xe1a00000 | (insn & 0xf),
10142                         contents + rel->r_offset);
10143         }
10144       else
10145         {
10146           _bfd_error_handler
10147             /* xgettext:c-format */
10148             (_("%pB(%pA+%#" PRIx64 "): "
10149                "unexpected %s instruction '%#lx' in TLS trampoline"),
10150              input_bfd, input_sec, (uint64_t) rel->r_offset,
10151              "ARM", insn);
10152           return bfd_reloc_notsupported;
10153         }
10154       break;
10155
10156     case R_ARM_TLS_CALL:
10157       /* GD->IE relaxation, turn the instruction into 'nop' or
10158          'ldr r0, [pc,r0]'  */
10159       insn = is_local ? 0xe1a00000 : 0xe79f0000;
10160       bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
10161       break;
10162
10163     case R_ARM_THM_TLS_CALL:
10164       /* GD->IE relaxation.  */
10165       if (!is_local)
10166         /* add r0,pc; ldr r0, [r0]  */
10167         insn = 0x44786800;
10168       else if (using_thumb2 (globals))
10169         /* nop.w */
10170         insn = 0xf3af8000;
10171       else
10172         /* nop; nop */
10173         insn = 0xbf00bf00;
10174
10175       bfd_put_16 (input_bfd, insn >> 16, contents + rel->r_offset);
10176       bfd_put_16 (input_bfd, insn & 0xffff, contents + rel->r_offset + 2);
10177       break;
10178     }
10179   return bfd_reloc_ok;
10180 }
10181
10182 /* For a given value of n, calculate the value of G_n as required to
10183    deal with group relocations.  We return it in the form of an
10184    encoded constant-and-rotation, together with the final residual.  If n is
10185    specified as less than zero, then final_residual is filled with the
10186    input value and no further action is performed.  */
10187
10188 static bfd_vma
10189 calculate_group_reloc_mask (bfd_vma value, int n, bfd_vma *final_residual)
10190 {
10191   int current_n;
10192   bfd_vma g_n;
10193   bfd_vma encoded_g_n = 0;
10194   bfd_vma residual = value; /* Also known as Y_n.  */
10195
10196   for (current_n = 0; current_n <= n; current_n++)
10197     {
10198       int shift;
10199
10200       /* Calculate which part of the value to mask.  */
10201       if (residual == 0)
10202         shift = 0;
10203       else
10204         {
10205           int msb;
10206
10207           /* Determine the most significant bit in the residual and
10208              align the resulting value to a 2-bit boundary.  */
10209           for (msb = 30; msb >= 0; msb -= 2)
10210             if (residual & (3 << msb))
10211               break;
10212
10213           /* The desired shift is now (msb - 6), or zero, whichever
10214              is the greater.  */
10215           shift = msb - 6;
10216           if (shift < 0)
10217             shift = 0;
10218         }
10219
10220       /* Calculate g_n in 32-bit as well as encoded constant+rotation form.  */
10221       g_n = residual & (0xff << shift);
10222       encoded_g_n = (g_n >> shift)
10223                     | ((g_n <= 0xff ? 0 : (32 - shift) / 2) << 8);
10224
10225       /* Calculate the residual for the next time around.  */
10226       residual &= ~g_n;
10227     }
10228
10229   *final_residual = residual;
10230
10231   return encoded_g_n;
10232 }
10233
10234 /* Given an ARM instruction, determine whether it is an ADD or a SUB.
10235    Returns 1 if it is an ADD, -1 if it is a SUB, and 0 otherwise.  */
10236
10237 static int
10238 identify_add_or_sub (bfd_vma insn)
10239 {
10240   int opcode = insn & 0x1e00000;
10241
10242   if (opcode == 1 << 23) /* ADD */
10243     return 1;
10244
10245   if (opcode == 1 << 22) /* SUB */
10246     return -1;
10247
10248   return 0;
10249 }
10250
10251 /* Perform a relocation as part of a final link.  */
10252
10253 static bfd_reloc_status_type
10254 elf32_arm_final_link_relocate (reloc_howto_type *           howto,
10255                                bfd *                        input_bfd,
10256                                bfd *                        output_bfd,
10257                                asection *                   input_section,
10258                                bfd_byte *                   contents,
10259                                Elf_Internal_Rela *          rel,
10260                                bfd_vma                      value,
10261                                struct bfd_link_info *       info,
10262                                asection *                   sym_sec,
10263                                const char *                 sym_name,
10264                                unsigned char                st_type,
10265                                enum arm_st_branch_type      branch_type,
10266                                struct elf_link_hash_entry * h,
10267                                bfd_boolean *                unresolved_reloc_p,
10268                                char **                      error_message)
10269 {
10270   unsigned long                 r_type = howto->type;
10271   unsigned long                 r_symndx;
10272   bfd_byte *                    hit_data = contents + rel->r_offset;
10273   bfd_vma *                     local_got_offsets;
10274   bfd_vma *                     local_tlsdesc_gotents;
10275   asection *                    sgot;
10276   asection *                    splt;
10277   asection *                    sreloc = NULL;
10278   asection *                    srelgot;
10279   bfd_vma                       addend;
10280   bfd_signed_vma                signed_addend;
10281   unsigned char                 dynreloc_st_type;
10282   bfd_vma                       dynreloc_value;
10283   struct elf32_arm_link_hash_table * globals;
10284   struct elf32_arm_link_hash_entry *eh;
10285   union gotplt_union           *root_plt;
10286   struct arm_plt_info          *arm_plt;
10287   bfd_vma                       plt_offset;
10288   bfd_vma                       gotplt_offset;
10289   bfd_boolean                   has_iplt_entry;
10290   bfd_boolean                   resolved_to_zero;
10291
10292   globals = elf32_arm_hash_table (info);
10293   if (globals == NULL)
10294     return bfd_reloc_notsupported;
10295
10296   BFD_ASSERT (is_arm_elf (input_bfd));
10297   BFD_ASSERT (howto != NULL);
10298
10299   /* Some relocation types map to different relocations depending on the
10300      target.  We pick the right one here.  */
10301   r_type = arm_real_reloc_type (globals, r_type);
10302
10303   /* It is possible to have linker relaxations on some TLS access
10304      models.  Update our information here.  */
10305   r_type = elf32_arm_tls_transition (info, r_type, h);
10306
10307   if (r_type != howto->type)
10308     howto = elf32_arm_howto_from_type (r_type);
10309
10310   eh = (struct elf32_arm_link_hash_entry *) h;
10311   sgot = globals->root.sgot;
10312   local_got_offsets = elf_local_got_offsets (input_bfd);
10313   local_tlsdesc_gotents = elf32_arm_local_tlsdesc_gotent (input_bfd);
10314
10315   if (globals->root.dynamic_sections_created)
10316     srelgot = globals->root.srelgot;
10317   else
10318     srelgot = NULL;
10319
10320   r_symndx = ELF32_R_SYM (rel->r_info);
10321
10322   if (globals->use_rel)
10323     {
10324       addend = bfd_get_32 (input_bfd, hit_data) & howto->src_mask;
10325
10326       if (addend & ((howto->src_mask + 1) >> 1))
10327         {
10328           signed_addend = -1;
10329           signed_addend &= ~ howto->src_mask;
10330           signed_addend |= addend;
10331         }
10332       else
10333         signed_addend = addend;
10334     }
10335   else
10336     addend = signed_addend = rel->r_addend;
10337
10338   /* ST_BRANCH_TO_ARM is nonsense to thumb-only targets when we
10339      are resolving a function call relocation.  */
10340   if (using_thumb_only (globals)
10341       && (r_type == R_ARM_THM_CALL
10342           || r_type == R_ARM_THM_JUMP24)
10343       && branch_type == ST_BRANCH_TO_ARM)
10344     branch_type = ST_BRANCH_TO_THUMB;
10345
10346   /* Record the symbol information that should be used in dynamic
10347      relocations.  */
10348   dynreloc_st_type = st_type;
10349   dynreloc_value = value;
10350   if (branch_type == ST_BRANCH_TO_THUMB)
10351     dynreloc_value |= 1;
10352
10353   /* Find out whether the symbol has a PLT.  Set ST_VALUE, BRANCH_TYPE and
10354      VALUE appropriately for relocations that we resolve at link time.  */
10355   has_iplt_entry = FALSE;
10356   if (elf32_arm_get_plt_info (input_bfd, globals, eh, r_symndx, &root_plt,
10357                               &arm_plt)
10358       && root_plt->offset != (bfd_vma) -1)
10359     {
10360       plt_offset = root_plt->offset;
10361       gotplt_offset = arm_plt->got_offset;
10362
10363       if (h == NULL || eh->is_iplt)
10364         {
10365           has_iplt_entry = TRUE;
10366           splt = globals->root.iplt;
10367
10368           /* Populate .iplt entries here, because not all of them will
10369              be seen by finish_dynamic_symbol.  The lower bit is set if
10370              we have already populated the entry.  */
10371           if (plt_offset & 1)
10372             plt_offset--;
10373           else
10374             {
10375               if (elf32_arm_populate_plt_entry (output_bfd, info, root_plt, arm_plt,
10376                                                 -1, dynreloc_value))
10377                 root_plt->offset |= 1;
10378               else
10379                 return bfd_reloc_notsupported;
10380             }
10381
10382           /* Static relocations always resolve to the .iplt entry.  */
10383           st_type = STT_FUNC;
10384           value = (splt->output_section->vma
10385                    + splt->output_offset
10386                    + plt_offset);
10387           branch_type = ST_BRANCH_TO_ARM;
10388
10389           /* If there are non-call relocations that resolve to the .iplt
10390              entry, then all dynamic ones must too.  */
10391           if (arm_plt->noncall_refcount != 0)
10392             {
10393               dynreloc_st_type = st_type;
10394               dynreloc_value = value;
10395             }
10396         }
10397       else
10398         /* We populate the .plt entry in finish_dynamic_symbol.  */
10399         splt = globals->root.splt;
10400     }
10401   else
10402     {
10403       splt = NULL;
10404       plt_offset = (bfd_vma) -1;
10405       gotplt_offset = (bfd_vma) -1;
10406     }
10407
10408   resolved_to_zero = (h != NULL
10409                       && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
10410
10411   switch (r_type)
10412     {
10413     case R_ARM_NONE:
10414       /* We don't need to find a value for this symbol.  It's just a
10415          marker.  */
10416       *unresolved_reloc_p = FALSE;
10417       return bfd_reloc_ok;
10418
10419     case R_ARM_ABS12:
10420       if (!globals->vxworks_p)
10421         return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
10422       /* Fall through.  */
10423
10424     case R_ARM_PC24:
10425     case R_ARM_ABS32:
10426     case R_ARM_ABS32_NOI:
10427     case R_ARM_REL32:
10428     case R_ARM_REL32_NOI:
10429     case R_ARM_CALL:
10430     case R_ARM_JUMP24:
10431     case R_ARM_XPC25:
10432     case R_ARM_PREL31:
10433     case R_ARM_PLT32:
10434       /* Handle relocations which should use the PLT entry.  ABS32/REL32
10435          will use the symbol's value, which may point to a PLT entry, but we
10436          don't need to handle that here.  If we created a PLT entry, all
10437          branches in this object should go to it, except if the PLT is too
10438          far away, in which case a long branch stub should be inserted.  */
10439       if ((r_type != R_ARM_ABS32 && r_type != R_ARM_REL32
10440            && r_type != R_ARM_ABS32_NOI && r_type != R_ARM_REL32_NOI
10441            && r_type != R_ARM_CALL
10442            && r_type != R_ARM_JUMP24
10443            && r_type != R_ARM_PLT32)
10444           && plt_offset != (bfd_vma) -1)
10445         {
10446           /* If we've created a .plt section, and assigned a PLT entry
10447              to this function, it must either be a STT_GNU_IFUNC reference
10448              or not be known to bind locally.  In other cases, we should
10449              have cleared the PLT entry by now.  */
10450           BFD_ASSERT (has_iplt_entry || !SYMBOL_CALLS_LOCAL (info, h));
10451
10452           value = (splt->output_section->vma
10453                    + splt->output_offset
10454                    + plt_offset);
10455           *unresolved_reloc_p = FALSE;
10456           return _bfd_final_link_relocate (howto, input_bfd, input_section,
10457                                            contents, rel->r_offset, value,
10458                                            rel->r_addend);
10459         }
10460
10461       /* When generating a shared object or relocatable executable, these
10462          relocations are copied into the output file to be resolved at
10463          run time.  */
10464       if ((bfd_link_pic (info)
10465            || globals->root.is_relocatable_executable
10466            || globals->fdpic_p)
10467           && (input_section->flags & SEC_ALLOC)
10468           && !(globals->vxworks_p
10469                && strcmp (input_section->output_section->name,
10470                           ".tls_vars") == 0)
10471           && ((r_type != R_ARM_REL32 && r_type != R_ARM_REL32_NOI)
10472               || !SYMBOL_CALLS_LOCAL (info, h))
10473           && !(input_bfd == globals->stub_bfd
10474                && strstr (input_section->name, STUB_SUFFIX))
10475           && (h == NULL
10476               || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10477                   && !resolved_to_zero)
10478               || h->root.type != bfd_link_hash_undefweak)
10479           && r_type != R_ARM_PC24
10480           && r_type != R_ARM_CALL
10481           && r_type != R_ARM_JUMP24
10482           && r_type != R_ARM_PREL31
10483           && r_type != R_ARM_PLT32)
10484         {
10485           Elf_Internal_Rela outrel;
10486           bfd_boolean skip, relocate;
10487           int isrofixup = 0;
10488
10489           if ((r_type == R_ARM_REL32 || r_type == R_ARM_REL32_NOI)
10490               && !h->def_regular)
10491             {
10492               char *v = _("shared object");
10493
10494               if (bfd_link_executable (info))
10495                 v = _("PIE executable");
10496
10497               _bfd_error_handler
10498                 (_("%pB: relocation %s against external or undefined symbol `%s'"
10499                    " can not be used when making a %s; recompile with -fPIC"), input_bfd,
10500                  elf32_arm_howto_table_1[r_type].name, h->root.root.string, v);
10501               return bfd_reloc_notsupported;
10502             }
10503
10504           *unresolved_reloc_p = FALSE;
10505
10506           if (sreloc == NULL && globals->root.dynamic_sections_created)
10507             {
10508               sreloc = _bfd_elf_get_dynamic_reloc_section (input_bfd, input_section,
10509                                                            ! globals->use_rel);
10510
10511               if (sreloc == NULL)
10512                 return bfd_reloc_notsupported;
10513             }
10514
10515           skip = FALSE;
10516           relocate = FALSE;
10517
10518           outrel.r_addend = addend;
10519           outrel.r_offset =
10520             _bfd_elf_section_offset (output_bfd, info, input_section,
10521                                      rel->r_offset);
10522           if (outrel.r_offset == (bfd_vma) -1)
10523             skip = TRUE;
10524           else if (outrel.r_offset == (bfd_vma) -2)
10525             skip = TRUE, relocate = TRUE;
10526           outrel.r_offset += (input_section->output_section->vma
10527                               + input_section->output_offset);
10528
10529           if (skip)
10530             memset (&outrel, 0, sizeof outrel);
10531           else if (h != NULL
10532                    && h->dynindx != -1
10533                    && (!bfd_link_pic (info)
10534                        || !(bfd_link_pie (info)
10535                             || SYMBOLIC_BIND (info, h))
10536                        || !h->def_regular))
10537             outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
10538           else
10539             {
10540               int symbol;
10541
10542               /* This symbol is local, or marked to become local.  */
10543               BFD_ASSERT (r_type == R_ARM_ABS32 || r_type == R_ARM_ABS32_NOI
10544                           || (globals->fdpic_p && !bfd_link_pic(info)));
10545               if (globals->symbian_p)
10546                 {
10547                   asection *osec;
10548
10549                   /* On Symbian OS, the data segment and text segement
10550                      can be relocated independently.  Therefore, we
10551                      must indicate the segment to which this
10552                      relocation is relative.  The BPABI allows us to
10553                      use any symbol in the right segment; we just use
10554                      the section symbol as it is convenient.  (We
10555                      cannot use the symbol given by "h" directly as it
10556                      will not appear in the dynamic symbol table.)
10557
10558                      Note that the dynamic linker ignores the section
10559                      symbol value, so we don't subtract osec->vma
10560                      from the emitted reloc addend.  */
10561                   if (sym_sec)
10562                     osec = sym_sec->output_section;
10563                   else
10564                     osec = input_section->output_section;
10565                   symbol = elf_section_data (osec)->dynindx;
10566                   if (symbol == 0)
10567                     {
10568                       struct elf_link_hash_table *htab = elf_hash_table (info);
10569
10570                       if ((osec->flags & SEC_READONLY) == 0
10571                           && htab->data_index_section != NULL)
10572                         osec = htab->data_index_section;
10573                       else
10574                         osec = htab->text_index_section;
10575                       symbol = elf_section_data (osec)->dynindx;
10576                     }
10577                   BFD_ASSERT (symbol != 0);
10578                 }
10579               else
10580                 /* On SVR4-ish systems, the dynamic loader cannot
10581                    relocate the text and data segments independently,
10582                    so the symbol does not matter.  */
10583                 symbol = 0;
10584               if (dynreloc_st_type == STT_GNU_IFUNC)
10585                 /* We have an STT_GNU_IFUNC symbol that doesn't resolve
10586                    to the .iplt entry.  Instead, every non-call reference
10587                    must use an R_ARM_IRELATIVE relocation to obtain the
10588                    correct run-time address.  */
10589                 outrel.r_info = ELF32_R_INFO (symbol, R_ARM_IRELATIVE);
10590               else if (globals->fdpic_p && !bfd_link_pic(info))
10591                 isrofixup = 1;
10592               else
10593                 outrel.r_info = ELF32_R_INFO (symbol, R_ARM_RELATIVE);
10594               if (globals->use_rel)
10595                 relocate = TRUE;
10596               else
10597                 outrel.r_addend += dynreloc_value;
10598             }
10599
10600           if (isrofixup)
10601             arm_elf_add_rofixup(output_bfd, globals->srofixup, outrel.r_offset);
10602           else
10603             elf32_arm_add_dynreloc (output_bfd, info, sreloc, &outrel);
10604
10605           /* If this reloc is against an external symbol, we do not want to
10606              fiddle with the addend.  Otherwise, we need to include the symbol
10607              value so that it becomes an addend for the dynamic reloc.  */
10608           if (! relocate)
10609             return bfd_reloc_ok;
10610
10611           return _bfd_final_link_relocate (howto, input_bfd, input_section,
10612                                            contents, rel->r_offset,
10613                                            dynreloc_value, (bfd_vma) 0);
10614         }
10615       else switch (r_type)
10616         {
10617         case R_ARM_ABS12:
10618           return elf32_arm_abs12_reloc (input_bfd, hit_data, value + addend);
10619
10620         case R_ARM_XPC25:         /* Arm BLX instruction.  */
10621         case R_ARM_CALL:
10622         case R_ARM_JUMP24:
10623         case R_ARM_PC24:          /* Arm B/BL instruction.  */
10624         case R_ARM_PLT32:
10625           {
10626           struct elf32_arm_stub_hash_entry *stub_entry = NULL;
10627
10628           if (r_type == R_ARM_XPC25)
10629             {
10630               /* Check for Arm calling Arm function.  */
10631               /* FIXME: Should we translate the instruction into a BL
10632                  instruction instead ?  */
10633               if (branch_type != ST_BRANCH_TO_THUMB)
10634                 _bfd_error_handler
10635                   (_("\%pB: warning: %s BLX instruction targets"
10636                      " %s function '%s'"),
10637                    input_bfd, "ARM",
10638                    "ARM", h ? h->root.root.string : "(local)");
10639             }
10640           else if (r_type == R_ARM_PC24)
10641             {
10642               /* Check for Arm calling Thumb function.  */
10643               if (branch_type == ST_BRANCH_TO_THUMB)
10644                 {
10645                   if (elf32_arm_to_thumb_stub (info, sym_name, input_bfd,
10646                                                output_bfd, input_section,
10647                                                hit_data, sym_sec, rel->r_offset,
10648                                                signed_addend, value,
10649                                                error_message))
10650                     return bfd_reloc_ok;
10651                   else
10652                     return bfd_reloc_dangerous;
10653                 }
10654             }
10655
10656           /* Check if a stub has to be inserted because the
10657              destination is too far or we are changing mode.  */
10658           if (   r_type == R_ARM_CALL
10659               || r_type == R_ARM_JUMP24
10660               || r_type == R_ARM_PLT32)
10661             {
10662               enum elf32_arm_stub_type stub_type = arm_stub_none;
10663               struct elf32_arm_link_hash_entry *hash;
10664
10665               hash = (struct elf32_arm_link_hash_entry *) h;
10666               stub_type = arm_type_of_stub (info, input_section, rel,
10667                                             st_type, &branch_type,
10668                                             hash, value, sym_sec,
10669                                             input_bfd, sym_name);
10670
10671               if (stub_type != arm_stub_none)
10672                 {
10673                   /* The target is out of reach, so redirect the
10674                      branch to the local stub for this function.  */
10675                   stub_entry = elf32_arm_get_stub_entry (input_section,
10676                                                          sym_sec, h,
10677                                                          rel, globals,
10678                                                          stub_type);
10679                   {
10680                     if (stub_entry != NULL)
10681                       value = (stub_entry->stub_offset
10682                                + stub_entry->stub_sec->output_offset
10683                                + stub_entry->stub_sec->output_section->vma);
10684
10685                     if (plt_offset != (bfd_vma) -1)
10686                       *unresolved_reloc_p = FALSE;
10687                   }
10688                 }
10689               else
10690                 {
10691                   /* If the call goes through a PLT entry, make sure to
10692                      check distance to the right destination address.  */
10693                   if (plt_offset != (bfd_vma) -1)
10694                     {
10695                       value = (splt->output_section->vma
10696                                + splt->output_offset
10697                                + plt_offset);
10698                       *unresolved_reloc_p = FALSE;
10699                       /* The PLT entry is in ARM mode, regardless of the
10700                          target function.  */
10701                       branch_type = ST_BRANCH_TO_ARM;
10702                     }
10703                 }
10704             }
10705
10706           /* The ARM ELF ABI says that this reloc is computed as: S - P + A
10707              where:
10708               S is the address of the symbol in the relocation.
10709               P is address of the instruction being relocated.
10710               A is the addend (extracted from the instruction) in bytes.
10711
10712              S is held in 'value'.
10713              P is the base address of the section containing the
10714                instruction plus the offset of the reloc into that
10715                section, ie:
10716                  (input_section->output_section->vma +
10717                   input_section->output_offset +
10718                   rel->r_offset).
10719              A is the addend, converted into bytes, ie:
10720                  (signed_addend * 4)
10721
10722              Note: None of these operations have knowledge of the pipeline
10723              size of the processor, thus it is up to the assembler to
10724              encode this information into the addend.  */
10725           value -= (input_section->output_section->vma
10726                     + input_section->output_offset);
10727           value -= rel->r_offset;
10728           if (globals->use_rel)
10729             value += (signed_addend << howto->size);
10730           else
10731             /* RELA addends do not have to be adjusted by howto->size.  */
10732             value += signed_addend;
10733
10734           signed_addend = value;
10735           signed_addend >>= howto->rightshift;
10736
10737           /* A branch to an undefined weak symbol is turned into a jump to
10738              the next instruction unless a PLT entry will be created.
10739              Do the same for local undefined symbols (but not for STN_UNDEF).
10740              The jump to the next instruction is optimized as a NOP depending
10741              on the architecture.  */
10742           if (h ? (h->root.type == bfd_link_hash_undefweak
10743                    && plt_offset == (bfd_vma) -1)
10744               : r_symndx != STN_UNDEF && bfd_is_und_section (sym_sec))
10745             {
10746               value = (bfd_get_32 (input_bfd, hit_data) & 0xf0000000);
10747
10748               if (arch_has_arm_nop (globals))
10749                 value |= 0x0320f000;
10750               else
10751                 value |= 0x01a00000; /* Using pre-UAL nop: mov r0, r0.  */
10752             }
10753           else
10754             {
10755               /* Perform a signed range check.  */
10756               if (   signed_addend >   ((bfd_signed_vma)  (howto->dst_mask >> 1))
10757                   || signed_addend < - ((bfd_signed_vma) ((howto->dst_mask + 1) >> 1)))
10758                 return bfd_reloc_overflow;
10759
10760               addend = (value & 2);
10761
10762               value = (signed_addend & howto->dst_mask)
10763                 | (bfd_get_32 (input_bfd, hit_data) & (~ howto->dst_mask));
10764
10765               if (r_type == R_ARM_CALL)
10766                 {
10767                   /* Set the H bit in the BLX instruction.  */
10768                   if (branch_type == ST_BRANCH_TO_THUMB)
10769                     {
10770                       if (addend)
10771                         value |= (1 << 24);
10772                       else
10773                         value &= ~(bfd_vma)(1 << 24);
10774                     }
10775
10776                   /* Select the correct instruction (BL or BLX).  */
10777                   /* Only if we are not handling a BL to a stub. In this
10778                      case, mode switching is performed by the stub.  */
10779                   if (branch_type == ST_BRANCH_TO_THUMB && !stub_entry)
10780                     value |= (1 << 28);
10781                   else if (stub_entry || branch_type != ST_BRANCH_UNKNOWN)
10782                     {
10783                       value &= ~(bfd_vma)(1 << 28);
10784                       value |= (1 << 24);
10785                     }
10786                 }
10787             }
10788           }
10789           break;
10790
10791         case R_ARM_ABS32:
10792           value += addend;
10793           if (branch_type == ST_BRANCH_TO_THUMB)
10794             value |= 1;
10795           break;
10796
10797         case R_ARM_ABS32_NOI:
10798           value += addend;
10799           break;
10800
10801         case R_ARM_REL32:
10802           value += addend;
10803           if (branch_type == ST_BRANCH_TO_THUMB)
10804             value |= 1;
10805           value -= (input_section->output_section->vma
10806                     + input_section->output_offset + rel->r_offset);
10807           break;
10808
10809         case R_ARM_REL32_NOI:
10810           value += addend;
10811           value -= (input_section->output_section->vma
10812                     + input_section->output_offset + rel->r_offset);
10813           break;
10814
10815         case R_ARM_PREL31:
10816           value -= (input_section->output_section->vma
10817                     + input_section->output_offset + rel->r_offset);
10818           value += signed_addend;
10819           if (! h || h->root.type != bfd_link_hash_undefweak)
10820             {
10821               /* Check for overflow.  */
10822               if ((value ^ (value >> 1)) & (1 << 30))
10823                 return bfd_reloc_overflow;
10824             }
10825           value &= 0x7fffffff;
10826           value |= (bfd_get_32 (input_bfd, hit_data) & 0x80000000);
10827           if (branch_type == ST_BRANCH_TO_THUMB)
10828             value |= 1;
10829           break;
10830         }
10831
10832       bfd_put_32 (input_bfd, value, hit_data);
10833       return bfd_reloc_ok;
10834
10835     case R_ARM_ABS8:
10836       /* PR 16202: Refectch the addend using the correct size.  */
10837       if (globals->use_rel)
10838         addend = bfd_get_8 (input_bfd, hit_data);
10839       value += addend;
10840
10841       /* There is no way to tell whether the user intended to use a signed or
10842          unsigned addend.  When checking for overflow we accept either,
10843          as specified by the AAELF.  */
10844       if ((long) value > 0xff || (long) value < -0x80)
10845         return bfd_reloc_overflow;
10846
10847       bfd_put_8 (input_bfd, value, hit_data);
10848       return bfd_reloc_ok;
10849
10850     case R_ARM_ABS16:
10851       /* PR 16202: Refectch the addend using the correct size.  */
10852       if (globals->use_rel)
10853         addend = bfd_get_16 (input_bfd, hit_data);
10854       value += addend;
10855
10856       /* See comment for R_ARM_ABS8.  */
10857       if ((long) value > 0xffff || (long) value < -0x8000)
10858         return bfd_reloc_overflow;
10859
10860       bfd_put_16 (input_bfd, value, hit_data);
10861       return bfd_reloc_ok;
10862
10863     case R_ARM_THM_ABS5:
10864       /* Support ldr and str instructions for the thumb.  */
10865       if (globals->use_rel)
10866         {
10867           /* Need to refetch addend.  */
10868           addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
10869           /* ??? Need to determine shift amount from operand size.  */
10870           addend >>= howto->rightshift;
10871         }
10872       value += addend;
10873
10874       /* ??? Isn't value unsigned?  */
10875       if ((long) value > 0x1f || (long) value < -0x10)
10876         return bfd_reloc_overflow;
10877
10878       /* ??? Value needs to be properly shifted into place first.  */
10879       value |= bfd_get_16 (input_bfd, hit_data) & 0xf83f;
10880       bfd_put_16 (input_bfd, value, hit_data);
10881       return bfd_reloc_ok;
10882
10883     case R_ARM_THM_ALU_PREL_11_0:
10884       /* Corresponds to: addw.w reg, pc, #offset (and similarly for subw).  */
10885       {
10886         bfd_vma insn;
10887         bfd_signed_vma relocation;
10888
10889         insn = (bfd_get_16 (input_bfd, hit_data) << 16)
10890              | bfd_get_16 (input_bfd, hit_data + 2);
10891
10892         if (globals->use_rel)
10893           {
10894             signed_addend = (insn & 0xff) | ((insn & 0x7000) >> 4)
10895                           | ((insn & (1 << 26)) >> 15);
10896             if (insn & 0xf00000)
10897               signed_addend = -signed_addend;
10898           }
10899
10900         relocation = value + signed_addend;
10901         relocation -= Pa (input_section->output_section->vma
10902                           + input_section->output_offset
10903                           + rel->r_offset);
10904
10905         /* PR 21523: Use an absolute value.  The user of this reloc will
10906            have already selected an ADD or SUB insn appropriately.  */
10907         value = labs (relocation);
10908
10909         if (value >= 0x1000)
10910           return bfd_reloc_overflow;
10911
10912         /* Destination is Thumb.  Force bit 0 to 1 to reflect this.  */
10913         if (branch_type == ST_BRANCH_TO_THUMB)
10914           value |= 1;
10915
10916         insn = (insn & 0xfb0f8f00) | (value & 0xff)
10917              | ((value & 0x700) << 4)
10918              | ((value & 0x800) << 15);
10919         if (relocation < 0)
10920           insn |= 0xa00000;
10921
10922         bfd_put_16 (input_bfd, insn >> 16, hit_data);
10923         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
10924
10925         return bfd_reloc_ok;
10926       }
10927
10928     case R_ARM_THM_PC8:
10929       /* PR 10073:  This reloc is not generated by the GNU toolchain,
10930          but it is supported for compatibility with third party libraries
10931          generated by other compilers, specifically the ARM/IAR.  */
10932       {
10933         bfd_vma insn;
10934         bfd_signed_vma relocation;
10935
10936         insn = bfd_get_16 (input_bfd, hit_data);
10937
10938         if (globals->use_rel)
10939           addend = ((((insn & 0x00ff) << 2) + 4) & 0x3ff) -4;
10940
10941         relocation = value + addend;
10942         relocation -= Pa (input_section->output_section->vma
10943                           + input_section->output_offset
10944                           + rel->r_offset);
10945
10946         value = relocation;
10947
10948         /* We do not check for overflow of this reloc.  Although strictly
10949            speaking this is incorrect, it appears to be necessary in order
10950            to work with IAR generated relocs.  Since GCC and GAS do not
10951            generate R_ARM_THM_PC8 relocs, the lack of a check should not be
10952            a problem for them.  */
10953         value &= 0x3fc;
10954
10955         insn = (insn & 0xff00) | (value >> 2);
10956
10957         bfd_put_16 (input_bfd, insn, hit_data);
10958
10959         return bfd_reloc_ok;
10960       }
10961
10962     case R_ARM_THM_PC12:
10963       /* Corresponds to: ldr.w reg, [pc, #offset].  */
10964       {
10965         bfd_vma insn;
10966         bfd_signed_vma relocation;
10967
10968         insn = (bfd_get_16 (input_bfd, hit_data) << 16)
10969              | bfd_get_16 (input_bfd, hit_data + 2);
10970
10971         if (globals->use_rel)
10972           {
10973             signed_addend = insn & 0xfff;
10974             if (!(insn & (1 << 23)))
10975               signed_addend = -signed_addend;
10976           }
10977
10978         relocation = value + signed_addend;
10979         relocation -= Pa (input_section->output_section->vma
10980                           + input_section->output_offset
10981                           + rel->r_offset);
10982
10983         value = relocation;
10984
10985         if (value >= 0x1000)
10986           return bfd_reloc_overflow;
10987
10988         insn = (insn & 0xff7ff000) | value;
10989         if (relocation >= 0)
10990           insn |= (1 << 23);
10991
10992         bfd_put_16 (input_bfd, insn >> 16, hit_data);
10993         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
10994
10995         return bfd_reloc_ok;
10996       }
10997
10998     case R_ARM_THM_XPC22:
10999     case R_ARM_THM_CALL:
11000     case R_ARM_THM_JUMP24:
11001       /* Thumb BL (branch long instruction).  */
11002       {
11003         bfd_vma relocation;
11004         bfd_vma reloc_sign;
11005         bfd_boolean overflow = FALSE;
11006         bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
11007         bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
11008         bfd_signed_vma reloc_signed_max;
11009         bfd_signed_vma reloc_signed_min;
11010         bfd_vma check;
11011         bfd_signed_vma signed_check;
11012         int bitsize;
11013         const int thumb2 = using_thumb2 (globals);
11014         const int thumb2_bl = using_thumb2_bl (globals);
11015
11016         /* A branch to an undefined weak symbol is turned into a jump to
11017            the next instruction unless a PLT entry will be created.
11018            The jump to the next instruction is optimized as a NOP.W for
11019            Thumb-2 enabled architectures.  */
11020         if (h && h->root.type == bfd_link_hash_undefweak
11021             && plt_offset == (bfd_vma) -1)
11022           {
11023             if (thumb2)
11024               {
11025                 bfd_put_16 (input_bfd, 0xf3af, hit_data);
11026                 bfd_put_16 (input_bfd, 0x8000, hit_data + 2);
11027               }
11028             else
11029               {
11030                 bfd_put_16 (input_bfd, 0xe000, hit_data);
11031                 bfd_put_16 (input_bfd, 0xbf00, hit_data + 2);
11032               }
11033             return bfd_reloc_ok;
11034           }
11035
11036         /* Fetch the addend.  We use the Thumb-2 encoding (backwards compatible
11037            with Thumb-1) involving the J1 and J2 bits.  */
11038         if (globals->use_rel)
11039           {
11040             bfd_vma s = (upper_insn & (1 << 10)) >> 10;
11041             bfd_vma upper = upper_insn & 0x3ff;
11042             bfd_vma lower = lower_insn & 0x7ff;
11043             bfd_vma j1 = (lower_insn & (1 << 13)) >> 13;
11044             bfd_vma j2 = (lower_insn & (1 << 11)) >> 11;
11045             bfd_vma i1 = j1 ^ s ? 0 : 1;
11046             bfd_vma i2 = j2 ^ s ? 0 : 1;
11047
11048             addend = (i1 << 23) | (i2 << 22) | (upper << 12) | (lower << 1);
11049             /* Sign extend.  */
11050             addend = (addend | ((s ? 0 : 1) << 24)) - (1 << 24);
11051
11052             signed_addend = addend;
11053           }
11054
11055         if (r_type == R_ARM_THM_XPC22)
11056           {
11057             /* Check for Thumb to Thumb call.  */
11058             /* FIXME: Should we translate the instruction into a BL
11059                instruction instead ?  */
11060             if (branch_type == ST_BRANCH_TO_THUMB)
11061               _bfd_error_handler
11062                 (_("%pB: warning: %s BLX instruction targets"
11063                    " %s function '%s'"),
11064                  input_bfd, "Thumb",
11065                  "Thumb", h ? h->root.root.string : "(local)");
11066           }
11067         else
11068           {
11069             /* If it is not a call to Thumb, assume call to Arm.
11070                If it is a call relative to a section name, then it is not a
11071                function call at all, but rather a long jump.  Calls through
11072                the PLT do not require stubs.  */
11073             if (branch_type == ST_BRANCH_TO_ARM && plt_offset == (bfd_vma) -1)
11074               {
11075                 if (globals->use_blx && r_type == R_ARM_THM_CALL)
11076                   {
11077                     /* Convert BL to BLX.  */
11078                     lower_insn = (lower_insn & ~0x1000) | 0x0800;
11079                   }
11080                 else if ((   r_type != R_ARM_THM_CALL)
11081                          && (r_type != R_ARM_THM_JUMP24))
11082                   {
11083                     if (elf32_thumb_to_arm_stub
11084                         (info, sym_name, input_bfd, output_bfd, input_section,
11085                          hit_data, sym_sec, rel->r_offset, signed_addend, value,
11086                          error_message))
11087                       return bfd_reloc_ok;
11088                     else
11089                       return bfd_reloc_dangerous;
11090                   }
11091               }
11092             else if (branch_type == ST_BRANCH_TO_THUMB
11093                      && globals->use_blx
11094                      && r_type == R_ARM_THM_CALL)
11095               {
11096                 /* Make sure this is a BL.  */
11097                 lower_insn |= 0x1800;
11098               }
11099           }
11100
11101         enum elf32_arm_stub_type stub_type = arm_stub_none;
11102         if (r_type == R_ARM_THM_CALL || r_type == R_ARM_THM_JUMP24)
11103           {
11104             /* Check if a stub has to be inserted because the destination
11105                is too far.  */
11106             struct elf32_arm_stub_hash_entry *stub_entry;
11107             struct elf32_arm_link_hash_entry *hash;
11108
11109             hash = (struct elf32_arm_link_hash_entry *) h;
11110
11111             stub_type = arm_type_of_stub (info, input_section, rel,
11112                                           st_type, &branch_type,
11113                                           hash, value, sym_sec,
11114                                           input_bfd, sym_name);
11115
11116             if (stub_type != arm_stub_none)
11117               {
11118                 /* The target is out of reach or we are changing modes, so
11119                    redirect the branch to the local stub for this
11120                    function.  */
11121                 stub_entry = elf32_arm_get_stub_entry (input_section,
11122                                                        sym_sec, h,
11123                                                        rel, globals,
11124                                                        stub_type);
11125                 if (stub_entry != NULL)
11126                   {
11127                     value = (stub_entry->stub_offset
11128                              + stub_entry->stub_sec->output_offset
11129                              + stub_entry->stub_sec->output_section->vma);
11130
11131                     if (plt_offset != (bfd_vma) -1)
11132                       *unresolved_reloc_p = FALSE;
11133                   }
11134
11135                 /* If this call becomes a call to Arm, force BLX.  */
11136                 if (globals->use_blx && (r_type == R_ARM_THM_CALL))
11137                   {
11138                     if ((stub_entry
11139                          && !arm_stub_is_thumb (stub_entry->stub_type))
11140                         || branch_type != ST_BRANCH_TO_THUMB)
11141                       lower_insn = (lower_insn & ~0x1000) | 0x0800;
11142                   }
11143               }
11144           }
11145
11146         /* Handle calls via the PLT.  */
11147         if (stub_type == arm_stub_none && plt_offset != (bfd_vma) -1)
11148           {
11149             value = (splt->output_section->vma
11150                      + splt->output_offset
11151                      + plt_offset);
11152
11153             if (globals->use_blx
11154                 && r_type == R_ARM_THM_CALL
11155                 && ! using_thumb_only (globals))
11156               {
11157                 /* If the Thumb BLX instruction is available, convert
11158                    the BL to a BLX instruction to call the ARM-mode
11159                    PLT entry.  */
11160                 lower_insn = (lower_insn & ~0x1000) | 0x0800;
11161                 branch_type = ST_BRANCH_TO_ARM;
11162               }
11163             else
11164               {
11165                 if (! using_thumb_only (globals))
11166                   /* Target the Thumb stub before the ARM PLT entry.  */
11167                   value -= PLT_THUMB_STUB_SIZE;
11168                 branch_type = ST_BRANCH_TO_THUMB;
11169               }
11170             *unresolved_reloc_p = FALSE;
11171           }
11172
11173         relocation = value + signed_addend;
11174
11175         relocation -= (input_section->output_section->vma
11176                        + input_section->output_offset
11177                        + rel->r_offset);
11178
11179         check = relocation >> howto->rightshift;
11180
11181         /* If this is a signed value, the rightshift just dropped
11182            leading 1 bits (assuming twos complement).  */
11183         if ((bfd_signed_vma) relocation >= 0)
11184           signed_check = check;
11185         else
11186           signed_check = check | ~((bfd_vma) -1 >> howto->rightshift);
11187
11188         /* Calculate the permissable maximum and minimum values for
11189            this relocation according to whether we're relocating for
11190            Thumb-2 or not.  */
11191         bitsize = howto->bitsize;
11192         if (!thumb2_bl)
11193           bitsize -= 2;
11194         reloc_signed_max = (1 << (bitsize - 1)) - 1;
11195         reloc_signed_min = ~reloc_signed_max;
11196
11197         /* Assumes two's complement.  */
11198         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
11199           overflow = TRUE;
11200
11201         if ((lower_insn & 0x5000) == 0x4000)
11202           /* For a BLX instruction, make sure that the relocation is rounded up
11203              to a word boundary.  This follows the semantics of the instruction
11204              which specifies that bit 1 of the target address will come from bit
11205              1 of the base address.  */
11206           relocation = (relocation + 2) & ~ 3;
11207
11208         /* Put RELOCATION back into the insn.  Assumes two's complement.
11209            We use the Thumb-2 encoding, which is safe even if dealing with
11210            a Thumb-1 instruction by virtue of our overflow check above.  */
11211         reloc_sign = (signed_check < 0) ? 1 : 0;
11212         upper_insn = (upper_insn & ~(bfd_vma) 0x7ff)
11213                      | ((relocation >> 12) & 0x3ff)
11214                      | (reloc_sign << 10);
11215         lower_insn = (lower_insn & ~(bfd_vma) 0x2fff)
11216                      | (((!((relocation >> 23) & 1)) ^ reloc_sign) << 13)
11217                      | (((!((relocation >> 22) & 1)) ^ reloc_sign) << 11)
11218                      | ((relocation >> 1) & 0x7ff);
11219
11220         /* Put the relocated value back in the object file:  */
11221         bfd_put_16 (input_bfd, upper_insn, hit_data);
11222         bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
11223
11224         return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
11225       }
11226       break;
11227
11228     case R_ARM_THM_JUMP19:
11229       /* Thumb32 conditional branch instruction.  */
11230       {
11231         bfd_vma relocation;
11232         bfd_boolean overflow = FALSE;
11233         bfd_vma upper_insn = bfd_get_16 (input_bfd, hit_data);
11234         bfd_vma lower_insn = bfd_get_16 (input_bfd, hit_data + 2);
11235         bfd_signed_vma reloc_signed_max = 0xffffe;
11236         bfd_signed_vma reloc_signed_min = -0x100000;
11237         bfd_signed_vma signed_check;
11238         enum elf32_arm_stub_type stub_type = arm_stub_none;
11239         struct elf32_arm_stub_hash_entry *stub_entry;
11240         struct elf32_arm_link_hash_entry *hash;
11241
11242         /* Need to refetch the addend, reconstruct the top three bits,
11243            and squish the two 11 bit pieces together.  */
11244         if (globals->use_rel)
11245           {
11246             bfd_vma S     = (upper_insn & 0x0400) >> 10;
11247             bfd_vma upper = (upper_insn & 0x003f);
11248             bfd_vma J1    = (lower_insn & 0x2000) >> 13;
11249             bfd_vma J2    = (lower_insn & 0x0800) >> 11;
11250             bfd_vma lower = (lower_insn & 0x07ff);
11251
11252             upper |= J1 << 6;
11253             upper |= J2 << 7;
11254             upper |= (!S) << 8;
11255             upper -= 0x0100; /* Sign extend.  */
11256
11257             addend = (upper << 12) | (lower << 1);
11258             signed_addend = addend;
11259           }
11260
11261         /* Handle calls via the PLT.  */
11262         if (plt_offset != (bfd_vma) -1)
11263           {
11264             value = (splt->output_section->vma
11265                      + splt->output_offset
11266                      + plt_offset);
11267             /* Target the Thumb stub before the ARM PLT entry.  */
11268             value -= PLT_THUMB_STUB_SIZE;
11269             *unresolved_reloc_p = FALSE;
11270           }
11271
11272         hash = (struct elf32_arm_link_hash_entry *)h;
11273
11274         stub_type = arm_type_of_stub (info, input_section, rel,
11275                                       st_type, &branch_type,
11276                                       hash, value, sym_sec,
11277                                       input_bfd, sym_name);
11278         if (stub_type != arm_stub_none)
11279           {
11280             stub_entry = elf32_arm_get_stub_entry (input_section,
11281                                                    sym_sec, h,
11282                                                    rel, globals,
11283                                                    stub_type);
11284             if (stub_entry != NULL)
11285               {
11286                 value = (stub_entry->stub_offset
11287                         + stub_entry->stub_sec->output_offset
11288                         + stub_entry->stub_sec->output_section->vma);
11289               }
11290           }
11291
11292         relocation = value + signed_addend;
11293         relocation -= (input_section->output_section->vma
11294                        + input_section->output_offset
11295                        + rel->r_offset);
11296         signed_check = (bfd_signed_vma) relocation;
11297
11298         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
11299           overflow = TRUE;
11300
11301         /* Put RELOCATION back into the insn.  */
11302         {
11303           bfd_vma S  = (relocation & 0x00100000) >> 20;
11304           bfd_vma J2 = (relocation & 0x00080000) >> 19;
11305           bfd_vma J1 = (relocation & 0x00040000) >> 18;
11306           bfd_vma hi = (relocation & 0x0003f000) >> 12;
11307           bfd_vma lo = (relocation & 0x00000ffe) >>  1;
11308
11309           upper_insn = (upper_insn & 0xfbc0) | (S << 10) | hi;
11310           lower_insn = (lower_insn & 0xd000) | (J1 << 13) | (J2 << 11) | lo;
11311         }
11312
11313         /* Put the relocated value back in the object file:  */
11314         bfd_put_16 (input_bfd, upper_insn, hit_data);
11315         bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
11316
11317         return (overflow ? bfd_reloc_overflow : bfd_reloc_ok);
11318       }
11319
11320     case R_ARM_THM_JUMP11:
11321     case R_ARM_THM_JUMP8:
11322     case R_ARM_THM_JUMP6:
11323       /* Thumb B (branch) instruction).  */
11324       {
11325         bfd_signed_vma relocation;
11326         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
11327         bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
11328         bfd_signed_vma signed_check;
11329
11330         /* CZB cannot jump backward.  */
11331         if (r_type == R_ARM_THM_JUMP6)
11332           reloc_signed_min = 0;
11333
11334         if (globals->use_rel)
11335           {
11336             /* Need to refetch addend.  */
11337             addend = bfd_get_16 (input_bfd, hit_data) & howto->src_mask;
11338             if (addend & ((howto->src_mask + 1) >> 1))
11339               {
11340                 signed_addend = -1;
11341                 signed_addend &= ~ howto->src_mask;
11342                 signed_addend |= addend;
11343               }
11344             else
11345               signed_addend = addend;
11346             /* The value in the insn has been right shifted.  We need to
11347                undo this, so that we can perform the address calculation
11348                in terms of bytes.  */
11349             signed_addend <<= howto->rightshift;
11350           }
11351         relocation = value + signed_addend;
11352
11353         relocation -= (input_section->output_section->vma
11354                        + input_section->output_offset
11355                        + rel->r_offset);
11356
11357         relocation >>= howto->rightshift;
11358         signed_check = relocation;
11359
11360         if (r_type == R_ARM_THM_JUMP6)
11361           relocation = ((relocation & 0x0020) << 4) | ((relocation & 0x001f) << 3);
11362         else
11363           relocation &= howto->dst_mask;
11364         relocation |= (bfd_get_16 (input_bfd, hit_data) & (~ howto->dst_mask));
11365
11366         bfd_put_16 (input_bfd, relocation, hit_data);
11367
11368         /* Assumes two's complement.  */
11369         if (signed_check > reloc_signed_max || signed_check < reloc_signed_min)
11370           return bfd_reloc_overflow;
11371
11372         return bfd_reloc_ok;
11373       }
11374
11375     case R_ARM_ALU_PCREL7_0:
11376     case R_ARM_ALU_PCREL15_8:
11377     case R_ARM_ALU_PCREL23_15:
11378       {
11379         bfd_vma insn;
11380         bfd_vma relocation;
11381
11382         insn = bfd_get_32 (input_bfd, hit_data);
11383         if (globals->use_rel)
11384           {
11385             /* Extract the addend.  */
11386             addend = (insn & 0xff) << ((insn & 0xf00) >> 7);
11387             signed_addend = addend;
11388           }
11389         relocation = value + signed_addend;
11390
11391         relocation -= (input_section->output_section->vma
11392                        + input_section->output_offset
11393                        + rel->r_offset);
11394         insn = (insn & ~0xfff)
11395                | ((howto->bitpos << 7) & 0xf00)
11396                | ((relocation >> howto->bitpos) & 0xff);
11397         bfd_put_32 (input_bfd, value, hit_data);
11398       }
11399       return bfd_reloc_ok;
11400
11401     case R_ARM_GNU_VTINHERIT:
11402     case R_ARM_GNU_VTENTRY:
11403       return bfd_reloc_ok;
11404
11405     case R_ARM_GOTOFF32:
11406       /* Relocation is relative to the start of the
11407          global offset table.  */
11408
11409       BFD_ASSERT (sgot != NULL);
11410       if (sgot == NULL)
11411         return bfd_reloc_notsupported;
11412
11413       /* If we are addressing a Thumb function, we need to adjust the
11414          address by one, so that attempts to call the function pointer will
11415          correctly interpret it as Thumb code.  */
11416       if (branch_type == ST_BRANCH_TO_THUMB)
11417         value += 1;
11418
11419       /* Note that sgot->output_offset is not involved in this
11420          calculation.  We always want the start of .got.  If we
11421          define _GLOBAL_OFFSET_TABLE in a different way, as is
11422          permitted by the ABI, we might have to change this
11423          calculation.  */
11424       value -= sgot->output_section->vma;
11425       return _bfd_final_link_relocate (howto, input_bfd, input_section,
11426                                        contents, rel->r_offset, value,
11427                                        rel->r_addend);
11428
11429     case R_ARM_GOTPC:
11430       /* Use global offset table as symbol value.  */
11431       BFD_ASSERT (sgot != NULL);
11432
11433       if (sgot == NULL)
11434         return bfd_reloc_notsupported;
11435
11436       *unresolved_reloc_p = FALSE;
11437       value = sgot->output_section->vma;
11438       return _bfd_final_link_relocate (howto, input_bfd, input_section,
11439                                        contents, rel->r_offset, value,
11440                                        rel->r_addend);
11441
11442     case R_ARM_GOT32:
11443     case R_ARM_GOT_PREL:
11444       /* Relocation is to the entry for this symbol in the
11445          global offset table.  */
11446       if (sgot == NULL)
11447         return bfd_reloc_notsupported;
11448
11449       if (dynreloc_st_type == STT_GNU_IFUNC
11450           && plt_offset != (bfd_vma) -1
11451           && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)))
11452         {
11453           /* We have a relocation against a locally-binding STT_GNU_IFUNC
11454              symbol, and the relocation resolves directly to the runtime
11455              target rather than to the .iplt entry.  This means that any
11456              .got entry would be the same value as the .igot.plt entry,
11457              so there's no point creating both.  */
11458           sgot = globals->root.igotplt;
11459           value = sgot->output_offset + gotplt_offset;
11460         }
11461       else if (h != NULL)
11462         {
11463           bfd_vma off;
11464
11465           off = h->got.offset;
11466           BFD_ASSERT (off != (bfd_vma) -1);
11467           if ((off & 1) != 0)
11468             {
11469               /* We have already processsed one GOT relocation against
11470                  this symbol.  */
11471               off &= ~1;
11472               if (globals->root.dynamic_sections_created
11473                   && !SYMBOL_REFERENCES_LOCAL (info, h))
11474                 *unresolved_reloc_p = FALSE;
11475             }
11476           else
11477             {
11478               Elf_Internal_Rela outrel;
11479               int isrofixup = 0;
11480
11481               if (((h->dynindx != -1) || globals->fdpic_p)
11482                   && !SYMBOL_REFERENCES_LOCAL (info, h))
11483                 {
11484                   /* If the symbol doesn't resolve locally in a static
11485                      object, we have an undefined reference.  If the
11486                      symbol doesn't resolve locally in a dynamic object,
11487                      it should be resolved by the dynamic linker.  */
11488                   if (globals->root.dynamic_sections_created)
11489                     {
11490                       outrel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_GLOB_DAT);
11491                       *unresolved_reloc_p = FALSE;
11492                     }
11493                   else
11494                     outrel.r_info = 0;
11495                   outrel.r_addend = 0;
11496                 }
11497               else
11498                 {
11499                   if (dynreloc_st_type == STT_GNU_IFUNC)
11500                     outrel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
11501                   else if (bfd_link_pic (info)
11502                            && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
11503                                || h->root.type != bfd_link_hash_undefweak))
11504                     outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
11505                   else if (globals->fdpic_p)
11506                     isrofixup = 1;
11507                   else
11508                     outrel.r_info = 0;
11509                   outrel.r_addend = dynreloc_value;
11510                 }
11511
11512               /* The GOT entry is initialized to zero by default.
11513                  See if we should install a different value.  */
11514               if (outrel.r_addend != 0
11515                   && (outrel.r_info == 0 || globals->use_rel || isrofixup))
11516                 {
11517                   bfd_put_32 (output_bfd, outrel.r_addend,
11518                               sgot->contents + off);
11519                   outrel.r_addend = 0;
11520                 }
11521
11522               if (outrel.r_info != 0 && !isrofixup)
11523                 {
11524                   outrel.r_offset = (sgot->output_section->vma
11525                                      + sgot->output_offset
11526                                      + off);
11527                   elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
11528                 }
11529               else if (isrofixup)
11530                 {
11531                   arm_elf_add_rofixup(output_bfd,
11532                                       elf32_arm_hash_table(info)->srofixup,
11533                                       sgot->output_section->vma
11534                                       + sgot->output_offset + off);
11535                 }
11536               h->got.offset |= 1;
11537             }
11538           value = sgot->output_offset + off;
11539         }
11540       else
11541         {
11542           bfd_vma off;
11543
11544           BFD_ASSERT (local_got_offsets != NULL
11545                       && local_got_offsets[r_symndx] != (bfd_vma) -1);
11546
11547           off = local_got_offsets[r_symndx];
11548
11549           /* The offset must always be a multiple of 4.  We use the
11550              least significant bit to record whether we have already
11551              generated the necessary reloc.  */
11552           if ((off & 1) != 0)
11553             off &= ~1;
11554           else
11555             {
11556               if (globals->use_rel)
11557                 bfd_put_32 (output_bfd, dynreloc_value, sgot->contents + off);
11558
11559               if (bfd_link_pic (info) || dynreloc_st_type == STT_GNU_IFUNC)
11560                 {
11561                   Elf_Internal_Rela outrel;
11562
11563                   outrel.r_addend = addend + dynreloc_value;
11564                   outrel.r_offset = (sgot->output_section->vma
11565                                      + sgot->output_offset
11566                                      + off);
11567                   if (dynreloc_st_type == STT_GNU_IFUNC)
11568                     outrel.r_info = ELF32_R_INFO (0, R_ARM_IRELATIVE);
11569                   else
11570                     outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
11571                   elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
11572                 }
11573               else if (globals->fdpic_p)
11574                 {
11575                   /* For FDPIC executables, we use rofixup to fix
11576                      address at runtime.  */
11577                   arm_elf_add_rofixup(output_bfd, globals->srofixup,
11578                                       sgot->output_section->vma + sgot->output_offset
11579                                       + off);
11580                 }
11581
11582               local_got_offsets[r_symndx] |= 1;
11583             }
11584
11585           value = sgot->output_offset + off;
11586         }
11587       if (r_type != R_ARM_GOT32)
11588         value += sgot->output_section->vma;
11589
11590       return _bfd_final_link_relocate (howto, input_bfd, input_section,
11591                                        contents, rel->r_offset, value,
11592                                        rel->r_addend);
11593
11594     case R_ARM_TLS_LDO32:
11595       value = value - dtpoff_base (info);
11596
11597       return _bfd_final_link_relocate (howto, input_bfd, input_section,
11598                                        contents, rel->r_offset, value,
11599                                        rel->r_addend);
11600
11601     case R_ARM_TLS_LDM32:
11602     case R_ARM_TLS_LDM32_FDPIC:
11603       {
11604         bfd_vma off;
11605
11606         if (sgot == NULL)
11607           abort ();
11608
11609         off = globals->tls_ldm_got.offset;
11610
11611         if ((off & 1) != 0)
11612           off &= ~1;
11613         else
11614           {
11615             /* If we don't know the module number, create a relocation
11616                for it.  */
11617             if (bfd_link_pic (info))
11618               {
11619                 Elf_Internal_Rela outrel;
11620
11621                 if (srelgot == NULL)
11622                   abort ();
11623
11624                 outrel.r_addend = 0;
11625                 outrel.r_offset = (sgot->output_section->vma
11626                                    + sgot->output_offset + off);
11627                 outrel.r_info = ELF32_R_INFO (0, R_ARM_TLS_DTPMOD32);
11628
11629                 if (globals->use_rel)
11630                   bfd_put_32 (output_bfd, outrel.r_addend,
11631                               sgot->contents + off);
11632
11633                 elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
11634               }
11635             else
11636               bfd_put_32 (output_bfd, 1, sgot->contents + off);
11637
11638             globals->tls_ldm_got.offset |= 1;
11639           }
11640
11641         if (r_type == R_ARM_TLS_LDM32_FDPIC)
11642           {
11643             bfd_put_32(output_bfd,
11644                        globals->root.sgot->output_offset + off,
11645                        contents + rel->r_offset);
11646
11647             return bfd_reloc_ok;
11648           }
11649         else
11650           {
11651             value = sgot->output_section->vma + sgot->output_offset + off
11652               - (input_section->output_section->vma
11653                  + input_section->output_offset + rel->r_offset);
11654
11655             return _bfd_final_link_relocate (howto, input_bfd, input_section,
11656                                              contents, rel->r_offset, value,
11657                                              rel->r_addend);
11658           }
11659       }
11660
11661     case R_ARM_TLS_CALL:
11662     case R_ARM_THM_TLS_CALL:
11663     case R_ARM_TLS_GD32:
11664     case R_ARM_TLS_GD32_FDPIC:
11665     case R_ARM_TLS_IE32:
11666     case R_ARM_TLS_IE32_FDPIC:
11667     case R_ARM_TLS_GOTDESC:
11668     case R_ARM_TLS_DESCSEQ:
11669     case R_ARM_THM_TLS_DESCSEQ:
11670       {
11671         bfd_vma off, offplt;
11672         int indx = 0;
11673         char tls_type;
11674
11675         BFD_ASSERT (sgot != NULL);
11676
11677         if (h != NULL)
11678           {
11679             bfd_boolean dyn;
11680             dyn = globals->root.dynamic_sections_created;
11681             if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
11682                                                  bfd_link_pic (info),
11683                                                  h)
11684                 && (!bfd_link_pic (info)
11685                     || !SYMBOL_REFERENCES_LOCAL (info, h)))
11686               {
11687                 *unresolved_reloc_p = FALSE;
11688                 indx = h->dynindx;
11689               }
11690             off = h->got.offset;
11691             offplt = elf32_arm_hash_entry (h)->tlsdesc_got;
11692             tls_type = ((struct elf32_arm_link_hash_entry *) h)->tls_type;
11693           }
11694         else
11695           {
11696             BFD_ASSERT (local_got_offsets != NULL);
11697             off = local_got_offsets[r_symndx];
11698             offplt = local_tlsdesc_gotents[r_symndx];
11699             tls_type = elf32_arm_local_got_tls_type (input_bfd)[r_symndx];
11700           }
11701
11702         /* Linker relaxations happens from one of the
11703            R_ARM_{GOTDESC,CALL,DESCSEQ} relocations to IE or LE.  */
11704         if (ELF32_R_TYPE(rel->r_info) != r_type)
11705           tls_type = GOT_TLS_IE;
11706
11707         BFD_ASSERT (tls_type != GOT_UNKNOWN);
11708
11709         if ((off & 1) != 0)
11710           off &= ~1;
11711         else
11712           {
11713             bfd_boolean need_relocs = FALSE;
11714             Elf_Internal_Rela outrel;
11715             int cur_off = off;
11716
11717             /* The GOT entries have not been initialized yet.  Do it
11718                now, and emit any relocations.  If both an IE GOT and a
11719                GD GOT are necessary, we emit the GD first.  */
11720
11721             if ((bfd_link_pic (info) || indx != 0)
11722                 && (h == NULL
11723                     || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
11724                         && !resolved_to_zero)
11725                     || h->root.type != bfd_link_hash_undefweak))
11726               {
11727                 need_relocs = TRUE;
11728                 BFD_ASSERT (srelgot != NULL);
11729               }
11730
11731             if (tls_type & GOT_TLS_GDESC)
11732               {
11733                 bfd_byte *loc;
11734
11735                 /* We should have relaxed, unless this is an undefined
11736                    weak symbol.  */
11737                 BFD_ASSERT ((h && (h->root.type == bfd_link_hash_undefweak))
11738                             || bfd_link_pic (info));
11739                 BFD_ASSERT (globals->sgotplt_jump_table_size + offplt + 8
11740                             <= globals->root.sgotplt->size);
11741
11742                 outrel.r_addend = 0;
11743                 outrel.r_offset = (globals->root.sgotplt->output_section->vma
11744                                    + globals->root.sgotplt->output_offset
11745                                    + offplt
11746                                    + globals->sgotplt_jump_table_size);
11747
11748                 outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DESC);
11749                 sreloc = globals->root.srelplt;
11750                 loc = sreloc->contents;
11751                 loc += globals->next_tls_desc_index++ * RELOC_SIZE (globals);
11752                 BFD_ASSERT (loc + RELOC_SIZE (globals)
11753                            <= sreloc->contents + sreloc->size);
11754
11755                 SWAP_RELOC_OUT (globals) (output_bfd, &outrel, loc);
11756
11757                 /* For globals, the first word in the relocation gets
11758                    the relocation index and the top bit set, or zero,
11759                    if we're binding now.  For locals, it gets the
11760                    symbol's offset in the tls section.  */
11761                 bfd_put_32 (output_bfd,
11762                             !h ? value - elf_hash_table (info)->tls_sec->vma
11763                             : info->flags & DF_BIND_NOW ? 0
11764                             : 0x80000000 | ELF32_R_SYM (outrel.r_info),
11765                             globals->root.sgotplt->contents + offplt
11766                             + globals->sgotplt_jump_table_size);
11767
11768                 /* Second word in the relocation is always zero.  */
11769                 bfd_put_32 (output_bfd, 0,
11770                             globals->root.sgotplt->contents + offplt
11771                             + globals->sgotplt_jump_table_size + 4);
11772               }
11773             if (tls_type & GOT_TLS_GD)
11774               {
11775                 if (need_relocs)
11776                   {
11777                     outrel.r_addend = 0;
11778                     outrel.r_offset = (sgot->output_section->vma
11779                                        + sgot->output_offset
11780                                        + cur_off);
11781                     outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_DTPMOD32);
11782
11783                     if (globals->use_rel)
11784                       bfd_put_32 (output_bfd, outrel.r_addend,
11785                                   sgot->contents + cur_off);
11786
11787                     elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
11788
11789                     if (indx == 0)
11790                       bfd_put_32 (output_bfd, value - dtpoff_base (info),
11791                                   sgot->contents + cur_off + 4);
11792                     else
11793                       {
11794                         outrel.r_addend = 0;
11795                         outrel.r_info = ELF32_R_INFO (indx,
11796                                                       R_ARM_TLS_DTPOFF32);
11797                         outrel.r_offset += 4;
11798
11799                         if (globals->use_rel)
11800                           bfd_put_32 (output_bfd, outrel.r_addend,
11801                                       sgot->contents + cur_off + 4);
11802
11803                         elf32_arm_add_dynreloc (output_bfd, info,
11804                                                 srelgot, &outrel);
11805                       }
11806                   }
11807                 else
11808                   {
11809                     /* If we are not emitting relocations for a
11810                        general dynamic reference, then we must be in a
11811                        static link or an executable link with the
11812                        symbol binding locally.  Mark it as belonging
11813                        to module 1, the executable.  */
11814                     bfd_put_32 (output_bfd, 1,
11815                                 sgot->contents + cur_off);
11816                     bfd_put_32 (output_bfd, value - dtpoff_base (info),
11817                                 sgot->contents + cur_off + 4);
11818                   }
11819
11820                 cur_off += 8;
11821               }
11822
11823             if (tls_type & GOT_TLS_IE)
11824               {
11825                 if (need_relocs)
11826                   {
11827                     if (indx == 0)
11828                       outrel.r_addend = value - dtpoff_base (info);
11829                     else
11830                       outrel.r_addend = 0;
11831                     outrel.r_offset = (sgot->output_section->vma
11832                                        + sgot->output_offset
11833                                        + cur_off);
11834                     outrel.r_info = ELF32_R_INFO (indx, R_ARM_TLS_TPOFF32);
11835
11836                     if (globals->use_rel)
11837                       bfd_put_32 (output_bfd, outrel.r_addend,
11838                                   sgot->contents + cur_off);
11839
11840                     elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
11841                   }
11842                 else
11843                   bfd_put_32 (output_bfd, tpoff (info, value),
11844                               sgot->contents + cur_off);
11845                 cur_off += 4;
11846               }
11847
11848             if (h != NULL)
11849               h->got.offset |= 1;
11850             else
11851               local_got_offsets[r_symndx] |= 1;
11852           }
11853
11854         if ((tls_type & GOT_TLS_GD) && r_type != R_ARM_TLS_GD32 && r_type != R_ARM_TLS_GD32_FDPIC)
11855           off += 8;
11856         else if (tls_type & GOT_TLS_GDESC)
11857           off = offplt;
11858
11859         if (ELF32_R_TYPE(rel->r_info) == R_ARM_TLS_CALL
11860             || ELF32_R_TYPE(rel->r_info) == R_ARM_THM_TLS_CALL)
11861           {
11862             bfd_signed_vma offset;
11863             /* TLS stubs are arm mode.  The original symbol is a
11864                data object, so branch_type is bogus.  */
11865             branch_type = ST_BRANCH_TO_ARM;
11866             enum elf32_arm_stub_type stub_type
11867               = arm_type_of_stub (info, input_section, rel,
11868                                   st_type, &branch_type,
11869                                   (struct elf32_arm_link_hash_entry *)h,
11870                                   globals->tls_trampoline, globals->root.splt,
11871                                   input_bfd, sym_name);
11872
11873             if (stub_type != arm_stub_none)
11874               {
11875                 struct elf32_arm_stub_hash_entry *stub_entry
11876                   = elf32_arm_get_stub_entry
11877                   (input_section, globals->root.splt, 0, rel,
11878                    globals, stub_type);
11879                 offset = (stub_entry->stub_offset
11880                           + stub_entry->stub_sec->output_offset
11881                           + stub_entry->stub_sec->output_section->vma);
11882               }
11883             else
11884               offset = (globals->root.splt->output_section->vma
11885                         + globals->root.splt->output_offset
11886                         + globals->tls_trampoline);
11887
11888             if (ELF32_R_TYPE(rel->r_info) == R_ARM_TLS_CALL)
11889               {
11890                 unsigned long inst;
11891
11892                 offset -= (input_section->output_section->vma
11893                            + input_section->output_offset
11894                            + rel->r_offset + 8);
11895
11896                 inst = offset >> 2;
11897                 inst &= 0x00ffffff;
11898                 value = inst | (globals->use_blx ? 0xfa000000 : 0xeb000000);
11899               }
11900             else
11901               {
11902                 /* Thumb blx encodes the offset in a complicated
11903                    fashion.  */
11904                 unsigned upper_insn, lower_insn;
11905                 unsigned neg;
11906
11907                 offset -= (input_section->output_section->vma
11908                            + input_section->output_offset
11909                            + rel->r_offset + 4);
11910
11911                 if (stub_type != arm_stub_none
11912                     && arm_stub_is_thumb (stub_type))
11913                   {
11914                     lower_insn = 0xd000;
11915                   }
11916                 else
11917                   {
11918                     lower_insn = 0xc000;
11919                     /* Round up the offset to a word boundary.  */
11920                     offset = (offset + 2) & ~2;
11921                   }
11922
11923                 neg = offset < 0;
11924                 upper_insn = (0xf000
11925                               | ((offset >> 12) & 0x3ff)
11926                               | (neg << 10));
11927                 lower_insn |= (((!((offset >> 23) & 1)) ^ neg) << 13)
11928                               | (((!((offset >> 22) & 1)) ^ neg) << 11)
11929                               | ((offset >> 1) & 0x7ff);
11930                 bfd_put_16 (input_bfd, upper_insn, hit_data);
11931                 bfd_put_16 (input_bfd, lower_insn, hit_data + 2);
11932                 return bfd_reloc_ok;
11933               }
11934           }
11935         /* These relocations needs special care, as besides the fact
11936            they point somewhere in .gotplt, the addend must be
11937            adjusted accordingly depending on the type of instruction
11938            we refer to.  */
11939         else if ((r_type == R_ARM_TLS_GOTDESC) && (tls_type & GOT_TLS_GDESC))
11940           {
11941             unsigned long data, insn;
11942             unsigned thumb;
11943
11944             data = bfd_get_32 (input_bfd, hit_data);
11945             thumb = data & 1;
11946             data &= ~1u;
11947
11948             if (thumb)
11949               {
11950                 insn = bfd_get_16 (input_bfd, contents + rel->r_offset - data);
11951                 if ((insn & 0xf000) == 0xf000 || (insn & 0xf800) == 0xe800)
11952                   insn = (insn << 16)
11953                     | bfd_get_16 (input_bfd,
11954                                   contents + rel->r_offset - data + 2);
11955                 if ((insn & 0xf800c000) == 0xf000c000)
11956                   /* bl/blx */
11957                   value = -6;
11958                 else if ((insn & 0xffffff00) == 0x4400)
11959                   /* add */
11960                   value = -5;
11961                 else
11962                   {
11963                     _bfd_error_handler
11964                       /* xgettext:c-format */
11965                       (_("%pB(%pA+%#" PRIx64 "): "
11966                          "unexpected %s instruction '%#lx' "
11967                          "referenced by TLS_GOTDESC"),
11968                        input_bfd, input_section, (uint64_t) rel->r_offset,
11969                        "Thumb", insn);
11970                     return bfd_reloc_notsupported;
11971                   }
11972               }
11973             else
11974               {
11975                 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - data);
11976
11977                 switch (insn >> 24)
11978                   {
11979                   case 0xeb:  /* bl */
11980                   case 0xfa:  /* blx */
11981                     value = -4;
11982                     break;
11983
11984                   case 0xe0:    /* add */
11985                     value = -8;
11986                     break;
11987
11988                   default:
11989                     _bfd_error_handler
11990                       /* xgettext:c-format */
11991                       (_("%pB(%pA+%#" PRIx64 "): "
11992                          "unexpected %s instruction '%#lx' "
11993                          "referenced by TLS_GOTDESC"),
11994                        input_bfd, input_section, (uint64_t) rel->r_offset,
11995                        "ARM", insn);
11996                     return bfd_reloc_notsupported;
11997                   }
11998               }
11999
12000             value += ((globals->root.sgotplt->output_section->vma
12001                        + globals->root.sgotplt->output_offset + off)
12002                       - (input_section->output_section->vma
12003                          + input_section->output_offset
12004                          + rel->r_offset)
12005                       + globals->sgotplt_jump_table_size);
12006           }
12007         else
12008           value = ((globals->root.sgot->output_section->vma
12009                     + globals->root.sgot->output_offset + off)
12010                    - (input_section->output_section->vma
12011                       + input_section->output_offset + rel->r_offset));
12012
12013         if (globals->fdpic_p && (r_type == R_ARM_TLS_GD32_FDPIC ||
12014                                  r_type == R_ARM_TLS_IE32_FDPIC))
12015           {
12016             /* For FDPIC relocations, resolve to the offset of the GOT
12017                entry from the start of GOT.  */
12018             bfd_put_32(output_bfd,
12019                        globals->root.sgot->output_offset + off,
12020                        contents + rel->r_offset);
12021
12022             return bfd_reloc_ok;
12023           }
12024         else
12025           {
12026             return _bfd_final_link_relocate (howto, input_bfd, input_section,
12027                                              contents, rel->r_offset, value,
12028                                              rel->r_addend);
12029           }
12030       }
12031
12032     case R_ARM_TLS_LE32:
12033       if (bfd_link_dll (info))
12034         {
12035           _bfd_error_handler
12036             /* xgettext:c-format */
12037             (_("%pB(%pA+%#" PRIx64 "): %s relocation not permitted "
12038                "in shared object"),
12039              input_bfd, input_section, (uint64_t) rel->r_offset, howto->name);
12040           return bfd_reloc_notsupported;
12041         }
12042       else
12043         value = tpoff (info, value);
12044
12045       return _bfd_final_link_relocate (howto, input_bfd, input_section,
12046                                        contents, rel->r_offset, value,
12047                                        rel->r_addend);
12048
12049     case R_ARM_V4BX:
12050       if (globals->fix_v4bx)
12051         {
12052           bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12053
12054           /* Ensure that we have a BX instruction.  */
12055           BFD_ASSERT ((insn & 0x0ffffff0) == 0x012fff10);
12056
12057           if (globals->fix_v4bx == 2 && (insn & 0xf) != 0xf)
12058             {
12059               /* Branch to veneer.  */
12060               bfd_vma glue_addr;
12061               glue_addr = elf32_arm_bx_glue (info, insn & 0xf);
12062               glue_addr -= input_section->output_section->vma
12063                            + input_section->output_offset
12064                            + rel->r_offset + 8;
12065               insn = (insn & 0xf0000000) | 0x0a000000
12066                      | ((glue_addr >> 2) & 0x00ffffff);
12067             }
12068           else
12069             {
12070               /* Preserve Rm (lowest four bits) and the condition code
12071                  (highest four bits). Other bits encode MOV PC,Rm.  */
12072               insn = (insn & 0xf000000f) | 0x01a0f000;
12073             }
12074
12075           bfd_put_32 (input_bfd, insn, hit_data);
12076         }
12077       return bfd_reloc_ok;
12078
12079     case R_ARM_MOVW_ABS_NC:
12080     case R_ARM_MOVT_ABS:
12081     case R_ARM_MOVW_PREL_NC:
12082     case R_ARM_MOVT_PREL:
12083     /* Until we properly support segment-base-relative addressing then
12084        we assume the segment base to be zero, as for the group relocations.
12085        Thus R_ARM_MOVW_BREL_NC has the same semantics as R_ARM_MOVW_ABS_NC
12086        and R_ARM_MOVT_BREL has the same semantics as R_ARM_MOVT_ABS.  */
12087     case R_ARM_MOVW_BREL_NC:
12088     case R_ARM_MOVW_BREL:
12089     case R_ARM_MOVT_BREL:
12090       {
12091         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12092
12093         if (globals->use_rel)
12094           {
12095             addend = ((insn >> 4) & 0xf000) | (insn & 0xfff);
12096             signed_addend = (addend ^ 0x8000) - 0x8000;
12097           }
12098
12099         value += signed_addend;
12100
12101         if (r_type == R_ARM_MOVW_PREL_NC || r_type == R_ARM_MOVT_PREL)
12102           value -= (input_section->output_section->vma
12103                     + input_section->output_offset + rel->r_offset);
12104
12105         if (r_type == R_ARM_MOVW_BREL && value >= 0x10000)
12106           return bfd_reloc_overflow;
12107
12108         if (branch_type == ST_BRANCH_TO_THUMB)
12109           value |= 1;
12110
12111         if (r_type == R_ARM_MOVT_ABS || r_type == R_ARM_MOVT_PREL
12112             || r_type == R_ARM_MOVT_BREL)
12113           value >>= 16;
12114
12115         insn &= 0xfff0f000;
12116         insn |= value & 0xfff;
12117         insn |= (value & 0xf000) << 4;
12118         bfd_put_32 (input_bfd, insn, hit_data);
12119       }
12120       return bfd_reloc_ok;
12121
12122     case R_ARM_THM_MOVW_ABS_NC:
12123     case R_ARM_THM_MOVT_ABS:
12124     case R_ARM_THM_MOVW_PREL_NC:
12125     case R_ARM_THM_MOVT_PREL:
12126     /* Until we properly support segment-base-relative addressing then
12127        we assume the segment base to be zero, as for the above relocations.
12128        Thus R_ARM_THM_MOVW_BREL_NC has the same semantics as
12129        R_ARM_THM_MOVW_ABS_NC and R_ARM_THM_MOVT_BREL has the same semantics
12130        as R_ARM_THM_MOVT_ABS.  */
12131     case R_ARM_THM_MOVW_BREL_NC:
12132     case R_ARM_THM_MOVW_BREL:
12133     case R_ARM_THM_MOVT_BREL:
12134       {
12135         bfd_vma insn;
12136
12137         insn = bfd_get_16 (input_bfd, hit_data) << 16;
12138         insn |= bfd_get_16 (input_bfd, hit_data + 2);
12139
12140         if (globals->use_rel)
12141           {
12142             addend = ((insn >> 4)  & 0xf000)
12143                    | ((insn >> 15) & 0x0800)
12144                    | ((insn >> 4)  & 0x0700)
12145                    | (insn         & 0x00ff);
12146             signed_addend = (addend ^ 0x8000) - 0x8000;
12147           }
12148
12149         value += signed_addend;
12150
12151         if (r_type == R_ARM_THM_MOVW_PREL_NC || r_type == R_ARM_THM_MOVT_PREL)
12152           value -= (input_section->output_section->vma
12153                     + input_section->output_offset + rel->r_offset);
12154
12155         if (r_type == R_ARM_THM_MOVW_BREL && value >= 0x10000)
12156           return bfd_reloc_overflow;
12157
12158         if (branch_type == ST_BRANCH_TO_THUMB)
12159           value |= 1;
12160
12161         if (r_type == R_ARM_THM_MOVT_ABS || r_type == R_ARM_THM_MOVT_PREL
12162             || r_type == R_ARM_THM_MOVT_BREL)
12163           value >>= 16;
12164
12165         insn &= 0xfbf08f00;
12166         insn |= (value & 0xf000) << 4;
12167         insn |= (value & 0x0800) << 15;
12168         insn |= (value & 0x0700) << 4;
12169         insn |= (value & 0x00ff);
12170
12171         bfd_put_16 (input_bfd, insn >> 16, hit_data);
12172         bfd_put_16 (input_bfd, insn & 0xffff, hit_data + 2);
12173       }
12174       return bfd_reloc_ok;
12175
12176     case R_ARM_ALU_PC_G0_NC:
12177     case R_ARM_ALU_PC_G1_NC:
12178     case R_ARM_ALU_PC_G0:
12179     case R_ARM_ALU_PC_G1:
12180     case R_ARM_ALU_PC_G2:
12181     case R_ARM_ALU_SB_G0_NC:
12182     case R_ARM_ALU_SB_G1_NC:
12183     case R_ARM_ALU_SB_G0:
12184     case R_ARM_ALU_SB_G1:
12185     case R_ARM_ALU_SB_G2:
12186       {
12187         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12188         bfd_vma pc = input_section->output_section->vma
12189                      + input_section->output_offset + rel->r_offset;
12190         /* sb is the origin of the *segment* containing the symbol.  */
12191         bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
12192         bfd_vma residual;
12193         bfd_vma g_n;
12194         bfd_signed_vma signed_value;
12195         int group = 0;
12196
12197         /* Determine which group of bits to select.  */
12198         switch (r_type)
12199           {
12200           case R_ARM_ALU_PC_G0_NC:
12201           case R_ARM_ALU_PC_G0:
12202           case R_ARM_ALU_SB_G0_NC:
12203           case R_ARM_ALU_SB_G0:
12204             group = 0;
12205             break;
12206
12207           case R_ARM_ALU_PC_G1_NC:
12208           case R_ARM_ALU_PC_G1:
12209           case R_ARM_ALU_SB_G1_NC:
12210           case R_ARM_ALU_SB_G1:
12211             group = 1;
12212             break;
12213
12214           case R_ARM_ALU_PC_G2:
12215           case R_ARM_ALU_SB_G2:
12216             group = 2;
12217             break;
12218
12219           default:
12220             abort ();
12221           }
12222
12223         /* If REL, extract the addend from the insn.  If RELA, it will
12224            have already been fetched for us.  */
12225         if (globals->use_rel)
12226           {
12227             int negative;
12228             bfd_vma constant = insn & 0xff;
12229             bfd_vma rotation = (insn & 0xf00) >> 8;
12230
12231             if (rotation == 0)
12232               signed_addend = constant;
12233             else
12234               {
12235                 /* Compensate for the fact that in the instruction, the
12236                    rotation is stored in multiples of 2 bits.  */
12237                 rotation *= 2;
12238
12239                 /* Rotate "constant" right by "rotation" bits.  */
12240                 signed_addend = (constant >> rotation) |
12241                                 (constant << (8 * sizeof (bfd_vma) - rotation));
12242               }
12243
12244             /* Determine if the instruction is an ADD or a SUB.
12245                (For REL, this determines the sign of the addend.)  */
12246             negative = identify_add_or_sub (insn);
12247             if (negative == 0)
12248               {
12249                 _bfd_error_handler
12250                   /* xgettext:c-format */
12251                   (_("%pB(%pA+%#" PRIx64 "): only ADD or SUB instructions "
12252                      "are allowed for ALU group relocations"),
12253                   input_bfd, input_section, (uint64_t) rel->r_offset);
12254                 return bfd_reloc_overflow;
12255               }
12256
12257             signed_addend *= negative;
12258           }
12259
12260         /* Compute the value (X) to go in the place.  */
12261         if (r_type == R_ARM_ALU_PC_G0_NC
12262             || r_type == R_ARM_ALU_PC_G1_NC
12263             || r_type == R_ARM_ALU_PC_G0
12264             || r_type == R_ARM_ALU_PC_G1
12265             || r_type == R_ARM_ALU_PC_G2)
12266           /* PC relative.  */
12267           signed_value = value - pc + signed_addend;
12268         else
12269           /* Section base relative.  */
12270           signed_value = value - sb + signed_addend;
12271
12272         /* If the target symbol is a Thumb function, then set the
12273            Thumb bit in the address.  */
12274         if (branch_type == ST_BRANCH_TO_THUMB)
12275           signed_value |= 1;
12276
12277         /* Calculate the value of the relevant G_n, in encoded
12278            constant-with-rotation format.  */
12279         g_n = calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
12280                                           group, &residual);
12281
12282         /* Check for overflow if required.  */
12283         if ((r_type == R_ARM_ALU_PC_G0
12284              || r_type == R_ARM_ALU_PC_G1
12285              || r_type == R_ARM_ALU_PC_G2
12286              || r_type == R_ARM_ALU_SB_G0
12287              || r_type == R_ARM_ALU_SB_G1
12288              || r_type == R_ARM_ALU_SB_G2) && residual != 0)
12289           {
12290             _bfd_error_handler
12291               /* xgettext:c-format */
12292               (_("%pB(%pA+%#" PRIx64 "): overflow whilst "
12293                  "splitting %#" PRIx64 " for group relocation %s"),
12294                input_bfd, input_section, (uint64_t) rel->r_offset,
12295                (uint64_t) (signed_value < 0 ? -signed_value : signed_value),
12296                howto->name);
12297             return bfd_reloc_overflow;
12298           }
12299
12300         /* Mask out the value and the ADD/SUB part of the opcode; take care
12301            not to destroy the S bit.  */
12302         insn &= 0xff1ff000;
12303
12304         /* Set the opcode according to whether the value to go in the
12305            place is negative.  */
12306         if (signed_value < 0)
12307           insn |= 1 << 22;
12308         else
12309           insn |= 1 << 23;
12310
12311         /* Encode the offset.  */
12312         insn |= g_n;
12313
12314         bfd_put_32 (input_bfd, insn, hit_data);
12315       }
12316       return bfd_reloc_ok;
12317
12318     case R_ARM_LDR_PC_G0:
12319     case R_ARM_LDR_PC_G1:
12320     case R_ARM_LDR_PC_G2:
12321     case R_ARM_LDR_SB_G0:
12322     case R_ARM_LDR_SB_G1:
12323     case R_ARM_LDR_SB_G2:
12324       {
12325         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12326         bfd_vma pc = input_section->output_section->vma
12327                      + input_section->output_offset + rel->r_offset;
12328         /* sb is the origin of the *segment* containing the symbol.  */
12329         bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
12330         bfd_vma residual;
12331         bfd_signed_vma signed_value;
12332         int group = 0;
12333
12334         /* Determine which groups of bits to calculate.  */
12335         switch (r_type)
12336           {
12337           case R_ARM_LDR_PC_G0:
12338           case R_ARM_LDR_SB_G0:
12339             group = 0;
12340             break;
12341
12342           case R_ARM_LDR_PC_G1:
12343           case R_ARM_LDR_SB_G1:
12344             group = 1;
12345             break;
12346
12347           case R_ARM_LDR_PC_G2:
12348           case R_ARM_LDR_SB_G2:
12349             group = 2;
12350             break;
12351
12352           default:
12353             abort ();
12354           }
12355
12356         /* If REL, extract the addend from the insn.  If RELA, it will
12357            have already been fetched for us.  */
12358         if (globals->use_rel)
12359           {
12360             int negative = (insn & (1 << 23)) ? 1 : -1;
12361             signed_addend = negative * (insn & 0xfff);
12362           }
12363
12364         /* Compute the value (X) to go in the place.  */
12365         if (r_type == R_ARM_LDR_PC_G0
12366             || r_type == R_ARM_LDR_PC_G1
12367             || r_type == R_ARM_LDR_PC_G2)
12368           /* PC relative.  */
12369           signed_value = value - pc + signed_addend;
12370         else
12371           /* Section base relative.  */
12372           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         calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
12377                                     group - 1, &residual);
12378
12379         /* Check for overflow.  */
12380         if (residual >= 0x1000)
12381           {
12382             _bfd_error_handler
12383               /* xgettext:c-format */
12384               (_("%pB(%pA+%#" PRIx64 "): overflow whilst "
12385                  "splitting %#" PRIx64 " for group relocation %s"),
12386                input_bfd, input_section, (uint64_t) rel->r_offset,
12387                (uint64_t) (signed_value < 0 ? -signed_value : signed_value),
12388                howto->name);
12389             return bfd_reloc_overflow;
12390           }
12391
12392         /* Mask out the value and U bit.  */
12393         insn &= 0xff7ff000;
12394
12395         /* Set the U bit if the value to go in the place is non-negative.  */
12396         if (signed_value >= 0)
12397           insn |= 1 << 23;
12398
12399         /* Encode the offset.  */
12400         insn |= residual;
12401
12402         bfd_put_32 (input_bfd, insn, hit_data);
12403       }
12404       return bfd_reloc_ok;
12405
12406     case R_ARM_LDRS_PC_G0:
12407     case R_ARM_LDRS_PC_G1:
12408     case R_ARM_LDRS_PC_G2:
12409     case R_ARM_LDRS_SB_G0:
12410     case R_ARM_LDRS_SB_G1:
12411     case R_ARM_LDRS_SB_G2:
12412       {
12413         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12414         bfd_vma pc = input_section->output_section->vma
12415                      + input_section->output_offset + rel->r_offset;
12416         /* sb is the origin of the *segment* containing the symbol.  */
12417         bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
12418         bfd_vma residual;
12419         bfd_signed_vma signed_value;
12420         int group = 0;
12421
12422         /* Determine which groups of bits to calculate.  */
12423         switch (r_type)
12424           {
12425           case R_ARM_LDRS_PC_G0:
12426           case R_ARM_LDRS_SB_G0:
12427             group = 0;
12428             break;
12429
12430           case R_ARM_LDRS_PC_G1:
12431           case R_ARM_LDRS_SB_G1:
12432             group = 1;
12433             break;
12434
12435           case R_ARM_LDRS_PC_G2:
12436           case R_ARM_LDRS_SB_G2:
12437             group = 2;
12438             break;
12439
12440           default:
12441             abort ();
12442           }
12443
12444         /* If REL, extract the addend from the insn.  If RELA, it will
12445            have already been fetched for us.  */
12446         if (globals->use_rel)
12447           {
12448             int negative = (insn & (1 << 23)) ? 1 : -1;
12449             signed_addend = negative * (((insn & 0xf00) >> 4) + (insn & 0xf));
12450           }
12451
12452         /* Compute the value (X) to go in the place.  */
12453         if (r_type == R_ARM_LDRS_PC_G0
12454             || r_type == R_ARM_LDRS_PC_G1
12455             || r_type == R_ARM_LDRS_PC_G2)
12456           /* PC relative.  */
12457           signed_value = value - pc + signed_addend;
12458         else
12459           /* Section base relative.  */
12460           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         calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
12465                                     group - 1, &residual);
12466
12467         /* Check for overflow.  */
12468         if (residual >= 0x100)
12469           {
12470             _bfd_error_handler
12471               /* xgettext:c-format */
12472               (_("%pB(%pA+%#" PRIx64 "): overflow whilst "
12473                  "splitting %#" PRIx64 " for group relocation %s"),
12474                input_bfd, input_section, (uint64_t) rel->r_offset,
12475                (uint64_t) (signed_value < 0 ? -signed_value : signed_value),
12476                howto->name);
12477             return bfd_reloc_overflow;
12478           }
12479
12480         /* Mask out the value and U bit.  */
12481         insn &= 0xff7ff0f0;
12482
12483         /* Set the U bit if the value to go in the place is non-negative.  */
12484         if (signed_value >= 0)
12485           insn |= 1 << 23;
12486
12487         /* Encode the offset.  */
12488         insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
12489
12490         bfd_put_32 (input_bfd, insn, hit_data);
12491       }
12492       return bfd_reloc_ok;
12493
12494     case R_ARM_LDC_PC_G0:
12495     case R_ARM_LDC_PC_G1:
12496     case R_ARM_LDC_PC_G2:
12497     case R_ARM_LDC_SB_G0:
12498     case R_ARM_LDC_SB_G1:
12499     case R_ARM_LDC_SB_G2:
12500       {
12501         bfd_vma insn = bfd_get_32 (input_bfd, hit_data);
12502         bfd_vma pc = input_section->output_section->vma
12503                      + input_section->output_offset + rel->r_offset;
12504         /* sb is the origin of the *segment* containing the symbol.  */
12505         bfd_vma sb = sym_sec ? sym_sec->output_section->vma : 0;
12506         bfd_vma residual;
12507         bfd_signed_vma signed_value;
12508         int group = 0;
12509
12510         /* Determine which groups of bits to calculate.  */
12511         switch (r_type)
12512           {
12513           case R_ARM_LDC_PC_G0:
12514           case R_ARM_LDC_SB_G0:
12515             group = 0;
12516             break;
12517
12518           case R_ARM_LDC_PC_G1:
12519           case R_ARM_LDC_SB_G1:
12520             group = 1;
12521             break;
12522
12523           case R_ARM_LDC_PC_G2:
12524           case R_ARM_LDC_SB_G2:
12525             group = 2;
12526             break;
12527
12528           default:
12529             abort ();
12530           }
12531
12532         /* If REL, extract the addend from the insn.  If RELA, it will
12533            have already been fetched for us.  */
12534         if (globals->use_rel)
12535           {
12536             int negative = (insn & (1 << 23)) ? 1 : -1;
12537             signed_addend = negative * ((insn & 0xff) << 2);
12538           }
12539
12540         /* Compute the value (X) to go in the place.  */
12541         if (r_type == R_ARM_LDC_PC_G0
12542             || r_type == R_ARM_LDC_PC_G1
12543             || r_type == R_ARM_LDC_PC_G2)
12544           /* PC relative.  */
12545           signed_value = value - pc + signed_addend;
12546         else
12547           /* Section base relative.  */
12548           signed_value = value - sb + signed_addend;
12549
12550         /* Calculate the value of the relevant G_{n-1} to obtain
12551            the residual at that stage.  */
12552         calculate_group_reloc_mask (signed_value < 0 ? - signed_value : signed_value,
12553                                     group - 1, &residual);
12554
12555         /* Check for overflow.  (The absolute value to go in the place must be
12556            divisible by four and, after having been divided by four, must
12557            fit in eight bits.)  */
12558         if ((residual & 0x3) != 0 || residual >= 0x400)
12559           {
12560             _bfd_error_handler
12561               /* xgettext:c-format */
12562               (_("%pB(%pA+%#" PRIx64 "): overflow whilst "
12563                  "splitting %#" PRIx64 " for group relocation %s"),
12564                input_bfd, input_section, (uint64_t) rel->r_offset,
12565                (uint64_t) (signed_value < 0 ? -signed_value : signed_value),
12566                howto->name);
12567             return bfd_reloc_overflow;
12568           }
12569
12570         /* Mask out the value and U bit.  */
12571         insn &= 0xff7fff00;
12572
12573         /* Set the U bit if the value to go in the place is non-negative.  */
12574         if (signed_value >= 0)
12575           insn |= 1 << 23;
12576
12577         /* Encode the offset.  */
12578         insn |= residual >> 2;
12579
12580         bfd_put_32 (input_bfd, insn, hit_data);
12581       }
12582       return bfd_reloc_ok;
12583
12584     case R_ARM_THM_ALU_ABS_G0_NC:
12585     case R_ARM_THM_ALU_ABS_G1_NC:
12586     case R_ARM_THM_ALU_ABS_G2_NC:
12587     case R_ARM_THM_ALU_ABS_G3_NC:
12588         {
12589             const int shift_array[4] = {0, 8, 16, 24};
12590             bfd_vma insn = bfd_get_16 (input_bfd, hit_data);
12591             bfd_vma addr = value;
12592             int shift = shift_array[r_type - R_ARM_THM_ALU_ABS_G0_NC];
12593
12594             /* Compute address.  */
12595             if (globals->use_rel)
12596                 signed_addend = insn & 0xff;
12597             addr += signed_addend;
12598             if (branch_type == ST_BRANCH_TO_THUMB)
12599                 addr |= 1;
12600             /* Clean imm8 insn.  */
12601             insn &= 0xff00;
12602             /* And update with correct part of address.  */
12603             insn |= (addr >> shift) & 0xff;
12604             /* Update insn.  */
12605             bfd_put_16 (input_bfd, insn, hit_data);
12606         }
12607
12608         *unresolved_reloc_p = FALSE;
12609         return bfd_reloc_ok;
12610
12611     case R_ARM_GOTOFFFUNCDESC:
12612       {
12613         if (h == NULL)
12614           {
12615             struct fdpic_local *local_fdpic_cnts = elf32_arm_local_fdpic_cnts(input_bfd);
12616             int dynindx = elf_section_data (sym_sec->output_section)->dynindx;
12617             int offset = local_fdpic_cnts[r_symndx].funcdesc_offset & ~1;
12618             bfd_vma addr = dynreloc_value - sym_sec->output_section->vma;
12619             bfd_vma seg = -1;
12620
12621             if (bfd_link_pic(info) && dynindx == 0)
12622               abort();
12623
12624             /* Resolve relocation.  */
12625             bfd_put_32(output_bfd, (offset + sgot->output_offset)
12626                        , contents + rel->r_offset);
12627             /* Emit R_ARM_FUNCDESC_VALUE or two fixups on funcdesc if
12628                not done yet.  */
12629             arm_elf_fill_funcdesc(output_bfd, info,
12630                                   &local_fdpic_cnts[r_symndx].funcdesc_offset,
12631                                   dynindx, offset, addr, dynreloc_value, seg);
12632           }
12633         else
12634           {
12635             int dynindx;
12636             int offset = eh->fdpic_cnts.funcdesc_offset & ~1;
12637             bfd_vma addr;
12638             bfd_vma seg = -1;
12639
12640             /* For static binaries, sym_sec can be null.  */
12641             if (sym_sec)
12642               {
12643                 dynindx = elf_section_data (sym_sec->output_section)->dynindx;
12644                 addr = dynreloc_value - sym_sec->output_section->vma;
12645               }
12646             else
12647               {
12648                 dynindx = 0;
12649                 addr = 0;
12650               }
12651
12652             if (bfd_link_pic(info) && dynindx == 0)
12653               abort();
12654
12655             /* This case cannot occur since funcdesc is allocated by
12656                the dynamic loader so we cannot resolve the relocation.  */
12657             if (h->dynindx != -1)
12658               abort();
12659
12660             /* Resolve relocation.  */
12661             bfd_put_32(output_bfd, (offset + sgot->output_offset),
12662                        contents + rel->r_offset);
12663             /* Emit R_ARM_FUNCDESC_VALUE on funcdesc if not done yet.  */
12664             arm_elf_fill_funcdesc(output_bfd, info,
12665                                   &eh->fdpic_cnts.funcdesc_offset,
12666                                   dynindx, offset, addr, dynreloc_value, seg);
12667           }
12668       }
12669       *unresolved_reloc_p = FALSE;
12670       return bfd_reloc_ok;
12671
12672     case R_ARM_GOTFUNCDESC:
12673       {
12674         if (h != NULL)
12675           {
12676             Elf_Internal_Rela outrel;
12677
12678             /* Resolve relocation.  */
12679             bfd_put_32(output_bfd, ((eh->fdpic_cnts.gotfuncdesc_offset & ~1)
12680                                     + sgot->output_offset),
12681                        contents + rel->r_offset);
12682             /* Add funcdesc and associated R_ARM_FUNCDESC_VALUE.  */
12683             if(h->dynindx == -1)
12684               {
12685                 int dynindx;
12686                 int offset = eh->fdpic_cnts.funcdesc_offset & ~1;
12687                 bfd_vma addr;
12688                 bfd_vma seg = -1;
12689
12690                 /* For static binaries sym_sec can be null.  */
12691                 if (sym_sec)
12692                   {
12693                     dynindx = elf_section_data (sym_sec->output_section)->dynindx;
12694                     addr = dynreloc_value - sym_sec->output_section->vma;
12695                   }
12696                 else
12697                   {
12698                     dynindx = 0;
12699                     addr = 0;
12700                   }
12701
12702                 /* Emit R_ARM_FUNCDESC_VALUE on funcdesc if not done yet.  */
12703                 arm_elf_fill_funcdesc(output_bfd, info,
12704                                       &eh->fdpic_cnts.funcdesc_offset,
12705                                       dynindx, offset, addr, dynreloc_value, seg);
12706               }
12707
12708             /* Add a dynamic relocation on GOT entry if not already done.  */
12709             if ((eh->fdpic_cnts.gotfuncdesc_offset & 1) == 0)
12710               {
12711                 if (h->dynindx == -1)
12712                   {
12713                     outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
12714                     if (h->root.type == bfd_link_hash_undefweak)
12715                       bfd_put_32(output_bfd, 0, sgot->contents
12716                                  + (eh->fdpic_cnts.gotfuncdesc_offset & ~1));
12717                     else
12718                       bfd_put_32(output_bfd, sgot->output_section->vma
12719                                  + sgot->output_offset
12720                                  + (eh->fdpic_cnts.funcdesc_offset & ~1),
12721                                  sgot->contents
12722                                  + (eh->fdpic_cnts.gotfuncdesc_offset & ~1));
12723                   }
12724                 else
12725                   {
12726                     outrel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_FUNCDESC);
12727                   }
12728                 outrel.r_offset = sgot->output_section->vma
12729                   + sgot->output_offset
12730                   + (eh->fdpic_cnts.gotfuncdesc_offset & ~1);
12731                 outrel.r_addend = 0;
12732                 if (h->dynindx == -1 && !bfd_link_pic(info))
12733                   if (h->root.type == bfd_link_hash_undefweak)
12734                     arm_elf_add_rofixup(output_bfd, globals->srofixup, -1);
12735                   else
12736                     arm_elf_add_rofixup(output_bfd, globals->srofixup, outrel.r_offset);
12737                 else
12738                   elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
12739                 eh->fdpic_cnts.gotfuncdesc_offset |= 1;
12740               }
12741           }
12742         else
12743           {
12744             /* Such relocation on static function should not have been
12745                emitted by the compiler.  */
12746             abort();
12747           }
12748       }
12749       *unresolved_reloc_p = FALSE;
12750       return bfd_reloc_ok;
12751
12752     case R_ARM_FUNCDESC:
12753       {
12754         if (h == NULL)
12755           {
12756             struct fdpic_local *local_fdpic_cnts = elf32_arm_local_fdpic_cnts(input_bfd);
12757             Elf_Internal_Rela outrel;
12758             int dynindx = elf_section_data (sym_sec->output_section)->dynindx;
12759             int offset = local_fdpic_cnts[r_symndx].funcdesc_offset & ~1;
12760             bfd_vma addr = dynreloc_value - sym_sec->output_section->vma;
12761             bfd_vma seg = -1;
12762
12763             if (bfd_link_pic(info) && dynindx == 0)
12764               abort();
12765
12766             /* Replace static FUNCDESC relocation with a
12767                R_ARM_RELATIVE dynamic relocation or with a rofixup for
12768                executable.  */
12769             outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
12770             outrel.r_offset = input_section->output_section->vma
12771               + input_section->output_offset + rel->r_offset;
12772             outrel.r_addend = 0;
12773             if (bfd_link_pic(info))
12774               elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
12775             else
12776               arm_elf_add_rofixup(output_bfd, globals->srofixup, outrel.r_offset);
12777
12778             bfd_put_32 (input_bfd, sgot->output_section->vma
12779                         + sgot->output_offset + offset, hit_data);
12780
12781             /* Emit R_ARM_FUNCDESC_VALUE on funcdesc if not done yet.  */
12782             arm_elf_fill_funcdesc(output_bfd, info,
12783                                   &local_fdpic_cnts[r_symndx].funcdesc_offset,
12784                                   dynindx, offset, addr, dynreloc_value, seg);
12785           }
12786         else
12787           {
12788             if (h->dynindx == -1)
12789               {
12790                 int dynindx;
12791                 int offset = eh->fdpic_cnts.funcdesc_offset & ~1;
12792                 bfd_vma addr;
12793                 bfd_vma seg = -1;
12794                 Elf_Internal_Rela outrel;
12795
12796                 /* For static binaries sym_sec can be null.  */
12797                 if (sym_sec)
12798                   {
12799                     dynindx = elf_section_data (sym_sec->output_section)->dynindx;
12800                     addr = dynreloc_value - sym_sec->output_section->vma;
12801                   }
12802                 else
12803                   {
12804                     dynindx = 0;
12805                     addr = 0;
12806                   }
12807
12808                 if (bfd_link_pic(info) && dynindx == 0)
12809                   abort();
12810
12811                 /* Replace static FUNCDESC relocation with a
12812                    R_ARM_RELATIVE dynamic relocation.  */
12813                 outrel.r_info = ELF32_R_INFO (0, R_ARM_RELATIVE);
12814                 outrel.r_offset = input_section->output_section->vma
12815                   + input_section->output_offset + rel->r_offset;
12816                 outrel.r_addend = 0;
12817                 if (bfd_link_pic(info))
12818                   elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
12819                 else
12820                   arm_elf_add_rofixup(output_bfd, globals->srofixup, outrel.r_offset);
12821
12822                 bfd_put_32 (input_bfd, sgot->output_section->vma
12823                             + sgot->output_offset + offset, hit_data);
12824
12825                 /* Emit R_ARM_FUNCDESC_VALUE on funcdesc if not done yet.  */
12826                 arm_elf_fill_funcdesc(output_bfd, info,
12827                                       &eh->fdpic_cnts.funcdesc_offset,
12828                                       dynindx, offset, addr, dynreloc_value, seg);
12829               }
12830             else
12831               {
12832                 Elf_Internal_Rela outrel;
12833
12834                 /* Add a dynamic relocation.  */
12835                 outrel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_FUNCDESC);
12836                 outrel.r_offset = input_section->output_section->vma
12837                   + input_section->output_offset + rel->r_offset;
12838                 outrel.r_addend = 0;
12839                 elf32_arm_add_dynreloc (output_bfd, info, srelgot, &outrel);
12840               }
12841           }
12842       }
12843       *unresolved_reloc_p = FALSE;
12844       return bfd_reloc_ok;
12845
12846     default:
12847       return bfd_reloc_notsupported;
12848     }
12849 }
12850
12851 /* Add INCREMENT to the reloc (of type HOWTO) at ADDRESS.  */
12852 static void
12853 arm_add_to_rel (bfd *              abfd,
12854                 bfd_byte *         address,
12855                 reloc_howto_type * howto,
12856                 bfd_signed_vma     increment)
12857 {
12858   bfd_signed_vma addend;
12859
12860   if (howto->type == R_ARM_THM_CALL
12861       || howto->type == R_ARM_THM_JUMP24)
12862     {
12863       int upper_insn, lower_insn;
12864       int upper, lower;
12865
12866       upper_insn = bfd_get_16 (abfd, address);
12867       lower_insn = bfd_get_16 (abfd, address + 2);
12868       upper = upper_insn & 0x7ff;
12869       lower = lower_insn & 0x7ff;
12870
12871       addend = (upper << 12) | (lower << 1);
12872       addend += increment;
12873       addend >>= 1;
12874
12875       upper_insn = (upper_insn & 0xf800) | ((addend >> 11) & 0x7ff);
12876       lower_insn = (lower_insn & 0xf800) | (addend & 0x7ff);
12877
12878       bfd_put_16 (abfd, (bfd_vma) upper_insn, address);
12879       bfd_put_16 (abfd, (bfd_vma) lower_insn, address + 2);
12880     }
12881   else
12882     {
12883       bfd_vma        contents;
12884
12885       contents = bfd_get_32 (abfd, address);
12886
12887       /* Get the (signed) value from the instruction.  */
12888       addend = contents & howto->src_mask;
12889       if (addend & ((howto->src_mask + 1) >> 1))
12890         {
12891           bfd_signed_vma mask;
12892
12893           mask = -1;
12894           mask &= ~ howto->src_mask;
12895           addend |= mask;
12896         }
12897
12898       /* Add in the increment, (which is a byte value).  */
12899       switch (howto->type)
12900         {
12901         default:
12902           addend += increment;
12903           break;
12904
12905         case R_ARM_PC24:
12906         case R_ARM_PLT32:
12907         case R_ARM_CALL:
12908         case R_ARM_JUMP24:
12909           addend <<= howto->size;
12910           addend += increment;
12911
12912           /* Should we check for overflow here ?  */
12913
12914           /* Drop any undesired bits.  */
12915           addend >>= howto->rightshift;
12916           break;
12917         }
12918
12919       contents = (contents & ~ howto->dst_mask) | (addend & howto->dst_mask);
12920
12921       bfd_put_32 (abfd, contents, address);
12922     }
12923 }
12924
12925 #define IS_ARM_TLS_RELOC(R_TYPE)        \
12926   ((R_TYPE) == R_ARM_TLS_GD32           \
12927    || (R_TYPE) == R_ARM_TLS_GD32_FDPIC  \
12928    || (R_TYPE) == R_ARM_TLS_LDO32       \
12929    || (R_TYPE) == R_ARM_TLS_LDM32       \
12930    || (R_TYPE) == R_ARM_TLS_LDM32_FDPIC \
12931    || (R_TYPE) == R_ARM_TLS_DTPOFF32    \
12932    || (R_TYPE) == R_ARM_TLS_DTPMOD32    \
12933    || (R_TYPE) == R_ARM_TLS_TPOFF32     \
12934    || (R_TYPE) == R_ARM_TLS_LE32        \
12935    || (R_TYPE) == R_ARM_TLS_IE32        \
12936    || (R_TYPE) == R_ARM_TLS_IE32_FDPIC  \
12937    || IS_ARM_TLS_GNU_RELOC (R_TYPE))
12938
12939 /* Specific set of relocations for the gnu tls dialect.  */
12940 #define IS_ARM_TLS_GNU_RELOC(R_TYPE)    \
12941   ((R_TYPE) == R_ARM_TLS_GOTDESC        \
12942    || (R_TYPE) == R_ARM_TLS_CALL        \
12943    || (R_TYPE) == R_ARM_THM_TLS_CALL    \
12944    || (R_TYPE) == R_ARM_TLS_DESCSEQ     \
12945    || (R_TYPE) == R_ARM_THM_TLS_DESCSEQ)
12946
12947 /* Relocate an ARM ELF section.  */
12948
12949 static bfd_boolean
12950 elf32_arm_relocate_section (bfd *                  output_bfd,
12951                             struct bfd_link_info * info,
12952                             bfd *                  input_bfd,
12953                             asection *             input_section,
12954                             bfd_byte *             contents,
12955                             Elf_Internal_Rela *    relocs,
12956                             Elf_Internal_Sym *     local_syms,
12957                             asection **            local_sections)
12958 {
12959   Elf_Internal_Shdr *symtab_hdr;
12960   struct elf_link_hash_entry **sym_hashes;
12961   Elf_Internal_Rela *rel;
12962   Elf_Internal_Rela *relend;
12963   const char *name;
12964   struct elf32_arm_link_hash_table * globals;
12965
12966   globals = elf32_arm_hash_table (info);
12967   if (globals == NULL)
12968     return FALSE;
12969
12970   symtab_hdr = & elf_symtab_hdr (input_bfd);
12971   sym_hashes = elf_sym_hashes (input_bfd);
12972
12973   rel = relocs;
12974   relend = relocs + input_section->reloc_count;
12975   for (; rel < relend; rel++)
12976     {
12977       int                          r_type;
12978       reloc_howto_type *           howto;
12979       unsigned long                r_symndx;
12980       Elf_Internal_Sym *           sym;
12981       asection *                   sec;
12982       struct elf_link_hash_entry * h;
12983       bfd_vma                      relocation;
12984       bfd_reloc_status_type        r;
12985       arelent                      bfd_reloc;
12986       char                         sym_type;
12987       bfd_boolean                  unresolved_reloc = FALSE;
12988       char *error_message = NULL;
12989
12990       r_symndx = ELF32_R_SYM (rel->r_info);
12991       r_type   = ELF32_R_TYPE (rel->r_info);
12992       r_type   = arm_real_reloc_type (globals, r_type);
12993
12994       if (   r_type == R_ARM_GNU_VTENTRY
12995           || r_type == R_ARM_GNU_VTINHERIT)
12996         continue;
12997
12998       howto = bfd_reloc.howto = elf32_arm_howto_from_type (r_type);
12999
13000       if (howto == NULL)
13001         return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
13002
13003       h = NULL;
13004       sym = NULL;
13005       sec = NULL;
13006
13007       if (r_symndx < symtab_hdr->sh_info)
13008         {
13009           sym = local_syms + r_symndx;
13010           sym_type = ELF32_ST_TYPE (sym->st_info);
13011           sec = local_sections[r_symndx];
13012
13013           /* An object file might have a reference to a local
13014              undefined symbol.  This is a daft object file, but we
13015              should at least do something about it.  V4BX & NONE
13016              relocations do not use the symbol and are explicitly
13017              allowed to use the undefined symbol, so allow those.
13018              Likewise for relocations against STN_UNDEF.  */
13019           if (r_type != R_ARM_V4BX
13020               && r_type != R_ARM_NONE
13021               && r_symndx != STN_UNDEF
13022               && bfd_is_und_section (sec)
13023               && ELF_ST_BIND (sym->st_info) != STB_WEAK)
13024             (*info->callbacks->undefined_symbol)
13025               (info, bfd_elf_string_from_elf_section
13026                (input_bfd, symtab_hdr->sh_link, sym->st_name),
13027                input_bfd, input_section,
13028                rel->r_offset, TRUE);
13029
13030           if (globals->use_rel)
13031             {
13032               relocation = (sec->output_section->vma
13033                             + sec->output_offset
13034                             + sym->st_value);
13035               if (!bfd_link_relocatable (info)
13036                   && (sec->flags & SEC_MERGE)
13037                   && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13038                 {
13039                   asection *msec;
13040                   bfd_vma addend, value;
13041
13042                   switch (r_type)
13043                     {
13044                     case R_ARM_MOVW_ABS_NC:
13045                     case R_ARM_MOVT_ABS:
13046                       value = bfd_get_32 (input_bfd, contents + rel->r_offset);
13047                       addend = ((value & 0xf0000) >> 4) | (value & 0xfff);
13048                       addend = (addend ^ 0x8000) - 0x8000;
13049                       break;
13050
13051                     case R_ARM_THM_MOVW_ABS_NC:
13052                     case R_ARM_THM_MOVT_ABS:
13053                       value = bfd_get_16 (input_bfd, contents + rel->r_offset)
13054                               << 16;
13055                       value |= bfd_get_16 (input_bfd,
13056                                            contents + rel->r_offset + 2);
13057                       addend = ((value & 0xf7000) >> 4) | (value & 0xff)
13058                                | ((value & 0x04000000) >> 15);
13059                       addend = (addend ^ 0x8000) - 0x8000;
13060                       break;
13061
13062                     default:
13063                       if (howto->rightshift
13064                           || (howto->src_mask & (howto->src_mask + 1)))
13065                         {
13066                           _bfd_error_handler
13067                             /* xgettext:c-format */
13068                             (_("%pB(%pA+%#" PRIx64 "): "
13069                                "%s relocation against SEC_MERGE section"),
13070                              input_bfd, input_section,
13071                              (uint64_t) rel->r_offset, howto->name);
13072                           return FALSE;
13073                         }
13074
13075                       value = bfd_get_32 (input_bfd, contents + rel->r_offset);
13076
13077                       /* Get the (signed) value from the instruction.  */
13078                       addend = value & howto->src_mask;
13079                       if (addend & ((howto->src_mask + 1) >> 1))
13080                         {
13081                           bfd_signed_vma mask;
13082
13083                           mask = -1;
13084                           mask &= ~ howto->src_mask;
13085                           addend |= mask;
13086                         }
13087                       break;
13088                     }
13089
13090                   msec = sec;
13091                   addend =
13092                     _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
13093                     - relocation;
13094                   addend += msec->output_section->vma + msec->output_offset;
13095
13096                   /* Cases here must match those in the preceding
13097                      switch statement.  */
13098                   switch (r_type)
13099                     {
13100                     case R_ARM_MOVW_ABS_NC:
13101                     case R_ARM_MOVT_ABS:
13102                       value = (value & 0xfff0f000) | ((addend & 0xf000) << 4)
13103                               | (addend & 0xfff);
13104                       bfd_put_32 (input_bfd, value, contents + rel->r_offset);
13105                       break;
13106
13107                     case R_ARM_THM_MOVW_ABS_NC:
13108                     case R_ARM_THM_MOVT_ABS:
13109                       value = (value & 0xfbf08f00) | ((addend & 0xf700) << 4)
13110                               | (addend & 0xff) | ((addend & 0x0800) << 15);
13111                       bfd_put_16 (input_bfd, value >> 16,
13112                                   contents + rel->r_offset);
13113                       bfd_put_16 (input_bfd, value,
13114                                   contents + rel->r_offset + 2);
13115                       break;
13116
13117                     default:
13118                       value = (value & ~ howto->dst_mask)
13119                               | (addend & howto->dst_mask);
13120                       bfd_put_32 (input_bfd, value, contents + rel->r_offset);
13121                       break;
13122                     }
13123                 }
13124             }
13125           else
13126             relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13127         }
13128       else
13129         {
13130           bfd_boolean warned, ignored;
13131
13132           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13133                                    r_symndx, symtab_hdr, sym_hashes,
13134                                    h, sec, relocation,
13135                                    unresolved_reloc, warned, ignored);
13136
13137           sym_type = h->type;
13138         }
13139
13140       if (sec != NULL && discarded_section (sec))
13141         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
13142                                          rel, 1, relend, howto, 0, contents);
13143
13144       if (bfd_link_relocatable (info))
13145         {
13146           /* This is a relocatable link.  We don't have to change
13147              anything, unless the reloc is against a section symbol,
13148              in which case we have to adjust according to where the
13149              section symbol winds up in the output section.  */
13150           if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13151             {
13152               if (globals->use_rel)
13153                 arm_add_to_rel (input_bfd, contents + rel->r_offset,
13154                                 howto, (bfd_signed_vma) sec->output_offset);
13155               else
13156                 rel->r_addend += sec->output_offset;
13157             }
13158           continue;
13159         }
13160
13161       if (h != NULL)
13162         name = h->root.root.string;
13163       else
13164         {
13165           name = (bfd_elf_string_from_elf_section
13166                   (input_bfd, symtab_hdr->sh_link, sym->st_name));
13167           if (name == NULL || *name == '\0')
13168             name = bfd_section_name (input_bfd, sec);
13169         }
13170
13171       if (r_symndx != STN_UNDEF
13172           && r_type != R_ARM_NONE
13173           && (h == NULL
13174               || h->root.type == bfd_link_hash_defined
13175               || h->root.type == bfd_link_hash_defweak)
13176           && IS_ARM_TLS_RELOC (r_type) != (sym_type == STT_TLS))
13177         {
13178           _bfd_error_handler
13179             ((sym_type == STT_TLS
13180               /* xgettext:c-format */
13181               ? _("%pB(%pA+%#" PRIx64 "): %s used with TLS symbol %s")
13182               /* xgettext:c-format */
13183               : _("%pB(%pA+%#" PRIx64 "): %s used with non-TLS symbol %s")),
13184              input_bfd,
13185              input_section,
13186              (uint64_t) rel->r_offset,
13187              howto->name,
13188              name);
13189         }
13190
13191       /* We call elf32_arm_final_link_relocate unless we're completely
13192          done, i.e., the relaxation produced the final output we want,
13193          and we won't let anybody mess with it. Also, we have to do
13194          addend adjustments in case of a R_ARM_TLS_GOTDESC relocation
13195          both in relaxed and non-relaxed cases.  */
13196       if ((elf32_arm_tls_transition (info, r_type, h) != (unsigned)r_type)
13197           || (IS_ARM_TLS_GNU_RELOC (r_type)
13198               && !((h ? elf32_arm_hash_entry (h)->tls_type :
13199                     elf32_arm_local_got_tls_type (input_bfd)[r_symndx])
13200                    & GOT_TLS_GDESC)))
13201         {
13202           r = elf32_arm_tls_relax (globals, input_bfd, input_section,
13203                                    contents, rel, h == NULL);
13204           /* This may have been marked unresolved because it came from
13205              a shared library.  But we've just dealt with that.  */
13206           unresolved_reloc = 0;
13207         }
13208       else
13209         r = bfd_reloc_continue;
13210
13211       if (r == bfd_reloc_continue)
13212         {
13213           unsigned char branch_type =
13214             h ? ARM_GET_SYM_BRANCH_TYPE (h->target_internal)
13215               : ARM_GET_SYM_BRANCH_TYPE (sym->st_target_internal);
13216
13217           r = elf32_arm_final_link_relocate (howto, input_bfd, output_bfd,
13218                                              input_section, contents, rel,
13219                                              relocation, info, sec, name,
13220                                              sym_type, branch_type, h,
13221                                              &unresolved_reloc,
13222                                              &error_message);
13223         }
13224
13225       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13226          because such sections are not SEC_ALLOC and thus ld.so will
13227          not process them.  */
13228       if (unresolved_reloc
13229           && !((input_section->flags & SEC_DEBUGGING) != 0
13230                && h->def_dynamic)
13231           && _bfd_elf_section_offset (output_bfd, info, input_section,
13232                                       rel->r_offset) != (bfd_vma) -1)
13233         {
13234           _bfd_error_handler
13235             /* xgettext:c-format */
13236             (_("%pB(%pA+%#" PRIx64 "): "
13237                "unresolvable %s relocation against symbol `%s'"),
13238              input_bfd,
13239              input_section,
13240              (uint64_t) rel->r_offset,
13241              howto->name,
13242              h->root.root.string);
13243           return FALSE;
13244         }
13245
13246       if (r != bfd_reloc_ok)
13247         {
13248           switch (r)
13249             {
13250             case bfd_reloc_overflow:
13251               /* If the overflowing reloc was to an undefined symbol,
13252                  we have already printed one error message and there
13253                  is no point complaining again.  */
13254               if (!h || h->root.type != bfd_link_hash_undefined)
13255                 (*info->callbacks->reloc_overflow)
13256                   (info, (h ? &h->root : NULL), name, howto->name,
13257                    (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
13258               break;
13259
13260             case bfd_reloc_undefined:
13261               (*info->callbacks->undefined_symbol)
13262                 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
13263               break;
13264
13265             case bfd_reloc_outofrange:
13266               error_message = _("out of range");
13267               goto common_error;
13268
13269             case bfd_reloc_notsupported:
13270               error_message = _("unsupported relocation");
13271               goto common_error;
13272
13273             case bfd_reloc_dangerous:
13274               /* error_message should already be set.  */
13275               goto common_error;
13276
13277             default:
13278               error_message = _("unknown error");
13279               /* Fall through.  */
13280
13281             common_error:
13282               BFD_ASSERT (error_message != NULL);
13283               (*info->callbacks->reloc_dangerous)
13284                 (info, error_message, input_bfd, input_section, rel->r_offset);
13285               break;
13286             }
13287         }
13288     }
13289
13290   return TRUE;
13291 }
13292
13293 /* Add a new unwind edit to the list described by HEAD, TAIL.  If TINDEX is zero,
13294    adds the edit to the start of the list.  (The list must be built in order of
13295    ascending TINDEX: the function's callers are primarily responsible for
13296    maintaining that condition).  */
13297
13298 static void
13299 add_unwind_table_edit (arm_unwind_table_edit **head,
13300                        arm_unwind_table_edit **tail,
13301                        arm_unwind_edit_type type,
13302                        asection *linked_section,
13303                        unsigned int tindex)
13304 {
13305   arm_unwind_table_edit *new_edit = (arm_unwind_table_edit *)
13306       xmalloc (sizeof (arm_unwind_table_edit));
13307
13308   new_edit->type = type;
13309   new_edit->linked_section = linked_section;
13310   new_edit->index = tindex;
13311
13312   if (tindex > 0)
13313     {
13314       new_edit->next = NULL;
13315
13316       if (*tail)
13317         (*tail)->next = new_edit;
13318
13319       (*tail) = new_edit;
13320
13321       if (!*head)
13322         (*head) = new_edit;
13323     }
13324   else
13325     {
13326       new_edit->next = *head;
13327
13328       if (!*tail)
13329         *tail = new_edit;
13330
13331       *head = new_edit;
13332     }
13333 }
13334
13335 static _arm_elf_section_data *get_arm_elf_section_data (asection *);
13336
13337 /* Increase the size of EXIDX_SEC by ADJUST bytes.  ADJUST mau be negative.  */
13338 static void
13339 adjust_exidx_size(asection *exidx_sec, int adjust)
13340 {
13341   asection *out_sec;
13342
13343   if (!exidx_sec->rawsize)
13344     exidx_sec->rawsize = exidx_sec->size;
13345
13346   bfd_set_section_size (exidx_sec->owner, exidx_sec, exidx_sec->size + adjust);
13347   out_sec = exidx_sec->output_section;
13348   /* Adjust size of output section.  */
13349   bfd_set_section_size (out_sec->owner, out_sec, out_sec->size +adjust);
13350 }
13351
13352 /* Insert an EXIDX_CANTUNWIND marker at the end of a section.  */
13353 static void
13354 insert_cantunwind_after(asection *text_sec, asection *exidx_sec)
13355 {
13356   struct _arm_elf_section_data *exidx_arm_data;
13357
13358   exidx_arm_data = get_arm_elf_section_data (exidx_sec);
13359   add_unwind_table_edit (
13360     &exidx_arm_data->u.exidx.unwind_edit_list,
13361     &exidx_arm_data->u.exidx.unwind_edit_tail,
13362     INSERT_EXIDX_CANTUNWIND_AT_END, text_sec, UINT_MAX);
13363
13364   exidx_arm_data->additional_reloc_count++;
13365
13366   adjust_exidx_size(exidx_sec, 8);
13367 }
13368
13369 /* Scan .ARM.exidx tables, and create a list describing edits which should be
13370    made to those tables, such that:
13371
13372      1. Regions without unwind data are marked with EXIDX_CANTUNWIND entries.
13373      2. Duplicate entries are merged together (EXIDX_CANTUNWIND, or unwind
13374         codes which have been inlined into the index).
13375
13376    If MERGE_EXIDX_ENTRIES is false, duplicate entries are not merged.
13377
13378    The edits are applied when the tables are written
13379    (in elf32_arm_write_section).  */
13380
13381 bfd_boolean
13382 elf32_arm_fix_exidx_coverage (asection **text_section_order,
13383                               unsigned int num_text_sections,
13384                               struct bfd_link_info *info,
13385                               bfd_boolean merge_exidx_entries)
13386 {
13387   bfd *inp;
13388   unsigned int last_second_word = 0, i;
13389   asection *last_exidx_sec = NULL;
13390   asection *last_text_sec = NULL;
13391   int last_unwind_type = -1;
13392
13393   /* Walk over all EXIDX sections, and create backlinks from the corrsponding
13394      text sections.  */
13395   for (inp = info->input_bfds; inp != NULL; inp = inp->link.next)
13396     {
13397       asection *sec;
13398
13399       for (sec = inp->sections; sec != NULL; sec = sec->next)
13400         {
13401           struct bfd_elf_section_data *elf_sec = elf_section_data (sec);
13402           Elf_Internal_Shdr *hdr = &elf_sec->this_hdr;
13403
13404           if (!hdr || hdr->sh_type != SHT_ARM_EXIDX)
13405             continue;
13406
13407           if (elf_sec->linked_to)
13408             {
13409               Elf_Internal_Shdr *linked_hdr
13410                 = &elf_section_data (elf_sec->linked_to)->this_hdr;
13411               struct _arm_elf_section_data *linked_sec_arm_data
13412                 = get_arm_elf_section_data (linked_hdr->bfd_section);
13413
13414               if (linked_sec_arm_data == NULL)
13415                 continue;
13416
13417               /* Link this .ARM.exidx section back from the text section it
13418                  describes.  */
13419               linked_sec_arm_data->u.text.arm_exidx_sec = sec;
13420             }
13421         }
13422     }
13423
13424   /* Walk all text sections in order of increasing VMA.  Eilminate duplicate
13425      index table entries (EXIDX_CANTUNWIND and inlined unwind opcodes),
13426      and add EXIDX_CANTUNWIND entries for sections with no unwind table data.  */
13427
13428   for (i = 0; i < num_text_sections; i++)
13429     {
13430       asection *sec = text_section_order[i];
13431       asection *exidx_sec;
13432       struct _arm_elf_section_data *arm_data = get_arm_elf_section_data (sec);
13433       struct _arm_elf_section_data *exidx_arm_data;
13434       bfd_byte *contents = NULL;
13435       int deleted_exidx_bytes = 0;
13436       bfd_vma j;
13437       arm_unwind_table_edit *unwind_edit_head = NULL;
13438       arm_unwind_table_edit *unwind_edit_tail = NULL;
13439       Elf_Internal_Shdr *hdr;
13440       bfd *ibfd;
13441
13442       if (arm_data == NULL)
13443         continue;
13444
13445       exidx_sec = arm_data->u.text.arm_exidx_sec;
13446       if (exidx_sec == NULL)
13447         {
13448           /* Section has no unwind data.  */
13449           if (last_unwind_type == 0 || !last_exidx_sec)
13450             continue;
13451
13452           /* Ignore zero sized sections.  */
13453           if (sec->size == 0)
13454             continue;
13455
13456           insert_cantunwind_after(last_text_sec, last_exidx_sec);
13457           last_unwind_type = 0;
13458           continue;
13459         }
13460
13461       /* Skip /DISCARD/ sections.  */
13462       if (bfd_is_abs_section (exidx_sec->output_section))
13463         continue;
13464
13465       hdr = &elf_section_data (exidx_sec)->this_hdr;
13466       if (hdr->sh_type != SHT_ARM_EXIDX)
13467         continue;
13468
13469       exidx_arm_data = get_arm_elf_section_data (exidx_sec);
13470       if (exidx_arm_data == NULL)
13471         continue;
13472
13473       ibfd = exidx_sec->owner;
13474
13475       if (hdr->contents != NULL)
13476         contents = hdr->contents;
13477       else if (! bfd_malloc_and_get_section (ibfd, exidx_sec, &contents))
13478         /* An error?  */
13479         continue;
13480
13481       if (last_unwind_type > 0)
13482         {
13483           unsigned int first_word = bfd_get_32 (ibfd, contents);
13484           /* Add cantunwind if first unwind item does not match section
13485              start.  */
13486           if (first_word != sec->vma)
13487             {
13488               insert_cantunwind_after (last_text_sec, last_exidx_sec);
13489               last_unwind_type = 0;
13490             }
13491         }
13492
13493       for (j = 0; j < hdr->sh_size; j += 8)
13494         {
13495           unsigned int second_word = bfd_get_32 (ibfd, contents + j + 4);
13496           int unwind_type;
13497           int elide = 0;
13498
13499           /* An EXIDX_CANTUNWIND entry.  */
13500           if (second_word == 1)
13501             {
13502               if (last_unwind_type == 0)
13503                 elide = 1;
13504               unwind_type = 0;
13505             }
13506           /* Inlined unwinding data.  Merge if equal to previous.  */
13507           else if ((second_word & 0x80000000) != 0)
13508             {
13509               if (merge_exidx_entries
13510                    && last_second_word == second_word && last_unwind_type == 1)
13511                 elide = 1;
13512               unwind_type = 1;
13513               last_second_word = second_word;
13514             }
13515           /* Normal table entry.  In theory we could merge these too,
13516              but duplicate entries are likely to be much less common.  */
13517           else
13518             unwind_type = 2;
13519
13520           if (elide && !bfd_link_relocatable (info))
13521             {
13522               add_unwind_table_edit (&unwind_edit_head, &unwind_edit_tail,
13523                                      DELETE_EXIDX_ENTRY, NULL, j / 8);
13524
13525               deleted_exidx_bytes += 8;
13526             }
13527
13528           last_unwind_type = unwind_type;
13529         }
13530
13531       /* Free contents if we allocated it ourselves.  */
13532       if (contents != hdr->contents)
13533         free (contents);
13534
13535       /* Record edits to be applied later (in elf32_arm_write_section).  */
13536       exidx_arm_data->u.exidx.unwind_edit_list = unwind_edit_head;
13537       exidx_arm_data->u.exidx.unwind_edit_tail = unwind_edit_tail;
13538
13539       if (deleted_exidx_bytes > 0)
13540         adjust_exidx_size(exidx_sec, -deleted_exidx_bytes);
13541
13542       last_exidx_sec = exidx_sec;
13543       last_text_sec = sec;
13544     }
13545
13546   /* Add terminating CANTUNWIND entry.  */
13547   if (!bfd_link_relocatable (info) && last_exidx_sec
13548       && last_unwind_type != 0)
13549     insert_cantunwind_after(last_text_sec, last_exidx_sec);
13550
13551   return TRUE;
13552 }
13553
13554 static bfd_boolean
13555 elf32_arm_output_glue_section (struct bfd_link_info *info, bfd *obfd,
13556                                bfd *ibfd, const char *name)
13557 {
13558   asection *sec, *osec;
13559
13560   sec = bfd_get_linker_section (ibfd, name);
13561   if (sec == NULL || (sec->flags & SEC_EXCLUDE) != 0)
13562     return TRUE;
13563
13564   osec = sec->output_section;
13565   if (elf32_arm_write_section (obfd, info, sec, sec->contents))
13566     return TRUE;
13567
13568   if (! bfd_set_section_contents (obfd, osec, sec->contents,
13569                                   sec->output_offset, sec->size))
13570     return FALSE;
13571
13572   return TRUE;
13573 }
13574
13575 static bfd_boolean
13576 elf32_arm_final_link (bfd *abfd, struct bfd_link_info *info)
13577 {
13578   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
13579   asection *sec, *osec;
13580
13581   if (globals == NULL)
13582     return FALSE;
13583
13584   /* Invoke the regular ELF backend linker to do all the work.  */
13585   if (!bfd_elf_final_link (abfd, info))
13586     return FALSE;
13587
13588   /* Process stub sections (eg BE8 encoding, ...).  */
13589   struct elf32_arm_link_hash_table *htab = elf32_arm_hash_table (info);
13590   unsigned int i;
13591   for (i=0; i<htab->top_id; i++)
13592     {
13593       sec = htab->stub_group[i].stub_sec;
13594       /* Only process it once, in its link_sec slot.  */
13595       if (sec && i == htab->stub_group[i].link_sec->id)
13596         {
13597           osec = sec->output_section;
13598           elf32_arm_write_section (abfd, info, sec, sec->contents);
13599           if (! bfd_set_section_contents (abfd, osec, sec->contents,
13600                                           sec->output_offset, sec->size))
13601             return FALSE;
13602         }
13603     }
13604
13605   /* Write out any glue sections now that we have created all the
13606      stubs.  */
13607   if (globals->bfd_of_glue_owner != NULL)
13608     {
13609       if (! elf32_arm_output_glue_section (info, abfd,
13610                                            globals->bfd_of_glue_owner,
13611                                            ARM2THUMB_GLUE_SECTION_NAME))
13612         return FALSE;
13613
13614       if (! elf32_arm_output_glue_section (info, abfd,
13615                                            globals->bfd_of_glue_owner,
13616                                            THUMB2ARM_GLUE_SECTION_NAME))
13617         return FALSE;
13618
13619       if (! elf32_arm_output_glue_section (info, abfd,
13620                                            globals->bfd_of_glue_owner,
13621                                            VFP11_ERRATUM_VENEER_SECTION_NAME))
13622         return FALSE;
13623
13624       if (! elf32_arm_output_glue_section (info, abfd,
13625                                            globals->bfd_of_glue_owner,
13626                                            STM32L4XX_ERRATUM_VENEER_SECTION_NAME))
13627         return FALSE;
13628
13629       if (! elf32_arm_output_glue_section (info, abfd,
13630                                            globals->bfd_of_glue_owner,
13631                                            ARM_BX_GLUE_SECTION_NAME))
13632         return FALSE;
13633     }
13634
13635   return TRUE;
13636 }
13637
13638 /* Return a best guess for the machine number based on the attributes.  */
13639
13640 static unsigned int
13641 bfd_arm_get_mach_from_attributes (bfd * abfd)
13642 {
13643   int arch = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC, Tag_CPU_arch);
13644
13645   switch (arch)
13646     {
13647     case TAG_CPU_ARCH_PRE_V4: return bfd_mach_arm_3M;
13648     case TAG_CPU_ARCH_V4: return bfd_mach_arm_4;
13649     case TAG_CPU_ARCH_V4T: return bfd_mach_arm_4T;
13650     case TAG_CPU_ARCH_V5T: return bfd_mach_arm_5T;
13651
13652     case TAG_CPU_ARCH_V5TE:
13653       {
13654         char * name;
13655
13656         BFD_ASSERT (Tag_CPU_name < NUM_KNOWN_OBJ_ATTRIBUTES);
13657         name = elf_known_obj_attributes (abfd) [OBJ_ATTR_PROC][Tag_CPU_name].s;
13658
13659         if (name)
13660           {
13661             if (strcmp (name, "IWMMXT2") == 0)
13662               return bfd_mach_arm_iWMMXt2;
13663
13664             if (strcmp (name, "IWMMXT") == 0)
13665               return bfd_mach_arm_iWMMXt;
13666
13667             if (strcmp (name, "XSCALE") == 0)
13668               {
13669                 int wmmx;
13670
13671                 BFD_ASSERT (Tag_WMMX_arch < NUM_KNOWN_OBJ_ATTRIBUTES);
13672                 wmmx = elf_known_obj_attributes (abfd) [OBJ_ATTR_PROC][Tag_WMMX_arch].i;
13673                 switch (wmmx)
13674                   {
13675                   case 1: return bfd_mach_arm_iWMMXt;
13676                   case 2: return bfd_mach_arm_iWMMXt2;
13677                   default: return bfd_mach_arm_XScale;
13678                   }
13679               }
13680           }
13681
13682         return bfd_mach_arm_5TE;
13683       }
13684
13685     case TAG_CPU_ARCH_V5TEJ:
13686         return bfd_mach_arm_5TEJ;
13687     case TAG_CPU_ARCH_V6:
13688         return bfd_mach_arm_6;
13689     case TAG_CPU_ARCH_V6KZ:
13690         return bfd_mach_arm_6KZ;
13691     case TAG_CPU_ARCH_V6T2:
13692         return bfd_mach_arm_6T2;
13693     case TAG_CPU_ARCH_V6K:
13694         return bfd_mach_arm_6K;
13695     case TAG_CPU_ARCH_V7:
13696         return bfd_mach_arm_7;
13697     case TAG_CPU_ARCH_V6_M:
13698         return bfd_mach_arm_6M;
13699     case TAG_CPU_ARCH_V6S_M:
13700         return bfd_mach_arm_6SM;
13701     case TAG_CPU_ARCH_V7E_M:
13702         return bfd_mach_arm_7EM;
13703     case TAG_CPU_ARCH_V8:
13704         return bfd_mach_arm_8;
13705     case TAG_CPU_ARCH_V8R:
13706         return bfd_mach_arm_8R;
13707     case TAG_CPU_ARCH_V8M_BASE:
13708         return bfd_mach_arm_8M_BASE;
13709     case TAG_CPU_ARCH_V8M_MAIN:
13710         return bfd_mach_arm_8M_MAIN;
13711
13712     default:
13713       /* Force entry to be added for any new known Tag_CPU_arch value.  */
13714       BFD_ASSERT (arch > MAX_TAG_CPU_ARCH);
13715
13716       /* Unknown Tag_CPU_arch value.  */
13717       return bfd_mach_arm_unknown;
13718     }
13719 }
13720
13721 /* Set the right machine number.  */
13722
13723 static bfd_boolean
13724 elf32_arm_object_p (bfd *abfd)
13725 {
13726   unsigned int mach;
13727
13728   mach = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
13729
13730   if (mach == bfd_mach_arm_unknown)
13731     {
13732       if (elf_elfheader (abfd)->e_flags & EF_ARM_MAVERICK_FLOAT)
13733         mach = bfd_mach_arm_ep9312;
13734       else
13735         mach = bfd_arm_get_mach_from_attributes (abfd);
13736     }
13737
13738   bfd_default_set_arch_mach (abfd, bfd_arch_arm, mach);
13739   return TRUE;
13740 }
13741
13742 /* Function to keep ARM specific flags in the ELF header.  */
13743
13744 static bfd_boolean
13745 elf32_arm_set_private_flags (bfd *abfd, flagword flags)
13746 {
13747   if (elf_flags_init (abfd)
13748       && elf_elfheader (abfd)->e_flags != flags)
13749     {
13750       if (EF_ARM_EABI_VERSION (flags) == EF_ARM_EABI_UNKNOWN)
13751         {
13752           if (flags & EF_ARM_INTERWORK)
13753             _bfd_error_handler
13754               (_("warning: not setting interworking flag of %pB since it has already been specified as non-interworking"),
13755                abfd);
13756           else
13757             _bfd_error_handler
13758               (_("warning: clearing the interworking flag of %pB due to outside request"),
13759                abfd);
13760         }
13761     }
13762   else
13763     {
13764       elf_elfheader (abfd)->e_flags = flags;
13765       elf_flags_init (abfd) = TRUE;
13766     }
13767
13768   return TRUE;
13769 }
13770
13771 /* Copy backend specific data from one object module to another.  */
13772
13773 static bfd_boolean
13774 elf32_arm_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
13775 {
13776   flagword in_flags;
13777   flagword out_flags;
13778
13779   if (! is_arm_elf (ibfd) || ! is_arm_elf (obfd))
13780     return TRUE;
13781
13782   in_flags  = elf_elfheader (ibfd)->e_flags;
13783   out_flags = elf_elfheader (obfd)->e_flags;
13784
13785   if (elf_flags_init (obfd)
13786       && EF_ARM_EABI_VERSION (out_flags) == EF_ARM_EABI_UNKNOWN
13787       && in_flags != out_flags)
13788     {
13789       /* Cannot mix APCS26 and APCS32 code.  */
13790       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
13791         return FALSE;
13792
13793       /* Cannot mix float APCS and non-float APCS code.  */
13794       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
13795         return FALSE;
13796
13797       /* If the src and dest have different interworking flags
13798          then turn off the interworking bit.  */
13799       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
13800         {
13801           if (out_flags & EF_ARM_INTERWORK)
13802             _bfd_error_handler
13803               (_("warning: clearing the interworking flag of %pB because non-interworking code in %pB has been linked with it"),
13804                obfd, ibfd);
13805
13806           in_flags &= ~EF_ARM_INTERWORK;
13807         }
13808
13809       /* Likewise for PIC, though don't warn for this case.  */
13810       if ((in_flags & EF_ARM_PIC) != (out_flags & EF_ARM_PIC))
13811         in_flags &= ~EF_ARM_PIC;
13812     }
13813
13814   elf_elfheader (obfd)->e_flags = in_flags;
13815   elf_flags_init (obfd) = TRUE;
13816
13817   return _bfd_elf_copy_private_bfd_data (ibfd, obfd);
13818 }
13819
13820 /* Values for Tag_ABI_PCS_R9_use.  */
13821 enum
13822 {
13823   AEABI_R9_V6,
13824   AEABI_R9_SB,
13825   AEABI_R9_TLS,
13826   AEABI_R9_unused
13827 };
13828
13829 /* Values for Tag_ABI_PCS_RW_data.  */
13830 enum
13831 {
13832   AEABI_PCS_RW_data_absolute,
13833   AEABI_PCS_RW_data_PCrel,
13834   AEABI_PCS_RW_data_SBrel,
13835   AEABI_PCS_RW_data_unused
13836 };
13837
13838 /* Values for Tag_ABI_enum_size.  */
13839 enum
13840 {
13841   AEABI_enum_unused,
13842   AEABI_enum_short,
13843   AEABI_enum_wide,
13844   AEABI_enum_forced_wide
13845 };
13846
13847 /* Determine whether an object attribute tag takes an integer, a
13848    string or both.  */
13849
13850 static int
13851 elf32_arm_obj_attrs_arg_type (int tag)
13852 {
13853   if (tag == Tag_compatibility)
13854     return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL;
13855   else if (tag == Tag_nodefaults)
13856     return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_NO_DEFAULT;
13857   else if (tag == Tag_CPU_raw_name || tag == Tag_CPU_name)
13858     return ATTR_TYPE_FLAG_STR_VAL;
13859   else if (tag < 32)
13860     return ATTR_TYPE_FLAG_INT_VAL;
13861   else
13862     return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
13863 }
13864
13865 /* The ABI defines that Tag_conformance should be emitted first, and that
13866    Tag_nodefaults should be second (if either is defined).  This sets those
13867    two positions, and bumps up the position of all the remaining tags to
13868    compensate.  */
13869 static int
13870 elf32_arm_obj_attrs_order (int num)
13871 {
13872   if (num == LEAST_KNOWN_OBJ_ATTRIBUTE)
13873     return Tag_conformance;
13874   if (num == LEAST_KNOWN_OBJ_ATTRIBUTE + 1)
13875     return Tag_nodefaults;
13876   if ((num - 2) < Tag_nodefaults)
13877     return num - 2;
13878   if ((num - 1) < Tag_conformance)
13879     return num - 1;
13880   return num;
13881 }
13882
13883 /* Attribute numbers >=64 (mod 128) can be safely ignored.  */
13884 static bfd_boolean
13885 elf32_arm_obj_attrs_handle_unknown (bfd *abfd, int tag)
13886 {
13887   if ((tag & 127) < 64)
13888     {
13889       _bfd_error_handler
13890         (_("%pB: unknown mandatory EABI object attribute %d"),
13891          abfd, tag);
13892       bfd_set_error (bfd_error_bad_value);
13893       return FALSE;
13894     }
13895   else
13896     {
13897       _bfd_error_handler
13898         (_("warning: %pB: unknown EABI object attribute %d"),
13899          abfd, tag);
13900       return TRUE;
13901     }
13902 }
13903
13904 /* Read the architecture from the Tag_also_compatible_with attribute, if any.
13905    Returns -1 if no architecture could be read.  */
13906
13907 static int
13908 get_secondary_compatible_arch (bfd *abfd)
13909 {
13910   obj_attribute *attr =
13911     &elf_known_obj_attributes_proc (abfd)[Tag_also_compatible_with];
13912
13913   /* Note: the tag and its argument below are uleb128 values, though
13914      currently-defined values fit in one byte for each.  */
13915   if (attr->s
13916       && attr->s[0] == Tag_CPU_arch
13917       && (attr->s[1] & 128) != 128
13918       && attr->s[2] == 0)
13919    return attr->s[1];
13920
13921   /* This tag is "safely ignorable", so don't complain if it looks funny.  */
13922   return -1;
13923 }
13924
13925 /* Set, or unset, the architecture of the Tag_also_compatible_with attribute.
13926    The tag is removed if ARCH is -1.  */
13927
13928 static void
13929 set_secondary_compatible_arch (bfd *abfd, int arch)
13930 {
13931   obj_attribute *attr =
13932     &elf_known_obj_attributes_proc (abfd)[Tag_also_compatible_with];
13933
13934   if (arch == -1)
13935     {
13936       attr->s = NULL;
13937       return;
13938     }
13939
13940   /* Note: the tag and its argument below are uleb128 values, though
13941      currently-defined values fit in one byte for each.  */
13942   if (!attr->s)
13943     attr->s = (char *) bfd_alloc (abfd, 3);
13944   attr->s[0] = Tag_CPU_arch;
13945   attr->s[1] = arch;
13946   attr->s[2] = '\0';
13947 }
13948
13949 /* Combine two values for Tag_CPU_arch, taking secondary compatibility tags
13950    into account.  */
13951
13952 static int
13953 tag_cpu_arch_combine (bfd *ibfd, int oldtag, int *secondary_compat_out,
13954                       int newtag, int secondary_compat)
13955 {
13956 #define T(X) TAG_CPU_ARCH_##X
13957   int tagl, tagh, result;
13958   const int v6t2[] =
13959     {
13960       T(V6T2),   /* PRE_V4.  */
13961       T(V6T2),   /* V4.  */
13962       T(V6T2),   /* V4T.  */
13963       T(V6T2),   /* V5T.  */
13964       T(V6T2),   /* V5TE.  */
13965       T(V6T2),   /* V5TEJ.  */
13966       T(V6T2),   /* V6.  */
13967       T(V7),     /* V6KZ.  */
13968       T(V6T2)    /* V6T2.  */
13969     };
13970   const int v6k[] =
13971     {
13972       T(V6K),    /* PRE_V4.  */
13973       T(V6K),    /* V4.  */
13974       T(V6K),    /* V4T.  */
13975       T(V6K),    /* V5T.  */
13976       T(V6K),    /* V5TE.  */
13977       T(V6K),    /* V5TEJ.  */
13978       T(V6K),    /* V6.  */
13979       T(V6KZ),   /* V6KZ.  */
13980       T(V7),     /* V6T2.  */
13981       T(V6K)     /* V6K.  */
13982     };
13983   const int v7[] =
13984     {
13985       T(V7),     /* PRE_V4.  */
13986       T(V7),     /* V4.  */
13987       T(V7),     /* V4T.  */
13988       T(V7),     /* V5T.  */
13989       T(V7),     /* V5TE.  */
13990       T(V7),     /* V5TEJ.  */
13991       T(V7),     /* V6.  */
13992       T(V7),     /* V6KZ.  */
13993       T(V7),     /* V6T2.  */
13994       T(V7),     /* V6K.  */
13995       T(V7)      /* V7.  */
13996     };
13997   const int v6_m[] =
13998     {
13999       -1,        /* PRE_V4.  */
14000       -1,        /* V4.  */
14001       T(V6K),    /* V4T.  */
14002       T(V6K),    /* V5T.  */
14003       T(V6K),    /* V5TE.  */
14004       T(V6K),    /* V5TEJ.  */
14005       T(V6K),    /* V6.  */
14006       T(V6KZ),   /* V6KZ.  */
14007       T(V7),     /* V6T2.  */
14008       T(V6K),    /* V6K.  */
14009       T(V7),     /* V7.  */
14010       T(V6_M)    /* V6_M.  */
14011     };
14012   const int v6s_m[] =
14013     {
14014       -1,        /* PRE_V4.  */
14015       -1,        /* V4.  */
14016       T(V6K),    /* V4T.  */
14017       T(V6K),    /* V5T.  */
14018       T(V6K),    /* V5TE.  */
14019       T(V6K),    /* V5TEJ.  */
14020       T(V6K),    /* V6.  */
14021       T(V6KZ),   /* V6KZ.  */
14022       T(V7),     /* V6T2.  */
14023       T(V6K),    /* V6K.  */
14024       T(V7),     /* V7.  */
14025       T(V6S_M),  /* V6_M.  */
14026       T(V6S_M)   /* V6S_M.  */
14027     };
14028   const int v7e_m[] =
14029     {
14030       -1,        /* PRE_V4.  */
14031       -1,        /* V4.  */
14032       T(V7E_M),  /* V4T.  */
14033       T(V7E_M),  /* V5T.  */
14034       T(V7E_M),  /* V5TE.  */
14035       T(V7E_M),  /* V5TEJ.  */
14036       T(V7E_M),  /* V6.  */
14037       T(V7E_M),  /* V6KZ.  */
14038       T(V7E_M),  /* V6T2.  */
14039       T(V7E_M),  /* V6K.  */
14040       T(V7E_M),  /* V7.  */
14041       T(V7E_M),  /* V6_M.  */
14042       T(V7E_M),  /* V6S_M.  */
14043       T(V7E_M)   /* V7E_M.  */
14044     };
14045   const int v8[] =
14046     {
14047       T(V8),            /* PRE_V4.  */
14048       T(V8),            /* V4.  */
14049       T(V8),            /* V4T.  */
14050       T(V8),            /* V5T.  */
14051       T(V8),            /* V5TE.  */
14052       T(V8),            /* V5TEJ.  */
14053       T(V8),            /* V6.  */
14054       T(V8),            /* V6KZ.  */
14055       T(V8),            /* V6T2.  */
14056       T(V8),            /* V6K.  */
14057       T(V8),            /* V7.  */
14058       T(V8),            /* V6_M.  */
14059       T(V8),            /* V6S_M.  */
14060       T(V8),            /* V7E_M.  */
14061       T(V8)             /* V8.  */
14062     };
14063   const int v8r[] =
14064     {
14065       T(V8R),           /* PRE_V4.  */
14066       T(V8R),           /* V4.  */
14067       T(V8R),           /* V4T.  */
14068       T(V8R),           /* V5T.  */
14069       T(V8R),           /* V5TE.  */
14070       T(V8R),           /* V5TEJ.  */
14071       T(V8R),           /* V6.  */
14072       T(V8R),           /* V6KZ.  */
14073       T(V8R),           /* V6T2.  */
14074       T(V8R),           /* V6K.  */
14075       T(V8R),           /* V7.  */
14076       T(V8R),           /* V6_M.  */
14077       T(V8R),           /* V6S_M.  */
14078       T(V8R),           /* V7E_M.  */
14079       T(V8),            /* V8.  */
14080       T(V8R),           /* V8R.  */
14081     };
14082   const int v8m_baseline[] =
14083     {
14084       -1,               /* PRE_V4.  */
14085       -1,               /* V4.  */
14086       -1,               /* V4T.  */
14087       -1,               /* V5T.  */
14088       -1,               /* V5TE.  */
14089       -1,               /* V5TEJ.  */
14090       -1,               /* V6.  */
14091       -1,               /* V6KZ.  */
14092       -1,               /* V6T2.  */
14093       -1,               /* V6K.  */
14094       -1,               /* V7.  */
14095       T(V8M_BASE),      /* V6_M.  */
14096       T(V8M_BASE),      /* V6S_M.  */
14097       -1,               /* V7E_M.  */
14098       -1,               /* V8.  */
14099       -1,               /* V8R.  */
14100       T(V8M_BASE)       /* V8-M BASELINE.  */
14101     };
14102   const int v8m_mainline[] =
14103     {
14104       -1,               /* PRE_V4.  */
14105       -1,               /* V4.  */
14106       -1,               /* V4T.  */
14107       -1,               /* V5T.  */
14108       -1,               /* V5TE.  */
14109       -1,               /* V5TEJ.  */
14110       -1,               /* V6.  */
14111       -1,               /* V6KZ.  */
14112       -1,               /* V6T2.  */
14113       -1,               /* V6K.  */
14114       T(V8M_MAIN),      /* V7.  */
14115       T(V8M_MAIN),      /* V6_M.  */
14116       T(V8M_MAIN),      /* V6S_M.  */
14117       T(V8M_MAIN),      /* V7E_M.  */
14118       -1,               /* V8.  */
14119       -1,               /* V8R.  */
14120       T(V8M_MAIN),      /* V8-M BASELINE.  */
14121       T(V8M_MAIN)       /* V8-M MAINLINE.  */
14122     };
14123   const int v4t_plus_v6_m[] =
14124     {
14125       -1,               /* PRE_V4.  */
14126       -1,               /* V4.  */
14127       T(V4T),           /* V4T.  */
14128       T(V5T),           /* V5T.  */
14129       T(V5TE),          /* V5TE.  */
14130       T(V5TEJ),         /* V5TEJ.  */
14131       T(V6),            /* V6.  */
14132       T(V6KZ),          /* V6KZ.  */
14133       T(V6T2),          /* V6T2.  */
14134       T(V6K),           /* V6K.  */
14135       T(V7),            /* V7.  */
14136       T(V6_M),          /* V6_M.  */
14137       T(V6S_M),         /* V6S_M.  */
14138       T(V7E_M),         /* V7E_M.  */
14139       T(V8),            /* V8.  */
14140       -1,               /* V8R.  */
14141       T(V8M_BASE),      /* V8-M BASELINE.  */
14142       T(V8M_MAIN),      /* V8-M MAINLINE.  */
14143       T(V4T_PLUS_V6_M)  /* V4T plus V6_M.  */
14144     };
14145   const int *comb[] =
14146     {
14147       v6t2,
14148       v6k,
14149       v7,
14150       v6_m,
14151       v6s_m,
14152       v7e_m,
14153       v8,
14154       v8r,
14155       v8m_baseline,
14156       v8m_mainline,
14157       /* Pseudo-architecture.  */
14158       v4t_plus_v6_m
14159     };
14160
14161   /* Check we've not got a higher architecture than we know about.  */
14162
14163   if (oldtag > MAX_TAG_CPU_ARCH || newtag > MAX_TAG_CPU_ARCH)
14164     {
14165       _bfd_error_handler (_("error: %pB: unknown CPU architecture"), ibfd);
14166       return -1;
14167     }
14168
14169   /* Override old tag if we have a Tag_also_compatible_with on the output.  */
14170
14171   if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
14172       || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
14173     oldtag = T(V4T_PLUS_V6_M);
14174
14175   /* And override the new tag if we have a Tag_also_compatible_with on the
14176      input.  */
14177
14178   if ((newtag == T(V6_M) && secondary_compat == T(V4T))
14179       || (newtag == T(V4T) && secondary_compat == T(V6_M)))
14180     newtag = T(V4T_PLUS_V6_M);
14181
14182   tagl = (oldtag < newtag) ? oldtag : newtag;
14183   result = tagh = (oldtag > newtag) ? oldtag : newtag;
14184
14185   /* Architectures before V6KZ add features monotonically.  */
14186   if (tagh <= TAG_CPU_ARCH_V6KZ)
14187     return result;
14188
14189   result = comb[tagh - T(V6T2)] ? comb[tagh - T(V6T2)][tagl] : -1;
14190
14191   /* Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
14192      as the canonical version.  */
14193   if (result == T(V4T_PLUS_V6_M))
14194     {
14195       result = T(V4T);
14196       *secondary_compat_out = T(V6_M);
14197     }
14198   else
14199     *secondary_compat_out = -1;
14200
14201   if (result == -1)
14202     {
14203       _bfd_error_handler (_("error: %pB: conflicting CPU architectures %d/%d"),
14204                           ibfd, oldtag, newtag);
14205       return -1;
14206     }
14207
14208   return result;
14209 #undef T
14210 }
14211
14212 /* Query attributes object to see if integer divide instructions may be
14213    present in an object.  */
14214 static bfd_boolean
14215 elf32_arm_attributes_accept_div (const obj_attribute *attr)
14216 {
14217   int arch = attr[Tag_CPU_arch].i;
14218   int profile = attr[Tag_CPU_arch_profile].i;
14219
14220   switch (attr[Tag_DIV_use].i)
14221     {
14222     case 0:
14223       /* Integer divide allowed if instruction contained in archetecture.  */
14224       if (arch == TAG_CPU_ARCH_V7 && (profile == 'R' || profile == 'M'))
14225         return TRUE;
14226       else if (arch >= TAG_CPU_ARCH_V7E_M)
14227         return TRUE;
14228       else
14229         return FALSE;
14230
14231     case 1:
14232       /* Integer divide explicitly prohibited.  */
14233       return FALSE;
14234
14235     default:
14236       /* Unrecognised case - treat as allowing divide everywhere.  */
14237     case 2:
14238       /* Integer divide allowed in ARM state.  */
14239       return TRUE;
14240     }
14241 }
14242
14243 /* Query attributes object to see if integer divide instructions are
14244    forbidden to be in the object.  This is not the inverse of
14245    elf32_arm_attributes_accept_div.  */
14246 static bfd_boolean
14247 elf32_arm_attributes_forbid_div (const obj_attribute *attr)
14248 {
14249   return attr[Tag_DIV_use].i == 1;
14250 }
14251
14252 /* Merge EABI object attributes from IBFD into OBFD.  Raise an error if there
14253    are conflicting attributes.  */
14254
14255 static bfd_boolean
14256 elf32_arm_merge_eabi_attributes (bfd *ibfd, struct bfd_link_info *info)
14257 {
14258   bfd *obfd = info->output_bfd;
14259   obj_attribute *in_attr;
14260   obj_attribute *out_attr;
14261   /* Some tags have 0 = don't care, 1 = strong requirement,
14262      2 = weak requirement.  */
14263   static const int order_021[3] = {0, 2, 1};
14264   int i;
14265   bfd_boolean result = TRUE;
14266   const char *sec_name = get_elf_backend_data (ibfd)->obj_attrs_section;
14267
14268   /* Skip the linker stubs file.  This preserves previous behavior
14269      of accepting unknown attributes in the first input file - but
14270      is that a bug?  */
14271   if (ibfd->flags & BFD_LINKER_CREATED)
14272     return TRUE;
14273
14274   /* Skip any input that hasn't attribute section.
14275      This enables to link object files without attribute section with
14276      any others.  */
14277   if (bfd_get_section_by_name (ibfd, sec_name) == NULL)
14278     return TRUE;
14279
14280   if (!elf_known_obj_attributes_proc (obfd)[0].i)
14281     {
14282       /* This is the first object.  Copy the attributes.  */
14283       _bfd_elf_copy_obj_attributes (ibfd, obfd);
14284
14285       out_attr = elf_known_obj_attributes_proc (obfd);
14286
14287       /* Use the Tag_null value to indicate the attributes have been
14288          initialized.  */
14289       out_attr[0].i = 1;
14290
14291       /* We do not output objects with Tag_MPextension_use_legacy - we move
14292          the attribute's value to Tag_MPextension_use.  */
14293       if (out_attr[Tag_MPextension_use_legacy].i != 0)
14294         {
14295           if (out_attr[Tag_MPextension_use].i != 0
14296               && out_attr[Tag_MPextension_use_legacy].i
14297                 != out_attr[Tag_MPextension_use].i)
14298             {
14299               _bfd_error_handler
14300                 (_("Error: %pB has both the current and legacy "
14301                    "Tag_MPextension_use attributes"), ibfd);
14302               result = FALSE;
14303             }
14304
14305           out_attr[Tag_MPextension_use] =
14306             out_attr[Tag_MPextension_use_legacy];
14307           out_attr[Tag_MPextension_use_legacy].type = 0;
14308           out_attr[Tag_MPextension_use_legacy].i = 0;
14309         }
14310
14311       return result;
14312     }
14313
14314   in_attr = elf_known_obj_attributes_proc (ibfd);
14315   out_attr = elf_known_obj_attributes_proc (obfd);
14316   /* This needs to happen before Tag_ABI_FP_number_model is merged.  */
14317   if (in_attr[Tag_ABI_VFP_args].i != out_attr[Tag_ABI_VFP_args].i)
14318     {
14319       /* Ignore mismatches if the object doesn't use floating point or is
14320          floating point ABI independent.  */
14321       if (out_attr[Tag_ABI_FP_number_model].i == AEABI_FP_number_model_none
14322           || (in_attr[Tag_ABI_FP_number_model].i != AEABI_FP_number_model_none
14323               && out_attr[Tag_ABI_VFP_args].i == AEABI_VFP_args_compatible))
14324         out_attr[Tag_ABI_VFP_args].i = in_attr[Tag_ABI_VFP_args].i;
14325       else if (in_attr[Tag_ABI_FP_number_model].i != AEABI_FP_number_model_none
14326                && in_attr[Tag_ABI_VFP_args].i != AEABI_VFP_args_compatible)
14327         {
14328           _bfd_error_handler
14329             (_("error: %pB uses VFP register arguments, %pB does not"),
14330              in_attr[Tag_ABI_VFP_args].i ? ibfd : obfd,
14331              in_attr[Tag_ABI_VFP_args].i ? obfd : ibfd);
14332           result = FALSE;
14333         }
14334     }
14335
14336   for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
14337     {
14338       /* Merge this attribute with existing attributes.  */
14339       switch (i)
14340         {
14341         case Tag_CPU_raw_name:
14342         case Tag_CPU_name:
14343           /* These are merged after Tag_CPU_arch.  */
14344           break;
14345
14346         case Tag_ABI_optimization_goals:
14347         case Tag_ABI_FP_optimization_goals:
14348           /* Use the first value seen.  */
14349           break;
14350
14351         case Tag_CPU_arch:
14352           {
14353             int secondary_compat = -1, secondary_compat_out = -1;
14354             unsigned int saved_out_attr = out_attr[i].i;
14355             int arch_attr;
14356             static const char *name_table[] =
14357               {
14358                 /* These aren't real CPU names, but we can't guess
14359                    that from the architecture version alone.  */
14360                 "Pre v4",
14361                 "ARM v4",
14362                 "ARM v4T",
14363                 "ARM v5T",
14364                 "ARM v5TE",
14365                 "ARM v5TEJ",
14366                 "ARM v6",
14367                 "ARM v6KZ",
14368                 "ARM v6T2",
14369                 "ARM v6K",
14370                 "ARM v7",
14371                 "ARM v6-M",
14372                 "ARM v6S-M",
14373                 "ARM v8",
14374                 "",
14375                 "ARM v8-M.baseline",
14376                 "ARM v8-M.mainline",
14377             };
14378
14379             /* Merge Tag_CPU_arch and Tag_also_compatible_with.  */
14380             secondary_compat = get_secondary_compatible_arch (ibfd);
14381             secondary_compat_out = get_secondary_compatible_arch (obfd);
14382             arch_attr = tag_cpu_arch_combine (ibfd, out_attr[i].i,
14383                                               &secondary_compat_out,
14384                                               in_attr[i].i,
14385                                               secondary_compat);
14386
14387             /* Return with error if failed to merge.  */
14388             if (arch_attr == -1)
14389               return FALSE;
14390
14391             out_attr[i].i = arch_attr;
14392
14393             set_secondary_compatible_arch (obfd, secondary_compat_out);
14394
14395             /* Merge Tag_CPU_name and Tag_CPU_raw_name.  */
14396             if (out_attr[i].i == saved_out_attr)
14397               ; /* Leave the names alone.  */
14398             else if (out_attr[i].i == in_attr[i].i)
14399               {
14400                 /* The output architecture has been changed to match the
14401                    input architecture.  Use the input names.  */
14402                 out_attr[Tag_CPU_name].s = in_attr[Tag_CPU_name].s
14403                   ? _bfd_elf_attr_strdup (obfd, in_attr[Tag_CPU_name].s)
14404                   : NULL;
14405                 out_attr[Tag_CPU_raw_name].s = in_attr[Tag_CPU_raw_name].s
14406                   ? _bfd_elf_attr_strdup (obfd, in_attr[Tag_CPU_raw_name].s)
14407                   : NULL;
14408               }
14409             else
14410               {
14411                 out_attr[Tag_CPU_name].s = NULL;
14412                 out_attr[Tag_CPU_raw_name].s = NULL;
14413               }
14414
14415             /* If we still don't have a value for Tag_CPU_name,
14416                make one up now.  Tag_CPU_raw_name remains blank.  */
14417             if (out_attr[Tag_CPU_name].s == NULL
14418                 && out_attr[i].i < ARRAY_SIZE (name_table))
14419               out_attr[Tag_CPU_name].s =
14420                 _bfd_elf_attr_strdup (obfd, name_table[out_attr[i].i]);
14421           }
14422           break;
14423
14424         case Tag_ARM_ISA_use:
14425         case Tag_THUMB_ISA_use:
14426         case Tag_WMMX_arch:
14427         case Tag_Advanced_SIMD_arch:
14428           /* ??? Do Advanced_SIMD (NEON) and WMMX conflict?  */
14429         case Tag_ABI_FP_rounding:
14430         case Tag_ABI_FP_exceptions:
14431         case Tag_ABI_FP_user_exceptions:
14432         case Tag_ABI_FP_number_model:
14433         case Tag_FP_HP_extension:
14434         case Tag_CPU_unaligned_access:
14435         case Tag_T2EE_use:
14436         case Tag_MPextension_use:
14437           /* Use the largest value specified.  */
14438           if (in_attr[i].i > out_attr[i].i)
14439             out_attr[i].i = in_attr[i].i;
14440           break;
14441
14442         case Tag_ABI_align_preserved:
14443         case Tag_ABI_PCS_RO_data:
14444           /* Use the smallest value specified.  */
14445           if (in_attr[i].i < out_attr[i].i)
14446             out_attr[i].i = in_attr[i].i;
14447           break;
14448
14449         case Tag_ABI_align_needed:
14450           if ((in_attr[i].i > 0 || out_attr[i].i > 0)
14451               && (in_attr[Tag_ABI_align_preserved].i == 0
14452                   || out_attr[Tag_ABI_align_preserved].i == 0))
14453             {
14454               /* This error message should be enabled once all non-conformant
14455                  binaries in the toolchain have had the attributes set
14456                  properly.
14457               _bfd_error_handler
14458                 (_("error: %pB: 8-byte data alignment conflicts with %pB"),
14459                  obfd, ibfd);
14460               result = FALSE; */
14461             }
14462           /* Fall through.  */
14463         case Tag_ABI_FP_denormal:
14464         case Tag_ABI_PCS_GOT_use:
14465           /* Use the "greatest" from the sequence 0, 2, 1, or the largest
14466              value if greater than 2 (for future-proofing).  */
14467           if ((in_attr[i].i > 2 && in_attr[i].i > out_attr[i].i)
14468               || (in_attr[i].i <= 2 && out_attr[i].i <= 2
14469                   && order_021[in_attr[i].i] > order_021[out_attr[i].i]))
14470             out_attr[i].i = in_attr[i].i;
14471           break;
14472
14473         case Tag_Virtualization_use:
14474           /* The virtualization tag effectively stores two bits of
14475              information: the intended use of TrustZone (in bit 0), and the
14476              intended use of Virtualization (in bit 1).  */
14477           if (out_attr[i].i == 0)
14478             out_attr[i].i = in_attr[i].i;
14479           else if (in_attr[i].i != 0
14480                    && in_attr[i].i != out_attr[i].i)
14481             {
14482               if (in_attr[i].i <= 3 && out_attr[i].i <= 3)
14483                 out_attr[i].i = 3;
14484               else
14485                 {
14486                   _bfd_error_handler
14487                     (_("error: %pB: unable to merge virtualization attributes "
14488                        "with %pB"),
14489                      obfd, ibfd);
14490                   result = FALSE;
14491                 }
14492             }
14493           break;
14494
14495         case Tag_CPU_arch_profile:
14496           if (out_attr[i].i != in_attr[i].i)
14497             {
14498               /* 0 will merge with anything.
14499                  'A' and 'S' merge to 'A'.
14500                  'R' and 'S' merge to 'R'.
14501                  'M' and 'A|R|S' is an error.  */
14502               if (out_attr[i].i == 0
14503                   || (out_attr[i].i == 'S'
14504                       && (in_attr[i].i == 'A' || in_attr[i].i == 'R')))
14505                 out_attr[i].i = in_attr[i].i;
14506               else if (in_attr[i].i == 0
14507                        || (in_attr[i].i == 'S'
14508                            && (out_attr[i].i == 'A' || out_attr[i].i == 'R')))
14509                 ; /* Do nothing.  */
14510               else
14511                 {
14512                   _bfd_error_handler
14513                     (_("error: %pB: conflicting architecture profiles %c/%c"),
14514                      ibfd,
14515                      in_attr[i].i ? in_attr[i].i : '0',
14516                      out_attr[i].i ? out_attr[i].i : '0');
14517                   result = FALSE;
14518                 }
14519             }
14520           break;
14521
14522         case Tag_DSP_extension:
14523           /* No need to change output value if any of:
14524              - pre (<=) ARMv5T input architecture (do not have DSP)
14525              - M input profile not ARMv7E-M and do not have DSP.  */
14526           if (in_attr[Tag_CPU_arch].i <= 3
14527               || (in_attr[Tag_CPU_arch_profile].i == 'M'
14528                   && in_attr[Tag_CPU_arch].i != 13
14529                   && in_attr[i].i == 0))
14530             ; /* Do nothing.  */
14531           /* Output value should be 0 if DSP part of architecture, ie.
14532              - post (>=) ARMv5te architecture output
14533              - A, R or S profile output or ARMv7E-M output architecture.  */
14534           else if (out_attr[Tag_CPU_arch].i >= 4
14535                    && (out_attr[Tag_CPU_arch_profile].i == 'A'
14536                        || out_attr[Tag_CPU_arch_profile].i == 'R'
14537                        || out_attr[Tag_CPU_arch_profile].i == 'S'
14538                        || out_attr[Tag_CPU_arch].i == 13))
14539             out_attr[i].i = 0;
14540           /* Otherwise, DSP instructions are added and not part of output
14541              architecture.  */
14542           else
14543             out_attr[i].i = 1;
14544           break;
14545
14546         case Tag_FP_arch:
14547             {
14548               /* Tag_ABI_HardFP_use is handled along with Tag_FP_arch since
14549                  the meaning of Tag_ABI_HardFP_use depends on Tag_FP_arch
14550                  when it's 0.  It might mean absence of FP hardware if
14551                  Tag_FP_arch is zero.  */
14552
14553 #define VFP_VERSION_COUNT 9
14554               static const struct
14555               {
14556                   int ver;
14557                   int regs;
14558               } vfp_versions[VFP_VERSION_COUNT] =
14559                 {
14560                   {0, 0},
14561                   {1, 16},
14562                   {2, 16},
14563                   {3, 32},
14564                   {3, 16},
14565                   {4, 32},
14566                   {4, 16},
14567                   {8, 32},
14568                   {8, 16}
14569                 };
14570               int ver;
14571               int regs;
14572               int newval;
14573
14574               /* If the output has no requirement about FP hardware,
14575                  follow the requirement of the input.  */
14576               if (out_attr[i].i == 0)
14577                 {
14578                   /* This assert is still reasonable, we shouldn't
14579                      produce the suspicious build attribute
14580                      combination (See below for in_attr).  */
14581                   BFD_ASSERT (out_attr[Tag_ABI_HardFP_use].i == 0);
14582                   out_attr[i].i = in_attr[i].i;
14583                   out_attr[Tag_ABI_HardFP_use].i
14584                     = in_attr[Tag_ABI_HardFP_use].i;
14585                   break;
14586                 }
14587               /* If the input has no requirement about FP hardware, do
14588                  nothing.  */
14589               else if (in_attr[i].i == 0)
14590                 {
14591                   /* We used to assert that Tag_ABI_HardFP_use was
14592                      zero here, but we should never assert when
14593                      consuming an object file that has suspicious
14594                      build attributes.  The single precision variant
14595                      of 'no FP architecture' is still 'no FP
14596                      architecture', so we just ignore the tag in this
14597                      case.  */
14598                   break;
14599                 }
14600
14601               /* Both the input and the output have nonzero Tag_FP_arch.
14602                  So Tag_ABI_HardFP_use is implied by Tag_FP_arch when it's zero.  */
14603
14604               /* If both the input and the output have zero Tag_ABI_HardFP_use,
14605                  do nothing.  */
14606               if (in_attr[Tag_ABI_HardFP_use].i == 0
14607                   && out_attr[Tag_ABI_HardFP_use].i == 0)
14608                 ;
14609               /* If the input and the output have different Tag_ABI_HardFP_use,
14610                  the combination of them is 0 (implied by Tag_FP_arch).  */
14611               else if (in_attr[Tag_ABI_HardFP_use].i
14612                        != out_attr[Tag_ABI_HardFP_use].i)
14613                 out_attr[Tag_ABI_HardFP_use].i = 0;
14614
14615               /* Now we can handle Tag_FP_arch.  */
14616
14617               /* Values of VFP_VERSION_COUNT or more aren't defined, so just
14618                  pick the biggest.  */
14619               if (in_attr[i].i >= VFP_VERSION_COUNT
14620                   && in_attr[i].i > out_attr[i].i)
14621                 {
14622                   out_attr[i] = in_attr[i];
14623                   break;
14624                 }
14625               /* The output uses the superset of input features
14626                  (ISA version) and registers.  */
14627               ver = vfp_versions[in_attr[i].i].ver;
14628               if (ver < vfp_versions[out_attr[i].i].ver)
14629                 ver = vfp_versions[out_attr[i].i].ver;
14630               regs = vfp_versions[in_attr[i].i].regs;
14631               if (regs < vfp_versions[out_attr[i].i].regs)
14632                 regs = vfp_versions[out_attr[i].i].regs;
14633               /* This assumes all possible supersets are also a valid
14634                  options.  */
14635               for (newval = VFP_VERSION_COUNT - 1; newval > 0; newval--)
14636                 {
14637                   if (regs == vfp_versions[newval].regs
14638                       && ver == vfp_versions[newval].ver)
14639                     break;
14640                 }
14641               out_attr[i].i = newval;
14642             }
14643           break;
14644         case Tag_PCS_config:
14645           if (out_attr[i].i == 0)
14646             out_attr[i].i = in_attr[i].i;
14647           else if (in_attr[i].i != 0 && out_attr[i].i != in_attr[i].i)
14648             {
14649               /* It's sometimes ok to mix different configs, so this is only
14650                  a warning.  */
14651               _bfd_error_handler
14652                 (_("warning: %pB: conflicting platform configuration"), ibfd);
14653             }
14654           break;
14655         case Tag_ABI_PCS_R9_use:
14656           if (in_attr[i].i != out_attr[i].i
14657               && out_attr[i].i != AEABI_R9_unused
14658               && in_attr[i].i != AEABI_R9_unused)
14659             {
14660               _bfd_error_handler
14661                 (_("error: %pB: conflicting use of R9"), ibfd);
14662               result = FALSE;
14663             }
14664           if (out_attr[i].i == AEABI_R9_unused)
14665             out_attr[i].i = in_attr[i].i;
14666           break;
14667         case Tag_ABI_PCS_RW_data:
14668           if (in_attr[i].i == AEABI_PCS_RW_data_SBrel
14669               && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_SB
14670               && out_attr[Tag_ABI_PCS_R9_use].i != AEABI_R9_unused)
14671             {
14672               _bfd_error_handler
14673                 (_("error: %pB: SB relative addressing conflicts with use of R9"),
14674                  ibfd);
14675               result = FALSE;
14676             }
14677           /* Use the smallest value specified.  */
14678           if (in_attr[i].i < out_attr[i].i)
14679             out_attr[i].i = in_attr[i].i;
14680           break;
14681         case Tag_ABI_PCS_wchar_t:
14682           if (out_attr[i].i && in_attr[i].i && out_attr[i].i != in_attr[i].i
14683               && !elf_arm_tdata (obfd)->no_wchar_size_warning)
14684             {
14685               _bfd_error_handler
14686                 (_("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"),
14687                  ibfd, in_attr[i].i, out_attr[i].i);
14688             }
14689           else if (in_attr[i].i && !out_attr[i].i)
14690             out_attr[i].i = in_attr[i].i;
14691           break;
14692         case Tag_ABI_enum_size:
14693           if (in_attr[i].i != AEABI_enum_unused)
14694             {
14695               if (out_attr[i].i == AEABI_enum_unused
14696                   || out_attr[i].i == AEABI_enum_forced_wide)
14697                 {
14698                   /* The existing object is compatible with anything.
14699                      Use whatever requirements the new object has.  */
14700                   out_attr[i].i = in_attr[i].i;
14701                 }
14702               else if (in_attr[i].i != AEABI_enum_forced_wide
14703                        && out_attr[i].i != in_attr[i].i
14704                        && !elf_arm_tdata (obfd)->no_enum_size_warning)
14705                 {
14706                   static const char *aeabi_enum_names[] =
14707                     { "", "variable-size", "32-bit", "" };
14708                   const char *in_name =
14709                     in_attr[i].i < ARRAY_SIZE(aeabi_enum_names)
14710                     ? aeabi_enum_names[in_attr[i].i]
14711                     : "<unknown>";
14712                   const char *out_name =
14713                     out_attr[i].i < ARRAY_SIZE(aeabi_enum_names)
14714                     ? aeabi_enum_names[out_attr[i].i]
14715                     : "<unknown>";
14716                   _bfd_error_handler
14717                     (_("warning: %pB uses %s enums yet the output is to use %s enums; use of enum values across objects may fail"),
14718                      ibfd, in_name, out_name);
14719                 }
14720             }
14721           break;
14722         case Tag_ABI_VFP_args:
14723           /* Aready done.  */
14724           break;
14725         case Tag_ABI_WMMX_args:
14726           if (in_attr[i].i != out_attr[i].i)
14727             {
14728               _bfd_error_handler
14729                 (_("error: %pB uses iWMMXt register arguments, %pB does not"),
14730                  ibfd, obfd);
14731               result = FALSE;
14732             }
14733           break;
14734         case Tag_compatibility:
14735           /* Merged in target-independent code.  */
14736           break;
14737         case Tag_ABI_HardFP_use:
14738           /* This is handled along with Tag_FP_arch.  */
14739           break;
14740         case Tag_ABI_FP_16bit_format:
14741           if (in_attr[i].i != 0 && out_attr[i].i != 0)
14742             {
14743               if (in_attr[i].i != out_attr[i].i)
14744                 {
14745                   _bfd_error_handler
14746                     (_("error: fp16 format mismatch between %pB and %pB"),
14747                      ibfd, obfd);
14748                   result = FALSE;
14749                 }
14750             }
14751           if (in_attr[i].i != 0)
14752             out_attr[i].i = in_attr[i].i;
14753           break;
14754
14755         case Tag_DIV_use:
14756           /* A value of zero on input means that the divide instruction may
14757              be used if available in the base architecture as specified via
14758              Tag_CPU_arch and Tag_CPU_arch_profile.  A value of 1 means that
14759              the user did not want divide instructions.  A value of 2
14760              explicitly means that divide instructions were allowed in ARM
14761              and Thumb state.  */
14762           if (in_attr[i].i == out_attr[i].i)
14763             /* Do nothing.  */ ;
14764           else if (elf32_arm_attributes_forbid_div (in_attr)
14765                    && !elf32_arm_attributes_accept_div (out_attr))
14766             out_attr[i].i = 1;
14767           else if (elf32_arm_attributes_forbid_div (out_attr)
14768                    && elf32_arm_attributes_accept_div (in_attr))
14769             out_attr[i].i = in_attr[i].i;
14770           else if (in_attr[i].i == 2)
14771             out_attr[i].i = in_attr[i].i;
14772           break;
14773
14774         case Tag_MPextension_use_legacy:
14775           /* We don't output objects with Tag_MPextension_use_legacy - we
14776              move the value to Tag_MPextension_use.  */
14777           if (in_attr[i].i != 0 && in_attr[Tag_MPextension_use].i != 0)
14778             {
14779               if (in_attr[Tag_MPextension_use].i != in_attr[i].i)
14780                 {
14781                   _bfd_error_handler
14782                     (_("%pB has both the current and legacy "
14783                        "Tag_MPextension_use attributes"),
14784                      ibfd);
14785                   result = FALSE;
14786                 }
14787             }
14788
14789           if (in_attr[i].i > out_attr[Tag_MPextension_use].i)
14790             out_attr[Tag_MPextension_use] = in_attr[i];
14791
14792           break;
14793
14794         case Tag_nodefaults:
14795           /* This tag is set if it exists, but the value is unused (and is
14796              typically zero).  We don't actually need to do anything here -
14797              the merge happens automatically when the type flags are merged
14798              below.  */
14799           break;
14800         case Tag_also_compatible_with:
14801           /* Already done in Tag_CPU_arch.  */
14802           break;
14803         case Tag_conformance:
14804           /* Keep the attribute if it matches.  Throw it away otherwise.
14805              No attribute means no claim to conform.  */
14806           if (!in_attr[i].s || !out_attr[i].s
14807               || strcmp (in_attr[i].s, out_attr[i].s) != 0)
14808             out_attr[i].s = NULL;
14809           break;
14810
14811         default:
14812           result
14813             = result && _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i);
14814         }
14815
14816       /* If out_attr was copied from in_attr then it won't have a type yet.  */
14817       if (in_attr[i].type && !out_attr[i].type)
14818         out_attr[i].type = in_attr[i].type;
14819     }
14820
14821   /* Merge Tag_compatibility attributes and any common GNU ones.  */
14822   if (!_bfd_elf_merge_object_attributes (ibfd, info))
14823     return FALSE;
14824
14825   /* Check for any attributes not known on ARM.  */
14826   result &= _bfd_elf_merge_unknown_attribute_list (ibfd, obfd);
14827
14828   return result;
14829 }
14830
14831
14832 /* Return TRUE if the two EABI versions are incompatible.  */
14833
14834 static bfd_boolean
14835 elf32_arm_versions_compatible (unsigned iver, unsigned over)
14836 {
14837   /* v4 and v5 are the same spec before and after it was released,
14838      so allow mixing them.  */
14839   if ((iver == EF_ARM_EABI_VER4 && over == EF_ARM_EABI_VER5)
14840       || (iver == EF_ARM_EABI_VER5 && over == EF_ARM_EABI_VER4))
14841     return TRUE;
14842
14843   return (iver == over);
14844 }
14845
14846 /* Merge backend specific data from an object file to the output
14847    object file when linking.  */
14848
14849 static bfd_boolean
14850 elf32_arm_merge_private_bfd_data (bfd *, struct bfd_link_info *);
14851
14852 /* Display the flags field.  */
14853
14854 static bfd_boolean
14855 elf32_arm_print_private_bfd_data (bfd *abfd, void * ptr)
14856 {
14857   FILE * file = (FILE *) ptr;
14858   unsigned long flags;
14859
14860   BFD_ASSERT (abfd != NULL && ptr != NULL);
14861
14862   /* Print normal ELF private data.  */
14863   _bfd_elf_print_private_bfd_data (abfd, ptr);
14864
14865   flags = elf_elfheader (abfd)->e_flags;
14866   /* Ignore init flag - it may not be set, despite the flags field
14867      containing valid data.  */
14868
14869   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
14870
14871   switch (EF_ARM_EABI_VERSION (flags))
14872     {
14873     case EF_ARM_EABI_UNKNOWN:
14874       /* The following flag bits are GNU extensions and not part of the
14875          official ARM ELF extended ABI.  Hence they are only decoded if
14876          the EABI version is not set.  */
14877       if (flags & EF_ARM_INTERWORK)
14878         fprintf (file, _(" [interworking enabled]"));
14879
14880       if (flags & EF_ARM_APCS_26)
14881         fprintf (file, " [APCS-26]");
14882       else
14883         fprintf (file, " [APCS-32]");
14884
14885       if (flags & EF_ARM_VFP_FLOAT)
14886         fprintf (file, _(" [VFP float format]"));
14887       else if (flags & EF_ARM_MAVERICK_FLOAT)
14888         fprintf (file, _(" [Maverick float format]"));
14889       else
14890         fprintf (file, _(" [FPA float format]"));
14891
14892       if (flags & EF_ARM_APCS_FLOAT)
14893         fprintf (file, _(" [floats passed in float registers]"));
14894
14895       if (flags & EF_ARM_PIC)
14896         fprintf (file, _(" [position independent]"));
14897
14898       if (flags & EF_ARM_NEW_ABI)
14899         fprintf (file, _(" [new ABI]"));
14900
14901       if (flags & EF_ARM_OLD_ABI)
14902         fprintf (file, _(" [old ABI]"));
14903
14904       if (flags & EF_ARM_SOFT_FLOAT)
14905         fprintf (file, _(" [software FP]"));
14906
14907       flags &= ~(EF_ARM_INTERWORK | EF_ARM_APCS_26 | EF_ARM_APCS_FLOAT
14908                  | EF_ARM_PIC | EF_ARM_NEW_ABI | EF_ARM_OLD_ABI
14909                  | EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT
14910                  | EF_ARM_MAVERICK_FLOAT);
14911       break;
14912
14913     case EF_ARM_EABI_VER1:
14914       fprintf (file, _(" [Version1 EABI]"));
14915
14916       if (flags & EF_ARM_SYMSARESORTED)
14917         fprintf (file, _(" [sorted symbol table]"));
14918       else
14919         fprintf (file, _(" [unsorted symbol table]"));
14920
14921       flags &= ~ EF_ARM_SYMSARESORTED;
14922       break;
14923
14924     case EF_ARM_EABI_VER2:
14925       fprintf (file, _(" [Version2 EABI]"));
14926
14927       if (flags & EF_ARM_SYMSARESORTED)
14928         fprintf (file, _(" [sorted symbol table]"));
14929       else
14930         fprintf (file, _(" [unsorted symbol table]"));
14931
14932       if (flags & EF_ARM_DYNSYMSUSESEGIDX)
14933         fprintf (file, _(" [dynamic symbols use segment index]"));
14934
14935       if (flags & EF_ARM_MAPSYMSFIRST)
14936         fprintf (file, _(" [mapping symbols precede others]"));
14937
14938       flags &= ~(EF_ARM_SYMSARESORTED | EF_ARM_DYNSYMSUSESEGIDX
14939                  | EF_ARM_MAPSYMSFIRST);
14940       break;
14941
14942     case EF_ARM_EABI_VER3:
14943       fprintf (file, _(" [Version3 EABI]"));
14944       break;
14945
14946     case EF_ARM_EABI_VER4:
14947       fprintf (file, _(" [Version4 EABI]"));
14948       goto eabi;
14949
14950     case EF_ARM_EABI_VER5:
14951       fprintf (file, _(" [Version5 EABI]"));
14952
14953       if (flags & EF_ARM_ABI_FLOAT_SOFT)
14954         fprintf (file, _(" [soft-float ABI]"));
14955
14956       if (flags & EF_ARM_ABI_FLOAT_HARD)
14957         fprintf (file, _(" [hard-float ABI]"));
14958
14959       flags &= ~(EF_ARM_ABI_FLOAT_SOFT | EF_ARM_ABI_FLOAT_HARD);
14960
14961     eabi:
14962       if (flags & EF_ARM_BE8)
14963         fprintf (file, _(" [BE8]"));
14964
14965       if (flags & EF_ARM_LE8)
14966         fprintf (file, _(" [LE8]"));
14967
14968       flags &= ~(EF_ARM_LE8 | EF_ARM_BE8);
14969       break;
14970
14971     default:
14972       fprintf (file, _(" <EABI version unrecognised>"));
14973       break;
14974     }
14975
14976   flags &= ~ EF_ARM_EABIMASK;
14977
14978   if (flags & EF_ARM_RELEXEC)
14979     fprintf (file, _(" [relocatable executable]"));
14980
14981   if (flags & EF_ARM_PIC)
14982     fprintf (file, _(" [position independent]"));
14983
14984   if (elf_elfheader (abfd)->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC)
14985     fprintf (file, _(" [FDPIC ABI supplement]"));
14986
14987   flags &= ~ (EF_ARM_RELEXEC | EF_ARM_PIC);
14988
14989   if (flags)
14990     fprintf (file, _("<Unrecognised flag bits set>"));
14991
14992   fputc ('\n', file);
14993
14994   return TRUE;
14995 }
14996
14997 static int
14998 elf32_arm_get_symbol_type (Elf_Internal_Sym * elf_sym, int type)
14999 {
15000   switch (ELF_ST_TYPE (elf_sym->st_info))
15001     {
15002     case STT_ARM_TFUNC:
15003       return ELF_ST_TYPE (elf_sym->st_info);
15004
15005     case STT_ARM_16BIT:
15006       /* If the symbol is not an object, return the STT_ARM_16BIT flag.
15007          This allows us to distinguish between data used by Thumb instructions
15008          and non-data (which is probably code) inside Thumb regions of an
15009          executable.  */
15010       if (type != STT_OBJECT && type != STT_TLS)
15011         return ELF_ST_TYPE (elf_sym->st_info);
15012       break;
15013
15014     default:
15015       break;
15016     }
15017
15018   return type;
15019 }
15020
15021 static asection *
15022 elf32_arm_gc_mark_hook (asection *sec,
15023                         struct bfd_link_info *info,
15024                         Elf_Internal_Rela *rel,
15025                         struct elf_link_hash_entry *h,
15026                         Elf_Internal_Sym *sym)
15027 {
15028   if (h != NULL)
15029     switch (ELF32_R_TYPE (rel->r_info))
15030       {
15031       case R_ARM_GNU_VTINHERIT:
15032       case R_ARM_GNU_VTENTRY:
15033         return NULL;
15034       }
15035
15036   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
15037 }
15038
15039 /* Look through the relocs for a section during the first phase.  */
15040
15041 static bfd_boolean
15042 elf32_arm_check_relocs (bfd *abfd, struct bfd_link_info *info,
15043                         asection *sec, const Elf_Internal_Rela *relocs)
15044 {
15045   Elf_Internal_Shdr *symtab_hdr;
15046   struct elf_link_hash_entry **sym_hashes;
15047   const Elf_Internal_Rela *rel;
15048   const Elf_Internal_Rela *rel_end;
15049   bfd *dynobj;
15050   asection *sreloc;
15051   struct elf32_arm_link_hash_table *htab;
15052   bfd_boolean call_reloc_p;
15053   bfd_boolean may_become_dynamic_p;
15054   bfd_boolean may_need_local_target_p;
15055   unsigned long nsyms;
15056
15057   if (bfd_link_relocatable (info))
15058     return TRUE;
15059
15060   BFD_ASSERT (is_arm_elf (abfd));
15061
15062   htab = elf32_arm_hash_table (info);
15063   if (htab == NULL)
15064     return FALSE;
15065
15066   sreloc = NULL;
15067
15068   /* Create dynamic sections for relocatable executables so that we can
15069      copy relocations.  */
15070   if (htab->root.is_relocatable_executable
15071       && ! htab->root.dynamic_sections_created)
15072     {
15073       if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
15074         return FALSE;
15075     }
15076
15077   if (htab->root.dynobj == NULL)
15078     htab->root.dynobj = abfd;
15079   if (!create_ifunc_sections (info))
15080     return FALSE;
15081
15082   dynobj = htab->root.dynobj;
15083
15084   symtab_hdr = & elf_symtab_hdr (abfd);
15085   sym_hashes = elf_sym_hashes (abfd);
15086   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
15087
15088   rel_end = relocs + sec->reloc_count;
15089   for (rel = relocs; rel < rel_end; rel++)
15090     {
15091       Elf_Internal_Sym *isym;
15092       struct elf_link_hash_entry *h;
15093       struct elf32_arm_link_hash_entry *eh;
15094       unsigned int r_symndx;
15095       int r_type;
15096
15097       r_symndx = ELF32_R_SYM (rel->r_info);
15098       r_type = ELF32_R_TYPE (rel->r_info);
15099       r_type = arm_real_reloc_type (htab, r_type);
15100
15101       if (r_symndx >= nsyms
15102           /* PR 9934: It is possible to have relocations that do not
15103              refer to symbols, thus it is also possible to have an
15104              object file containing relocations but no symbol table.  */
15105           && (r_symndx > STN_UNDEF || nsyms > 0))
15106         {
15107           _bfd_error_handler (_("%pB: bad symbol index: %d"), abfd,
15108                               r_symndx);
15109           return FALSE;
15110         }
15111
15112       h = NULL;
15113       isym = NULL;
15114       if (nsyms > 0)
15115         {
15116           if (r_symndx < symtab_hdr->sh_info)
15117             {
15118               /* A local symbol.  */
15119               isym = bfd_sym_from_r_symndx (&htab->sym_cache,
15120                                             abfd, r_symndx);
15121               if (isym == NULL)
15122                 return FALSE;
15123             }
15124           else
15125             {
15126               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
15127               while (h->root.type == bfd_link_hash_indirect
15128                      || h->root.type == bfd_link_hash_warning)
15129                 h = (struct elf_link_hash_entry *) h->root.u.i.link;
15130             }
15131         }
15132
15133       eh = (struct elf32_arm_link_hash_entry *) h;
15134
15135       call_reloc_p = FALSE;
15136       may_become_dynamic_p = FALSE;
15137       may_need_local_target_p = FALSE;
15138
15139       /* Could be done earlier, if h were already available.  */
15140       r_type = elf32_arm_tls_transition (info, r_type, h);
15141       switch (r_type)
15142         {
15143         case R_ARM_GOTOFFFUNCDESC:
15144           {
15145             if (h == NULL)
15146               {
15147                 if (!elf32_arm_allocate_local_sym_info (abfd))
15148                   return FALSE;
15149                 elf32_arm_local_fdpic_cnts(abfd)[r_symndx].gotofffuncdesc_cnt += 1;
15150                 elf32_arm_local_fdpic_cnts(abfd)[r_symndx].funcdesc_offset = -1;
15151               }
15152             else
15153               {
15154                 eh->fdpic_cnts.gotofffuncdesc_cnt++;
15155               }
15156           }
15157           break;
15158
15159         case R_ARM_GOTFUNCDESC:
15160           {
15161             if (h == NULL)
15162               {
15163                 /* Such a relocation is not supposed to be generated
15164                    by gcc on a static function. */
15165                 /* Anyway if needed it could be handled.  */
15166                 abort();
15167               }
15168             else
15169               {
15170                 eh->fdpic_cnts.gotfuncdesc_cnt++;
15171               }
15172           }
15173           break;
15174
15175         case R_ARM_FUNCDESC:
15176           {
15177             if (h == NULL)
15178               {
15179                 if (!elf32_arm_allocate_local_sym_info (abfd))
15180                   return FALSE;
15181                 elf32_arm_local_fdpic_cnts(abfd)[r_symndx].funcdesc_cnt += 1;
15182                 elf32_arm_local_fdpic_cnts(abfd)[r_symndx].funcdesc_offset = -1;
15183               }
15184             else
15185               {
15186                 eh->fdpic_cnts.funcdesc_cnt++;
15187               }
15188           }
15189           break;
15190
15191           case R_ARM_GOT32:
15192           case R_ARM_GOT_PREL:
15193           case R_ARM_TLS_GD32:
15194           case R_ARM_TLS_GD32_FDPIC:
15195           case R_ARM_TLS_IE32:
15196           case R_ARM_TLS_IE32_FDPIC:
15197           case R_ARM_TLS_GOTDESC:
15198           case R_ARM_TLS_DESCSEQ:
15199           case R_ARM_THM_TLS_DESCSEQ:
15200           case R_ARM_TLS_CALL:
15201           case R_ARM_THM_TLS_CALL:
15202             /* This symbol requires a global offset table entry.  */
15203             {
15204               int tls_type, old_tls_type;
15205
15206               switch (r_type)
15207                 {
15208                 case R_ARM_TLS_GD32: tls_type = GOT_TLS_GD; break;
15209                 case R_ARM_TLS_GD32_FDPIC: tls_type = GOT_TLS_GD; break;
15210
15211                 case R_ARM_TLS_IE32: tls_type = GOT_TLS_IE; break;
15212                 case R_ARM_TLS_IE32_FDPIC: tls_type = GOT_TLS_IE; break;
15213
15214                 case R_ARM_TLS_GOTDESC:
15215                 case R_ARM_TLS_CALL: case R_ARM_THM_TLS_CALL:
15216                 case R_ARM_TLS_DESCSEQ: case R_ARM_THM_TLS_DESCSEQ:
15217                   tls_type = GOT_TLS_GDESC; break;
15218
15219                 default: tls_type = GOT_NORMAL; break;
15220                 }
15221
15222               if (!bfd_link_executable (info) && (tls_type & GOT_TLS_IE))
15223                 info->flags |= DF_STATIC_TLS;
15224
15225               if (h != NULL)
15226                 {
15227                   h->got.refcount++;
15228                   old_tls_type = elf32_arm_hash_entry (h)->tls_type;
15229                 }
15230               else
15231                 {
15232                   /* This is a global offset table entry for a local symbol.  */
15233                   if (!elf32_arm_allocate_local_sym_info (abfd))
15234                     return FALSE;
15235                   elf_local_got_refcounts (abfd)[r_symndx] += 1;
15236                   old_tls_type = elf32_arm_local_got_tls_type (abfd) [r_symndx];
15237                 }
15238
15239               /* If a variable is accessed with both tls methods, two
15240                  slots may be created.  */
15241               if (GOT_TLS_GD_ANY_P (old_tls_type)
15242                   && GOT_TLS_GD_ANY_P (tls_type))
15243                 tls_type |= old_tls_type;
15244
15245               /* We will already have issued an error message if there
15246                  is a TLS/non-TLS mismatch, based on the symbol
15247                  type.  So just combine any TLS types needed.  */
15248               if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
15249                   && tls_type != GOT_NORMAL)
15250                 tls_type |= old_tls_type;
15251
15252               /* If the symbol is accessed in both IE and GDESC
15253                  method, we're able to relax. Turn off the GDESC flag,
15254                  without messing up with any other kind of tls types
15255                  that may be involved.  */
15256               if ((tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_GDESC))
15257                 tls_type &= ~GOT_TLS_GDESC;
15258
15259               if (old_tls_type != tls_type)
15260                 {
15261                   if (h != NULL)
15262                     elf32_arm_hash_entry (h)->tls_type = tls_type;
15263                   else
15264                     elf32_arm_local_got_tls_type (abfd) [r_symndx] = tls_type;
15265                 }
15266             }
15267             /* Fall through.  */
15268
15269           case R_ARM_TLS_LDM32:
15270           case R_ARM_TLS_LDM32_FDPIC:
15271             if (r_type == R_ARM_TLS_LDM32 || r_type == R_ARM_TLS_LDM32_FDPIC)
15272                 htab->tls_ldm_got.refcount++;
15273             /* Fall through.  */
15274
15275           case R_ARM_GOTOFF32:
15276           case R_ARM_GOTPC:
15277             if (htab->root.sgot == NULL
15278                 && !create_got_section (htab->root.dynobj, info))
15279               return FALSE;
15280             break;
15281
15282           case R_ARM_PC24:
15283           case R_ARM_PLT32:
15284           case R_ARM_CALL:
15285           case R_ARM_JUMP24:
15286           case R_ARM_PREL31:
15287           case R_ARM_THM_CALL:
15288           case R_ARM_THM_JUMP24:
15289           case R_ARM_THM_JUMP19:
15290             call_reloc_p = TRUE;
15291             may_need_local_target_p = TRUE;
15292             break;
15293
15294           case R_ARM_ABS12:
15295             /* VxWorks uses dynamic R_ARM_ABS12 relocations for
15296                ldr __GOTT_INDEX__ offsets.  */
15297             if (!htab->vxworks_p)
15298               {
15299                 may_need_local_target_p = TRUE;
15300                 break;
15301               }
15302             else goto jump_over;
15303
15304             /* Fall through.  */
15305
15306           case R_ARM_MOVW_ABS_NC:
15307           case R_ARM_MOVT_ABS:
15308           case R_ARM_THM_MOVW_ABS_NC:
15309           case R_ARM_THM_MOVT_ABS:
15310             if (bfd_link_pic (info))
15311               {
15312                 _bfd_error_handler
15313                   (_("%pB: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
15314                    abfd, elf32_arm_howto_table_1[r_type].name,
15315                    (h) ? h->root.root.string : "a local symbol");
15316                 bfd_set_error (bfd_error_bad_value);
15317                 return FALSE;
15318               }
15319
15320             /* Fall through.  */
15321           case R_ARM_ABS32:
15322           case R_ARM_ABS32_NOI:
15323         jump_over:
15324             if (h != NULL && bfd_link_executable (info))
15325               {
15326                 h->pointer_equality_needed = 1;
15327               }
15328             /* Fall through.  */
15329           case R_ARM_REL32:
15330           case R_ARM_REL32_NOI:
15331           case R_ARM_MOVW_PREL_NC:
15332           case R_ARM_MOVT_PREL:
15333           case R_ARM_THM_MOVW_PREL_NC:
15334           case R_ARM_THM_MOVT_PREL:
15335
15336             /* Should the interworking branches be listed here?  */
15337             if ((bfd_link_pic (info) || htab->root.is_relocatable_executable
15338                  || htab->fdpic_p)
15339                 && (sec->flags & SEC_ALLOC) != 0)
15340               {
15341                 if (h == NULL
15342                     && elf32_arm_howto_from_type (r_type)->pc_relative)
15343                   {
15344                     /* In shared libraries and relocatable executables,
15345                        we treat local relative references as calls;
15346                        see the related SYMBOL_CALLS_LOCAL code in
15347                        allocate_dynrelocs.  */
15348                     call_reloc_p = TRUE;
15349                     may_need_local_target_p = TRUE;
15350                   }
15351                 else
15352                   /* We are creating a shared library or relocatable
15353                      executable, and this is a reloc against a global symbol,
15354                      or a non-PC-relative reloc against a local symbol.
15355                      We may need to copy the reloc into the output.  */
15356                   may_become_dynamic_p = TRUE;
15357               }
15358             else
15359               may_need_local_target_p = TRUE;
15360             break;
15361
15362         /* This relocation describes the C++ object vtable hierarchy.
15363            Reconstruct it for later use during GC.  */
15364         case R_ARM_GNU_VTINHERIT:
15365           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
15366             return FALSE;
15367           break;
15368
15369         /* This relocation describes which C++ vtable entries are actually
15370            used.  Record for later use during GC.  */
15371         case R_ARM_GNU_VTENTRY:
15372           BFD_ASSERT (h != NULL);
15373           if (h != NULL
15374               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
15375             return FALSE;
15376           break;
15377         }
15378
15379       if (h != NULL)
15380         {
15381           if (call_reloc_p)
15382             /* We may need a .plt entry if the function this reloc
15383                refers to is in a different object, regardless of the
15384                symbol's type.  We can't tell for sure yet, because
15385                something later might force the symbol local.  */
15386             h->needs_plt = 1;
15387           else if (may_need_local_target_p)
15388             /* If this reloc is in a read-only section, we might
15389                need a copy reloc.  We can't check reliably at this
15390                stage whether the section is read-only, as input
15391                sections have not yet been mapped to output sections.
15392                Tentatively set the flag for now, and correct in
15393                adjust_dynamic_symbol.  */
15394             h->non_got_ref = 1;
15395         }
15396
15397       if (may_need_local_target_p
15398           && (h != NULL || ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC))
15399         {
15400           union gotplt_union *root_plt;
15401           struct arm_plt_info *arm_plt;
15402           struct arm_local_iplt_info *local_iplt;
15403
15404           if (h != NULL)
15405             {
15406               root_plt = &h->plt;
15407               arm_plt = &eh->plt;
15408             }
15409           else
15410             {
15411               local_iplt = elf32_arm_create_local_iplt (abfd, r_symndx);
15412               if (local_iplt == NULL)
15413                 return FALSE;
15414               root_plt = &local_iplt->root;
15415               arm_plt = &local_iplt->arm;
15416             }
15417
15418           /* If the symbol is a function that doesn't bind locally,
15419              this relocation will need a PLT entry.  */
15420           if (root_plt->refcount != -1)
15421             root_plt->refcount += 1;
15422
15423           if (!call_reloc_p)
15424             arm_plt->noncall_refcount++;
15425
15426           /* It's too early to use htab->use_blx here, so we have to
15427              record possible blx references separately from
15428              relocs that definitely need a thumb stub.  */
15429
15430           if (r_type == R_ARM_THM_CALL)
15431             arm_plt->maybe_thumb_refcount += 1;
15432
15433           if (r_type == R_ARM_THM_JUMP24
15434               || r_type == R_ARM_THM_JUMP19)
15435             arm_plt->thumb_refcount += 1;
15436         }
15437
15438       if (may_become_dynamic_p)
15439         {
15440           struct elf_dyn_relocs *p, **head;
15441
15442           /* Create a reloc section in dynobj.  */
15443           if (sreloc == NULL)
15444             {
15445               sreloc = _bfd_elf_make_dynamic_reloc_section
15446                 (sec, dynobj, 2, abfd, ! htab->use_rel);
15447
15448               if (sreloc == NULL)
15449                 return FALSE;
15450
15451               /* BPABI objects never have dynamic relocations mapped.  */
15452               if (htab->symbian_p)
15453                 {
15454                   flagword flags;
15455
15456                   flags = bfd_get_section_flags (dynobj, sreloc);
15457                   flags &= ~(SEC_LOAD | SEC_ALLOC);
15458                   bfd_set_section_flags (dynobj, sreloc, flags);
15459                 }
15460             }
15461
15462           /* If this is a global symbol, count the number of
15463              relocations we need for this symbol.  */
15464           if (h != NULL)
15465             head = &((struct elf32_arm_link_hash_entry *) h)->dyn_relocs;
15466           else
15467             {
15468               head = elf32_arm_get_local_dynreloc_list (abfd, r_symndx, isym);
15469               if (head == NULL)
15470                 return FALSE;
15471             }
15472
15473           p = *head;
15474           if (p == NULL || p->sec != sec)
15475             {
15476               bfd_size_type amt = sizeof *p;
15477
15478               p = (struct elf_dyn_relocs *) bfd_alloc (htab->root.dynobj, amt);
15479               if (p == NULL)
15480                 return FALSE;
15481               p->next = *head;
15482               *head = p;
15483               p->sec = sec;
15484               p->count = 0;
15485               p->pc_count = 0;
15486             }
15487
15488           if (elf32_arm_howto_from_type (r_type)->pc_relative)
15489             p->pc_count += 1;
15490           p->count += 1;
15491           if (h == NULL && htab->fdpic_p && !bfd_link_pic(info)
15492               && r_type != R_ARM_ABS32 && r_type != R_ARM_ABS32_NOI) {
15493             /* Here we only support R_ARM_ABS32 and R_ARM_ABS32_NOI
15494                that will become rofixup.  */
15495             /* This is due to the fact that we suppose all will become rofixup.  */
15496             fprintf(stderr, "FDPIC does not yet support %d relocation to become dynamic for executable\n", r_type);
15497             _bfd_error_handler
15498               (_("FDPIC does not yet support %s relocation"
15499                  " to become dynamic for executable"),
15500                elf32_arm_howto_table_1[r_type].name);
15501             abort();
15502           }
15503         }
15504     }
15505
15506   return TRUE;
15507 }
15508
15509 static void
15510 elf32_arm_update_relocs (asection *o,
15511                          struct bfd_elf_section_reloc_data *reldata)
15512 {
15513   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
15514   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
15515   const struct elf_backend_data *bed;
15516   _arm_elf_section_data *eado;
15517   struct bfd_link_order *p;
15518   bfd_byte *erela_head, *erela;
15519   Elf_Internal_Rela *irela_head, *irela;
15520   Elf_Internal_Shdr *rel_hdr;
15521   bfd *abfd;
15522   unsigned int count;
15523
15524   eado = get_arm_elf_section_data (o);
15525
15526   if (!eado || eado->elf.this_hdr.sh_type != SHT_ARM_EXIDX)
15527     return;
15528
15529   abfd = o->owner;
15530   bed = get_elf_backend_data (abfd);
15531   rel_hdr = reldata->hdr;
15532
15533   if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
15534     {
15535       swap_in = bed->s->swap_reloc_in;
15536       swap_out = bed->s->swap_reloc_out;
15537     }
15538   else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
15539     {
15540       swap_in = bed->s->swap_reloca_in;
15541       swap_out = bed->s->swap_reloca_out;
15542     }
15543   else
15544     abort ();
15545
15546   erela_head = rel_hdr->contents;
15547   irela_head = (Elf_Internal_Rela *) bfd_zmalloc
15548     ((NUM_SHDR_ENTRIES (rel_hdr) + 1) * sizeof (*irela_head));
15549
15550   erela = erela_head;
15551   irela = irela_head;
15552   count = 0;
15553
15554   for (p = o->map_head.link_order; p; p = p->next)
15555     {
15556       if (p->type == bfd_section_reloc_link_order
15557           || p->type == bfd_symbol_reloc_link_order)
15558         {
15559           (*swap_in) (abfd, erela, irela);
15560           erela += rel_hdr->sh_entsize;
15561           irela++;
15562           count++;
15563         }
15564       else if (p->type == bfd_indirect_link_order)
15565         {
15566           struct bfd_elf_section_reloc_data *input_reldata;
15567           arm_unwind_table_edit *edit_list, *edit_tail;
15568           _arm_elf_section_data *eadi;
15569           bfd_size_type j;
15570           bfd_vma offset;
15571           asection *i;
15572
15573           i = p->u.indirect.section;
15574
15575           eadi = get_arm_elf_section_data (i);
15576           edit_list = eadi->u.exidx.unwind_edit_list;
15577           edit_tail = eadi->u.exidx.unwind_edit_tail;
15578           offset = o->vma + i->output_offset;
15579
15580           if (eadi->elf.rel.hdr &&
15581               eadi->elf.rel.hdr->sh_entsize == rel_hdr->sh_entsize)
15582             input_reldata = &eadi->elf.rel;
15583           else if (eadi->elf.rela.hdr &&
15584                    eadi->elf.rela.hdr->sh_entsize == rel_hdr->sh_entsize)
15585             input_reldata = &eadi->elf.rela;
15586           else
15587             abort ();
15588
15589           if (edit_list)
15590             {
15591               for (j = 0; j < NUM_SHDR_ENTRIES (input_reldata->hdr); j++)
15592                 {
15593                   arm_unwind_table_edit *edit_node, *edit_next;
15594                   bfd_vma bias;
15595                   bfd_vma reloc_index;
15596
15597                   (*swap_in) (abfd, erela, irela);
15598                   reloc_index = (irela->r_offset - offset) / 8;
15599
15600                   bias = 0;
15601                   edit_node = edit_list;
15602                   for (edit_next = edit_list;
15603                        edit_next && edit_next->index <= reloc_index;
15604                        edit_next = edit_node->next)
15605                     {
15606                       bias++;
15607                       edit_node = edit_next;
15608                     }
15609
15610                   if (edit_node->type != DELETE_EXIDX_ENTRY
15611                       || edit_node->index != reloc_index)
15612                     {
15613                       irela->r_offset -= bias * 8;
15614                       irela++;
15615                       count++;
15616                     }
15617
15618                   erela += rel_hdr->sh_entsize;
15619                 }
15620
15621               if (edit_tail->type == INSERT_EXIDX_CANTUNWIND_AT_END)
15622                 {
15623                   /* New relocation entity.  */
15624                   asection *text_sec = edit_tail->linked_section;
15625                   asection *text_out = text_sec->output_section;
15626                   bfd_vma exidx_offset = offset + i->size - 8;
15627
15628                   irela->r_addend = 0;
15629                   irela->r_offset = exidx_offset;
15630                   irela->r_info = ELF32_R_INFO
15631                     (text_out->target_index, R_ARM_PREL31);
15632                   irela++;
15633                   count++;
15634                 }
15635             }
15636           else
15637             {
15638               for (j = 0; j < NUM_SHDR_ENTRIES (input_reldata->hdr); j++)
15639                 {
15640                   (*swap_in) (abfd, erela, irela);
15641                   erela += rel_hdr->sh_entsize;
15642                   irela++;
15643                 }
15644
15645               count += NUM_SHDR_ENTRIES (input_reldata->hdr);
15646             }
15647         }
15648     }
15649
15650   reldata->count = count;
15651   rel_hdr->sh_size = count * rel_hdr->sh_entsize;
15652
15653   erela = erela_head;
15654   irela = irela_head;
15655   while (count > 0)
15656     {
15657       (*swap_out) (abfd, irela, erela);
15658       erela += rel_hdr->sh_entsize;
15659       irela++;
15660       count--;
15661     }
15662
15663   free (irela_head);
15664
15665   /* Hashes are no longer valid.  */
15666   free (reldata->hashes);
15667   reldata->hashes = NULL;
15668 }
15669
15670 /* Unwinding tables are not referenced directly.  This pass marks them as
15671    required if the corresponding code section is marked.  Similarly, ARMv8-M
15672    secure entry functions can only be referenced by SG veneers which are
15673    created after the GC process. They need to be marked in case they reside in
15674    their own section (as would be the case if code was compiled with
15675    -ffunction-sections).  */
15676
15677 static bfd_boolean
15678 elf32_arm_gc_mark_extra_sections (struct bfd_link_info *info,
15679                                   elf_gc_mark_hook_fn gc_mark_hook)
15680 {
15681   bfd *sub;
15682   Elf_Internal_Shdr **elf_shdrp;
15683   asection *cmse_sec;
15684   obj_attribute *out_attr;
15685   Elf_Internal_Shdr *symtab_hdr;
15686   unsigned i, sym_count, ext_start;
15687   const struct elf_backend_data *bed;
15688   struct elf_link_hash_entry **sym_hashes;
15689   struct elf32_arm_link_hash_entry *cmse_hash;
15690   bfd_boolean again, is_v8m, first_bfd_browse = TRUE;
15691
15692   _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
15693
15694   out_attr = elf_known_obj_attributes_proc (info->output_bfd);
15695   is_v8m = out_attr[Tag_CPU_arch].i >= TAG_CPU_ARCH_V8M_BASE
15696            && out_attr[Tag_CPU_arch_profile].i == 'M';
15697
15698   /* Marking EH data may cause additional code sections to be marked,
15699      requiring multiple passes.  */
15700   again = TRUE;
15701   while (again)
15702     {
15703       again = FALSE;
15704       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
15705         {
15706           asection *o;
15707
15708           if (! is_arm_elf (sub))
15709             continue;
15710
15711           elf_shdrp = elf_elfsections (sub);
15712           for (o = sub->sections; o != NULL; o = o->next)
15713             {
15714               Elf_Internal_Shdr *hdr;
15715
15716               hdr = &elf_section_data (o)->this_hdr;
15717               if (hdr->sh_type == SHT_ARM_EXIDX
15718                   && hdr->sh_link
15719                   && hdr->sh_link < elf_numsections (sub)
15720                   && !o->gc_mark
15721                   && elf_shdrp[hdr->sh_link]->bfd_section->gc_mark)
15722                 {
15723                   again = TRUE;
15724                   if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
15725                     return FALSE;
15726                 }
15727             }
15728
15729           /* Mark section holding ARMv8-M secure entry functions.  We mark all
15730              of them so no need for a second browsing.  */
15731           if (is_v8m && first_bfd_browse)
15732             {
15733               sym_hashes = elf_sym_hashes (sub);
15734               bed = get_elf_backend_data (sub);
15735               symtab_hdr = &elf_tdata (sub)->symtab_hdr;
15736               sym_count = symtab_hdr->sh_size / bed->s->sizeof_sym;
15737               ext_start = symtab_hdr->sh_info;
15738
15739               /* Scan symbols.  */
15740               for (i = ext_start; i < sym_count; i++)
15741                 {
15742                   cmse_hash = elf32_arm_hash_entry (sym_hashes[i - ext_start]);
15743
15744                   /* Assume it is a special symbol.  If not, cmse_scan will
15745                      warn about it and user can do something about it.  */
15746                   if (ARM_GET_SYM_CMSE_SPCL (cmse_hash->root.target_internal))
15747                     {
15748                       cmse_sec = cmse_hash->root.root.u.def.section;
15749                       if (!cmse_sec->gc_mark
15750                           && !_bfd_elf_gc_mark (info, cmse_sec, gc_mark_hook))
15751                         return FALSE;
15752                     }
15753                 }
15754             }
15755         }
15756       first_bfd_browse = FALSE;
15757     }
15758
15759   return TRUE;
15760 }
15761
15762 /* Treat mapping symbols as special target symbols.  */
15763
15764 static bfd_boolean
15765 elf32_arm_is_target_special_symbol (bfd * abfd ATTRIBUTE_UNUSED, asymbol * sym)
15766 {
15767   return bfd_is_arm_special_symbol_name (sym->name,
15768                                          BFD_ARM_SPECIAL_SYM_TYPE_ANY);
15769 }
15770
15771 /* This is a copy of elf_find_function() from elf.c except that
15772    ARM mapping symbols are ignored when looking for function names
15773    and STT_ARM_TFUNC is considered to a function type.  */
15774
15775 static bfd_boolean
15776 arm_elf_find_function (bfd *         abfd ATTRIBUTE_UNUSED,
15777                        asymbol **    symbols,
15778                        asection *    section,
15779                        bfd_vma       offset,
15780                        const char ** filename_ptr,
15781                        const char ** functionname_ptr)
15782 {
15783   const char * filename = NULL;
15784   asymbol * func = NULL;
15785   bfd_vma low_func = 0;
15786   asymbol ** p;
15787
15788   for (p = symbols; *p != NULL; p++)
15789     {
15790       elf_symbol_type *q;
15791
15792       q = (elf_symbol_type *) *p;
15793
15794       switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
15795         {
15796         default:
15797           break;
15798         case STT_FILE:
15799           filename = bfd_asymbol_name (&q->symbol);
15800           break;
15801         case STT_FUNC:
15802         case STT_ARM_TFUNC:
15803         case STT_NOTYPE:
15804           /* Skip mapping symbols.  */
15805           if ((q->symbol.flags & BSF_LOCAL)
15806               && bfd_is_arm_special_symbol_name (q->symbol.name,
15807                     BFD_ARM_SPECIAL_SYM_TYPE_ANY))
15808             continue;
15809           /* Fall through.  */
15810           if (bfd_get_section (&q->symbol) == section
15811               && q->symbol.value >= low_func
15812               && q->symbol.value <= offset)
15813             {
15814               func = (asymbol *) q;
15815               low_func = q->symbol.value;
15816             }
15817           break;
15818         }
15819     }
15820
15821   if (func == NULL)
15822     return FALSE;
15823
15824   if (filename_ptr)
15825     *filename_ptr = filename;
15826   if (functionname_ptr)
15827     *functionname_ptr = bfd_asymbol_name (func);
15828
15829   return TRUE;
15830 }
15831
15832
15833 /* Find the nearest line to a particular section and offset, for error
15834    reporting.   This code is a duplicate of the code in elf.c, except
15835    that it uses arm_elf_find_function.  */
15836
15837 static bfd_boolean
15838 elf32_arm_find_nearest_line (bfd *          abfd,
15839                              asymbol **     symbols,
15840                              asection *     section,
15841                              bfd_vma        offset,
15842                              const char **  filename_ptr,
15843                              const char **  functionname_ptr,
15844                              unsigned int * line_ptr,
15845                              unsigned int * discriminator_ptr)
15846 {
15847   bfd_boolean found = FALSE;
15848
15849   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
15850                                      filename_ptr, functionname_ptr,
15851                                      line_ptr, discriminator_ptr,
15852                                      dwarf_debug_sections, 0,
15853                                      & elf_tdata (abfd)->dwarf2_find_line_info))
15854     {
15855       if (!*functionname_ptr)
15856         arm_elf_find_function (abfd, symbols, section, offset,
15857                                *filename_ptr ? NULL : filename_ptr,
15858                                functionname_ptr);
15859
15860       return TRUE;
15861     }
15862
15863   /* Skip _bfd_dwarf1_find_nearest_line since no known ARM toolchain
15864      uses DWARF1.  */
15865
15866   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
15867                                              & found, filename_ptr,
15868                                              functionname_ptr, line_ptr,
15869                                              & elf_tdata (abfd)->line_info))
15870     return FALSE;
15871
15872   if (found && (*functionname_ptr || *line_ptr))
15873     return TRUE;
15874
15875   if (symbols == NULL)
15876     return FALSE;
15877
15878   if (! arm_elf_find_function (abfd, symbols, section, offset,
15879                                filename_ptr, functionname_ptr))
15880     return FALSE;
15881
15882   *line_ptr = 0;
15883   return TRUE;
15884 }
15885
15886 static bfd_boolean
15887 elf32_arm_find_inliner_info (bfd *          abfd,
15888                              const char **  filename_ptr,
15889                              const char **  functionname_ptr,
15890                              unsigned int * line_ptr)
15891 {
15892   bfd_boolean found;
15893   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
15894                                          functionname_ptr, line_ptr,
15895                                          & elf_tdata (abfd)->dwarf2_find_line_info);
15896   return found;
15897 }
15898
15899 /* Find dynamic relocs for H that apply to read-only sections.  */
15900
15901 static asection *
15902 readonly_dynrelocs (struct elf_link_hash_entry *h)
15903 {
15904   struct elf_dyn_relocs *p;
15905
15906   for (p = elf32_arm_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
15907     {
15908       asection *s = p->sec->output_section;
15909
15910       if (s != NULL && (s->flags & SEC_READONLY) != 0)
15911         return p->sec;
15912     }
15913   return NULL;
15914 }
15915
15916 /* Adjust a symbol defined by a dynamic object and referenced by a
15917    regular object.  The current definition is in some section of the
15918    dynamic object, but we're not including those sections.  We have to
15919    change the definition to something the rest of the link can
15920    understand.  */
15921
15922 static bfd_boolean
15923 elf32_arm_adjust_dynamic_symbol (struct bfd_link_info * info,
15924                                  struct elf_link_hash_entry * h)
15925 {
15926   bfd * dynobj;
15927   asection *s, *srel;
15928   struct elf32_arm_link_hash_entry * eh;
15929   struct elf32_arm_link_hash_table *globals;
15930
15931   globals = elf32_arm_hash_table (info);
15932   if (globals == NULL)
15933     return FALSE;
15934
15935   dynobj = elf_hash_table (info)->dynobj;
15936
15937   /* Make sure we know what is going on here.  */
15938   BFD_ASSERT (dynobj != NULL
15939               && (h->needs_plt
15940                   || h->type == STT_GNU_IFUNC
15941                   || h->is_weakalias
15942                   || (h->def_dynamic
15943                       && h->ref_regular
15944                       && !h->def_regular)));
15945
15946   eh = (struct elf32_arm_link_hash_entry *) h;
15947
15948   /* If this is a function, put it in the procedure linkage table.  We
15949      will fill in the contents of the procedure linkage table later,
15950      when we know the address of the .got section.  */
15951   if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
15952     {
15953       /* Calls to STT_GNU_IFUNC symbols always use a PLT, even if the
15954          symbol binds locally.  */
15955       if (h->plt.refcount <= 0
15956           || (h->type != STT_GNU_IFUNC
15957               && (SYMBOL_CALLS_LOCAL (info, h)
15958                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
15959                       && h->root.type == bfd_link_hash_undefweak))))
15960         {
15961           /* This case can occur if we saw a PLT32 reloc in an input
15962              file, but the symbol was never referred to by a dynamic
15963              object, or if all references were garbage collected.  In
15964              such a case, we don't actually need to build a procedure
15965              linkage table, and we can just do a PC24 reloc instead.  */
15966           h->plt.offset = (bfd_vma) -1;
15967           eh->plt.thumb_refcount = 0;
15968           eh->plt.maybe_thumb_refcount = 0;
15969           eh->plt.noncall_refcount = 0;
15970           h->needs_plt = 0;
15971         }
15972
15973       return TRUE;
15974     }
15975   else
15976     {
15977       /* It's possible that we incorrectly decided a .plt reloc was
15978          needed for an R_ARM_PC24 or similar reloc to a non-function sym
15979          in check_relocs.  We can't decide accurately between function
15980          and non-function syms in check-relocs; Objects loaded later in
15981          the link may change h->type.  So fix it now.  */
15982       h->plt.offset = (bfd_vma) -1;
15983       eh->plt.thumb_refcount = 0;
15984       eh->plt.maybe_thumb_refcount = 0;
15985       eh->plt.noncall_refcount = 0;
15986     }
15987
15988   /* If this is a weak symbol, and there is a real definition, the
15989      processor independent code will have arranged for us to see the
15990      real definition first, and we can just use the same value.  */
15991   if (h->is_weakalias)
15992     {
15993       struct elf_link_hash_entry *def = weakdef (h);
15994       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
15995       h->root.u.def.section = def->root.u.def.section;
15996       h->root.u.def.value = def->root.u.def.value;
15997       return TRUE;
15998     }
15999
16000   /* If there are no non-GOT references, we do not need a copy
16001      relocation.  */
16002   if (!h->non_got_ref)
16003     return TRUE;
16004
16005   /* This is a reference to a symbol defined by a dynamic object which
16006      is not a function.  */
16007
16008   /* If we are creating a shared library, we must presume that the
16009      only references to the symbol are via the global offset table.
16010      For such cases we need not do anything here; the relocations will
16011      be handled correctly by relocate_section.  Relocatable executables
16012      can reference data in shared objects directly, so we don't need to
16013      do anything here.  */
16014   if (bfd_link_pic (info) || globals->root.is_relocatable_executable)
16015     return TRUE;
16016
16017   /* We must allocate the symbol in our .dynbss section, which will
16018      become part of the .bss section of the executable.  There will be
16019      an entry for this symbol in the .dynsym section.  The dynamic
16020      object will contain position independent code, so all references
16021      from the dynamic object to this symbol will go through the global
16022      offset table.  The dynamic linker will use the .dynsym entry to
16023      determine the address it must put in the global offset table, so
16024      both the dynamic object and the regular object will refer to the
16025      same memory location for the variable.  */
16026   /* If allowed, we must generate a R_ARM_COPY reloc to tell the dynamic
16027      linker to copy the initial value out of the dynamic object and into
16028      the runtime process image.  We need to remember the offset into the
16029      .rel(a).bss section we are going to use.  */
16030   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
16031     {
16032       s = globals->root.sdynrelro;
16033       srel = globals->root.sreldynrelro;
16034     }
16035   else
16036     {
16037       s = globals->root.sdynbss;
16038       srel = globals->root.srelbss;
16039     }
16040   if (info->nocopyreloc == 0
16041       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
16042       && h->size != 0)
16043     {
16044       elf32_arm_allocate_dynrelocs (info, srel, 1);
16045       h->needs_copy = 1;
16046     }
16047
16048   return _bfd_elf_adjust_dynamic_copy (info, h, s);
16049 }
16050
16051 /* Allocate space in .plt, .got and associated reloc sections for
16052    dynamic relocs.  */
16053
16054 static bfd_boolean
16055 allocate_dynrelocs_for_symbol (struct elf_link_hash_entry *h, void * inf)
16056 {
16057   struct bfd_link_info *info;
16058   struct elf32_arm_link_hash_table *htab;
16059   struct elf32_arm_link_hash_entry *eh;
16060   struct elf_dyn_relocs *p;
16061
16062   if (h->root.type == bfd_link_hash_indirect)
16063     return TRUE;
16064
16065   eh = (struct elf32_arm_link_hash_entry *) h;
16066
16067   info = (struct bfd_link_info *) inf;
16068   htab = elf32_arm_hash_table (info);
16069   if (htab == NULL)
16070     return FALSE;
16071
16072   if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
16073       && h->plt.refcount > 0)
16074     {
16075       /* Make sure this symbol is output as a dynamic symbol.
16076          Undefined weak syms won't yet be marked as dynamic.  */
16077       if (h->dynindx == -1 && !h->forced_local
16078           && h->root.type == bfd_link_hash_undefweak)
16079         {
16080           if (! bfd_elf_link_record_dynamic_symbol (info, h))
16081             return FALSE;
16082         }
16083
16084       /* If the call in the PLT entry binds locally, the associated
16085          GOT entry should use an R_ARM_IRELATIVE relocation instead of
16086          the usual R_ARM_JUMP_SLOT.  Put it in the .iplt section rather
16087          than the .plt section.  */
16088       if (h->type == STT_GNU_IFUNC && SYMBOL_CALLS_LOCAL (info, h))
16089         {
16090           eh->is_iplt = 1;
16091           if (eh->plt.noncall_refcount == 0
16092               && SYMBOL_REFERENCES_LOCAL (info, h))
16093             /* All non-call references can be resolved directly.
16094                This means that they can (and in some cases, must)
16095                resolve directly to the run-time target, rather than
16096                to the PLT.  That in turns means that any .got entry
16097                would be equal to the .igot.plt entry, so there's
16098                no point having both.  */
16099             h->got.refcount = 0;
16100         }
16101
16102       if (bfd_link_pic (info)
16103           || eh->is_iplt
16104           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
16105         {
16106           elf32_arm_allocate_plt_entry (info, eh->is_iplt, &h->plt, &eh->plt);
16107
16108           /* If this symbol is not defined in a regular file, and we are
16109              not generating a shared library, then set the symbol to this
16110              location in the .plt.  This is required to make function
16111              pointers compare as equal between the normal executable and
16112              the shared library.  */
16113           if (! bfd_link_pic (info)
16114               && !h->def_regular)
16115             {
16116               h->root.u.def.section = htab->root.splt;
16117               h->root.u.def.value = h->plt.offset;
16118
16119               /* Make sure the function is not marked as Thumb, in case
16120                  it is the target of an ABS32 relocation, which will
16121                  point to the PLT entry.  */
16122               ARM_SET_SYM_BRANCH_TYPE (h->target_internal, ST_BRANCH_TO_ARM);
16123             }
16124
16125           /* VxWorks executables have a second set of relocations for
16126              each PLT entry.  They go in a separate relocation section,
16127              which is processed by the kernel loader.  */
16128           if (htab->vxworks_p && !bfd_link_pic (info))
16129             {
16130               /* There is a relocation for the initial PLT entry:
16131                  an R_ARM_32 relocation for _GLOBAL_OFFSET_TABLE_.  */
16132               if (h->plt.offset == htab->plt_header_size)
16133                 elf32_arm_allocate_dynrelocs (info, htab->srelplt2, 1);
16134
16135               /* There are two extra relocations for each subsequent
16136                  PLT entry: an R_ARM_32 relocation for the GOT entry,
16137                  and an R_ARM_32 relocation for the PLT entry.  */
16138               elf32_arm_allocate_dynrelocs (info, htab->srelplt2, 2);
16139             }
16140         }
16141       else
16142         {
16143           h->plt.offset = (bfd_vma) -1;
16144           h->needs_plt = 0;
16145         }
16146     }
16147   else
16148     {
16149       h->plt.offset = (bfd_vma) -1;
16150       h->needs_plt = 0;
16151     }
16152
16153   eh = (struct elf32_arm_link_hash_entry *) h;
16154   eh->tlsdesc_got = (bfd_vma) -1;
16155
16156   if (h->got.refcount > 0)
16157     {
16158       asection *s;
16159       bfd_boolean dyn;
16160       int tls_type = elf32_arm_hash_entry (h)->tls_type;
16161       int indx;
16162
16163       /* Make sure this symbol is output as a dynamic symbol.
16164          Undefined weak syms won't yet be marked as dynamic.  */
16165       if (htab->root.dynamic_sections_created && h->dynindx == -1 && !h->forced_local
16166           && h->root.type == bfd_link_hash_undefweak)
16167         {
16168           if (! bfd_elf_link_record_dynamic_symbol (info, h))
16169             return FALSE;
16170         }
16171
16172       if (!htab->symbian_p)
16173         {
16174           s = htab->root.sgot;
16175           h->got.offset = s->size;
16176
16177           if (tls_type == GOT_UNKNOWN)
16178             abort ();
16179
16180           if (tls_type == GOT_NORMAL)
16181             /* Non-TLS symbols need one GOT slot.  */
16182             s->size += 4;
16183           else
16184             {
16185               if (tls_type & GOT_TLS_GDESC)
16186                 {
16187                   /* R_ARM_TLS_DESC needs 2 GOT slots.  */
16188                   eh->tlsdesc_got
16189                     = (htab->root.sgotplt->size
16190                        - elf32_arm_compute_jump_table_size (htab));
16191                   htab->root.sgotplt->size += 8;
16192                   h->got.offset = (bfd_vma) -2;
16193                   /* plt.got_offset needs to know there's a TLS_DESC
16194                      reloc in the middle of .got.plt.  */
16195                   htab->num_tls_desc++;
16196                 }
16197
16198               if (tls_type & GOT_TLS_GD)
16199                 {
16200                   /* R_ARM_TLS_GD32 and R_ARM_TLS_GD32_FDPIC need two
16201                      consecutive GOT slots.  If the symbol is both GD
16202                      and GDESC, got.offset may have been
16203                      overwritten.  */
16204                   h->got.offset = s->size;
16205                   s->size += 8;
16206                 }
16207
16208               if (tls_type & GOT_TLS_IE)
16209                 /* R_ARM_TLS_IE32/R_ARM_TLS_IE32_FDPIC need one GOT
16210                    slot.  */
16211                 s->size += 4;
16212             }
16213
16214           dyn = htab->root.dynamic_sections_created;
16215
16216           indx = 0;
16217           if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
16218                                                bfd_link_pic (info),
16219                                                h)
16220               && (!bfd_link_pic (info)
16221                   || !SYMBOL_REFERENCES_LOCAL (info, h)))
16222             indx = h->dynindx;
16223
16224           if (tls_type != GOT_NORMAL
16225               && (bfd_link_pic (info) || indx != 0)
16226               && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
16227                   || h->root.type != bfd_link_hash_undefweak))
16228             {
16229               if (tls_type & GOT_TLS_IE)
16230                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16231
16232               if (tls_type & GOT_TLS_GD)
16233                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16234
16235               if (tls_type & GOT_TLS_GDESC)
16236                 {
16237                   elf32_arm_allocate_dynrelocs (info, htab->root.srelplt, 1);
16238                   /* GDESC needs a trampoline to jump to.  */
16239                   htab->tls_trampoline = -1;
16240                 }
16241
16242               /* Only GD needs it.  GDESC just emits one relocation per
16243                  2 entries.  */
16244               if ((tls_type & GOT_TLS_GD) && indx != 0)
16245                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16246             }
16247           else if (((indx != -1) || htab->fdpic_p)
16248                    && !SYMBOL_REFERENCES_LOCAL (info, h))
16249             {
16250               if (htab->root.dynamic_sections_created)
16251                 /* Reserve room for the GOT entry's R_ARM_GLOB_DAT relocation.  */
16252                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16253             }
16254           else if (h->type == STT_GNU_IFUNC
16255                    && eh->plt.noncall_refcount == 0)
16256             /* No non-call references resolve the STT_GNU_IFUNC's PLT entry;
16257                they all resolve dynamically instead.  Reserve room for the
16258                GOT entry's R_ARM_IRELATIVE relocation.  */
16259             elf32_arm_allocate_irelocs (info, htab->root.srelgot, 1);
16260           else if (bfd_link_pic (info)
16261                    && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
16262                        || h->root.type != bfd_link_hash_undefweak))
16263             /* Reserve room for the GOT entry's R_ARM_RELATIVE relocation.  */
16264             elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16265           else if (htab->fdpic_p && tls_type == GOT_NORMAL)
16266             /* Reserve room for rofixup for FDPIC executable.  */
16267             /* TLS relocs do not need space since they are completely
16268                resolved.  */
16269             htab->srofixup->size += 4;
16270         }
16271     }
16272   else
16273     h->got.offset = (bfd_vma) -1;
16274
16275   /* FDPIC support.  */
16276   if (eh->fdpic_cnts.gotofffuncdesc_cnt > 0)
16277     {
16278       /* Symbol musn't be exported.  */
16279       if (h->dynindx != -1)
16280         abort();
16281
16282       /* We only allocate one function descriptor with its associated relocation.  */
16283       if (eh->fdpic_cnts.funcdesc_offset == -1)
16284         {
16285           asection *s = htab->root.sgot;
16286
16287           eh->fdpic_cnts.funcdesc_offset = s->size;
16288           s->size += 8;
16289           /* We will add an R_ARM_FUNCDESC_VALUE relocation or two rofixups.  */
16290           if (bfd_link_pic(info))
16291             elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16292           else
16293             htab->srofixup->size += 8;
16294         }
16295     }
16296
16297   if (eh->fdpic_cnts.gotfuncdesc_cnt > 0)
16298     {
16299       asection *s = htab->root.sgot;
16300
16301       if (htab->root.dynamic_sections_created && h->dynindx == -1
16302           && !h->forced_local)
16303         if (! bfd_elf_link_record_dynamic_symbol (info, h))
16304           return FALSE;
16305
16306       if (h->dynindx == -1)
16307         {
16308           /* We only allocate one function descriptor with its associated relocation. q */
16309           if (eh->fdpic_cnts.funcdesc_offset == -1)
16310             {
16311
16312               eh->fdpic_cnts.funcdesc_offset = s->size;
16313               s->size += 8;
16314               /* We will add an R_ARM_FUNCDESC_VALUE relocation or two rofixups.  */
16315               if (bfd_link_pic(info))
16316                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16317               else
16318                 htab->srofixup->size += 8;
16319             }
16320         }
16321
16322       /* Add one entry into the GOT and a R_ARM_FUNCDESC or
16323          R_ARM_RELATIVE/rofixup relocation on it.  */
16324       eh->fdpic_cnts.gotfuncdesc_offset = s->size;
16325       s->size += 4;
16326       if (h->dynindx == -1 && !bfd_link_pic(info))
16327         htab->srofixup->size += 4;
16328       else
16329         elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16330     }
16331
16332   if (eh->fdpic_cnts.funcdesc_cnt > 0)
16333     {
16334       if (htab->root.dynamic_sections_created && h->dynindx == -1
16335           && !h->forced_local)
16336         if (! bfd_elf_link_record_dynamic_symbol (info, h))
16337           return FALSE;
16338
16339       if (h->dynindx == -1)
16340         {
16341           /* We only allocate one function descriptor with its associated relocation.  */
16342           if (eh->fdpic_cnts.funcdesc_offset == -1)
16343             {
16344               asection *s = htab->root.sgot;
16345
16346               eh->fdpic_cnts.funcdesc_offset = s->size;
16347               s->size += 8;
16348               /* We will add an R_ARM_FUNCDESC_VALUE relocation or two rofixups.  */
16349               if (bfd_link_pic(info))
16350                 elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16351               else
16352                 htab->srofixup->size += 8;
16353             }
16354         }
16355       if (h->dynindx == -1 && !bfd_link_pic(info))
16356         {
16357           /* For FDPIC executable we replace R_ARM_RELATIVE with a rofixup.  */
16358           htab->srofixup->size += 4 * eh->fdpic_cnts.funcdesc_cnt;
16359         }
16360       else
16361         {
16362           /* Will need one dynamic reloc per reference. will be either
16363              R_ARM_FUNCDESC or R_ARM_RELATIVE for hidden symbols.  */
16364           elf32_arm_allocate_dynrelocs (info, htab->root.srelgot,
16365                                         eh->fdpic_cnts.funcdesc_cnt);
16366         }
16367     }
16368
16369   /* Allocate stubs for exported Thumb functions on v4t.  */
16370   if (!htab->use_blx && h->dynindx != -1
16371       && h->def_regular
16372       && ARM_GET_SYM_BRANCH_TYPE (h->target_internal) == ST_BRANCH_TO_THUMB
16373       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
16374     {
16375       struct elf_link_hash_entry * th;
16376       struct bfd_link_hash_entry * bh;
16377       struct elf_link_hash_entry * myh;
16378       char name[1024];
16379       asection *s;
16380       bh = NULL;
16381       /* Create a new symbol to regist the real location of the function.  */
16382       s = h->root.u.def.section;
16383       sprintf (name, "__real_%s", h->root.root.string);
16384       _bfd_generic_link_add_one_symbol (info, s->owner,
16385                                         name, BSF_GLOBAL, s,
16386                                         h->root.u.def.value,
16387                                         NULL, TRUE, FALSE, &bh);
16388
16389       myh = (struct elf_link_hash_entry *) bh;
16390       myh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
16391       myh->forced_local = 1;
16392       ARM_SET_SYM_BRANCH_TYPE (myh->target_internal, ST_BRANCH_TO_THUMB);
16393       eh->export_glue = myh;
16394       th = record_arm_to_thumb_glue (info, h);
16395       /* Point the symbol at the stub.  */
16396       h->type = ELF_ST_INFO (ELF_ST_BIND (h->type), STT_FUNC);
16397       ARM_SET_SYM_BRANCH_TYPE (h->target_internal, ST_BRANCH_TO_ARM);
16398       h->root.u.def.section = th->root.u.def.section;
16399       h->root.u.def.value = th->root.u.def.value & ~1;
16400     }
16401
16402   if (eh->dyn_relocs == NULL)
16403     return TRUE;
16404
16405   /* In the shared -Bsymbolic case, discard space allocated for
16406      dynamic pc-relative relocs against symbols which turn out to be
16407      defined in regular objects.  For the normal shared case, discard
16408      space for pc-relative relocs that have become local due to symbol
16409      visibility changes.  */
16410
16411   if (bfd_link_pic (info) || htab->root.is_relocatable_executable || htab->fdpic_p)
16412     {
16413       /* Relocs that use pc_count are PC-relative forms, which will appear
16414          on something like ".long foo - ." or "movw REG, foo - .".  We want
16415          calls to protected symbols to resolve directly to the function
16416          rather than going via the plt.  If people want function pointer
16417          comparisons to work as expected then they should avoid writing
16418          assembly like ".long foo - .".  */
16419       if (SYMBOL_CALLS_LOCAL (info, h))
16420         {
16421           struct elf_dyn_relocs **pp;
16422
16423           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
16424             {
16425               p->count -= p->pc_count;
16426               p->pc_count = 0;
16427               if (p->count == 0)
16428                 *pp = p->next;
16429               else
16430                 pp = &p->next;
16431             }
16432         }
16433
16434       if (htab->vxworks_p)
16435         {
16436           struct elf_dyn_relocs **pp;
16437
16438           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
16439             {
16440               if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
16441                 *pp = p->next;
16442               else
16443                 pp = &p->next;
16444             }
16445         }
16446
16447       /* Also discard relocs on undefined weak syms with non-default
16448          visibility.  */
16449       if (eh->dyn_relocs != NULL
16450           && h->root.type == bfd_link_hash_undefweak)
16451         {
16452           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
16453               || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
16454             eh->dyn_relocs = NULL;
16455
16456           /* Make sure undefined weak symbols are output as a dynamic
16457              symbol in PIEs.  */
16458           else if (htab->root.dynamic_sections_created && h->dynindx == -1
16459                    && !h->forced_local)
16460             {
16461               if (! bfd_elf_link_record_dynamic_symbol (info, h))
16462                 return FALSE;
16463             }
16464         }
16465
16466       else if (htab->root.is_relocatable_executable && h->dynindx == -1
16467                && h->root.type == bfd_link_hash_new)
16468         {
16469           /* Output absolute symbols so that we can create relocations
16470              against them.  For normal symbols we output a relocation
16471              against the section that contains them.  */
16472           if (! bfd_elf_link_record_dynamic_symbol (info, h))
16473             return FALSE;
16474         }
16475
16476     }
16477   else
16478     {
16479       /* For the non-shared case, discard space for relocs against
16480          symbols which turn out to need copy relocs or are not
16481          dynamic.  */
16482
16483       if (!h->non_got_ref
16484           && ((h->def_dynamic
16485                && !h->def_regular)
16486               || (htab->root.dynamic_sections_created
16487                   && (h->root.type == bfd_link_hash_undefweak
16488                       || h->root.type == bfd_link_hash_undefined))))
16489         {
16490           /* Make sure this symbol is output as a dynamic symbol.
16491              Undefined weak syms won't yet be marked as dynamic.  */
16492           if (h->dynindx == -1 && !h->forced_local
16493               && h->root.type == bfd_link_hash_undefweak)
16494             {
16495               if (! bfd_elf_link_record_dynamic_symbol (info, h))
16496                 return FALSE;
16497             }
16498
16499           /* If that succeeded, we know we'll be keeping all the
16500              relocs.  */
16501           if (h->dynindx != -1)
16502             goto keep;
16503         }
16504
16505       eh->dyn_relocs = NULL;
16506
16507     keep: ;
16508     }
16509
16510   /* Finally, allocate space.  */
16511   for (p = eh->dyn_relocs; p != NULL; p = p->next)
16512     {
16513       asection *sreloc = elf_section_data (p->sec)->sreloc;
16514
16515       if (h->type == STT_GNU_IFUNC
16516           && eh->plt.noncall_refcount == 0
16517           && SYMBOL_REFERENCES_LOCAL (info, h))
16518         elf32_arm_allocate_irelocs (info, sreloc, p->count);
16519       else if (h->dynindx != -1 && (!bfd_link_pic(info) || !info->symbolic || !h->def_regular))
16520         elf32_arm_allocate_dynrelocs (info, sreloc, p->count);
16521       else if (htab->fdpic_p && !bfd_link_pic(info))
16522         htab->srofixup->size += 4 * p->count;
16523       else
16524         elf32_arm_allocate_dynrelocs (info, sreloc, p->count);
16525     }
16526
16527   return TRUE;
16528 }
16529
16530 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
16531    read-only sections.  */
16532
16533 static bfd_boolean
16534 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
16535 {
16536   asection *sec;
16537
16538   if (h->root.type == bfd_link_hash_indirect)
16539     return TRUE;
16540
16541   sec = readonly_dynrelocs (h);
16542   if (sec != NULL)
16543     {
16544       struct bfd_link_info *info = (struct bfd_link_info *) info_p;
16545
16546       info->flags |= DF_TEXTREL;
16547       info->callbacks->minfo
16548         (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
16549          sec->owner, h->root.root.string, sec);
16550
16551       /* Not an error, just cut short the traversal.  */
16552       return FALSE;
16553     }
16554
16555   return TRUE;
16556 }
16557
16558 void
16559 bfd_elf32_arm_set_byteswap_code (struct bfd_link_info *info,
16560                                  int byteswap_code)
16561 {
16562   struct elf32_arm_link_hash_table *globals;
16563
16564   globals = elf32_arm_hash_table (info);
16565   if (globals == NULL)
16566     return;
16567
16568   globals->byteswap_code = byteswap_code;
16569 }
16570
16571 /* Set the sizes of the dynamic sections.  */
16572
16573 static bfd_boolean
16574 elf32_arm_size_dynamic_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
16575                                  struct bfd_link_info * info)
16576 {
16577   bfd * dynobj;
16578   asection * s;
16579   bfd_boolean plt;
16580   bfd_boolean relocs;
16581   bfd *ibfd;
16582   struct elf32_arm_link_hash_table *htab;
16583
16584   htab = elf32_arm_hash_table (info);
16585   if (htab == NULL)
16586     return FALSE;
16587
16588   dynobj = elf_hash_table (info)->dynobj;
16589   BFD_ASSERT (dynobj != NULL);
16590   check_use_blx (htab);
16591
16592   if (elf_hash_table (info)->dynamic_sections_created)
16593     {
16594       /* Set the contents of the .interp section to the interpreter.  */
16595       if (bfd_link_executable (info) && !info->nointerp)
16596         {
16597           s = bfd_get_linker_section (dynobj, ".interp");
16598           BFD_ASSERT (s != NULL);
16599           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
16600           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
16601         }
16602     }
16603
16604   /* Set up .got offsets for local syms, and space for local dynamic
16605      relocs.  */
16606   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
16607     {
16608       bfd_signed_vma *local_got;
16609       bfd_signed_vma *end_local_got;
16610       struct arm_local_iplt_info **local_iplt_ptr, *local_iplt;
16611       char *local_tls_type;
16612       bfd_vma *local_tlsdesc_gotent;
16613       bfd_size_type locsymcount;
16614       Elf_Internal_Shdr *symtab_hdr;
16615       asection *srel;
16616       bfd_boolean is_vxworks = htab->vxworks_p;
16617       unsigned int symndx;
16618       struct fdpic_local *local_fdpic_cnts;
16619
16620       if (! is_arm_elf (ibfd))
16621         continue;
16622
16623       for (s = ibfd->sections; s != NULL; s = s->next)
16624         {
16625           struct elf_dyn_relocs *p;
16626
16627           for (p = (struct elf_dyn_relocs *)
16628                    elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
16629             {
16630               if (!bfd_is_abs_section (p->sec)
16631                   && bfd_is_abs_section (p->sec->output_section))
16632                 {
16633                   /* Input section has been discarded, either because
16634                      it is a copy of a linkonce section or due to
16635                      linker script /DISCARD/, so we'll be discarding
16636                      the relocs too.  */
16637                 }
16638               else if (is_vxworks
16639                        && strcmp (p->sec->output_section->name,
16640                                   ".tls_vars") == 0)
16641                 {
16642                   /* Relocations in vxworks .tls_vars sections are
16643                      handled specially by the loader.  */
16644                 }
16645               else if (p->count != 0)
16646                 {
16647                   srel = elf_section_data (p->sec)->sreloc;
16648                   if (htab->fdpic_p && !bfd_link_pic(info))
16649                     htab->srofixup->size += 4 * p->count;
16650                   else
16651                     elf32_arm_allocate_dynrelocs (info, srel, p->count);
16652                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
16653                     info->flags |= DF_TEXTREL;
16654                 }
16655             }
16656         }
16657
16658       local_got = elf_local_got_refcounts (ibfd);
16659       if (!local_got)
16660         continue;
16661
16662       symtab_hdr = & elf_symtab_hdr (ibfd);
16663       locsymcount = symtab_hdr->sh_info;
16664       end_local_got = local_got + locsymcount;
16665       local_iplt_ptr = elf32_arm_local_iplt (ibfd);
16666       local_tls_type = elf32_arm_local_got_tls_type (ibfd);
16667       local_tlsdesc_gotent = elf32_arm_local_tlsdesc_gotent (ibfd);
16668       local_fdpic_cnts = elf32_arm_local_fdpic_cnts (ibfd);
16669       symndx = 0;
16670       s = htab->root.sgot;
16671       srel = htab->root.srelgot;
16672       for (; local_got < end_local_got;
16673            ++local_got, ++local_iplt_ptr, ++local_tls_type,
16674            ++local_tlsdesc_gotent, ++symndx, ++local_fdpic_cnts)
16675         {
16676           *local_tlsdesc_gotent = (bfd_vma) -1;
16677           local_iplt = *local_iplt_ptr;
16678
16679           /* FDPIC support.  */
16680           if (local_fdpic_cnts->gotofffuncdesc_cnt > 0)
16681             {
16682               if (local_fdpic_cnts->funcdesc_offset == -1)
16683                 {
16684                   local_fdpic_cnts->funcdesc_offset = s->size;
16685                   s->size += 8;
16686
16687                   /* We will add an R_ARM_FUNCDESC_VALUE relocation or two rofixups.  */
16688                   if (bfd_link_pic(info))
16689                     elf32_arm_allocate_dynrelocs (info, srel, 1);
16690                   else
16691                     htab->srofixup->size += 8;
16692                 }
16693             }
16694
16695           if (local_fdpic_cnts->funcdesc_cnt > 0)
16696             {
16697               if (local_fdpic_cnts->funcdesc_offset == -1)
16698                 {
16699                   local_fdpic_cnts->funcdesc_offset = s->size;
16700                   s->size += 8;
16701
16702                   /* We will add an R_ARM_FUNCDESC_VALUE relocation or two rofixups.  */
16703                   if (bfd_link_pic(info))
16704                     elf32_arm_allocate_dynrelocs (info, srel, 1);
16705                   else
16706                     htab->srofixup->size += 8;
16707                 }
16708
16709               /* We will add n R_ARM_RELATIVE relocations or n rofixups.  */
16710               if (bfd_link_pic(info))
16711                 elf32_arm_allocate_dynrelocs (info, srel, local_fdpic_cnts->funcdesc_cnt);
16712               else
16713                 htab->srofixup->size += 4 * local_fdpic_cnts->funcdesc_cnt;
16714             }
16715
16716           if (local_iplt != NULL)
16717             {
16718               struct elf_dyn_relocs *p;
16719
16720               if (local_iplt->root.refcount > 0)
16721                 {
16722                   elf32_arm_allocate_plt_entry (info, TRUE,
16723                                                 &local_iplt->root,
16724                                                 &local_iplt->arm);
16725                   if (local_iplt->arm.noncall_refcount == 0)
16726                     /* All references to the PLT are calls, so all
16727                        non-call references can resolve directly to the
16728                        run-time target.  This means that the .got entry
16729                        would be the same as the .igot.plt entry, so there's
16730                        no point creating both.  */
16731                     *local_got = 0;
16732                 }
16733               else
16734                 {
16735                   BFD_ASSERT (local_iplt->arm.noncall_refcount == 0);
16736                   local_iplt->root.offset = (bfd_vma) -1;
16737                 }
16738
16739               for (p = local_iplt->dyn_relocs; p != NULL; p = p->next)
16740                 {
16741                   asection *psrel;
16742
16743                   psrel = elf_section_data (p->sec)->sreloc;
16744                   if (local_iplt->arm.noncall_refcount == 0)
16745                     elf32_arm_allocate_irelocs (info, psrel, p->count);
16746                   else
16747                     elf32_arm_allocate_dynrelocs (info, psrel, p->count);
16748                 }
16749             }
16750           if (*local_got > 0)
16751             {
16752               Elf_Internal_Sym *isym;
16753
16754               *local_got = s->size;
16755               if (*local_tls_type & GOT_TLS_GD)
16756                 /* TLS_GD relocs need an 8-byte structure in the GOT.  */
16757                 s->size += 8;
16758               if (*local_tls_type & GOT_TLS_GDESC)
16759                 {
16760                   *local_tlsdesc_gotent = htab->root.sgotplt->size
16761                     - elf32_arm_compute_jump_table_size (htab);
16762                   htab->root.sgotplt->size += 8;
16763                   *local_got = (bfd_vma) -2;
16764                   /* plt.got_offset needs to know there's a TLS_DESC
16765                      reloc in the middle of .got.plt.  */
16766                   htab->num_tls_desc++;
16767                 }
16768               if (*local_tls_type & GOT_TLS_IE)
16769                 s->size += 4;
16770
16771               if (*local_tls_type & GOT_NORMAL)
16772                 {
16773                   /* If the symbol is both GD and GDESC, *local_got
16774                      may have been overwritten.  */
16775                   *local_got = s->size;
16776                   s->size += 4;
16777                 }
16778
16779               isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, symndx);
16780               if (isym == NULL)
16781                 return FALSE;
16782
16783               /* If all references to an STT_GNU_IFUNC PLT are calls,
16784                  then all non-call references, including this GOT entry,
16785                  resolve directly to the run-time target.  */
16786               if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
16787                   && (local_iplt == NULL
16788                       || local_iplt->arm.noncall_refcount == 0))
16789                 elf32_arm_allocate_irelocs (info, srel, 1);
16790               else if (bfd_link_pic (info) || output_bfd->flags & DYNAMIC || htab->fdpic_p)
16791                 {
16792                   if ((bfd_link_pic (info) && !(*local_tls_type & GOT_TLS_GDESC)))
16793                     elf32_arm_allocate_dynrelocs (info, srel, 1);
16794                   else if (htab->fdpic_p && *local_tls_type & GOT_NORMAL)
16795                     htab->srofixup->size += 4;
16796
16797                   if ((bfd_link_pic (info) || htab->fdpic_p)
16798                       && *local_tls_type & GOT_TLS_GDESC)
16799                     {
16800                       elf32_arm_allocate_dynrelocs (info,
16801                                                     htab->root.srelplt, 1);
16802                       htab->tls_trampoline = -1;
16803                     }
16804                 }
16805             }
16806           else
16807             *local_got = (bfd_vma) -1;
16808         }
16809     }
16810
16811   if (htab->tls_ldm_got.refcount > 0)
16812     {
16813       /* Allocate two GOT entries and one dynamic relocation (if necessary)
16814          for R_ARM_TLS_LDM32/R_ARM_TLS_LDM32_FDPIC relocations.  */
16815       htab->tls_ldm_got.offset = htab->root.sgot->size;
16816       htab->root.sgot->size += 8;
16817       if (bfd_link_pic (info))
16818         elf32_arm_allocate_dynrelocs (info, htab->root.srelgot, 1);
16819     }
16820   else
16821     htab->tls_ldm_got.offset = -1;
16822
16823   /* At the very end of the .rofixup section is a pointer to the GOT,
16824      reserve space for it. */
16825   if (htab->fdpic_p && htab->srofixup != NULL)
16826     htab->srofixup->size += 4;
16827
16828   /* Allocate global sym .plt and .got entries, and space for global
16829      sym dynamic relocs.  */
16830   elf_link_hash_traverse (& htab->root, allocate_dynrelocs_for_symbol, info);
16831
16832   /* Here we rummage through the found bfds to collect glue information.  */
16833   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
16834     {
16835       if (! is_arm_elf (ibfd))
16836         continue;
16837
16838       /* Initialise mapping tables for code/data.  */
16839       bfd_elf32_arm_init_maps (ibfd);
16840
16841       if (!bfd_elf32_arm_process_before_allocation (ibfd, info)
16842           || !bfd_elf32_arm_vfp11_erratum_scan (ibfd, info)
16843           || !bfd_elf32_arm_stm32l4xx_erratum_scan (ibfd, info))
16844         _bfd_error_handler (_("errors encountered processing file %pB"), ibfd);
16845     }
16846
16847   /* Allocate space for the glue sections now that we've sized them.  */
16848   bfd_elf32_arm_allocate_interworking_sections (info);
16849
16850   /* For every jump slot reserved in the sgotplt, reloc_count is
16851      incremented.  However, when we reserve space for TLS descriptors,
16852      it's not incremented, so in order to compute the space reserved
16853      for them, it suffices to multiply the reloc count by the jump
16854      slot size.  */
16855   if (htab->root.srelplt)
16856     htab->sgotplt_jump_table_size = elf32_arm_compute_jump_table_size(htab);
16857
16858   if (htab->tls_trampoline)
16859     {
16860       if (htab->root.splt->size == 0)
16861         htab->root.splt->size += htab->plt_header_size;
16862
16863       htab->tls_trampoline = htab->root.splt->size;
16864       htab->root.splt->size += htab->plt_entry_size;
16865
16866       /* If we're not using lazy TLS relocations, don't generate the
16867          PLT and GOT entries they require.  */
16868       if (!(info->flags & DF_BIND_NOW))
16869         {
16870           htab->dt_tlsdesc_got = htab->root.sgot->size;
16871           htab->root.sgot->size += 4;
16872
16873           htab->dt_tlsdesc_plt = htab->root.splt->size;
16874           htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
16875         }
16876     }
16877
16878   /* The check_relocs and adjust_dynamic_symbol entry points have
16879      determined the sizes of the various dynamic sections.  Allocate
16880      memory for them.  */
16881   plt = FALSE;
16882   relocs = FALSE;
16883   for (s = dynobj->sections; s != NULL; s = s->next)
16884     {
16885       const char * name;
16886
16887       if ((s->flags & SEC_LINKER_CREATED) == 0)
16888         continue;
16889
16890       /* It's OK to base decisions on the section name, because none
16891          of the dynobj section names depend upon the input files.  */
16892       name = bfd_get_section_name (dynobj, s);
16893
16894       if (s == htab->root.splt)
16895         {
16896           /* Remember whether there is a PLT.  */
16897           plt = s->size != 0;
16898         }
16899       else if (CONST_STRNEQ (name, ".rel"))
16900         {
16901           if (s->size != 0)
16902             {
16903               /* Remember whether there are any reloc sections other
16904                  than .rel(a).plt and .rela.plt.unloaded.  */
16905               if (s != htab->root.srelplt && s != htab->srelplt2)
16906                 relocs = TRUE;
16907
16908               /* We use the reloc_count field as a counter if we need
16909                  to copy relocs into the output file.  */
16910               s->reloc_count = 0;
16911             }
16912         }
16913       else if (s != htab->root.sgot
16914                && s != htab->root.sgotplt
16915                && s != htab->root.iplt
16916                && s != htab->root.igotplt
16917                && s != htab->root.sdynbss
16918                && s != htab->root.sdynrelro
16919                && s != htab->srofixup)
16920         {
16921           /* It's not one of our sections, so don't allocate space.  */
16922           continue;
16923         }
16924
16925       if (s->size == 0)
16926         {
16927           /* If we don't need this section, strip it from the
16928              output file.  This is mostly to handle .rel(a).bss and
16929              .rel(a).plt.  We must create both sections in
16930              create_dynamic_sections, because they must be created
16931              before the linker maps input sections to output
16932              sections.  The linker does that before
16933              adjust_dynamic_symbol is called, and it is that
16934              function which decides whether anything needs to go
16935              into these sections.  */
16936           s->flags |= SEC_EXCLUDE;
16937           continue;
16938         }
16939
16940       if ((s->flags & SEC_HAS_CONTENTS) == 0)
16941         continue;
16942
16943       /* Allocate memory for the section contents.  */
16944       s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
16945       if (s->contents == NULL)
16946         return FALSE;
16947     }
16948
16949   if (elf_hash_table (info)->dynamic_sections_created)
16950     {
16951       /* Add some entries to the .dynamic section.  We fill in the
16952          values later, in elf32_arm_finish_dynamic_sections, but we
16953          must add the entries now so that we get the correct size for
16954          the .dynamic section.  The DT_DEBUG entry is filled in by the
16955          dynamic linker and used by the debugger.  */
16956 #define add_dynamic_entry(TAG, VAL) \
16957   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
16958
16959      if (bfd_link_executable (info))
16960         {
16961           if (!add_dynamic_entry (DT_DEBUG, 0))
16962             return FALSE;
16963         }
16964
16965       if (plt)
16966         {
16967           if (   !add_dynamic_entry (DT_PLTGOT, 0)
16968               || !add_dynamic_entry (DT_PLTRELSZ, 0)
16969               || !add_dynamic_entry (DT_PLTREL,
16970                                      htab->use_rel ? DT_REL : DT_RELA)
16971               || !add_dynamic_entry (DT_JMPREL, 0))
16972             return FALSE;
16973
16974           if (htab->dt_tlsdesc_plt
16975               && (!add_dynamic_entry (DT_TLSDESC_PLT,0)
16976                   || !add_dynamic_entry (DT_TLSDESC_GOT,0)))
16977             return FALSE;
16978         }
16979
16980       if (relocs)
16981         {
16982           if (htab->use_rel)
16983             {
16984               if (!add_dynamic_entry (DT_REL, 0)
16985                   || !add_dynamic_entry (DT_RELSZ, 0)
16986                   || !add_dynamic_entry (DT_RELENT, RELOC_SIZE (htab)))
16987                 return FALSE;
16988             }
16989           else
16990             {
16991               if (!add_dynamic_entry (DT_RELA, 0)
16992                   || !add_dynamic_entry (DT_RELASZ, 0)
16993                   || !add_dynamic_entry (DT_RELAENT, RELOC_SIZE (htab)))
16994                 return FALSE;
16995             }
16996         }
16997
16998       /* If any dynamic relocs apply to a read-only section,
16999          then we need a DT_TEXTREL entry.  */
17000       if ((info->flags & DF_TEXTREL) == 0)
17001         elf_link_hash_traverse (&htab->root, maybe_set_textrel, info);
17002
17003       if ((info->flags & DF_TEXTREL) != 0)
17004         {
17005           if (!add_dynamic_entry (DT_TEXTREL, 0))
17006             return FALSE;
17007         }
17008       if (htab->vxworks_p
17009           && !elf_vxworks_add_dynamic_entries (output_bfd, info))
17010         return FALSE;
17011     }
17012 #undef add_dynamic_entry
17013
17014   return TRUE;
17015 }
17016
17017 /* Size sections even though they're not dynamic.  We use it to setup
17018    _TLS_MODULE_BASE_, if needed.  */
17019
17020 static bfd_boolean
17021 elf32_arm_always_size_sections (bfd *output_bfd,
17022                                 struct bfd_link_info *info)
17023 {
17024   asection *tls_sec;
17025   struct elf32_arm_link_hash_table *htab;
17026
17027   htab = elf32_arm_hash_table (info);
17028
17029   if (bfd_link_relocatable (info))
17030     return TRUE;
17031
17032   tls_sec = elf_hash_table (info)->tls_sec;
17033
17034   if (tls_sec)
17035     {
17036       struct elf_link_hash_entry *tlsbase;
17037
17038       tlsbase = elf_link_hash_lookup
17039         (elf_hash_table (info), "_TLS_MODULE_BASE_", TRUE, TRUE, FALSE);
17040
17041       if (tlsbase)
17042         {
17043           struct bfd_link_hash_entry *bh = NULL;
17044           const struct elf_backend_data *bed
17045             = get_elf_backend_data (output_bfd);
17046
17047           if (!(_bfd_generic_link_add_one_symbol
17048                 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
17049                  tls_sec, 0, NULL, FALSE,
17050                  bed->collect, &bh)))
17051             return FALSE;
17052
17053           tlsbase->type = STT_TLS;
17054           tlsbase = (struct elf_link_hash_entry *)bh;
17055           tlsbase->def_regular = 1;
17056           tlsbase->other = STV_HIDDEN;
17057           (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
17058         }
17059     }
17060
17061   if (htab->fdpic_p && !bfd_link_relocatable (info)
17062       && !bfd_elf_stack_segment_size (output_bfd, info,
17063                                       "__stacksize", DEFAULT_STACK_SIZE))
17064     return FALSE;
17065
17066   return TRUE;
17067 }
17068
17069 /* Finish up dynamic symbol handling.  We set the contents of various
17070    dynamic sections here.  */
17071
17072 static bfd_boolean
17073 elf32_arm_finish_dynamic_symbol (bfd * output_bfd,
17074                                  struct bfd_link_info * info,
17075                                  struct elf_link_hash_entry * h,
17076                                  Elf_Internal_Sym * sym)
17077 {
17078   struct elf32_arm_link_hash_table *htab;
17079   struct elf32_arm_link_hash_entry *eh;
17080
17081   htab = elf32_arm_hash_table (info);
17082   if (htab == NULL)
17083     return FALSE;
17084
17085   eh = (struct elf32_arm_link_hash_entry *) h;
17086
17087   if (h->plt.offset != (bfd_vma) -1)
17088     {
17089       if (!eh->is_iplt)
17090         {
17091           BFD_ASSERT (h->dynindx != -1);
17092           if (! elf32_arm_populate_plt_entry (output_bfd, info, &h->plt, &eh->plt,
17093                                               h->dynindx, 0))
17094             return FALSE;
17095         }
17096
17097       if (!h->def_regular)
17098         {
17099           /* Mark the symbol as undefined, rather than as defined in
17100              the .plt section.  */
17101           sym->st_shndx = SHN_UNDEF;
17102           /* If the symbol is weak we need to clear the value.
17103              Otherwise, the PLT entry would provide a definition for
17104              the symbol even if the symbol wasn't defined anywhere,
17105              and so the symbol would never be NULL.  Leave the value if
17106              there were any relocations where pointer equality matters
17107              (this is a clue for the dynamic linker, to make function
17108              pointer comparisons work between an application and shared
17109              library).  */
17110           if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
17111             sym->st_value = 0;
17112         }
17113       else if (eh->is_iplt && eh->plt.noncall_refcount != 0)
17114         {
17115           /* At least one non-call relocation references this .iplt entry,
17116              so the .iplt entry is the function's canonical address.  */
17117           sym->st_info = ELF_ST_INFO (ELF_ST_BIND (sym->st_info), STT_FUNC);
17118           ARM_SET_SYM_BRANCH_TYPE (sym->st_target_internal, ST_BRANCH_TO_ARM);
17119           sym->st_shndx = (_bfd_elf_section_from_bfd_section
17120                            (output_bfd, htab->root.iplt->output_section));
17121           sym->st_value = (h->plt.offset
17122                            + htab->root.iplt->output_section->vma
17123                            + htab->root.iplt->output_offset);
17124         }
17125     }
17126
17127   if (h->needs_copy)
17128     {
17129       asection * s;
17130       Elf_Internal_Rela rel;
17131
17132       /* This symbol needs a copy reloc.  Set it up.  */
17133       BFD_ASSERT (h->dynindx != -1
17134                   && (h->root.type == bfd_link_hash_defined
17135                       || h->root.type == bfd_link_hash_defweak));
17136
17137       rel.r_addend = 0;
17138       rel.r_offset = (h->root.u.def.value
17139                       + h->root.u.def.section->output_section->vma
17140                       + h->root.u.def.section->output_offset);
17141       rel.r_info = ELF32_R_INFO (h->dynindx, R_ARM_COPY);
17142       if (h->root.u.def.section == htab->root.sdynrelro)
17143         s = htab->root.sreldynrelro;
17144       else
17145         s = htab->root.srelbss;
17146       elf32_arm_add_dynreloc (output_bfd, info, s, &rel);
17147     }
17148
17149   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  On VxWorks,
17150      and for FDPIC, the _GLOBAL_OFFSET_TABLE_ symbol is not absolute:
17151      it is relative to the ".got" section.  */
17152   if (h == htab->root.hdynamic
17153       || (!htab->fdpic_p && !htab->vxworks_p && h == htab->root.hgot))
17154     sym->st_shndx = SHN_ABS;
17155
17156   return TRUE;
17157 }
17158
17159 static void
17160 arm_put_trampoline (struct elf32_arm_link_hash_table *htab, bfd *output_bfd,
17161                     void *contents,
17162                     const unsigned long *template, unsigned count)
17163 {
17164   unsigned ix;
17165
17166   for (ix = 0; ix != count; ix++)
17167     {
17168       unsigned long insn = template[ix];
17169
17170       /* Emit mov pc,rx if bx is not permitted.  */
17171       if (htab->fix_v4bx == 1 && (insn & 0x0ffffff0) == 0x012fff10)
17172         insn = (insn & 0xf000000f) | 0x01a0f000;
17173       put_arm_insn (htab, output_bfd, insn, (char *)contents + ix*4);
17174     }
17175 }
17176
17177 /* Install the special first PLT entry for elf32-arm-nacl.  Unlike
17178    other variants, NaCl needs this entry in a static executable's
17179    .iplt too.  When we're handling that case, GOT_DISPLACEMENT is
17180    zero.  For .iplt really only the last bundle is useful, and .iplt
17181    could have a shorter first entry, with each individual PLT entry's
17182    relative branch calculated differently so it targets the last
17183    bundle instead of the instruction before it (labelled .Lplt_tail
17184    above).  But it's simpler to keep the size and layout of PLT0
17185    consistent with the dynamic case, at the cost of some dead code at
17186    the start of .iplt and the one dead store to the stack at the start
17187    of .Lplt_tail.  */
17188 static void
17189 arm_nacl_put_plt0 (struct elf32_arm_link_hash_table *htab, bfd *output_bfd,
17190                    asection *plt, bfd_vma got_displacement)
17191 {
17192   unsigned int i;
17193
17194   put_arm_insn (htab, output_bfd,
17195                 elf32_arm_nacl_plt0_entry[0]
17196                 | arm_movw_immediate (got_displacement),
17197                 plt->contents + 0);
17198   put_arm_insn (htab, output_bfd,
17199                 elf32_arm_nacl_plt0_entry[1]
17200                 | arm_movt_immediate (got_displacement),
17201                 plt->contents + 4);
17202
17203   for (i = 2; i < ARRAY_SIZE (elf32_arm_nacl_plt0_entry); ++i)
17204     put_arm_insn (htab, output_bfd,
17205                   elf32_arm_nacl_plt0_entry[i],
17206                   plt->contents + (i * 4));
17207 }
17208
17209 /* Finish up the dynamic sections.  */
17210
17211 static bfd_boolean
17212 elf32_arm_finish_dynamic_sections (bfd * output_bfd, struct bfd_link_info * info)
17213 {
17214   bfd * dynobj;
17215   asection * sgot;
17216   asection * sdyn;
17217   struct elf32_arm_link_hash_table *htab;
17218
17219   htab = elf32_arm_hash_table (info);
17220   if (htab == NULL)
17221     return FALSE;
17222
17223   dynobj = elf_hash_table (info)->dynobj;
17224
17225   sgot = htab->root.sgotplt;
17226   /* A broken linker script might have discarded the dynamic sections.
17227      Catch this here so that we do not seg-fault later on.  */
17228   if (sgot != NULL && bfd_is_abs_section (sgot->output_section))
17229     return FALSE;
17230   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
17231
17232   if (elf_hash_table (info)->dynamic_sections_created)
17233     {
17234       asection *splt;
17235       Elf32_External_Dyn *dyncon, *dynconend;
17236
17237       splt = htab->root.splt;
17238       BFD_ASSERT (splt != NULL && sdyn != NULL);
17239       BFD_ASSERT (htab->symbian_p || sgot != NULL);
17240
17241       dyncon = (Elf32_External_Dyn *) sdyn->contents;
17242       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
17243
17244       for (; dyncon < dynconend; dyncon++)
17245         {
17246           Elf_Internal_Dyn dyn;
17247           const char * name;
17248           asection * s;
17249
17250           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
17251
17252           switch (dyn.d_tag)
17253             {
17254               unsigned int type;
17255
17256             default:
17257               if (htab->vxworks_p
17258                   && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
17259                 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
17260               break;
17261
17262             case DT_HASH:
17263               name = ".hash";
17264               goto get_vma_if_bpabi;
17265             case DT_STRTAB:
17266               name = ".dynstr";
17267               goto get_vma_if_bpabi;
17268             case DT_SYMTAB:
17269               name = ".dynsym";
17270               goto get_vma_if_bpabi;
17271             case DT_VERSYM:
17272               name = ".gnu.version";
17273               goto get_vma_if_bpabi;
17274             case DT_VERDEF:
17275               name = ".gnu.version_d";
17276               goto get_vma_if_bpabi;
17277             case DT_VERNEED:
17278               name = ".gnu.version_r";
17279               goto get_vma_if_bpabi;
17280
17281             case DT_PLTGOT:
17282               name = htab->symbian_p ? ".got" : ".got.plt";
17283               goto get_vma;
17284             case DT_JMPREL:
17285               name = RELOC_SECTION (htab, ".plt");
17286             get_vma:
17287               s = bfd_get_linker_section (dynobj, name);
17288               if (s == NULL)
17289                 {
17290                   _bfd_error_handler
17291                     (_("could not find section %s"), name);
17292                   bfd_set_error (bfd_error_invalid_operation);
17293                   return FALSE;
17294                 }
17295               if (!htab->symbian_p)
17296                 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
17297               else
17298                 /* In the BPABI, tags in the PT_DYNAMIC section point
17299                    at the file offset, not the memory address, for the
17300                    convenience of the post linker.  */
17301                 dyn.d_un.d_ptr = s->output_section->filepos + s->output_offset;
17302               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
17303               break;
17304
17305             get_vma_if_bpabi:
17306               if (htab->symbian_p)
17307                 goto get_vma;
17308               break;
17309
17310             case DT_PLTRELSZ:
17311               s = htab->root.srelplt;
17312               BFD_ASSERT (s != NULL);
17313               dyn.d_un.d_val = s->size;
17314               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
17315               break;
17316
17317             case DT_RELSZ:
17318             case DT_RELASZ:
17319             case DT_REL:
17320             case DT_RELA:
17321               /* In the BPABI, the DT_REL tag must point at the file
17322                  offset, not the VMA, of the first relocation
17323                  section.  So, we use code similar to that in
17324                  elflink.c, but do not check for SHF_ALLOC on the
17325                  relocation section, since relocation sections are
17326                  never allocated under the BPABI.  PLT relocs are also
17327                  included.  */
17328               if (htab->symbian_p)
17329                 {
17330                   unsigned int i;
17331                   type = ((dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
17332                           ? SHT_REL : SHT_RELA);
17333                   dyn.d_un.d_val = 0;
17334                   for (i = 1; i < elf_numsections (output_bfd); i++)
17335                     {
17336                       Elf_Internal_Shdr *hdr
17337                         = elf_elfsections (output_bfd)[i];
17338                       if (hdr->sh_type == type)
17339                         {
17340                           if (dyn.d_tag == DT_RELSZ
17341                               || dyn.d_tag == DT_RELASZ)
17342                             dyn.d_un.d_val += hdr->sh_size;
17343                           else if ((ufile_ptr) hdr->sh_offset
17344                                    <= dyn.d_un.d_val - 1)
17345                             dyn.d_un.d_val = hdr->sh_offset;
17346                         }
17347                     }
17348                   bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
17349                 }
17350               break;
17351
17352             case DT_TLSDESC_PLT:
17353               s = htab->root.splt;
17354               dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
17355                                 + htab->dt_tlsdesc_plt);
17356               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
17357               break;
17358
17359             case DT_TLSDESC_GOT:
17360               s = htab->root.sgot;
17361               dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
17362                                 + htab->dt_tlsdesc_got);
17363               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
17364               break;
17365
17366               /* Set the bottom bit of DT_INIT/FINI if the
17367                  corresponding function is Thumb.  */
17368             case DT_INIT:
17369               name = info->init_function;
17370               goto get_sym;
17371             case DT_FINI:
17372               name = info->fini_function;
17373             get_sym:
17374               /* If it wasn't set by elf_bfd_final_link
17375                  then there is nothing to adjust.  */
17376               if (dyn.d_un.d_val != 0)
17377                 {
17378                   struct elf_link_hash_entry * eh;
17379
17380                   eh = elf_link_hash_lookup (elf_hash_table (info), name,
17381                                              FALSE, FALSE, TRUE);
17382                   if (eh != NULL
17383                       && ARM_GET_SYM_BRANCH_TYPE (eh->target_internal)
17384                          == ST_BRANCH_TO_THUMB)
17385                     {
17386                       dyn.d_un.d_val |= 1;
17387                       bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
17388                     }
17389                 }
17390               break;
17391             }
17392         }
17393
17394       /* Fill in the first entry in the procedure linkage table.  */
17395       if (splt->size > 0 && htab->plt_header_size)
17396         {
17397           const bfd_vma *plt0_entry;
17398           bfd_vma got_address, plt_address, got_displacement;
17399
17400           /* Calculate the addresses of the GOT and PLT.  */
17401           got_address = sgot->output_section->vma + sgot->output_offset;
17402           plt_address = splt->output_section->vma + splt->output_offset;
17403
17404           if (htab->vxworks_p)
17405             {
17406               /* The VxWorks GOT is relocated by the dynamic linker.
17407                  Therefore, we must emit relocations rather than simply
17408                  computing the values now.  */
17409               Elf_Internal_Rela rel;
17410
17411               plt0_entry = elf32_arm_vxworks_exec_plt0_entry;
17412               put_arm_insn (htab, output_bfd, plt0_entry[0],
17413                             splt->contents + 0);
17414               put_arm_insn (htab, output_bfd, plt0_entry[1],
17415                             splt->contents + 4);
17416               put_arm_insn (htab, output_bfd, plt0_entry[2],
17417                             splt->contents + 8);
17418               bfd_put_32 (output_bfd, got_address, splt->contents + 12);
17419
17420               /* Generate a relocation for _GLOBAL_OFFSET_TABLE_.  */
17421               rel.r_offset = plt_address + 12;
17422               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
17423               rel.r_addend = 0;
17424               SWAP_RELOC_OUT (htab) (output_bfd, &rel,
17425                                      htab->srelplt2->contents);
17426             }
17427           else if (htab->nacl_p)
17428             arm_nacl_put_plt0 (htab, output_bfd, splt,
17429                                got_address + 8 - (plt_address + 16));
17430           else if (using_thumb_only (htab))
17431             {
17432               got_displacement = got_address - (plt_address + 12);
17433
17434               plt0_entry = elf32_thumb2_plt0_entry;
17435               put_arm_insn (htab, output_bfd, plt0_entry[0],
17436                             splt->contents + 0);
17437               put_arm_insn (htab, output_bfd, plt0_entry[1],
17438                             splt->contents + 4);
17439               put_arm_insn (htab, output_bfd, plt0_entry[2],
17440                             splt->contents + 8);
17441
17442               bfd_put_32 (output_bfd, got_displacement, splt->contents + 12);
17443             }
17444           else
17445             {
17446               got_displacement = got_address - (plt_address + 16);
17447
17448               plt0_entry = elf32_arm_plt0_entry;
17449               put_arm_insn (htab, output_bfd, plt0_entry[0],
17450                             splt->contents + 0);
17451               put_arm_insn (htab, output_bfd, plt0_entry[1],
17452                             splt->contents + 4);
17453               put_arm_insn (htab, output_bfd, plt0_entry[2],
17454                             splt->contents + 8);
17455               put_arm_insn (htab, output_bfd, plt0_entry[3],
17456                             splt->contents + 12);
17457
17458 #ifdef FOUR_WORD_PLT
17459               /* The displacement value goes in the otherwise-unused
17460                  last word of the second entry.  */
17461               bfd_put_32 (output_bfd, got_displacement, splt->contents + 28);
17462 #else
17463               bfd_put_32 (output_bfd, got_displacement, splt->contents + 16);
17464 #endif
17465             }
17466         }
17467
17468       /* UnixWare sets the entsize of .plt to 4, although that doesn't
17469          really seem like the right value.  */
17470       if (splt->output_section->owner == output_bfd)
17471         elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
17472
17473       if (htab->dt_tlsdesc_plt)
17474         {
17475           bfd_vma got_address
17476             = sgot->output_section->vma + sgot->output_offset;
17477           bfd_vma gotplt_address = (htab->root.sgot->output_section->vma
17478                                     + htab->root.sgot->output_offset);
17479           bfd_vma plt_address
17480             = splt->output_section->vma + splt->output_offset;
17481
17482           arm_put_trampoline (htab, output_bfd,
17483                               splt->contents + htab->dt_tlsdesc_plt,
17484                               dl_tlsdesc_lazy_trampoline, 6);
17485
17486           bfd_put_32 (output_bfd,
17487                       gotplt_address + htab->dt_tlsdesc_got
17488                       - (plt_address + htab->dt_tlsdesc_plt)
17489                       - dl_tlsdesc_lazy_trampoline[6],
17490                       splt->contents + htab->dt_tlsdesc_plt + 24);
17491           bfd_put_32 (output_bfd,
17492                       got_address - (plt_address + htab->dt_tlsdesc_plt)
17493                       - dl_tlsdesc_lazy_trampoline[7],
17494                       splt->contents + htab->dt_tlsdesc_plt + 24 + 4);
17495         }
17496
17497       if (htab->tls_trampoline)
17498         {
17499           arm_put_trampoline (htab, output_bfd,
17500                               splt->contents + htab->tls_trampoline,
17501                               tls_trampoline, 3);
17502 #ifdef FOUR_WORD_PLT
17503           bfd_put_32 (output_bfd, 0x00000000,
17504                       splt->contents + htab->tls_trampoline + 12);
17505 #endif
17506         }
17507
17508       if (htab->vxworks_p
17509           && !bfd_link_pic (info)
17510           && htab->root.splt->size > 0)
17511         {
17512           /* Correct the .rel(a).plt.unloaded relocations.  They will have
17513              incorrect symbol indexes.  */
17514           int num_plts;
17515           unsigned char *p;
17516
17517           num_plts = ((htab->root.splt->size - htab->plt_header_size)
17518                       / htab->plt_entry_size);
17519           p = htab->srelplt2->contents + RELOC_SIZE (htab);
17520
17521           for (; num_plts; num_plts--)
17522             {
17523               Elf_Internal_Rela rel;
17524
17525               SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
17526               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_ARM_ABS32);
17527               SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
17528               p += RELOC_SIZE (htab);
17529
17530               SWAP_RELOC_IN (htab) (output_bfd, p, &rel);
17531               rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_ARM_ABS32);
17532               SWAP_RELOC_OUT (htab) (output_bfd, &rel, p);
17533               p += RELOC_SIZE (htab);
17534             }
17535         }
17536     }
17537
17538   if (htab->nacl_p && htab->root.iplt != NULL && htab->root.iplt->size > 0)
17539     /* NaCl uses a special first entry in .iplt too.  */
17540     arm_nacl_put_plt0 (htab, output_bfd, htab->root.iplt, 0);
17541
17542   /* Fill in the first three entries in the global offset table.  */
17543   if (sgot)
17544     {
17545       if (sgot->size > 0)
17546         {
17547           if (sdyn == NULL)
17548             bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
17549           else
17550             bfd_put_32 (output_bfd,
17551                         sdyn->output_section->vma + sdyn->output_offset,
17552                         sgot->contents);
17553           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
17554           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
17555         }
17556
17557       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
17558     }
17559
17560   /* At the very end of the .rofixup section is a pointer to the GOT.  */
17561   if (htab->fdpic_p && htab->srofixup != NULL)
17562     {
17563       struct elf_link_hash_entry *hgot = htab->root.hgot;
17564
17565       bfd_vma got_value = hgot->root.u.def.value
17566         + hgot->root.u.def.section->output_section->vma
17567         + hgot->root.u.def.section->output_offset;
17568
17569       arm_elf_add_rofixup(output_bfd, htab->srofixup, got_value);
17570
17571       /* Make sure we allocated and generated the same number of fixups.  */
17572       BFD_ASSERT (htab->srofixup->reloc_count * 4 == htab->srofixup->size);
17573     }
17574
17575   return TRUE;
17576 }
17577
17578 static void
17579 elf32_arm_post_process_headers (bfd * abfd, struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
17580 {
17581   Elf_Internal_Ehdr * i_ehdrp;  /* ELF file header, internal form.  */
17582   struct elf32_arm_link_hash_table *globals;
17583   struct elf_segment_map *m;
17584
17585   i_ehdrp = elf_elfheader (abfd);
17586
17587   if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_UNKNOWN)
17588     i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_ARM;
17589   else
17590     _bfd_elf_post_process_headers (abfd, link_info);
17591   i_ehdrp->e_ident[EI_ABIVERSION] = ARM_ELF_ABI_VERSION;
17592
17593   if (link_info)
17594     {
17595       globals = elf32_arm_hash_table (link_info);
17596       if (globals != NULL && globals->byteswap_code)
17597         i_ehdrp->e_flags |= EF_ARM_BE8;
17598
17599       if (globals->fdpic_p)
17600         i_ehdrp->e_ident[EI_OSABI] |= ELFOSABI_ARM_FDPIC;
17601     }
17602
17603   if (EF_ARM_EABI_VERSION (i_ehdrp->e_flags) == EF_ARM_EABI_VER5
17604       && ((i_ehdrp->e_type == ET_DYN) || (i_ehdrp->e_type == ET_EXEC)))
17605     {
17606       int abi = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC, Tag_ABI_VFP_args);
17607       if (abi == AEABI_VFP_args_vfp)
17608         i_ehdrp->e_flags |= EF_ARM_ABI_FLOAT_HARD;
17609       else
17610         i_ehdrp->e_flags |= EF_ARM_ABI_FLOAT_SOFT;
17611     }
17612
17613   /* Scan segment to set p_flags attribute if it contains only sections with
17614      SHF_ARM_PURECODE flag.  */
17615   for (m = elf_seg_map (abfd); m != NULL; m = m->next)
17616     {
17617       unsigned int j;
17618
17619       if (m->count == 0)
17620         continue;
17621       for (j = 0; j < m->count; j++)
17622         {
17623           if (!(elf_section_flags (m->sections[j]) & SHF_ARM_PURECODE))
17624             break;
17625         }
17626       if (j == m->count)
17627         {
17628           m->p_flags = PF_X;
17629           m->p_flags_valid = 1;
17630         }
17631     }
17632 }
17633
17634 static enum elf_reloc_type_class
17635 elf32_arm_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
17636                             const asection *rel_sec ATTRIBUTE_UNUSED,
17637                             const Elf_Internal_Rela *rela)
17638 {
17639   switch ((int) ELF32_R_TYPE (rela->r_info))
17640     {
17641     case R_ARM_RELATIVE:
17642       return reloc_class_relative;
17643     case R_ARM_JUMP_SLOT:
17644       return reloc_class_plt;
17645     case R_ARM_COPY:
17646       return reloc_class_copy;
17647     case R_ARM_IRELATIVE:
17648       return reloc_class_ifunc;
17649     default:
17650       return reloc_class_normal;
17651     }
17652 }
17653
17654 static void
17655 elf32_arm_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
17656 {
17657   bfd_arm_update_notes (abfd, ARM_NOTE_SECTION);
17658 }
17659
17660 /* Return TRUE if this is an unwinding table entry.  */
17661
17662 static bfd_boolean
17663 is_arm_elf_unwind_section_name (bfd * abfd ATTRIBUTE_UNUSED, const char * name)
17664 {
17665   return (CONST_STRNEQ (name, ELF_STRING_ARM_unwind)
17666           || CONST_STRNEQ (name, ELF_STRING_ARM_unwind_once));
17667 }
17668
17669
17670 /* Set the type and flags for an ARM section.  We do this by
17671    the section name, which is a hack, but ought to work.  */
17672
17673 static bfd_boolean
17674 elf32_arm_fake_sections (bfd * abfd, Elf_Internal_Shdr * hdr, asection * sec)
17675 {
17676   const char * name;
17677
17678   name = bfd_get_section_name (abfd, sec);
17679
17680   if (is_arm_elf_unwind_section_name (abfd, name))
17681     {
17682       hdr->sh_type = SHT_ARM_EXIDX;
17683       hdr->sh_flags |= SHF_LINK_ORDER;
17684     }
17685
17686   if (sec->flags & SEC_ELF_PURECODE)
17687     hdr->sh_flags |= SHF_ARM_PURECODE;
17688
17689   return TRUE;
17690 }
17691
17692 /* Handle an ARM specific section when reading an object file.  This is
17693    called when bfd_section_from_shdr finds a section with an unknown
17694    type.  */
17695
17696 static bfd_boolean
17697 elf32_arm_section_from_shdr (bfd *abfd,
17698                              Elf_Internal_Shdr * hdr,
17699                              const char *name,
17700                              int shindex)
17701 {
17702   /* There ought to be a place to keep ELF backend specific flags, but
17703      at the moment there isn't one.  We just keep track of the
17704      sections by their name, instead.  Fortunately, the ABI gives
17705      names for all the ARM specific sections, so we will probably get
17706      away with this.  */
17707   switch (hdr->sh_type)
17708     {
17709     case SHT_ARM_EXIDX:
17710     case SHT_ARM_PREEMPTMAP:
17711     case SHT_ARM_ATTRIBUTES:
17712       break;
17713
17714     default:
17715       return FALSE;
17716     }
17717
17718   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
17719     return FALSE;
17720
17721   return TRUE;
17722 }
17723
17724 static _arm_elf_section_data *
17725 get_arm_elf_section_data (asection * sec)
17726 {
17727   if (sec && sec->owner && is_arm_elf (sec->owner))
17728     return elf32_arm_section_data (sec);
17729   else
17730     return NULL;
17731 }
17732
17733 typedef struct
17734 {
17735   void *flaginfo;
17736   struct bfd_link_info *info;
17737   asection *sec;
17738   int sec_shndx;
17739   int (*func) (void *, const char *, Elf_Internal_Sym *,
17740                asection *, struct elf_link_hash_entry *);
17741 } output_arch_syminfo;
17742
17743 enum map_symbol_type
17744 {
17745   ARM_MAP_ARM,
17746   ARM_MAP_THUMB,
17747   ARM_MAP_DATA
17748 };
17749
17750
17751 /* Output a single mapping symbol.  */
17752
17753 static bfd_boolean
17754 elf32_arm_output_map_sym (output_arch_syminfo *osi,
17755                           enum map_symbol_type type,
17756                           bfd_vma offset)
17757 {
17758   static const char *names[3] = {"$a", "$t", "$d"};
17759   Elf_Internal_Sym sym;
17760
17761   sym.st_value = osi->sec->output_section->vma
17762                  + osi->sec->output_offset
17763                  + offset;
17764   sym.st_size = 0;
17765   sym.st_other = 0;
17766   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
17767   sym.st_shndx = osi->sec_shndx;
17768   sym.st_target_internal = 0;
17769   elf32_arm_section_map_add (osi->sec, names[type][1], offset);
17770   return osi->func (osi->flaginfo, names[type], &sym, osi->sec, NULL) == 1;
17771 }
17772
17773 /* Output mapping symbols for the PLT entry described by ROOT_PLT and ARM_PLT.
17774    IS_IPLT_ENTRY_P says whether the PLT is in .iplt rather than .plt.  */
17775
17776 static bfd_boolean
17777 elf32_arm_output_plt_map_1 (output_arch_syminfo *osi,
17778                             bfd_boolean is_iplt_entry_p,
17779                             union gotplt_union *root_plt,
17780                             struct arm_plt_info *arm_plt)
17781 {
17782   struct elf32_arm_link_hash_table *htab;
17783   bfd_vma addr, plt_header_size;
17784
17785   if (root_plt->offset == (bfd_vma) -1)
17786     return TRUE;
17787
17788   htab = elf32_arm_hash_table (osi->info);
17789   if (htab == NULL)
17790     return FALSE;
17791
17792   if (is_iplt_entry_p)
17793     {
17794       osi->sec = htab->root.iplt;
17795       plt_header_size = 0;
17796     }
17797   else
17798     {
17799       osi->sec = htab->root.splt;
17800       plt_header_size = htab->plt_header_size;
17801     }
17802   osi->sec_shndx = (_bfd_elf_section_from_bfd_section
17803                     (osi->info->output_bfd, osi->sec->output_section));
17804
17805   addr = root_plt->offset & -2;
17806   if (htab->symbian_p)
17807     {
17808       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
17809         return FALSE;
17810       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 4))
17811         return FALSE;
17812     }
17813   else if (htab->vxworks_p)
17814     {
17815       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
17816         return FALSE;
17817       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 8))
17818         return FALSE;
17819       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr + 12))
17820         return FALSE;
17821       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 20))
17822         return FALSE;
17823     }
17824   else if (htab->nacl_p)
17825     {
17826       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
17827         return FALSE;
17828     }
17829   else if (htab->fdpic_p)
17830     {
17831       enum map_symbol_type type = using_thumb_only(htab)
17832         ? ARM_MAP_THUMB
17833         : ARM_MAP_ARM;
17834
17835       if (elf32_arm_plt_needs_thumb_stub_p (osi->info, arm_plt))
17836         if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr - 4))
17837           return FALSE;
17838       if (!elf32_arm_output_map_sym (osi, type, addr))
17839         return FALSE;
17840       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 16))
17841         return FALSE;
17842       if (htab->plt_entry_size == 4 * ARRAY_SIZE(elf32_arm_fdpic_plt_entry))
17843         if (!elf32_arm_output_map_sym (osi, type, addr + 24))
17844           return FALSE;
17845     }
17846   else if (using_thumb_only (htab))
17847     {
17848       if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr))
17849         return FALSE;
17850     }
17851   else
17852     {
17853       bfd_boolean thumb_stub_p;
17854
17855       thumb_stub_p = elf32_arm_plt_needs_thumb_stub_p (osi->info, arm_plt);
17856       if (thumb_stub_p)
17857         {
17858           if (!elf32_arm_output_map_sym (osi, ARM_MAP_THUMB, addr - 4))
17859             return FALSE;
17860         }
17861 #ifdef FOUR_WORD_PLT
17862       if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
17863         return FALSE;
17864       if (!elf32_arm_output_map_sym (osi, ARM_MAP_DATA, addr + 12))
17865         return FALSE;
17866 #else
17867       /* A three-word PLT with no Thumb thunk contains only Arm code,
17868          so only need to output a mapping symbol for the first PLT entry and
17869          entries with thumb thunks.  */
17870       if (thumb_stub_p || addr == plt_header_size)
17871         {
17872           if (!elf32_arm_output_map_sym (osi, ARM_MAP_ARM, addr))
17873             return FALSE;
17874         }
17875 #endif
17876     }
17877
17878   return TRUE;
17879 }
17880
17881 /* Output mapping symbols for PLT entries associated with H.  */
17882
17883 static bfd_boolean
17884 elf32_arm_output_plt_map (struct elf_link_hash_entry *h, void *inf)
17885 {
17886   output_arch_syminfo *osi = (output_arch_syminfo *) inf;
17887   struct elf32_arm_link_hash_entry *eh;
17888
17889   if (h->root.type == bfd_link_hash_indirect)
17890     return TRUE;
17891
17892   if (h->root.type == bfd_link_hash_warning)
17893     /* When warning symbols are created, they **replace** the "real"
17894        entry in the hash table, thus we never get to see the real
17895        symbol in a hash traversal.  So look at it now.  */
17896     h = (struct elf_link_hash_entry *) h->root.u.i.link;
17897
17898   eh = (struct elf32_arm_link_hash_entry *) h;
17899   return elf32_arm_output_plt_map_1 (osi, SYMBOL_CALLS_LOCAL (osi->info, h),
17900                                      &h->plt, &eh->plt);
17901 }
17902
17903 /* Bind a veneered symbol to its veneer identified by its hash entry
17904    STUB_ENTRY.  The veneered location thus loose its symbol.  */
17905
17906 static void
17907 arm_stub_claim_sym (struct elf32_arm_stub_hash_entry *stub_entry)
17908 {
17909   struct elf32_arm_link_hash_entry *hash = stub_entry->h;
17910
17911   BFD_ASSERT (hash);
17912   hash->root.root.u.def.section = stub_entry->stub_sec;
17913   hash->root.root.u.def.value = stub_entry->stub_offset;
17914   hash->root.size = stub_entry->stub_size;
17915 }
17916
17917 /* Output a single local symbol for a generated stub.  */
17918
17919 static bfd_boolean
17920 elf32_arm_output_stub_sym (output_arch_syminfo *osi, const char *name,
17921                            bfd_vma offset, bfd_vma size)
17922 {
17923   Elf_Internal_Sym sym;
17924
17925   sym.st_value = osi->sec->output_section->vma
17926                  + osi->sec->output_offset
17927                  + offset;
17928   sym.st_size = size;
17929   sym.st_other = 0;
17930   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
17931   sym.st_shndx = osi->sec_shndx;
17932   sym.st_target_internal = 0;
17933   return osi->func (osi->flaginfo, name, &sym, osi->sec, NULL) == 1;
17934 }
17935
17936 static bfd_boolean
17937 arm_map_one_stub (struct bfd_hash_entry * gen_entry,
17938                   void * in_arg)
17939 {
17940   struct elf32_arm_stub_hash_entry *stub_entry;
17941   asection *stub_sec;
17942   bfd_vma addr;
17943   char *stub_name;
17944   output_arch_syminfo *osi;
17945   const insn_sequence *template_sequence;
17946   enum stub_insn_type prev_type;
17947   int size;
17948   int i;
17949   enum map_symbol_type sym_type;
17950
17951   /* Massage our args to the form they really have.  */
17952   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
17953   osi = (output_arch_syminfo *) in_arg;
17954
17955   stub_sec = stub_entry->stub_sec;
17956
17957   /* Ensure this stub is attached to the current section being
17958      processed.  */
17959   if (stub_sec != osi->sec)
17960     return TRUE;
17961
17962   addr = (bfd_vma) stub_entry->stub_offset;
17963   template_sequence = stub_entry->stub_template;
17964
17965   if (arm_stub_sym_claimed (stub_entry->stub_type))
17966     arm_stub_claim_sym (stub_entry);
17967   else
17968     {
17969       stub_name = stub_entry->output_name;
17970       switch (template_sequence[0].type)
17971         {
17972         case ARM_TYPE:
17973           if (!elf32_arm_output_stub_sym (osi, stub_name, addr,
17974                                           stub_entry->stub_size))
17975             return FALSE;
17976           break;
17977         case THUMB16_TYPE:
17978         case THUMB32_TYPE:
17979           if (!elf32_arm_output_stub_sym (osi, stub_name, addr | 1,
17980                                           stub_entry->stub_size))
17981             return FALSE;
17982           break;
17983         default:
17984           BFD_FAIL ();
17985           return 0;
17986         }
17987     }
17988
17989   prev_type = DATA_TYPE;
17990   size = 0;
17991   for (i = 0; i < stub_entry->stub_template_size; i++)
17992     {
17993       switch (template_sequence[i].type)
17994         {
17995         case ARM_TYPE:
17996           sym_type = ARM_MAP_ARM;
17997           break;
17998
17999         case THUMB16_TYPE:
18000         case THUMB32_TYPE:
18001           sym_type = ARM_MAP_THUMB;
18002           break;
18003
18004         case DATA_TYPE:
18005           sym_type = ARM_MAP_DATA;
18006           break;
18007
18008         default:
18009           BFD_FAIL ();
18010           return FALSE;
18011         }
18012
18013       if (template_sequence[i].type != prev_type)
18014         {
18015           prev_type = template_sequence[i].type;
18016           if (!elf32_arm_output_map_sym (osi, sym_type, addr + size))
18017             return FALSE;
18018         }
18019
18020       switch (template_sequence[i].type)
18021         {
18022         case ARM_TYPE:
18023         case THUMB32_TYPE:
18024           size += 4;
18025           break;
18026
18027         case THUMB16_TYPE:
18028           size += 2;
18029           break;
18030
18031         case DATA_TYPE:
18032           size += 4;
18033           break;
18034
18035         default:
18036           BFD_FAIL ();
18037           return FALSE;
18038         }
18039     }
18040
18041   return TRUE;
18042 }
18043
18044 /* Output mapping symbols for linker generated sections,
18045    and for those data-only sections that do not have a
18046    $d.  */
18047
18048 static bfd_boolean
18049 elf32_arm_output_arch_local_syms (bfd *output_bfd,
18050                                   struct bfd_link_info *info,
18051                                   void *flaginfo,
18052                                   int (*func) (void *, const char *,
18053                                                Elf_Internal_Sym *,
18054                                                asection *,
18055                                                struct elf_link_hash_entry *))
18056 {
18057   output_arch_syminfo osi;
18058   struct elf32_arm_link_hash_table *htab;
18059   bfd_vma offset;
18060   bfd_size_type size;
18061   bfd *input_bfd;
18062
18063   htab = elf32_arm_hash_table (info);
18064   if (htab == NULL)
18065     return FALSE;
18066
18067   check_use_blx (htab);
18068
18069   osi.flaginfo = flaginfo;
18070   osi.info = info;
18071   osi.func = func;
18072
18073   /* Add a $d mapping symbol to data-only sections that
18074      don't have any mapping symbol.  This may result in (harmless) redundant
18075      mapping symbols.  */
18076   for (input_bfd = info->input_bfds;
18077        input_bfd != NULL;
18078        input_bfd = input_bfd->link.next)
18079     {
18080       if ((input_bfd->flags & (BFD_LINKER_CREATED | HAS_SYMS)) == HAS_SYMS)
18081         for (osi.sec = input_bfd->sections;
18082              osi.sec != NULL;
18083              osi.sec = osi.sec->next)
18084           {
18085             if (osi.sec->output_section != NULL
18086                 && ((osi.sec->output_section->flags & (SEC_ALLOC | SEC_CODE))
18087                     != 0)
18088                 && (osi.sec->flags & (SEC_HAS_CONTENTS | SEC_LINKER_CREATED))
18089                    == SEC_HAS_CONTENTS
18090                 && get_arm_elf_section_data (osi.sec) != NULL
18091                 && get_arm_elf_section_data (osi.sec)->mapcount == 0
18092                 && osi.sec->size > 0
18093                 && (osi.sec->flags & SEC_EXCLUDE) == 0)
18094               {
18095                 osi.sec_shndx = _bfd_elf_section_from_bfd_section
18096                   (output_bfd, osi.sec->output_section);
18097                 if (osi.sec_shndx != (int)SHN_BAD)
18098                   elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 0);
18099               }
18100           }
18101     }
18102
18103   /* ARM->Thumb glue.  */
18104   if (htab->arm_glue_size > 0)
18105     {
18106       osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
18107                                         ARM2THUMB_GLUE_SECTION_NAME);
18108
18109       osi.sec_shndx = _bfd_elf_section_from_bfd_section
18110           (output_bfd, osi.sec->output_section);
18111       if (bfd_link_pic (info) || htab->root.is_relocatable_executable
18112           || htab->pic_veneer)
18113         size = ARM2THUMB_PIC_GLUE_SIZE;
18114       else if (htab->use_blx)
18115         size = ARM2THUMB_V5_STATIC_GLUE_SIZE;
18116       else
18117         size = ARM2THUMB_STATIC_GLUE_SIZE;
18118
18119       for (offset = 0; offset < htab->arm_glue_size; offset += size)
18120         {
18121           elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, offset);
18122           elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, offset + size - 4);
18123         }
18124     }
18125
18126   /* Thumb->ARM glue.  */
18127   if (htab->thumb_glue_size > 0)
18128     {
18129       osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
18130                                         THUMB2ARM_GLUE_SECTION_NAME);
18131
18132       osi.sec_shndx = _bfd_elf_section_from_bfd_section
18133           (output_bfd, osi.sec->output_section);
18134       size = THUMB2ARM_GLUE_SIZE;
18135
18136       for (offset = 0; offset < htab->thumb_glue_size; offset += size)
18137         {
18138           elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, offset);
18139           elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, offset + 4);
18140         }
18141     }
18142
18143   /* ARMv4 BX veneers.  */
18144   if (htab->bx_glue_size > 0)
18145     {
18146       osi.sec = bfd_get_linker_section (htab->bfd_of_glue_owner,
18147                                         ARM_BX_GLUE_SECTION_NAME);
18148
18149       osi.sec_shndx = _bfd_elf_section_from_bfd_section
18150           (output_bfd, osi.sec->output_section);
18151
18152       elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0);
18153     }
18154
18155   /* Long calls stubs.  */
18156   if (htab->stub_bfd && htab->stub_bfd->sections)
18157     {
18158       asection* stub_sec;
18159
18160       for (stub_sec = htab->stub_bfd->sections;
18161            stub_sec != NULL;
18162            stub_sec = stub_sec->next)
18163         {
18164           /* Ignore non-stub sections.  */
18165           if (!strstr (stub_sec->name, STUB_SUFFIX))
18166             continue;
18167
18168           osi.sec = stub_sec;
18169
18170           osi.sec_shndx = _bfd_elf_section_from_bfd_section
18171             (output_bfd, osi.sec->output_section);
18172
18173           bfd_hash_traverse (&htab->stub_hash_table, arm_map_one_stub, &osi);
18174         }
18175     }
18176
18177   /* Finally, output mapping symbols for the PLT.  */
18178   if (htab->root.splt && htab->root.splt->size > 0)
18179     {
18180       osi.sec = htab->root.splt;
18181       osi.sec_shndx = (_bfd_elf_section_from_bfd_section
18182                        (output_bfd, osi.sec->output_section));
18183
18184       /* Output mapping symbols for the plt header.  SymbianOS does not have a
18185          plt header.  */
18186       if (htab->vxworks_p)
18187         {
18188           /* VxWorks shared libraries have no PLT header.  */
18189           if (!bfd_link_pic (info))
18190             {
18191               if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
18192                 return FALSE;
18193               if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 12))
18194                 return FALSE;
18195             }
18196         }
18197       else if (htab->nacl_p)
18198         {
18199           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
18200             return FALSE;
18201         }
18202       else if (using_thumb_only (htab) && !htab->fdpic_p)
18203         {
18204           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, 0))
18205             return FALSE;
18206           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 12))
18207             return FALSE;
18208           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_THUMB, 16))
18209             return FALSE;
18210         }
18211       else if (!htab->symbian_p && !htab->fdpic_p)
18212         {
18213           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
18214             return FALSE;
18215 #ifndef FOUR_WORD_PLT
18216           if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA, 16))
18217             return FALSE;
18218 #endif
18219         }
18220     }
18221   if (htab->nacl_p && htab->root.iplt && htab->root.iplt->size > 0)
18222     {
18223       /* NaCl uses a special first entry in .iplt too.  */
18224       osi.sec = htab->root.iplt;
18225       osi.sec_shndx = (_bfd_elf_section_from_bfd_section
18226                        (output_bfd, osi.sec->output_section));
18227       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, 0))
18228         return FALSE;
18229     }
18230   if ((htab->root.splt && htab->root.splt->size > 0)
18231       || (htab->root.iplt && htab->root.iplt->size > 0))
18232     {
18233       elf_link_hash_traverse (&htab->root, elf32_arm_output_plt_map, &osi);
18234       for (input_bfd = info->input_bfds;
18235            input_bfd != NULL;
18236            input_bfd = input_bfd->link.next)
18237         {
18238           struct arm_local_iplt_info **local_iplt;
18239           unsigned int i, num_syms;
18240
18241           local_iplt = elf32_arm_local_iplt (input_bfd);
18242           if (local_iplt != NULL)
18243             {
18244               num_syms = elf_symtab_hdr (input_bfd).sh_info;
18245               for (i = 0; i < num_syms; i++)
18246                 if (local_iplt[i] != NULL
18247                     && !elf32_arm_output_plt_map_1 (&osi, TRUE,
18248                                                     &local_iplt[i]->root,
18249                                                     &local_iplt[i]->arm))
18250                   return FALSE;
18251             }
18252         }
18253     }
18254   if (htab->dt_tlsdesc_plt != 0)
18255     {
18256       /* Mapping symbols for the lazy tls trampoline.  */
18257       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, htab->dt_tlsdesc_plt))
18258         return FALSE;
18259
18260       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA,
18261                                      htab->dt_tlsdesc_plt + 24))
18262         return FALSE;
18263     }
18264   if (htab->tls_trampoline != 0)
18265     {
18266       /* Mapping symbols for the tls trampoline.  */
18267       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_ARM, htab->tls_trampoline))
18268         return FALSE;
18269 #ifdef FOUR_WORD_PLT
18270       if (!elf32_arm_output_map_sym (&osi, ARM_MAP_DATA,
18271                                      htab->tls_trampoline + 12))
18272         return FALSE;
18273 #endif
18274     }
18275
18276   return TRUE;
18277 }
18278
18279 /* Filter normal symbols of CMSE entry functions of ABFD to include in
18280    the import library.  All SYMCOUNT symbols of ABFD can be examined
18281    from their pointers in SYMS.  Pointers of symbols to keep should be
18282    stored continuously at the beginning of that array.
18283
18284    Returns the number of symbols to keep.  */
18285
18286 static unsigned int
18287 elf32_arm_filter_cmse_symbols (bfd *abfd ATTRIBUTE_UNUSED,
18288                                struct bfd_link_info *info,
18289                                asymbol **syms, long symcount)
18290 {
18291   size_t maxnamelen;
18292   char *cmse_name;
18293   long src_count, dst_count = 0;
18294   struct elf32_arm_link_hash_table *htab;
18295
18296   htab = elf32_arm_hash_table (info);
18297   if (!htab->stub_bfd || !htab->stub_bfd->sections)
18298     symcount = 0;
18299
18300   maxnamelen = 128;
18301   cmse_name = (char *) bfd_malloc (maxnamelen);
18302   for (src_count = 0; src_count < symcount; src_count++)
18303     {
18304       struct elf32_arm_link_hash_entry *cmse_hash;
18305       asymbol *sym;
18306       flagword flags;
18307       char *name;
18308       size_t namelen;
18309
18310       sym = syms[src_count];
18311       flags = sym->flags;
18312       name = (char *) bfd_asymbol_name (sym);
18313
18314       if ((flags & BSF_FUNCTION) != BSF_FUNCTION)
18315         continue;
18316       if (!(flags & (BSF_GLOBAL | BSF_WEAK)))
18317         continue;
18318
18319       namelen = strlen (name) + sizeof (CMSE_PREFIX) + 1;
18320       if (namelen > maxnamelen)
18321         {
18322           cmse_name = (char *)
18323             bfd_realloc (cmse_name, namelen);
18324           maxnamelen = namelen;
18325         }
18326       snprintf (cmse_name, maxnamelen, "%s%s", CMSE_PREFIX, name);
18327       cmse_hash = (struct elf32_arm_link_hash_entry *)
18328         elf_link_hash_lookup (&(htab)->root, cmse_name, FALSE, FALSE, TRUE);
18329
18330       if (!cmse_hash
18331           || (cmse_hash->root.root.type != bfd_link_hash_defined
18332               && cmse_hash->root.root.type != bfd_link_hash_defweak)
18333           || cmse_hash->root.type != STT_FUNC)
18334         continue;
18335
18336       if (!ARM_GET_SYM_CMSE_SPCL (cmse_hash->root.target_internal))
18337         continue;
18338
18339       syms[dst_count++] = sym;
18340     }
18341   free (cmse_name);
18342
18343   syms[dst_count] = NULL;
18344
18345   return dst_count;
18346 }
18347
18348 /* Filter symbols of ABFD to include in the import library.  All
18349    SYMCOUNT symbols of ABFD can be examined from their pointers in
18350    SYMS.  Pointers of symbols to keep should be stored continuously at
18351    the beginning of that array.
18352
18353    Returns the number of symbols to keep.  */
18354
18355 static unsigned int
18356 elf32_arm_filter_implib_symbols (bfd *abfd ATTRIBUTE_UNUSED,
18357                                  struct bfd_link_info *info,
18358                                  asymbol **syms, long symcount)
18359 {
18360   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
18361
18362   /* Requirement 8 of "ARM v8-M Security Extensions: Requirements on
18363      Development Tools" (ARM-ECM-0359818) mandates Secure Gateway import
18364      library to be a relocatable object file.  */
18365   BFD_ASSERT (!(bfd_get_file_flags (info->out_implib_bfd) & EXEC_P));
18366   if (globals->cmse_implib)
18367     return elf32_arm_filter_cmse_symbols (abfd, info, syms, symcount);
18368   else
18369     return _bfd_elf_filter_global_symbols (abfd, info, syms, symcount);
18370 }
18371
18372 /* Allocate target specific section data.  */
18373
18374 static bfd_boolean
18375 elf32_arm_new_section_hook (bfd *abfd, asection *sec)
18376 {
18377   if (!sec->used_by_bfd)
18378     {
18379       _arm_elf_section_data *sdata;
18380       bfd_size_type amt = sizeof (*sdata);
18381
18382       sdata = (_arm_elf_section_data *) bfd_zalloc (abfd, amt);
18383       if (sdata == NULL)
18384         return FALSE;
18385       sec->used_by_bfd = sdata;
18386     }
18387
18388   return _bfd_elf_new_section_hook (abfd, sec);
18389 }
18390
18391
18392 /* Used to order a list of mapping symbols by address.  */
18393
18394 static int
18395 elf32_arm_compare_mapping (const void * a, const void * b)
18396 {
18397   const elf32_arm_section_map *amap = (const elf32_arm_section_map *) a;
18398   const elf32_arm_section_map *bmap = (const elf32_arm_section_map *) b;
18399
18400   if (amap->vma > bmap->vma)
18401     return 1;
18402   else if (amap->vma < bmap->vma)
18403     return -1;
18404   else if (amap->type > bmap->type)
18405     /* Ensure results do not depend on the host qsort for objects with
18406        multiple mapping symbols at the same address by sorting on type
18407        after vma.  */
18408     return 1;
18409   else if (amap->type < bmap->type)
18410     return -1;
18411   else
18412     return 0;
18413 }
18414
18415 /* Add OFFSET to lower 31 bits of ADDR, leaving other bits unmodified.  */
18416
18417 static unsigned long
18418 offset_prel31 (unsigned long addr, bfd_vma offset)
18419 {
18420   return (addr & ~0x7ffffffful) | ((addr + offset) & 0x7ffffffful);
18421 }
18422
18423 /* Copy an .ARM.exidx table entry, adding OFFSET to (applied) PREL31
18424    relocations.  */
18425
18426 static void
18427 copy_exidx_entry (bfd *output_bfd, bfd_byte *to, bfd_byte *from, bfd_vma offset)
18428 {
18429   unsigned long first_word = bfd_get_32 (output_bfd, from);
18430   unsigned long second_word = bfd_get_32 (output_bfd, from + 4);
18431
18432   /* High bit of first word is supposed to be zero.  */
18433   if ((first_word & 0x80000000ul) == 0)
18434     first_word = offset_prel31 (first_word, offset);
18435
18436   /* If the high bit of the first word is clear, and the bit pattern is not 0x1
18437      (EXIDX_CANTUNWIND), this is an offset to an .ARM.extab entry.  */
18438   if ((second_word != 0x1) && ((second_word & 0x80000000ul) == 0))
18439     second_word = offset_prel31 (second_word, offset);
18440
18441   bfd_put_32 (output_bfd, first_word, to);
18442   bfd_put_32 (output_bfd, second_word, to + 4);
18443 }
18444
18445 /* Data for make_branch_to_a8_stub().  */
18446
18447 struct a8_branch_to_stub_data
18448 {
18449   asection *writing_section;
18450   bfd_byte *contents;
18451 };
18452
18453
18454 /* Helper to insert branches to Cortex-A8 erratum stubs in the right
18455    places for a particular section.  */
18456
18457 static bfd_boolean
18458 make_branch_to_a8_stub (struct bfd_hash_entry *gen_entry,
18459                        void *in_arg)
18460 {
18461   struct elf32_arm_stub_hash_entry *stub_entry;
18462   struct a8_branch_to_stub_data *data;
18463   bfd_byte *contents;
18464   unsigned long branch_insn;
18465   bfd_vma veneered_insn_loc, veneer_entry_loc;
18466   bfd_signed_vma branch_offset;
18467   bfd *abfd;
18468   unsigned int loc;
18469
18470   stub_entry = (struct elf32_arm_stub_hash_entry *) gen_entry;
18471   data = (struct a8_branch_to_stub_data *) in_arg;
18472
18473   if (stub_entry->target_section != data->writing_section
18474       || stub_entry->stub_type < arm_stub_a8_veneer_lwm)
18475     return TRUE;
18476
18477   contents = data->contents;
18478
18479   /* We use target_section as Cortex-A8 erratum workaround stubs are only
18480      generated when both source and target are in the same section.  */
18481   veneered_insn_loc = stub_entry->target_section->output_section->vma
18482                       + stub_entry->target_section->output_offset
18483                       + stub_entry->source_value;
18484
18485   veneer_entry_loc = stub_entry->stub_sec->output_section->vma
18486                      + stub_entry->stub_sec->output_offset
18487                      + stub_entry->stub_offset;
18488
18489   if (stub_entry->stub_type == arm_stub_a8_veneer_blx)
18490     veneered_insn_loc &= ~3u;
18491
18492   branch_offset = veneer_entry_loc - veneered_insn_loc - 4;
18493
18494   abfd = stub_entry->target_section->owner;
18495   loc = stub_entry->source_value;
18496
18497   /* We attempt to avoid this condition by setting stubs_always_after_branch
18498      in elf32_arm_size_stubs if we've enabled the Cortex-A8 erratum workaround.
18499      This check is just to be on the safe side...  */
18500   if ((veneered_insn_loc & ~0xfff) == (veneer_entry_loc & ~0xfff))
18501     {
18502       _bfd_error_handler (_("%pB: error: Cortex-A8 erratum stub is "
18503                             "allocated in unsafe location"), abfd);
18504       return FALSE;
18505     }
18506
18507   switch (stub_entry->stub_type)
18508     {
18509     case arm_stub_a8_veneer_b:
18510     case arm_stub_a8_veneer_b_cond:
18511       branch_insn = 0xf0009000;
18512       goto jump24;
18513
18514     case arm_stub_a8_veneer_blx:
18515       branch_insn = 0xf000e800;
18516       goto jump24;
18517
18518     case arm_stub_a8_veneer_bl:
18519       {
18520         unsigned int i1, j1, i2, j2, s;
18521
18522         branch_insn = 0xf000d000;
18523
18524       jump24:
18525         if (branch_offset < -16777216 || branch_offset > 16777214)
18526           {
18527             /* There's not much we can do apart from complain if this
18528                happens.  */
18529             _bfd_error_handler (_("%pB: error: Cortex-A8 erratum stub out "
18530                                   "of range (input file too large)"), abfd);
18531             return FALSE;
18532           }
18533
18534         /* i1 = not(j1 eor s), so:
18535            not i1 = j1 eor s
18536            j1 = (not i1) eor s.  */
18537
18538         branch_insn |= (branch_offset >> 1) & 0x7ff;
18539         branch_insn |= ((branch_offset >> 12) & 0x3ff) << 16;
18540         i2 = (branch_offset >> 22) & 1;
18541         i1 = (branch_offset >> 23) & 1;
18542         s = (branch_offset >> 24) & 1;
18543         j1 = (!i1) ^ s;
18544         j2 = (!i2) ^ s;
18545         branch_insn |= j2 << 11;
18546         branch_insn |= j1 << 13;
18547         branch_insn |= s << 26;
18548       }
18549       break;
18550
18551     default:
18552       BFD_FAIL ();
18553       return FALSE;
18554     }
18555
18556   bfd_put_16 (abfd, (branch_insn >> 16) & 0xffff, &contents[loc]);
18557   bfd_put_16 (abfd, branch_insn & 0xffff, &contents[loc + 2]);
18558
18559   return TRUE;
18560 }
18561
18562 /* Beginning of stm32l4xx work-around.  */
18563
18564 /* Functions encoding instructions necessary for the emission of the
18565    fix-stm32l4xx-629360.
18566    Encoding is extracted from the
18567    ARM (C) Architecture Reference Manual
18568    ARMv7-A and ARMv7-R edition
18569    ARM DDI 0406C.b (ID072512).  */
18570
18571 static inline bfd_vma
18572 create_instruction_branch_absolute (int branch_offset)
18573 {
18574   /* A8.8.18 B (A8-334)
18575      B target_address (Encoding T4).  */
18576   /* 1111 - 0Sii - iiii - iiii - 10J1 - Jiii - iiii - iiii.  */
18577   /* jump offset is:  S:I1:I2:imm10:imm11:0.  */
18578   /* with : I1 = NOT (J1 EOR S) I2 = NOT (J2 EOR S).  */
18579
18580   int s = ((branch_offset & 0x1000000) >> 24);
18581   int j1 = s ^ !((branch_offset & 0x800000) >> 23);
18582   int j2 = s ^ !((branch_offset & 0x400000) >> 22);
18583
18584   if (branch_offset < -(1 << 24) || branch_offset >= (1 << 24))
18585     BFD_ASSERT (0 && "Error: branch out of range.  Cannot create branch.");
18586
18587   bfd_vma patched_inst = 0xf0009000
18588     | s << 26 /* S.  */
18589     | (((unsigned long) (branch_offset) >> 12) & 0x3ff) << 16 /* imm10.  */
18590     | j1 << 13 /* J1.  */
18591     | j2 << 11 /* J2.  */
18592     | (((unsigned long) (branch_offset) >> 1) & 0x7ff); /* imm11.  */
18593
18594   return patched_inst;
18595 }
18596
18597 static inline bfd_vma
18598 create_instruction_ldmia (int base_reg, int wback, int reg_mask)
18599 {
18600   /* A8.8.57 LDM/LDMIA/LDMFD (A8-396)
18601      LDMIA Rn!, {Ra, Rb, Rc, ...} (Encoding T2).  */
18602   bfd_vma patched_inst = 0xe8900000
18603     | (/*W=*/wback << 21)
18604     | (base_reg << 16)
18605     | (reg_mask & 0x0000ffff);
18606
18607   return patched_inst;
18608 }
18609
18610 static inline bfd_vma
18611 create_instruction_ldmdb (int base_reg, int wback, int reg_mask)
18612 {
18613   /* A8.8.60 LDMDB/LDMEA (A8-402)
18614      LDMDB Rn!, {Ra, Rb, Rc, ...} (Encoding T1).  */
18615   bfd_vma patched_inst = 0xe9100000
18616     | (/*W=*/wback << 21)
18617     | (base_reg << 16)
18618     | (reg_mask & 0x0000ffff);
18619
18620   return patched_inst;
18621 }
18622
18623 static inline bfd_vma
18624 create_instruction_mov (int target_reg, int source_reg)
18625 {
18626   /* A8.8.103 MOV (register) (A8-486)
18627      MOV Rd, Rm (Encoding T1).  */
18628   bfd_vma patched_inst = 0x4600
18629     | (target_reg & 0x7)
18630     | ((target_reg & 0x8) >> 3) << 7
18631     | (source_reg << 3);
18632
18633   return patched_inst;
18634 }
18635
18636 static inline bfd_vma
18637 create_instruction_sub (int target_reg, int source_reg, int value)
18638 {
18639   /* A8.8.221 SUB (immediate) (A8-708)
18640      SUB Rd, Rn, #value (Encoding T3).  */
18641   bfd_vma patched_inst = 0xf1a00000
18642     | (target_reg << 8)
18643     | (source_reg << 16)
18644     | (/*S=*/0 << 20)
18645     | ((value & 0x800) >> 11) << 26
18646     | ((value & 0x700) >>  8) << 12
18647     | (value & 0x0ff);
18648
18649   return patched_inst;
18650 }
18651
18652 static inline bfd_vma
18653 create_instruction_vldmia (int base_reg, int is_dp, int wback, int num_words,
18654                            int first_reg)
18655 {
18656   /* A8.8.332 VLDM (A8-922)
18657      VLMD{MODE} Rn{!}, {list} (Encoding T1 or T2).  */
18658   bfd_vma patched_inst = (is_dp ? 0xec900b00 : 0xec900a00)
18659     | (/*W=*/wback << 21)
18660     | (base_reg << 16)
18661     | (num_words & 0x000000ff)
18662     | (((unsigned)first_reg >> 1) & 0x0000000f) << 12
18663     | (first_reg & 0x00000001) << 22;
18664
18665   return patched_inst;
18666 }
18667
18668 static inline bfd_vma
18669 create_instruction_vldmdb (int base_reg, int is_dp, int num_words,
18670                            int first_reg)
18671 {
18672   /* A8.8.332 VLDM (A8-922)
18673      VLMD{MODE} Rn!, {} (Encoding T1 or T2).  */
18674   bfd_vma patched_inst = (is_dp ? 0xed300b00 : 0xed300a00)
18675     | (base_reg << 16)
18676     | (num_words & 0x000000ff)
18677     | (((unsigned)first_reg >>1 ) & 0x0000000f) << 12
18678     | (first_reg & 0x00000001) << 22;
18679
18680   return patched_inst;
18681 }
18682
18683 static inline bfd_vma
18684 create_instruction_udf_w (int value)
18685 {
18686   /* A8.8.247 UDF (A8-758)
18687      Undefined (Encoding T2).  */
18688   bfd_vma patched_inst = 0xf7f0a000
18689     | (value & 0x00000fff)
18690     | (value & 0x000f0000) << 16;
18691
18692   return patched_inst;
18693 }
18694
18695 static inline bfd_vma
18696 create_instruction_udf (int value)
18697 {
18698   /* A8.8.247 UDF (A8-758)
18699      Undefined (Encoding T1).  */
18700   bfd_vma patched_inst = 0xde00
18701     | (value & 0xff);
18702
18703   return patched_inst;
18704 }
18705
18706 /* Functions writing an instruction in memory, returning the next
18707    memory position to write to.  */
18708
18709 static inline bfd_byte *
18710 push_thumb2_insn32 (struct elf32_arm_link_hash_table * htab,
18711                     bfd * output_bfd, bfd_byte *pt, insn32 insn)
18712 {
18713   put_thumb2_insn (htab, output_bfd, insn, pt);
18714   return pt + 4;
18715 }
18716
18717 static inline bfd_byte *
18718 push_thumb2_insn16 (struct elf32_arm_link_hash_table * htab,
18719                     bfd * output_bfd, bfd_byte *pt, insn32 insn)
18720 {
18721   put_thumb_insn (htab, output_bfd, insn, pt);
18722   return pt + 2;
18723 }
18724
18725 /* Function filling up a region in memory with T1 and T2 UDFs taking
18726    care of alignment.  */
18727
18728 static bfd_byte *
18729 stm32l4xx_fill_stub_udf (struct elf32_arm_link_hash_table * htab,
18730                          bfd *                   output_bfd,
18731                          const bfd_byte * const  base_stub_contents,
18732                          bfd_byte * const        from_stub_contents,
18733                          const bfd_byte * const  end_stub_contents)
18734 {
18735   bfd_byte *current_stub_contents = from_stub_contents;
18736
18737   /* Fill the remaining of the stub with deterministic contents : UDF
18738      instructions.
18739      Check if realignment is needed on modulo 4 frontier using T1, to
18740      further use T2.  */
18741   if ((current_stub_contents < end_stub_contents)
18742       && !((current_stub_contents - base_stub_contents) % 2)
18743       && ((current_stub_contents - base_stub_contents) % 4))
18744     current_stub_contents =
18745       push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
18746                           create_instruction_udf (0));
18747
18748   for (; current_stub_contents < end_stub_contents;)
18749     current_stub_contents =
18750       push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18751                           create_instruction_udf_w (0));
18752
18753   return current_stub_contents;
18754 }
18755
18756 /* Functions writing the stream of instructions equivalent to the
18757    derived sequence for ldmia, ldmdb, vldm respectively.  */
18758
18759 static void
18760 stm32l4xx_create_replacing_stub_ldmia (struct elf32_arm_link_hash_table * htab,
18761                                        bfd * output_bfd,
18762                                        const insn32 initial_insn,
18763                                        const bfd_byte *const initial_insn_addr,
18764                                        bfd_byte *const base_stub_contents)
18765 {
18766   int wback = (initial_insn & 0x00200000) >> 21;
18767   int ri, rn = (initial_insn & 0x000F0000) >> 16;
18768   int insn_all_registers = initial_insn & 0x0000ffff;
18769   int insn_low_registers, insn_high_registers;
18770   int usable_register_mask;
18771   int nb_registers = elf32_arm_popcount (insn_all_registers);
18772   int restore_pc = (insn_all_registers & (1 << 15)) ? 1 : 0;
18773   int restore_rn = (insn_all_registers & (1 << rn)) ? 1 : 0;
18774   bfd_byte *current_stub_contents = base_stub_contents;
18775
18776   BFD_ASSERT (is_thumb2_ldmia (initial_insn));
18777
18778   /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
18779      smaller than 8 registers load sequences that do not cause the
18780      hardware issue.  */
18781   if (nb_registers <= 8)
18782     {
18783       /* UNTOUCHED : LDMIA Rn{!}, {R-all-register-list}.  */
18784       current_stub_contents =
18785         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18786                             initial_insn);
18787
18788       /* B initial_insn_addr+4.  */
18789       if (!restore_pc)
18790         current_stub_contents =
18791           push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18792                               create_instruction_branch_absolute
18793                               (initial_insn_addr - current_stub_contents));
18794
18795       /* Fill the remaining of the stub with deterministic contents.  */
18796       current_stub_contents =
18797         stm32l4xx_fill_stub_udf (htab, output_bfd,
18798                                  base_stub_contents, current_stub_contents,
18799                                  base_stub_contents +
18800                                  STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
18801
18802       return;
18803     }
18804
18805   /* - reg_list[13] == 0.  */
18806   BFD_ASSERT ((insn_all_registers & (1 << 13))==0);
18807
18808   /* - reg_list[14] & reg_list[15] != 1.  */
18809   BFD_ASSERT ((insn_all_registers & 0xC000) != 0xC000);
18810
18811   /* - if (wback==1) reg_list[rn] == 0.  */
18812   BFD_ASSERT (!wback || !restore_rn);
18813
18814   /* - nb_registers > 8.  */
18815   BFD_ASSERT (elf32_arm_popcount (insn_all_registers) > 8);
18816
18817   /* At this point, LDMxx initial insn loads between 9 and 14 registers.  */
18818
18819   /* In the following algorithm, we split this wide LDM using 2 LDM insns:
18820     - One with the 7 lowest registers (register mask 0x007F)
18821       This LDM will finally contain between 2 and 7 registers
18822     - One with the 7 highest registers (register mask 0xDF80)
18823       This ldm will finally contain between 2 and 7 registers.  */
18824   insn_low_registers = insn_all_registers & 0x007F;
18825   insn_high_registers = insn_all_registers & 0xDF80;
18826
18827   /* A spare register may be needed during this veneer to temporarily
18828      handle the base register.  This register will be restored with the
18829      last LDM operation.
18830      The usable register may be any general purpose register (that
18831      excludes PC, SP, LR : register mask is 0x1FFF).  */
18832   usable_register_mask = 0x1FFF;
18833
18834   /* Generate the stub function.  */
18835   if (wback)
18836     {
18837       /* LDMIA Rn!, {R-low-register-list} : (Encoding T2).  */
18838       current_stub_contents =
18839         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18840                             create_instruction_ldmia
18841                             (rn, /*wback=*/1, insn_low_registers));
18842
18843       /* LDMIA Rn!, {R-high-register-list} : (Encoding T2).  */
18844       current_stub_contents =
18845         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18846                             create_instruction_ldmia
18847                             (rn, /*wback=*/1, insn_high_registers));
18848       if (!restore_pc)
18849         {
18850           /* B initial_insn_addr+4.  */
18851           current_stub_contents =
18852             push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18853                                 create_instruction_branch_absolute
18854                                 (initial_insn_addr - current_stub_contents));
18855        }
18856     }
18857   else /* if (!wback).  */
18858     {
18859       ri = rn;
18860
18861       /* If Rn is not part of the high-register-list, move it there.  */
18862       if (!(insn_high_registers & (1 << rn)))
18863         {
18864           /* Choose a Ri in the high-register-list that will be restored.  */
18865           ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
18866
18867           /* MOV Ri, Rn.  */
18868           current_stub_contents =
18869             push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
18870                                 create_instruction_mov (ri, rn));
18871         }
18872
18873       /* LDMIA Ri!, {R-low-register-list} : (Encoding T2).  */
18874       current_stub_contents =
18875         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18876                             create_instruction_ldmia
18877                             (ri, /*wback=*/1, insn_low_registers));
18878
18879       /* LDMIA Ri, {R-high-register-list} : (Encoding T2).  */
18880       current_stub_contents =
18881         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18882                             create_instruction_ldmia
18883                             (ri, /*wback=*/0, insn_high_registers));
18884
18885       if (!restore_pc)
18886         {
18887           /* B initial_insn_addr+4.  */
18888           current_stub_contents =
18889             push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18890                                 create_instruction_branch_absolute
18891                                 (initial_insn_addr - current_stub_contents));
18892         }
18893     }
18894
18895   /* Fill the remaining of the stub with deterministic contents.  */
18896   current_stub_contents =
18897     stm32l4xx_fill_stub_udf (htab, output_bfd,
18898                              base_stub_contents, current_stub_contents,
18899                              base_stub_contents +
18900                              STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
18901 }
18902
18903 static void
18904 stm32l4xx_create_replacing_stub_ldmdb (struct elf32_arm_link_hash_table * htab,
18905                                        bfd * output_bfd,
18906                                        const insn32 initial_insn,
18907                                        const bfd_byte *const initial_insn_addr,
18908                                        bfd_byte *const base_stub_contents)
18909 {
18910   int wback = (initial_insn & 0x00200000) >> 21;
18911   int ri, rn = (initial_insn & 0x000f0000) >> 16;
18912   int insn_all_registers = initial_insn & 0x0000ffff;
18913   int insn_low_registers, insn_high_registers;
18914   int usable_register_mask;
18915   int restore_pc = (insn_all_registers & (1 << 15)) ? 1 : 0;
18916   int restore_rn = (insn_all_registers & (1 << rn)) ? 1 : 0;
18917   int nb_registers = elf32_arm_popcount (insn_all_registers);
18918   bfd_byte *current_stub_contents = base_stub_contents;
18919
18920   BFD_ASSERT (is_thumb2_ldmdb (initial_insn));
18921
18922   /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
18923      smaller than 8 registers load sequences that do not cause the
18924      hardware issue.  */
18925   if (nb_registers <= 8)
18926     {
18927       /* UNTOUCHED : LDMIA Rn{!}, {R-all-register-list}.  */
18928       current_stub_contents =
18929         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18930                             initial_insn);
18931
18932       /* B initial_insn_addr+4.  */
18933       current_stub_contents =
18934         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18935                             create_instruction_branch_absolute
18936                             (initial_insn_addr - current_stub_contents));
18937
18938       /* Fill the remaining of the stub with deterministic contents.  */
18939       current_stub_contents =
18940         stm32l4xx_fill_stub_udf (htab, output_bfd,
18941                                  base_stub_contents, current_stub_contents,
18942                                  base_stub_contents +
18943                                  STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
18944
18945       return;
18946     }
18947
18948   /* - reg_list[13] == 0.  */
18949   BFD_ASSERT ((insn_all_registers & (1 << 13)) == 0);
18950
18951   /* - reg_list[14] & reg_list[15] != 1.  */
18952   BFD_ASSERT ((insn_all_registers & 0xC000) != 0xC000);
18953
18954   /* - if (wback==1) reg_list[rn] == 0.  */
18955   BFD_ASSERT (!wback || !restore_rn);
18956
18957   /* - nb_registers > 8.  */
18958   BFD_ASSERT (elf32_arm_popcount (insn_all_registers) > 8);
18959
18960   /* At this point, LDMxx initial insn loads between 9 and 14 registers.  */
18961
18962   /* In the following algorithm, we split this wide LDM using 2 LDM insn:
18963     - One with the 7 lowest registers (register mask 0x007F)
18964       This LDM will finally contain between 2 and 7 registers
18965     - One with the 7 highest registers (register mask 0xDF80)
18966       This ldm will finally contain between 2 and 7 registers.  */
18967   insn_low_registers = insn_all_registers & 0x007F;
18968   insn_high_registers = insn_all_registers & 0xDF80;
18969
18970   /* A spare register may be needed during this veneer to temporarily
18971      handle the base register.  This register will be restored with
18972      the last LDM operation.
18973      The usable register may be any general purpose register (that excludes
18974      PC, SP, LR : register mask is 0x1FFF).  */
18975   usable_register_mask = 0x1FFF;
18976
18977   /* Generate the stub function.  */
18978   if (!wback && !restore_pc && !restore_rn)
18979     {
18980       /* Choose a Ri in the low-register-list that will be restored.  */
18981       ri = ctz (insn_low_registers & usable_register_mask & ~(1 << rn));
18982
18983       /* MOV Ri, Rn.  */
18984       current_stub_contents =
18985         push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
18986                             create_instruction_mov (ri, rn));
18987
18988       /* LDMDB Ri!, {R-high-register-list}.  */
18989       current_stub_contents =
18990         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18991                             create_instruction_ldmdb
18992                             (ri, /*wback=*/1, insn_high_registers));
18993
18994       /* LDMDB Ri, {R-low-register-list}.  */
18995       current_stub_contents =
18996         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
18997                             create_instruction_ldmdb
18998                             (ri, /*wback=*/0, insn_low_registers));
18999
19000       /* B initial_insn_addr+4.  */
19001       current_stub_contents =
19002         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19003                             create_instruction_branch_absolute
19004                             (initial_insn_addr - current_stub_contents));
19005     }
19006   else if (wback && !restore_pc && !restore_rn)
19007     {
19008       /* LDMDB Rn!, {R-high-register-list}.  */
19009       current_stub_contents =
19010         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19011                             create_instruction_ldmdb
19012                             (rn, /*wback=*/1, insn_high_registers));
19013
19014       /* LDMDB Rn!, {R-low-register-list}.  */
19015       current_stub_contents =
19016         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19017                             create_instruction_ldmdb
19018                             (rn, /*wback=*/1, insn_low_registers));
19019
19020       /* B initial_insn_addr+4.  */
19021       current_stub_contents =
19022         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19023                             create_instruction_branch_absolute
19024                             (initial_insn_addr - current_stub_contents));
19025     }
19026   else if (!wback && restore_pc && !restore_rn)
19027     {
19028       /* Choose a Ri in the high-register-list that will be restored.  */
19029       ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
19030
19031       /* SUB Ri, Rn, #(4*nb_registers).  */
19032       current_stub_contents =
19033         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19034                             create_instruction_sub (ri, rn, (4 * nb_registers)));
19035
19036       /* LDMIA Ri!, {R-low-register-list}.  */
19037       current_stub_contents =
19038         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19039                             create_instruction_ldmia
19040                             (ri, /*wback=*/1, insn_low_registers));
19041
19042       /* LDMIA Ri, {R-high-register-list}.  */
19043       current_stub_contents =
19044         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19045                             create_instruction_ldmia
19046                             (ri, /*wback=*/0, insn_high_registers));
19047     }
19048   else if (wback && restore_pc && !restore_rn)
19049     {
19050       /* Choose a Ri in the high-register-list that will be restored.  */
19051       ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
19052
19053       /* SUB Rn, Rn, #(4*nb_registers)  */
19054       current_stub_contents =
19055         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19056                             create_instruction_sub (rn, rn, (4 * nb_registers)));
19057
19058       /* MOV Ri, Rn.  */
19059       current_stub_contents =
19060         push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
19061                             create_instruction_mov (ri, rn));
19062
19063       /* LDMIA Ri!, {R-low-register-list}.  */
19064       current_stub_contents =
19065         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19066                             create_instruction_ldmia
19067                             (ri, /*wback=*/1, insn_low_registers));
19068
19069       /* LDMIA Ri, {R-high-register-list}.  */
19070       current_stub_contents =
19071         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19072                             create_instruction_ldmia
19073                             (ri, /*wback=*/0, insn_high_registers));
19074     }
19075   else if (!wback && !restore_pc && restore_rn)
19076     {
19077       ri = rn;
19078       if (!(insn_low_registers & (1 << rn)))
19079         {
19080           /* Choose a Ri in the low-register-list that will be restored.  */
19081           ri = ctz (insn_low_registers & usable_register_mask & ~(1 << rn));
19082
19083           /* MOV Ri, Rn.  */
19084           current_stub_contents =
19085             push_thumb2_insn16 (htab, output_bfd, current_stub_contents,
19086                                 create_instruction_mov (ri, rn));
19087         }
19088
19089       /* LDMDB Ri!, {R-high-register-list}.  */
19090       current_stub_contents =
19091         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19092                             create_instruction_ldmdb
19093                             (ri, /*wback=*/1, insn_high_registers));
19094
19095       /* LDMDB Ri, {R-low-register-list}.  */
19096       current_stub_contents =
19097         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19098                             create_instruction_ldmdb
19099                             (ri, /*wback=*/0, insn_low_registers));
19100
19101       /* B initial_insn_addr+4.  */
19102       current_stub_contents =
19103         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19104                             create_instruction_branch_absolute
19105                             (initial_insn_addr - current_stub_contents));
19106     }
19107   else if (!wback && restore_pc && restore_rn)
19108     {
19109       ri = rn;
19110       if (!(insn_high_registers & (1 << rn)))
19111         {
19112           /* Choose a Ri in the high-register-list that will be restored.  */
19113           ri = ctz (insn_high_registers & usable_register_mask & ~(1 << rn));
19114         }
19115
19116       /* SUB Ri, Rn, #(4*nb_registers).  */
19117       current_stub_contents =
19118         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19119                             create_instruction_sub (ri, rn, (4 * nb_registers)));
19120
19121       /* LDMIA Ri!, {R-low-register-list}.  */
19122       current_stub_contents =
19123         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19124                             create_instruction_ldmia
19125                             (ri, /*wback=*/1, insn_low_registers));
19126
19127       /* LDMIA Ri, {R-high-register-list}.  */
19128       current_stub_contents =
19129         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19130                             create_instruction_ldmia
19131                             (ri, /*wback=*/0, insn_high_registers));
19132     }
19133   else if (wback && restore_rn)
19134     {
19135       /* The assembler should not have accepted to encode this.  */
19136       BFD_ASSERT (0 && "Cannot patch an instruction that has an "
19137         "undefined behavior.\n");
19138     }
19139
19140   /* Fill the remaining of the stub with deterministic contents.  */
19141   current_stub_contents =
19142     stm32l4xx_fill_stub_udf (htab, output_bfd,
19143                              base_stub_contents, current_stub_contents,
19144                              base_stub_contents +
19145                              STM32L4XX_ERRATUM_LDM_VENEER_SIZE);
19146
19147 }
19148
19149 static void
19150 stm32l4xx_create_replacing_stub_vldm (struct elf32_arm_link_hash_table * htab,
19151                                       bfd * output_bfd,
19152                                       const insn32 initial_insn,
19153                                       const bfd_byte *const initial_insn_addr,
19154                                       bfd_byte *const base_stub_contents)
19155 {
19156   int num_words = ((unsigned int) initial_insn << 24) >> 24;
19157   bfd_byte *current_stub_contents = base_stub_contents;
19158
19159   BFD_ASSERT (is_thumb2_vldm (initial_insn));
19160
19161   /* In BFD_ARM_STM32L4XX_FIX_ALL mode we may have to deal with
19162      smaller than 8 words load sequences that do not cause the
19163      hardware issue.  */
19164   if (num_words <= 8)
19165     {
19166       /* Untouched instruction.  */
19167       current_stub_contents =
19168         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19169                             initial_insn);
19170
19171       /* B initial_insn_addr+4.  */
19172       current_stub_contents =
19173         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19174                             create_instruction_branch_absolute
19175                             (initial_insn_addr - current_stub_contents));
19176     }
19177   else
19178     {
19179       bfd_boolean is_dp = /* DP encoding.  */
19180         (initial_insn & 0xfe100f00) == 0xec100b00;
19181       bfd_boolean is_ia_nobang = /* (IA without !).  */
19182         (((initial_insn << 7) >> 28) & 0xd) == 0x4;
19183       bfd_boolean is_ia_bang = /* (IA with !) - includes VPOP.  */
19184         (((initial_insn << 7) >> 28) & 0xd) == 0x5;
19185       bfd_boolean is_db_bang = /* (DB with !).  */
19186         (((initial_insn << 7) >> 28) & 0xd) == 0x9;
19187       int base_reg = ((unsigned int) initial_insn << 12) >> 28;
19188       /* d = UInt (Vd:D);.  */
19189       int first_reg = ((((unsigned int) initial_insn << 16) >> 28) << 1)
19190         | (((unsigned int)initial_insn << 9) >> 31);
19191
19192       /* Compute the number of 8-words chunks needed to split.  */
19193       int chunks = (num_words % 8) ? (num_words / 8 + 1) : (num_words / 8);
19194       int chunk;
19195
19196       /* The test coverage has been done assuming the following
19197          hypothesis that exactly one of the previous is_ predicates is
19198          true.  */
19199       BFD_ASSERT (    (is_ia_nobang ^ is_ia_bang ^ is_db_bang)
19200                   && !(is_ia_nobang & is_ia_bang & is_db_bang));
19201
19202       /* We treat the cutting of the words in one pass for all
19203          cases, then we emit the adjustments:
19204
19205          vldm rx, {...}
19206          -> vldm rx!, {8_words_or_less} for each needed 8_word
19207          -> sub rx, rx, #size (list)
19208
19209          vldm rx!, {...}
19210          -> vldm rx!, {8_words_or_less} for each needed 8_word
19211          This also handles vpop instruction (when rx is sp)
19212
19213          vldmd rx!, {...}
19214          -> vldmb rx!, {8_words_or_less} for each needed 8_word.  */
19215       for (chunk = 0; chunk < chunks; ++chunk)
19216         {
19217           bfd_vma new_insn = 0;
19218
19219           if (is_ia_nobang || is_ia_bang)
19220             {
19221               new_insn = create_instruction_vldmia
19222                 (base_reg,
19223                  is_dp,
19224                  /*wback= .  */1,
19225                  chunks - (chunk + 1) ?
19226                  8 : num_words - chunk * 8,
19227                  first_reg + chunk * 8);
19228             }
19229           else if (is_db_bang)
19230             {
19231               new_insn = create_instruction_vldmdb
19232                 (base_reg,
19233                  is_dp,
19234                  chunks - (chunk + 1) ?
19235                  8 : num_words - chunk * 8,
19236                  first_reg + chunk * 8);
19237             }
19238
19239           if (new_insn)
19240             current_stub_contents =
19241               push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19242                                   new_insn);
19243         }
19244
19245       /* Only this case requires the base register compensation
19246          subtract.  */
19247       if (is_ia_nobang)
19248         {
19249           current_stub_contents =
19250             push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19251                                 create_instruction_sub
19252                                 (base_reg, base_reg, 4*num_words));
19253         }
19254
19255       /* B initial_insn_addr+4.  */
19256       current_stub_contents =
19257         push_thumb2_insn32 (htab, output_bfd, current_stub_contents,
19258                             create_instruction_branch_absolute
19259                             (initial_insn_addr - current_stub_contents));
19260     }
19261
19262   /* Fill the remaining of the stub with deterministic contents.  */
19263   current_stub_contents =
19264     stm32l4xx_fill_stub_udf (htab, output_bfd,
19265                              base_stub_contents, current_stub_contents,
19266                              base_stub_contents +
19267                              STM32L4XX_ERRATUM_VLDM_VENEER_SIZE);
19268 }
19269
19270 static void
19271 stm32l4xx_create_replacing_stub (struct elf32_arm_link_hash_table * htab,
19272                                  bfd * output_bfd,
19273                                  const insn32 wrong_insn,
19274                                  const bfd_byte *const wrong_insn_addr,
19275                                  bfd_byte *const stub_contents)
19276 {
19277   if (is_thumb2_ldmia (wrong_insn))
19278     stm32l4xx_create_replacing_stub_ldmia (htab, output_bfd,
19279                                            wrong_insn, wrong_insn_addr,
19280                                            stub_contents);
19281   else if (is_thumb2_ldmdb (wrong_insn))
19282     stm32l4xx_create_replacing_stub_ldmdb (htab, output_bfd,
19283                                            wrong_insn, wrong_insn_addr,
19284                                            stub_contents);
19285   else if (is_thumb2_vldm (wrong_insn))
19286     stm32l4xx_create_replacing_stub_vldm (htab, output_bfd,
19287                                           wrong_insn, wrong_insn_addr,
19288                                           stub_contents);
19289 }
19290
19291 /* End of stm32l4xx work-around.  */
19292
19293
19294 /* Do code byteswapping.  Return FALSE afterwards so that the section is
19295    written out as normal.  */
19296
19297 static bfd_boolean
19298 elf32_arm_write_section (bfd *output_bfd,
19299                          struct bfd_link_info *link_info,
19300                          asection *sec,
19301                          bfd_byte *contents)
19302 {
19303   unsigned int mapcount, errcount;
19304   _arm_elf_section_data *arm_data;
19305   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (link_info);
19306   elf32_arm_section_map *map;
19307   elf32_vfp11_erratum_list *errnode;
19308   elf32_stm32l4xx_erratum_list *stm32l4xx_errnode;
19309   bfd_vma ptr;
19310   bfd_vma end;
19311   bfd_vma offset = sec->output_section->vma + sec->output_offset;
19312   bfd_byte tmp;
19313   unsigned int i;
19314
19315   if (globals == NULL)
19316     return FALSE;
19317
19318   /* If this section has not been allocated an _arm_elf_section_data
19319      structure then we cannot record anything.  */
19320   arm_data = get_arm_elf_section_data (sec);
19321   if (arm_data == NULL)
19322     return FALSE;
19323
19324   mapcount = arm_data->mapcount;
19325   map = arm_data->map;
19326   errcount = arm_data->erratumcount;
19327
19328   if (errcount != 0)
19329     {
19330       unsigned int endianflip = bfd_big_endian (output_bfd) ? 3 : 0;
19331
19332       for (errnode = arm_data->erratumlist; errnode != 0;
19333            errnode = errnode->next)
19334         {
19335           bfd_vma target = errnode->vma - offset;
19336
19337           switch (errnode->type)
19338             {
19339             case VFP11_ERRATUM_BRANCH_TO_ARM_VENEER:
19340               {
19341                 bfd_vma branch_to_veneer;
19342                 /* Original condition code of instruction, plus bit mask for
19343                    ARM B instruction.  */
19344                 unsigned int insn = (errnode->u.b.vfp_insn & 0xf0000000)
19345                                   | 0x0a000000;
19346
19347                 /* The instruction is before the label.  */
19348                 target -= 4;
19349
19350                 /* Above offset included in -4 below.  */
19351                 branch_to_veneer = errnode->u.b.veneer->vma
19352                                    - errnode->vma - 4;
19353
19354                 if ((signed) branch_to_veneer < -(1 << 25)
19355                     || (signed) branch_to_veneer >= (1 << 25))
19356                   _bfd_error_handler (_("%pB: error: VFP11 veneer out of "
19357                                         "range"), output_bfd);
19358
19359                 insn |= (branch_to_veneer >> 2) & 0xffffff;
19360                 contents[endianflip ^ target] = insn & 0xff;
19361                 contents[endianflip ^ (target + 1)] = (insn >> 8) & 0xff;
19362                 contents[endianflip ^ (target + 2)] = (insn >> 16) & 0xff;
19363                 contents[endianflip ^ (target + 3)] = (insn >> 24) & 0xff;
19364               }
19365               break;
19366
19367             case VFP11_ERRATUM_ARM_VENEER:
19368               {
19369                 bfd_vma branch_from_veneer;
19370                 unsigned int insn;
19371
19372                 /* Take size of veneer into account.  */
19373                 branch_from_veneer = errnode->u.v.branch->vma
19374                                      - errnode->vma - 12;
19375
19376                 if ((signed) branch_from_veneer < -(1 << 25)
19377                     || (signed) branch_from_veneer >= (1 << 25))
19378                   _bfd_error_handler (_("%pB: error: VFP11 veneer out of "
19379                                         "range"), output_bfd);
19380
19381                 /* Original instruction.  */
19382                 insn = errnode->u.v.branch->u.b.vfp_insn;
19383                 contents[endianflip ^ target] = insn & 0xff;
19384                 contents[endianflip ^ (target + 1)] = (insn >> 8) & 0xff;
19385                 contents[endianflip ^ (target + 2)] = (insn >> 16) & 0xff;
19386                 contents[endianflip ^ (target + 3)] = (insn >> 24) & 0xff;
19387
19388                 /* Branch back to insn after original insn.  */
19389                 insn = 0xea000000 | ((branch_from_veneer >> 2) & 0xffffff);
19390                 contents[endianflip ^ (target + 4)] = insn & 0xff;
19391                 contents[endianflip ^ (target + 5)] = (insn >> 8) & 0xff;
19392                 contents[endianflip ^ (target + 6)] = (insn >> 16) & 0xff;
19393                 contents[endianflip ^ (target + 7)] = (insn >> 24) & 0xff;
19394               }
19395               break;
19396
19397             default:
19398               abort ();
19399             }
19400         }
19401     }
19402
19403   if (arm_data->stm32l4xx_erratumcount != 0)
19404     {
19405       for (stm32l4xx_errnode = arm_data->stm32l4xx_erratumlist;
19406            stm32l4xx_errnode != 0;
19407            stm32l4xx_errnode = stm32l4xx_errnode->next)
19408         {
19409           bfd_vma target = stm32l4xx_errnode->vma - offset;
19410
19411           switch (stm32l4xx_errnode->type)
19412             {
19413             case STM32L4XX_ERRATUM_BRANCH_TO_VENEER:
19414               {
19415                 unsigned int insn;
19416                 bfd_vma branch_to_veneer =
19417                   stm32l4xx_errnode->u.b.veneer->vma - stm32l4xx_errnode->vma;
19418
19419                 if ((signed) branch_to_veneer < -(1 << 24)
19420                     || (signed) branch_to_veneer >= (1 << 24))
19421                   {
19422                     bfd_vma out_of_range =
19423                       ((signed) branch_to_veneer < -(1 << 24)) ?
19424                       - branch_to_veneer - (1 << 24) :
19425                       ((signed) branch_to_veneer >= (1 << 24)) ?
19426                       branch_to_veneer - (1 << 24) : 0;
19427
19428                     _bfd_error_handler
19429                       (_("%pB(%#" PRIx64 "): error: "
19430                          "cannot create STM32L4XX veneer; "
19431                          "jump out of range by %" PRId64 " bytes; "
19432                          "cannot encode branch instruction"),
19433                        output_bfd,
19434                        (uint64_t) (stm32l4xx_errnode->vma - 4),
19435                        (int64_t) out_of_range);
19436                     continue;
19437                   }
19438
19439                 insn = create_instruction_branch_absolute
19440                   (stm32l4xx_errnode->u.b.veneer->vma - stm32l4xx_errnode->vma);
19441
19442                 /* The instruction is before the label.  */
19443                 target -= 4;
19444
19445                 put_thumb2_insn (globals, output_bfd,
19446                                  (bfd_vma) insn, contents + target);
19447               }
19448               break;
19449
19450             case STM32L4XX_ERRATUM_VENEER:
19451               {
19452                 bfd_byte * veneer;
19453                 bfd_byte * veneer_r;
19454                 unsigned int insn;
19455
19456                 veneer = contents + target;
19457                 veneer_r = veneer
19458                   + stm32l4xx_errnode->u.b.veneer->vma
19459                   - stm32l4xx_errnode->vma - 4;
19460
19461                 if ((signed) (veneer_r - veneer -
19462                               STM32L4XX_ERRATUM_VLDM_VENEER_SIZE >
19463                               STM32L4XX_ERRATUM_LDM_VENEER_SIZE ?
19464                               STM32L4XX_ERRATUM_VLDM_VENEER_SIZE :
19465                               STM32L4XX_ERRATUM_LDM_VENEER_SIZE) < -(1 << 24)
19466                     || (signed) (veneer_r - veneer) >= (1 << 24))
19467                   {
19468                     _bfd_error_handler (_("%pB: error: cannot create STM32L4XX "
19469                                           "veneer"), output_bfd);
19470                      continue;
19471                   }
19472
19473                 /* Original instruction.  */
19474                 insn = stm32l4xx_errnode->u.v.branch->u.b.insn;
19475
19476                 stm32l4xx_create_replacing_stub
19477                   (globals, output_bfd, insn, (void*)veneer_r, (void*)veneer);
19478               }
19479               break;
19480
19481             default:
19482               abort ();
19483             }
19484         }
19485     }
19486
19487   if (arm_data->elf.this_hdr.sh_type == SHT_ARM_EXIDX)
19488     {
19489       arm_unwind_table_edit *edit_node
19490         = arm_data->u.exidx.unwind_edit_list;
19491       /* Now, sec->size is the size of the section we will write.  The original
19492          size (before we merged duplicate entries and inserted EXIDX_CANTUNWIND
19493          markers) was sec->rawsize.  (This isn't the case if we perform no
19494          edits, then rawsize will be zero and we should use size).  */
19495       bfd_byte *edited_contents = (bfd_byte *) bfd_malloc (sec->size);
19496       unsigned int input_size = sec->rawsize ? sec->rawsize : sec->size;
19497       unsigned int in_index, out_index;
19498       bfd_vma add_to_offsets = 0;
19499
19500       for (in_index = 0, out_index = 0; in_index * 8 < input_size || edit_node;)
19501         {
19502           if (edit_node)
19503             {
19504               unsigned int edit_index = edit_node->index;
19505
19506               if (in_index < edit_index && in_index * 8 < input_size)
19507                 {
19508                   copy_exidx_entry (output_bfd, edited_contents + out_index * 8,
19509                                     contents + in_index * 8, add_to_offsets);
19510                   out_index++;
19511                   in_index++;
19512                 }
19513               else if (in_index == edit_index
19514                        || (in_index * 8 >= input_size
19515                            && edit_index == UINT_MAX))
19516                 {
19517                   switch (edit_node->type)
19518                     {
19519                     case DELETE_EXIDX_ENTRY:
19520                       in_index++;
19521                       add_to_offsets += 8;
19522                       break;
19523
19524                     case INSERT_EXIDX_CANTUNWIND_AT_END:
19525                       {
19526                         asection *text_sec = edit_node->linked_section;
19527                         bfd_vma text_offset = text_sec->output_section->vma
19528                                               + text_sec->output_offset
19529                                               + text_sec->size;
19530                         bfd_vma exidx_offset = offset + out_index * 8;
19531                         unsigned long prel31_offset;
19532
19533                         /* Note: this is meant to be equivalent to an
19534                            R_ARM_PREL31 relocation.  These synthetic
19535                            EXIDX_CANTUNWIND markers are not relocated by the
19536                            usual BFD method.  */
19537                         prel31_offset = (text_offset - exidx_offset)
19538                                         & 0x7ffffffful;
19539                         if (bfd_link_relocatable (link_info))
19540                           {
19541                             /* Here relocation for new EXIDX_CANTUNWIND is
19542                                created, so there is no need to
19543                                adjust offset by hand.  */
19544                             prel31_offset = text_sec->output_offset
19545                                             + text_sec->size;
19546                           }
19547
19548                         /* First address we can't unwind.  */
19549                         bfd_put_32 (output_bfd, prel31_offset,
19550                                     &edited_contents[out_index * 8]);
19551
19552                         /* Code for EXIDX_CANTUNWIND.  */
19553                         bfd_put_32 (output_bfd, 0x1,
19554                                     &edited_contents[out_index * 8 + 4]);
19555
19556                         out_index++;
19557                         add_to_offsets -= 8;
19558                       }
19559                       break;
19560                     }
19561
19562                   edit_node = edit_node->next;
19563                 }
19564             }
19565           else
19566             {
19567               /* No more edits, copy remaining entries verbatim.  */
19568               copy_exidx_entry (output_bfd, edited_contents + out_index * 8,
19569                                 contents + in_index * 8, add_to_offsets);
19570               out_index++;
19571               in_index++;
19572             }
19573         }
19574
19575       if (!(sec->flags & SEC_EXCLUDE) && !(sec->flags & SEC_NEVER_LOAD))
19576         bfd_set_section_contents (output_bfd, sec->output_section,
19577                                   edited_contents,
19578                                   (file_ptr) sec->output_offset, sec->size);
19579
19580       return TRUE;
19581     }
19582
19583   /* Fix code to point to Cortex-A8 erratum stubs.  */
19584   if (globals->fix_cortex_a8)
19585     {
19586       struct a8_branch_to_stub_data data;
19587
19588       data.writing_section = sec;
19589       data.contents = contents;
19590
19591       bfd_hash_traverse (& globals->stub_hash_table, make_branch_to_a8_stub,
19592                          & data);
19593     }
19594
19595   if (mapcount == 0)
19596     return FALSE;
19597
19598   if (globals->byteswap_code)
19599     {
19600       qsort (map, mapcount, sizeof (* map), elf32_arm_compare_mapping);
19601
19602       ptr = map[0].vma;
19603       for (i = 0; i < mapcount; i++)
19604         {
19605           if (i == mapcount - 1)
19606             end = sec->size;
19607           else
19608             end = map[i + 1].vma;
19609
19610           switch (map[i].type)
19611             {
19612             case 'a':
19613               /* Byte swap code words.  */
19614               while (ptr + 3 < end)
19615                 {
19616                   tmp = contents[ptr];
19617                   contents[ptr] = contents[ptr + 3];
19618                   contents[ptr + 3] = tmp;
19619                   tmp = contents[ptr + 1];
19620                   contents[ptr + 1] = contents[ptr + 2];
19621                   contents[ptr + 2] = tmp;
19622                   ptr += 4;
19623                 }
19624               break;
19625
19626             case 't':
19627               /* Byte swap code halfwords.  */
19628               while (ptr + 1 < end)
19629                 {
19630                   tmp = contents[ptr];
19631                   contents[ptr] = contents[ptr + 1];
19632                   contents[ptr + 1] = tmp;
19633                   ptr += 2;
19634                 }
19635               break;
19636
19637             case 'd':
19638               /* Leave data alone.  */
19639               break;
19640             }
19641           ptr = end;
19642         }
19643     }
19644
19645   free (map);
19646   arm_data->mapcount = -1;
19647   arm_data->mapsize = 0;
19648   arm_data->map = NULL;
19649
19650   return FALSE;
19651 }
19652
19653 /* Mangle thumb function symbols as we read them in.  */
19654
19655 static bfd_boolean
19656 elf32_arm_swap_symbol_in (bfd * abfd,
19657                           const void *psrc,
19658                           const void *pshn,
19659                           Elf_Internal_Sym *dst)
19660 {
19661   Elf_Internal_Shdr *symtab_hdr;
19662   const char *name = NULL;
19663
19664   if (!bfd_elf32_swap_symbol_in (abfd, psrc, pshn, dst))
19665     return FALSE;
19666   dst->st_target_internal = 0;
19667
19668   /* New EABI objects mark thumb function symbols by setting the low bit of
19669      the address.  */
19670   if (ELF_ST_TYPE (dst->st_info) == STT_FUNC
19671       || ELF_ST_TYPE (dst->st_info) == STT_GNU_IFUNC)
19672     {
19673       if (dst->st_value & 1)
19674         {
19675           dst->st_value &= ~(bfd_vma) 1;
19676           ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal,
19677                                    ST_BRANCH_TO_THUMB);
19678         }
19679       else
19680         ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal, ST_BRANCH_TO_ARM);
19681     }
19682   else if (ELF_ST_TYPE (dst->st_info) == STT_ARM_TFUNC)
19683     {
19684       dst->st_info = ELF_ST_INFO (ELF_ST_BIND (dst->st_info), STT_FUNC);
19685       ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal, ST_BRANCH_TO_THUMB);
19686     }
19687   else if (ELF_ST_TYPE (dst->st_info) == STT_SECTION)
19688     ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal, ST_BRANCH_LONG);
19689   else
19690     ARM_SET_SYM_BRANCH_TYPE (dst->st_target_internal, ST_BRANCH_UNKNOWN);
19691
19692   /* Mark CMSE special symbols.  */
19693   symtab_hdr = & elf_symtab_hdr (abfd);
19694   if (symtab_hdr->sh_size)
19695     name = bfd_elf_sym_name (abfd, symtab_hdr, dst, NULL);
19696   if (name && CONST_STRNEQ (name, CMSE_PREFIX))
19697     ARM_SET_SYM_CMSE_SPCL (dst->st_target_internal);
19698
19699   return TRUE;
19700 }
19701
19702
19703 /* Mangle thumb function symbols as we write them out.  */
19704
19705 static void
19706 elf32_arm_swap_symbol_out (bfd *abfd,
19707                            const Elf_Internal_Sym *src,
19708                            void *cdst,
19709                            void *shndx)
19710 {
19711   Elf_Internal_Sym newsym;
19712
19713   /* We convert STT_ARM_TFUNC symbols into STT_FUNC with the low bit
19714      of the address set, as per the new EABI.  We do this unconditionally
19715      because objcopy does not set the elf header flags until after
19716      it writes out the symbol table.  */
19717   if (ARM_GET_SYM_BRANCH_TYPE (src->st_target_internal) == ST_BRANCH_TO_THUMB)
19718     {
19719       newsym = *src;
19720       if (ELF_ST_TYPE (src->st_info) != STT_GNU_IFUNC)
19721         newsym.st_info = ELF_ST_INFO (ELF_ST_BIND (src->st_info), STT_FUNC);
19722       if (newsym.st_shndx != SHN_UNDEF)
19723         {
19724           /* Do this only for defined symbols. At link type, the static
19725              linker will simulate the work of dynamic linker of resolving
19726              symbols and will carry over the thumbness of found symbols to
19727              the output symbol table. It's not clear how it happens, but
19728              the thumbness of undefined symbols can well be different at
19729              runtime, and writing '1' for them will be confusing for users
19730              and possibly for dynamic linker itself.
19731           */
19732           newsym.st_value |= 1;
19733         }
19734
19735       src = &newsym;
19736     }
19737   bfd_elf32_swap_symbol_out (abfd, src, cdst, shndx);
19738 }
19739
19740 /* Add the PT_ARM_EXIDX program header.  */
19741
19742 static bfd_boolean
19743 elf32_arm_modify_segment_map (bfd *abfd,
19744                               struct bfd_link_info *info ATTRIBUTE_UNUSED)
19745 {
19746   struct elf_segment_map *m;
19747   asection *sec;
19748
19749   sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
19750   if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
19751     {
19752       /* If there is already a PT_ARM_EXIDX header, then we do not
19753          want to add another one.  This situation arises when running
19754          "strip"; the input binary already has the header.  */
19755       m = elf_seg_map (abfd);
19756       while (m && m->p_type != PT_ARM_EXIDX)
19757         m = m->next;
19758       if (!m)
19759         {
19760           m = (struct elf_segment_map *)
19761               bfd_zalloc (abfd, sizeof (struct elf_segment_map));
19762           if (m == NULL)
19763             return FALSE;
19764           m->p_type = PT_ARM_EXIDX;
19765           m->count = 1;
19766           m->sections[0] = sec;
19767
19768           m->next = elf_seg_map (abfd);
19769           elf_seg_map (abfd) = m;
19770         }
19771     }
19772
19773   return TRUE;
19774 }
19775
19776 /* We may add a PT_ARM_EXIDX program header.  */
19777
19778 static int
19779 elf32_arm_additional_program_headers (bfd *abfd,
19780                                       struct bfd_link_info *info ATTRIBUTE_UNUSED)
19781 {
19782   asection *sec;
19783
19784   sec = bfd_get_section_by_name (abfd, ".ARM.exidx");
19785   if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
19786     return 1;
19787   else
19788     return 0;
19789 }
19790
19791 /* Hook called by the linker routine which adds symbols from an object
19792    file.  */
19793
19794 static bfd_boolean
19795 elf32_arm_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
19796                            Elf_Internal_Sym *sym, const char **namep,
19797                            flagword *flagsp, asection **secp, bfd_vma *valp)
19798 {
19799   if (elf32_arm_hash_table (info) == NULL)
19800     return FALSE;
19801
19802   if (elf32_arm_hash_table (info)->vxworks_p
19803       && !elf_vxworks_add_symbol_hook (abfd, info, sym, namep,
19804                                        flagsp, secp, valp))
19805     return FALSE;
19806
19807   return TRUE;
19808 }
19809
19810 /* We use this to override swap_symbol_in and swap_symbol_out.  */
19811 const struct elf_size_info elf32_arm_size_info =
19812 {
19813   sizeof (Elf32_External_Ehdr),
19814   sizeof (Elf32_External_Phdr),
19815   sizeof (Elf32_External_Shdr),
19816   sizeof (Elf32_External_Rel),
19817   sizeof (Elf32_External_Rela),
19818   sizeof (Elf32_External_Sym),
19819   sizeof (Elf32_External_Dyn),
19820   sizeof (Elf_External_Note),
19821   4,
19822   1,
19823   32, 2,
19824   ELFCLASS32, EV_CURRENT,
19825   bfd_elf32_write_out_phdrs,
19826   bfd_elf32_write_shdrs_and_ehdr,
19827   bfd_elf32_checksum_contents,
19828   bfd_elf32_write_relocs,
19829   elf32_arm_swap_symbol_in,
19830   elf32_arm_swap_symbol_out,
19831   bfd_elf32_slurp_reloc_table,
19832   bfd_elf32_slurp_symbol_table,
19833   bfd_elf32_swap_dyn_in,
19834   bfd_elf32_swap_dyn_out,
19835   bfd_elf32_swap_reloc_in,
19836   bfd_elf32_swap_reloc_out,
19837   bfd_elf32_swap_reloca_in,
19838   bfd_elf32_swap_reloca_out
19839 };
19840
19841 static bfd_vma
19842 read_code32 (const bfd *abfd, const bfd_byte *addr)
19843 {
19844   /* V7 BE8 code is always little endian.  */
19845   if ((elf_elfheader (abfd)->e_flags & EF_ARM_BE8) != 0)
19846     return bfd_getl32 (addr);
19847
19848   return bfd_get_32 (abfd, addr);
19849 }
19850
19851 static bfd_vma
19852 read_code16 (const bfd *abfd, const bfd_byte *addr)
19853 {
19854   /* V7 BE8 code is always little endian.  */
19855   if ((elf_elfheader (abfd)->e_flags & EF_ARM_BE8) != 0)
19856     return bfd_getl16 (addr);
19857
19858   return bfd_get_16 (abfd, addr);
19859 }
19860
19861 /* Return size of plt0 entry starting at ADDR
19862    or (bfd_vma) -1 if size can not be determined.  */
19863
19864 static bfd_vma
19865 elf32_arm_plt0_size (const bfd *abfd, const bfd_byte *addr)
19866 {
19867   bfd_vma first_word;
19868   bfd_vma plt0_size;
19869
19870   first_word = read_code32 (abfd, addr);
19871
19872   if (first_word == elf32_arm_plt0_entry[0])
19873     plt0_size = 4 * ARRAY_SIZE (elf32_arm_plt0_entry);
19874   else if (first_word == elf32_thumb2_plt0_entry[0])
19875     plt0_size = 4 * ARRAY_SIZE (elf32_thumb2_plt0_entry);
19876   else
19877     /* We don't yet handle this PLT format.  */
19878     return (bfd_vma) -1;
19879
19880   return plt0_size;
19881 }
19882
19883 /* Return size of plt entry starting at offset OFFSET
19884    of plt section located at address START
19885    or (bfd_vma) -1 if size can not be determined.  */
19886
19887 static bfd_vma
19888 elf32_arm_plt_size (const bfd *abfd, const bfd_byte *start, bfd_vma offset)
19889 {
19890   bfd_vma first_insn;
19891   bfd_vma plt_size = 0;
19892   const bfd_byte *addr = start + offset;
19893
19894   /* PLT entry size if fixed on Thumb-only platforms.  */
19895   if (read_code32 (abfd, start) == elf32_thumb2_plt0_entry[0])
19896       return 4 * ARRAY_SIZE (elf32_thumb2_plt_entry);
19897
19898   /* Respect Thumb stub if necessary.  */
19899   if (read_code16 (abfd, addr) == elf32_arm_plt_thumb_stub[0])
19900     {
19901       plt_size += 2 * ARRAY_SIZE(elf32_arm_plt_thumb_stub);
19902     }
19903
19904   /* Strip immediate from first add.  */
19905   first_insn = read_code32 (abfd, addr + plt_size) & 0xffffff00;
19906
19907 #ifdef FOUR_WORD_PLT
19908   if (first_insn == elf32_arm_plt_entry[0])
19909     plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry);
19910 #else
19911   if (first_insn == elf32_arm_plt_entry_long[0])
19912     plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry_long);
19913   else if (first_insn == elf32_arm_plt_entry_short[0])
19914     plt_size += 4 * ARRAY_SIZE (elf32_arm_plt_entry_short);
19915 #endif
19916   else
19917     /* We don't yet handle this PLT format.  */
19918     return (bfd_vma) -1;
19919
19920   return plt_size;
19921 }
19922
19923 /* Implementation is shamelessly borrowed from _bfd_elf_get_synthetic_symtab.  */
19924
19925 static long
19926 elf32_arm_get_synthetic_symtab (bfd *abfd,
19927                                long symcount ATTRIBUTE_UNUSED,
19928                                asymbol **syms ATTRIBUTE_UNUSED,
19929                                long dynsymcount,
19930                                asymbol **dynsyms,
19931                                asymbol **ret)
19932 {
19933   asection *relplt;
19934   asymbol *s;
19935   arelent *p;
19936   long count, i, n;
19937   size_t size;
19938   Elf_Internal_Shdr *hdr;
19939   char *names;
19940   asection *plt;
19941   bfd_vma offset;
19942   bfd_byte *data;
19943
19944   *ret = NULL;
19945
19946   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
19947     return 0;
19948
19949   if (dynsymcount <= 0)
19950     return 0;
19951
19952   relplt = bfd_get_section_by_name (abfd, ".rel.plt");
19953   if (relplt == NULL)
19954     return 0;
19955
19956   hdr = &elf_section_data (relplt)->this_hdr;
19957   if (hdr->sh_link != elf_dynsymtab (abfd)
19958       || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
19959     return 0;
19960
19961   plt = bfd_get_section_by_name (abfd, ".plt");
19962   if (plt == NULL)
19963     return 0;
19964
19965   if (!elf32_arm_size_info.slurp_reloc_table (abfd, relplt, dynsyms, TRUE))
19966     return -1;
19967
19968   data = plt->contents;
19969   if (data == NULL)
19970     {
19971       if (!bfd_get_full_section_contents(abfd, (asection *) plt, &data) || data == NULL)
19972         return -1;
19973       bfd_cache_section_contents((asection *) plt, data);
19974     }
19975
19976   count = relplt->size / hdr->sh_entsize;
19977   size = count * sizeof (asymbol);
19978   p = relplt->relocation;
19979   for (i = 0; i < count; i++, p += elf32_arm_size_info.int_rels_per_ext_rel)
19980     {
19981       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
19982       if (p->addend != 0)
19983         size += sizeof ("+0x") - 1 + 8;
19984     }
19985
19986   s = *ret = (asymbol *) bfd_malloc (size);
19987   if (s == NULL)
19988     return -1;
19989
19990   offset = elf32_arm_plt0_size (abfd, data);
19991   if (offset == (bfd_vma) -1)
19992     return -1;
19993
19994   names = (char *) (s + count);
19995   p = relplt->relocation;
19996   n = 0;
19997   for (i = 0; i < count; i++, p += elf32_arm_size_info.int_rels_per_ext_rel)
19998     {
19999       size_t len;
20000
20001       bfd_vma plt_size = elf32_arm_plt_size (abfd, data, offset);
20002       if (plt_size == (bfd_vma) -1)
20003         break;
20004
20005       *s = **p->sym_ptr_ptr;
20006       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
20007          we are defining a symbol, ensure one of them is set.  */
20008       if ((s->flags & BSF_LOCAL) == 0)
20009         s->flags |= BSF_GLOBAL;
20010       s->flags |= BSF_SYNTHETIC;
20011       s->section = plt;
20012       s->value = offset;
20013       s->name = names;
20014       s->udata.p = NULL;
20015       len = strlen ((*p->sym_ptr_ptr)->name);
20016       memcpy (names, (*p->sym_ptr_ptr)->name, len);
20017       names += len;
20018       if (p->addend != 0)
20019         {
20020           char buf[30], *a;
20021
20022           memcpy (names, "+0x", sizeof ("+0x") - 1);
20023           names += sizeof ("+0x") - 1;
20024           bfd_sprintf_vma (abfd, buf, p->addend);
20025           for (a = buf; *a == '0'; ++a)
20026             ;
20027           len = strlen (a);
20028           memcpy (names, a, len);
20029           names += len;
20030         }
20031       memcpy (names, "@plt", sizeof ("@plt"));
20032       names += sizeof ("@plt");
20033       ++s, ++n;
20034       offset += plt_size;
20035     }
20036
20037   return n;
20038 }
20039
20040 static bfd_boolean
20041 elf32_arm_section_flags (flagword *flags, const Elf_Internal_Shdr * hdr)
20042 {
20043   if (hdr->sh_flags & SHF_ARM_PURECODE)
20044     *flags |= SEC_ELF_PURECODE;
20045   return TRUE;
20046 }
20047
20048 static flagword
20049 elf32_arm_lookup_section_flags (char *flag_name)
20050 {
20051   if (!strcmp (flag_name, "SHF_ARM_PURECODE"))
20052     return SHF_ARM_PURECODE;
20053
20054   return SEC_NO_FLAGS;
20055 }
20056
20057 static unsigned int
20058 elf32_arm_count_additional_relocs (asection *sec)
20059 {
20060   struct _arm_elf_section_data *arm_data;
20061   arm_data = get_arm_elf_section_data (sec);
20062
20063   return arm_data == NULL ? 0 : arm_data->additional_reloc_count;
20064 }
20065
20066 /* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which
20067    has a type >= SHT_LOOS.  Returns TRUE if these fields were initialised
20068    FALSE otherwise.  ISECTION is the best guess matching section from the
20069    input bfd IBFD, but it might be NULL.  */
20070
20071 static bfd_boolean
20072 elf32_arm_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
20073                                        bfd *obfd ATTRIBUTE_UNUSED,
20074                                        const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
20075                                        Elf_Internal_Shdr *osection)
20076 {
20077   switch (osection->sh_type)
20078     {
20079     case SHT_ARM_EXIDX:
20080       {
20081         Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
20082         Elf_Internal_Shdr **iheaders = elf_elfsections (ibfd);
20083         unsigned i = 0;
20084
20085         osection->sh_flags = SHF_ALLOC | SHF_LINK_ORDER;
20086         osection->sh_info = 0;
20087
20088         /* The sh_link field must be set to the text section associated with
20089            this index section.  Unfortunately the ARM EHABI does not specify
20090            exactly how to determine this association.  Our caller does try
20091            to match up OSECTION with its corresponding input section however
20092            so that is a good first guess.  */
20093         if (isection != NULL
20094             && osection->bfd_section != NULL
20095             && isection->bfd_section != NULL
20096             && isection->bfd_section->output_section != NULL
20097             && isection->bfd_section->output_section == osection->bfd_section
20098             && iheaders != NULL
20099             && isection->sh_link > 0
20100             && isection->sh_link < elf_numsections (ibfd)
20101             && iheaders[isection->sh_link]->bfd_section != NULL
20102             && iheaders[isection->sh_link]->bfd_section->output_section != NULL
20103             )
20104           {
20105             for (i = elf_numsections (obfd); i-- > 0;)
20106               if (oheaders[i]->bfd_section
20107                   == iheaders[isection->sh_link]->bfd_section->output_section)
20108                 break;
20109           }
20110
20111         if (i == 0)
20112           {
20113             /* Failing that we have to find a matching section ourselves.  If
20114                we had the output section name available we could compare that
20115                with input section names.  Unfortunately we don't.  So instead
20116                we use a simple heuristic and look for the nearest executable
20117                section before this one.  */
20118             for (i = elf_numsections (obfd); i-- > 0;)
20119               if (oheaders[i] == osection)
20120                 break;
20121             if (i == 0)
20122               break;
20123
20124             while (i-- > 0)
20125               if (oheaders[i]->sh_type == SHT_PROGBITS
20126                   && (oheaders[i]->sh_flags & (SHF_ALLOC | SHF_EXECINSTR))
20127                   == (SHF_ALLOC | SHF_EXECINSTR))
20128                 break;
20129           }
20130
20131         if (i)
20132           {
20133             osection->sh_link = i;
20134             /* If the text section was part of a group
20135                then the index section should be too.  */
20136             if (oheaders[i]->sh_flags & SHF_GROUP)
20137               osection->sh_flags |= SHF_GROUP;
20138             return TRUE;
20139           }
20140       }
20141       break;
20142
20143     case SHT_ARM_PREEMPTMAP:
20144       osection->sh_flags = SHF_ALLOC;
20145       break;
20146
20147     case SHT_ARM_ATTRIBUTES:
20148     case SHT_ARM_DEBUGOVERLAY:
20149     case SHT_ARM_OVERLAYSECTION:
20150     default:
20151       break;
20152     }
20153
20154   return FALSE;
20155 }
20156
20157 /* Returns TRUE if NAME is an ARM mapping symbol.
20158    Traditionally the symbols $a, $d and $t have been used.
20159    The ARM ELF standard also defines $x (for A64 code).  It also allows a
20160    period initiated suffix to be added to the symbol: "$[adtx]\.[:sym_char]+".
20161    Other tools might also produce $b (Thumb BL), $f, $p, $m and $v, but we do
20162    not support them here.  $t.x indicates the start of ThumbEE instructions.  */
20163
20164 static bfd_boolean
20165 is_arm_mapping_symbol (const char * name)
20166 {
20167   return name != NULL /* Paranoia.  */
20168     && name[0] == '$' /* Note: if objcopy --prefix-symbols has been used then
20169                          the mapping symbols could have acquired a prefix.
20170                          We do not support this here, since such symbols no
20171                          longer conform to the ARM ELF ABI.  */
20172     && (name[1] == 'a' || name[1] == 'd' || name[1] == 't' || name[1] == 'x')
20173     && (name[2] == 0 || name[2] == '.');
20174   /* FIXME: Strictly speaking the symbol is only a valid mapping symbol if
20175      any characters that follow the period are legal characters for the body
20176      of a symbol's name.  For now we just assume that this is the case.  */
20177 }
20178
20179 /* Make sure that mapping symbols in object files are not removed via the
20180    "strip --strip-unneeded" tool.  These symbols are needed in order to
20181    correctly generate interworking veneers, and for byte swapping code
20182    regions.  Once an object file has been linked, it is safe to remove the
20183    symbols as they will no longer be needed.  */
20184
20185 static void
20186 elf32_arm_backend_symbol_processing (bfd *abfd, asymbol *sym)
20187 {
20188   if (((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
20189       && sym->section != bfd_abs_section_ptr
20190       && is_arm_mapping_symbol (sym->name))
20191     sym->flags |= BSF_KEEP;
20192 }
20193
20194 #undef  elf_backend_copy_special_section_fields
20195 #define elf_backend_copy_special_section_fields elf32_arm_copy_special_section_fields
20196
20197 #define ELF_ARCH                        bfd_arch_arm
20198 #define ELF_TARGET_ID                   ARM_ELF_DATA
20199 #define ELF_MACHINE_CODE                EM_ARM
20200 #ifdef __QNXTARGET__
20201 #define ELF_MAXPAGESIZE                 0x1000
20202 #else
20203 #define ELF_MAXPAGESIZE                 0x10000
20204 #endif
20205 #define ELF_MINPAGESIZE                 0x1000
20206 #define ELF_COMMONPAGESIZE              0x1000
20207
20208 #define bfd_elf32_mkobject                      elf32_arm_mkobject
20209
20210 #define bfd_elf32_bfd_copy_private_bfd_data     elf32_arm_copy_private_bfd_data
20211 #define bfd_elf32_bfd_merge_private_bfd_data    elf32_arm_merge_private_bfd_data
20212 #define bfd_elf32_bfd_set_private_flags         elf32_arm_set_private_flags
20213 #define bfd_elf32_bfd_print_private_bfd_data    elf32_arm_print_private_bfd_data
20214 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_link_hash_table_create
20215 #define bfd_elf32_bfd_reloc_type_lookup         elf32_arm_reloc_type_lookup
20216 #define bfd_elf32_bfd_reloc_name_lookup         elf32_arm_reloc_name_lookup
20217 #define bfd_elf32_find_nearest_line             elf32_arm_find_nearest_line
20218 #define bfd_elf32_find_inliner_info             elf32_arm_find_inliner_info
20219 #define bfd_elf32_new_section_hook              elf32_arm_new_section_hook
20220 #define bfd_elf32_bfd_is_target_special_symbol  elf32_arm_is_target_special_symbol
20221 #define bfd_elf32_bfd_final_link                elf32_arm_final_link
20222 #define bfd_elf32_get_synthetic_symtab  elf32_arm_get_synthetic_symtab
20223
20224 #define elf_backend_get_symbol_type             elf32_arm_get_symbol_type
20225 #define elf_backend_gc_mark_hook                elf32_arm_gc_mark_hook
20226 #define elf_backend_gc_mark_extra_sections      elf32_arm_gc_mark_extra_sections
20227 #define elf_backend_check_relocs                elf32_arm_check_relocs
20228 #define elf_backend_update_relocs               elf32_arm_update_relocs
20229 #define elf_backend_relocate_section            elf32_arm_relocate_section
20230 #define elf_backend_write_section               elf32_arm_write_section
20231 #define elf_backend_adjust_dynamic_symbol       elf32_arm_adjust_dynamic_symbol
20232 #define elf_backend_create_dynamic_sections     elf32_arm_create_dynamic_sections
20233 #define elf_backend_finish_dynamic_symbol       elf32_arm_finish_dynamic_symbol
20234 #define elf_backend_finish_dynamic_sections     elf32_arm_finish_dynamic_sections
20235 #define elf_backend_size_dynamic_sections       elf32_arm_size_dynamic_sections
20236 #define elf_backend_always_size_sections        elf32_arm_always_size_sections
20237 #define elf_backend_init_index_section          _bfd_elf_init_2_index_sections
20238 #define elf_backend_post_process_headers        elf32_arm_post_process_headers
20239 #define elf_backend_reloc_type_class            elf32_arm_reloc_type_class
20240 #define elf_backend_object_p                    elf32_arm_object_p
20241 #define elf_backend_fake_sections               elf32_arm_fake_sections
20242 #define elf_backend_section_from_shdr           elf32_arm_section_from_shdr
20243 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
20244 #define elf_backend_copy_indirect_symbol        elf32_arm_copy_indirect_symbol
20245 #define elf_backend_size_info                   elf32_arm_size_info
20246 #define elf_backend_modify_segment_map          elf32_arm_modify_segment_map
20247 #define elf_backend_additional_program_headers  elf32_arm_additional_program_headers
20248 #define elf_backend_output_arch_local_syms      elf32_arm_output_arch_local_syms
20249 #define elf_backend_filter_implib_symbols       elf32_arm_filter_implib_symbols
20250 #define elf_backend_begin_write_processing      elf32_arm_begin_write_processing
20251 #define elf_backend_add_symbol_hook             elf32_arm_add_symbol_hook
20252 #define elf_backend_count_additional_relocs     elf32_arm_count_additional_relocs
20253 #define elf_backend_symbol_processing           elf32_arm_backend_symbol_processing
20254
20255 #define elf_backend_can_refcount       1
20256 #define elf_backend_can_gc_sections    1
20257 #define elf_backend_plt_readonly       1
20258 #define elf_backend_want_got_plt       1
20259 #define elf_backend_want_plt_sym       0
20260 #define elf_backend_want_dynrelro      1
20261 #define elf_backend_may_use_rel_p      1
20262 #define elf_backend_may_use_rela_p     0
20263 #define elf_backend_default_use_rela_p 0
20264 #define elf_backend_dtrel_excludes_plt 1
20265
20266 #define elf_backend_got_header_size     12
20267 #define elf_backend_extern_protected_data 1
20268
20269 #undef  elf_backend_obj_attrs_vendor
20270 #define elf_backend_obj_attrs_vendor            "aeabi"
20271 #undef  elf_backend_obj_attrs_section
20272 #define elf_backend_obj_attrs_section           ".ARM.attributes"
20273 #undef  elf_backend_obj_attrs_arg_type
20274 #define elf_backend_obj_attrs_arg_type          elf32_arm_obj_attrs_arg_type
20275 #undef  elf_backend_obj_attrs_section_type
20276 #define elf_backend_obj_attrs_section_type      SHT_ARM_ATTRIBUTES
20277 #define elf_backend_obj_attrs_order             elf32_arm_obj_attrs_order
20278 #define elf_backend_obj_attrs_handle_unknown    elf32_arm_obj_attrs_handle_unknown
20279
20280 #undef  elf_backend_section_flags
20281 #define elf_backend_section_flags               elf32_arm_section_flags
20282 #undef  elf_backend_lookup_section_flags_hook
20283 #define elf_backend_lookup_section_flags_hook   elf32_arm_lookup_section_flags
20284
20285 #define elf_backend_linux_prpsinfo32_ugid16     TRUE
20286
20287 #include "elf32-target.h"
20288
20289 /* Native Client targets.  */
20290
20291 #undef  TARGET_LITTLE_SYM
20292 #define TARGET_LITTLE_SYM               arm_elf32_nacl_le_vec
20293 #undef  TARGET_LITTLE_NAME
20294 #define TARGET_LITTLE_NAME              "elf32-littlearm-nacl"
20295 #undef  TARGET_BIG_SYM
20296 #define TARGET_BIG_SYM                  arm_elf32_nacl_be_vec
20297 #undef  TARGET_BIG_NAME
20298 #define TARGET_BIG_NAME                 "elf32-bigarm-nacl"
20299
20300 /* Like elf32_arm_link_hash_table_create -- but overrides
20301    appropriately for NaCl.  */
20302
20303 static struct bfd_link_hash_table *
20304 elf32_arm_nacl_link_hash_table_create (bfd *abfd)
20305 {
20306   struct bfd_link_hash_table *ret;
20307
20308   ret = elf32_arm_link_hash_table_create (abfd);
20309   if (ret)
20310     {
20311       struct elf32_arm_link_hash_table *htab
20312         = (struct elf32_arm_link_hash_table *) ret;
20313
20314       htab->nacl_p = 1;
20315
20316       htab->plt_header_size = 4 * ARRAY_SIZE (elf32_arm_nacl_plt0_entry);
20317       htab->plt_entry_size = 4 * ARRAY_SIZE (elf32_arm_nacl_plt_entry);
20318     }
20319   return ret;
20320 }
20321
20322 /* Since NaCl doesn't use the ARM-specific unwind format, we don't
20323    really need to use elf32_arm_modify_segment_map.  But we do it
20324    anyway just to reduce gratuitous differences with the stock ARM backend.  */
20325
20326 static bfd_boolean
20327 elf32_arm_nacl_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
20328 {
20329   return (elf32_arm_modify_segment_map (abfd, info)
20330           && nacl_modify_segment_map (abfd, info));
20331 }
20332
20333 static void
20334 elf32_arm_nacl_final_write_processing (bfd *abfd, bfd_boolean linker)
20335 {
20336   elf32_arm_final_write_processing (abfd, linker);
20337   nacl_final_write_processing (abfd, linker);
20338 }
20339
20340 static bfd_vma
20341 elf32_arm_nacl_plt_sym_val (bfd_vma i, const asection *plt,
20342                             const arelent *rel ATTRIBUTE_UNUSED)
20343 {
20344   return plt->vma
20345     + 4 * (ARRAY_SIZE (elf32_arm_nacl_plt0_entry) +
20346            i * ARRAY_SIZE (elf32_arm_nacl_plt_entry));
20347 }
20348
20349 #undef  elf32_bed
20350 #define elf32_bed                               elf32_arm_nacl_bed
20351 #undef  bfd_elf32_bfd_link_hash_table_create
20352 #define bfd_elf32_bfd_link_hash_table_create    \
20353   elf32_arm_nacl_link_hash_table_create
20354 #undef  elf_backend_plt_alignment
20355 #define elf_backend_plt_alignment               4
20356 #undef  elf_backend_modify_segment_map
20357 #define elf_backend_modify_segment_map          elf32_arm_nacl_modify_segment_map
20358 #undef  elf_backend_modify_program_headers
20359 #define elf_backend_modify_program_headers      nacl_modify_program_headers
20360 #undef  elf_backend_final_write_processing
20361 #define elf_backend_final_write_processing      elf32_arm_nacl_final_write_processing
20362 #undef bfd_elf32_get_synthetic_symtab
20363 #undef  elf_backend_plt_sym_val
20364 #define elf_backend_plt_sym_val                 elf32_arm_nacl_plt_sym_val
20365 #undef  elf_backend_copy_special_section_fields
20366
20367 #undef  ELF_MINPAGESIZE
20368 #undef  ELF_COMMONPAGESIZE
20369
20370
20371 #include "elf32-target.h"
20372
20373 /* Reset to defaults.  */
20374 #undef  elf_backend_plt_alignment
20375 #undef  elf_backend_modify_segment_map
20376 #define elf_backend_modify_segment_map          elf32_arm_modify_segment_map
20377 #undef  elf_backend_modify_program_headers
20378 #undef  elf_backend_final_write_processing
20379 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
20380 #undef  ELF_MINPAGESIZE
20381 #define ELF_MINPAGESIZE                 0x1000
20382 #undef  ELF_COMMONPAGESIZE
20383 #define ELF_COMMONPAGESIZE              0x1000
20384
20385
20386 /* FDPIC Targets.  */
20387
20388 #undef  TARGET_LITTLE_SYM
20389 #define TARGET_LITTLE_SYM               arm_elf32_fdpic_le_vec
20390 #undef  TARGET_LITTLE_NAME
20391 #define TARGET_LITTLE_NAME              "elf32-littlearm-fdpic"
20392 #undef  TARGET_BIG_SYM
20393 #define TARGET_BIG_SYM                  arm_elf32_fdpic_be_vec
20394 #undef  TARGET_BIG_NAME
20395 #define TARGET_BIG_NAME                 "elf32-bigarm-fdpic"
20396 #undef elf_match_priority
20397 #define elf_match_priority              128
20398 #undef ELF_OSABI
20399 #define ELF_OSABI               ELFOSABI_ARM_FDPIC
20400
20401 /* Like elf32_arm_link_hash_table_create -- but overrides
20402    appropriately for FDPIC.  */
20403
20404 static struct bfd_link_hash_table *
20405 elf32_arm_fdpic_link_hash_table_create (bfd *abfd)
20406 {
20407   struct bfd_link_hash_table *ret;
20408
20409   ret = elf32_arm_link_hash_table_create (abfd);
20410   if (ret)
20411     {
20412       struct elf32_arm_link_hash_table *htab = (struct elf32_arm_link_hash_table *) ret;
20413
20414       htab->fdpic_p = 1;
20415     }
20416   return ret;
20417 }
20418
20419 /* We need dynamic symbols for every section, since segments can
20420    relocate independently.  */
20421 static bfd_boolean
20422 elf32_arm_fdpic_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
20423                                     struct bfd_link_info *info
20424                                     ATTRIBUTE_UNUSED,
20425                                     asection *p ATTRIBUTE_UNUSED)
20426 {
20427   switch (elf_section_data (p)->this_hdr.sh_type)
20428     {
20429     case SHT_PROGBITS:
20430     case SHT_NOBITS:
20431       /* If sh_type is yet undecided, assume it could be
20432          SHT_PROGBITS/SHT_NOBITS.  */
20433     case SHT_NULL:
20434       return FALSE;
20435
20436       /* There shouldn't be section relative relocations
20437          against any other section.  */
20438     default:
20439       return TRUE;
20440     }
20441 }
20442
20443 #undef  elf32_bed
20444 #define elf32_bed                               elf32_arm_fdpic_bed
20445
20446 #undef  bfd_elf32_bfd_link_hash_table_create
20447 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_fdpic_link_hash_table_create
20448
20449 #undef elf_backend_omit_section_dynsym
20450 #define elf_backend_omit_section_dynsym         elf32_arm_fdpic_omit_section_dynsym
20451
20452 #include "elf32-target.h"
20453
20454 #undef elf_match_priority
20455 #undef ELF_OSABI
20456 #undef elf_backend_omit_section_dynsym
20457
20458 /* VxWorks Targets.  */
20459
20460 #undef  TARGET_LITTLE_SYM
20461 #define TARGET_LITTLE_SYM               arm_elf32_vxworks_le_vec
20462 #undef  TARGET_LITTLE_NAME
20463 #define TARGET_LITTLE_NAME              "elf32-littlearm-vxworks"
20464 #undef  TARGET_BIG_SYM
20465 #define TARGET_BIG_SYM                  arm_elf32_vxworks_be_vec
20466 #undef  TARGET_BIG_NAME
20467 #define TARGET_BIG_NAME                 "elf32-bigarm-vxworks"
20468
20469 /* Like elf32_arm_link_hash_table_create -- but overrides
20470    appropriately for VxWorks.  */
20471
20472 static struct bfd_link_hash_table *
20473 elf32_arm_vxworks_link_hash_table_create (bfd *abfd)
20474 {
20475   struct bfd_link_hash_table *ret;
20476
20477   ret = elf32_arm_link_hash_table_create (abfd);
20478   if (ret)
20479     {
20480       struct elf32_arm_link_hash_table *htab
20481         = (struct elf32_arm_link_hash_table *) ret;
20482       htab->use_rel = 0;
20483       htab->vxworks_p = 1;
20484     }
20485   return ret;
20486 }
20487
20488 static void
20489 elf32_arm_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
20490 {
20491   elf32_arm_final_write_processing (abfd, linker);
20492   elf_vxworks_final_write_processing (abfd, linker);
20493 }
20494
20495 #undef  elf32_bed
20496 #define elf32_bed elf32_arm_vxworks_bed
20497
20498 #undef  bfd_elf32_bfd_link_hash_table_create
20499 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_vxworks_link_hash_table_create
20500 #undef  elf_backend_final_write_processing
20501 #define elf_backend_final_write_processing      elf32_arm_vxworks_final_write_processing
20502 #undef  elf_backend_emit_relocs
20503 #define elf_backend_emit_relocs                 elf_vxworks_emit_relocs
20504
20505 #undef  elf_backend_may_use_rel_p
20506 #define elf_backend_may_use_rel_p       0
20507 #undef  elf_backend_may_use_rela_p
20508 #define elf_backend_may_use_rela_p      1
20509 #undef  elf_backend_default_use_rela_p
20510 #define elf_backend_default_use_rela_p  1
20511 #undef  elf_backend_want_plt_sym
20512 #define elf_backend_want_plt_sym        1
20513 #undef  ELF_MAXPAGESIZE
20514 #define ELF_MAXPAGESIZE                 0x1000
20515
20516 #include "elf32-target.h"
20517
20518
20519 /* Merge backend specific data from an object file to the output
20520    object file when linking.  */
20521
20522 static bfd_boolean
20523 elf32_arm_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
20524 {
20525   bfd *obfd = info->output_bfd;
20526   flagword out_flags;
20527   flagword in_flags;
20528   bfd_boolean flags_compatible = TRUE;
20529   asection *sec;
20530
20531   /* Check if we have the same endianness.  */
20532   if (! _bfd_generic_verify_endian_match (ibfd, info))
20533     return FALSE;
20534
20535   if (! is_arm_elf (ibfd) || ! is_arm_elf (obfd))
20536     return TRUE;
20537
20538   if (!elf32_arm_merge_eabi_attributes (ibfd, info))
20539     return FALSE;
20540
20541   /* The input BFD must have had its flags initialised.  */
20542   /* The following seems bogus to me -- The flags are initialized in
20543      the assembler but I don't think an elf_flags_init field is
20544      written into the object.  */
20545   /* BFD_ASSERT (elf_flags_init (ibfd)); */
20546
20547   in_flags  = elf_elfheader (ibfd)->e_flags;
20548   out_flags = elf_elfheader (obfd)->e_flags;
20549
20550   /* In theory there is no reason why we couldn't handle this.  However
20551      in practice it isn't even close to working and there is no real
20552      reason to want it.  */
20553   if (EF_ARM_EABI_VERSION (in_flags) >= EF_ARM_EABI_VER4
20554       && !(ibfd->flags & DYNAMIC)
20555       && (in_flags & EF_ARM_BE8))
20556     {
20557       _bfd_error_handler (_("error: %pB is already in final BE8 format"),
20558                           ibfd);
20559       return FALSE;
20560     }
20561
20562   if (!elf_flags_init (obfd))
20563     {
20564       /* If the input is the default architecture and had the default
20565          flags then do not bother setting the flags for the output
20566          architecture, instead allow future merges to do this.  If no
20567          future merges ever set these flags then they will retain their
20568          uninitialised values, which surprise surprise, correspond
20569          to the default values.  */
20570       if (bfd_get_arch_info (ibfd)->the_default
20571           && elf_elfheader (ibfd)->e_flags == 0)
20572         return TRUE;
20573
20574       elf_flags_init (obfd) = TRUE;
20575       elf_elfheader (obfd)->e_flags = in_flags;
20576
20577       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
20578           && bfd_get_arch_info (obfd)->the_default)
20579         return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
20580
20581       return TRUE;
20582     }
20583
20584   /* Determine what should happen if the input ARM architecture
20585      does not match the output ARM architecture.  */
20586   if (! bfd_arm_merge_machines (ibfd, obfd))
20587     return FALSE;
20588
20589   /* Identical flags must be compatible.  */
20590   if (in_flags == out_flags)
20591     return TRUE;
20592
20593   /* Check to see if the input BFD actually contains any sections.  If
20594      not, its flags may not have been initialised either, but it
20595      cannot actually cause any incompatiblity.  Do not short-circuit
20596      dynamic objects; their section list may be emptied by
20597     elf_link_add_object_symbols.
20598
20599     Also check to see if there are no code sections in the input.
20600     In this case there is no need to check for code specific flags.
20601     XXX - do we need to worry about floating-point format compatability
20602     in data sections ?  */
20603   if (!(ibfd->flags & DYNAMIC))
20604     {
20605       bfd_boolean null_input_bfd = TRUE;
20606       bfd_boolean only_data_sections = TRUE;
20607
20608       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
20609         {
20610           /* Ignore synthetic glue sections.  */
20611           if (strcmp (sec->name, ".glue_7")
20612               && strcmp (sec->name, ".glue_7t"))
20613             {
20614               if ((bfd_get_section_flags (ibfd, sec)
20615                    & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
20616                   == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
20617                 only_data_sections = FALSE;
20618
20619               null_input_bfd = FALSE;
20620               break;
20621             }
20622         }
20623
20624       if (null_input_bfd || only_data_sections)
20625         return TRUE;
20626     }
20627
20628   /* Complain about various flag mismatches.  */
20629   if (!elf32_arm_versions_compatible (EF_ARM_EABI_VERSION (in_flags),
20630                                       EF_ARM_EABI_VERSION (out_flags)))
20631     {
20632       _bfd_error_handler
20633         (_("error: source object %pB has EABI version %d, but target %pB has EABI version %d"),
20634          ibfd, (in_flags & EF_ARM_EABIMASK) >> 24,
20635          obfd, (out_flags & EF_ARM_EABIMASK) >> 24);
20636       return FALSE;
20637     }
20638
20639   /* Not sure what needs to be checked for EABI versions >= 1.  */
20640   /* VxWorks libraries do not use these flags.  */
20641   if (get_elf_backend_data (obfd) != &elf32_arm_vxworks_bed
20642       && get_elf_backend_data (ibfd) != &elf32_arm_vxworks_bed
20643       && EF_ARM_EABI_VERSION (in_flags) == EF_ARM_EABI_UNKNOWN)
20644     {
20645       if ((in_flags & EF_ARM_APCS_26) != (out_flags & EF_ARM_APCS_26))
20646         {
20647           _bfd_error_handler
20648             (_("error: %pB is compiled for APCS-%d, whereas target %pB uses APCS-%d"),
20649              ibfd, in_flags & EF_ARM_APCS_26 ? 26 : 32,
20650              obfd, out_flags & EF_ARM_APCS_26 ? 26 : 32);
20651           flags_compatible = FALSE;
20652         }
20653
20654       if ((in_flags & EF_ARM_APCS_FLOAT) != (out_flags & EF_ARM_APCS_FLOAT))
20655         {
20656           if (in_flags & EF_ARM_APCS_FLOAT)
20657             _bfd_error_handler
20658               (_("error: %pB passes floats in float registers, whereas %pB passes them in integer registers"),
20659                ibfd, obfd);
20660           else
20661             _bfd_error_handler
20662               (_("error: %pB passes floats in integer registers, whereas %pB passes them in float registers"),
20663                ibfd, obfd);
20664
20665           flags_compatible = FALSE;
20666         }
20667
20668       if ((in_flags & EF_ARM_VFP_FLOAT) != (out_flags & EF_ARM_VFP_FLOAT))
20669         {
20670           if (in_flags & EF_ARM_VFP_FLOAT)
20671             _bfd_error_handler
20672               (_("error: %pB uses %s instructions, whereas %pB does not"),
20673                ibfd, "VFP", obfd);
20674           else
20675             _bfd_error_handler
20676               (_("error: %pB uses %s instructions, whereas %pB does not"),
20677                ibfd, "FPA", obfd);
20678
20679           flags_compatible = FALSE;
20680         }
20681
20682       if ((in_flags & EF_ARM_MAVERICK_FLOAT) != (out_flags & EF_ARM_MAVERICK_FLOAT))
20683         {
20684           if (in_flags & EF_ARM_MAVERICK_FLOAT)
20685             _bfd_error_handler
20686               (_("error: %pB uses %s instructions, whereas %pB does not"),
20687                ibfd, "Maverick", obfd);
20688           else
20689             _bfd_error_handler
20690               (_("error: %pB does not use %s instructions, whereas %pB does"),
20691                ibfd, "Maverick", obfd);
20692
20693           flags_compatible = FALSE;
20694         }
20695
20696 #ifdef EF_ARM_SOFT_FLOAT
20697       if ((in_flags & EF_ARM_SOFT_FLOAT) != (out_flags & EF_ARM_SOFT_FLOAT))
20698         {
20699           /* We can allow interworking between code that is VFP format
20700              layout, and uses either soft float or integer regs for
20701              passing floating point arguments and results.  We already
20702              know that the APCS_FLOAT flags match; similarly for VFP
20703              flags.  */
20704           if ((in_flags & EF_ARM_APCS_FLOAT) != 0
20705               || (in_flags & EF_ARM_VFP_FLOAT) == 0)
20706             {
20707               if (in_flags & EF_ARM_SOFT_FLOAT)
20708                 _bfd_error_handler
20709                   (_("error: %pB uses software FP, whereas %pB uses hardware FP"),
20710                    ibfd, obfd);
20711               else
20712                 _bfd_error_handler
20713                   (_("error: %pB uses hardware FP, whereas %pB uses software FP"),
20714                    ibfd, obfd);
20715
20716               flags_compatible = FALSE;
20717             }
20718         }
20719 #endif
20720
20721       /* Interworking mismatch is only a warning.  */
20722       if ((in_flags & EF_ARM_INTERWORK) != (out_flags & EF_ARM_INTERWORK))
20723         {
20724           if (in_flags & EF_ARM_INTERWORK)
20725             {
20726               _bfd_error_handler
20727                 (_("warning: %pB supports interworking, whereas %pB does not"),
20728                  ibfd, obfd);
20729             }
20730           else
20731             {
20732               _bfd_error_handler
20733                 (_("warning: %pB does not support interworking, whereas %pB does"),
20734                  ibfd, obfd);
20735             }
20736         }
20737     }
20738
20739   return flags_compatible;
20740 }
20741
20742
20743 /* Symbian OS Targets.  */
20744
20745 #undef  TARGET_LITTLE_SYM
20746 #define TARGET_LITTLE_SYM               arm_elf32_symbian_le_vec
20747 #undef  TARGET_LITTLE_NAME
20748 #define TARGET_LITTLE_NAME              "elf32-littlearm-symbian"
20749 #undef  TARGET_BIG_SYM
20750 #define TARGET_BIG_SYM                  arm_elf32_symbian_be_vec
20751 #undef  TARGET_BIG_NAME
20752 #define TARGET_BIG_NAME                 "elf32-bigarm-symbian"
20753
20754 /* Like elf32_arm_link_hash_table_create -- but overrides
20755    appropriately for Symbian OS.  */
20756
20757 static struct bfd_link_hash_table *
20758 elf32_arm_symbian_link_hash_table_create (bfd *abfd)
20759 {
20760   struct bfd_link_hash_table *ret;
20761
20762   ret = elf32_arm_link_hash_table_create (abfd);
20763   if (ret)
20764     {
20765       struct elf32_arm_link_hash_table *htab
20766         = (struct elf32_arm_link_hash_table *)ret;
20767       /* There is no PLT header for Symbian OS.  */
20768       htab->plt_header_size = 0;
20769       /* The PLT entries are each one instruction and one word.  */
20770       htab->plt_entry_size = 4 * ARRAY_SIZE (elf32_arm_symbian_plt_entry);
20771       htab->symbian_p = 1;
20772       /* Symbian uses armv5t or above, so use_blx is always true.  */
20773       htab->use_blx = 1;
20774       htab->root.is_relocatable_executable = 1;
20775     }
20776   return ret;
20777 }
20778
20779 static const struct bfd_elf_special_section
20780 elf32_arm_symbian_special_sections[] =
20781 {
20782   /* In a BPABI executable, the dynamic linking sections do not go in
20783      the loadable read-only segment.  The post-linker may wish to
20784      refer to these sections, but they are not part of the final
20785      program image.  */
20786   { STRING_COMMA_LEN (".dynamic"),       0, SHT_DYNAMIC,  0 },
20787   { STRING_COMMA_LEN (".dynstr"),        0, SHT_STRTAB,   0 },
20788   { STRING_COMMA_LEN (".dynsym"),        0, SHT_DYNSYM,   0 },
20789   { STRING_COMMA_LEN (".got"),           0, SHT_PROGBITS, 0 },
20790   { STRING_COMMA_LEN (".hash"),          0, SHT_HASH,     0 },
20791   /* These sections do not need to be writable as the SymbianOS
20792      postlinker will arrange things so that no dynamic relocation is
20793      required.  */
20794   { STRING_COMMA_LEN (".init_array"),    0, SHT_INIT_ARRAY,    SHF_ALLOC },
20795   { STRING_COMMA_LEN (".fini_array"),    0, SHT_FINI_ARRAY,    SHF_ALLOC },
20796   { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC },
20797   { NULL,                             0, 0, 0,                 0 }
20798 };
20799
20800 static void
20801 elf32_arm_symbian_begin_write_processing (bfd *abfd,
20802                                           struct bfd_link_info *link_info)
20803 {
20804   /* BPABI objects are never loaded directly by an OS kernel; they are
20805      processed by a postlinker first, into an OS-specific format.  If
20806      the D_PAGED bit is set on the file, BFD will align segments on
20807      page boundaries, so that an OS can directly map the file.  With
20808      BPABI objects, that just results in wasted space.  In addition,
20809      because we clear the D_PAGED bit, map_sections_to_segments will
20810      recognize that the program headers should not be mapped into any
20811      loadable segment.  */
20812   abfd->flags &= ~D_PAGED;
20813   elf32_arm_begin_write_processing (abfd, link_info);
20814 }
20815
20816 static bfd_boolean
20817 elf32_arm_symbian_modify_segment_map (bfd *abfd,
20818                                       struct bfd_link_info *info)
20819 {
20820   struct elf_segment_map *m;
20821   asection *dynsec;
20822
20823   /* BPABI shared libraries and executables should have a PT_DYNAMIC
20824      segment.  However, because the .dynamic section is not marked
20825      with SEC_LOAD, the generic ELF code will not create such a
20826      segment.  */
20827   dynsec = bfd_get_section_by_name (abfd, ".dynamic");
20828   if (dynsec)
20829     {
20830       for (m = elf_seg_map (abfd); m != NULL; m = m->next)
20831         if (m->p_type == PT_DYNAMIC)
20832           break;
20833
20834       if (m == NULL)
20835         {
20836           m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
20837           m->next = elf_seg_map (abfd);
20838           elf_seg_map (abfd) = m;
20839         }
20840     }
20841
20842   /* Also call the generic arm routine.  */
20843   return elf32_arm_modify_segment_map (abfd, info);
20844 }
20845
20846 /* Return address for Ith PLT stub in section PLT, for relocation REL
20847    or (bfd_vma) -1 if it should not be included.  */
20848
20849 static bfd_vma
20850 elf32_arm_symbian_plt_sym_val (bfd_vma i, const asection *plt,
20851                                const arelent *rel ATTRIBUTE_UNUSED)
20852 {
20853   return plt->vma + 4 * ARRAY_SIZE (elf32_arm_symbian_plt_entry) * i;
20854 }
20855
20856 #undef  elf32_bed
20857 #define elf32_bed elf32_arm_symbian_bed
20858
20859 /* The dynamic sections are not allocated on SymbianOS; the postlinker
20860    will process them and then discard them.  */
20861 #undef  ELF_DYNAMIC_SEC_FLAGS
20862 #define ELF_DYNAMIC_SEC_FLAGS \
20863   (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED)
20864
20865 #undef elf_backend_emit_relocs
20866
20867 #undef  bfd_elf32_bfd_link_hash_table_create
20868 #define bfd_elf32_bfd_link_hash_table_create    elf32_arm_symbian_link_hash_table_create
20869 #undef  elf_backend_special_sections
20870 #define elf_backend_special_sections            elf32_arm_symbian_special_sections
20871 #undef  elf_backend_begin_write_processing
20872 #define elf_backend_begin_write_processing      elf32_arm_symbian_begin_write_processing
20873 #undef  elf_backend_final_write_processing
20874 #define elf_backend_final_write_processing      elf32_arm_final_write_processing
20875
20876 #undef  elf_backend_modify_segment_map
20877 #define elf_backend_modify_segment_map elf32_arm_symbian_modify_segment_map
20878
20879 /* There is no .got section for BPABI objects, and hence no header.  */
20880 #undef  elf_backend_got_header_size
20881 #define elf_backend_got_header_size 0
20882
20883 /* Similarly, there is no .got.plt section.  */
20884 #undef  elf_backend_want_got_plt
20885 #define elf_backend_want_got_plt 0
20886
20887 #undef  elf_backend_plt_sym_val
20888 #define elf_backend_plt_sym_val         elf32_arm_symbian_plt_sym_val
20889
20890 #undef  elf_backend_may_use_rel_p
20891 #define elf_backend_may_use_rel_p       1
20892 #undef  elf_backend_may_use_rela_p
20893 #define elf_backend_may_use_rela_p      0
20894 #undef  elf_backend_default_use_rela_p
20895 #define elf_backend_default_use_rela_p  0
20896 #undef  elf_backend_want_plt_sym
20897 #define elf_backend_want_plt_sym        0
20898 #undef  elf_backend_dtrel_excludes_plt
20899 #define elf_backend_dtrel_excludes_plt  0
20900 #undef  ELF_MAXPAGESIZE
20901 #define ELF_MAXPAGESIZE                 0x8000
20902
20903 #include "elf32-target.h"